@dommaker/harness 4.0.1 → 4.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/dist/bootstrap/bootstrap.d.ts +15 -0
  2. package/dist/bootstrap/bootstrap.js +18 -1
  3. package/dist/bootstrap/index.d.ts +2 -2
  4. package/dist/bootstrap/index.js +3 -2
  5. package/dist/cli/commands/acceptance.d.ts +8 -0
  6. package/dist/cli/commands/acceptance.js +45 -24
  7. package/dist/cli/commands/check.d.ts +2 -0
  8. package/dist/cli/commands/check.js +36 -5
  9. package/dist/cli/commands/command.js +1 -1
  10. package/dist/cli/commands/definitions.js +3 -0
  11. package/dist/cli/commands/release.d.ts +11 -3
  12. package/dist/cli/commands/release.js +159 -52
  13. package/dist/cli/commands/report.d.ts +2 -0
  14. package/dist/cli/commands/report.js +54 -6
  15. package/dist/cli/commands/security.d.ts +3 -3
  16. package/dist/cli/commands/security.js +3 -2
  17. package/dist/cli/commands/sync-docs/capabilities-syncer.d.ts +3 -40
  18. package/dist/cli/commands/sync-docs/capabilities-syncer.js +5 -159
  19. package/dist/cli/commands/sync-docs/compact.js +2 -2
  20. package/dist/cli/commands/sync-docs/context-syncer.d.ts +4 -36
  21. package/dist/cli/commands/sync-docs/context-syncer.js +4 -221
  22. package/dist/cli/commands/sync-docs/index.d.ts +8 -8
  23. package/dist/cli/commands/sync-docs/index.js +8 -8
  24. package/dist/cli/commands/sync-docs/main-flow.d.ts +5 -4
  25. package/dist/cli/commands/sync-docs/main-flow.js +6 -5
  26. package/dist/cli/commands/sync-docs/sync-output.d.ts +1 -1
  27. package/dist/cli/commands/sync-docs/sync-output.js +4 -0
  28. package/dist/cli/commands/sync-docs/sync-writes.d.ts +1 -1
  29. package/dist/cli/commands/validate.d.ts +2 -0
  30. package/dist/cli/commands/validate.js +40 -1
  31. package/dist/core/command-execution-policy.d.ts +59 -0
  32. package/dist/core/command-execution-policy.js +167 -0
  33. package/dist/core/constraints/capabilities-parser.d.ts +2 -1
  34. package/dist/core/constraints/capabilities-parser.js +2 -1
  35. package/dist/core/constraints/checker.d.ts +17 -5
  36. package/dist/core/constraints/checker.js +20 -10
  37. package/dist/core/constraints/checkers/no-hardcoded-credentials.js +14 -15
  38. package/dist/core/constraints/checkers/no-test-simplification.js +8 -6
  39. package/dist/core/constraints/checkers/templated/exec.d.ts +4 -0
  40. package/dist/core/constraints/checkers/templated/exec.js +11 -48
  41. package/dist/core/constraints/checkers/templated/file-exists.js +7 -4
  42. package/dist/core/constraints/checkers/templated/regex-scan.js +11 -15
  43. package/dist/core/constraints/checkers/types.d.ts +13 -2
  44. package/dist/core/constraints/checkers/types.js +5 -2
  45. package/dist/core/constraints/construction-sites.d.ts +1 -1
  46. package/dist/core/constraints/construction-sites.js +1 -1
  47. package/dist/core/constraints/injection-writer.d.ts +1 -1
  48. package/dist/core/constraints/injection-writer.js +1 -1
  49. package/dist/core/constraints/project-snapshot.d.ts +43 -0
  50. package/dist/core/constraints/project-snapshot.js +103 -0
  51. package/dist/core/constraints/run-env.d.ts +35 -0
  52. package/dist/core/constraints/run-env.js +36 -0
  53. package/dist/{cli/commands/sync-docs → core/docs-sync}/agents-syncer.d.ts +2 -0
  54. package/dist/{cli/commands/sync-docs → core/docs-sync}/agents-syncer.js +5 -3
  55. package/dist/core/docs-sync/capabilities-table.d.ts +46 -0
  56. package/dist/core/docs-sync/capabilities-table.js +200 -0
  57. package/dist/core/docs-sync/context-collector.d.ts +40 -0
  58. package/dist/core/docs-sync/context-collector.js +227 -0
  59. package/dist/{cli/commands/sync-docs → core/docs-sync}/project-reader.d.ts +5 -1
  60. package/dist/{cli/commands/sync-docs → core/docs-sync}/project-reader.js +10 -6
  61. package/dist/{cli/commands/sync-docs → core/docs-sync}/sync-state.d.ts +15 -9
  62. package/dist/{cli/commands/sync-docs → core/docs-sync}/sync-state.js +26 -29
  63. package/dist/core/validators/check-handlers/command.js +22 -0
  64. package/dist/core/validators/check-handlers/output.js +26 -3
  65. package/dist/core/validators/checkpoint.d.ts +4 -0
  66. package/dist/core/validators/checkpoint.js +10 -2
  67. package/dist/gates/acceptance.d.ts +8 -3
  68. package/dist/gates/acceptance.js +76 -13
  69. package/dist/gates/command.d.ts +3 -3
  70. package/dist/gates/command.js +2 -2
  71. package/dist/gates/contract.d.ts +3 -3
  72. package/dist/gates/performance.d.ts +10 -3
  73. package/dist/gates/performance.js +49 -4
  74. package/dist/gates/review.d.ts +3 -3
  75. package/dist/gates/security.d.ts +3 -3
  76. package/dist/gates/security.js +26 -18
  77. package/dist/gates/types.d.ts +45 -16
  78. package/dist/knowledge/ingest.d.ts +31 -0
  79. package/dist/knowledge/ingest.js +192 -52
  80. package/dist/knowledge/lifecycle.d.ts +15 -0
  81. package/dist/knowledge/lifecycle.js +62 -0
  82. package/dist/knowledge/query.d.ts +10 -0
  83. package/dist/knowledge/query.js +60 -8
  84. package/dist/knowledge/store.d.ts +56 -3
  85. package/dist/knowledge/store.js +108 -30
  86. package/dist/types/checkpoint.d.ts +28 -1
  87. package/dist/types/command-execution-policy.d.ts +73 -0
  88. package/dist/types/command-execution-policy.js +16 -0
  89. package/dist/utils/exec.d.ts +7 -1
  90. package/dist/utils/exec.js +7 -1
  91. package/package.json +1 -1
  92. package/dist/core/constraints/check-cache.d.ts +0 -43
  93. package/dist/core/constraints/check-cache.js +0 -77
  94. package/dist/core/constraints/checkers/templated/glob.d.ts +0 -8
  95. package/dist/core/constraints/checkers/templated/glob.js +0 -14
  96. /package/dist/{cli/commands/sync-docs → core/docs-sync}/preserve-block.d.ts +0 -0
  97. /package/dist/{cli/commands/sync-docs → core/docs-sync}/preserve-block.js +0 -0
