Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
203 changes: 159 additions & 44 deletions README.md

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions docs/adr/0004-reviews-that-converge-and-a-ratchet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 4. Reviews that converge, a project checklist, and a ratchet

Date: 2026-11-18 · Status: accepted

## Context

Each review round dispatched a fresh reviewer with the same hand-written brief. Fresh reviewers
re-read everything, re-flag old code, and phrase old findings anew, so rounds do not converge (the
Claude Code team saw one change run fifty rounds overnight, mostly on code from before the first
pass). The adverse-case list in `acceptance.md` is generic; nothing turned a project's own incidents
into something the next review checks. Nothing measured whether a change to `code-review.md` made
reviews better or worse. And a finding fixed in code was fixed once: no check stopped it recurring.

## Decision

- `review-brief.mjs` writes every brief: the tree, a depth (quick, standard, deep; deep for
contracts, migrations, `sensitive` paths and changed checks, where a Blocker must survive an
attempt to disprove it), the `docs/checklist.md` items the diff triggers, and from round 2 the
previous findings plus only the diff since the reviewed tree. It refuses a fourth round.
The reviewer stays fresh each round (independence from the builder), but no longer starts blind.
- Findings in untouched code are `Pre-existing` and do not block. A finding that is one of a class
lists every instance.
- `docs/checklist.md`: items with a diff-readable trigger, a check, and a source incident. Read from
the base commit; changing it is a guarded change (tier 3).
- `shared/ratchet.md`: every Blocker, revert and user correction becomes, in order of preference, a
deterministic check, a checklist item, or a contract line; admitted with evidence, shipped as its
own change, pruned monthly.
- `evals/`: known-bug cases and clean controls, built and briefed the way real changes are; score
recall and false Blockers before changing the reviewer.
- Also: `sensitive` paths are tier 3; `run-verify` stops a lane at the first failing check; commits
carry a `Verified:` trailer; evidence and PR bodies state the door, blast radius and how to revert;
a CI template reruns the checks from the base branch's config and computes a checks-only tier.

## Consequences

Briefs are generated, so changing what reviewers get means changing a script with tests. Round state
lives in `.verify/rounds-<lane>.json` and resets when `acceptance.md` changes. The eval set is small
and backend-only; it grows with real incidents, and UI cases need screenshots that are not built yet.
CI does not run the independent review; how it should (committed reviews, or a reviewer in CI) is
still open.
34 changes: 34 additions & 0 deletions evals/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Reviewer evals

`code-review.md` is the prompt every project's backend review runs on. Changing it, the brief
(`review-brief.mjs`) or the reviewer's model changes what gets caught, so every such change is
scored here first: known bugs it must find, and clean changes it must approve without inventing
Blockers.

```bash
node evals/run.mjs all --reviewer "claude -p --permission-mode acceptEdits" # prepare, review, score
node evals/run.mjs all # prepare only, print briefs
node evals/run.mjs score <project dir> <case> # score one review
```

Each case becomes a small git project: `before/` committed on `main`, `after/` on a branch, the
acceptance criteria written, `run-verify` run, and the brief written by `review-brief.mjs`, exactly as
in a real change. The reviewer gets only the brief.

| Metric | Means |
| --- | --- |
| recall | bug cases where an open Blocker or Should-fix line matches every `match` regex |
| as Blocker | the same, counting only Blockers |
| clean approved | clean cases approved with no open Blocker |
| false Blockers | open Blockers across clean cases |

Results go to `evals/results/`. Compare with the last run before you merge a change to the
reviewer; a drop in recall or a rise in false Blockers is a regression.

## Adding a case

Every incident a review should have caught becomes a case (`shared/ratchet.md`). Keep the code as
small as the bug allows, write the `answer` as one sentence, and make `match` specific enough that a
vague finding does not count ("orders" and "not scoped|owner|IDOR", not just "security"). Add a
clean case for every two bug cases, so tuning for recall cannot pass by flagging everything. Keep
cases used to tune the prompt apart from a few held back for a final check.
8 changes: 8 additions & 0 deletions evals/cases/check-then-insert-signup/after/src/signup.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { users } from './db.mjs';

