@aibyzero/cm-workflow 0.16.4 → 0.16.6

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 (283) hide show
  1. package/.codex-plugin/plugin.json +1 -1
  2. package/AGENTS.md +22 -0
  3. package/CHANGELOG.md +57 -0
  4. package/README.md +22 -4
  5. package/THIRD_PARTY_NOTICES.md +9 -0
  6. package/VERSION +1 -1
  7. package/docs/architecture.md +43 -0
  8. package/docs/execution-optimizations.md +33 -0
  9. package/docs/external-models.md +114 -0
  10. package/docs/fixes/.reviews/20260929-bootstrap-unchanged-review-r1.md +7 -0
  11. package/docs/fixes/.reviews/20260930-develop-package-limit-recovery-r4.md +9 -0
  12. package/docs/fixes/20260929-bootstrap-rule-refresh-conflict.md +22 -0
  13. package/docs/fixes/20260929-bootstrap-unchanged-review.md +26 -0
  14. package/docs/fixes/20260930-develop-package-limit-recovery.md +65 -0
  15. package/docs/human-correction.md +31 -0
  16. package/docs/installation.md +48 -7
  17. package/docs/js-workflow-control.md +293 -33
  18. package/docs/live-evidence-drivers.md +62 -0
  19. package/docs/next-steps.md +15 -10
  20. package/docs/runtime-failover.md +1 -1
  21. package/docs/untested-branches.md +27 -4
  22. package/docs/user-guide.md +23 -8
  23. package/install-codex.sh +28 -11
  24. package/package.json +1 -1
  25. package/runtime/js/cm-ai/accepted-fix.mjs +23 -5
  26. package/runtime/js/cm-ai/bootstrap-feature.mjs +15 -0
  27. package/runtime/js/cm-ai/claude-review-adapter.mjs +7 -4
  28. package/runtime/js/cm-ai/claude-review-stream.mjs +12 -0
  29. package/runtime/js/cm-ai/claude-tool-preview.mjs +4 -4
  30. package/runtime/js/cm-ai/cm-ai-admission.mjs +115 -41
  31. package/runtime/js/cm-ai/cm-ai-context-refresh.mjs +3 -2
  32. package/runtime/js/cm-ai/cm-ai-conversation-entry.mjs +208 -34
  33. package/runtime/js/cm-ai/cm-ai-learning-handoff-writer.mjs +11 -9
  34. package/runtime/js/cm-ai/cm-ai-learning-writer.mjs +22 -5
  35. package/runtime/js/cm-ai/cm-ai-qa-log.mjs +181 -57
  36. package/runtime/js/cm-ai/cm-ai-run-finalizer.mjs +15 -58
  37. package/runtime/js/cm-ai/codex-config.mjs +3 -2
  38. package/runtime/js/cm-ai/codex-review-adapter.mjs +56 -17
  39. package/runtime/js/cm-ai/committed-rule-basis.mjs +76 -0
  40. package/runtime/js/cm-ai/developer-adapter.mjs +6 -2
  41. package/runtime/js/cm-ai/diagnostic-reason.mjs +10 -0
  42. package/runtime/js/cm-ai/drive-bootstrap.mjs +315 -0
  43. package/runtime/js/cm-ai/drive-core.mjs +47 -9
  44. package/runtime/js/cm-ai/durable-runner-state.mjs +413 -56
  45. package/runtime/js/cm-ai/effect-abandon-log.mjs +19 -0
  46. package/runtime/js/cm-ai/effect-contract.mjs +44 -2
  47. package/runtime/js/cm-ai/execution-policy.mjs +58 -0
  48. package/runtime/js/cm-ai/execution-store-limits.mjs +18 -0
  49. package/runtime/js/cm-ai/execution-store.mjs +6 -4
  50. package/runtime/js/cm-ai/external-group-models.mjs +53 -0
  51. package/runtime/js/cm-ai/external-model-launch.mjs +23 -0
  52. package/runtime/js/cm-ai/external-models.mjs +52 -0
  53. package/runtime/js/cm-ai/external-run-guard.mjs +96 -0
  54. package/runtime/js/cm-ai/fix-code-association.mjs +171 -18
  55. package/runtime/js/cm-ai/host-bootstrap.mjs +126 -29
  56. package/runtime/js/cm-ai/host-check.mjs +30 -4
  57. package/runtime/js/cm-ai/host-conversation-execution.mjs +66 -20
  58. package/runtime/js/cm-ai/host-handoff.mjs +13 -6
  59. package/runtime/js/cm-ai/host-progress.mjs +80 -0
  60. package/runtime/js/cm-ai/host-project-execution.mjs +16 -11
  61. package/runtime/js/cm-ai/host-qa-executor.mjs +23 -9
  62. package/runtime/js/cm-ai/host-qa-fix-owner.mjs +23 -6
  63. package/runtime/js/cm-ai/host-qa-policy.mjs +16 -7
  64. package/runtime/js/cm-ai/host-session.mjs +12 -4
  65. package/runtime/js/cm-ai/host-tool-bridge.mjs +1 -1
  66. package/runtime/js/cm-ai/host-workflow-capabilities.mjs +16 -5
  67. package/runtime/js/cm-ai/host.mjs +1 -1
  68. package/runtime/js/cm-ai/knowledge-closeout.mjs +56 -0
  69. package/runtime/js/cm-ai/launch-mismatch.mjs +42 -0
  70. package/runtime/js/cm-ai/legacy-model-run-preview.mjs +24 -0
  71. package/runtime/js/cm-ai/legacy-model-settings.mjs +59 -0
  72. package/runtime/js/cm-ai/live-evidence.mjs +96 -0
  73. package/runtime/js/cm-ai/log-rows.mjs +21 -0
  74. package/runtime/js/cm-ai/model-configuration-file.mjs +63 -0
  75. package/runtime/js/cm-ai/model_configuration_write.py +214 -0
  76. package/runtime/js/cm-ai/native-usage-log.mjs +26 -0
  77. package/runtime/js/cm-ai/operator-guidance.mjs +86 -0
  78. package/runtime/js/cm-ai/owned-process-cleanup.mjs +22 -0
  79. package/runtime/js/cm-ai/project-qa-gate.mjs +84 -0
  80. package/runtime/js/cm-ai/provider-review-observation.mjs +68 -9
  81. package/runtime/js/cm-ai/provider-usage.mjs +39 -0
  82. package/runtime/js/cm-ai/qa-config-revision.mjs +44 -10
  83. package/runtime/js/cm-ai/review-abandon-log.mjs +21 -0
  84. package/runtime/js/cm-ai/review-package.mjs +438 -31
  85. package/runtime/js/cm-ai/review-presentation.mjs +45 -0
  86. package/runtime/js/cm-ai/review-reconciliation.mjs +20 -0
  87. package/runtime/js/cm-ai/review-runner.mjs +47 -1
  88. package/runtime/js/cm-ai/reviewed-deliveries.mjs +97 -0
  89. package/runtime/js/cm-ai/reviewed-evidence-supersede.mjs +104 -10
  90. package/runtime/js/cm-ai/reviewed-evidence-supersession-record.mjs +42 -3
  91. package/runtime/js/cm-ai/specification-material.mjs +154 -12
  92. package/runtime/js/cm-ai/status-projection.mjs +93 -0
  93. package/runtime/js/cm-ai/task-commit.mjs +2 -2
  94. package/runtime/js/cm-ai/task-runner.mjs +658 -75
  95. package/runtime/js/cm-ai/tool-preview.mjs +8 -4
  96. package/runtime/js/cm-ai/worker-claude-developer.mjs +16 -7
  97. package/runtime/js/cm-ai/worker-claude.mjs +63 -19
  98. package/runtime/js/cm-ai/worker-codex-developer.mjs +15 -9
  99. package/runtime/js/cm-ai/worker-codex.mjs +45 -19
  100. package/runtime/js/cm-fix/cause-evidence.mjs +5 -3
  101. package/runtime/js/cm-fix/cause-invocation.mjs +6 -3
  102. package/runtime/js/cm-fix/cause-package.mjs +10 -1
  103. package/runtime/js/cm-fix/execution.mjs +279 -52
  104. package/runtime/js/cm-fix/final-review.mjs +18 -10
  105. package/runtime/js/cm-fix/host-review.mjs +32 -9
  106. package/runtime/js/cm-fix/host.mjs +6 -1
  107. package/runtime/js/cm-fix/layout.mjs +1 -1
  108. package/runtime/js/cm-fix/progress.mjs +5 -0
  109. package/runtime/js/cm-fix/protected-edits.mjs +15 -4
  110. package/runtime/js/cm-fix/qa-source.mjs +6 -4
  111. package/runtime/js/cm-fix/review-reconciliation.mjs +64 -0
  112. package/runtime/js/cm-prd/analysis.mjs +2 -1
  113. package/runtime/js/cm-prd/change.mjs +28 -10
  114. package/runtime/js/cm-prd/draft.mjs +5 -2
  115. package/runtime/js/cm-prd/response-recovery.mjs +23 -0
  116. package/runtime/js/cm-prd/review-preparation.mjs +10 -3
  117. package/runtime/js/cm-prd/review-publication.mjs +27 -15
  118. package/runtime/js/cm-prd/review-time.mjs +29 -0
  119. package/runtime/js/cm-prd/self-check.mjs +6 -3
  120. package/runtime/js/cm-prd/session.mjs +53 -6
  121. package/runtime/js/cm-prd/summary.mjs +3 -1
  122. package/runtime/js/cm-prd/validation-reason.mjs +20 -0
  123. package/runtime/js/cm-security/finalize.mjs +32 -0
  124. package/runtime/js/spec-task-line.mjs +117 -0
  125. package/runtime/js/specs-status.mjs +10 -3
  126. package/runtime/review.md +5 -0
  127. package/runtime/task-gates.md +6 -0
  128. package/runtime/workflow-config.md +10 -1
  129. package/scripts/cm-ai-abandon-effect-position.test.mjs +201 -0
  130. package/scripts/cm-ai-admission.mjs +18 -11
  131. package/scripts/cm-ai-admission.test.mjs +65 -1
  132. package/scripts/cm-ai-batch-drive.mjs +135 -34
  133. package/scripts/cm-ai-batch-drive.test.mjs +209 -3
  134. package/scripts/cm-ai-batch-host.mjs +63 -24
  135. package/scripts/cm-ai-batch-host.test.mjs +2 -1
  136. package/scripts/cm-ai-batch-run.mjs +101 -34
  137. package/scripts/cm-ai-batch-run.test.mjs +56 -5
  138. package/scripts/cm-ai-bootstrap-driver-preflight.test.mjs +45 -0
  139. package/scripts/cm-ai-bootstrap-flow.test.mjs +159 -0
  140. package/scripts/cm-ai-bootstrap-merge.test.mjs +162 -0
  141. package/scripts/cm-ai-bootstrap.test.mjs +174 -89
  142. package/scripts/cm-ai-conversation-entry-ops.test.mjs +97 -2
  143. package/scripts/cm-ai-develop-driver-gaps.test.mjs +924 -0
  144. package/scripts/cm-ai-drive-bootstrap-rules.test.mjs +468 -0
  145. package/scripts/cm-ai-drive-timeout.test.mjs +17 -0
  146. package/scripts/cm-ai-drive.mjs +526 -60
  147. package/scripts/cm-ai-drive.test.mjs +336 -10
  148. package/scripts/cm-ai-fix-later-delivery.test.mjs +383 -0
  149. package/scripts/cm-ai-host.mjs +142 -43
  150. package/scripts/cm-ai-host.test.mjs +232 -12
  151. package/scripts/cm-ai-launch-diagnostics.test.mjs +142 -0
  152. package/scripts/cm-ai-multi-root.test.mjs +9 -3
  153. package/scripts/cm-ai-operator-guidance.test.mjs +94 -0
  154. package/scripts/cm-ai-progress.test.mjs +521 -0
  155. package/scripts/cm-ai-project-qa-gate.test.mjs +308 -0
  156. package/scripts/cm-ai-qa-config-revision.test.mjs +187 -15
  157. package/scripts/cm-ai-qa-fingerprint-config.test.mjs +157 -0
  158. package/scripts/cm-ai-qa-fix-cross-session.test.mjs +27 -2
  159. package/scripts/cm-ai-qa-rerun-timeouts.test.mjs +400 -0
  160. package/scripts/cm-ai-review-drift-precedence.test.mjs +90 -0
  161. package/scripts/cm-ai-review-failure.test.mjs +977 -0
  162. package/scripts/cm-ai-review-timeout-config.test.mjs +29 -7
  163. package/scripts/cm-ai-review-timeout.test.mjs +199 -4
  164. package/scripts/cm-ai-run.mjs +206 -62
  165. package/scripts/cm-ai-run.test.mjs +6 -4
  166. package/scripts/cm-ai-runner-error-code-regression.test.mjs +219 -0
  167. package/scripts/cm-ai-selection-guard.test.mjs +100 -0
  168. package/scripts/cm-ai-session-chain.test.mjs +7 -3
  169. package/scripts/cm-ai-spec-rebind.test.mjs +481 -0
  170. package/scripts/cm-ai-supersede.test.mjs +571 -17
  171. package/scripts/cm-ai-verification-gate.test.mjs +2 -1
  172. package/scripts/cm-bootstrap-review-recovery.test.mjs +70 -0
  173. package/scripts/cm-capability-gate.test.mjs +21 -2
  174. package/scripts/cm-check-drive.test.mjs +2 -1
  175. package/scripts/cm-check-host.test.mjs +1 -1
  176. package/scripts/cm-check-runtime.sh +12 -1
  177. package/scripts/cm-claude-developer-worker.test.mjs +3 -1
  178. package/scripts/cm-claude-install-layout.test.mjs +26 -0
  179. package/scripts/cm-claude-probe.test.mjs +10 -2
  180. package/scripts/cm-claude-review-stream.test.mjs +25 -0
  181. package/scripts/cm-claude-worker.test.mjs +11 -4
  182. package/scripts/cm-codex-developer-worker.test.mjs +4 -2
  183. package/scripts/cm-codex-plugin-metadata.py +83 -0
  184. package/scripts/cm-codex-plugin-metadata.test.mjs +55 -0
  185. package/scripts/cm-efficiency-checks.test.mjs +37 -0
  186. package/scripts/cm-efficiency-diagnostics.test.mjs +74 -0
  187. package/scripts/cm-efficiency-offline.mjs +35 -0
  188. package/scripts/cm-efficiency-policy.test.mjs +48 -0
  189. package/scripts/cm-efficiency-presentation.test.mjs +35 -0
  190. package/scripts/cm-efficiency-response.test.mjs +117 -0
  191. package/scripts/cm-efficiency-usage.test.mjs +148 -0
  192. package/scripts/cm-external-batch-host.test.mjs +157 -0
  193. package/scripts/cm-external-fix-host.test.mjs +281 -0
  194. package/scripts/cm-external-fix-reconciliation.test.mjs +105 -0
  195. package/scripts/cm-external-group-models.test.mjs +60 -0
  196. package/scripts/cm-external-model-host.test.mjs +180 -0
  197. package/scripts/cm-external-model-recovery.test.mjs +108 -0
  198. package/scripts/cm-external-models.test.mjs +89 -0
  199. package/scripts/cm-external-process-cleanup.test.mjs +71 -0
  200. package/scripts/cm-external-qa-fix.test.mjs +170 -0
  201. package/scripts/cm-external-review-reconciliation.test.mjs +314 -0
  202. package/scripts/cm-failover.mjs +8 -8
  203. package/scripts/cm-fix-abandon.test.mjs +123 -5
  204. package/scripts/cm-fix-cause-package.test.mjs +2 -0
  205. package/scripts/cm-fix-drive.mjs +70 -23
  206. package/scripts/cm-fix-drive.test.mjs +36 -1
  207. package/scripts/cm-fix-final-review.test.mjs +11 -6
  208. package/scripts/cm-fix-host.mjs +24 -14
  209. package/scripts/cm-fix-protected-edits.test.mjs +12 -0
  210. package/scripts/cm-fix-rediagnosis.test.mjs +168 -0
  211. package/scripts/cm-fix-review-budget.test.mjs +147 -0
  212. package/scripts/cm-fixture-process-cleanup.test.mjs +95 -0
  213. package/scripts/cm-host-check.test.mjs +6 -3
  214. package/scripts/cm-host-qa-executor.test.mjs +213 -17
  215. package/scripts/cm-host-session-diagnostic.test.mjs +10 -0
  216. package/scripts/cm-interactive-qa-diagnostic.test.mjs +53 -0
  217. package/scripts/cm-knowledge-closeout.test.mjs +160 -0
  218. package/scripts/cm-live-evidence.test.mjs +64 -0
  219. package/scripts/cm-log-event.mjs +25 -2
  220. package/scripts/cm-log-event.py +15 -0
  221. package/scripts/cm-log-event.test.mjs +31 -0
  222. package/scripts/cm-model-setup.mjs +79 -0
  223. package/scripts/cm-prd-completion.test.mjs +75 -0
  224. package/scripts/cm-prd-draft.test.mjs +14 -0
  225. package/scripts/cm-prd-drive.mjs +32 -11
  226. package/scripts/cm-prd-drive.test.mjs +15 -0
  227. package/scripts/cm-prd-gate-diagnostic.test.mjs +100 -0
  228. package/scripts/cm-prd-host.mjs +13 -4
  229. package/scripts/cm-prd-review-gate.mjs +126 -81
  230. package/scripts/cm-prd-review-gate.test.mjs +105 -1
  231. package/scripts/cm-prd-review-preparation.test.mjs +15 -0
  232. package/scripts/cm-prd-review-publication.test.mjs +6 -0
  233. package/scripts/cm-prd-self-check.test.mjs +6 -0
  234. package/scripts/cm-prd-summary.test.mjs +9 -0
  235. package/scripts/cm-release-smoke.sh +5 -1
  236. package/scripts/cm-review-dependency-snapshot.test.mjs +15 -0
  237. package/scripts/cm-review-inventory.test.mjs +253 -1
  238. package/scripts/cm-runtime-i18n.mjs +15 -4
  239. package/scripts/cm-runtime-install.mjs +8 -4
  240. package/scripts/cm-runtime.mjs +116 -23
  241. package/scripts/cm-runtime.test.mjs +121 -12
  242. package/scripts/cm-security.test.mjs +32 -0
  243. package/scripts/cm-spec-manifest.mjs +28 -10
  244. package/scripts/cm-spec-manifest.test.mjs +8 -4
  245. package/scripts/cm-spec-task-line.test.mjs +138 -0
  246. package/scripts/cm-specs-status.test.mjs +12 -1
  247. package/scripts/cm-task-gate-correction.test.mjs +117 -0
  248. package/scripts/cm-task-gate.mjs +164 -20
  249. package/scripts/cm-task-gate.py +2 -1
  250. package/scripts/cm-test-branch-impact.test.mjs +6 -3
  251. package/scripts/cm-test-drive.mjs +10 -4
  252. package/scripts/cm-test-drive.test.mjs +14 -1
  253. package/scripts/cm-tool-preview-context.test.mjs +12 -0
  254. package/scripts/cm-usage-report.py +22 -3
  255. package/scripts/cm-workflow-config.mjs +10 -1
  256. package/scripts/fixtures/bootstrap-review-runner.mjs +102 -0
  257. package/scripts/fixtures/claude-review-process.mjs +1 -0
  258. package/scripts/fixtures/live-evidence-driver.mjs +35 -0
  259. package/scripts/fixtures/process-cleanup.mjs +101 -0
  260. package/scripts/test-cm-log-event.py +2 -0
  261. package/scripts/test-cm-native-usage.py +62 -0
  262. package/scripts/test-cm-prd-review-gate.py +2 -1
  263. package/scripts/test-external-model-setup.py +91 -0
  264. package/scripts/test-model-configuration-write.py +201 -0
  265. package/scripts/test-npm-install.py +11 -2
  266. package/scripts/test-scan-public-safety.py +1 -1
  267. package/scripts/test-spec-manifest.py +16 -8
  268. package/scripts/test-task-gate.py +2 -2
  269. package/scripts/test-workflow-config.py +1 -0
  270. package/scripts/validate-public-repo.py +1 -0
  271. package/skills/cm-ai/SKILL.md +2 -2
  272. package/skills/cm-ai/references/N1-init.md +1 -1
  273. package/skills/cm-ai/references/N8-finish.md +5 -0
  274. package/skills/cm-ai/references/js-host.md +270 -43
  275. package/skills/cm-ai/references/knowledge-closeout.md +90 -0
  276. package/skills/cm-fix/SKILL.md +1 -1
  277. package/skills/cm-fix/references/js-host.md +52 -3
  278. package/skills/cm-prd/references/js-change-recovery.md +1 -1
  279. package/skills/cm-prd/references/js-host.md +6 -0
  280. package/skills/cm-runtime/SKILL.md +29 -6
  281. package/skills/cm-security/SKILL.md +4 -2
  282. package/skills/cm-security/references/scan-contract.md +1 -0
  283. package/templates/cm-workflow.yml +1 -1