@@ -11,63 +11,21 @@
11
11
  * - 超时:到点杀整个进程组(含 shell 的子孙进程)并判违规
12
12
  * - 截断:stdout/stderr 各至多留 MAX_STREAM_CHARS,超出只记「已截断」标记
13
13
  *
14
+ * 命令执行策略(D3 P0.5 Phase 1):env.commandPolicy.readOnly = true 时不执行
15
+ * 命令,返回三态 CheckSkip(原因由策略统一措辞);普通模式经策略执行缝执行
16
+ * (缺省真实 spawn 执行,语义如上;无策略 = 默认执行,库调用兼容)。
17
+ *
14
18
  * 参数:
15
19
  * - command(必填):命令行(经 shell 执行,cwd = 项目根)
16
20
  * - timeoutMs(可选):超时毫秒数,缺省 DEFAULT_TIMEOUT_MS,上限 MAX_TIMEOUT_MS
17
21
  */
18
22
  Object.defineProperty(exports, "__esModule", { value: true });
19
23
  exports.execScript = void 0;
20
- const child_process_1 = require("child_process");
21
24
  const types_1 = require("../types");
25
+ const command_execution_policy_1 = require("../../../command-execution-policy");
22
26
  const clip_1 = require("../../../../utils/clip");
23
27
  const DEFAULT_TIMEOUT_MS = 30_000;
24
28
  const MAX_TIMEOUT_MS = 600_000;
25
- /** stdout/stderr 各自保留的字符上限(超出只记截断标记,不拖垮证据面与 trace) */
26
- const MAX_STREAM_CHARS = 8_192;
27
- function runCommand(command, cwd, timeoutMs) {
28
- return new Promise((resolve) => {
29
- // detached 成组:超时整组 SIGKILL,shell 的子孙进程一并终止,不留孤儿拖住机器
30
- const child = (0, child_process_1.spawn)(command, { cwd, shell: true, detached: true });
31
- let stdout = '';
32
- let stderr = '';
33
- let truncated = false;
34
- let timedOut = false;
35
- let settled = false;
36
- const collect = (chunk, current) => {
37
- if (current.length >= MAX_STREAM_CHARS) {
38
- truncated = true;
39
- return current;
40
- }
41
- const next = current + chunk.toString('utf-8');
42
- if (next.length > MAX_STREAM_CHARS)
43
- truncated = true;
44
- return next.slice(0, MAX_STREAM_CHARS);
45
- };
46
- const finish = (partial) => {
47
- if (settled)
48
- return;
49
- settled = true;
50
- clearTimeout(timer);
51
- resolve({ ...partial, timedOut, stdout, stderr, truncated });
52
- };
53
- const timer = setTimeout(() => {
54
- timedOut = true;
55
- try {
56
- if (child.pid !== undefined)
57
- process.kill(-child.pid, 'SIGKILL');
58
- }
59
- catch {
60
- // 进程组 kill 失败(竞态:子进程已退,ESRCH)→ 退化单进程 kill,best-effort 收尾
61
- child.kill('SIGKILL');
62
- }
63
- }, timeoutMs);
64
- timer.unref();
65
- child.stdout.on('data', (chunk) => { stdout = collect(chunk, stdout); });
66
- child.stderr.on('data', (chunk) => { stderr = collect(chunk, stderr); });
67
- child.on('error', (err) => finish({ code: null, signal: null, spawnError: err.message }));
68
- child.on('close', (code, signal) => finish({ code, signal }));
69
- });
70
- }
71
29
  /** 失败原因一行话(自描述,进证据首行) */