export async function signup(email) {
const existing = await users.findByEmail(email);
if (existing) return { status: 409 };
const user = await users.insert({ email, createdAt: Date.now() });
return { status: 201, body: user };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { signup } from '../src/signup.mjs';
const a = await signup('a@x.io');
const b = await signup('a@x.io');
if (a.status !== 201 || b.status !== 409) process.exit(1);
7 changes: 7 additions & 0 deletions evals/cases/check-then-insert-signup/before/src/db.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const rows = [];
export const users = {
async findByEmail(email) { await tick(); return rows.find((r) => r.email === email) ?? null; },
async insert(row) { await tick(); rows.push(row); return row; },
all: () => rows,
};
const tick = () => new Promise((r) => setTimeout(r, 1));
1 change: 1 addition & 0 deletions evals/cases/check-then-insert-signup/before/src/signup.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process.exit(0);
14 changes: 14 additions & 0 deletions evals/cases/check-then-insert-signup/case.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"lane": "api",
"kind": "bug",
"request": "Sign-up must never create two accounts for the same email.",
"acceptance": [
"A1 POST /signup with a new email creates one account",
"A2 two POST /signup calls with the same email at the same time leave exactly one account"
],
"answer": "signup checks for the email and then inserts, with no unique constraint, so two concurrent requests both insert.",
"match": [
"(signup|users|email)",
"(race|concurren|unique|at the same time|check.then.insert|duplicate|atomic|constraint)"
]
}
10 changes: 10 additions & 0 deletions evals/cases/clean-pagination-clamp/after/src/products.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const all = Array.from({ length: 250 }, (_, i) => ({ id: i + 1 }));
const DEFAULT_LIMIT = 20;
const MAX_LIMIT = 100;

