rungate 0.2.0 → 0.2.1

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 (164) hide show
  1. package/package.json +14 -3
  2. package/.claude/agents/aditi.md +0 -47
  3. package/.claude/agents/marcus.md +0 -50
  4. package/.claude/agents/quinn.md +0 -58
  5. package/.claude/agents/rook.md +0 -47
  6. package/.claude/agents/serena.md +0 -46
  7. package/.claude/rungate.json +0 -11
  8. package/.fallowrc.json +0 -28
  9. package/.github/copilot-instructions.md +0 -20
  10. package/.rungate/conformity-findings.json +0 -648
  11. package/AGENTS.md +0 -106
  12. package/CODE-MAP.md +0 -50
  13. package/HARNESS.md +0 -88
  14. package/bun.lock +0 -26
  15. package/bunfig.toml +0 -2
  16. package/config/brief-policies.json +0 -28
  17. package/config/ceremony-profiles.json +0 -212
  18. package/config/consumer-cascade-map.json +0 -24
  19. package/config/doc-cascade-map.json +0 -24
  20. package/config/eval.json +0 -84
  21. package/config/profiles/account-plan.yaml +0 -36
  22. package/config/profiles/campaign.yaml +0 -94
  23. package/config/prove-evidence.schema.json +0 -65
  24. package/config/spec-policies.json +0 -36
  25. package/config/workflow-schema.json +0 -281
  26. package/gates/SCHEMA-GUIDE.md +0 -121
  27. package/gates/ac-quality.test.ts +0 -106
  28. package/gates/adversarial.test.ts +0 -274
  29. package/gates/brief-assembler.test.ts +0 -429
  30. package/gates/brief-assembler.ts +0 -334
  31. package/gates/bunfig.toml +0 -2
  32. package/gates/chain.test.ts +0 -91
  33. package/gates/e2e-smoke.test.ts +0 -234
  34. package/gates/error-classifier.test.ts +0 -249
  35. package/gates/error-classifier.ts +0 -89
  36. package/gates/orchestrator.test.ts +0 -950
  37. package/gates/orchestrator.ts +0 -569
  38. package/gates/preload.ts +0 -77
  39. package/gates/prompts/ac-adversary.md +0 -20
  40. package/gates/prompts/evidence-validator.md +0 -22
  41. package/gates/prompts/prove-reproducer.md +0 -53
  42. package/gates/prove.test.ts +0 -164
  43. package/gates/run-gate.ts +0 -936
  44. package/gates/schema-parity.test.ts +0 -197
  45. package/gates/schema.ts +0 -362
  46. package/gates/self-heal.test.ts +0 -511
  47. package/gates/self-heal.ts +0 -121
  48. package/gates/ship-orchestrator.test.ts +0 -379
  49. package/gates/ship-orchestrator.ts +0 -363
  50. package/gates/test-fixtures/broken-no-environments.json +0 -60
  51. package/gates/test-fixtures/valid-standard.json +0 -54
  52. package/gates/test-utils.ts +0 -43
  53. package/gates/witness.ts +0 -220
  54. package/gates/workflow.test.ts +0 -1053
  55. package/hooks/AgentBriefGuard.hook.ts +0 -586
  56. package/hooks/AgentVerdictCapture.hook.ts +0 -134
  57. package/hooks/AutoVerifyGate.hook.ts +0 -76
  58. package/hooks/GateEnforcement.hook.ts +0 -273
  59. package/hooks/IssueCloseGuard.hook.ts +0 -116
  60. package/hooks/MergeGuard.hook.ts +0 -55
  61. package/hooks/StaleTTLCleanup.hook.ts +0 -133
  62. package/hooks/VerifyPhaseLock.hook.ts +0 -81
  63. package/hooks/WorkflowStateGuard.hook.ts +0 -37
  64. package/hooks/lib/agentDetection.ts +0 -70
  65. package/hooks/lib/findWorkflow.ts +0 -101
  66. package/hooks/lib/parseStdin.ts +0 -37
  67. package/hooks/lib/paths.ts +0 -111
  68. package/hooks/lib/utils.ts +0 -81
  69. package/reference/migration-manifest.json +0 -554
  70. package/reference/prompts/agent-aditi.md +0 -38
  71. package/reference/prompts/agent-marcus.md +0 -38
  72. package/reference/prompts/agent-quinn.md +0 -38
  73. package/reference/prompts/agent-rook.md +0 -37
  74. package/reference/prompts/agent-serena.md +0 -37
  75. package/reference/prompts/council-skill.md +0 -170
  76. package/reference/prompts/goal-skill.md +0 -235
  77. package/reference/prompts/prove-skill.md +0 -283
  78. package/reference/prompts/quinn-standard.md +0 -246
  79. package/reference/prompts/ship-convergence.md +0 -65
  80. package/reference/prompts/ship-escalation.md +0 -43
  81. package/reference/prompts/ship-iteration.md +0 -382
  82. package/reference/prompts/ship-pending-patches.md +0 -246
  83. package/reference/prompts/ship-reference.md +0 -250
  84. package/reference/prompts/ship-skill.md +0 -347
  85. package/reference/prompts/ship-templates.md +0 -215
  86. package/reference/scripts/batch-status.sh +0 -68
  87. package/reference/scripts/chain-compat-check.sh +0 -67
  88. package/reference/scripts/context-audit.sh +0 -314
  89. package/reference/scripts/dead-export-check.sh +0 -61
  90. package/reference/scripts/generate-qc-tables.sh +0 -92
  91. package/reference/scripts/harness-init.sh +0 -111
  92. package/reference/scripts/pai-054-baseline-check.sh +0 -40
  93. package/reference/scripts/prove-smoke-test.sh +0 -112
  94. package/reference/scripts/setup-keychain-salt.sh +0 -12
  95. package/reference/scripts/skill-optimizer-friday.sh +0 -88
  96. package/reference/scripts/skill-qc-test.sh +0 -351
  97. package/reference/scripts/skill-synthesize-tonight.sh +0 -66
  98. package/reference/specs/HARNESS-EXTRACTION-SPEC.md +0 -263
  99. package/reference/specs/harness-v3-bash-deletion-plan.md +0 -65
  100. package/reference/specs/harness-v3-migration-plan.md +0 -188
  101. package/scripts/decision-reconcile.sh +0 -74
  102. package/scripts/decision-reconcile.ts +0 -152
  103. package/scripts/git-hooks/pre-commit +0 -76
  104. package/scripts/git-hooks/pre-push +0 -271
  105. package/scripts/harness-skill-check.sh +0 -52
  106. package/scripts/lib/gap-emit.sh +0 -65
  107. package/scripts/lib/jq-update.sh +0 -30
  108. package/scripts/lib/resolve-slug.sh +0 -30
  109. package/scripts/lib/verify-spec-compliance.sh +0 -102
  110. package/scripts/prove-backfill.sh +0 -87
  111. package/scripts/scaffold-rungate-config.ts +0 -91
  112. package/scripts/self-containment-check.sh +0 -109
  113. package/scripts/skill-qc-validator.sh +0 -402
  114. package/scripts/slug-resolver.sh +0 -14
  115. package/specs/BOOTSTRAP-DATA-FLOW-SPEC.md +0 -2493
  116. package/specs/BOOTSTRAP-TEST-PLAN.md +0 -571
  117. package/specs/HARNESS-GATES.md +0 -256
  118. package/specs/HARNESS-SKILL-CHAIN.md +0 -286
  119. package/specs/HARNESS-SKILL-CONTRACT.md +0 -93
  120. package/specs/HARNESS-STANDARD.md +0 -488
  121. package/specs/SPEC-TEMPLATE.md +0 -53
  122. package/specs/bootstrap-data-flow.png +0 -0
  123. package/specs/harness-automation-matrix.md +0 -314
  124. package/specs/harness-automation-matrix.rules.json +0 -64
  125. package/test/anti.test.ts +0 -80
  126. package/test/contract-negative.test.ts +0 -86
  127. package/test/contract.test.ts +0 -158
  128. package/test/external-deps.test.ts +0 -48
  129. package/test/fixtures/golden-project/.env.example +0 -3
  130. package/test/fixtures/golden-project/Makefile +0 -7
  131. package/test/fixtures/golden-project/docs/adr/ADR-001-framework.md +0 -23
  132. package/test/fixtures/golden-project/docs/setup-guide.md +0 -10
  133. package/test/fixtures/golden-project/package.json +0 -10
  134. package/test/fixtures/golden-project/specs/api-spec.md +0 -20
  135. package/test/fixtures/golden-project/src/auth.ts +0 -8
  136. package/test/fixtures/golden-project/src/db.ts +0 -11
  137. package/test/fixtures/golden-project/src/index.ts +0 -19
  138. package/test/fixtures/golden-project/src/utils/format.ts +0 -3
  139. package/test/fixtures/golden-project/test/auth.test.ts +0 -15
  140. package/test/fixtures/golden-project/tsconfig.json +0 -12
  141. package/test/meta-sc-coverage.test.ts +0 -159
  142. package/test/phase-0.test.ts +0 -846
  143. package/test/phase-1.test.ts +0 -617
  144. package/test/phase-2.test.ts +0 -620
  145. package/test/phase-3.test.ts +0 -99
  146. package/test/scaffold-conformity.test.ts +0 -8
  147. package/test/schema-canary.test.ts +0 -51
  148. package/test/spec-compliance-auto.test.ts +0 -64
  149. package/test/spec-compliance.test.ts +0 -295
  150. package/test/spec-discovery.test.ts +0 -68
  151. package/test/spec-drift.test.ts +0 -5
  152. package/test/structure.test.ts +0 -165
  153. package/test/unit/agent-brief-guard-ship.test.ts +0 -293
  154. package/test/unit/extract-constraints.test.ts +0 -145
  155. package/test/unit/issue-close-guard.test.ts +0 -204
  156. package/test/unit/scaffold-project.test.ts +0 -417
  157. package/test/unit/skill-cooccurrence.test.ts +0 -177
  158. package/test/unit/skill-sequence-logger.test.ts +0 -244
  159. package/test/unit/workflow-state-guard.test.ts +0 -95
  160. package/workflows/batch-ship.js +0 -295
  161. package/workflows/council.js +0 -481
  162. package/workflows/prove.js +0 -560
  163. package/workflows/ship.js +0 -789
  164. package/workflows/verify.js +0 -236
@@ -1,511 +0,0 @@
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
- });
@@ -1,121 +0,0 @@
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
- }