rungate 0.2.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 (196) hide show
  1. package/.claude/agents/aditi.md +47 -0
  2. package/.claude/agents/marcus.md +50 -0
  3. package/.claude/agents/quinn.md +58 -0
  4. package/.claude/agents/rook.md +47 -0
  5. package/.claude/agents/serena.md +46 -0
  6. package/.claude/rungate.json +11 -0
  7. package/.fallowrc.json +28 -0
  8. package/.github/copilot-instructions.md +20 -0
  9. package/.rungate/conformity-findings.json +648 -0
  10. package/AGENTS.md +106 -0
  11. package/CODE-MAP.md +50 -0
  12. package/HARNESS.md +88 -0
  13. package/bun.lock +26 -0
  14. package/bunfig.toml +2 -0
  15. package/config/brief-policies.json +28 -0
  16. package/config/ceremony-profiles.json +212 -0
  17. package/config/consumer-cascade-map.json +24 -0
  18. package/config/doc-cascade-map.json +24 -0
  19. package/config/eval.json +84 -0
  20. package/config/profiles/account-plan.yaml +36 -0
  21. package/config/profiles/campaign.yaml +94 -0
  22. package/config/prove-evidence.schema.json +65 -0
  23. package/config/spec-policies.json +36 -0
  24. package/config/workflow-schema.json +281 -0
  25. package/gates/SCHEMA-GUIDE.md +121 -0
  26. package/gates/ac-quality.test.ts +106 -0
  27. package/gates/adversarial.test.ts +274 -0
  28. package/gates/brief-assembler.test.ts +429 -0
  29. package/gates/brief-assembler.ts +334 -0
  30. package/gates/bunfig.toml +2 -0
  31. package/gates/chain.test.ts +91 -0
  32. package/gates/e2e-smoke.test.ts +234 -0
  33. package/gates/error-classifier.test.ts +249 -0
  34. package/gates/error-classifier.ts +89 -0
  35. package/gates/orchestrator.test.ts +950 -0
  36. package/gates/orchestrator.ts +569 -0
  37. package/gates/preload.ts +77 -0
  38. package/gates/prompts/ac-adversary.md +20 -0
  39. package/gates/prompts/evidence-validator.md +22 -0
  40. package/gates/prompts/prove-reproducer.md +53 -0
  41. package/gates/prove.test.ts +164 -0
  42. package/gates/run-gate.ts +936 -0
  43. package/gates/schema-parity.test.ts +197 -0
  44. package/gates/schema.ts +362 -0
  45. package/gates/self-heal.test.ts +511 -0
  46. package/gates/self-heal.ts +121 -0
  47. package/gates/ship-orchestrator.test.ts +379 -0
  48. package/gates/ship-orchestrator.ts +363 -0
  49. package/gates/test-fixtures/broken-no-environments.json +60 -0
  50. package/gates/test-fixtures/valid-standard.json +54 -0
  51. package/gates/test-utils.ts +43 -0
  52. package/gates/witness.ts +220 -0
  53. package/gates/workflow.test.ts +1053 -0
  54. package/hooks/AgentBriefGuard.hook.ts +586 -0
  55. package/hooks/AgentVerdictCapture.hook.ts +134 -0
  56. package/hooks/AutoVerifyGate.hook.ts +76 -0
  57. package/hooks/GateEnforcement.hook.ts +273 -0
  58. package/hooks/IssueCloseGuard.hook.ts +116 -0
  59. package/hooks/MergeGuard.hook.ts +55 -0
  60. package/hooks/StaleTTLCleanup.hook.ts +133 -0
  61. package/hooks/VerifyPhaseLock.hook.ts +81 -0
  62. package/hooks/WorkflowStateGuard.hook.ts +37 -0
  63. package/hooks/lib/agentDetection.ts +70 -0
  64. package/hooks/lib/findWorkflow.ts +101 -0
  65. package/hooks/lib/parseStdin.ts +37 -0
  66. package/hooks/lib/paths.ts +111 -0
  67. package/hooks/lib/utils.ts +81 -0
  68. package/lib/conformity.ts +959 -0
  69. package/lib/paths.ts +23 -0
  70. package/lib/rungate-schema.ts +60 -0
  71. package/lib/signal-phrases.ts +7 -0
  72. package/package.json +20 -0
  73. package/prompts/ac-adversary.md +20 -0
  74. package/prompts/ac-format.md +34 -0
  75. package/prompts/aditi.md +29 -0
  76. package/prompts/blast-radius.md +29 -0
  77. package/prompts/coding-principles.md +34 -0
  78. package/prompts/container-rebuild.md +29 -0
  79. package/prompts/container-verify.md +28 -0
  80. package/prompts/discovery.md +27 -0
  81. package/prompts/environment.md +28 -0
  82. package/prompts/escalation-decision-tree.md +38 -0
  83. package/prompts/evidence-hierarchy.md +31 -0
  84. package/prompts/evidence-validator.md +22 -0
  85. package/prompts/marcus.md +29 -0
  86. package/prompts/prevention.md +26 -0
  87. package/prompts/prove-reproducer.md +43 -0
  88. package/prompts/quinn-decision-tree.md +31 -0
  89. package/prompts/quinn-ui-brief.md +106 -0
  90. package/prompts/quinn.md +29 -0
  91. package/prompts/rca.md +28 -0
  92. package/prompts/read-before-write.md +30 -0
  93. package/prompts/regression.md +26 -0
  94. package/prompts/rook.md +29 -0
  95. package/prompts/serena.md +28 -0
  96. package/prompts/testing-strategy.md +34 -0
  97. package/reference/migration-manifest.json +554 -0
  98. package/reference/prompts/agent-aditi.md +38 -0
  99. package/reference/prompts/agent-marcus.md +38 -0
  100. package/reference/prompts/agent-quinn.md +38 -0
  101. package/reference/prompts/agent-rook.md +37 -0
  102. package/reference/prompts/agent-serena.md +37 -0
  103. package/reference/prompts/council-skill.md +170 -0
  104. package/reference/prompts/goal-skill.md +235 -0
  105. package/reference/prompts/prove-skill.md +283 -0
  106. package/reference/prompts/quinn-standard.md +246 -0
  107. package/reference/prompts/ship-convergence.md +65 -0
  108. package/reference/prompts/ship-escalation.md +43 -0
  109. package/reference/prompts/ship-iteration.md +382 -0
  110. package/reference/prompts/ship-pending-patches.md +246 -0
  111. package/reference/prompts/ship-reference.md +250 -0
  112. package/reference/prompts/ship-skill.md +347 -0
  113. package/reference/prompts/ship-templates.md +215 -0
  114. package/reference/scripts/batch-status.sh +68 -0
  115. package/reference/scripts/chain-compat-check.sh +67 -0
  116. package/reference/scripts/context-audit.sh +314 -0
  117. package/reference/scripts/dead-export-check.sh +61 -0
  118. package/reference/scripts/generate-qc-tables.sh +92 -0
  119. package/reference/scripts/harness-init.sh +111 -0
  120. package/reference/scripts/pai-054-baseline-check.sh +40 -0
  121. package/reference/scripts/prove-smoke-test.sh +112 -0
  122. package/reference/scripts/setup-keychain-salt.sh +12 -0
  123. package/reference/scripts/skill-optimizer-friday.sh +88 -0
  124. package/reference/scripts/skill-qc-test.sh +351 -0
  125. package/reference/scripts/skill-synthesize-tonight.sh +66 -0
  126. package/reference/specs/HARNESS-EXTRACTION-SPEC.md +263 -0
  127. package/reference/specs/harness-v3-bash-deletion-plan.md +65 -0
  128. package/reference/specs/harness-v3-migration-plan.md +188 -0
  129. package/scripts/decision-reconcile.sh +74 -0
  130. package/scripts/decision-reconcile.ts +152 -0
  131. package/scripts/extract-constraints.ts +260 -0
  132. package/scripts/generate-code-map.ts +339 -0
  133. package/scripts/git-hooks/pre-commit +76 -0
  134. package/scripts/git-hooks/pre-push +271 -0
  135. package/scripts/harness-skill-check.sh +52 -0
  136. package/scripts/lib/gap-emit.sh +65 -0
  137. package/scripts/lib/jq-update.sh +30 -0
  138. package/scripts/lib/resolve-slug.sh +30 -0
  139. package/scripts/lib/verify-spec-compliance.sh +102 -0
  140. package/scripts/prove-backfill.sh +87 -0
  141. package/scripts/scaffold-project.ts +1488 -0
  142. package/scripts/scaffold-rungate-config.ts +91 -0
  143. package/scripts/self-containment-check.sh +109 -0
  144. package/scripts/skill-qc-validator.sh +402 -0
  145. package/scripts/slug-resolver.sh +14 -0
  146. package/scripts/sync-spec-tests.ts +236 -0
  147. package/specs/BOOTSTRAP-DATA-FLOW-SPEC.md +2493 -0
  148. package/specs/BOOTSTRAP-TEST-PLAN.md +571 -0
  149. package/specs/HARNESS-GATES.md +256 -0
  150. package/specs/HARNESS-SKILL-CHAIN.md +286 -0
  151. package/specs/HARNESS-SKILL-CONTRACT.md +93 -0
  152. package/specs/HARNESS-STANDARD.md +488 -0
  153. package/specs/SPEC-TEMPLATE.md +53 -0
  154. package/specs/bootstrap-data-flow.png +0 -0
  155. package/specs/harness-automation-matrix.md +314 -0
  156. package/specs/harness-automation-matrix.rules.json +64 -0
  157. package/test/anti.test.ts +80 -0
  158. package/test/contract-negative.test.ts +86 -0
  159. package/test/contract.test.ts +158 -0
  160. package/test/external-deps.test.ts +48 -0
  161. package/test/fixtures/golden-project/.env.example +3 -0
  162. package/test/fixtures/golden-project/Makefile +7 -0
  163. package/test/fixtures/golden-project/docs/adr/ADR-001-framework.md +23 -0
  164. package/test/fixtures/golden-project/docs/setup-guide.md +10 -0
  165. package/test/fixtures/golden-project/package.json +10 -0
  166. package/test/fixtures/golden-project/specs/api-spec.md +20 -0
  167. package/test/fixtures/golden-project/src/auth.ts +8 -0
  168. package/test/fixtures/golden-project/src/db.ts +11 -0
  169. package/test/fixtures/golden-project/src/index.ts +19 -0
  170. package/test/fixtures/golden-project/src/utils/format.ts +3 -0
  171. package/test/fixtures/golden-project/test/auth.test.ts +15 -0
  172. package/test/fixtures/golden-project/tsconfig.json +12 -0
  173. package/test/meta-sc-coverage.test.ts +159 -0
  174. package/test/phase-0.test.ts +846 -0
  175. package/test/phase-1.test.ts +617 -0
  176. package/test/phase-2.test.ts +620 -0
  177. package/test/phase-3.test.ts +99 -0
  178. package/test/scaffold-conformity.test.ts +8 -0
  179. package/test/schema-canary.test.ts +51 -0
  180. package/test/spec-compliance-auto.test.ts +64 -0
  181. package/test/spec-compliance.test.ts +295 -0
  182. package/test/spec-discovery.test.ts +68 -0
  183. package/test/spec-drift.test.ts +5 -0
  184. package/test/structure.test.ts +165 -0
  185. package/test/unit/agent-brief-guard-ship.test.ts +293 -0
  186. package/test/unit/extract-constraints.test.ts +145 -0
  187. package/test/unit/issue-close-guard.test.ts +204 -0
  188. package/test/unit/scaffold-project.test.ts +417 -0
  189. package/test/unit/skill-cooccurrence.test.ts +177 -0
  190. package/test/unit/skill-sequence-logger.test.ts +244 -0
  191. package/test/unit/workflow-state-guard.test.ts +95 -0
  192. package/workflows/batch-ship.js +295 -0
  193. package/workflows/council.js +481 -0
  194. package/workflows/prove.js +560 -0
  195. package/workflows/ship.js +789 -0
  196. package/workflows/verify.js +236 -0
