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,234 @@
1
+ import { test, expect, describe } from "bun:test";
2
+ import { readFileSync, existsSync } from "fs";
3
+ import { join } from "path";
4
+ import { WorkflowStateSchema } from "./schema";
5
+ import { SIGNAL_PHRASE_PATTERNS } from "../lib/conformity";
6
+
7
+ const STANDALONE = !process.env.TEST_WORK_DIR;
8
+ const TEST_DIR = process.env.TEST_WORK_DIR || "/tmp/gate-e2e-smoke";
9
+ const SF = `${TEST_DIR}/workflow-state.json`;
10
+
11
+ function loadState(): any | null {
12
+ if (!existsSync(SF)) return null;
13
+ const raw = JSON.parse(readFileSync(SF, "utf-8"));
14
+ return WorkflowStateSchema.passthrough().parse(raw);
15
+ }
16
+
17
+ function sf(path: string): any {
18
+ if (!existsSync(SF)) return undefined;
19
+ const wf = JSON.parse(readFileSync(SF, "utf-8"));
20
+ const parts = path.replace(/^\./, "").split(".").filter(Boolean);
21
+ let val: any = wf;
22
+ for (const p of parts) {
23
+ if (val == null) return undefined;
24
+ val = val[p];
25
+ }
26
+ return val;
27
+ }
28
+
29
+ function loadSpec(path: string, projectRoot?: string): string {
30
+ const resolved = path.startsWith('/') ? path : join(projectRoot || '.', path);
31
+ return readFileSync(resolved, "utf-8");
32
+ }
33
+
34
+ function extractSpecRules(spec: string): string[] {
35
+ const rules: string[] = [];
36
+ for (const p of SIGNAL_PHRASE_PATTERNS) {
37
+ for (const m of spec.matchAll(new RegExp(p.source, p.flags))) {
38
+ rules.push(m[0].replace(/^[-*] /, "").trim().replace(/[.;]$/, ""));
39
+ }
40
+ }
41
+ return [...new Set(rules)];
42
+ }
43
+
44
+ describe("E2E: governing spec compliance", () => {
45
+ if (STANDALONE || !existsSync(SF)) {
46
+ test("skipped — no active workflow (standalone run)", () => {});
47
+ return;
48
+ }
49
+ const tier = sf("sizing")?.ceremonyTier;
50
+ if (tier === "LIGHT") {
51
+ test("skipped — spec checks not required for LIGHT tier", () => {});
52
+ return;
53
+ }
54
+
55
+ test("governing spec exists and is readable", () => {
56
+ const wf = loadState() as any;
57
+ expect(wf.sourceSpecs?.[0]?.path).toBeDefined();
58
+ const specPath = wf.sourceSpecs[0].path.startsWith('/') ? wf.sourceSpecs[0].path : join(wf.projectRoot || '.', wf.sourceSpecs[0].path);
59
+ expect(existsSync(specPath)).toBe(true);
60
+ });
61
+
62
+ test("spec has extractable rules", () => {
63
+ const wf = loadState() as any;
64
+ const spec = loadSpec(wf.sourceSpecs[0].path, wf.projectRoot);
65
+ const rules = extractSpecRules(spec);
66
+ expect(rules.length).toBeGreaterThan(0);
67
+ });
68
+ });
69
+
70
+ describe("E2E: hardcoded values violate spec", () => {
71
+ if (STANDALONE || !existsSync(SF)) {
72
+ test("skipped — no active workflow (standalone run)", () => {});
73
+ return;
74
+ }
75
+ const tier = sf("sizing")?.ceremonyTier;
76
+ if (tier === "LIGHT") {
77
+ test("skipped — spec checks not required for LIGHT tier", () => {});
78
+ return;
79
+ }
80
+
81
+ test("no hardcoded paths in AC statements", () => {
82
+ const wf = loadState() as any;
83
+ const spec = loadSpec(wf.sourceSpecs[0].path, wf.projectRoot);
84
+
85
+ if (!spec.match(/never hardcod|read from config|configurable/i)) return;
86
+
87
+ const hardcodedPath = /\/usr\/|\/opt\/|\/home\/|\/tmp\/[a-z]/i;
88
+ const hardcodedPort = /\bport\s+\d{4}\b/i;
89
+ const hits = wf.acs.filter((ac: any) =>
90
+ hardcodedPath.test(ac.statement) || hardcodedPort.test(ac.statement)
91
+ );
92
+
93
+ expect(hits.map((ac: any) => `${ac.id}: ${ac.statement}`)).toEqual([]);
94
+ });
95
+
96
+ test("no hardcoded numeric thresholds in AC statements", () => {
97
+ const wf = loadState() as any;
98
+ const spec = loadSpec(wf.sourceSpecs[0].path, wf.projectRoot);
99
+
100
+ if (!spec.match(/configurable|environment variable|config/i)) return;
101
+
102
+ const hardcodedNum = /\bhardcoded to \d+\.?\d*\b/i;
103
+ const hits = wf.acs.filter((ac: any) => hardcodedNum.test(ac.statement));
104
+
105
+ expect(hits.map((ac: any) => `${ac.id}: ${ac.statement}`)).toEqual([]);
106
+ });
107
+ });
108
+
109
+ describe("E2E: behavioral language violates spec", () => {
110
+ if (STANDALONE || !existsSync(SF)) {
111
+ test("skipped — no active workflow (standalone run)", () => {});
112
+ return;
113
+ }
114
+ test("no 'DA should' or behavioral delegation in ACs", () => {
115
+ const wf = loadState() as any;
116
+ if (!wf) return;
117
+ const behavioral = /\b(DA should|remember to|make sure|don't forget)\b/i;
118
+ const hits = wf.acs.filter((ac: any) => behavioral.test(ac.statement));
119
+
120
+ expect(hits.map((ac: any) => `${ac.id}: ${ac.statement}`)).toEqual([]);
121
+ });
122
+ });
123
+
124
+ describe("E2E: spec-prohibited patterns in ACs", () => {
125
+ if (STANDALONE || !existsSync(SF)) {
126
+ test("skipped — no active workflow (standalone run)", () => {});
127
+ return;
128
+ }
129
+ const tier = sf("sizing")?.ceremonyTier;
130
+ if (tier === "LIGHT") {
131
+ test("skipped — spec checks not required for LIGHT tier", () => {});
132
+ return;
133
+ }
134
+
135
+ test("no setTimeout when spec says use backoff", () => {
136
+ const wf = loadState() as any;
137
+ const spec = loadSpec(wf.sourceSpecs[0].path, wf.projectRoot);
138
+
139
+ if (!spec.match(/not use setTimeout|exponential backoff/i)) return;
140
+
141
+ const hits = wf.acs.filter((ac: any) =>
142
+ /setTimeout/i.test(ac.statement) || /setTimeout/i.test(ac.evidenceMethod?.command || "")
143
+ );
144
+
145
+ expect(hits.map((ac: any) => `${ac.id}: ${ac.statement}`)).toEqual([]);
146
+ });
147
+
148
+ test("no stdout/console.log when spec says use logger", () => {
149
+ const wf = loadState() as any;
150
+ const spec = loadSpec(wf.sourceSpecs[0].path, wf.projectRoot);
151
+
152
+ if (!spec.match(/not write directly to stdout|use the logger/i)) return;
153
+
154
+ const hits = wf.acs.filter((ac: any) =>
155
+ /console\.log|stdout|directly/i.test(ac.statement)
156
+ );
157
+
158
+ expect(hits.map((ac: any) => `${ac.id}: ${ac.statement}`)).toEqual([]);
159
+ });
160
+
161
+ test("no string error handling when spec says use typed errors", () => {
162
+ const wf = loadState() as any;
163
+ const spec = loadSpec(wf.sourceSpecs[0].path, wf.projectRoot);
164
+
165
+ if (!spec.match(/structured error|typed error|never raw string/i)) return;
166
+
167
+ const hits = wf.acs.filter((ac: any) =>
168
+ /string message|raw.*throw|catch.*string/i.test(ac.statement)
169
+ );
170
+
171
+ expect(hits.map((ac: any) => `${ac.id}: ${ac.statement}`)).toEqual([]);
172
+ });
173
+ });
174
+
175
+ describe("E2E: Marcus brief cites governing spec", () => {
176
+ if (STANDALONE || !existsSync(SF)) {
177
+ test("skipped — no active workflow (standalone run)", () => {});
178
+ return;
179
+ }
180
+ const tier = sf("sizing")?.ceremonyTier;
181
+ if (tier === "LIGHT") {
182
+ test("skipped — spec checks not required for LIGHT tier", () => {});
183
+ return;
184
+ }
185
+
186
+ test("brief references spec constraints", () => {
187
+ const wf = loadState() as any;
188
+ if (!wf.sourceSpecs?.[0]?.path || !wf.agents?.marcus?.brief) return;
189
+
190
+ const spec = loadSpec(wf.sourceSpecs[0].path, wf.projectRoot);
191
+ const rules = extractSpecRules(spec);
192
+ const brief = wf.agents.marcus.brief.toLowerCase();
193
+
194
+ const cited = rules.filter(r =>
195
+ brief.includes(r.slice(0, 25).toLowerCase())
196
+ );
197
+
198
+ expect(cited.length).toBeGreaterThanOrEqual(
199
+ Math.min(3, rules.length)
200
+ );
201
+ });
202
+ });
203
+
204
+ // ═══ ADR-008 MIGRATION COMPLETENESS ════════════════════════════════════
205
+
206
+ describe("E2E: ADR-008 migration", () => {
207
+ test("ADR-008 'What to delete' files are actually deleted", () => {
208
+ const adrPath = join(process.env.HOME || "", ".claude", "PAI", "ADR", "ADR-008-harness-v3-bun-tests-migration.md");
209
+ if (!existsSync(adrPath)) return;
210
+ const adr = readFileSync(adrPath, "utf-8");
211
+
212
+ const deletionTargets = [
213
+ "skills/ship/gate-runner.sh",
214
+ "skills/ship/gate-checks.sh",
215
+ ];
216
+
217
+ const existing = deletionTargets.filter(f =>
218
+ existsSync(join(process.env.HOME || "", ".claude", f))
219
+ );
220
+ expect(existing).toEqual([]);
221
+ });
222
+
223
+ test("stop-the-bleeding: no .sh files in skills/ship/", () => {
224
+ const { execSync } = require("child_process");
225
+ const shipDir = join(process.env.HOME || "", ".claude", "skills", "ship");
226
+ try {
227
+ const result = execSync(`find "${shipDir}" -name '*.sh'`, { encoding: "utf-8", timeout: 5000 }).trim();
228
+ const files = result ? result.split("\n").filter(Boolean) : [];
229
+ expect(files).toEqual([]);
230
+ } catch {
231
+ // find command failed — skip
232
+ }
233
+ });
234
+ });
@@ -0,0 +1,250 @@
1
+ import { test, expect, describe } from "bun:test";
2
+ import { classifyFailures, type FailureCategory, type Classification } from "./error-classifier";
3
+
4
+ function fail(check: string, detail = ""): { check: string; result: string; detail: string } {
5
+ return { check, result: "FAIL", detail };
6
+ }
7
+
8
+ describe("error-classifier", () => {
9
+ describe("STATE category", () => {
10
+ test("workflow-state.json validates", () => {
11
+ const c = classifyFailures([fail("workflow-state.json validates", "schema error")]);
12
+ expect(c.category).toBe("STATE");
13
+ expect(c.retryable).toBe(true);
14
+ expect(c.regressionTarget).toBeUndefined();
15
+ });
16
+
17
+ test("sourceSpecs-required", () => {
18
+ const c = classifyFailures([fail("sourceSpecs-required")]);
19
+ expect(c.category).toBe("STATE");
20
+ });
21
+
22
+ test("discovery-evidence", () => {
23
+ const c = classifyFailures([fail("discovery-evidence")]);
24
+ expect(c.category).toBe("STATE");
25
+ });
26
+
27
+ test("spec-elements-in-acs", () => {
28
+ const c = classifyFailures([fail("spec-elements-in-acs")]);
29
+ expect(c.category).toBe("STATE");
30
+ });
31
+
32
+ test("no 'DA should'", () => {
33
+ const c = classifyFailures([fail("no 'DA should'")]);
34
+ expect(c.category).toBe("STATE");
35
+ });
36
+
37
+ test("checks containing threshold", () => {
38
+ const c = classifyFailures([fail("weak-threshold-check")]);
39
+ expect(c.category).toBe("STATE");
40
+ });
41
+
42
+ test("checks containing statement", () => {
43
+ const c = classifyFailures([fail("statement-length-check")]);
44
+ expect(c.category).toBe("STATE");
45
+ });
46
+
47
+ test("checks containing schema", () => {
48
+ const c = classifyFailures([fail("schema-version-check")]);
49
+ expect(c.category).toBe("STATE");
50
+ });
51
+ });
52
+
53
+ describe("CODE category", () => {
54
+ test("tests-pass", () => {
55
+ const c = classifyFailures([fail("tests-pass", "3 tests failed")]);
56
+ expect(c.category).toBe("CODE");
57
+ expect(c.retryable).toBe(true);
58
+ expect(c.regressionTarget).toBe("BUILD");
59
+ });
60
+
61
+ test("tsc-pass", () => {
62
+ const c = classifyFailures([fail("tsc-pass", "type errors")]);
63
+ expect(c.category).toBe("CODE");
64
+ expect(c.regressionTarget).toBe("BUILD");
65
+ });
66
+
67
+ test("code-committed", () => {
68
+ const c = classifyFailures([fail("code-committed")]);
69
+ expect(c.category).toBe("CODE");
70
+ expect(c.regressionTarget).toBe("BUILD");
71
+ });
72
+
73
+ test("brief-ac-alignment", () => {
74
+ const c = classifyFailures([fail("brief-ac-alignment")]);
75
+ expect(c.category).toBe("CODE");
76
+ expect(c.regressionTarget).toBe("BUILD");
77
+ });
78
+ });
79
+
80
+ describe("DISCOVERY category", () => {
81
+ test("ac-issue-alignment", () => {
82
+ const c = classifyFailures([fail("ac-issue-alignment")]);
83
+ expect(c.category).toBe("DISCOVERY");
84
+ expect(c.retryable).toBe(true);
85
+ expect(c.regressionTarget).toBe("DISCOVERY");
86
+ });
87
+
88
+ test("issue-goal-captured", () => {
89
+ const c = classifyFailures([fail("issue-goal-captured")]);
90
+ expect(c.category).toBe("DISCOVERY");
91
+ expect(c.regressionTarget).toBe("DISCOVERY");
92
+ });
93
+
94
+ test("sizing-declared", () => {
95
+ const c = classifyFailures([fail("sizing-declared")]);
96
+ expect(c.category).toBe("DISCOVERY");
97
+ expect(c.regressionTarget).toBe("DISCOVERY");
98
+ });
99
+ });
100
+
101
+ describe("ENVIRONMENT category", () => {
102
+ test("container-has-fix", () => {
103
+ const c = classifyFailures([fail("container-has-fix")]);
104
+ expect(c.category).toBe("ENVIRONMENT");
105
+ expect(c.retryable).toBe(true);
106
+ expect(c.regressionTarget).toBeUndefined();
107
+ });
108
+
109
+ test("local-api-validated", () => {
110
+ const c = classifyFailures([fail("local-api-validated")]);
111
+ expect(c.category).toBe("ENVIRONMENT");
112
+ });
113
+
114
+ test("local-ui-validated", () => {
115
+ const c = classifyFailures([fail("local-ui-validated")]);
116
+ expect(c.category).toBe("ENVIRONMENT");
117
+ });
118
+
119
+ test("prod-rebuild-pass", () => {
120
+ const c = classifyFailures([fail("prod-rebuild-pass")]);
121
+ expect(c.category).toBe("ENVIRONMENT");
122
+ });
123
+
124
+ test("prod-smoke-pass", () => {
125
+ const c = classifyFailures([fail("prod-smoke-pass")]);
126
+ expect(c.category).toBe("ENVIRONMENT");
127
+ });
128
+
129
+ test("prod-quinn-pass", () => {
130
+ const c = classifyFailures([fail("prod-quinn-pass")]);
131
+ expect(c.category).toBe("ENVIRONMENT");
132
+ });
133
+
134
+ test("code-pushed", () => {
135
+ const c = classifyFailures([fail("code-pushed")]);
136
+ expect(c.category).toBe("ENVIRONMENT");
137
+ });
138
+
139
+ test("branch-merged", () => {
140
+ const c = classifyFailures([fail("branch-merged")]);
141
+ expect(c.category).toBe("ENVIRONMENT");
142
+ });
143
+ });
144
+
145
+ describe("NON_RETRYABLE category", () => {
146
+ test("failure containing auth", () => {
147
+ const c = classifyFailures([fail("github-auth-check", "auth token expired")]);
148
+ expect(c.category).toBe("NON_RETRYABLE");
149
+ expect(c.retryable).toBe(false);
150
+ expect(c.regressionTarget).toBeUndefined();
151
+ });
152
+
153
+ test("failure containing credential", () => {
154
+ const c = classifyFailures([fail("credential-check", "no credentials")]);
155
+ expect(c.category).toBe("NON_RETRYABLE");
156
+ });
157
+
158
+ test("failure containing permission", () => {
159
+ const c = classifyFailures([fail("permission-denied", "insufficient")]);
160
+ expect(c.category).toBe("NON_RETRYABLE");
161
+ });
162
+
163
+ test("failure containing timeout", () => {
164
+ const c = classifyFailures([fail("api-timeout", "30s exceeded")]);
165
+ expect(c.category).toBe("NON_RETRYABLE");
166
+ });
167
+
168
+ test("unknown check defaults to CODE (safe default — attempt fix before giving up)", () => {
169
+ const c = classifyFailures([fail("totally-unknown-check-xyz")]);
170
+ expect(c.category).toBe("CODE");
171
+ expect(c.retryable).toBe(true);
172
+ expect(c.regressionTarget).toBe("BUILD");
173
+ });
174
+ });
175
+
176
+ describe("priority ordering", () => {
177
+ test("NON_RETRYABLE beats everything", () => {
178
+ const c = classifyFailures([
179
+ fail("tests-pass"), // CODE
180
+ fail("api-timeout"), // NON_RETRYABLE
181
+ fail("container-has-fix"), // ENVIRONMENT
182
+ ]);
183
+ expect(c.category).toBe("NON_RETRYABLE");
184
+ expect(c.retryable).toBe(false);
185
+ expect(c.failures).toHaveLength(3);
186
+ });
187
+
188
+ test("DISCOVERY beats CODE", () => {
189
+ const c = classifyFailures([
190
+ fail("tests-pass"), // CODE
191
+ fail("ac-issue-alignment"), // DISCOVERY
192
+ ]);
193
+ expect(c.category).toBe("DISCOVERY");
194
+ expect(c.regressionTarget).toBe("DISCOVERY");
195
+ });
196
+
197
+ test("CODE beats STATE", () => {
198
+ const c = classifyFailures([
199
+ fail("workflow-state.json validates"), // STATE
200
+ fail("tsc-pass"), // CODE
201
+ ]);
202
+ expect(c.category).toBe("CODE");
203
+ expect(c.regressionTarget).toBe("BUILD");
204
+ });
205
+
206
+ test("STATE beats ENVIRONMENT", () => {
207
+ const c = classifyFailures([
208
+ fail("container-has-fix"), // ENVIRONMENT
209
+ fail("sourceSpecs-required"), // STATE
210
+ ]);
211
+ expect(c.category).toBe("STATE");
212
+ });
213
+
214
+ test("all categories mixed picks NON_RETRYABLE", () => {
215
+ const c = classifyFailures([
216
+ fail("container-has-fix"), // ENVIRONMENT
217
+ fail("sourceSpecs-required"), // STATE
218
+ fail("tests-pass"), // CODE
219
+ fail("ac-issue-alignment"), // DISCOVERY
220
+ fail("permission-check"), // NON_RETRYABLE
221
+ ]);
222
+ expect(c.category).toBe("NON_RETRYABLE");
223
+ expect(c.failures).toHaveLength(5);
224
+ });
225
+ });
226
+
227
+ describe("edge cases", () => {
228
+ test("empty failures array", () => {
229
+ const c = classifyFailures([]);
230
+ expect(c.category).toBe("NON_RETRYABLE");
231
+ expect(c.retryable).toBe(false);
232
+ expect(c.failures).toHaveLength(0);
233
+ });
234
+
235
+ test("single failure preserves detail", () => {
236
+ const c = classifyFailures([fail("tests-pass", "AssertionError in foo.test.ts")]);
237
+ expect(c.failures[0].detail).toBe("AssertionError in foo.test.ts");
238
+ });
239
+
240
+ test("NON_RETRYABLE keyword in detail field does not trigger", () => {
241
+ const c = classifyFailures([fail("tests-pass", "auth module test failed")]);
242
+ expect(c.category).toBe("CODE");
243
+ });
244
+
245
+ test("case-insensitive NON_RETRYABLE check matching", () => {
246
+ const c = classifyFailures([fail("AUTH-TOKEN-EXPIRED")]);
247
+ expect(c.category).toBe("NON_RETRYABLE");
248
+ });
249
+ });
250
+ });
@@ -0,0 +1,99 @@
1
+ export type FailureCategory = "STATE" | "CODE" | "DISCOVERY" | "ENVIRONMENT" | "NON_RETRYABLE";
2
+
3
+ // ── Gate contract interfaces (SC-373) ─────────────────────────────────────
4
+
5
+ export interface ClassifyInput {
6
+ failures: Array<{ check: string; result: string; detail: string }>;
7
+ }
8
+
9
+ export type ClassifyResult = Classification;
10
+
11
+ export interface Classification {
12
+ category: FailureCategory;
13
+ regressionTarget?: "BUILD" | "DISCOVERY";
14
+ retryable: boolean;
15
+ failures: Array<{ check: string; result: string; detail: string }>;
16
+ }
17
+
18
+ const PRIORITY: FailureCategory[] = ["NON_RETRYABLE", "DISCOVERY", "CODE", "STATE", "ENVIRONMENT"];
19
+
20
+ const STATE_CHECKS = new Set([
21
+ "workflow-state.json validates",
22
+ "sourceSpecs-required",
23
+ "discovery-evidence",
24
+ "spec-elements-in-acs",
25
+ "no 'DA should'",
26
+ ]);
27
+
28
+ const CODE_CHECKS = new Set([
29
+ "tests-pass",
30
+ "tsc-pass",
31
+ "code-committed",
32
+ "brief-ac-alignment",
33
+ ]);
34
+
35
+ const DISCOVERY_CHECKS = new Set([
36
+ "ac-issue-alignment",
37
+ "issue-goal-captured",
38
+ "sizing-declared",
39
+ ]);
40
+
41
+ const ENVIRONMENT_CHECKS = new Set([
42
+ "container-has-fix",
43
+ "local-api-validated",
44
+ "local-ui-validated",
45
+ "prod-rebuild-pass",
46
+ "prod-smoke-pass",
47
+ "prod-quinn-pass",
48
+ "code-pushed",
49
+ "branch-merged",
50
+ ]);
51
+
52
+ const STATE_KEYWORDS = ["threshold", "statement", "schema"];
53
+ const CODE_KEYWORDS = ["ac-verdict", "tests-pass", "tsc-pass", "code-committed"];
54
+ const NON_RETRYABLE_KEYWORDS = ["auth", "credential", "permission", "timeout"];
55
+
56
+ function categorizeCheck(check: string): FailureCategory {
57
+ const lower = check.toLowerCase();
58
+
59
+ if (NON_RETRYABLE_KEYWORDS.some((kw) => lower.includes(kw))) return "NON_RETRYABLE";
60
+ if (STATE_CHECKS.has(check)) return "STATE";
61
+ if (CODE_CHECKS.has(check)) return "CODE";
62
+ if (DISCOVERY_CHECKS.has(check)) return "DISCOVERY";
63
+ if (ENVIRONMENT_CHECKS.has(check)) return "ENVIRONMENT";
64
+ if (STATE_KEYWORDS.some((kw) => lower.includes(kw))) return "STATE";
65
+ if (CODE_KEYWORDS.some((kw) => lower.includes(kw))) return "CODE";
66
+
67
+ return "CODE";
68
+ }
69
+
70
+ const CATEGORY_META: Record<FailureCategory, { retryable: boolean; regressionTarget?: "BUILD" | "DISCOVERY" }> = {
71
+ STATE: { retryable: true },
72
+ CODE: { retryable: true, regressionTarget: "BUILD" },
73
+ DISCOVERY: { retryable: true, regressionTarget: "DISCOVERY" },
74
+ ENVIRONMENT: { retryable: true },
75
+ NON_RETRYABLE: { retryable: false },
76
+ };
77
+
78
+ export function classifyFailures(
79
+ failures: Array<{ check: string; result: string; detail: string }>,
80
+ ): Classification {
81
+ if (failures.length === 0) {
82
+ return { category: "NON_RETRYABLE", retryable: false, failures: [] };
83
+ }
84
+
85
+ const categories = new Set<FailureCategory>();
86
+ for (const f of failures) {
87
+ categories.add(categorizeCheck(f.check));
88
+ }
89
+
90
+ const winner = PRIORITY.find((p) => categories.has(p)) ?? "NON_RETRYABLE";
91
+ const meta = CATEGORY_META[winner];
92
+
93
+ return {
94
+ category: winner,
95
+ retryable: meta.retryable,
96
+ regressionTarget: meta.regressionTarget,
97
+ failures,
98
+ };
99
+ }