rungate 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. package/package.json +14 -3
  2. package/.claude/agents/aditi.md +0 -47
  3. package/.claude/agents/marcus.md +0 -50
  4. package/.claude/agents/quinn.md +0 -58
  5. package/.claude/agents/rook.md +0 -47
  6. package/.claude/agents/serena.md +0 -46
  7. package/.claude/rungate.json +0 -11
  8. package/.fallowrc.json +0 -28
  9. package/.github/copilot-instructions.md +0 -20
  10. package/.rungate/conformity-findings.json +0 -648
  11. package/AGENTS.md +0 -106
  12. package/CODE-MAP.md +0 -50
  13. package/HARNESS.md +0 -88
  14. package/bun.lock +0 -26
  15. package/bunfig.toml +0 -2
  16. package/config/brief-policies.json +0 -28
  17. package/config/ceremony-profiles.json +0 -212
  18. package/config/consumer-cascade-map.json +0 -24
  19. package/config/doc-cascade-map.json +0 -24
  20. package/config/eval.json +0 -84
  21. package/config/profiles/account-plan.yaml +0 -36
  22. package/config/profiles/campaign.yaml +0 -94
  23. package/config/prove-evidence.schema.json +0 -65
  24. package/config/spec-policies.json +0 -36
  25. package/config/workflow-schema.json +0 -281
  26. package/gates/SCHEMA-GUIDE.md +0 -121
  27. package/gates/ac-quality.test.ts +0 -106
  28. package/gates/adversarial.test.ts +0 -274
  29. package/gates/brief-assembler.test.ts +0 -429
  30. package/gates/brief-assembler.ts +0 -334
  31. package/gates/bunfig.toml +0 -2
  32. package/gates/chain.test.ts +0 -91
  33. package/gates/e2e-smoke.test.ts +0 -234
  34. package/gates/error-classifier.test.ts +0 -249
  35. package/gates/error-classifier.ts +0 -89
  36. package/gates/orchestrator.test.ts +0 -950
  37. package/gates/orchestrator.ts +0 -569
  38. package/gates/preload.ts +0 -77
  39. package/gates/prompts/ac-adversary.md +0 -20
  40. package/gates/prompts/evidence-validator.md +0 -22
  41. package/gates/prompts/prove-reproducer.md +0 -53
  42. package/gates/prove.test.ts +0 -164
  43. package/gates/run-gate.ts +0 -936
  44. package/gates/schema-parity.test.ts +0 -197
  45. package/gates/schema.ts +0 -362
  46. package/gates/self-heal.test.ts +0 -511
  47. package/gates/self-heal.ts +0 -121
  48. package/gates/ship-orchestrator.test.ts +0 -379
  49. package/gates/ship-orchestrator.ts +0 -363
  50. package/gates/test-fixtures/broken-no-environments.json +0 -60
  51. package/gates/test-fixtures/valid-standard.json +0 -54
  52. package/gates/test-utils.ts +0 -43
  53. package/gates/witness.ts +0 -220
  54. package/gates/workflow.test.ts +0 -1053
  55. package/hooks/AgentBriefGuard.hook.ts +0 -586
  56. package/hooks/AgentVerdictCapture.hook.ts +0 -134
  57. package/hooks/AutoVerifyGate.hook.ts +0 -76
  58. package/hooks/GateEnforcement.hook.ts +0 -273
  59. package/hooks/IssueCloseGuard.hook.ts +0 -116
  60. package/hooks/MergeGuard.hook.ts +0 -55
  61. package/hooks/StaleTTLCleanup.hook.ts +0 -133
  62. package/hooks/VerifyPhaseLock.hook.ts +0 -81
  63. package/hooks/WorkflowStateGuard.hook.ts +0 -37
  64. package/hooks/lib/agentDetection.ts +0 -70
  65. package/hooks/lib/findWorkflow.ts +0 -101
  66. package/hooks/lib/parseStdin.ts +0 -37
  67. package/hooks/lib/paths.ts +0 -111
  68. package/hooks/lib/utils.ts +0 -81
  69. package/reference/migration-manifest.json +0 -554
  70. package/reference/prompts/agent-aditi.md +0 -38
  71. package/reference/prompts/agent-marcus.md +0 -38
  72. package/reference/prompts/agent-quinn.md +0 -38
  73. package/reference/prompts/agent-rook.md +0 -37
  74. package/reference/prompts/agent-serena.md +0 -37
  75. package/reference/prompts/council-skill.md +0 -170
  76. package/reference/prompts/goal-skill.md +0 -235
  77. package/reference/prompts/prove-skill.md +0 -283
  78. package/reference/prompts/quinn-standard.md +0 -246
  79. package/reference/prompts/ship-convergence.md +0 -65
  80. package/reference/prompts/ship-escalation.md +0 -43
  81. package/reference/prompts/ship-iteration.md +0 -382
  82. package/reference/prompts/ship-pending-patches.md +0 -246
  83. package/reference/prompts/ship-reference.md +0 -250
  84. package/reference/prompts/ship-skill.md +0 -347
  85. package/reference/prompts/ship-templates.md +0 -215
  86. package/reference/scripts/batch-status.sh +0 -68
  87. package/reference/scripts/chain-compat-check.sh +0 -67
  88. package/reference/scripts/context-audit.sh +0 -314
  89. package/reference/scripts/dead-export-check.sh +0 -61
  90. package/reference/scripts/generate-qc-tables.sh +0 -92
  91. package/reference/scripts/harness-init.sh +0 -111
  92. package/reference/scripts/pai-054-baseline-check.sh +0 -40
  93. package/reference/scripts/prove-smoke-test.sh +0 -112
  94. package/reference/scripts/setup-keychain-salt.sh +0 -12
  95. package/reference/scripts/skill-optimizer-friday.sh +0 -88
  96. package/reference/scripts/skill-qc-test.sh +0 -351
  97. package/reference/scripts/skill-synthesize-tonight.sh +0 -66
  98. package/reference/specs/HARNESS-EXTRACTION-SPEC.md +0 -263
  99. package/reference/specs/harness-v3-bash-deletion-plan.md +0 -65
  100. package/reference/specs/harness-v3-migration-plan.md +0 -188
  101. package/scripts/decision-reconcile.sh +0 -74
  102. package/scripts/decision-reconcile.ts +0 -152
  103. package/scripts/git-hooks/pre-commit +0 -76
  104. package/scripts/git-hooks/pre-push +0 -271
  105. package/scripts/harness-skill-check.sh +0 -52
  106. package/scripts/lib/gap-emit.sh +0 -65
  107. package/scripts/lib/jq-update.sh +0 -30
  108. package/scripts/lib/resolve-slug.sh +0 -30
  109. package/scripts/lib/verify-spec-compliance.sh +0 -102
  110. package/scripts/prove-backfill.sh +0 -87
  111. package/scripts/scaffold-rungate-config.ts +0 -91
  112. package/scripts/self-containment-check.sh +0 -109
  113. package/scripts/skill-qc-validator.sh +0 -402
  114. package/scripts/slug-resolver.sh +0 -14
  115. package/specs/BOOTSTRAP-DATA-FLOW-SPEC.md +0 -2493
  116. package/specs/BOOTSTRAP-TEST-PLAN.md +0 -571
  117. package/specs/HARNESS-GATES.md +0 -256
  118. package/specs/HARNESS-SKILL-CHAIN.md +0 -286
  119. package/specs/HARNESS-SKILL-CONTRACT.md +0 -93
  120. package/specs/HARNESS-STANDARD.md +0 -488
  121. package/specs/SPEC-TEMPLATE.md +0 -53
  122. package/specs/bootstrap-data-flow.png +0 -0
  123. package/specs/harness-automation-matrix.md +0 -314
  124. package/specs/harness-automation-matrix.rules.json +0 -64
  125. package/test/anti.test.ts +0 -80
  126. package/test/contract-negative.test.ts +0 -86
  127. package/test/contract.test.ts +0 -158
  128. package/test/external-deps.test.ts +0 -48
  129. package/test/fixtures/golden-project/.env.example +0 -3
  130. package/test/fixtures/golden-project/Makefile +0 -7
  131. package/test/fixtures/golden-project/docs/adr/ADR-001-framework.md +0 -23
  132. package/test/fixtures/golden-project/docs/setup-guide.md +0 -10
  133. package/test/fixtures/golden-project/package.json +0 -10
  134. package/test/fixtures/golden-project/specs/api-spec.md +0 -20
  135. package/test/fixtures/golden-project/src/auth.ts +0 -8
  136. package/test/fixtures/golden-project/src/db.ts +0 -11
  137. package/test/fixtures/golden-project/src/index.ts +0 -19
  138. package/test/fixtures/golden-project/src/utils/format.ts +0 -3
  139. package/test/fixtures/golden-project/test/auth.test.ts +0 -15
  140. package/test/fixtures/golden-project/tsconfig.json +0 -12
  141. package/test/meta-sc-coverage.test.ts +0 -159
  142. package/test/phase-0.test.ts +0 -846
  143. package/test/phase-1.test.ts +0 -617
  144. package/test/phase-2.test.ts +0 -620
  145. package/test/phase-3.test.ts +0 -99
  146. package/test/scaffold-conformity.test.ts +0 -8
  147. package/test/schema-canary.test.ts +0 -51
  148. package/test/spec-compliance-auto.test.ts +0 -64
  149. package/test/spec-compliance.test.ts +0 -295
  150. package/test/spec-discovery.test.ts +0 -68
  151. package/test/spec-drift.test.ts +0 -5
  152. package/test/structure.test.ts +0 -165
  153. package/test/unit/agent-brief-guard-ship.test.ts +0 -293
  154. package/test/unit/extract-constraints.test.ts +0 -145
  155. package/test/unit/issue-close-guard.test.ts +0 -204
  156. package/test/unit/scaffold-project.test.ts +0 -417
  157. package/test/unit/skill-cooccurrence.test.ts +0 -177
  158. package/test/unit/skill-sequence-logger.test.ts +0 -244
  159. package/test/unit/workflow-state-guard.test.ts +0 -95
  160. package/workflows/batch-ship.js +0 -295
  161. package/workflows/council.js +0 -481
  162. package/workflows/prove.js +0 -560
  163. package/workflows/ship.js +0 -789
  164. package/workflows/verify.js +0 -236
