Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
claude/adoring-hopper-5x74bqclaude/affectionate-feynman-ykrf1hclaude/architecture-audit-design-wxprenclaude/build-status-update-3MXsfclaude/charming-meitner-mllb5rclaude/compare-gate-gluecron-s4mFQclaude/confident-faraday-tikcwbclaude/continue-work-XMTlIclaude/crontech-gluecron-deploy-7MIECclaude/crontech-platform-setup-SeKfwclaude/design-2026claude/ecstatic-ptolemy-jMdigclaude/enhance-github-integration-QNHdGclaude/fix-aa-loop-issue-PonMQclaude/fix-actions-and-processclaude/fix-desktop-errors-XqoW8claude/fix-red-workflowsclaude/fix-website-access-6FKJNclaude/gatetest-integration-hardeningclaude/github-audit-improvements-bDFr9claude/gluecron-launch-status-FoMRlclaude/hopeful-lamport-olfCTclaude/issue-to-pr-and-protectionsclaude/jolly-heisenberg-2sg1Qclaude/launch-preparation-QmTb6claude/new-session-xk1l7claude/plan-platform-architecture-kkN4yclaude/platform-analysis-roadmap-1nUGLclaude/platform-launch-assessment-8dWV8claude/polish-platform-release-AeDrUclaude/resume-previous-work-KzyLwclaude/review-crontech-handoff-qYEVqclaude/review-project-completeness-lHhS2claude/review-readme-docs-ulqPKclaude/serene-edison-rj87weclaude/setup-multi-repo-dev-BCwNQclaude/ship-fixes-and-tests-Jvz1cclaude/site-audit-competitive-pctlwgclaude/site-migration-vercel-XstpKclaude/standalone-product-repos-XHFTDcopilot/feat-smart-empty-states-keyboard-first-enhancementcopilot/feat-smart-morning-digest-review-context-restorecopilot/fix-and-process-workflowscopilot/update-ai-powered-code-reviewfeat/debt-mapfeat/push-policy-codeowners-hardeningfeat/smart-digest-contextfeat/stage-impactfeat/t1-secret-migrationfeat/u-polishfeat/w-self-hostfeat/w2-claude-configfix/agent-journey-orphan-sweepgatetest/auto-fix-1776586424172gatetest/auto-fix-1776586534814gatetest/auto-fix-1776590685143gatetest/auto-fix-1776590808199mainops/redeploy-retriggerstyle/dxt-cta-themeworktree-agent-a3377aad30d55da26worktree-agent-a7ef607b7ee1d6c74
repair-flywheel-wiring.test.ts11.9 KB · 343 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/**
 * Repair-flywheel ↔ ci-autofix wiring tests (BUILD_BIBLE §7 finding 1).
 *
 * Drives `resolveAutofix` / `recordAutofixOutcome` through their
 * dependency-injection seam (`CiAutofixDeps`) so no DB, git, or Anthropic
 * call is touched — same pattern as the auto-merge fast-lane suite.
 *
 * Covers the closed-loop contract:
 *   - Tier-0 cache hit serves the cached patch WITHOUT an AI call and
 *     records a 'cached'-tier pending row with cache lineage + the patch
 *   - cache miss / low success rate / missing patch fall through to AI
 *   - outcomes settle via updateOutcome on apply success AND failure
 *   - flywheel errors (lookup, record, settle) never break the autofix
 *     path — everything fails open to the AI tier
 *   - isAiAvailable() gating: no key + cache miss → no fix, AI never called;
 *     no key + cache hit → cached fix still served (graceful degradation)
 */

import { describe, it, expect } from "bun:test";
import { readFileSync } from "node:fs";
import { join } from "node:path";

import {
  resolveAutofix,
  recordAutofixOutcome,
  extractFlywheelEntryId,
  CACHE_MIN_SUCCESS_RATE,
  FLYWHEEL_MARKER_PREFIX,
  type AutofixPlan,
  type CiAutofixDeps,
  type ClaudeAutofixResponse,
} from "../lib/ci-autofix";
import type { CachedRepair, RecordRepairInput } from "../lib/repair-flywheel";