@@ -2,10 +2,12 @@
2
2
  import fs from 'node:fs';
3
3
  import path from 'node:path';
4
4
  import {createHash} from 'node:crypto';
5
- import {buildManifest,verifyManifest,normalizeRuntimeMarks} from '../../../scripts/cm-spec-manifest.mjs';
6
- import {parseFeatureTaskText,declaredAcceptanceIds} from './cm-ai-admission.mjs';
5
+ import {verifyApprovedManifest,normalizeRuntimeMarks,normalizeRuntimeMarkBytes} from '../../../scripts/cm-spec-manifest.mjs';
6
+ import {parseFeatureTaskText,declaredAcceptanceIds,DEPENDENCY} from './cm-ai-admission.mjs';
7
+ import {taskLines,TASK_GRAMMAR,LEGACY_TASK_GRAMMAR} from '../spec-task-line.mjs';
7
8
  import {readReviewSourceFiles,readReviewSourceExcerpt} from './review-package.mjs';
8
- import {json,need,shape,hex,freeze,digest} from './effect-contract.mjs';
9
+ import {json,need,shape,hex,id,freeze,digest} from './effect-contract.mjs';
10
+ import {readExecutionSnapshot} from './execution-snapshot.mjs';
9
11
 
10
12
  const DESIGN_LIMIT=64*1024;
