@dommaker/harness 4.0.1 → 5.0.0

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 (117) hide show
  1. package/README.md +7 -8
  2. package/dist/bootstrap/bootstrap.d.ts +15 -0
  3. package/dist/bootstrap/bootstrap.js +18 -1
  4. package/dist/bootstrap/index.d.ts +2 -2
  5. package/dist/bootstrap/index.js +3 -2
  6. package/dist/cli/commands/acceptance.d.ts +8 -0
  7. package/dist/cli/commands/acceptance.js +45 -24
  8. package/dist/cli/commands/check.d.ts +2 -0
  9. package/dist/cli/commands/check.js +36 -5
  10. package/dist/cli/commands/command.js +1 -1
  11. package/dist/cli/commands/definitions.js +4 -1
  12. package/dist/cli/commands/release.d.ts +11 -3
  13. package/dist/cli/commands/release.js +159 -52
  14. package/dist/cli/commands/report.d.ts +2 -0
  15. package/dist/cli/commands/report.js +54 -6
  16. package/dist/cli/commands/security.d.ts +3 -3
  17. package/dist/cli/commands/security.js +3 -2
  18. package/dist/cli/commands/sync-docs/capabilities-syncer.d.ts +3 -40
  19. package/dist/cli/commands/sync-docs/capabilities-syncer.js +5 -159
  20. package/dist/cli/commands/sync-docs/compact.js +2 -2
  21. package/dist/cli/commands/sync-docs/context-syncer.d.ts +4 -36
  22. package/dist/cli/commands/sync-docs/context-syncer.js +4 -221
  23. package/dist/cli/commands/sync-docs/index.d.ts +8 -8
  24. package/dist/cli/commands/sync-docs/index.js +8 -8
  25. package/dist/cli/commands/sync-docs/main-flow.d.ts +5 -4
  26. package/dist/cli/commands/sync-docs/main-flow.js +6 -5
  27. package/dist/cli/commands/sync-docs/sync-output.d.ts +1 -1
  28. package/dist/cli/commands/sync-docs/sync-output.js +4 -0
  29. package/dist/cli/commands/sync-docs/sync-writes.d.ts +1 -1
  30. package/dist/cli/commands/validate.d.ts +2 -0
  31. package/dist/cli/commands/validate.js +40 -1
  32. package/dist/core/command-execution-policy.d.ts +59 -0
  33. package/dist/core/command-execution-policy.js +167 -0
  34. package/dist/core/config-files.d.ts +8 -0
  35. package/dist/core/config-files.js +62 -0
  36. package/dist/core/constraints/capabilities-parser.d.ts +2 -1
  37. package/dist/core/constraints/capabilities-parser.js +2 -1
  38. package/dist/core/constraints/checker.d.ts +18 -6
  39. package/dist/core/constraints/checker.js +21 -11
  40. package/dist/core/constraints/checkers/no-hardcoded-credentials.js +14 -15
  41. package/dist/core/constraints/checkers/no-test-simplification.js +8 -6
  42. package/dist/core/constraints/checkers/templated/exec.d.ts +4 -0
  43. package/dist/core/constraints/checkers/templated/exec.js +11 -48
  44. package/dist/core/constraints/checkers/templated/file-exists.js +7 -4
  45. package/dist/core/constraints/checkers/templated/regex-scan.js +11 -15
  46. package/dist/core/constraints/checkers/types.d.ts +13 -2
  47. package/dist/core/constraints/checkers/types.js +5 -2
  48. package/dist/core/constraints/construction-sites.d.ts +1 -1
  49. package/dist/core/constraints/construction-sites.js +1 -1
  50. package/dist/core/constraints/definitions.d.ts +2 -2
  51. package/dist/core/constraints/definitions.js +2 -2
  52. package/dist/core/constraints/injection-writer.d.ts +1 -1
  53. package/dist/core/constraints/injection-writer.js +1 -1
  54. package/dist/core/constraints/project-snapshot.d.ts +43 -0
  55. package/dist/core/constraints/project-snapshot.js +103 -0
  56. package/dist/core/constraints/run-env.d.ts +35 -0
  57. package/dist/core/constraints/run-env.js +36 -0
  58. package/dist/{cli/commands/sync-docs → core/docs-sync}/agents-syncer.d.ts +2 -0
  59. package/dist/{cli/commands/sync-docs → core/docs-sync}/agents-syncer.js +5 -3
  60. package/dist/core/docs-sync/capabilities-table.d.ts +46 -0
  61. package/dist/core/docs-sync/capabilities-table.js +200 -0
  62. package/dist/core/docs-sync/context-collector.d.ts +40 -0
  63. package/dist/core/docs-sync/context-collector.js +227 -0
  64. package/dist/{cli/commands/sync-docs → core/docs-sync}/project-reader.d.ts +5 -1
  65. package/dist/{cli/commands/sync-docs → core/docs-sync}/project-reader.js +10 -6
  66. package/dist/{cli/commands/sync-docs → core/docs-sync}/sync-state.d.ts +15 -9
  67. package/dist/{cli/commands/sync-docs → core/docs-sync}/sync-state.js +26 -29
  68. package/dist/core/effective-constraints.d.ts +1 -1
  69. package/dist/core/effective-constraints.js +1 -1
  70. package/dist/core/validators/check-handlers/command.js +22 -0
  71. package/dist/core/validators/check-handlers/output.js +26 -3
  72. package/dist/core/validators/checkpoint.d.ts +4 -0
  73. package/dist/core/validators/checkpoint.js +10 -2
  74. package/dist/gates/acceptance.d.ts +8 -4
  75. package/dist/gates/acceptance.js +76 -14
  76. package/dist/gates/command.d.ts +3 -4
  77. package/dist/gates/command.js +2 -3
  78. package/dist/gates/contract.d.ts +3 -4
  79. package/dist/gates/contract.js +0 -1
  80. package/dist/gates/definitions.d.ts +2 -4
  81. package/dist/gates/definitions.js +2 -8
  82. package/dist/gates/performance.d.ts +10 -4
  83. package/dist/gates/performance.js +49 -5
  84. package/dist/gates/review.d.ts +3 -4
  85. package/dist/gates/review.js +0 -1
  86. package/dist/gates/security.d.ts +3 -4
  87. package/dist/gates/security.js +26 -19
  88. package/dist/gates/types.d.ts +46 -19
  89. package/dist/index.d.ts +5 -4
  90. package/dist/index.js +9 -6
  91. package/dist/knowledge/index.d.ts +1 -1
  92. package/dist/knowledge/index.js +1 -2
  93. package/dist/knowledge/ingest.d.ts +32 -17
  94. package/dist/knowledge/ingest.js +188 -90
  95. package/dist/knowledge/lifecycle.d.ts +15 -0
  96. package/dist/knowledge/lifecycle.js +62 -0
  97. package/dist/knowledge/query.d.ts +11 -1
  98. package/dist/knowledge/query.js +61 -9
  99. package/dist/knowledge/store.d.ts +56 -3
  100. package/dist/knowledge/store.js +108 -30
  101. package/dist/monitoring/index.d.ts +2 -1
  102. package/dist/monitoring/index.js +3 -1
  103. package/dist/monitoring/traces.d.ts +12 -0
  104. package/dist/monitoring/traces.js +26 -4
  105. package/dist/types/checkpoint.d.ts +28 -1
  106. package/dist/types/command-execution-policy.d.ts +73 -0
  107. package/dist/types/command-execution-policy.js +16 -0
  108. package/dist/types/constraint.d.ts +0 -12
  109. package/dist/utils/exec.d.ts +7 -1
  110. package/dist/utils/exec.js +7 -1
  111. package/package.json +1 -1
  112. package/dist/core/constraints/check-cache.d.ts +0 -43
  113. package/dist/core/constraints/check-cache.js +0 -77
  114. package/dist/core/constraints/checkers/templated/glob.d.ts +0 -8
  115. package/dist/core/constraints/checkers/templated/glob.js +0 -14
  116. /package/dist/{cli/commands/sync-docs → core/docs-sync}/preserve-block.d.ts +0 -0
  117. /package/dist/{cli/commands/sync-docs → core/docs-sync}/preserve-block.js +0 -0
