@dommaker/harness 1.6.0 → 1.8.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 (560) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/bin/harness.js +17 -1
  3. package/dist/agents/index.d.ts +6 -2
  4. package/dist/agents/index.d.ts.map +1 -1
  5. package/dist/agents/index.js +6 -16
  6. package/dist/agents/index.js.map +1 -1
  7. package/dist/cli/commands/__tests__/gate-decision.d.ts +18 -0
  8. package/dist/cli/commands/__tests__/gate-decision.d.ts.map +1 -0
  9. package/dist/cli/commands/__tests__/gate-decision.js +26 -0
  10. package/dist/cli/commands/__tests__/gate-decision.js.map +1 -0
  11. package/dist/cli/commands/acceptance.d.ts.map +1 -1
  12. package/dist/cli/commands/acceptance.js +17 -28
  13. package/dist/cli/commands/acceptance.js.map +1 -1
  14. package/dist/cli/commands/check.d.ts +11 -3
  15. package/dist/cli/commands/check.d.ts.map +1 -1
  16. package/dist/cli/commands/check.js +26 -11
  17. package/dist/cli/commands/check.js.map +1 -1
  18. package/dist/cli/commands/command.d.ts +0 -1
  19. package/dist/cli/commands/command.d.ts.map +1 -1
  20. package/dist/cli/commands/command.js +8 -6
  21. package/dist/cli/commands/command.js.map +1 -1
  22. package/dist/cli/commands/constraints-retire.d.ts.map +1 -1
  23. package/dist/cli/commands/constraints-retire.js +36 -57
  24. package/dist/cli/commands/constraints-retire.js.map +1 -1
  25. package/dist/cli/commands/contract.d.ts.map +1 -1
  26. package/dist/cli/commands/contract.js +36 -42
  27. package/dist/cli/commands/contract.js.map +1 -1
  28. package/dist/cli/commands/definitions.d.ts +2 -0
  29. package/dist/cli/commands/definitions.d.ts.map +1 -1
  30. package/dist/cli/commands/definitions.js +11 -6
  31. package/dist/cli/commands/definitions.js.map +1 -1
  32. package/dist/cli/commands/failure.d.ts.map +1 -1
  33. package/dist/cli/commands/failure.js +16 -2
  34. package/dist/cli/commands/failure.js.map +1 -1
  35. package/dist/cli/commands/init.d.ts +14 -8
  36. package/dist/cli/commands/init.d.ts.map +1 -1
  37. package/dist/cli/commands/init.js +172 -364
  38. package/dist/cli/commands/init.js.map +1 -1
  39. package/dist/cli/commands/knowledge-view.d.ts +90 -0
  40. package/dist/cli/commands/knowledge-view.d.ts.map +1 -0
  41. package/dist/cli/commands/knowledge-view.js +198 -0
  42. package/dist/cli/commands/knowledge-view.js.map +1 -0
  43. package/dist/cli/commands/knowledge.d.ts +137 -52
  44. package/dist/cli/commands/knowledge.d.ts.map +1 -1
  45. package/dist/cli/commands/knowledge.js +534 -400
  46. package/dist/cli/commands/knowledge.js.map +1 -1
  47. package/dist/cli/commands/passes-gate.d.ts +4 -1
  48. package/dist/cli/commands/passes-gate.d.ts.map +1 -1
  49. package/dist/cli/commands/passes-gate.js +11 -2
  50. package/dist/cli/commands/passes-gate.js.map +1 -1
  51. package/dist/cli/commands/performance.d.ts.map +1 -1
  52. package/dist/cli/commands/performance.js +19 -31
  53. package/dist/cli/commands/performance.js.map +1 -1
  54. package/dist/cli/commands/report.d.ts.map +1 -1
  55. package/dist/cli/commands/report.js +2 -1
  56. package/dist/cli/commands/report.js.map +1 -1
  57. package/dist/cli/commands/review.d.ts.map +1 -1
  58. package/dist/cli/commands/review.js +13 -25
  59. package/dist/cli/commands/review.js.map +1 -1
  60. package/dist/cli/commands/scaffold-templates.d.ts +73 -0
  61. package/dist/cli/commands/scaffold-templates.d.ts.map +1 -0
  62. package/dist/cli/commands/scaffold-templates.js +424 -0
  63. package/dist/cli/commands/scaffold-templates.js.map +1 -0
  64. package/dist/cli/commands/scaffold.d.ts +105 -0
  65. package/dist/cli/commands/scaffold.d.ts.map +1 -0
  66. package/dist/cli/commands/scaffold.js +263 -0
  67. package/dist/cli/commands/scaffold.js.map +1 -0
  68. package/dist/cli/commands/security.d.ts.map +1 -1
  69. package/dist/cli/commands/security.js +22 -24
  70. package/dist/cli/commands/security.js.map +1 -1
  71. package/dist/cli/commands/spec-baseline-check.d.ts +43 -0
  72. package/dist/cli/commands/spec-baseline-check.d.ts.map +1 -1
  73. package/dist/cli/commands/spec-baseline-check.js +109 -34
  74. package/dist/cli/commands/spec-baseline-check.js.map +1 -1
  75. package/dist/cli/commands/sync-docs/context-syncer.d.ts +21 -2
  76. package/dist/cli/commands/sync-docs/context-syncer.d.ts.map +1 -1
  77. package/dist/cli/commands/sync-docs/context-syncer.js +112 -28
  78. package/dist/cli/commands/sync-docs/context-syncer.js.map +1 -1
  79. package/dist/cli/commands/sync-docs/index.d.ts +1 -1
  80. package/dist/cli/commands/sync-docs/index.d.ts.map +1 -1
  81. package/dist/cli/commands/sync-docs/index.js +86 -27
  82. package/dist/cli/commands/sync-docs/index.js.map +1 -1
  83. package/dist/cli/commands/sync-docs/project-reader.d.ts +24 -1
  84. package/dist/cli/commands/sync-docs/project-reader.d.ts.map +1 -1
  85. package/dist/cli/commands/sync-docs/project-reader.js +33 -7
  86. package/dist/cli/commands/sync-docs/project-reader.js.map +1 -1
  87. package/dist/cli/commands/validate.d.ts +0 -16
  88. package/dist/cli/commands/validate.d.ts.map +1 -1
  89. package/dist/cli/commands/validate.js +2 -97
  90. package/dist/cli/commands/validate.js.map +1 -1
  91. package/dist/cli/gate-command.d.ts +37 -0
  92. package/dist/cli/gate-command.d.ts.map +1 -0
  93. package/dist/cli/gate-command.js +64 -0
  94. package/dist/cli/gate-command.js.map +1 -0
  95. package/dist/completion-checkers/index.d.ts +1 -1
  96. package/dist/completion-checkers/index.d.ts.map +1 -1
  97. package/dist/completion-checkers/index.js +0 -15
  98. package/dist/completion-checkers/index.js.map +1 -1
  99. package/dist/context/knowledge-injector.d.ts +4 -2
  100. package/dist/context/knowledge-injector.d.ts.map +1 -1
  101. package/dist/context/knowledge-injector.js +13 -6
  102. package/dist/context/knowledge-injector.js.map +1 -1
  103. package/dist/core/constraints/agent-prompt-renderer.js +2 -2
  104. package/dist/core/constraints/agent-prompt-renderer.js.map +1 -1
  105. package/dist/core/constraints/capabilities-reconcile.d.ts +20 -0
  106. package/dist/core/constraints/capabilities-reconcile.d.ts.map +1 -1
  107. package/dist/core/constraints/capabilities-reconcile.js +26 -4
  108. package/dist/core/constraints/capabilities-reconcile.js.map +1 -1
  109. package/dist/core/constraints/check-cache.d.ts +1 -1
  110. package/dist/core/constraints/check-cache.js +1 -1
  111. package/dist/core/constraints/checker.d.ts +8 -3
  112. package/dist/core/constraints/checker.d.ts.map +1 -1
  113. package/dist/core/constraints/checker.js +30 -19
  114. package/dist/core/constraints/checker.js.map +1 -1
  115. package/dist/core/constraints/checkers/capability-sync.d.ts +3 -1
  116. package/dist/core/constraints/checkers/capability-sync.d.ts.map +1 -1
  117. package/dist/core/constraints/checkers/capability-sync.js +17 -24
  118. package/dist/core/constraints/checkers/capability-sync.js.map +1 -1
  119. package/dist/core/constraints/checkers/context-doc-sync.d.ts +1 -1
  120. package/dist/core/constraints/checkers/context-doc-sync.d.ts.map +1 -1
  121. package/dist/core/constraints/checkers/context-doc-sync.js +9 -6
  122. package/dist/core/constraints/checkers/context-doc-sync.js.map +1 -1
  123. package/dist/core/constraints/checkers/docs-freshness.d.ts.map +1 -1
  124. package/dist/core/constraints/checkers/docs-freshness.js +24 -30
  125. package/dist/core/constraints/checkers/docs-freshness.js.map +1 -1
  126. package/dist/core/constraints/checkers/governance-presence.d.ts.map +1 -1
  127. package/dist/core/constraints/checkers/governance-presence.js +10 -4
  128. package/dist/core/constraints/checkers/governance-presence.js.map +1 -1
  129. package/dist/core/constraints/checkers/types.d.ts +10 -6
  130. package/dist/core/constraints/checkers/types.d.ts.map +1 -1
  131. package/dist/core/constraints/checkers/types.js +7 -3
  132. package/dist/core/constraints/checkers/types.js.map +1 -1
  133. package/dist/core/constraints/context-builder.d.ts +11 -10
  134. package/dist/core/constraints/context-builder.d.ts.map +1 -1
  135. package/dist/core/constraints/context-builder.js +31 -27
  136. package/dist/core/constraints/context-builder.js.map +1 -1
  137. package/dist/core/constraints/context-reconcile.d.ts +75 -0
  138. package/dist/core/constraints/context-reconcile.d.ts.map +1 -0
  139. package/dist/core/constraints/context-reconcile.js +161 -0
  140. package/dist/core/constraints/context-reconcile.js.map +1 -0
  141. package/dist/core/constraints/doc-freshness/runner.d.ts +7 -0
  142. package/dist/core/constraints/doc-freshness/runner.d.ts.map +1 -1
  143. package/dist/core/constraints/doc-freshness/runner.js +8 -3
  144. package/dist/core/constraints/doc-freshness/runner.js.map +1 -1
  145. package/dist/core/constraints/git-evidence.js +1 -1
  146. package/dist/core/constraints/git-evidence.js.map +1 -1
  147. package/dist/core/constraints/injection-drift.d.ts +7 -2
  148. package/dist/core/constraints/injection-drift.d.ts.map +1 -1
  149. package/dist/core/constraints/injection-drift.js +13 -7
  150. package/dist/core/constraints/injection-drift.js.map +1 -1
  151. package/dist/core/constraints/run-env.d.ts +92 -0
  152. package/dist/core/constraints/run-env.d.ts.map +1 -0
  153. package/dist/core/constraints/run-env.js +175 -0
  154. package/dist/core/constraints/run-env.js.map +1 -0
  155. package/dist/core/constraints/triggers.d.ts +22 -0
  156. package/dist/core/constraints/triggers.d.ts.map +1 -0
  157. package/dist/core/constraints/triggers.js +31 -0
  158. package/dist/core/constraints/triggers.js.map +1 -0
  159. package/dist/core/effective-constraints.d.ts +13 -4
  160. package/dist/core/effective-constraints.d.ts.map +1 -1
  161. package/dist/core/effective-constraints.js +16 -6
  162. package/dist/core/effective-constraints.js.map +1 -1
  163. package/dist/core/effective-set.d.ts +8 -15
  164. package/dist/core/effective-set.d.ts.map +1 -1
  165. package/dist/core/effective-set.js +7 -12
  166. package/dist/core/effective-set.js.map +1 -1
  167. package/dist/core/index.d.ts +2 -1
  168. package/dist/core/index.d.ts.map +1 -1
  169. package/dist/core/index.js.map +1 -1
  170. package/dist/core/project-config-loader.d.ts +39 -17
  171. package/dist/core/project-config-loader.d.ts.map +1 -1
  172. package/dist/core/project-config-loader.js +51 -122
  173. package/dist/core/project-config-loader.js.map +1 -1
  174. package/dist/core/validators/check-handlers/http.d.ts +2 -2
  175. package/dist/core/validators/check-handlers/http.d.ts.map +1 -1
  176. package/dist/core/validators/check-handlers/http.js +12 -7
  177. package/dist/core/validators/check-handlers/http.js.map +1 -1
  178. package/dist/core/validators/index.d.ts +1 -1
  179. package/dist/core/validators/index.d.ts.map +1 -1
  180. package/dist/core/validators/passes-gate.d.ts.map +1 -1
  181. package/dist/core/validators/passes-gate.js +2 -2
  182. package/dist/core/validators/passes-gate.js.map +1 -1
  183. package/dist/failure/classifier.d.ts +1 -1
  184. package/dist/failure/classifier.d.ts.map +1 -1
  185. package/dist/failure/classifier.js +8 -8
  186. package/dist/failure/classifier.js.map +1 -1
  187. package/dist/failure/index.d.ts +2 -1
  188. package/dist/failure/index.d.ts.map +1 -1
  189. package/dist/failure/index.js +9 -17
  190. package/dist/failure/index.js.map +1 -1
  191. package/dist/failure/recorder.d.ts +1 -1
  192. package/dist/failure/recorder.d.ts.map +1 -1
  193. package/dist/gates/checker-gate.d.ts +2 -1
  194. package/dist/gates/checker-gate.d.ts.map +1 -1
  195. package/dist/gates/checker-gate.js +3 -2
  196. package/dist/gates/checker-gate.js.map +1 -1
  197. package/dist/gates/command.d.ts +10 -4
  198. package/dist/gates/command.d.ts.map +1 -1
  199. package/dist/gates/command.js +36 -62
  200. package/dist/gates/command.js.map +1 -1
  201. package/dist/gates/contract.d.ts.map +1 -1
  202. package/dist/gates/contract.js +0 -4
  203. package/dist/gates/contract.js.map +1 -1
  204. package/dist/gates/definitions.d.ts +2 -1
  205. package/dist/gates/definitions.d.ts.map +1 -1
  206. package/dist/gates/definitions.js +6 -5
  207. package/dist/gates/definitions.js.map +1 -1
  208. package/dist/gates/index.d.ts +0 -4
  209. package/dist/gates/index.d.ts.map +1 -1
  210. package/dist/gates/index.js +1 -6
  211. package/dist/gates/index.js.map +1 -1
  212. package/dist/gates/performance.d.ts.map +1 -1
  213. package/dist/gates/performance.js +0 -4
  214. package/dist/gates/performance.js.map +1 -1
  215. package/dist/gates/runner.d.ts +1 -1
  216. package/dist/gates/runner.js +1 -1
  217. package/dist/gates/security.d.ts +1 -1
  218. package/dist/gates/security.d.ts.map +1 -1
  219. package/dist/gates/security.js +5 -6
  220. package/dist/gates/security.js.map +1 -1
  221. package/dist/gates/types.d.ts +14 -17
  222. package/dist/gates/types.d.ts.map +1 -1
  223. package/dist/gates/types.js.map +1 -1
  224. package/dist/hooks/bootstrap.d.ts +6 -4
  225. package/dist/hooks/bootstrap.d.ts.map +1 -1
  226. package/dist/hooks/bootstrap.js +25 -13
  227. package/dist/hooks/bootstrap.js.map +1 -1
  228. package/dist/hooks/config.d.ts +6 -2
  229. package/dist/hooks/config.d.ts.map +1 -1
  230. package/dist/hooks/config.js +5 -1
  231. package/dist/hooks/config.js.map +1 -1
  232. package/dist/hooks/index.d.ts +1 -1
  233. package/dist/hooks/index.d.ts.map +1 -1
  234. package/dist/hooks/index.js +0 -15
  235. package/dist/hooks/index.js.map +1 -1
  236. package/dist/hooks/pipeline.d.ts.map +1 -1
  237. package/dist/hooks/pipeline.js +2 -4
  238. package/dist/hooks/pipeline.js.map +1 -1
  239. package/dist/hooks/registry.d.ts +11 -8
  240. package/dist/hooks/registry.d.ts.map +1 -1
  241. package/dist/hooks/registry.js +22 -7
  242. package/dist/hooks/registry.js.map +1 -1
  243. package/dist/hooks/types.d.ts +23 -7
  244. package/dist/hooks/types.d.ts.map +1 -1
  245. package/dist/index.d.ts +4 -4
  246. package/dist/index.d.ts.map +1 -1
  247. package/dist/index.js +3 -4
  248. package/dist/index.js.map +1 -1
  249. package/dist/knowledge/audit-scoring.d.ts +87 -0
  250. package/dist/knowledge/audit-scoring.d.ts.map +1 -0
  251. package/dist/knowledge/audit-scoring.js +470 -0
  252. package/dist/knowledge/audit-scoring.js.map +1 -0
  253. package/dist/knowledge/audit.d.ts +11 -59
  254. package/dist/knowledge/audit.d.ts.map +1 -1
  255. package/dist/knowledge/audit.js +47 -522
  256. package/dist/knowledge/audit.js.map +1 -1
  257. package/dist/knowledge/import.d.ts.map +1 -1
  258. package/dist/knowledge/import.js +2 -0
  259. package/dist/knowledge/import.js.map +1 -1
  260. package/dist/knowledge/index-generator.d.ts.map +1 -1
  261. package/dist/knowledge/index-generator.js +4 -5
  262. package/dist/knowledge/index-generator.js.map +1 -1
  263. package/dist/knowledge/index.d.ts +3 -2
  264. package/dist/knowledge/index.d.ts.map +1 -1
  265. package/dist/knowledge/index.js +3 -16
  266. package/dist/knowledge/index.js.map +1 -1
  267. package/dist/knowledge/ingest.js +1 -1
  268. package/dist/knowledge/ingest.js.map +1 -1
  269. package/dist/knowledge/lifecycle.d.ts.map +1 -1
  270. package/dist/knowledge/lifecycle.js +4 -2
  271. package/dist/knowledge/lifecycle.js.map +1 -1
  272. package/dist/knowledge/migration.d.ts +3 -0
  273. package/dist/knowledge/migration.d.ts.map +1 -1
  274. package/dist/knowledge/migration.js +8 -2
  275. package/dist/knowledge/migration.js.map +1 -1
  276. package/dist/knowledge/query.d.ts +6 -0
  277. package/dist/knowledge/query.d.ts.map +1 -1
  278. package/dist/knowledge/query.js +8 -2
  279. package/dist/knowledge/query.js.map +1 -1
  280. package/dist/knowledge/reference-tracker.d.ts.map +1 -1
  281. package/dist/knowledge/reference-tracker.js +4 -6
  282. package/dist/knowledge/reference-tracker.js.map +1 -1
  283. package/dist/knowledge/store.d.ts +22 -16
  284. package/dist/knowledge/store.d.ts.map +1 -1
  285. package/dist/knowledge/store.js +53 -12
  286. package/dist/knowledge/store.js.map +1 -1
  287. package/dist/knowledge/tree-walker.d.ts +19 -0
  288. package/dist/knowledge/tree-walker.d.ts.map +1 -0
  289. package/dist/knowledge/tree-walker.js +30 -0
  290. package/dist/knowledge/tree-walker.js.map +1 -0
  291. package/dist/knowledge/types.d.ts +16 -0
  292. package/dist/knowledge/types.d.ts.map +1 -1
  293. package/dist/monitoring/traces.d.ts +5 -3
  294. package/dist/monitoring/traces.d.ts.map +1 -1
  295. package/dist/monitoring/traces.js +19 -5
  296. package/dist/monitoring/traces.js.map +1 -1
  297. package/dist/test-setup/jsonl-fake-fs.d.ts +30 -0
  298. package/dist/test-setup/jsonl-fake-fs.d.ts.map +1 -0
  299. package/dist/test-setup/jsonl-fake-fs.js +49 -0
  300. package/dist/test-setup/jsonl-fake-fs.js.map +1 -0
  301. package/dist/tools/index.d.ts +4 -1
  302. package/dist/tools/index.d.ts.map +1 -1
  303. package/dist/tools/index.js +7 -15
  304. package/dist/tools/index.js.map +1 -1
  305. package/dist/types/checkpoint.d.ts +5 -0
  306. package/dist/types/checkpoint.d.ts.map +1 -1
  307. package/dist/types/passes-gate.d.ts +0 -13
  308. package/dist/types/passes-gate.d.ts.map +1 -1
  309. package/dist/types/project-config.d.ts +18 -0
  310. package/dist/types/project-config.d.ts.map +1 -1
  311. package/dist/types/trace.d.ts +13 -4
  312. package/dist/types/trace.d.ts.map +1 -1
  313. package/dist/types/trace.js +4 -3
  314. package/dist/types/trace.js.map +1 -1
  315. package/dist/utils/exec.d.ts +4 -27
  316. package/dist/utils/exec.d.ts.map +1 -1
  317. package/dist/utils/exec.js +4 -94
  318. package/dist/utils/exec.js.map +1 -1
  319. package/dist/utils/jsonl.d.ts +17 -7
  320. package/dist/utils/jsonl.d.ts.map +1 -1
  321. package/dist/utils/jsonl.js +144 -17
  322. package/dist/utils/jsonl.js.map +1 -1
  323. package/dist/utils/numeric-flag.d.ts +44 -0
  324. package/dist/utils/numeric-flag.d.ts.map +1 -0
  325. package/dist/utils/numeric-flag.js +62 -0
  326. package/dist/utils/numeric-flag.js.map +1 -0
  327. package/package.json +1 -1
  328. package/src/CONTEXT.md +5 -2
  329. package/src/__tests__/acceptance-gate.test.ts +7 -4
  330. package/src/__tests__/checker-extra.test.ts +3 -2
  331. package/src/__tests__/checker.test.ts +4 -3
  332. package/src/__tests__/checkpoint.test.ts +26 -9
  333. package/src/__tests__/context-files-resolution.test.ts +29 -15
  334. package/src/__tests__/contract-gate.test.ts +19 -40
  335. package/src/__tests__/error-classifier.test.ts +1 -1
  336. package/src/__tests__/exec.test.ts +45 -107
  337. package/src/__tests__/failure-recorder.test.ts +7 -5
  338. package/src/__tests__/hooks-pipeline.test.ts +34 -47
  339. package/src/__tests__/layering.test.ts +217 -25
  340. package/src/__tests__/passes-gate.test.ts +9 -4
  341. package/src/__tests__/performance-gate.test.ts +3 -15
  342. package/src/__tests__/project-config-loader-extra.test.ts +31 -57
  343. package/src/__tests__/project-config-loader.test.ts +6 -3
  344. package/src/__tests__/public-exports.test.ts +27 -6
  345. package/src/__tests__/public-type-surface.test.ts +360 -0
  346. package/src/__tests__/public-value-surface.test.ts +182 -0
  347. package/src/__tests__/review-gate.test.ts +4 -6
  348. package/src/__tests__/security-gate.test.ts +5 -20
  349. package/src/__tests__/skip-semantics.test.ts +5 -3
  350. package/src/__tests__/spec-validator.test.ts +3 -2
  351. package/src/__tests__/sub-barrels-explicit.test.ts +87 -0
  352. package/src/__tests__/test-fixture-hygiene.test.ts +53 -0
  353. package/src/__tests__/trace-analyzer-extra.test.ts +5 -3
  354. package/src/__tests__/trace-analyzer.test.ts +5 -3
  355. package/src/__tests__/trace-collector.test.ts +5 -3
  356. package/src/agents/index.ts +13 -2
  357. package/src/cli/__tests__/gate-command.test.ts +162 -0
  358. package/src/cli/commands/CONTEXT.md +29 -5
  359. package/src/cli/commands/__tests__/acceptance.test.ts +24 -25
  360. package/src/cli/commands/__tests__/bin-exit-mapping.test.ts +15 -0
  361. package/src/cli/commands/__tests__/check-drift.test.ts +42 -1
  362. package/src/cli/commands/__tests__/check-read-count.test.ts +167 -0
  363. package/src/cli/commands/__tests__/check-skip-output.test.ts +4 -1
  364. package/src/cli/commands/__tests__/check.test.ts +43 -36
  365. package/src/cli/commands/__tests__/command-gate-dead-options.test.ts +60 -0
  366. package/src/cli/commands/__tests__/command.test.ts +33 -17
  367. package/src/cli/commands/__tests__/constraints-retire.test.ts +52 -0
  368. package/src/cli/commands/__tests__/context-syncer.test.ts +141 -0
  369. package/src/cli/commands/__tests__/contract.test.ts +20 -20
  370. package/src/cli/commands/__tests__/failure.test.ts +5 -4
  371. package/src/cli/commands/__tests__/gate-decision.ts +30 -0
  372. package/src/cli/commands/__tests__/init-dead-options.test.ts +50 -0
  373. package/src/cli/commands/__tests__/init-governance-io.test.ts +139 -0
  374. package/src/cli/commands/__tests__/init-injection.test.ts +3 -3
  375. package/src/cli/commands/__tests__/init-ondisk.test.ts +867 -0
  376. package/src/cli/commands/__tests__/init.test.ts +82 -6
  377. package/src/cli/commands/__tests__/knowledge-audit-threshold.test.ts +146 -0
  378. package/src/cli/commands/__tests__/knowledge-flywheel-consistency.test.ts +1 -1
  379. package/src/cli/commands/__tests__/knowledge-tone-mapping.test.ts +244 -0
  380. package/src/cli/commands/__tests__/knowledge-view.test.ts +839 -0
  381. package/src/cli/commands/__tests__/knowledge.test.ts +33 -15
  382. package/src/cli/commands/__tests__/numeric-flag-assembly.test.ts +368 -0
  383. package/src/cli/commands/__tests__/passes-gate-dead-options.test.ts +89 -1
  384. package/src/cli/commands/__tests__/performance.test.ts +19 -21
  385. package/src/cli/commands/__tests__/posteval-plan.test.ts +5 -3
  386. package/src/cli/commands/__tests__/pre-push-hook.test.ts +181 -0
  387. package/src/cli/commands/__tests__/project-path-anchoring.test.ts +95 -2
  388. package/src/cli/commands/__tests__/project-path-convention.test.ts +103 -25
  389. package/src/cli/commands/__tests__/registry.test.ts +2 -0
  390. package/src/cli/commands/__tests__/report.test.ts +23 -1
  391. package/src/cli/commands/__tests__/review.test.ts +10 -17
  392. package/src/cli/commands/__tests__/scaffold.test.ts +505 -0
  393. package/src/cli/commands/__tests__/security-default-scan.test.ts +75 -0
  394. package/src/cli/commands/__tests__/security.test.ts +21 -24
  395. package/src/cli/commands/__tests__/spec-baseline-check.test.ts +3 -2
  396. package/src/cli/commands/__tests__/spec-baseline-index-cap.test.ts +131 -0
  397. package/src/cli/commands/__tests__/spec-baseline-read-count.test.ts +325 -0
  398. package/src/cli/commands/__tests__/sync-docs-agents.test.ts +19 -4
  399. package/src/cli/commands/__tests__/sync-docs-context-drift.test.ts +232 -0
  400. package/src/cli/commands/__tests__/sync-docs-module-order.test.ts +111 -0
  401. package/src/cli/commands/__tests__/sync-docs-read-count.test.ts +192 -0
  402. package/src/cli/commands/__tests__/sync-docs.test.ts +3 -2
  403. package/src/cli/commands/__tests__/validate.test.ts +1 -48
  404. package/src/cli/commands/acceptance.ts +24 -29
  405. package/src/cli/commands/check.ts +40 -17
  406. package/src/cli/commands/command.ts +9 -8
  407. package/src/cli/commands/constraints-retire.ts +57 -70
  408. package/src/cli/commands/contract.ts +44 -47
  409. package/src/cli/commands/definitions.ts +12 -6
  410. package/src/cli/commands/failure.ts +16 -3
  411. package/src/cli/commands/init.ts +242 -389
  412. package/src/cli/commands/knowledge-view.ts +224 -0
  413. package/src/cli/commands/knowledge.ts +643 -448
  414. package/src/cli/commands/passes-gate.ts +12 -3
  415. package/src/cli/commands/performance.ts +31 -38
  416. package/src/cli/commands/report.ts +3 -2
  417. package/src/cli/commands/review.ts +22 -28
  418. package/src/cli/commands/scaffold-templates.ts +406 -0
  419. package/src/cli/commands/scaffold.ts +302 -0
  420. package/src/cli/commands/security.ts +27 -23
  421. package/src/cli/commands/spec-baseline-check.ts +158 -33
  422. package/src/cli/commands/sync-docs/__tests__/context-export-surface.test.ts +102 -0
  423. package/src/cli/commands/sync-docs/__tests__/module-descriptions.test.ts +196 -0
  424. package/src/cli/commands/sync-docs/context-syncer.ts +137 -31
  425. package/src/cli/commands/sync-docs/index.ts +103 -28
  426. package/src/cli/commands/sync-docs/project-reader.ts +51 -7
  427. package/src/cli/commands/validate.ts +1 -104
  428. package/src/cli/gate-command.ts +87 -0
  429. package/src/completion-checkers/index.ts +10 -1
  430. package/src/context/CONTEXT.md +2 -1
  431. package/src/context/__tests__/knowledge-injector.test.ts +27 -0
  432. package/src/context/__tests__/session-manager.test.ts +19 -12
  433. package/src/context/knowledge-injector.ts +14 -7
  434. package/src/core/CONTEXT.md +10 -7
  435. package/src/core/__tests__/effective-set.test.ts +36 -60
  436. package/src/core/constraints/__tests__/context-reconcile.test.ts +246 -0
  437. package/src/core/constraints/__tests__/run-env.test.ts +377 -0
  438. package/src/core/constraints/__tests__/triggers.test.ts +70 -0
  439. package/src/core/constraints/agent-prompt-renderer.ts +1 -1
  440. package/src/core/constraints/capabilities-reconcile.ts +28 -4
  441. package/src/core/constraints/check-cache.ts +1 -1
  442. package/src/core/constraints/checker.ts +30 -14
  443. package/src/core/constraints/checkers/__tests__/context-doc-sync.test.ts +8 -3
  444. package/src/core/constraints/checkers/__tests__/governance-presence.test.ts +16 -7
  445. package/src/core/constraints/checkers/__tests__/no-hardcoded-credentials.test.ts +2 -1
  446. package/src/core/constraints/checkers/capability-sync.ts +27 -28
  447. package/src/core/constraints/checkers/context-doc-sync.ts +9 -7
  448. package/src/core/constraints/checkers/docs-freshness.ts +28 -33
  449. package/src/core/constraints/checkers/governance-presence.ts +10 -6
  450. package/src/core/constraints/checkers/types.ts +14 -8
  451. package/src/core/constraints/context-builder.ts +35 -26
  452. package/src/core/constraints/context-reconcile.ts +200 -0
  453. package/src/core/constraints/doc-freshness/__tests__/runner.test.ts +25 -0
  454. package/src/core/constraints/doc-freshness/runner.ts +18 -4
  455. package/src/core/constraints/git-evidence.ts +1 -1
  456. package/src/core/constraints/injection-drift.ts +19 -8
  457. package/src/core/constraints/run-env.ts +207 -0
  458. package/src/core/constraints/triggers.ts +33 -0
  459. package/src/core/effective-constraints.ts +17 -6
  460. package/src/core/effective-set.ts +8 -24
  461. package/src/core/index.ts +2 -1
  462. package/src/core/project-config-loader.ts +54 -102
  463. package/src/core/validators/__tests__/http-retry-sleep.test.ts +231 -0
  464. package/src/core/validators/check-handlers/http.ts +18 -7
  465. package/src/core/validators/index.ts +0 -1
  466. package/src/core/validators/passes-gate.ts +2 -3
  467. package/src/failure/CONTEXT.md +2 -1
  468. package/src/failure/classifier.ts +2 -2
  469. package/src/failure/index.ts +14 -2
  470. package/src/failure/recorder.ts +1 -1
  471. package/src/gates/CONTEXT.md +13 -7
  472. package/src/gates/__tests__/checker-gate.test.ts +1 -1
  473. package/src/gates/__tests__/command-single-predicate.test.ts +135 -0
  474. package/src/gates/__tests__/command.test.ts +8 -15
  475. package/src/gates/__tests__/contract.test.ts +5 -5
  476. package/src/gates/__tests__/gate-interface.test.ts +0 -17
  477. package/src/gates/__tests__/performance.test.ts +0 -13
  478. package/src/gates/__tests__/review.test.ts +0 -1
  479. package/src/gates/__tests__/runner.test.ts +1 -1
  480. package/src/gates/__tests__/security-default-scan.test.ts +97 -0
  481. package/src/gates/__tests__/security.test.ts +0 -13
  482. package/src/gates/checker-gate.ts +3 -2
  483. package/src/gates/command.ts +51 -74
  484. package/src/gates/contract.ts +0 -5
  485. package/src/gates/definitions.ts +6 -5
  486. package/src/gates/index.ts +0 -5
  487. package/src/gates/performance.ts +1 -6
  488. package/src/gates/runner.ts +1 -1
  489. package/src/gates/security.ts +9 -11
  490. package/src/gates/types.ts +20 -23
  491. package/src/hooks/CONTEXT.md +10 -9
  492. package/src/hooks/__tests__/bootstrap.test.ts +29 -6
  493. package/src/hooks/__tests__/pipeline.test.ts +28 -17
  494. package/src/hooks/__tests__/registry.test.ts +51 -3
  495. package/src/hooks/bootstrap.ts +35 -15
  496. package/src/hooks/config.ts +6 -2
  497. package/src/hooks/index.ts +10 -1
  498. package/src/hooks/pipeline.ts +4 -6
  499. package/src/hooks/registry.ts +31 -12
  500. package/src/hooks/types.ts +24 -7
  501. package/src/index.ts +1 -3
  502. package/src/knowledge/CONTEXT.md +14 -7
  503. package/src/knowledge/__tests__/audit-labels.test.ts +8 -5
  504. package/src/knowledge/__tests__/audit-scoring.test.ts +230 -0
  505. package/src/knowledge/__tests__/audit-write-count.test.ts +131 -0
  506. package/src/knowledge/__tests__/audit.test.ts +291 -208
  507. package/src/knowledge/__tests__/doctor.test.ts +3 -2
  508. package/src/knowledge/__tests__/ingest.test.ts +3 -2
  509. package/src/knowledge/__tests__/lifecycle.test.ts +57 -2
  510. package/src/knowledge/__tests__/migration.test.ts +35 -6
  511. package/src/knowledge/__tests__/query.test.ts +3 -2
  512. package/src/knowledge/__tests__/reference-tracker.test.ts +23 -2
  513. package/src/knowledge/__tests__/store.test.ts +149 -4
  514. package/src/knowledge/__tests__/tree-walker.test.ts +132 -0
  515. package/src/knowledge/audit-scoring.ts +589 -0
  516. package/src/knowledge/audit.ts +55 -599
  517. package/src/knowledge/import.ts +2 -0
  518. package/src/knowledge/index-generator.ts +4 -6
  519. package/src/knowledge/index.ts +27 -2
  520. package/src/knowledge/ingest.ts +1 -1
  521. package/src/knowledge/lifecycle.ts +5 -2
  522. package/src/knowledge/migration.ts +8 -2
  523. package/src/knowledge/query.ts +8 -1
  524. package/src/knowledge/reference-tracker.ts +5 -7
  525. package/src/knowledge/store.ts +75 -16
  526. package/src/knowledge/tree-walker.ts +29 -0
  527. package/src/knowledge/types.ts +19 -0
  528. package/src/monitoring/CONTEXT.md +5 -2
  529. package/src/monitoring/__tests__/context-tracker.test.ts +16 -8
  530. package/src/monitoring/__tests__/trace-file-anchoring.test.ts +80 -0
  531. package/src/monitoring/traces.ts +19 -5
  532. package/src/release/CONTEXT.md +1 -1
  533. package/src/release/__tests__/integrity.test.ts +21 -12
  534. package/src/test-setup/__tests__/jsonl-fake-fs.test.ts +98 -0
  535. package/src/test-setup/jsonl-fake-fs.ts +59 -0
  536. package/src/tools/index.ts +5 -1
  537. package/src/types/checkpoint.ts +6 -0
  538. package/src/types/passes-gate.ts +0 -17
  539. package/src/types/project-config.ts +21 -0
  540. package/src/types/trace.ts +14 -4
  541. package/src/utils/__tests__/file-walk.test.ts +3 -2
  542. package/src/utils/__tests__/jsonl-bounded-read.test.ts +106 -0
  543. package/src/utils/__tests__/jsonl-skip-disposition.test.ts +10 -6
  544. package/src/utils/__tests__/jsonl.test.ts +61 -14
  545. package/src/utils/__tests__/numeric-flag.test.ts +80 -0
  546. package/src/utils/__tests__/package-version.test.ts +2 -2
  547. package/src/utils/exec.ts +4 -64
  548. package/src/utils/jsonl.ts +159 -20
  549. package/src/utils/numeric-flag.ts +64 -0
  550. package/dist/failure/types.d.ts +0 -9
  551. package/dist/failure/types.d.ts.map +0 -1
  552. package/dist/failure/types.js +0 -25
  553. package/dist/failure/types.js.map +0 -1
  554. package/dist/gates/effective-gates.d.ts +0 -30
  555. package/dist/gates/effective-gates.d.ts.map +0 -1
  556. package/dist/gates/effective-gates.js +0 -81
  557. package/dist/gates/effective-gates.js.map +0 -1
  558. package/src/failure/types.ts +0 -9
  559. package/src/gates/__tests__/effective-gates.test.ts +0 -77
  560. package/src/gates/effective-gates.ts +0 -99
