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
ai-ci-healer.test.ts15.7 KB · 511 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/**
 * Tests for src/lib/ai-ci-healer.ts.
 *
 * Two layers:
 *   1. Pure helpers — buildCiHealPrompt + fixesToFindings + small private
 *      helpers exposed via __test. Always run.
 *   2. End-to-end with a fake Claude client + a real DB row + bare repo.
 *      Gated on DATABASE_URL via the HAS_DB skipIf pattern used across
 *      the suite.
 *
 * The Anthropic client is faked via the public `client` option so we never
 * touch the network or require ANTHROPIC_API_KEY. We also DI the
 * patch-generator into `healOneRun` so each test pins its own outcome.
 */

import { describe, it, expect, beforeAll, afterAll } from "bun:test";
import { join } from "path";
import { mkdir, rm } from "fs/promises";
import { eq, and } from "drizzle-orm";
import {
  analyzeFailedWorkflowRun,
  buildCiHealPrompt,
  fixesToFindings,
  healOneRun,
  runCiHealerTick,
  __test,
} from "../lib/ai-ci-healer";
import { db } from "../db";
import {
  auditLog,
  pullRequests,
  repositories,
  users,
  workflowJobs,
  workflowRuns,
  workflows,
} from "../db/schema";
import {
  createOrUpdateFileOnBranch,
  initBareRepo,
} from "../git/repository";

const HAS_DB = Boolean(process.env.DATABASE_URL);

const TEST_REPOS = join(
  import.meta.dir,
  "../../.test-repos-ai-ci-healer-" + Date.now()
);

beforeAll(async () => {
  process.env.GIT_REPOS_PATH = TEST_REPOS;
  process.env.DATABASE_URL = process.env.DATABASE_URL || "";
  // Make sure autopilot doesn't short-circuit our tests via env.
  delete process.env.AUTOPILOT_DISABLED;
  await rm(TEST_REPOS, { recursive: true, force: true });
  await mkdir(TEST_REPOS, { recursive: true });
});

afterAll(async () => {
  await rm(TEST_REPOS, { recursive: true, force: true });
});

// ---------------------------------------------------------------------------
// Pure helpers
// ---------------------------------------------------------------------------

describe("buildCiHealPrompt", () => {
  it("embeds repo, sha, yaml, and failed job logs", () => {
    const prompt = buildCiHealPrompt({
      repoFullName: "alice/web",
      commitSha: "abcdef1234567890",
      workflowYaml: "name: ci\non: [push]\njobs:\n  build:\n    steps: []",
      failedJobs: [
        { name: "build", conclusion: "failure", logs: "TypeError: x is not a function" },
      ],
    });
    expect(prompt).toContain("alice/web");
    expect(prompt).toContain("abcdef123456");
    expect(prompt).toContain("name: ci");
    expect(prompt).toContain("TypeError: x is not a function");
    // Strict JSON schema cue
    expect(prompt).toContain('"rootCause"');
    expect(prompt).toContain('"suggestedFixes"');
  });

  it("handles the empty-jobs case gracefully", () => {
    const prompt = buildCiHealPrompt({
      repoFullName: "x/y",
      commitSha: "1234567",
      workflowYaml: "name: ci",
      failedJobs: [],
    });
    expect(prompt).toContain("(no failed-job logs available)");
  });
});

describe("fixesToFindings", () => {
  it("maps SuggestedFix[] onto GateTestFinding[] preserving path + severity", () => {
    const findings = fixesToFindings(
      [
        { path: "src/a.ts", description: "fix imports", severity: "high" },
        { path: "src/b.ts", description: "guard null", severity: "medium" },
      ],
      "deadbeef"
    );
    expect(findings.length).toBe(2);
    expect(findings[0].path).toBe("src/a.ts");
    expect(findings[0].severity).toBe("high");
    expect(findings[0].id).toContain("ci-heal-deadbeef");
    expect(findings[1].path).toBe("src/b.ts");
    expect(findings[1].severity).toBe("medium");
  });

  it("drops fixes with no path", () => {
    const findings = fixesToFindings(
      [
        { path: "", description: "noop" },
        { path: "src/c.ts", description: "ok" },
      ],
      "x"
    );
    expect(findings.length).toBe(1);
    expect(findings[0].path).toBe("src/c.ts");
  });

  it("defaults severity to high when omitted", () => {
    const findings = fixesToFindings(
      [{ path: "src/d.ts", description: "no sev" }],
      "x"
    );
    expect(findings[0].severity).toBe("high");
  });
});

