rungate 0.8.0 → 0.8.2
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.
- package/gates/.gate-salt +1 -0
- package/gates/SCHEMA-GUIDE.md +485 -0
- package/gates/ac-quality.test.ts +106 -0
- package/gates/adversarial.test.ts +283 -0
- package/gates/brief-assembler.test.ts +487 -0
- package/gates/brief-assembler.ts +448 -0
- package/gates/bunfig.toml +2 -0
- package/gates/chain.test.ts +91 -0
- package/gates/e2e-smoke.test.ts +234 -0
- package/gates/error-classifier.test.ts +250 -0
- package/gates/error-classifier.ts +99 -0
- package/gates/gate-executor.ts +3071 -0
- package/gates/orchestrator.test.ts +968 -0
- package/gates/orchestrator.ts +827 -0
- package/gates/preload.ts +89 -0
- package/gates/prompts/ac-adversary.md +20 -0
- package/gates/prompts/evidence-validator.md +22 -0
- package/gates/prompts/prove-reproducer.md +53 -0
- package/gates/prompts.sha256 +3 -0
- package/gates/prove.test.ts +164 -0
- package/gates/rook-state.test.ts +108 -0
- package/gates/run-attribution.ts +290 -0
- package/gates/run-gate.ts +163 -0
- package/gates/schema-parity.test.ts +378 -0
- package/gates/schema.ts +1209 -0
- package/gates/self-heal.test.ts +511 -0
- package/gates/self-heal.ts +125 -0
- package/gates/ship-orchestrator.test.ts +399 -0
- package/gates/ship-orchestrator.ts +384 -0
- package/gates/spec-compliance-probe.ts +159 -0
- package/gates/test-fixtures/broken-no-environments.json +110 -0
- package/gates/test-fixtures/consumer-directory-layout.json +90 -0
- package/gates/test-fixtures/valid-standard.json +91 -0
- package/gates/test-utils.ts +43 -0
- package/gates/typecheck-gate.ts +111 -0
- package/gates/witness.ts +349 -0
- package/gates/workflow.test.ts +2018 -0
- package/hooks/BashWriteGuard.hook.ts +19 -0
- package/hooks/GateResultProtection.hook.ts +27 -1
- package/hooks/VerifyPhaseLock.hook.ts +51 -11
- package/hooks/WorkflowStateGuard.hook.ts +24 -0
- package/lib/ai-review-rounds.ts +462 -0
- package/lib/bash-file-read.ts +17 -3
- package/lib/bash-write-guard.ts +1285 -50
- package/lib/distribution-standing.ts +75 -4
- package/lib/failure-intake.ts +12 -4
- package/lib/file-extensions.ts +81 -0
- package/lib/finding-arbiter.ts +1015 -0
- package/lib/finding-jury.ts +537 -0
- package/lib/gate-enforcement.ts +171 -43
- package/lib/gate-result-protection.ts +12 -2
- package/lib/generators/code-map.ts +55 -4
- package/lib/generators/types.ts +27 -1
- package/lib/github.ts +55 -0
- package/lib/interpreters.ts +64 -0
- package/lib/issue-create-override.ts +69 -8
- package/lib/issue-filing-gate.ts +605 -0
- package/lib/js-top-level-code.ts +20 -2
- package/lib/junit-checks.ts +239 -1
- package/lib/package-closure.ts +114 -0
- package/lib/publish-window.ts +287 -0
- package/lib/scaffold/ai-review-findings.ts +142 -3
- package/lib/scaffold/ai-review-step.ts +1012 -18
- package/lib/scaffold/managed-workflow.ts +163 -4
- package/lib/scaffold/steps.ts +498 -22
- package/lib/scanner.ts +13 -4
- package/lib/ticked-sc-symbols.ts +361 -0
- package/lib/transcript-checker.ts +6 -0
- package/lib/workflow-actions.ts +18 -0
- package/package.json +3 -1
- package/scripts/arbitrate-finding.ts +224 -0
- package/scripts/generate-code-map.ts +18 -10
- package/scripts/sync-sc-status.ts +82 -8
|
@@ -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,125 @@
|
|
|
1
|
+
import { readFileSync, existsSync } from "fs";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
import { execFileSync } from "child_process";
|
|
4
|
+
|
|
5
|
+
// ── Gate contract interfaces (SC-373) ─────────────────────────────────────
|
|
6
|
+
|
|
7
|
+
export interface HealInput {
|
|
8
|
+
gate: string;
|
|
9
|
+
slug: string;
|
|
10
|
+
issue: number;
|
|
11
|
+
projectRoot: string;
|
|
12
|
+
workDir: string;
|
|
13
|
+
maxAttempts?: number;
|
|
14
|
+
dryRun?: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface HealResult {
|
|
18
|
+
result: "PASS" | "FAIL";
|
|
19
|
+
attempts: number;
|
|
20
|
+
failures?: Array<{ check: string; result: string; detail: string }>;
|
|
21
|
+
circuitBroken?: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const VALID_GATES = ["scope", "verify", "ship", "prove", "merge"] as const;
|
|
25
|
+
type GateName = (typeof VALID_GATES)[number];
|
|
26
|
+
|
|
27
|
+
export async function runWithHeal(opts: HealInput): Promise<HealResult> {
|
|
28
|
+
const { gate, slug, issue, projectRoot, workDir, maxAttempts = 3, dryRun = false } = opts;
|
|
29
|
+
|
|
30
|
+
if (!VALID_GATES.includes(gate as GateName)) {
|
|
31
|
+
throw new Error(`Invalid gate: "${gate}". Must be one of: ${VALID_GATES.join(", ")}`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const sf = join(workDir, "workflow-state.json");
|
|
35
|
+
if (!existsSync(sf)) {
|
|
36
|
+
throw new Error(`workflow-state.json not found at ${sf}`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Snapshot protected fields before gate execution for integrity check
|
|
40
|
+
const preState = JSON.parse(readFileSync(sf, "utf-8"));
|
|
41
|
+
const beforeSnapshot = snapshotProtectedFields(preState);
|
|
42
|
+
|
|
43
|
+
if (!dryRun) {
|
|
44
|
+
try {
|
|
45
|
+
execFileSync("bun", [
|
|
46
|
+
"run", join(projectRoot, "gates", "run-gate.ts"),
|
|
47
|
+
"--gate", gate,
|
|
48
|
+
"--slug", slug,
|
|
49
|
+
"--issue", String(issue),
|
|
50
|
+
], { timeout: 120_000, cwd: projectRoot });
|
|
51
|
+
} catch {
|
|
52
|
+
// Gate process exits non-zero on FAIL — that's expected
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const state = JSON.parse(readFileSync(sf, "utf-8"));
|
|
57
|
+
|
|
58
|
+
// Integrity check: verify protected fields weren't tampered with outside gate system
|
|
59
|
+
const afterSnapshot = snapshotProtectedFields(state);
|
|
60
|
+
const integrity = checkIntegrity(beforeSnapshot, afterSnapshot);
|
|
61
|
+
if (integrity.violated) {
|
|
62
|
+
console.error(`[self-heal] Integrity violation detected: ${integrity.changes.join("; ")}`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const gateResult = state.gates?.[gate];
|
|
66
|
+
|
|
67
|
+
if (!gateResult) {
|
|
68
|
+
return { result: "FAIL", attempts: 1, failures: [{ check: "gate-execution", result: "FAIL", detail: `No gate result found for ${gate}` }] };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const attempt = gateResult.attempt ?? 1;
|
|
72
|
+
|
|
73
|
+
if (gateResult.result === "PASS") {
|
|
74
|
+
return { result: "PASS", attempts: attempt };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const failures = gateResult.failures ?? [];
|
|
78
|
+
|
|
79
|
+
if (attempt >= maxAttempts) {
|
|
80
|
+
return { result: "FAIL", attempts: attempt, failures, circuitBroken: true };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return { result: "FAIL", attempts: attempt, failures };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function snapshotProtectedFields(state: Record<string, any>): Record<string, any> {
|
|
87
|
+
const gates = JSON.parse(JSON.stringify(state.gates ?? {}));
|
|
88
|
+
const acs = (state.acs ?? []).map((ac: any) => ({
|
|
89
|
+
id: ac.id,
|
|
90
|
+
verdict: ac.verdict,
|
|
91
|
+
evidence: JSON.parse(JSON.stringify(ac.evidence ?? null)),
|
|
92
|
+
}));
|
|
93
|
+
return { gates, acs };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function checkIntegrity(
|
|
97
|
+
before: Record<string, any>,
|
|
98
|
+
after: Record<string, any>,
|
|
99
|
+
): { violated: boolean; changes: string[] } {
|
|
100
|
+
const changes: string[] = [];
|
|
101
|
+
|
|
102
|
+
if (JSON.stringify(before.gates) !== JSON.stringify(after.gates)) {
|
|
103
|
+
changes.push("gates: protected field modified");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const beforeAcs: any[] = before.acs ?? [];
|
|
107
|
+
const afterAcs: any[] = after.acs ?? [];
|
|
108
|
+
|
|
109
|
+
for (let i = 0; i < beforeAcs.length; i++) {
|
|
110
|
+
const b = beforeAcs[i];
|
|
111
|
+
const a = afterAcs.find((x: any) => x.id === b.id);
|
|
112
|
+
if (!a) {
|
|
113
|
+
changes.push(`${b.id}: AC removed`);
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (b.verdict !== a.verdict) {
|
|
117
|
+
changes.push(`${b.id}: verdict changed (${b.verdict} -> ${a.verdict})`);
|
|
118
|
+
}
|
|
119
|
+
if (JSON.stringify(b.evidence) !== JSON.stringify(a.evidence)) {
|
|
120
|
+
changes.push(`${b.id}: evidence modified`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return { violated: changes.length > 0, changes };
|
|
125
|
+
}
|