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.
Files changed (73) hide show
  1. package/gates/.gate-salt +1 -0
  2. package/gates/SCHEMA-GUIDE.md +485 -0
  3. package/gates/ac-quality.test.ts +106 -0
  4. package/gates/adversarial.test.ts +283 -0
  5. package/gates/brief-assembler.test.ts +487 -0
  6. package/gates/brief-assembler.ts +448 -0
  7. package/gates/bunfig.toml +2 -0
  8. package/gates/chain.test.ts +91 -0
  9. package/gates/e2e-smoke.test.ts +234 -0
  10. package/gates/error-classifier.test.ts +250 -0
  11. package/gates/error-classifier.ts +99 -0
  12. package/gates/gate-executor.ts +3071 -0
  13. package/gates/orchestrator.test.ts +968 -0
  14. package/gates/orchestrator.ts +827 -0
  15. package/gates/preload.ts +89 -0
  16. package/gates/prompts/ac-adversary.md +20 -0
  17. package/gates/prompts/evidence-validator.md +22 -0
  18. package/gates/prompts/prove-reproducer.md +53 -0
  19. package/gates/prompts.sha256 +3 -0
  20. package/gates/prove.test.ts +164 -0
  21. package/gates/rook-state.test.ts +108 -0
  22. package/gates/run-attribution.ts +290 -0
  23. package/gates/run-gate.ts +163 -0
  24. package/gates/schema-parity.test.ts +378 -0
  25. package/gates/schema.ts +1209 -0
  26. package/gates/self-heal.test.ts +511 -0
  27. package/gates/self-heal.ts +125 -0
  28. package/gates/ship-orchestrator.test.ts +399 -0
  29. package/gates/ship-orchestrator.ts +384 -0
  30. package/gates/spec-compliance-probe.ts +159 -0
  31. package/gates/test-fixtures/broken-no-environments.json +110 -0
  32. package/gates/test-fixtures/consumer-directory-layout.json +90 -0
  33. package/gates/test-fixtures/valid-standard.json +91 -0
  34. package/gates/test-utils.ts +43 -0
  35. package/gates/typecheck-gate.ts +111 -0
  36. package/gates/witness.ts +349 -0
  37. package/gates/workflow.test.ts +2018 -0
  38. package/hooks/BashWriteGuard.hook.ts +19 -0
  39. package/hooks/GateResultProtection.hook.ts +27 -1
  40. package/hooks/VerifyPhaseLock.hook.ts +51 -11
  41. package/hooks/WorkflowStateGuard.hook.ts +24 -0
  42. package/lib/ai-review-rounds.ts +462 -0
  43. package/lib/bash-file-read.ts +17 -3
  44. package/lib/bash-write-guard.ts +1285 -50
  45. package/lib/distribution-standing.ts +75 -4
  46. package/lib/failure-intake.ts +12 -4
  47. package/lib/file-extensions.ts +81 -0
  48. package/lib/finding-arbiter.ts +1015 -0
  49. package/lib/finding-jury.ts +537 -0
  50. package/lib/gate-enforcement.ts +171 -43
  51. package/lib/gate-result-protection.ts +12 -2
  52. package/lib/generators/code-map.ts +55 -4
  53. package/lib/generators/types.ts +27 -1
  54. package/lib/github.ts +55 -0
  55. package/lib/interpreters.ts +64 -0
  56. package/lib/issue-create-override.ts +69 -8
  57. package/lib/issue-filing-gate.ts +605 -0
  58. package/lib/js-top-level-code.ts +20 -2
  59. package/lib/junit-checks.ts +239 -1
  60. package/lib/package-closure.ts +114 -0
  61. package/lib/publish-window.ts +287 -0
  62. package/lib/scaffold/ai-review-findings.ts +142 -3
  63. package/lib/scaffold/ai-review-step.ts +1012 -18
  64. package/lib/scaffold/managed-workflow.ts +163 -4
  65. package/lib/scaffold/steps.ts +498 -22
  66. package/lib/scanner.ts +13 -4
  67. package/lib/ticked-sc-symbols.ts +361 -0
  68. package/lib/transcript-checker.ts +6 -0
  69. package/lib/workflow-actions.ts +18 -0
  70. package/package.json +3 -1
  71. package/scripts/arbitrate-finding.ts +224 -0
  72. package/scripts/generate-code-map.ts +18 -10
  73. package/scripts/sync-sc-status.ts +82 -8