package/gates/run-gate.ts DELETED
@@ -1,936 +0,0 @@
1
- #!/usr/bin/env bun
2
- import { execSync } from "child_process";
3
- import { readFileSync, writeFileSync, existsSync, unlinkSync } from "fs";
4
- import { join } from "path";
5
- import {
6
- writeGateResult,
7
- generateHmac,
8
- generateShipEvidence,
9
- runConvergence,
10
- writeGateSummary,
11
- markCommandlessACsAsSkip,
12
- type GateResult,
13
- } from "./orchestrator";
14
- import { writeWitness } from "./witness";
15
- import { harnessRoot } from "../lib/paths";
16
- import { safeParseProjectHarness, type ProjectHarness } from "../lib/rungate-schema";
17
- import {
18
- slugExists,
19
- shouldRebuildContainer,
20
- shouldRefreshScaffold,
21
- worktreeBranchName,
22
- checkPortCollision,
23
- trackPreExistingFailures,
24
- releaseLock,
25
- detectFileSetOverlap,
26
- sequentialMergeOrder,
27
- shouldRunCIVerification,
28
- requiresResearchEscalation,
29
- } from "./ship-orchestrator";
30
-
31
- export function parseTestResults(output: string): GateResult[] {
32
- const results: GateResult[] = [];
33
- const lines = output.split("\n");
34
- for (let i = 0; i < lines.length; i++) {
35
- const pm = lines[i].match(/\(pass\)\s+(.+?)(?:\s+\[|$)/);
36
- if (pm) {
37
- results.push({ check: pm[1].trim(), result: "PASS", detail: "passed" });
38
- continue;
39
- }
40
- const fm = lines[i].match(/\(fail\)\s+(.+?)(?:\s+\[|$)/);
41
- if (fm) {
42
- const detailLines: string[] = [];
43
- for (let j = i + 1; j < lines.length; j++) {
44
- if (/\(pass\)\s+/.test(lines[j]) || /\(fail\)\s+/.test(lines[j])) break;
45
- const trimmed = lines[j].trim();
46
- if (trimmed) detailLines.push(trimmed);
47
- }
48
- results.push({ check: fm[1].trim(), result: "FAIL", detail: detailLines.length > 0 ? detailLines.join("\n") : "failed" });
49
- }
50
- }
51
- return results;
52
- }
53
-
54
- if (!import.meta.main) {
55
- // Imported as module — skip main execution
56
- } else {
57
-
58
- const args = process.argv.slice(2);
59
- let gate = "";
60
- let slug = "";
61
- let issue = 0;
62
-
63
- for (let i = 0; i < args.length; i++) {
64
- if (args[i] === "--gate" && args[i + 1]) gate = args[++i];
65
- if (args[i] === "--slug" && args[i + 1]) slug = args[++i];
66
- if (args[i] === "--issue" && args[i + 1]) issue = parseInt(args[++i]);
67
- }
68
-
69
- if (!gate || !["scope", "verify", "ship", "merge", "prove"].includes(gate)) {
70
- console.error("Usage: bun run gates/run-gate.ts --gate scope|verify|ship|merge|prove --slug SLUG [--issue NUM]");
71
- process.exit(1);
72
- }
73
-
74
- const WORK_DIR = join(process.env.RUNGATE_WORK_DIR || join(process.env.HOME || "", ".rungate"), slug);
75
- const SF = join(WORK_DIR, "workflow-state.json");
76
- const CEREMONY_PROFILE = existsSync(join(__dirname, "ceremony-profiles.json"))
77
- ? join(__dirname, "ceremony-profiles.json")
78
- : join(process.env.HOME || "", ".claude", "skills", "ship", "ceremony-profiles.json");
79
-
80
- if (!existsSync(SF)) {
81
- console.error(`BLOCKED — ${SF} not found`);
82
- process.exit(1);
83
- }
84
-
85
- const state = JSON.parse(readFileSync(SF, "utf-8"));
86
- if (!issue) issue = state.issue || 0;
87
- if (!slug) slug = state.slug || "";
88
- const issueRepo = state.issueRepo || state.repo || "";
89
-
90
- // Validate rungate.json against Zod schema (SC-6)
91
- let validatedHarness: ProjectHarness | null = null;
92
- const projectHarnessPath = state.projectRoot
93
- ? join(state.projectRoot, ".claude", "rungate.json")
94
- : "";
95
- if (projectHarnessPath && existsSync(projectHarnessPath)) {
96
- const rawHarness = JSON.parse(readFileSync(projectHarnessPath, "utf-8"));
97
- const result = safeParseProjectHarness(rawHarness);
98
- if (!result.success) {
99
- console.warn(`WARN: rungate.json schema validation failed:`);
100
- for (const err of result.error.issues) {
101
- console.warn(` - ${err.path.join(".")}: ${err.message}`);
102
- }
103
- } else {
104
- validatedHarness = result.data;
105
- }
106
- }
107
-
108
- // Auto-populate AC verdicts (ported from ac-populate.sh)
109
- let acUpdated = false;
110
- for (let i = 0; i < (state.acs || []).length; i++) {
111
- const ac = state.acs[i];
112
- if (ac.verdict && ac.verdict !== "PENDING") continue;
113
- const cmd = ac.evidenceMethod?.command;
114
- if (!cmd) continue;
115
- try {
116
- const output = execSync(cmd, { encoding: "utf-8", timeout: 10000, cwd: state.projectRoot || process.cwd() }).trim();
117
- const lastLine = output.split("\n").pop() || "";
118
- let verdict: "PASS" | "FAIL" = "FAIL";
119
- const { op, value } = ac.threshold || {};
120
- if (op && value !== undefined) {
121
- const num = parseFloat(lastLine);
122
- const exp = parseFloat(String(value));
123
- switch (op) {
124
- case "==": verdict = lastLine === String(value) || (!isNaN(num) && num === exp) ? "PASS" : "FAIL"; break;
125
- case "!=": verdict = lastLine !== String(value) ? "PASS" : "FAIL"; break;
126
- case ">=": verdict = !isNaN(num) && num >= exp ? "PASS" : "FAIL"; break;
127
- case "<=": verdict = !isNaN(num) && num <= exp ? "PASS" : "FAIL"; break;
128
- case ">": verdict = !isNaN(num) && num > exp ? "PASS" : "FAIL"; break;
129
- case "<": verdict = !isNaN(num) && num < exp ? "PASS" : "FAIL"; break;
130
- case "contains": verdict = output.includes(String(value)) ? "PASS" : "FAIL"; break;
131
- }
132
- }
133
- state.acs[i].verdict = verdict;
134
- state.acs[i].evidence = { type: "command-output", content: lastLine };
135
- acUpdated = true;
136
- } catch { /* command failed — leave as PENDING */ }
137
- }
138
- // #513 SC-A1: ACs without evidenceMethod.command get SKIP, not generic output
139
- const skipped = markCommandlessACsAsSkip(SF);
140
- if (skipped > 0) {
141
- // Re-read state after markCommandlessACsAsSkip wrote it
142
- const refreshed = JSON.parse(readFileSync(SF, "utf-8"));
143
- Object.assign(state, refreshed);
144
- } else if (acUpdated) {
145
- writeFileSync(SF, JSON.stringify(state, null, 2));
146
- }
147
-
148
- // B3: Prove Reproducer — runs BEFORE test suite so prove.test.ts can verify/override (ADR-009)
149
- if (gate === "prove") {
150
- const tier = state.sizing?.ceremonyTier || "STANDARD";
151
- if (tier === "LIGHT") {
152
- console.log("prove gate: LIGHT tier — skipping reproducer (API check only)");
153
- } else {
154
- let issueBody = "";
155
- let issueTitle = "";
156
- if (issue && issueRepo) {
157
- try {
158
- const ghOut = execSync(
159
- `gh issue view ${issue} --repo ${issueRepo} --json body,title`,
160
- { encoding: "utf-8", timeout: 15000 }
161
- );
162
- const parsed = JSON.parse(ghOut);
163
- issueBody = parsed.body || "";
164
- issueTitle = parsed.title || "";
165
- console.log(`prove gate: read issue #${issue} from GitHub (${issueTitle.slice(0, 60)})`);
166
- } catch (e: any) {
167
- console.error(`prove gate: FAIL — could not read issue from GitHub: ${e.message?.slice(0, 200)}`);
168
- process.exit(1);
169
- }
170
- } else {
171
- console.error("prove gate: FAIL — issue number and repo required for prove");
172
- process.exit(1);
173
- }
174
-
175
- const projectRoot = state.projectRoot || "";
176
- const harnessPath = join(projectRoot, ".claude", "rungate.json");
177
- let apiBase = "";
178
- let uiBase = "";
179
- let devStart = "";
180
- if (existsSync(harnessPath)) {
181
- const harness = JSON.parse(readFileSync(harnessPath, "utf-8"));
182
- apiBase = harness.dev?.apiBase || "";
183
- uiBase = harness.dev?.uiBase || "";
184
- devStart = harness.dev?.start || "";
185
- }
186
-
187
- if (apiBase) {
188
- try {
189
- execSync(`curl -sf ${apiBase}/api/admin/health`, { timeout: 10000, encoding: "utf-8" });
190
- console.log(`prove gate: health check PASS (${apiBase})`);
191
- } catch {
192
- console.error(`prove gate: FAIL — dev server not responding at ${apiBase}`);
193
- if (devStart) console.error(`Start it with: ${devStart}`);
194
- process.exit(1);
195
- }
196
- }
197
-
198
- // Prod health check — WARN only (prove.test.ts enforces the hard gate via #294/#484)
199
- const prodApiBase = (() => {
200
- if (existsSync(harnessPath)) {
201
- const harness = JSON.parse(readFileSync(harnessPath, "utf-8"));
202
- return harness.prod?.apiBase || "";
203
- }
204
- return "";
205
- })();
206
- if (prodApiBase) {
207
- try {
208
- execSync(`curl -sf ${prodApiBase}/api/admin/health`, { timeout: 10000, encoding: "utf-8" });
209
- console.log(`prove gate: prod health check PASS (${prodApiBase})`);
210
- } catch {
211
- console.warn(`WARN: prove gate — prod server not responding at ${prodApiBase} (STANDARD+ requires prod evidence)`);
212
- }
213
- }
214
-
215
- const proveInput = {
216
- issueBody, issueTitle, issueNumber: issue,
217
- acs: state.acs || [], apiBase, uiBase,
218
- beforeState: state.beforeState || null, projectRoot,
219
- };
220
- const proveInputPath = join(WORK_DIR, "prove-input.json");
221
- writeFileSync(proveInputPath, JSON.stringify(proveInput, null, 2));
222
-
223
- const proveProjectPrompt = join(__dirname, "prompts", "prove-reproducer.md");
224
- const proveHomePrompt = join(process.env.HOME || "", ".claude", "gates", "prompts", "prove-reproducer.md");
225
- const provePromptPath = existsSync(proveProjectPrompt) ? proveProjectPrompt : proveHomePrompt;
226
- if (existsSync(provePromptPath)) {
227
- console.log("B3: Spawning Prove Reproducer (Sonnet)...");
228
- let reproducerOutput = "";
229
- try {
230
- reproducerOutput = execSync(
231
- `cat '${proveInputPath}' | claude -p - --model sonnet --system-prompt-file "${provePromptPath}" --output-format text --allowedTools "Bash,Read" --max-turns 10`,
232
- { encoding: "utf-8", timeout: 180000, cwd: harnessRoot() }
233
- );
234
- } catch (e: any) {
235
- reproducerOutput = e.stdout || "";
236
- console.warn(`WARN: B3 reproducer process exited with error: ${e.message?.slice(0, 200)}`);
237
- }
238
-
239
- const jsonMatch = reproducerOutput.match(/\{[\s\S]*"verdict"[\s\S]*\}/);
240
- if (jsonMatch) {
241
- try {
242
- const parsed = JSON.parse(jsonMatch[0]);
243
- const proveEvidence = {
244
- issueNumber: issue, verdict: parsed.verdict || "INCONCLUSIVE",
245
- commitSHA: execSync("git rev-parse HEAD", { encoding: "utf-8", cwd: projectRoot || undefined }).trim(),
246
- capturedAt: new Date().toISOString(),
247
- criteriaResults: parsed.criteriaResults || [],
248
- reproduced: parsed.reproduced ?? false,
249
- evidence: parsed.evidence || [],
250
- source: "B3-prove-reproducer",
251
- };
252
- writeFileSync(join(WORK_DIR, "prove-evidence.json"), JSON.stringify(proveEvidence, null, 2));
253
- console.log(`B3: prove-evidence.json written — verdict: ${proveEvidence.verdict}`);
254
- } catch {
255
- console.warn("WARN: B3 reproducer JSON parse failed");
256
- }
257
- } else {
258
- console.warn("WARN: B3 reproducer produced no parseable JSON — prove.test.ts will generate evidence");
259
- }
260
- }
261
- }
262
- }
263
-
264
- // Pre-flight: duplicate slug check (SC-44)
265
- if (gate === "scope") {
266
- const existingSlug = slugExists(state.issue);
267
- if (existingSlug && existingSlug !== state.slug) {
268
- console.warn(`WARN: Issue #${state.issue} already has slug "${existingSlug}" — current slug is "${state.slug}"`);
269
- }
270
- }
271
-
272
- // Pre-flight: container rebuild check (SC-7)
273
- if (gate === "scope") {
274
- const canRebuild = shouldRebuildContainer(state.projectRoot || process.cwd());
275
- if (!canRebuild) {
276
- console.log("Container rebuild: SKIP (prod.rebuild is null)");
277
- }
278
- }
279
-
280
- // Pre-flight: worktree branch naming (SC-136)
281
- if (gate === "scope" && state.worktree) {
282
- const branch = worktreeBranchName(state.issue, state.slug);
283
- console.log(`Worktree branch: ${branch}`);
284
- if (state.worktree?.activePorts) {
285
- const collision = checkPortCollision(state.worktree.basePort || 3000, state.worktree.offset || 0, state.worktree.activePorts);
286
- if (collision) console.error(`WARN: Port collision detected for offset ${state.worktree.offset}`);
287
- }
288
- }
289
-
290
- // Pre-flight: file-set overlap detection for parallel work (SC-69)
291
- if (gate === "scope" && state.parallelFiles && state.briefFiles) {
292
- const overlap = detectFileSetOverlap(state.parallelFiles, state.briefFiles);
293
- if (overlap.length > 0) {
294
- console.error(`WARN: File overlap with parallel issue: ${overlap.join(", ")}`);
295
- }
296
- }
297
-
298
- // Pre-flight: spec-compliance check (runs at scope gate — catches spec↔code drift)
299
- if (gate === "scope") {
300
- // Auto-regenerate spec tests from spec before checking (closed-loop sync)
301
- try {
302
- execSync(`bun scripts/sync-spec-tests.ts 2>&1`, { encoding: "utf-8", timeout: 10000, cwd: harnessRoot() });
303
- } catch { /* sync script failure is non-blocking */ }
304
-
305
- try {
306
- const complianceOutput = execSync(
307
- `bun test test/spec-compliance.test.ts test/spec-compliance-auto.test.ts 2>&1`,
308
- { encoding: "utf-8", timeout: 30000, cwd: harnessRoot() }
309
- );
310
- const complianceFails = complianceOutput.match(/(\d+)\s+fail/);
311
- if (complianceFails && parseInt(complianceFails[1]) > 0) {
312
- console.error(`SPEC DRIFT DETECTED: ship.js has diverged from HARNESS-SKILL-CHAIN.md`);
313
- console.error(`Run: bun test test/spec-compliance.test.ts — to see which claims drifted`);
314
- console.error(complianceOutput.split("\n").filter(l => l.includes("(fail)")).join("\n"));
315
- } else {
316
- const compliancePasses = complianceOutput.match(/(\d+)\s+pass/);
317
- console.log(`Spec compliance: ${compliancePasses ? compliancePasses[1] : '?'} checks PASS — ship.js matches spec`);
318
- }
319
- } catch (e: any) {
320
- const out = e.stdout || "";
321
- console.error(`SPEC DRIFT DETECTED: spec-compliance tests failed`);
322
- console.error(out.split("\n").filter((l: string) => l.includes("(fail)")).join("\n"));
323
- }
324
- }
325
-
326
- // Type check (reads typeCheck from rungate.json — SC-10)
327
- if (gate === "scope" || gate === "verify") {
328
- const harnessPath = join(state.projectRoot || process.cwd(), ".claude", "rungate.json");
329
- const harnessConfig = existsSync(harnessPath) ? JSON.parse(readFileSync(harnessPath, "utf-8")) : null;
330
- const typeCheckCmd = state.dev?.typeCheck || harnessConfig?.dev?.typeCheck;
331
- if (typeCheckCmd) {
332
- try {
333
- execSync(typeCheckCmd, { encoding: "utf-8", timeout: 30000, cwd: state.projectRoot || process.cwd() });
334
- console.log(`Type check PASS: ${typeCheckCmd}`);
335
- } catch (e: any) {
336
- console.error(`Type check FAIL: ${typeCheckCmd}`);
337
- console.error((e.stdout || "").split("\n").slice(0, 10).join("\n"));
338
- }
339
- }
340
- }
341
-
342
- // Blast radius check — filesRead ≥ filesChanged (SC-59, SC-60)
343
- if (gate === "verify" && state.blastRadius) {
344
- const { filesRead = 0, filesChanged = 0 } = state.blastRadius;
345
- if (filesRead < filesChanged) {
346
- console.error(`WARN: Blast radius — read ${filesRead} files but changed ${filesChanged} (read should be ≥ changed)`);
347
- }
348
- const readWriteRatio = filesRead / Math.max(filesChanged, 1);
349
- if (readWriteRatio < 3) {
350
- console.error(`WARN: Read/write ratio ${readWriteRatio.toFixed(1)} (target ≥ 3:1)`);
351
- }
352
- }
353
-
354
- // Fix-on-find check — silently dropped issues = FAIL (SC-109)
355
- if (gate === "verify" && state.foundIssues) {
356
- const dropped = (state.foundIssues || []).filter((i: any) => !i.disposition);
357
- if (dropped.length > 0) {
358
- console.error(`FAIL: ${dropped.length} found issues silently dropped — must be fixed, filed, or scoped out`);
359
- }
360
- }
361
-
362
- // Files changed outside brief scope = WARN (SC-134)
363
- if (gate === "verify" && state.filesChangedOutsideBrief?.length > 0) {
364
- console.error(`WARN: Files changed outside brief: ${state.filesChangedOutsideBrief.join(", ")}`);
365
- }
366
-
367
- // Track pre-existing failures at scope (SC-33)
368
- if (gate === "scope" && state.preExistingFailures?.length > 0) {
369
- trackPreExistingFailures(state.slug, state.preExistingFailures);
370
- }
371
-
372
- // Post-verify: scaffold refresh check (SC-38)
373
- if (gate === "verify" && shouldRefreshScaffold(state.slug)) {
374
- console.log("Post-verify: scaffold refresh recommended (verify gate PASS)");
375
- }
376
-
377
- // Research escalation check — iteration 2+ requires research before retry (SC-112)
378
- if (gate === "verify" && requiresResearchEscalation(state.slug)) {
379
- console.log("Research escalation: iteration 2+ — research tool invocation required before next attempt");
380
- }
381
-
382
- // Post-ship: release lock and check merge queue (SC-47, SC-72)
383
- if (gate === "ship") {
384
- releaseLock(state.slug);
385
- if (state.mergeQueue) {
386
- const ordered = sequentialMergeOrder(state.mergeQueue);
387
- const next = ordered[0];
388
- if (next && shouldRunCIVerification(next)) {
389
- console.log(`Next in merge queue: ${next.slug} (CI verification required)`);
390
- }
391
- }
392
- }
393
-
394
- // Run tests
395
- let testOutput: string;
396
- let testExitCode = 0;
397
- try {
398
- testOutput = execSync(
399
- `bun test gates/ --test-name-pattern "${gate === 'scope' ? 'schema|scope' : gate === 'verify' ? 'schema|scope|verify|B1|B2' : gate === 'ship' ? 'schema|scope|verify|ship|B1|B2' : gate === 'prove' ? 'prove' : '.*'}" 2>&1`,
400
- {
401
- encoding: "utf-8",
402
- timeout: 120000,
403
- cwd: harnessRoot(),
404
- env: { ...process.env, TEST_WORK_DIR: WORK_DIR, GATE: gate, PROJECT_ROOT: state.projectRoot || process.cwd() },
405
- },
406
- );
407
- } catch (e: any) {
408
- testOutput = e.stdout || "";
409
- testExitCode = e.status || 1;
410
- }
411
-
412
- // Parse test results from Bun text output
413
- let passes = 0;
414
- let fails = 0;
415
- let warns = 0;
416
- const results: GateResult[] = [];
417
-
418
- // Extract pass/fail counts from Bun's summary (last occurrence: " 139 pass\n 5 fail")
419
- const allPassMatches = [...testOutput.matchAll(/^\s*(\d+)\s+pass\s*$/gm)];
420
- const allFailMatches = [...testOutput.matchAll(/^\s*(\d+)\s+fail\s*$/gm)];
421
- const passMatch = allPassMatches.length > 0 ? allPassMatches[allPassMatches.length - 1] : null;
422
- const failMatch = allFailMatches.length > 0 ? allFailMatches[allFailMatches.length - 1] : null;
423
- if (passMatch) passes = parseInt(passMatch[1]);
424
- if (failMatch) fails = parseInt(failMatch[1]);
425
-
426
- // Extract individual test results: "(pass) name" or "(fail) name"
427
- results.push(...parseTestResults(testOutput));
428
-
429
- // Fallback: if no individual results parsed, create summary entry
430
- if (results.length === 0) {
431
- if (fails > 0 || testExitCode !== 0) {
432
- results.push({ check: "test-suite", result: "FAIL", detail: `${fails} tests failed` });
433
- } else {
434
- results.push({ check: "test-suite", result: "PASS", detail: `${passes} tests passed` });
435
- }
436
- }
437
-
438
- console.log(`\nTest results: ${passes} pass, ${fails} fail, ${warns} warn`);
439
-
440
- // Read conformity findings from project (if bun test wrote them)
441
- const projectRoot = state.projectRoot || process.cwd();
442
- const findingsPath = join(projectRoot, ".rungate", "conformity-findings.json");
443
- if (existsSync(findingsPath)) {
444
- try {
445
- const findings = JSON.parse(readFileSync(findingsPath, "utf-8"));
446
- if (findings.total > 0) {
447
- console.log(`\n── CONFORMITY FINDINGS (${findingsPath}) ──`);
448
- if (findings.findings?.length > 0) {
449
- console.log(` Issues: ${findings.failures} FAIL, ${findings.warnings} WARN`);
450
- for (const f of findings.findings) {
451
- console.log(` ${f.severity}: ${f.file} — ${f.message}`);
452
- if (f.fixCommand) console.log(` Fix: ${f.fixCommand}`);
453
- }
454
- }
455
- if (findings.constraintCandidates?.length > 0) {
456
- console.log(` Constraint candidates: ${findings.candidateCount} pending review`);
457
- for (const c of findings.constraintCandidates) {
458
- console.log(` "${c.rule}" (${c.source})`);
459
- }
460
- }
461
- if (findings.staleness?.length > 0) {
462
- console.log(` Stale docs: ${findings.staleCount}`);
463
- for (const s of findings.staleness) {
464
- console.log(` ${s.file} — ${s.daysSince}d old (${s.type} threshold: ${s.threshold}d)`);
465
- }
466
- }
467
- console.log(`──────────────────────────────────────────`);
468
- }
469
- // Store in workflow state for agents to read
470
- if (!state.conformityFindings) {
471
- state.conformityFindings = findings;
472
- }
473
- } catch {}
474
- }
475
-
476
- // Batch diagnosis output — enumerate ALL failures grouped by category (#485)
477
- if (fails > 0) {
478
- const failResults = results.filter(r => r.result === "FAIL");
479
- const infra = failResults.filter(r => /B1|B2|B3|adversary|reproducer|health|port/.test(r.check));
480
- const acRelated = failResults.filter(r => /evidence|verdict|AC|ac[- ]|threshold/.test(r.check));
481
- const chain = failResults.filter(r => /hash|chain|pushed|merge|spec-trace/.test(r.check));
482
- const other = failResults.filter(r => !infra.includes(r) && !acRelated.includes(r) && !chain.includes(r));
483
-
484
- console.log(`\nBATCH DIAGNOSIS: ${failResults.length} failures — fix all before re-running`);
485
- if (infra.length) console.log(` Infrastructure (${infra.length}): ${infra.map(r => r.check.split(" > ").pop()).join(", ")}`);
486
- if (acRelated.length) console.log(` AC/Evidence (${acRelated.length}): ${acRelated.map(r => r.check.split(" > ").pop()).join(", ")}`);
487
- if (chain.length) console.log(` Chain integrity (${chain.length}): ${chain.map(r => r.check.split(" > ").pop()).join(", ")}`);
488
- if (other.length) console.log(` Other (${other.length}): ${other.map(r => r.check.split(" > ").pop()).join(", ")}`);
489
- }
490
-
491
- // Agent result collectors for B1/B2 verify-time spawns
492
- let b2AgentResult: any = null;
493
- let b1VerifyAgentResult: any = null;
494
-
495
- // B1: AC Adversary — fire-and-forget at scope, checked at verify (ADR-009)
496
- if (gate === "scope" && fails === 0 && testExitCode === 0 && !process.env.RUNGATE_SKIP_AGENTS) {
497
- const tier = state.sizing?.ceremonyTier || "STANDARD";
498
- if (tier !== "LIGHT") {
499
- const b1ProjectPrompt = join(__dirname, "prompts", "ac-adversary.md");
500
- const b1HomePrompt = join(process.env.HOME || "", ".claude", "gates", "prompts", "ac-adversary.md");
501
- const promptPath = existsSync(b1ProjectPrompt) ? b1ProjectPrompt : b1HomePrompt;
502
- if (!existsSync(promptPath)) {
503
- console.warn("WARN: ac-adversary.md prompt not found — adversary will not run");
504
- } else {
505
- const reportPath = join(WORK_DIR, "adversary-report.json");
506
- // Remove stale report so verify can detect fresh vs missing
507
- try { unlinkSync(reportPath); } catch {}
508
- const acInputFile = join(WORK_DIR, "adversary-input.json");
509
- writeFileSync(acInputFile, JSON.stringify(state.acs || [], null, 2));
510
- // Spawn adversary in background — text-only, no tools needed
511
- const { spawn } = require("child_process");
512
- const child = spawn("sh", ["-c",
513
- `claude -p "$(cat '${acInputFile}')" --model haiku --system-prompt-file "${promptPath}" --output-format json --allowedTools "" --max-turns 3 > "${reportPath}.tmp" 2>/dev/null && mv "${reportPath}.tmp" "${reportPath}"`
514
- ], { detached: true, stdio: "ignore", cwd: harnessRoot() });
515
- child.unref();
516
- console.log("B1: AC Adversary spawned in background — verify gate will check result");
517
- }
518
- }
519
- }
520
-
521
- // Dev server liveness — OUTCOME ACs require running server (#90 RCA)
522
- if (gate === "verify" && fails === 0 && testExitCode === 0) {
523
- const hasOutcomeACs = (state.acs || []).some((ac: any) => ac.type === "OUTCOME");
524
- if (hasOutcomeACs && state.projectRoot) {
525
- const harnessPath = join(state.projectRoot, ".claude", "rungate.json");
526
- if (existsSync(harnessPath)) {
527
- const harness = JSON.parse(readFileSync(harnessPath, "utf-8"));
528
- const apiBase = harness?.dev?.apiBase;
529
- if (apiBase) {
530
- try {
531
- execSync(`curl -sf ${apiBase}/api/admin/health`, { timeout: 5000, encoding: "utf-8" });
532
- console.log(`Dev server liveness: ${apiBase} — UP`);
533
- } catch {
534
- fails++;
535
- results.push({ check: "dev-server-liveness: OUTCOME ACs require running dev server", result: "FAIL" as const, detail: `${apiBase} not responding — run '${harness?.dev?.start || "make dev-all"}' first` });
536
- console.log(`Dev server liveness: ${apiBase} — DOWN. Start with: ${harness?.dev?.start || "make dev-all"}`);
537
- }
538
- }
539
- }
540
- }
541
- }
542
-
543
- // B1 report check — verify gate reads adversary result (ADR-009)
544
- if (gate === "verify" && fails === 0 && testExitCode === 0) {
545
- const tier = state.sizing?.ceremonyTier || "STANDARD";
546
- if (tier !== "LIGHT") {
547
- const reportPath = join(WORK_DIR, "adversary-report.json");
548
- if (!existsSync(reportPath)) {
549
- fails++;
550
- results.push({ check: "B1-adversary: report missing", result: "FAIL" as const, detail: "adversary-report.json not found — adversary may still be running or failed. Re-run verify." });
551
- console.log("B1: adversary report not yet available — verify FAIL");
552
- } else {
553
- try {
554
- const raw = readFileSync(reportPath, "utf-8");
555
- const jsonMatch = raw.match(/\{[\s\S]*"gameable"[\s\S]*\}/);
556
- if (jsonMatch) {
557
- const report = JSON.parse(jsonMatch[0]);
558
- console.log(`B1: Adversary report — gameable: ${report.gameable}, approved: ${report.approved}`);
559
- if (report.gameable > 0) {
560
- let outcomeCount = 0;
561
- for (const exploit of report.exploits || []) {
562
- const ac = (state.acs || []).find((a: any) => a.id === exploit.acId);
563
- const isOutcome = ac?.type === "OUTCOME";
564
- if (isOutcome) {
565
- fails++;
566
- outcomeCount++;
567
- results.push({ check: `B1-adversary: ${exploit.acId} gameable (OUTCOME)`, result: "FAIL" as const, detail: exploit.exploit });
568
- } else {
569
- warns++;
570
- results.push({ check: `B1-adversary: ${exploit.acId} gameable`, result: "WARN" as const, detail: exploit.exploit });
571
- }
572
- }
573
- if (outcomeCount > 0) console.log(`B1: ${outcomeCount} OUTCOME AC(s) gameable — FAIL (need behavioral evidence)`);
574
- const codeCount = (report.gameable || 0) - outcomeCount;
575
- if (codeCount > 0) console.log(`B1: ${codeCount} CODE AC(s) gameable — WARN`);
576
- }
577
- } else {
578
- console.warn("WARN: B1 adversary report has no parseable JSON — treating as incomplete");
579
- }
580
- } catch (e: any) {
581
- console.warn(`WARN: B1 report read failed: ${e.message?.slice(0, 200)}`);
582
- }
583
- }
584
- }
585
- }
586
-
587
- // B2: Evidence Validator — run evidence commands in clean worktree (ADR-009)
588
- if (gate === "verify" && fails === 0 && testExitCode === 0) {
589
- const tier = state.sizing?.ceremonyTier || "STANDARD";
590
- if (tier !== "LIGHT") {
591
- const scopeSha = state.gates?.scope?.commitSha;
592
- const fixSha = execSync("git rev-parse HEAD", { encoding: "utf-8", cwd: state.projectRoot || undefined }).trim();
593
-
594
- if (scopeSha && state.projectRoot) {
595
- console.log("B2: Running evidence commands in clean worktree...");
596
- const tmpWorktree = join("/tmp", `pai-evidence-${Date.now()}`);
597
-
598
- try {
599
- // Create worktree at fix commit
600
- execSync(`git worktree add "${tmpWorktree}" ${fixSha} --detach`, { cwd: state.projectRoot, encoding: "utf-8" });
601
-
602
- const verdicts: any[] = [];
603
- for (const ac of state.acs || []) {
604
- if (!ac.evidenceMethod?.command) continue;
605
- const cmd = ac.evidenceMethod.command;
606
-
607
- // Run in fix worktree
608
- let fixOutput = "";
609
- try {
610
- fixOutput = execSync(cmd, { encoding: "utf-8", timeout: 15000, cwd: tmpWorktree }).trim();
611
- } catch (e: any) {
612
- fixOutput = (e.stdout || "").trim() || `ERROR: ${e.message?.slice(0, 100)}`;
613
- }
614
-
615
- // Metamorphic: run same command at scope SHA
616
- let preFixOutput = "";
617
- const scopeWorktree = join("/tmp", `pai-metamorphic-${Date.now()}`);
618
- try {
619
- execSync(`git worktree add "${scopeWorktree}" ${scopeSha} --detach`, { cwd: state.projectRoot, encoding: "utf-8" });
620
- preFixOutput = execSync(cmd, { encoding: "utf-8", timeout: 15000, cwd: scopeWorktree }).trim();
621
- } catch (e: any) {
622
- preFixOutput = "METAMORPHIC_SKIP";
623
- } finally {
624
- try { execSync(`git worktree remove "${scopeWorktree}" --force`, { cwd: state.projectRoot }); } catch {}
625
- }
626
-
627
- // Compare thresholds
628
- const actual = parseFloat(fixOutput) || 0;
629
- const expected = parseFloat(ac.threshold?.value || "0");
630
- const op = ac.threshold?.op || ">=";
631
- let passes = false;
632
- if (op === ">=") passes = actual >= expected;
633
- else if (op === "==") passes = actual === expected;
634
- else if (op === ">") passes = actual > expected;
635
- else if (op === "<=") passes = actual <= expected;
636
-
637
- // Metamorphic check
638
- if (fixOutput === preFixOutput && preFixOutput !== "METAMORPHIC_SKIP") {
639
- console.warn(`WARN: metamorphic — ${ac.id} evidence identical pre/post fix (trivially satisfiable)`);
640
- }
641
-
642
- verdicts.push({
643
- acId: ac.id,
644
- command: cmd,
645
- rawOutput: fixOutput,
646
- threshold: ac.threshold,
647
- actual: fixOutput,
648
- verdict: passes ? "PASS" : "FAIL",
649
- metamorphic: fixOutput === preFixOutput ? "IDENTICAL" : "DIFFERENT",
650
- });
651
- }
652
-
653
- // Write verdicts to workflow-state (gate-runner writes, not DA)
654
- for (const v of verdicts) {
655
- const ac = (state.acs || []).find((a: any) => a.id === v.acId);
656
- if (ac) {
657
- ac.verdict = v.verdict;
658
- ac.evidence = { type: "command-output", content: v.rawOutput };
659
- }
660
- }
661
-
662
- console.log(`B2: ${verdicts.filter(v => v.verdict === "PASS").length}/${verdicts.length} evidence commands PASS`);
663
-
664
- } catch (e: any) {
665
- console.warn(`WARN: B2 worktree setup failed: ${e.message?.slice(0, 200)}`);
666
- } finally {
667
- try { execSync(`git worktree remove "${tmpWorktree}" --force`, { cwd: state.projectRoot }); } catch {}
668
- }
669
- }
670
- }
671
- }
672
-
673
- // B2 Agent: Evidence Validator — LLM review of evidence quality (#1407, ADR-009)
674
- if (gate === "verify" && fails === 0 && testExitCode === 0 && !process.env.RUNGATE_SKIP_AGENTS) {
675
- const tier = state.sizing?.ceremonyTier || "STANDARD";
676
- if (tier !== "LIGHT") {
677
- const b2ProjectPrompt = join(__dirname, "prompts", "evidence-validator.md");
678
- const b2HomePrompt = join(process.env.HOME || "", ".claude", "gates", "prompts", "evidence-validator.md");
679
- const b2PromptPath = existsSync(b2ProjectPrompt) ? b2ProjectPrompt : b2HomePrompt;
680
- if (existsSync(b2PromptPath)) {
681
- console.log("B2: Spawning Evidence Validator agent...");
682
- const b2Input = JSON.stringify({
683
- acs: (state.acs || []).map((ac: any) => ({
684
- id: ac.id, type: ac.type, statement: ac.statement,
685
- threshold: ac.threshold, evidence: ac.evidence, verdict: ac.verdict,
686
- evidenceMethod: ac.evidenceMethod,
687
- })),
688
- }, null, 2);
689
- const b2InputPath = join(WORK_DIR, "b2-input.json");
690
- writeFileSync(b2InputPath, b2Input);
691
-
692
- let b2Output = "";
693
- try {
694
- b2Output = execSync(
695
- `cat '${b2InputPath}' | claude -p - --model haiku --system-prompt-file "${b2PromptPath}" --output-format text --allowedTools "" --max-turns 3`,
696
- { encoding: "utf-8", timeout: 60000, cwd: harnessRoot() },
697
- );
698
- } catch (e: any) {
699
- b2Output = e.stdout || "";
700
- console.warn(`WARN: B2 Evidence Validator exited with error: ${e.message?.slice(0, 200)}`);
701
- }
702
-
703
- const b2Json = b2Output.match(/\{[\s\S]*"verdicts"[\s\S]*\}/);
704
- if (b2Json) {
705
- try {
706
- const b2Report = JSON.parse(b2Json[0]);
707
- b2AgentResult = {
708
- ts: new Date().toISOString(),
709
- verdicts: b2Report.verdicts || [],
710
- source: "B2-evidence-validator-agent",
711
- };
712
- const b2Fails = (b2Report.verdicts || []).filter((v: any) => v.verdict === "FAIL");
713
- if (b2Fails.length > 0) {
714
- for (const v of b2Fails) {
715
- warns++;
716
- results.push({ check: `B2-validator: ${v.acId} evidence gap`, result: "WARN" as const, detail: v.rawOutput || "threshold not met" });
717
- }
718
- console.log(`B2: ${b2Fails.length} evidence gap(s) found — WARN`);
719
- } else {
720
- console.log(`B2: All evidence validated — ${(b2Report.verdicts || []).length} ACs checked`);
721
- }
722
- } catch {
723
- console.warn("WARN: B2 Evidence Validator JSON parse failed");
724
- }
725
- } else {
726
- console.warn("WARN: B2 Evidence Validator produced no parseable JSON");
727
- }
728
- } else {
729
- console.warn("WARN: evidence-validator.md prompt not found — B2 agent will not run");
730
- }
731
- }
732
- }
733
-
734
- // B1 Agent: AC Adversary at verify — challenges evidence with full context (#1406, ADR-009)
735
- if (gate === "verify" && fails === 0 && testExitCode === 0 && !process.env.RUNGATE_SKIP_AGENTS) {
736
- const tier = state.sizing?.ceremonyTier || "STANDARD";
737
- if (tier !== "LIGHT") {
738
- const b1ProjectPrompt = join(__dirname, "prompts", "ac-adversary.md");
739
- const b1HomePrompt = join(process.env.HOME || "", ".claude", "gates", "prompts", "ac-adversary.md");
740
- const b1PromptPath = existsSync(b1ProjectPrompt) ? b1ProjectPrompt : b1HomePrompt;
741
- if (existsSync(b1PromptPath)) {
742
- console.log("B1: Spawning AC Adversary agent at verify...");
743
- const b1Input = JSON.stringify({
744
- acs: (state.acs || []).map((ac: any) => ({
745
- id: ac.id, type: ac.type, statement: ac.statement,
746
- threshold: ac.threshold, evidence: ac.evidence, verdict: ac.verdict,
747
- evidenceMethod: ac.evidenceMethod,
748
- })),
749
- }, null, 2);
750
- const b1InputPath = join(WORK_DIR, "b1-verify-input.json");
751
- writeFileSync(b1InputPath, b1Input);
752
-
753
- let b1Output = "";
754
- try {
755
- b1Output = execSync(
756
- `cat '${b1InputPath}' | claude -p - --model haiku --system-prompt-file "${b1PromptPath}" --output-format text --allowedTools "" --max-turns 3`,
757
- { encoding: "utf-8", timeout: 60000, cwd: harnessRoot() },
758
- );
759
- } catch (e: any) {
760
- b1Output = e.stdout || "";
761
- console.warn(`WARN: B1 AC Adversary exited with error: ${e.message?.slice(0, 200)}`);
762
- }
763
-
764
- const b1Json = b1Output.match(/\{[\s\S]*"gameable"[\s\S]*\}/);
765
- if (b1Json) {
766
- try {
767
- const b1Report = JSON.parse(b1Json[0]);
768
- b1VerifyAgentResult = {
769
- ts: new Date().toISOString(),
770
- gameable: b1Report.gameable || 0,
771
- approved: b1Report.approved ?? true,
772
- exploits: b1Report.exploits || [],
773
- source: "B1-adversary-verify-agent",
774
- };
775
- if (b1Report.gameable > 0) {
776
- for (const exploit of b1Report.exploits || []) {
777
- warns++;
778
- results.push({ check: `B1-adversary-verify: ${exploit.acId} gameable`, result: "WARN" as const, detail: exploit.exploit });
779
- }
780
- console.log(`B1: ${b1Report.gameable} gameable AC(s) found at verify — WARN`);
781
- } else {
782
- console.log(`B1: All ACs approved at verify — no exploits found`);
783
- }
784
- } catch {
785
- console.warn("WARN: B1 AC Adversary JSON parse failed");
786
- }
787
- } else {
788
- console.warn("WARN: B1 AC Adversary produced no parseable JSON");
789
- }
790
- }
791
- }
792
- }
793
-
794
- // Write gate result (pass projectRoot for cross-repo SHA accuracy, #91)
795
- const { resultVal, attempt } = writeGateResult(SF, gate, passes, fails, warns, results, state.projectRoot);
796
- console.log(`Gate ${gate}: ${resultVal} (attempt ${attempt})`);
797
-
798
- // Persist B1/B2 agent results to workflow-state.json AFTER writeGateResult (#1406, #1407)
799
- if (gate === "verify" && (b2AgentResult || b1VerifyAgentResult)) {
800
- const freshState = JSON.parse(readFileSync(SF, "utf-8"));
801
- freshState.gates = freshState.gates || {};
802
- freshState.gates.verify = freshState.gates.verify || {};
803
- if (b2AgentResult) {
804
- freshState.gates.verify.evidenceValidator = b2AgentResult;
805
- }
806
- if (b1VerifyAgentResult) {
807
- freshState.gates.verify.adversary = b1VerifyAgentResult;
808
- }
809
- if (state.conformityFindings) {
810
- freshState.conformityFindings = state.conformityFindings;
811
- }
812
- writeFileSync(SF, JSON.stringify(freshState, null, 2));
813
- }
814
-
815
- // Write tamper-evident witness for every gate
816
- try {
817
- const witnessPath = writeWitness(slug, gate, resultVal, testOutput, issue, state.projectRoot);
818
- console.log(`Witness: ${witnessPath}`);
819
- } catch (e: any) {
820
- console.error(`Witness write failed: ${e.message}`);
821
- }
822
-
823
- if (gate === "merge") {
824
- // C3 diff-validator: lightweight pre-merge checks (ADR-009)
825
- const marcusBranch = state.agents?.marcus?.branch;
826
- const scopeSha = state.gates?.scope?.commitSha;
827
-
828
- // Determine diff base: worktree branch or scope SHA for non-worktree
829
- let diffBase: string;
830
- if (marcusBranch && marcusBranch !== "main") {
831
- diffBase = `main...${marcusBranch}`;
832
- } else if (scopeSha) {
833
- diffBase = `${scopeSha}...HEAD`;
834
- } else {
835
- console.log("merge gate: no branch or scope SHA — skipping diff checks");
836
- // Still write gate result
837
- const { resultVal, attempt } = writeGateResult(SF, gate, 1, 0, 0, [], state.projectRoot);
838
- console.log(`Gate ${gate}: ${resultVal} (attempt ${attempt})`);
839
- process.exit(0);
840
- }
841
-
842
- let mergePass = 0;
843
- let mergeFail = 0;
844
- const mergeResults: any[] = [];
845
-
846
- // Check 1: no test file deletions
847
- const deletedResult = execSync(`git diff ${diffBase} --diff-filter=D --name-only`, { encoding: "utf-8", cwd: state.projectRoot || undefined }).trim();
848
- const deletedTests = deletedResult.split("\n").filter(f => /\.test\.(ts|tsx)$/.test(f));
849
- if (deletedTests.length > 0) {
850
- mergeFail++;
851
- mergeResults.push({ check: "merge: no test file deletions", result: "FAIL", detail: deletedTests.join(", ") });
852
- } else {
853
- mergePass++;
854
- mergeResults.push({ check: "merge: no test file deletions", result: "PASS" });
855
- }
856
-
857
- // Check 2: no gate/hook modifications outside scope (WARN only)
858
- const changedResult = execSync(`git diff ${diffBase} --name-only`, { encoding: "utf-8", cwd: state.projectRoot || undefined }).trim();
859
- const gateHookChanges = changedResult.split("\n").filter(f => /^(gates|hooks)\//.test(f));
860
- const issueTargetsGates = (state.issueGoal || "").toLowerCase().includes("gate");
861
- if (gateHookChanges.length > 0 && !issueTargetsGates) {
862
- mergeResults.push({ check: "merge: gate/hook modifications", result: "WARN", detail: gateHookChanges.join(", ") });
863
- } else {
864
- mergePass++;
865
- mergeResults.push({ check: "merge: gate/hook modifications", result: "PASS" });
866
- }
867
-
868
- // Check 3: scope proportionality
869
- const statResult = execSync(`git diff ${diffBase} --shortstat`, { encoding: "utf-8", cwd: state.projectRoot || undefined }).trim();
870
- const insertions = parseInt((statResult.match(/(\d+) insertion/) || ["0","0"])[1]);
871
- const size = state.sizing?.predicted || "M";
872
- if ((size === "XS" && insertions > 200) || (size === "S" && insertions > 500)) {
873
- mergeResults.push({ check: "merge: scope proportionality", result: "WARN", detail: `${insertions} insertions for ${size} issue` });
874
- } else {
875
- mergePass++;
876
- mergeResults.push({ check: "merge: scope proportionality", result: "PASS" });
877
- }
878
-
879
- const { resultVal: mergeResultVal, attempt: mergeAttempt } = writeGateResult(SF, gate, mergePass, mergeFail, 0, mergeResults, state.projectRoot);
880
- console.log(`Gate ${gate}: ${mergeResultVal} (attempt ${mergeAttempt})`);
881
- process.exit(mergeResultVal === "PASS" ? 0 : 1);
882
- }
883
-
884
- // Prove gate: add proven label on PROVEN verdict, exit based on test results (#1390)
885
- if (gate === "prove") {
886
- // Read prove-evidence.json for verdict
887
- const proveEvidencePath = join(WORK_DIR, "prove-evidence.json");
888
- if (existsSync(proveEvidencePath)) {
889
- try {
890
- const proveEvidence = JSON.parse(readFileSync(proveEvidencePath, "utf-8"));
891
- if (proveEvidence.verdict === "PROVEN" && issue && issueRepo) {
892
- // Add proven label to issue via gh CLI
893
- try {
894
- execSync(`gh issue edit ${issue} --repo ${issueRepo} --add-label "proven"`, {
895
- encoding: "utf-8",
896
- timeout: 15000,
897
- });
898
- console.log(`prove gate: added "proven" label to #${issue}`);
899
- } catch (e: any) {
900
- console.warn(`WARN: prove gate — failed to add proven label: ${e.message?.slice(0, 200)}`);
901
- }
902
- }
903
- } catch {
904
- // prove-evidence.json parse failure — already warned earlier
905
- }
906
- }
907
- process.exit(resultVal === "PASS" ? 0 : 1);
908
- }
909
-
910
- // Ship-specific: HMAC + evidence
911
- if (gate === "ship" && resultVal === "PASS") {
912
- const hash = generateHmac(SF, slug, issue, state.projectRoot);
913
- console.log(`HMAC: ${hash}`);
914
- const evidencePath = generateShipEvidence(SF, WORK_DIR, issue, issueRepo, passes, fails, warns);
915
- console.log(`Ship evidence: ${evidencePath}`);
916
- }
917
-
918
- // Verify-specific: convergence
919
- if (gate === "verify" && resultVal === "FAIL" && existsSync(CEREMONY_PROFILE)) {
920
- const convergenceResult = runConvergence(SF, gate, resultVal, CEREMONY_PROFILE);
921
- if (convergenceResult.action === "circuit-break") {
922
- console.log(`CIRCUIT BREAK — iteration ${convergenceResult.iteration}`);
923
- process.exit(2);
924
- } else if (convergenceResult.action === "stall") {
925
- console.log(`STALL DETECTED — same failures, iteration ${convergenceResult.iteration}`);
926
- } else if (convergenceResult.action === "loop-back") {
927
- console.log(`LOOP-BACK — iteration ${convergenceResult.iteration}`);
928
- }
929
- }
930
-
931
- // Gate summary
932
- const sessionId = process.env.SESSION_ID || "unknown";
933
- const summaryResult = writeGateSummary(gate, passes, fails, warns, results, issue, slug, sessionId);
934
-
935
- process.exit(summaryResult === "BLOCKED" ? 1 : 0);
936
- } // end if (import.meta.main)