chore: 收敛模板证据哈希并接入提交钩子

This commit is contained in:
que01 committed 2026-10-21 14:41:23 +08:00
1 parent ec0cf78d69
commit 669d4a7de6
7 files changed
+146 -15

No files matched your search

@@ -1,7 +1,7 @@
{ {
"schemaVersion": 1, "schemaVersion": 1,
"scope": "模板完成证据登记,保留命令、日志与产物哈希", "scope": "模板完成证据登记,保留命令、日志与产物哈希",
"generatedAt": "2026-10-21T06:14:04.698Z", "generatedAt": "2026-10-21T06:41:20.085Z",
"templates": { "templates": {
"acheng-campus-food": { "acheng-campus-food": {
"real-manifest-interaction": { "real-manifest-interaction": {
@@ -256,5 +256,5 @@
} }
} }
}, },
"sourceTreeHash": "e25ff96a374cec651bc82f2b142f662039831c34104005579fec55d921c9f7f7" "sourceTreeHash": "1b979d8c64ea424e32b7b20a96a2d97c692cc37099f8a9e41d3b45178655a410"
} }
+6
View File
@@ -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
+7 -1
View File
@@ -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 仍会让命令失败。 已逐条完成源码审阅的 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,不能猜测为通过。诊断器包含正反故障注入测试。 AST 解析可追踪 workspace.brand、presentation、object 和局部别名。动态路径、跨组件和间接映射等未知情况标为 review,不能猜测为通过。诊断器包含正反故障注入测试。
@@ -94,3 +94,9 @@ Taro 小程序构建默认会把 CSS 中的 HTML 标签选择器重写为 `.h5-<
4. 原生控件必查:显示类型、宽度、高度令牌、文字垂直居中、点击命中、安全区和底部遮挡。 4. 原生控件必查:显示类型、宽度、高度令牌、文字垂直居中、点击命中、安全区和底部遮挡。
5. 真机必查:request/media 合法域名、登录、拨号、复制、滚动、字体缩放和 iPhone 底部安全区。 5. 真机必查:request/media 合法域名、登录、拨号、复制、滚动、字体缩放和 iPhone 底部安全区。
6. 修复方式先查共享 reset、令牌和 Taro 运行时,再改模板局部样式;同类问题必须补产物契约测试或视觉清单项,不允许只改单个模板的单个按钮。 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` 已跟踪的证据文件。
+2
View File
@@ -14,6 +14,8 @@
"format": "prettier --write .", "format": "prettier --write .",
"format:check": "prettier --check .", "format:check": "prettier --check .",
"check": "pnpm format:check && pnpm lint && pnpm typecheck && pnpm template: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:migrate": "pnpm --filter @personal-brand/api db:migrate",
"db:seed": "pnpm --filter @personal-brand/api db:seed", "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", "test:api-client": "pnpm --filter @personal-brand/api exec tsx --test ../../packages/api-client/test/**/*.test.ts",
+35 -12
View File
@@ -431,32 +431,55 @@ function walkRepositoryFiles(root, directory = root) {
return files; 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 rootPath = path.resolve(root);
const { staged = false } = options;
let files; let files;
let readContent;
if (fs.existsSync(path.join(rootPath, ".git"))) { if (fs.existsSync(path.join(rootPath, ".git"))) {
const output = execFileSync( // 只统计已跟踪文件:未纳入版本控制的文件不属于任何一次可复现的交付。
"git", // staged 模式从 Git index 读取内容,供 pre-commit 使用,避免部分暂存时把
["ls-files", "-z", "--cached", "--others", "--exclude-standard"], // 工作区里的未提交改动算进提交对应的证据哈希。
{ cwd: rootPath, maxBuffer: 64 * 1024 * 1024 }, const output = execFileSync("git", ["ls-files", "-z", "--cached"], {
); cwd: rootPath,
maxBuffer: 64 * 1024 * 1024,
});
files = output files = output
.toString("utf8") .toString("utf8")
.split("\0") .split("\0")
.filter((file) => file && !file.startsWith(".codex-tasks/")) .filter((file) => file && isEvidenceSourcePath(file))
.map((file) => path.join(rootPath, file)) .map((file) => path.join(rootPath, file));
.filter((file) => fs.existsSync(file) && fs.statSync(file).isFile()); readContent = staged
? (file) =>
execFileSync("git", ["show", ":" + path.relative(rootPath, file)], {
cwd: rootPath,
maxBuffer: 64 * 1024 * 1024,
})
: (file) => fs.readFileSync(file);
} else { } else {
files = walkRepositoryFiles(rootPath).filter( files = walkRepositoryFiles(rootPath).filter((file) =>
(file) => !path.relative(rootPath, file).startsWith(".codex-tasks/"), isEvidenceSourcePath(path.relative(rootPath, file)),
); );
readContent = (file) => fs.readFileSync(file);
} }
files.sort(); files.sort();
const hash = crypto.createHash("sha256"); const hash = crypto.createHash("sha256");
for (const file of files) { for (const file of files) {
hash.update(path.relative(rootPath, file)); hash.update(path.relative(rootPath, file));
hash.update("\0"); hash.update("\0");
hash.update(fs.readFileSync(file)); hash.update(readContent(file));
hash.update("\0"); hash.update("\0");
} }
return hash.digest("hex"); return hash.digest("hex");
+32
View File
@@ -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" });
+62
View File
@@ -9,6 +9,7 @@ import {
applyCompletionEvidence, applyCompletionEvidence,
applyManualReview, applyManualReview,
diagnoseTemplate, diagnoseTemplate,
isEvidenceSourcePath,
sourceTreeHash, sourceTreeHash,
} from "../lib/template-doctor.mjs"; } from "../lib/template-doctor.mjs";
@@ -78,6 +79,67 @@ test("CLI 自动发现全部模板,问题退出 1,未知模板退出 2", ()
assert.equal(missing.status, 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("完成证据登记必须覆盖全部要求并校验产物哈希", () => { test("完成证据登记必须覆盖全部要求并校验产物哈希", () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "template-doctor-evidence-")); const root = fs.mkdtempSync(path.join(os.tmpdir(), "template-doctor-evidence-"));
const artifactPath = path.join(root, "integration.log"); const artifactPath = path.join(root, "integration.log");