From 816229133712c08d47cf7162dce1aa4972845af4 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 09:50:53 +1100 Subject: [PATCH] Reviews that converge, a project checklist and a ratchet (0.4.0) - review-brief.mjs writes every reviewer brief: tree, depth (quick, standard, deep with a disproof pass for Blockers), checklist items the diff triggers, round 2+ previous findings and the incremental diff; refuses a fourth round. Round state resets with a new acceptance.md. - docs/checklist.md: diff-triggered items (lib/checklist.mjs), read from the base commit, guarded (tier 3). - Pre-existing findings are reported but do not block; class findings list every instance. - shared/ratchet.md: turn Blockers, reverts and corrections into checks; setup steps for lint guardrails, boundaries, sensitive paths. - evals/: known-bug and clean cases, prepared like real changes; scorer for recall and false Blockers. - sensitive paths are tier 3; run-verify stops a lane at the first failure (--keep-going); stamp.mjs adds a Verified: trailer via a prepare-commit-msg hook; evidence and PR body state door, blast radius and how to revert; make-evidence --checks-only and a CI template. - Fix: a previous change's review was counted as round 1 of the next. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014d2MvQ5v7FCph37jwFCC7i --- README.md | 40 ++++- ...004-reviews-that-converge-and-a-ratchet.md | 40 +++++ evals/README.md | 34 ++++ .../after/src/signup.mjs | 8 + .../after/test/signup.test.mjs | 4 + .../before/src/db.mjs | 7 + .../before/src/signup.mjs | 1 + .../before/test/signup.test.mjs | 1 + .../cases/check-then-insert-signup/case.json | 14 ++ .../after/src/products.mjs | 10 ++ .../after/test/products.test.mjs | 7 + .../before/src/products.mjs | 4 + .../before/test/products.test.mjs | 2 + evals/cases/clean-pagination-clamp/case.json | 11 ++ .../after/src/checkout.mjs | 2 + .../after/src/total.mjs | 4 + .../after/test/total.test.mjs | 3 + .../before/src/checkout.mjs | 2 + .../before/src/total.mjs | 4 + .../before/test/total.test.mjs | 2 + evals/cases/clean-rename-with-tests/case.json | 9 + .../idor-order-lookup/after/src/routes.mjs | 10 ++ .../after/test/routes.test.mjs | 3 + .../cases/idor-order-lookup/before/src/db.mjs | 1 + .../idor-order-lookup/before/src/routes.mjs | 1 + .../before/test/routes.test.mjs | 2 + evals/cases/idor-order-lookup/case.json | 14 ++ .../after/src/orders.mjs | 11 ++ .../after/test/orders.test.mjs | 3 + .../before/src/orders.mjs | 1 + .../before/src/payments.mjs | 1 + .../before/test/orders.test.mjs | 1 + evals/cases/swallowed-payment-error/case.json | 14 ++ evals/run.mjs | 152 ++++++++++++++++ plugins/workflow/.claude-plugin/plugin.json | 2 +- plugins/workflow/scripts/lib/checklist.mjs | 71 ++++++++ .../workflow/scripts/lib/verify-config.mjs | 20 +++ plugins/workflow/scripts/make-evidence.mjs | 51 +++++- plugins/workflow/scripts/review-brief.mjs | 170 ++++++++++++++++++ plugins/workflow/scripts/run-verify.mjs | 12 +- plugins/workflow/scripts/stamp.mjs | 87 +++++++++ plugins/workflow/scripts/turn-start.mjs | 8 +- plugins/workflow/shared/acceptance.md | 2 + plugins/workflow/shared/ratchet.md | 76 ++++++++ plugins/workflow/shared/verify-ci.yml | 58 ++++++ plugins/workflow/shared/verify.config.json | 4 +- .../skills/building-consistent-ui/SKILL.md | 1 + .../building-consistent-ui/design-review.md | 11 +- .../skills/building-consistent-ui/setup.md | 10 +- .../skills/building-consistent-ui/verify.md | 38 ++-- .../skills/building-reliable-backend/SKILL.md | 1 + .../building-reliable-backend/code-review.md | 19 +- .../skills/building-reliable-backend/setup.md | 22 ++- .../building-reliable-backend/verify.md | 40 +++-- test/evals.test.mjs | 39 ++++ test/review-brief.test.mjs | 144 +++++++++++++++ test/stamp-ci.test.mjs | 108 +++++++++++ 57 files changed, 1357 insertions(+), 60 deletions(-) create mode 100644 docs/adr/0004-reviews-that-converge-and-a-ratchet.md create mode 100644 evals/README.md create mode 100644 evals/cases/check-then-insert-signup/after/src/signup.mjs create mode 100644 evals/cases/check-then-insert-signup/after/test/signup.test.mjs create mode 100644 evals/cases/check-then-insert-signup/before/src/db.mjs create mode 100644 evals/cases/check-then-insert-signup/before/src/signup.mjs create mode 100644 evals/cases/check-then-insert-signup/before/test/signup.test.mjs create mode 100644 evals/cases/check-then-insert-signup/case.json create mode 100644 evals/cases/clean-pagination-clamp/after/src/products.mjs create mode 100644 evals/cases/clean-pagination-clamp/after/test/products.test.mjs create mode 100644 evals/cases/clean-pagination-clamp/before/src/products.mjs create mode 100644 evals/cases/clean-pagination-clamp/before/test/products.test.mjs create mode 100644 evals/cases/clean-pagination-clamp/case.json create mode 100644 evals/cases/clean-rename-with-tests/after/src/checkout.mjs create mode 100644 evals/cases/clean-rename-with-tests/after/src/total.mjs create mode 100644 evals/cases/clean-rename-with-tests/after/test/total.test.mjs create mode 100644 evals/cases/clean-rename-with-tests/before/src/checkout.mjs create mode 100644 evals/cases/clean-rename-with-tests/before/src/total.mjs create mode 100644 evals/cases/clean-rename-with-tests/before/test/total.test.mjs create mode 100644 evals/cases/clean-rename-with-tests/case.json create mode 100644 evals/cases/idor-order-lookup/after/src/routes.mjs create mode 100644 evals/cases/idor-order-lookup/after/test/routes.test.mjs create mode 100644 evals/cases/idor-order-lookup/before/src/db.mjs create mode 100644 evals/cases/idor-order-lookup/before/src/routes.mjs create mode 100644 evals/cases/idor-order-lookup/before/test/routes.test.mjs create mode 100644 evals/cases/idor-order-lookup/case.json create mode 100644 evals/cases/swallowed-payment-error/after/src/orders.mjs create mode 100644 evals/cases/swallowed-payment-error/after/test/orders.test.mjs create mode 100644 evals/cases/swallowed-payment-error/before/src/orders.mjs create mode 100644 evals/cases/swallowed-payment-error/before/src/payments.mjs create mode 100644 evals/cases/swallowed-payment-error/before/test/orders.test.mjs create mode 100644 evals/cases/swallowed-payment-error/case.json create mode 100644 evals/run.mjs create mode 100644 plugins/workflow/scripts/lib/checklist.mjs create mode 100644 plugins/workflow/scripts/review-brief.mjs create mode 100644 plugins/workflow/scripts/stamp.mjs create mode 100644 plugins/workflow/shared/ratchet.md create mode 100644 plugins/workflow/shared/verify-ci.yml create mode 100644 test/evals.test.mjs create mode 100644 test/review-brief.test.mjs create mode 100644 test/stamp-ci.test.mjs diff --git a/README.md b/README.md index dd387bf..a560469 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,9 @@ | 2 验收标准 | 写 `.verify/acceptance.md`:可观察的结果,每条写明用什么证明;逐项过一遍异常情况清单 | 无效输入、键盘、慢请求、双击提交、390 宽…… | 边界值、越权、重复请求、并发写、下游失败…… | | 3 实现 | 按约定做 | 需要时出 3 个结构不同的原型 | 先改契约,再按验收标准一条条先写测试后写实现 | | 4 机器检查 | `run-verify.mjs` 按 lane 跑,只认真实退出码 | 样式、类型、截图 + 可访问性、用户路径 | 类型、单元、真实数据库的集成测试、**负向检查**、契约、迁移、用户路径 | -| 5 独立评审 | 没参与实现的子代理评审,逐条标记验收标准 | `design-review.md` 看截图 | `code-review.md` 看 diff,按专项逐一检查,并跑一遍证明关键事实 | -| 6 证据和分档 | `make-evidence.mjs` 生成证据包,验收标准和机器检查分开写,合并分档由脚本算 | 前后对比截图、录屏 | 检查日志、负向检查结果、契约与迁移改动 | +| 5 独立评审 | `review-brief.mjs` 写 brief,交给没参与实现的子代理,逐条标记验收标准和触发的清单项 | `design-review.md` 看截图 | `code-review.md` 看 diff,按专项逐一检查,并跑一遍证明关键事实 | +| 6 证据和分档 | `make-evidence.mjs` 生成证据包,验收标准和机器检查分开写,合并分档由脚本算,写明能否撤回、影响范围、怎么回滚 | 前后对比截图、录屏 | 检查日志、负向检查结果、契约与迁移改动 | +| 7 沉淀 | 评审的 Blocker、回滚、用户的纠正,按 `shared/ratchet.md` 变成检查或清单项 | 同左 | 同左 | 改动大小决定流程轻重:改文案、改颜色这类小改动不用追问,验收标准写 1 到 3 条就行;新流程、接口或数据契约变更、迁移、鉴权才走完整流程。 @@ -45,6 +46,26 @@ 评审在标题下写 `Tree: <这个哈希>`,证据包也写上它。收尾检查和分档只认哈希对得上的运行和评审;`touch`、`checkout` 或者评审之后再改文件,都骗不过去。运行期间如果有检查改写了源码(比如格式化工具开了写入模式),这次运行直接判失败。 +**评审 brief**:`review-brief.mjs` 给每条 lane 的评审写同一份输入:要评的内容哈希、深度,以及 `docs/checklist.md` +里被这次 diff 触发的条目(每条写明触发条件、检查什么、来自哪次事故)。深度分三档:quick(≤30 行、没碰共享模块)、 +standard、deep。契约、迁移、`sensitive` 路径、改了检查本身的改动走 deep,每个 Blocker 都要先尝试推翻它。 +从第 2 轮起,brief 会附上上一轮的发现,评审只看新增的 diff,旧发现逐条标"已修 / 仍未修 / 延后",不再换个说法重复提。 +第 4 轮直接拒绝,交给你决定。没被这次改动碰到的问题标为 `Pre-existing`,照样报告,但不挡合并。 + +**沉淀**(`shared/ratchet.md`):每个 Blocker、每次回滚、每次用户纠正,都按"能写成检查就写成检查"的顺序处理: +lint 规则或类型 > 由 diff 触发的清单项 > 写进约定文件。新规则要有证据(至少两次,或一次真实事故),先在整个仓库上跑一遍 +确认不误报,单独提交(属于改了检查本身,3 档)。每月清理一次。 + +**sensitive 路径**:在 lane 配置里填 `sensitive`(鉴权、计费、权限、删数据、结账),改到就固定 3 档,评审走 deep。 + +**提交盖章**:`node /scripts/stamp.mjs --install` 装一个 `prepare-commit-msg` 钩子,每个改了 lane 文件的提交带上 +`Verified: tree=… tier=… lanes=…`,没验证或提交内容和验证过的不一致就写 `Verified: none (原因)`。这个钩子从不拦提交。查没验证的提交: +`git log --format='%h %s%n %(trailers:key=Verified,valueonly)' origin/main..`。 + +**CI**:把 `shared/verify-ci.yml` 复制到项目的 `.github/workflows/verify.yml`。每个 PR 都会用基线分支的配置在 PR 内容上重跑检查, +按检查结果和改动范围算出分档(`make-evidence --checks-only`),写进 job summary,失败时 status 为红,可以设为必过。 +CI 里不跑独立评审;要不要在 CI 里跑评审,是下一步要定的事。 + **收尾检查**:Stop 钩子。这一轮改动了某条 lane 的文件(和基线提交相比,已提交、未提交、删除都算;内容和这一轮开始时 不同,由 UserPromptSubmit 钩子记录),而这条 lane 的 `run-verify` 没通过或对应的不是当前内容,独立评审缺失、未通过 或对应的不是当前内容,验收标准没有全部在评审里标记,或者证据包不是当前内容的,第一次结束会被拦下;仍未通过就结束时, @@ -79,6 +100,13 @@ baoyu-design 是可选的看图调色工具,只在项目仓库外用(它含 两者都会写约定文件、接好 `.verify/config.json`,没有可脚本化的用户路径时生成 `ctl` 控制命令,最后在未改动的代码上跑一遍全部检查。 之后正常让 Claude 改代码即可。 +### 从 0.3 升级 + +- 评审改为先跑 `review-brief.mjs` 生成 brief(`verify.md` 里写了命令)。延后的 Should-fix 写进 `.verify/deferred-.md`。 +- 评审模板多了 `## Checklist` 和 `Pre-existing`;清单项没过(`- [ ]`)会挡住收尾检查。 +- 可选:在 lane 配置里填 `sensitive`,建 `docs/checklist.md`(空的就行),装提交盖章钩子,接 CI 模板。 +- `run-verify` 现在遇到第一个失败的检查就停下这条 lane,要跑完全部用 `--keep-going`。检查顺序请按从快到慢排。 + ### 从 0.2 升级 - 评审文件要在标题下多写一行 `Tree: `,没有这一行收尾检查不放行。 @@ -103,7 +131,13 @@ baoyu-design 是可选的看图调色工具,只在项目仓库外用(它含 | `plugins/workflow/scripts/run-verify.mjs` | 按 lane 跑检查(含内置负向检查),按真实退出码记录;`--restore`、`--migrate` | | `plugins/workflow/scripts/make-evidence.mjs` | 生成证据包:合并分档、验收标准、机器检查、前后对比截图、录屏;`--pr` 输出 PR 描述 | | `plugins/workflow/scripts/stop-gate.mjs` | Stop 钩子 | -| `plugins/workflow/scripts/turn-start.mjs` | UserPromptSubmit 钩子:记录这一轮开始时的内容哈希 | +| `plugins/workflow/scripts/turn-start.mjs` | UserPromptSubmit 钩子:记录这一轮开始时的内容哈希和插件位置 | +| `plugins/workflow/scripts/review-brief.mjs` | 写评审 brief:内容哈希、深度、触发的清单项、轮次(最多 3 轮) | +| `plugins/workflow/scripts/stamp.mjs` | 提交盖章:`Verified:` trailer;`--install` 装 git 钩子 | +| `plugins/workflow/scripts/lib/checklist.mjs` | 解析 `docs/checklist.md`,按 diff 选出触发的条目 | +| `plugins/workflow/shared/ratchet.md` | 把犯过的错变成检查:分类、准入、单独提交、清理;清单格式 | +| `plugins/workflow/shared/verify-ci.yml` | 项目用的 CI 模板:PR 上重跑检查、算分档 | +| `evals/` | 评审的 eval:已知 bug 要找到,干净改动不能误报;改 `code-review.md` 或 brief 之前先跑 | | `plugins/workflow/scripts/lib/verify-config.mjs` | 读配置(从基线提交)、算改动范围和内容哈希、找出"改了检查本身"的改动 | | `test/` | 这些脚本自己的测试:`npm test`(Node 20+,无依赖),CI 见 `.github/workflows/test.yml` | | `plugins/workflow/scripts/check-raw-styles.mjs` | 找出写死的颜色、Tailwind 色板类、任意值 | diff --git a/docs/adr/0004-reviews-that-converge-and-a-ratchet.md b/docs/adr/0004-reviews-that-converge-and-a-ratchet.md new file mode 100644 index 0000000..f2cf9a7 --- /dev/null +++ b/docs/adr/0004-reviews-that-converge-and-a-ratchet.md @@ -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-.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. diff --git a/evals/README.md b/evals/README.md new file mode 100644 index 0000000..e83278b --- /dev/null +++ b/evals/README.md @@ -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 # 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. diff --git a/evals/cases/check-then-insert-signup/after/src/signup.mjs b/evals/cases/check-then-insert-signup/after/src/signup.mjs new file mode 100644 index 0000000..2978a62 --- /dev/null +++ b/evals/cases/check-then-insert-signup/after/src/signup.mjs @@ -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 }; +} diff --git a/evals/cases/check-then-insert-signup/after/test/signup.test.mjs b/evals/cases/check-then-insert-signup/after/test/signup.test.mjs new file mode 100644 index 0000000..a1c6d8f --- /dev/null +++ b/evals/cases/check-then-insert-signup/after/test/signup.test.mjs @@ -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); diff --git a/evals/cases/check-then-insert-signup/before/src/db.mjs b/evals/cases/check-then-insert-signup/before/src/db.mjs new file mode 100644 index 0000000..ba5d430 --- /dev/null +++ b/evals/cases/check-then-insert-signup/before/src/db.mjs @@ -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)); diff --git a/evals/cases/check-then-insert-signup/before/src/signup.mjs b/evals/cases/check-then-insert-signup/before/src/signup.mjs new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/evals/cases/check-then-insert-signup/before/src/signup.mjs @@ -0,0 +1 @@ +export {}; diff --git a/evals/cases/check-then-insert-signup/before/test/signup.test.mjs b/evals/cases/check-then-insert-signup/before/test/signup.test.mjs new file mode 100644 index 0000000..dcbbff6 --- /dev/null +++ b/evals/cases/check-then-insert-signup/before/test/signup.test.mjs @@ -0,0 +1 @@ +process.exit(0); diff --git a/evals/cases/check-then-insert-signup/case.json b/evals/cases/check-then-insert-signup/case.json new file mode 100644 index 0000000..6202a3b --- /dev/null +++ b/evals/cases/check-then-insert-signup/case.json @@ -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)" + ] +} diff --git a/evals/cases/clean-pagination-clamp/after/src/products.mjs b/evals/cases/clean-pagination-clamp/after/src/products.mjs new file mode 100644 index 0000000..d1a58c6 --- /dev/null +++ b/evals/cases/clean-pagination-clamp/after/src/products.mjs @@ -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) }; +} diff --git a/evals/cases/clean-pagination-clamp/after/test/products.test.mjs b/evals/cases/clean-pagination-clamp/after/test/products.test.mjs new file mode 100644 index 0000000..49fe1cd --- /dev/null +++ b/evals/cases/clean-pagination-clamp/after/test/products.test.mjs @@ -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'); diff --git a/evals/cases/clean-pagination-clamp/before/src/products.mjs b/evals/cases/clean-pagination-clamp/before/src/products.mjs new file mode 100644 index 0000000..22b6f2a --- /dev/null +++ b/evals/cases/clean-pagination-clamp/before/src/products.mjs @@ -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)) }; +} diff --git a/evals/cases/clean-pagination-clamp/before/test/products.test.mjs b/evals/cases/clean-pagination-clamp/before/test/products.test.mjs new file mode 100644 index 0000000..ca6efa6 --- /dev/null +++ b/evals/cases/clean-pagination-clamp/before/test/products.test.mjs @@ -0,0 +1,2 @@ +import { listProducts } from '../src/products.mjs'; +if (listProducts({}).body.length !== 250) process.exit(1); diff --git a/evals/cases/clean-pagination-clamp/case.json b/evals/cases/clean-pagination-clamp/case.json new file mode 100644 index 0000000..afabea7 --- /dev/null +++ b/evals/cases/clean-pagination-clamp/case.json @@ -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." +} diff --git a/evals/cases/clean-rename-with-tests/after/src/checkout.mjs b/evals/cases/clean-rename-with-tests/after/src/checkout.mjs new file mode 100644 index 0000000..b26f9cb --- /dev/null +++ b/evals/cases/clean-rename-with-tests/after/src/checkout.mjs @@ -0,0 +1,2 @@ +import { orderTotal } from './total.mjs'; +export const checkout = (cart) => ({ total: orderTotal(cart.items, cart.discount) }); diff --git a/evals/cases/clean-rename-with-tests/after/src/total.mjs b/evals/cases/clean-rename-with-tests/after/src/total.mjs new file mode 100644 index 0000000..2a39997 --- /dev/null +++ b/evals/cases/clean-rename-with-tests/after/src/total.mjs @@ -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); +} diff --git a/evals/cases/clean-rename-with-tests/after/test/total.test.mjs b/evals/cases/clean-rename-with-tests/after/test/total.test.mjs new file mode 100644 index 0000000..a16c73d --- /dev/null +++ b/evals/cases/clean-rename-with-tests/after/test/total.test.mjs @@ -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); diff --git a/evals/cases/clean-rename-with-tests/before/src/checkout.mjs b/evals/cases/clean-rename-with-tests/before/src/checkout.mjs new file mode 100644 index 0000000..54edd72 --- /dev/null +++ b/evals/cases/clean-rename-with-tests/before/src/checkout.mjs @@ -0,0 +1,2 @@ +import { calcTotal } from './total.mjs'; +export const checkout = (cart) => ({ total: calcTotal(cart.items, cart.discount) }); diff --git a/evals/cases/clean-rename-with-tests/before/src/total.mjs b/evals/cases/clean-rename-with-tests/before/src/total.mjs new file mode 100644 index 0000000..9ea7599 --- /dev/null +++ b/evals/cases/clean-rename-with-tests/before/src/total.mjs @@ -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); +} diff --git a/evals/cases/clean-rename-with-tests/before/test/total.test.mjs b/evals/cases/clean-rename-with-tests/before/test/total.test.mjs new file mode 100644 index 0000000..00fe449 --- /dev/null +++ b/evals/cases/clean-rename-with-tests/before/test/total.test.mjs @@ -0,0 +1,2 @@ +import { calcTotal } from '../src/total.mjs'; +if (calcTotal([{ price: 5, qty: 2 }], 3) !== 7) process.exit(1); diff --git a/evals/cases/clean-rename-with-tests/case.json b/evals/cases/clean-rename-with-tests/case.json new file mode 100644 index 0000000..1f90338 --- /dev/null +++ b/evals/cases/clean-rename-with-tests/case.json @@ -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." +} diff --git a/evals/cases/idor-order-lookup/after/src/routes.mjs b/evals/cases/idor-order-lookup/after/src/routes.mjs new file mode 100644 index 0000000..2036599 --- /dev/null +++ b/evals/cases/idor-order-lookup/after/src/routes.mjs @@ -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 }; + }, +}; diff --git a/evals/cases/idor-order-lookup/after/test/routes.test.mjs b/evals/cases/idor-order-lookup/after/test/routes.test.mjs new file mode 100644 index 0000000..e2365bb --- /dev/null +++ b/evals/cases/idor-order-lookup/after/test/routes.test.mjs @@ -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); } diff --git a/evals/cases/idor-order-lookup/before/src/db.mjs b/evals/cases/idor-order-lookup/before/src/db.mjs new file mode 100644 index 0000000..85cbe42 --- /dev/null +++ b/evals/cases/idor-order-lookup/before/src/db.mjs @@ -0,0 +1 @@ +export const orders = new Map([[1, { id: 1, customerId: 'c1', total: 30 }], [2, { id: 2, customerId: 'c2', total: 99 }]]); diff --git a/evals/cases/idor-order-lookup/before/src/routes.mjs b/evals/cases/idor-order-lookup/before/src/routes.mjs new file mode 100644 index 0000000..69a7788 --- /dev/null +++ b/evals/cases/idor-order-lookup/before/src/routes.mjs @@ -0,0 +1 @@ +export const routes = {}; diff --git a/evals/cases/idor-order-lookup/before/test/routes.test.mjs b/evals/cases/idor-order-lookup/before/test/routes.test.mjs new file mode 100644 index 0000000..5e76387 --- /dev/null +++ b/evals/cases/idor-order-lookup/before/test/routes.test.mjs @@ -0,0 +1,2 @@ +import { routes } from '../src/routes.mjs'; +if (typeof routes !== 'object') process.exit(1); diff --git a/evals/cases/idor-order-lookup/case.json b/evals/cases/idor-order-lookup/case.json new file mode 100644 index 0000000..4c54f8d --- /dev/null +++ b/evals/cases/idor-order-lookup/case.json @@ -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)" + ] +} diff --git a/evals/cases/swallowed-payment-error/after/src/orders.mjs b/evals/cases/swallowed-payment-error/after/src/orders.mjs new file mode 100644 index 0000000..2570e32 --- /dev/null +++ b/evals/cases/swallowed-payment-error/after/src/orders.mjs @@ -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 }; +} diff --git a/evals/cases/swallowed-payment-error/after/test/orders.test.mjs b/evals/cases/swallowed-payment-error/after/test/orders.test.mjs new file mode 100644 index 0000000..75662b8 --- /dev/null +++ b/evals/cases/swallowed-payment-error/after/test/orders.test.mjs @@ -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); diff --git a/evals/cases/swallowed-payment-error/before/src/orders.mjs b/evals/cases/swallowed-payment-error/before/src/orders.mjs new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/evals/cases/swallowed-payment-error/before/src/orders.mjs @@ -0,0 +1 @@ +export {}; diff --git a/evals/cases/swallowed-payment-error/before/src/payments.mjs b/evals/cases/swallowed-payment-error/before/src/payments.mjs new file mode 100644 index 0000000..d2e11ae --- /dev/null +++ b/evals/cases/swallowed-payment-error/before/src/payments.mjs @@ -0,0 +1 @@ +export async function charge(amount) { if (amount > 1000) throw new Error('declined'); return { id: 'ch_1' }; } diff --git a/evals/cases/swallowed-payment-error/before/test/orders.test.mjs b/evals/cases/swallowed-payment-error/before/test/orders.test.mjs new file mode 100644 index 0000000..dcbbff6 --- /dev/null +++ b/evals/cases/swallowed-payment-error/before/test/orders.test.mjs @@ -0,0 +1 @@ +process.exit(0); diff --git a/evals/cases/swallowed-payment-error/case.json b/evals/cases/swallowed-payment-error/case.json new file mode 100644 index 0000000..d515400 --- /dev/null +++ b/evals/cases/swallowed-payment-error/case.json @@ -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)" + ] +} diff --git a/evals/run.mjs b/evals/run.mjs new file mode 100644 index 0000000..6172aa9 --- /dev/null +++ b/evals/run.mjs @@ -0,0 +1,152 @@ +#!/usr/bin/env node +// Reviewer evals: does code-review.md find known bugs, and does it leave clean changes alone? +// +// node evals/run.mjs prepare [--out ] build the case as a project, run the checks, write the brief +// node evals/run.mjs score score the .verify/review-api.md a reviewer wrote there +// node evals/run.mjs all [--reviewer ""] [--case ] +// +// `all` prepares every case; with --reviewer it runs the command in each project with the brief on +// stdin (for example: --reviewer "claude -p --permission-mode acceptEdits"), scores the review, prints +// a table and writes evals/results/