@@ -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;
@@ -0,0 +1,200 @@
1
+ "use strict";
2
+ /**
3
+ * CAPABILITIES.md file-table 文本正本(工单 22 / harness#171;P2.8 自
4
+ * cli/commands/sync-docs/capabilities-syncer.ts 下沉 core/docs-sync/,纯移位):
5
+ * 登记条目解析(parseCapabilitiesFiles)、表格排版判定(normalizeCapabilitiesTableLayout,
6
+ * check 与 fix 共用正本)、--compact 目录条目折叠(compactCapabilitiesContent)。
7
+ * 写侧 updateCapabilitiesFile 留在 cli/commands/sync-docs/capabilities-syncer.ts。
8
+ * 能力清单格式(计数行)的解析与计数在 core/constraints/capabilities-parser。
9
+ */
10
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ var desc = Object.getOwnPropertyDescriptor(m, k);
13
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
14
+ desc = { enumerable: true, get: function() { return m[k]; } };
15
+ }
16
+ Object.defineProperty(o, k2, desc);
17
+ }) : (function(o, m, k, k2) {
18
+ if (k2 === undefined) k2 = k;
19
+ o[k2] = m[k];
20
+ }));
21
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
22
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
23
+ }) : function(o, v) {
24
+ o["default"] = v;
25
+ });
26
+ var __importStar = (this && this.__importStar) || (function () {
27
+ var ownKeys = function(o) {
28
+ ownKeys = Object.getOwnPropertyNames || function (o) {
29
+ var ar = [];
30
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
31
+ return ar;
32
+ };
33
+ return ownKeys(o);
34
+ };
35
+ return function (mod) {
36
+ if (mod && mod.__esModule) return mod;
37
+ var result = {};
38
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
39
+ __setModuleDefault(result, mod);
40
+ return result;
41
+ };
42
+ })();
43
+ Object.defineProperty(exports, "__esModule", { value: true });
44
+ exports.parseCapabilitiesFiles = parseCapabilitiesFiles;
45
+ exports.normalizeCapabilitiesTableLayout = normalizeCapabilitiesTableLayout;
46
+ exports.compactCapabilitiesContent = compactCapabilitiesContent;
47
+ const path = __importStar(require("path"));
48
+ const capabilities_parser_1 = require("../constraints/capabilities-parser");
49
+ /**
50
+ * 解析 CAPABILITIES.md 中的文件路径
51
+ *
52
+ * 提取所有表格行中提到的 .ts/.tsx/.js/.jsx 文件名(不含路径前缀)
53
+ * 用于模糊匹配:只要文档中提到了该文件名就算已记录
54
+ */
55
+ async function parseCapabilitiesFiles(capabilitiesPath) {
56
+ // 工单 19-B:解析收敛到 core/constraints/capabilities-parser;
57
+ // 此处保持原语义——文件条目取 basename,目录条目(以 / 结尾)原样保留
58
+ const entries = (0, capabilities_parser_1.readCapabilitiesEntries)(capabilitiesPath, { includeDirs: true });
59
+ const files = [];
60
+ for (const entry of entries) {
61
+ // split('/') 恒产出 ≥1 段,末段索引恒有值
62
+ const segments = entry.split('/');
63
+ const value = entry.endsWith('/') ? entry : segments[segments.length - 1];
64
+ if (!files.includes(value))
65
+ files.push(value);
66
+ }
67
+ return files;
68
+ }
69
+ /** 表格行:以 `|` 起始(表头、分隔行、数据行都算) */
70
+ const TABLE_ROW_REGEX = /^\s*\|/;
71
+ /** 表格分隔行(|------|------|) */
72
+ const TABLE_SEPARATOR_REGEX = /^\s*\|[\s:|-]+\|\s*$/;
73
+ /** 围栏代码块的起止行(``` / ~~~,允许缩进与信息串) */
74
+ const FENCE_LINE_REGEX = /^\s*(?:```|~~~)/;
75
+ /**
76
+ * 按行切开并标出围栏代码块(``` / ~~~)内的行
77
+ *
78
+ * 只服务排版收拢:CAPABILITIES.md 是手写文档,块内长得像表格的示例行不是排版脏行,
79
+ * 收它就是删用户正文(harness#171)。登记条目面不在此列——见 cli 侧
80
+ * capabilities-syncer.ts「幽灵行删除不豁免围栏」的取舍注释。
81
+ */
82
+ function parseLayoutLines(content) {
83
+ const parsed = [];
84
+ let inFence = false;
85
+ for (const text of content.split('\n')) {
86
+ if (FENCE_LINE_REGEX.test(text)) {
87
+ parsed.push({ text, fenced: true });
88
+ inFence = !inFence;
89
+ continue;
90
+ }
91
+ parsed.push({ text, fenced: inFence });
92
+ }
93
+ return parsed;
94
+ }
95
+ const isTableRow = (line) => line !== undefined && !line.fenced && TABLE_ROW_REGEX.test(line.text);
96
+ const isTableSeparator = (line) => line !== undefined && !line.fenced && TABLE_SEPARATOR_REGEX.test(line.text);
97
+ /** 走一遍两条收拢规则(不迭代) */
98
+ function collapseTableLayoutOnce(content) {
99
+ const parsed = parseLayoutLines(content);
100
+ // ① 收拢表格内空行:一段连续空行,两侧最近非空行都是表格行 → 整段丢弃
101
+ const kept = [];
102
+ let blankLines = 0;
103
+ for (let i = 0; i < parsed.length;) {
104
+ if (parsed[i].text.trim() !== '') {
105
+ kept.push(parsed[i]);
106
+ i++;
107
+ continue;
108
+ }
109
+ let end = i;
110
+ while (end < parsed.length && parsed[end].text.trim() === '')
111
+ end++;
112
+ if (isTableRow(kept[kept.length - 1]) && isTableRow(parsed[end])) {
113
+ blankLines += end - i;
114
+ }
115
+ else {
116
+ kept.push(...parsed.slice(i, end));
117
+ }
118
+ i = end;
119
+ }
120
+ // ② 收掉空表:表头 + 分隔行后面没有数据行
121
+ const out = [];
122
+ let emptyTables = 0;
123
+ for (let i = 0; i < kept.length; i++) {
124
+ if (isTableRow(kept[i]) && isTableSeparator(kept[i + 1]) && !isTableRow(kept[i + 2])) {
125
+ emptyTables++;
126
+ i++;
127
+ continue;
128
+ }
129
+ out.push(kept[i].text);
130
+ }
131
+ return { content: out.join('\n'), blankLines, emptyTables };
132
+ }
133
+ /**
134
+ * 收拢 CAPABILITIES.md 的表格排版(harness#171)
135
+ *
136
+ * 两条规则:① 删掉夹在两个表格行之间的空行(CommonMark 会在此切断表格);
137
+ * ② 收掉没有数据行的表头+分隔行。表格外的空行(段落分隔)不动;围栏代码块内的行整体豁免。
138
+ *
139
+ * 规则互相制造对方的触发点(①把两张空表之间的那个空行吃掉后,②一轮只收得掉后一张),
140
+ * 所以跑到不动点:**一次 `sync-docs` 必须把 `--check` 报出来的东西全清掉**,
141
+ * 否则下游 CI 修完还是红的。计数 = 各轮合计(每轮只删当轮存在的行,不会重计)。
142
+ *
143
+ * 幂等,且 `--check` 与写模式共用此正本——判定面就是「返回内容与入参是否不同」,
144
+ * 因此不存在「check 报了 fix 修不掉」的不收敛(ADR-0009 口径)。
145
+ */
146
+ function normalizeCapabilitiesTableLayout(content) {
147
+ let current = content;
148
+ let blankLines = 0;
149
+ let emptyTables = 0;
150
+ for (;;) {
151
+ const pass = collapseTableLayoutOnce(current);
152
+ if (pass.blankLines === 0 && pass.emptyTables === 0) {
153
+ return { content: current, blankLines, emptyTables };
154
+ }
155
+ blankLines += pass.blankLines;
156
+ emptyTables += pass.emptyTables;
157
+ current = pass.content;
158
+ }
159
+ }
160
+ /**
161
+ * 将 CAPABILITIES.md 文件表格折叠为目录条目(--compact 一次性迁移)
162
+ *
163
+ * 按文件条目(第二列)的 dirname 分组:同组 ≥2 个文件条目折叠为一行目录条目
164
+ * (替换组内第一行、删除其余行,说明取组内第一行,为空则用目录名);
165
+ * 单独成组的文件行、表头、非表格文本、PRESERVE 块原样保留。
166
+ * 已折叠的目录条目行不再匹配文件条目,故幂等。
167
+ */
168
+ function compactCapabilitiesContent(content) {
169
+ const lines = content.split('\n');
170
+ const fileEntryRegex = /\.(?:ts|tsx|js|jsx)$/;
171
+ const rowRegex = /^\|[^|]+\|[^|]+\|[^|]+\|\s*$/;
172
+ // 收集文件条目行,按 dirname 分组(无目录前缀的条目不参与折叠)
173
+ const groups = new Map();
174
+ for (let i = 0; i < lines.length; i++) {
175
+ if (!rowRegex.test(lines[i]))
176
+ continue;
177
+ const cells = lines[i].split('|').map(c => c.trim());
178
+ const file = cells[2] ?? '';
179
+ if (!fileEntryRegex.test(file))
180
+ continue;
181
+ const dir = path.posix.dirname(file);
182
+ if (dir === '.')
183
+ continue;
184
+ const group = groups.get(dir) ?? [];
185
+ group.push({ lineIndex: i, file, desc: cells[3] ?? '' });
186
+ groups.set(dir, group);
187
+ }
188
+ const deleteLines = new Set();
189
+ for (const [dir, rows] of groups) {
190
+ if (rows.length < 2)
191
+ continue;
192
+ const name = path.posix.basename(dir);
193
+ const desc = rows[0].desc || name;
194
+ lines[rows[0].lineIndex] = `| ${name} | ${dir}/ | ${desc} |`;
195
+ for (const row of rows.slice(1)) {
196
+ deleteLines.add(row.lineIndex);
197
+ }
198
+ }
199
+ return lines.filter((_, i) => !deleteLines.has(i)).join('\n');
200
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * CONTEXT.md 采集器(harness#142 / #202;P2.8 自 cli/commands/sync-docs/context-syncer.ts
3
+ * 下沉 core/docs-sync/,纯移位):既有文件发现 + mtime 采集 + 目录导出面采集
4
+ * (供 context-reconcile 的内容判定喂数)+ 构造点计数采集(供 construction-sites
5
+ * 的标记判定喂数)。判定本体都是 core/constraints/ 的纯函数,本文件不做判定。
6
+ * 写侧 createContextMd 留在 cli(scaffold 模板正本在 cli 层)。
7
+ */
8
+ /**
9
+ * 自动发现已有 CONTEXT.md 文件的目录(相对于 projectPath)
10
+ */
11
+ export declare function findExistingContextFiles(projectPath: string, srcDirs: string[]): Promise<string[]>;
12
+ /**
13
+ * 获取目录下最新 .ts 文件的修改时间
14
+ * 返回 null 如果目录不存在或没有 .ts 文件
15
+ */
16
+ export declare function getLatestTsMtime(dirPath: string): Promise<number | null>;
17
+ /** 目录导出面采集结果(harness#142) */
18
+ export interface ContextExportSurface {
19
+ /** 目录内所有 .ts 导出的符号名(值 + 类型,含 barrel 再导出) */
20
+ surface: string[];
21
+ /** `index.ts` barrel 再导出的**值**符号(类型-only 已剔除);无 index.ts → 空 */
22
+ barrelExports: string[];
23
+ }
24
+ /**
25
+ * 采集目录导出面(内容判定的代码侧):目录内 .ts(排 `__tests__/`、`.d.ts`)的导出符号全集,
26
+ * 以及 barrel(`index.ts`)再导出的值符号清单。fs IO 全在此,判定不在(ADR-0025)。
27
+ */
28
+ export declare function collectContextExportSurface(dirPath: string, projectPath: string): Promise<ContextExportSurface>;
29
+ /**
30
+ * 构造点计数器(harness#202 / ADR-0039):(类名, 是否计测试目录) → 全仓
31
+ * `new X(` 直构造出现次数。fs 采集全在此,计数口径正本是 core 侧
32
+ * `tallyConstructionSites`(逐文件调用后累加)。
33
+ *
34
+ * 排除口径沿用 DEFAULT_SKIP_DIRS(node_modules/__tests__/dist);
35
+ * includeTests 时把 `__tests__` 从跳过名单放出。两种口径各自懒采集一次
36
+ * (全仓走一遍不便宜),结果按口径 memo——一次 sync-docs 运行里同口径
37
+ * 的多个标记共用同一份计数。
38
+ */
39
+ export type ConstructionSiteCounter = (className: string, includeTests: boolean) => number;
40
+ export declare function createConstructionSiteCounter(projectPath: string): ConstructionSiteCounter;
@@ -0,0 +1,227 @@
1
+ "use strict";
2
+ /**
3
+ * CONTEXT.md 采集器(harness#142 / #202;P2.8 自 cli/commands/sync-docs/context-syncer.ts
4
+ * 下沉 core/docs-sync/,纯移位):既有文件发现 + mtime 采集 + 目录导出面采集
5
+ * (供 context-reconcile 的内容判定喂数)+ 构造点计数采集(供 construction-sites
6
+ * 的标记判定喂数)。判定本体都是 core/constraints/ 的纯函数,本文件不做判定。
7
+ * 写侧 createContextMd 留在 cli(scaffold 模板正本在 cli 层)。
8
+ */
9
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ var desc = Object.getOwnPropertyDescriptor(m, k);
12
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
13
+ desc = { enumerable: true, get: function() { return m[k]; } };
14
+ }
15
+ Object.defineProperty(o, k2, desc);
16
+ }) : (function(o, m, k, k2) {
17
+ if (k2 === undefined) k2 = k;
18
+ o[k2] = m[k];
19
+ }));
20
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
21
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
22
+ }) : function(o, v) {
23
+ o["default"] = v;
24
+ });
25
+ var __importStar = (this && this.__importStar) || (function () {
26
+ var ownKeys = function(o) {
27
+ ownKeys = Object.getOwnPropertyNames || function (o) {
28
+ var ar = [];
29
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
30
+ return ar;
31
+ };
32
+ return ownKeys(o);
33
+ };
34
+ return function (mod) {
35
+ if (mod && mod.__esModule) return mod;
36
+ var result = {};
37
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
38
+ __setModuleDefault(result, mod);
39
+ return result;
40
+ };
41
+ })();
42
+ Object.defineProperty(exports, "__esModule", { value: true });
43
+ exports.findExistingContextFiles = findExistingContextFiles;
44
+ exports.getLatestTsMtime = getLatestTsMtime;
45
+ exports.collectContextExportSurface = collectContextExportSurface;
46
+ exports.createConstructionSiteCounter = createConstructionSiteCounter;
47
+ const fs = __importStar(require("fs/promises"));
48
+ const fs_1 = require("fs");
49
+ const path = __importStar(require("path"));
50
+ const file_walk_1 = require("../../utils/file-walk");
51
+ const context_reconcile_1 = require("../constraints/context-reconcile");
52
+ const construction_sites_1 = require("../constraints/construction-sites");
53
+ /**
54
+ * 自动发现已有 CONTEXT.md 文件的目录(相对于 projectPath)
55
+ */
56
+ async function findExistingContextFiles(projectPath, srcDirs) {
57
+ const dirs = [];
58
+ async function scan(dir) {
59
+ // fail-fast:目录不可读直接抛,不静默漏掉整棵子树的 CONTEXT.md
60
+ const entries = await fs.readdir(dir);
61
+ for (const entry of entries) {
62
+ if (file_walk_1.DEFAULT_SKIP_DIRS.includes(entry))
63
+ continue;
64
+ const entryPath = path.join(dir, entry);
65
+ const stat = await fs.stat(entryPath);
66
+ if (stat.isDirectory()) {
67
+ // 检查该目录是否有 CONTEXT.md
68
+ if ((0, fs_1.existsSync)(path.join(entryPath, 'CONTEXT.md'))) {
69
+ dirs.push(path.relative(projectPath, entryPath));
70
+ }
71
+ await scan(entryPath);
72
+ }
73
+ }
74
+ }
75
+ for (const srcDir of srcDirs) {
76
+ // 源码根不在场 = 无可发现(缺失判定归调用方 4a 的 contextMissing),不扫
77
+ const root = path.join(projectPath, srcDir);
78
+ if ((0, fs_1.existsSync)(root))
79
+ await scan(root);
80
+ }
81
+ return dirs;
82
+ }
83
+ /**
84
+ * 获取目录下最新 .ts 文件的修改时间
85
+ * 返回 null 如果目录不存在或没有 .ts 文件
86
+ */
87
+ async function getLatestTsMtime(dirPath) {
88
+ let latest = null;
89
+ async function scan(dir) {
90
+ // fail-fast:目录不可读直接抛(理由同 findExistingContextFiles)
91
+ const entries = await fs.readdir(dir);
92
+ for (const entry of entries) {
93
+ if (file_walk_1.DEFAULT_SKIP_DIRS.includes(entry))
94
+ continue;
95
+ const entryPath = path.join(dir, entry);
96
+ const stat = await fs.stat(entryPath);
97
+ if (stat.isDirectory()) {
98
+ await scan(entryPath);
99
+ }
100
+ else if (entry.endsWith('.ts') && !entry.endsWith('.d.ts')) {
101
+ if (latest === null || stat.mtimeMs > latest) {
102
+ latest = stat.mtimeMs;
103
+ }
104
+ }
105
+ }
106
+ }
107
+ // 目录不存在 → null(函数契约);存在但扫不出 → 抛(fail-fast)
108
+ if (!(0, fs_1.existsSync)(dirPath))
109
+ return null;
110
+ await scan(dirPath);
111
+ return latest;
112
+ }
113
+ /**
114
+ * 把相对 spec 解析为仓内可读的 .ts 文件(`./x` → `x.ts` 或 `x/index.ts`);
115
+ * 非相对 spec、越出项目根、命中 `.d.ts`/`node_modules`/跳过目录一律 null(不并入导出面)。
116
+ */
117
+ function resolveTsTarget(fromFile, spec, projectPath) {
118
+ if (!spec.startsWith('.'))
119
+ return null;
120
+ const base = path.resolve(path.dirname(fromFile), spec);
121
+ for (const candidate of [`${base}.ts`, path.join(base, 'index.ts')]) {
122
+ if (candidate.endsWith('.d.ts'))
123
+ continue;
124
+ const rel = path.relative(projectPath, candidate);
125
+ if (!rel || rel.startsWith('..') || rel.includes('node_modules'))
126
+ continue;
127
+ if (rel.split(path.sep).some((segment) => file_walk_1.DEFAULT_SKIP_DIRS.includes(segment)))
128
+ continue;
129
+ if ((0, fs_1.existsSync)(candidate))
130
+ return candidate;
131
+ }
132
+ return null;
133
+ }
134
+ /**
135
+ * 单文件导出表(含再导出与 `export *` 展开),memo 化 + 在途集合破环。
136
+ *
137
+ * 递归只沿 `export *` 与具名再导出的来源走,读到的都是仓内 .ts;
138
+ * 环上(A `export * from './b'`、B `export * from './a'`)在途文件交空表,
139
+ * 与 tsc 的星号环语义一致(不因此丢名字——真实名字总有非环上的一条声明路径)。
140
+ */
141
+ async function collectFileExports(file, projectPath, cache, inProgress) {
142
+ const cached = cache.get(file);
143
+ if (cached)
144
+ return cached;
145
+ if (inProgress.has(file))
146
+ return new Map();
147
+ inProgress.add(file);
148
+ const out = new Map();
149
+ // fail-fast:文件来自导出面扫描/相对导入解析,读失败 = 真 IO 故障。
150
+ // 曾经 catch 成空表并写进 cache——把「读不了」伪装成「没有导出」,幽灵判定就此失真
151
+ const source = await fs.readFile(file, 'utf-8');
152
+ const { symbols, starFrom } = (0, context_reconcile_1.parseExportStatements)(source);
153
+ for (const symbol of symbols) {
154
+ let typeOnly = symbol.typeOnly;
155
+ if (symbol.from) {
156
+ const origin = resolveTsTarget(file, symbol.from, projectPath);
157
+ // 环上(origin 在途)时交回的是空表,那是「不知道」不是「没有」→ 不做虚判
158
+ if (origin && !inProgress.has(origin)) {
159
+ const originExports = await collectFileExports(origin, projectPath, cache, inProgress);
160
+ // 正本没有这个本名 → 这条再导出是虚的(改名未跟上的 barrel),不计入导出面,
161
+ // 否则文档里的幽灵会被 barrel 自己洗白(harness#142 反证撞出的口径漏洞)
162
+ if (!originExports.has(symbol.localName))
163
+ continue;
164
+ if (!typeOnly && originExports.get(symbol.localName) === true)
165
+ typeOnly = true;
166
+ }
167
+ }
168
+ out.set(symbol.name, typeOnly);
169
+ }
170
+ for (const spec of starFrom) {
171
+ const target = resolveTsTarget(file, spec, projectPath);
172
+ if (!target)
173
+ continue;
174
+ const targetExports = await collectFileExports(target, projectPath, cache, inProgress);
175
+ for (const [name, typeOnly] of targetExports)
176
+ if (!out.has(name))
177
+ out.set(name, typeOnly);
178
+ }
179
+ inProgress.delete(file);
180
+ cache.set(file, out);
181
+ return out;
182
+ }
183
+ /**
184
+ * 采集目录导出面(内容判定的代码侧):目录内 .ts(排 `__tests__/`、`.d.ts`)的导出符号全集,
185
+ * 以及 barrel(`index.ts`)再导出的值符号清单。fs IO 全在此,判定不在(ADR-0025)。
186
+ */
187
+ async function collectContextExportSurface(dirPath, projectPath) {
188
+ const files = (0, file_walk_1.findTsSourceFiles)(dirPath);
189
+ const cache = new Map();
190
+ const surface = new Set();
191
+ for (const file of files) {
192
+ const fileExports = await collectFileExports(file, projectPath, cache, new Set());
193
+ for (const name of fileExports.keys())
194
+ surface.add(name);
195
+ }
196
+ const barrelExports = [];
197
+ const barrelFile = path.join(dirPath, 'index.ts');
198
+ if (files.includes(barrelFile)) {
199
+ const barrel = await collectFileExports(barrelFile, projectPath, cache, new Set());
200
+ for (const [name, typeOnly] of barrel)
201
+ if (!typeOnly)
202
+ barrelExports.push(name);
203
+ }
204
+ return { surface: [...surface], barrelExports };
205
+ }
206
+ function createConstructionSiteCounter(projectPath) {
207
+ const tallies = new Map();
208
+ const tallyFor = (includeTests) => {
209
+ let tally = tallies.get(includeTests);
210
+ if (tally)
211
+ return tally;
212
+ tally = new Map();
213
+ const skipDirs = includeTests
214
+ ? file_walk_1.DEFAULT_SKIP_DIRS.filter((d) => d !== '__tests__')
215
+ : file_walk_1.DEFAULT_SKIP_DIRS;
216
+ for (const file of (0, file_walk_1.findTsSourceFiles)(projectPath, { skipDirs })) {
217
+ // fail-fast:文件来自 walk 结果,读失败直接抛,不静默漏计构造点
218
+ const source = (0, fs_1.readFileSync)(file, 'utf-8');
219
+ for (const [name, count] of (0, construction_sites_1.tallyConstructionSites)(source)) {
220
+ tally.set(name, (tally.get(name) ?? 0) + count);
221
+ }
222
+ }
223
+ tallies.set(includeTests, tally);
224
+ return tally;
225
+ };
226
+ return (className, includeTests) => tallyFor(includeTests).get(className) ?? 0;
227
+ }
@@ -1,7 +1,11 @@
1
1
  /**
2
2
  * 项目读取器(工单 22):package.json / config.yml / 源码结构的项目信息提取
3
+ *
4
+ * P2.8 自 cli/commands/sync-docs/ 下沉 core/docs-sync/(采集与判定收口,
5
+ * handoff P2 第 4 项):纯移位,职责与口径不变。判定本体仍在
6
+ * core/constraints/ 的 reconcile 族纯函数。
3
7
  */