describe("__test internals", () => {
  it("normaliseSeverity accepts the canonical levels case-insensitively", () => {
    expect(__test.normaliseSeverity("HIGH")).toBe("high");
    expect(__test.normaliseSeverity("Medium")).toBe("medium");
    expect(__test.normaliseSeverity("critical")).toBe("critical");
    expect(__test.normaliseSeverity("low")).toBe("low");
  });

  it("normaliseSeverity rejects garbage", () => {
    expect(__test.normaliseSeverity("bananas")).toBeUndefined();
    expect(__test.normaliseSeverity(undefined)).toBeUndefined();
    expect(__test.normaliseSeverity(null)).toBeUndefined();
    expect(__test.normaliseSeverity(42)).toBeUndefined();
  });

  it("truncate caps long strings + appends marker", () => {
    expect(__test.truncate("abc", 10)).toBe("abc");
    const long = "x".repeat(20);
    const t = __test.truncate(long, 5);
    expect(t.startsWith("xxxxx")).toBe(true);
    expect(t).toContain("(truncated)");
  });
});

// ---------------------------------------------------------------------------
// Skip when AUTOPILOT_DISABLED: tick must no-op cleanly
// ---------------------------------------------------------------------------

describe("runCiHealerTick — env gates", () => {
  it("no-ops when AUTOPILOT_DISABLED=1", async () => {
    const prev = process.env.AUTOPILOT_DISABLED;
    process.env.AUTOPILOT_DISABLED = "1";
    try {
      const summary = await runCiHealerTick({
        findCandidates: async () => {
          throw new Error("should not be called");
        },
      });
      expect(summary).toEqual({
        considered: 0,
        healed: 0,
        gaveUp: 0,
        skipped: 0,
      });
    } finally {
      if (prev === undefined) delete process.env.AUTOPILOT_DISABLED;
      else process.env.AUTOPILOT_DISABLED = prev;
    }
  });

  it("no-ops when ANTHROPIC_API_KEY is unset", async () => {
    const prev = process.env.ANTHROPIC_API_KEY;
    delete process.env.ANTHROPIC_API_KEY;
    try {
      const summary = await runCiHealerTick({
        findCandidates: async () => {
          throw new Error("should not be called");
        },
      });
      expect(summary.considered).toBe(0);
      expect(summary.healed).toBe(0);
    } finally {
      if (prev !== undefined) process.env.ANTHROPIC_API_KEY = prev;
    }
  });
});

// ---------------------------------------------------------------------------
// DB-backed end-to-end. Builds a real failed run + jobs + repo + workflow
// rows, then drives `healOneRun` with a fake Claude client.
// ---------------------------------------------------------------------------

function fakeClient(responseText: string) {
  return {
    messages: {
      create: async () => ({
        content: [{ type: "text" as const, text: responseText }],
      }),
    },
  } as any;
}

interface Fixture {
  repoId: string;
  repoName: string;
  ownerUsername: string;
  workflowId: string;
  runId: string;
  baseSha: string;
}

