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,511 @@
1
+ import { test, expect, describe, beforeEach, afterEach } from "bun:test";
2
+ import { mkdirSync, writeFileSync, readFileSync, rmSync, readdirSync, existsSync } from "fs";
3
+ import { join } from "path";
4
+
5
+ const TEST_BASE = `/tmp/self-heal-test-${process.pid}`;
6
+ const TEST_SLUG = `heal-test-${process.pid}`;
7
+ const WORK_DIR = join(TEST_BASE, TEST_SLUG);
8
+ const SF = join(WORK_DIR, "workflow-state.json");
9
+
10
+ function minimalState(overrides: Record<string, any> = {}): Record<string, any> {
11
+ return {
12
+ schemaVersion: 2,
13
+ issue: 9999,
14
+ repo: "test/repo",
15
+ issueRepo: "test/repo",
16
+ projectRoot: "/tmp/test-project",
17
+ slug: TEST_SLUG,
18
+ phase: "BUILD",
19
+ issueGoal: "Test goal for self-heal validation",
20
+ acs: [],
21
+ gates: {},
22
+ changelog: [],
23
+ ...overrides,
24
+ };
25
+ }
26
+
27
+ function createState(overrides: Record<string, any> = {}): void {
28
+ mkdirSync(WORK_DIR, { recursive: true });
29
+ writeFileSync(SF, JSON.stringify(minimalState(overrides), null, 2));
30
+ }
31
+
32
+ function readState(): any {
33
+ return JSON.parse(readFileSync(SF, "utf-8"));
34
+ }
35
+
36
+ import { runWithHeal, snapshotProtectedFields, checkIntegrity, type HealResult } from "./self-heal";
37
+
38
+ describe("self-heal", () => {
39
+ beforeEach(() => {
40
+ mkdirSync(WORK_DIR, { recursive: true });
41
+ });
42
+
43
+ afterEach(() => {
44
+ try { rmSync(TEST_BASE, { recursive: true, force: true }); } catch {}
45
+ });
46
+
47
+ describe("runWithHeal", () => {
48
+ test("returns PASS immediately when gate passes on first attempt", async () => {
49
+ createState({
50
+ phase: "SCOPE",
51
+ gates: { scope: { result: "PASS", attempt: 1, failures: [], ts: new Date().toISOString(), commitSha: "abc123" } },
52
+ });
53
+
54
+ const result = await runWithHeal({
55
+ gate: "scope",
56
+ slug: TEST_SLUG,
57
+ issue: 9999,
58
+ projectRoot: "/tmp/test-project",
59
+ workDir: WORK_DIR,
60
+ maxAttempts: 3,
61
+ dryRun: true,
62
+ });
63
+
64
+ expect(result.result).toBe("PASS");
65
+ expect(result.attempts).toBe(1);
66
+ expect(result.failures).toBeUndefined();
67
+ expect(result.circuitBroken).toBeUndefined();
68
+ });
69
+
70
+ test("returns FAIL with failures when gate fails", async () => {
71
+ createState({
72
+ phase: "BUILD",
73
+ gates: {
74
+ verify: {
75
+ result: "FAIL",
76
+ attempt: 1,
77
+ failures: [
78
+ { check: "tests-pass", result: "FAIL", detail: "3 tests failed" },
79
+ { check: "tsc-pass", result: "FAIL", detail: "type errors" },
80
+ ],
81
+ ts: new Date().toISOString(),
82
+ commitSha: "abc123",
83
+ },
84
+ },
85
+ });
86
+
87
+ const result = await runWithHeal({
88
+ gate: "verify",
89
+ slug: TEST_SLUG,
90
+ issue: 9999,
91
+ projectRoot: "/tmp/test-project",
92
+ workDir: WORK_DIR,
93
+ maxAttempts: 3,
94
+ dryRun: true,
95
+ });
96
+
97
+ expect(result.result).toBe("FAIL");
98
+ expect(result.attempts).toBe(1);
99
+ expect(result.failures).toHaveLength(2);
100
+ expect(result.failures![0].check).toBe("tests-pass");
101
+ expect(result.circuitBroken).toBeFalsy();
102
+ });
103
+
104
+ test("circuit breaks at maxAttempts", async () => {
105
+ createState({
106
+ phase: "BUILD",
107
+ gates: {
108
+ verify: {
109
+ result: "FAIL",
110
+ attempt: 3,
111
+ failures: [{ check: "tests-pass", result: "FAIL", detail: "still failing" }],
112
+ ts: new Date().toISOString(),
113
+ commitSha: "abc123",
114
+ },
115
+ },
116
+ });
117
+
118
+ const result = await runWithHeal({
119
+ gate: "verify",
120
+ slug: TEST_SLUG,
121
+ issue: 9999,
122
+ projectRoot: "/tmp/test-project",
123
+ workDir: WORK_DIR,
124
+ maxAttempts: 3,
125
+ dryRun: true,
126
+ });
127
+
128
+ expect(result.result).toBe("FAIL");
129
+ expect(result.circuitBroken).toBe(true);
130
+ });
131
+
132
+ test("defaults maxAttempts to 3", async () => {
133
+ createState({
134
+ phase: "BUILD",
135
+ gates: {
136
+ scope: {
137
+ result: "FAIL",
138
+ attempt: 3,
139
+ failures: [{ check: "acs-exist", result: "FAIL", detail: "no ACs" }],
140
+ ts: new Date().toISOString(),
141
+ commitSha: "abc123",
142
+ },
143
+ },
144
+ });
145
+
146
+ const result = await runWithHeal({
147
+ gate: "scope",
148
+ slug: TEST_SLUG,
149
+ issue: 9999,
150
+ projectRoot: "/tmp/test-project",
151
+ workDir: WORK_DIR,
152
+ dryRun: true,
153
+ });
154
+
155
+ expect(result.circuitBroken).toBe(true);
156
+ });
157
+
158
+ test("validates gate name", async () => {
159
+ createState();
160
+
161
+ await expect(
162
+ runWithHeal({
163
+ gate: "invalid" as any,
164
+ slug: TEST_SLUG,
165
+ issue: 9999,
166
+ projectRoot: "/tmp/test-project",
167
+ workDir: WORK_DIR,
168
+ dryRun: true,
169
+ }),
170
+ ).rejects.toThrow(/invalid gate/i);
171
+ });
172
+
173
+ test("handles missing workflow-state.json", async () => {
174
+ await expect(
175
+ runWithHeal({
176
+ gate: "scope",
177
+ slug: TEST_SLUG,
178
+ issue: 9999,
179
+ projectRoot: "/tmp/test-project",
180
+ workDir: "/tmp/nonexistent-dir-xyz",
181
+ dryRun: true,
182
+ }),
183
+ ).rejects.toThrow(/workflow-state\.json/i);
184
+ });
185
+
186
+ test("returns failures as structured objects", async () => {
187
+ const failures = [
188
+ { check: "all-acs-pass", result: "FAIL", detail: "AC-1 PENDING, AC-2 FAIL" },
189
+ { check: "code-committed", result: "FAIL", detail: "uncommitted changes" },
190
+ ];
191
+
192
+ createState({
193
+ phase: "BUILD",
194
+ gates: {
195
+ verify: {
196
+ result: "FAIL",
197
+ attempt: 1,
198
+ failures,
199
+ ts: new Date().toISOString(),
200
+ commitSha: "abc123",
201
+ },
202
+ },
203
+ });
204
+
205
+ const result = await runWithHeal({
206
+ gate: "verify",
207
+ slug: TEST_SLUG,
208
+ issue: 9999,
209
+ projectRoot: "/tmp/test-project",
210
+ workDir: WORK_DIR,
211
+ dryRun: true,
212
+ });
213
+
214
+ expect(result.failures).toEqual(failures);
215
+ });
216
+
217
+ test("attempt count comes from gate state", async () => {
218
+ createState({
219
+ phase: "BUILD",
220
+ gates: {
221
+ verify: {
222
+ result: "FAIL",
223
+ attempt: 2,
224
+ failures: [{ check: "tests-pass", result: "FAIL", detail: "failing" }],
225
+ ts: new Date().toISOString(),
226
+ commitSha: "abc123",
227
+ },
228
+ },
229
+ });
230
+
231
+ const result = await runWithHeal({
232
+ gate: "verify",
233
+ slug: TEST_SLUG,
234
+ issue: 9999,
235
+ projectRoot: "/tmp/test-project",
236
+ workDir: WORK_DIR,
237
+ maxAttempts: 5,
238
+ dryRun: true,
239
+ });
240
+
241
+ expect(result.attempts).toBe(2);
242
+ expect(result.circuitBroken).toBeFalsy();
243
+ });
244
+ });
245
+
246
+ describe("snapshotProtectedFields", () => {
247
+ test("captures gates object", () => {
248
+ const state = minimalState({
249
+ gates: { scope: { result: "PASS", attempt: 1 } },
250
+ });
251
+ const snap = snapshotProtectedFields(state);
252
+ expect(snap.gates).toEqual({ scope: { result: "PASS", attempt: 1 } });
253
+ });
254
+
255
+ test("captures acs verdicts and evidence", () => {
256
+ const state = minimalState({
257
+ acs: [
258
+ { id: "AC-1", statement: "test stmt five words", verdict: "PASS", evidence: { type: "test-output", content: "ok" } },
259
+ { id: "AC-2", statement: "another stmt five words", verdict: "PENDING", evidence: null },
260
+ ],
261
+ });
262
+ const snap = snapshotProtectedFields(state);
263
+ expect(snap.acs).toHaveLength(2);
264
+ expect(snap.acs[0]).toEqual({ id: "AC-1", verdict: "PASS", evidence: { type: "test-output", content: "ok" } });
265
+ expect(snap.acs[1]).toEqual({ id: "AC-2", verdict: "PENDING", evidence: null });
266
+ });
267
+
268
+ test("deep-copies so mutations don't affect snapshot", () => {
269
+ const state = minimalState({
270
+ gates: { scope: { result: "PASS", attempt: 1 } },
271
+ acs: [{ id: "AC-1", statement: "test stmt five words", verdict: "PASS", evidence: { type: "test-output", content: "ok" } }],
272
+ });
273
+ const snap = snapshotProtectedFields(state);
274
+
275
+ state.gates.scope.result = "FAIL";
276
+ state.acs[0].verdict = "FAIL";
277
+ state.acs[0].evidence = { type: "manual-attestation", content: "changed" };
278
+
279
+ expect(snap.gates.scope.result).toBe("PASS");
280
+ expect(snap.acs[0].verdict).toBe("PASS");
281
+ expect(snap.acs[0].evidence.content).toBe("ok");
282
+ });
283
+
284
+ test("handles missing gates and empty acs", () => {
285
+ const state = minimalState({ gates: {}, acs: [] });
286
+ const snap = snapshotProtectedFields(state);
287
+ expect(snap.gates).toEqual({});
288
+ expect(snap.acs).toEqual([]);
289
+ });
290
+ });
291
+
292
+ describe("prove self-heal loop", () => {
293
+ test("prove self-heal: computeVerdict returns UNPROVEN when any criteria FAIL", () => {
294
+ // Simulates prove.js computeVerdict logic — validates circuit breaker precondition
295
+ const criteriaResults = [
296
+ { scId: "SC-1", verdict: "PASS", evidence: "tests pass" },
297
+ { scId: "SC-2", verdict: "FAIL", evidence: "regression found" },
298
+ ];
299
+ const fc = criteriaResults.filter(cr => cr.verdict === "FAIL").length;
300
+ const v = criteriaResults.length === 0 ? "INCONCLUSIVE" : fc > 0 ? "UNPROVEN" : "PROVEN";
301
+ expect(v).toBe("UNPROVEN");
302
+ expect(fc).toBe(1);
303
+ });
304
+
305
+ test("prove self-heal: circuit breaker triggers at maxAttempts=3", () => {
306
+ // Simulates the prove.js self-heal loop circuit breaker
307
+ const MAX_SELF_HEAL_ATTEMPTS = 3;
308
+ let iteration = 0;
309
+ let verdict = "UNPROVEN";
310
+
311
+ // Simulate 3 iterations that all remain UNPROVEN
312
+ while (verdict === "UNPROVEN" && iteration < MAX_SELF_HEAL_ATTEMPTS) {
313
+ iteration++;
314
+ // Each iteration fails to fix — verdict stays UNPROVEN
315
+ verdict = "UNPROVEN";
316
+ }
317
+
318
+ expect(iteration).toBe(3);
319
+ expect(iteration >= MAX_SELF_HEAL_ATTEMPTS).toBe(true);
320
+ expect(verdict).toBe("UNPROVEN");
321
+ });
322
+
323
+ test("prove self-heal: loop exits early on PROVEN", () => {
324
+ // Simulates the prove.js self-heal loop exiting when fix succeeds
325
+ const MAX_SELF_HEAL_ATTEMPTS = 3;
326
+ let iteration = 0;
327
+ let verdict = "UNPROVEN";
328
+
329
+ while (verdict === "UNPROVEN" && iteration < MAX_SELF_HEAL_ATTEMPTS) {
330
+ iteration++;
331
+ // Fix succeeds on iteration 2
332
+ if (iteration === 2) verdict = "PROVEN";
333
+ }
334
+
335
+ expect(iteration).toBe(2);
336
+ expect(verdict).toBe("PROVEN");
337
+ });
338
+
339
+ test("prove self-heal: iteration references tracked in return value", () => {
340
+ // Validates prove.js returns selfHealIterations + circuitBroken
341
+ const MAX_SELF_HEAL_ATTEMPTS = 3;
342
+ let selfHealIteration = 0;
343
+ let verdict = "UNPROVEN";
344
+
345
+ while (verdict === "UNPROVEN" && selfHealIteration < MAX_SELF_HEAL_ATTEMPTS) {
346
+ selfHealIteration++;
347
+ if (selfHealIteration === 2) verdict = "PROVEN";
348
+ }
349
+
350
+ const result = {
351
+ selfHealIterations: selfHealIteration,
352
+ circuitBroken: selfHealIteration >= MAX_SELF_HEAL_ATTEMPTS && verdict === "UNPROVEN",
353
+ verdict,
354
+ };
355
+
356
+ expect(result.selfHealIterations).toBe(2);
357
+ expect(result.circuitBroken).toBe(false);
358
+ expect(result.verdict).toBe("PROVEN");
359
+ });
360
+ });
361
+
362
+ describe("checkIntegrity", () => {
363
+ test("no violation when nothing changed", () => {
364
+ const state = minimalState({
365
+ gates: { scope: { result: "PASS", attempt: 1 } },
366
+ acs: [{ id: "AC-1", statement: "test stmt five words", verdict: "PASS", evidence: { type: "test-output", content: "ok" } }],
367
+ });
368
+ const before = snapshotProtectedFields(state);
369
+ const after = snapshotProtectedFields(state);
370
+ const result = checkIntegrity(before, after);
371
+ expect(result.violated).toBe(false);
372
+ expect(result.changes).toHaveLength(0);
373
+ });
374
+
375
+ test("detects gate result modification", () => {
376
+ const state = minimalState({
377
+ gates: { scope: { result: "PASS", attempt: 1 } },
378
+ });
379
+ const before = snapshotProtectedFields(state);
380
+ state.gates.scope.result = "FAIL";
381
+ const after = snapshotProtectedFields(state);
382
+ const result = checkIntegrity(before, after);
383
+ expect(result.violated).toBe(true);
384
+ expect(result.changes.some((c: string) => c.includes("gates"))).toBe(true);
385
+ });
386
+
387
+ test("detects verdict modification", () => {
388
+ const state = minimalState({
389
+ acs: [{ id: "AC-1", statement: "test stmt five words", verdict: "PASS", evidence: null }],
390
+ });
391
+ const before = snapshotProtectedFields(state);
392
+ state.acs[0].verdict = "FAIL";
393
+ const after = snapshotProtectedFields(state);
394
+ const result = checkIntegrity(before, after);
395
+ expect(result.violated).toBe(true);
396
+ expect(result.changes.some((c: string) => c.includes("AC-1") && c.includes("verdict"))).toBe(true);
397
+ });
398
+
399
+ test("detects evidence modification", () => {
400
+ const state = minimalState({
401
+ acs: [{ id: "AC-1", statement: "test stmt five words", verdict: "PASS", evidence: { type: "test-output", content: "ok" } }],
402
+ });
403
+ const before = snapshotProtectedFields(state);
404
+ state.acs[0].evidence = { type: "manual-attestation", content: "tampered" };
405
+ const after = snapshotProtectedFields(state);
406
+ const result = checkIntegrity(before, after);
407
+ expect(result.violated).toBe(true);
408
+ expect(result.changes.some((c: string) => c.includes("AC-1") && c.includes("evidence"))).toBe(true);
409
+ });
410
+
411
+ test("allows non-protected field changes", () => {
412
+ const state = minimalState({
413
+ gates: { scope: { result: "PASS", attempt: 1 } },
414
+ acs: [{ id: "AC-1", statement: "test stmt five words", verdict: "PASS", evidence: null }],
415
+ });
416
+ const before = snapshotProtectedFields(state);
417
+ // Only modify non-protected fields
418
+ state.phase = "VERIFY";
419
+ state.issueGoal = "Modified goal";
420
+ const after = snapshotProtectedFields(state);
421
+ const result = checkIntegrity(before, after);
422
+ expect(result.violated).toBe(false);
423
+ });
424
+ });
425
+ });
426
+
427
+ // ═══ WITNESS-VERDICT CROSS-VALIDATION (#521) ═══════════════════════════
428
+ describe("witness-verdict cross-validation", () => {
429
+ const WITNESS_TEST_BASE = `/tmp/witness-verdict-test-${process.pid}`;
430
+ const WITNESS_TEST_SLUG = `witness-test-${process.pid}`;
431
+ const WITNESS_WORK_DIR = join(WITNESS_TEST_BASE, WITNESS_TEST_SLUG);
432
+ const WITNESS_DIR = join(WITNESS_WORK_DIR, "witnesses");
433
+
434
+ beforeEach(() => {
435
+ mkdirSync(WITNESS_DIR, { recursive: true });
436
+ });
437
+
438
+ afterEach(() => {
439
+ try { rmSync(WITNESS_TEST_BASE, { recursive: true, force: true }); } catch {}
440
+ });
441
+
442
+ function writeWitnessFile(gate: string, result: "PASS" | "FAIL", hmac: string = "abc123def456"): void {
443
+ const ts = new Date().toISOString().replace(/\.\d{3}Z$/, "Z");
444
+ const record = { gate, result, slug: WITNESS_TEST_SLUG, issue: 9999, commitSha: "deadbeef", timestamp: ts, testOutputHash: "hash123", hmac };
445
+ const filename = `${gate}-${ts.replace(/[:.]/g, "-")}.json`;
446
+ writeFileSync(join(WITNESS_DIR, filename), JSON.stringify(record, null, 2));
447
+ }
448
+
449
+ test("witness-ac-verdict: PASS when verify+ship witnesses exist with HMAC", () => {
450
+ writeWitnessFile("scope", "PASS");
451
+ writeWitnessFile("verify", "PASS");
452
+ writeWitnessFile("ship", "PASS");
453
+
454
+ const witnessFiles = readdirSync(WITNESS_DIR).filter(f => f.endsWith(".json"));
455
+ const witnesses = witnessFiles.map(f => JSON.parse(readFileSync(join(WITNESS_DIR, f), "utf-8")));
456
+ const passedGatesWithHmac = new Set(
457
+ witnesses.filter((w: any) => w.result === "PASS" && w.hmac && w.hmac.length > 0).map((w: any) => w.gate),
458
+ );
459
+
460
+ const requiredGates = ["verify", "ship"];
461
+ const missingGates = requiredGates.filter(g => !passedGatesWithHmac.has(g));
462
+ expect(missingGates).toEqual([]);
463
+ });
464
+
465
+ test("witness-ac-verdict: FAIL when verify witness missing for PASS ACs", () => {
466
+ // Only scope witness, missing verify+ship — AC PASS should be rejected
467
+ writeWitnessFile("scope", "PASS");
468
+
469
+ const witnessFiles = readdirSync(WITNESS_DIR).filter(f => f.endsWith(".json"));
470
+ const witnesses = witnessFiles.map(f => JSON.parse(readFileSync(join(WITNESS_DIR, f), "utf-8")));
471
+ const passedGatesWithHmac = new Set(
472
+ witnesses.filter((w: any) => w.result === "PASS" && w.hmac && w.hmac.length > 0).map((w: any) => w.gate),
473
+ );
474
+
475
+ const requiredGates = ["verify", "ship"];
476
+ const missingGates = requiredGates.filter(g => !passedGatesWithHmac.has(g));
477
+ expect(missingGates.length).toBeGreaterThan(0);
478
+ expect(missingGates).toContain("verify");
479
+ expect(missingGates).toContain("ship");
480
+ });
481
+
482
+ test("witness-ac-verdict: FAIL when witness has empty HMAC (manual edit detected)", () => {
483
+ // Witness exists but HMAC is empty — indicates manual creation
484
+ const ts = new Date().toISOString().replace(/\.\d{3}Z$/, "Z");
485
+ const tampered = { gate: "verify", result: "PASS", slug: WITNESS_TEST_SLUG, issue: 9999, commitSha: "deadbeef", timestamp: ts, testOutputHash: "hash123", hmac: "" };
486
+ writeFileSync(join(WITNESS_DIR, `verify-${ts.replace(/[:.]/g, "-")}.json`), JSON.stringify(tampered, null, 2));
487
+ writeWitnessFile("ship", "PASS");
488
+
489
+ const witnessFiles = readdirSync(WITNESS_DIR).filter(f => f.endsWith(".json"));
490
+ const witnesses = witnessFiles.map(f => JSON.parse(readFileSync(join(WITNESS_DIR, f), "utf-8")));
491
+ const passedGatesWithHmac = new Set(
492
+ witnesses.filter((w: any) => w.result === "PASS" && w.hmac && w.hmac.length > 0).map((w: any) => w.gate),
493
+ );
494
+
495
+ // verify gate has empty HMAC — should not be in passedGatesWithHmac
496
+ expect(passedGatesWithHmac.has("verify")).toBe(false);
497
+ expect(passedGatesWithHmac.has("ship")).toBe(true);
498
+ });
499
+
500
+ test("witness-ac-verdict: no witness directory with PASS ACs = FAIL", () => {
501
+ // Remove the witness directory entirely
502
+ rmSync(WITNESS_DIR, { recursive: true, force: true });
503
+
504
+ const passACs = [{ id: "AC-1", verdict: "PASS" }];
505
+ const witnessExists = existsSync(WITNESS_DIR);
506
+
507
+ // Should fail: PASS ACs but no witness directory
508
+ expect(witnessExists).toBe(false);
509
+ expect(passACs.length).toBeGreaterThan(0);
510
+ });
511
+ });
@@ -0,0 +1,121 @@
1
+ import { readFileSync, existsSync } from "fs";
2
+ import { join } from "path";
3
+ import { execFileSync } from "child_process";
4
+
5
+ export interface HealResult {
6
+ result: "PASS" | "FAIL";
7
+ attempts: number;
8
+ failures?: Array<{ check: string; result: string; detail: string }>;
9
+ circuitBroken?: boolean;
10
+ }
11
+
12
+ const VALID_GATES = ["scope", "verify", "ship", "prove", "merge"] as const;
13
+ type GateName = (typeof VALID_GATES)[number];
14
+
15
+ export async function runWithHeal(opts: {
16
+ gate: string;
17
+ slug: string;
18
+ issue: number;
19
+ projectRoot: string;
20
+ workDir: string;
21
+ maxAttempts?: number;
22
+ dryRun?: boolean;
23
+ }): Promise<HealResult> {
24
+ const { gate, slug, issue, projectRoot, workDir, maxAttempts = 3, dryRun = false } = opts;
25
+
26
+ if (!VALID_GATES.includes(gate as GateName)) {
27
+ throw new Error(`Invalid gate: "${gate}". Must be one of: ${VALID_GATES.join(", ")}`);
28
+ }
29
+
30
+ const sf = join(workDir, "workflow-state.json");
31
+ if (!existsSync(sf)) {
32
+ throw new Error(`workflow-state.json not found at ${sf}`);
33
+ }
34
+
35
+ // Snapshot protected fields before gate execution for integrity check
36
+ const preState = JSON.parse(readFileSync(sf, "utf-8"));
37
+ const beforeSnapshot = snapshotProtectedFields(preState);
38
+
39
+ if (!dryRun) {
40
+ try {
41
+ execFileSync("bun", [
42
+ "run", join(projectRoot, "gates", "run-gate.ts"),
43
+ "--gate", gate,
44
+ "--slug", slug,
45
+ "--issue", String(issue),
46
+ ], { timeout: 120_000, cwd: projectRoot });
47
+ } catch {
48
+ // Gate process exits non-zero on FAIL — that's expected
49
+ }
50
+ }
51
+
52
+ const state = JSON.parse(readFileSync(sf, "utf-8"));
53
+
54
+ // Integrity check: verify protected fields weren't tampered with outside gate system
55
+ const afterSnapshot = snapshotProtectedFields(state);
56
+ const integrity = checkIntegrity(beforeSnapshot, afterSnapshot);
57
+ if (integrity.violated) {
58
+ console.error(`[self-heal] Integrity violation detected: ${integrity.changes.join("; ")}`);
59
+ }
60
+
61
+ const gateResult = state.gates?.[gate];
62
+
63
+ if (!gateResult) {
64
+ return { result: "FAIL", attempts: 1, failures: [{ check: "gate-execution", result: "FAIL", detail: `No gate result found for ${gate}` }] };
65
+ }
66
+
67
+ const attempt = gateResult.attempt ?? 1;
68
+
69
+ if (gateResult.result === "PASS") {
70
+ return { result: "PASS", attempts: attempt };
71
+ }
72
+
73
+ const failures = gateResult.failures ?? [];
74
+
75
+ if (attempt >= maxAttempts) {
76
+ return { result: "FAIL", attempts: attempt, failures, circuitBroken: true };
77
+ }
78
+
79
+ return { result: "FAIL", attempts: attempt, failures };
80
+ }
81
+
82
+ export function snapshotProtectedFields(state: Record<string, any>): Record<string, any> {
83
+ const gates = JSON.parse(JSON.stringify(state.gates ?? {}));
84
+ const acs = (state.acs ?? []).map((ac: any) => ({
85
+ id: ac.id,
86
+ verdict: ac.verdict,
87
+ evidence: JSON.parse(JSON.stringify(ac.evidence ?? null)),
88
+ }));
89
+ return { gates, acs };
90
+ }
91
+
92
+ export function checkIntegrity(
93
+ before: Record<string, any>,
94
+ after: Record<string, any>,
95
+ ): { violated: boolean; changes: string[] } {
96
+ const changes: string[] = [];
97
+
98
+ if (JSON.stringify(before.gates) !== JSON.stringify(after.gates)) {
99
+ changes.push("gates: protected field modified");
100
+ }
101
+
102
+ const beforeAcs: any[] = before.acs ?? [];
103
+ const afterAcs: any[] = after.acs ?? [];
104
+
105
+ for (let i = 0; i < beforeAcs.length; i++) {
106
+ const b = beforeAcs[i];
107
+ const a = afterAcs.find((x: any) => x.id === b.id);
108
+ if (!a) {
109
+ changes.push(`${b.id}: AC removed`);
110
+ continue;
111
+ }
112
+ if (b.verdict !== a.verdict) {
113
+ changes.push(`${b.id}: verdict changed (${b.verdict} -> ${a.verdict})`);
114
+ }
115
+ if (JSON.stringify(b.evidence) !== JSON.stringify(a.evidence)) {
116
+ changes.push(`${b.id}: evidence modified`);
117
+ }
118
+ }
119
+
120
+ return { violated: changes.length > 0, changes };
121
+ }