export function listProducts(query) {
const raw = query.limit ?? String(DEFAULT_LIMIT);
if (!/^\d+$/.test(raw) || Number(raw) < 1) return { status: 400, body: { error: 'limit must be a positive integer' } };
const limit = Math.min(Number(raw), MAX_LIMIT);
return { status: 200, body: all.slice(0, limit) };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { listProducts } from '../src/products.mjs';
const eq = (a, b, m) => { if (a !== b) { console.error(m, a, b); process.exit(1); } };
eq(listProducts({}).body.length, 20, 'default');
eq(listProducts({ limit: '500' }).body.length, 100, 'clamp');
eq(listProducts({ limit: '5' }).body.length, 5, 'small');
eq(listProducts({ limit: '0' }).status, 400, 'zero');
eq(listProducts({ limit: 'abc' }).status, 400, 'nan');
4 changes: 4 additions & 0 deletions evals/cases/clean-pagination-clamp/before/src/products.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const all = Array.from({ length: 250 }, (_, i) => ({ id: i + 1 }));
export function listProducts(query) {
return { status: 200, body: all.slice(0, Number(query.limit ?? all.length)) };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { listProducts } from '../src/products.mjs';
if (listProducts({}).body.length !== 250) process.exit(1);
11 changes: 11 additions & 0 deletions evals/cases/clean-pagination-clamp/case.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"lane": "api",
"kind": "clean",
"request": "Cap the page size of GET /products at 100 and default it to 20.",
"acceptance": [
"A1 no limit returns 20 items",
"A2 limit=500 returns 100 items",
"A3 limit=0 or a non-number returns 400"
],
"answer": "Correct: the limit is parsed, validated and clamped, with tests for each case."
}
2 changes: 2 additions & 0 deletions evals/cases/clean-rename-with-tests/after/src/checkout.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { orderTotal } from './total.mjs';
export const checkout = (cart) => ({ total: orderTotal(cart.items, cart.discount) });
4 changes: 4 additions & 0 deletions evals/cases/clean-rename-with-tests/after/src/total.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function orderTotal(items, discount = 0) {
const sum = items.reduce((s, i) => s + i.price * i.qty, 0);
return Math.max(0, sum - discount);
}
3 changes: 3 additions & 0 deletions evals/cases/clean-rename-with-tests/after/test/total.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { orderTotal } from '../src/total.mjs';
if (orderTotal([{ price: 5, qty: 2 }], 3) !== 7) process.exit(1);
if (orderTotal([{ price: 1, qty: 1 }], 5) !== 0) process.exit(1);
2 changes: 2 additions & 0 deletions evals/cases/clean-rename-with-tests/before/src/checkout.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { calcTotal } from './total.mjs';
export const checkout = (cart) => ({ total: calcTotal(cart.items, cart.discount) });
4 changes: 4 additions & 0 deletions evals/cases/clean-rename-with-tests/before/src/total.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function calcTotal(items, discount = 0) {
const sum = items.reduce((s, i) => s + i.price * i.qty, 0);
return Math.max(0, sum - discount);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { calcTotal } from '../src/total.mjs';
if (calcTotal([{ price: 5, qty: 2 }], 3) !== 7) process.exit(1);
9 changes: 9 additions & 0 deletions evals/cases/clean-rename-with-tests/case.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"lane": "api",
"kind": "clean",
"request": "Rename calcTotal to orderTotal everywhere; no behaviour change.",
"acceptance": [
"A1 orderTotal returns the same totals calcTotal did, including discounts"
],
"answer": "Correct: a pure rename, callers and tests updated."
}
10 changes: 10 additions & 0 deletions evals/cases/idor-order-lookup/after/src/routes.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { orders } from './db.mjs';

export const routes = {
'GET /orders/:id': (req) => {
if (!req.user) return { status: 401 };
const order = orders.get(Number(req.params.id));
if (!order) return { status: 404 };
return { status: 200, body: order };
},
};
3 changes: 3 additions & 0 deletions evals/cases/idor-order-lookup/after/test/routes.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { routes } from '../src/routes.mjs';
const r = routes['GET /orders/:id']({ user: { id: 'c1' }, params: { id: '1' } });
if (r.status !== 200 || r.body.id !== 1) { console.error('own order'); process.exit(1); }
1 change: 1 addition & 0 deletions evals/cases/idor-order-lookup/before/src/db.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const orders = new Map([[1, { id: 1, customerId: 'c1', total: 30 }], [2, { id: 2, customerId: 'c2', total: 99 }]]);
1 change: 1 addition & 0 deletions evals/cases/idor-order-lookup/before/src/routes.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const routes = {};
2 changes: 2 additions & 0 deletions evals/cases/idor-order-lookup/before/test/routes.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { routes } from '../src/routes.mjs';
if (typeof routes !== 'object') process.exit(1);
14 changes: 14 additions & 0 deletions evals/cases/idor-order-lookup/case.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"lane": "api",
"kind": "bug",
"request": "Add GET /orders/:id so a signed-in customer can see one of their orders.",
"acceptance": [
"A1 GET /orders/:id for the caller's own order returns 200 with the order",
"A2 GET /orders/:id for another customer's order returns 404 and no order data"
],
"answer": "getOrder looks the order up by id only, so any signed-in user can read another customer's order (IDOR).",
"match": [
"orders",
"(scope|owner|customer_?id|user_?id|idor|another (user|customer)|not (scoped|filtered)|authori)"
]
}
11 changes: 11 additions & 0 deletions evals/cases/swallowed-payment-error/after/src/orders.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { charge } from './payments.mjs';

export async function placeOrder(order) {
try {
await charge(order.total);
} catch {
// payment provider is flaky; keep going
}
order.status = 'paid';
return { status: 201, body: order };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { placeOrder } from '../src/orders.mjs';
const r = await placeOrder({ total: 20 });
if (r.status !== 201 || r.body.status !== 'paid') process.exit(1);
1 change: 1 addition & 0 deletions evals/cases/swallowed-payment-error/before/src/orders.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export async function charge(amount) { if (amount > 1000) throw new Error('declined'); return { id: 'ch_1' }; }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process.exit(0);
14 changes: 14 additions & 0 deletions evals/cases/swallowed-payment-error/case.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"lane": "api",
"kind": "bug",
"request": "Charge the card when an order is placed; the customer must see an error if the charge fails.",
"acceptance": [
"A1 placing an order charges the card once and returns 201",
"A2 when the charge fails the caller gets an error and the order is not marked paid"
],
"answer": "placeOrder catches the charge error, ignores it, and still marks the order paid and returns 201.",
"match": [
"(charge|payment|placeOrder|catch)",
"(swallow|ignor|empty catch|catch \\{|still (marks|returns)|paid|success)"
]
}
Loading
Loading