async function seedFailedRun(label: string): Promise<Fixture> {
  const username = `cihealer_${label}_${Date.now()}_${Math.random()
    .toString(36)
    .slice(2, 6)}`;
  const [u] = await db
    .insert(users)
    .values({
      username,
      email: `${username}@example.com`,
      passwordHash: "x",
    })
    .returning({ id: users.id });

  const repoName = `subject_${label}_${Date.now()}`;
  const [r] = await db
    .insert(repositories)
    .values({
      ownerId: u.id,
      name: repoName,
      diskPath: `/tmp/${username}/${repoName}`,
      defaultBranch: "main",
    })
    .returning({ id: repositories.id, name: repositories.name });

  await initBareRepo(username, repoName);
  const seeded = await createOrUpdateFileOnBranch({
    owner: username,
    name: repoName,
    branch: "main",
    filePath: "src/index.ts",
    bytes: new TextEncoder().encode(
      "export function broken(){ return undefined.length; }\n"
    ),
    message: "seed",
    authorName: "Seeder",
    authorEmail: "s@e.com",
  });
  if ("error" in seeded) throw new Error("seed failed: " + seeded.error);

  const [w] = await db
    .insert(workflows)
    .values({
      repositoryId: r.id,
      name: "ci",
      path: ".gluecron/workflows/ci.yml",
      yaml: "name: ci\non: [push]\njobs:\n  build:\n    steps:\n      - run: bun test",
      parsed: JSON.stringify({
        name: "ci",
        on: ["push"],
        jobs: { build: { steps: [{ run: "bun test" }] } },
      }),
    })
    .returning({ id: workflows.id });

  // Insert run as `failure` with createdAt safely in the past so the
  // candidate finder considers it (HEAL_MIN_AGE_MS = 60s).
  const oldCreatedAt = new Date(Date.now() - 5 * 60 * 1000);
  const [run] = await db
    .insert(workflowRuns)
    .values({
      workflowId: w.id,
      repositoryId: r.id,
      runNumber: 1,
      event: "push",
      ref: "refs/heads/main",
      commitSha: seeded.commitSha,
      status: "failure",
      conclusion: "failure",
      queuedAt: oldCreatedAt,
      startedAt: oldCreatedAt,
      finishedAt: oldCreatedAt,
      createdAt: oldCreatedAt,
    })
    .returning({ id: workflowRuns.id });

  await db.insert(workflowJobs).values({
    runId: run.id,
    name: "build",
    jobOrder: 0,
    runsOn: "default",
    status: "failure",
    conclusion: "failure",
    exitCode: 1,
    steps: "[]",
    logs:
      "==> bun test\nTypeError: Cannot read properties of undefined (reading 'length')\n  at broken (src/index.ts:1:38)\n[exit 1 in 120ms]",
    startedAt: oldCreatedAt,
    finishedAt: oldCreatedAt,
  });

  return {
    repoId: r.id,
    repoName,
    ownerUsername: username,
    workflowId: w.id,
    runId: run.id,
    baseSha: seeded.commitSha,
  };
}

