File size: 6,622 Bytes
b92d20c
 
63156a1
 
bef50a8
 
b92d20c
bef50a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b92d20c
 
bef50a8
 
 
 
 
 
 
 
 
b92d20c
 
bef50a8
 
 
 
 
 
 
 
 
 
b92d20c
63156a1
bef50a8
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# CodeReview-Env β€” Hackathon Impact Statement

**(Meta Γ— PyTorch Γ— HuggingFace OpenEnv Hackathon 2026)**

---

## The Problem

Code review is one of the most time-consuming, high-stakes, and yet most poorly-automated
tasks in software engineering:

- Developers spend **6–12 hours per week** reviewing pull requests.
- Most LLMs today produce dangerously generic feedback: _"Looks good!"_, _"LGTM"_, _"Maybe add a test."_
- This low-quality output creates **alert fatigue** β€” developers stop trusting AI feedback
  and the review becomes perfunctory.
- Real production incidents (auth bypasses, duplicate payments, pagination data corruption)
  are missed because the model never looked beyond the first few lines of a diff.

---

## Why This Environment Is Unique

### 1. Genuinely Multi-Step Episodic Structure

Most RL environments for LLMs are single-step (prompt-in, completion-out). CodeReview-Env is
**genuinely multi-step**: an agent must decide _in sequence_ whether to gather more evidence
(`open_artifact`) or commit to a final review (`submit_review`). This forces the model to
learn a temporal exploration strategy, not just a single inference pattern.

```
reset() β†’ observe diff + ticket
  β”œβ”€β”€ open_artifact(auth_middleware)   β†’ +0.12 reward, artifact revealed
  β”œβ”€β”€ open_artifact(security_policy)  β†’ +0.10 reward, policy revealed
  └── submit_review(findings=[...])   β†’ grader score + efficiency bonus, done=True
```

### 2. Deterministic, Reproducible Grading β€” No LLM-as-a-Judge

The core reward signal uses a **multi-criterion keyword grader** (`server/tasks.py`) with
deterministic, reproducible scoring β€” no external LLM calls, no flakiness:

- Per-criterion scoring uses 5 weighted factors: `issue keywords`, `recommendation keywords`,
  `severity label`, `file path`, and `evidence trail` (which artifacts were opened).
- Rewards are always in the strictly open interval `(0.05, 0.95)` β€” never exactly 0 or 1 β€”
  enforced by `_clamp_score()` at every layer (`tasks.py`, `reward.py`, `environment.py`).

### 3. Evidence-Gated Difficulty Hierarchy

The three tasks are carefully designed so that **the harder tasks require more artifact
exploration to score well**, creating a natural curriculum:

| Task | # Criteria | Artifacts Needed to Score β‰₯ 0.60 | Baseline Score |
|---|---|---|---|
| `pagination-regression` | 2 | 1 (`test_log`) | ~0.74 |
| `tenant-export-auth` | 2 | 2 (`auth_middleware` + `security_policy`) | ~0.61 |
| `refund-idempotency` | 3 | 4 (`payment_client` + `worker_log` + `db_model` + `regression_test`) | ~0.38 |

The hardest task (`refund-idempotency`) requires multi-artifact correlation to detect all
three issues: the **retry-without-idempotency**, the **concurrent status-update race**, and
the **missing regression test**. A model that skips evidence gathering will miss at least one.

### 4. Strict Pydantic Enforcement

Agent outputs are validated by `codereview_env/models.py` before entering the environment:
- `ReviewFinding` requires `title` (β‰₯5 chars), `file_path` (β‰₯3 chars), `rationale` (β‰₯20 chars),
  `recommendation` (β‰₯12 chars), and a valid `severity` enum.
- `CodeReviewObservation` enforces `score` and `reward` fields as Pydantic `gt=0.0`, `lt=1.0`.

This forces agents to output **structured, actionable findings** rather than free-text markdown.

### 5. Interactive Web Dashboard

Unlike typical headless RL environments, CodeReview-Env ships with a fully integrated
real-time web dashboard (`frontend/index.html`) served at `/` by the FastAPI backend:

- Live episode tracking β€” watch the agent open artifacts and form its review.
- Diff and artifact viewer β€” renders code files and policies directly in the browser.
- Reward breakdown β€” shows how the grader evaluated each finding in real-time.
- Task selector β€” switch between all three tasks interactively.

### 6. Safety Layer

`codereview_env/safety.py` provides:
- **`PaginationValidator`** β€” Guards all pagination inputs against type errors and out-of-range
  values before they reach the underlying pagination system. This mirrors the exact bug in the
  `pagination-regression` task, making the environment self-documenting.
- **`SafeRewardCalculator`** β€” Wraps reward math with the same `max/min` clamping and
  late-rounding pattern used across the rest of the codebase.

---

## Technical Novelty Summary

| Feature | Other RL Envs | CodeReview-Env |
|---|---|---|
| Episode structure | Single step | Multi-step (up to 7 steps) |
| Reward signal | LLM judge (flaky) | Deterministic keywords + evidence trail |
| Reward bounds | Often 0.0 or 1.0 | Strictly (0.05, 0.95) β€” enforced everywhere |
| Output validation | Free-form text | Pydantic schema with min-length constraints |
| Artifact exploration | None | 3–6 artifacts per task, each with reward |
| Difficulty tiers | Flat | 3 tiers with evidence-gated scoring |
| Training framework | Custom | TRL-compatible GRPO reward function |
| Deployment | Often local-only | Docker + HuggingFace Spaces ready |

---

## Real-World Impact

- A model trained here could autonomously triage PRs at **enterprise scale** β€” reviewing
  100+ PRs per day with precise, finding-level comments.
- Replaces vague AI suggestions with **targeted, line-specific security interventions** that
  static analysis tools universally miss (auth bypasses, TOCTOU races, payment idempotency).
- Trained agents learn to **justify their findings** β€” providing rationale and recommendation
  in a format engineers can immediately act on.

---

## Future Extensions

- **Continuous task generation** β€” Dynamically generate new CVE-inspired tasks to prevent
  benchmark overfitting.
- **CI/CD integration** β€” Deploy the trained agent as a GitHub Actions bot that reviews
  incoming PRs automatically.
- **Fix-Application loop** β€” Add a `push_commit` action where the agent patches the code
  based on its own review findings and verifies unit tests pass.
- **Multi-agent review** β€” Multiple agents reviewing the same PR and reaching consensus.

---

## Compatibility Matrix

Fully tested and optimized for:

| Framework | Usage |
|---|---|
| `openenv-core` | Environment base class (`Environment[Obs, Act, State]`) |
| `TRL` (GRPO / PPO) | Reward function via `CodeReviewEnv.get_reward_breakdown()` |
| `Unsloth` | Drop-in with TRL GRPO config |
| `SkyRL` | Compatible via standard OpenAI-style API |
| HuggingFace Spaces | Docker SDK, port 7860, root-level Dockerfile |

See [`examples/run_grpo_training.py`](examples/run_grpo_training.py) for the TRL GRPO
training integration boilerplate.