02.2案例档案/zh/projects/zero-core/
第 02 张,共 06 张
ZERO CORE
我的 AI agent 工作流背后的私有中枢
- 角色
- 独立
- 背景
- 一个私有的中枢,保管我的 Claude Code 会话所遵守的规则
- 范围
- 围绕我自己作品的规则、测试和审查 agent
- 成果
- 前 41 次审查提出 95 个问题 · 130 个测试
依赖
- Cloudflare Workers
- D1
- Access
- TypeScript
- Claude API
- Claude Code hooks
变更日志
- 我的 Claude Code 会话遵守的规则集中在一处,并交给每一个会话
- 工作完成后,一个独立的审查 agent 复核改动报告
- 130 个自动化测试,每次部署前都会运行
问题
我的 Claude Code 会话应该每次都遵守同样的规则,我还想看到它们做了什么。只存在我脑子里、或者一个我忘了更新的文件里的规则,只对我记得去提的那几个会话有用。
设计
四个部分,画在图 02 里。
- 1Claude Code 会话 — 我工作的地方;学到东西时,它提议改规则。
- 2hook,在我的电脑上 — 四件事:会话开始时注入适用的规则(有大小上限);拦下违反硬规则的工具调用;同步用量;每次改动后提交改动报告。
- 3Cloudflare Worker,在 Access 之后 — hook 和面板都和它通信。
- 4D1 — 保存规则、提议、报告、用量和待办。
- 5审查 agent — 在全新上下文里读每份改动报告的 diff。
- 6面板 — 八个分区,外加只读的助手“零点”。我在这里批准提议;批准的提议成为下个会话的规则。
我电脑上的 hook。每个会话里都跑着一个 Claude Code hook。会话开始时,它把适用的规则注入进去,有大小上限,规则不会挤占工作的空间。会话中,它拦下违反硬规则的工具调用,比如提交密钥、加入追踪脚本。它同步用量,每次改动之后提交一份改动报告。
Cloudflare Worker 和 D1。规则、提议、报告、用量和待办,保存在 Cloudflare Access 之后的一个 Cloudflare Worker 和它的 D1 数据库里。每份改动报告里的 diff,都会交给一个全新上下文的审查 agent,所以它没见过写出这次改动的那段思路。
回到我手里。Claude 学到东西时,会提议改规则;我在面板里批准,批准的提议就成为下一个会话拿到的规则。面板有八个分区,还有一个只读的助手“零点”:它回答关于规则的问题,不能改它们。