// ---------------------------------------------------------------------------
// Fixtures
// ---------------------------------------------------------------------------

const CACHED_PATCH = `--- a/src/foo.ts
+++ b/src/foo.ts
@@ -1,3 +1,3 @@
-const x: string = 42;
+const x = 42;
`;

const AI_FIX: ClaudeAutofixResponse = {
  patch: "--- a/src/bar.ts\n+++ b/src/bar.ts\n@@ -1 +1 @@\n-old\n+new\n",
  explanation: "Fixes the type error in bar.ts.",
  confidence: "high",
  affectedFiles: ["src/bar.ts"],
};

const FAILURE_TEXT =
  "error TS2322: Type 'number' is not assignable to type 'string' at src/foo.ts:1:7";

const PATTERN_ID = "11111111-2222-3333-4444-555555555555";
const NEW_ENTRY_ID = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";

function makeCached(overrides: Partial<CachedRepair> = {}): CachedRepair {
  return {
    id: PATTERN_ID,
    patchSummary: "Drop the bogus string annotation on x.",
    patch: CACHED_PATCH,
    filesChanged: ["src/foo.ts"],
    commitSha: null,
    hitCount: 3,
    successRate: 1,
    classification: null,
    appliedCount: 4,
    ...overrides,
  };
}

interface Harness {
  deps: CiAutofixDeps;
  aiCalls: number;
  recorded: RecordRepairInput[];
  outcomes: Array<{ id: string; outcome: string }>;
  generateAiFix: () => Promise<ClaudeAutofixResponse | null>;
}

function makeHarness(opts: {
  cached?: CachedRepair | null;
  aiFix?: ClaudeAutofixResponse | null;
  aiAvailable?: boolean;
  // Throw-injection hooks (default false everywhere)
  lookupThrows?: boolean;
  recordThrows?: boolean;
  updateOutcomeThrows?: boolean;
} = {}): Harness {
  const recorded: RecordRepairInput[] = [];
  const outcomes: Array<{ id: string; outcome: string }> = [];

  const harness: Harness = {
    aiCalls: 0,
    recorded,
    outcomes,
    generateAiFix: async () => {
      harness.aiCalls += 1;
      return opts.aiFix === undefined ? AI_FIX : opts.aiFix;
    },
    deps: {
      findCachedRepair: async () => {
        if (opts.lookupThrows) throw new Error("flywheel db down");
        return opts.cached ?? null;
      },
      recordRepair: async (input) => {
        if (opts.recordThrows) throw new Error("insert failed");
        recorded.push(input);
        return NEW_ENTRY_ID;
      },
      updateOutcome: async (id, outcome) => {
        if (opts.updateOutcomeThrows) throw new Error("update failed");
        outcomes.push({ id, outcome });
      },
      aiAvailable: () => opts.aiAvailable ?? true,
    },
  };
  return harness;
}

function run(h: Harness): Promise<AutofixPlan | null> {
  return resolveAutofix({
    repositoryId: "repo-1",
    failureText: FAILURE_TEXT,
    generateAiFix: h.generateAiFix,
    deps: h.deps,
  });
}

// ---------------------------------------------------------------------------
// Tier 0: cache hit
// ---------------------------------------------------------------------------