4
- import type { ConstructionSiteDrift } from '../../../core/constraints/construction-sites';
8
+ import type { ConstructionSiteDrift } from '../constraints/construction-sites';
5
9
  export interface ModuleInfo {
6
10
  name: string;
7
11
  file: string;
@@ -1,6 +1,10 @@
1
1
  "use strict";
2
2
  /**
3
3
  * 项目读取器(工单 22):package.json / config.yml / 源码结构的项目信息提取
4
+ *
5
+ * P2.8 自 cli/commands/sync-docs/ 下沉 core/docs-sync/(采集与判定收口,
6
+ * handoff P2 第 4 项):纯移位,职责与口径不变。判定本体仍在
7
+ * core/constraints/ 的 reconcile 族纯函数。
4
8
  */
5
9
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
@@ -47,12 +51,12 @@ exports.extractFileDescriptions = extractFileDescriptions;
47
51
  const fs = __importStar(require("fs/promises"));
48
52
  const fs_1 = require("fs");
49
53
  const path = __importStar(require("path"));
50
- const project_config_loader_1 = require("../../../core/project-config-loader");
51
- const governance_accessors_1 = require("../../../core/constraints/governance-accessors");
52
- const detect_source_roots_1 = require("../../../utils/detect-source-roots");
53
- const file_walk_1 = require("../../../utils/file-walk");
54
- const attempt_1 = require("../../../utils/attempt");
55
- const package_json_1 = require("../../../utils/package-json");
54
+ const project_config_loader_1 = require("../project-config-loader");
55
+ const governance_accessors_1 = require("../constraints/governance-accessors");
56
+ const detect_source_roots_1 = require("../../utils/detect-source-roots");
57
+ const file_walk_1 = require("../../utils/file-walk");
58
+ const attempt_1 = require("../../utils/attempt");
59
+ const package_json_1 = require("../../utils/package-json");
56
60
  /** 描述提取的并发上限(#147 固定缺省,按票下裁决不做成配置面) */
57
61
  const DESCRIPTION_CONCURRENCY = 16;
58
62
  /** 读取 package.json(不存在返回 null;在场但损坏 → 抛出,fail-fast 不装「没有」) */
@@ -1,17 +1,18 @@
1
1
  /**
2
- * sync-docs 主流程的「采集 → 判定」两段(ADR-0040 Phase 3 自 main-flow.ts 拆段,纯移位)
2
+ * sync-docs 的「采集 → 判定」主入口(ADR-0040 Phase 3 拆段;P2.8 自
3
+ * cli/commands/sync-docs/ 下沉 core/docs-sync/,handoff P2 第 4 项:
4
+ * 文档同步模块负责采集与判定,CLI 只保留输出)
3
5
  *
4
6
  * - collectSyncState:扫描源码模块 → CAPABILITIES.md 两种格式判定与对比 →
5
7
  * CONTEXT.md 缺失/漂移/mtime → AGENTS.md 期望内容 → 表格排版,全部收成一枚 SyncState;
6
8
  * - judgeSyncState:SyncState → 问题旗标组(hasIssues 族)与可定位的漂移 reason。
7
- * 出口(JSON/人读输出、写入自愈、gate 判定)分别住 sync-output.ts / sync-writes.ts /
8
- * main-flow.ts 尾部。
9
+ * 出口(JSON/人读输出、写入自愈)住 cli/commands/sync-docs/ 的 sync-output.ts /
10
+ * sync-writes.ts;「未找到 srcDir」警告属输出职责,由输出侧从 missingSrcDirs 打印。
9
11
  */
10
- import { type CapabilityDefinitionSource } from '../../../core/constraints/capabilities-parser';
11
- import { type CapabilitiesMode } from '../../../core/constraints/governance-accessors';
12
+ import { type CapabilityDefinitionSource } from '../constraints/capabilities-parser';
13
+ import { type CapabilitiesMode } from '../constraints/governance-accessors';
12
14
  import type { ModuleInfo, SyncResult } from './project-reader';
13
- import { normalizeCapabilitiesTableLayout } from './capabilities-syncer';
14
- import { type CommandIO } from '../../command-contract';
15
+ import { normalizeCapabilitiesTableLayout } from './capabilities-table';
15
16
  export interface SyncDocsOptions {
16
17
  /** 项目路径 */
17
18
  projectPath?: string;
@@ -48,11 +49,16 @@ export interface SyncState {
48
49
  agentsMdStale: boolean;
49
50
  agentsMdMalformedPreserve: string[];
50
51
  tableLayout: TableLayoutVerdict;
52
+ /**
53
+ * 配置要求但不在场的源码根(采集期跳过)。提示打印是输出职责,
54
+ * 由 cli 侧 sync-output.ts 从本字段打出(stdout 字节序与旧实现一致)。
55
+ */
56
+ missingSrcDirs: string[];
51
57
  }
52
58
  /**
53
- * 采集段:扫描与全部判定面数据收集(不写任何文件)
59
+ * 采集段:扫描与全部判定面数据收集(不写任何文件、不打印——纯采集+判定)
54
60
  */
55
- export declare function collectSyncState(projectPath: string, options: SyncDocsOptions, io: CommandIO, definitions: CapabilityDefinitionSource): Promise<SyncState>;
61
+ export declare function collectSyncState(projectPath: string, options: SyncDocsOptions, definitions: CapabilityDefinitionSource): Promise<SyncState>;
56
62
  /** 判定段产物:问题旗标组 + 可定位的 CONTEXT.md 漂移 reason */
57
63
  export interface SyncVerdict {
58
64
  hasTableLayoutIssues: boolean;