代码
从它的源码里摘出四小段,省略的行用 // … 标出,每段上方是文件名。
export async function preToolUse(input: HookInput, deps: Deps): Promise<HookOutput> { const cache = await deps.readCache(); if (!cache || !input.tool_name) return {}; const snap = cache.snapshot; const call = { tool: input.tool_name, input: input.tool_input ?? {} }; const verdict = evaluate(activeGuards(snap, input.cwd), call); // … const deny: { g: ActiveGuard; detail: string }[] = verdict.deny.map((g) => ({ g, detail: '' })); // … const at = now(); if (deny.length) { const first = deny[0]!; // … return { hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: 'deny', permissionDecisionReason: `Blocked by the owner's hard rule in ZERO CORE: ${describe(first.g)}.${first.detail} Do not try another way around it; tell the owner what you were about to do and ask.`, }, systemMessage: `ZERO CORE blocked ${input.tool_name}: ${first.g.rule.text}${first.detail}`, }; } // …}/** The answer with an ETag (a hash of its body): a request that already holds exactly this gets 304 and no body. Cache-Control stays no-store (index.ts), so nothing along the way keeps it; the panel itself remembers the tag. */export async function etagged(req: Request, data: unknown): Promise<Response> { const text = JSON.stringify(data); const sum = new Uint8Array(await crypto.subtle.digest('SHA-1', new TextEncoder().encode(text))); const tag = `"${[...sum].map((x) => x.toString(16).padStart(2, '0')).join('')}"`; const held = req.headers.get('If-None-Match'); if (held && held.split(',').some((t) => t.trim().replace(/^W\//, '') === tag)) return new Response(null, { status: 304, headers: { ETag: tag } }); return new Response(text, { headers: { 'Content-Type': 'application/json; charset=utf-8', ETag: tag } });}export type PetEvent = | { t: 'start'; id: number } | { t: 'tool'; name: string } | { t: 'text'; d: string } | { t: 'ping' } | { t: 'done'; reply: PetTalkReply | PetDeeperReply } | { t: 'error'; message: string };// …/** One event as a line. */export const encodeEvent = (ev: PetEvent): string => `${JSON.stringify(ev)}\n`;// …export function parseEvent(line: string): PetEvent | null { let v: unknown; try { v = JSON.parse(line); } catch { return null; } if (!v || typeof v !== 'object') return null; const o = v as Record<string, unknown>; switch (o.t) { case 'start': return typeof o.id === 'number' ? { t: 'start', id: o.id } : null; case 'tool': return typeof o.name === 'string' ? { t: 'tool', name: o.name } : null; case 'text': return typeof o.d === 'string' ? { t: 'text', d: o.d } : null; case 'ping': return { t: 'ping' }; case 'done': return o.reply && typeof o.reply === 'object' ? { t: 'done', reply: o.reply as PetTalkReply | PetDeeperReply } : null; case 'error': return { t: 'error', message: typeof o.message === 'string' ? o.message : 'something went wrong on the server' }; default: return null; }}/** The review: the diff zc sends is read by a fresh-context reviewer and dropped; the verdict is kept. zc may instead say why it sends none (`skip`). A report already reviewed is not reviewed again. */export async function reviewReport(env: Env, id: string, b: Body): Promise<{ verdict: Verdict; review: Review | null; note: string | null }> { const db = env.DB; const row = await db.prepare('SELECT * FROM reports WHERE id = ?').bind(id).first<Record<string, unknown>>(); if (!row) throw new HttpError(404, 'no such report'); const r = reportRow(row); if (r.verdict !== 'pending' && r.verdict !== 'skipped') return { verdict: r.verdict, review: r.review, note: r.review_note }; const skip = text(b, 'skip', 300); const diff = typeof b.diff === 'string' ? b.diff : ''; if (diff.length > DIFF_LIMIT + 4000) bad(`diff: at most ${DIFF_LIMIT} characters (zc cuts it down)`); let verdict: Verdict = 'skipped'; let review: Review | null = null; let note: string | null = skip; let model: string | null = null; let usage: { input_tokens: number; output_tokens: number } | null = null; if (!skip) { if (!diff.trim()) note = 'no changes to read'; else { const out = await reviewDiff(env, { title: r.title, points: r.points, files: r.files }, diff); if ('error' in out) note = `not reviewed: ${out.error}`; else { verdict = out.verdict; review = out.review; note = null; model = REVIEW_MODEL; usage = out.usage; } } } // … return { verdict, review, note };}审查 agent 抓到了什么
2026 年 10 月 29 日到 11 月 2 日提交的最初 45 份改动报告里,审查 agent 读了 41 份(跳过 4 份),提出 95 个问题或疑虑。其中我修了多少没有记录,所以不给数字。它找到的三个,和我改了什么:
- 30 秒一次的刷新,每次重绘都会把滚动过的区块索引拉回当前打开的区块。现在只有打开的区块变了,才把它带进视野。
- 重发一个会话的用量而不带标题,会抹掉已经存好的标题。现在重发的行保留已存的标题。
- 保存硬规则失败后,点“重试”只是重新进入确认状态,并没有保存。现在重试就会保存。
怎么测试
130 个单元测试,用 node --test 运行;我写这段的 2026 年 11 月 3 日,npm test 打出 130 个通过、0 个失败。每份改动报告的 diff 还会交给审查 agent,在全新上下文里读一遍。
数字
- 122 KB → 0.3 KB:内容没变时的一次 30 秒轮询,用 ETag 和 304 代替整份响应
- 130 个测试,8 个面板分区