CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
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 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 | /**
* Autonomous Issue-to-Merged-PR Loop (ai-loop).
*
* After spec-to-pr creates a PR, this module drives a self-healing cycle:
* 1. Check if the latest gate run for the PR is green or red.
* 2. Green → call performMerge and mark the PR as merged.
* 3. Red and attempts < MAX_ATTEMPTS → call triggerCiAutofix, poll for a
* new gate run (up to 2 minutes), then loop.
* 4. Attempts exhausted → post a failure comment and give up.
*
* Idempotency:
* - Before starting, check for a <!-- gluecron:ai-loop:v1 --> marker in
* existing PR comments. If present: skip (already handled).
* - Each attempt is annotated with <!-- gluecron:ai-loop:attempt:N -->.
*
* Safe-default: the env var AI_LOOP_ENABLED must equal "1" for fire-and-
* forget callers that pass through ai-build-tasks.ts. The `runAutonomousLoop`
* export itself has no such guard so tests and targeted callers can invoke it
* unconditionally.
*
* Guard: isAiAvailable() is checked at the top of runAutonomousLoop; callers
* may also check it before invoking fire-and-forget. When the API key is
* absent the function returns immediately with {success:false}.
*/
import { and, desc, eq, sql } from "drizzle-orm";
import { db } from "../db";
import { gateRuns, prComments, pullRequests, repositories, users } from "../db/schema";
import { isAiAvailable } from "./ai-client";
import { performMerge } from "./pr-merge";
import { triggerCiAutofix } from "./ci-autofix";
import { getBotUserIdOrFallback } from "./bot-user";
// Local copy to avoid import cycle with ai-build-tasks.ts
const AI_BUILD_MARKER = "<!-- gluecron:ai-build:v1 -->";
// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------
export interface LoopResult {
success: boolean;
attempts: number;
mergedAt?: Date;
failReason?: string;
}
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
/** Stable marker embedded in the initial "loop started" comment. */
export const AI_LOOP_MARKER = "<!-- gluecron:ai-loop:v1 -->";
/** Per-attempt progress marker prefix. */
const AI_LOOP_ATTEMPT_PREFIX = "<!-- gluecron:ai-loop:attempt:";
/** Maximum fix-and-retry cycles before giving up. */
const MAX_ATTEMPTS: number = 3;
/** How long to poll for a new gate run after triggering autofix (ms). */
const POLL_TIMEOUT_MS = 2 * 60 * 1000;
/** Interval between polls (ms). */
const POLL_INTERVAL_MS = 10 * 1000;
// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------
/**
* Return true if any PR comment contains the ai-loop:v1 idempotency marker.
*/
async function hasLoopMarker(prId: string): Promise<boolean> {
try {
const rows = await db
.select({ id: prComments.id })
.from(prComments)
.where(
and(
eq(prComments.pullRequestId, prId),
sql`${prComments.body} LIKE ${"%" + AI_LOOP_MARKER + "%"}`
)
)
.limit(1);
return rows.length > 0;
} catch {
// Conservative: assume already handled on DB error.
return true;
}
}
/**
* Post a comment on the PR authored by the bot (or fallback to the PR author).
* Never throws.
*/
async function postComment(
prId: string,
fallbackAuthorId: string,
body: string
): Promise<void> {
try {
const authorId = await getBotUserIdOrFallback(fallbackAuthorId);
await db.insert(prComments).values({
pullRequestId: prId,
authorId,
body,
isAiReview: true,
});
} catch (err) {
console.error("[ai-loop] postComment failed:", err);
}
}
/**
* Load the latest gate run for this PR. Returns null when none exists.
*/
async function loadLatestGateRun(prId: string): Promise<{
id: string;
status: string;
summary: string | null;
details: string | null;
createdAt: Date;
} | null> {
try {
const rows = await db
.select({
id: gateRuns.id,
status: gateRuns.status,
summary: gateRuns.summary,
details: gateRuns.details,
createdAt: gateRuns.createdAt,
})
.from(gateRuns)
.where(eq(gateRuns.pullRequestId, prId))
.orderBy(desc(gateRuns.createdAt))
.limit(1);
return rows[0] ?? null;
} catch {
return null;
}
}
/**
* Update the ai_loop_attempts and ai_loop_status columns on the PR row.
* Best-effort — failures are logged but not rethrown.
*/
async function updatePrLoopState(
prId: string,
attempts: number,
status: "running" | "merged" | "failed"
): Promise<void> {
try {
await db
.update(pullRequests)
.set({
aiLoopAttempts: attempts,
aiLoopStatus: status,
updatedAt: new Date(),
})
.where(eq(pullRequests.id, prId));
} catch (err) {
console.error("[ai-loop] updatePrLoopState failed:", err);
}
}
/**
* Poll until a gate run newer than `afterDate` appears for this PR (or times out).
* Returns the new gate run, or null on timeout.
*/
async function pollForNewGateRun(
prId: string,
afterDate: Date
): Promise<{ id: string; status: string } | null> {
const deadline = Date.now() + POLL_TIMEOUT_MS;
while (Date.now() < deadline) {
await new Promise<void>((resolve) => setTimeout(resolve, POLL_INTERVAL_MS));
try {
const rows = await db
.select({ id: gateRuns.id, status: gateRuns.status })
.from(gateRuns)
.where(
and(
eq(gateRuns.pullRequestId, prId),
sql`${gateRuns.createdAt} > ${afterDate}`
)
)
.orderBy(desc(gateRuns.createdAt))
.limit(1);
if (rows.length > 0) return rows[0];
} catch {
// continue polling
}
}
return null;
}
// ---------------------------------------------------------------------------
// Core export
// ---------------------------------------------------------------------------
/**
* Drive the autonomous fix-and-merge loop for a single PR.
*
* Flow:
* • Guard: isAiAvailable() → no-op return when ANTHROPIC_API_KEY missing.
* • Idempotency: check for AI_LOOP_MARKER in existing PR comments.
* • Load PR + repo metadata needed for performMerge.
* • Loop up to MAX_ATTEMPTS:
* - Fetch latest gate run.
* - No gate run yet → wait for one (poll up to 2 min).
* - Gate is green (passed/skipped) → performMerge → done.
* - Gate is red → post attempt comment, triggerCiAutofix, poll for
* new gate run, loop.
* - Gate is pending/running → wait for it to settle (poll 2 min).
* • Attempts exhausted → post failure comment, set aiLoopStatus='failed'.
*/
export async function runAutonomousLoop(
prId: string,
repoId: string
): Promise<LoopResult> {
if (!isAiAvailable()) {
return { success: false, attempts: 0, failReason: "ANTHROPIC_API_KEY not set" };
}
// Load PR row.
let pr: {
id: string;
number: number;
title: string;
body: string | null;
baseBranch: string;
headBranch: string;
state: string;
isDraft: boolean;
authorId: string;
repositoryId: string;
aiLoopAttempts: number;
aiLoopStatus: string | null;
} | undefined;
try {
const rows = await db
.select({
id: pullRequests.id,
number: pullRequests.number,
title: pullRequests.title,
body: pullRequests.body,
baseBranch: pullRequests.baseBranch,
headBranch: pullRequests.headBranch,
state: pullRequests.state,
isDraft: pullRequests.isDraft,
authorId: pullRequests.authorId,
repositoryId: pullRequests.repositoryId,
aiLoopAttempts: pullRequests.aiLoopAttempts,
aiLoopStatus: pullRequests.aiLoopStatus,
})
.from(pullRequests)
.where(eq(pullRequests.id, prId))
.limit(1);
pr = rows[0];
} catch (err) {
return {
success: false,
attempts: 0,
failReason: `DB load failed: ${err instanceof Error ? err.message : String(err)}`,
};
}
if (!pr) {
return { success: false, attempts: 0, failReason: "PR not found" };
}
if (pr.state !== "open") {
return {
success: false,
attempts: 0,
failReason: `PR is not open (state=${pr.state})`,
};
}
// Already handled by another loop run.
if (pr.aiLoopStatus === "running" || pr.aiLoopStatus === "merged") {
return {
success: pr.aiLoopStatus === "merged",
attempts: pr.aiLoopAttempts,
failReason:
pr.aiLoopStatus === "running"
? "Another loop run is already in progress"
: undefined,
};
}
// Idempotency: skip if the loop marker already exists.
if (await hasLoopMarker(prId)) {
return {
success: false,
attempts: 0,
failReason: "Loop already started (marker present)",
};
}
// Load repo + owner for performMerge.
let repoRow: { name: string; ownerUsername: string } | undefined;
try {
const rows = await db
.select({
name: repositories.name,
ownerUsername: users.username,
})
.from(repositories)
.innerJoin(users, eq(users.id, repositories.ownerId))
.where(eq(repositories.id, repoId))
.limit(1);
repoRow = rows[0];
} catch {
/* fall through — caught below */
}
if (!repoRow) {
return { success: false, attempts: 0, failReason: "Repo not found" };
}
// Mark loop as started: post the idempotency marker comment and update DB.
await postComment(
prId,
pr.authorId,
`${AI_LOOP_MARKER}\nThe autonomous AI loop has started for this PR. It will attempt to fix any CI failures and merge automatically (up to ${MAX_ATTEMPTS} attempts).`
);
await updatePrLoopState(prId, 0, "running");
let attempts = 0;
// ---------------------------------------------------------------------------
// Main retry loop
// ---------------------------------------------------------------------------
while (attempts < MAX_ATTEMPTS) {
// Re-fetch PR state in case it was closed/merged externally.
try {
const rows = await db
.select({ state: pullRequests.state, isDraft: pullRequests.isDraft })
.from(pullRequests)
.where(eq(pullRequests.id, prId))
.limit(1);
const current = rows[0];
if (!current || current.state !== "open") {
return {
success: current?.state === "merged",
attempts,
failReason:
current?.state !== "merged"
? `PR no longer open (state=${current?.state ?? "unknown"})`
: undefined,
};
}
// Refresh isDraft flag in case someone changed it.
pr = { ...pr, isDraft: current.isDraft };
} catch {
// Continue with stale data — best effort.
}
// Fetch the latest gate run.
let gateRun = await loadLatestGateRun(prId);
// If no gate run exists yet, wait for one.
if (!gateRun) {
const found = await pollForNewGateRun(prId, new Date(0));
if (!found) {
// Still nothing — treat as a failure to progress.
break;
}
gateRun = { ...found, summary: null, details: null, createdAt: new Date() };
}
// If gate is still pending/running, wait for it to settle.
if (gateRun.status === "pending" || gateRun.status === "running") {
const settled = await pollForNewGateRun(prId, new Date(gateRun.createdAt.getTime() - 1));
if (settled) {
gateRun = { ...gateRun, ...settled };
}
// If still not settled, we'll try to evaluate with what we have.
}
const gateGreen =
gateRun.status === "passed" ||
gateRun.status === "skipped" ||
gateRun.status === "repaired";
if (gateGreen) {
// Gate is green — attempt to merge.
const mergeResult = await performMerge({
pr: {
id: pr.id,
number: pr.number,
title: pr.title,
body: pr.body,
baseBranch: pr.baseBranch,
headBranch: pr.headBranch,
repositoryId: pr.repositoryId,
authorId: pr.authorId,
state: "open",
isDraft: pr.isDraft,
},
ownerName: repoRow.ownerUsername,
repoName: repoRow.name,
actorUserId: pr.authorId,
hasConflicts: false,
});
if (mergeResult.ok) {
const mergedAt = new Date();
await updatePrLoopState(prId, attempts, "merged");
await postComment(
prId,
pr.authorId,
`${AI_LOOP_MARKER}\nThe autonomous AI loop successfully merged this PR after ${attempts === 0 ? "0 fix attempts (gate was already green)" : `${attempts} fix attempt${attempts === 1 ? "" : "s"}`}.`
);
return { success: true, attempts, mergedAt };
}
// Merge failed despite green gate — this is unexpected; give up.
const reason = mergeResult.error || "unknown merge error";
await updatePrLoopState(prId, attempts, "failed");
await postComment(
prId,
pr.authorId,
`${AI_LOOP_MARKER}\n**AI Loop: merge failed**\n\nThe gate was green but the merge failed: ${reason}\n\nManual intervention required.`
);
return { success: false, attempts, failReason: `Merge failed: ${reason}` };
}
// Gate is red — attempt a fix.
attempts += 1;
await updatePrLoopState(prId, attempts, "running");
const attemptMarker = `${AI_LOOP_ATTEMPT_PREFIX}${attempts} -->`;
await postComment(
prId,
pr.authorId,
`${attemptMarker}\n**AI Loop: fix attempt ${attempts}/${MAX_ATTEMPTS}**\n\nGate run \`${gateRun.id}\` reported status \`${gateRun.status}\`. Triggering CI autofix…`
);
// Trigger the autofix (fire-and-forget inside ci-autofix, but we await
// the wrapper because it does the Claude call synchronously).
await triggerCiAutofix(gateRun.id);
// Wait for a new gate run to appear (autofix triggers a new push → new run).
const newRun = await pollForNewGateRun(prId, gateRun.createdAt);
if (!newRun) {
// Autofix didn't produce a new gate run within the timeout.
if (attempts >= MAX_ATTEMPTS) break;
// Try the next attempt anyway — maybe the push just didn't start a new run.
}
}
// Exhausted all attempts.
await updatePrLoopState(prId, attempts, "failed");
await postComment(
prId,
pr.authorId,
`${AI_LOOP_MARKER}\n**AI Loop: exhausted ${MAX_ATTEMPTS} fix attempts**\n\nThe autonomous loop was unable to repair CI failures after ${MAX_ATTEMPTS} attempt${MAX_ATTEMPTS > 1 ? "s" : ""}. Manual intervention required.\n\nCC: @${repoRow.ownerUsername}`
);
return {
success: false,
attempts,
failReason: `Exhausted ${MAX_ATTEMPTS} fix attempts`,
};
}
// ---------------------------------------------------------------------------
// Autopilot sweep helper
// ---------------------------------------------------------------------------
/**
* Scan for open PRs whose body contains the AI_BUILD_MARKER (created by the
* ai-build flow) and that have no AI_LOOP_MARKER comment yet. Runs up to
* `cap` per tick. Called by the autopilot `ai-loop-sweep` task.
*
* Never throws.
*/
export async function runAiLoopSweepOnce(cap = 5): Promise<{
considered: number;
started: number;
skipped: number;
}> {
if (!isAiAvailable()) {
return { considered: 0, started: 0, skipped: 0 };
}
let candidates: { id: string; repositoryId: string }[] = [];
try {
candidates = await db
.select({ id: pullRequests.id, repositoryId: pullRequests.repositoryId })
.from(pullRequests)
.where(
and(
eq(pullRequests.state, "open"),
sql`${pullRequests.body} LIKE ${"%" + AI_BUILD_MARKER + "%"}`
)
)
.limit(cap * 3); // over-fetch so we can filter idempotent ones in JS
} catch (err) {
console.error("[ai-loop] sweep candidate query failed:", err);
return { considered: 0, started: 0, skipped: 0 };
}
let considered = 0;
let started = 0;
let skipped = 0;
for (const cand of candidates) {
if (started >= cap) break;
considered += 1;
// Skip if loop already has a marker comment.
const already = await hasLoopMarker(cand.id);
if (already) {
skipped += 1;
continue;
}
// Fire-and-forget — the loop is long-running (up to 6 minutes).
try {
void runAutonomousLoop(cand.id, cand.repositoryId).catch((err) => {
console.error(
`[ai-loop] runAutonomousLoop threw for pr=${cand.id}:`,
err
);
});
started += 1;
} catch (err) {
console.error(`[ai-loop] sweep: failed to start loop for pr=${cand.id}:`, err);
skipped += 1;
}
}
return { considered, started, skipped };
}
|