@@ -0,0 +1,399 @@
1
+ import { test, expect, describe, beforeEach, afterEach } from "bun:test";
2
+ import { mkdirSync, writeFileSync, readFileSync, rmSync, existsSync } from "fs";
3
+ import { join } from "path";
4
+
5
+ const TEST_BASE = `/tmp/ship-orch-test-${process.pid}`;
6
+ const TEST_SLUG = `orch-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: "GOAL",
19
+ issueGoal: "Test goal for ship orchestrator 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 {
37
+ advancePhase,
38
+ currentState,
39
+ runGate,
40
+ setGateExecutor,
41
+ getIterationCount,
42
+ isCircuitBroken,
43
+ validatePhaseTransition,
44
+ getRequiredGate,
45
+ MAX_ITERATIONS,
46
+ } from "./ship-orchestrator";
47
+
48
+ // Mock gate executor — writes FAIL result to workflow-state.json instead of spawning bun subprocess
49
+ function mockGateExecutor(gate: string, slug: string, _issue: number): void {
50
+ const sf = join(TEST_BASE, slug, "workflow-state.json");
51
+ const state = JSON.parse(readFileSync(sf, "utf-8"));
52
+ state.gates = state.gates || {};
53
+ state.gates[gate] = { result: "FAIL", attempt: 1, failures: [{ check: "mock-gate", detail: "mock executor" }] };
54
+ writeFileSync(sf, JSON.stringify(state, null, 2));
55
+ }
56
+
57
+ describe("ship-orchestrator", () => {
58
+ // Use mock executor to prevent subprocess spawning
59
+ beforeEach(() => setGateExecutor(mockGateExecutor));
60
+ afterEach(() => setGateExecutor(null));
61
+
62
+ /**
63
+ * `workDirBase()` in ./ship-orchestrator reads RUNGATE_WORK_DIR, so these
64
+ * tests have to point it at their own scratch base. It used to be set once at
65
+ * module scope, above the import, on the theory that the import had to see
66
+ * it — but `ship-orchestrator.ts:10` reads the variable INSIDE the function,
67
+ * so the assignment never needed to be there and the comment was wrong.
68
+ *
69
+ * What it cost: `bun test gates/` is one process, so that write landed in
70
+ * every other gate test file's environment and stayed there. Six checks in
71
+ * `workflow.test.ts` resolved this run's artifacts from it, and whether they
72
+ * saw `/tmp/ship-orch-test-<pid>` instead of the real directory came down to
73
+ * which file bun's walk over `gates/` reached first. On APFS this file runs
74
+ * last and nothing happens; on CI's ext4 it does not. That is the whole of
75
+ * #700 — six red CI runs, zero local.
76
+ *
77
+ * Scoped and restored, the way gates/adversarial.test.ts already did it.
78
+ */
79
+ let origWorkDir: string | undefined;
80
+ beforeEach(() => {
81
+ origWorkDir = process.env.RUNGATE_WORK_DIR;
82
+ process.env.RUNGATE_WORK_DIR = TEST_BASE;
83
+ mkdirSync(WORK_DIR, { recursive: true });
84
+ });
85
+
86
+ afterEach(() => {
87
+ if (origWorkDir === undefined) delete process.env.RUNGATE_WORK_DIR;
88
+ else process.env.RUNGATE_WORK_DIR = origWorkDir;
89
+ try { rmSync(TEST_BASE, { recursive: true, force: true }); } catch {}
90
+ });
91
+
92
+ // ── currentState ─────────────────────────────────────────────────
93
+
94
+ describe("currentState", () => {
95
+ test("returns workflow state for valid slug", async () => {
96
+ createState({ phase: "BUILD" });
97
+ const state = await currentState(TEST_SLUG);
98
+ expect(state.phase).toBe("BUILD");
99
+ expect(state.slug).toBe(TEST_SLUG);
100
+ expect(state.issue).toBe(9999);
101
+ });
102
+
103
+ test("throws for missing slug", async () => {
104
+ await expect(currentState("nonexistent-slug-xyz-12345")).rejects.toThrow(/not found/);
105
+ });
106
+ });
107
+
108
+ // ── getRequiredGate ──────────────────────────────────────────────
109
+
110
+ describe("getRequiredGate", () => {
111
+ test("returns scope for SCOPE phase", () => {
112
+ expect(getRequiredGate("SCOPE")).toBe("scope");
113
+ });
114
+
115
+ test("returns verify for BUILD phase", () => {
116
+ expect(getRequiredGate("BUILD")).toBe("verify");
117
+ });
118
+
119
+ test("returns ship for SHIP phase", () => {
120
+ expect(getRequiredGate("SHIP")).toBe("ship");
121
+ });
122
+
123
+ test("returns null for ungated phases", () => {
124
+ expect(getRequiredGate("GOAL")).toBeNull();
125
+ expect(getRequiredGate("DISCOVERY")).toBeNull();
126
+ expect(getRequiredGate("DONE")).toBeNull();
127
+ });
128
+ });
129
+
130
+ // ── validatePhaseTransition ──────────────────────────────────────
131
+
132
+ describe("validatePhaseTransition", () => {
133
+ test("GOAL → DISCOVERY is valid", () => {
134
+ expect(validatePhaseTransition("GOAL", "DISCOVERY")).toBe(true);
135
+ });
136
+
137
+ test("DISCOVERY → SCOPE is valid", () => {
138
+ expect(validatePhaseTransition("DISCOVERY", "SCOPE")).toBe(true);
139
+ });
140
+
141
+ test("SCOPE → BUILD is valid (via scope gate)", () => {
142
+ expect(validatePhaseTransition("SCOPE", "BUILD")).toBe(true);
143
+ });
144
+
145
+ test("BUILD → SHIP is valid (via verify gate)", () => {
146
+ expect(validatePhaseTransition("BUILD", "SHIP")).toBe(true);
147
+ });
148
+
149
+ test("SHIP → DONE is valid (via ship gate)", () => {
150
+ expect(validatePhaseTransition("SHIP", "DONE")).toBe(true);
151
+ });
152
+
153
+ test("cannot skip phases — GOAL → BUILD is invalid", () => {
154
+ expect(validatePhaseTransition("GOAL", "BUILD")).toBe(false);
155
+ });
156
+
157
+ test("cannot skip phases — GOAL → SHIP is invalid", () => {
158
+ expect(validatePhaseTransition("GOAL", "SHIP")).toBe(false);
159
+ });
160
+
161
+ test("cannot advance from DONE", () => {
162
+ expect(validatePhaseTransition("DONE", "GOAL")).toBe(false);
163
+ });
164
+
165
+ test("cannot advance from CIRCUIT_BREAK", () => {
166
+ expect(validatePhaseTransition("CIRCUIT_BREAK", "GOAL")).toBe(false);
167
+ });
168
+
169
+ test("VERIFY → SHIP is valid (direct)", () => {
170
+ expect(validatePhaseTransition("VERIFY", "SHIP")).toBe(true);
171
+ });
172
+ });
173
+
174
+ // ── advancePhase — ungated transitions ───────────────────────────
175
+
176
+ describe("advancePhase — ungated transitions", () => {
177
+ test("advances GOAL → DISCOVERY without gate", async () => {
178
+ createState({ phase: "GOAL" });
179
+ const result = await advancePhase(TEST_SLUG);
180
+ expect(result.phase).toBe("DISCOVERY");
181
+ expect(result.gateResult).toBe("SKIPPED");
182
+ const state = readState();
183
+ expect(state.phase).toBe("DISCOVERY");
184
+ });
185
+
186
+ test("advances DISCOVERY → SCOPE without gate", async () => {
187
+ createState({ phase: "DISCOVERY" });
188
+ const result = await advancePhase(TEST_SLUG);
189
+ expect(result.phase).toBe("SCOPE");
190
+ expect(result.gateResult).toBe("SKIPPED");
191
+ });
192
+
193
+ test("advances VERIFY → SHIP without gate", async () => {
194
+ createState({ phase: "VERIFY" });
195
+ const result = await advancePhase(TEST_SLUG);
196
+ expect(result.phase).toBe("SHIP");
197
+ expect(result.gateResult).toBe("SKIPPED");
198
+ });
199
+
200
+ test("blocks advancement from DONE", async () => {
201
+ createState({ phase: "DONE" });
202
+ const result = await advancePhase(TEST_SLUG);
203
+ expect(result.phase).toBe("DONE");
204
+ expect(result.gateResult).toBe("SKIPPED");
205
+ });
206
+
207
+ test("blocks advancement from CIRCUIT_BREAK", async () => {
208
+ createState({ phase: "CIRCUIT_BREAK" });
209
+ const result = await advancePhase(TEST_SLUG);
210
+ expect(result.phase).toBe("CIRCUIT_BREAK");
211
+ expect(result.gateResult).toBe("SKIPPED");
212
+ });
213
+
214
+ test("records phase advancement in changelog", async () => {
215
+ createState({ phase: "GOAL", changelog: [] });
216
+ await advancePhase(TEST_SLUG);
217
+ const state = readState();
218
+ const last = state.changelog[state.changelog.length - 1];
219
+ expect(last.event).toBe("phase-advance");
220
+ expect(last.detail).toContain("GOAL");
221
+ expect(last.detail).toContain("DISCOVERY");
222
+ expect(last.actor).toBe("gate-runner");
223
+ });
224
+
225
+ test("tracks phase timings on advancement", async () => {
226
+ createState({ phase: "GOAL", changelog: [] });
227
+ await advancePhase(TEST_SLUG);
228
+ const state = readState();
229
+ expect(state.phaseTimings).toBeDefined();
230
+ expect(state.phaseTimings.DISCOVERY).toBeDefined();
231
+ expect(state.phaseTimings.DISCOVERY.length).toBe(1);
232
+ expect(state.phaseTimings.DISCOVERY[0].enteredTs).toBeTruthy();
233
+ expect(state.phaseTimings.DISCOVERY[0].source).toBe("gate-runner");
234
+ });
235
+
236
+ test("sequential ungated advances work correctly", async () => {
237
+ createState({ phase: "GOAL" });
238
+ let result = await advancePhase(TEST_SLUG);
239
+ expect(result.phase).toBe("DISCOVERY");
240
+ result = await advancePhase(TEST_SLUG);
241
+ expect(result.phase).toBe("SCOPE");
242
+ });
243
+ });
244
+
245
+ // ── runGate — phase validation ───────────────────────────────────
246
+
247
+ describe("runGate — phase validation", () => {
248
+ test("rejects scope gate from GOAL phase", async () => {
249
+ createState({ phase: "GOAL" });
250
+ await expect(runGate(TEST_SLUG, "scope")).rejects.toThrow(/Cannot run scope gate from phase GOAL/);
251
+ });
252
+
253
+ test("rejects verify gate from SCOPE phase", async () => {
254
+ createState({ phase: "SCOPE" });
255
+ await expect(runGate(TEST_SLUG, "verify")).rejects.toThrow(/Cannot run verify gate from phase SCOPE/);
256
+ });
257
+
258
+ test("rejects ship gate from BUILD phase", async () => {
259
+ createState({ phase: "BUILD" });
260
+ await expect(runGate(TEST_SLUG, "ship")).rejects.toThrow(/Cannot run ship gate from phase BUILD/);
261
+ });
262
+
263
+ test("rejects scope gate from DONE phase", async () => {
264
+ createState({ phase: "DONE" });
265
+ await expect(runGate(TEST_SLUG, "scope")).rejects.toThrow(/Cannot run scope gate from phase DONE/);
266
+ });
267
+
268
+ test("scope gate allowed from SCOPE phase", async () => {
269
+ createState({ phase: "SCOPE" });
270
+ // This will try to call run-gate.ts and fail due to missing ACs etc,
271
+ // but it should NOT throw a phase validation error
272
+ const result = await runGate(TEST_SLUG, "scope");
273
+ // Gate will fail because no ACs, but that proves the phase validation passed
274
+ expect(result.gateResult).toBe("FAIL");
275
+ });
276
+ });
277
+
278
+ // ── Circuit breaker ─────────────────────────────────────────────
279
+
280
+ describe("circuit breaker", () => {
281
+ test("MAX_ITERATIONS is 3", () => {
282
+ expect(MAX_ITERATIONS).toBe(3);
283
+ });
284
+
285
+ test("getIterationCount returns 0 for fresh workflow", () => {
286
+ createState({ phase: "BUILD", changelog: [] });
287
+ expect(getIterationCount(TEST_SLUG)).toBe(0);
288
+ });
289
+
290
+ test("getIterationCount counts iteration-loopback events", () => {
291
+ createState({
292
+ phase: "BUILD",
293
+ changelog: [
294
+ { ts: "2026-01-01T00:00:00Z", event: "iteration-loopback", detail: "iteration 1", actor: "gate-runner" },
295
+ { ts: "2026-01-01T00:01:00Z", event: "iteration-loopback", detail: "iteration 2", actor: "gate-runner" },
296
+ ],
297
+ });
298
+ expect(getIterationCount(TEST_SLUG)).toBe(2);
299
+ });
300
+
301
+ test("isCircuitBroken returns false below threshold", () => {
302
+ createState({
303
+ phase: "BUILD",
304
+ changelog: [
305
+ { ts: "2026-01-01T00:00:00Z", event: "iteration-loopback", detail: "iteration 1", actor: "gate-runner" },
306
+ { ts: "2026-01-01T00:01:00Z", event: "iteration-loopback", detail: "iteration 2", actor: "gate-runner" },
307
+ ],
308
+ });
309
+ expect(isCircuitBroken(TEST_SLUG)).toBe(false);
310
+ });
311
+
312
+ test("isCircuitBroken returns true at threshold", () => {
313
+ createState({
314
+ phase: "BUILD",
315
+ changelog: [
316
+ { ts: "2026-01-01T00:00:00Z", event: "iteration-loopback", detail: "iteration 1", actor: "gate-runner" },
317
+ { ts: "2026-01-01T00:01:00Z", event: "iteration-loopback", detail: "iteration 2", actor: "gate-runner" },
318
+ { ts: "2026-01-01T00:02:00Z", event: "iteration-loopback", detail: "iteration 3", actor: "gate-runner" },
319
+ ],
320
+ });
321
+ expect(isCircuitBroken(TEST_SLUG)).toBe(true);
322
+ });
323
+
324
+ test("isCircuitBroken returns true when phase is CIRCUIT_BREAK", () => {
325
+ createState({ phase: "CIRCUIT_BREAK", changelog: [] });
326
+ expect(isCircuitBroken(TEST_SLUG)).toBe(true);
327
+ });
328
+
329
+ test("runGate returns circuit breaker result when broken", async () => {
330
+ createState({
331
+ phase: "BUILD",
332
+ changelog: [
333
+ { ts: "2026-01-01T00:00:00Z", event: "iteration-loopback", detail: "1", actor: "gate-runner" },
334
+ { ts: "2026-01-01T00:01:00Z", event: "iteration-loopback", detail: "2", actor: "gate-runner" },
335
+ { ts: "2026-01-01T00:02:00Z", event: "iteration-loopback", detail: "3", actor: "gate-runner" },
336
+ ],
337
+ });
338
+ const result = await runGate(TEST_SLUG, "verify");
339
+ expect(result.circuitBreaker).toBe(true);
340
+ expect(result.gateResult).toBe("FAIL");
341
+ });
342
+ });
343
+
344
+ // ── advancePhase — gated transitions ─────────────────────────────
345
+
346
+ describe("advancePhase — gated transitions", () => {
347
+ test("advancePhase from SCOPE triggers scope gate", async () => {
348
+ createState({ phase: "SCOPE" });
349
+ const result = await advancePhase(TEST_SLUG);
350
+ // Gate will fail because no ACs, but it was invoked (not SKIPPED)
351
+ expect(result.gateResult).not.toBe("SKIPPED");
352
+ });
353
+
354
+ test("advancePhase from BUILD triggers verify gate", async () => {
355
+ createState({ phase: "BUILD" });
356
+ const result = await advancePhase(TEST_SLUG);
357
+ expect(result.gateResult).not.toBe("SKIPPED");
358
+ });
359
+
360
+ test("advancePhase from SHIP triggers ship gate", async () => {
361
+ createState({ phase: "SHIP" });
362
+ const result = await advancePhase(TEST_SLUG);
363
+ expect(result.gateResult).not.toBe("SKIPPED");
364
+ });
365
+
366
+ test("gate failure blocks phase advancement", async () => {
367
+ createState({ phase: "SCOPE" });
368
+ const result = await advancePhase(TEST_SLUG);
369
+ expect(result.gateResult).toBe("FAIL");
370
+ // Phase should not have advanced (gate runner behavior)
371
+ const state = readState();
372
+ // State may still be SCOPE because the gate failed
373
+ expect(["SCOPE", "BUILD"]).toContain(state.phase);
374
+ });
375
+ });
376
+
377
+ // ── State consistency ────────────────────────────────────────────
378
+
379
+ describe("state consistency", () => {
380
+ test("updatedTs is set on phase advancement", async () => {
381
+ createState({ phase: "GOAL" });
382
+ const before = readState();
383
+ await advancePhase(TEST_SLUG);
384
+ const after = readState();
385
+ expect(after.updatedTs).toBeTruthy();
386
+ expect(after.updatedTs).not.toBe(before.updatedTs);
387
+ });
388
+
389
+ test("multiple advances maintain changelog order", async () => {
390
+ createState({ phase: "GOAL", changelog: [] });
391
+ await advancePhase(TEST_SLUG); // GOAL → DISCOVERY
392
+ await advancePhase(TEST_SLUG); // DISCOVERY → SCOPE
393
+ const state = readState();
394
+ expect(state.changelog.length).toBe(2);
395
+ expect(state.changelog[0].detail).toContain("GOAL");
396
+ expect(state.changelog[1].detail).toContain("DISCOVERY");
397
+ });
398
+ });
399
+ });