File size: 7,491 Bytes
5246c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# OpenCodeReview Production Revamp and Offline Evaluation Plan

## Scope

This plan includes the earlier audit recommendations and adds a repeatable offline correctness benchmark using `code-review-bench/code-review-bench`.

The goal is to improve review correctness before allowing automatic use:

```
offline benchmark -> inspect failures -> change prompts/retrieval/router -> rerun benchmark -> live PR cross-check
```

## Earlier recommendations included

- Durable job storage instead of the process-local API registry
- Durable LangGraph checkpoints instead of ephemeral SQLite
- Correct posting receipts with posted/partial/failed states
- React polling, findings, verdict, approval, rejection, and delivery status
- GitHub OAuth/GitHub App authentication
- Per-user authorization and rate limits
- Idempotent GitHub posting
- HF Bucket storage for diffs, vectors, reports, caches, and RAGAS artifacts
- Supabase/Postgres for jobs, users, approvals, and audit events
- RAGAS status and score persistence
- Fast/free model routing with fallback and budgets
- Repository policies and review modes
- Automated unit, integration, and browser tests
- CI deployment gates and health checks
- Model, prompt, latency, cost, and quality observability
- Live validation against a real pull request before release

## Offline benchmark

### Primary dataset

Use [code-review-bench/code-review-bench](https://huggingface.co/datasets/code-review-bench/code-review-bench).

Use the `offline` split first because it contains expert-curated golden issues. Use `online` later for comparison against other review tools.

The benchmark must run without:

- GitHub comments
- GitHub write permissions
- human approval
- repository mutation
- production credentials
- external posting

### Prediction format

The review engine must export one JSON object per row:

```json
{
  "task_id": "stable-row-id",
  "findings": [
    {
      "file_path": "src/example.py",
      "line_start": 42,
      "line_end": 44,
      "severity": "high",
      "category": "correctness",
      "comment": "Explanation of the issue",
      "confidence": 0.87
    }
  ],
  "verdict": {
    "recommendation": "request_changes",
    "overall_score": 6.5
  },
  "model": "provider/model",
  "prompt_version": "review-v1",
  "latency_ms": 12000,
  "cost": 0.0
}
```

### Offline scoring

Score every generated finding against the expert issue using:

- file path match
- line overlap with tolerance
- semantic keyword overlap
- severity agreement
- category agreement

Report:

- precision
- recall
- F1
- finding-level accuracy
- file-location accuracy
- severity accuracy
- false-positive rate
- no-issue accuracy
- average confidence
- confidence calibration
- latency
- cost

Do not use exact patch matching as the primary metric. Several valid fixes can solve the same issue.

### Error buckets

Every failure should be assigned one bucket:

- missed issue
- wrong file
- wrong line
- wrong severity
- wrong category
- vague explanation
- false positive
- duplicate finding
- unsupported claim
- malformed output
- timeout/provider failure
- retrieval failure

### Improvement loop

After each run:

1. Save predictions and scores.
2. Export false positives and missed issues.
3. Inspect the worst examples.
4. Classify root cause:
   - ingestion
   - retrieval
   - prompt
   - model
   - aggregation
   - line mapping
   - schema validation
5. Apply the smallest targeted change.
6. Rerun the same fixed validation split.
7. Compare against the previous result.
8. Keep the change only if quality improves without unacceptable latency/cost regression.

Keep a regression set of every previously fixed failure.

## Implementation phases

### Phase 1: offline correctness harness

- Add `eval_data/code_review_bench_eval.py`.
- Download the dataset through `datasets`.
- Normalize offline rows into a stable input format.
- Accept agent predictions as JSONL.
- Compute matching metrics and failure buckets.
- Write `eval_data/code_review_bench_results.json`.
- Add a smoke fixture with three rows for CI.

### Phase 2: dry-run review adapter

- Add a review-engine mode that accepts a prepared diff and changed files.
- Disable GitHub writes.
- Disable human interrupt.
- Return findings, verdict, model metadata, and RAGAS task metadata.
- Add `--dataset code-review-bench` and `--max-rows N`.
- Store large inputs/outputs in the HF Bucket.

### Phase 3: RAGAS correctness

- Persist RAGAS task state with the review ID.
- Do not mark RAGAS complete when scores are empty.
- Return completed metric values or a clear failure reason.
- Compare retrieval metrics against issue-detection metrics.
- Add a no-LLM deterministic retrieval baseline.

### Phase 4: durable production workflow

- Move jobs and approvals to Supabase/Postgres.
- Store LangGraph thread IDs and head SHAs.
- Use a durable queue/worker.
- Add retries, timeouts, cancellation, and dead-letter states.
- Store artifacts and reports in the HF Bucket.

### Phase 5: authentication and GitHub

- Implement GitHub OAuth or GitHub App login.
- Use secure HTTP-only sessions.
- Associate jobs with users.
- Enforce repository access and job ownership.
- Add idempotency keys.
- Recheck PR head SHA before posting.
- Return exact GitHub comment URLs and failure counts.

### Phase 6: model routing

- Add `fast`, `balanced`, and `deep` profiles.
- Prefer free/local providers when configured.
- Use provider health checks.
- Retry only transient errors.
- Enforce token, latency, and cost budgets.
- Record provider/model/prompt version for every run.

### Phase 7: release verification

- Unit tests
- API contract tests
- Graph interrupt/resume tests
- Fake GitHub integration tests
- Browser tests
- Container build
- Vercel build
- Secret scan
- Dependency audit
- Deployment health check
- Offline benchmark gate
- Live PR cross-check

## Release gates

Do not call a release production-ready unless:

- offline F1 meets the agreed threshold
- false-positive rate is measured
- no malformed predictions occur in the smoke set
- RAGAS returns real metrics or an explicit failure state
- review state survives a worker restart
- approval is authenticated
- posting is idempotent
- a live PR review matches an independent cross-check
- Vercel and backend health checks pass

## Live cross-check procedure

1. Select a public PR not used in development.
2. Run it through the React/Vercel flow.
3. Save the generated findings.
4. Run the Hugging Face/legacy flow against the same repository, PR, and head SHA.
5. Compare:
   - finding count
   - file and line locations
   - severity
   - explanation
   - verdict
   - RAGAS status and scores
6. Do not approve or post until the comparison is complete.
7. Record the result in the evaluation report.

## Storage design

HF Bucket:

```text
eval/<dataset>/<run-id>/
  manifest.json
  predictions.jsonl
  scores.json
  failures.jsonl
  ragas.json
  reports/
```

Postgres:

```text
reviews
review_findings
review_events
model_runs
approvals
evaluation_runs
```

## Correctness principles

- A finding is correct only when its claim is supported by the diff/context.
- A high confidence score must correspond to a high likelihood of correctness.
- A valid review may contain no findings.
- A patch that passes tests is not automatically a good review.
- The system must distinguish “no issue found” from “review failed.”
- Every result must include model, prompt version, dataset revision, and timestamp.