rungate 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (282) hide show
  1. package/bin/rungate.ts +70 -0
  2. package/lib/aes-calculator.ts +66 -0
  3. package/lib/agent-audit.ts +139 -0
  4. package/lib/agent-brief-validation.ts +359 -0
  5. package/lib/agents-md-refs.ts +90 -0
  6. package/lib/bash-file-read.ts +197 -0
  7. package/lib/behavioral-cache.ts +78 -0
  8. package/lib/branch-cleanup.ts +234 -0
  9. package/lib/brief-context-parser.ts +35 -0
  10. package/lib/canary.ts +117 -0
  11. package/lib/check-registry.ts +226 -0
  12. package/lib/ci-checkout.ts +400 -0
  13. package/lib/cli-commands.ts +137 -0
  14. package/lib/compliance-backfill.ts +220 -0
  15. package/lib/compliance-report.ts +504 -0
  16. package/lib/compliance.ts +361 -0
  17. package/lib/config-loader.ts +255 -0
  18. package/lib/conformity.ts +1818 -131
  19. package/lib/container-provenance.ts +230 -0
  20. package/lib/create-brief.ts +150 -0
  21. package/lib/create-sc.ts +272 -0
  22. package/lib/deep-merge.ts +69 -0
  23. package/lib/dev-liveness.ts +214 -0
  24. package/lib/directive-extractor.ts +210 -0
  25. package/lib/distribution-standing.ts +113 -0
  26. package/lib/doc-hygiene.ts +238 -0
  27. package/lib/eval-criteria.ts +6 -0
  28. package/lib/evidence-portability.ts +123 -0
  29. package/lib/evidence-prevalidator.ts +390 -0
  30. package/lib/evidence-required.ts +150 -0
  31. package/lib/evidence-satisfiability.ts +288 -0
  32. package/lib/frontmatter.ts +163 -0
  33. package/lib/gap-scanner.ts +348 -0
  34. package/lib/gate-enforcement.ts +366 -0
  35. package/lib/generators/agent-briefs.ts +136 -0
  36. package/lib/generators/agents-md.ts +506 -0
  37. package/lib/generators/code-map.ts +60 -0
  38. package/lib/generators/types.ts +184 -0
  39. package/lib/git-ref-normalizer.ts +107 -0
  40. package/lib/github.ts +539 -0
  41. package/lib/harness-identity.ts +63 -0
  42. package/lib/hill-climb.ts +164 -0
  43. package/lib/instruction-effectiveness.ts +131 -0
  44. package/lib/issue-closure-audit.ts +225 -0
  45. package/lib/issue-create-override.ts +343 -0
  46. package/lib/js-top-level-code.ts +250 -0
  47. package/lib/liveness-contradiction.ts +149 -0
  48. package/lib/organize.ts +505 -0
  49. package/lib/paths.ts +197 -6
  50. package/lib/post-fix-verify.ts +157 -0
  51. package/lib/prior-branch.ts +489 -0
  52. package/lib/project-state-shape.ts +166 -0
  53. package/lib/promote-outputs.ts +156 -0
  54. package/lib/prove-label-gate.ts +35 -0
  55. package/lib/reachability.ts +302 -0
  56. package/lib/release-preflight.ts +186 -0
  57. package/lib/rule-health-pipeline.ts +264 -0
  58. package/lib/rule-health-trend.ts +161 -0
  59. package/lib/rule-provenance.ts +149 -0
  60. package/lib/rule-registry.ts +128 -0
  61. package/lib/rungate-schema.ts +114 -4
  62. package/lib/sc-claim-forms.ts +179 -0
  63. package/lib/sc-cover.ts +150 -0
  64. package/lib/sc-guard.ts +157 -0
  65. package/lib/sc-ids.ts +344 -0
  66. package/lib/scaffold/defaults.ts +165 -0
  67. package/lib/scaffold/managed-workflow.ts +370 -0
  68. package/lib/scaffold/owned-files.ts +265 -0
  69. package/lib/scaffold/steps.ts +2680 -0
  70. package/lib/scanner.ts +701 -0
  71. package/lib/screenshot-upload.ts +73 -0
  72. package/lib/secret-patterns.ts +112 -0
  73. package/lib/security-verdict.ts +616 -0
  74. package/lib/signal-phrases.ts +1 -0
  75. package/lib/spec-change-conformity.ts +80 -0
  76. package/lib/spec-files.ts +62 -0
  77. package/lib/spec-registry.ts +133 -0
  78. package/lib/spec-updater.ts +96 -0
  79. package/lib/stale-cleanup.ts +224 -0
  80. package/lib/stale-issue-scanner.ts +36 -0
  81. package/lib/strip-js-comments.ts +269 -0
  82. package/lib/suite-measurement.ts +228 -0
  83. package/lib/task-completion-checks.ts +177 -0
  84. package/lib/test-suite-lock.ts +1136 -0
  85. package/lib/transcript-checker.ts +1140 -0
  86. package/lib/validators/spec-validators.ts +171 -0
  87. package/lib/verdict-capture.ts +73 -0
  88. package/lib/verified-push.ts +358 -0
  89. package/lib/workflow-security.ts +174 -0
  90. package/lib/worktree-cleanup.ts +250 -0
  91. package/lib/worktree-collect.ts +283 -0
  92. package/lib/worktree-isolation.ts +82 -0
  93. package/package.json +36 -3
  94. package/prompts/ac-adversary.md +14 -2
  95. package/prompts/aditi.md +5 -0
  96. package/prompts/blast-radius.md +5 -0
  97. package/prompts/container-verify.md +5 -0
  98. package/prompts/discovery.md +4 -0
  99. package/prompts/environment.md +5 -0
  100. package/prompts/evidence-hierarchy.md +5 -0
  101. package/prompts/evidence-validator.md +11 -0
  102. package/prompts/marcus.md +5 -0
  103. package/prompts/prevention.md +5 -0
  104. package/prompts/prove-reproducer.md +12 -0
  105. package/prompts/quinn-decision-tree.md +5 -0
  106. package/prompts/quinn-ui-brief.md +5 -0
  107. package/prompts/quinn.md +5 -0
  108. package/prompts/rca.md +5 -0
  109. package/prompts/read-before-write.md +4 -0
  110. package/prompts/regression.md +5 -0
  111. package/prompts/serena.md +5 -0
  112. package/scripts/audit-specs.ts +464 -0
  113. package/scripts/convert-spec.ts +293 -0
  114. package/scripts/create-sc.ts +92 -0
  115. package/scripts/create-spec.ts +166 -0
  116. package/scripts/extract-constraints.ts +4 -3
  117. package/scripts/generate-code-map.ts +1 -1
  118. package/scripts/scaffold-project.ts +138 -1426
  119. package/scripts/sync-spec-tests.ts +211 -126
  120. package/.claude/agents/aditi.md +0 -47
  121. package/.claude/agents/marcus.md +0 -50
  122. package/.claude/agents/quinn.md +0 -58
  123. package/.claude/agents/rook.md +0 -47
  124. package/.claude/agents/serena.md +0 -46
  125. package/.claude/rungate.json +0 -11
  126. package/.fallowrc.json +0 -28
  127. package/.github/copilot-instructions.md +0 -20
  128. package/.rungate/conformity-findings.json +0 -648
  129. package/AGENTS.md +0 -106
  130. package/CODE-MAP.md +0 -50
  131. package/HARNESS.md +0 -88
  132. package/bun.lock +0 -26
  133. package/bunfig.toml +0 -2
  134. package/config/brief-policies.json +0 -28
  135. package/config/ceremony-profiles.json +0 -212
  136. package/config/consumer-cascade-map.json +0 -24
  137. package/config/doc-cascade-map.json +0 -24
  138. package/config/eval.json +0 -84
  139. package/config/profiles/account-plan.yaml +0 -36
  140. package/config/profiles/campaign.yaml +0 -94
  141. package/config/prove-evidence.schema.json +0 -65
  142. package/config/spec-policies.json +0 -36
  143. package/config/workflow-schema.json +0 -281
  144. package/gates/SCHEMA-GUIDE.md +0 -121
  145. package/gates/ac-quality.test.ts +0 -106
  146. package/gates/adversarial.test.ts +0 -274
  147. package/gates/brief-assembler.test.ts +0 -429
  148. package/gates/brief-assembler.ts +0 -334
  149. package/gates/bunfig.toml +0 -2
  150. package/gates/chain.test.ts +0 -91
  151. package/gates/e2e-smoke.test.ts +0 -234
  152. package/gates/error-classifier.test.ts +0 -249
  153. package/gates/error-classifier.ts +0 -89
  154. package/gates/orchestrator.test.ts +0 -950
  155. package/gates/orchestrator.ts +0 -569
  156. package/gates/preload.ts +0 -77
  157. package/gates/prompts/ac-adversary.md +0 -20
  158. package/gates/prompts/evidence-validator.md +0 -22
  159. package/gates/prompts/prove-reproducer.md +0 -53
  160. package/gates/prove.test.ts +0 -164
  161. package/gates/run-gate.ts +0 -936
  162. package/gates/schema-parity.test.ts +0 -197
  163. package/gates/schema.ts +0 -362
  164. package/gates/self-heal.test.ts +0 -511
  165. package/gates/self-heal.ts +0 -121
  166. package/gates/ship-orchestrator.test.ts +0 -379
  167. package/gates/ship-orchestrator.ts +0 -363
  168. package/gates/test-fixtures/broken-no-environments.json +0 -60
  169. package/gates/test-fixtures/valid-standard.json +0 -54
  170. package/gates/test-utils.ts +0 -43
  171. package/gates/witness.ts +0 -220
  172. package/gates/workflow.test.ts +0 -1053
  173. package/hooks/AgentBriefGuard.hook.ts +0 -586
  174. package/hooks/AgentVerdictCapture.hook.ts +0 -134
  175. package/hooks/AutoVerifyGate.hook.ts +0 -76
  176. package/hooks/GateEnforcement.hook.ts +0 -273
  177. package/hooks/IssueCloseGuard.hook.ts +0 -116
  178. package/hooks/MergeGuard.hook.ts +0 -55
  179. package/hooks/StaleTTLCleanup.hook.ts +0 -133
  180. package/hooks/VerifyPhaseLock.hook.ts +0 -81
  181. package/hooks/WorkflowStateGuard.hook.ts +0 -37
  182. package/hooks/lib/agentDetection.ts +0 -70
  183. package/hooks/lib/findWorkflow.ts +0 -101
  184. package/hooks/lib/parseStdin.ts +0 -37
  185. package/hooks/lib/paths.ts +0 -111
  186. package/hooks/lib/utils.ts +0 -81
  187. package/reference/migration-manifest.json +0 -554
  188. package/reference/prompts/agent-aditi.md +0 -38
  189. package/reference/prompts/agent-marcus.md +0 -38
  190. package/reference/prompts/agent-quinn.md +0 -38
  191. package/reference/prompts/agent-rook.md +0 -37
  192. package/reference/prompts/agent-serena.md +0 -37
  193. package/reference/prompts/council-skill.md +0 -170
  194. package/reference/prompts/goal-skill.md +0 -235
  195. package/reference/prompts/prove-skill.md +0 -283
  196. package/reference/prompts/quinn-standard.md +0 -246
  197. package/reference/prompts/ship-convergence.md +0 -65
  198. package/reference/prompts/ship-escalation.md +0 -43
  199. package/reference/prompts/ship-iteration.md +0 -382
  200. package/reference/prompts/ship-pending-patches.md +0 -246
  201. package/reference/prompts/ship-reference.md +0 -250
  202. package/reference/prompts/ship-skill.md +0 -347
  203. package/reference/prompts/ship-templates.md +0 -215
  204. package/reference/scripts/batch-status.sh +0 -68
  205. package/reference/scripts/chain-compat-check.sh +0 -67
  206. package/reference/scripts/context-audit.sh +0 -314
  207. package/reference/scripts/dead-export-check.sh +0 -61
  208. package/reference/scripts/generate-qc-tables.sh +0 -92
  209. package/reference/scripts/harness-init.sh +0 -111
  210. package/reference/scripts/pai-054-baseline-check.sh +0 -40
  211. package/reference/scripts/prove-smoke-test.sh +0 -112
  212. package/reference/scripts/setup-keychain-salt.sh +0 -12
  213. package/reference/scripts/skill-optimizer-friday.sh +0 -88
  214. package/reference/scripts/skill-qc-test.sh +0 -351
  215. package/reference/scripts/skill-synthesize-tonight.sh +0 -66
  216. package/reference/specs/HARNESS-EXTRACTION-SPEC.md +0 -263
  217. package/reference/specs/harness-v3-bash-deletion-plan.md +0 -65
  218. package/reference/specs/harness-v3-migration-plan.md +0 -188
  219. package/scripts/decision-reconcile.sh +0 -74
  220. package/scripts/decision-reconcile.ts +0 -152
  221. package/scripts/git-hooks/pre-commit +0 -76
  222. package/scripts/git-hooks/pre-push +0 -271
  223. package/scripts/harness-skill-check.sh +0 -52
  224. package/scripts/lib/gap-emit.sh +0 -65
  225. package/scripts/lib/jq-update.sh +0 -30
  226. package/scripts/lib/resolve-slug.sh +0 -30
  227. package/scripts/lib/verify-spec-compliance.sh +0 -102
  228. package/scripts/prove-backfill.sh +0 -87
  229. package/scripts/scaffold-rungate-config.ts +0 -91
  230. package/scripts/self-containment-check.sh +0 -109
  231. package/scripts/skill-qc-validator.sh +0 -402
  232. package/scripts/slug-resolver.sh +0 -14
  233. package/specs/BOOTSTRAP-DATA-FLOW-SPEC.md +0 -2493
  234. package/specs/BOOTSTRAP-TEST-PLAN.md +0 -571
  235. package/specs/HARNESS-GATES.md +0 -256
  236. package/specs/HARNESS-SKILL-CHAIN.md +0 -286
  237. package/specs/HARNESS-SKILL-CONTRACT.md +0 -93
  238. package/specs/HARNESS-STANDARD.md +0 -488
  239. package/specs/SPEC-TEMPLATE.md +0 -53
  240. package/specs/bootstrap-data-flow.png +0 -0
  241. package/specs/harness-automation-matrix.md +0 -314
  242. package/specs/harness-automation-matrix.rules.json +0 -64
  243. package/test/anti.test.ts +0 -80
  244. package/test/contract-negative.test.ts +0 -86
  245. package/test/contract.test.ts +0 -158
  246. package/test/external-deps.test.ts +0 -48
  247. package/test/fixtures/golden-project/.env.example +0 -3
  248. package/test/fixtures/golden-project/Makefile +0 -7
  249. package/test/fixtures/golden-project/docs/adr/ADR-001-framework.md +0 -23
  250. package/test/fixtures/golden-project/docs/setup-guide.md +0 -10
  251. package/test/fixtures/golden-project/package.json +0 -10
  252. package/test/fixtures/golden-project/specs/api-spec.md +0 -20
  253. package/test/fixtures/golden-project/src/auth.ts +0 -8
  254. package/test/fixtures/golden-project/src/db.ts +0 -11
  255. package/test/fixtures/golden-project/src/index.ts +0 -19
  256. package/test/fixtures/golden-project/src/utils/format.ts +0 -3
  257. package/test/fixtures/golden-project/test/auth.test.ts +0 -15
  258. package/test/fixtures/golden-project/tsconfig.json +0 -12
  259. package/test/meta-sc-coverage.test.ts +0 -159
  260. package/test/phase-0.test.ts +0 -846
  261. package/test/phase-1.test.ts +0 -617
  262. package/test/phase-2.test.ts +0 -620
  263. package/test/phase-3.test.ts +0 -99
  264. package/test/scaffold-conformity.test.ts +0 -8
  265. package/test/schema-canary.test.ts +0 -51
  266. package/test/spec-compliance-auto.test.ts +0 -64
  267. package/test/spec-compliance.test.ts +0 -295
  268. package/test/spec-discovery.test.ts +0 -68
  269. package/test/spec-drift.test.ts +0 -5
  270. package/test/structure.test.ts +0 -165
  271. package/test/unit/agent-brief-guard-ship.test.ts +0 -293
  272. package/test/unit/extract-constraints.test.ts +0 -145
  273. package/test/unit/issue-close-guard.test.ts +0 -204
  274. package/test/unit/scaffold-project.test.ts +0 -417
  275. package/test/unit/skill-cooccurrence.test.ts +0 -177
  276. package/test/unit/skill-sequence-logger.test.ts +0 -244
  277. package/test/unit/workflow-state-guard.test.ts +0 -95
  278. package/workflows/batch-ship.js +0 -295
  279. package/workflows/council.js +0 -481
  280. package/workflows/prove.js +0 -560
  281. package/workflows/ship.js +0 -789
  282. 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
- }