@@ -4,10 +4,12 @@
4
4
 
5
5
  import { security, auditDetails } from '../security';
6
6
  import { captureIO, type CapturingIO } from '../../command-contract';
7
+ import { decide, fakeGate } from './gate-decision';
7
8
  import { SecurityGate } from '../../../gates/security';
8
9
 
9
10
  jest.mock('../../../gates/security', () => ({
10
11
  SecurityGate: jest.fn().mockImplementation(() => ({
12
+ evaluate: jest.fn(),
11
13
  scan: jest.fn(),
12
14
  })),
13
15
  }));
@@ -33,12 +35,9 @@ describe('security command', () => {
33
35
 
34
36
  describe('security', () => {
35
37
  it('should print success when scan passes', async () => {
36
- const mockScan = jest.fn().mockResolvedValue({
37
- passed: true,
38
- message: 'no vulnerabilities',
39
- details: { critical: 0, high: 0, moderate: 0, low: 0 },
40
- });
41
- MockGate.mockImplementation(() => ({ scan: mockScan }) as any);
38
+ MockGate.mockImplementation(() =>
39
+ fakeGate(decide('security', true, 'no vulnerabilities', { critical: 0, high: 0, moderate: 0, low: 0 })) as any
40
+ );
42
41
 
43
42
  const result = await security({}, io);
44
43
 
@@ -47,34 +46,33 @@ describe('security command', () => {
47
46
  });
48
47
 
49
48
  it('should print failure and exit 1 when scan fails', async () => {
50
- const mockScan = jest.fn().mockResolvedValue({
51
- passed: false,
52
- message: 'vulnerabilities found',
53
- details: {
54
- critical: 1,
55
- high: 2,
56
- vulnerabilities: [
57
- { name: 'pkg-a', severity: 'critical', via: 'CVE-2026-0001' },
58
- { name: 'pkg-b', severity: 'high', via: 'CVE-2026-0002' },
59
- ],
60
- },
61
- });
62
- MockGate.mockImplementation(() => ({ scan: mockScan }) as any);
49
+ MockGate.mockImplementation(() =>
50
+ fakeGate(
51
+ decide('security', false, 'vulnerabilities found', {
52
+ critical: 1,
53
+ high: 2,
54
+ vulnerabilities: [
55
+ { name: 'pkg-a', severity: 'critical', via: 'CVE-2026-0001' },
56
+ { name: 'pkg-b', severity: 'high', via: 'CVE-2026-0002' },
57
+ ],
58
+ })
59
+ ) as any
60
+ );
63
61
 
64
62
  const result = await security({}, io);
65
63
 
66
64
  expect(io.outText()).toContain('安全门控检查失败');
65
+ expect(io.outText()).toContain(' 阈值: high');
67
66
  expect(io.outText()).toContain('pkg-a');
68
67
  expect(io.outText()).toContain('pkg-b');
69
68
  expect(result).toEqual({
70
69
  kind: 'fail',
71
- reason: 'security gate denied (threshold high): vulnerabilities found',
70
+ reason: 'security gate denied: vulnerabilities found',
72
71
  });
73
72
  });
74
73
 
75
74
  it('should default severity to high', async () => {
76
- const mockScan = jest.fn().mockResolvedValue({ passed: true, message: 'ok' });
77
- MockGate.mockImplementation(() => ({ scan: mockScan }) as any);
75
+ MockGate.mockImplementation(() => fakeGate(decide('security', true, 'ok')) as any);
78
76
 
79
77
  await security({}, io);
80
78
 
@@ -84,8 +82,7 @@ describe('security command', () => {
84
82
  });
85
83
 
86
84
  it('should use custom severity', async () => {
87
- const mockScan = jest.fn().mockResolvedValue({ passed: true, message: 'ok' });
88
- MockGate.mockImplementation(() => ({ scan: mockScan }) as any);
85
+ MockGate.mockImplementation(() => fakeGate(decide('security', true, 'ok')) as any);
89
86
 
90
87
  await security({ severity: 'critical' }, io);
91
88
 
@@ -5,6 +5,7 @@
5
5
  import * as fs from 'fs';
6
6
  import { captureIO, type CapturingIO } from '../../command-contract';
7
7
  import * as path from 'path';
8
+ import * as os from 'os';
8
9
  import { extractBaselineSection, specBaselineCheck } from '../spec-baseline-check';
9
10
 
10
11
  let io: CapturingIO;
@@ -13,11 +14,11 @@ beforeEach(() => {
13
14
  });
14
15
 
15
16
  describe('spec-baseline-check', () => {
16
- const tempDir = path.join(process.cwd(), 'temp-test-spec-baseline');
17
+ let tempDir: string;
17
18
  let originalExitCode: string | number | undefined;
18
19
 
19
20
  beforeAll(() => {
20
- fs.mkdirSync(tempDir, { recursive: true });
21
+ tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'temp-test-spec-baseline-'));
21
22
  });
22
23
 
23
24
  afterAll(() => {
@@ -0,0 +1,131 @@
1
+ /**
2
+ * spec-baseline-check 共享索引的驻留上界闸(#162,统一复审遗留项 3)
3
+ *
4
+ * #146 把 `sourceContents()` 改成全仓内容常驻一个 Map:排除面只有
5
+ * node_modules / dist / 隐藏目录与 .ts/.js 后缀,**无单文件字节、总量、条目上限**——
6
+ * 峰值内存由 O(单文件) 变 O(全仓源码),studio 级 monorepo 跑本命令会承担这个峰值,
7
+ * 而本仓 146 个源文件的规模测不出来。
8
+ *
9
+ * 裁决(票面修法二):索引加总量上限(条目数 + 字节数双闸),超限即放弃驻留、
10
+ * 回落逐文件流式读——峰值回到 O(单文件),正常规模仓保留「一次遍历」收益。
11
+ *
12
+ * 可观测判据 = 驻留与否的读取行为差:上限内每份源文件一次运行只读一遍
13
+ * (任意关键词重放不重读);超限后每个关键词都重新流式读一遍(证明没有偷偷驻留)。
14
+ *
15
+ * 反证(各改坏一次即红):
16
+ * - 摘掉上限常量 `SOURCE_INDEX_MAX_ENTRIES` / `SOURCE_INDEX_MAX_BYTES`
17
+ * → 缺省形参失去引用,tsc 直接红;
18
+ * - 删掉超限回落分支(超限也照常驻留)
19
+ * → 「超条目/字节上限」两例的「每关键词重读一遍」断言转红。
20
+ */
21
+
22
+ import { describe, it, expect, jest } from '@jest/globals';
23
+ import * as fs from 'fs';
24
+ import * as os from 'os';
25
+ import * as path from 'path';
26
+
27
+ // 计件挂 globalThis,形状照 `spec-baseline-read-count.test.ts`:
28
+ // jest.mock 工厂会被提前调用,闭包外提会撞 TDZ
29
+ jest.mock('fs', () => {
30
+ const actual = jest.requireActual<typeof import('fs')>('fs');
31
+ const g = globalThis as unknown as { __SBC162_READS__?: string[] };
32
+ if (!g.__SBC162_READS__) g.__SBC162_READS__ = [];
33
+ const bucket = g.__SBC162_READS__;
34
+ return {
35
+ ...actual,
36
+ readFileSync: jest.fn((p: unknown, ...rest: unknown[]) => {
37
+ bucket.push(Buffer.isBuffer(p) ? p.toString('utf8') : String(p));
38
+ return (actual.readFileSync as (...args: unknown[]) => unknown)(p, ...rest);
39
+ }),
40
+ };
41
+ });
42
+
43
+ import {
44
+ createBaselineIndex,
45
+ SOURCE_INDEX_MAX_ENTRIES,
46
+ SOURCE_INDEX_MAX_BYTES,
47
+ } from '../spec-baseline-check';
48
+
49
+ const reads = () =>
50
+ ((globalThis as unknown as { __SBC162_READS__?: string[] }).__SBC162_READS__ ||= []);
51
+
52
+ function write(dir: string, rel: string, content: string): void {
53
+ const target = path.join(dir, rel);
54
+ fs.mkdirSync(path.dirname(target), { recursive: true });
55
+ fs.writeFileSync(target, content, 'utf-8');
56
+ }
57
+
58
+ /** 4 份源文件:needleA 命中 2 份、needleB 命中 1 份、miss 命中 0 份 */
59
+ function fixtureProject(): string {
60
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'sbc-index-cap-'));
61
+ write(dir, 'src/a.ts', 'export const a = "needleA";\n');
62
+ write(dir, 'src/b.ts', 'export const b = "needleA needleB";\n');
63
+ write(dir, 'src/c.ts', 'export const c = "plain";\n');
64
+ write(dir, 'tools.js', 'module.exports = "plain";\n');
65
+ return dir;
66
+ }
67
+
68
+ /** dir 内 .ts/.js 源文件被 readFileSync 读到的总次数 */
69
+ function sourceReadCount(dir: string): number {
70
+ return reads().filter((file) => {
71
+ const rel = path.relative(dir, file);
72
+ return rel && !rel.startsWith('..') && (rel.endsWith('.ts') || rel.endsWith('.js'));
73
+ }).length;
74
+ }
75
+
76
+ describe('spec-baseline-check 共享索引的驻留上界(#162)', () => {
77
+ let dir: string;
78
+
79
+ beforeEach(() => {
80
+ // 原地清空:mock 工厂在装载时就 captured 了引用,换新数组计数会落到旧的去向(假绿)
81
+ reads().length = 0;
82
+ dir = fixtureProject();
83
+ });
84
+
85
+ afterEach(() => {
86
+ fs.rmSync(dir, { recursive: true, force: true });
87
+ });
88
+
89
+ it('上限常量存在且为正数(摘掉常量 → tsc 红;归零 → 下两例的口径全变)', () => {
90
+ expect(SOURCE_INDEX_MAX_ENTRIES).toBeGreaterThan(0);
91
+ expect(SOURCE_INDEX_MAX_BYTES).toBeGreaterThan(0);
92
+ });
93
+
94
+ it('上限内:驻留索引重放——每份源文件一次运行只读一遍,任意关键词不触发重读', () => {
95
+ const index = createBaselineIndex(dir);
96
+
97
+ expect(index.countFilesContaining('needleA')).toBe(2);
98
+ expect(index.countFilesContaining('needleB')).toBe(1);
99
+ expect(index.countFilesContaining('miss')).toBe(0);
100
+ // 三个关键词跑完,4 份源文件总共只读了 4 次(驻留重放,改前逐关键词重读 = 12)
101
+ expect(sourceReadCount(dir)).toBe(4);
102
+ });
103
+
104
+ it('超条目上限:放弃驻留、回落逐文件流式读——每个关键词重读一遍,判定不变', () => {
105
+ const index = createBaselineIndex(dir, {
106
+ maxEntries: 2, // 夹具 4 份源文件 > 2
107
+ maxBytes: SOURCE_INDEX_MAX_BYTES,
108
+ });
109
+
110
+ expect(index.countFilesContaining('needleA')).toBe(2);
111
+ const afterFirst = sourceReadCount(dir);
112
+ // 首个关键词额外付一次「建到上限即弃」的中止尝试(读取 ≤ 上限 + 1 份)+ 一遍流式全扫
113
+ expect(afterFirst).toBeLessThanOrEqual(2 + 1 + 4);
114
+
115
+ expect(index.countFilesContaining('needleB')).toBe(1);
116
+ // 无驻留的证据:第二个关键词仍重新流式读完全仓(驻留着此处增量 = 0)
117
+ expect(sourceReadCount(dir) - afterFirst).toBe(4);
118
+ });
119
+
120
+ it('超字节上限:同样回落流式读', () => {
121
+ const index = createBaselineIndex(dir, {
122
+ maxEntries: SOURCE_INDEX_MAX_ENTRIES,
123
+ maxBytes: 10, // 任意一份文件就超过
124
+ });
125
+
126
+ expect(index.countFilesContaining('needleA')).toBe(2);
127
+ const afterFirst = sourceReadCount(dir);
128
+ expect(index.countFilesContaining('miss')).toBe(0);
129
+ expect(sourceReadCount(dir) - afterFirst).toBe(4);
130
+ });
131
+ });
@@ -0,0 +1,325 @@
1
+ /**
2
+ * 一次 spec-baseline-check 运行内的读取计数闸(#146,母票 #140 A 票)
3
+ *
4
+ * 形状照 `check-read-count.test.ts`(ADR-0023 决策 5 ①):真执行、只在中间记一笔,
5
+ * 不改任何行为。数的是**底层文件读取发生了几次**,不是观察口被请求几次。
6
+ *
7
+ * 夹具必须让三条验证路径(文件存在性 / 依赖 / 代码模式)都真评估,否则闸门是空跑的:
8
+ * `package.json 文件存在` 走第一条,`依赖 … 已安装` 走第二条,`… 已实现` 走第三条。
9
+ *
10
+ * 本票的承诺面 = 全仓内容扫描与 `package.json` 解析各「一次运行一遍」,与前置条件条数
11
+ * (N)、每条的关键词数(K)无关。故除整表冻结外,另有一条 N×K 缩放不变式(第三例)。
12
+ */
13
+
14
+ import * as fs from 'fs';
15
+ import * as os from 'os';
16
+ import * as path from 'path';
17
+ import { specBaselineCheck } from '../spec-baseline-check';
18
+ import { captureIO, type CapturingIO } from '../../command-contract';
19
+
20
+ type ReadCall = { file: string; via: 'readFileSync' | 'open' | 'readdir' | 'exists' };
21
+
22
+ // 计数器挂在 globalThis:jest.mock 工厂会被提前调用,闭包外提会撞 TDZ
23
+ jest.mock('fs', () => {
24
+ const actual = jest.requireActual<typeof import('fs')>('fs');
25
+ const g = globalThis as unknown as {
26
+ __SBC146_READS__?: ReadCall[];
27
+ __SBC146_DIRS__?: string[];
28
+ };
29
+ if (!g.__SBC146_READS__) g.__SBC146_READS__ = [];
30
+ if (!g.__SBC146_DIRS__) g.__SBC146_DIRS__ = [];
31
+ const bucket = g.__SBC146_READS__;
32
+ const dirs = g.__SBC146_DIRS__;
33
+ const name = (p: unknown): string => (Buffer.isBuffer(p) ? p.toString('utf8') : String(p));
34
+ const passthrough = (fn: string) =>
35
+ (actual as unknown as Record<string, (...args: unknown[]) => unknown>)[fn];
36
+
37
+ return {
38
+ ...actual,
39
+ readFileSync: jest.fn((p: unknown, ...rest: unknown[]) => {
40
+ bucket.push({ file: name(p), via: 'readFileSync' });
41
+ return passthrough('readFileSync')(p, ...rest);
42
+ }),
43
+ // 只读打开也算一次读取(写操作不计)
44
+ openSync: jest.fn((p: unknown, flags?: unknown, ...rest: unknown[]) => {
45
+ if (flags === undefined || flags === 'r' || flags === 'rs') {
46
+ bucket.push({ file: name(p), via: 'open' });
47
+ }
48
+ return passthrough('openSync')(p, flags, ...rest);
49
+ }),
50
+ // 存在性探测(stat 口径)单列一条:同一落点在一次运行内问两次也只问一次
51
+ existsSync: jest.fn((p: unknown, ...rest: unknown[]) => {
52
+ bucket.push({ file: name(p), via: 'exists' });
53
+ return passthrough('existsSync')(p, ...rest);
54
+ }),
55
+ // 目录列举单独计件:全仓 walk 的次数就是「扫描发生了几遍」的口径
56
+ readdirSync: jest.fn((p: unknown, ...rest: unknown[]) => {
57
+ dirs.push(name(p));
58
+ return passthrough('readdirSync')(p, ...rest);
59
+ }),
60
+ };
61
+ });
62
+
63
+ const reads = () =>
64
+ ((globalThis as unknown as { __SBC146_READS__?: ReadCall[] }).__SBC146_READS__ ||= []);
65
+ const dirs = () =>
66
+ ((globalThis as unknown as { __SBC146_DIRS__?: string[] }).__SBC146_DIRS__ ||= []);
67
+
68
+ function write(dir: string, rel: string, content: string): void {
69
+ const target = path.join(dir, rel);
70
+ fs.mkdirSync(path.dirname(target), { recursive: true });
71
+ fs.writeFileSync(target, content, 'utf-8');
72
+ }
73
+
74
+ /** 源码 6 份(walk 口径内)+ 三份必须被跳过的同名内容 + package.json 与 spec 落点 */
75
+ function fixtureProject(): string {
76
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'sbc-read-count-'));
77
+ write(
78
+ dir,
79
+ 'package.json',
80
+ JSON.stringify({
81
+ name: 'sbc-fixture',
82
+ dependencies: { 'left-pad': '^1.0.0' },
83
+ devDependencies: { '@types/right-pad': '^1.0.0' },
84
+ })
85
+ );
86
+ write(dir, 'src/a.ts', 'export const a = "alphaOne";\nexport const a2 = "alphaOne again";\n');
87
+ write(dir, 'src/b.ts', 'export const b = "betaTwo";\n');
88
+ write(dir, 'src/c.ts', 'export const c = "gammaThree";\n');
89
+ write(dir, 'src/d.ts', 'export const d = "zetaSix";\n');
90
+ write(dir, 'src/nested/e.ts', 'export const e = "alphaOne";\n');
91
+ write(dir, 'tools.js', 'module.exports = "epsilonFive";\n');
92
+ // 以下三处内容与上面的关键词重叠,但都在 walk 的跳过名单里(node_modules / dist / 隐藏目录)
93
+ write(dir, 'dist/ignored.ts', 'export const x = "alphaOne betaTwo gammaThree";\n');
94
+ write(dir, 'node_modules/pkg/index.ts', 'export const x = "alphaOne betaTwo gammaThree";\n');
95
+ write(dir, '.harness/hidden.ts', 'export const x = "alphaOne betaTwo gammaThree";\n');
96
+ // 只活在 node_modules 里的包:依赖检查的兜底落点(walk 不读它的内容,但存在性要 stat)
97
+ fs.mkdirSync(path.join(dir, 'node_modules', 'ghost-pkg'), { recursive: true });
98
+
99
+ // 三条验证路径全覆盖,并留一条不满足项(deltaFour 全仓无)→ 退出码非零
100
+ write(
101
+ dir,
102
+ 'spec.md',
103
+ [
104
+ '# Fixture Spec',
105
+ '',
106
+ '## Baseline',
107
+ '- `package.json` 文件存在',
108
+ '- `package.json` 文件存在(再确认)',
109
+ '- 依赖 `left-pad` 已安装',
110
+ '- 依赖 `right-pad` 已安装',
111
+ '- `alphaOne` 与 `betaTwo` 已实现',
112
+ '- `gammaThree` 与 `deltaFour` 已完成',
113
+ '- `epsilonFive` 与 `zetaSix` implemented',
114
+ '',
115
+ '## Details',
116
+ '- 不该被提取',
117
+ ].join('\n')
118
+ );
119
+ // N×K 缩放对照:同样的取数口径,前置条件 2 条 / 关键词 1 个
120
+ write(
121
+ dir,
122
+ 'spec-small.md',
123
+ ['# Small', '## Baseline', '- 依赖 `left-pad` 已安装', '- `alphaOne` 已实现'].join('\n')
124
+ );
125
+ // ...与前置条件 7 条 / 关键词 15 个(含三个全仓不存在的)
126
+ write(
127
+ dir,
128
+ 'spec-big.md',
129
+ [
130
+ '# Big',
131
+ '## Baseline',
132
+ '- 依赖 `left-pad` 已安装',
133
+ '- 依赖 `right-pad` 已安装',
134
+ '- `alphaOne` 与 `betaTwo` 与 `gammaThree` 已实现',
135
+ '- `zetaSix` 与 `epsilonFive` 与 `deltaFour` 已完成',
136
+ '- `alphaOne` 与 `betaTwo` 与 `zetaSix` implemented',
137
+ '- `gammaThree` 与 `epsilonFive` 与 `missingOne` 已存在',
138
+ '- `deltaFour` 与 `missingTwo` 与 `missingThree` 已实现',
139
+ ].join('\n')
140
+ );
141
+ return dir;
142
+ }
143
+
144
+ describe('一次 spec-baseline-check 的读取计数闸(#146)', () => {
145
+ let io: CapturingIO;
146
+ let dir: string;
147
+
148
+ /** 相对项目根的**文件**名 → 内容读取次数(readFileSync 与只读 open 各算一次) */
149
+ function contentCounts(): Map<string, number> {
150
+ const counts = new Map<string, number>();
151
+ for (const call of reads()) {
152
+ if (call.via === 'readdir' || call.via === 'exists') continue;
153
+ const rel = path.relative(dir, call.file);
154
+ if (!rel || rel.startsWith('..')) continue; // 非本项目文件(模块加载等)不计
155
+ counts.set(rel, (counts.get(rel) ?? 0) + 1);
156
+ }
157
+ return counts;
158
+ }
159
+
160
+ /** 相对项目根的路径 → 存在性探测次数 */
161
+ function statCounts(): Map<string, number> {
162
+ const counts = new Map<string, number>();
163
+ for (const call of reads()) {
164
+ if (call.via !== 'exists') continue;
165
+ const rel = path.relative(dir, call.file);
166
+ if (!rel || rel.startsWith('..')) continue;
167
+ counts.set(rel, (counts.get(rel) ?? 0) + 1);
168
+ }
169
+ return counts;
170
+ }
171
+
172
+ /** 相对项目根的目录名 → readdirSync 次数(全仓扫描遍历过的目录) */
173
+ function dirCounts(): Map<string, number> {
174
+ const counts = new Map<string, number>();
175
+ for (const call of dirs()) {
176
+ const rel = path.relative(dir, call);
177
+ if (!rel) {
178
+ counts.set('.', (counts.get('.') ?? 0) + 1);
179
+ } else if (!rel.startsWith('..')) {
180
+ counts.set(rel, (counts.get(rel) ?? 0) + 1);
181
+ }
182
+ }
183
+ return counts;
184
+ }
185
+
186
+ function reset(): void {
187
+ // 原地清空:mock 工厂在装载时就 captured 了这些引用,换新数组计数会落到旧的去向(假绿)
188
+ reads().length = 0;
189
+ dirs().length = 0;
190
+ }
191
+
192
+ beforeEach(() => {
193
+ reset();
194
+ io = captureIO();
195
+ dir = fixtureProject();
196
+ });
197
+
198
+ afterEach(() => {
199
+ fs.rmSync(dir, { recursive: true, force: true });
200
+ });
201
+
202
+ it('整张读取表如实冻结:本次运行读了哪些项目文件、各读几次', async () => {
203
+ const result = await specBaselineCheck(path.join(dir, 'spec.md'), { json: true, projectPath: dir }, io);
204
+ expect(result.kind).toBe('fail');
205
+
206
+ // 正对照:三条验证路径都真评估了(否则下面的表是空跑)
207
+ const results = JSON.parse(io.outText()) as Array<{ prerequisite: string; evidence: string }>;
208
+ const evidence = results.map((r) => r.evidence).join('\n');
209
+ expect(evidence).toContain('文件存在: package.json');
210
+ expect(evidence).toContain('依赖已安装: left-pad');
211
+ expect(evidence).toContain('代码中找到: alphaOne (2 files), betaTwo (1 files)');
212
+ expect(evidence).toContain('找到: gammaThree (1 files); 未找到: deltaFour');
213
+
214
+ expect([...contentCounts().entries()].sort()).toEqual(
215
+ [
216
+ // —— 一次运行一份:本票的承诺面 ——
217
+ ['package.json', 1], // 两条依赖前置共用同一份解析(改前 = 2)
218
+ ['spec.md', 1], // 命令入口读 spec 本身
219
+ ['src/a.ts', 1],
220
+ ['src/b.ts', 1],
221
+ ['src/c.ts', 1],
222
+ ['src/d.ts', 1],
223
+ ['src/nested/e.ts', 1],
224
+ ['tools.js', 1], // 表上无 dist/、node_modules/、.harness/ 条目 = 跳过口径未变
225
+ ].sort()
226
+ );
227
+
228
+ // 同一落点的存在性探测:两条前置问 `package.json` 是否在世,只 stat 一次(改前 = 2)
229
+ expect([...statCounts().entries()].sort()).toEqual(
230
+ [
231
+ ['package.json', 1],
232
+ ['spec.md', 1],
233
+ ].sort()
234
+ );
235
+
236
+ // 全仓 walk 只发生一遍(3 个目录各列举一次)
237
+ expect([...dirCounts().entries()].sort()).toEqual(
238
+ [['.', 1], ['src', 1], [path.join('src', 'nested'), 1]].sort()
239
+ );
240
+ });
241
+
242
+ it('除上表点名条目外,项目文件一次运行内一律至多读一次', async () => {
243
+ await specBaselineCheck(path.join(dir, 'spec.md'), { json: true, projectPath: dir }, io);
244
+
245
+ const offenders = [...contentCounts()].filter(([, n]) => n > 1);
246
+ expect(offenders).toEqual([]);
247
+ });
248
+
249
+ it('读取次数与前置条件条数、关键词数无关(N×K → 常数)', async () => {
250
+ await specBaselineCheck(path.join(dir, 'spec-small.md'), { json: true, projectPath: dir }, io);
251
+ const small = new Map(contentCounts());
252
+ const smallDirs = [...dirCounts().entries()].sort();
253
+ const smallReadTotal = small.get('spec-small.md');
254
+
255
+ // 正对照:big 的前置条件确实更多(2 条 vs 7 条、关键词 1 个 vs 15 个)
256
+ expect((JSON.parse(io.outText()) as unknown[]).length).toBe(2);
257
+ const ioBig = captureIO();
258
+ reset();
259
+ await specBaselineCheck(path.join(dir, 'spec-big.md'), { json: true, projectPath: dir }, ioBig);
260
+ const big = new Map(contentCounts());
261
+ const bigDirs = [...dirCounts().entries()].sort();
262
+ expect((JSON.parse(ioBig.outText()) as unknown[]).length).toBe(7);
263
+
264
+ // 同一份数据的同一口径:把 spec 自身那次读取剔掉后,两张表逐条相同
265
+ small.delete('spec-small.md');
266
+ big.delete('spec-big.md');
267
+ expect([...big.entries()].sort()).toEqual([...small.entries()].sort());
268
+ expect(smallReadTotal).toBe(1);
269
+
270
+ // 扫描口径同样与条数无关:15 个关键词仍在同一遍 walk 上重放
271
+ expect(bigDirs).toEqual(smallDirs);
272
+ expect(big.get('package.json')).toBe(1);
273
+ });
274
+
275
+ it('package.json 读取失败态同样入库:两条依赖前置只尝试读一次', async () => {
276
+ fs.rmSync(path.join(dir, 'package.json'));
277
+
278
+ await specBaselineCheck(path.join(dir, 'spec.md'), { json: true, projectPath: dir }, io);
279
+
280
+ // 判定照旧:读不到就是「无法读取 package.json」,每条前置各报一次
281
+ const results = JSON.parse(io.outText()) as Array<{ satisfied: boolean; evidence: string }>;
282
+ const failures = results.filter((r) => r.evidence === '无法读取 package.json');
283
+ expect(failures).toHaveLength(2);
284
+ expect(failures.every((r) => !r.satisfied)).toBe(true);
285
+
286
+ // 但失败的那次尝试只发生一遍(改前 = 2)
287
+ expect(contentCounts().get('package.json')).toBe(1);
288
+ });
289
+
290
+ it('只走依赖路径的前置不触发全仓扫描,node_modules 兜底照旧生效', async () => {
291
+ write(
292
+ dir,
293
+ 'spec-dep-only.md',
294
+ ['# Deps Only', '## Baseline', '- 依赖 `ghost-pkg` 与 `nowhere-pkg` 已安装'].join('\n')
295
+ );
296
+ reset();
297
+
298
+ const result = await specBaselineCheck(
299
+ path.join(dir, 'spec-dep-only.md'),
300
+ { json: true, projectPath: dir },
301
+ io
302
+ );
303
+
304
+ // 判定照旧:一个活在 node_modules 里、一个两处都无 → 部分存在
305
+ expect(result.kind).toBe('fail');
306
+ expect(JSON.parse(io.outText())[0].evidence).toBe('已安装: ghost-pkg; 缺失: nowhere-pkg');
307
+
308
+ // 懒建:没有任何前置要读源码内容,全仓一次都没扫(改前也是 0,此条守的是「别把它改成 eager」)
309
+ expect([...contentCounts().entries()].sort()).toEqual(
310
+ [
311
+ ['package.json', 1],
312
+ ['spec-dep-only.md', 1],
313
+ ].sort()
314
+ );
315
+ expect(dirCounts().size).toBe(0);
316
+ // 兜底的存在性探测确实发生,且同一落点只 stat 一次
317
+ expect([...statCounts().entries()].sort()).toEqual(
318
+ [
319
+ [path.join('node_modules', 'ghost-pkg'), 1],
320
+ [path.join('node_modules', 'nowhere-pkg'), 1],
321
+ ['spec-dep-only.md', 1],
322
+ ].sort()
323
+ );
324
+ });
325
+ });
@@ -5,18 +5,33 @@
5
5
  import * as fs from 'fs';
6
6
  import { captureIO, type CapturingIO } from '../../command-contract';
7
7
  import * as path from 'path';
8
+ import * as os from 'os';
8
9
  import { syncDocs } from '../sync-docs';
9
10
 
11
+ /**
12
+ * 环境固定:CONTEXT.md 的 mtime 过时判定在 CI 下被有意关掉(#142——全新 checkout 的 mtime
13
+ * 由 clone 顺序决定,比出来的「过时」是假灯)。本文件的夹具都是现场写的、断言的是本地行为,
14
+ * 所以环境必须由用例自己声明,不能跟着跑测试的机器走(否则同一份代码在开发机绿、在 CI 红)。
15
+ * 「CI 下关掉 mtime 判定」这条规则本身由 sync-docs-context-drift.test.ts 负责验证。
16
+ */
17
+ const SAVED_CI = process.env.CI;
18
+
10
19
  let io: CapturingIO;
11
20
  beforeEach(() => {
12
21
  io = captureIO();
22
+ delete process.env.CI;
23
+ });
24
+
25
+ afterEach(() => {
26
+ if (SAVED_CI === undefined) delete process.env.CI;
27
+ else process.env.CI = SAVED_CI;
13
28
  });
14
29
 
15
30
  describe('sync-docs --agents', () => {
16
- const tempDir = path.join(process.cwd(), 'temp-test-sync-docs-agents');
31
+ let tempDir: string;
17
32
 
18
33
  beforeAll(() => {
19
- fs.mkdirSync(tempDir, { recursive: true });
34
+ tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'temp-test-sync-docs-agents-'));
20
35
  });
21
36
 
22
37
  afterAll(() => {
@@ -489,11 +504,11 @@ describe('sync-docs --agents', () => {
489
504
  expect(parsed.resolution.some((r: { action: string }) => r.action === 'update-context-md')).toBe(true);
490
505
  expect(parsed.agentsMd).toEqual({ file: 'AGENTS.md', exists: false, stale: true });
491
506
 
492
- // 人读模式:输出过时提示
507
+ // 人读模式:mtime 差异只给提示(harness#142 降级),判失败的是缺失
493
508
  io = captureIO();
494
509
  const humanResult = await syncDocs({ projectPath: testDir, agents: true, check: true }, io);
495
510
  expect(humanResult).toEqual({ kind: 'fail', reason: expect.stringContaining('文档不是最新') });
496
- expect(io.outText()).toContain('可能过时');
511
+ expect(io.outText()).toContain('源码比文档新');
497
512
  expect(io.outText()).toContain('缺少 CONTEXT.md');
498
513
 
499
514
  fs.rmSync(testDir, { recursive: true, force: true });