11
13
  const filenames=['requirements.md','design.md','tasks.md','test-cases.json'];
@@ -15,7 +17,8 @@ const featureName=value=>need(typeof value==='string'&&/^\d+\.[^/\\\x00-\x1f]+$/
15
17
  export function readSpecificationMaterial(raw,taskId){
16
18
  const v=json(raw,2*1024*1024);
17
19
  shape(v,['feature','task','acceptanceCriteria','designExcerpt','testCases','sources',
18
- ...(Object.hasOwn(v,'truncated')?['truncated']:[])]);
20
+ ...['truncated','taskScopeDigest'].filter(key=>Object.hasOwn(v,key))]);
21
+ if(Object.hasOwn(v,'taskScopeDigest'))hex(v.taskScopeDigest);
19
22
  featureName(v.feature);
20
23
  shape(v.task,['id','description',...(Object.hasOwn(v.task,'verification')?['verification']:[])]);
21
24
  need(v.task.id===taskId&&/^T-[A-Za-z0-9][A-Za-z0-9._-]*$/.test(taskId));
@@ -38,6 +41,41 @@ export function readSpecificationMaterial(raw,taskId){
38
41
  return freeze(v);
39
42
  }
40
43
 
44
+ // Everything in tasks.md and test-cases.json that can belong to this task:
45
+ // every line that names it wherever it sits (another task's sub-item, a
46
+ // dependency line, a note), its whole list item (continuation lines and
47
+ // sub-bullets included), and shared text naming no task. Left out are only
48
+ // lines naming other tasks alone: other tasks' items and their sub-lines,
49
+ // their dependency lines, and their test cases that do not name this task.
50
+ // Recorded for new runs so an explicit rebind can prove the re-approved change
51
+ // did not touch this task.
52
+ const TASK_ID_TOKEN=/(?<![A-Za-z0-9])T-[A-Za-z0-9][A-Za-z0-9._-]*/g;
53
+ const indentOf=line=>/^[ \t]*/.exec(line)[0].length;
54
+ const MENTIONS_TASK=/(?<![A-Za-z0-9])T-[A-Za-z0-9]/;
55
+ const namesTask=(text,taskId)=>(text.match(TASK_ID_TOKEN)??[]).some(id=>id.replace(/[._-]+$/,'')===taskId);
56
+ export function taskScopeText(tasks,taskId,grammar=TASK_GRAMMAR){
57
+ const kept=[];let owner=null,ownerIndent=0;
58
+ for(const {body,fenced,declaration} of taskLines(tasks,{grammar})){
59
+ if(!body.trim())continue;
60
+ if(declaration){owner=declaration.id;ownerIndent=indentOf(body);}
61
+ // Only lines indented under an item belong to it; a fence or heading at
62
+ // its level ends it, leaving the rest to the shared/named rules below.
63
+ else if(owner!==null&&!(indentOf(body)>ownerIndent&&!/^\s*#/.test(body)))owner=null;
64
+ const dependency=fenced||declaration?null:body.match(DEPENDENCY);
65
+ const belongs=declaration?declaration.id:owner??dependency?.[1]??null;
66
+ if(namesTask(body,taskId)||belongs===taskId||belongs===null&&!MENTIONS_TASK.test(body))kept.push(body);
67
+ }
68
+ return kept.join('\n');
69
+ }
70
+ function taskScopeDigest(texts,taskId,grammar){
71
+ let cases=null;
72
+ if(texts['test-cases.json']!==undefined){
73
+ const {cases:all,...contract}=JSON.parse(texts['test-cases.json']);
74
+ cases={...contract,cases:all.filter(item=>item.taskIds.includes(taskId)||namesTask(JSON.stringify(item),taskId))};
75
+ }
76
+ return digest({tasks:taskScopeText(texts['tasks.md'],taskId,grammar),testCases:cases});
77
+ }
78
+
41
79
  function verificationFor(source,taskId){
42
80
  let level=null;const lines=[];
43
81
  for(const line of source.split(/\r?\n/)){
@@ -60,7 +98,10 @@ export function captureSpecificationMaterial({specsRoot,feature,taskId}){
60
98
  const statusRecord=readReviewSourceFiles(specsRoot,['.cm-specs-status'])[0];
61
99
  const status=JSON.parse(Buffer.from(statusRecord.contentBase64,'base64').toString('utf8'));
62
100
  need(status.status==='approved'&&Array.isArray(status.features)&&status.features.includes(feature));
63
- const manifest=buildManifest(specsRoot);verifyManifest(manifest,statusPath);
101
+ // Sources are the approved rows themselves. A legacy approval therefore keeps
102
+ // its recorded digests, and material captured before the shared task grammar
103
+ // compares equal to material captured after it.
104
+ const manifest=verifyApprovedManifest(specsRoot,statusPath);
64
105
  need(JSON.stringify([...status.features].sort())===JSON.stringify([...new Set(manifest.map(row=>row.path.split('/')[0]))].sort()));
65
106
  const sources=manifest.filter(row=>row.path.startsWith(feature+'/'));
66
107
  const designPath=feature+'/design.md';
@@ -69,14 +110,17 @@ export function captureSpecificationMaterial({specsRoot,feature,taskId}){
69
110
  need(design.sha256===sources.find(row=>row.path===designPath)?.sha256);
70
111
  for(const record of records){
71
112
  const name=path.basename(record.path),bytes=Buffer.from(record.contentBase64,'base64');
72
- // Hash the very bytes that supply the material, using the existing runtime-mark contract.
73
- const normalized=Buffer.from(normalizeRuntimeMarks(bytes.toString('latin1'),name),'latin1');
74
- need(hash(normalized)===sources.find(row=>row.path===record.path)?.sha256);
113
+ // Hash the very bytes that supply the material, using the runtime-mark contract
114
+ // that approved them (current grammar, or the legacy rule for old approvals).
115
+ const approved=sources.find(row=>row.path===record.path)?.sha256;
116
+ need([false,true].some(legacy=>hash(normalizeRuntimeMarkBytes(bytes,name,{legacy}))===approved));
75
117
  texts[name]=normalizeRuntimeMarks(bytes.toString('utf8'),name);
76
118
  }
77
119
  need(readReviewSourceFiles(specsRoot,['.cm-specs-status'])[0].sha256===statusRecord.sha256);
78
- verifyManifest(buildManifest(specsRoot),statusPath);
79
- const parsed=parseFeatureTaskText(texts['tasks.md'],{allowDependencyPunctuation:true});
120
+ verifyApprovedManifest(specsRoot,statusPath);
121
+ // The approval binds the grammar its bytes were approved with (spec-task-line.mjs).
122
+ const grammar=status.taskGrammar===TASK_GRAMMAR?TASK_GRAMMAR:LEGACY_TASK_GRAMMAR;
123
+ const parsed=parseFeatureTaskText(texts['tasks.md'],{allowDependencyPunctuation:true,grammar});
80
124
  need(!parsed.error);
81
125
  const selected=parsed.tasks.find(task=>task.id===taskId&&!task.dropped);need(selected);
82
126
  const verification=verificationFor(texts['tasks.md'],taskId);
@@ -86,13 +130,111 @@ export function captureSpecificationMaterial({specsRoot,feature,taskId}){
86
130
  const cases=texts['test-cases.json']===undefined?[]:JSON.parse(texts['test-cases.json']).cases;
87
131
  need(Array.isArray(cases)&&cases.every(item=>item&&Array.isArray(item.taskIds)));
88
132
  return readSpecificationMaterial({feature,task,acceptanceCriteria,designExcerpt:design.excerpt,
89
- ...(design.truncated?{truncated:true}:{}),testCases:cases.filter(item=>item.taskIds.includes(taskId)),sources},taskId);
133
+ ...(design.truncated?{truncated:true}:{}),testCases:cases.filter(item=>item.taskIds.includes(taskId)),sources,
134
+ taskScopeDigest:taskScopeDigest(texts,taskId,grammar)},taskId);
90
135
  }catch{throw Object.assign(new Error('spec_drift'),{code:'spec_drift'});}
91
136
  }
92
137
 
138
+ // Material captured by older versions has no taskScopeDigest; compare a fresh
139
+ // capture in the bound representation so their runs keep verifying.
140
+ const asBound=(bound,current)=>{
141
+ if(Object.hasOwn(bound,'taskScopeDigest'))return current;
142
+ const {taskScopeDigest:unused,...legacy}=current;return legacy;
143
+ };
144
+ // Rebind eligibility, deliberately conservative: requirements.md and design.md
145
+ // must be unchanged as whole files (acceptance items may span lines, and prose
146
+ // can state requirements); in tasks.md and test-cases.json only other tasks'
147
+ // items, dependency lines and cases may differ (taskScopeDigest). A run bound
148
+ // before taskScopeDigest existed cannot prove that and is never rebindable.
149
+ const CONTENT_FIELDS=['feature','task','acceptanceCriteria','designExcerpt','truncated','testCases','taskScopeDigest'];
150
+ const FIELD_NAMES={feature:'feature',task:'本任务描述/验证要求',acceptanceCriteria:'验收标准',
151
+ designExcerpt:'设计摘录',truncated:'设计摘录',testCases:'本任务测试用例',
152
+ taskScopeDigest:'tasks.md/test-cases.json 中本任务的条目、续行、依赖、用例或共用说明'};
153
+ const WHOLE_FILES={'requirements.md':'requirements.md(整份核对,含验收标准续行与说明)','design.md':'design.md(整份核对)'};
154
+
155
+ // Explicit, journaled rebind (task-runner rebindSpecification). It can only ever
156
+ // replace `sources`: the accepted material is the bound material with the
157
+ // approved hashes of the re-approved files, never different content.
158
+ export function readSpecificationRebind(raw,expected=null){
159
+ const record=json(raw,64*1024);
160
+ shape(record,['version','taskId','boundDigest','sources','files','reason','reboundAt']);
161
+ need(record.version===1,'spec_rebind_invalid');id(record.taskId);hex(record.boundDigest);
162
+ need(Array.isArray(record.sources)&&[3,4].includes(record.sources.length),'spec_rebind_invalid');
163
+ for(const row of record.sources){shape(row,['path','sha256']);need(typeof row.path==='string','spec_rebind_invalid');hex(row.sha256);}
164
+ need(Array.isArray(record.files)&&record.files.length>0&&record.files.length<=filenames.length
165
+ &&new Set(record.files).size===record.files.length
166
+ &&record.files.every(file=>typeof file==='string'&&filenames.includes(file.split('/').at(-1))),'spec_rebind_invalid');
167
+ need(typeof record.reason==='string'&&record.reason.trim().length>0&&Buffer.byteLength(record.reason,'utf8')<=500
168
+ &&!/[\r\n\0]/.test(record.reason),'spec_rebind_reason_required');
169
+ need(typeof record.reboundAt==='string'&&Number.isFinite(Date.parse(record.reboundAt))
170
+ &&new Date(record.reboundAt).toISOString()===record.reboundAt,'spec_rebind_invalid');
171
+ if(expected)need(record.taskId===expected.taskId&&record.boundDigest===expected.boundDigest,'spec_rebind_invalid');
172
+ return freeze(record);
173
+ }
174
+
175
+ // The run's own journal is the only authority for a rebind; any read or
176
+ // validation problem means no rebind, so verification fails closed.
177
+ function acceptedRebindMaterial(baseline){
178
+ try{
179
+ const snapshot=readExecutionSnapshot({specsRoot:baseline.specificationRoot,
180
+ identity:{repositoryId:baseline.identity.repositoryId,runId:baseline.identity.runId}});
181
+ const row=snapshot.records.findLast(item=>item.payload?.version===3&&item.payload.type==='specification-rebound');
182
+ if(!row)return null;
183
+ const record=readSpecificationRebind(row.payload.record,{taskId:baseline.identity.taskId,
184
+ boundDigest:digest(baseline.specification)});
185
+ return readSpecificationMaterial({...baseline.specification,sources:record.sources},baseline.identity.taskId);
186
+ }catch{return null;}
187
+ }
188
+ const accepted=(baseline,captured)=>{
189
+ const current=asBound(baseline.specification,captured);
190
+ if(digest(current)===digest(baseline.specification))return true;
191
+ const rebound=acceptedRebindMaterial(baseline);
192
+ return rebound!==null&&digest(current)===digest(rebound);
193
+ };
194
+
93
195
  export function verifySpecificationMaterial(baseline){
94
196
  const current=captureSpecificationMaterial({specsRoot:baseline.specificationRoot,
95
197
  feature:baseline.specification.feature,taskId:baseline.identity.taskId});
96
- need(digest(current)===digest(baseline.specification),'spec_drift');
198
+ need(accepted(baseline,current),'spec_drift');
199
+ return baseline.specification;
200
+ }
201
+
202
+ // A freshly captured material equal to the bound (or explicitly rebound) one is
203
+ // represented by the bound material, so reviewed baselines and packages keep
204
+ // their digests. Anything else is spec_drift.
205
+ export function boundSpecificationMaterial(baseline,captured){
206
+ need(accepted(baseline,captured),'spec_drift');
97
207
  return baseline.specification;
98
208
  }
209
+
210
+ // Diagnostic for status and rebind: null when the bound material (or an
211
+ // explicit rebind) still verifies; otherwise what changed and whether an
212
+ // explicit rebind is allowed. Never grants anything itself.
213
+ export function inspectSpecificationDrift(baseline){
214
+ let current;
215
+ try{current=captureSpecificationMaterial({specsRoot:baseline.specificationRoot,
216
+ feature:baseline.specification.feature,taskId:baseline.identity.taskId});}
217
+ catch{return freeze({approved:false,files:[],fields:[],rebindable:false});}
218
+ if(accepted(baseline,current))return null;
219
+ const bound=baseline.specification,before=new Map(bound.sources.map(row=>[row.path,row.sha256]));
220
+ const after=new Map(current.sources.map(row=>[row.path,row.sha256]));
221
+ const files=[...new Set([...before.keys(),...after.keys()])].filter(file=>before.get(file)!==after.get(file)).sort();
222
+ const fields=[...new Set([
223
+ ...files.map(file=>WHOLE_FILES[file.split('/').at(-1)]).filter(Boolean),
224
+ ...(before.size!==after.size?['test-cases.json 的有无']:[]),
225
+ ...(Object.hasOwn(bound,'taskScopeDigest')?[]:['本运行由旧版本创建,未记录本任务段落摘要,无法证明其未变']),
226
+ ...CONTENT_FIELDS.filter(field=>Object.hasOwn(bound,'taskScopeDigest')||field!=='taskScopeDigest')
227
+ .filter(field=>digest(current[field]??null)!==digest(bound[field]??null)).map(field=>FIELD_NAMES[field])])];
228
+ return freeze({approved:true,files,fields,rebindable:fields.length===0,sources:current.sources});
229
+ }
230
+
231
+ export function describeSpecificationDrift(drift){
232
+ if(!drift.approved)return '规格当前未处于已批准且与批准清单一致的状态(cm-prd 变更尚未重新批准,或规格文件被直接修改);'
233
+ +'先完成 cm-prd --change 并重新批准,之后按 status 提示继续。';
234
+ const files=drift.files.join('、');
235
+ if(drift.rebindable)return `规格已重新批准,变更文件:${files};只有其他任务的条目、依赖或用例变了,requirements.md、design.md 与本任务相关内容均未变。`
236
+ +'用 --mode resume --rebind-spec-material --spec-rebind-reason 原因 重开本运行即可显式换绑,已有开发与审查结论保留。';
237
+ return `规格已变更:${files};以下与本任务相关的内容已变或无法证明未变:${drift.fields.join('、')},不能换绑。`
238
+ +'出口:还原这些规格改动并重新批准后本运行可继续;或 cancel 本运行,还原本运行改动的代码后用 '
239
+ +'--supersede-reviewed-evidence --supersede-reason 原因 新建运行重做。';
240
+ }
@@ -0,0 +1,93 @@
1
+ // Advisory display only. The existing log lock serializes ownership checks and
2
+ // replacement across processes; neither this file nor its token grants work.
3
+ import fs from 'node:fs';
4
+ import path from 'node:path';
5
+ import {randomUUID} from 'node:crypto';
6
+ import {spawnSync} from 'node:child_process';
7
+ import {fileURLToPath} from 'node:url';
8
+ import {json,need,shape,text,validIdentity} from './effect-contract.mjs';
9
+
10
+ const writer=fileURLToPath(new URL('../../../scripts/cm-log-event.py',import.meta.url));
11
+ const LIMIT=64*1024;
12
+ export function statusTarget(specsDir){
13
+ need(typeof specsDir==='string'&&path.isAbsolute(specsDir),'status_invalid');
14
+ const specs=fs.realpathSync(specsDir),target=path.join(specs,'.cm-status.json');
15
+ need(fs.lstatSync(specs).isDirectory(),'status_invalid');
16
+ try{
17
+ const stat=fs.lstatSync(target);
18
+ need(stat.isFile()&&!stat.isSymbolicLink()&&stat.nlink===1&&stat.size<=LIMIT,'status_invalid');
19
+ }catch(error){if(error.code!=='ENOENT')throw error;}
20
+ return {specs,target};
21
+ }
22
+ export function readStatusProjection(specsDir){
23
+ const {target}=statusTarget(specsDir);
24
+ try{
25
+ const value=json(JSON.parse(fs.readFileSync(target,'utf8')),LIMIT);
26
+ need(value&&typeof value==='object'&&!Array.isArray(value),'status_invalid');return value;
27
+ }catch(error){if(error.code==='ENOENT')return null;throw error;}
28
+ }
29
+ function readInput(raw){
30
+ const value=json(raw,LIMIT);
31
+ shape(value,['feature','identity','node','state','detail','claim','expectedToken',
32
+ ...['code','caseId','allowedNodes','previousAttempt'].filter(key=>Object.hasOwn(value,key))]);
33
+ validIdentity(value.identity);
34
+ for(const key of ['feature','node','state','detail']){
35
+ text(value[key]);need(Buffer.byteLength(value[key])<=2000&&!/[\r\n\0]/.test(value[key]),'status_invalid');
36
+ }
37
+ need(typeof value.claim==='boolean'&&(value.expectedToken===null||typeof value.expectedToken==='string'),'status_invalid');
38
+ need(!Object.hasOwn(value,'code')||value.code===null||/^[a-z][a-z0-9_]{0,63}$/.test(value.code),'status_invalid');
39
+ need(!Object.hasOwn(value,'caseId')||typeof value.caseId==='string','status_invalid');
40
+ need(!Object.hasOwn(value,'allowedNodes')||Array.isArray(value.allowedNodes)
41
+ &&value.allowedNodes.length<=8&&value.allowedNodes.every(node=>typeof node==='string'),'status_invalid');
42
+ need(!Object.hasOwn(value,'previousAttempt')||value.state==='changes_requested'
43
+ &&Number.isSafeInteger(value.previousAttempt)&&value.previousAttempt>0
44
+ &&value.previousAttempt===value.identity.attempt-1,'status_invalid');
45
+ return value;
46
+ }
47
+ const owns=(previous,value)=>previous.feature===value.feature&&previous.task===value.identity.taskId
48
+ &&(previous.run_id===undefined||previous.run_id===value.identity.runId)
49
+ &&(previous.attempt===undefined||previous.attempt===value.identity.attempt||previous.attempt===value.previousAttempt);
50
+
51
+ // Invoked only by cm-log-event under its Python platform lock. Validation and
52
+ // rename are deliberately in the SAME critical section, not two lock calls.
53
+ export function statusProjectionCommand(argv,environment){
54
+ need(argv.length===5&&argv[0]==='--status-only'&&argv[1]==='--specs-dir'
55
+ &&argv[3]==='--status-json','status_invalid');
56
+ const {specs,target}=statusTarget(argv[2]),value=readInput(JSON.parse(argv[4]));
57
+ if(environment.CM_LOG_PREFLIGHT==='1')return null;
58
+ need(environment.CM_LOG_LOCK_ADAPTER==='1'&&Number(environment.CM_LOG_LOCK_PARENT_PID)===process.ppid
59
+ &&environment.CM_LOG_PROJECT_LOCK===path.join(specs,'.cm-run.lock'),'status_lock_required');
60
+ const previous=readStatusProjection(specs);
61
+ if(!value.claim&&previous&&(!owns(previous,value)
62
+ ||value.allowedNodes&&!value.allowedNodes.includes(previous.node)
63
+ ||value.caseId&&previous.case_id&&previous.case_id!==value.caseId))return {written:false,token:null};
64
+ if(value.expectedToken!==null&&previous?.progress_id!==value.expectedToken)return {written:false,token:null};
65
+ const token=randomUUID(),projection={node:value.node,feature:value.feature,task:value.identity.taskId,
66
+ run_id:value.identity.runId,attempt:value.identity.attempt,state:value.state,detail:value.detail,
67
+ at:new Date().toTimeString().slice(0,8),progress_id:token,
68
+ ...(value.code?{code:value.code}:{}),...(value.caseId?{case_id:value.caseId}:{})};
69
+ const temporary=path.join(specs,`.cm-status.json.tmp.${process.pid}.${token}`);
70
+ let descriptor;
71
+ try{
72
+ descriptor=fs.openSync(temporary,fs.constants.O_WRONLY|fs.constants.O_CREAT|fs.constants.O_EXCL|(fs.constants.O_NOFOLLOW??0),0o644);
73
+ fs.writeFileSync(descriptor,JSON.stringify(projection)+'\n');fs.fsyncSync(descriptor);fs.closeSync(descriptor);descriptor=undefined;
74
+ fs.renameSync(temporary,target);
75
+ if(process.platform!=='win32'){
76
+ const directory=fs.openSync(specs,fs.constants.O_RDONLY|(fs.constants.O_NOFOLLOW??0));
77
+ try{fs.fsyncSync(directory);}finally{fs.closeSync(directory);}
78
+ }
79
+ }finally{
80
+ if(descriptor!==undefined)fs.closeSync(descriptor);
81
+ try{fs.unlinkSync(temporary);}catch(error){if(error.code!=='ENOENT')throw error;}
82
+ }
83
+ return {written:true,token};
84
+ }
85
+ export function writeStatusProjection({specsDir,...raw}){
86
+ const {specs}=statusTarget(specsDir),value=readInput({claim:false,expectedToken:null,...raw});
87
+ const result=spawnSync('python3',[writer,'--status-only','--specs-dir',specs,'--status-json',JSON.stringify(value)],
88
+ {timeout:12000,maxBuffer:LIMIT,encoding:'utf8'});
89
+ need(!result.error&&result.status===0&&result.signal===null,'status_write_failed');
90
+ const receipt=JSON.parse(result.stdout);shape(receipt,['written','token']);
91
+ need(typeof receipt.written==='boolean'&&(receipt.token===null||typeof receipt.token==='string'),'status_write_failed');
92
+ return receipt;
93
+ }
@@ -4,7 +4,7 @@ import path from 'node:path';
4
4
  import { createHash,randomUUID } from 'node:crypto';
5
5
  import { types } from 'node:util';
6
6
  import {createRequire} from 'node:module';
7
- import { verifyReviewPackage,reviewSpecsPath } from './review-package.mjs';
7
+ import { verifyCompletionReviewPackage,reviewSpecsPath } from './review-package.mjs';
8
8
  import { checkCompletion } from './gate-bridge.mjs';
9
9
  import { need,shape,json,digest,validIdentity } from './effect-contract.mjs';
10
10
  import {planBytes,readCommitIntent,readCommitResult} from './task-commit-codec.mjs';
@@ -64,7 +64,7 @@ function readFile(p) {
64
64
  function nativeRevision(p,f) {return digest({path:p,stat:statFields(f.stat),sha256:sha(f.bytes)});}
65
65
  function completionSelectors(selectors,identity,projectRoot){return {...selectors,task:identity.taskId,projectRoot};}
66
66
  function verifyProof(v) {
67
- verifyReviewPackage({root:v.root,baseline:v.baseline,checks:v.checks,reviewPackage:v.reviewPackage,expectedDigest:v.reviewPackage.packageDigest,
67
+ verifyCompletionReviewPackage({root:v.root,baseline:v.baseline,checks:v.checks,reviewPackage:v.reviewPackage,expectedDigest:v.reviewPackage.packageDigest,
68
68
  ...(Object.hasOwn(v.reviewPackage,'handoff')?{handoffPath:v.selectors.handoff}:{})});
69
69
  checkCompletion({receipt:v.receipt,registered:v.registered,execution:v.execution,reviewPackage:v.reviewPackage,identity:v.identity});
70
70
  }