describe("resolveAutofix — cache hit (Tier 0)", () => {
  it("serves the cached patch without calling the AI", async () => {
    const h = makeHarness({ cached: makeCached() });
    const plan = await run(h);

    expect(plan).not.toBeNull();
    expect(plan!.source).toBe("cache");
    expect(plan!.fix.patch).toBe(CACHED_PATCH);
    expect(plan!.cachedPatternId).toBe(PATTERN_ID);
    expect(h.aiCalls).toBe(0);
  });

  it("records a 'cached'-tier pending row with cache lineage + the patch", async () => {
    const h = makeHarness({ cached: makeCached() });
    const plan = await run(h);

    expect(h.recorded.length).toBe(1);
    expect(h.recorded[0]!.tier).toBe("cached");
    expect(h.recorded[0]!.parentPatternId).toBe(PATTERN_ID);
    expect(h.recorded[0]!.repositoryId).toBe("repo-1");
    expect(h.recorded[0]!.failureText).toBe(FAILURE_TEXT);
    // Carried forward so the new row is itself replayable once it settles
    expect(h.recorded[0]!.patch).toBe(CACHED_PATCH);
    // outcome defaults to 'pending' inside recordRepair — must not be forced
    expect(h.recorded[0]!.outcome).toBeUndefined();
    expect(plan!.flywheelEntryId).toBe(NEW_ENTRY_ID);
  });

  it("maps success rate to confidence (>=0.9 high, else medium)", async () => {
    const high = await run(makeHarness({ cached: makeCached({ successRate: 0.95 }) }));
    expect(high!.fix.confidence).toBe("high");

    const med = await run(makeHarness({ cached: makeCached({ successRate: 0.6 }) }));
    expect(med!.fix.confidence).toBe("medium");
  });

  it("works even when the AI is unavailable (graceful degradation)", async () => {
    const h = makeHarness({ cached: makeCached(), aiAvailable: false });
    const plan = await run(h);

    expect(plan).not.toBeNull();
    expect(plan!.source).toBe("cache");
    expect(h.aiCalls).toBe(0);
  });
});

// ---------------------------------------------------------------------------
// Cache miss / unusable hit → fall through to the AI tier
// ---------------------------------------------------------------------------

describe("resolveAutofix — fall-through to AI (Tier 2)", () => {
  it("cache miss falls through to the AI and records an 'ai-sonnet' row", async () => {
    const h = makeHarness({ cached: null });
    const plan = await run(h);

    expect(h.aiCalls).toBe(1);
    expect(plan!.source).toBe("ai");
    expect(plan!.fix).toEqual(AI_FIX);
    expect(plan!.cachedPatternId).toBeNull();
    expect(h.recorded.length).toBe(1);
    expect(h.recorded[0]!.tier).toBe("ai-sonnet");
    // The AI patch is stored so the entry is replayable once it succeeds
    expect(h.recorded[0]!.patch).toBe(AI_FIX.patch);
    expect(plan!.flywheelEntryId).toBe(NEW_ENTRY_ID);
  });

  it("hit below CACHE_MIN_SUCCESS_RATE is not replayed", async () => {
    const h = makeHarness({
      cached: makeCached({ successRate: CACHE_MIN_SUCCESS_RATE - 0.01 }),
    });
    const plan = await run(h);

    expect(h.aiCalls).toBe(1);
    expect(plan!.source).toBe("ai");
  });

  it("hit with no stored patch (pre-0105 row) is not replayed", async () => {
    const h = makeHarness({ cached: makeCached({ patch: null }) });
    const plan = await run(h);

    expect(h.aiCalls).toBe(1);
    expect(plan!.source).toBe("ai");
  });

  it("returns null when the AI declines (no patch)", async () => {
    const h = makeHarness({ cached: null, aiFix: null });
    expect(await run(h)).toBeNull();
    expect(h.recorded.length).toBe(0);
  });

  it("returns null on a low-confidence AI fix without recording it", async () => {
    const h = makeHarness({
      cached: null,
      aiFix: { ...AI_FIX, confidence: "low" },
    });
    expect(await run(h)).toBeNull();
    expect(h.recorded.length).toBe(0);
  });

  it("returns null (and never calls the AI) when no key + cache miss", async () => {
    const h = makeHarness({ cached: null, aiAvailable: false });
    expect(await run(h)).toBeNull();
    expect(h.aiCalls).toBe(0);
  });
});

// ---------------------------------------------------------------------------
// Fail-open: flywheel errors never break the autofix path
// ---------------------------------------------------------------------------