describe.skipIf(!HAS_DB)("ai-ci-healer DB-backed E2E", () => {
  it("opens a patch PR + writes an ai.ci.healed audit row on the happy path", async () => {
    const fx = await seedFailedRun("happy");

    const cannedClaude = JSON.stringify({
      rootCause:
        "broken() references undefined.length, which throws at runtime.",
      fixable: true,
      suggestedFixes: [
        {
          path: "src/index.ts",
          description: "Return a numeric literal instead of dereferencing undefined.",
          severity: "high",
        },
      ],
    });

    const cannedPatch = JSON.stringify({
      explanation: "Replaced the bad expression with a safe literal.",
      patches: [
        {
          path: "src/index.ts",
          new_content: "export function broken(){ return 0; }\n",
        },
      ],
    });

    const client = fakeClient(cannedClaude);

    // Use the real patch generator — feed it the same fake client for the
    // patch call. The generator picks `client` from the opts we forward.
    const { generatePatchForGateTestFinding } = await import(
      "../lib/ai-patch-generator"
    );
    const out = await healOneRun(fx.runId, {
      client,
      // Wrap the real generator so we can swap in the second fake response
      // for the patch step (it makes a fresh call). The healer hands the
      // client through, so we wrap to replace the response between steps.
      generatePatch: async (opts) => {
        return generatePatchForGateTestFinding({
          ...opts,
          client: fakeClient(cannedPatch),
          branchOverride: `ai-patch/ci-heal-${Date.now()}`,
        });
      },
    });

    expect(out.outcome).toBe("healed");
    expect(typeof out.prNumber).toBe("number");
    expect(out.branch).toContain("ai-patch/ci-heal-");

    // PR row exists
    const prs = await db
      .select({ number: pullRequests.number, headBranch: pullRequests.headBranch })
      .from(pullRequests)
      .where(eq(pullRequests.repositoryId, fx.repoId));
    expect(prs.length).toBe(1);
    expect(prs[0].number).toBe(out.prNumber!);

    // Audit marker present
    const audits = await db
      .select({ action: auditLog.action })
      .from(auditLog)
      .where(
        and(
          eq(auditLog.targetType, "workflow_run"),
          eq(auditLog.targetId, fx.runId)
        )
      );
    expect(audits.some((a) => a.action === "ai.ci.healed")).toBe(true);
  }, 30_000);

  it("writes ai.ci.gave_up + opens no PR when Claude says unfixable", async () => {
    const fx = await seedFailedRun("unfix");

    const cannedClaude = JSON.stringify({
      rootCause: "The npm registry returned 503 mid-install.",
      fixable: false,
      suggestedFixes: [],
      unfixableReason: "External registry outage — retry later.",
    });

    const client = fakeClient(cannedClaude);
    const out = await healOneRun(fx.runId, { client });
    expect(out.outcome).toBe("gave_up");

    // No PR row
    const prs = await db
      .select({ number: pullRequests.number })
      .from(pullRequests)
      .where(eq(pullRequests.repositoryId, fx.repoId));
    expect(prs.length).toBe(0);

    // Audit marker present as gave_up
    const audits = await db
      .select({ action: auditLog.action })
      .from(auditLog)
      .where(
        and(
          eq(auditLog.targetType, "workflow_run"),
          eq(auditLog.targetId, fx.runId)
        )
      );
    expect(audits.some((a) => a.action === "ai.ci.gave_up")).toBe(true);
    expect(audits.some((a) => a.action === "ai.ci.healed")).toBe(false);
  }, 30_000);

  it("skips runs that already have a marker (no double-processing)", async () => {
    const fx = await seedFailedRun("dedupe");

    // Pre-insert a marker
    await db.insert(auditLog).values({
      action: "ai.ci.healed",
      targetType: "workflow_run",
      targetId: fx.runId,
      metadata: JSON.stringify({ pre: true }),
    });

    // Should refuse to act — Claude must NOT be called.
    let claudeCalled = 0;
    const client = {
      messages: {
        create: async () => {
          claudeCalled += 1;
          return { content: [{ type: "text" as const, text: "{}" }] };
        },
      },
    } as any;

    const out = await healOneRun(fx.runId, { client });
    expect(out.outcome).toBe("skipped");
    expect(claudeCalled).toBe(0);
  }, 30_000);
});

// ---------------------------------------------------------------------------
// analyzeFailedWorkflowRun direct-call sanity (the public surface the
// task description explicitly calls out as the entry point).
// ---------------------------------------------------------------------------

describe.skipIf(!HAS_DB)("analyzeFailedWorkflowRun", () => {
  it("returns parsed analysis with patchablePaths when Claude says fixable", async () => {
    const fx = await seedFailedRun("analyze");
    const canned = JSON.stringify({
      rootCause: "Null deref in src/index.ts.",
      fixable: true,
      suggestedFixes: [
        { path: "src/index.ts", description: "Add null guard.", severity: "high" },
      ],
    });
    const analysis = await analyzeFailedWorkflowRun(fx.runId, {
      client: fakeClient(canned),
    });
    expect(analysis).not.toBeNull();
    expect(analysis!.rootCause).toContain("Null deref");
    expect(analysis!.patchablePaths).toEqual(["src/index.ts"]);
    expect(analysis!.suggestedFixes.length).toBe(1);
  }, 30_000);

  it("returns null when Claude says unfixable", async () => {
    const fx = await seedFailedRun("analyze-unfix");
    const canned = JSON.stringify({
      rootCause: "Registry outage.",
      fixable: false,
      suggestedFixes: [],
    });
    const analysis = await analyzeFailedWorkflowRun(fx.runId, {
      client: fakeClient(canned),
    });
    expect(analysis).toBeNull();
  }, 30_000);

  it("returns null for non-failure runs", async () => {
    const fx = await seedFailedRun("non-failure");
    // Flip the run back to success — analyzer should bail.
    await db
      .update(workflowRuns)
      .set({ status: "success", conclusion: "success" })
      .where(eq(workflowRuns.id, fx.runId));
    const analysis = await analyzeFailedWorkflowRun(fx.runId, {
      client: fakeClient('{"rootCause":"x","fixable":true,"suggestedFixes":[]}'),
    });
    expect(analysis).toBeNull();
  }, 30_000);
});