From 669d4a7de6aba3b97ffd46230bd7b34bb10c91b9 Mon Sep 17 00:00:00 2001 From: que01 Date: Wed, 21 Oct 2026 14:41:03 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=94=B6=E6=95=9B=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E8=AF=81=E6=8D=AE=E5=93=88=E5=B8=8C=E5=B9=B6=E6=8E=A5=E5=85=A5?= =?UTF-8?q?=E6=8F=90=E4=BA=A4=E9=92=A9=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../raw/completion-evidence.json | 4 +- .githooks/pre-commit | 6 ++ docs/template-completion-rules.md | 8 ++- package.json | 2 + scripts/lib/template-doctor.mjs | 47 ++++++++++---- scripts/template-evidence-refresh.mjs | 32 ++++++++++ scripts/test/template-doctor.test.mjs | 62 +++++++++++++++++++ 7 files changed, 146 insertions(+), 15 deletions(-) create mode 100755 .githooks/pre-commit create mode 100755 scripts/template-evidence-refresh.mjs diff --git a/.codex-tasks/template-audit-execution/raw/completion-evidence.json b/.codex-tasks/template-audit-execution/raw/completion-evidence.json index f100185..a276d09 100644 --- a/.codex-tasks/template-audit-execution/raw/completion-evidence.json +++ b/.codex-tasks/template-audit-execution/raw/completion-evidence.json @@ -1,7 +1,7 @@ { "schemaVersion": 1, "scope": "模板完成证据登记,保留命令、日志与产物哈希", - "generatedAt": "2026-10-21T06:14:04.698Z", + "generatedAt": "2026-10-21T06:41:20.085Z", "templates": { "acheng-campus-food": { "real-manifest-interaction": { @@ -256,5 +256,5 @@ } } }, - "sourceTreeHash": "e25ff96a374cec651bc82f2b142f662039831c34104005579fec55d921c9f7f7" + "sourceTreeHash": "1b979d8c64ea424e32b7b20a96a2d97c692cc37099f8a9e41d3b45178655a410" } diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..72d81e7 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,6 @@ +#!/bin/sh +# 提交前让模板完成证据的源码哈希跟随当前工作树,避免额外的 chore 提交。 +set -e +[ -f scripts/template-evidence-refresh.mjs ] || exit 0 +command -v node >/dev/null 2>&1 || exit 0 +node scripts/template-evidence-refresh.mjs --stage diff --git a/docs/template-completion-rules.md b/docs/template-completion-rules.md index f7079f3..c6629e1 100644 --- a/docs/template-completion-rules.md +++ b/docs/template-completion-rules.md @@ -30,7 +30,7 @@ staticStatus 只代表支持范围内的静态检查。completionStatus 保持 u 已逐条完成源码审阅的 review 可登记在 `.codex-tasks/template-audit-execution/raw/manual-review.json`。登记必须使用和诊断报告一致的 `file/page/target/binding/line/message`,并保留结论与证据;doctor 只把精确匹配的 review 标记为 `source-reviewed`,不改变 `completionStatus`。未匹配的新 review 和全部 blocker 仍会让命令失败。 -完成证据登记在 `.codex-tasks/template-audit-execution/raw/completion-evidence.json`。每套模板必须提供 `real-manifest-interaction`、`save-reload-publish-isolation`、`visual-reference`、`empty-error-branches` 四类 `passed` 证据;每类证据必须包含实际命令、`exitCode=0`、日志或截图产物及其 SHA-256。registry 还必须记录 source tree hash;doctor 会计算当前仓库源码树(排除 `.codex-tasks/` 证据目录)的 hash,并在不匹配时拒绝完成态。doctor 会校验证据覆盖全部模板、产物存在且哈希一致。全部通过时 `completionStatus=complete`;证据文件缺失时保持 `unverified`;登记不完整或哈希不匹配时对应模板为 `incomplete` 并让命令失败。 +完成证据登记在 `.codex-tasks/template-audit-execution/raw/completion-evidence.json`。每套模板必须提供 `real-manifest-interaction`、`save-reload-publish-isolation`、`visual-reference`、`empty-error-branches` 四类 `passed` 证据;每类证据必须包含实际命令、`exitCode=0`、日志或截图产物及其 SHA-256。registry 还必须记录 source tree hash;doctor 会计算当前仓库源码树的 hash,并在不匹配时拒绝完成态。哈希范围只包含会影响模板交付的源码:排除 `.codex-tasks/`、`.gitea/`、`deploy/` 以及除 `docs/template-completion-rules.md` 之外的全部 markdown。改文档、改 CI 或改部署脚本不再让已完成证据失效,改模板、renderer、诊断器或本文件仍然会。doctor 会校验证据覆盖全部模板、产物存在且哈希一致。全部通过时 `completionStatus=complete`;证据文件缺失时保持 `unverified`;登记不完整或哈希不匹配时对应模板为 `incomplete` 并让命令失败。 AST 解析可追踪 workspace.brand、presentation、object 和局部别名。动态路径、跨组件和间接映射等未知情况标为 review,不能猜测为通过。诊断器包含正反故障注入测试。 @@ -94,3 +94,9 @@ Taro 小程序构建默认会把 CSS 中的 HTML 标签选择器重写为 `.h5-< 4. 原生控件必查:显示类型、宽度、高度令牌、文字垂直居中、点击命中、安全区和底部遮挡。 5. 真机必查:request/media 合法域名、登录、拨号、复制、滚动、字体缩放和 iPhone 底部安全区。 6. 修复方式先查共享 reset、令牌和 Taro 运行时,再改模板局部样式;同类问题必须补产物契约测试或视觉清单项,不允许只改单个模板的单个按钮。 + +## 证据哈希维护 + +`git config core.hooksPath .githooks` 由 `pnpm install` 的 `prepare` 自动设置,`.githooks/pre-commit` 会在提交前调用 `node scripts/template-evidence-refresh.mjs --stage`,把最新的 `sourceTreeHash` 合并进同一个提交。不要再用「先提交业务改动、再提交 `chore: 更新模板完成证据源码哈希`」的两段式流程:那会让真正的改动永远拿不到绿色 CI。 + +需要手动重算时执行 `pnpm evidence:refresh`;加 `--stage` 会同时 `git add -f` 已跟踪的证据文件。 diff --git a/package.json b/package.json index 7832279..e29e732 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,8 @@ "format": "prettier --write .", "format:check": "prettier --check .", "check": "pnpm format:check && pnpm lint && pnpm typecheck && pnpm template:check", + "prepare": "git config core.hooksPath .githooks || true", + "evidence:refresh": "node scripts/template-evidence-refresh.mjs", "db:migrate": "pnpm --filter @personal-brand/api db:migrate", "db:seed": "pnpm --filter @personal-brand/api db:seed", "test:api-client": "pnpm --filter @personal-brand/api exec tsx --test ../../packages/api-client/test/**/*.test.ts", diff --git a/scripts/lib/template-doctor.mjs b/scripts/lib/template-doctor.mjs index 4deeea5..3265a1c 100644 --- a/scripts/lib/template-doctor.mjs +++ b/scripts/lib/template-doctor.mjs @@ -431,32 +431,55 @@ function walkRepositoryFiles(root, directory = root) { return files; } -export function sourceTreeHash(root) { +const EVIDENCE_SCOPE_IGNORED_PREFIXES = [".codex-tasks/", ".gitea/", "deploy/", "scripts/deploy/"]; +const EVIDENCE_SCOPE_IGNORED_EXTENSIONS = [".md", ".mdx"]; +const EVIDENCE_SCOPE_ALWAYS_INCLUDED = ["docs/template-completion-rules.md"]; + +// 证据哈希只覆盖会影响模板交付的源码:文档、CI 配置和部署脚本的改动不使证据失效。 +export function isEvidenceSourcePath(relativePath) { + const normalized = relativePath.split(path.sep).join("/"); + if (EVIDENCE_SCOPE_ALWAYS_INCLUDED.includes(normalized)) return true; + if (EVIDENCE_SCOPE_IGNORED_PREFIXES.some((prefix) => normalized.startsWith(prefix))) return false; + return !EVIDENCE_SCOPE_IGNORED_EXTENSIONS.some((extension) => normalized.endsWith(extension)); +} + +export function sourceTreeHash(root, options = {}) { const rootPath = path.resolve(root); + const { staged = false } = options; let files; + let readContent; if (fs.existsSync(path.join(rootPath, ".git"))) { - const output = execFileSync( - "git", - ["ls-files", "-z", "--cached", "--others", "--exclude-standard"], - { cwd: rootPath, maxBuffer: 64 * 1024 * 1024 }, - ); + // 只统计已跟踪文件:未纳入版本控制的文件不属于任何一次可复现的交付。 + // staged 模式从 Git index 读取内容,供 pre-commit 使用,避免部分暂存时把 + // 工作区里的未提交改动算进提交对应的证据哈希。 + const output = execFileSync("git", ["ls-files", "-z", "--cached"], { + cwd: rootPath, + maxBuffer: 64 * 1024 * 1024, + }); files = output .toString("utf8") .split("\0") - .filter((file) => file && !file.startsWith(".codex-tasks/")) - .map((file) => path.join(rootPath, file)) - .filter((file) => fs.existsSync(file) && fs.statSync(file).isFile()); + .filter((file) => file && isEvidenceSourcePath(file)) + .map((file) => path.join(rootPath, file)); + readContent = staged + ? (file) => + execFileSync("git", ["show", ":" + path.relative(rootPath, file)], { + cwd: rootPath, + maxBuffer: 64 * 1024 * 1024, + }) + : (file) => fs.readFileSync(file); } else { - files = walkRepositoryFiles(rootPath).filter( - (file) => !path.relative(rootPath, file).startsWith(".codex-tasks/"), + files = walkRepositoryFiles(rootPath).filter((file) => + isEvidenceSourcePath(path.relative(rootPath, file)), ); + readContent = (file) => fs.readFileSync(file); } files.sort(); const hash = crypto.createHash("sha256"); for (const file of files) { hash.update(path.relative(rootPath, file)); hash.update("\0"); - hash.update(fs.readFileSync(file)); + hash.update(readContent(file)); hash.update("\0"); } return hash.digest("hex"); diff --git a/scripts/template-evidence-refresh.mjs b/scripts/template-evidence-refresh.mjs new file mode 100755 index 0000000..673fe81 --- /dev/null +++ b/scripts/template-evidence-refresh.mjs @@ -0,0 +1,32 @@ +#!/usr/bin/env node +import fs from "node:fs"; +import path from "node:path"; +import { execFileSync } from "node:child_process"; +import { sourceTreeHash } from "./lib/template-doctor.mjs"; + +const REGISTRY = path.join( + ".codex-tasks", + "template-audit-execution", + "raw", + "completion-evidence.json", +); +const shouldStage = process.argv.includes("--stage"); + +if (!fs.existsSync(REGISTRY)) { + console.log(`template evidence: ${REGISTRY} 不存在,跳过`); + process.exit(0); +} + +const registry = JSON.parse(fs.readFileSync(REGISTRY, "utf8")); +const nextHash = sourceTreeHash(process.cwd(), { staged: shouldStage }); +if (registry.sourceTreeHash === nextHash) { + console.log("template evidence: 源码哈希已是最新"); + process.exit(0); +} + +registry.sourceTreeHash = nextHash; +registry.generatedAt = new Date().toISOString(); +fs.writeFileSync(REGISTRY, JSON.stringify(registry, null, 2) + "\n"); +console.log(`template evidence: sourceTreeHash 更新为 ${nextHash}`); + +if (shouldStage) execFileSync("git", ["add", "-f", REGISTRY], { stdio: "inherit" }); diff --git a/scripts/test/template-doctor.test.mjs b/scripts/test/template-doctor.test.mjs index ceac9f6..5faca3f 100644 --- a/scripts/test/template-doctor.test.mjs +++ b/scripts/test/template-doctor.test.mjs @@ -9,6 +9,7 @@ import { applyCompletionEvidence, applyManualReview, diagnoseTemplate, + isEvidenceSourcePath, sourceTreeHash, } from "../lib/template-doctor.mjs"; @@ -78,6 +79,67 @@ test("CLI 自动发现全部模板,问题退出 1,未知模板退出 2", () assert.equal(missing.status, 2); }); +test("证据哈希范围排除文档、CI 与部署脚本,保留模板交付相关源码", () => { + assert.equal(isEvidenceSourcePath("docs/ci.md"), false); + assert.equal(isEvidenceSourcePath("PROJECT_STATUS.md"), false); + assert.equal(isEvidenceSourcePath(".gitea/workflows/ci.yml"), false); + assert.equal(isEvidenceSourcePath("deploy/install-release.mjs"), false); + assert.equal(isEvidenceSourcePath("scripts/deploy/status.mjs"), false); + assert.equal(isEvidenceSourcePath(".codex-tasks/template-audit-execution/raw/a.json"), false); + assert.equal(isEvidenceSourcePath("docs/template-completion-rules.md"), true); + assert.equal(isEvidenceSourcePath("apps/api/src/main.ts"), true); + assert.equal(isEvidenceSourcePath("packages/template-linjing-editorial/template.json"), true); +}); + +test("文档改动不改变证据哈希,源码改动仍然改变", () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), "template-doctor-scope-")); + try { + fs.mkdirSync(path.join(root, "docs"), { recursive: true }); + fs.writeFileSync(path.join(root, "app.ts"), "export const value = 1;\n"); + const baseline = sourceTreeHash(root); + + fs.writeFileSync(path.join(root, "PROJECT_STATUS.md"), "状态更新\n"); + fs.writeFileSync(path.join(root, "docs", "ci.md"), "文档更新\n"); + assert.equal(sourceTreeHash(root), baseline); + + fs.writeFileSync(path.join(root, "docs", "template-completion-rules.md"), "规则更新\n"); + assert.notEqual(sourceTreeHash(root), baseline); + + fs.writeFileSync(path.join(root, "app.ts"), "export const value = 2;\n"); + assert.notEqual(sourceTreeHash(root), baseline); + } finally { + fs.rmSync(root, { recursive: true, force: true }); + } +}); + +test("pre-commit 证据哈希按暂存区计算,不受部分暂存和未跟踪文件影响", () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), "template-doctor-staged-")); + const git = (...args) => { + const result = spawnSync("git", args, { cwd: root, encoding: "utf8" }); + assert.equal(result.status, 0, result.stderr); + return result.stdout; + }; + try { + git("init", "-q"); + fs.writeFileSync(path.join(root, "app.ts"), "export const value = 1;\n"); + git("add", "app.ts"); + const stagedBaseline = sourceTreeHash(root, { staged: true }); + + fs.writeFileSync(path.join(root, "app.ts"), "export const value = 2;\n"); + fs.writeFileSync(path.join(root, "untracked.ts"), "export const draft = true;\n"); + const stagedWithUnstagedEdits = sourceTreeHash(root, { staged: true }); + assert.equal(stagedWithUnstagedEdits, stagedBaseline); + assert.notEqual(sourceTreeHash(root), stagedBaseline); + + git("-c", "user.name=test", "-c", "user.email=test@example.com", "commit", "-qm", "one"); + git("checkout", "--", "app.ts"); + assert.equal(sourceTreeHash(root), stagedBaseline); + assert.equal(sourceTreeHash(root, { staged: true }), stagedBaseline); + } finally { + fs.rmSync(root, { recursive: true, force: true }); + } +}); + test("完成证据登记必须覆盖全部要求并校验产物哈希", () => { const root = fs.mkdtempSync(path.join(os.tmpdir(), "template-doctor-evidence-")); const artifactPath = path.join(root, "integration.log");