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
33 changes: 33 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: test

on:
push:
branches: [main]
pull_request:

permissions:
contents: read

jobs:
scripts:
name: scripts (node ${{ matrix.node }}, ${{ matrix.os }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
node: [20, 22]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- name: Syntax check every script
run: |
for f in plugins/workflow/scripts/*.mjs plugins/workflow/scripts/lib/*.mjs; do node --check "$f"; done
- name: Hooks and plugin manifests are valid JSON
run: |
for f in plugins/workflow/hooks/hooks.json plugins/workflow/.claude-plugin/plugin.json .claude-plugin/marketplace.json plugins/workflow/shared/verify.config.json; do
node -e "JSON.parse(require('fs').readFileSync(process.argv[1], 'utf8'))" "$f" || { echo "invalid JSON: $f"; exit 1; }
done
- run: npm test
38 changes: 33 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,30 @@
| --- | --- | --- |
| 1 | 检查全过、评审通过、验收标准全部满足,且没有下面的情况 | 扫一眼证据包即可合并 |
| 2 | 改了截图基线或共享模块;有验收标准"未验证";改了源码却没改测试 | 你要看前后对比和标出来的部分 |
| 3 | 改了设计约定、后端约定、接口契约或迁移 | 你确认之后才能合并 |
| 未就绪 | 检查失败、评审未通过、有验收标准未满足或没被评审 | 不合并 |
| 3 | 改了设计约定、后端约定、接口契约或迁移;或者**改了检查本身**(见下) | 你确认之后才能合并 |
| 未就绪 | 检查失败、评审未通过、有验收标准未满足或没被评审;或者运行、评审对应的不是当前磁盘上的内容 | 不合并 |

**收尾检查**:Stop 钩子。有 lane 的文件被改了,但这条 lane 的 `run-verify` 没通过、独立评审缺失、过期或未通过,
验收标准没有全部在评审里标记,或者证据包比评审旧,第一次结束会被拦下;仍未通过就结束时,AI 必须写明"未验证"
(钩子只拦一次,避免无限循环)。关闭:环境变量 `UI_WORKFLOW_GATE=off`。
另外,代码里新增了 `@ts-ignore`、`eslint-disable`、`noqa` 这类"关掉检查"的注释,或者加了重试,至少是 2 档。

**改动不能改判它的检查**:检查一律从基线提交(和默认分支的 merge-base,可用 `VERIFY_BASE` 指定)上的
`.verify/config.json` 读取,工作区里改过的配置不生效。确实要改检查的改动(接新检查、调整 lane)用
`run-verify.mjs --config working` 跑,但这类改动固定是 3 档。算作"改了检查本身"的有:`.verify/config.json`;
测试、lint、类型和 CI 的配置(`playwright.config.*`、`eslint.config.*`、`tsconfig*.json`、`.github/workflows/**`、
`.claude/settings.json` 等,完整清单见 `scripts/lib/verify-config.mjs` 的 `DEFAULT_GUARD`,项目可以在配置的 `guard`
里追加);删掉的测试文件;测试里新增的 `.skip`、`.only`、`xit`、`@pytest.mark.skip`,以及截图对比的 `threshold`、
`maxDiffPixels`、`mask`。

**按内容认,不按时间认**:`run-verify` 记录工作区内容的哈希(`tree`,不含 `.verify/` 和被 `.gitignore` 忽略的文件),
评审在标题下写 `Tree: <这个哈希>`,证据包也写上它。收尾检查和分档只认哈希对得上的运行和评审;`touch`、`checkout`
或者评审之后再改文件,都骗不过去。运行期间如果有检查改写了源码(比如格式化工具开了写入模式),这次运行直接判失败。

**收尾检查**:Stop 钩子。这一轮改动了某条 lane 的文件(和基线提交相比,已提交、未提交、删除都算;内容和这一轮开始时
不同,由 UserPromptSubmit 钩子记录),而这条 lane 的 `run-verify` 没通过或对应的不是当前内容,独立评审缺失、未通过
或对应的不是当前内容,验收标准没有全部在评审里标记,或者证据包不是当前内容的,第一次结束会被拦下;仍未通过就结束时,
AI 必须写明"未验证"(钩子只拦一次,避免无限循环)。只聊天、没改内容的一轮不会被拦。

`UI_WORKFLOW_GATE`:`enforce`(默认,拦截)、`observe`(只记录本来会拦什么,用来先看误报)、`off`(关闭)。
每次判断都追加到 `.verify/gate.log`。

## 安装

Expand Down Expand Up @@ -61,6 +79,13 @@ baoyu-design 是可选的看图调色工具,只在项目仓库外用(它含
两者都会写约定文件、接好 `.verify/config.json`,没有可脚本化的用户路径时生成 `ctl` 控制命令,最后在未改动的代码上跑一遍全部检查。
之后正常让 Claude 改代码即可。

### 从 0.2 升级

- 评审文件要在标题下多写一行 `Tree: <last-run.json 里这条 lane 的 tree>`,没有这一行收尾检查不放行。
按新的 `verify.md` 派评审即可,brief 里已经写了。
- 检查改为从基线提交读配置。如果 `.verify/config.json` 还没合进默认分支,先用 `run-verify.mjs --config working`。
- Stop 钩子现在也会检查这一轮里已经提交的改动。

### 从 0.1 升级

0.1 用的是 `.ui-verify/`。在项目里运行 `node <plugin>/scripts/run-verify.mjs --migrate`,它会把目录改成 `.verify/`、
Expand All @@ -78,6 +103,9 @@ 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/lib/verify-config.mjs` | 读配置(从基线提交)、算改动范围和内容哈希、找出"改了检查本身"的改动 |
| `test/` | 这些脚本自己的测试:`npm test`(Node 20+,无依赖),CI 见 `.github/workflows/test.yml` |
| `plugins/workflow/scripts/check-raw-styles.mjs` | 找出写死的颜色、Tailwind 色板类、任意值 |
| `plugins/workflow/scripts/find-tools.mjs` | 找到已安装的 taste-skill、ui-ux-pro-max、baoyu-design |
| `docs/adr/` | 关键取舍的决策记录 |
Expand Down
47 changes: 47 additions & 0 deletions docs/adr/0003-checks-out-of-reach-and-bound-to-content.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 3. Checks are out of the change's reach, and verification is bound to content

Date: 2026-11-18 · Status: accepted

## Context

Three holes let an agent's change pass verification it should not:

- The change could edit its own judge. `run-verify`, the Stop gate and the merge tier all read
`.verify/config.json` from the working tree, and `make-evidence` left `.verify/` out of the change,
so deleting a check or narrowing a lane's `files` left the tier at 1. Deleted files were invisible
too (`workingChanges` drops them), so deleting a test raised nothing.
- Freshness was decided by file times. "Review newer than run, run newer than the files" breaks on
`touch`, `checkout` and `rebase`, and says nothing about what content a review saw.
- The change was measured against the wrong thing. The Stop gate looked only at uncommitted files,
so committing during a turn skipped it; `make-evidence` diffed against the upstream branch, so
pushed commits stopped counting.

The Claude Code team's verification write-up states the rule plainly: a change must not be able to
modify its own checks, and a review applies to one specific commit.

## Decision

- **Base config.** The checks come from `.verify/config.json` on the base commit (merge-base with the
default branch, or `VERIFY_BASE`). `run-verify --config working` uses the working copy, for a
change that sets up or extends the checks.
- **Guarded changes.** A change to a guard file (`DEFAULT_GUARD` in `lib/verify-config.mjs`, plus the
config's `guard`), a deleted test, or a test gaining a skip, focus, threshold or mask is merge tier
3. A line anywhere that silences a type or lint check, or adds retries, is tier 2.
- **Content hash.** `run-verify` records a hash of the working tree (a `git write-tree` of a copy of
the index after `add -A`, without `.verify/` and ignored files). Reviews name it on a `Tree:` line,
the evidence pack prints it, and the Stop gate and the tier accept only runs and reviews of the tree
now on disk. A check that changes the tree while it runs fails the run.
- **The change is everything since the base**, commits on the branch included, deletions included.
A UserPromptSubmit hook records the tree when a turn starts, so the Stop gate checks only turns
that changed content.
- **Gate modes.** `UI_WORKFLOW_GATE=enforce|observe|off`; every decision goes to `.verify/gate.log`.
- **The scripts have tests** (`test/`, `npm test`) and CI, because they are the judge.

## Consequences

A repository with no remote has no default branch to measure from, so the base is `HEAD` and a
committed edit to the config becomes the base; set `VERIFY_BASE` there. Setting up checks on a branch needs `--config working` until they reach the default branch. Reviews
written before 0.3 lack `Tree:` and must be redone. Hashing the tree costs one `git add -A` into a
temporary index per run and per stop. The guard patterns are a starting list and will need
project additions and pruning. Everything here still runs on the agent's machine, where the agent
can set `UI_WORKFLOW_GATE=off`; the authority for merges belongs in CI, which is the next step.
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "ui-workflow-dev",
"private": true,
"type": "module",
"description": "Tests for the ui-workflow plugin's scripts. The plugin itself has no dependencies.",
"engines": {
"node": ">=20"
},
"scripts": {
"test": "node --test test/*.test.mjs"
}
}
4 changes: 2 additions & 2 deletions plugins/workflow/.claude-plugin/plugin.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ui-workflow",
"version": "0.2.0",
"description": "Front-end and back-end changes you can trust: acceptance criteria first, a design contract and a backend contract, checks per lane (ui, api) including a negative check, independent review, an evidence pack with a computed merge tier, and a Stop gate.",
"version": "0.3.0",
"description": "Front-end and back-end changes you can trust: acceptance criteria first, a design contract and a backend contract, checks per lane (ui, api) including a negative check, independent review, an evidence pack with a computed merge tier, and a Stop gate. Checks are read from the base commit, runs and reviews are bound to a content hash, and changes to the checks themselves are tier 3.",
"author": {
"name": "davidxiaodev"
},
Expand Down
11 changes: 11 additions & 0 deletions plugins/workflow/hooks/hooks.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
{
"hooks": {
"UserPromptSubmit": [
{
"hooks": [
{
"type": "command",
"command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/turn-start.mjs\"",
"timeout": 15
}
]
}
],
"Stop": [
{
"hooks": [
Expand Down
36 changes: 4 additions & 32 deletions plugins/workflow/scripts/lib/negative.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
//
// It edits the working tree in place, so it backs every file up first, restores in a finally block,
// and refuses to start while an older backup exists (a crashed run): `run-verify.mjs --restore`.
import { execFileSync, spawnSync } from "node:child_process";
import { spawnSync } from "node:child_process";
import { copyFileSync, existsSync, mkdirSync, readFileSync, rmSync, unlinkSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { gitLines, inLane, matchAny } from "./verify-config.mjs";
import { changedSince, gitShow, inLane, matchAny, resolveBase } from "./verify-config.mjs";

const BACKUP = "negative-backup";

Expand Down Expand Up @@ -45,7 +45,7 @@ export function runNegative({ root, dir, lane, tests, spec, timeoutMs, env }) {
};
}

const base = resolveBase(root, spec.base);
const base = spec.base ? resolveBase(root, spec.base) : resolveBase(root);
if (!base) return { exitCode: 2, note: "no base commit to compare with (no commits yet?)", log: "" };
const changed = changedSince(root, base).filter((c) => inLane(lane, c.path));
const isTest = (p) => matchAny(spec.tests ?? tests, p);
Expand Down Expand Up @@ -87,7 +87,7 @@ export function runNegative({ root, dir, lane, tests, spec, timeoutMs, env }) {
}
manifest.files.push({ path: s.path, existed });
writeFileSync(join(backupDir, "manifest.json"), JSON.stringify(manifest, null, 2));
const old = baseContent(root, base, s.path);
const old = gitShow(root, base, s.path, { buffer: true });
if (old === null) {
if (existed) unlinkSync(target);
} else {
Expand All @@ -113,31 +113,3 @@ export function runNegative({ root, dir, lane, tests, spec, timeoutMs, env }) {
log: log.join("\n"),
};
}

function resolveBase(root, wanted) {
const candidates = wanted ? [wanted] : ["origin/HEAD", "origin/main", "origin/master"];
for (const ref of candidates) {
const mb = gitLines(root, ["merge-base", "HEAD", ref])[0];
if (mb) return mb;
}
return gitLines(root, ["rev-parse", "HEAD"])[0] ?? null;
}

/** Files that differ between base and the working tree, including untracked ones. */
function changedSince(root, base) {
const out = new Map();
for (const line of gitLines(root, ["diff", "--name-status", "--no-renames", base])) {
const [status, path] = line.split("\t");
out.set(path, { path, status });
}
for (const path of gitLines(root, ["ls-files", "--others", "--exclude-standard"])) out.set(path, { path, status: "A" });
return [...out.values()];
}

function baseContent(root, base, path) {
try {
return execFileSync("git", ["show", `${base}:${path}`], { cwd: root, maxBuffer: 64 << 20, stdio: ["ignore", "pipe", "ignore"] });
} catch {
return null;
}
}
Loading
Loading