Spaces:
Sleeping
Sleeping
Added submission scripts
Browse filesFiles changed (7)
- .gitignore+1-1
- Dockerfile+8-3
- README.md+111-61
- inference.py+14-10
- openenv.yaml+5
- pyproject.toml+1-1
- start.shAdded+8
2 unmodified lines34567892 unmodified lines*.py[codz]*$py.classwandb/# C extensions*.sodocs/2 unmodified lines34567892 unmodified lines*.py[codz]*$py.classwandb/patch_hawk/# C extensions*.sodocs/
18 unmodified lines192021222324252618 unmodified linesCOPY inference.py .COPY config.yaml .# Expose the OpenEnv server portEXPOSE 7860# Launch the OpenEnv HTTP serverCMD ["openenv", "serve", "--env", "patchhawk.agent.environment:PatchHawkEnv", "--port", "7860"]18 unmodified lines1920212223242526272829303118 unmodified linesCOPY inference.py .COPY config.yaml .# Copy and configure the startup scriptCOPY start.sh .RUN chmod +x start.sh# Expose both the OpenEnv API port and Streamlit portEXPOSE 7860EXPOSE 8501# Launch both serversCMD ["./start.sh"]
3 unmodified lines4567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071083 unmodified lines[](https://huggingface.co/ramprasathk07/patchhawk)[](https://python.org)[](https://github.com/pytorch/openenv)> **PatchHawk is an autonomous DevSecOps agent powered by Group Relative Policy Optimization (GRPO). It doesn't just detect vulnerabilities; it validates them in isolated containers and generates verified patches.**---## π The Approach: Cyber-Physical RL LoopMost security LLMs suffer from "hallucinated security"βthey claim a bug is fixed without ever running the code. PatchHawk solves this by implementing a **Cyber-Physical Reinforcement Learning Loop**:1. **Detection**: The agent analyzes code snippets for supply-chain attacks (typosquatting, backdoors, exfiltration).2. **Simulation**: The agent can choose to "Detonate" suspicious code in a hardened **Docker Sandbox** to observe real syscalls and network behavior.3. **Correction**: If malicious, the agent generates a Python patch.4. **Verification**: The environment automatically runs the patch through a 3-stage validation (Syntax -> Unit Tests -> Re-Attack Detonation) inside Docker.5. **Reward**: The model is rewarded only if the patch **natively passes** all stages.---## π§ Training Style: GRPO (Group Relative Policy Optimization)PatchHawk uses **GRPO**, the same technique used in DeepSeek-R1, to train our security agent via trial and error.- **Trial & Error**: The model is tasked with fixing complex vulnerabilities. It generates multiple attempts (Groups) for the same problem.- **XML Reasoning**: The model is trained to use absolute XML structure:```xml<thought>Analyze the base64 encoded string... it is a reverse shell.</thought><risk_score>0.98</risk_score><action>3</action><patch>import os...</patch>```- **Relative Scoring**: Instead of using a static "Teacher" model, PatchHawk compares the scores of the 4 attempts against each other. It learns that the attempt that passed the **Docker Syntax Check** is superior to the one that didn't.---## π Action Space & Scoring Rubric (0.0 to 1.0 Evaluator)The environment manages a complex reward system to move beyond sparse "win/loss" signals.| Action ID | Action Name | Reward (Base) | Logic || :--- | :--- | :--- | :--- || **0** | **ANALYZE** | `0.0` | "Do nothing/Observe". Optimal for benign code. || **1** | **EXECUTE_SANDBOX** | `+0.1` | Safely detonate payload in Docker and extract telemetry. || **2** | **BLOCK_PR** | `+2.0 / -1.0` | Reject PR. Heavily rewarded for malware, penalized for False Positives. || **3** | **SUBMIT_PATCH** | **+3.0 / -1.5** | **The Goal.** Reward requires a clean run in the Docker Sandbox. || **4** | **REQUEST_REVIEW** | `0.0` | Escalate to a human expert. |### π Dynamic Bonuses* **Risk Accuracy Bonus (+2.0)**: The agent earns a reward of `(1.0 - abs(actual - predicted)) * 2.0`. This ensures it learns to accurately classify risk even if it doesn't take the aggressive patch action.* **Safety Penalty (-1.0)**: Any patch that fails a Docker syntax check or units tests results in a heavy penalty to discourage "lazy packaging".---## π³ Docker Usage & SecurityPatchHawk requires a local Docker daemon. The sandbox is strictly isolated:- **No Network**: Containers run with `--network none`.- **Resource Caps**: Limited to `256MB RAM` and `0.5 CPU` cores.- **Non-Root**: Tasks execute as a limited-privilege user.- **Validation**: The 3-stage pipeline checks:1. `py_compile`: Does the patch even run?2. `pytest`: Does it break existing functionality?3. `Re-Attack`: If we run the original exploit, does the new patch stop it?---## π Installation```bash# 1. Clone & Installgit clone https://github.com/ramprasathk07/PatchHawk.gitcd PatchHawkpip install -r requirements.txt# 2. Setup Environmentcp .env.example .env# Fill in HF_TOKEN for local LLM fallback# 3. Build the Validator Boxdocker build -t patchhawk-sandbox:latest -f docker/Dockerfile.sandbox .# 4. Generate the Training Dataset (1,500 samples)python -m patchhawk.data.generate_scenarios --num-samples 1500```---## π Dashboard & UILaunch the **Security Operations Center (SOC)** to watch the agent work in real-time:```bashstreamlit run patchhawk/app/dashboard.py```Features:- **Terminal Trace**: See the raw thought process (XML/JSON) of the agent.- **Docker Telemetry**: View real-time output from the sandbox validation.- **Reward Signal**: Audit why the agent earned (+/-) rewards for its specific decision.---## π LicenseMIT Β© Ramprasath K & The PatchHawk Team3 unmodified lines4567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571583 unmodified lines[](https://huggingface.co/ramprasathk07/patchhawk)[](https://python.org)[](https://github.com/pytorch/openenv)[](https://opensource.org/licenses/MIT)> **PatchHawk is an state-of-the-art autonomous DevSecOps agent powered by Group Relative Policy Optimization (GRPO). It goes beyond detection by validating vulnerabilities in isolated Docker sandboxes and generating verified, syntax-correct patches.**---## π½οΈ The Vision: Cyber-Physical RL LoopTraditional security scanners often produce high signal-to-noise ratios and "hallucinated" vulnerabilities. PatchHawk bridges this gap by implementing a **Cyber-Physical Reinforcement Learning Loop**, where the model's reward is tied to the actual execution success of its patches in a real environment.```mermaidgraph TDA[Source Code / PR] --> B{PatchHawk Agent}B -->|Analyze| C[Static Analysis]B -->|Test| D[Docker Sandbox]D -->|Detonate| E[Behavioral Telemetry]E --> F[Reward Signal]B -->|Patch| G[Verification Pipeline]G -->|Syntax Check| H{Success?}G -->|Unit Tests| I{Pass?}G -->|Re-Attack| J{Defeated?}H & I & J -->|All Pass| K[Positive Reward +3.0]H | I | J -->|Failure| L[Negative Penalty -1.5]K --> M[Model Update/Optimization]```---## β¨ Key Features- π‘οΈ **Autonomous Detection**: Sophisticated analysis of supply-chain vectors (typosquatting, backdoors, exfiltration).- π³ **Hardened Sandboxing**: High-fidelity Docker isolation with zero-network access and strict resource caps.- π§ **GRPO-Driven Learning**: Uses Group Relative Policy Optimization (DeepSeek-R1 style) for reasoning and trial-and-error mastery.- π§© **XML Reasoning**: Enforces a structured `<thought>...</thought>` chain for transparent decision-making.- π **SOC Dashboard**: Real-time Streamlit interface for auditing agent behavior and reward telemetry.- β **OpenEnv Compliant**: Fully integrated with the [PyTorch OpenEnv](https://github.com/pytorch/openenv) framework.---## π Project StructureThe codebase is organized into modular components for training, inference, and environment simulation.```textPatchHawk/βββ src/envs/patchhawk/ # π¦ Core OpenEnv Submission Packageβ βββ server/ # FastAPI environment serverβ βββ models.py # Type-safe contract definitionsβ βββ client.py # Environment interaction clientβ βββ inference.py # Main agent execution loopβββ patchhawk/ # π§ Logic & Trainingβ βββ data/ # Scenario generation & datasetsβ βββ training/ # GRPO/Unsloth training scriptsβ βββ app/ # Streamlit SOC Dashboardβββ docker/ # π³ Container configurationsβββ config.yaml # Environment & Agent configurationβββ openenv.yaml # OpenEnv metadata```---## π Getting Started### Prerequisites- **Python 3.12+**- **Docker Engine** (running locally)- **Nvidia GPU** (8GB+ VRAM recommended for local training/inference)### 1. Installation```bash# Clone the repositorygit clone https://github.com/ramprasathk07/PatchHawk.gitcd PatchHawk# Create virtual environment and install core dependenciespython -m venv .venvsource .venv/bin/activate # Windows: .venv\Scripts\activatepip install -e .```### 2. Environment Setup```bash# Setup environment variablescp .env.example .env# Edit .env to include your HF_TOKEN and OpenAI/Anthropic keys# Build the validation sandboxdocker build -t patchhawk-sandbox:latest -f docker/Dockerfile.sandbox .```### 3. Running the Agent (Dry Run)```bash# Start the environment serverpython -m server.app --port 8000# Execute the inference looppython src/envs/patchhawk/inference.py --env-url http://localhost:8000```---## π Reward Rubric (Action Space)PatchHawk implements a granular scoring system to guide the agent toward safe and effective decisions.| Action ID | Action Name | Base Reward | Success Criteria || :--- | :--- | :--- | :--- || **0** | `ANALYZE` | `0.0` | Observation step; used for data gathering. || **1** | `DETONATE` | `+0.1` | Successfully extract telemetry from Docker. || **2** | `BLOCK_PR` | `+2.0 / -1.0` | Rewarded for malware; penalized for False Positives. || **3** | `SUBMIT_PATCH` | `+3.0 / -1.5` | **The Goal.** Requires pass in Syntax -> Test -> Re-Attack. || **4** | `ESCALATE` | `0.0` | Hand off to human expert if uncertainty is high. |### Dynamic Scaling- **Risk Accuracy**: Agent receives up to `+2.0` bonus for predicting the exact risk score.- **Safety Multiplier**: Frequent failed syntax checks trigger a decay factor on all rewards.---## π Dashboard & UILaunch the **Security Operations Center (SOC)** to watch the agent reason in real-time.```bashstreamlit run patchhawk/app/dashboard.py```- **Terminal Trace**: Live XML reasoning logs.- **Docker Monitor**: Real-time stdout/stderr from the sandbox.- **Reward Audit**: Detailed breakdown of why specific points were awarded.---## πΊοΈ Roadmap- [ ] **Multi-Agent Coordination**: Deploying "Attacker" vs "Defender" models for automated red-teaming.- [ ] **CVE Ingestion**: Automated generation of training scenarios from current NVD databases.- [ ] **Cross-Language Support**: Expanding beyond Python to Go, Javascript, and Rust.- [ ] **Kubernetes Native**: Orchestrating sandboxes at scale using K8s instead of local Docker.---## π LicenseDistributed under the **MIT License**. See `LICENSE` or the project root for more information.Developed with β€οΈ by **Ramprasath K & The PatchHawk Team**Ramprasath K & The PatchHawk Team
34 unmodified lines3536373839404142434445464748146 unmodified lines19519619719819920020158 unmodified lines26026126226326426526631 unmodified lines2982993003013023033043053063071 unmodified line30931031131231331434 unmodified linesexcept ImportError:passAPI_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/hf-inference/v1")# Prefer explicit MODEL_NAME, fallback to GRPO_POLICY_MODEL from .env, then default to 32B model.MODEL_NAME = os.getenv("MODEL_NAME", os.getenv("GRPO_POLICY_MODEL", "Qwen/Qwen2.5-Coder-32B-Instruct"))HF_TOKEN = os.getenv("HF_TOKEN", "")DRY_RUN = os.getenv("DRY_RUN", "0") == "1"SINGLE_TASK = os.getenv("TASK", "")TASK_DEFS = [{146 unmodified lines)return response.choices[0].message.content or ""except Exception as e:print(f"[LLM ERROR] Remote API failed: {e}. Initiating local Fallback...", flush=True)return _call_llm_local(messages)58 unmodified lines"""Run one episode and return summary dict."""obs = env.reset(task_id=task_id)print(f"[START] task={task_id} env=PatchHawk model={MODEL_NAME}")trajectory: List[Tuple[PatchHawkAction, PatchHawkObservation]] = []rewards: List[PatchHawkReward] = []31 unmodified linesaction_name = PatchHawkEnv.ACTION_NAMES[action.action_type]_done = str(obs.done).lower()_err = "null" if error is None else errorprint(f"[STEP] step={step_num} action={action_name} "f"reward={step_reward.value:.2f} done={_done} error={_err}",flush=True,)error = None # reset for next step1 unmodified line# ββ Grade ββββββββββββββββββββββββββββββββββββββββββββββββββββscore = grader_fn(env, trajectory)rewards_str = ",".join(f"{r.value:.2f}" for r in rewards)success = score >= 1.0print(34 unmodified lines35363738394041424344454647146 unmodified lines19419519619719819920058 unmodified lines25926026126226326426531 unmodified lines2972982993003013023033043053063073081 unmodified line31031131231331431531631731834 unmodified linesexcept ImportError:passAPI_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/hf-inference/v1")MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-Coder-32B-Instruct")HF_TOKEN = os.getenv("HF_TOKEN", "")LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME", "patch_hawkv1:latest")DRY_RUN = os.getenv("DRY_RUN", "0") == "1"SINGLE_TASK = os.getenv("TASK", "")BENCHMARK = os.getenv("BENCHMARK", "PatchHawk")TASK_DEFS = [{146 unmodified lines)return response.choices[0].message.content or ""except Exception as e:print(f"[LLM ERROR] Remote API failed: {e}. Initiating local Fallback...", file=sys.stderr, flush=True)return _call_llm_local(messages)58 unmodified lines"""Run one episode and return summary dict."""obs = env.reset(task_id=task_id)print(f"[START] task={task_id} env={BENCHMARK} model={MODEL_NAME}", flush=True)trajectory: List[Tuple[PatchHawkAction, PatchHawkObservation]] = []rewards: List[PatchHawkReward] = []31 unmodified linesaction_name = PatchHawkEnv.ACTION_NAMES[action.action_type]_done = str(obs.done).lower()# Sanitize error and action to ensure single-line stdout compliance_err = "null" if error is None else str(error).replace("\n", " ")_act = str(action_name).replace("\n", " ")print(f"[STEP] step={step_num} action={_act} reward={step_reward.value:.2f} done={_done} error={_err}",flush=True,)error = None # reset for next step1 unmodified line# ββ Grade ββββββββββββββββββββββββββββββββββββββββββββββββββββscore = grader_fn(env, trajectory)# Ensure score is in [0, 1]score = min(max(float(score), 0.0), 1.0)rewards_str = ",".join(f"{r.value:.2f}" for r in rewards)success = score >= 1.0print(
12345name: PatchHawkversion: 1.0.0description: Detect and patch supply-chain vulnerabilities in Python code.tags: [security, supply-chain, code-review, llm-agent]tasks:12345678910name: PatchHawkversion: 1.0.0spec_version: 1type: spaceruntime: fastapiapp: server.app:appport: 7860description: Detect and patch supply-chain vulnerabilities in Python code.tags: [security, supply-chain, code-review, llm-agent]tasks:
43 unmodified lines4445464743 unmodified linesserver = "server.app:main"[tool.setuptools.packages.find]include = ["patchhawk*", "server*"]43 unmodified lines4445464743 unmodified linesserver = "server.app:main"[tool.setuptools.packages.find]include = ["patchhawk*", "server*", "src*"]
12345678#!/bin/bash# Start the OpenEnv API server (Hackathon Compliance)echo "Starting OpenEnv API server on port 7860..."uvicorn server.app:app --host 0.0.0.0 --port 7860 &
# Start the Streamlit Dashboard (User UI)echo "Starting Streamlit Dashboard on port 8501..."streamlit run patchhawk/app/dashboard.py --server.port 8501 --server.address 0.0.0.0.gitignore+1-1
Dockerfile+8-3
README.md+111-61
inference.py+14-10
openenv.yaml+5
pyproject.toml+1-1
start.shAdded+8