describe("resolveAutofix — flywheel errors fail open", () => {
  it("a throwing findCachedRepair degrades to the AI tier", async () => {
    const h = makeHarness({ lookupThrows: true });
    const plan = await run(h);

    expect(plan!.source).toBe("ai");
    expect(h.aiCalls).toBe(1);
  });

  it("a throwing recordRepair still serves the cached fix (entry id null)", async () => {
    const h = makeHarness({ cached: makeCached(), recordThrows: true });
    const plan = await run(h);

    expect(plan!.source).toBe("cache");
    expect(plan!.fix.patch).toBe(CACHED_PATCH);
    expect(plan!.flywheelEntryId).toBeNull();
    expect(h.aiCalls).toBe(0);
  });

  it("a throwing recordRepair still serves the AI fix (entry id null)", async () => {
    const h = makeHarness({ cached: null, recordThrows: true });
    const plan = await run(h);

    expect(plan!.source).toBe("ai");
    expect(plan!.fix).toEqual(AI_FIX);
    expect(plan!.flywheelEntryId).toBeNull();
  });
});

// ---------------------------------------------------------------------------
// Outcome settling (recordAutofixOutcome ← applyAutofix)
// ---------------------------------------------------------------------------

describe("recordAutofixOutcome", () => {
  const bodyWithMarker = `<!-- gluecron:ci-autofix:v1 -->\n${FLYWHEEL_MARKER_PREFIX}${NEW_ENTRY_ID} -->\n\n## 🔧 AI Auto-Fix`;

  it("settles 'success' for the entry referenced by the comment", async () => {
    const h = makeHarness();
    await recordAutofixOutcome(bodyWithMarker, "success", h.deps);
    expect(h.outcomes).toEqual([{ id: NEW_ENTRY_ID, outcome: "success" }]);
  });

  it("settles 'failed' when the apply blows up", async () => {
    const h = makeHarness();
    await recordAutofixOutcome(bodyWithMarker, "failed", h.deps);
    expect(h.outcomes).toEqual([{ id: NEW_ENTRY_ID, outcome: "failed" }]);
  });

  it("is a no-op when the comment carries no flywheel marker", async () => {
    const h = makeHarness();
    await recordAutofixOutcome("<!-- gluecron:ci-autofix:v1 -->", "success", h.deps);
    expect(h.outcomes.length).toBe(0);
  });

  it("never throws even when updateOutcome throws", async () => {
    const h = makeHarness({ updateOutcomeThrows: true });
    await expect(
      recordAutofixOutcome(bodyWithMarker, "success", h.deps)
    ).resolves.toBeUndefined();
  });
});

describe("extractFlywheelEntryId", () => {
  it("round-trips an id through the comment marker", () => {
    const body = `hello\n${FLYWHEEL_MARKER_PREFIX}${PATTERN_ID} -->\nworld`;
    expect(extractFlywheelEntryId(body)).toBe(PATTERN_ID);
  });

  it("returns null when absent", () => {
    expect(extractFlywheelEntryId("no markers here")).toBeNull();
  });
});

// ---------------------------------------------------------------------------
// Wiring regression guards — the seam must actually be plumbed into the
// live paths (same readFileSync technique as the fast-lane suite).
// ---------------------------------------------------------------------------

describe("ci-autofix source wiring", () => {
  const src = readFileSync(
    join(import.meta.dir, "../lib/ci-autofix.ts"),
    "utf8"
  );

  it("_runAutofix routes through resolveAutofix (cache before AI)", () => {
    expect(src).toContain("const plan = await resolveAutofix({");
  });

  it("applyAutofix settles both success and failed outcomes", () => {
    expect(src).toContain('recordAutofixOutcome(comment.body, "success"');
    expect(src).toContain('recordAutofixOutcome(comment.body, "failed"');
  });

  it("imports the flywheel cache + outcome functions", () => {
    expect(src).toContain("findCachedRepair");
    expect(src).toContain("updateOutcome");
    expect(src).toContain('from "./repair-flywheel"');
  });
});