rungate 0.8.0 → 0.8.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 (54) 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/GateResultProtection.hook.ts +27 -1
  39. package/hooks/WorkflowStateGuard.hook.ts +24 -0
  40. package/lib/bash-write-guard.ts +290 -36
  41. package/lib/gate-enforcement.ts +171 -43
  42. package/lib/gate-result-protection.ts +12 -2
  43. package/lib/generators/code-map.ts +55 -4
  44. package/lib/generators/types.ts +27 -1
  45. package/lib/js-top-level-code.ts +20 -2
  46. package/lib/junit-checks.ts +239 -1
  47. package/lib/package-closure.ts +114 -0
  48. package/lib/scaffold/ai-review-findings.ts +142 -3
  49. package/lib/scaffold/ai-review-step.ts +145 -16
  50. package/lib/scaffold/steps.ts +206 -10
  51. package/lib/scanner.ts +4 -2
  52. package/lib/transcript-checker.ts +6 -0
  53. package/package.json +2 -1
  54. package/scripts/generate-code-map.ts +13 -8
@@ -0,0 +1,283 @@
1
+ import { test, expect, describe, beforeEach, afterEach } from "bun:test";
2
+ import { readFileSync, writeFileSync, mkdirSync, rmSync, existsSync } from "fs";
3
+ import { join } from "path";
4
+ import { writeWitness, verifyWitness, verifyWitnessChain } from "./witness";
5
+ import { ensureGateSalt, gateSaltPath } from "../lib/paths";
6
+
7
+ // HOME_DIR is set so workDir resolves correctly via RUNGATE_WORK_DIR
8
+ const HOME_DIR = `/tmp/adversarial-home-${process.pid}`;
9
+ const SLUG = "adversarial-test";
10
+ const WORK_DIR = join(HOME_DIR, ".rungate", SLUG);
11
+ const WITNESS_DIR = join(WORK_DIR, "witnesses");
12
+
13
+ const origHome = process.env.HOME;
14
+ const origRungateWorkDir = process.env.RUNGATE_WORK_DIR;
15
+
16
+ beforeEach(() => {
17
+ mkdirSync(WITNESS_DIR, { recursive: true });
18
+ // Copy gate salt into test HOME so HMAC functions work
19
+ const saltSrc = join(origHome!, ".claude", "hooks", "lib", ".gate-salt");
20
+ const saltDst = join(HOME_DIR, ".claude", "hooks", "lib", ".gate-salt");
21
+ mkdirSync(join(HOME_DIR, ".claude", "hooks", "lib"), { recursive: true });
22
+ if (existsSync(saltSrc)) {
23
+ writeFileSync(saltDst, readFileSync(saltSrc));
24
+ }
25
+ process.env.HOME = HOME_DIR;
26
+ delete process.env.RUNGATE_WORK_DIR;
27
+ });
28
+
29
+ afterEach(() => {
30
+ process.env.HOME = origHome;
31
+ if (origRungateWorkDir !== undefined) process.env.RUNGATE_WORK_DIR = origRungateWorkDir;
32
+ else delete process.env.RUNGATE_WORK_DIR;
33
+ try { rmSync(HOME_DIR, { recursive: true, force: true }); } catch {}
34
+ });
35
+
36
+ // ── Helper: create a properly-signed witness file ───────────────────
37
+
38
+ function createSignedWitness(
39
+ gate: string,
40
+ result: "PASS" | "FAIL",
41
+ commitSha: string,
42
+ overrides: Partial<Record<string, unknown>> = {},
43
+ ): string {
44
+ // ensureGateSalt rather than a bare read of gateSaltPath(): the salt is
45
+ // gitignored, so on a fresh checkout it exists only if gates/orchestrator.test.ts
46
+ // already ran generateHmac. These eight tests were passing on file ordering.
47
+ const salt = readFileSync(ensureGateSalt(), "utf-8").trim();
48
+ const { createHmac } = require("crypto");
49
+
50
+ const ts = new Date().toISOString().replace(/\.\d{3}Z$/, "Z");
51
+ const testOutputHash = createHmac("sha256", "test-output")
52
+ .update("test output")
53
+ .digest("hex")
54
+ .slice(0, 16);
55
+
56
+ const record: Record<string, unknown> = {
57
+ gate,
58
+ result,
59
+ slug: SLUG,
60
+ issue: 471,
61
+ commitSha,
62
+ timestamp: ts,
63
+ testOutputHash,
64
+ ...overrides,
65
+ };
66
+
67
+ const input = [
68
+ record.gate,
69
+ record.result,
70
+ record.slug,
71
+ record.issue,
72
+ record.commitSha,
73
+ record.timestamp,
74
+ record.testOutputHash,
75
+ ].join(":");
76
+ const hmac = createHmac("sha256", salt).update(input).digest("hex").slice(0, 16);
77
+ record.hmac = hmac;
78
+
79
+ const filename = `${gate}-${ts.replace(/[:.]/g, "-")}.json`;
80
+ const witnessPath = join(WITNESS_DIR, filename);
81
+ writeFileSync(witnessPath, JSON.stringify(record, null, 2) + "\n");
82
+ return witnessPath;
83
+ }
84
+
85
+ function createForgedWitness(
86
+ gate: string,
87
+ result: "PASS" | "FAIL",
88
+ commitSha: string,
89
+ ): string {
90
+ // The forgery path never reads the salt, but verifyWitness does — and on a
91
+ // fresh checkout the salt does not exist, so this test failed with ENOENT
92
+ // instead of the HMAC mismatch it asserts. "Rejected because the salt was
93
+ // missing" and "rejected because the signature is wrong" are different
94
+ // outcomes, and only one of them proves the check works.
95
+ ensureGateSalt();
96
+
97
+ const ts = new Date().toISOString().replace(/\.\d{3}Z$/, "Z");
98
+ const record = {
99
+ gate,
100
+ result,
101
+ slug: SLUG,
102
+ issue: 471,
103
+ commitSha,
104
+ timestamp: ts,
105
+ testOutputHash: "forged-hash-value",
106
+ hmac: "aaaa1111bbbb2222", // forged HMAC
107
+ };
108
+
109
+ const filename = `${gate}-${ts.replace(/[:.]/g, "-")}.json`;
110
+ const witnessPath = join(WITNESS_DIR, filename);
111
+ writeFileSync(witnessPath, JSON.stringify(record, null, 2) + "\n");
112
+ return witnessPath;
113
+ }
114
+
115
+ // ── Test 1: Write protection — blocks direct gate result writes ─────
116
+
117
+ describe("adversarial-write-protection", () => {
118
+ test("detects gate result fields in workflow-state.json content", () => {
119
+ // Simulate what GateResultProtection.hook.ts checks:
120
+ // Parse JSON content for gate result fields
121
+ const blockedContent = JSON.stringify({
122
+ phase: "BUILD",
123
+ gates: {
124
+ scope: { result: "PASS", attempt: 1 },
125
+ },
126
+ });
127
+
128
+ // Check: does content contain gate result fields?
129
+ const parsed = JSON.parse(blockedContent);
130
+ const hasGateResult =
131
+ parsed.gates?.scope?.result ||
132
+ parsed.gates?.verify?.result ||
133
+ parsed.gates?.ship?.result;
134
+ const hasDonePhase = parsed.phase === "DONE";
135
+
136
+ expect(hasGateResult).toBeTruthy();
137
+ expect(hasDonePhase).toBe(false);
138
+ });
139
+
140
+ test("allows non-gate-field writes to workflow-state.json", () => {
141
+ const allowedContent = JSON.stringify({
142
+ phase: "BUILD",
143
+ acs: [{ id: "SC-1", statement: "test" }],
144
+ changelog: [{ ts: "2026-09-10", event: "ac-added" }],
145
+ });
146
+
147
+ const parsed = JSON.parse(allowedContent);
148
+ const hasGateResult =
149
+ parsed.gates?.scope?.result ||
150
+ parsed.gates?.verify?.result ||
151
+ parsed.gates?.ship?.result;
152
+ const hasDonePhase = parsed.phase === "DONE";
153
+
154
+ expect(hasGateResult).toBeFalsy();
155
+ expect(hasDonePhase).toBe(false);
156
+ });
157
+
158
+ test("detects DONE phase as blocked", () => {
159
+ const blockedContent = JSON.stringify({ phase: "DONE" });
160
+ const parsed = JSON.parse(blockedContent);
161
+ expect(parsed.phase === "DONE").toBe(true);
162
+ });
163
+
164
+ test("detects gate result in Edit new_string", () => {
165
+ // Simulates Edit tool — check if new_string contains gate result patterns
166
+ const editNewString = '"result": "PASS"';
167
+ const blocked =
168
+ editNewString.includes('"result": "PASS"') ||
169
+ editNewString.includes('"result": "FAIL"') ||
170
+ editNewString.includes('"phase": "DONE"');
171
+
172
+ expect(blocked).toBe(true);
173
+ });
174
+ });
175
+
176
+ // ── Test 2: HMAC forgery — rejects witness without valid HMAC ───────
177
+
178
+ describe("adversarial-hmac-forgery", () => {
179
+ test("rejects witness with forged HMAC", () => {
180
+ const forgedPath = createForgedWitness("scope", "PASS", "abc123");
181
+ const result = verifyWitness(forgedPath);
182
+
183
+ expect(result.valid).toBe(false);
184
+ expect(result.error).toContain("HMAC mismatch");
185
+ });
186
+
187
+ test("accepts witness with valid HMAC", () => {
188
+ const validPath = createSignedWitness("scope", "PASS", "abc123");
189
+ const result = verifyWitness(validPath);
190
+
191
+ expect(result.valid).toBe(true);
192
+ expect(result.error).toBeUndefined();
193
+ });
194
+
195
+ test("rejects witness with missing fields", () => {
196
+ const ts = new Date().toISOString().replace(/\.\d{3}Z$/, "Z");
197
+ const incomplete = { gate: "scope", result: "PASS" }; // missing most fields
198
+ const witnessPath = join(WITNESS_DIR, "incomplete.json");
199
+ writeFileSync(witnessPath, JSON.stringify(incomplete));
200
+
201
+ const result = verifyWitness(witnessPath);
202
+ expect(result.valid).toBe(false);
203
+ expect(result.error).toContain("Missing field");
204
+ });
205
+
206
+ test("rejects witness with tampered result field", () => {
207
+ // Create valid witness, then tamper with result
208
+ const validPath = createSignedWitness("scope", "FAIL", "abc123");
209
+ const data = JSON.parse(readFileSync(validPath, "utf-8"));
210
+ data.result = "PASS"; // tamper: FAIL -> PASS
211
+ writeFileSync(validPath, JSON.stringify(data));
212
+
213
+ const result = verifyWitness(validPath);
214
+ expect(result.valid).toBe(false);
215
+ expect(result.error).toContain("HMAC mismatch");
216
+ });
217
+ });
218
+
219
+ // ── Test 3: Ground-truth — detects stale commit SHA ─────────────────
220
+
221
+ describe("adversarial-ground-truth", () => {
222
+ test("detects stale commit SHA in witness chain", () => {
223
+ const oldSha = "aaa111bbb222ccc333";
224
+ const currentSha = "ddd444eee555fff666";
225
+
226
+ createSignedWitness("scope", "PASS", oldSha);
227
+ createSignedWitness("ship", "PASS", oldSha);
228
+
229
+ // Verify chain with different HEAD
230
+ const result = verifyWitnessChain(SLUG, currentSha);
231
+ expect(result.valid).toBe(false);
232
+ expect(result.errors.some(e => e.includes("Stale witness"))).toBe(true);
233
+ });
234
+
235
+ test("accepts matching commit SHA", () => {
236
+ const sha = "abc123def456789";
237
+
238
+ createSignedWitness("scope", "PASS", sha);
239
+ createSignedWitness("ship", "PASS", sha);
240
+
241
+ const result = verifyWitnessChain(SLUG, sha);
242
+ expect(result.valid).toBe(true);
243
+ expect(result.errors).toHaveLength(0);
244
+ });
245
+ });
246
+
247
+ // ── Test 4: Chain incomplete — blocks push with missing gates ───────
248
+
249
+ describe("adversarial-chain-incomplete", () => {
250
+ test("rejects chain with only scope (no ship)", () => {
251
+ createSignedWitness("scope", "PASS", "abc123");
252
+ // No ship witness
253
+
254
+ const result = verifyWitnessChain(SLUG, "abc123");
255
+ expect(result.valid).toBe(false);
256
+ expect(result.errors.some(e => e.includes("Missing PASS witness for required gate: ship"))).toBe(true);
257
+ });
258
+
259
+ test("rejects chain with only ship (no scope)", () => {
260
+ createSignedWitness("ship", "PASS", "abc123");
261
+ // No scope witness
262
+
263
+ const result = verifyWitnessChain(SLUG, "abc123");
264
+ expect(result.valid).toBe(false);
265
+ expect(result.errors.some(e => e.includes("Missing PASS witness for required gate: scope"))).toBe(true);
266
+ });
267
+
268
+ test("rejects empty witness directory", () => {
269
+ // WITNESS_DIR exists but is empty
270
+ const result = verifyWitnessChain(SLUG, "abc123");
271
+ expect(result.valid).toBe(false);
272
+ expect(result.errors.some(e => e.includes("No witness files found"))).toBe(true);
273
+ });
274
+
275
+ test("rejects when required gate has only FAIL witness", () => {
276
+ createSignedWitness("scope", "PASS", "abc123");
277
+ createSignedWitness("ship", "FAIL", "abc123"); // FAIL, not PASS
278
+
279
+ const result = verifyWitnessChain(SLUG, "abc123");
280
+ expect(result.valid).toBe(false);
281
+ expect(result.errors.some(e => e.includes("Missing PASS witness for required gate: ship"))).toBe(true);
282
+ });
283
+ });