72
30
  function failureReason(command, result, timeoutMs) {
73
31
  if (result.spawnError)
@@ -98,7 +56,12 @@ exports.execScript = {
98
56
  return {
99
57
  id,
100
58
  async evaluate(env) {
101
- const result = await runCommand(command, env.projectPath, timeoutMs);
59
+ // read-only:不执行项目声明命令,三态 skipped(不计 pass/fail,不假绿)
60
+ const policy = env.commandPolicy;
61
+ if (policy?.readOnly) {
62
+ return { skip: true, reason: policy.skipReason(command) };
63
+ }
64
+ const result = await (policy ? policy.execute : command_execution_policy_1.runShellCommand)(command, env.projectPath, timeoutMs);
102
65
  if (!result.timedOut && !result.spawnError && result.code === 0)
103
66
  return true;
104
67
  const lines = [];
@@ -19,7 +19,8 @@ exports.fileExists = void 0;
19
19
  const fs_1 = require("fs");
20
20
  const path_1 = require("path");
21
21
  const types_1 = require("../types");
22
- const file_walk_1 = require("../../../../utils/file-walk");
22
+ const glob_match_1 = require("../../../../utils/glob-match");
23
+ const run_env_1 = require("../../run-env");
23
24
  exports.fileExists = {
24
25
  validateParams(params) {
25
26
  const errors = [];
@@ -48,13 +49,15 @@ exports.fileExists = {
48
49
  const rel = params.path;
49
50
  const globPattern = params.glob;
50
51
  const mustExist = params.mustExist !== false;
52
+ // glob 在创建时编译一次;匹配在 run 级项目快照的清单上做(同 run 多约束共享一次遍历)
53
+ const globRe = globPattern !== undefined ? (0, glob_match_1.globToRegExp)(globPattern) : undefined;
51
54
  return {
52
55
  id,
53
56
  evaluate(env) {
54
57
  const pathHit = rel !== undefined && (0, fs_1.existsSync)((0, path_1.join)(env.projectPath, rel));
55
- // 目录与文件都算命中;node_modules 全层级跳过防扫穿
56
- const hits = globPattern !== undefined
57
- ? (0, file_walk_1.walkMatches)(env.projectPath, globPattern, { includeDirs: true, skipDirs: ['node_modules'] })
58
+ // 目录与文件都算命中(快照清单含目录);.git/node_modules 由快照排除
59
+ const hits = globRe !== undefined
60
+ ? (0, run_env_1.projectSnapshotFor)(env).files().filter(f => globRe.test(f))
58
61
  : [];
59
62
  if (mustExist) {
60
63
  const missing = [];
@@ -13,16 +13,14 @@
13
13
  */
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.regexScan = void 0;
16
- const fs_1 = require("fs");
17
- const path_1 = require("path");
18
16
  const types_1 = require("../types");
19
- const glob_1 = require("./glob");
20
- const diff_parse_1 = require("../../../../utils/diff-parse");
17
+ const glob_match_1 = require("../../../../utils/glob-match");
21
18
  const clip_1 = require("../../../../utils/clip");
22
19
  const error_1 = require("../../../../utils/error");
20
+ const run_env_1 = require("../../run-env");
23
21
  /** 从 unified diff 中取新增行(文件归属见 utils/diff-parse 正本,glob 过滤文件集) */
24
- function collectAddedLines(diff, inScope) {
25
- return (0, diff_parse_1.parseUnifiedDiff)(diff)
22
+ function collectAddedLines(lines, inScope) {
23
+ return lines
26
24
  .filter(l => l.kind === 'added' && l.file !== '' && inScope(l.file))
27
25
  .map(l => ({ file: l.file, line: l.text }));
28
26
  }
@@ -57,19 +55,17 @@ exports.regexScan = {
57
55
  },
58
56
  create(id, params) {
59
57
  const pattern = new RegExp(params.pattern);
60
- const glob = params.glob;
58
+ // glob 在创建时编译一次,不在扫描期间逐文件重复编译
59
+ const globRe = typeof params.glob === 'string' ? (0, glob_match_1.globToRegExp)(params.glob) : undefined;
61
60
  const scope = params.scope ?? 'diff';
62
- const inScope = (file) => !glob || (0, glob_1.matchGlob)(file, glob);
61
+ const inScope = (file) => !globRe || globRe.test(file);
63
62
  const scanChangedFiles = (env) => {
64
63
  const hits = [];
65
64
  for (const file of (env.context.changedFiles ?? []).filter(inScope)) {
66
- let content;
67
- try {
68
- content = (0, fs_1.readFileSync)((0, path_1.join)(env.projectPath, file), 'utf-8');
69
- }
70
- catch {
65
+ // 经 run 级快照读内容:同 run 内多条约束共享同一文件读取
66
+ const content = (0, run_env_1.projectSnapshotFor)(env).content(file);
67
+ if (content === undefined)
71
68
  continue; // 文件不可读(已删除/二进制)→ 跳过
72
- }
73
69
  for (const line of content.split('\n')) {
74
70
  if (pattern.test(line))
75
71
  hits.push(`${file}: ${(0, clip_1.clip)(line)}`);
@@ -88,7 +84,7 @@ exports.regexScan = {
88
84
  let scanned = false;
89
85
  try {
90
86
  const diff = await env.stagedDiff();
91
- const added = collectAddedLines(diff, inScope);
87
+ const added = collectAddedLines((0, run_env_1.projectSnapshotFor)(env).parseDiff(diff), inScope);
92
88
  if (added.length > 0) {
93
89
  scanned = true;
94
90
  const hits = added.filter(a => pattern.test(a.line)).map(a => `${a.file}: ${(0, clip_1.clip)(a.line)}`);
@@ -5,6 +5,7 @@
5
5
  * 通过 ConstraintCheck 接口 + CheckEnv 环境与编排层解耦。
6
6
  */
7
7
  import type { ConstraintContext } from '../../../types/constraint';
8
+ import type { CommandExecutionPolicy } from '../../../types/command-execution-policy';
8
9
  import { type RunEnv } from '../run-env';
9
10
  /**
10
11
  * 检查环境:一次 run 内共享的观察面与证据提供者(ADR-0023 决策 1)
@@ -13,7 +14,7 @@ import { type RunEnv } from '../run-env';
13
14
  * + `context` + git/扫描证据。前者不含 context,故只能派生不能合并成一个对象。
14
15
  *
15
16
  * - stagedDiff/stagedDiffNames:run 内 git 命令至多一次(工单 18 / #87)
16
- * - srcScan:run 内同根源码扫描至多一次(CheckCache)
17
+ * - srcScan:run 内同根源码扫描至多一次(缓存挂在 RunEnv 上,P1.1)
17
18
  */
18
19
  export interface CheckEnv extends RunEnv {
19
20
  /** 约束上下文(operation/changedFiles/各类证据标志) */
@@ -31,6 +32,14 @@ export interface CheckEnv extends RunEnv {
31
32
  * 对空证据做出「假合规」判定。必传:buildCheckEnv 恒填充,手写 env 一律显式给。
32
33
  */
33
34
  evidenceAvailable(input: CheckEvidenceInput): boolean;
35
+ /**
36
+ * 命令执行策略(D3 P0.5 Phase 1;可选成员,缺省 = 默认执行)
37
+ *
38
+ * 由编排层(ConstraintChecker 构造注入)挂上:readOnly = true 时 exec 模板
39
+ * checker 不执行 params.command,返回三态 skipped(skipReason 策略统一措辞)。
40
+ * 手写 env 不携带 = 默认执行(库调用兼容)。
41
+ */
42
+ commandPolicy?: CommandExecutionPolicy;
34
43
  }
35
44
  /**
36
45
  * 证据提供者组:git diff / 源码扫描的 memoized I/O
@@ -58,10 +67,12 @@ export type CheckEvidenceInput = 'stagedDiff' | 'stagedDiffNames';
58
67
  * checker 由编排层按 needs 契约降级 skip
59
68
  * - runEnv = 本 run 的运行级观察面(ADR-0023):传入即与 context-builder 及
60
69
  * 其余 checker 共用同一份上行数据读取;不传则自造一枚,只服务本次调用
70
+ * - commandPolicy = 本 run 的命令执行策略(D3):传入即挂上 env,exec 模板
71
+ * checker 在 read-only 下三态跳过;不传 = 默认执行
61
72
  *
62
73
  * 下游消费方若需 git 证据,传真实 providers 即可。
63
74
  */
64
- export declare function buildCheckEnv(context: ConstraintContext, evidence: EvidenceProviders | 'none', runEnv?: RunEnv): CheckEnv;
75
+ export declare function buildCheckEnv(context: ConstraintContext, evidence: EvidenceProviders | 'none', runEnv?: RunEnv, commandPolicy?: CommandExecutionPolicy): CheckEnv;
65
76
  /**
66
77
  * 带原因的跳过(harness#182)
67
78
  *
@@ -21,10 +21,12 @@ const run_env_1 = require("../run-env");
21
21
  * checker 由编排层按 needs 契约降级 skip
22
22
  * - runEnv = 本 run 的运行级观察面(ADR-0023):传入即与 context-builder 及
23
23
  * 其余 checker 共用同一份上行数据读取;不传则自造一枚,只服务本次调用
24
+ * - commandPolicy = 本 run 的命令执行策略(D3):传入即挂上 env,exec 模板
25
+ * checker 在 read-only 下三态跳过;不传 = 默认执行
24
26
  *
25
27
  * 下游消费方若需 git 证据,传真实 providers 即可。
26
28
  */
27
- function buildCheckEnv(context, evidence, runEnv) {
29
+ function buildCheckEnv(context, evidence, runEnv, commandPolicy) {
28
30
  const projectPath = context.projectPath || process.cwd();
29
31
  const run = runEnv ?? (0, run_env_1.createRunEnv)(projectPath);
30
32
  if (evidence === 'none') {
@@ -35,10 +37,11 @@ function buildCheckEnv(context, evidence, runEnv) {
35
37
  stagedDiffNames: async () => '',
36
38
  srcScan: () => [],
37
39
  evidenceAvailable: () => false,
40
+ commandPolicy,
38
41
  };
39
42
  }
40
43
  const { available, ...providers } = evidence;
41
- return { ...run, context, ...providers, evidenceAvailable: available };
44
+ return { ...run, context, ...providers, evidenceAvailable: available, commandPolicy };
42
45
  }
43
46
  /** 证据条数上限:trace 是 JSONL,缺口上百项时不能整仓落盘 */
44
47
  exports.MAX_EVIDENCE_ITEMS = 10;
@@ -7,7 +7,7 @@
7
7
  * 声明「类 X 的仓内直构造点恰为 N 处」;无标记的散文维持现状不判。
8
8
  *
9
9
  * 纯判定 module:标记解析、`new X(` 计数口径、期望/实际对撞全在此,
10
- * 不吃 fs——文件遍历与正文读取在调用方(cli 侧 sync-docs/context-syncer),
10
+ * 不吃 fs——文件遍历与正文读取在调用方(P2.8 起 core/docs-sync/context-collector.ts),
11
11
  * 与 context-reconcile 同一套「判定不吃 fs、清单由调用方组装」形状。
12
12
  *
13
13
  * 计数口径:
@@ -8,7 +8,7 @@
8
8
  * 声明「类 X 的仓内直构造点恰为 N 处」;无标记的散文维持现状不判。
9
9
  *
10
10
  * 纯判定 module:标记解析、`new X(` 计数口径、期望/实际对撞全在此,
11
- * 不吃 fs——文件遍历与正文读取在调用方(cli 侧 sync-docs/context-syncer),
11
+ * 不吃 fs——文件遍历与正文读取在调用方(P2.8 起 core/docs-sync/context-collector.ts),
12
12
  * 与 context-reconcile 同一套「判定不吃 fs、清单由调用方组装」形状。
13
13
  *
14
14
  * 计数口径:
@@ -64,7 +64,7 @@ export declare function hasGovernanceContract(content: string): boolean;
64
64
  export declare function countGovernanceHeadings(content: string): number;
65
65
  /**
66
66
  * PRESERVE 标记块存在性判定:块存在且块体非空(governance_presence 在场
67
- * 守护消费,#83 收编其手写切片)。标记契约与 sync-docs/preserve-block 一致:
67
+ * 守护消费,#83 收编其手写切片)。标记契约与 core/docs-sync/preserve-block 一致:
68
68
  * 首尾标记均须独占一行——开始标记行首锚定(行尾仅空白),结束标记行 trim
69
69
  * 后等于标记文本。结束标记嵌在行中 / 缺失 / 乱序均为畸形块 → false,在场
70
70
  * 守护按契约丢失报警(原 presence「行中结束标记也算在场」显式收紧,#83)。
@@ -146,7 +146,7 @@ function countGovernanceHeadings(content) {
146
146
  }
147
147
  /**
148
148
  * PRESERVE 标记块存在性判定:块存在且块体非空(governance_presence 在场
149
- * 守护消费,#83 收编其手写切片)。标记契约与 sync-docs/preserve-block 一致:
149
+ * 守护消费,#83 收编其手写切片)。标记契约与 core/docs-sync/preserve-block 一致:
150
150
  * 首尾标记均须独占一行——开始标记行首锚定(行尾仅空白),结束标记行 trim
151
151
  * 后等于标记文本。结束标记嵌在行中 / 缺失 / 乱序均为畸形块 → false,在场
152
152
  * 守护按契约丢失报警(原 presence「行中结束标记也算在场」显式收紧,#83)。
@@ -0,0 +1,43 @@
1
+ /**
2
+ * 运行级项目快照(P1.2)
3
+ *
4
+ * 一次 `harness check` 一份、跑完即弃(ADR-0023 同形:入口构造、沿调用链显式传递、
5
+ * 不做进程级全局)。把 run 内反复发生的项目读取收拢到一枚惰性快照上:
6
+ *
7
+ * - **文件清单**:`files()` 一次遍历收齐项目内文件与目录(相对项目根、正斜杠口径),
8
+ * 同 run 内多条约束(file-exists 等)共享,不再每约束各走一遍。
9
+ * - **目录排除口径**:精确跳过 `.git`(仓库元数据,任何层级都不下钻)与
10
+ * `node_modules`(依赖)。**不设 skipHidden**——`.harness` 等受管隐藏目录是
11
+ * file-exists 的合法判定对象,必须可匹配。dist/coverage 等构建产物**不排除**:
12
+ * 它们同样是约束的合法目标(如「禁止 dist 入库」),排除与否的分层语义是——
13
+ * 源码清单(`RunEnv.srcScan` / findTsSourceFiles 的 DEFAULT_SKIP_DIRS)排除
14
+ * dist/__tests__,项目快照不排除;两边各司其职,不互相借用名单。
15
+ * - **文件内容**:`content(rel)` 同 run 内同一文件至多读一次;缺失/不可读 → undefined
16
+ * (消费方按「跳过该文件」兜底,与旧 try/catch 口径一致)。
17
+ * - **diff 解析**:`parseDiff(diff)` 同一份 diff 至多彩一次(staged diff 全文经
18
+ * GitEvidence memo 本就一份,解析此前每条解析约束各跑一遍)。
19
+ *
20
+ * 清单与内容都是「本 run 的一份视图」:run 中途落盘的文件不进本枚快照,
21
+ * 跨 run 隔离由「一次 run 一枚」保证(同 RunEnv 其余观察口)。
22
+ */
23
+ import { type ParsedDiffLine } from '../../utils/diff-parse';
24
+ export interface ProjectSnapshot {
25
+ /**
26
+ * 项目内文件与目录的相对路径清单(正斜杠,含目录本身;只读,勿改返回的数组)
27
+ *
28
+ * 首次调用时遍历一次,之后本 run 内返回同一份缓存。
29
+ */
30
+ files(): string[];
31
+ /**
32
+ * 读文件内容(相对项目根);缺失/不可读 → undefined
33
+ *
34
+ * 同 run 内同一文件至多读一次;结果含「读不出」结论本身(memo 不区分成败)。
35
+ */
36
+ content(rel: string): string | undefined;
37
+ /**
38
+ * 解析 unified diff;同一份 diff 文本至多彩一次(返回共享数组,只读勿改)
39
+ */
40
+ parseDiff(diff: string): ParsedDiffLine[];
41
+ }
42
+ /** 构造运行级项目快照(由 createRunEnv 懒建;也可单独用于一次性读取) */
43
+ export declare function createProjectSnapshot(projectPath: string): ProjectSnapshot;
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ /**
3
+ * 运行级项目快照(P1.2)
4
+ *
5
+ * 一次 `harness check` 一份、跑完即弃(ADR-0023 同形:入口构造、沿调用链显式传递、
6
+ * 不做进程级全局)。把 run 内反复发生的项目读取收拢到一枚惰性快照上:
7
+ *
8
+ * - **文件清单**:`files()` 一次遍历收齐项目内文件与目录(相对项目根、正斜杠口径),
9
+ * 同 run 内多条约束(file-exists 等)共享,不再每约束各走一遍。
10
+ * - **目录排除口径**:精确跳过 `.git`(仓库元数据,任何层级都不下钻)与
11
+ * `node_modules`(依赖)。**不设 skipHidden**——`.harness` 等受管隐藏目录是
12
+ * file-exists 的合法判定对象,必须可匹配。dist/coverage 等构建产物**不排除**:
13
+ * 它们同样是约束的合法目标(如「禁止 dist 入库」),排除与否的分层语义是——
14
+ * 源码清单(`RunEnv.srcScan` / findTsSourceFiles 的 DEFAULT_SKIP_DIRS)排除
15
+ * dist/__tests__,项目快照不排除;两边各司其职,不互相借用名单。
16
+ * - **文件内容**:`content(rel)` 同 run 内同一文件至多读一次;缺失/不可读 → undefined
17
+ * (消费方按「跳过该文件」兜底,与旧 try/catch 口径一致)。
18
+ * - **diff 解析**:`parseDiff(diff)` 同一份 diff 至多彩一次(staged diff 全文经
19
+ * GitEvidence memo 本就一份,解析此前每条解析约束各跑一遍)。
20
+ *
21
+ * 清单与内容都是「本 run 的一份视图」:run 中途落盘的文件不进本枚快照,
22
+ * 跨 run 隔离由「一次 run 一枚」保证(同 RunEnv 其余观察口)。
23
+ */
24
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
25
+ if (k2 === undefined) k2 = k;
26
+ var desc = Object.getOwnPropertyDescriptor(m, k);
27
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
28
+ desc = { enumerable: true, get: function() { return m[k]; } };
29
+ }
30
+ Object.defineProperty(o, k2, desc);
31
+ }) : (function(o, m, k, k2) {
32
+ if (k2 === undefined) k2 = k;
33
+ o[k2] = m[k];
34
+ }));
35
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
36
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
37
+ }) : function(o, v) {
38
+ o["default"] = v;
39
+ });
40
+ var __importStar = (this && this.__importStar) || (function () {
41
+ var ownKeys = function(o) {
42
+ ownKeys = Object.getOwnPropertyNames || function (o) {
43
+ var ar = [];
44
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
45
+ return ar;
46
+ };
47
+ return ownKeys(o);
48
+ };
49
+ return function (mod) {
50
+ if (mod && mod.__esModule) return mod;
51
+ var result = {};
52
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
53
+ __setModuleDefault(result, mod);
54
+ return result;
55
+ };
56
+ })();
57
+ Object.defineProperty(exports, "__esModule", { value: true });
58
+ exports.createProjectSnapshot = createProjectSnapshot;
59
+ const fs = __importStar(require("fs"));
60
+ const path = __importStar(require("path"));
61
+ const file_walk_1 = require("../../utils/file-walk");
62
+ const diff_parse_1 = require("../../utils/diff-parse");
63
+ /** 项目快照遍历的排除名单:仓库元数据 + 依赖。dist/coverage 刻意不在内(见文件头) */
64
+ const SNAPSHOT_SKIP_DIRS = ['.git', 'node_modules'];
65
+ /** 构造运行级项目快照(由 createRunEnv 懒建;也可单独用于一次性读取) */
66
+ function createProjectSnapshot(projectPath) {
67
+ /** 文件清单 memo:undefined = 未遍历 */
68
+ let listing;
69
+ /** 文件内容 memo:rel → 内容(undefined = 读不出) */
70
+ const contents = new Map();
71
+ /** diff 解析 memo:单份缓存——一个 run 只有一份 staged diff */
72
+ let parsed = null;
73
+ return {
74
+ files() {
75
+ if (!listing) {
76
+ listing = (0, file_walk_1.walkFiles)(projectPath, {
77
+ skipDirs: SNAPSHOT_SKIP_DIRS,
78
+ includeDirs: true,
79
+ }).map(p => path.relative(projectPath, p).split(path.sep).join('/'));
80
+ }
81
+ return listing;
82
+ },
83
+ content(rel) {
84
+ if (!contents.has(rel)) {
85
+ let text;
86
+ try {
87
+ text = fs.readFileSync(path.join(projectPath, rel), 'utf-8');
88
+ }
89
+ catch {
90
+ text = undefined; // 已删除/二进制/权限 → 跳过该文件
91
+ }
92
+ contents.set(rel, text);
93
+ }
94
+ return contents.get(rel);
95
+ },
96
+ parseDiff(diff) {
97
+ if (!parsed || parsed.source !== diff) {
98
+ parsed = { source: diff, lines: (0, diff_parse_1.parseUnifiedDiff)(diff) };
99
+ }
100
+ return parsed.lines;
101
+ },
102
+ };
103
+ }
@@ -13,6 +13,7 @@
13
13
  * 测试在同进程内反复改文件,常驻缓存会把「先改后读」读成老内容。
14
14
  */
15
15
  import { type CapabilityVerdict } from './capabilities-reconcile';
16
+ import { type ProjectSnapshot } from './project-snapshot';
16
17
  /** 能力表在项目根下的落点(checker 侧存在性探测与本读面共用此常量) */
17
18
  export declare const CAPABILITIES_FILE_REL = "CAPABILITIES.md";
18
19
  export interface RunEnv {
@@ -39,6 +40,27 @@ export interface RunEnv {
39
40
  * 出现第三个消费方且清单不同,就必须改成按输入分列的缓存而不是静默复用。
40
41
  */
41
42
  capabilities(populationFiles: string[]): ProjectCapabilities | undefined;
43
+ /**
44
+ * 源码根(相对路径)下的源文件清单(一次运行内同一根源码根至多扫描一次)
45
+ *
46
+ * 缓存挂在本观察面上(P1.1):一次 run 一份、跑完即弃,跨 run 天然隔离——
47
+ * 不再用 checker 实例上的 TTL 缓存 + 手工 invalidate 掩盖 run 边界。
48
+ */
49
+ srcScan(root: string): string[];
50
+ /**
51
+ * 运行级项目快照(P1.2):文件清单 / 文件内容 / diff 解析的 run 内共享读取
52
+ *
53
+ * 懒建 + run 内 memo(createRunEnv 生产路径恒提供,多条约束靠它共享一次遍历)。
54
+ * 与 srcScan 分工:srcScan 给「源码根下的 TS 源文件清单」(排除 dist/__tests__),
55
+ * snapshot 给「项目级文件/目录清单 + 内容 + diff 解析」(精确排除 .git/node_modules,
56
+ * 不排除 dist/coverage——构建产物是存在性约束的合法判定对象)。
57
+ *
58
+ * **optional 是兼容边界**:`CheckEnv` 经包根公开导出,外部旧式手写实现
59
+ * 不含本成员也必须能编译——checker 一律经 `projectSnapshotFor()` 取快照
60
+ * (提供 = 共享 env 的 memo;未提供 = 一次性 fallback,保持旧读取行为),
61
+ * 不得把生产路径的快照切成每约束一份新缓存,也不要求外部实现方立刻补成员。
62
+ */
63
+ snapshot?(): ProjectSnapshot;
42
64
  }
43
65
  /**
44
66
  * 一次运行内的能力表面貌:原文 + 源码根 + 代码实况清单 + 对照判定
@@ -69,3 +91,16 @@ export declare function createRunEnv(projectPath: string): RunEnv;
69
91
  export type RunTarget = RunEnv | string;
70
92
  /** 归一 RunTarget(非 RunEnv 即按项目根自造一枚一次性观察面) */
71
93
  export declare function resolveRunEnv(target?: RunTarget): RunEnv;
94
+ /**
95
+ * 经 RunEnv 取项目快照——内置 checker 的唯一直达口(P1.2)
96
+ *
97
+ * - env 提供 `snapshot()`(生产路径,createRunEnv 恒提供):共享其 run 级 memo——
98
+ * 多条约束一次遍历 / 同一文件读取 / 同一 diff 解析靠这个共享成立;
99
+ * - 未提供(外部旧式手写实现,兼容边界):构造一枚**一次性**快照,
100
+ * 「每次调用读当下内容」,与 RunTarget 传路径形状同口径——
101
+ * 不为兼容路径引入跨调用缓存(env 复用跨 run 的旧式实现不会被悄悄钉住)。
102
+ *
103
+ * checker 不得绕过本 helper 各自 `createProjectSnapshot`——那会把生产路径的
104
+ * 共享快照切成每约束一份新缓存,「同一文件至多读一次」随之失效。
105
+ */
106
+ export declare function projectSnapshotFor(run: RunEnv): ProjectSnapshot;
@@ -50,11 +50,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
50
50
  exports.CAPABILITIES_FILE_REL = void 0;
51
51
  exports.createRunEnv = createRunEnv;
52
52
  exports.resolveRunEnv = resolveRunEnv;
53
+ exports.projectSnapshotFor = projectSnapshotFor;
53
54
  const fs = __importStar(require("fs"));
54
55
  const path = __importStar(require("path"));
55
56
  const yaml = __importStar(require("../../utils/yaml"));
56
57
  const detect_source_roots_1 = require("../../utils/detect-source-roots");
58
+ const file_walk_1 = require("../../utils/file-walk");
57
59
  const capabilities_reconcile_1 = require("./capabilities-reconcile");
60
+ const project_snapshot_1 = require("./project-snapshot");
58
61
  /** config.yml 在项目根下的相对路径(读取口径唯一落点) */
59
62
  const CONFIG_FILE_REL = path.join('.harness', 'config.yml');
60
63
  /** 能力表在项目根下的落点(checker 侧存在性探测与本读面共用此常量) */
@@ -70,6 +73,10 @@ function createRunEnv(projectPath) {
70
73
  let configRaw;
71
74
  /** 能力表 memo:undefined = 未算,null = 项目无 CAPABILITIES.md */
72
75
  let caps;
76
+ /** 源码根扫描 memo:root → 相对路径清单(同 run 复用,跑完即弃) */
77
+ const scans = new Map();
78
+ /** 项目快照 memo:undefined = 未建(懒建:无消费方不遍历) */
79
+ let snap;
73
80
  return {
74
81
  projectPath,
75
82
  sourceRoots: roots,
@@ -99,6 +106,20 @@ function createRunEnv(projectPath) {
99
106
  }
100
107
  return caps ?? undefined;
101
108
  },
109
+ srcScan(root) {
110
+ let cached = scans.get(root);
111
+ if (!cached) {
112
+ cached = (0, file_walk_1.findTsSourceFiles)(path.join(projectPath, root), { skipIndex: true }).map((f) => path.relative(projectPath, f));
113
+ scans.set(root, cached);
114
+ }
115
+ return cached;
116
+ },
117
+ snapshot() {
118
+ if (!snap) {
119
+ snap = (0, project_snapshot_1.createProjectSnapshot)(projectPath);
120
+ }
121
+ return snap;
122
+ },
102
123
  };
103
124
  /** 源根探测 memo(sourceRoots() 与能力表共用同一份) */
104
125
  function roots() {
@@ -131,3 +152,18 @@ function resolveRunEnv(target) {
131
152
  return target;
132
153
  return createRunEnv(target || process.cwd());
133
154
  }
155
+ /**
156
+ * 经 RunEnv 取项目快照——内置 checker 的唯一直达口(P1.2)
157
+ *
158
+ * - env 提供 `snapshot()`(生产路径,createRunEnv 恒提供):共享其 run 级 memo——
159
+ * 多条约束一次遍历 / 同一文件读取 / 同一 diff 解析靠这个共享成立;
160
+ * - 未提供(外部旧式手写实现,兼容边界):构造一枚**一次性**快照,
161
+ * 「每次调用读当下内容」,与 RunTarget 传路径形状同口径——
162
+ * 不为兼容路径引入跨调用缓存(env 复用跨 run 的旧式实现不会被悄悄钉住)。
163
+ *
164
+ * checker 不得绕过本 helper 各自 `createProjectSnapshot`——那会把生产路径的
165
+ * 共享快照切成每约束一份新缓存,「同一文件至多读一次」随之失效。
166
+ */
167
+ function projectSnapshotFor(run) {
168
+ return run.snapshot?.() ?? (0, project_snapshot_1.createProjectSnapshot)(run.projectPath);
169
+ }
@@ -1,5 +1,7 @@
1
1
  /**
2
2
  * AGENTS.md 同步器(工单 22):agent 导读生成(项目简介/目录结构/常用命令/约束治理/知识入口)
3
+ *
4
+ * P2.8 自 cli/commands/sync-docs/ 下沉 core/docs-sync/(采集与判定收口),纯移位。
3
5
  */
4
6
  export interface GovernanceInfo {
5
7
  hasConfig: boolean;
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  /**
3
3
  * AGENTS.md 同步器(工单 22):agent 导读生成(项目简介/目录结构/常用命令/约束治理/知识入口)
4
+ *
5
+ * P2.8 自 cli/commands/sync-docs/ 下沉 core/docs-sync/(采集与判定收口),纯移位。
4
6
  */
5
7
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
6
8
  if (k2 === undefined) k2 = k;
@@ -40,9 +42,9 @@ exports.buildAgentsMd = buildAgentsMd;
40
42
  const fs = __importStar(require("fs/promises"));
41
43
  const fs_1 = require("fs");
42
44
  const path = __importStar(require("path"));
43
- const project_config_loader_1 = require("../../../core/project-config-loader");
44
- const injection_writer_1 = require("../../../core/constraints/injection-writer");
45
- const file_walk_1 = require("../../../utils/file-walk");
45
+ const project_config_loader_1 = require("../project-config-loader");
46
+ const injection_writer_1 = require("../constraints/injection-writer");
47
+ const file_walk_1 = require("../../utils/file-walk");
46
48
  const project_reader_1 = require("./project-reader");
47
49
  /** 生成 AGENTS.md 目录结构表时跳过的目录(依赖/构建产物/报告等) */
48
50
  const AGENTS_MD_SKIP_DIRS = new Set([
@@ -0,0 +1,46 @@
1
+ /**
2
+ * CAPABILITIES.md file-table 文本正本(工单 22 / harness#171;P2.8 自
3
+ * cli/commands/sync-docs/capabilities-syncer.ts 下沉 core/docs-sync/,纯移位):
4
+ * 登记条目解析(parseCapabilitiesFiles)、表格排版判定(normalizeCapabilitiesTableLayout,
5
+ * check 与 fix 共用正本)、--compact 目录条目折叠(compactCapabilitiesContent)。
6
+ * 写侧 updateCapabilitiesFile 留在 cli/commands/sync-docs/capabilities-syncer.ts。
7
+ * 能力清单格式(计数行)的解析与计数在 core/constraints/capabilities-parser。
8
+ */
9
+ /**
10
+ * 解析 CAPABILITIES.md 中的文件路径
11
+ *
12
+ * 提取所有表格行中提到的 .ts/.tsx/.js/.jsx 文件名(不含路径前缀)
13
+ * 用于模糊匹配:只要文档中提到了该文件名就算已记录
14
+ */
15
+ export declare function parseCapabilitiesFiles(capabilitiesPath: string): Promise<string[]>;
16
+ export interface TableLayoutNormalization {
17
+ /** 收拢后的内容 */
18
+ content: string;
19
+ /** 被删掉的「表格内空行」行数 */
20
+ blankLines: number;
21
+ /** 被收掉的「空表」(表头+分隔行且无数据行)张数 */
22
+ emptyTables: number;
23
+ }
24
+ /**
25
+ * 收拢 CAPABILITIES.md 的表格排版(harness#171)
26
+ *
27
+ * 两条规则:① 删掉夹在两个表格行之间的空行(CommonMark 会在此切断表格);
28
+ * ② 收掉没有数据行的表头+分隔行。表格外的空行(段落分隔)不动;围栏代码块内的行整体豁免。
29
+ *
30
+ * 规则互相制造对方的触发点(①把两张空表之间的那个空行吃掉后,②一轮只收得掉后一张),
31
+ * 所以跑到不动点:**一次 `sync-docs` 必须把 `--check` 报出来的东西全清掉**,
32
+ * 否则下游 CI 修完还是红的。计数 = 各轮合计(每轮只删当轮存在的行,不会重计)。
33
+ *
34
+ * 幂等,且 `--check` 与写模式共用此正本——判定面就是「返回内容与入参是否不同」,
35
+ * 因此不存在「check 报了 fix 修不掉」的不收敛(ADR-0009 口径)。
36
+ */
37
+ export declare function normalizeCapabilitiesTableLayout(content: string): TableLayoutNormalization;
38
+ /**
39
+ * 将 CAPABILITIES.md 文件表格折叠为目录条目(--compact 一次性迁移)
40
+ *
41
+ * 按文件条目(第二列)的 dirname 分组:同组 ≥2 个文件条目折叠为一行目录条目
42
+ * (替换组内第一行、删除其余行,说明取组内第一行,为空则用目录名);
43
+ * 单独成组的文件行、表头、非表格文本、PRESERVE 块原样保留。
44
+ * 已折叠的目录条目行不再匹配文件条目,故幂等。
45
+ */
46
+ export declare function compactCapabilitiesContent(content: string): string;