Spaces:
Running
Running
Commit ·
4ec75cf
0
Parent(s):
first commit
Browse files- .gitignore +16 -0
- Dockerfile +26 -0
- README.md +379 -0
- inference.py +341 -0
- openenv.yaml +59 -0
- pyproject.toml +19 -0
- server/__init__.py +1 -0
- server/app.py +631 -0
- server/benchmark_store.py +69 -0
- server/datasets/__init__.py +1 -0
- server/datasets/clinical_cases.py +180 -0
- server/datasets/dependency_cases.py +280 -0
- server/datasets/security_cases.py +211 -0
- server/debug_panel.html +1196 -0
- server/demo_agent.py +140 -0
- server/graders/__init__.py +1 -0
- server/graders/base_grader.py +79 -0
- server/graders/clinical_grader.py +154 -0
- server/graders/dependency_grader.py +243 -0
- server/graders/security_grader.py +99 -0
- server/models/__init__.py +1 -0
- server/models/clinical_models.py +19 -0
- server/models/dependency_models.py +22 -0
- server/models/security_models.py +23 -0
- server/router.py +322 -0
- server/session.py +41 -0
- server/validation/__init__.py +1 -0
- server/validation/validator.py +282 -0
- server/web_ui.py +365 -0
- tests/test_endpoints.py +131 -0
- tests/test_grader_variance.py +138 -0
- validate-submission.sh +70 -0
.gitignore
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ── Never commit these ──
|
| 2 |
+
.env
|
| 3 |
+
venv/
|
| 4 |
+
__pycache__/
|
| 5 |
+
*.pyc
|
| 6 |
+
*.egg-info/
|
| 7 |
+
multi_agent_dev_tools_env.egg-info/
|
| 8 |
+
dist/
|
| 9 |
+
build/
|
| 10 |
+
test_output.txt
|
| 11 |
+
|
| 12 |
+
# ── Internal / personal files ──
|
| 13 |
+
unnecessary/
|
| 14 |
+
results/
|
| 15 |
+
.pytest_cache/
|
| 16 |
+
uv.lock
|
Dockerfile
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
# ── OpenEnv labels (required for HF Space tagging) ──
|
| 4 |
+
LABEL org.opencontainers.image.title="multi-agent-dev-tools-env"
|
| 5 |
+
LABEL org.opencontainers.image.description="Multi-Agent Dev Tools RL Environment"
|
| 6 |
+
LABEL openenv="true"
|
| 7 |
+
|
| 8 |
+
WORKDIR /app
|
| 9 |
+
|
| 10 |
+
# Install dependencies
|
| 11 |
+
COPY pyproject.toml .
|
| 12 |
+
RUN pip install --no-cache-dir . 2>/dev/null || pip install --no-cache-dir \
|
| 13 |
+
fastapi uvicorn pydantic openai requests packaging gradio python-dotenv
|
| 14 |
+
|
| 15 |
+
# Copy project files
|
| 16 |
+
COPY . .
|
| 17 |
+
|
| 18 |
+
# Expose port 7860 (HuggingFace Spaces standard)
|
| 19 |
+
EXPOSE 7860
|
| 20 |
+
|
| 21 |
+
# Health check for HF Spaces
|
| 22 |
+
HEALTHCHECK --interval=30s --timeout=10s --retries=3 \
|
| 23 |
+
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:7860/')" || exit 1
|
| 24 |
+
|
| 25 |
+
# Start the server
|
| 26 |
+
CMD ["uvicorn", "server.app:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
| 1 |
+
# 🛠️ Multi-Agent Dev Tools Environment
|
| 2 |
+
|
| 3 |
+
> A multi-domain RL environment for training and evaluating AI agents on **real-world developer and clinical tasks**.
|
| 4 |
+
> Built for the **Scaler × Meta × PyTorch × Hugging Face OpenEnv Hackathon 2026**.
|
| 5 |
+
|
| 6 |
+
---
|
| 7 |
+
|
| 8 |
+
## 💡 Why This Environment?
|
| 9 |
+
|
| 10 |
+
Most existing RL benchmarks test agents on **static, single-turn tasks** — classify this image, answer this question. But real developer workflows are **multi-turn, iterative, and require revision**:
|
| 11 |
+
|
| 12 |
+
- A security reviewer doesn't just find a bug — they **identify → propose a fix → revise after feedback**
|
| 13 |
+
- A DevOps engineer doesn't just flag outdated packages — they **resolve version conflicts across an entire dependency graph**
|
| 14 |
+
- A clinical coordinator doesn't just spot missing steps — they **prioritize by urgency and plan a dependency-safe recovery**
|
| 15 |
+
|
| 16 |
+
**No existing RL environment tests agents on this full identify → act → revise cycle.** This environment fills that gap by providing 9 tasks across 3 real-world domains with progressive difficulty, rich partial-credit scoring, and iterative multi-turn episodes.
|
| 17 |
+
|
| 18 |
+
**Who would use this?** Teams training AI coding assistants (code review bots), dependency management agents (Dependabot-like systems), and clinical decision support systems.
|
| 19 |
+
|
| 20 |
+
---
|
| 21 |
+
|
| 22 |
+
## 🎯 What Is This?
|
| 23 |
+
|
| 24 |
+
This is a **training gym for AI agents** — not the agent itself.
|
| 25 |
+
Think of it like a driving test course: you build the course, and different AI "drivers" take the test.
|
| 26 |
+
|
| 27 |
+
An AI agent connects to this environment via API, receives a **task** (e.g., "find the vulnerability in this code"), sends back an **action** (its answer), and gets a **reward score** (0.0 – 1.0) based on how good the answer is.
|
| 28 |
+
|
| 29 |
+
```
|
| 30 |
+
POST /reset
|
| 31 |
+
AI Agent ────────────────────────► This Environment
|
| 32 |
+
│
|
| 33 |
+
├── Picks a task case
|
| 34 |
+
├── Returns: observation (the problem)
|
| 35 |
+
◄──────────────────────── │
|
| 36 |
+
│
|
| 37 |
+
POST /step │
|
| 38 |
+
────────────────────────► │
|
| 39 |
+
├── Validates the action (3 stages)
|
| 40 |
+
├── Grades it (domain-specific grader)
|
| 41 |
+
◄──────────────────────── ├── Returns: reward + done + next observation
|
| 42 |
+
│
|
| 43 |
+
(repeat until done) │
|
| 44 |
+
```
|
| 45 |
+
|
| 46 |
+
---
|
| 47 |
+
|
| 48 |
+
## 🏗️ Three Domains, Nine Tasks
|
| 49 |
+
|
| 50 |
+
### 🔒 Domain 1: MCP Security Auditing
|
| 51 |
+
|
| 52 |
+
Agents must identify vulnerabilities in code snippets, propose fixes, and iteratively revise based on reviewer feedback.
|
| 53 |
+
|
| 54 |
+
| Task | Difficulty | Subtype | Max Steps | Threshold | Actions |
|
| 55 |
+
|------|-----------|---------|-----------|-----------|---------|
|
| 56 |
+
| `sec_easy` | Easy | `single` | 4 | 0.80 | `identify_vulnerability` |
|
| 57 |
+
| `sec_medium` | Medium | `multi` | 6 | 0.75 | `identify` → `propose_fix` → `revise_fix` |
|
| 58 |
+
| `sec_hard` | Hard | `adversarial` | 8 | 0.70 | `identify` → `propose_fix` → `revise_fix` (reviewer) |
|
| 59 |
+
|
| 60 |
+
**Dataset:** 10 ground-truth cases covering SQL injection, XSS, IDOR, hardcoded secrets, missing auth, JWT misuse, path traversal, SSRF.
|
| 61 |
+
|
| 62 |
+
### 📦 Domain 2: PyTorch Migration Time-Machine
|
| 63 |
+
|
| 64 |
+
Agents must detect deprecated APIs, resolve version conflicts, and fix `torch.compile` graph-break patterns.
|
| 65 |
+
|
| 66 |
+
| Task | Difficulty | Subtype | Max Steps | Threshold | Actions |
|
| 67 |
+
|------|-----------|---------|-----------|-----------|---------|
|
| 68 |
+
| `dep_easy` | Easy | `flag` | 4 | 0.80 | `flag_outdated` |
|
| 69 |
+
| `dep_medium` | Medium | `resolve` | 6 | 0.75 | `resolve_conflict` |
|
| 70 |
+
| `dep_hard` | Hard | `migrate` | 8 | 0.70 | `migrate_api` / `validate_tree` |
|
| 71 |
+
|
| 72 |
+
**Dataset:** 10 ground-truth cases covering Variable, cuda(), DataParallel, ONNX export, torch.compile graph-breaks.
|
| 73 |
+
|
| 74 |
+
### 🏥 Domain 3: Clinical Workflow Chaos Simulator
|
| 75 |
+
|
| 76 |
+
Agents must detect missing steps in hospital workflows, rank them by priority, and plan dependency-ordered recovery sequences.
|
| 77 |
+
|
| 78 |
+
| Task | Difficulty | Max Steps | Threshold | Actions |
|
| 79 |
+
|------|-----------|-----------|-----------|---------|
|
| 80 |
+
| `cli_easy` | Easy | 4 | 0.80 | `detect_gap` |
|
| 81 |
+
| `cli_medium` | Medium | 6 | 0.75 | `detect_gap` → `rank_issues` |
|
| 82 |
+
| `cli_hard` | Hard | 6 | 0.70 | `detect_gap` → `rank_issues` → `order_steps` |
|
| 83 |
+
|
| 84 |
+
**Dataset:** 10 ground-truth cases covering surgery prep, ER triage, chemotherapy, cardiac emergency, blood transfusion.
|
| 85 |
+
|
| 86 |
+
---
|
| 87 |
+
|
| 88 |
+
## 📊 Observation & Action Spaces
|
| 89 |
+
|
| 90 |
+
### Observation Space
|
| 91 |
+
|
| 92 |
+
Every observation includes these core fields:
|
| 93 |
+
|
| 94 |
+
| Field | Type | Description |
|
| 95 |
+
|-------|------|-------------|
|
| 96 |
+
| `task_type` | `str` | Domain: `security`, `dependency`, or `clinical` |
|
| 97 |
+
| `task_id` | `str` | Task identifier (e.g., `sec_easy`) |
|
| 98 |
+
| `task_subtype` | `str` | Variant: `single`, `multi`, `flag`, `resolve`, `migrate` |
|
| 99 |
+
| `task_description` | `str` | Human-readable problem description |
|
| 100 |
+
| `available_actions` | `list[dict]` | Valid actions with parameter specs |
|
| 101 |
+
| `turn` | `int` | Current step number |
|
| 102 |
+
| `done` | `bool` | Whether episode has ended |
|
| 103 |
+
|
| 104 |
+
Domain-specific fields are added (e.g., `code_snippet` for security, `compatibility_matrix` for dependency, `events` and `dependency_graph` for clinical).
|
| 105 |
+
|
| 106 |
+
### Action Space
|
| 107 |
+
|
| 108 |
+
Actions are JSON objects with `action_type` and domain-specific parameters:
|
| 109 |
+
|
| 110 |
+
```json
|
| 111 |
+
{"action_type": "identify_vulnerability", "vuln_type": "sql_injection", "cvss_score": 8.5, "severity": "critical", "affected_line": 3}
|
| 112 |
+
{"action_type": "propose_fix", "fix_code": "db.execute(query, (param,))", "explanation": "Use parameterized queries"}
|
| 113 |
+
{"action_type": "flag_outdated", "packages": {"torch": "1.9.0"}, "deprecated_api": "torch.autograd.Variable", "replacement": "plain tensor"}
|
| 114 |
+
{"action_type": "detect_gap", "missing_steps": ["pre_op_consent"], "risk_level": "critical"}
|
| 115 |
+
```
|
| 116 |
+
|
| 117 |
+
---
|
| 118 |
+
|
| 119 |
+
## 📊 Scoring System
|
| 120 |
+
|
| 121 |
+
### Two-Layer Grading Architecture
|
| 122 |
+
|
| 123 |
+
**Layer 1: `base_grader.py`** — Universal reward pipeline applied to ALL domains:
|
| 124 |
+
|
| 125 |
+
```
|
| 126 |
+
reward = safe_score(correctness + repetition_penalty + harmful_penalty + efficiency_bonus)
|
| 127 |
+
```
|
| 128 |
+
|
| 129 |
+
| Component | Formula | Range |
|
| 130 |
+
|-----------|---------|-------|
|
| 131 |
+
| `compute_correctness()` | Domain-specific (see below) | 0.0 – 1.0 |
|
| 132 |
+
| `repetition_penalty` | −0.15 × count(same action in last 3 turns) | −0.45 – 0.0 |
|
| 133 |
+
| `harmful_output_penalty` | −0.30 if forbidden pattern detected | −0.30 – 0.0 |
|
| 134 |
+
| `efficiency_bonus` | +0.10 if `correctness >= 0.8` and early finish | 0.0 – 0.10 |
|
| 135 |
+
| `safe_score()` | `clamp(score, 0.0, 1.0)` | 0.0 – 1.0 |
|
| 136 |
+
|
| 137 |
+
**Layer 2: Domain-specific graders:**
|
| 138 |
+
|
| 139 |
+
#### Security Grader
|
| 140 |
+
| Action | Component | Weight |
|
| 141 |
+
|--------|-----------|--------|
|
| 142 |
+
| `identify_vulnerability` | vuln_type match | ×0.45 |
|
| 143 |
+
| `identify_vulnerability` | CVSS in range (partial: ±3.0) | ×0.30 |
|
| 144 |
+
| `identify_vulnerability` | severity match (adjacent: ×0.40) | ×0.25 |
|
| 145 |
+
| `propose_fix` | token coverage + identifier preserved (floor: 0.25) | up to 1.15 |
|
| 146 |
+
| `revise_fix` | feedback keyword coverage − regression (floor: 0.20) | 0.0 – 1.0 |
|
| 147 |
+
|
| 148 |
+
#### Dependency Grader
|
| 149 |
+
| Action | Formula |
|
| 150 |
+
|--------|---------|
|
| 151 |
+
| `flag_outdated` | F1 × 0.55 + deprecated_api_match × 0.45 |
|
| 152 |
+
| `resolve_conflict` | valid_pkgs / conflict_count + tree_bonus(0.15) − downgrade(0.10) |
|
| 153 |
+
| `migrate_api` | order_score × 0.30 + completeness × 0.40 + fix_quality × 0.30 |
|
| 154 |
+
|
| 155 |
+
#### Clinical Grader
|
| 156 |
+
| Action | Formula |
|
| 157 |
+
|--------|---------|
|
| 158 |
+
| `detect_gap` | F1(predicted, expected) × 0.65 + risk_match × 0.35 |
|
| 159 |
+
| `rank_issues` | completeness × 0.40 + NDCG@k × 0.60 |
|
| 160 |
+
| `order_steps` | order_violations × 0.40 + completeness × 0.40 + efficiency × 0.20 |
|
| 161 |
+
|
| 162 |
+
### GRPO Training Signal Quality
|
| 163 |
+
|
| 164 |
+
This environment is specifically designed for **Group Relative Policy Optimization**:
|
| 165 |
+
|
| 166 |
+
- **Smooth reward ramp** — Scores transition smoothly from 0.0 → 1.0, never binary
|
| 167 |
+
- **Partial credit everywhere** — F1 scoring, NDCG ranking, adjacent-severity credit
|
| 168 |
+
- **Progressive penalty learning** — Schema penalty (−0.20), repetition (−0.15), harmful (−0.30)
|
| 169 |
+
- **Efficiency bonus** — Agents learn to solve faster by finishing early
|
| 170 |
+
- **Floor scores** — Valid workflow attempts always get minimum credit (0.20–0.25)
|
| 171 |
+
|
| 172 |
+
---
|
| 173 |
+
|
| 174 |
+
## 🔐 Validation (3 Stages)
|
| 175 |
+
|
| 176 |
+
Every action goes through 3-stage validation before reaching the grader:
|
| 177 |
+
|
| 178 |
+
1. **Schema** — Required fields present? Correct types? (Auto-casts `"8.5"` → `8.5`)
|
| 179 |
+
2. **Domain** — Is `vuln_type` in the valid set? Is `cvss_score` in [0, 10]?
|
| 180 |
+
3. **Consistency** — Is `revise_fix` called after `propose_fix`? No identical repeats?
|
| 181 |
+
|
| 182 |
+
If validation fails, the agent gets a **rich feedback observation** (not just 0.0):
|
| 183 |
+
```json
|
| 184 |
+
{
|
| 185 |
+
"validation_failed": true,
|
| 186 |
+
"error_type": "domain_error",
|
| 187 |
+
"message": "cvss_score 12.5 out of range",
|
| 188 |
+
"hint": "cvss_score must be a float between 0.0 and 10.0",
|
| 189 |
+
"available_actions": ["identify_vulnerability", "propose_fix", "revise_fix"]
|
| 190 |
+
}
|
| 191 |
+
```
|
| 192 |
+
|
| 193 |
+
---
|
| 194 |
+
|
| 195 |
+
## 🏛️ Architecture
|
| 196 |
+
|
| 197 |
+
```
|
| 198 |
+
project-root/
|
| 199 |
+
├── inference.py # Baseline agent (OpenAI-compatible, spec-compliant logs)
|
| 200 |
+
├── openenv.yaml # OpenEnv manifest (9 tasks declared)
|
| 201 |
+
├── pyproject.toml # Python package config with openenv-core dependency
|
| 202 |
+
├── Dockerfile # Docker build for HF Spaces (port 7860)
|
| 203 |
+
├── server/
|
| 204 |
+
│ ├── app.py # FastAPI endpoints: /, /reset, /step, /state, /debug
|
| 205 |
+
│ ├── router.py # Central dispatcher: observations, done conditions, score_details
|
| 206 |
+
│ ├── session.py # In-memory session state management
|
| 207 |
+
│ ├── benchmark_store.py # Persistent JSON results store (survives restarts)
|
| 208 |
+
│ ├── demo_agent.py # Rule-based demo agent for Gradio UI
|
| 209 |
+
│ ├── web_ui.py # Gradio UI with task runner and history
|
| 210 |
+
│ ├── debug_panel.html # Interactive HTML debug panel
|
| 211 |
+
│ ├── validation/
|
| 212 |
+
│ │ └── validator.py # 3-stage validation: Schema → Domain → Consistency
|
| 213 |
+
│ ├── graders/
|
| 214 |
+
│ │ ├── base_grader.py # safe_score, grade_dynamic, penalties, bonuses
|
| 215 |
+
│ │ ├── security_grader.py # Vuln detection, fix quality, feedback coverage
|
| 216 |
+
│ │ ├── dependency_grader.py # F1 scoring, version checking, graph ordering
|
| 217 |
+
│ │ └── clinical_grader.py # F1, NDCG ranking, dependency-violation counting
|
| 218 |
+
│ └── datasets/
|
| 219 |
+
│ ├── security_cases.py # 10 cases: SQL injection, XSS, IDOR, SSRF, etc.
|
| 220 |
+
│ ├── dependency_cases.py # 10 cases: Variable, cuda(), DataParallel, graph-breaks
|
| 221 |
+
│ └── clinical_cases.py # 10 cases: surgery prep, ER triage, chemo, cardiac
|
| 222 |
+
└── results/
|
| 223 |
+
└── run_history.json # Persistent benchmark results (auto-created)
|
| 224 |
+
```
|
| 225 |
+
|
| 226 |
+
---
|
| 227 |
+
|
| 228 |
+
## 📡 API Endpoints
|
| 229 |
+
|
| 230 |
+
| Method | Path | Description |
|
| 231 |
+
|--------|------|-------------|
|
| 232 |
+
| `GET /` | Health check | Returns status, task list, spec version |
|
| 233 |
+
| `POST /reset` | Start episode | `{"task_id": "sec_easy"}` → `{episode_id, observation}` |
|
| 234 |
+
| `POST /step` | Submit action | `{episode_id, action_type, ...}` → `{reward, done, observation}` |
|
| 235 |
+
| `GET /state` | Query state | `?episode_id=xxx` → `{step_count, done, reward_acc}` |
|
| 236 |
+
| `GET /debug` | Debug panel | Interactive HTML benchmark runner |
|
| 237 |
+
| `GET /web` | Gradio UI | Full task browser with run history |
|
| 238 |
+
|
| 239 |
+
### Step Response Format
|
| 240 |
+
|
| 241 |
+
```json
|
| 242 |
+
{
|
| 243 |
+
"episode_id": "uuid-string",
|
| 244 |
+
"step_count": 2,
|
| 245 |
+
"reward": 0.75,
|
| 246 |
+
"done": false,
|
| 247 |
+
"observation": {
|
| 248 |
+
"task_type": "security",
|
| 249 |
+
"task_id": "sec_easy",
|
| 250 |
+
"task_subtype": "single",
|
| 251 |
+
"task_description": "Identify the SQL injection vulnerability...",
|
| 252 |
+
"turn": 1,
|
| 253 |
+
"done": false,
|
| 254 |
+
"available_actions": [...]
|
| 255 |
+
},
|
| 256 |
+
"score_details": {
|
| 257 |
+
"vuln_type_match": 1.0,
|
| 258 |
+
"cvss_in_range": 1.0,
|
| 259 |
+
"severity_match": 0.0
|
| 260 |
+
}
|
| 261 |
+
}
|
| 262 |
+
```
|
| 263 |
+
|
| 264 |
+
---
|
| 265 |
+
|
| 266 |
+
## 🚀 Setup & Running
|
| 267 |
+
|
| 268 |
+
### Prerequisites
|
| 269 |
+
- Python 3.10+
|
| 270 |
+
- `pip install fastapi uvicorn openai requests packaging gradio python-dotenv`
|
| 271 |
+
|
| 272 |
+
### Running Locally
|
| 273 |
+
|
| 274 |
+
```bash
|
| 275 |
+
# 1. Start the environment server
|
| 276 |
+
cd multi-agent-dev-tools-env
|
| 277 |
+
uvicorn server.app:app --host 0.0.0.0 --port 7860
|
| 278 |
+
|
| 279 |
+
# 2. Run baseline inference (in another terminal)
|
| 280 |
+
export API_BASE_URL="https://router.huggingface.co/v1"
|
| 281 |
+
export MODEL_NAME="Qwen/Qwen2.5-72B-Instruct"
|
| 282 |
+
export HF_TOKEN="your_token_here"
|
| 283 |
+
export ENV_URL="http://localhost:7860"
|
| 284 |
+
python inference.py
|
| 285 |
+
```
|
| 286 |
+
|
| 287 |
+
### Docker
|
| 288 |
+
|
| 289 |
+
```bash
|
| 290 |
+
docker build -t multi-agent-dev-tools-env .
|
| 291 |
+
docker run -p 7860:7860 multi-agent-dev-tools-env
|
| 292 |
+
```
|
| 293 |
+
|
| 294 |
+
### Deploy to Hugging Face Spaces
|
| 295 |
+
|
| 296 |
+
```bash
|
| 297 |
+
huggingface-cli login
|
| 298 |
+
openenv push --repo-id <username>/multi-agent-dev-tools-env
|
| 299 |
+
```
|
| 300 |
+
|
| 301 |
+
---
|
| 302 |
+
|
| 303 |
+
## 📝 Mandatory Log Format
|
| 304 |
+
|
| 305 |
+
The `inference.py` emits structured stdout logs matching the spec exactly:
|
| 306 |
+
|
| 307 |
+
```
|
| 308 |
+
[START] task=sec_easy env=multi-agent-dev-tools-env model=Qwen/Qwen2.5-72B-Instruct
|
| 309 |
+
[STEP] step=1 action=identify_vulnerability reward=0.85 done=false error=null
|
| 310 |
+
[STEP] step=2 action=propose_fix reward=1.00 done=true error=null
|
| 311 |
+
[END] success=true steps=2 score=1.00 rewards=0.85,1.00
|
| 312 |
+
```
|
| 313 |
+
|
| 314 |
+
### Environment Variables (Required)
|
| 315 |
+
|
| 316 |
+
| Variable | Description | Example |
|
| 317 |
+
|----------|-------------|---------|
|
| 318 |
+
| `API_BASE_URL` | LLM API endpoint | `https://router.huggingface.co/v1` |
|
| 319 |
+
| `MODEL_NAME` | Model identifier | `Qwen/Qwen2.5-72B-Instruct` |
|
| 320 |
+
| `HF_TOKEN` | API key / HF token | `hf_xxxxx` or `gsk_xxxxx` |
|
| 321 |
+
| `ENV_URL` | Environment URL | `http://localhost:7860` |
|
| 322 |
+
|
| 323 |
+
---
|
| 324 |
+
|
| 325 |
+
## 📈 Baseline Scores
|
| 326 |
+
|
| 327 |
+
Tested with multiple model families for universal compatibility:
|
| 328 |
+
|
| 329 |
+
| Model | Family | Parameters | Average Score |
|
| 330 |
+
|-------|--------|------------|---------------|
|
| 331 |
+
| Llama 3.3 70B | Meta | 70B | **0.97** |
|
| 332 |
+
| Qwen3-32B | Alibaba | 32B | **0.99** |
|
| 333 |
+
| DeepSeek V3.2 | DeepSeek | MoE | **0.96** |
|
| 334 |
+
|
| 335 |
+
The environment provides smooth reward gradients that enable GRPO training of smaller models (8B+).
|
| 336 |
+
|
| 337 |
+
---
|
| 338 |
+
|
| 339 |
+
## 🔧 Key Design Decisions
|
| 340 |
+
|
| 341 |
+
1. **Data-driven done conditions** — `completion_threshold` and `required_sequence` stored per case
|
| 342 |
+
2. **Universal model compatibility** — Strips `<think>`, `<reasoning>`, `<antThinking>` etc.
|
| 343 |
+
3. **Type-casting validator** — Auto-converts `"8.5"` → `8.5` before rejecting
|
| 344 |
+
4. **Floor scores** — Valid workflow attempts always get minimum credit
|
| 345 |
+
5. **Deterministic case selection** — `hash(episode_id) % len(cases)` for reproducibility
|
| 346 |
+
6. **Compatibility matrix separation** — Prevents context truncation for large observations
|
| 347 |
+
7. **Patch-level version fuzzy** — `2.1.1` matches `2.1.0` by major.minor
|
| 348 |
+
8. **Hallucination filter** — `_score_rank` filters step IDs not in `available_steps`
|
| 349 |
+
9. **Persistent results** — `benchmark_store.py` writes to disk, survives restarts
|
| 350 |
+
10. **Robust dependency fallback** — Works without `packaging` module via manual version parsing
|
| 351 |
+
|
| 352 |
+
---
|
| 353 |
+
|
| 354 |
+
## ☑️ Compliance Checklist
|
| 355 |
+
|
| 356 |
+
### Phase 1: Automated Validation (Pass/Fail)
|
| 357 |
+
- [x] HF Space deploys and responds to `GET /`
|
| 358 |
+
- [x] `openenv.yaml` present with all 9 task IDs
|
| 359 |
+
- [x] `POST /reset` returns `episode_id` + `observation` for all 9 tasks
|
| 360 |
+
- [x] `POST /step` returns `reward` (float, 0.0–1.0) + `done` (bool) + `observation`
|
| 361 |
+
- [x] `GET /state` returns episode state
|
| 362 |
+
- [x] All endpoints return HTTP 200 (never 500)
|
| 363 |
+
- [x] `Dockerfile` at project root, builds cleanly
|
| 364 |
+
- [x] `inference.py` at project root, runs under 20 min
|
| 365 |
+
- [x] `openenv validate` passes
|
| 366 |
+
|
| 367 |
+
### Phase 2: Agentic Evaluation (Scored)
|
| 368 |
+
- [x] Observations include `task_type`, `task_subtype`, `task_description`, `available_actions`
|
| 369 |
+
- [x] Partial credit graders (F1, NDCG, weighted sub-scores) — not binary
|
| 370 |
+
- [x] Score variance across 9 tasks (varied difficulty = varied scores)
|
| 371 |
+
- [x] `score_details` in step response for grading transparency
|
| 372 |
+
- [x] `safe_score()` clamps all rewards to [0.0, 1.0]
|
| 373 |
+
|
| 374 |
+
### Phase 3: Human Review
|
| 375 |
+
- [x] 3 real-world domains (security, dependency, clinical)
|
| 376 |
+
- [x] Multi-turn iterative workflows (identify → fix → revise)
|
| 377 |
+
- [x] Rich validation hints for agent learning
|
| 378 |
+
- [x] Debug panel with benchmark runner UI
|
| 379 |
+
- [x] GRPO-compatible reward shaping
|
inference.py
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# inference.py <-- MUST be at project root
|
| 2 |
+
# Mandatory baseline inference script for OpenEnv hackathon.
|
| 3 |
+
# Uses OpenAI-compatible client for HuggingFace Inference API.
|
| 4 |
+
#
|
| 5 |
+
# STDOUT FORMAT (mandatory — any deviation causes scoring failure):
|
| 6 |
+
# [START] task=<task_name> env=<benchmark> model=<model_name>
|
| 7 |
+
# [STEP] step=<n> action=<action_str> reward=<0.00> done=<true|false> error=<msg|null>
|
| 8 |
+
# [END] success=<true|false> steps=<n> score=<0.00> rewards=<r1,r2,...,rn>
|
| 9 |
+
#
|
| 10 |
+
# Universal model compatibility:
|
| 11 |
+
# Strips <think>, <thinking>, <reasoning>, <reflection>, <thought>, <antThinking>
|
| 12 |
+
# Handles unclosed thinking tags, markdown fences, prose before/after JSON
|
| 13 |
+
# Type coercion for string→float, string→list, etc.
|
| 14 |
+
|
| 15 |
+
import os
|
| 16 |
+
import re
|
| 17 |
+
import json
|
| 18 |
+
import textwrap
|
| 19 |
+
import requests
|
| 20 |
+
from openai import OpenAI
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
from dotenv import load_dotenv
|
| 24 |
+
load_dotenv()
|
| 25 |
+
except ImportError:
|
| 26 |
+
pass
|
| 27 |
+
|
| 28 |
+
# ── Mandatory environment variables (spec-required names) ──
|
| 29 |
+
API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
|
| 30 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
|
| 31 |
+
HF_TOKEN = os.getenv("HF_TOKEN") or os.getenv("API_KEY") or ""
|
| 32 |
+
ENV_URL = os.getenv("ENV_URL", "http://localhost:7860")
|
| 33 |
+
|
| 34 |
+
MAX_STEPS = 8
|
| 35 |
+
TEMPERATURE = 0.1
|
| 36 |
+
MAX_TOKENS = 400
|
| 37 |
+
BENCHMARK = "multi-agent-dev-tools-env"
|
| 38 |
+
|
| 39 |
+
TASKS = [
|
| 40 |
+
"sec_easy", "sec_medium", "sec_hard",
|
| 41 |
+
"dep_easy", "dep_medium", "dep_hard",
|
| 42 |
+
"cli_easy", "cli_medium", "cli_hard",
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
# ── Generic System Prompt (works for ALL LLMs) ──
|
| 46 |
+
SYSTEM_PROMPT = textwrap.dedent("""\
|
| 47 |
+
You are an autonomous multi-domain analyst agent inside an RL environment.
|
| 48 |
+
|
| 49 |
+
YOUR JOB:
|
| 50 |
+
1. Read the observation — it contains task_type, task_subtype, task_description,
|
| 51 |
+
available_actions (with parameter specs), and domain-specific data.
|
| 52 |
+
2. Choose the correct action from available_actions.
|
| 53 |
+
3. Respond with ONLY a valid JSON object. No markdown fences. No prose. No thinking tags.
|
| 54 |
+
|
| 55 |
+
DOMAIN RULES:
|
| 56 |
+
- security: Workflow is ALWAYS: identify_vulnerability → propose_fix → revise_fix (if feedback)
|
| 57 |
+
vuln_type MUST be one of: sql_injection|xss|idor|hardcoded_secret|missing_auth|jwt_misuse|path_traversal|ssrf|rate_limit_missing|xxe
|
| 58 |
+
severity MUST be: critical|high|medium|low. cvss_score: 0.0-10.0 (float).
|
| 59 |
+
NEVER call identify_vulnerability twice. After identify, ALWAYS call propose_fix next.
|
| 60 |
+
|
| 61 |
+
- dependency:
|
| 62 |
+
task_subtype=flag → flag_outdated (find deprecated packages/APIs)
|
| 63 |
+
task_subtype=resolve → resolve_conflict (pick compatible versions from compatibility_matrix)
|
| 64 |
+
task_subtype=migrate → migrate_api (fix ALL graph-break IDs, include code_changes for each)
|
| 65 |
+
|
| 66 |
+
- clinical: ALWAYS follow this order: detect_gap → rank_issues → order_steps
|
| 67 |
+
Use ONLY step IDs from observation.available_steps.
|
| 68 |
+
risk_level MUST be: critical|high|medium|low
|
| 69 |
+
If dependency_graph is present, ensure prerequisites come BEFORE dependent steps.
|
| 70 |
+
|
| 71 |
+
EXACT FORMAT EXAMPLES — copy field names exactly:
|
| 72 |
+
{"action_type": "identify_vulnerability", "vuln_type": "sql_injection", "cvss_score": 8.5, "severity": "critical", "affected_line": 3}
|
| 73 |
+
{"action_type": "propose_fix", "fix_code": "db.execute(query, (param,))", "explanation": "Use parameterized query to prevent SQL injection"}
|
| 74 |
+
{"action_type": "revise_fix", "fix_code": "cursor.execute(sql, values)", "addressed_feedback": "Used parameterized queries and added input validation"}
|
| 75 |
+
{"action_type": "flag_outdated", "packages": {"torch": "1.9.0"}, "deprecated_api": "torch.autograd.Variable", "replacement": "plain tensor"}
|
| 76 |
+
{"action_type": "resolve_conflict", "packages": {"torch": "2.1.0", "numpy": "1.24.0"}, "reasoning": "torch 2.1 requires numpy >=1.24"}
|
| 77 |
+
{"action_type": "migrate_api", "completed_items": ["break_001", "break_002", "break_003"], "code_changes": {"break_001": "use torch.where", "break_002": "use tensor.shape[0]", "break_003": "use .detach().numpy()"}}
|
| 78 |
+
{"action_type": "detect_gap", "missing_steps": ["pre_op_consent"], "risk_level": "critical"}
|
| 79 |
+
{"action_type": "rank_issues", "priority_order": ["resolve_insurance", "pre_op_consent", "book_specialist"]}
|
| 80 |
+
{"action_type": "order_steps", "recovery_steps": ["resolve_insurance", "complete_pre_op", "book_specialist", "schedule_surgery"]}
|
| 81 |
+
|
| 82 |
+
CRITICAL: Output ONLY the JSON object. Nothing before or after it.
|
| 83 |
+
""")
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def build_user_prompt(step_num: int, obs: dict, history: list) -> str:
|
| 87 |
+
"""Build a focused user prompt from observation and history.
|
| 88 |
+
Works with ALL models — keeps context compact to avoid truncation.
|
| 89 |
+
"""
|
| 90 |
+
task_type = obs.get("task_type", "unknown")
|
| 91 |
+
task_id = obs.get("task_id", "unknown")
|
| 92 |
+
task_sub = obs.get("task_subtype", "")
|
| 93 |
+
|
| 94 |
+
parts = [f"Step {step_num} | task_type={task_type} | task_id={task_id} | subtype={task_sub}"]
|
| 95 |
+
|
| 96 |
+
# History summary — short to avoid confusing models
|
| 97 |
+
if history:
|
| 98 |
+
used = [h["action_type"] for h in history]
|
| 99 |
+
last = history[-1]
|
| 100 |
+
parts.append(f"Actions used so far: {used}")
|
| 101 |
+
parts.append(f"Last reward: {last['reward']:.2f}")
|
| 102 |
+
if last["reward"] == 0.0:
|
| 103 |
+
parts.append("WARNING: Last action scored 0.0 — it was wrong or invalid. Do NOT repeat it.")
|
| 104 |
+
elif last["reward"] < 0.4:
|
| 105 |
+
parts.append(f"WARNING: Low score ({last['reward']:.2f}). Try a better approach.")
|
| 106 |
+
|
| 107 |
+
# Validation failure — show prominently
|
| 108 |
+
if obs.get("validation_failed"):
|
| 109 |
+
parts.append(f"\nACTION VALIDATION FAILED!")
|
| 110 |
+
parts.append(f"Error: {obs.get('message', 'unknown error')}")
|
| 111 |
+
hint = obs.get("hint", obs.get("available_actions", ""))
|
| 112 |
+
parts.append(f"Hint: {hint}")
|
| 113 |
+
parts.append("Fix your JSON and try again with a VALID action.")
|
| 114 |
+
|
| 115 |
+
# Reviewer feedback for security tasks
|
| 116 |
+
if obs.get("reviewer_feedback"):
|
| 117 |
+
parts.append(f"\nREVIEWER FEEDBACK (address this in your revise_fix):")
|
| 118 |
+
parts.append(obs["reviewer_feedback"])
|
| 119 |
+
|
| 120 |
+
# Full observation — separate compat matrix to avoid truncation
|
| 121 |
+
obs_copy = dict(obs)
|
| 122 |
+
compat = obs_copy.pop("compatibility_matrix", None)
|
| 123 |
+
obs_text = json.dumps(obs_copy, default=str)
|
| 124 |
+
if len(obs_text) > 1800:
|
| 125 |
+
obs_text = obs_text[:1800] + "..."
|
| 126 |
+
parts.append(f"\nObservation:\n{obs_text}")
|
| 127 |
+
|
| 128 |
+
if compat:
|
| 129 |
+
parts.append(f"\nCompatibility Matrix (use this to choose correct versions):\n{json.dumps(compat, indent=2)}")
|
| 130 |
+
|
| 131 |
+
# Next action hint — helps ALL models stay on track
|
| 132 |
+
if task_type == "security":
|
| 133 |
+
used_types = [h["action_type"] for h in history]
|
| 134 |
+
if not history or "identify_vulnerability" not in used_types:
|
| 135 |
+
parts.append("\nNEXT ACTION: identify_vulnerability")
|
| 136 |
+
elif "propose_fix" not in used_types:
|
| 137 |
+
parts.append("\nNEXT ACTION: propose_fix")
|
| 138 |
+
else:
|
| 139 |
+
parts.append("\nNEXT ACTION: revise_fix (address the reviewer_feedback)")
|
| 140 |
+
elif task_type == "clinical":
|
| 141 |
+
used_types = [h["action_type"] for h in history]
|
| 142 |
+
if "detect_gap" not in used_types:
|
| 143 |
+
parts.append("\nNEXT ACTION: detect_gap")
|
| 144 |
+
elif "rank_issues" not in used_types:
|
| 145 |
+
parts.append("\nNEXT ACTION: rank_issues (use the step IDs from available_steps)")
|
| 146 |
+
elif "order_steps" not in used_types:
|
| 147 |
+
parts.append("\nNEXT ACTION: order_steps (respect dependency_graph ordering)")
|
| 148 |
+
|
| 149 |
+
parts.append("\nOutput ONLY a single JSON object:")
|
| 150 |
+
return "\n".join(parts)
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
def parse_action(raw_text: str) -> dict:
|
| 154 |
+
"""Parse LLM response into action dict.
|
| 155 |
+
|
| 156 |
+
Universal compatibility — handles ALL known model output patterns:
|
| 157 |
+
- Qwen3/DeepSeek R1: <think>...</think>{json}
|
| 158 |
+
- QwQ: <reasoning>...</reasoning>{json}
|
| 159 |
+
- Gemini: <thought>...</thought>{json}
|
| 160 |
+
- Claude: <antThinking>...</antThinking>{json}
|
| 161 |
+
- Mistral/Mixtral: plain prose before JSON
|
| 162 |
+
- All models: ```json fences, unclosed tags, nested JSON
|
| 163 |
+
"""
|
| 164 |
+
text = raw_text.strip()
|
| 165 |
+
|
| 166 |
+
# Strip ALL known reasoning/thinking blocks (closed and unclosed)
|
| 167 |
+
for tag in ["think", "thinking", "reasoning", "reflection", "thought", "antThinking"]:
|
| 168 |
+
open_tag = f"<{tag}>"
|
| 169 |
+
close_tag = f"</{tag}>"
|
| 170 |
+
if open_tag in text:
|
| 171 |
+
if close_tag in text:
|
| 172 |
+
# Normal case: strip everything between tags
|
| 173 |
+
text = text.split(close_tag)[-1].strip()
|
| 174 |
+
else:
|
| 175 |
+
# Unclosed tag: take everything after the open tag and find JSON
|
| 176 |
+
text = text.split(open_tag)[-1].strip()
|
| 177 |
+
|
| 178 |
+
# Strip markdown code fences
|
| 179 |
+
if "```json" in text:
|
| 180 |
+
text = text.split("```json")[1].split("```")[0].strip()
|
| 181 |
+
elif "```" in text:
|
| 182 |
+
parts = text.split("```")
|
| 183 |
+
if len(parts) >= 3:
|
| 184 |
+
text = parts[1].strip()
|
| 185 |
+
|
| 186 |
+
# Find first JSON object if text has prose before/after
|
| 187 |
+
if not text.startswith("{"):
|
| 188 |
+
start = text.find("{")
|
| 189 |
+
if start >= 0:
|
| 190 |
+
end = text.rfind("}")
|
| 191 |
+
if end > start:
|
| 192 |
+
text = text[start:end + 1]
|
| 193 |
+
|
| 194 |
+
try:
|
| 195 |
+
return json.loads(text)
|
| 196 |
+
except (json.JSONDecodeError, TypeError):
|
| 197 |
+
pass
|
| 198 |
+
|
| 199 |
+
# Regex fallback: find outermost JSON object (handles nested braces)
|
| 200 |
+
match = re.search(r"\{(?:[^{}]|\{[^{}]*\})*\}", text, re.DOTALL)
|
| 201 |
+
if match:
|
| 202 |
+
try:
|
| 203 |
+
return json.loads(match.group())
|
| 204 |
+
except (json.JSONDecodeError, TypeError):
|
| 205 |
+
pass
|
| 206 |
+
|
| 207 |
+
return {"action_type": "error", "raw": text[:100]}
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
def run_task(client: OpenAI, task_id: str) -> float:
|
| 211 |
+
"""Run a single task through the environment. Returns score in [0, 1]."""
|
| 212 |
+
|
| 213 |
+
# Reset environment
|
| 214 |
+
resp = requests.post(f"{ENV_URL}/reset", json={"task_id": task_id}, timeout=30)
|
| 215 |
+
data = resp.json()
|
| 216 |
+
|
| 217 |
+
if "error" in data and not data.get("episode_id"):
|
| 218 |
+
# ── MANDATORY: [START] line even on error ──
|
| 219 |
+
print(f"[START] task={task_id} env={BENCHMARK} model={MODEL_NAME}", flush=True)
|
| 220 |
+
print(f"[END] success=false steps=0 score=0.00 rewards=", flush=True)
|
| 221 |
+
return 0.0
|
| 222 |
+
|
| 223 |
+
episode_id = data.get("episode_id", "unknown")
|
| 224 |
+
obs = data.get("observation", data)
|
| 225 |
+
|
| 226 |
+
# ── MANDATORY [START] — exact spec format ──
|
| 227 |
+
print(f"[START] task={task_id} env={BENCHMARK} model={MODEL_NAME}", flush=True)
|
| 228 |
+
|
| 229 |
+
rewards = []
|
| 230 |
+
history = []
|
| 231 |
+
step_num = 0
|
| 232 |
+
last_error = None
|
| 233 |
+
|
| 234 |
+
for step_num in range(1, MAX_STEPS + 1):
|
| 235 |
+
user_prompt = build_user_prompt(step_num, obs, history)
|
| 236 |
+
|
| 237 |
+
error_msg = None
|
| 238 |
+
try:
|
| 239 |
+
reply = client.chat.completions.create(
|
| 240 |
+
model=MODEL_NAME,
|
| 241 |
+
messages=[
|
| 242 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 243 |
+
{"role": "user", "content": user_prompt},
|
| 244 |
+
],
|
| 245 |
+
temperature=TEMPERATURE,
|
| 246 |
+
max_tokens=MAX_TOKENS,
|
| 247 |
+
)
|
| 248 |
+
response_text = (reply.choices[0].message.content or "").strip()
|
| 249 |
+
except Exception as e:
|
| 250 |
+
error_msg = str(e)
|
| 251 |
+
response_text = '{"action_type": "error"}'
|
| 252 |
+
|
| 253 |
+
action = parse_action(response_text)
|
| 254 |
+
action_type = action.get("action_type", "unknown")
|
| 255 |
+
action["episode_id"] = episode_id
|
| 256 |
+
|
| 257 |
+
try:
|
| 258 |
+
step_resp = requests.post(f"{ENV_URL}/step", json=action, timeout=30)
|
| 259 |
+
step_data = step_resp.json()
|
| 260 |
+
except Exception as e:
|
| 261 |
+
error_msg = str(e)
|
| 262 |
+
# ── MANDATORY [STEP] line on connection error ──
|
| 263 |
+
print(f"[STEP] step={step_num} action={action_type} reward=0.00 done=true error={error_msg}", flush=True)
|
| 264 |
+
rewards.append(0.0)
|
| 265 |
+
break
|
| 266 |
+
|
| 267 |
+
reward = float(step_data.get("reward", 0.0))
|
| 268 |
+
done = bool(step_data.get("done", False))
|
| 269 |
+
obs = step_data.get("observation", step_data)
|
| 270 |
+
step_error = step_data.get("error") or error_msg
|
| 271 |
+
last_error = step_error
|
| 272 |
+
|
| 273 |
+
rewards.append(reward)
|
| 274 |
+
history.append({"step": step_num, "action_type": action_type, "reward": reward, "done": done})
|
| 275 |
+
|
| 276 |
+
# Show 'invalid' for validation failures
|
| 277 |
+
display_action = action_type
|
| 278 |
+
if obs.get("validation_failed"):
|
| 279 |
+
display_action = "invalid"
|
| 280 |
+
|
| 281 |
+
# ── MANDATORY [STEP] — exact spec format ──
|
| 282 |
+
error_val = step_error if step_error else "null"
|
| 283 |
+
print(f"[STEP] step={step_num} action={display_action} reward={reward:.2f} done={str(done).lower()} error={error_val}", flush=True)
|
| 284 |
+
|
| 285 |
+
if done:
|
| 286 |
+
break
|
| 287 |
+
|
| 288 |
+
# Score = max(rewards) — agent's best single-step performance, clamped to [0, 1]
|
| 289 |
+
score = round(min(max(max(rewards) if rewards else 0.0, 0.0), 1.0), 2)
|
| 290 |
+
success = score > 0.0
|
| 291 |
+
rewards_str = ",".join(f"{r:.2f}" for r in rewards)
|
| 292 |
+
|
| 293 |
+
# ── MANDATORY [END] — exact spec format ──
|
| 294 |
+
print(f"[END] success={str(success).lower()} steps={step_num} score={score:.2f} rewards={rewards_str}", flush=True)
|
| 295 |
+
|
| 296 |
+
return score
|
| 297 |
+
|
| 298 |
+
|
| 299 |
+
def main() -> None:
|
| 300 |
+
"""Run all 9 tasks and report final scores."""
|
| 301 |
+
if not HF_TOKEN:
|
| 302 |
+
print("ERROR: Set HF_TOKEN or API_KEY environment variable.", flush=True)
|
| 303 |
+
return
|
| 304 |
+
|
| 305 |
+
client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)
|
| 306 |
+
|
| 307 |
+
# Health check
|
| 308 |
+
try:
|
| 309 |
+
health = requests.get(f"{ENV_URL}/", timeout=10, headers={"Accept": "application/json"})
|
| 310 |
+
health_data = health.json()
|
| 311 |
+
print(f"Environment: {health_data.get('env', 'unknown')} | Tasks: {health_data.get('tasks', 0)}", flush=True)
|
| 312 |
+
except Exception as e:
|
| 313 |
+
print(f"ERROR: Cannot connect to environment at {ENV_URL}: {e}", flush=True)
|
| 314 |
+
return
|
| 315 |
+
|
| 316 |
+
scores = {}
|
| 317 |
+
for task_id in TASKS:
|
| 318 |
+
try:
|
| 319 |
+
scores[task_id] = run_task(client, task_id)
|
| 320 |
+
except Exception as e:
|
| 321 |
+
print(f"[START] task={task_id} env={BENCHMARK} model={MODEL_NAME}", flush=True)
|
| 322 |
+
print(f"[END] success=false steps=0 score=0.00 rewards=", flush=True)
|
| 323 |
+
scores[task_id] = 0.0
|
| 324 |
+
|
| 325 |
+
avg = round(sum(scores.values()) / max(len(scores), 1), 2)
|
| 326 |
+
print(f"\n✅ All tasks complete! Average: {avg:.2f}", flush=True)
|
| 327 |
+
|
| 328 |
+
# Final scores JSON — evaluator may parse this
|
| 329 |
+
print(json.dumps({"final_scores": scores}), flush=True)
|
| 330 |
+
|
| 331 |
+
# Persist results to disk
|
| 332 |
+
try:
|
| 333 |
+
from server.benchmark_store import append_result
|
| 334 |
+
append_result(MODEL_NAME, MODEL_NAME, scores)
|
| 335 |
+
print(f"💾 Results saved (avg: {avg:.4f})", flush=True)
|
| 336 |
+
except Exception:
|
| 337 |
+
pass
|
| 338 |
+
|
| 339 |
+
|
| 340 |
+
if __name__ == "__main__":
|
| 341 |
+
main()
|
openenv.yaml
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
spec_version: 1
|
| 2 |
+
name: multi-agent-dev-tools-env
|
| 3 |
+
description: >
|
| 4 |
+
A multi-domain RL environment for training AI agents on real-world developer
|
| 5 |
+
and clinical tasks. Covers MCP security auditing, PyTorch migration debugging,
|
| 6 |
+
and clinical workflow chaos recovery. 9 tasks across 3 domains with graded
|
| 7 |
+
difficulty (easy/medium/hard).
|
| 8 |
+
type: environment
|
| 9 |
+
runtime: docker
|
| 10 |
+
port: 7860
|
| 11 |
+
|
| 12 |
+
# Action and Observation spaces use typed Pydantic models
|
| 13 |
+
# See server/models/ for full definitions
|
| 14 |
+
|
| 15 |
+
tasks:
|
| 16 |
+
- id: sec_easy
|
| 17 |
+
name: Single vulnerability classification
|
| 18 |
+
difficulty: easy
|
| 19 |
+
description: Identify vulnerability type, CVSS score, and severity from a tool-call snippet.
|
| 20 |
+
|
| 21 |
+
- id: sec_medium
|
| 22 |
+
name: Vulnerability identification + fix proposal
|
| 23 |
+
difficulty: medium
|
| 24 |
+
description: Identify the vulnerability and propose a secure code fix.
|
| 25 |
+
|
| 26 |
+
- id: sec_hard
|
| 27 |
+
name: Adversarial patch defense with reviewer feedback
|
| 28 |
+
difficulty: hard
|
| 29 |
+
description: Identify, fix, and iteratively revise based on reviewer feedback.
|
| 30 |
+
|
| 31 |
+
- id: dep_easy
|
| 32 |
+
name: PyTorch 1.x deprecated API detection
|
| 33 |
+
difficulty: easy
|
| 34 |
+
description: Flag outdated packages and deprecated API usage.
|
| 35 |
+
|
| 36 |
+
- id: dep_medium
|
| 37 |
+
name: Version conflict chain resolution
|
| 38 |
+
difficulty: medium
|
| 39 |
+
description: Resolve version conflicts using compatibility matrix constraints.
|
| 40 |
+
|
| 41 |
+
- id: dep_hard
|
| 42 |
+
name: torch.compile graph-break hunter
|
| 43 |
+
difficulty: hard
|
| 44 |
+
description: Fix torch.compile graph-break patterns in dependency order.
|
| 45 |
+
|
| 46 |
+
- id: cli_easy
|
| 47 |
+
name: Single workflow gap detection
|
| 48 |
+
difficulty: easy
|
| 49 |
+
description: Detect missing steps in a clinical workflow and assess risk.
|
| 50 |
+
|
| 51 |
+
- id: cli_medium
|
| 52 |
+
name: Multi-gap priority ranking
|
| 53 |
+
difficulty: medium
|
| 54 |
+
description: Detect gaps and rank them by clinical priority.
|
| 55 |
+
|
| 56 |
+
- id: cli_hard
|
| 57 |
+
name: Dependency-ordered recovery planning
|
| 58 |
+
difficulty: hard
|
| 59 |
+
description: Plan a dependency-safe recovery sequence for a disrupted clinical workflow.
|
pyproject.toml
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "multi-agent-dev-tools-env"
|
| 3 |
+
version = "1.0.0"
|
| 4 |
+
requires-python = ">=3.10"
|
| 5 |
+
dependencies = [
|
| 6 |
+
"fastapi>=0.110.0",
|
| 7 |
+
"uvicorn>=0.29.0",
|
| 8 |
+
"pydantic>=2.0.0",
|
| 9 |
+
"openai>=1.0.0",
|
| 10 |
+
"requests>=2.31.0",
|
| 11 |
+
"packaging>=24.0",
|
| 12 |
+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
# Endpoints: GET /, GET /debug, POST /reset, POST /step, GET /state, POST /inference
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
import sys
|
| 7 |
+
import json
|
| 8 |
+
import random
|
| 9 |
+
import uuid
|
| 10 |
+
import subprocess
|
| 11 |
+
from fastapi import FastAPI, Request
|
| 12 |
+
from fastapi.responses import JSONResponse, HTMLResponse
|
| 13 |
+
|
| 14 |
+
from .session import create_session, SESSIONS, TASK_TYPE_MAP, SessionState
|
| 15 |
+
from .router import route_step, build_initial_obs
|
| 16 |
+
from .validation.validator import validate_action
|
| 17 |
+
from .datasets.security_cases import SECURITY_CASES
|
| 18 |
+
from .datasets.dependency_cases import DEPENDENCY_CASES
|
| 19 |
+
from .datasets.clinical_cases import CLINICAL_CASES
|
| 20 |
+
|
| 21 |
+
app = FastAPI(title='Multi-Agent Dev Tools Environment')
|
| 22 |
+
|
| 23 |
+
# ── Load Debug Panel HTML ──
|
| 24 |
+
_DEBUG_HTML_PATH = os.path.join(os.path.dirname(__file__), 'debug_panel.html')
|
| 25 |
+
|
| 26 |
+
def _load_debug_html() -> str:
|
| 27 |
+
try:
|
| 28 |
+
with open(_DEBUG_HTML_PATH, 'r', encoding='utf-8') as f:
|
| 29 |
+
return f.read()
|
| 30 |
+
except FileNotFoundError:
|
| 31 |
+
return '<h1>Debug panel not found. Place debug_panel.html in server/ directory.</h1>'
|
| 32 |
+
|
| 33 |
+
_DEBUG_HTML = _load_debug_html()
|
| 34 |
+
|
| 35 |
+
# ── Mount Gradio UI ──
|
| 36 |
+
try:
|
| 37 |
+
from .web_ui import build_ui
|
| 38 |
+
import gradio as gr
|
| 39 |
+
ui_app = build_ui()
|
| 40 |
+
app = gr.mount_gradio_app(app, ui_app, path='/web')
|
| 41 |
+
except Exception as e:
|
| 42 |
+
import traceback
|
| 43 |
+
print(f'[WARNING] Gradio UI not mounted: {e}')
|
| 44 |
+
traceback.print_exc()
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# ── Dataset Loader ──
|
| 48 |
+
DATASETS = {
|
| 49 |
+
'sec_easy': SECURITY_CASES.get('sec_easy', []),
|
| 50 |
+
'sec_medium': SECURITY_CASES.get('sec_medium', []),
|
| 51 |
+
'sec_hard': SECURITY_CASES.get('sec_hard', []),
|
| 52 |
+
'dep_easy': DEPENDENCY_CASES.get('dep_easy', []),
|
| 53 |
+
'dep_medium': DEPENDENCY_CASES.get('dep_medium', []),
|
| 54 |
+
'dep_hard': DEPENDENCY_CASES.get('dep_hard', []),
|
| 55 |
+
'cli_easy': CLINICAL_CASES.get('cli_easy', []),
|
| 56 |
+
'cli_medium': CLINICAL_CASES.get('cli_medium', []),
|
| 57 |
+
'cli_hard': CLINICAL_CASES.get('cli_hard', []),
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# Per-domain max steps (must match grader config)
|
| 62 |
+
DOMAIN_MAX_STEPS = {'security': 8, 'dependency': 8, 'clinical': 6}
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def load_case(task_id: str, episode_id: str = '') -> dict:
|
| 66 |
+
"""Load a deterministic case for reproducibility.
|
| 67 |
+
Same episode_id always gets same case (judges can re-run and match)."""
|
| 68 |
+
cases = DATASETS.get(task_id, [])
|
| 69 |
+
if not cases:
|
| 70 |
+
return {}
|
| 71 |
+
idx = hash(episode_id) % len(cases)
|
| 72 |
+
return cases[idx]
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
# build_initial_obs is imported from router.py — single source of truth for observations
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
# ═══════════════════════════════════════════════════════════
|
| 79 |
+
# ENDPOINTS — All return HTTP 200 ALWAYS
|
| 80 |
+
# ═══════════════════════════════════════════════════════════
|
| 81 |
+
|
| 82 |
+
@app.get('/')
|
| 83 |
+
async def health(request: Request):
|
| 84 |
+
"""Health check + debug panel. Returns HTML for browsers, JSON for automated scripts."""
|
| 85 |
+
try:
|
| 86 |
+
accept = request.headers.get('accept', '')
|
| 87 |
+
if 'text/html' in accept:
|
| 88 |
+
return HTMLResponse(content=_DEBUG_HTML, status_code=200)
|
| 89 |
+
return {
|
| 90 |
+
'status': 'ok',
|
| 91 |
+
'env': 'Multi-Agent Real-World Ecosystem',
|
| 92 |
+
'domains': ['security', 'pytorch', 'clinical'],
|
| 93 |
+
'tasks': 9,
|
| 94 |
+
'task_ids': [
|
| 95 |
+
'sec_easy', 'sec_medium', 'sec_hard',
|
| 96 |
+
'dep_easy', 'dep_medium', 'dep_hard',
|
| 97 |
+
'cli_easy', 'cli_medium', 'cli_hard',
|
| 98 |
+
],
|
| 99 |
+
'spec': 'OpenEnv v1',
|
| 100 |
+
}
|
| 101 |
+
except Exception as e:
|
| 102 |
+
return JSONResponse(status_code=200, content={'status': 'error', 'error': str(e)})
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
@app.post('/reset')
|
| 106 |
+
async def reset(request: Request):
|
| 107 |
+
"""Create a new episode for a task. Returns episode_id + initial observation."""
|
| 108 |
+
try:
|
| 109 |
+
body = await request.json()
|
| 110 |
+
task_id = body.get('task_id', 'sec_easy')
|
| 111 |
+
|
| 112 |
+
if task_id not in TASK_TYPE_MAP:
|
| 113 |
+
return JSONResponse(status_code=200, content={
|
| 114 |
+
'error': f'Unknown task_id: {task_id}',
|
| 115 |
+
'observation': {},
|
| 116 |
+
'done': True,
|
| 117 |
+
})
|
| 118 |
+
|
| 119 |
+
ep_id = str(uuid.uuid4())
|
| 120 |
+
task_case = load_case(task_id, ep_id)
|
| 121 |
+
session = create_session(task_id, task_case)
|
| 122 |
+
session.episode_id = ep_id
|
| 123 |
+
SESSIONS[session.episode_id] = session
|
| 124 |
+
|
| 125 |
+
# Cleanup old done sessions to prevent memory leaks on HF Spaces
|
| 126 |
+
done_ids = [eid for eid, s in SESSIONS.items() if s.done]
|
| 127 |
+
for eid in done_ids:
|
| 128 |
+
del SESSIONS[eid]
|
| 129 |
+
|
| 130 |
+
obs = build_initial_obs(session)
|
| 131 |
+
|
| 132 |
+
return {
|
| 133 |
+
'episode_id': session.episode_id,
|
| 134 |
+
'observation': obs,
|
| 135 |
+
}
|
| 136 |
+
except Exception as e:
|
| 137 |
+
return JSONResponse(status_code=200, content={
|
| 138 |
+
'error': str(e),
|
| 139 |
+
'observation': {},
|
| 140 |
+
'done': True,
|
| 141 |
+
'reward': 0.0,
|
| 142 |
+
})
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
@app.post('/step')
|
| 146 |
+
async def step(request: Request):
|
| 147 |
+
"""Submit an action for an episode. Returns reward + next observation."""
|
| 148 |
+
try:
|
| 149 |
+
body = await request.json()
|
| 150 |
+
ep_id = body.get('episode_id')
|
| 151 |
+
session = SESSIONS.get(ep_id)
|
| 152 |
+
|
| 153 |
+
if not session:
|
| 154 |
+
return JSONResponse(status_code=200, content={
|
| 155 |
+
'reward': 0.0,
|
| 156 |
+
'done': True,
|
| 157 |
+
'error': 'unknown episode_id',
|
| 158 |
+
'observation': {},
|
| 159 |
+
})
|
| 160 |
+
|
| 161 |
+
if session.done:
|
| 162 |
+
return JSONResponse(status_code=200, content={
|
| 163 |
+
'reward': 0.0,
|
| 164 |
+
'done': True,
|
| 165 |
+
'observation': {'message': 'Episode already complete.'},
|
| 166 |
+
})
|
| 167 |
+
|
| 168 |
+
# Run pre-action validation
|
| 169 |
+
valid, val_obs = validate_action(body, session)
|
| 170 |
+
if not valid:
|
| 171 |
+
last_r = 0.0
|
| 172 |
+
if session.history:
|
| 173 |
+
last_r = session.history[-1].get('reward', 0.0)
|
| 174 |
+
return {
|
| 175 |
+
'reward': last_r,
|
| 176 |
+
'done': False,
|
| 177 |
+
'observation': val_obs,
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
# Route to grader
|
| 181 |
+
result = route_step(session, body)
|
| 182 |
+
|
| 183 |
+
# Update session state
|
| 184 |
+
session.step_count += 1
|
| 185 |
+
session.last_actions.append(body.get('action_type', 'unknown'))
|
| 186 |
+
session.history.append(body)
|
| 187 |
+
session.reward_acc += result.get('reward', 0.0)
|
| 188 |
+
session.done = result.get('done', False)
|
| 189 |
+
|
| 190 |
+
# Enrich observation with strategic context
|
| 191 |
+
step_obs = result.get('observation', {})
|
| 192 |
+
step_obs['task_type'] = session.task_type
|
| 193 |
+
step_obs['task_id'] = session.task_id
|
| 194 |
+
step_obs['step_count'] = session.step_count
|
| 195 |
+
task_max = DOMAIN_MAX_STEPS.get(session.task_type, 8)
|
| 196 |
+
step_obs['max_steps'] = task_max
|
| 197 |
+
step_obs['previous_reward'] = round(float(result.get('reward', 0.0)), 4)
|
| 198 |
+
step_obs['steps_remaining'] = max(0, task_max - session.step_count)
|
| 199 |
+
step_obs['reward_so_far'] = round(session.reward_acc, 4)
|
| 200 |
+
step_obs['trajectory_score'] = round(
|
| 201 |
+
session.reward_acc / max(session.step_count, 1), 4
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
# Turn guidance — tell agent what to do next
|
| 205 |
+
last_action = body.get('action_type', '')
|
| 206 |
+
if session.task_type == 'security':
|
| 207 |
+
if last_action == 'identify_vulnerability':
|
| 208 |
+
step_obs['next_expected_action'] = 'propose_fix'
|
| 209 |
+
step_obs['guidance'] = 'Vulnerability identified. Now propose a fix using propose_fix.'
|
| 210 |
+
elif last_action == 'propose_fix':
|
| 211 |
+
step_obs['next_expected_action'] = 'revise_fix'
|
| 212 |
+
step_obs['guidance'] = 'Fix proposed. If reviewer_feedback is present, use revise_fix.'
|
| 213 |
+
elif session.task_type == 'clinical':
|
| 214 |
+
if last_action == 'detect_gap':
|
| 215 |
+
step_obs['next_expected_action'] = 'rank_issues'
|
| 216 |
+
step_obs['guidance'] = 'Gaps detected. Now rank issues by priority using rank_issues.'
|
| 217 |
+
elif last_action == 'rank_issues':
|
| 218 |
+
step_obs['next_expected_action'] = 'order_steps'
|
| 219 |
+
step_obs['guidance'] = 'Issues ranked. Now create recovery plan using order_steps.'
|
| 220 |
+
|
| 221 |
+
# Cleanup session if done
|
| 222 |
+
if session.done:
|
| 223 |
+
SESSIONS.pop(session.episode_id, None)
|
| 224 |
+
|
| 225 |
+
return {
|
| 226 |
+
'reward': round(float(result.get('reward', 0.0)), 4),
|
| 227 |
+
'done': bool(result.get('done', False)),
|
| 228 |
+
'observation': step_obs,
|
| 229 |
+
}
|
| 230 |
+
except Exception as e:
|
| 231 |
+
return JSONResponse(status_code=200, content={
|
| 232 |
+
'reward': 0.0,
|
| 233 |
+
'done': True,
|
| 234 |
+
'error': str(e),
|
| 235 |
+
'observation': {},
|
| 236 |
+
})
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
@app.get('/state')
|
| 240 |
+
async def state(episode_id: str = ''):
|
| 241 |
+
"""Get current state of an episode."""
|
| 242 |
+
try:
|
| 243 |
+
session = SESSIONS.get(episode_id)
|
| 244 |
+
if not session:
|
| 245 |
+
return {
|
| 246 |
+
'episode_id': episode_id,
|
| 247 |
+
'step_count': 0,
|
| 248 |
+
'done': True,
|
| 249 |
+
}
|
| 250 |
+
return {
|
| 251 |
+
'episode_id': session.episode_id,
|
| 252 |
+
'step_count': session.step_count,
|
| 253 |
+
'active_domain': session.task_type,
|
| 254 |
+
'reward_acc': round(session.reward_acc, 4),
|
| 255 |
+
'done': session.done,
|
| 256 |
+
}
|
| 257 |
+
except Exception as e:
|
| 258 |
+
return JSONResponse(status_code=200, content={'error': str(e)})
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
# ═══════════════════════════════════════════════════════════
|
| 262 |
+
# DEBUG PANEL — guaranteed HTML endpoint
|
| 263 |
+
# ═══════════════════════════════════════════════════════════
|
| 264 |
+
|
| 265 |
+
@app.get('/debug', response_class=HTMLResponse)
|
| 266 |
+
async def debug_panel():
|
| 267 |
+
"""Always serves the debug panel HTML regardless of Accept header."""
|
| 268 |
+
try:
|
| 269 |
+
html = _load_debug_html() # Reload from disk each time for development
|
| 270 |
+
return HTMLResponse(content=html, status_code=200)
|
| 271 |
+
except Exception as e:
|
| 272 |
+
return HTMLResponse(content=f'<h1>Error loading debug panel: {e}</h1>', status_code=200)
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
# ═══════════════════════════════════════════════════════════
|
| 276 |
+
# INFERENCE — run inference.py from browser
|
| 277 |
+
# ═══════════════════════════════════════════════════════════
|
| 278 |
+
|
| 279 |
+
@app.post('/inference')
|
| 280 |
+
async def run_inference(request: Request):
|
| 281 |
+
"""Runs inference.py as a subprocess and returns parsed scores."""
|
| 282 |
+
try:
|
| 283 |
+
env_vars = os.environ.copy()
|
| 284 |
+
env_vars['ENV_URL'] = env_vars.get('ENV_URL', 'http://localhost:7860')
|
| 285 |
+
|
| 286 |
+
# Find inference.py at project root (one level up from server/)
|
| 287 |
+
inference_path = os.path.join(
|
| 288 |
+
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
|
| 289 |
+
'inference.py'
|
| 290 |
+
)
|
| 291 |
+
|
| 292 |
+
if not os.path.exists(inference_path):
|
| 293 |
+
return JSONResponse(status_code=200, content={
|
| 294 |
+
'error': 'inference.py not found at project root',
|
| 295 |
+
'path_checked': inference_path,
|
| 296 |
+
})
|
| 297 |
+
|
| 298 |
+
result = subprocess.run(
|
| 299 |
+
[sys.executable, inference_path],
|
| 300 |
+
capture_output=True, text=True, timeout=1200, # 20 min max
|
| 301 |
+
env=env_vars
|
| 302 |
+
)
|
| 303 |
+
|
| 304 |
+
stdout = result.stdout or ''
|
| 305 |
+
stderr = result.stderr or ''
|
| 306 |
+
|
| 307 |
+
# Parse [END] lines for scores
|
| 308 |
+
logs = []
|
| 309 |
+
final_scores = {}
|
| 310 |
+
for line in stdout.splitlines():
|
| 311 |
+
line = line.strip()
|
| 312 |
+
if not line:
|
| 313 |
+
continue
|
| 314 |
+
logs.append(line)
|
| 315 |
+
if line.startswith('[END]'):
|
| 316 |
+
parts = {}
|
| 317 |
+
for token in line.split():
|
| 318 |
+
if '=' in token:
|
| 319 |
+
k, v = token.split('=', 1)
|
| 320 |
+
parts[k] = v
|
| 321 |
+
task_id = parts.get('task_id', '')
|
| 322 |
+
total_reward = parts.get('total_reward', '0')
|
| 323 |
+
if task_id:
|
| 324 |
+
try:
|
| 325 |
+
final_scores[task_id] = float(total_reward)
|
| 326 |
+
except ValueError:
|
| 327 |
+
final_scores[task_id] = 0.0
|
| 328 |
+
|
| 329 |
+
# Also try final JSON summary line
|
| 330 |
+
for line in reversed(stdout.splitlines()):
|
| 331 |
+
line = line.strip()
|
| 332 |
+
if line.startswith('{') and 'final_scores' in line:
|
| 333 |
+
try:
|
| 334 |
+
parsed = json.loads(line)
|
| 335 |
+
if 'final_scores' in parsed:
|
| 336 |
+
final_scores = parsed['final_scores']
|
| 337 |
+
except Exception:
|
| 338 |
+
pass
|
| 339 |
+
break
|
| 340 |
+
|
| 341 |
+
avg = (
|
| 342 |
+
round(sum(final_scores.values()) / len(final_scores), 4)
|
| 343 |
+
if final_scores else 0.0
|
| 344 |
+
)
|
| 345 |
+
|
| 346 |
+
return JSONResponse(status_code=200, content={
|
| 347 |
+
'status': 'ok' if result.returncode == 0 else 'completed_with_errors',
|
| 348 |
+
'final_scores': final_scores,
|
| 349 |
+
'average_score': avg,
|
| 350 |
+
'logs': logs[-50:],
|
| 351 |
+
'stderr': stderr[-500:] if stderr else '',
|
| 352 |
+
'returncode': result.returncode,
|
| 353 |
+
})
|
| 354 |
+
|
| 355 |
+
except subprocess.TimeoutExpired:
|
| 356 |
+
return JSONResponse(status_code=200, content={
|
| 357 |
+
'error': 'inference.py timed out after 20 minutes',
|
| 358 |
+
'final_scores': {},
|
| 359 |
+
})
|
| 360 |
+
except Exception as e:
|
| 361 |
+
return JSONResponse(status_code=200, content={
|
| 362 |
+
'error': str(e),
|
| 363 |
+
'final_scores': {},
|
| 364 |
+
})
|
| 365 |
+
|
| 366 |
+
|
| 367 |
+
# ═══════════════════════════════════════════════════════════
|
| 368 |
+
# BENCHMARK RUNNER — run from the UI with custom API keys
|
| 369 |
+
# ═══════════════════════════════════════════════════════════
|
| 370 |
+
|
| 371 |
+
TASK_IDS = [
|
| 372 |
+
'sec_easy', 'sec_medium', 'sec_hard',
|
| 373 |
+
'dep_easy', 'dep_medium', 'dep_hard',
|
| 374 |
+
'cli_easy', 'cli_medium', 'cli_hard',
|
| 375 |
+
]
|
| 376 |
+
|
| 377 |
+
|
| 378 |
+
def _parse_llm_response(raw_text: str) -> str:
|
| 379 |
+
"""Strip thinking blocks and markdown from LLM response. Universal model compat."""
|
| 380 |
+
text = raw_text.strip()
|
| 381 |
+
# Strip ALL known reasoning/thinking blocks (closed and unclosed)
|
| 382 |
+
for tag in ['think', 'thinking', 'reasoning', 'reflection', 'thought', 'antThinking']:
|
| 383 |
+
open_tag = f'<{tag}>'
|
| 384 |
+
close_tag = f'</{tag}>'
|
| 385 |
+
if open_tag in text:
|
| 386 |
+
if close_tag in text:
|
| 387 |
+
text = text.split(close_tag)[-1].strip()
|
| 388 |
+
else:
|
| 389 |
+
text = text.split(open_tag)[-1].strip()
|
| 390 |
+
# Strip markdown fences
|
| 391 |
+
if '```json' in text:
|
| 392 |
+
text = text.split('```json')[1].split('```')[0].strip()
|
| 393 |
+
elif '```' in text:
|
| 394 |
+
parts = text.split('```')
|
| 395 |
+
if len(parts) >= 3:
|
| 396 |
+
text = parts[1].strip()
|
| 397 |
+
# Find JSON object
|
| 398 |
+
if not text.startswith('{'):
|
| 399 |
+
start = text.find('{')
|
| 400 |
+
if start >= 0:
|
| 401 |
+
end = text.rfind('}')
|
| 402 |
+
if end > start:
|
| 403 |
+
text = text[start:end + 1]
|
| 404 |
+
return text
|
| 405 |
+
|
| 406 |
+
|
| 407 |
+
def _run_single_task_inline(task_id, api_base, api_key, model_id, system_prompt):
|
| 408 |
+
"""Run one task against the local server. Yields dict events."""
|
| 409 |
+
import re
|
| 410 |
+
import requests as req
|
| 411 |
+
|
| 412 |
+
logs = []
|
| 413 |
+
try:
|
| 414 |
+
from openai import OpenAI
|
| 415 |
+
client = OpenAI(base_url=api_base, api_key=api_key)
|
| 416 |
+
except Exception as e:
|
| 417 |
+
msg = f'[ERROR] OpenAI client init failed: {e}'
|
| 418 |
+
logs.append(msg)
|
| 419 |
+
yield {'type': 'log', 'level': 'err', 'msg': msg}
|
| 420 |
+
yield {'type': 'task_done', 'task_id': task_id, 'score': 0.0, 'logs': logs}
|
| 421 |
+
return
|
| 422 |
+
|
| 423 |
+
# Reset
|
| 424 |
+
try:
|
| 425 |
+
resp = req.post('http://localhost:7860/reset', json={'task_id': task_id}, timeout=30)
|
| 426 |
+
data = resp.json()
|
| 427 |
+
except Exception as e:
|
| 428 |
+
msg = f'[ERROR] Reset failed: {e}'
|
| 429 |
+
logs.append(msg)
|
| 430 |
+
yield {'type': 'log', 'level': 'err', 'msg': msg}
|
| 431 |
+
yield {'type': 'task_done', 'task_id': task_id, 'score': 0.0, 'logs': logs}
|
| 432 |
+
return
|
| 433 |
+
|
| 434 |
+
ep_id = data.get('episode_id', 'unknown')
|
| 435 |
+
obs = data.get('observation', data)
|
| 436 |
+
msg = f'[START] task={task_id} env=multi-agent-dev-tools-env model={model_id}'
|
| 437 |
+
logs.append(msg)
|
| 438 |
+
yield {'type': 'log', 'level': 'info', 'msg': msg}
|
| 439 |
+
|
| 440 |
+
messages = [{'role': 'system', 'content': system_prompt}]
|
| 441 |
+
rewards = []
|
| 442 |
+
history = []
|
| 443 |
+
done = False
|
| 444 |
+
max_steps = 8
|
| 445 |
+
|
| 446 |
+
while not done and len(rewards) < max_steps:
|
| 447 |
+
step_num = len(rewards) + 1
|
| 448 |
+
# Build focused prompt with history context
|
| 449 |
+
obs_text = json.dumps(obs, default=str)
|
| 450 |
+
if len(obs_text) > 1500:
|
| 451 |
+
obs_text = obs_text[:1500] + '...'
|
| 452 |
+
user_parts = [f'Step {step_num} | Observation:']
|
| 453 |
+
if history:
|
| 454 |
+
user_parts.append(f'Previous actions: {[h["action_type"] for h in history]}')
|
| 455 |
+
if history[-1]['reward'] == 0.0:
|
| 456 |
+
user_parts.append('WARNING: Last action scored 0.0 — do NOT repeat it.')
|
| 457 |
+
user_parts.append(obs_text)
|
| 458 |
+
user_parts.append('Output ONLY a single JSON object:')
|
| 459 |
+
messages.append({'role': 'user', 'content': '\n'.join(user_parts)})
|
| 460 |
+
|
| 461 |
+
try:
|
| 462 |
+
reply = client.chat.completions.create(
|
| 463 |
+
model=model_id, messages=messages, max_tokens=400, temperature=0.1
|
| 464 |
+
)
|
| 465 |
+
agent_text = (reply.choices[0].message.content or '').strip()
|
| 466 |
+
except Exception as e:
|
| 467 |
+
agent_text = '{"action_type":"invalid"}'
|
| 468 |
+
msg = f'[WARN] API error: {str(e)[:100]}'
|
| 469 |
+
logs.append(msg)
|
| 470 |
+
yield {'type': 'log', 'level': 'warn', 'msg': msg}
|
| 471 |
+
|
| 472 |
+
# Universal think-block + markdown stripping
|
| 473 |
+
raw = _parse_llm_response(agent_text)
|
| 474 |
+
|
| 475 |
+
messages.append({'role': 'assistant', 'content': raw})
|
| 476 |
+
if len(messages) > 12:
|
| 477 |
+
messages = [messages[0]] + messages[-10:]
|
| 478 |
+
|
| 479 |
+
try:
|
| 480 |
+
action = json.loads(raw)
|
| 481 |
+
except Exception:
|
| 482 |
+
# Regex fallback
|
| 483 |
+
match = re.search(r'\{(?:[^{}]|\{[^{}]*\})*\}', raw, re.DOTALL)
|
| 484 |
+
if match:
|
| 485 |
+
try:
|
| 486 |
+
action = json.loads(match.group())
|
| 487 |
+
except Exception:
|
| 488 |
+
action = {'action_type': 'invalid'}
|
| 489 |
+
else:
|
| 490 |
+
action = {'action_type': 'invalid'}
|
| 491 |
+
|
| 492 |
+
# Step
|
| 493 |
+
try:
|
| 494 |
+
step_resp = req.post('http://localhost:7860/step', json={
|
| 495 |
+
'episode_id': ep_id, **action
|
| 496 |
+
}, timeout=30)
|
| 497 |
+
step_data = step_resp.json()
|
| 498 |
+
except Exception as e:
|
| 499 |
+
msg = f'[ERROR] Step failed: {e}'
|
| 500 |
+
logs.append(msg)
|
| 501 |
+
yield {'type': 'log', 'level': 'err', 'msg': msg}
|
| 502 |
+
break
|
| 503 |
+
|
| 504 |
+
reward = float(step_data.get('reward', 0.0))
|
| 505 |
+
done = bool(step_data.get('done', False))
|
| 506 |
+
obs = step_data.get('observation', step_data)
|
| 507 |
+
rewards.append(reward)
|
| 508 |
+
|
| 509 |
+
atype = action.get('action_type', '?')
|
| 510 |
+
display_action = atype
|
| 511 |
+
if obs.get('validation_failed'):
|
| 512 |
+
display_action = 'invalid'
|
| 513 |
+
history.append({'action_type': atype, 'reward': reward})
|
| 514 |
+
|
| 515 |
+
error_val = step_data.get('error', 'null') or 'null'
|
| 516 |
+
msg = f'[STEP] step={step_num} action={display_action} reward={reward:.2f} done={str(done).lower()} error={error_val}'
|
| 517 |
+
logs.append(msg)
|
| 518 |
+
yield {'type': 'log', 'level': 'info', 'msg': msg}
|
| 519 |
+
|
| 520 |
+
# Score = max(rewards) — same logic as inference.py
|
| 521 |
+
score = round(max(rewards) if rewards else 0.0, 2)
|
| 522 |
+
score = min(max(score, 0.0), 1.0)
|
| 523 |
+
success = score > 0.0
|
| 524 |
+
rewards_str = ','.join(f'{r:.2f}' for r in rewards)
|
| 525 |
+
|
| 526 |
+
msg = f'[END] success={str(success).lower()} steps={len(rewards)} score={score:.2f} rewards={rewards_str}'
|
| 527 |
+
logs.append(msg)
|
| 528 |
+
yield {'type': 'log', 'level': 'ok', 'msg': msg}
|
| 529 |
+
yield {'type': 'task_done', 'task_id': task_id, 'score': score, 'logs': logs}
|
| 530 |
+
|
| 531 |
+
|
| 532 |
+
@app.post('/benchmark/run')
|
| 533 |
+
def run_benchmark(body: dict):
|
| 534 |
+
"""Run all 9 tasks with a given model config. Streams results via SSE."""
|
| 535 |
+
from datetime import datetime
|
| 536 |
+
from fastapi.responses import StreamingResponse
|
| 537 |
+
from .benchmark_store import append_result
|
| 538 |
+
import json
|
| 539 |
+
|
| 540 |
+
model_name = body.get('model_name', 'Unknown Model')
|
| 541 |
+
model_id = body.get('model_id', '')
|
| 542 |
+
api_base = body.get('api_base', '')
|
| 543 |
+
api_key = body.get('api_key', '')
|
| 544 |
+
|
| 545 |
+
if not model_id or not api_base or not api_key:
|
| 546 |
+
return JSONResponse(status_code=200, content={'error': 'missing_fields'})
|
| 547 |
+
|
| 548 |
+
system_prompt = body.get('system_prompt', '') or BENCHMARK_SYSTEM_PROMPT
|
| 549 |
+
|
| 550 |
+
def event_stream():
|
| 551 |
+
scores = {}
|
| 552 |
+
all_logs = []
|
| 553 |
+
for task_id in TASK_IDS:
|
| 554 |
+
for event in _run_single_task_inline(task_id, api_base, api_key, model_id, system_prompt):
|
| 555 |
+
if event.get('type') == 'log':
|
| 556 |
+
all_logs.append(event['msg'])
|
| 557 |
+
elif event.get('type') == 'task_done':
|
| 558 |
+
scores[task_id] = event['score']
|
| 559 |
+
yield f"data: {json.dumps(event)}\n\n"
|
| 560 |
+
|
| 561 |
+
avg = round(sum(scores.values()) / len(scores), 4) if scores else 0.0
|
| 562 |
+
|
| 563 |
+
result = {
|
| 564 |
+
'model_name': model_name,
|
| 565 |
+
'model_id': model_id,
|
| 566 |
+
'api_base': api_base,
|
| 567 |
+
'scores': scores,
|
| 568 |
+
'average': avg,
|
| 569 |
+
'timestamp': datetime.now().isoformat(),
|
| 570 |
+
'logs': all_logs,
|
| 571 |
+
}
|
| 572 |
+
|
| 573 |
+
# Persist to disk via benchmark_store
|
| 574 |
+
try:
|
| 575 |
+
append_result(model_name, model_id, scores)
|
| 576 |
+
except Exception:
|
| 577 |
+
pass
|
| 578 |
+
yield f"data: {json.dumps({'type': 'done', 'result': result})}\n\n"
|
| 579 |
+
|
| 580 |
+
return StreamingResponse(event_stream(), media_type="text/event-stream")
|
| 581 |
+
|
| 582 |
+
|
| 583 |
+
@app.get('/benchmark/results')
|
| 584 |
+
async def get_benchmark_results():
|
| 585 |
+
"""Return all saved benchmark results (persisted to disk)."""
|
| 586 |
+
from .benchmark_store import get_all
|
| 587 |
+
results = get_all()
|
| 588 |
+
return JSONResponse(status_code=200, content={
|
| 589 |
+
'results': results,
|
| 590 |
+
'count': len(results),
|
| 591 |
+
})
|
| 592 |
+
|
| 593 |
+
|
| 594 |
+
@app.post('/benchmark/clear')
|
| 595 |
+
async def clear_benchmark_results():
|
| 596 |
+
"""Clear all saved benchmark results."""
|
| 597 |
+
from .benchmark_store import _save
|
| 598 |
+
_save([])
|
| 599 |
+
return JSONResponse(status_code=200, content={'status': 'cleared'})
|
| 600 |
+
|
| 601 |
+
|
| 602 |
+
# Default system prompt for benchmark
|
| 603 |
+
BENCHMARK_SYSTEM_PROMPT = '''You are a multi-domain analyst agent. Each observation has a task_type field.
|
| 604 |
+
Read it. Respond ONLY with a single valid JSON object. No prose, no markdown, no explanation.
|
| 605 |
+
|
| 606 |
+
IF task_type == 'security':
|
| 607 |
+
Turn 1 ALWAYS: {"action_type":"identify_vulnerability","vuln_type":"sql_injection","cvss_score":9.1,"severity":"critical"}
|
| 608 |
+
Turn 2 ALWAYS: {"action_type":"propose_fix","fix_code":"db.execute(sql, (param,))","explanation":"Use parameterized query"}
|
| 609 |
+
Turn 3+ (reviewer_feedback present): {"action_type":"revise_fix","fix_code":"<fixed code>","addressed_feedback":"<COPY feedback verbatim>"}
|
| 610 |
+
|
| 611 |
+
IF task_type == 'dependency':
|
| 612 |
+
task_subtype=flag: {"action_type":"flag_outdated","packages":{"torch":"1.9.0"},"deprecated_api":"torch.autograd.Variable","replacement":"plain tensor"}
|
| 613 |
+
task_subtype=resolve: READ compatibility_matrix. {"action_type":"resolve_conflict","packages":{"torch":"2.1.0","numpy":"1.24.0"},"reasoning":"..."}
|
| 614 |
+
task_subtype=migrate: {"action_type":"migrate_api","completed_items":["break_001"],"code_changes":{"break_001":"torch.where"}}
|
| 615 |
+
|
| 616 |
+
IF task_type == 'clinical':
|
| 617 |
+
Turn 1: {"action_type":"detect_gap","missing_steps":["step1","step2"],"risk_level":"critical"}
|
| 618 |
+
Turn 2: {"action_type":"rank_issues","priority_order":["most_urgent","least_urgent"]}
|
| 619 |
+
Turn 3: {"action_type":"order_steps","recovery_steps":["first","second","last"]}
|
| 620 |
+
|
| 621 |
+
ALWAYS: Output ONLY a single JSON object. Follow guidance and next_expected_action.
|
| 622 |
+
'''
|
| 623 |
+
|
| 624 |
+
def main():
|
| 625 |
+
import uvicorn
|
| 626 |
+
uvicorn.run("server.app:app", host="0.0.0.0", port=7860, reload=False)
|
| 627 |
+
|
| 628 |
+
if __name__ == "__main__":
|
| 629 |
+
main()
|
| 630 |
+
|
| 631 |
+
|
server/benchmark_store.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# server/benchmark_store.py
|
| 2 |
+
# Persists benchmark results to disk so they survive server restarts.
|
| 3 |
+
# Used by both inference.py (CLI) and web_ui.py (frontend).
|
| 4 |
+
|
| 5 |
+
import json
|
| 6 |
+
import os
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
from typing import List, Dict
|
| 9 |
+
|
| 10 |
+
_STORE_PATH = os.path.join(
|
| 11 |
+
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
|
| 12 |
+
'results', 'run_history.json'
|
| 13 |
+
)
|
| 14 |
+
os.makedirs(os.path.dirname(_STORE_PATH), exist_ok=True)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def _load() -> List[Dict]:
|
| 18 |
+
"""Load all benchmark results from disk."""
|
| 19 |
+
if not os.path.exists(_STORE_PATH):
|
| 20 |
+
return []
|
| 21 |
+
try:
|
| 22 |
+
with open(_STORE_PATH, 'r', encoding='utf-8') as f:
|
| 23 |
+
data = json.load(f)
|
| 24 |
+
return data if isinstance(data, list) else []
|
| 25 |
+
except (json.JSONDecodeError, IOError):
|
| 26 |
+
return []
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def _save(results: List[Dict]) -> None:
|
| 30 |
+
"""Save all benchmark results to disk."""
|
| 31 |
+
try:
|
| 32 |
+
with open(_STORE_PATH, 'w', encoding='utf-8') as f:
|
| 33 |
+
json.dump(results, f, indent=2, default=str)
|
| 34 |
+
except IOError as e:
|
| 35 |
+
print(f"[benchmark_store] WARNING: Could not save results: {e}")
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def append_result(model: str, model_id: str, scores: Dict[str, float]) -> Dict:
|
| 39 |
+
"""Add a new benchmark result and persist to disk. Returns the saved entry."""
|
| 40 |
+
avg = round(sum(scores.values()) / max(len(scores), 1), 4)
|
| 41 |
+
entry = {
|
| 42 |
+
'model': model,
|
| 43 |
+
'model_id': model_id,
|
| 44 |
+
'scores': scores,
|
| 45 |
+
'avg': avg,
|
| 46 |
+
'type': 'full_run',
|
| 47 |
+
'timestamp': datetime.utcnow().isoformat(),
|
| 48 |
+
}
|
| 49 |
+
results = _load()
|
| 50 |
+
results.append(entry)
|
| 51 |
+
_save(results)
|
| 52 |
+
return entry
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def get_all() -> List[Dict]:
|
| 56 |
+
"""Return all benchmark results, newest first."""
|
| 57 |
+
results = _load()
|
| 58 |
+
return sorted(results, key=lambda x: x.get('timestamp', ''), reverse=True)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def get_leaderboard() -> List[Dict]:
|
| 62 |
+
"""Return deduplicated leaderboard: best score per model_id."""
|
| 63 |
+
results = _load()
|
| 64 |
+
best: Dict[str, Dict] = {}
|
| 65 |
+
for r in results:
|
| 66 |
+
mid = r.get('model_id', r.get('model', 'unknown'))
|
| 67 |
+
if mid not in best or r.get('avg', 0) > best[mid].get('avg', 0):
|
| 68 |
+
best[mid] = r
|
| 69 |
+
return sorted(best.values(), key=lambda x: x.get('avg', 0), reverse=True)
|
server/datasets/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# server/datasets package
|
server/datasets/clinical_cases.py
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# server/datasets/clinical_cases.py
|
| 2 |
+
# Ground truth cases for Clinical Workflow Chaos Simulator tasks.
|
| 3 |
+
# Covers: gap detection, priority ranking, dependency-ordered recovery planning.
|
| 4 |
+
|
| 5 |
+
CLINICAL_CASES = {
|
| 6 |
+
'cli_easy': [
|
| 7 |
+
{
|
| 8 |
+
'case_id': 'cli_easy_001',
|
| 9 |
+
'completion_threshold': 0.80,
|
| 10 |
+
'max_steps': 4,
|
| 11 |
+
'done_conditions': {'min_actions': 1, 'required_sequence': ['detect_gap']},
|
| 12 |
+
'patient_id': 'P101',
|
| 13 |
+
'patient_events': ['admission', 'surgery_scheduled', 'surgery_performed'],
|
| 14 |
+
'events': ['admission', 'surgery_scheduled', 'surgery_performed'],
|
| 15 |
+
'expected_missing_steps': ['pre_op_consent'],
|
| 16 |
+
'expected_risk': 'critical',
|
| 17 |
+
'available_steps': ['pre_op_consent', 'blood_work', 'anesthesia_consult'],
|
| 18 |
+
'task_description': 'A patient is scheduled for surgery but the pre-operative checklist is incomplete. Identify the missing step and assess the risk level.',
|
| 19 |
+
},
|
| 20 |
+
{
|
| 21 |
+
'case_id': 'cli_easy_002',
|
| 22 |
+
'completion_threshold': 0.80,
|
| 23 |
+
'max_steps': 4,
|
| 24 |
+
'done_conditions': {'min_actions': 1, 'required_sequence': ['detect_gap']},
|
| 25 |
+
'patient_id': 'P102',
|
| 26 |
+
'patient_events': ['admission', 'diagnosis', 'medication_prescribed', 'discharge'],
|
| 27 |
+
'events': ['admission', 'diagnosis', 'medication_prescribed', 'discharge'],
|
| 28 |
+
'expected_missing_steps': ['allergy_check'],
|
| 29 |
+
'expected_risk': 'high',
|
| 30 |
+
'available_steps': ['allergy_check', 'follow_up_scheduled', 'lab_results_reviewed'],
|
| 31 |
+
'task_description': 'Find the missing safety check in this medication workflow.',
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
'case_id': 'cli_easy_003',
|
| 35 |
+
'completion_threshold': 0.80,
|
| 36 |
+
'max_steps': 4,
|
| 37 |
+
'done_conditions': {'min_actions': 1, 'required_sequence': ['detect_gap']},
|
| 38 |
+
'patient_id': 'P103',
|
| 39 |
+
'patient_events': ['er_admission', 'triage', 'treatment', 'discharge'],
|
| 40 |
+
'events': ['er_admission', 'triage', 'treatment', 'discharge'],
|
| 41 |
+
'expected_missing_steps': ['insurance_verification'],
|
| 42 |
+
'expected_risk': 'medium',
|
| 43 |
+
'available_steps': ['insurance_verification', 'attending_consult', 'social_work_referral'],
|
| 44 |
+
'task_description': 'Identify the missing administrative step in this ER workflow.',
|
| 45 |
+
},
|
| 46 |
+
{
|
| 47 |
+
'case_id': 'cli_easy_004',
|
| 48 |
+
'completion_threshold': 0.80,
|
| 49 |
+
'max_steps': 4,
|
| 50 |
+
'done_conditions': {'min_actions': 1, 'required_sequence': ['detect_gap']},
|
| 51 |
+
'patient_id': 'P104',
|
| 52 |
+
'patient_events': ['admission', 'ct_scan_ordered', 'ct_scan_performed', 'diagnosis'],
|
| 53 |
+
'events': ['admission', 'ct_scan_ordered', 'ct_scan_performed', 'diagnosis'],
|
| 54 |
+
'expected_missing_steps': ['contrast_allergy_screen'],
|
| 55 |
+
'expected_risk': 'high',
|
| 56 |
+
'available_steps': ['contrast_allergy_screen', 'kidney_function_test', 'radiologist_review'],
|
| 57 |
+
'task_description': 'Find the missing safety step before this contrast CT scan.',
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
'case_id': 'cli_easy_005',
|
| 61 |
+
'completion_threshold': 0.80,
|
| 62 |
+
'max_steps': 4,
|
| 63 |
+
'done_conditions': {'min_actions': 1, 'required_sequence': ['detect_gap']},
|
| 64 |
+
'patient_id': 'P105',
|
| 65 |
+
'patient_events': ['admission', 'blood_transfusion_ordered', 'transfusion_started'],
|
| 66 |
+
'events': ['admission', 'blood_transfusion_ordered', 'transfusion_started'],
|
| 67 |
+
'expected_missing_steps': ['blood_type_crossmatch'],
|
| 68 |
+
'expected_risk': 'critical',
|
| 69 |
+
'available_steps': ['blood_type_crossmatch', 'consent_form', 'vital_signs_baseline'],
|
| 70 |
+
'task_description': 'Find the critical missing step before blood transfusion.',
|
| 71 |
+
},
|
| 72 |
+
],
|
| 73 |
+
'cli_medium': [
|
| 74 |
+
{
|
| 75 |
+
'case_id': 'cli_medium_001',
|
| 76 |
+
'completion_threshold': 0.75,
|
| 77 |
+
'max_steps': 6,
|
| 78 |
+
'done_conditions': {'min_actions': 2, 'required_sequence': ['detect_gap', 'rank_issues']},
|
| 79 |
+
'patient_id': 'P201',
|
| 80 |
+
'patient_events': ['admission', 'surgery_planned', 'insurance_denied', 'specialist_unavailable'],
|
| 81 |
+
'events': ['admission', 'surgery_planned', 'insurance_denied', 'specialist_unavailable'],
|
| 82 |
+
'expected_missing_steps': ['resolve_insurance', 'pre_op_consent', 'book_specialist'],
|
| 83 |
+
'expected_risk': 'critical',
|
| 84 |
+
'priority_order': ['resolve_insurance', 'pre_op_consent', 'book_specialist'],
|
| 85 |
+
'available_steps': ['resolve_insurance', 'pre_op_consent', 'book_specialist', 'schedule_surgery'],
|
| 86 |
+
'dependency_graph': {
|
| 87 |
+
'schedule_surgery': ['resolve_insurance', 'pre_op_consent', 'book_specialist'],
|
| 88 |
+
'pre_op_consent': [],
|
| 89 |
+
'book_specialist': [],
|
| 90 |
+
'resolve_insurance': [],
|
| 91 |
+
},
|
| 92 |
+
'task_description': 'Multiple steps are missing in this surgical patient workflow. Detect all gaps and rank them by clinical priority.',
|
| 93 |
+
},
|
| 94 |
+
{
|
| 95 |
+
'case_id': 'cli_medium_002',
|
| 96 |
+
'completion_threshold': 0.75,
|
| 97 |
+
'max_steps': 6,
|
| 98 |
+
'done_conditions': {'min_actions': 2, 'required_sequence': ['detect_gap', 'rank_issues']},
|
| 99 |
+
'patient_id': 'P202',
|
| 100 |
+
'patient_events': ['er_admission', 'triage_level_2', 'medication_given'],
|
| 101 |
+
'events': ['er_admission', 'triage_level_2', 'medication_given'],
|
| 102 |
+
'expected_missing_steps': ['allergy_check', 'attending_notification', 'vital_signs_check'],
|
| 103 |
+
'expected_risk': 'high',
|
| 104 |
+
'priority_order': ['allergy_check', 'vital_signs_check', 'attending_notification'],
|
| 105 |
+
'available_steps': ['allergy_check', 'attending_notification', 'vital_signs_check', 'lab_order'],
|
| 106 |
+
'dependency_graph': {
|
| 107 |
+
'allergy_check': [],
|
| 108 |
+
'vital_signs_check': [],
|
| 109 |
+
'attending_notification': [],
|
| 110 |
+
'lab_order': ['vital_signs_check'],
|
| 111 |
+
},
|
| 112 |
+
'task_description': 'Multiple safety steps were skipped in this ER case. Find and rank them.',
|
| 113 |
+
},
|
| 114 |
+
{
|
| 115 |
+
'case_id': 'cli_medium_003',
|
| 116 |
+
'completion_threshold': 0.75,
|
| 117 |
+
'max_steps': 6,
|
| 118 |
+
'done_conditions': {'min_actions': 2, 'required_sequence': ['detect_gap', 'rank_issues']},
|
| 119 |
+
'patient_id': 'P203',
|
| 120 |
+
'patient_events': ['admission', 'chemo_ordered', 'chemo_started', 'adverse_reaction'],
|
| 121 |
+
'events': ['admission', 'chemo_ordered', 'chemo_started', 'adverse_reaction'],
|
| 122 |
+
'expected_missing_steps': ['baseline_labs', 'oncologist_approval', 'dose_verification'],
|
| 123 |
+
'expected_risk': 'critical',
|
| 124 |
+
'priority_order': ['oncologist_approval', 'dose_verification', 'baseline_labs'],
|
| 125 |
+
'available_steps': ['baseline_labs', 'oncologist_approval', 'dose_verification', 'pharmacy_review'],
|
| 126 |
+
'dependency_graph': {
|
| 127 |
+
'oncologist_approval': [],
|
| 128 |
+
'dose_verification': ['oncologist_approval'],
|
| 129 |
+
'baseline_labs': [],
|
| 130 |
+
'pharmacy_review': ['dose_verification'],
|
| 131 |
+
},
|
| 132 |
+
'task_description': 'Critical chemotherapy workflow violations. Find all gaps and prioritize.',
|
| 133 |
+
},
|
| 134 |
+
],
|
| 135 |
+
'cli_hard': [
|
| 136 |
+
{
|
| 137 |
+
'case_id': 'cli_hard_001',
|
| 138 |
+
'completion_threshold': 0.70,
|
| 139 |
+
'max_steps': 6,
|
| 140 |
+
'done_conditions': {'min_actions': 3, 'required_sequence': ['detect_gap', 'rank_issues', 'order_steps']},
|
| 141 |
+
'patient_id': 'P301',
|
| 142 |
+
'patient_events': ['surgery_planned', 'insurance_denied', 'pre_op_test_skipped'],
|
| 143 |
+
'events': ['surgery_planned', 'insurance_denied', 'pre_op_test_skipped'],
|
| 144 |
+
'expected_missing_steps': ['resolve_insurance', 'complete_pre_op', 'book_specialist', 'schedule_surgery'],
|
| 145 |
+
'expected_risk': 'critical',
|
| 146 |
+
'priority_order': ['resolve_insurance', 'complete_pre_op', 'book_specialist', 'schedule_surgery'],
|
| 147 |
+
'dependency_graph': {
|
| 148 |
+
'schedule_surgery': ['resolve_insurance', 'complete_pre_op', 'book_specialist'],
|
| 149 |
+
'complete_pre_op': ['resolve_insurance'],
|
| 150 |
+
'book_specialist': [],
|
| 151 |
+
'resolve_insurance': [],
|
| 152 |
+
},
|
| 153 |
+
'required_steps': ['resolve_insurance', 'complete_pre_op', 'book_specialist', 'schedule_surgery'],
|
| 154 |
+
'available_steps': ['resolve_insurance', 'complete_pre_op', 'book_specialist', 'schedule_surgery'],
|
| 155 |
+
'task_description': 'A complex surgical patient has multiple workflow failures. Detect all gaps, rank by priority, and plan a dependency-ordered recovery sequence that respects prerequisite constraints.',
|
| 156 |
+
},
|
| 157 |
+
{
|
| 158 |
+
'case_id': 'cli_hard_002',
|
| 159 |
+
'completion_threshold': 0.70,
|
| 160 |
+
'max_steps': 6,
|
| 161 |
+
'done_conditions': {'min_actions': 3, 'required_sequence': ['detect_gap', 'rank_issues', 'order_steps']},
|
| 162 |
+
'patient_id': 'P302',
|
| 163 |
+
'patient_events': ['cardiac_event', 'icu_admission', 'multiple_failures_detected'],
|
| 164 |
+
'events': ['cardiac_event', 'icu_admission', 'multiple_failures_detected'],
|
| 165 |
+
'expected_missing_steps': ['stabilize_vitals', 'cardiology_consult', 'imaging_ordered', 'medication_review', 'family_notification'],
|
| 166 |
+
'expected_risk': 'critical',
|
| 167 |
+
'priority_order': ['stabilize_vitals', 'cardiology_consult', 'imaging_ordered', 'medication_review', 'family_notification'],
|
| 168 |
+
'dependency_graph': {
|
| 169 |
+
'family_notification': ['stabilize_vitals'],
|
| 170 |
+
'medication_review': ['cardiology_consult', 'imaging_ordered'],
|
| 171 |
+
'imaging_ordered': ['stabilize_vitals'],
|
| 172 |
+
'cardiology_consult': ['stabilize_vitals'],
|
| 173 |
+
'stabilize_vitals': [],
|
| 174 |
+
},
|
| 175 |
+
'required_steps': ['stabilize_vitals', 'cardiology_consult', 'imaging_ordered', 'medication_review', 'family_notification'],
|
| 176 |
+
'available_steps': ['stabilize_vitals', 'cardiology_consult', 'imaging_ordered', 'medication_review', 'family_notification'],
|
| 177 |
+
'task_description': 'Complex cardiac emergency recovery plan. Multiple dependency chains. Medication review needs both cardiology consult AND imaging. Respect ALL prerequisites.',
|
| 178 |
+
},
|
| 179 |
+
],
|
| 180 |
+
}
|
server/datasets/dependency_cases.py
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# server/datasets/dependency_cases.py
|
| 2 |
+
# Ground truth cases for PyTorch Migration Time-Machine tasks.
|
| 3 |
+
# Covers: deprecated API detection, version conflict resolution, graph-break fixing.
|
| 4 |
+
|
| 5 |
+
DEPENDENCY_CASES = {
|
| 6 |
+
'dep_easy': [
|
| 7 |
+
{
|
| 8 |
+
'case_id': 'dep_easy_001',
|
| 9 |
+
'task_subtype': 'flag',
|
| 10 |
+
'completion_threshold': 0.80,
|
| 11 |
+
'max_steps': 4,
|
| 12 |
+
'done_conditions': {'min_actions': 1, 'required_sequence': ['flag_outdated']},
|
| 13 |
+
'expected_outdated_packages': ['torch'],
|
| 14 |
+
'expected_deprecated_api': 'torch.autograd.Variable',
|
| 15 |
+
'replacement': 'plain tensor (remove Variable wrapper)',
|
| 16 |
+
'code_snippet': '''import torch
|
| 17 |
+
from torch.autograd import Variable
|
| 18 |
+
|
| 19 |
+
x = Variable(torch.randn(3, 4), requires_grad=True)
|
| 20 |
+
y = Variable(torch.randn(3, 4))
|
| 21 |
+
z = x + y''',
|
| 22 |
+
'task_description': 'Identify outdated PyTorch packages and deprecated APIs in this legacy training script.',
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
'case_id': 'dep_easy_002',
|
| 26 |
+
'task_subtype': 'flag',
|
| 27 |
+
'completion_threshold': 0.80,
|
| 28 |
+
'max_steps': 4,
|
| 29 |
+
'done_conditions': {'min_actions': 1, 'required_sequence': ['flag_outdated']},
|
| 30 |
+
'expected_outdated_packages': ['torch'],
|
| 31 |
+
'expected_deprecated_api': 'tensor.data.numpy()',
|
| 32 |
+
'replacement': 'tensor.detach().numpy()',
|
| 33 |
+
'code_snippet': '''import torch
|
| 34 |
+
|
| 35 |
+
model = torch.nn.Linear(10, 5)
|
| 36 |
+
x = torch.randn(1, 10)
|
| 37 |
+
output = model(x)
|
| 38 |
+
result = output.data.numpy() # deprecated''',
|
| 39 |
+
'task_description': 'Find deprecated tensor conversion API in this code.',
|
| 40 |
+
},
|
| 41 |
+
{
|
| 42 |
+
'case_id': 'dep_easy_003',
|
| 43 |
+
'task_subtype': 'flag',
|
| 44 |
+
'completion_threshold': 0.80,
|
| 45 |
+
'max_steps': 4,
|
| 46 |
+
'done_conditions': {'min_actions': 1, 'required_sequence': ['flag_outdated']},
|
| 47 |
+
'expected_outdated_packages': ['torch'],
|
| 48 |
+
'expected_deprecated_api': 'model.cuda()',
|
| 49 |
+
'replacement': 'model.to(device)',
|
| 50 |
+
'code_snippet': '''import torch
|
| 51 |
+
|
| 52 |
+
model = torch.nn.Sequential(
|
| 53 |
+
torch.nn.Linear(784, 128),
|
| 54 |
+
torch.nn.ReLU(),
|
| 55 |
+
torch.nn.Linear(128, 10)
|
| 56 |
+
)
|
| 57 |
+
model.cuda() # deprecated device placement
|
| 58 |
+
x = torch.randn(1, 784).cuda()''',
|
| 59 |
+
'task_description': 'Detect deprecated device placement API in this model code.',
|
| 60 |
+
},
|
| 61 |
+
{
|
| 62 |
+
'case_id': 'dep_easy_004',
|
| 63 |
+
'task_subtype': 'flag',
|
| 64 |
+
'completion_threshold': 0.80,
|
| 65 |
+
'max_steps': 4,
|
| 66 |
+
'done_conditions': {'min_actions': 1, 'required_sequence': ['flag_outdated']},
|
| 67 |
+
'expected_outdated_packages': ['torch'],
|
| 68 |
+
'expected_deprecated_api': 'torch.onnx.export',
|
| 69 |
+
'replacement': 'torch.onnx.dynamo_export',
|
| 70 |
+
'code_snippet': '''import torch
|
| 71 |
+
|
| 72 |
+
model = torch.nn.Linear(10, 5)
|
| 73 |
+
dummy = torch.randn(1, 10)
|
| 74 |
+
torch.onnx.export(model, dummy, "model.onnx",
|
| 75 |
+
opset_version=11)''',
|
| 76 |
+
'task_description': 'Find the deprecated ONNX export API in this code.',
|
| 77 |
+
},
|
| 78 |
+
{
|
| 79 |
+
'case_id': 'dep_easy_005',
|
| 80 |
+
'task_subtype': 'flag',
|
| 81 |
+
'completion_threshold': 0.80,
|
| 82 |
+
'max_steps': 4,
|
| 83 |
+
'done_conditions': {'min_actions': 1, 'required_sequence': ['flag_outdated']},
|
| 84 |
+
'expected_outdated_packages': ['torch'],
|
| 85 |
+
'expected_deprecated_api': 'torch.nn.DataParallel',
|
| 86 |
+
'replacement': 'torch.nn.parallel.DistributedDataParallel or FSDP',
|
| 87 |
+
'code_snippet': '''import torch
|
| 88 |
+
import torch.nn as nn
|
| 89 |
+
|
| 90 |
+
model = nn.Linear(100, 10)
|
| 91 |
+
model = nn.DataParallel(model) # deprecated
|
| 92 |
+
model.cuda()''',
|
| 93 |
+
'task_description': 'Find deprecated parallelism API in this training code.',
|
| 94 |
+
},
|
| 95 |
+
],
|
| 96 |
+
'dep_medium': [
|
| 97 |
+
{
|
| 98 |
+
'case_id': 'dep_medium_001',
|
| 99 |
+
'task_subtype': 'resolve',
|
| 100 |
+
'completion_threshold': 0.75,
|
| 101 |
+
'max_steps': 6,
|
| 102 |
+
'done_conditions': {'min_actions': 1, 'required_sequence': ['resolve_conflict']},
|
| 103 |
+
'conflict_packages': ['torch', 'numpy'],
|
| 104 |
+
'compatibility_matrix': {
|
| 105 |
+
'torch': {
|
| 106 |
+
'2.1.0': {'numpy': '>=1.24,<2.0'},
|
| 107 |
+
'2.0.0': {'numpy': '>=1.22,<1.26'},
|
| 108 |
+
'1.13.0': {'numpy': '>=1.19,<1.25'},
|
| 109 |
+
},
|
| 110 |
+
'numpy': {
|
| 111 |
+
'1.26.0': {},
|
| 112 |
+
'1.24.0': {},
|
| 113 |
+
'1.22.0': {},
|
| 114 |
+
'1.19.0': {},
|
| 115 |
+
'1.16.0': {},
|
| 116 |
+
},
|
| 117 |
+
},
|
| 118 |
+
'requirements': {'torch': '1.9.0', 'numpy': '1.16.0'},
|
| 119 |
+
'code_snippet': '''# requirements.txt
|
| 120 |
+
torch==1.9.0
|
| 121 |
+
numpy==1.16.0
|
| 122 |
+
torchvision==0.10.0''',
|
| 123 |
+
'task_description': 'Resolve the version conflict between torch and numpy. Find compatible versions using the compatibility matrix.',
|
| 124 |
+
},
|
| 125 |
+
{
|
| 126 |
+
'case_id': 'dep_medium_002',
|
| 127 |
+
'task_subtype': 'resolve',
|
| 128 |
+
'completion_threshold': 0.75,
|
| 129 |
+
'max_steps': 6,
|
| 130 |
+
'done_conditions': {'min_actions': 1, 'required_sequence': ['resolve_conflict']},
|
| 131 |
+
'conflict_packages': ['torch', 'numpy', 'torchvision'],
|
| 132 |
+
'compatibility_matrix': {
|
| 133 |
+
'torch': {
|
| 134 |
+
'2.2.0': {'numpy': '>=1.24,<2.0', 'torchvision': '>=0.17'},
|
| 135 |
+
'2.1.0': {'numpy': '>=1.24,<2.0', 'torchvision': '>=0.16'},
|
| 136 |
+
'2.0.0': {'numpy': '>=1.22,<1.26', 'torchvision': '>=0.15'},
|
| 137 |
+
},
|
| 138 |
+
'numpy': {
|
| 139 |
+
'1.26.0': {},
|
| 140 |
+
'1.24.0': {},
|
| 141 |
+
'1.22.0': {},
|
| 142 |
+
},
|
| 143 |
+
'torchvision': {
|
| 144 |
+
'0.17.0': {'torch': '>=2.2'},
|
| 145 |
+
'0.16.0': {'torch': '>=2.1'},
|
| 146 |
+
'0.15.0': {'torch': '>=2.0'},
|
| 147 |
+
},
|
| 148 |
+
},
|
| 149 |
+
'requirements': {'torch': '1.12.0', 'numpy': '1.21.0', 'torchvision': '0.13.0'},
|
| 150 |
+
'code_snippet': '''# requirements.txt
|
| 151 |
+
torch==1.12.0
|
| 152 |
+
numpy==1.21.0
|
| 153 |
+
torchvision==0.13.0
|
| 154 |
+
# CUDA 11.7''',
|
| 155 |
+
'task_description': 'Resolve three-way conflict between PyTorch, NumPy, and TorchVision.',
|
| 156 |
+
},
|
| 157 |
+
{
|
| 158 |
+
'case_id': 'dep_medium_003',
|
| 159 |
+
'task_subtype': 'resolve',
|
| 160 |
+
'completion_threshold': 0.75,
|
| 161 |
+
'max_steps': 6,
|
| 162 |
+
'done_conditions': {'min_actions': 1, 'required_sequence': ['resolve_conflict']},
|
| 163 |
+
'conflict_packages': ['torch', 'transformers'],
|
| 164 |
+
'compatibility_matrix': {
|
| 165 |
+
'torch': {
|
| 166 |
+
'2.1.0': {'transformers': '>=4.35'},
|
| 167 |
+
'2.0.0': {'transformers': '>=4.30'},
|
| 168 |
+
},
|
| 169 |
+
'transformers': {
|
| 170 |
+
'4.37.0': {'torch': '>=2.0'},
|
| 171 |
+
'4.35.0': {'torch': '>=2.0'},
|
| 172 |
+
'4.30.0': {'torch': '>=1.13'},
|
| 173 |
+
},
|
| 174 |
+
},
|
| 175 |
+
'requirements': {'torch': '1.11.0', 'transformers': '4.20.0'},
|
| 176 |
+
'code_snippet': '''# requirements.txt
|
| 177 |
+
torch==1.11.0
|
| 178 |
+
transformers==4.20.0''',
|
| 179 |
+
'task_description': 'Resolve conflict between PyTorch and Transformers library versions.',
|
| 180 |
+
},
|
| 181 |
+
],
|
| 182 |
+
'dep_hard': [
|
| 183 |
+
{
|
| 184 |
+
'case_id': 'dep_hard_001',
|
| 185 |
+
'task_subtype': 'migrate',
|
| 186 |
+
'completion_threshold': 0.70,
|
| 187 |
+
'max_steps': 8,
|
| 188 |
+
'done_conditions': {'min_actions': 2, 'required_sequence': ['migrate_api']},
|
| 189 |
+
'graph_breaks': ['break_001', 'break_002', 'break_003'],
|
| 190 |
+
'checklist_dependency_graph': {
|
| 191 |
+
'break_003': ['break_001', 'break_002'],
|
| 192 |
+
'break_002': ['break_001'],
|
| 193 |
+
'break_001': [],
|
| 194 |
+
},
|
| 195 |
+
'correct_fix_map': {
|
| 196 |
+
'break_001': 'torch.where',
|
| 197 |
+
'break_002': 'tensor.shape[0]',
|
| 198 |
+
'break_003': '.detach().numpy()',
|
| 199 |
+
},
|
| 200 |
+
'code_snippet': '''import torch
|
| 201 |
+
|
| 202 |
+
@torch.compile
|
| 203 |
+
def forward(x):
|
| 204 |
+
# break_001: data-dependent control flow
|
| 205 |
+
if x.item() > 0.5:
|
| 206 |
+
x = x * 2
|
| 207 |
+
|
| 208 |
+
# break_002: Python builtin on tensor
|
| 209 |
+
batch_size = len(x)
|
| 210 |
+
|
| 211 |
+
# break_003: numpy conversion inside compile
|
| 212 |
+
result = x.numpy()
|
| 213 |
+
return result''',
|
| 214 |
+
'break_descriptions': [
|
| 215 |
+
'break_001: line 6 — data-dependent control flow: if x.item() > 0.5',
|
| 216 |
+
'break_002: line 9 — Python builtin on tensor: len(x)',
|
| 217 |
+
'break_003: line 12 — numpy inside compiled function: x.numpy()',
|
| 218 |
+
],
|
| 219 |
+
'graph_break_report': [
|
| 220 |
+
'break_001: line 6 — data-dependent control flow: if x.item() > 0.5',
|
| 221 |
+
'break_002: line 9 — Python builtin on tensor: len(x)',
|
| 222 |
+
'break_003: line 12 — numpy inside compiled function: x.numpy()',
|
| 223 |
+
],
|
| 224 |
+
'task_description': 'This PyTorch model uses torch.compile but has multiple graph-break patterns. Fix them in dependency order.',
|
| 225 |
+
},
|
| 226 |
+
{
|
| 227 |
+
'case_id': 'dep_hard_002',
|
| 228 |
+
'task_subtype': 'migrate',
|
| 229 |
+
'completion_threshold': 0.70,
|
| 230 |
+
'max_steps': 8,
|
| 231 |
+
'done_conditions': {'min_actions': 2, 'required_sequence': ['migrate_api']},
|
| 232 |
+
'graph_breaks': ['break_a', 'break_b', 'break_c', 'break_d'],
|
| 233 |
+
'checklist_dependency_graph': {
|
| 234 |
+
'break_d': ['break_b', 'break_c'],
|
| 235 |
+
'break_c': ['break_a'],
|
| 236 |
+
'break_b': ['break_a'],
|
| 237 |
+
'break_a': [],
|
| 238 |
+
},
|
| 239 |
+
'correct_fix_map': {
|
| 240 |
+
'break_a': 'torch.where',
|
| 241 |
+
'break_b': 'tensor.shape[0]',
|
| 242 |
+
'break_c': 'torch.tensor',
|
| 243 |
+
'break_d': '.detach()',
|
| 244 |
+
},
|
| 245 |
+
'code_snippet': '''import torch
|
| 246 |
+
|
| 247 |
+
@torch.compile(fullgraph=True)
|
| 248 |
+
def training_step(model, x, labels):
|
| 249 |
+
# break_a: data-dependent branch
|
| 250 |
+
if x.max().item() > 1.0:
|
| 251 |
+
x = x / x.max()
|
| 252 |
+
|
| 253 |
+
# break_b: Python len() on tensor
|
| 254 |
+
n_samples = len(x)
|
| 255 |
+
|
| 256 |
+
# break_c: Python list to tensor inside compile
|
| 257 |
+
weights = torch.FloatTensor([1.0, 2.0, 3.0])
|
| 258 |
+
|
| 259 |
+
# break_d: in-place operation on leaf tensor
|
| 260 |
+
x += 0.1 # in-place modification
|
| 261 |
+
|
| 262 |
+
output = model(x)
|
| 263 |
+
loss = torch.nn.functional.cross_entropy(output, labels)
|
| 264 |
+
return loss''',
|
| 265 |
+
'break_descriptions': [
|
| 266 |
+
'break_a: line 6 — data-dependent: if x.max().item() > 1.0',
|
| 267 |
+
'break_b: line 10 — Python builtin: len(x)',
|
| 268 |
+
'break_c: line 13 — legacy constructor: torch.FloatTensor()',
|
| 269 |
+
'break_d: line 16 — in-place op on leaf: x += 0.1',
|
| 270 |
+
],
|
| 271 |
+
'graph_break_report': [
|
| 272 |
+
'break_a: line 6 — data-dependent: if x.max().item() > 1.0',
|
| 273 |
+
'break_b: line 10 — Python builtin: len(x)',
|
| 274 |
+
'break_c: line 13 — legacy constructor: torch.FloatTensor()',
|
| 275 |
+
'break_d: line 16 — in-place op on leaf: x += 0.1',
|
| 276 |
+
],
|
| 277 |
+
'task_description': 'Fix all 4 graph-break patterns in this compiled training step. Dependencies must be resolved in order.',
|
| 278 |
+
},
|
| 279 |
+
],
|
| 280 |
+
}
|
server/datasets/security_cases.py
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# server/datasets/security_cases.py
|
| 2 |
+
# Ground truth cases for MCP Security Sandbox tasks.
|
| 3 |
+
# Graders compare agent answers against these cases.
|
| 4 |
+
|
| 5 |
+
SECURITY_CASES = {
|
| 6 |
+
'sec_easy': [
|
| 7 |
+
{
|
| 8 |
+
'case_id': 'sec_easy_001',
|
| 9 |
+
'task_subtype': 'single',
|
| 10 |
+
'completion_threshold': 0.80,
|
| 11 |
+
'max_steps': 4,
|
| 12 |
+
'done_conditions': {'min_actions': 1, 'required_sequence': ['identify_vulnerability']},
|
| 13 |
+
'expected_vuln_type': 'sql_injection',
|
| 14 |
+
'cvss_range': [7.5, 9.8],
|
| 15 |
+
'expected_severity': 'critical',
|
| 16 |
+
'required_fix_tokens': ['?', 'execute', 'param'],
|
| 17 |
+
'must_preserve_identifier': 'execute_query',
|
| 18 |
+
'current_feedback_keywords': ['sql', 'injection'],
|
| 19 |
+
'original_vuln_pattern': "query + user_input",
|
| 20 |
+
'tool_call': 'db.execute("SELECT * FROM users WHERE name = \'" + user_input + "\'")',
|
| 21 |
+
'task_description': 'Identify the SQL injection vulnerability in this MCP database tool call and propose a fix.',
|
| 22 |
+
},
|
| 23 |
+
{
|
| 24 |
+
'case_id': 'sec_easy_002',
|
| 25 |
+
'task_subtype': 'single',
|
| 26 |
+
'completion_threshold': 0.80,
|
| 27 |
+
'max_steps': 4,
|
| 28 |
+
'done_conditions': {'min_actions': 1, 'required_sequence': ['identify_vulnerability']},
|
| 29 |
+
'expected_vuln_type': 'xss',
|
| 30 |
+
'cvss_range': [4.0, 6.5],
|
| 31 |
+
'expected_severity': 'medium',
|
| 32 |
+
'required_fix_tokens': ['escape', 'html'],
|
| 33 |
+
'must_preserve_identifier': 'render_html',
|
| 34 |
+
'current_feedback_keywords': ['xss', 'script'],
|
| 35 |
+
'original_vuln_pattern': '<script>',
|
| 36 |
+
'tool_call': 'render_html(f"<div>{user_comment}</div>")',
|
| 37 |
+
'task_description': 'Scan this HTML rendering call for XSS vulnerability.',
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
'case_id': 'sec_easy_003',
|
| 41 |
+
'task_subtype': 'single',
|
| 42 |
+
'completion_threshold': 0.80,
|
| 43 |
+
'max_steps': 4,
|
| 44 |
+
'done_conditions': {'min_actions': 1, 'required_sequence': ['identify_vulnerability']},
|
| 45 |
+
'expected_vuln_type': 'hardcoded_secret',
|
| 46 |
+
'cvss_range': [5.0, 7.5],
|
| 47 |
+
'expected_severity': 'high',
|
| 48 |
+
'required_fix_tokens': ['env', 'os'],
|
| 49 |
+
'must_preserve_identifier': 'connect_db',
|
| 50 |
+
'current_feedback_keywords': ['hardcoded', 'password'],
|
| 51 |
+
'original_vuln_pattern': 'password="admin123"',
|
| 52 |
+
'tool_call': 'connect_db(host="prod-db.internal", password="admin123", user="root")',
|
| 53 |
+
'task_description': 'Scan this database connection call for hardcoded credentials.',
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
'case_id': 'sec_easy_004',
|
| 57 |
+
'task_subtype': 'single',
|
| 58 |
+
'completion_threshold': 0.80,
|
| 59 |
+
'max_steps': 4,
|
| 60 |
+
'done_conditions': {'min_actions': 1, 'required_sequence': ['identify_vulnerability']},
|
| 61 |
+
'expected_vuln_type': 'path_traversal',
|
| 62 |
+
'cvss_range': [6.0, 8.5],
|
| 63 |
+
'expected_severity': 'high',
|
| 64 |
+
'required_fix_tokens': ['basename', 'path'],
|
| 65 |
+
'must_preserve_identifier': 'read_file',
|
| 66 |
+
'current_feedback_keywords': ['path', 'traversal', '..'],
|
| 67 |
+
'original_vuln_pattern': '../../../etc/passwd',
|
| 68 |
+
'tool_call': 'read_file(filepath=user_input) # user sends "../../etc/passwd"',
|
| 69 |
+
'task_description': 'Scan this file read call for path traversal vulnerability.',
|
| 70 |
+
},
|
| 71 |
+
{
|
| 72 |
+
'case_id': 'sec_easy_005',
|
| 73 |
+
'task_subtype': 'single',
|
| 74 |
+
'completion_threshold': 0.80,
|
| 75 |
+
'max_steps': 4,
|
| 76 |
+
'done_conditions': {'min_actions': 1, 'required_sequence': ['identify_vulnerability']},
|
| 77 |
+
'expected_vuln_type': 'missing_auth',
|
| 78 |
+
'cvss_range': [7.0, 9.0],
|
| 79 |
+
'expected_severity': 'critical',
|
| 80 |
+
'required_fix_tokens': ['auth', 'token', 'verify'],
|
| 81 |
+
'must_preserve_identifier': 'delete_user',
|
| 82 |
+
'current_feedback_keywords': ['authentication', 'missing'],
|
| 83 |
+
'original_vuln_pattern': 'no auth check',
|
| 84 |
+
'tool_call': 'delete_user(user_id=request.params["id"]) # no authentication check',
|
| 85 |
+
'task_description': 'Scan this admin action call for missing authentication.',
|
| 86 |
+
},
|
| 87 |
+
],
|
| 88 |
+
'sec_medium': [
|
| 89 |
+
{
|
| 90 |
+
'case_id': 'sec_medium_001',
|
| 91 |
+
'task_subtype': 'multi',
|
| 92 |
+
'completion_threshold': 0.75,
|
| 93 |
+
'max_steps': 6,
|
| 94 |
+
'done_conditions': {'min_actions': 3, 'required_sequence': ['identify_vulnerability', 'propose_fix', 'revise_fix']},
|
| 95 |
+
'expected_vuln_type': 'sql_injection',
|
| 96 |
+
'cvss_range': [7.5, 9.8],
|
| 97 |
+
'expected_severity': 'critical',
|
| 98 |
+
'required_fix_tokens': ['?', 'execute', 'param'],
|
| 99 |
+
'must_preserve_identifier': 'search_products',
|
| 100 |
+
'current_feedback_keywords': ['sql', 'union', 'injection'],
|
| 101 |
+
'original_vuln_pattern': 'query + ',
|
| 102 |
+
'reviewer_feedback': 'Your fix prevents basic injection but UNION-based attacks are still possible. Use parameterized queries with bound parameters.',
|
| 103 |
+
'additional_vulns': [
|
| 104 |
+
{'vuln_type': 'xss', 'severity': 'medium', 'line': 15},
|
| 105 |
+
{'vuln_type': 'missing_auth', 'severity': 'high', 'line': 3},
|
| 106 |
+
],
|
| 107 |
+
'tool_call': '''def search_products(query, user_id):
|
| 108 |
+
# No auth check on line 3
|
| 109 |
+
results = db.execute("SELECT * FROM products WHERE name LIKE '%" + query + "%'")
|
| 110 |
+
return f"<div>{results}</div>" # XSS on line 15''',
|
| 111 |
+
'task_description': 'This MCP tool has multiple security issues. Identify the most critical vulnerability, propose a fix, and revise based on reviewer feedback.',
|
| 112 |
+
},
|
| 113 |
+
{
|
| 114 |
+
'case_id': 'sec_medium_002',
|
| 115 |
+
'task_subtype': 'multi',
|
| 116 |
+
'completion_threshold': 0.75,
|
| 117 |
+
'max_steps': 6,
|
| 118 |
+
'done_conditions': {'min_actions': 3, 'required_sequence': ['identify_vulnerability', 'propose_fix', 'revise_fix']},
|
| 119 |
+
'expected_vuln_type': 'ssrf',
|
| 120 |
+
'cvss_range': [6.5, 9.0],
|
| 121 |
+
'expected_severity': 'high',
|
| 122 |
+
'required_fix_tokens': ['allowlist', 'url'],
|
| 123 |
+
'must_preserve_identifier': 'fetch_url',
|
| 124 |
+
'current_feedback_keywords': ['ssrf', 'internal', 'url'],
|
| 125 |
+
'original_vuln_pattern': 'requests.get(user_url)',
|
| 126 |
+
'reviewer_feedback': 'Fix addresses external URLs but internal network requests (169.254.x.x, localhost) are still allowed. Add an allowlist or block private IP ranges.',
|
| 127 |
+
'additional_vulns': [
|
| 128 |
+
{'vuln_type': 'hardcoded_secret', 'severity': 'medium', 'line': 2},
|
| 129 |
+
],
|
| 130 |
+
'tool_call': '''def fetch_url(user_url):
|
| 131 |
+
API_KEY = "sk-secret-key-12345" # hardcoded secret
|
| 132 |
+
resp = requests.get(user_url, headers={"Authorization": API_KEY})
|
| 133 |
+
return resp.text''',
|
| 134 |
+
'task_description': 'Scan this URL fetcher for all vulnerabilities including SSRF and credential exposure.',
|
| 135 |
+
},
|
| 136 |
+
{
|
| 137 |
+
'case_id': 'sec_medium_003',
|
| 138 |
+
'task_subtype': 'multi',
|
| 139 |
+
'completion_threshold': 0.75,
|
| 140 |
+
'max_steps': 6,
|
| 141 |
+
'done_conditions': {'min_actions': 3, 'required_sequence': ['identify_vulnerability', 'propose_fix', 'revise_fix']},
|
| 142 |
+
'expected_vuln_type': 'idor',
|
| 143 |
+
'cvss_range': [5.5, 8.0],
|
| 144 |
+
'expected_severity': 'high',
|
| 145 |
+
'required_fix_tokens': ['user_id', 'owner'],
|
| 146 |
+
'must_preserve_identifier': 'get_document',
|
| 147 |
+
'current_feedback_keywords': ['idor', 'authorization', 'object'],
|
| 148 |
+
'original_vuln_pattern': 'doc_id from request',
|
| 149 |
+
'reviewer_feedback': 'Fix checks user ownership but does not validate the requesting user session. Add session-based auth check.',
|
| 150 |
+
'additional_vulns': [
|
| 151 |
+
{'vuln_type': 'rate_limit_missing', 'severity': 'medium', 'line': 1},
|
| 152 |
+
],
|
| 153 |
+
'tool_call': '''@app.get("/documents/{doc_id}") # no rate limiting
|
| 154 |
+
def get_document(doc_id: int, request: Request):
|
| 155 |
+
# No check if requesting user owns this document
|
| 156 |
+
return db.documents.find_one({"_id": doc_id})''',
|
| 157 |
+
'task_description': 'Scan this document access endpoint for authorization and rate limiting issues.',
|
| 158 |
+
},
|
| 159 |
+
],
|
| 160 |
+
'sec_hard': [
|
| 161 |
+
{
|
| 162 |
+
'case_id': 'sec_hard_001',
|
| 163 |
+
'task_subtype': 'adversarial',
|
| 164 |
+
'completion_threshold': 0.70,
|
| 165 |
+
'max_steps': 8,
|
| 166 |
+
'done_conditions': {'min_actions': 3, 'required_sequence': ['identify_vulnerability', 'propose_fix', 'revise_fix']},
|
| 167 |
+
'expected_vuln_type': 'sql_injection',
|
| 168 |
+
'cvss_range': [8.0, 10.0],
|
| 169 |
+
'expected_severity': 'critical',
|
| 170 |
+
'required_fix_tokens': ['?', 'execute', 'param'],
|
| 171 |
+
'must_preserve_identifier': 'send_email',
|
| 172 |
+
'current_feedback_keywords': ['still vulnerable', 'sql still present', 'parameterize'],
|
| 173 |
+
'original_vuln_pattern': "query + user_input",
|
| 174 |
+
'reviewer_feedback': 'Fix still allows SQL injection through the search parameter. Use parameterized queries.',
|
| 175 |
+
'reviewer_feedback_sequence': [
|
| 176 |
+
'Fix still allows SQL injection through the search parameter. Use parameterized queries.',
|
| 177 |
+
'Better, but concatenation on line 5 still vulnerable. Also add input validation.',
|
| 178 |
+
],
|
| 179 |
+
'tool_call': '''def send_email(recipient, subject, body):
|
| 180 |
+
query = "INSERT INTO emails (to_addr, subject, body) VALUES ('" + recipient + "', '" + subject + "', '" + body + "')"
|
| 181 |
+
db.execute(query)
|
| 182 |
+
log(f"Email sent to {recipient}")
|
| 183 |
+
return {"status": "sent", "to": recipient}''',
|
| 184 |
+
'task_description': 'Rewrite this email tool call to remove SQL injection while preserving ALL business logic. A reviewer will evaluate your fix.',
|
| 185 |
+
},
|
| 186 |
+
{
|
| 187 |
+
'case_id': 'sec_hard_002',
|
| 188 |
+
'task_subtype': 'adversarial',
|
| 189 |
+
'completion_threshold': 0.70,
|
| 190 |
+
'max_steps': 8,
|
| 191 |
+
'done_conditions': {'min_actions': 3, 'required_sequence': ['identify_vulnerability', 'propose_fix', 'revise_fix']},
|
| 192 |
+
'expected_vuln_type': 'jwt_misuse',
|
| 193 |
+
'cvss_range': [7.5, 9.5],
|
| 194 |
+
'expected_severity': 'critical',
|
| 195 |
+
'required_fix_tokens': ['verify', 'algorithm', 'HS256', 'secret'],
|
| 196 |
+
'must_preserve_identifier': 'authenticate_user',
|
| 197 |
+
'current_feedback_keywords': ['jwt', 'algorithm', 'none attack', 'verify signature'],
|
| 198 |
+
'original_vuln_pattern': 'algorithms=["none"]',
|
| 199 |
+
'reviewer_feedback': 'Fix does not prevent algorithm=none attack. Explicitly set allowed algorithms.',
|
| 200 |
+
'reviewer_feedback_sequence': [
|
| 201 |
+
'Fix does not prevent algorithm=none attack. Explicitly set allowed algorithms.',
|
| 202 |
+
'Algorithm is set but secret key is still derived from user input. Use server secret.',
|
| 203 |
+
],
|
| 204 |
+
'tool_call': '''def authenticate_user(token):
|
| 205 |
+
payload = jwt.decode(token, options={"verify_signature": False})
|
| 206 |
+
user_id = payload.get("user_id")
|
| 207 |
+
return get_user(user_id)''',
|
| 208 |
+
'task_description': 'Rewrite this JWT authentication to prevent algorithm confusion attacks while preserving user lookup logic.',
|
| 209 |
+
},
|
| 210 |
+
],
|
| 211 |
+
}
|
server/debug_panel.html
ADDED
|
@@ -0,0 +1,1196 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>OpenEnv Debug Panel — Multi-Agent Ecosystem</title>
|
| 7 |
+
<link rel="preconnect" href="https://fonts.googleapis.com">
|
| 8 |
+
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&family=JetBrains+Mono:wght@400;600&display=swap" rel="stylesheet">
|
| 9 |
+
<style>
|
| 10 |
+
*{box-sizing:border-box;margin:0;padding:0}
|
| 11 |
+
:root{
|
| 12 |
+
--bg:#0d1017;--surface:#151822;--surface2:#1c2030;--border:#262d40;
|
| 13 |
+
--blue:#4f8ef7;--green:#22c55e;--amber:#f59e0b;--red:#ef4444;--purple:#a855f7;--cyan:#22d3ee;
|
| 14 |
+
--text:#e2e8f0;--muted:#6b7a94;--mono:'JetBrains Mono','Fira Code',monospace;
|
| 15 |
+
}
|
| 16 |
+
body{background:var(--bg);color:var(--text);font-family:'Inter','Segoe UI',sans-serif;font-size:14px;height:100vh;overflow:hidden}
|
| 17 |
+
|
| 18 |
+
/* ── Header ── */
|
| 19 |
+
.header{background:linear-gradient(135deg,#131828 0%,#1a2040 100%);border-bottom:1px solid var(--border);padding:12px 20px;display:flex;align-items:center;gap:14px;flex-shrink:0}
|
| 20 |
+
.header-logo{display:flex;align-items:center;gap:10px}
|
| 21 |
+
.logo-dot{width:10px;height:10px;border-radius:50%;animation:pulse 2s infinite}
|
| 22 |
+
.logo-dot.green{background:var(--green);box-shadow:0 0 8px var(--green)}
|
| 23 |
+
.logo-dot.err{background:var(--red);box-shadow:0 0 8px var(--red)}
|
| 24 |
+
@keyframes pulse{0%,100%{opacity:1}50%{opacity:.5}}
|
| 25 |
+
.header h1{font-size:16px;font-weight:700;color:#fff;white-space:nowrap}
|
| 26 |
+
.badge{padding:3px 10px;border-radius:99px;font-size:10px;font-weight:600;background:#1e3a5f;color:var(--blue);border:1px solid #2563eb33}
|
| 27 |
+
|
| 28 |
+
/* ── Full Layout ── */
|
| 29 |
+
.layout{display:grid;grid-template-columns:280px 1fr;height:calc(100vh - 50px)}
|
| 30 |
+
.sidebar{background:var(--surface);border-right:1px solid var(--border);overflow-y:auto;padding:12px;display:flex;flex-direction:column;gap:10px}
|
| 31 |
+
.main{display:flex;flex-direction:column;overflow:hidden;min-height:0}
|
| 32 |
+
|
| 33 |
+
/* ── Cards ── */
|
| 34 |
+
.card{background:var(--surface2);border:1px solid var(--border);border-radius:8px;overflow:hidden}
|
| 35 |
+
.card-hdr{padding:8px 12px;border-bottom:1px solid var(--border);font-size:11px;font-weight:600;color:var(--muted);text-transform:uppercase;letter-spacing:.04em;display:flex;align-items:center;gap:6px;background:var(--surface)}
|
| 36 |
+
.card-body{padding:10px}
|
| 37 |
+
|
| 38 |
+
/* ── Domain tabs ── */
|
| 39 |
+
.domain-tabs{display:flex;gap:3px;background:var(--bg);border-radius:6px;padding:3px}
|
| 40 |
+
.domain-tab{flex:1;padding:6px 0;border:none;border-radius:5px;cursor:pointer;font-size:11px;font-weight:600;color:var(--muted);background:transparent;transition:all .2s}
|
| 41 |
+
.domain-tab.active{color:#fff}
|
| 42 |
+
.domain-tab[data-domain="security"].active{background:#1e1a2e;color:var(--purple);box-shadow:0 0 0 1px #a855f744}
|
| 43 |
+
.domain-tab[data-domain="pytorch"].active{background:#1a2a1a;color:var(--green);box-shadow:0 0 0 1px #22c55e44}
|
| 44 |
+
.domain-tab[data-domain="clinical"].active{background:#1a2030;color:var(--cyan);box-shadow:0 0 0 1px #22d3ee44}
|
| 45 |
+
|
| 46 |
+
/* ── Task list ── */
|
| 47 |
+
.task-list{display:flex;flex-direction:column;gap:3px}
|
| 48 |
+
.task-btn{padding:7px 10px;border:1px solid var(--border);border-radius:6px;background:transparent;color:var(--text);cursor:pointer;text-align:left;display:flex;align-items:center;gap:8px;transition:all .15s;font-size:12px}
|
| 49 |
+
.task-btn:hover{border-color:var(--blue);background:#1e254033}
|
| 50 |
+
.task-btn.active{border-color:var(--blue);background:#1e2540;color:#fff}
|
| 51 |
+
.task-btn .diff{font-size:9px;font-weight:700;padding:2px 7px;border-radius:99px;margin-left:auto}
|
| 52 |
+
.diff-easy{background:#14532d33;color:var(--green);border:1px solid #22c55e44}
|
| 53 |
+
.diff-medium{background:#78350f33;color:var(--amber);border:1px solid #f59e0b44}
|
| 54 |
+
.diff-hard{background:#7f1d1d33;color:var(--red);border:1px solid #ef444444}
|
| 55 |
+
|
| 56 |
+
/* ── Form elements ── */
|
| 57 |
+
label{display:block;font-size:10px;color:var(--muted);font-weight:600;text-transform:uppercase;letter-spacing:.04em;margin-bottom:4px}
|
| 58 |
+
input,select,textarea{width:100%;background:var(--bg);border:1px solid var(--border);border-radius:5px;padding:7px 9px;color:var(--text);font-size:12px;font-family:inherit;outline:none;transition:border .15s}
|
| 59 |
+
input:focus,select:focus,textarea:focus{border-color:var(--blue)}
|
| 60 |
+
textarea{resize:vertical;font-family:var(--mono);font-size:11px;min-height:60px}
|
| 61 |
+
.field{margin-bottom:8px}
|
| 62 |
+
|
| 63 |
+
/* ── Buttons ── */
|
| 64 |
+
.btn{padding:7px 14px;border:none;border-radius:6px;cursor:pointer;font-size:12px;font-weight:600;transition:all .15s;display:inline-flex;align-items:center;gap:5px}
|
| 65 |
+
.btn-primary{background:var(--blue);color:#fff}
|
| 66 |
+
.btn-primary:hover{background:#3b7de8}
|
| 67 |
+
.btn-success{background:#166534;color:var(--green);border:1px solid #22c55e44}
|
| 68 |
+
.btn-success:hover{background:#14532d}
|
| 69 |
+
.btn-danger{background:#7f1d1d;color:var(--red);border:1px solid #ef444444}
|
| 70 |
+
.btn-ghost{background:transparent;color:var(--muted);border:1px solid var(--border);font-size:11px}
|
| 71 |
+
.btn-ghost:hover{color:var(--text);border-color:var(--text)}
|
| 72 |
+
.btn:disabled{opacity:.4;cursor:not-allowed}
|
| 73 |
+
|
| 74 |
+
/* ── Top bar ── */
|
| 75 |
+
.main-topbar{padding:8px 16px;border-bottom:1px solid var(--border);display:flex;align-items:center;gap:10px;flex-wrap:wrap;background:var(--surface);flex-shrink:0}
|
| 76 |
+
.info-chip{background:var(--bg);border:1px solid var(--border);border-radius:5px;padding:4px 8px;font-size:10px;white-space:nowrap}
|
| 77 |
+
.info-chip span{color:var(--muted);margin-right:3px}
|
| 78 |
+
.info-chip strong{color:var(--text)}
|
| 79 |
+
|
| 80 |
+
/* ── Main content: 3 rows ── */
|
| 81 |
+
.content-area{display:flex;flex-direction:column;flex:1;overflow:hidden;min-height:0}
|
| 82 |
+
|
| 83 |
+
/* Row 1: Observation + Reward (flexible) */
|
| 84 |
+
.obs-reward-area{display:grid;grid-template-columns:1fr 340px;flex:1;overflow:hidden;min-height:0;border-bottom:1px solid var(--border)}
|
| 85 |
+
|
| 86 |
+
/* Row 2: Action builder (auto height, scrollable) */
|
| 87 |
+
.action-section{border-bottom:1px solid var(--border);background:var(--surface);padding:10px 16px;max-height:220px;overflow-y:auto;flex-shrink:0}
|
| 88 |
+
.action-tabs{display:flex;gap:3px;flex-wrap:wrap}
|
| 89 |
+
.action-tab{padding:4px 10px;border:1px solid var(--border);border-radius:5px;cursor:pointer;font-size:10px;font-weight:600;color:var(--muted);background:transparent}
|
| 90 |
+
.action-tab.active{border-color:var(--blue);color:var(--blue);background:#1e2540}
|
| 91 |
+
.action-fields{display:none;grid-template-columns:1fr 1fr;gap:8px}
|
| 92 |
+
.action-fields.visible{display:grid}
|
| 93 |
+
.action-fields .full{grid-column:1/-1}
|
| 94 |
+
|
| 95 |
+
/* Row 3: Step log (fixed 160px) */
|
| 96 |
+
.step-log{background:var(--bg);border-top:1px solid var(--border);overflow-y:auto;padding:8px 12px;font-family:var(--mono);font-size:11px;line-height:1.7;height:160px;flex-shrink:0}
|
| 97 |
+
.log-line{display:flex;gap:8px;align-items:baseline}
|
| 98 |
+
.log-time{color:var(--muted);flex-shrink:0;min-width:52px}
|
| 99 |
+
.log-tag{flex-shrink:0;font-weight:700;min-width:56px}
|
| 100 |
+
.log-tag.start{color:var(--blue)}
|
| 101 |
+
.log-tag.step{color:var(--amber)}
|
| 102 |
+
.log-tag.end{color:var(--green)}
|
| 103 |
+
.log-tag.error{color:var(--red)}
|
| 104 |
+
.log-tag.info{color:var(--purple)}
|
| 105 |
+
.log-msg{color:var(--text);word-break:break-all}
|
| 106 |
+
|
| 107 |
+
/* ── JSON viewer ── */
|
| 108 |
+
.json-view{background:var(--bg);font-family:var(--mono);font-size:11px;line-height:1.5;overflow-y:auto;padding:12px;white-space:pre-wrap;word-break:break-all;flex:1}
|
| 109 |
+
.json-key{color:#93c5fd}
|
| 110 |
+
.json-str{color:#86efac}
|
| 111 |
+
.json-num{color:#fbbf24}
|
| 112 |
+
.json-bool{color:#f87171}
|
| 113 |
+
.json-null{color:var(--muted)}
|
| 114 |
+
|
| 115 |
+
/* ── Reward ── */
|
| 116 |
+
.reward-section{padding:12px;overflow-y:auto;background:var(--surface)}
|
| 117 |
+
.reward-display{text-align:center;padding:10px 0}
|
| 118 |
+
.reward-number{font-size:42px;font-weight:800;font-family:var(--mono);line-height:1}
|
| 119 |
+
.reward-bar-wrap{margin:8px 0;height:8px;background:var(--border);border-radius:99px;overflow:hidden}
|
| 120 |
+
.reward-bar{height:100%;border-radius:99px;transition:width .5s ease;background:linear-gradient(90deg,var(--green),#84cc16)}
|
| 121 |
+
.reward-label{font-size:10px;color:var(--muted)}
|
| 122 |
+
.breakdown-item{display:flex;justify-content:space-between;align-items:center;padding:4px 0;border-bottom:1px solid var(--border);font-size:11px}
|
| 123 |
+
.breakdown-item:last-child{border:none}
|
| 124 |
+
.breakdown-val.pos{color:var(--green)}
|
| 125 |
+
.breakdown-val.neg{color:var(--red)}
|
| 126 |
+
|
| 127 |
+
/* ── Task meta ── */
|
| 128 |
+
.task-meta{background:var(--bg);border:1px solid var(--border);border-radius:6px;padding:8px 10px;font-size:11px;line-height:1.6;color:var(--muted)}
|
| 129 |
+
.task-meta strong{color:var(--text);display:block;margin-bottom:3px;font-size:12px}
|
| 130 |
+
|
| 131 |
+
/* ── Inference runner ── */
|
| 132 |
+
.inference-panel{background:var(--surface2);border:1px solid var(--border);border-radius:8px;padding:10px;margin-top:4px}
|
| 133 |
+
.inference-progress{display:flex;gap:4px;flex-wrap:wrap;margin:6px 0}
|
| 134 |
+
.task-chip{padding:2px 6px;border-radius:4px;font-size:9px;font-weight:700;border:1px solid var(--border);color:var(--muted)}
|
| 135 |
+
.task-chip.running{border-color:var(--amber);color:var(--amber);animation:pulse 1s infinite}
|
| 136 |
+
.task-chip.done{border-color:var(--green);color:var(--green)}
|
| 137 |
+
.task-chip.fail{border-color:var(--red);color:var(--red)}
|
| 138 |
+
|
| 139 |
+
/* ── Status indicator ── */
|
| 140 |
+
.status-dot{width:8px;height:8px;border-radius:50%;display:inline-block;flex-shrink:0}
|
| 141 |
+
|
| 142 |
+
/* ── Responsive ── */
|
| 143 |
+
@media(max-width:900px){
|
| 144 |
+
.layout{grid-template-columns:1fr;grid-template-rows:auto 1fr}
|
| 145 |
+
.sidebar{border-right:none;border-bottom:1px solid var(--border);max-height:260px;flex-direction:row;flex-wrap:wrap;overflow-x:auto}
|
| 146 |
+
.obs-reward-area{grid-template-columns:1fr}
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
/* ── Page Navigation ── */
|
| 150 |
+
.page-tabs{display:flex;gap:2px;background:var(--bg);border-radius:6px;padding:2px;margin-left:16px}
|
| 151 |
+
.page-tab{padding:5px 14px;border:none;border-radius:5px;cursor:pointer;font-size:11px;font-weight:600;color:var(--muted);background:transparent;transition:all .2s}
|
| 152 |
+
.page-tab.active{color:#fff;background:var(--blue);box-shadow:0 0 12px #4f8ef733}
|
| 153 |
+
.page-tab:hover:not(.active){color:var(--text);background:var(--surface2)}
|
| 154 |
+
|
| 155 |
+
.page{display:none;height:calc(100vh - 50px);overflow:hidden}
|
| 156 |
+
.page.visible{display:flex;flex-direction:column}
|
| 157 |
+
|
| 158 |
+
/* ── Benchmark Page ── */
|
| 159 |
+
.bench-layout{display:grid;grid-template-columns:360px 1fr;height:100%;overflow:hidden}
|
| 160 |
+
.bench-sidebar{background:var(--surface);border-right:1px solid var(--border);padding:16px;overflow-y:auto}
|
| 161 |
+
.bench-main{display:flex;flex-direction:column;overflow:hidden}
|
| 162 |
+
|
| 163 |
+
.bench-card{background:var(--surface2);border:1px solid var(--border);border-radius:10px;overflow:hidden;margin-bottom:12px}
|
| 164 |
+
.bench-card-hdr{padding:10px 14px;border-bottom:1px solid var(--border);font-size:12px;font-weight:700;color:var(--text);display:flex;align-items:center;gap:8px;background:linear-gradient(135deg,var(--surface) 0%,var(--surface2) 100%)}
|
| 165 |
+
.bench-card-body{padding:12px}
|
| 166 |
+
|
| 167 |
+
.preset-row{display:flex;gap:4px;flex-wrap:wrap;margin-bottom:10px}
|
| 168 |
+
.preset-btn{padding:4px 10px;border:1px solid var(--border);border-radius:5px;cursor:pointer;font-size:10px;font-weight:600;color:var(--muted);background:transparent;transition:all .15s}
|
| 169 |
+
.preset-btn:hover{border-color:var(--blue);color:var(--blue)}
|
| 170 |
+
.preset-btn.active{border-color:var(--blue);background:#1e2540;color:var(--blue)}
|
| 171 |
+
|
| 172 |
+
.bench-field{margin-bottom:10px}
|
| 173 |
+
.bench-field label{font-size:10px;color:var(--muted);font-weight:600;text-transform:uppercase;letter-spacing:.04em;margin-bottom:4px;display:block}
|
| 174 |
+
.bench-field input,.bench-field select{width:100%;background:var(--bg);border:1px solid var(--border);border-radius:6px;padding:8px 10px;color:var(--text);font-size:12px;font-family:inherit;outline:none;transition:border .15s}
|
| 175 |
+
.bench-field input:focus{border-color:var(--blue)}
|
| 176 |
+
.bench-field input[type=password]{font-family:var(--mono);letter-spacing:2px}
|
| 177 |
+
|
| 178 |
+
.run-btn{width:100%;padding:10px;border:none;border-radius:8px;cursor:pointer;font-size:13px;font-weight:700;color:#fff;background:linear-gradient(135deg,#4f8ef7 0%,#a855f7 100%);transition:all .2s;display:flex;align-items:center;justify-content:center;gap:8px}
|
| 179 |
+
.run-btn:hover{transform:translateY(-1px);box-shadow:0 4px 20px #4f8ef744}
|
| 180 |
+
.run-btn:disabled{opacity:.5;cursor:not-allowed;transform:none;box-shadow:none}
|
| 181 |
+
.run-btn.running{background:linear-gradient(135deg,#f59e0b 0%,#ef4444 100%);animation:pulse 1.5s infinite}
|
| 182 |
+
|
| 183 |
+
/* ── Results Table ── */
|
| 184 |
+
.results-area{flex:1;overflow-y:auto;padding:16px;background:var(--bg)}
|
| 185 |
+
.results-table{width:100%;border-collapse:collapse;font-size:12px}
|
| 186 |
+
.results-table th{padding:8px 10px;text-align:left;font-size:10px;font-weight:700;color:var(--muted);text-transform:uppercase;letter-spacing:.04em;border-bottom:2px solid var(--border);position:sticky;top:0;background:var(--bg);z-index:1}
|
| 187 |
+
.results-table td{padding:6px 10px;border-bottom:1px solid var(--border)}
|
| 188 |
+
.results-table tr:hover{background:var(--surface2)}
|
| 189 |
+
.score-cell{font-family:var(--mono);font-weight:700;font-size:12px}
|
| 190 |
+
.score-high{color:var(--green)}
|
| 191 |
+
.score-mid{color:var(--amber)}
|
| 192 |
+
.score-low{color:var(--red)}
|
| 193 |
+
.avg-cell{font-size:14px;font-weight:800}
|
| 194 |
+
|
| 195 |
+
/* ── Bar Chart ── */
|
| 196 |
+
.chart-container{padding:16px;border-top:1px solid var(--border);background:var(--surface);flex-shrink:0;max-height:280px;overflow-y:auto}
|
| 197 |
+
.chart-bar-row{display:flex;align-items:center;gap:8px;margin-bottom:6px}
|
| 198 |
+
.chart-label{width:120px;font-size:11px;font-weight:600;color:var(--text);text-align:right;flex-shrink:0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
|
| 199 |
+
.chart-bar-bg{flex:1;height:22px;background:var(--bg);border-radius:4px;overflow:hidden;border:1px solid var(--border)}
|
| 200 |
+
.chart-bar-fill{height:100%;border-radius:3px;transition:width .8s ease;display:flex;align-items:center;padding:0 6px;font-size:10px;font-weight:700;color:#fff;white-space:nowrap;min-width:0}
|
| 201 |
+
|
| 202 |
+
/* ── Benchmark Log ── */
|
| 203 |
+
.bench-log{background:var(--bg);border-top:1px solid var(--border);height:200px;overflow-y:auto;padding:8px 12px;font-family:var(--mono);font-size:11px;line-height:1.6;flex-shrink:0}
|
| 204 |
+
.bench-log .log-warn{color:var(--amber)}
|
| 205 |
+
.bench-log .log-err{color:var(--red)}
|
| 206 |
+
.bench-log .log-ok{color:var(--green)}
|
| 207 |
+
.bench-log .log-info{color:var(--blue)}
|
| 208 |
+
|
| 209 |
+
/* ── Empty State ── */
|
| 210 |
+
.empty-state{display:flex;flex-direction:column;align-items:center;justify-content:center;height:100%;color:var(--muted);gap:12px}
|
| 211 |
+
.empty-state .icon{font-size:48px;opacity:.3}
|
| 212 |
+
.empty-state p{font-size:13px;text-align:center;max-width:260px;line-height:1.5}
|
| 213 |
+
</style>
|
| 214 |
+
</head>
|
| 215 |
+
<body>
|
| 216 |
+
|
| 217 |
+
<!-- ── HEADER ── -->
|
| 218 |
+
<div class="header">
|
| 219 |
+
<div class="header-logo">
|
| 220 |
+
<div class="logo-dot green" id="status-dot"></div>
|
| 221 |
+
<h1>OpenEnv Debug Panel</h1>
|
| 222 |
+
<span class="badge">Multi-Agent Ecosystem</span>
|
| 223 |
+
</div>
|
| 224 |
+
<div style="display:flex;gap:8px;margin-left:auto;align-items:center">
|
| 225 |
+
<div class="page-tabs">
|
| 226 |
+
<button class="page-tab active" onclick="switchPage('debug')" id="ptab-debug">🔧 Debug</button>
|
| 227 |
+
<button class="page-tab" onclick="switchPage('benchmark')" id="ptab-benchmark">📊 Benchmark</button>
|
| 228 |
+
</div>
|
| 229 |
+
<span class="badge" style="background:#1a2a1a;color:var(--green);border-color:#22c55e33">Security · PyTorch · Clinical</span>
|
| 230 |
+
<span id="server-status" style="font-size:10px;color:var(--muted)">Checking...</span>
|
| 231 |
+
</div>
|
| 232 |
+
</div>
|
| 233 |
+
|
| 234 |
+
<!-- ══ PAGE: DEBUG ══ -->
|
| 235 |
+
<div class="page visible" id="page-debug">
|
| 236 |
+
|
| 237 |
+
<!-- ── LAYOUT ── -->
|
| 238 |
+
<div class="layout">
|
| 239 |
+
|
| 240 |
+
<!-- SIDEBAR -->
|
| 241 |
+
<div class="sidebar">
|
| 242 |
+
|
| 243 |
+
<!-- Domain Selector -->
|
| 244 |
+
<div class="card">
|
| 245 |
+
<div class="card-hdr">🎯 Domain</div>
|
| 246 |
+
<div class="card-body" style="padding:6px">
|
| 247 |
+
<div class="domain-tabs">
|
| 248 |
+
<button class="domain-tab active" data-domain="security" onclick="switchDomain('security')">Security</button>
|
| 249 |
+
<button class="domain-tab" data-domain="pytorch" onclick="switchDomain('pytorch')">PyTorch</button>
|
| 250 |
+
<button class="domain-tab" data-domain="clinical" onclick="switchDomain('clinical')">Clinical</button>
|
| 251 |
+
</div>
|
| 252 |
+
</div>
|
| 253 |
+
</div>
|
| 254 |
+
|
| 255 |
+
<!-- Task Selector -->
|
| 256 |
+
<div class="card">
|
| 257 |
+
<div class="card-hdr">📋 Tasks</div>
|
| 258 |
+
<div class="card-body" style="padding:6px">
|
| 259 |
+
<div class="task-list" id="task-list"></div>
|
| 260 |
+
</div>
|
| 261 |
+
</div>
|
| 262 |
+
|
| 263 |
+
<!-- Task Info -->
|
| 264 |
+
<div class="card">
|
| 265 |
+
<div class="card-hdr">ℹ️ Task Info</div>
|
| 266 |
+
<div class="card-body">
|
| 267 |
+
<div class="task-meta" id="task-meta">Select a task to see details.</div>
|
| 268 |
+
</div>
|
| 269 |
+
</div>
|
| 270 |
+
|
| 271 |
+
<!-- Run Full Inference -->
|
| 272 |
+
<div class="inference-panel">
|
| 273 |
+
<div style="font-size:11px;font-weight:700;color:var(--text);margin-bottom:6px">⚡ Full Inference Run</div>
|
| 274 |
+
<div style="font-size:10px;color:var(--muted);margin-bottom:8px">Runs all 9 tasks via /inference endpoint.</div>
|
| 275 |
+
<button class="btn btn-success" style="width:100%;font-size:11px" onclick="runFullInference()" id="inf-btn">▶ Run All 9 Tasks</button>
|
| 276 |
+
<div class="inference-progress" id="inf-progress" style="display:none"></div>
|
| 277 |
+
<div id="inf-scores" style="margin-top:6px;font-family:var(--mono);font-size:10px"></div>
|
| 278 |
+
</div>
|
| 279 |
+
|
| 280 |
+
</div>
|
| 281 |
+
|
| 282 |
+
<!-- MAIN PANEL -->
|
| 283 |
+
<div class="main">
|
| 284 |
+
|
| 285 |
+
<!-- Top bar -->
|
| 286 |
+
<div class="main-topbar">
|
| 287 |
+
<div style="display:flex;gap:8px;flex:1;flex-wrap:wrap">
|
| 288 |
+
<div class="info-chip"><span>Task:</span><strong id="chip-task">—</strong></div>
|
| 289 |
+
<div class="info-chip"><span>Episode:</span><strong id="chip-episode" style="font-family:var(--mono);font-size:9px">—</strong></div>
|
| 290 |
+
<div class="info-chip"><span>Step:</span><strong id="chip-step">0</strong></div>
|
| 291 |
+
<div class="info-chip"><span>Reward:</span><strong id="chip-reward" style="color:var(--green)">0.0000</strong></div>
|
| 292 |
+
<div class="info-chip"><span>Done:</span><strong id="chip-done">—</strong></div>
|
| 293 |
+
</div>
|
| 294 |
+
<div style="display:flex;gap:6px">
|
| 295 |
+
<button class="btn btn-primary" onclick="doReset()" id="btn-reset">⟳ Reset</button>
|
| 296 |
+
<button class="btn btn-success" onclick="doStep()" id="btn-step" disabled>▶ Step</button>
|
| 297 |
+
<button class="btn btn-ghost" onclick="clearLog()">🗑 Clear</button>
|
| 298 |
+
</div>
|
| 299 |
+
</div>
|
| 300 |
+
|
| 301 |
+
<!-- Content area: 3 flex rows -->
|
| 302 |
+
<div class="content-area">
|
| 303 |
+
|
| 304 |
+
<!-- ROW 1: Observation + Reward -->
|
| 305 |
+
<div class="obs-reward-area">
|
| 306 |
+
<!-- Observation -->
|
| 307 |
+
<div style="display:flex;flex-direction:column;overflow:hidden;border-right:1px solid var(--border)">
|
| 308 |
+
<div class="card-hdr">📥 Observation</div>
|
| 309 |
+
<div class="json-view" id="obs-view">
|
| 310 |
+
<span style="color:var(--muted)">Press Reset to load the first observation...</span>
|
| 311 |
+
</div>
|
| 312 |
+
</div>
|
| 313 |
+
<!-- Reward -->
|
| 314 |
+
<div style="display:flex;flex-direction:column;overflow:hidden">
|
| 315 |
+
<div class="card-hdr">🏆 Reward</div>
|
| 316 |
+
<div class="reward-section">
|
| 317 |
+
<div class="reward-display">
|
| 318 |
+
<div class="reward-number" id="reward-num" style="color:var(--muted)">—</div>
|
| 319 |
+
<div class="reward-bar-wrap"><div class="reward-bar" id="reward-bar" style="width:0%"></div></div>
|
| 320 |
+
<div class="reward-label" id="reward-label">No reward yet</div>
|
| 321 |
+
</div>
|
| 322 |
+
<div id="reward-breakdown"></div>
|
| 323 |
+
<div id="step-result-raw" style="margin-top:6px"></div>
|
| 324 |
+
</div>
|
| 325 |
+
</div>
|
| 326 |
+
</div>
|
| 327 |
+
|
| 328 |
+
<!-- ROW 2: Action builder -->
|
| 329 |
+
<div class="action-section">
|
| 330 |
+
<div style="display:flex;align-items:center;gap:8px;margin-bottom:8px">
|
| 331 |
+
<div style="font-size:11px;font-weight:700;color:var(--text)">⚡ Build Action</div>
|
| 332 |
+
<div class="action-tabs" id="action-tabs"></div>
|
| 333 |
+
<button class="btn btn-ghost" style="margin-left:auto" onclick="toggleRawJson()">{ } Raw JSON</button>
|
| 334 |
+
</div>
|
| 335 |
+
<div id="action-fields-container"></div>
|
| 336 |
+
<div id="raw-json-area" style="display:none">
|
| 337 |
+
<div class="field">
|
| 338 |
+
<label>Raw JSON Action</label>
|
| 339 |
+
<textarea id="raw-action" rows="3" placeholder='{"action_type":"identify_vulnerability","vuln_type":"sql_injection","cvss_score":7.5,"severity":"high"}'></textarea>
|
| 340 |
+
</div>
|
| 341 |
+
</div>
|
| 342 |
+
</div>
|
| 343 |
+
|
| 344 |
+
</div>
|
| 345 |
+
|
| 346 |
+
<!-- ROW 3: Step log (outside content-area, fixed height) -->
|
| 347 |
+
<div class="step-log" id="step-log">
|
| 348 |
+
<div class="log-line"><span class="log-tag info">INFO</span><span class="log-msg">Debug panel ready. Select a task and press Reset to start.</span></div>
|
| 349 |
+
</div>
|
| 350 |
+
|
| 351 |
+
</div>
|
| 352 |
+
</div>
|
| 353 |
+
|
| 354 |
+
<script>
|
| 355 |
+
// ═══════════════════════════════════════════════
|
| 356 |
+
// DATA
|
| 357 |
+
// ═══════════════════════════════════════════════
|
| 358 |
+
const TASKS = {
|
| 359 |
+
security: [
|
| 360 |
+
{ id:'sec_easy', label:'Injection Detection', diff:'easy', desc:'Identify whether a tool-call has a vulnerability. Return vuln_type, cvss_score, severity.', actions:['identify_vulnerability'] },
|
| 361 |
+
{ id:'sec_medium', label:'Multi-Vuln Scan', diff:'medium', desc:'Scan a code module for multiple vulnerabilities, then propose fixes.', actions:['identify_vulnerability','propose_fix'] },
|
| 362 |
+
{ id:'sec_hard', label:'Auto-Sanitize + Review', diff:'hard', desc:'Identify, fix, and revise code based on reviewer feedback. Multi-turn.', actions:['identify_vulnerability','propose_fix','revise_fix'] },
|
| 363 |
+
],
|
| 364 |
+
pytorch: [
|
| 365 |
+
{ id:'dep_easy', label:'Deprecation Mapper', diff:'easy', desc:'Detect deprecated PyTorch 1.x APIs and flag with replacements.', actions:['flag_outdated'] },
|
| 366 |
+
{ id:'dep_medium', label:'Dependency Resolver', diff:'medium', desc:'Resolve version conflicts using a compatibility matrix.', actions:['resolve_conflict'] },
|
| 367 |
+
{ id:'dep_hard', label:'Graph-Break Hunter', diff:'hard', desc:'Find and fix torch.compile breaking patterns.', actions:['migrate_api'] },
|
| 368 |
+
],
|
| 369 |
+
clinical: [
|
| 370 |
+
{ id:'cli_easy', label:'Gap Detection', diff:'easy', desc:'Identify missing mandatory steps before a procedure.', actions:['detect_gap'] },
|
| 371 |
+
{ id:'cli_medium', label:'Priority Recovery', diff:'medium', desc:'Detect gaps then rank clinical issues by urgency.', actions:['detect_gap','rank_issues'] },
|
| 372 |
+
{ id:'cli_hard', label:'Full Re-plan', diff:'hard', desc:'Detect, rank, and reorder recovery steps respecting dependencies.', actions:['detect_gap','rank_issues','order_steps'] },
|
| 373 |
+
]
|
| 374 |
+
};
|
| 375 |
+
|
| 376 |
+
const ACTION_SCHEMAS = {
|
| 377 |
+
identify_vulnerability: {
|
| 378 |
+
label: 'Identify Vuln',
|
| 379 |
+
fields: [
|
| 380 |
+
{ key:'vuln_type', label:'Vulnerability Type', type:'select', options:['sql_injection','xss','idor','hardcoded_secret','missing_auth','jwt_misuse','path_traversal','ssrf','rate_limit_missing','xxe'] },
|
| 381 |
+
{ key:'cvss_score', label:'CVSS Score (0–10)', type:'number', placeholder:'7.5', min:0, max:10, step:0.1 },
|
| 382 |
+
{ key:'severity', label:'Severity', type:'select', options:['critical','high','medium','low','info'] },
|
| 383 |
+
{ key:'affected_line', label:'Affected Line', type:'number', placeholder:'3' },
|
| 384 |
+
]
|
| 385 |
+
},
|
| 386 |
+
propose_fix: {
|
| 387 |
+
label: 'Propose Fix',
|
| 388 |
+
fields: [
|
| 389 |
+
{ key:'fix_code', label:'Fixed Code', type:'textarea', placeholder:'db.execute(sql, (param,))', full:true },
|
| 390 |
+
{ key:'explanation', label:'Explanation', type:'textarea', placeholder:'Use parameterized queries', full:true },
|
| 391 |
+
]
|
| 392 |
+
},
|
| 393 |
+
revise_fix: {
|
| 394 |
+
label: 'Revise Fix',
|
| 395 |
+
fields: [
|
| 396 |
+
{ key:'fix_code', label:'Revised Code', type:'textarea', placeholder:'Complete corrected code', full:true },
|
| 397 |
+
{ key:'addressed_feedback', label:'Addressed Feedback', type:'textarea', placeholder:'Paste reviewer_feedback here', full:true },
|
| 398 |
+
]
|
| 399 |
+
},
|
| 400 |
+
flag_outdated: {
|
| 401 |
+
label: 'Flag Outdated',
|
| 402 |
+
fields: [
|
| 403 |
+
{ key:'packages_json', label:'Outdated Packages (JSON)', type:'textarea', placeholder:'{"torch": "1.9.0", "numpy": "1.21.0"}', full:true },
|
| 404 |
+
{ key:'deprecated_api', label:'Deprecated API', type:'text', placeholder:'torch.autograd.Variable' },
|
| 405 |
+
{ key:'replacement', label:'Replacement', type:'text', placeholder:'plain tensor' },
|
| 406 |
+
]
|
| 407 |
+
},
|
| 408 |
+
resolve_conflict: {
|
| 409 |
+
label: 'Resolve Conflict',
|
| 410 |
+
fields: [
|
| 411 |
+
{ key:'packages_json', label:'Resolved Packages (JSON)', type:'textarea', placeholder:'{"torch":"2.1.0","numpy":"1.24.3"}', full:true },
|
| 412 |
+
{ key:'reasoning', label:'Reasoning', type:'textarea', placeholder:'torch 2.1 requires numpy>=1.24', full:true },
|
| 413 |
+
]
|
| 414 |
+
},
|
| 415 |
+
migrate_api: {
|
| 416 |
+
label: 'Migrate API',
|
| 417 |
+
fields: [
|
| 418 |
+
{ key:'completed_items_json', label:'Completed Break IDs (JSON)', type:'textarea', placeholder:'["break_001"]', full:true },
|
| 419 |
+
{ key:'code_changes_json', label:'Code Changes (JSON)', type:'textarea', placeholder:'{"break_001":"use torch.where"}', full:true },
|
| 420 |
+
]
|
| 421 |
+
},
|
| 422 |
+
detect_gap: {
|
| 423 |
+
label: 'Detect Gap',
|
| 424 |
+
fields: [
|
| 425 |
+
{ key:'missing_steps_json', label:'Missing Steps (JSON array)', type:'textarea', placeholder:'["pre_op_consent","blood_test"]', full:true },
|
| 426 |
+
{ key:'risk_level', label:'Risk Level', type:'select', options:['critical','high','medium','low'] },
|
| 427 |
+
]
|
| 428 |
+
},
|
| 429 |
+
rank_issues: {
|
| 430 |
+
label: 'Rank Issues',
|
| 431 |
+
fields: [
|
| 432 |
+
{ key:'priority_order_json', label:'Priority Order (highest first)', type:'textarea', placeholder:'["blood_test","pre_op_consent"]', full:true },
|
| 433 |
+
]
|
| 434 |
+
},
|
| 435 |
+
order_steps: {
|
| 436 |
+
label: 'Order Steps',
|
| 437 |
+
fields: [
|
| 438 |
+
{ key:'recovery_steps_json', label:'Recovery Steps (ordered)', type:'textarea', placeholder:'["specialist","alt_treatment","post_op"]', full:true },
|
| 439 |
+
]
|
| 440 |
+
}
|
| 441 |
+
};
|
| 442 |
+
|
| 443 |
+
// ═══════════════════════════════════════════════
|
| 444 |
+
// STATE
|
| 445 |
+
// ═══════════════════════════════════════════════
|
| 446 |
+
let state = {
|
| 447 |
+
domain: 'security',
|
| 448 |
+
task: TASKS.security[0],
|
| 449 |
+
episodeId: null,
|
| 450 |
+
step: 0,
|
| 451 |
+
totalReward: 0,
|
| 452 |
+
done: false,
|
| 453 |
+
currentAction: 'identify_vulnerability',
|
| 454 |
+
rawMode: false
|
| 455 |
+
};
|
| 456 |
+
|
| 457 |
+
// ═══════════════════════════════════════════════
|
| 458 |
+
// INIT
|
| 459 |
+
// ═══════════════════════════════════════════════
|
| 460 |
+
function init() {
|
| 461 |
+
renderTaskList();
|
| 462 |
+
selectTask(state.task);
|
| 463 |
+
checkServerHealth();
|
| 464 |
+
setInterval(checkServerHealth, 15000);
|
| 465 |
+
}
|
| 466 |
+
|
| 467 |
+
// ═══════════════════════════════════════════════
|
| 468 |
+
// DOMAIN / TASK
|
| 469 |
+
// ═══════════════════════════════════════════════
|
| 470 |
+
function switchDomain(domain) {
|
| 471 |
+
state.domain = domain;
|
| 472 |
+
state.task = TASKS[domain][0];
|
| 473 |
+
document.querySelectorAll('.domain-tab').forEach(t => t.classList.toggle('active', t.dataset.domain === domain));
|
| 474 |
+
renderTaskList();
|
| 475 |
+
selectTask(state.task);
|
| 476 |
+
}
|
| 477 |
+
|
| 478 |
+
function renderTaskList() {
|
| 479 |
+
const list = document.getElementById('task-list');
|
| 480 |
+
list.innerHTML = '';
|
| 481 |
+
TASKS[state.domain].forEach(task => {
|
| 482 |
+
const btn = document.createElement('button');
|
| 483 |
+
btn.className = 'task-btn' + (task.id === state.task.id ? ' active' : '');
|
| 484 |
+
btn.innerHTML = `<span>${task.label}</span><span class="diff diff-${task.diff}">${task.diff.toUpperCase()}</span>`;
|
| 485 |
+
btn.onclick = () => selectTask(task);
|
| 486 |
+
list.appendChild(btn);
|
| 487 |
+
});
|
| 488 |
+
}
|
| 489 |
+
|
| 490 |
+
function selectTask(task) {
|
| 491 |
+
state.task = task;
|
| 492 |
+
state.episodeId = null;
|
| 493 |
+
state.step = 0;
|
| 494 |
+
state.totalReward = 0;
|
| 495 |
+
state.done = false;
|
| 496 |
+
document.querySelectorAll('.task-btn').forEach(b => b.classList.toggle('active', b.querySelector('span').textContent === task.label));
|
| 497 |
+
document.getElementById('task-meta').innerHTML = `<strong>${task.label} (${task.id})</strong>${task.desc}<br><br><span style="color:var(--blue)">Actions:</span> ${task.actions.join(' → ')}`;
|
| 498 |
+
document.getElementById('chip-task').textContent = task.id;
|
| 499 |
+
document.getElementById('chip-episode').textContent = '—';
|
| 500 |
+
document.getElementById('chip-step').textContent = '0';
|
| 501 |
+
document.getElementById('chip-reward').textContent = '0.0000';
|
| 502 |
+
document.getElementById('chip-done').textContent = '—';
|
| 503 |
+
document.getElementById('obs-view').innerHTML = '<span style="color:var(--muted)">Press Reset to start this task...</span>';
|
| 504 |
+
document.getElementById('reward-num').textContent = '—';
|
| 505 |
+
document.getElementById('reward-num').style.color = 'var(--muted)';
|
| 506 |
+
document.getElementById('reward-bar').style.width = '0%';
|
| 507 |
+
document.getElementById('reward-label').textContent = 'No reward yet';
|
| 508 |
+
document.getElementById('reward-breakdown').innerHTML = '';
|
| 509 |
+
document.getElementById('step-result-raw').innerHTML = '';
|
| 510 |
+
document.getElementById('btn-step').disabled = true;
|
| 511 |
+
document.getElementById('btn-step').textContent = '▶ Step';
|
| 512 |
+
state.currentAction = task.actions[0];
|
| 513 |
+
renderActionTabs();
|
| 514 |
+
renderActionFields();
|
| 515 |
+
log('info', `Selected: ${task.id} | ${task.label}`);
|
| 516 |
+
}
|
| 517 |
+
|
| 518 |
+
// ═══════════════════════════════════════════════
|
| 519 |
+
// ACTION BUILDER
|
| 520 |
+
// ═══════════════════════════════════════════════
|
| 521 |
+
|
| 522 |
+
// Pre-built examples for each action type (shown when fields are empty)
|
| 523 |
+
const ACTION_EXAMPLES = {
|
| 524 |
+
identify_vulnerability: {
|
| 525 |
+
action_type: 'identify_vulnerability',
|
| 526 |
+
vuln_type: 'sql_injection',
|
| 527 |
+
cvss_score: 8.5,
|
| 528 |
+
severity: 'critical',
|
| 529 |
+
},
|
| 530 |
+
propose_fix: {
|
| 531 |
+
action_type: 'propose_fix',
|
| 532 |
+
fix_code: 'db.execute("SELECT * FROM users WHERE name = ?", (user_input,))',
|
| 533 |
+
explanation: 'Use parameterized query to prevent SQL injection',
|
| 534 |
+
},
|
| 535 |
+
revise_fix: {
|
| 536 |
+
action_type: 'revise_fix',
|
| 537 |
+
fix_code: 'db.execute("SELECT * FROM users WHERE name = ?", (sanitize(user_input),))',
|
| 538 |
+
addressed_feedback: 'Added input validation on top of parameterized query',
|
| 539 |
+
},
|
| 540 |
+
flag_outdated: {
|
| 541 |
+
action_type: 'flag_outdated',
|
| 542 |
+
packages: { torch: '1.9.0' },
|
| 543 |
+
deprecated_api: 'torch.autograd.Variable',
|
| 544 |
+
replacement: 'plain tensor (remove Variable wrapper)',
|
| 545 |
+
},
|
| 546 |
+
resolve_conflict: {
|
| 547 |
+
action_type: 'resolve_conflict',
|
| 548 |
+
packages: { torch: '2.1.0', numpy: '1.24.0' },
|
| 549 |
+
reasoning: 'torch 2.1 requires numpy>=1.24 per compatibility matrix',
|
| 550 |
+
},
|
| 551 |
+
migrate_api: {
|
| 552 |
+
action_type: 'migrate_api',
|
| 553 |
+
completed_items: ['break_001', 'break_002', 'break_003'],
|
| 554 |
+
code_changes: {
|
| 555 |
+
break_001: 'use torch.where instead of if x.item()',
|
| 556 |
+
break_002: 'use tensor.shape[0] instead of len(x)',
|
| 557 |
+
break_003: 'use x.detach().numpy() outside compiled fn',
|
| 558 |
+
},
|
| 559 |
+
},
|
| 560 |
+
detect_gap: {
|
| 561 |
+
action_type: 'detect_gap',
|
| 562 |
+
missing_steps: ['pre_op_consent', 'blood_work'],
|
| 563 |
+
risk_level: 'critical',
|
| 564 |
+
},
|
| 565 |
+
rank_issues: {
|
| 566 |
+
action_type: 'rank_issues',
|
| 567 |
+
priority_order: ['resolve_insurance', 'pre_op_consent', 'book_specialist'],
|
| 568 |
+
},
|
| 569 |
+
order_steps: {
|
| 570 |
+
action_type: 'order_steps',
|
| 571 |
+
recovery_steps: ['resolve_insurance', 'book_specialist', 'complete_pre_op', 'schedule_surgery'],
|
| 572 |
+
},
|
| 573 |
+
};
|
| 574 |
+
|
| 575 |
+
function renderActionTabs() {
|
| 576 |
+
const tabs = document.getElementById('action-tabs');
|
| 577 |
+
tabs.innerHTML = '';
|
| 578 |
+
state.task.actions.forEach(a => {
|
| 579 |
+
const t = document.createElement('button');
|
| 580 |
+
t.className = 'action-tab' + (a === state.currentAction ? ' active' : '');
|
| 581 |
+
t.textContent = ACTION_SCHEMAS[a]?.label || a;
|
| 582 |
+
t.onclick = () => { state.currentAction = a; renderActionTabs(); renderActionFields(); syncRawJson(); };
|
| 583 |
+
tabs.appendChild(t);
|
| 584 |
+
});
|
| 585 |
+
}
|
| 586 |
+
|
| 587 |
+
function renderActionFields() {
|
| 588 |
+
const container = document.getElementById('action-fields-container');
|
| 589 |
+
const schema = ACTION_SCHEMAS[state.currentAction];
|
| 590 |
+
if (!schema) { container.innerHTML = '<div style="color:var(--muted);font-size:11px">No schema.</div>'; return; }
|
| 591 |
+
container.innerHTML = '';
|
| 592 |
+
const grid = document.createElement('div');
|
| 593 |
+
grid.className = 'action-fields visible';
|
| 594 |
+
schema.fields.forEach(f => {
|
| 595 |
+
const wrap = document.createElement('div');
|
| 596 |
+
wrap.className = 'field' + (f.full ? ' full' : '');
|
| 597 |
+
const lbl = document.createElement('label');
|
| 598 |
+
lbl.textContent = f.label;
|
| 599 |
+
wrap.appendChild(lbl);
|
| 600 |
+
let el;
|
| 601 |
+
if (f.type === 'select') {
|
| 602 |
+
el = document.createElement('select');
|
| 603 |
+
el.id = 'af-' + f.key;
|
| 604 |
+
f.options.forEach(o => { const op = document.createElement('option'); op.value = op.textContent = o; el.appendChild(op); });
|
| 605 |
+
el.addEventListener('change', syncRawJson);
|
| 606 |
+
} else if (f.type === 'textarea') {
|
| 607 |
+
el = document.createElement('textarea');
|
| 608 |
+
el.id = 'af-' + f.key;
|
| 609 |
+
el.placeholder = f.placeholder || '';
|
| 610 |
+
el.rows = 2;
|
| 611 |
+
el.addEventListener('input', syncRawJson);
|
| 612 |
+
} else {
|
| 613 |
+
el = document.createElement('input');
|
| 614 |
+
el.type = f.type || 'text';
|
| 615 |
+
el.id = 'af-' + f.key;
|
| 616 |
+
el.placeholder = f.placeholder || '';
|
| 617 |
+
if (f.min !== undefined) el.min = f.min;
|
| 618 |
+
if (f.max !== undefined) el.max = f.max;
|
| 619 |
+
if (f.step !== undefined) el.step = f.step;
|
| 620 |
+
el.addEventListener('input', syncRawJson);
|
| 621 |
+
}
|
| 622 |
+
wrap.appendChild(el);
|
| 623 |
+
grid.appendChild(wrap);
|
| 624 |
+
});
|
| 625 |
+
container.appendChild(grid);
|
| 626 |
+
// Set initial raw JSON
|
| 627 |
+
syncRawJson();
|
| 628 |
+
}
|
| 629 |
+
|
| 630 |
+
function buildAction() {
|
| 631 |
+
if (state.rawMode) {
|
| 632 |
+
try { return JSON.parse(document.getElementById('raw-action').value); }
|
| 633 |
+
catch(e) { log('error', 'Invalid JSON: ' + e.message); return null; }
|
| 634 |
+
}
|
| 635 |
+
return _buildActionFromFields();
|
| 636 |
+
}
|
| 637 |
+
|
| 638 |
+
function _buildActionFromFields() {
|
| 639 |
+
const schema = ACTION_SCHEMAS[state.currentAction];
|
| 640 |
+
const action = { action_type: state.currentAction };
|
| 641 |
+
schema.fields.forEach(f => {
|
| 642 |
+
const el = document.getElementById('af-' + f.key);
|
| 643 |
+
if (!el) return;
|
| 644 |
+
let val = el.value.trim();
|
| 645 |
+
if (!val) return;
|
| 646 |
+
if (f.key.endsWith('_json')) {
|
| 647 |
+
try { action[f.key.replace('_json','')] = JSON.parse(val); }
|
| 648 |
+
catch(e) { action[f.key.replace('_json','')] = val; }
|
| 649 |
+
} else if (f.type === 'number') {
|
| 650 |
+
action[f.key] = parseFloat(val);
|
| 651 |
+
} else {
|
| 652 |
+
action[f.key] = val;
|
| 653 |
+
}
|
| 654 |
+
});
|
| 655 |
+
return action;
|
| 656 |
+
}
|
| 657 |
+
|
| 658 |
+
function syncRawJson() {
|
| 659 |
+
const action = _buildActionFromFields();
|
| 660 |
+
// If form is mostly empty, show the example instead
|
| 661 |
+
const fieldCount = Object.keys(action).length;
|
| 662 |
+
const display = fieldCount <= 1 ? ACTION_EXAMPLES[state.currentAction] || action : action;
|
| 663 |
+
document.getElementById('raw-action').value = JSON.stringify(display, null, 2);
|
| 664 |
+
}
|
| 665 |
+
|
| 666 |
+
function toggleRawJson() {
|
| 667 |
+
state.rawMode = !state.rawMode;
|
| 668 |
+
document.getElementById('raw-json-area').style.display = state.rawMode ? 'block' : 'none';
|
| 669 |
+
document.getElementById('action-fields-container').style.display = state.rawMode ? 'none' : 'block';
|
| 670 |
+
if (state.rawMode) syncRawJson();
|
| 671 |
+
}
|
| 672 |
+
|
| 673 |
+
// ═══════════════════════════════════════════════
|
| 674 |
+
// API CALLS
|
| 675 |
+
// ═══════════════════════════════════════════════
|
| 676 |
+
async function doReset() {
|
| 677 |
+
const btn = document.getElementById('btn-reset');
|
| 678 |
+
btn.disabled = true; btn.textContent = '⟳ Resetting...';
|
| 679 |
+
try {
|
| 680 |
+
log('start', `[START] task_id=${state.task.id}`);
|
| 681 |
+
const res = await fetch('/reset', {
|
| 682 |
+
method:'POST', headers:{'Content-Type':'application/json'},
|
| 683 |
+
body: JSON.stringify({ task_id: state.task.id })
|
| 684 |
+
});
|
| 685 |
+
const data = await res.json();
|
| 686 |
+
if (data.error) throw new Error(data.error);
|
| 687 |
+
state.episodeId = data.episode_id;
|
| 688 |
+
state.step = 0; state.totalReward = 0; state.done = false;
|
| 689 |
+
document.getElementById('chip-episode').textContent = (state.episodeId||'').slice(0,8)+'…';
|
| 690 |
+
document.getElementById('chip-step').textContent = '0';
|
| 691 |
+
document.getElementById('chip-reward').textContent = '0.0000';
|
| 692 |
+
document.getElementById('chip-done').textContent = 'false';
|
| 693 |
+
renderObs(data.observation || data);
|
| 694 |
+
document.getElementById('btn-step').disabled = false;
|
| 695 |
+
document.getElementById('btn-step').textContent = '▶ Step';
|
| 696 |
+
log('info', `Episode: ${state.episodeId}`);
|
| 697 |
+
} catch(e) {
|
| 698 |
+
log('error', 'Reset failed: ' + e.message);
|
| 699 |
+
} finally {
|
| 700 |
+
btn.disabled = false; btn.textContent = '⟳ Reset';
|
| 701 |
+
}
|
| 702 |
+
}
|
| 703 |
+
|
| 704 |
+
async function doStep() {
|
| 705 |
+
if (!state.episodeId) { log('error', 'No episode. Press Reset first.'); return; }
|
| 706 |
+
if (state.done) { log('info', 'Done. Press Reset for new episode.'); return; }
|
| 707 |
+
const action = buildAction();
|
| 708 |
+
if (!action) return;
|
| 709 |
+
action.episode_id = state.episodeId;
|
| 710 |
+
const btn = document.getElementById('btn-step');
|
| 711 |
+
btn.disabled = true; btn.textContent = '▶ Stepping...';
|
| 712 |
+
try {
|
| 713 |
+
const res = await fetch('/step', {
|
| 714 |
+
method:'POST', headers:{'Content-Type':'application/json'},
|
| 715 |
+
body: JSON.stringify(action)
|
| 716 |
+
});
|
| 717 |
+
const data = await res.json();
|
| 718 |
+
const reward = typeof data.reward === 'number' ? data.reward : 0;
|
| 719 |
+
const done = data.done === true || data.done === 'True';
|
| 720 |
+
state.step++; state.totalReward += reward; state.done = done;
|
| 721 |
+
document.getElementById('chip-step').textContent = state.step;
|
| 722 |
+
document.getElementById('chip-reward').textContent = state.totalReward.toFixed(4);
|
| 723 |
+
document.getElementById('chip-done').textContent = String(done);
|
| 724 |
+
document.getElementById('chip-done').style.color = done ? 'var(--green)' : 'var(--muted)';
|
| 725 |
+
renderObs(data.observation || data);
|
| 726 |
+
renderReward(reward, data);
|
| 727 |
+
|
| 728 |
+
// Auto-switch to next expected action if provided
|
| 729 |
+
const nextAction = (data.observation || {}).next_expected_action;
|
| 730 |
+
if (nextAction && ACTION_SCHEMAS[nextAction] && state.task.actions.includes(nextAction)) {
|
| 731 |
+
state.currentAction = nextAction;
|
| 732 |
+
renderActionTabs();
|
| 733 |
+
renderActionFields();
|
| 734 |
+
}
|
| 735 |
+
|
| 736 |
+
log('step', `[STEP] step=${state.step} action=${action.action_type} reward=${reward.toFixed(4)} done=${done}`);
|
| 737 |
+
if (done) {
|
| 738 |
+
log('end', `[END] task_id=${state.task.id} total_reward=${state.totalReward.toFixed(4)} steps=${state.step}`);
|
| 739 |
+
btn.disabled = true; btn.textContent = '✓ Done';
|
| 740 |
+
}
|
| 741 |
+
} catch(e) {
|
| 742 |
+
log('error', 'Step failed: ' + e.message);
|
| 743 |
+
} finally {
|
| 744 |
+
if (!state.done) { btn.disabled = false; btn.textContent = '▶ Step'; }
|
| 745 |
+
}
|
| 746 |
+
}
|
| 747 |
+
|
| 748 |
+
// ═══════════════════════════════════════════════
|
| 749 |
+
// RENDER
|
| 750 |
+
// ═══════════════════════════════════════════════
|
| 751 |
+
function renderObs(obs) {
|
| 752 |
+
document.getElementById('obs-view').innerHTML = syntaxHighlight(JSON.stringify(obs, null, 2));
|
| 753 |
+
}
|
| 754 |
+
|
| 755 |
+
function renderReward(reward, data) {
|
| 756 |
+
const r = Math.max(0, Math.min(1, reward));
|
| 757 |
+
const color = r >= 0.7 ? 'var(--green)' : r >= 0.4 ? 'var(--amber)' : 'var(--red)';
|
| 758 |
+
document.getElementById('reward-num').textContent = reward.toFixed(4);
|
| 759 |
+
document.getElementById('reward-num').style.color = color;
|
| 760 |
+
document.getElementById('reward-bar').style.width = (r*100)+'%';
|
| 761 |
+
document.getElementById('reward-bar').style.background = r >= 0.7 ? 'linear-gradient(90deg,#16a34a,#22c55e)' : r >= 0.4 ? 'linear-gradient(90deg,#b45309,#f59e0b)' : 'linear-gradient(90deg,#991b1b,#ef4444)';
|
| 762 |
+
document.getElementById('reward-label').textContent = r >= 0.7 ? '✓ Good' : r >= 0.4 ? '⚠ Partial' : r > 0 ? '✗ Low' : '✗ Zero';
|
| 763 |
+
|
| 764 |
+
const bd = document.getElementById('reward-breakdown');
|
| 765 |
+
const breakdown = data.reward_breakdown || data.breakdown || null;
|
| 766 |
+
if (breakdown && typeof breakdown === 'object') {
|
| 767 |
+
bd.innerHTML = '<div style="font-size:10px;font-weight:700;color:var(--muted);text-transform:uppercase;margin:8px 0 4px">Breakdown</div>';
|
| 768 |
+
Object.entries(breakdown).forEach(([k,v]) => {
|
| 769 |
+
const pos = v >= 0;
|
| 770 |
+
bd.innerHTML += `<div class="breakdown-item"><span>${k.replace(/_/g,' ')}</span><span class="breakdown-val ${pos?'pos':'neg'}">${pos?'+':''}${typeof v==='number'?v.toFixed(4):v}</span></div>`;
|
| 771 |
+
});
|
| 772 |
+
} else bd.innerHTML = '';
|
| 773 |
+
|
| 774 |
+
const raw = document.getElementById('step-result-raw');
|
| 775 |
+
const filtered = {...data}; delete filtered.observation;
|
| 776 |
+
raw.innerHTML = '<div style="font-size:10px;color:var(--muted);margin-top:6px;font-family:var(--mono);white-space:pre-wrap;max-height:120px;overflow-y:auto">' + syntaxHighlight(JSON.stringify(filtered, null, 2)) + '</div>';
|
| 777 |
+
}
|
| 778 |
+
|
| 779 |
+
function syntaxHighlight(json) {
|
| 780 |
+
return json
|
| 781 |
+
.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>')
|
| 782 |
+
.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, m => {
|
| 783 |
+
let cls = 'json-num';
|
| 784 |
+
if (/^"/.test(m)) cls = /:$/.test(m) ? 'json-key' : 'json-str';
|
| 785 |
+
else if (/true|false/.test(m)) cls = 'json-bool';
|
| 786 |
+
else if (/null/.test(m)) cls = 'json-null';
|
| 787 |
+
return `<span class="${cls}">${m}</span>`;
|
| 788 |
+
});
|
| 789 |
+
}
|
| 790 |
+
|
| 791 |
+
// ═══════════════════════════════════════════════
|
| 792 |
+
// LOG
|
| 793 |
+
// ═══════════════════════════════════════════════
|
| 794 |
+
function log(type, msg) {
|
| 795 |
+
const logEl = document.getElementById('step-log');
|
| 796 |
+
const line = document.createElement('div');
|
| 797 |
+
line.className = 'log-line';
|
| 798 |
+
const now = new Date();
|
| 799 |
+
const t = `${String(now.getHours()).padStart(2,'0')}:${String(now.getMinutes()).padStart(2,'0')}:${String(now.getSeconds()).padStart(2,'0')}`;
|
| 800 |
+
const tagMap = {start:'START',step:'STEP',end:'END',error:'ERROR',info:'INFO'};
|
| 801 |
+
line.innerHTML = `<span class="log-time">${t}</span><span class="log-tag ${type}">[${tagMap[type]||type.toUpperCase()}]</span><span class="log-msg">${msg}</span>`;
|
| 802 |
+
logEl.appendChild(line);
|
| 803 |
+
logEl.scrollTop = logEl.scrollHeight;
|
| 804 |
+
}
|
| 805 |
+
|
| 806 |
+
function clearLog() {
|
| 807 |
+
document.getElementById('step-log').innerHTML = '';
|
| 808 |
+
log('info', 'Log cleared.');
|
| 809 |
+
}
|
| 810 |
+
|
| 811 |
+
// ═══════════════════════════════════════════════
|
| 812 |
+
// FULL INFERENCE
|
| 813 |
+
// ═══════════════════════════════════════════════
|
| 814 |
+
async function runFullInference() {
|
| 815 |
+
const btn = document.getElementById('inf-btn');
|
| 816 |
+
btn.disabled = true; btn.textContent = '⏳ Running...';
|
| 817 |
+
const prog = document.getElementById('inf-progress');
|
| 818 |
+
const scores = document.getElementById('inf-scores');
|
| 819 |
+
prog.style.display = 'flex'; prog.innerHTML = '';
|
| 820 |
+
scores.innerHTML = '';
|
| 821 |
+
const allTasks = ['sec_easy','sec_medium','sec_hard','dep_easy','dep_medium','dep_hard','cli_easy','cli_medium','cli_hard'];
|
| 822 |
+
allTasks.forEach(t => { prog.innerHTML += `<span class="task-chip" id="chip-inf-${t}">${t}</span>`; });
|
| 823 |
+
log('info', 'Starting full inference via /inference...');
|
| 824 |
+
try {
|
| 825 |
+
const res = await fetch('/inference', { method:'POST', headers:{'Content-Type':'application/json'}, body:'{}' });
|
| 826 |
+
const data = await res.json();
|
| 827 |
+
if (data.error) { log('error', 'Inference error: ' + data.error); return; }
|
| 828 |
+
const final = data.final_scores || {};
|
| 829 |
+
allTasks.forEach(t => {
|
| 830 |
+
const chip = document.getElementById('chip-inf-'+t);
|
| 831 |
+
const sc = final[t];
|
| 832 |
+
if (sc !== undefined) {
|
| 833 |
+
chip.classList.add(sc > 0.3 ? 'done' : 'fail');
|
| 834 |
+
chip.textContent = `${t}: ${typeof sc==='number'?sc.toFixed(3):sc}`;
|
| 835 |
+
} else chip.classList.add('fail');
|
| 836 |
+
});
|
| 837 |
+
const avg = data.average_score || 0;
|
| 838 |
+
scores.innerHTML = `<div style="padding:6px;background:var(--bg);border-radius:4px;border:1px solid var(--border)"><span style="font-size:10px;color:var(--muted)">Average: </span><strong style="color:var(--green)">${avg.toFixed ? avg.toFixed(4) : avg}</strong></div>`;
|
| 839 |
+
log('end', `Inference done. Average: ${avg}`);
|
| 840 |
+
} catch(e) {
|
| 841 |
+
log('error', 'Inference failed: ' + e.message);
|
| 842 |
+
} finally {
|
| 843 |
+
btn.disabled = false; btn.textContent = '▶ Run All 9 Tasks';
|
| 844 |
+
}
|
| 845 |
+
}
|
| 846 |
+
|
| 847 |
+
// ═══════════════════════════════════════════════
|
| 848 |
+
// HEALTH CHECK — uses /reset OPTIONS or simple GET
|
| 849 |
+
// ═══════════════════════════════════════════════
|
| 850 |
+
async function checkServerHealth() {
|
| 851 |
+
try {
|
| 852 |
+
const res = await fetch('/', {
|
| 853 |
+
headers: { 'Accept': 'application/json' },
|
| 854 |
+
signal: AbortSignal.timeout(3000)
|
| 855 |
+
});
|
| 856 |
+
if (res.ok) {
|
| 857 |
+
document.getElementById('status-dot').className = 'logo-dot green';
|
| 858 |
+
document.getElementById('server-status').textContent = 'Server online';
|
| 859 |
+
document.getElementById('server-status').style.color = 'var(--green)';
|
| 860 |
+
} else throw new Error('not ok');
|
| 861 |
+
} catch(e) {
|
| 862 |
+
document.getElementById('status-dot').className = 'logo-dot err';
|
| 863 |
+
document.getElementById('server-status').textContent = 'Server unreachable';
|
| 864 |
+
document.getElementById('server-status').style.color = 'var(--red)';
|
| 865 |
+
}
|
| 866 |
+
}
|
| 867 |
+
|
| 868 |
+
init();
|
| 869 |
+
</script>
|
| 870 |
+
|
| 871 |
+
</div><!-- end page-debug -->
|
| 872 |
+
|
| 873 |
+
<!-- ══ PAGE: BENCHMARK ══ -->
|
| 874 |
+
<div class="page" id="page-benchmark">
|
| 875 |
+
<div class="bench-layout">
|
| 876 |
+
<!-- Benchmark Sidebar -->
|
| 877 |
+
<div class="bench-sidebar">
|
| 878 |
+
|
| 879 |
+
<div class="bench-card">
|
| 880 |
+
<div class="bench-card-hdr">🔑 API Configuration</div>
|
| 881 |
+
<div class="bench-card-body">
|
| 882 |
+
<label style="font-size:10px;color:var(--muted);margin-bottom:6px;display:block">Quick Presets</label>
|
| 883 |
+
<div class="preset-row">
|
| 884 |
+
<button class="preset-btn" onclick="applyPreset('groq')">⚡ Groq</button>
|
| 885 |
+
<button class="preset-btn" onclick="applyPreset('openrouter')">🌐 OpenRouter</button>
|
| 886 |
+
<button class="preset-btn" onclick="applyPreset('huggingface')">🤗 HuggingFace</button>
|
| 887 |
+
<button class="preset-btn" onclick="applyPreset('custom')">✏️ Custom</button>
|
| 888 |
+
</div>
|
| 889 |
+
|
| 890 |
+
<div class="bench-field">
|
| 891 |
+
<label>API Base URL</label>
|
| 892 |
+
<input type="text" id="bench-api-base" placeholder="https://api.groq.com/openai/v1" />
|
| 893 |
+
</div>
|
| 894 |
+
|
| 895 |
+
<div class="bench-field">
|
| 896 |
+
<label>API Key</label>
|
| 897 |
+
<input type="password" id="bench-api-key" placeholder="sk-..." />
|
| 898 |
+
</div>
|
| 899 |
+
|
| 900 |
+
<div class="bench-field">
|
| 901 |
+
<label>Model Display Name</label>
|
| 902 |
+
<input type="text" id="bench-model-name" placeholder="Llama-3.3-70B" />
|
| 903 |
+
</div>
|
| 904 |
+
|
| 905 |
+
<div class="bench-field">
|
| 906 |
+
<label>Model ID</label>
|
| 907 |
+
<input type="text" id="bench-model-id" placeholder="llama-3.3-70b-versatile" />
|
| 908 |
+
</div>
|
| 909 |
+
</div>
|
| 910 |
+
</div>
|
| 911 |
+
|
| 912 |
+
<button class="run-btn" id="bench-run-btn" onclick="runBenchmark()">
|
| 913 |
+
🚀 Run Benchmark (9 Tasks)
|
| 914 |
+
</button>
|
| 915 |
+
|
| 916 |
+
<div class="bench-card" style="margin-top:12px">
|
| 917 |
+
<div class="bench-card-hdr">📊 Run History
|
| 918 |
+
<button class="btn-ghost" style="margin-left:auto;font-size:9px;padding:2px 6px" onclick="clearResults()">Clear All</button>
|
| 919 |
+
</div>
|
| 920 |
+
<div class="bench-card-body" id="bench-history" style="max-height:200px;overflow-y:auto">
|
| 921 |
+
<div style="color:var(--muted);font-size:11px;text-align:center;padding:12px">No runs yet. Configure a model above and run.</div>
|
| 922 |
+
</div>
|
| 923 |
+
</div>
|
| 924 |
+
|
| 925 |
+
<div class="bench-card">
|
| 926 |
+
<div class="bench-card-hdr">ℹ️ Tips</div>
|
| 927 |
+
<div class="bench-card-body" style="font-size:11px;color:var(--muted);line-height:1.5">
|
| 928 |
+
<p>• <strong>Groq</strong> — Fast, free tier, use llama-3.3-70b-versatile</p>
|
| 929 |
+
<p>• <strong>OpenRouter</strong> — Many models, free tier has rate limits</p>
|
| 930 |
+
<p>• <strong>HuggingFace</strong> — Use your HF token with router.huggingface.co/v1</p>
|
| 931 |
+
<p style="margin-top:6px;color:var(--amber)">⚠️ Free tier models may hit rate limits on 9 tasks</p>
|
| 932 |
+
</div>
|
| 933 |
+
</div>
|
| 934 |
+
</div>
|
| 935 |
+
|
| 936 |
+
<!-- Benchmark Main -->
|
| 937 |
+
<div class="bench-main">
|
| 938 |
+
|
| 939 |
+
<!-- Results Table -->
|
| 940 |
+
<div class="results-area" id="bench-results">
|
| 941 |
+
<div class="empty-state">
|
| 942 |
+
<div class="icon">📊</div>
|
| 943 |
+
<p>Run a benchmark to see results here. Configure your API key and model on the left, then click Run.</p>
|
| 944 |
+
</div>
|
| 945 |
+
</div>
|
| 946 |
+
|
| 947 |
+
<!-- Comparison Chart -->
|
| 948 |
+
<div class="chart-container" id="bench-chart" style="display:none">
|
| 949 |
+
<div style="font-size:11px;font-weight:700;color:var(--muted);text-transform:uppercase;letter-spacing:.04em;margin-bottom:10px">Model Comparison — Average Score</div>
|
| 950 |
+
<div id="chart-bars"></div>
|
| 951 |
+
</div>
|
| 952 |
+
|
| 953 |
+
<!-- Log -->
|
| 954 |
+
<div class="bench-log" id="bench-log">
|
| 955 |
+
<div style="color:var(--muted)">Benchmark logs will appear here...</div>
|
| 956 |
+
</div>
|
| 957 |
+
</div>
|
| 958 |
+
</div>
|
| 959 |
+
</div><!-- end page-benchmark -->
|
| 960 |
+
|
| 961 |
+
<script>
|
| 962 |
+
// ══════════════════════════════════
|
| 963 |
+
// PAGE SWITCHING
|
| 964 |
+
// ══════════════════════════════════
|
| 965 |
+
function switchPage(page) {
|
| 966 |
+
document.querySelectorAll('.page').forEach(p => p.classList.remove('visible'));
|
| 967 |
+
document.querySelectorAll('.page-tab').forEach(t => t.classList.remove('active'));
|
| 968 |
+
document.getElementById('page-' + page).classList.add('visible');
|
| 969 |
+
document.getElementById('ptab-' + page).classList.add('active');
|
| 970 |
+
if (page === 'benchmark') loadBenchResults();
|
| 971 |
+
}
|
| 972 |
+
|
| 973 |
+
// ══════════════════════════════════
|
| 974 |
+
// API PRESETS
|
| 975 |
+
// ══════════════════════════════════
|
| 976 |
+
const PRESETS = {
|
| 977 |
+
groq: { base: 'https://api.groq.com/openai/v1', models: ['llama-3.3-70b-versatile','mixtral-8x7b-32768','gemma2-9b-it'], default_name: 'Llama-3.3-70B', default_id: 'llama-3.3-70b-versatile' },
|
| 978 |
+
openrouter: { base: 'https://openrouter.ai/api/v1', models: ['nvidia/nemotron-3-super-120b-a12b:free','qwen/qwen3.6-plus:free','deepseek/deepseek-r1:free'], default_name: 'Nemotron-120B', default_id: 'nvidia/nemotron-3-super-120b-a12b:free' },
|
| 979 |
+
huggingface: { base: 'https://router.huggingface.co/v1', models: ['Qwen/Qwen2.5-72B-Instruct','meta-llama/Llama-3.1-70B-Instruct'], default_name: 'Qwen-2.5-72B', default_id: 'Qwen/Qwen2.5-72B-Instruct' },
|
| 980 |
+
custom: { base: '', models: [], default_name: '', default_id: '' },
|
| 981 |
+
};
|
| 982 |
+
|
| 983 |
+
function applyPreset(name) {
|
| 984 |
+
document.querySelectorAll('.preset-btn').forEach(b => b.classList.remove('active'));
|
| 985 |
+
event.target.classList.add('active');
|
| 986 |
+
const p = PRESETS[name];
|
| 987 |
+
document.getElementById('bench-api-base').value = p.base;
|
| 988 |
+
document.getElementById('bench-model-name').value = p.default_name;
|
| 989 |
+
document.getElementById('bench-model-id').value = p.default_id;
|
| 990 |
+
if (name !== 'custom') document.getElementById('bench-api-key').focus();
|
| 991 |
+
}
|
| 992 |
+
|
| 993 |
+
// ══════════════════════════════════
|
| 994 |
+
// RUN BENCHMARK
|
| 995 |
+
// ══════════════════════════════════
|
| 996 |
+
let benchRunning = false;
|
| 997 |
+
|
| 998 |
+
async function runBenchmark() {
|
| 999 |
+
if (benchRunning) return;
|
| 1000 |
+
const apiBase = document.getElementById('bench-api-base').value.trim();
|
| 1001 |
+
const apiKey = document.getElementById('bench-api-key').value.trim();
|
| 1002 |
+
const modelName = document.getElementById('bench-model-name').value.trim() || 'Unknown';
|
| 1003 |
+
const modelId = document.getElementById('bench-model-id').value.trim();
|
| 1004 |
+
|
| 1005 |
+
if (!apiBase || !apiKey || !modelId) {
|
| 1006 |
+
alert('Please fill in API Base URL, API Key, and Model ID');
|
| 1007 |
+
return;
|
| 1008 |
+
}
|
| 1009 |
+
|
| 1010 |
+
benchRunning = true;
|
| 1011 |
+
const btn = document.getElementById('bench-run-btn');
|
| 1012 |
+
btn.disabled = true;
|
| 1013 |
+
btn.classList.add('running');
|
| 1014 |
+
btn.innerHTML = '⏳ Running 9 tasks...';
|
| 1015 |
+
|
| 1016 |
+
const logEl = document.getElementById('bench-log');
|
| 1017 |
+
logEl.innerHTML = '';
|
| 1018 |
+
benchLog('info', `Starting benchmark: ${modelName} (${modelId})`);
|
| 1019 |
+
benchLog('info', `API: ${apiBase}`);
|
| 1020 |
+
benchLog('info', `Running 9 tasks... This may take 2-5 minutes.`);
|
| 1021 |
+
|
| 1022 |
+
try {
|
| 1023 |
+
const res = await fetch('/benchmark/run', {
|
| 1024 |
+
method: 'POST',
|
| 1025 |
+
headers: {'Content-Type': 'application/json'},
|
| 1026 |
+
body: JSON.stringify({
|
| 1027 |
+
model_name: modelName,
|
| 1028 |
+
model_id: modelId,
|
| 1029 |
+
api_base: apiBase,
|
| 1030 |
+
api_key: apiKey,
|
| 1031 |
+
})
|
| 1032 |
+
});
|
| 1033 |
+
|
| 1034 |
+
if (res.headers.get('content-type').includes('application/json')) {
|
| 1035 |
+
const data = await res.json();
|
| 1036 |
+
if (data.error) benchLog('err', 'Error: ' + data.error);
|
| 1037 |
+
throw new Error('Benchmark failed to start');
|
| 1038 |
+
}
|
| 1039 |
+
|
| 1040 |
+
const reader = res.body.getReader();
|
| 1041 |
+
const decoder = new TextDecoder();
|
| 1042 |
+
let done = false;
|
| 1043 |
+
let buffer = '';
|
| 1044 |
+
|
| 1045 |
+
while (!done) {
|
| 1046 |
+
const { value, done: readerDone } = await reader.read();
|
| 1047 |
+
done = readerDone;
|
| 1048 |
+
if (value) {
|
| 1049 |
+
buffer += decoder.decode(value, { stream: true });
|
| 1050 |
+
let parts = buffer.split('\n\n');
|
| 1051 |
+
buffer = parts.pop();
|
| 1052 |
+
|
| 1053 |
+
for (const part of parts) {
|
| 1054 |
+
if (part.startsWith('data: ')) {
|
| 1055 |
+
try {
|
| 1056 |
+
const event = JSON.parse(part.substring(6));
|
| 1057 |
+
|
| 1058 |
+
if (event.type === 'log') {
|
| 1059 |
+
benchLog(event.level, event.msg);
|
| 1060 |
+
} else if (event.type === 'task_done') {
|
| 1061 |
+
benchLog('info', `🎯 Task ${event.task_id} completed with score: ${event.score.toFixed(4)}`);
|
| 1062 |
+
} else if (event.type === 'done') {
|
| 1063 |
+
benchLog('ok', `✅ All tasks complete! Average: ${event.result.average}`);
|
| 1064 |
+
renderResults();
|
| 1065 |
+
renderChart();
|
| 1066 |
+
}
|
| 1067 |
+
} catch(e) {}
|
| 1068 |
+
}
|
| 1069 |
+
}
|
| 1070 |
+
}
|
| 1071 |
+
}
|
| 1072 |
+
|
| 1073 |
+
} catch(e) {
|
| 1074 |
+
benchLog('err', 'Execution error: ' + e.message);
|
| 1075 |
+
} finally {
|
| 1076 |
+
benchRunning = false;
|
| 1077 |
+
btn.disabled = false;
|
| 1078 |
+
btn.classList.remove('running');
|
| 1079 |
+
btn.innerHTML = '🚀 Run Benchmark (9 Tasks)';
|
| 1080 |
+
}
|
| 1081 |
+
}
|
| 1082 |
+
|
| 1083 |
+
function benchLog(type, msg) {
|
| 1084 |
+
const logEl = document.getElementById('bench-log');
|
| 1085 |
+
const cls = type === 'err' ? 'log-err' : type === 'warn' ? 'log-warn' : type === 'ok' ? 'log-ok' : 'log-info';
|
| 1086 |
+
const time = new Date().toLocaleTimeString('en-US',{hour12:false,hour:'2-digit',minute:'2-digit',second:'2-digit'});
|
| 1087 |
+
logEl.innerHTML += `<div class="${cls}"><span style="color:var(--muted)">${time}</span> ${msg}</div>`;
|
| 1088 |
+
logEl.scrollTop = logEl.scrollHeight;
|
| 1089 |
+
}
|
| 1090 |
+
|
| 1091 |
+
// ══════════════════════════════════
|
| 1092 |
+
// RESULTS RENDERING
|
| 1093 |
+
// ══════════════════════════════════
|
| 1094 |
+
const BENCH_TASKS = ['sec_easy','sec_medium','sec_hard','dep_easy','dep_medium','dep_hard','cli_easy','cli_medium','cli_hard'];
|
| 1095 |
+
const BENCH_COLORS = ['#4f8ef7','#a855f7','#22c55e','#f59e0b','#ef4444','#22d3ee','#f472b6','#84cc16','#fb923c'];
|
| 1096 |
+
|
| 1097 |
+
async function loadBenchResults() {
|
| 1098 |
+
try {
|
| 1099 |
+
const res = await fetch('/benchmark/results');
|
| 1100 |
+
const data = await res.json();
|
| 1101 |
+
if (data.results && data.results.length > 0) {
|
| 1102 |
+
renderResults(data.results);
|
| 1103 |
+
renderChart(data.results);
|
| 1104 |
+
renderHistory(data.results);
|
| 1105 |
+
}
|
| 1106 |
+
} catch(e) {}
|
| 1107 |
+
}
|
| 1108 |
+
|
| 1109 |
+
function renderResults(results) {
|
| 1110 |
+
if (!results) {
|
| 1111 |
+
fetch('/benchmark/results').then(r=>r.json()).then(d => { if(d.results) renderResults(d.results); });
|
| 1112 |
+
return;
|
| 1113 |
+
}
|
| 1114 |
+
if (results.length === 0) return;
|
| 1115 |
+
|
| 1116 |
+
const el = document.getElementById('bench-results');
|
| 1117 |
+
let html = '<table class="results-table"><thead><tr><th>Model</th>';
|
| 1118 |
+
BENCH_TASKS.forEach(t => html += `<th>${t.replace('_',' ').toUpperCase()}</th>`);
|
| 1119 |
+
html += '<th>AVG</th><th>Time</th></tr></thead><tbody>';
|
| 1120 |
+
|
| 1121 |
+
results.forEach((r, i) => {
|
| 1122 |
+
html += `<tr>`;
|
| 1123 |
+
html += `<td style="font-weight:700;color:${BENCH_COLORS[i % BENCH_COLORS.length]}">${r.model_name}</td>`;
|
| 1124 |
+
BENCH_TASKS.forEach(t => {
|
| 1125 |
+
const s = r.scores[t] || 0;
|
| 1126 |
+
const cls = s >= 0.8 ? 'score-high' : s >= 0.4 ? 'score-mid' : 'score-low';
|
| 1127 |
+
html += `<td class="score-cell ${cls}">${s.toFixed(2)}</td>`;
|
| 1128 |
+
});
|
| 1129 |
+
const avgCls = r.average >= 0.7 ? 'score-high' : r.average >= 0.4 ? 'score-mid' : 'score-low';
|
| 1130 |
+
html += `<td class="score-cell avg-cell ${avgCls}">${r.average.toFixed(3)}</td>`;
|
| 1131 |
+
const ts = new Date(r.timestamp);
|
| 1132 |
+
html += `<td style="font-size:10px;color:var(--muted)">${ts.toLocaleTimeString()}</td>`;
|
| 1133 |
+
html += '</tr>';
|
| 1134 |
+
});
|
| 1135 |
+
|
| 1136 |
+
html += '</tbody></table>';
|
| 1137 |
+
el.innerHTML = html;
|
| 1138 |
+
}
|
| 1139 |
+
|
| 1140 |
+
function renderChart(results) {
|
| 1141 |
+
if (!results) {
|
| 1142 |
+
fetch('/benchmark/results').then(r=>r.json()).then(d => { if(d.results) renderChart(d.results); });
|
| 1143 |
+
return;
|
| 1144 |
+
}
|
| 1145 |
+
if (results.length === 0) return;
|
| 1146 |
+
|
| 1147 |
+
const container = document.getElementById('bench-chart');
|
| 1148 |
+
container.style.display = 'block';
|
| 1149 |
+
const bars = document.getElementById('chart-bars');
|
| 1150 |
+
|
| 1151 |
+
let html = '';
|
| 1152 |
+
results.forEach((r, i) => {
|
| 1153 |
+
const pct = Math.round(r.average * 100);
|
| 1154 |
+
const color = BENCH_COLORS[i % BENCH_COLORS.length];
|
| 1155 |
+
const gradient = `linear-gradient(90deg, ${color}88, ${color})`;
|
| 1156 |
+
html += `<div class="chart-bar-row">
|
| 1157 |
+
<div class="chart-label">${r.model_name}</div>
|
| 1158 |
+
<div class="chart-bar-bg">
|
| 1159 |
+
<div class="chart-bar-fill" style="width:${pct}%;background:${gradient}">${r.average.toFixed(3)}</div>
|
| 1160 |
+
</div>
|
| 1161 |
+
</div>`;
|
| 1162 |
+
});
|
| 1163 |
+
bars.innerHTML = html;
|
| 1164 |
+
}
|
| 1165 |
+
|
| 1166 |
+
function renderHistory(results) {
|
| 1167 |
+
const el = document.getElementById('bench-history');
|
| 1168 |
+
if (!results || results.length === 0) {
|
| 1169 |
+
el.innerHTML = '<div style="color:var(--muted);font-size:11px;text-align:center;padding:12px">No runs yet.</div>';
|
| 1170 |
+
return;
|
| 1171 |
+
}
|
| 1172 |
+
let html = '';
|
| 1173 |
+
results.forEach((r, i) => {
|
| 1174 |
+
const avgCls = r.average >= 0.7 ? 'score-high' : r.average >= 0.4 ? 'score-mid' : 'score-low';
|
| 1175 |
+
const ts = new Date(r.timestamp);
|
| 1176 |
+
html += `<div style="display:flex;align-items:center;gap:8px;padding:6px 0;border-bottom:1px solid var(--border);font-size:11px">
|
| 1177 |
+
<span style="color:${BENCH_COLORS[i % BENCH_COLORS.length]};font-weight:700">${r.model_name}</span>
|
| 1178 |
+
<span class="score-cell ${avgCls}" style="margin-left:auto">${r.average.toFixed(3)}</span>
|
| 1179 |
+
<span style="color:var(--muted);font-size:9px">${ts.toLocaleTimeString()}</span>
|
| 1180 |
+
</div>`;
|
| 1181 |
+
});
|
| 1182 |
+
el.innerHTML = html;
|
| 1183 |
+
}
|
| 1184 |
+
|
| 1185 |
+
async function clearResults() {
|
| 1186 |
+
if (!confirm('Clear all benchmark results?')) return;
|
| 1187 |
+
await fetch('/benchmark/clear', {method:'POST'});
|
| 1188 |
+
document.getElementById('bench-results').innerHTML = '<div class="empty-state"><div class="icon">📊</div><p>No results. Run a benchmark to see data.</p></div>';
|
| 1189 |
+
document.getElementById('bench-chart').style.display = 'none';
|
| 1190 |
+
document.getElementById('bench-history').innerHTML = '<div style="color:var(--muted);font-size:11px;text-align:center;padding:12px">No runs yet.</div>';
|
| 1191 |
+
}
|
| 1192 |
+
</script>
|
| 1193 |
+
|
| 1194 |
+
</body>
|
| 1195 |
+
</html>
|
| 1196 |
+
|
server/demo_agent.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# server/demo_agent.py
|
| 2 |
+
# Simple rule-based demo agent for the Gradio UI.
|
| 3 |
+
# Uses hardcoded heuristics to show the environment works without calling a real LLM.
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def demo_action(obs):
|
| 7 |
+
"""Generate a simple action based on observation. Used by the UI demo."""
|
| 8 |
+
task_type = obs.get('task_type', '')
|
| 9 |
+
task_id = obs.get('task_id', '')
|
| 10 |
+
turn = obs.get('turn', 0)
|
| 11 |
+
|
| 12 |
+
if task_type == 'security':
|
| 13 |
+
return _security_action(obs, task_id, turn)
|
| 14 |
+
elif task_type == 'dependency':
|
| 15 |
+
return _dependency_action(obs, task_id, turn)
|
| 16 |
+
elif task_type == 'clinical':
|
| 17 |
+
return _clinical_action(obs, task_id, turn)
|
| 18 |
+
else:
|
| 19 |
+
return {'action_type': 'invalid'}
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def _security_action(obs, task_id, turn):
|
| 23 |
+
if turn == 0:
|
| 24 |
+
tool_call = obs.get('tool_call', '')
|
| 25 |
+
# Simple heuristic to detect common vulnerability types
|
| 26 |
+
vuln_type = 'sql_injection'
|
| 27 |
+
severity = 'critical'
|
| 28 |
+
cvss = 8.5
|
| 29 |
+
if 'script' in tool_call.lower() or 'xss' in tool_call.lower():
|
| 30 |
+
vuln_type = 'xss'
|
| 31 |
+
severity = 'medium'
|
| 32 |
+
cvss = 5.0
|
| 33 |
+
elif 'password' in tool_call.lower() or 'secret' in tool_call.lower():
|
| 34 |
+
vuln_type = 'hardcoded_secret'
|
| 35 |
+
severity = 'high'
|
| 36 |
+
cvss = 6.5
|
| 37 |
+
elif 'jwt' in tool_call.lower() or 'token' in tool_call.lower():
|
| 38 |
+
vuln_type = 'jwt_misuse'
|
| 39 |
+
severity = 'critical'
|
| 40 |
+
cvss = 8.0
|
| 41 |
+
elif 'path' in tool_call.lower() or '..' in tool_call:
|
| 42 |
+
vuln_type = 'path_traversal'
|
| 43 |
+
severity = 'high'
|
| 44 |
+
cvss = 7.0
|
| 45 |
+
elif 'auth' in tool_call.lower() and 'no' in tool_call.lower():
|
| 46 |
+
vuln_type = 'missing_auth'
|
| 47 |
+
severity = 'critical'
|
| 48 |
+
cvss = 8.5
|
| 49 |
+
|
| 50 |
+
return {
|
| 51 |
+
'action_type': 'identify_vulnerability',
|
| 52 |
+
'vuln_type': vuln_type,
|
| 53 |
+
'cvss_score': cvss,
|
| 54 |
+
'severity': severity,
|
| 55 |
+
'affected_line': 1,
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
elif 'reviewer_feedback' in obs:
|
| 59 |
+
return {
|
| 60 |
+
'action_type': 'revise_fix',
|
| 61 |
+
'fix_code': 'sanitize_input(parameterized_query)',
|
| 62 |
+
'addressed_feedback': obs.get('reviewer_feedback', 'fixed the issue'),
|
| 63 |
+
}
|
| 64 |
+
else:
|
| 65 |
+
return {
|
| 66 |
+
'action_type': 'propose_fix',
|
| 67 |
+
'fix_code': 'use parameterized query with ? placeholder',
|
| 68 |
+
'explanation': 'Replace string concatenation with parameterized queries',
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def _dependency_action(obs, task_id, turn):
|
| 73 |
+
task_subtype = obs.get('task_subtype', 'flag')
|
| 74 |
+
|
| 75 |
+
if task_subtype == 'flag':
|
| 76 |
+
return {
|
| 77 |
+
'action_type': 'flag_outdated',
|
| 78 |
+
'packages': {'torch': '1.9.0'},
|
| 79 |
+
'deprecated_api': 'torch.autograd.Variable',
|
| 80 |
+
'replacement': 'plain tensor',
|
| 81 |
+
}
|
| 82 |
+
elif task_subtype == 'resolve':
|
| 83 |
+
return {
|
| 84 |
+
'action_type': 'resolve_conflict',
|
| 85 |
+
'packages': {'torch': '2.1.0', 'numpy': '1.24.0'},
|
| 86 |
+
'reasoning': 'PyTorch 2.1 requires NumPy 1.24+',
|
| 87 |
+
}
|
| 88 |
+
else: # migrate
|
| 89 |
+
return {
|
| 90 |
+
'action_type': 'migrate_api',
|
| 91 |
+
'completed_items': ['break_001', 'break_002'],
|
| 92 |
+
'code_changes': {
|
| 93 |
+
'break_001': 'torch.where(condition, x*2, x)',
|
| 94 |
+
'break_002': 'x.shape[0]',
|
| 95 |
+
},
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def _clinical_action(obs, task_id, turn):
|
| 100 |
+
available_steps = obs.get('available_steps', [])
|
| 101 |
+
|
| 102 |
+
if turn == 0:
|
| 103 |
+
return {
|
| 104 |
+
'action_type': 'detect_gap',
|
| 105 |
+
'missing_steps': available_steps[:2] if available_steps else ['unknown_step'],
|
| 106 |
+
'risk_level': 'critical',
|
| 107 |
+
}
|
| 108 |
+
elif turn == 1:
|
| 109 |
+
return {
|
| 110 |
+
'action_type': 'rank_issues',
|
| 111 |
+
'priority_order': available_steps[:3] if available_steps else ['unknown_step'],
|
| 112 |
+
}
|
| 113 |
+
else:
|
| 114 |
+
dep_graph = obs.get('dependency_graph', {})
|
| 115 |
+
# Simple topological sort attempt
|
| 116 |
+
ordered = _simple_topo_sort(available_steps, dep_graph)
|
| 117 |
+
return {
|
| 118 |
+
'action_type': 'order_steps',
|
| 119 |
+
'recovery_steps': ordered,
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def _simple_topo_sort(steps, dep_graph):
|
| 124 |
+
"""Simple topological sort for dependency ordering."""
|
| 125 |
+
if not dep_graph:
|
| 126 |
+
return steps
|
| 127 |
+
result = []
|
| 128 |
+
remaining = set(steps)
|
| 129 |
+
for _ in range(len(steps) + 1):
|
| 130 |
+
if not remaining:
|
| 131 |
+
break
|
| 132 |
+
for step in list(remaining):
|
| 133 |
+
prereqs = dep_graph.get(step, [])
|
| 134 |
+
if all(p in result for p in prereqs):
|
| 135 |
+
result.append(step)
|
| 136 |
+
remaining.remove(step)
|
| 137 |
+
break
|
| 138 |
+
# Add any unresolved steps
|
| 139 |
+
result.extend(remaining)
|
| 140 |
+
return result
|
server/graders/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# server/graders package
|
server/graders/base_grader.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# server/graders/base_grader.py
|
| 2 |
+
# Core grading utilities used by ALL domain graders.
|
| 3 |
+
# Contains: safe_score (Bug 1 fix), penalty functions, grade_dynamic entry point.
|
| 4 |
+
|
| 5 |
+
from typing import Dict, Any, List, Callable
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def safe_score(raw) -> float:
|
| 9 |
+
"""Always clamp to [0.0, 1.0]. Never crash. Handles None, str, out-of-range."""
|
| 10 |
+
if raw is None:
|
| 11 |
+
return 0.0 # BUG 1 FIX — must be first line
|
| 12 |
+
try:
|
| 13 |
+
return round(max(0.0, min(1.0, float(raw))), 4)
|
| 14 |
+
except (TypeError, ValueError):
|
| 15 |
+
return 0.0
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def repetition_penalty(action_type: str, last_actions: List[str], window: int = 3) -> float:
|
| 19 |
+
"""Penalise repeating the same action type in the last N steps."""
|
| 20 |
+
count = last_actions[-window:].count(action_type)
|
| 21 |
+
return -0.15 * count
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def invalid_action_penalty(action_type: str, valid_actions: List[str]) -> float:
|
| 25 |
+
"""Penalise actions not in the valid set for this domain."""
|
| 26 |
+
return -0.20 if action_type not in valid_actions else 0.0
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def harmful_output_penalty(action: Dict, forbidden_patterns: List[str]) -> float:
|
| 30 |
+
"""Penalise destructive patterns like 'os.remove' or 'drop table'."""
|
| 31 |
+
action_str = str(action).lower()
|
| 32 |
+
for p in forbidden_patterns:
|
| 33 |
+
if p.lower() in action_str:
|
| 34 |
+
return -0.30
|
| 35 |
+
return 0.0
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def efficiency_bonus(step_count: int, max_steps: int, done: bool) -> float:
|
| 39 |
+
"""Reward finishing early (before half the max steps)."""
|
| 40 |
+
return 0.10 if done and step_count < max_steps // 2 else 0.0
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def grade_dynamic(
|
| 44 |
+
action: Dict[str, Any],
|
| 45 |
+
session,
|
| 46 |
+
compute_correctness_fn: Callable,
|
| 47 |
+
valid_actions: List[str],
|
| 48 |
+
forbidden_patterns: List[str] = None,
|
| 49 |
+
max_steps: int = 8
|
| 50 |
+
) -> float:
|
| 51 |
+
"""Full reward pipeline. Entry point for all domain graders.
|
| 52 |
+
|
| 53 |
+
Pipeline: invalid check → repetition → correctness → harmful → efficiency → clamp
|
| 54 |
+
"""
|
| 55 |
+
if forbidden_patterns is None:
|
| 56 |
+
forbidden_patterns = []
|
| 57 |
+
|
| 58 |
+
action_type = action.get('action_type', 'unknown')
|
| 59 |
+
|
| 60 |
+
# Penalties
|
| 61 |
+
inv = invalid_action_penalty(action_type, valid_actions)
|
| 62 |
+
rep = repetition_penalty(action_type, session.last_actions)
|
| 63 |
+
harm = harmful_output_penalty(action, forbidden_patterns)
|
| 64 |
+
|
| 65 |
+
# If action type is invalid, skip the grader entirely
|
| 66 |
+
if inv < 0:
|
| 67 |
+
return safe_score(inv + rep)
|
| 68 |
+
|
| 69 |
+
# Core correctness score from domain-specific grader
|
| 70 |
+
correctness = compute_correctness_fn(action, session.task_case)
|
| 71 |
+
|
| 72 |
+
# Efficiency bonus — session.done is always False at this point (set by router
|
| 73 |
+
# AFTER grade() returns), so use correctness >= 0.8 as proxy for "solved well"
|
| 74 |
+
eff = efficiency_bonus(session.step_count + 1, max_steps, correctness is not None and correctness >= 0.8)
|
| 75 |
+
|
| 76 |
+
# Combine and clamp
|
| 77 |
+
raw = correctness + rep + harm + eff
|
| 78 |
+
return safe_score(raw)
|
| 79 |
+
|
server/graders/clinical_grader.py
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# server/graders/clinical_grader.py
|
| 2 |
+
# Grader for Clinical Workflow Chaos Simulator tasks (cli_easy, cli_medium, cli_hard).
|
| 3 |
+
# Bug 2 FIXED: propose_recovery is NOT in VALID_ACTIONS.
|
| 4 |
+
# Uses NDCG ranking and dependency violation counting.
|
| 5 |
+
|
| 6 |
+
import math
|
| 7 |
+
from typing import Dict, List
|
| 8 |
+
from .base_grader import grade_dynamic, safe_score
|
| 9 |
+
|
| 10 |
+
# Bug 2 FIX: propose_recovery is NOT here — it has no grader branch
|
| 11 |
+
VALID_ACTIONS = ['detect_gap', 'rank_issues', 'order_steps']
|
| 12 |
+
FORBIDDEN = []
|
| 13 |
+
RISK_ORDER = ['low', 'medium', 'high', 'critical']
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def _adj_risk(predicted, target):
|
| 17 |
+
"""Check if risk level is off by exactly one level (partial credit)."""
|
| 18 |
+
try:
|
| 19 |
+
return abs(RISK_ORDER.index(predicted) - RISK_ORDER.index(target)) == 1
|
| 20 |
+
except ValueError:
|
| 21 |
+
return False
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def _f1(predicted: List, expected: List) -> float:
|
| 25 |
+
"""Compute F1 score between predicted and expected lists."""
|
| 26 |
+
if not expected:
|
| 27 |
+
return 1.0 if not predicted else 0.0
|
| 28 |
+
if not predicted:
|
| 29 |
+
return 0.0
|
| 30 |
+
p_s = set(str(x).strip() for x in predicted)
|
| 31 |
+
e_s = set(str(x).strip() for x in expected)
|
| 32 |
+
tp = len(p_s & e_s)
|
| 33 |
+
prec = tp / len(p_s) if p_s else 0.0
|
| 34 |
+
rec = tp / len(e_s) if e_s else 0.0
|
| 35 |
+
return round(2 * prec * rec / max(prec + rec, 0.001), 4)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def _ndcg(predicted: List, ideal: List, k: int = None) -> float:
|
| 39 |
+
"""NDCG@k: rewards getting highest-priority items ranked first.
|
| 40 |
+
|
| 41 |
+
If ideal = ['insurance_auth', 'pre_op_consent', 'book_specialist']:
|
| 42 |
+
- Getting 'insurance_auth' first is worth more than getting it last.
|
| 43 |
+
- Each position is worth less than the previous (logarithmic discount).
|
| 44 |
+
- NDCG=1.0 means perfect ranking. NDCG=0.0 means completely reversed.
|
| 45 |
+
"""
|
| 46 |
+
if not ideal:
|
| 47 |
+
return 1.0
|
| 48 |
+
if k is None:
|
| 49 |
+
k = len(ideal)
|
| 50 |
+
|
| 51 |
+
def dcg(order):
|
| 52 |
+
score = 0.0
|
| 53 |
+
for i, item in enumerate(order[:k]):
|
| 54 |
+
if item in ideal:
|
| 55 |
+
relevance = len(ideal) - ideal.index(item)
|
| 56 |
+
score += relevance / math.log2(i + 2)
|
| 57 |
+
return score
|
| 58 |
+
|
| 59 |
+
ideal_dcg = dcg(ideal)
|
| 60 |
+
return round(dcg(predicted) / ideal_dcg, 4) if ideal_dcg > 0 else 0.0
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def _count_violations(proposed: List, dep_graph: Dict) -> int:
|
| 64 |
+
"""Count steps where a prerequisite appears AFTER the step needing it."""
|
| 65 |
+
violations = 0
|
| 66 |
+
for i, step in enumerate(proposed):
|
| 67 |
+
for prereq in dep_graph.get(step, []):
|
| 68 |
+
if prereq not in proposed[:i]:
|
| 69 |
+
violations += 1
|
| 70 |
+
return violations
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def _score_detect(action: Dict, case: Dict) -> float:
|
| 74 |
+
"""Score gap detection (cli_easy). F1 on missing steps + risk level match."""
|
| 75 |
+
exp = case.get('expected_missing_steps', [])
|
| 76 |
+
pred = action.get('missing_steps', [])
|
| 77 |
+
|
| 78 |
+
# Normalize to lists
|
| 79 |
+
if isinstance(exp, str):
|
| 80 |
+
exp = [exp]
|
| 81 |
+
if isinstance(pred, str):
|
| 82 |
+
pred = [pred]
|
| 83 |
+
|
| 84 |
+
# F1 on missing step detection (65% weight)
|
| 85 |
+
step_score = _f1(pred, exp)
|
| 86 |
+
|
| 87 |
+
# Risk level match: exact or adjacent (35% weight)
|
| 88 |
+
er = case.get('expected_risk', '')
|
| 89 |
+
pr = action.get('risk_level', '')
|
| 90 |
+
risk_score = 1.0 if pr == er else (0.5 if _adj_risk(pr, er) else 0.0)
|
| 91 |
+
|
| 92 |
+
return 0.65 * step_score + 0.35 * risk_score
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def _score_rank(action: Dict, case: Dict) -> float:
|
| 96 |
+
"""Score priority ranking (cli_medium). Completeness + NDCG ordering."""
|
| 97 |
+
ideal = case.get('priority_order', [])
|
| 98 |
+
predicted = action.get('priority_order', [])
|
| 99 |
+
|
| 100 |
+
if not ideal:
|
| 101 |
+
return 0.5
|
| 102 |
+
|
| 103 |
+
# Filter predicted to only include valid step IDs (prevents hallucinated IDs from scoring)
|
| 104 |
+
valid_ids = set(case.get('available_steps', []))
|
| 105 |
+
if valid_ids:
|
| 106 |
+
predicted = [p for p in predicted if p in valid_ids]
|
| 107 |
+
|
| 108 |
+
# Completeness: are all items present? (40% weight)
|
| 109 |
+
completeness = _f1(predicted, ideal)
|
| 110 |
+
|
| 111 |
+
# Ranking quality: NDCG (60% weight)
|
| 112 |
+
ranking = _ndcg(predicted, ideal)
|
| 113 |
+
|
| 114 |
+
return 0.40 * completeness + 0.60 * ranking
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def _score_order(action: Dict, case: Dict) -> float:
|
| 118 |
+
"""Score dependency-ordered recovery (cli_hard). Order + completeness + efficiency."""
|
| 119 |
+
dep_graph = case.get('dependency_graph', {})
|
| 120 |
+
required = case.get('required_steps', [])
|
| 121 |
+
proposed = action.get('recovery_steps', [])
|
| 122 |
+
|
| 123 |
+
if not proposed:
|
| 124 |
+
return 0.0
|
| 125 |
+
|
| 126 |
+
# Dependency violations: -0.25 each (40% weight)
|
| 127 |
+
viol = _count_violations(proposed, dep_graph)
|
| 128 |
+
order = max(0.0, 1.0 - viol * 0.25)
|
| 129 |
+
|
| 130 |
+
# Completeness: F1 against required steps (40% weight)
|
| 131 |
+
completeness = _f1(proposed, required)
|
| 132 |
+
|
| 133 |
+
# Efficiency: penalize extra unnecessary steps (20% weight)
|
| 134 |
+
extra = max(0, len(proposed) - len(required))
|
| 135 |
+
efficiency = max(0.0, 1.0 - extra * 0.10)
|
| 136 |
+
|
| 137 |
+
return safe_score(order * 0.40 + completeness * 0.40 + efficiency * 0.20)
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def compute_correctness(action: Dict, case: Dict) -> float:
|
| 141 |
+
"""Route to correct scoring function based on action_type."""
|
| 142 |
+
atype = action.get('action_type')
|
| 143 |
+
if atype == 'detect_gap':
|
| 144 |
+
return _score_detect(action, case)
|
| 145 |
+
if atype == 'rank_issues':
|
| 146 |
+
return _score_rank(action, case)
|
| 147 |
+
if atype == 'order_steps':
|
| 148 |
+
return _score_order(action, case)
|
| 149 |
+
return None
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
def grade(action: Dict, session) -> float:
|
| 153 |
+
"""Entry point called by router. Runs full reward pipeline."""
|
| 154 |
+
return grade_dynamic(action, session, compute_correctness, VALID_ACTIONS, FORBIDDEN, max_steps=6)
|
server/graders/dependency_grader.py
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# server/graders/dependency_grader.py
|
| 2 |
+
# Grader for PyTorch Migration Time-Machine tasks (dep_easy, dep_medium, dep_hard).
|
| 3 |
+
# Covers: deprecated API detection, version conflict resolution, graph-break fixing.
|
| 4 |
+
|
| 5 |
+
from typing import Dict
|
| 6 |
+
from .base_grader import grade_dynamic, safe_score
|
| 7 |
+
|
| 8 |
+
try:
|
| 9 |
+
from packaging.version import Version
|
| 10 |
+
from packaging.specifiers import SpecifierSet
|
| 11 |
+
_HAS_PACKAGING = True
|
| 12 |
+
except ImportError:
|
| 13 |
+
_HAS_PACKAGING = False
|
| 14 |
+
|
| 15 |
+
VALID_ACTIONS = ['flag_outdated', 'resolve_conflict', 'migrate_api', 'validate_tree']
|
| 16 |
+
FORBIDDEN = []
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _normalize_ver(v: str) -> str:
|
| 20 |
+
"""Normalize version: '2.1' → '2.1.0', '1' → '1.0.0'."""
|
| 21 |
+
parts = str(v).strip().split('.')
|
| 22 |
+
while len(parts) < 3:
|
| 23 |
+
parts.append('0')
|
| 24 |
+
return '.'.join(parts[:3])
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def _parse_version_tuple(v: str) -> tuple:
|
| 28 |
+
"""Parse '2.1.0' into (2, 1, 0). Robust fallback when packaging is unavailable."""
|
| 29 |
+
try:
|
| 30 |
+
parts = _normalize_ver(v).split('.')
|
| 31 |
+
return tuple(int(p) for p in parts[:3])
|
| 32 |
+
except (ValueError, AttributeError):
|
| 33 |
+
return (0, 0, 0)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def _simple_version_check(ver_str: str, constraint: str) -> bool:
|
| 37 |
+
"""Check if ver_str satisfies a constraint like '>=1.24,<2.0' WITHOUT packaging.
|
| 38 |
+
Handles: >=, <=, >, <, ==, != and comma-separated constraints.
|
| 39 |
+
"""
|
| 40 |
+
ver = _parse_version_tuple(ver_str)
|
| 41 |
+
parts = [c.strip() for c in constraint.split(',') if c.strip()]
|
| 42 |
+
for part in parts:
|
| 43 |
+
if part.startswith('>='):
|
| 44 |
+
if ver < _parse_version_tuple(part[2:]):
|
| 45 |
+
return False
|
| 46 |
+
elif part.startswith('<='):
|
| 47 |
+
if ver > _parse_version_tuple(part[2:]):
|
| 48 |
+
return False
|
| 49 |
+
elif part.startswith('!='):
|
| 50 |
+
if ver == _parse_version_tuple(part[2:]):
|
| 51 |
+
return False
|
| 52 |
+
elif part.startswith('>'):
|
| 53 |
+
if ver <= _parse_version_tuple(part[1:]):
|
| 54 |
+
return False
|
| 55 |
+
elif part.startswith('<'):
|
| 56 |
+
if ver >= _parse_version_tuple(part[1:]):
|
| 57 |
+
return False
|
| 58 |
+
elif part.startswith('=='):
|
| 59 |
+
if ver != _parse_version_tuple(part[2:]):
|
| 60 |
+
return False
|
| 61 |
+
else:
|
| 62 |
+
# Bare version string — treat as ==
|
| 63 |
+
if ver != _parse_version_tuple(part):
|
| 64 |
+
return False
|
| 65 |
+
return True
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def _f1(predicted, expected):
|
| 69 |
+
"""Compute F1 score between predicted and expected sets."""
|
| 70 |
+
if not expected:
|
| 71 |
+
return 1.0 if not predicted else 0.0
|
| 72 |
+
if not predicted:
|
| 73 |
+
return 0.0
|
| 74 |
+
pred_s = set(str(p).strip() for p in predicted)
|
| 75 |
+
exp_s = set(str(e).strip() for e in expected)
|
| 76 |
+
tp = len(pred_s & exp_s)
|
| 77 |
+
p = tp / len(pred_s) if pred_s else 0.0
|
| 78 |
+
r = tp / len(exp_s) if exp_s else 0.0
|
| 79 |
+
return round(2 * p * r / max(p + r, 0.001), 4)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def _downgrades(proposed: Dict, case: Dict) -> int:
|
| 83 |
+
"""Count unnecessary version downgrades (dep_medium penalty)."""
|
| 84 |
+
reqs = case.get('requirements', {})
|
| 85 |
+
count = 0
|
| 86 |
+
for pkg, ver in proposed.items():
|
| 87 |
+
if pkg in reqs:
|
| 88 |
+
try:
|
| 89 |
+
if _HAS_PACKAGING:
|
| 90 |
+
if Version(_normalize_ver(ver)) < Version(_normalize_ver(reqs[pkg])):
|
| 91 |
+
count += 1
|
| 92 |
+
else:
|
| 93 |
+
if _parse_version_tuple(ver) < _parse_version_tuple(reqs[pkg]):
|
| 94 |
+
count += 1
|
| 95 |
+
except Exception:
|
| 96 |
+
pass
|
| 97 |
+
return count
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def _score_flag(action: Dict, case: Dict) -> float:
|
| 101 |
+
"""Score deprecated API detection (dep_easy)."""
|
| 102 |
+
exp = set(case.get('expected_outdated_packages', []))
|
| 103 |
+
flagged = set(action.get('packages', {}).keys())
|
| 104 |
+
|
| 105 |
+
# F1 on package detection (55% weight)
|
| 106 |
+
p = len(flagged & exp) / max(len(flagged), 1)
|
| 107 |
+
r = len(flagged & exp) / max(len(exp), 1)
|
| 108 |
+
f1 = 2 * p * r / max(p + r, 0.001)
|
| 109 |
+
|
| 110 |
+
# Deprecated API match (45% weight) — fuzzy for model variations
|
| 111 |
+
expected_api = case.get('expected_deprecated_api', '')
|
| 112 |
+
actual_api = action.get('deprecated_api', '') or ''
|
| 113 |
+
if actual_api == expected_api:
|
| 114 |
+
dep_ok = 1.0
|
| 115 |
+
elif expected_api and expected_api.split('.')[-1] in actual_api:
|
| 116 |
+
dep_ok = 0.7 # last segment match e.g. "Variable" in "autograd.Variable"
|
| 117 |
+
elif expected_api and any(p in actual_api for p in expected_api.split('.')):
|
| 118 |
+
dep_ok = 0.4 # partial segment match
|
| 119 |
+
else:
|
| 120 |
+
dep_ok = 0.0
|
| 121 |
+
|
| 122 |
+
return f1 * 0.55 + dep_ok * 0.45
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
def _score_resolve(action: Dict, case: Dict) -> float:
|
| 126 |
+
"""Score version conflict resolution (dep_medium). Cross-checks compatibility matrix constraints."""
|
| 127 |
+
compat = case.get('compatibility_matrix', {})
|
| 128 |
+
proposed = action.get('packages', {})
|
| 129 |
+
conflict_pkgs = case.get('conflict_packages', [])
|
| 130 |
+
|
| 131 |
+
# Count valid proposed versions WITH cross-constraint checking
|
| 132 |
+
valid = 0
|
| 133 |
+
for pkg, ver in proposed.items():
|
| 134 |
+
if pkg not in compat:
|
| 135 |
+
continue
|
| 136 |
+
norm_ver = _normalize_ver(ver)
|
| 137 |
+
# Try exact match first, then normalized
|
| 138 |
+
pkg_versions = compat[pkg]
|
| 139 |
+
matched_ver = None
|
| 140 |
+
if ver in pkg_versions:
|
| 141 |
+
matched_ver = ver
|
| 142 |
+
elif norm_ver in pkg_versions:
|
| 143 |
+
matched_ver = norm_ver
|
| 144 |
+
else:
|
| 145 |
+
for k in pkg_versions:
|
| 146 |
+
if _normalize_ver(k) == norm_ver:
|
| 147 |
+
matched_ver = k
|
| 148 |
+
break
|
| 149 |
+
# Patch-level fuzzy: match major.minor only (e.g. "2.1.1" → "2.1.0")
|
| 150 |
+
if not matched_ver:
|
| 151 |
+
norm_major_minor = '.'.join(norm_ver.split('.')[:2])
|
| 152 |
+
for k in pkg_versions:
|
| 153 |
+
if '.'.join(_normalize_ver(k).split('.')[:2]) == norm_major_minor:
|
| 154 |
+
matched_ver = k
|
| 155 |
+
break
|
| 156 |
+
if not matched_ver:
|
| 157 |
+
continue
|
| 158 |
+
|
| 159 |
+
# Check cross-dependency constraints using packaging or fallback
|
| 160 |
+
deps = pkg_versions[matched_ver]
|
| 161 |
+
cross_ok = True
|
| 162 |
+
if isinstance(deps, dict):
|
| 163 |
+
for dep_pkg, constraint in deps.items():
|
| 164 |
+
if dep_pkg in proposed:
|
| 165 |
+
dep_ver = _normalize_ver(proposed[dep_pkg])
|
| 166 |
+
try:
|
| 167 |
+
if _HAS_PACKAGING:
|
| 168 |
+
if Version(dep_ver) not in SpecifierSet(constraint):
|
| 169 |
+
cross_ok = False
|
| 170 |
+
break
|
| 171 |
+
else:
|
| 172 |
+
if not _simple_version_check(dep_ver, constraint):
|
| 173 |
+
cross_ok = False
|
| 174 |
+
break
|
| 175 |
+
except Exception:
|
| 176 |
+
pass
|
| 177 |
+
if cross_ok:
|
| 178 |
+
valid += 1
|
| 179 |
+
|
| 180 |
+
base = valid / max(len(conflict_pkgs), 1)
|
| 181 |
+
bonus = 0.15 if valid == len(conflict_pkgs) else 0.0
|
| 182 |
+
down = _downgrades(proposed, case) * 0.10
|
| 183 |
+
|
| 184 |
+
return safe_score(base + bonus - down)
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
def _score_migrate(action: Dict, case: Dict) -> float:
|
| 188 |
+
"""Score graph-break migration (dep_hard). Checks coverage, order, fix quality."""
|
| 189 |
+
checklist = case.get('graph_breaks', []) # list of break IDs
|
| 190 |
+
dep_graph = case.get('checklist_dependency_graph', {})
|
| 191 |
+
completed = action.get('completed_items', [])
|
| 192 |
+
fix_map = case.get('correct_fix_map', {}) # break_id -> required_token
|
| 193 |
+
|
| 194 |
+
if not checklist:
|
| 195 |
+
return 0.5
|
| 196 |
+
|
| 197 |
+
# Early exit: if agent submitted nothing, score is 0
|
| 198 |
+
if not completed:
|
| 199 |
+
return 0.0
|
| 200 |
+
|
| 201 |
+
# Dependency order violations
|
| 202 |
+
viol = sum(
|
| 203 |
+
1 for item in completed
|
| 204 |
+
for pre in dep_graph.get(item, [])
|
| 205 |
+
if pre not in completed
|
| 206 |
+
)
|
| 207 |
+
order_score = max(0.0, 1.0 - viol * 0.20)
|
| 208 |
+
|
| 209 |
+
# Checklist coverage
|
| 210 |
+
covered = [b for b in checklist if b in completed]
|
| 211 |
+
completeness = len(covered) / max(len(checklist), 1)
|
| 212 |
+
|
| 213 |
+
# Fix quality: does each fix contain the required token?
|
| 214 |
+
fix_qs = []
|
| 215 |
+
for b in covered:
|
| 216 |
+
if b not in fix_map:
|
| 217 |
+
continue
|
| 218 |
+
expected_token = fix_map[b].lower()
|
| 219 |
+
actual_fix = str(action.get('code_changes', {}).get(b, '')).lower()
|
| 220 |
+
if expected_token in actual_fix or actual_fix in expected_token:
|
| 221 |
+
fix_qs.append(1.0)
|
| 222 |
+
else:
|
| 223 |
+
fix_qs.append(0.6) # Generous partial credit
|
| 224 |
+
fix_quality = sum(fix_qs) / max(len(fix_qs), 1) if fix_qs else 0.0
|
| 225 |
+
|
| 226 |
+
return safe_score(order_score * 0.30 + completeness * 0.40 + fix_quality * 0.30)
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
def compute_correctness(action: Dict, case: Dict) -> float:
|
| 230 |
+
"""Route to correct scoring function based on action_type."""
|
| 231 |
+
atype = action.get('action_type')
|
| 232 |
+
if atype == 'flag_outdated':
|
| 233 |
+
return _score_flag(action, case)
|
| 234 |
+
if atype == 'resolve_conflict':
|
| 235 |
+
return _score_resolve(action, case)
|
| 236 |
+
if atype in ('migrate_api', 'validate_tree'):
|
| 237 |
+
return _score_migrate(action, case)
|
| 238 |
+
return None
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
def grade(action: Dict, session) -> float:
|
| 242 |
+
"""Entry point called by router. Runs full reward pipeline."""
|
| 243 |
+
return grade_dynamic(action, session, compute_correctness, VALID_ACTIONS, FORBIDDEN, max_steps=8)
|
server/graders/security_grader.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# server/graders/security_grader.py
|
| 2 |
+
# Grader for MCP Security Sandbox tasks (sec_easy, sec_medium, sec_hard).
|
| 3 |
+
# Bug 4 FIXED: _score_identify does NOT early-return on wrong vuln_type.
|
| 4 |
+
|
| 5 |
+
from typing import Dict
|
| 6 |
+
from .base_grader import grade_dynamic, safe_score
|
| 7 |
+
|
| 8 |
+
VALID_ACTIONS = ['identify_vulnerability', 'propose_fix', 'revise_fix']
|
| 9 |
+
FORBIDDEN = ['os.remove', 'shutil.rmtree', 'drop table', 'delete from']
|
| 10 |
+
SEV_ORDER = ['low', 'medium', 'high', 'critical']
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def _adj_sev(predicted, target):
|
| 14 |
+
"""Check if severity is off by exactly one level (partial credit)."""
|
| 15 |
+
try:
|
| 16 |
+
return abs(SEV_ORDER.index(predicted) - SEV_ORDER.index(target)) == 1
|
| 17 |
+
except ValueError:
|
| 18 |
+
return False
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def _score_identify(action: Dict, case: Dict) -> float:
|
| 22 |
+
"""Score vulnerability identification. Bug 4 FIX: always score all 3 components."""
|
| 23 |
+
# Detection: correct vuln_type? (45% weight)
|
| 24 |
+
det = 1.0 if action.get('vuln_type') == case.get('expected_vuln_type', '') else 0.0
|
| 25 |
+
|
| 26 |
+
# BUG 4 FIX: do NOT early-return here. Always score CVSS and severity.
|
| 27 |
+
# This gives the agent partial credit even when vuln_type is wrong.
|
| 28 |
+
|
| 29 |
+
# CVSS: within expected range? (30% weight)
|
| 30 |
+
lo, hi = case.get('cvss_range', [0.0, 10.0])
|
| 31 |
+
v = float(action.get('cvss_score', -1))
|
| 32 |
+
cvss = 1.0 if lo <= v <= hi else (0.5 if abs(v - (lo + hi) / 2) <= 3.0 else 0.0)
|
| 33 |
+
|
| 34 |
+
# Severity: exact match or adjacent? (25% weight)
|
| 35 |
+
s, es = action.get('severity', ''), case.get('expected_severity', '')
|
| 36 |
+
sev = 1.0 if s == es else (0.4 if _adj_sev(s, es) else 0.0)
|
| 37 |
+
|
| 38 |
+
return det * 0.45 + cvss * 0.30 + sev * 0.25
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def _score_propose(action: Dict, case: Dict) -> float:
|
| 42 |
+
"""Score proposed fix. Checks token coverage and identifier preservation."""
|
| 43 |
+
tokens = case.get('required_fix_tokens', [])
|
| 44 |
+
if isinstance(tokens, dict):
|
| 45 |
+
tokens = tokens.get(case.get('expected_vuln_type', ''), [])
|
| 46 |
+
# Safety: flatten to list of strings only
|
| 47 |
+
tokens = [t for t in tokens if isinstance(t, str)]
|
| 48 |
+
|
| 49 |
+
fix = action.get('fix_code', '')
|
| 50 |
+
if not fix:
|
| 51 |
+
return 0.0
|
| 52 |
+
|
| 53 |
+
# Token coverage: allow missing 1 token to still get full score
|
| 54 |
+
if not tokens:
|
| 55 |
+
coverage = 0.5
|
| 56 |
+
else:
|
| 57 |
+
divisor = max(1, len(tokens) - 1)
|
| 58 |
+
coverage = min(1.0, sum(1 for t in tokens if t.lower() in fix.lower()) / divisor)
|
| 59 |
+
|
| 60 |
+
# Identifier preservation: did the fix keep the key function name?
|
| 61 |
+
key_id = case.get('must_preserve_identifier', '')
|
| 62 |
+
preservation = 0.15 if key_id and key_id in fix else 0.0
|
| 63 |
+
|
| 64 |
+
# Floor: any non-empty fix_code gets at least 0.25 (agent showed correct workflow)
|
| 65 |
+
return max(0.25, safe_score(coverage + preservation))
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def _score_revise(action: Dict, case: Dict) -> float:
|
| 69 |
+
"""Score revised fix after reviewer feedback. Checks coverage and regression."""
|
| 70 |
+
kw = case.get('current_feedback_keywords', [])
|
| 71 |
+
addressed = action.get('addressed_feedback', '')
|
| 72 |
+
fix = action.get('fix_code', '')
|
| 73 |
+
|
| 74 |
+
# Feedback keyword coverage: allow missing 1 keyword
|
| 75 |
+
divisor = max(1, len(kw) - 1)
|
| 76 |
+
cov = min(1.0, sum(1 for k in kw if k.lower() in addressed.lower()) / divisor)
|
| 77 |
+
|
| 78 |
+
# Regression check: does the fix_code still contain the original vulnerability? (-20%)
|
| 79 |
+
reg = 0.20 if case.get('original_vuln_pattern', '') in fix else 0.0
|
| 80 |
+
|
| 81 |
+
# Floor: any non-empty addressed_feedback gets at least 0.20
|
| 82 |
+
return max(0.20, safe_score(cov - reg))
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def compute_correctness(action: Dict, case: Dict) -> float:
|
| 86 |
+
"""Route to correct scoring function based on action_type."""
|
| 87 |
+
atype = action.get('action_type')
|
| 88 |
+
if atype == 'identify_vulnerability':
|
| 89 |
+
return _score_identify(action, case)
|
| 90 |
+
if atype == 'propose_fix':
|
| 91 |
+
return _score_propose(action, case)
|
| 92 |
+
if atype == 'revise_fix':
|
| 93 |
+
return _score_revise(action, case)
|
| 94 |
+
return None # safe_score(None) = 0.0
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def grade(action: Dict, session) -> float:
|
| 98 |
+
"""Entry point called by router. Runs full reward pipeline."""
|
| 99 |
+
return grade_dynamic(action, session, compute_correctness, VALID_ACTIONS, FORBIDDEN, max_steps=8)
|
server/models/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# server/models package
|
server/models/clinical_models.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# server/models/clinical_models.py
|
| 2 |
+
from pydantic import BaseModel, Field
|
| 3 |
+
from typing import List
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class DetectGap(BaseModel):
|
| 7 |
+
action_type: str = 'detect_gap'
|
| 8 |
+
missing_steps: List[str] = Field(..., description='IDs of missing workflow steps')
|
| 9 |
+
risk_level: str = Field(..., description='critical|high|medium|low')
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class RankIssues(BaseModel):
|
| 13 |
+
action_type: str = 'rank_issues'
|
| 14 |
+
priority_order: List[str] = Field(..., description='step IDs from highest to lowest priority')
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class OrderSteps(BaseModel):
|
| 18 |
+
action_type: str = 'order_steps'
|
| 19 |
+
recovery_steps: List[str] = Field(..., description='step IDs in dependency-safe execution order')
|
server/models/dependency_models.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# server/models/dependency_models.py
|
| 2 |
+
from pydantic import BaseModel, Field
|
| 3 |
+
from typing import Dict, List, Optional
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class FlagOutdated(BaseModel):
|
| 7 |
+
action_type: str = 'flag_outdated'
|
| 8 |
+
packages: Dict[str, str] = Field(..., description='package_name: current_version')
|
| 9 |
+
deprecated_api: Optional[str] = None
|
| 10 |
+
replacement: Optional[str] = None
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class ResolveConflict(BaseModel):
|
| 14 |
+
action_type: str = 'resolve_conflict'
|
| 15 |
+
packages: Dict[str, str] = Field(..., description='package_name: proposed_version')
|
| 16 |
+
reasoning: str = Field(..., max_length=100)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class MigrateApi(BaseModel):
|
| 20 |
+
action_type: str = 'migrate_api'
|
| 21 |
+
completed_items: List[str] = Field(..., description='list of break_ids fixed')
|
| 22 |
+
code_changes: Dict[str, str] = Field(..., description='break_id: fix summary')
|
server/models/security_models.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# server/models/security_models.py
|
| 2 |
+
from pydantic import BaseModel, Field
|
| 3 |
+
from typing import Optional
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class IdentifyVulnerability(BaseModel):
|
| 7 |
+
action_type: str = 'identify_vulnerability'
|
| 8 |
+
vuln_type: str = Field(..., description='Type of vulnerability detected')
|
| 9 |
+
cvss_score: float = Field(..., ge=0.0, le=10.0)
|
| 10 |
+
severity: str = Field(..., description='critical|high|medium|low')
|
| 11 |
+
affected_line: int = Field(..., ge=1)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class ProposeFix(BaseModel):
|
| 15 |
+
action_type: str = 'propose_fix'
|
| 16 |
+
fix_code: str = Field(..., max_length=500)
|
| 17 |
+
explanation: str = Field(..., max_length=200)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class ReviseFix(BaseModel):
|
| 21 |
+
action_type: str = 'revise_fix'
|
| 22 |
+
fix_code: str = Field(..., max_length=500)
|
| 23 |
+
addressed_feedback: str = Field(..., max_length=200)
|
server/router.py
ADDED
|
@@ -0,0 +1,322 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# server/router.py
|
| 2 |
+
# Central dispatcher. Routes validated actions to the correct domain grader.
|
| 3 |
+
# Returns rich observations with task_subtype, score_details, and data-driven done conditions.
|
| 4 |
+
|
| 5 |
+
from typing import Dict
|
| 6 |
+
from .session import SessionState
|
| 7 |
+
from .graders import security_grader, dependency_grader, clinical_grader
|
| 8 |
+
|
| 9 |
+
# Map domain names to their grader modules
|
| 10 |
+
GRADERS = {
|
| 11 |
+
'security': security_grader,
|
| 12 |
+
'dependency': dependency_grader,
|
| 13 |
+
'clinical': clinical_grader,
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def route_step(session: SessionState, action: Dict) -> Dict:
|
| 18 |
+
"""Route a validated action to the correct grader and return enriched result."""
|
| 19 |
+
grader = GRADERS.get(session.task_type)
|
| 20 |
+
if not grader:
|
| 21 |
+
return {
|
| 22 |
+
'reward': 0.0,
|
| 23 |
+
'done': True,
|
| 24 |
+
'observation': {'error': f'Unknown task_type: {session.task_type}'},
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
# Run the domain grader
|
| 28 |
+
reward = grader.grade(action, session)
|
| 29 |
+
|
| 30 |
+
# Check if episode is done (data-driven from case)
|
| 31 |
+
case = session.task_case
|
| 32 |
+
max_steps = case.get('max_steps', 8)
|
| 33 |
+
done = _check_done(session, action, reward, max_steps)
|
| 34 |
+
|
| 35 |
+
# Build the next observation (rich, self-describing)
|
| 36 |
+
obs = _build_step_obs(session, action, reward, done)
|
| 37 |
+
|
| 38 |
+
# Score breakdown for debugging and UI
|
| 39 |
+
score_details = _compute_score_details(action, session)
|
| 40 |
+
|
| 41 |
+
return {
|
| 42 |
+
'episode_id': session.episode_id,
|
| 43 |
+
'step_count': session.step_count + 1,
|
| 44 |
+
'reward': round(float(reward), 4),
|
| 45 |
+
'done': bool(done),
|
| 46 |
+
'observation': obs,
|
| 47 |
+
'score_details': score_details,
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def _check_done(session: SessionState, action: Dict, reward: float, max_steps: int) -> bool:
|
| 52 |
+
"""Data-driven done condition from case definition.
|
| 53 |
+
|
| 54 |
+
Three triggers (from OpenEnv tech ref Section 7.2):
|
| 55 |
+
1. required_sequence complete (all required action types performed)
|
| 56 |
+
2. reward >= completion_threshold
|
| 57 |
+
3. max steps reached
|
| 58 |
+
"""
|
| 59 |
+
next_step = session.step_count + 1
|
| 60 |
+
case = session.task_case
|
| 61 |
+
|
| 62 |
+
# Always done if max steps reached
|
| 63 |
+
if next_step >= max_steps:
|
| 64 |
+
return True
|
| 65 |
+
|
| 66 |
+
# Check minimum actions before allowing completion by threshold
|
| 67 |
+
done_conditions = case.get('done_conditions', {})
|
| 68 |
+
min_actions = done_conditions.get('min_actions', 1)
|
| 69 |
+
if next_step < min_actions:
|
| 70 |
+
return False
|
| 71 |
+
|
| 72 |
+
# Completion threshold from case
|
| 73 |
+
threshold = case.get('completion_threshold', 0.85)
|
| 74 |
+
if reward >= threshold:
|
| 75 |
+
return True
|
| 76 |
+
|
| 77 |
+
# Required sequence check — only end if agent is actually scoring well
|
| 78 |
+
# This prevents premature termination when all action types are done but rewards are 0.0
|
| 79 |
+
required_seq = done_conditions.get('required_sequence', [])
|
| 80 |
+
if required_seq and reward >= 0.3:
|
| 81 |
+
all_actions = session.last_actions + [action.get('action_type', '')]
|
| 82 |
+
seq_complete = all(a in all_actions for a in required_seq)
|
| 83 |
+
if seq_complete:
|
| 84 |
+
return True
|
| 85 |
+
|
| 86 |
+
return False
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def build_initial_obs(session: SessionState) -> dict:
|
| 90 |
+
"""Build the initial observation returned by /reset.
|
| 91 |
+
|
| 92 |
+
CRITICAL: Every observation MUST include task_type, task_subtype,
|
| 93 |
+
task_description, and available_actions with params.
|
| 94 |
+
"""
|
| 95 |
+
case = session.task_case
|
| 96 |
+
task_type = session.task_type
|
| 97 |
+
task_id = session.task_id
|
| 98 |
+
|
| 99 |
+
obs = {
|
| 100 |
+
'task_type': task_type,
|
| 101 |
+
'task_id': task_id,
|
| 102 |
+
'task_subtype': case.get('task_subtype', 'standard'),
|
| 103 |
+
'task_description': case.get('task_description', ''),
|
| 104 |
+
'turn': 0,
|
| 105 |
+
'done': False,
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
if task_type == 'security':
|
| 109 |
+
obs['code_snippet'] = case.get('tool_call', '')
|
| 110 |
+
obs['reviewer_feedback'] = None
|
| 111 |
+
obs['available_actions'] = [
|
| 112 |
+
{'name': 'identify_vulnerability',
|
| 113 |
+
'params': ['vuln_type:str', 'cvss_score:float', 'severity:str', 'affected_line:int']},
|
| 114 |
+
{'name': 'propose_fix',
|
| 115 |
+
'params': ['fix_code:str', 'explanation:str']},
|
| 116 |
+
{'name': 'revise_fix',
|
| 117 |
+
'params': ['fix_code:str', 'addressed_feedback:str']},
|
| 118 |
+
]
|
| 119 |
+
|
| 120 |
+
elif task_type == 'dependency':
|
| 121 |
+
obs['code_snippet'] = case.get('code_snippet', '')
|
| 122 |
+
subtype = case.get('task_subtype', '')
|
| 123 |
+
if subtype == 'flag':
|
| 124 |
+
obs['requirements'] = case.get('requirements', {})
|
| 125 |
+
obs['available_actions'] = [
|
| 126 |
+
{'name': 'flag_outdated',
|
| 127 |
+
'params': ['packages:dict', 'deprecated_api:str|null', 'replacement:str|null']},
|
| 128 |
+
]
|
| 129 |
+
elif subtype == 'resolve':
|
| 130 |
+
obs['conflict_packages'] = case.get('conflict_packages', [])
|
| 131 |
+
obs['compatibility_matrix'] = case.get('compatibility_matrix', {})
|
| 132 |
+
obs['current_requirements'] = case.get('requirements', {})
|
| 133 |
+
obs['compatibility_hint'] = 'Check torch 2.x compatibility with numpy and cuda-toolkit versions'
|
| 134 |
+
obs['available_actions'] = [
|
| 135 |
+
{'name': 'resolve_conflict',
|
| 136 |
+
'params': ['packages:dict', 'reasoning:str']},
|
| 137 |
+
]
|
| 138 |
+
elif subtype == 'migrate':
|
| 139 |
+
obs['graph_break_report'] = case.get('graph_break_report', case.get('break_descriptions', []))
|
| 140 |
+
obs['available_actions'] = [
|
| 141 |
+
{'name': 'migrate_api',
|
| 142 |
+
'params': ['completed_items:list', 'code_changes:dict']},
|
| 143 |
+
{'name': 'validate_tree',
|
| 144 |
+
'params': ['completed_items:list']},
|
| 145 |
+
]
|
| 146 |
+
|
| 147 |
+
elif task_type == 'clinical':
|
| 148 |
+
obs['patient_id'] = case.get('patient_id', '')
|
| 149 |
+
obs['events'] = case.get('events', case.get('patient_events', []))
|
| 150 |
+
obs['available_steps'] = case.get('available_steps', [])
|
| 151 |
+
if task_id in ('cli_medium', 'cli_hard'):
|
| 152 |
+
obs['dependency_graph'] = case.get('dependency_graph', {})
|
| 153 |
+
obs['available_actions'] = [
|
| 154 |
+
{'name': 'detect_gap',
|
| 155 |
+
'params': ['missing_steps:list', 'risk_level:str']},
|
| 156 |
+
{'name': 'rank_issues',
|
| 157 |
+
'params': ['priority_order:list']},
|
| 158 |
+
{'name': 'order_steps',
|
| 159 |
+
'params': ['recovery_steps:list']},
|
| 160 |
+
]
|
| 161 |
+
|
| 162 |
+
return obs
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
def _build_step_obs(session: SessionState, action: Dict, reward: float, done: bool) -> Dict:
|
| 166 |
+
"""Build observation returned after each step().
|
| 167 |
+
|
| 168 |
+
Always includes: task_type, task_id, task_subtype, turn, done.
|
| 169 |
+
Includes domain-specific data so generic agents can navigate.
|
| 170 |
+
"""
|
| 171 |
+
case = session.task_case
|
| 172 |
+
task_type = session.task_type
|
| 173 |
+
|
| 174 |
+
obs = {
|
| 175 |
+
'task_type': task_type,
|
| 176 |
+
'task_id': session.task_id,
|
| 177 |
+
'task_subtype': case.get('task_subtype', 'standard'),
|
| 178 |
+
'turn': session.step_count + 1,
|
| 179 |
+
'done': done,
|
| 180 |
+
'last_reward': round(reward, 4),
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
if done:
|
| 184 |
+
obs['message'] = 'Episode complete.'
|
| 185 |
+
return obs
|
| 186 |
+
|
| 187 |
+
if task_type == 'security':
|
| 188 |
+
obs['task_description'] = case.get('task_description', '')
|
| 189 |
+
obs['code_snippet'] = case.get('tool_call', '')
|
| 190 |
+
atype = action.get('action_type', '')
|
| 191 |
+
# Provide reviewer feedback after propose_fix (for medium/hard)
|
| 192 |
+
if atype == 'propose_fix':
|
| 193 |
+
fb = case.get('reviewer_feedback', '')
|
| 194 |
+
if fb:
|
| 195 |
+
obs['reviewer_feedback'] = fb
|
| 196 |
+
elif atype == 'revise_fix':
|
| 197 |
+
# For hard tasks with feedback sequence
|
| 198 |
+
fb_seq = case.get('reviewer_feedback_sequence', [])
|
| 199 |
+
if fb_seq:
|
| 200 |
+
fb_idx = min(len(session.history), len(fb_seq) - 1)
|
| 201 |
+
if fb_idx >= 0:
|
| 202 |
+
obs['reviewer_feedback'] = fb_seq[fb_idx]
|
| 203 |
+
obs['available_actions'] = [
|
| 204 |
+
{'name': 'identify_vulnerability',
|
| 205 |
+
'params': ['vuln_type:str', 'cvss_score:float', 'severity:str', 'affected_line:int']},
|
| 206 |
+
{'name': 'propose_fix',
|
| 207 |
+
'params': ['fix_code:str', 'explanation:str']},
|
| 208 |
+
{'name': 'revise_fix',
|
| 209 |
+
'params': ['fix_code:str', 'addressed_feedback:str']},
|
| 210 |
+
]
|
| 211 |
+
|
| 212 |
+
elif task_type == 'dependency':
|
| 213 |
+
obs['task_description'] = case.get('task_description', '')
|
| 214 |
+
obs['code_snippet'] = case.get('code_snippet', '')
|
| 215 |
+
subtype = case.get('task_subtype', '')
|
| 216 |
+
if subtype == 'migrate':
|
| 217 |
+
obs['graph_break_report'] = case.get('graph_break_report', case.get('break_descriptions', []))
|
| 218 |
+
obs['available_actions'] = [
|
| 219 |
+
{'name': 'migrate_api', 'params': ['completed_items:list', 'code_changes:dict']},
|
| 220 |
+
{'name': 'validate_tree', 'params': ['completed_items:list']},
|
| 221 |
+
]
|
| 222 |
+
elif subtype == 'resolve':
|
| 223 |
+
obs['conflict_packages'] = case.get('conflict_packages', [])
|
| 224 |
+
obs['available_actions'] = [
|
| 225 |
+
{'name': 'resolve_conflict', 'params': ['packages:dict', 'reasoning:str']},
|
| 226 |
+
]
|
| 227 |
+
else:
|
| 228 |
+
obs['available_actions'] = [
|
| 229 |
+
{'name': 'flag_outdated',
|
| 230 |
+
'params': ['packages:dict', 'deprecated_api:str|null', 'replacement:str|null']},
|
| 231 |
+
]
|
| 232 |
+
|
| 233 |
+
elif task_type == 'clinical':
|
| 234 |
+
obs['task_description'] = case.get('task_description', '')
|
| 235 |
+
obs['patient_id'] = case.get('patient_id', '')
|
| 236 |
+
obs['events'] = case.get('events', case.get('patient_events', []))
|
| 237 |
+
obs['available_steps'] = case.get('available_steps', [])
|
| 238 |
+
if session.task_id in ('cli_medium', 'cli_hard'):
|
| 239 |
+
obs['dependency_graph'] = case.get('dependency_graph', {})
|
| 240 |
+
obs['available_actions'] = [
|
| 241 |
+
{'name': 'detect_gap', 'params': ['missing_steps:list', 'risk_level:str']},
|
| 242 |
+
{'name': 'rank_issues', 'params': ['priority_order:list']},
|
| 243 |
+
{'name': 'order_steps', 'params': ['recovery_steps:list']},
|
| 244 |
+
]
|
| 245 |
+
|
| 246 |
+
return obs
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
def _compute_score_details(action: Dict, session: SessionState) -> Dict[str, float]:
|
| 250 |
+
"""Compute per-component score breakdown for UI display and judge transparency."""
|
| 251 |
+
atype = action.get('action_type', '')
|
| 252 |
+
case = session.task_case
|
| 253 |
+
details = {}
|
| 254 |
+
|
| 255 |
+
if session.task_type == 'security':
|
| 256 |
+
if atype == 'identify_vulnerability':
|
| 257 |
+
details['vuln_type_match'] = 1.0 if action.get('vuln_type') == case.get('expected_vuln_type') else 0.0
|
| 258 |
+
lo, hi = case.get('cvss_range', [0, 10])
|
| 259 |
+
try:
|
| 260 |
+
v = float(action.get('cvss_score', -1))
|
| 261 |
+
details['cvss_in_range'] = 1.0 if lo <= v <= hi else (0.5 if abs(v - (lo + hi) / 2) <= 3.0 else 0.0)
|
| 262 |
+
except (TypeError, ValueError):
|
| 263 |
+
details['cvss_in_range'] = 0.0
|
| 264 |
+
details['severity_match'] = 1.0 if action.get('severity') == case.get('expected_severity') else 0.0
|
| 265 |
+
elif atype == 'propose_fix':
|
| 266 |
+
tokens = case.get('required_fix_tokens', [])
|
| 267 |
+
if isinstance(tokens, dict):
|
| 268 |
+
tokens = tokens.get(case.get('expected_vuln_type', ''), [])
|
| 269 |
+
tokens = [t for t in tokens if isinstance(t, str)]
|
| 270 |
+
fix = action.get('fix_code', '')
|
| 271 |
+
details['token_coverage'] = sum(1 for t in tokens if t.lower() in fix.lower()) / max(len(tokens), 1) if fix else 0.0
|
| 272 |
+
key_id = case.get('must_preserve_identifier', '')
|
| 273 |
+
details['id_preserved'] = 1.0 if key_id and key_id in fix else 0.0
|
| 274 |
+
elif atype == 'revise_fix':
|
| 275 |
+
kws = case.get('current_feedback_keywords', [])
|
| 276 |
+
addressed = action.get('addressed_feedback', '')
|
| 277 |
+
details['feedback_addressed'] = sum(1 for kw in kws if kw.lower() in addressed.lower()) / max(len(kws), 1) if addressed else 0.0
|
| 278 |
+
orig = case.get('original_vuln_pattern', '')
|
| 279 |
+
fix = action.get('fix_code', '')
|
| 280 |
+
details['vuln_removed'] = 1.0 if orig and orig not in fix else 0.3
|
| 281 |
+
|
| 282 |
+
elif session.task_type == 'dependency':
|
| 283 |
+
if atype == 'flag_outdated':
|
| 284 |
+
expected = set(case.get('expected_outdated_packages', []))
|
| 285 |
+
provided = set(action.get('packages', {}).keys())
|
| 286 |
+
if expected:
|
| 287 |
+
tp = len(expected & provided)
|
| 288 |
+
p = tp / max(len(provided), 1)
|
| 289 |
+
r = tp / max(len(expected), 1)
|
| 290 |
+
details['pkg_f1'] = round(2 * p * r / max(p + r, 0.001), 4)
|
| 291 |
+
details['api_match'] = 1.0 if action.get('deprecated_api') == case.get('expected_deprecated_api') else 0.0
|
| 292 |
+
elif atype == 'resolve_conflict':
|
| 293 |
+
proposed = action.get('packages', {})
|
| 294 |
+
conflict = case.get('conflict_packages', [])
|
| 295 |
+
details['packages_proposed'] = len(proposed)
|
| 296 |
+
details['conflict_count'] = len(conflict)
|
| 297 |
+
elif atype in ('migrate_api', 'validate_tree'):
|
| 298 |
+
checklist = case.get('graph_breaks', [])
|
| 299 |
+
completed = action.get('completed_items', [])
|
| 300 |
+
details['items_completed'] = len(completed)
|
| 301 |
+
details['total_items'] = len(checklist)
|
| 302 |
+
|
| 303 |
+
elif session.task_type == 'clinical':
|
| 304 |
+
if atype == 'detect_gap':
|
| 305 |
+
expected = set(case.get('expected_missing_steps', []))
|
| 306 |
+
provided = set(action.get('missing_steps', []))
|
| 307 |
+
if expected:
|
| 308 |
+
tp = len(expected & provided)
|
| 309 |
+
p = tp / max(len(provided), 1)
|
| 310 |
+
r = tp / max(len(expected), 1)
|
| 311 |
+
details['step_f1'] = round(2 * p * r / max(p + r, 0.001), 4)
|
| 312 |
+
details['risk_match'] = 1.0 if action.get('risk_level') == case.get('expected_risk') else 0.0
|
| 313 |
+
elif atype == 'rank_issues':
|
| 314 |
+
expected = case.get('priority_order', [])
|
| 315 |
+
provided = action.get('priority_order', [])
|
| 316 |
+
details['ranking_overlap'] = len(set(expected) & set(provided)) / max(len(expected), 1) if expected else 0.0
|
| 317 |
+
elif atype == 'order_steps':
|
| 318 |
+
expected = case.get('required_steps', case.get('expected_missing_steps', []))
|
| 319 |
+
provided = action.get('recovery_steps', [])
|
| 320 |
+
details['steps_overlap'] = len(set(expected) & set(provided)) / max(len(expected), 1) if expected else 0.0
|
| 321 |
+
|
| 322 |
+
return details
|
server/session.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# server/session.py
|
| 2 |
+
# Foundation module — everything depends on this.
|
| 3 |
+
# Manages episode state, task-to-domain mapping, and in-memory session storage.
|
| 4 |
+
|
| 5 |
+
from dataclasses import dataclass, field
|
| 6 |
+
from typing import List, Dict, Any
|
| 7 |
+
import uuid
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@dataclass
|
| 11 |
+
class SessionState:
|
| 12 |
+
"""Holds all data for a single episode (one run of one task)."""
|
| 13 |
+
episode_id: str = field(default_factory=lambda: str(uuid.uuid4()))
|
| 14 |
+
task_type: str = '' # 'security' | 'dependency' | 'clinical'
|
| 15 |
+
task_id: str = '' # e.g. 'sec_easy'
|
| 16 |
+
task_case: Dict[str, Any] = field(default_factory=dict) # ground truth — NEVER shared with agent
|
| 17 |
+
history: List[Dict] = field(default_factory=list) # all past actions
|
| 18 |
+
last_actions: List[str] = field(default_factory=list) # action_type strings for repetition penalty
|
| 19 |
+
step_count: int = 0
|
| 20 |
+
reward_acc: float = 0.0
|
| 21 |
+
done: bool = False
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# Maps each of the 9 task IDs to its domain
|
| 25 |
+
TASK_TYPE_MAP = {
|
| 26 |
+
'sec_easy': 'security', 'sec_medium': 'security', 'sec_hard': 'security',
|
| 27 |
+
'dep_easy': 'dependency', 'dep_medium': 'dependency', 'dep_hard': 'dependency',
|
| 28 |
+
'cli_easy': 'clinical', 'cli_medium': 'clinical', 'cli_hard': 'clinical',
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
# In-memory store for all active sessions
|
| 32 |
+
SESSIONS: Dict[str, SessionState] = {}
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def create_session(task_id: str, task_case: Dict) -> SessionState:
|
| 36 |
+
"""Create a new session for a given task. Returns the SessionState object."""
|
| 37 |
+
s = SessionState()
|
| 38 |
+
s.task_id = task_id
|
| 39 |
+
s.task_type = TASK_TYPE_MAP.get(task_id, 'unknown')
|
| 40 |
+
s.task_case = task_case
|
| 41 |
+
return s
|
server/validation/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# server/validation package
|
server/validation/validator.py
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# server/validation/validator.py
|
| 2 |
+
# 3-stage pre-action validation: Schema → Domain → Consistency.
|
| 3 |
+
# IMPORTANT: Validator should HELP agents, not trap them.
|
| 4 |
+
# - Auto-coerce types where possible (string "8.5" → float 8.5)
|
| 5 |
+
# - Only hard-reject truly unrecoverable actions (wrong domain)
|
| 6 |
+
# - Silently truncate oversized fields instead of rejecting
|
| 7 |
+
# - Rich hints so agent can self-correct on next step
|
| 8 |
+
|
| 9 |
+
from typing import Dict, Tuple
|
| 10 |
+
|
| 11 |
+
VALID_VULN_TYPES = {
|
| 12 |
+
'sql_injection', 'xss', 'idor', 'hardcoded_secret', 'missing_auth',
|
| 13 |
+
'jwt_misuse', 'path_traversal', 'ssrf', 'rate_limit_missing', 'xxe'
|
| 14 |
+
}
|
| 15 |
+
VALID_SEVERITIES = {'critical', 'high', 'medium', 'low'}
|
| 16 |
+
VALID_RISK_LEVELS = {'critical', 'high', 'medium', 'low'}
|
| 17 |
+
|
| 18 |
+
# Which actions belong to which domain
|
| 19 |
+
DOMAIN_ACTIONS = {
|
| 20 |
+
'security': {'identify_vulnerability', 'propose_fix', 'revise_fix'},
|
| 21 |
+
'dependency': {'flag_outdated', 'resolve_conflict', 'migrate_api', 'validate_tree'},
|
| 22 |
+
'clinical': {'detect_gap', 'rank_issues', 'order_steps'},
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
# Required fields and their types for each action
|
| 26 |
+
ACTION_SCHEMAS = {
|
| 27 |
+
'identify_vulnerability': {
|
| 28 |
+
'vuln_type': str,
|
| 29 |
+
'cvss_score': (int, float),
|
| 30 |
+
'severity': str,
|
| 31 |
+
},
|
| 32 |
+
'propose_fix': {
|
| 33 |
+
'fix_code': str,
|
| 34 |
+
'explanation': str,
|
| 35 |
+
},
|
| 36 |
+
'revise_fix': {
|
| 37 |
+
'fix_code': str,
|
| 38 |
+
'addressed_feedback': str,
|
| 39 |
+
},
|
| 40 |
+
'flag_outdated': {
|
| 41 |
+
'packages': dict,
|
| 42 |
+
# deprecated_api and replacement are optional — handled below
|
| 43 |
+
},
|
| 44 |
+
'resolve_conflict': {
|
| 45 |
+
'packages': dict,
|
| 46 |
+
'reasoning': str,
|
| 47 |
+
},
|
| 48 |
+
'migrate_api': {
|
| 49 |
+
'completed_items': list,
|
| 50 |
+
'code_changes': dict,
|
| 51 |
+
},
|
| 52 |
+
'validate_tree': {
|
| 53 |
+
'completed_items': list,
|
| 54 |
+
},
|
| 55 |
+
'detect_gap': {
|
| 56 |
+
'missing_steps': list,
|
| 57 |
+
'risk_level': str,
|
| 58 |
+
},
|
| 59 |
+
'rank_issues': {
|
| 60 |
+
'priority_order': list,
|
| 61 |
+
},
|
| 62 |
+
'order_steps': {
|
| 63 |
+
'recovery_steps': list,
|
| 64 |
+
},
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
# Fields that are optional (won't cause hard rejection if missing)
|
| 68 |
+
OPTIONAL_FIELDS = {
|
| 69 |
+
'flag_outdated': {'deprecated_api', 'replacement'},
|
| 70 |
+
'identify_vulnerability': {'affected_line'},
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def _coerce(action: Dict, schema: Dict) -> Dict:
|
| 75 |
+
"""Try to coerce field types before validating. Modifies action in-place.
|
| 76 |
+
|
| 77 |
+
This is critical for model compatibility — different LLMs output
|
| 78 |
+
numbers as strings, lists as comma-separated strings, etc.
|
| 79 |
+
"""
|
| 80 |
+
for field, expected_type in schema.items():
|
| 81 |
+
if field not in action:
|
| 82 |
+
continue
|
| 83 |
+
val = action[field]
|
| 84 |
+
# Already correct type
|
| 85 |
+
if isinstance(val, expected_type):
|
| 86 |
+
continue
|
| 87 |
+
# Try coercions
|
| 88 |
+
try:
|
| 89 |
+
target = expected_type[0] if isinstance(expected_type, tuple) else expected_type
|
| 90 |
+
if target == float:
|
| 91 |
+
action[field] = float(val)
|
| 92 |
+
elif target == int:
|
| 93 |
+
action[field] = int(val)
|
| 94 |
+
elif target == str and not isinstance(val, str):
|
| 95 |
+
action[field] = str(val)
|
| 96 |
+
elif target == list and isinstance(val, str):
|
| 97 |
+
# Try JSON parse first, then comma split
|
| 98 |
+
try:
|
| 99 |
+
import json as _j
|
| 100 |
+
parsed = _j.loads(val)
|
| 101 |
+
if isinstance(parsed, list):
|
| 102 |
+
action[field] = parsed
|
| 103 |
+
except Exception:
|
| 104 |
+
action[field] = [x.strip(' "\'') for x in val.strip('[]').split(',') if x.strip()]
|
| 105 |
+
elif target == dict and isinstance(val, str):
|
| 106 |
+
import json as _j
|
| 107 |
+
action[field] = _j.loads(val)
|
| 108 |
+
except Exception:
|
| 109 |
+
pass # Leave as-is; domain check will catch real problems
|
| 110 |
+
return action
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def validate_action(action: Dict, session) -> Tuple[bool, Dict]:
|
| 114 |
+
"""3-stage validation. Returns (is_valid, feedback_observation).
|
| 115 |
+
|
| 116 |
+
Philosophy: be lenient on format (coerce types), strict on cross-domain actions.
|
| 117 |
+
An action in the wrong domain = hard reject.
|
| 118 |
+
An action with slightly wrong types = coerce and pass through.
|
| 119 |
+
"""
|
| 120 |
+
atype = action.get('action_type', '')
|
| 121 |
+
|
| 122 |
+
# ── Stage 1: Is this a known action type? ──
|
| 123 |
+
all_valid = set(ACTION_SCHEMAS.keys())
|
| 124 |
+
if atype not in all_valid:
|
| 125 |
+
return False, _fb(
|
| 126 |
+
'invalid_action_type',
|
| 127 |
+
f'Unknown action_type: {repr(atype)}',
|
| 128 |
+
session,
|
| 129 |
+
hint=f'Valid actions for {session.task_type}: {sorted(DOMAIN_ACTIONS.get(session.task_type, []))}',
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
# ── Cross-domain check FIRST (before coercion) ──
|
| 133 |
+
domain_valid = DOMAIN_ACTIONS.get(session.task_type, set())
|
| 134 |
+
if atype not in domain_valid:
|
| 135 |
+
return False, _fb(
|
| 136 |
+
'wrong_domain_action',
|
| 137 |
+
f'{repr(atype)} is not valid for task_type={repr(session.task_type)}',
|
| 138 |
+
session,
|
| 139 |
+
hint=f'Valid actions: {sorted(domain_valid)}',
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
# ── Coerce types before schema check (be helpful to all models) ──
|
| 143 |
+
schema = ACTION_SCHEMAS.get(atype, {})
|
| 144 |
+
action = _coerce(action, schema)
|
| 145 |
+
|
| 146 |
+
# ── Stage 2: Check required fields are present ──
|
| 147 |
+
optional = OPTIONAL_FIELDS.get(atype, set())
|
| 148 |
+
required_fields = [f for f in schema if f not in optional]
|
| 149 |
+
missing = [f for f in required_fields if f not in action]
|
| 150 |
+
if missing:
|
| 151 |
+
return False, _fb(
|
| 152 |
+
'missing_fields',
|
| 153 |
+
f'Missing required fields: {missing}',
|
| 154 |
+
session,
|
| 155 |
+
hint=f'Required for {atype}: {required_fields}',
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
# ── Stage 3: Domain value validation ──
|
| 159 |
+
errs = _domain_check(action, atype)
|
| 160 |
+
if errs:
|
| 161 |
+
return False, _fb(
|
| 162 |
+
'domain_error',
|
| 163 |
+
f'Invalid field values: {errs}',
|
| 164 |
+
session,
|
| 165 |
+
hint=_domain_hint(atype, errs),
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
# ── Stage 4: Consistency check ──
|
| 169 |
+
cons = _consistency_check(action, atype, session)
|
| 170 |
+
if cons:
|
| 171 |
+
return False, _fb('consistency_error', cons['message'], session, hint=cons['hint'])
|
| 172 |
+
|
| 173 |
+
return True, {}
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
def _domain_check(action: Dict, atype: str) -> list:
|
| 177 |
+
"""Check values are within allowed ranges/enums. Returns list of error dicts."""
|
| 178 |
+
errors = []
|
| 179 |
+
|
| 180 |
+
if atype == 'identify_vulnerability':
|
| 181 |
+
vt = action.get('vuln_type', '')
|
| 182 |
+
if vt not in VALID_VULN_TYPES:
|
| 183 |
+
errors.append({'field': 'vuln_type', 'value': vt, 'allowed': sorted(VALID_VULN_TYPES)})
|
| 184 |
+
try:
|
| 185 |
+
cvss = float(action.get('cvss_score', -1))
|
| 186 |
+
if not (0.0 <= cvss <= 10.0):
|
| 187 |
+
errors.append({'field': 'cvss_score', 'value': cvss, 'allowed': '0.0 to 10.0'})
|
| 188 |
+
except (TypeError, ValueError):
|
| 189 |
+
errors.append({'field': 'cvss_score', 'value': action.get('cvss_score'), 'allowed': '0.0 to 10.0'})
|
| 190 |
+
sev = action.get('severity', '')
|
| 191 |
+
if sev not in VALID_SEVERITIES:
|
| 192 |
+
errors.append({'field': 'severity', 'value': sev, 'allowed': sorted(VALID_SEVERITIES)})
|
| 193 |
+
|
| 194 |
+
elif atype in ('propose_fix', 'revise_fix'):
|
| 195 |
+
fix = action.get('fix_code', '')
|
| 196 |
+
if len(fix) > 2000:
|
| 197 |
+
# Silently truncate instead of rejecting — don't penalize verbose agents
|
| 198 |
+
action['fix_code'] = fix[:2000]
|
| 199 |
+
|
| 200 |
+
elif atype == 'detect_gap':
|
| 201 |
+
rl = action.get('risk_level', '')
|
| 202 |
+
if rl not in VALID_RISK_LEVELS:
|
| 203 |
+
errors.append({'field': 'risk_level', 'value': rl, 'allowed': sorted(VALID_RISK_LEVELS)})
|
| 204 |
+
|
| 205 |
+
elif atype == 'resolve_conflict':
|
| 206 |
+
pkgs = action.get('packages', {})
|
| 207 |
+
if not isinstance(pkgs, dict) or len(pkgs) == 0:
|
| 208 |
+
errors.append({'field': 'packages', 'issue': 'must be a non-empty dict of {package: version}'})
|
| 209 |
+
|
| 210 |
+
elif atype == 'migrate_api':
|
| 211 |
+
items = action.get('completed_items', [])
|
| 212 |
+
changes = action.get('code_changes', {})
|
| 213 |
+
if not isinstance(items, list) or len(items) == 0:
|
| 214 |
+
errors.append({'field': 'completed_items', 'issue': 'must be a non-empty list of break IDs'})
|
| 215 |
+
if not isinstance(changes, dict):
|
| 216 |
+
errors.append({'field': 'code_changes', 'issue': 'must be a dict of {break_id: fix_description}'})
|
| 217 |
+
|
| 218 |
+
return errors
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
def _domain_hint(atype: str, errors: list) -> str:
|
| 222 |
+
"""Generate a helpful hint for domain errors."""
|
| 223 |
+
fields = [e.get('field', '') for e in errors]
|
| 224 |
+
if 'vuln_type' in fields:
|
| 225 |
+
return "vuln_type must be one of: sql_injection, xss, idor, hardcoded_secret, missing_auth, jwt_misuse, path_traversal, ssrf, rate_limit_missing, xxe"
|
| 226 |
+
if 'severity' in fields:
|
| 227 |
+
return "severity must be one of: critical, high, medium, low"
|
| 228 |
+
if 'risk_level' in fields:
|
| 229 |
+
return "risk_level must be one of: critical, high, medium, low"
|
| 230 |
+
if 'cvss_score' in fields:
|
| 231 |
+
return "cvss_score must be a float between 0.0 and 10.0"
|
| 232 |
+
return f"Check field values for: {fields}"
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
def _consistency_check(action: Dict, atype: str, session) -> dict:
|
| 236 |
+
"""Check that action makes sense given session history."""
|
| 237 |
+
hist_types = [h.get('action_type') for h in session.history]
|
| 238 |
+
|
| 239 |
+
if atype == 'revise_fix' and 'propose_fix' not in hist_types:
|
| 240 |
+
return {
|
| 241 |
+
'message': 'Cannot call revise_fix before propose_fix',
|
| 242 |
+
'hint': 'Call propose_fix first, then revise_fix if you get reviewer feedback'
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
if atype == 'rank_issues' and 'detect_gap' not in hist_types:
|
| 246 |
+
return {
|
| 247 |
+
'message': 'Cannot call rank_issues before detect_gap',
|
| 248 |
+
'hint': 'Call detect_gap first, then rank_issues'
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
if atype == 'order_steps' and 'detect_gap' not in hist_types:
|
| 252 |
+
return {
|
| 253 |
+
'message': 'Cannot call order_steps before detect_gap',
|
| 254 |
+
'hint': 'Call detect_gap first, then rank_issues, then order_steps'
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
# Reject identical resolve_conflict proposals (infinite loop prevention)
|
| 258 |
+
if atype == 'resolve_conflict':
|
| 259 |
+
for prev in session.history:
|
| 260 |
+
if (prev.get('action_type') == 'resolve_conflict' and
|
| 261 |
+
prev.get('packages') == action.get('packages', {})):
|
| 262 |
+
return {
|
| 263 |
+
'message': 'Identical version proposal already submitted — this combination was rejected',
|
| 264 |
+
'hint': 'Try different package versions. Check the compatibility_matrix in the observation.'
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
return {}
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
def _fb(error_type: str, message: str, session, **kwargs) -> Dict:
|
| 271 |
+
"""Build a feedback observation for validation failures."""
|
| 272 |
+
obs = {
|
| 273 |
+
'validation_failed': True,
|
| 274 |
+
'error_type': error_type,
|
| 275 |
+
'message': message,
|
| 276 |
+
'turn': session.step_count,
|
| 277 |
+
'task_type': session.task_type,
|
| 278 |
+
'task_id': getattr(session, 'task_id', ''),
|
| 279 |
+
'available_actions': sorted(DOMAIN_ACTIONS.get(session.task_type, [])),
|
| 280 |
+
}
|
| 281 |
+
obs.update(kwargs)
|
| 282 |
+
return obs
|
server/web_ui.py
ADDED
|
@@ -0,0 +1,365 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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import time
|
| 9 |
+
from datetime import datetime
|
| 10 |
+
|
| 11 |
+
ENV_URL = 'http://localhost:7860'
|
| 12 |
+
RESULTS_FILE = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'results', 'run_history.json')
|
| 13 |
+
os.makedirs(os.path.dirname(RESULTS_FILE), exist_ok=True)
|
| 14 |
+
|
| 15 |
+
# ── Task info for the UI ──
|
| 16 |
+
TASK_INFO = {
|
| 17 |
+
'sec_easy': {
|
| 18 |
+
'name': '🔒 Security — Easy',
|
| 19 |
+
'desc': 'Identify a single vulnerability in a code snippet.\nThe agent must classify the vulnerability type (e.g., SQL injection, XSS), estimate the CVSS score, and determine severity.',
|
| 20 |
+
'domain': 'Security (MCP Sandbox)',
|
| 21 |
+
'example': '{"action_type":"identify_vulnerability","vuln_type":"sql_injection","cvss_score":9.1,"severity":"critical","affected_line":3}',
|
| 22 |
+
},
|
| 23 |
+
'sec_medium': {
|
| 24 |
+
'name': '🔒 Security — Medium',
|
| 25 |
+
'desc': 'Identify a vulnerability AND propose a secure code fix.\nThe agent performs vulnerability identification on turn 1, then proposes a fix on turn 2.',
|
| 26 |
+
'domain': 'Security (MCP Sandbox)',
|
| 27 |
+
'example': 'Turn 1: identify_vulnerability → Turn 2: propose_fix with fix_code',
|
| 28 |
+
},
|
| 29 |
+
'sec_hard': {
|
| 30 |
+
'name': '🔒 Security — Hard',
|
| 31 |
+
'desc': 'Identify → Fix → Revise based on reviewer feedback.\nMulti-turn: the agent must iteratively improve its fix when a reviewer provides feedback.',
|
| 32 |
+
'domain': 'Security (MCP Sandbox)',
|
| 33 |
+
'example': 'Turn 1: identify → Turn 2: propose_fix → Turn 3+: revise_fix (with reviewer feedback)',
|
| 34 |
+
},
|
| 35 |
+
'dep_easy': {
|
| 36 |
+
'name': '📦 Dependency — Easy',
|
| 37 |
+
'desc': 'Flag outdated packages and deprecated API usage.\nThe agent scans code for old package versions and deprecated function calls.',
|
| 38 |
+
'domain': 'PyTorch Migration',
|
| 39 |
+
'example': '{"action_type":"flag_outdated","packages":{"torch":"1.7.0"},"deprecated_api":"torch.no_grad","replacement":"torch.inference_mode"}',
|
| 40 |
+
},
|
| 41 |
+
'dep_medium': {
|
| 42 |
+
'name': '📦 Dependency — Medium',
|
| 43 |
+
'desc': 'Resolve version conflicts using a compatibility matrix.\nThe agent must propose compatible versions that satisfy cross-package constraints.',
|
| 44 |
+
'domain': 'PyTorch Migration',
|
| 45 |
+
'example': '{"action_type":"resolve_conflict","packages":{"torch":"2.1.0","numpy":"1.24.0"},"reasoning":"torch 2.1 requires numpy >= 1.24"}',
|
| 46 |
+
},
|
| 47 |
+
'dep_hard': {
|
| 48 |
+
'name': '📦 Dependency — Hard',
|
| 49 |
+
'desc': 'Fix torch.compile graph-break patterns in dependency order.\nThe agent must fix multiple graph-break issues in the correct order based on their dependencies.',
|
| 50 |
+
'domain': 'PyTorch Migration',
|
| 51 |
+
'example': '{"action_type":"migrate_api","completed_items":["break_1"],"code_changes":{"break_1":"replaced torch.no_grad with inference_mode"}}',
|
| 52 |
+
},
|
| 53 |
+
'cli_easy': {
|
| 54 |
+
'name': '🏥 Clinical — Easy',
|
| 55 |
+
'desc': 'Detect missing steps in a clinical workflow and assess risk.\nThe agent identifies which required steps are missing from a patient workflow.',
|
| 56 |
+
'domain': 'Clinical Workflow Recovery',
|
| 57 |
+
'example': '{"action_type":"detect_gap","missing_steps":["insurance_auth","pre_op_consent"],"risk_level":"critical"}',
|
| 58 |
+
},
|
| 59 |
+
'cli_medium': {
|
| 60 |
+
'name': '🏥 Clinical — Medium',
|
| 61 |
+
'desc': 'Detect gaps AND rank them by clinical priority.\nThe agent must both find missing steps and rank them by importance.',
|
| 62 |
+
'domain': 'Clinical Workflow Recovery',
|
| 63 |
+
'example': 'Turn 1: detect_gap → Turn 2: rank_issues with priority_order list',
|
| 64 |
+
},
|
| 65 |
+
'cli_hard': {
|
| 66 |
+
'name': '🏥 Clinical — Hard',
|
| 67 |
+
'desc': 'Plan a dependency-ordered recovery sequence.\nThe agent must respect the dependency graph when ordering recovery steps.',
|
| 68 |
+
'domain': 'Clinical Workflow Recovery',
|
| 69 |
+
'example': 'insurance_auth → pre_op_consent → specialist → surgery (respecting dependencies)',
|
| 70 |
+
},
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def _load_history():
|
| 75 |
+
if os.path.exists(RESULTS_FILE):
|
| 76 |
+
try:
|
| 77 |
+
with open(RESULTS_FILE, 'r') as f:
|
| 78 |
+
return json.load(f)
|
| 79 |
+
except Exception:
|
| 80 |
+
return []
|
| 81 |
+
return []
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def _save_run(run_data):
|
| 85 |
+
history = _load_history()
|
| 86 |
+
history.append(run_data)
|
| 87 |
+
with open(RESULTS_FILE, 'w') as f:
|
| 88 |
+
json.dump(history, f, indent=2)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def get_task_info(task_id):
|
| 92 |
+
"""Return description for selected task."""
|
| 93 |
+
info = TASK_INFO.get(task_id, {})
|
| 94 |
+
return (
|
| 95 |
+
f"### {info.get('name', task_id)}\n\n"
|
| 96 |
+
f"**Domain:** {info.get('domain', '?')}\n\n"
|
| 97 |
+
f"{info.get('desc', '')}\n\n"
|
| 98 |
+
f"**Example action:**\n```json\n{info.get('example', '')}\n```"
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def run_single_task(task_id: str):
|
| 103 |
+
"""Run a single task with the demo agent."""
|
| 104 |
+
from .demo_agent import demo_action
|
| 105 |
+
|
| 106 |
+
logs = []
|
| 107 |
+
rewards = []
|
| 108 |
+
|
| 109 |
+
r = requests.post(f'{ENV_URL}/reset', json={'task_id': task_id}, timeout=30).json()
|
| 110 |
+
ep_id = r.get('episode_id', '')
|
| 111 |
+
obs = r.get('observation', r)
|
| 112 |
+
logs.append(f'[START] task={task_id} episode={ep_id[:12]}...')
|
| 113 |
+
|
| 114 |
+
done = False
|
| 115 |
+
step = 0
|
| 116 |
+
while not done and step < 8:
|
| 117 |
+
action = demo_action(obs)
|
| 118 |
+
action['episode_id'] = ep_id
|
| 119 |
+
sr = requests.post(f'{ENV_URL}/step', json=action, timeout=30).json()
|
| 120 |
+
reward = sr.get('reward', 0.0)
|
| 121 |
+
done = sr.get('done', False)
|
| 122 |
+
obs = sr.get('observation', sr)
|
| 123 |
+
rewards.append(round(reward, 4))
|
| 124 |
+
atype = action.get('action_type', '?')
|
| 125 |
+
logs.append(f' Step {step + 1}: action={atype} reward={reward:.4f} done={done}')
|
| 126 |
+
step += 1
|
| 127 |
+
|
| 128 |
+
total = round(sum(rewards), 4)
|
| 129 |
+
logs.append(f'[END] total_reward={total} steps={step}')
|
| 130 |
+
return '\n'.join(logs), rewards, total
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def run_task_ui(task_id: str, model_name: str):
|
| 134 |
+
"""Run a single task and return display outputs."""
|
| 135 |
+
if not model_name.strip():
|
| 136 |
+
model_name = 'Demo Agent (rule-based)'
|
| 137 |
+
|
| 138 |
+
log_str, rewards, total = run_single_task(task_id)
|
| 139 |
+
|
| 140 |
+
reward_lines = ['Reward per step:']
|
| 141 |
+
for i, r in enumerate(rewards):
|
| 142 |
+
bar = '█' * int(r * 20)
|
| 143 |
+
reward_lines.append(f' Step {i + 1}: {bar} {r:.4f}')
|
| 144 |
+
reward_str = '\n'.join(reward_lines)
|
| 145 |
+
|
| 146 |
+
info = TASK_INFO.get(task_id, {})
|
| 147 |
+
domain = info.get('domain', 'Unknown')
|
| 148 |
+
difficulty = task_id.split('_')[1].upper()
|
| 149 |
+
score = min(max(total / max(len(rewards), 1), 0), 1)
|
| 150 |
+
|
| 151 |
+
score_md = f'''### ✅ Results
|
| 152 |
+
| Field | Value |
|
| 153 |
+
|-------|-------|
|
| 154 |
+
| **Model** | `{model_name}` |
|
| 155 |
+
| **Task** | `{task_id}` |
|
| 156 |
+
| **Domain** | {domain} |
|
| 157 |
+
| **Difficulty** | {difficulty} |
|
| 158 |
+
| **Score** | **{score:.4f}** |
|
| 159 |
+
| **Total Reward** | {total:.4f} |
|
| 160 |
+
| **Steps** | {len(rewards)} |
|
| 161 |
+
'''
|
| 162 |
+
|
| 163 |
+
_save_run({
|
| 164 |
+
'model': model_name, 'task_id': task_id, 'domain': domain,
|
| 165 |
+
'total_reward': total, 'score': round(score, 4),
|
| 166 |
+
'steps': len(rewards), 'timestamp': datetime.now().isoformat(),
|
| 167 |
+
})
|
| 168 |
+
|
| 169 |
+
return log_str, reward_str, score_md
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
def run_all_tasks_ui(model_name: str):
|
| 173 |
+
"""Run all 9 tasks and return a performance dashboard."""
|
| 174 |
+
if not model_name.strip():
|
| 175 |
+
model_name = 'Demo Agent (rule-based)'
|
| 176 |
+
|
| 177 |
+
tasks = list(TASK_INFO.keys())
|
| 178 |
+
all_logs = []
|
| 179 |
+
all_scores = {}
|
| 180 |
+
|
| 181 |
+
for task_id in tasks:
|
| 182 |
+
log_str, rewards, total = run_single_task(task_id)
|
| 183 |
+
all_logs.append(log_str)
|
| 184 |
+
score = min(max(total / max(len(rewards), 1), 0), 1)
|
| 185 |
+
all_scores[task_id] = round(score, 4)
|
| 186 |
+
|
| 187 |
+
full_log = '\n\n'.join(all_logs)
|
| 188 |
+
|
| 189 |
+
sec = [all_scores[t] for t in tasks if t.startswith('sec')]
|
| 190 |
+
dep = [all_scores[t] for t in tasks if t.startswith('dep')]
|
| 191 |
+
cli = [all_scores[t] for t in tasks if t.startswith('cli')]
|
| 192 |
+
|
| 193 |
+
rows = []
|
| 194 |
+
for task_id, score in all_scores.items():
|
| 195 |
+
info = TASK_INFO.get(task_id, {})
|
| 196 |
+
bar = '█' * int(min(score, 1.0) * 15)
|
| 197 |
+
rows.append(f'| `{task_id}` | {info.get("domain", "?")} | {bar} | **{score:.4f}** |')
|
| 198 |
+
|
| 199 |
+
avg = sum(all_scores.values()) / 9
|
| 200 |
+
sec_avg = sum(sec) / 3
|
| 201 |
+
dep_avg = sum(dep) / 3
|
| 202 |
+
cli_avg = sum(cli) / 3
|
| 203 |
+
|
| 204 |
+
dashboard = f'''## 📊 Model Performance Dashboard
|
| 205 |
+
|
| 206 |
+
**Model:** `{model_name}`
|
| 207 |
+
**Time:** {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
|
| 208 |
+
|
| 209 |
+
### Per-Task Scores
|
| 210 |
+
| Task | Domain | Performance | Score |
|
| 211 |
+
|------|--------|-------------|-------|
|
| 212 |
+
{chr(10).join(rows)}
|
| 213 |
+
|
| 214 |
+
### Domain Averages
|
| 215 |
+
| Domain | Avg Score | Rating |
|
| 216 |
+
|--------|-----------|--------|
|
| 217 |
+
| 🔒 Security | {sec_avg:.4f} | {"🟢 Excellent" if sec_avg > 0.7 else "🟡 Good" if sec_avg > 0.4 else "🔴 Needs Work"} |
|
| 218 |
+
| 📦 PyTorch Migration | {dep_avg:.4f} | {"🟢 Excellent" if dep_avg > 0.7 else "🟡 Good" if dep_avg > 0.4 else "🔴 Needs Work"} |
|
| 219 |
+
| 🏥 Clinical Workflow | {cli_avg:.4f} | {"🟢 Excellent" if cli_avg > 0.7 else "🟡 Good" if cli_avg > 0.4 else "🔴 Needs Work"} |
|
| 220 |
+
|
| 221 |
+
### Overall: **{avg:.4f}**
|
| 222 |
+
'''
|
| 223 |
+
|
| 224 |
+
_save_run({
|
| 225 |
+
'model': model_name, 'type': 'full_run', 'scores': all_scores,
|
| 226 |
+
'avg': round(avg, 4), 'timestamp': datetime.now().isoformat(),
|
| 227 |
+
})
|
| 228 |
+
|
| 229 |
+
return full_log, dashboard
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
def show_history():
|
| 233 |
+
history = _load_history()
|
| 234 |
+
if not history:
|
| 235 |
+
return 'No runs yet. Run a task first!'
|
| 236 |
+
lines = ['## 📜 Run History\n']
|
| 237 |
+
for i, run in enumerate(reversed(history[-10:])):
|
| 238 |
+
ts = run.get('timestamp', '?')[:19]
|
| 239 |
+
model = run.get('model', '?')
|
| 240 |
+
if run.get('type') == 'full_run':
|
| 241 |
+
avg = run.get('avg', 0)
|
| 242 |
+
lines.append(f'**#{len(history) - i}** | `{ts}` | `{model}` | All 9 tasks | Avg: **{avg:.4f}**')
|
| 243 |
+
else:
|
| 244 |
+
task = run.get('task_id', '?')
|
| 245 |
+
score = run.get('score', 0)
|
| 246 |
+
lines.append(f'**#{len(history) - i}** | `{ts}` | `{model}` | `{task}` | Score: **{score:.4f}**')
|
| 247 |
+
return '\n\n'.join(lines)
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
def build_ui():
|
| 251 |
+
with gr.Blocks(title='Multi-Agent Dev Tools Env', theme=gr.themes.Soft()) as demo:
|
| 252 |
+
gr.Markdown('''# 🛠️ Multi-Agent Dev Tools Environment
|
| 253 |
+
**A multi-domain RL environment for training AI agents on real-world tasks.**
|
| 254 |
+
|
| 255 |
+
This environment tests AI agents across **3 domains** with **9 tasks** of increasing difficulty.
|
| 256 |
+
Agents receive observations (problems), send actions (answers), and get reward scores (0.0 – 1.0).
|
| 257 |
+
''')
|
| 258 |
+
|
| 259 |
+
with gr.Tab('🎯 Single Task'):
|
| 260 |
+
with gr.Row():
|
| 261 |
+
task_dd = gr.Dropdown(
|
| 262 |
+
choices=list(TASK_INFO.keys()),
|
| 263 |
+
value='sec_easy',
|
| 264 |
+
label='🎯 Select Task',
|
| 265 |
+
)
|
| 266 |
+
model_input = gr.Textbox(
|
| 267 |
+
label='🤖 Model Name',
|
| 268 |
+
value='Demo Agent (rule-based)',
|
| 269 |
+
placeholder='e.g. Qwen/Qwen2.5-72B-Instruct',
|
| 270 |
+
)
|
| 271 |
+
run_btn = gr.Button('▶️ Run Task', variant='primary', scale=1)
|
| 272 |
+
|
| 273 |
+
task_info_md = gr.Markdown(get_task_info('sec_easy'))
|
| 274 |
+
task_dd.change(fn=get_task_info, inputs=[task_dd], outputs=[task_info_md])
|
| 275 |
+
|
| 276 |
+
with gr.Row():
|
| 277 |
+
logs_box = gr.Textbox(label='📋 Episode Log', lines=10)
|
| 278 |
+
rewards_box = gr.Textbox(label='📊 Reward History', lines=10)
|
| 279 |
+
|
| 280 |
+
score_md = gr.Markdown('*Results will appear after running a task...*')
|
| 281 |
+
|
| 282 |
+
run_btn.click(
|
| 283 |
+
fn=run_task_ui,
|
| 284 |
+
inputs=[task_dd, model_input],
|
| 285 |
+
outputs=[logs_box, rewards_box, score_md],
|
| 286 |
+
)
|
| 287 |
+
|
| 288 |
+
with gr.Tab('🏆 Run All 9 Tasks'):
|
| 289 |
+
gr.Markdown('Run all 9 tasks at once and see a full performance dashboard with domain averages.')
|
| 290 |
+
with gr.Row():
|
| 291 |
+
model_all = gr.Textbox(
|
| 292 |
+
label='🤖 Model Name',
|
| 293 |
+
value='Demo Agent (rule-based)',
|
| 294 |
+
)
|
| 295 |
+
run_all_btn = gr.Button('🚀 Run All 9 Tasks', variant='primary')
|
| 296 |
+
|
| 297 |
+
all_logs = gr.Textbox(label='📋 Full Run Log', lines=12)
|
| 298 |
+
dashboard_md = gr.Markdown('*Dashboard will appear after running all tasks...*')
|
| 299 |
+
|
| 300 |
+
run_all_btn.click(
|
| 301 |
+
fn=run_all_tasks_ui,
|
| 302 |
+
inputs=[model_all],
|
| 303 |
+
outputs=[all_logs, dashboard_md],
|
| 304 |
+
)
|
| 305 |
+
|
| 306 |
+
with gr.Tab('📜 Run History'):
|
| 307 |
+
history_md = gr.Markdown('Click refresh to see past runs.')
|
| 308 |
+
refresh_btn = gr.Button('🔄 Refresh History')
|
| 309 |
+
refresh_btn.click(fn=show_history, outputs=[history_md])
|
| 310 |
+
|
| 311 |
+
with gr.Tab('📖 How It Works'):
|
| 312 |
+
gr.Markdown('''## How This Environment Works
|
| 313 |
+
|
| 314 |
+
### Overview
|
| 315 |
+
This is a **training gym for AI agents**. You build an agent, connect it to this environment
|
| 316 |
+
via the API, and it gets scored on how well it solves real-world tasks.
|
| 317 |
+
|
| 318 |
+
### The Flow
|
| 319 |
+
```
|
| 320 |
+
1. Agent calls POST /reset with a task_id → Gets an observation (the problem)
|
| 321 |
+
2. Agent analyzes the observation and sends POST /step with its action
|
| 322 |
+
3. Environment validates the action and grades it
|
| 323 |
+
4. Returns a reward score (0.0 – 1.0) and the next observation
|
| 324 |
+
5. Repeat until the episode ends (done=true) or max steps reached
|
| 325 |
+
```
|
| 326 |
+
|
| 327 |
+
### Three Domains
|
| 328 |
+
| Domain | Tasks | What Agents Do |
|
| 329 |
+
|--------|-------|---------------|
|
| 330 |
+
| 🔒 **Security** | sec_easy, sec_medium, sec_hard | Identify vulnerabilities, propose fixes, revise based on feedback |
|
| 331 |
+
| 📦 **Dependency** | dep_easy, dep_medium, dep_hard | Flag outdated packages, resolve conflicts, fix graph-breaks |
|
| 332 |
+
| 🏥 **Clinical** | cli_easy, cli_medium, cli_hard | Detect workflow gaps, rank by priority, plan recovery |
|
| 333 |
+
|
| 334 |
+
### Reward Signals
|
| 335 |
+
- Scores range from **0.0** (completely wrong) to **1.0** (perfect)
|
| 336 |
+
- Partial credit is awarded for partially correct answers
|
| 337 |
+
- Invalid or malformed actions receive lower scores
|
| 338 |
+
- The environment provides feedback on validation failures to help agents improve
|
| 339 |
+
|
| 340 |
+
### API Endpoints
|
| 341 |
+
| Method | Path | Description |
|
| 342 |
+
|--------|------|-------------|
|
| 343 |
+
| `GET /` | Health check | Returns status and task count |
|
| 344 |
+
| `POST /reset` | Start episode | `{"task_id":"sec_easy"}` → observation |
|
| 345 |
+
| `POST /step` | Submit action | `{action_type, ...}` → reward + next observation |
|
| 346 |
+
| `GET /state` | Get state | Query current episode state |
|
| 347 |
+
|
| 348 |
+
### Getting Started
|
| 349 |
+
```python
|
| 350 |
+
import requests
|
| 351 |
+
|
| 352 |
+
# Start an episode
|
| 353 |
+
resp = requests.post("http://localhost:7860/reset", json={"task_id": "sec_easy"})
|
| 354 |
+
data = resp.json()
|
| 355 |
+
episode_id = data["episode_id"]
|
| 356 |
+
observation = data["observation"]
|
| 357 |
+
|
| 358 |
+
# Send an action
|
| 359 |
+
action = {"episode_id": episode_id, "action_type": "identify_vulnerability", ...}
|
| 360 |
+
result = requests.post("http://localhost:7860/step", json=action)
|
| 361 |
+
print(result.json()) # {"reward": 0.85, "done": true, "observation": {...}}
|
| 362 |
+
```
|
| 363 |
+
''')
|
| 364 |
+
|
| 365 |
+
return demo
|
tests/test_endpoints.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# tests/test_endpoints.py
|
| 2 |
+
# Basic endpoint tests for the environment.
|
| 3 |
+
# Run: python -m pytest tests/ -v
|
| 4 |
+
|
| 5 |
+
import requests
|
| 6 |
+
import pytest
|
| 7 |
+
|
| 8 |
+
BASE_URL = 'http://localhost:7860'
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def test_health_check():
|
| 12 |
+
"""GET / should return 200 with status ok."""
|
| 13 |
+
r = requests.get(f'{BASE_URL}/')
|
| 14 |
+
assert r.status_code == 200
|
| 15 |
+
data = r.json()
|
| 16 |
+
assert data['status'] == 'ok'
|
| 17 |
+
assert data['tasks'] == 9
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def test_reset_valid_task():
|
| 21 |
+
"""POST /reset with valid task_id should return episode_id and observation."""
|
| 22 |
+
r = requests.post(f'{BASE_URL}/reset', json={'task_id': 'sec_easy'})
|
| 23 |
+
assert r.status_code == 200
|
| 24 |
+
data = r.json()
|
| 25 |
+
assert 'episode_id' in data
|
| 26 |
+
assert 'observation' in data
|
| 27 |
+
assert data['observation']['task_type'] == 'security'
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def test_reset_all_tasks():
|
| 31 |
+
"""POST /reset should work for all 9 task IDs."""
|
| 32 |
+
tasks = [
|
| 33 |
+
'sec_easy', 'sec_medium', 'sec_hard',
|
| 34 |
+
'dep_easy', 'dep_medium', 'dep_hard',
|
| 35 |
+
'cli_easy', 'cli_medium', 'cli_hard',
|
| 36 |
+
]
|
| 37 |
+
for task_id in tasks:
|
| 38 |
+
r = requests.post(f'{BASE_URL}/reset', json={'task_id': task_id})
|
| 39 |
+
assert r.status_code == 200
|
| 40 |
+
data = r.json()
|
| 41 |
+
assert 'episode_id' in data, f'No episode_id for {task_id}'
|
| 42 |
+
assert 'observation' in data, f'No observation for {task_id}'
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def test_reset_invalid_task():
|
| 46 |
+
"""POST /reset with invalid task_id should still return 200."""
|
| 47 |
+
r = requests.post(f'{BASE_URL}/reset', json={'task_id': 'nonexistent'})
|
| 48 |
+
assert r.status_code == 200
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def test_step_valid_action():
|
| 52 |
+
"""POST /step with valid action should return reward and observation."""
|
| 53 |
+
# Reset first
|
| 54 |
+
r = requests.post(f'{BASE_URL}/reset', json={'task_id': 'sec_easy'})
|
| 55 |
+
ep_id = r.json()['episode_id']
|
| 56 |
+
|
| 57 |
+
# Step
|
| 58 |
+
action = {
|
| 59 |
+
'episode_id': ep_id,
|
| 60 |
+
'action_type': 'identify_vulnerability',
|
| 61 |
+
'vuln_type': 'sql_injection',
|
| 62 |
+
'cvss_score': 9.1,
|
| 63 |
+
'severity': 'critical',
|
| 64 |
+
'affected_line': 1,
|
| 65 |
+
}
|
| 66 |
+
r = requests.post(f'{BASE_URL}/step', json=action)
|
| 67 |
+
assert r.status_code == 200
|
| 68 |
+
data = r.json()
|
| 69 |
+
assert 'reward' in data
|
| 70 |
+
assert 'done' in data
|
| 71 |
+
assert 'observation' in data
|
| 72 |
+
assert 0.0 <= data['reward'] <= 1.0
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def test_step_invalid_episode():
|
| 76 |
+
"""POST /step with invalid episode_id should return 200 with done=True."""
|
| 77 |
+
r = requests.post(f'{BASE_URL}/step', json={
|
| 78 |
+
'episode_id': 'nonexistent',
|
| 79 |
+
'action_type': 'identify_vulnerability',
|
| 80 |
+
})
|
| 81 |
+
assert r.status_code == 200
|
| 82 |
+
data = r.json()
|
| 83 |
+
assert data['done'] is True
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def test_state_endpoint():
|
| 87 |
+
"""GET /state should return episode info."""
|
| 88 |
+
r = requests.post(f'{BASE_URL}/reset', json={'task_id': 'sec_easy'})
|
| 89 |
+
ep_id = r.json()['episode_id']
|
| 90 |
+
|
| 91 |
+
r = requests.get(f'{BASE_URL}/state', params={'episode_id': ep_id})
|
| 92 |
+
assert r.status_code == 200
|
| 93 |
+
data = r.json()
|
| 94 |
+
assert data['episode_id'] == ep_id
|
| 95 |
+
assert data['done'] is False
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def test_reward_range():
|
| 99 |
+
"""Rewards should always be in [0.0, 1.0]."""
|
| 100 |
+
tasks = ['sec_easy', 'dep_easy', 'cli_easy']
|
| 101 |
+
for task_id in tasks:
|
| 102 |
+
r = requests.post(f'{BASE_URL}/reset', json={'task_id': task_id})
|
| 103 |
+
ep_id = r.json()['episode_id']
|
| 104 |
+
|
| 105 |
+
# Send an invalid action
|
| 106 |
+
r = requests.post(f'{BASE_URL}/step', json={
|
| 107 |
+
'episode_id': ep_id,
|
| 108 |
+
'action_type': 'invalid_action_type',
|
| 109 |
+
})
|
| 110 |
+
data = r.json()
|
| 111 |
+
assert 0.0 <= data['reward'] <= 1.0, f'Reward out of range for {task_id}'
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def test_step_enriched_observation():
|
| 115 |
+
"""Step observations should include task context fields."""
|
| 116 |
+
r = requests.post(f'{BASE_URL}/reset', json={'task_id': 'sec_easy'})
|
| 117 |
+
ep_id = r.json()['episode_id']
|
| 118 |
+
|
| 119 |
+
action = {
|
| 120 |
+
'episode_id': ep_id,
|
| 121 |
+
'action_type': 'identify_vulnerability',
|
| 122 |
+
'vuln_type': 'sql_injection',
|
| 123 |
+
'cvss_score': 9.1,
|
| 124 |
+
'severity': 'critical',
|
| 125 |
+
'affected_line': 1,
|
| 126 |
+
}
|
| 127 |
+
r = requests.post(f'{BASE_URL}/step', json=action)
|
| 128 |
+
obs = r.json()['observation']
|
| 129 |
+
assert 'task_type' in obs
|
| 130 |
+
assert 'max_steps' in obs
|
| 131 |
+
assert 'steps_remaining' in obs
|
tests/test_grader_variance.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# tests/test_grader_variance.py
|
| 2 |
+
# Phase 2 of judging runs a variance check. If all graders return the same score
|
| 3 |
+
# for different quality answers, the submission is DISQUALIFIED.
|
| 4 |
+
# Run: python -m pytest tests/test_grader_variance.py -v
|
| 5 |
+
|
| 6 |
+
import sys
|
| 7 |
+
sys.path.insert(0, '.')
|
| 8 |
+
|
| 9 |
+
from server.graders.base_grader import safe_score
|
| 10 |
+
from server.graders.security_grader import compute_correctness as sec_cc
|
| 11 |
+
from server.graders.dependency_grader import compute_correctness as dep_cc
|
| 12 |
+
from server.graders.clinical_grader import compute_correctness as cli_cc
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# ── Security Case for Testing ──
|
| 16 |
+
SEC_CASE = {
|
| 17 |
+
'expected_vuln_type': 'sql_injection',
|
| 18 |
+
'cvss_range': [7.5, 9.8],
|
| 19 |
+
'expected_severity': 'critical',
|
| 20 |
+
'required_fix_tokens': ['?', 'parameterized'],
|
| 21 |
+
'current_feedback_keywords': ['sql', 'injection'],
|
| 22 |
+
'original_vuln_pattern': 'query+',
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def test_sec_identify_variance():
|
| 27 |
+
"""Security grader must return 3+ different scores for different quality answers."""
|
| 28 |
+
perfect = {
|
| 29 |
+
'action_type': 'identify_vulnerability',
|
| 30 |
+
'vuln_type': 'sql_injection',
|
| 31 |
+
'cvss_score': 8.5,
|
| 32 |
+
'severity': 'critical',
|
| 33 |
+
'affected_line': 1,
|
| 34 |
+
}
|
| 35 |
+
partial = {
|
| 36 |
+
'action_type': 'identify_vulnerability',
|
| 37 |
+
'vuln_type': 'xss', # wrong vuln_type
|
| 38 |
+
'cvss_score': 8.5, # but correct CVSS
|
| 39 |
+
'severity': 'critical', # and correct severity
|
| 40 |
+
'affected_line': 1,
|
| 41 |
+
}
|
| 42 |
+
wrong = {
|
| 43 |
+
'action_type': 'identify_vulnerability',
|
| 44 |
+
'vuln_type': 'xss', # wrong everything
|
| 45 |
+
'cvss_score': 2.0,
|
| 46 |
+
'severity': 'low',
|
| 47 |
+
'affected_line': 1,
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
s1 = safe_score(sec_cc(perfect, SEC_CASE))
|
| 51 |
+
s2 = safe_score(sec_cc(partial, SEC_CASE))
|
| 52 |
+
s3 = safe_score(sec_cc(wrong, SEC_CASE))
|
| 53 |
+
|
| 54 |
+
assert len({round(s, 2) for s in [s1, s2, s3]}) >= 3, f'No variance: {s1},{s2},{s3}'
|
| 55 |
+
assert s1 > s2 > s3, f'Wrong ordering: {s1},{s2},{s3}'
|
| 56 |
+
print(f' Security identify variance: {s1:.4f} > {s2:.4f} > {s3:.4f} PASS')
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def test_dep_resolve_variance():
|
| 60 |
+
"""Dependency grader must return different scores for different quality answers."""
|
| 61 |
+
case = {
|
| 62 |
+
'conflict_packages': ['torch', 'numpy'],
|
| 63 |
+
'compatibility_matrix': {
|
| 64 |
+
'torch': {'2.1.0': {'numpy': '>=1.24'}, '1.9.0': {}},
|
| 65 |
+
'numpy': {'1.24.0': {}, '1.16.0': {}},
|
| 66 |
+
},
|
| 67 |
+
'requirements': {'torch': '1.9.0', 'numpy': '1.16.0'},
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
full = {'action_type': 'resolve_conflict', 'packages': {'torch': '2.1.0', 'numpy': '1.24.0'}, 'reasoning': 'ok'}
|
| 71 |
+
part = {'action_type': 'resolve_conflict', 'packages': {'torch': '2.1.0', 'numpy': '1.16.0'}, 'reasoning': 'ok'}
|
| 72 |
+
empty = {'action_type': 'resolve_conflict', 'packages': {}, 'reasoning': 'ok'}
|
| 73 |
+
|
| 74 |
+
s1 = safe_score(dep_cc(full, case))
|
| 75 |
+
s2 = safe_score(dep_cc(part, case))
|
| 76 |
+
s3 = safe_score(dep_cc(empty, case))
|
| 77 |
+
|
| 78 |
+
assert s1 > s2 >= s3, f'No variance: {s1},{s2},{s3}'
|
| 79 |
+
print(f' Dependency resolve variance: {s1:.4f} > {s2:.4f} >= {s3:.4f} PASS')
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def test_cli_order_variance():
|
| 83 |
+
"""Clinical grader must return different scores for correct vs violated dependency order."""
|
| 84 |
+
case = {
|
| 85 |
+
'dependency_graph': {
|
| 86 |
+
'schedule_surgery': ['resolve_insurance', 'complete_pre_op'],
|
| 87 |
+
'complete_pre_op': ['resolve_insurance'],
|
| 88 |
+
'resolve_insurance': [],
|
| 89 |
+
},
|
| 90 |
+
'required_steps': ['resolve_insurance', 'complete_pre_op', 'schedule_surgery'],
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
correct = {
|
| 94 |
+
'action_type': 'order_steps',
|
| 95 |
+
'recovery_steps': ['resolve_insurance', 'complete_pre_op', 'schedule_surgery'],
|
| 96 |
+
}
|
| 97 |
+
violated = {
|
| 98 |
+
'action_type': 'order_steps',
|
| 99 |
+
'recovery_steps': ['schedule_surgery', 'complete_pre_op', 'resolve_insurance'],
|
| 100 |
+
}
|
| 101 |
+
partial = {
|
| 102 |
+
'action_type': 'order_steps',
|
| 103 |
+
'recovery_steps': ['resolve_insurance', 'complete_pre_op'],
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
s1 = safe_score(cli_cc(correct, case))
|
| 107 |
+
s2 = safe_score(cli_cc(violated, case))
|
| 108 |
+
s3 = safe_score(cli_cc(partial, case))
|
| 109 |
+
|
| 110 |
+
assert s1 > s2, f'Violation not penalised: correct={s1}, violated={s2}'
|
| 111 |
+
assert s1 > s3, f'Completeness not rewarded: correct={s1}, partial={s3}'
|
| 112 |
+
print(f' Clinical order variance: {s1:.4f} > violated:{s2:.4f}, partial:{s3:.4f} PASS')
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def test_safe_score_none():
|
| 116 |
+
"""Bug 1 fix: safe_score(None) must return 0.0, not crash."""
|
| 117 |
+
assert safe_score(None) == 0.0
|
| 118 |
+
assert safe_score(1.5) == 1.0
|
| 119 |
+
assert safe_score(-0.5) == 0.0
|
| 120 |
+
assert safe_score('bad') == 0.0
|
| 121 |
+
print(' safe_score(None) guard: PASS')
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def test_clinical_valid_actions():
|
| 125 |
+
"""Bug 2 fix: propose_recovery must NOT be in clinical VALID_ACTIONS."""
|
| 126 |
+
from server.graders.clinical_grader import VALID_ACTIONS
|
| 127 |
+
assert 'propose_recovery' not in VALID_ACTIONS, 'Bug 2 still present!'
|
| 128 |
+
assert set(VALID_ACTIONS) == {'detect_gap', 'rank_issues', 'order_steps'}
|
| 129 |
+
print(' Clinical VALID_ACTIONS (Bug 2): PASS')
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
if __name__ == '__main__':
|
| 133 |
+
test_safe_score_none()
|
| 134 |
+
test_clinical_valid_actions()
|
| 135 |
+
test_sec_identify_variance()
|
| 136 |
+
test_dep_resolve_variance()
|
| 137 |
+
test_cli_order_variance()
|
| 138 |
+
print('\nALL VARIANCE TESTS PASSED ✅')
|
validate-submission.sh
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env bash
|
| 2 |
+
#
|
| 3 |
+
# validate-submission.sh — OpenEnv Submission Validator
|
| 4 |
+
# Usage: ./validate-submission.sh <ping_url> [repo_dir]
|
| 5 |
+
|
| 6 |
+
set -uo pipefail
|
| 7 |
+
|
| 8 |
+
DOCKER_BUILD_TIMEOUT=600
|
| 9 |
+
if [ -t 1 ]; then
|
| 10 |
+
RED='\033[0;31m'
|
| 11 |
+
GREEN='\033[0;32m'
|
| 12 |
+
YELLOW='\033[1;33m'
|
| 13 |
+
BOLD='\033[1m'
|
| 14 |
+
NC='\033[0m'
|
| 15 |
+
else
|
| 16 |
+
RED='' GREEN='' YELLOW='' BOLD='' NC=''
|
| 17 |
+
fi
|
| 18 |
+
|
| 19 |
+
PING_URL="${1:-}"
|
| 20 |
+
REPO_DIR="${2:-.}"
|
| 21 |
+
|
| 22 |
+
if [ -z "$PING_URL" ]; then
|
| 23 |
+
printf "Usage: %s <ping_url> [repo_dir]\n" "$0"
|
| 24 |
+
exit 1
|
| 25 |
+
fi
|
| 26 |
+
|
| 27 |
+
REPO_DIR="$(cd "$REPO_DIR" 2>/dev/null && pwd)"
|
| 28 |
+
PING_URL="${PING_URL%/}"
|
| 29 |
+
PASS=0
|
| 30 |
+
|
| 31 |
+
log() { printf "[%s] %b\n" "$(date -u +%H:%M:%S)" "$*"; }
|
| 32 |
+
pass() { log "${GREEN}PASSED${NC} -- $1"; PASS=$((PASS + 1)); }
|
| 33 |
+
fail() { log "${RED}FAILED${NC} -- $1"; }
|
| 34 |
+
|
| 35 |
+
printf "\n${BOLD}========================================${NC}\n"
|
| 36 |
+
printf "${BOLD} OpenEnv Submission Validator${NC}\n"
|
| 37 |
+
printf "${BOLD}========================================${NC}\n"
|
| 38 |
+
log "Repo: $REPO_DIR"
|
| 39 |
+
log "Ping URL: $PING_URL"
|
| 40 |
+
|
| 41 |
+
# Step 1: Ping
|
| 42 |
+
log "${BOLD}Step 1/3: Pinging HF Space${NC}"
|
| 43 |
+
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" -X POST \
|
| 44 |
+
-H "Content-Type: application/json" -d '{}' \
|
| 45 |
+
"$PING_URL/reset" --max-time 30 2>/dev/null || printf "000")
|
| 46 |
+
if [ "$HTTP_CODE" = "200" ]; then
|
| 47 |
+
pass "HF Space is live"
|
| 48 |
+
else
|
| 49 |
+
fail "HF Space /reset returned HTTP $HTTP_CODE"
|
| 50 |
+
fi
|
| 51 |
+
|
| 52 |
+
# Step 2: Docker build
|
| 53 |
+
log "${BOLD}Step 2/3: Docker build${NC}"
|
| 54 |
+
if command -v docker &>/dev/null; then
|
| 55 |
+
docker build "$REPO_DIR" && pass "Docker build succeeded" || fail "Docker build failed"
|
| 56 |
+
else
|
| 57 |
+
fail "docker not found"
|
| 58 |
+
fi
|
| 59 |
+
|
| 60 |
+
# Step 3: openenv validate
|
| 61 |
+
log "${BOLD}Step 3/3: openenv validate${NC}"
|
| 62 |
+
if command -v openenv &>/dev/null; then
|
| 63 |
+
(cd "$REPO_DIR" && openenv validate) && pass "openenv validate passed" || fail "openenv validate failed"
|
| 64 |
+
else
|
| 65 |
+
fail "openenv not found"
|
| 66 |
+
fi
|
| 67 |
+
|
| 68 |
+
printf "\n${BOLD}========================================${NC}\n"
|
| 69 |
+
printf "${GREEN}${BOLD} $PASS/3 checks passed${NC}\n"
|
| 70 |
+
printf "${BOLD}========================================${NC}\n"
|