check-stale-doc-phrases.mjs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env node
  2. // 规则改了以后,旧说法常留在别的文档里(同一条规则在多处复述)。这里登记已作废的说法,
  3. // 在当前事实文档里出现即失败。历史计划与重构记录保留当时的写法,不检查。
  4. import { readdir, readFile } from "node:fs/promises";
  5. import { join } from "node:path";
  6. import { fileURLToPath } from "node:url";
  7. const REPO_ROOT = fileURLToPath(new URL("..", import.meta.url));
  8. const SCAN_ROOTS = ["docs", "apps/zcode-cli/docs"];
  9. const EXCLUDED_PREFIXES = ["docs/plans/", "docs/v4-refactor/"];
  10. const STALE_PHRASES = [
  11. {
  12. pattern: /drain (exactly|at most) one guide|one guided drain|最多一条进入同一 turn/i,
  13. rule: "同一边界一次取出全部已等待的 guide(turn-steering.md「Drain 边界」)",
  14. },
  15. {
  16. pattern: /triggerInputId/,
  17. rule: "工作段按 guided user 的 triggerEntityId 切分",
  18. },
  19. {
  20. pattern: /hook additional context 拼/i,
  21. rule: "工具 Hook 文本是批次后的 tool_hook_context 附件,不拼进 tool result(hooks.md「聚合规则」)",
  22. },
  23. ];
  24. async function listMarkdownFiles(dir) {
  25. const entries = await readdir(join(REPO_ROOT, dir), { withFileTypes: true });
  26. const nested = await Promise.all(
  27. entries.map(async (entry) => {
  28. const path = `${dir}/${entry.name}`;
  29. if (EXCLUDED_PREFIXES.some((prefix) => `${path}/`.startsWith(prefix))) return [];
  30. if (entry.isDirectory()) return listMarkdownFiles(path);
  31. return entry.name.endsWith(".md") ? [path] : [];
  32. }),
  33. );
  34. return nested.flat();
  35. }
  36. const files = (await Promise.all(SCAN_ROOTS.map(listMarkdownFiles))).flat();
  37. const findings = [];
  38. for (const file of files) {
  39. const lines = (await readFile(join(REPO_ROOT, file), "utf8")).split("\n");
  40. lines.forEach((line, index) => {
  41. for (const { pattern, rule } of STALE_PHRASES) {
  42. if (pattern.test(line)) findings.push(`${file}:${index + 1}: ${line.trim()}\n -> ${rule}`);
  43. }
  44. });
  45. }
  46. if (findings.length > 0) {
  47. console.error(`Stale doc phrases found (${findings.length}):\n${findings.join("\n")}`);
  48. process.exit(1);
  49. }
  50. console.log(`No stale doc phrases in ${files.length} files.`);