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
40 changes: 37 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 条就行;新流程、接口或数据契约变更、迁移、鉴权才走完整流程。

Expand Down Expand Up @@ -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 <plugin>/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` 没通过或对应的不是当前内容,独立评审缺失、未通过
或对应的不是当前内容,验收标准没有全部在评审里标记,或者证据包不是当前内容的,第一次结束会被拦下;仍未通过就结束时,
Expand Down Expand Up @@ -79,6 +100,13 @@ baoyu-design 是可选的看图调色工具,只在项目仓库外用(它含
两者都会写约定文件、接好 `.verify/config.json`,没有可脚本化的用户路径时生成 `ctl` 控制命令,最后在未改动的代码上跑一遍全部检查。
之后正常让 Claude 改代码即可。

### 从 0.3 升级

- 评审改为先跑 `review-brief.mjs` 生成 brief(`verify.md` 里写了命令)。延后的 Should-fix 写进 `.verify/deferred-<lane>.md`。
- 评审模板多了 `## Checklist` 和 `Pre-existing`;清单项没过(`- [ ]`)会挡住收尾检查。
- 可选:在 lane 配置里填 `sensitive`,建 `docs/checklist.md`(空的就行),装提交盖章钩子,接 CI 模板。
- `run-verify` 现在遇到第一个失败的检查就停下这条 lane,要跑完全部用 `--keep-going`。检查顺序请按从快到慢排。

### 从 0.2 升级

- 评审文件要在标题下多写一行 `Tree: <last-run.json 里这条 lane 的 tree>`,没有这一行收尾检查不放行。
Expand All @@ -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 色板类、任意值 |
Expand Down
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