@@ -0,0 +1,950 @@
1
+ import { test, expect, describe, beforeEach, afterEach } from "bun:test";
2
+ import { readFileSync, writeFileSync, mkdirSync, rmSync, existsSync, statSync } from "fs";
3
+ import { join } from "path";
4
+ import {
5
+ writeGateResult,
6
+ generateHmac,
7
+ runConvergence,
8
+ generateShipEvidence,
9
+ writeGateSummary,
10
+ writeWorkflowState,
11
+ initWorkflow,
12
+ writeACs,
13
+ markCommandlessACsAsSkip,
14
+ initBatchPlan,
15
+ recordBatchShipped,
16
+ recordBatchSkipped,
17
+ validateBatchComplete,
18
+ } from "./orchestrator";
19
+
20
+ const TMP_DIR = `/tmp/orchestrator-test-${process.pid}`;
21
+ const SF = join(TMP_DIR, "workflow-state.json");
22
+ const PROFILE = join(TMP_DIR, "ceremony-profiles.json");
23
+ const PENDING = join(process.env.HOME || "", ".claude", "MEMORY", "STATE", "gate-pending.json");
24
+
25
+ function makeState(overrides: Record<string, any> = {}): any {
26
+ return {
27
+ schemaVersion: 2,
28
+ issue: 432,
29
+ slug: "test-orch",
30
+ phase: "SCOPE",
31
+ issueGoal: "Port orchestrator",
32
+ acs: [
33
+ {
34
+ id: "SC-1",
35
+ statement: "orchestrator.ts file exists on disk and is valid",
36
+ threshold: { op: "==", value: "true" },
37
+ evidenceMethod: { type: "FILE_EXISTS" },
38
+ evidence: null,
39
+ },
40
+ ],
41
+ gates: {},
42
+ changelog: [],
43
+ sizing: { predicted: "M", ceremonyTier: "STANDARD" },
44
+ phaseTimings: {},
45
+ ...overrides,
46
+ };
47
+ }
48
+
49
+ function makeProfile(): any {
50
+ return {
51
+ tiers: {
52
+ LIGHT: { maxIterations: 3, checks: { scope: [], verify: [], ship: [] } },
53
+ STANDARD: { maxIterations: 5, checks: { scope: [], verify: [], ship: [] } },
54
+ THOROUGH: { maxIterations: 8, checks: { scope: [], verify: [], ship: [] } },
55
+ },
56
+ };
57
+ }
58
+
59
+ beforeEach(() => {
60
+ mkdirSync(TMP_DIR, { recursive: true });
61
+ writeFileSync(SF, JSON.stringify(makeState(), null, 2));
62
+ writeFileSync(PROFILE, JSON.stringify(makeProfile(), null, 2));
63
+ });
64
+
65
+ afterEach(() => {
66
+ rmSync(TMP_DIR, { recursive: true, force: true });
67
+ try { rmSync(PENDING, { force: true }); } catch {}
68
+ });
69
+
70
+ // ═══ writeGateResult ═══════════════════════════════════════════════════
71
+
72
+ describe("writeGateResult", () => {
73
+ test("writes correct gate result to workflow state", () => {
74
+ const results = [
75
+ { check: "acs-exist", result: "PASS" as const, detail: "3 ACs" },
76
+ { check: "sizing", result: "PASS" as const, detail: "size=M" },
77
+ ];
78
+ const { resultVal, attempt } = writeGateResult(SF, "scope", 2, 0, 0, results);
79
+
80
+ expect(resultVal).toBe("PASS");
81
+ expect(attempt).toBe(1);
82
+
83
+ const state = JSON.parse(readFileSync(SF, "utf-8"));
84
+ expect(state.gates.scope.result).toBe("PASS");
85
+ expect(state.gates.scope.attempt).toBe(1);
86
+ expect(state.gates.scope.failures).toEqual([]);
87
+ expect(state.gates.scope.ts).toBeTruthy();
88
+ expect(state.gates.scope.commitSha).toBeTruthy();
89
+ });
90
+
91
+ test("records failures when checks fail", () => {
92
+ const results = [
93
+ { check: "acs-exist", result: "FAIL" as const, detail: "no ACs" },
94
+ { check: "sizing", result: "PASS" as const, detail: "size=M" },
95
+ ];
96
+ const { resultVal } = writeGateResult(SF, "scope", 1, 1, 0, results);
97
+
98
+ expect(resultVal).toBe("FAIL");
99
+
100
+ const state = JSON.parse(readFileSync(SF, "utf-8"));
101
+ expect(state.gates.scope.result).toBe("FAIL");
102
+ expect(state.gates.scope.failures).toHaveLength(1);
103
+ expect(state.gates.scope.failures[0].check).toBe("acs-exist");
104
+ });
105
+
106
+ test("advances phase on PASS: scope->BUILD", () => {
107
+ const results = [{ check: "acs-exist", result: "PASS" as const, detail: "ok" }];
108
+ writeGateResult(SF, "scope", 1, 0, 0, results);
109
+
110
+ const state = JSON.parse(readFileSync(SF, "utf-8"));
111
+ expect(state.phase).toBe("BUILD");
112
+ expect(state.phaseTimings.BUILD).toHaveLength(1);
113
+ expect(state.phaseTimings.BUILD[0].enteredTs).toBeTruthy();
114
+ });
115
+
116
+ test("advances phase on PASS: verify->SHIP", () => {
117
+ writeFileSync(SF, JSON.stringify(makeState({ phase: "VERIFY" }), null, 2));
118
+ const results = [{ check: "all-pass", result: "PASS" as const, detail: "ok" }];
119
+ writeGateResult(SF, "verify", 1, 0, 0, results);
120
+
121
+ const state = JSON.parse(readFileSync(SF, "utf-8"));
122
+ expect(state.phase).toBe("SHIP");
123
+ });
124
+
125
+ test("advances phase on PASS: ship->DONE", () => {
126
+ writeFileSync(SF, JSON.stringify(makeState({ phase: "SHIP" }), null, 2));
127
+ const results = [{ check: "all-pass", result: "PASS" as const, detail: "ok" }];
128
+ writeGateResult(SF, "ship", 1, 0, 0, results);
129
+
130
+ const state = JSON.parse(readFileSync(SF, "utf-8"));
131
+ expect(state.phase).toBe("DONE");
132
+ expect(state.mergeCommitSha).toBeTruthy();
133
+ });
134
+
135
+ test("does NOT advance phase on FAIL", () => {
136
+ const results = [{ check: "acs-exist", result: "FAIL" as const, detail: "none" }];
137
+ writeGateResult(SF, "scope", 0, 1, 0, results);
138
+
139
+ const state = JSON.parse(readFileSync(SF, "utf-8"));
140
+ expect(state.phase).toBe("SCOPE");
141
+ });
142
+
143
+ // #514 SC-1: Phase cannot advance to DONE when non-OUTCOME AC has verdict FAIL
144
+ test("blocks ship→DONE when non-OUTCOME AC has FAIL verdict", () => {
145
+ writeFileSync(SF, JSON.stringify(makeState({
146
+ phase: "SHIP",
147
+ acs: [
148
+ {
149
+ id: "SC-1", type: "CODE",
150
+ statement: "function exists and is callable from CLI",
151
+ threshold: { op: "==", value: "true" },
152
+ evidenceMethod: { type: "GREP_CHECK" },
153
+ evidence: { type: "command-output", content: "false" },
154
+ verdict: "FAIL",
155
+ },
156
+ ],
157
+ }), null, 2));
158
+
159
+ const results = [{ check: "all-pass", result: "PASS" as const, detail: "ok" }];
160
+ const { resultVal } = writeGateResult(SF, "ship", 1, 0, 0, results);
161
+
162
+ expect(resultVal).toBe("FAIL");
163
+ const state = JSON.parse(readFileSync(SF, "utf-8"));
164
+ expect(state.phase).not.toBe("DONE");
165
+ });
166
+
167
+ test("blocks verify→SHIP when non-OUTCOME AC has FAIL verdict", () => {
168
+ writeFileSync(SF, JSON.stringify(makeState({
169
+ phase: "VERIFY",
170
+ acs: [
171
+ {
172
+ id: "SC-1", type: "CODE",
173
+ statement: "function exists and is callable from CLI",
174
+ threshold: { op: "==", value: "true" },
175
+ evidenceMethod: { type: "GREP_CHECK" },
176
+ evidence: { type: "command-output", content: "false" },
177
+ verdict: "FAIL",
178
+ },
179
+ ],
180
+ }), null, 2));
181
+
182
+ const results = [{ check: "all-pass", result: "PASS" as const, detail: "ok" }];
183
+ const { resultVal } = writeGateResult(SF, "verify", 1, 0, 0, results);
184
+
185
+ expect(resultVal).toBe("FAIL");
186
+ const state = JSON.parse(readFileSync(SF, "utf-8"));
187
+ expect(state.phase).not.toBe("SHIP");
188
+ });
189
+
190
+ test("allows ship→DONE when OUTCOME AC has FAIL (OUTCOME exemption)", () => {
191
+ writeFileSync(SF, JSON.stringify(makeState({
192
+ phase: "SHIP",
193
+ acs: [
194
+ {
195
+ id: "SC-1", type: "OUTCOME",
196
+ statement: "batch completes with success message visible",
197
+ threshold: { op: "==", value: "true" },
198
+ evidenceMethod: { type: "PLAYWRIGHT" },
199
+ evidence: null,
200
+ verdict: "FAIL",
201
+ },
202
+ ],
203
+ }), null, 2));
204
+
205
+ const results = [{ check: "all-pass", result: "PASS" as const, detail: "ok" }];
206
+ const { resultVal } = writeGateResult(SF, "ship", 1, 0, 0, results);
207
+
208
+ expect(resultVal).toBe("PASS");
209
+ const state = JSON.parse(readFileSync(SF, "utf-8"));
210
+ expect(state.phase).toBe("DONE");
211
+ });
212
+
213
+ test("allows advance when all ACs are PASS or SKIP", () => {
214
+ writeFileSync(SF, JSON.stringify(makeState({
215
+ phase: "SHIP",
216
+ acs: [
217
+ {
218
+ id: "SC-1", type: "CODE",
219
+ statement: "function exists and is callable from CLI",
220
+ threshold: { op: "==", value: "true" },
221
+ evidenceMethod: { type: "GREP_CHECK", command: "echo true" },
222
+ evidence: { type: "command-output", content: "true" },
223
+ verdict: "PASS",
224
+ },
225
+ {
226
+ id: "SC-2", type: "CODE",
227
+ statement: "file includes the expected configuration value",
228
+ threshold: { op: "==", value: "true" },
229
+ evidenceMethod: { type: "FILE_EXISTS" },
230
+ evidence: { type: "manual-attestation", content: "No command" },
231
+ verdict: "SKIP",
232
+ },
233
+ ],
234
+ }), null, 2));
235
+
236
+ const results = [{ check: "all-pass", result: "PASS" as const, detail: "ok" }];
237
+ const { resultVal } = writeGateResult(SF, "ship", 1, 0, 0, results);
238
+
239
+ expect(resultVal).toBe("PASS");
240
+ const state = JSON.parse(readFileSync(SF, "utf-8"));
241
+ expect(state.phase).toBe("DONE");
242
+ });
243
+
244
+ // #514 SC-2: Ship gate failure resets phase to VERIFY
245
+ test("ship gate FAIL resets phase to VERIFY", () => {
246
+ writeFileSync(SF, JSON.stringify(makeState({ phase: "SHIP" }), null, 2));
247
+ const results = [{ check: "code-committed", result: "FAIL" as const, detail: "not committed" }];
248
+ writeGateResult(SF, "ship", 0, 1, 0, results);
249
+
250
+ const state = JSON.parse(readFileSync(SF, "utf-8"));
251
+ expect(state.phase).toBe("VERIFY");
252
+ const regression = state.changelog.find((e: any) => e.event === "ship-fail-regression");
253
+ expect(regression).toBeTruthy();
254
+ });
255
+
256
+ test("increments attempt number", () => {
257
+ const results = [{ check: "x", result: "FAIL" as const, detail: "d" }];
258
+ writeGateResult(SF, "scope", 0, 1, 0, results);
259
+ writeGateResult(SF, "scope", 0, 1, 0, results);
260
+
261
+ const state = JSON.parse(readFileSync(SF, "utf-8"));
262
+ expect(state.gates.scope.attempt).toBe(2);
263
+ });
264
+ });
265
+
266
+ // ═══ generateHmac ══════════════════════════════════════════════════════
267
+
268
+ describe("generateHmac", () => {
269
+ test("computes hash and writes to workflow state", () => {
270
+ writeFileSync(SF, JSON.stringify(makeState({
271
+ phase: "SHIP",
272
+ gates: { ship: { result: "PASS", attempt: 1, failures: [], ts: "t" } },
273
+ }), null, 2));
274
+
275
+ const hash = generateHmac(SF, "test-orch", 432);
276
+ expect(hash).toHaveLength(16);
277
+ expect(/^[0-9a-f]{16}$/.test(hash)).toBe(true);
278
+
279
+ const state = JSON.parse(readFileSync(SF, "utf-8"));
280
+ expect(state.gates.ship.hash).toBe(hash);
281
+ });
282
+
283
+ test("salt file has chmod 600", () => {
284
+ const saltPath = join(process.env.HOME || "", ".claude", "hooks", "lib", ".gate-salt");
285
+ generateHmac(SF, "test-orch", 432);
286
+
287
+ const perms = statSync(saltPath).mode & 0o777;
288
+ expect(perms).toBe(0o600);
289
+ });
290
+ });
291
+
292
+ // ═══ runConvergence ════════════════════════════════════════════════════
293
+
294
+ describe("runConvergence", () => {
295
+ test("skips when gate != verify or result != FAIL", () => {
296
+ const r1 = runConvergence(SF, "scope", "FAIL", PROFILE);
297
+ expect(r1.action).toBe("skip");
298
+
299
+ const r2 = runConvergence(SF, "verify", "PASS", PROFILE);
300
+ expect(r2.action).toBe("skip");
301
+ });
302
+
303
+ test("fires circuit breaker at maxIterations", () => {
304
+ const changelog = Array.from({ length: 4 }, (_, i) => ({
305
+ ts: "t",
306
+ event: "iteration-loopback",
307
+ detail: `iteration ${i + 1}`,
308
+ failureHash: `fail-${i}`,
309
+ actor: "gate-runner",
310
+ }));
311
+
312
+ writeFileSync(SF, JSON.stringify(makeState({
313
+ phase: "VERIFY",
314
+ changelog,
315
+ gates: { verify: { result: "FAIL", failures: [{ check: "x", result: "FAIL", detail: "d" }] } },
316
+ }), null, 2));
317
+
318
+ const result = runConvergence(SF, "verify", "FAIL", PROFILE);
319
+ expect(result.action).toBe("circuit-break");
320
+ expect(result.iteration).toBe(5);
321
+
322
+ const state = JSON.parse(readFileSync(SF, "utf-8"));
323
+ expect(state.phase).toBe("CIRCUIT_BREAK");
324
+ });
325
+
326
+ test("detects stall: same failureHash in consecutive iterations", () => {
327
+ const changelog = [
328
+ {
329
+ ts: "t",
330
+ event: "iteration-loopback",
331
+ detail: "iteration 1",
332
+ failureHash: "x",
333
+ actor: "gate-runner",
334
+ },
335
+ ];
336
+
337
+ writeFileSync(SF, JSON.stringify(makeState({
338
+ phase: "VERIFY",
339
+ changelog,
340
+ gates: { verify: { result: "FAIL", failures: [{ check: "x", result: "FAIL", detail: "d" }] } },
341
+ }), null, 2));
342
+
343
+ const result = runConvergence(SF, "verify", "FAIL", PROFILE);
344
+ expect(result.action).toBe("stall");
345
+
346
+ const state = JSON.parse(readFileSync(SF, "utf-8"));
347
+ const councilEvents = state.changelog.filter((e: any) => e.event === "council-recommended");
348
+ expect(councilEvents.length).toBe(1);
349
+ });
350
+
351
+ test("preserves read-before-append ordering", () => {
352
+ writeFileSync(SF, JSON.stringify(makeState({
353
+ phase: "VERIFY",
354
+ changelog: [],
355
+ gates: { verify: { result: "FAIL", failures: [{ check: "a", result: "FAIL", detail: "d" }] } },
356
+ }), null, 2));
357
+
358
+ // First iteration — no previous hash to compare
359
+ const r1 = runConvergence(SF, "verify", "FAIL", PROFILE);
360
+ expect(r1.action).toBe("loop-back");
361
+
362
+ // Second iteration with different failures — should NOT be stall
363
+ const state2 = JSON.parse(readFileSync(SF, "utf-8"));
364
+ state2.gates.verify.failures = [{ check: "b", result: "FAIL", detail: "d" }];
365
+ writeFileSync(SF, JSON.stringify(state2, null, 2));
366
+
367
+ const r2 = runConvergence(SF, "verify", "FAIL", PROFILE);
368
+ expect(r2.action).toBe("loop-back");
369
+
370
+ const finalState = JSON.parse(readFileSync(SF, "utf-8"));
371
+ const loopbacks = finalState.changelog.filter((e: any) => e.event === "iteration-loopback");
372
+ expect(loopbacks).toHaveLength(2);
373
+ expect(loopbacks[0].failureHash).not.toBe(loopbacks[1].failureHash);
374
+ });
375
+
376
+ test("loops back to BUILD phase", () => {
377
+ writeFileSync(SF, JSON.stringify(makeState({
378
+ phase: "VERIFY",
379
+ gates: { verify: { result: "FAIL", failures: [{ check: "x", result: "FAIL", detail: "d" }] } },
380
+ }), null, 2));
381
+
382
+ runConvergence(SF, "verify", "FAIL", PROFILE);
383
+
384
+ const state = JSON.parse(readFileSync(SF, "utf-8"));
385
+ expect(state.phase).toBe("BUILD");
386
+ expect(state.phaseTimings.BUILD).toHaveLength(1);
387
+ });
388
+ });
389
+
390
+ // ═══ generateShipEvidence ══════════════════════════════════════════════
391
+
392
+ describe("generateShipEvidence", () => {
393
+ test("writes valid ship-evidence.json", () => {
394
+ writeFileSync(SF, JSON.stringify(makeState({
395
+ phase: "SHIP",
396
+ acs: [
397
+ {
398
+ id: "SC-1",
399
+ statement: "file exists on disk and is accessible",
400
+ verdict: "PASS",
401
+ evidence: { type: "file-citation", content: "found" },
402
+ threshold: { op: "==", value: "true" },
403
+ evidenceMethod: { type: "FILE_EXISTS" },
404
+ },
405
+ ],
406
+ }), null, 2));
407
+
408
+ const path = generateShipEvidence(SF, TMP_DIR, 432, "", 1, 0, 0);
409
+
410
+ expect(existsSync(path)).toBe(true);
411
+ const evidence = JSON.parse(readFileSync(path, "utf-8"));
412
+ expect(evidence.contractVersion).toBe("1.0");
413
+ expect(evidence.issueNumber).toBe(432);
414
+ expect(evidence.gateOut.status).toBe("PASS");
415
+ expect(evidence.gateOut.evidence).toHaveLength(1);
416
+ expect(evidence.mergeCommitSha).toBeTruthy();
417
+ expect(evidence.capturedAt).toBeTruthy();
418
+ expect(evidence.iterationCount).toBeGreaterThanOrEqual(1);
419
+ expect(evidence.sizing.predicted).toBe("M");
420
+ });
421
+ });
422
+
423
+ // ═══ writeGateSummary ══════════════════════════════════════════════════
424
+
425
+ describe("writeGateSummary", () => {
426
+ test("writes gate-pending.json on FAIL", () => {
427
+ const results = [
428
+ { check: "acs-exist", result: "FAIL" as const, detail: "no ACs" },
429
+ ];
430
+
431
+ const outcome = writeGateSummary("scope", 0, 1, 0, results, 432, "test", "sess-1");
432
+
433
+ expect(outcome).toBe("BLOCKED");
434
+ expect(existsSync(PENDING)).toBe(true);
435
+
436
+ const pending = JSON.parse(readFileSync(PENDING, "utf-8"));
437
+ expect(pending.gate).toBe("scope");
438
+ expect(pending.issue).toBe(432);
439
+ expect(pending.failures).toHaveLength(1);
440
+ expect(pending.failures[0].check).toBe("acs-exist");
441
+ expect(pending.strike_count).toBe(0);
442
+ expect(pending.max_strikes).toBe(3);
443
+ });
444
+
445
+ test("deletes gate-pending.json on PASS", () => {
446
+ // Create a pending file first
447
+ mkdirSync(join(process.env.HOME || "", ".claude", "MEMORY", "STATE"), { recursive: true });
448
+ writeFileSync(PENDING, JSON.stringify({ gate: "old" }));
449
+ expect(existsSync(PENDING)).toBe(true);
450
+
451
+ const results = [
452
+ { check: "acs-exist", result: "PASS" as const, detail: "3 ACs" },
453
+ ];
454
+ const outcome = writeGateSummary("scope", 1, 0, 0, results, 432, "test", "sess-1");
455
+
456
+ expect(outcome).toBe("ALL CLEAR");
457
+ expect(existsSync(PENDING)).toBe(false);
458
+ });
459
+ });
460
+
461
+ // ═══ writeWorkflowState ══════════════════════════════════════════════════
462
+
463
+ describe("writeWorkflowState", () => {
464
+ test("writes valid state to disk", () => {
465
+ const state = makeState();
466
+ writeWorkflowState(SF, state);
467
+ const written = JSON.parse(readFileSync(SF, "utf-8"));
468
+ expect(written.schemaVersion).toBe(2);
469
+ expect(written.phase).toBe("SCOPE");
470
+ expect(written.slug).toBe("test-orch");
471
+ });
472
+
473
+ test("throws on invalid phase enum", () => {
474
+ const state = makeState({ phase: "INVALID_PHASE" });
475
+ expect(() => writeWorkflowState(SF, state)).toThrow(/expected/i);
476
+ });
477
+
478
+ test("throws on invalid threshold op 'eq'", () => {
479
+ const state = makeState({
480
+ acs: [{
481
+ id: "SC-1",
482
+ statement: "test fixture validates gate behavior correctly",
483
+ threshold: { op: "eq", value: "true" },
484
+ evidenceMethod: { type: "GREP_CHECK" },
485
+ evidence: null,
486
+ }],
487
+ });
488
+ expect(() => writeWorkflowState(SF, state)).toThrow(/expected/i);
489
+ });
490
+
491
+ test("error message includes valid enum values", () => {
492
+ const state = makeState({ phase: "BOGUS" });
493
+ try {
494
+ writeWorkflowState(SF, state);
495
+ throw new Error("should have thrown");
496
+ } catch (err: any) {
497
+ expect(err.message).toContain("GOAL");
498
+ expect(err.message).toContain("BUILD");
499
+ expect(err.message).toContain("VERIFY");
500
+ }
501
+ });
502
+
503
+ test("passes through extra fields via passthrough()", () => {
504
+ const state = { ...makeState(), customField: "preserved" };
505
+ writeWorkflowState(SF, state);
506
+ const written = JSON.parse(readFileSync(SF, "utf-8"));
507
+ expect(written.customField).toBe("preserved");
508
+ });
509
+ });
510
+
511
+ // ═══ initWorkflow ════════════════════════════════════════════════════════
512
+
513
+ describe("initWorkflow", () => {
514
+ test("creates workflow-state.json with correct initial structure", () => {
515
+ const target = join(TMP_DIR, "sub", "workflow-state.json");
516
+ initWorkflow(target, {
517
+ issue: 453,
518
+ repo: "hornjason/pai-config",
519
+ projectRoot: "/tmp/test",
520
+ slug: "write-time-validation",
521
+ issueGoal: "Add Zod write-time validation",
522
+ });
523
+
524
+ expect(existsSync(target)).toBe(true);
525
+ const state = JSON.parse(readFileSync(target, "utf-8"));
526
+ expect(state.schemaVersion).toBe(2);
527
+ expect(state.phase).toBe("GOAL");
528
+ expect(state.acs).toEqual([]);
529
+ expect(state.gates).toEqual({});
530
+ expect(state.changelog).toEqual([]);
531
+ expect(state.issue).toBe(453);
532
+ expect(state.slug).toBe("write-time-validation");
533
+ expect(state.issueGoal).toBe("Add Zod write-time validation");
534
+ });
535
+
536
+ test("sets issueRepo to opts.issueRepo when provided", () => {
537
+ const target = join(TMP_DIR, "init-test-1.json");
538
+ initWorkflow(target, {
539
+ issue: 453,
540
+ repo: "hornjason/pai-config",
541
+ issueRepo: "hornjason/asaCommandCenter",
542
+ projectRoot: "/tmp/test",
543
+ slug: "test",
544
+ issueGoal: "test goal",
545
+ });
546
+
547
+ const state = JSON.parse(readFileSync(target, "utf-8"));
548
+ expect(state.issueRepo).toBe("hornjason/asaCommandCenter");
549
+ });
550
+
551
+ test("defaults issueRepo to opts.repo when not provided", () => {
552
+ const target = join(TMP_DIR, "init-test-2.json");
553
+ initWorkflow(target, {
554
+ issue: 453,
555
+ repo: "hornjason/pai-config",
556
+ projectRoot: "/tmp/test",
557
+ slug: "test",
558
+ issueGoal: "test goal",
559
+ });
560
+
561
+ const state = JSON.parse(readFileSync(target, "utf-8"));
562
+ expect(state.issueRepo).toBe("hornjason/pai-config");
563
+ });
564
+
565
+ test("creates parent directory if it doesn't exist", () => {
566
+ const target = join(TMP_DIR, "deep", "nested", "dir", "workflow-state.json");
567
+ initWorkflow(target, {
568
+ issue: 453,
569
+ repo: "hornjason/pai-config",
570
+ projectRoot: "/tmp/test",
571
+ slug: "test",
572
+ issueGoal: "test goal",
573
+ });
574
+
575
+ expect(existsSync(target)).toBe(true);
576
+ });
577
+
578
+ test("includes optional fields when provided", () => {
579
+ const target = join(TMP_DIR, "init-opts.json");
580
+ initWorkflow(target, {
581
+ issue: 453,
582
+ repo: "hornjason/pai-config",
583
+ projectRoot: "/tmp/test",
584
+ slug: "test",
585
+ issueGoal: "test goal",
586
+ sizing: { predicted: "M", ceremonyTier: "STANDARD" },
587
+ sourceSpecs: [{ path: "spec.md", citedInDiscovery: true }],
588
+ bootstrappedFrom: "session-abc",
589
+ });
590
+
591
+ const state = JSON.parse(readFileSync(target, "utf-8"));
592
+ expect(state.sizing.predicted).toBe("M");
593
+ expect(state.sourceSpecs).toHaveLength(1);
594
+ expect(state.bootstrappedFrom).toBe("session-abc");
595
+ });
596
+ });
597
+
598
+ // ═══ writeACs ════════════════════════════════════════════════════════════
599
+
600
+ describe("writeACs", () => {
601
+ test("writes ACs with evidence null and verdict PENDING", () => {
602
+ writeFileSync(SF, JSON.stringify(makeState(), null, 2));
603
+ writeACs(SF, [
604
+ {
605
+ id: "SC-1",
606
+ type: "CODE",
607
+ statement: "writeWorkflowState function exists and is callable",
608
+ threshold: { op: "==", value: "true" },
609
+ evidenceMethod: { type: "GREP_CHECK" },
610
+ },
611
+ {
612
+ id: "SC-2",
613
+ type: "OUTCOME",
614
+ statement: "invalid enum value throws validation error",
615
+ threshold: { op: ">=", value: "2" },
616
+ evidenceMethod: { type: "BUN_TEST" },
617
+ },
618
+ ]);
619
+
620
+ const state = JSON.parse(readFileSync(SF, "utf-8"));
621
+ expect(state.acs).toHaveLength(2);
622
+ expect(state.acs[0].evidence).toBeNull();
623
+ expect(state.acs[0].verdict).toBe("PENDING");
624
+ expect(state.acs[1].evidence).toBeNull();
625
+ expect(state.acs[1].verdict).toBe("PENDING");
626
+ });
627
+
628
+ test("throws on invalid AC type enum", () => {
629
+ writeFileSync(SF, JSON.stringify(makeState(), null, 2));
630
+ expect(() => writeACs(SF, [
631
+ {
632
+ id: "SC-1",
633
+ type: "INVALID_TYPE" as any,
634
+ statement: "test fixture validates gate behavior correctly",
635
+ threshold: { op: "==", value: "true" },
636
+ evidenceMethod: { type: "GREP_CHECK" },
637
+ },
638
+ ])).toThrow();
639
+ });
640
+
641
+ test("throws on invalid threshold op", () => {
642
+ writeFileSync(SF, JSON.stringify(makeState(), null, 2));
643
+ expect(() => writeACs(SF, [
644
+ {
645
+ id: "SC-1",
646
+ statement: "test fixture validates gate behavior correctly",
647
+ threshold: { op: "eq" as any, value: "true" },
648
+ evidenceMethod: { type: "GREP_CHECK" },
649
+ },
650
+ ])).toThrow();
651
+ });
652
+
653
+ test("preserves existing state fields", () => {
654
+ writeFileSync(SF, JSON.stringify(makeState({
655
+ phase: "BUILD",
656
+ gates: { scope: { result: "PASS", attempt: 1 } },
657
+ }), null, 2));
658
+
659
+ writeACs(SF, [
660
+ {
661
+ id: "SC-1",
662
+ statement: "test fixture validates gate behavior correctly",
663
+ threshold: { op: "==", value: "true" },
664
+ evidenceMethod: { type: "GREP_CHECK" },
665
+ },
666
+ ]);
667
+
668
+ const state = JSON.parse(readFileSync(SF, "utf-8"));
669
+ expect(state.phase).toBe("BUILD");
670
+ expect(state.gates.scope.result).toBe("PASS");
671
+ expect(state.acs).toHaveLength(1);
672
+ });
673
+ });
674
+
675
+ // ═══ AFK Batch Plan (#468) ═══════════════════════════════════════════
676
+
677
+ describe("initBatchPlan", () => {
678
+ test("creates valid afk-batch.json with planned issues", () => {
679
+ const batchDir = join(TMP_DIR, "batch");
680
+ initBatchPlan(batchDir, [100, 101, 102]);
681
+
682
+ const fp = join(batchDir, "afk-batch.json");
683
+ expect(existsSync(fp)).toBe(true);
684
+
685
+ const plan = JSON.parse(readFileSync(fp, "utf-8"));
686
+ expect(plan.planned).toEqual([100, 101, 102]);
687
+ expect(plan.shipped).toEqual([]);
688
+ expect(plan.skipped).toEqual([]);
689
+ expect(plan.inProgress).toBeNull();
690
+ expect(plan.approvedAt).toBeTruthy();
691
+ });
692
+
693
+ test("creates parent directory if needed", () => {
694
+ const batchDir = join(TMP_DIR, "deep", "nested", "batch");
695
+ initBatchPlan(batchDir, [200]);
696
+
697
+ expect(existsSync(join(batchDir, "afk-batch.json"))).toBe(true);
698
+ });
699
+ });
700
+
701
+ describe("recordBatchShipped", () => {
702
+ test("moves issue to shipped array", () => {
703
+ const batchDir = join(TMP_DIR, "batch-ship");
704
+ initBatchPlan(batchDir, [100, 101, 102]);
705
+ recordBatchShipped(batchDir, 100);
706
+
707
+ const plan = JSON.parse(readFileSync(join(batchDir, "afk-batch.json"), "utf-8"));
708
+ expect(plan.shipped).toEqual([100]);
709
+ expect(plan.planned).toEqual([100, 101, 102]);
710
+ });
711
+
712
+ test("does not duplicate if called twice", () => {
713
+ const batchDir = join(TMP_DIR, "batch-ship-dup");
714
+ initBatchPlan(batchDir, [100]);
715
+ recordBatchShipped(batchDir, 100);
716
+ recordBatchShipped(batchDir, 100);
717
+
718
+ const plan = JSON.parse(readFileSync(join(batchDir, "afk-batch.json"), "utf-8"));
719
+ expect(plan.shipped).toEqual([100]);
720
+ });
721
+
722
+ test("clears inProgress when shipped issue matches", () => {
723
+ const batchDir = join(TMP_DIR, "batch-ship-ip");
724
+ initBatchPlan(batchDir, [100, 101]);
725
+
726
+ // Manually set inProgress
727
+ const fp = join(batchDir, "afk-batch.json");
728
+ const plan = JSON.parse(readFileSync(fp, "utf-8"));
729
+ plan.inProgress = 100;
730
+ writeFileSync(fp, JSON.stringify(plan, null, 2));
731
+
732
+ recordBatchShipped(batchDir, 100);
733
+
734
+ const updated = JSON.parse(readFileSync(fp, "utf-8"));
735
+ expect(updated.inProgress).toBeNull();
736
+ });
737
+ });
738
+
739
+ describe("recordBatchSkipped", () => {
740
+ test("rejects 'needs investigation' as skip reason", () => {
741
+ const batchDir = join(TMP_DIR, "batch-skip-reject");
742
+ initBatchPlan(batchDir, [100]);
743
+
744
+ expect(() => recordBatchSkipped(batchDir, 100, "needs investigation")).toThrow(/Rejected skip reason/);
745
+ });
746
+
747
+ test("rejects 'needs browser testing' as skip reason", () => {
748
+ const batchDir = join(TMP_DIR, "batch-skip-reject2");
749
+ initBatchPlan(batchDir, [100]);
750
+
751
+ expect(() => recordBatchSkipped(batchDir, 100, "needs browser testing")).toThrow(/Rejected skip reason/);
752
+ });
753
+
754
+ test("rejects 'can't do from here' as skip reason", () => {
755
+ const batchDir = join(TMP_DIR, "batch-skip-reject3");
756
+ initBatchPlan(batchDir, [100]);
757
+
758
+ expect(() => recordBatchSkipped(batchDir, 100, "can't do from here")).toThrow(/Rejected skip reason/);
759
+ });
760
+
761
+ test("accepts valid skip reason with delegation action", () => {
762
+ const batchDir = join(TMP_DIR, "batch-skip-valid");
763
+ initBatchPlan(batchDir, [100, 101]);
764
+
765
+ recordBatchSkipped(batchDir, 100, "spawned Quinn, awaiting results", "Quinn QA pass pending");
766
+
767
+ const plan = JSON.parse(readFileSync(join(batchDir, "afk-batch.json"), "utf-8"));
768
+ expect(plan.skipped).toHaveLength(1);
769
+ expect(plan.skipped[0].issue).toBe(100);
770
+ expect(plan.skipped[0].reason).toBe("spawned Quinn, awaiting results");
771
+ expect(plan.skipped[0].delegationAction).toBe("Quinn QA pass pending");
772
+ });
773
+
774
+ test("accepts 'closed as by-design with evidence' as skip reason", () => {
775
+ const batchDir = join(TMP_DIR, "batch-skip-bydesign");
776
+ initBatchPlan(batchDir, [100]);
777
+
778
+ recordBatchSkipped(batchDir, 100, "closed as by-design with evidence");
779
+
780
+ const plan = JSON.parse(readFileSync(join(batchDir, "afk-batch.json"), "utf-8"));
781
+ expect(plan.skipped).toHaveLength(1);
782
+ });
783
+
784
+ test("clears inProgress when skipped issue matches", () => {
785
+ const batchDir = join(TMP_DIR, "batch-skip-ip");
786
+ initBatchPlan(batchDir, [100]);
787
+
788
+ const fp = join(batchDir, "afk-batch.json");
789
+ const plan = JSON.parse(readFileSync(fp, "utf-8"));
790
+ plan.inProgress = 100;
791
+ writeFileSync(fp, JSON.stringify(plan, null, 2));
792
+
793
+ recordBatchSkipped(batchDir, 100, "closed as by-design with evidence");
794
+
795
+ const updated = JSON.parse(readFileSync(fp, "utf-8"));
796
+ expect(updated.inProgress).toBeNull();
797
+ });
798
+ });
799
+
800
+ describe("validateBatchComplete", () => {
801
+ test("returns complete when all planned issues are shipped", () => {
802
+ const batchDir = join(TMP_DIR, "batch-complete");
803
+ initBatchPlan(batchDir, [100, 101, 102]);
804
+ recordBatchShipped(batchDir, 100);
805
+ recordBatchShipped(batchDir, 101);
806
+ recordBatchShipped(batchDir, 102);
807
+
808
+ const result = validateBatchComplete(batchDir);
809
+ expect(result.complete).toBe(true);
810
+ expect(result.missing).toEqual([]);
811
+ });
812
+
813
+ test("returns complete when mix of shipped and skipped covers all planned", () => {
814
+ const batchDir = join(TMP_DIR, "batch-complete-mix");
815
+ initBatchPlan(batchDir, [100, 101, 102]);
816
+ recordBatchShipped(batchDir, 100);
817
+ recordBatchShipped(batchDir, 101);
818
+ recordBatchSkipped(batchDir, 102, "closed as by-design with evidence");
819
+
820
+ const result = validateBatchComplete(batchDir);
821
+ expect(result.complete).toBe(true);
822
+ expect(result.missing).toEqual([]);
823
+ });
824
+
825
+ test("returns incomplete with missing issues", () => {
826
+ const batchDir = join(TMP_DIR, "batch-incomplete");
827
+ initBatchPlan(batchDir, [100, 101, 102]);
828
+ recordBatchShipped(batchDir, 100);
829
+
830
+ const result = validateBatchComplete(batchDir);
831
+ expect(result.complete).toBe(false);
832
+ expect(result.missing).toEqual([101, 102]);
833
+ });
834
+
835
+ test("returns complete for empty plan", () => {
836
+ const batchDir = join(TMP_DIR, "batch-empty");
837
+ initBatchPlan(batchDir, []);
838
+
839
+ const result = validateBatchComplete(batchDir);
840
+ expect(result.complete).toBe(true);
841
+ expect(result.missing).toEqual([]);
842
+ });
843
+ });
844
+
845
+ // ═══ markCommandlessACsAsSkip (#513) ══════════════════════════════════
846
+
847
+ describe("markCommandlessACsAsSkip", () => {
848
+ test("marks ACs without evidenceMethod.command as SKIP", () => {
849
+ writeFileSync(SF, JSON.stringify(makeState({
850
+ acs: [
851
+ {
852
+ id: "SC-1",
853
+ statement: "function exists and is callable from CLI",
854
+ threshold: { op: "==", value: "true" },
855
+ evidenceMethod: { type: "FILE_EXISTS" },
856
+ evidence: null,
857
+ verdict: "PENDING",
858
+ },
859
+ ],
860
+ }), null, 2));
861
+
862
+ const count = markCommandlessACsAsSkip(SF);
863
+ expect(count).toBe(1);
864
+
865
+ const state = JSON.parse(readFileSync(SF, "utf-8"));
866
+ expect(state.acs[0].verdict).toBe("SKIP");
867
+ expect(state.acs[0].evidence.content).toContain("No evidenceMethod.command");
868
+ });
869
+
870
+ test("does not touch ACs with a command", () => {
871
+ writeFileSync(SF, JSON.stringify(makeState({
872
+ acs: [
873
+ {
874
+ id: "SC-1",
875
+ statement: "function exists and is callable from CLI",
876
+ threshold: { op: "==", value: "true" },
877
+ evidenceMethod: { type: "COMMAND", command: "echo true" },
878
+ evidence: null,
879
+ verdict: "PENDING",
880
+ },
881
+ ],
882
+ }), null, 2));
883
+
884
+ const count = markCommandlessACsAsSkip(SF);
885
+ expect(count).toBe(0);
886
+
887
+ const state = JSON.parse(readFileSync(SF, "utf-8"));
888
+ expect(state.acs[0].verdict).toBe("PENDING");
889
+ });
890
+
891
+ test("skips ACs that already have non-PENDING verdict", () => {
892
+ writeFileSync(SF, JSON.stringify(makeState({
893
+ acs: [
894
+ {
895
+ id: "SC-1",
896
+ statement: "function exists and is callable from CLI",
897
+ threshold: { op: "==", value: "true" },
898
+ evidenceMethod: { type: "FILE_EXISTS" },
899
+ evidence: { type: "command-output", content: "true" },
900
+ verdict: "PASS",
901
+ },
902
+ ],
903
+ }), null, 2));
904
+
905
+ const count = markCommandlessACsAsSkip(SF);
906
+ expect(count).toBe(0);
907
+
908
+ const state = JSON.parse(readFileSync(SF, "utf-8"));
909
+ expect(state.acs[0].verdict).toBe("PASS");
910
+ });
911
+
912
+ test("handles mixed ACs — only marks commandless PENDING ones", () => {
913
+ writeFileSync(SF, JSON.stringify(makeState({
914
+ acs: [
915
+ {
916
+ id: "SC-1",
917
+ statement: "function exists and is callable from CLI",
918
+ threshold: { op: "==", value: "true" },
919
+ evidenceMethod: { type: "COMMAND", command: "echo true" },
920
+ evidence: null,
921
+ verdict: "PENDING",
922
+ },
923
+ {
924
+ id: "SC-2",
925
+ statement: "file includes the expected configuration value",
926
+ threshold: { op: "==", value: "true" },
927
+ evidenceMethod: { type: "FILE_EXISTS" },
928
+ evidence: null,
929
+ verdict: "PENDING",
930
+ },
931
+ {
932
+ id: "SC-3",
933
+ statement: "output matches the expected formatting pattern",
934
+ threshold: { op: "==", value: "true" },
935
+ evidenceMethod: { type: "COMMAND", command: "echo true" },
936
+ evidence: { type: "command-output", content: "true" },
937
+ verdict: "PASS",
938
+ },
939
+ ],
940
+ }), null, 2));
941
+
942
+ const count = markCommandlessACsAsSkip(SF);
943
+ expect(count).toBe(1);
944
+
945
+ const state = JSON.parse(readFileSync(SF, "utf-8"));
946
+ expect(state.acs[0].verdict).toBe("PENDING");
947
+ expect(state.acs[1].verdict).toBe("SKIP");
948
+ expect(state.acs[2].verdict).toBe("PASS");
949
+ });
950
+ });