Blame · Line-by-line history
ai-loop.ts
Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.
| f5d020f | 1 | /** |
| 2 | * Autonomous Issue-to-Merged-PR Loop (ai-loop). | |
| 3 | * | |
| 4 | * After spec-to-pr creates a PR, this module drives a self-healing cycle: | |
| 5 | * 1. Check if the latest gate run for the PR is green or red. | |
| 6 | * 2. Green → call performMerge and mark the PR as merged. | |
| 7 | * 3. Red and attempts < MAX_ATTEMPTS → call triggerCiAutofix, poll for a | |
| 8 | * new gate run (up to 2 minutes), then loop. | |
| 9 | * 4. Attempts exhausted → post a failure comment and give up. | |
| 10 | * | |
| 11 | * Idempotency: | |
| 12 | * - Before starting, check for a <!-- gluecron:ai-loop:v1 --> marker in | |
| 13 | * existing PR comments. If present: skip (already handled). | |
| 14 | * - Each attempt is annotated with <!-- gluecron:ai-loop:attempt:N -->. | |
| 15 | * | |
| 16 | * Safe-default: the env var AI_LOOP_ENABLED must equal "1" for fire-and- | |
| 17 | * forget callers that pass through ai-build-tasks.ts. The `runAutonomousLoop` | |
| 18 | * export itself has no such guard so tests and targeted callers can invoke it | |
| 19 | * unconditionally. | |
| 20 | * | |
| 21 | * Guard: isAiAvailable() is checked at the top of runAutonomousLoop; callers | |
| 22 | * may also check it before invoking fire-and-forget. When the API key is | |
| 23 | * absent the function returns immediately with {success:false}. | |
| 24 | */ | |
| 25 | ||
| 26 | import { and, desc, eq, sql } from "drizzle-orm"; | |
| 27 | import { db } from "../db"; | |
| 28 | import { gateRuns, prComments, pullRequests, repositories, users } from "../db/schema"; | |
| 29 | import { isAiAvailable } from "./ai-client"; | |
| 30 | import { performMerge } from "./pr-merge"; | |
| 31 | import { triggerCiAutofix } from "./ci-autofix"; | |
| 32 | import { getBotUserIdOrFallback } from "./bot-user"; | |
| 33 | import { AI_BUILD_MARKER } from "./ai-build-tasks"; | |
| 34 | ||
| 35 | // --------------------------------------------------------------------------- | |
| 36 | // Public types | |
| 37 | // --------------------------------------------------------------------------- | |
| 38 | ||
| 39 | export interface LoopResult { | |
| 40 | success: boolean; | |
| 41 | attempts: number; | |
| 42 | mergedAt?: Date; | |
| 43 | failReason?: string; | |
| 44 | } | |
| 45 | ||
| 46 | // --------------------------------------------------------------------------- | |
| 47 | // Constants | |
| 48 | // --------------------------------------------------------------------------- | |
| 49 | ||
| 50 | /** Stable marker embedded in the initial "loop started" comment. */ | |
| 51 | export const AI_LOOP_MARKER = "<!-- gluecron:ai-loop:v1 -->"; | |
| 52 | /** Per-attempt progress marker prefix. */ | |
| 53 | const AI_LOOP_ATTEMPT_PREFIX = "<!-- gluecron:ai-loop:attempt:"; | |
| 54 | ||
| 55 | /** Maximum fix-and-retry cycles before giving up. */ | |
| 56 | const MAX_ATTEMPTS = 3; | |
| 57 | ||
| 58 | /** How long to poll for a new gate run after triggering autofix (ms). */ | |
| 59 | const POLL_TIMEOUT_MS = 2 * 60 * 1000; | |
| 60 | ||
| 61 | /** Interval between polls (ms). */ | |
| 62 | const POLL_INTERVAL_MS = 10 * 1000; | |
| 63 | ||
| 64 | // --------------------------------------------------------------------------- | |
| 65 | // Internal helpers | |
| 66 | // --------------------------------------------------------------------------- | |
| 67 | ||
| 68 | /** | |
| 69 | * Return true if any PR comment contains the ai-loop:v1 idempotency marker. | |
| 70 | */ | |
| 71 | async function hasLoopMarker(prId: string): Promise<boolean> { | |
| 72 | try { | |
| 73 | const rows = await db | |
| 74 | .select({ id: prComments.id }) | |
| 75 | .from(prComments) | |
| 76 | .where( | |
| 77 | and( | |
| 78 | eq(prComments.pullRequestId, prId), | |
| 79 | sql`${prComments.body} LIKE ${"%" + AI_LOOP_MARKER + "%"}` | |
| 80 | ) | |
| 81 | ) | |
| 82 | .limit(1); | |
| 83 | return rows.length > 0; | |
| 84 | } catch { | |
| 85 | // Conservative: assume already handled on DB error. | |
| 86 | return true; | |
| 87 | } | |
| 88 | } | |
| 89 | ||
| 90 | /** | |
| 91 | * Post a comment on the PR authored by the bot (or fallback to the PR author). | |
| 92 | * Never throws. | |
| 93 | */ | |
| 94 | async function postComment( | |
| 95 | prId: string, | |
| 96 | fallbackAuthorId: string, | |
| 97 | body: string | |
| 98 | ): Promise<void> { | |
| 99 | try { | |
| 100 | const authorId = await getBotUserIdOrFallback(fallbackAuthorId); | |
| 101 | await db.insert(prComments).values({ | |
| 102 | pullRequestId: prId, | |
| 103 | authorId, | |
| 104 | body, | |
| 105 | isAiReview: true, | |
| 106 | }); | |
| 107 | } catch (err) { | |
| 108 | console.error("[ai-loop] postComment failed:", err); | |
| 109 | } | |
| 110 | } | |
| 111 | ||
| 112 | /** | |
| 113 | * Load the latest gate run for this PR. Returns null when none exists. | |
| 114 | */ | |
| 115 | async function loadLatestGateRun(prId: string): Promise<{ | |
| 116 | id: string; | |
| 117 | status: string; | |
| 118 | summary: string | null; | |
| 119 | details: string | null; | |
| 120 | createdAt: Date; | |
| 121 | } | null> { | |
| 122 | try { | |
| 123 | const rows = await db | |
| 124 | .select({ | |
| 125 | id: gateRuns.id, | |
| 126 | status: gateRuns.status, | |
| 127 | summary: gateRuns.summary, | |
| 128 | details: gateRuns.details, | |
| 129 | createdAt: gateRuns.createdAt, | |
| 130 | }) | |
| 131 | .from(gateRuns) | |
| 132 | .where(eq(gateRuns.pullRequestId, prId)) | |
| 133 | .orderBy(desc(gateRuns.createdAt)) | |
| 134 | .limit(1); | |
| 135 | return rows[0] ?? null; | |
| 136 | } catch { | |
| 137 | return null; | |
| 138 | } | |
| 139 | } | |
| 140 | ||
| 141 | /** | |
| 142 | * Update the ai_loop_attempts and ai_loop_status columns on the PR row. | |
| 143 | * Best-effort — failures are logged but not rethrown. | |
| 144 | */ | |
| 145 | async function updatePrLoopState( | |
| 146 | prId: string, | |
| 147 | attempts: number, | |
| 148 | status: "running" | "merged" | "failed" | |
| 149 | ): Promise<void> { | |
| 150 | try { | |
| 151 | await db | |
| 152 | .update(pullRequests) | |
| 153 | .set({ | |
| 154 | aiLoopAttempts: attempts, | |
| 155 | aiLoopStatus: status, | |
| 156 | updatedAt: new Date(), | |
| 157 | }) | |
| 158 | .where(eq(pullRequests.id, prId)); | |
| 159 | } catch (err) { | |
| 160 | console.error("[ai-loop] updatePrLoopState failed:", err); | |
| 161 | } | |
| 162 | } | |
| 163 | ||
| 164 | /** | |
| 165 | * Poll until a gate run newer than `afterDate` appears for this PR (or times out). | |
| 166 | * Returns the new gate run, or null on timeout. | |
| 167 | */ | |
| 168 | async function pollForNewGateRun( | |
| 169 | prId: string, | |
| 170 | afterDate: Date | |
| 171 | ): Promise<{ id: string; status: string } | null> { | |
| 172 | const deadline = Date.now() + POLL_TIMEOUT_MS; | |
| 173 | while (Date.now() < deadline) { | |
| 174 | await new Promise<void>((resolve) => setTimeout(resolve, POLL_INTERVAL_MS)); | |
| 175 | try { | |
| 176 | const rows = await db | |
| 177 | .select({ id: gateRuns.id, status: gateRuns.status }) | |
| 178 | .from(gateRuns) | |
| 179 | .where( | |
| 180 | and( | |
| 181 | eq(gateRuns.pullRequestId, prId), | |
| 182 | sql`${gateRuns.createdAt} > ${afterDate}` | |
| 183 | ) | |
| 184 | ) | |
| 185 | .orderBy(desc(gateRuns.createdAt)) | |
| 186 | .limit(1); | |
| 187 | if (rows.length > 0) return rows[0]; | |
| 188 | } catch { | |
| 189 | // continue polling | |
| 190 | } | |
| 191 | } | |
| 192 | return null; | |
| 193 | } | |
| 194 | ||
| 195 | // --------------------------------------------------------------------------- | |
| 196 | // Core export | |
| 197 | // --------------------------------------------------------------------------- | |
| 198 | ||
| 199 | /** | |
| 200 | * Drive the autonomous fix-and-merge loop for a single PR. | |
| 201 | * | |
| 202 | * Flow: | |
| 203 | * • Guard: isAiAvailable() → no-op return when ANTHROPIC_API_KEY missing. | |
| 204 | * • Idempotency: check for AI_LOOP_MARKER in existing PR comments. | |
| 205 | * • Load PR + repo metadata needed for performMerge. | |
| 206 | * • Loop up to MAX_ATTEMPTS: | |
| 207 | * - Fetch latest gate run. | |
| 208 | * - No gate run yet → wait for one (poll up to 2 min). | |
| 209 | * - Gate is green (passed/skipped) → performMerge → done. | |
| 210 | * - Gate is red → post attempt comment, triggerCiAutofix, poll for | |
| 211 | * new gate run, loop. | |
| 212 | * - Gate is pending/running → wait for it to settle (poll 2 min). | |
| 213 | * • Attempts exhausted → post failure comment, set aiLoopStatus='failed'. | |
| 214 | */ | |
| 215 | export async function runAutonomousLoop( | |
| 216 | prId: string, | |
| 217 | repoId: string | |
| 218 | ): Promise<LoopResult> { | |
| 219 | if (!isAiAvailable()) { | |
| 220 | return { success: false, attempts: 0, failReason: "ANTHROPIC_API_KEY not set" }; | |
| 221 | } | |
| 222 | ||
| 223 | // Load PR row. | |
| 224 | let pr: { | |
| 225 | id: string; | |
| 226 | number: number; | |
| 227 | title: string; | |
| 228 | body: string | null; | |
| 229 | baseBranch: string; | |
| 230 | headBranch: string; | |
| 231 | state: string; | |
| 232 | isDraft: boolean; | |
| 233 | authorId: string; | |
| 234 | repositoryId: string; | |
| 235 | aiLoopAttempts: number; | |
| 236 | aiLoopStatus: string | null; | |
| 237 | } | undefined; | |
| 238 | ||
| 239 | try { | |
| 240 | const rows = await db | |
| 241 | .select({ | |
| 242 | id: pullRequests.id, | |
| 243 | number: pullRequests.number, | |
| 244 | title: pullRequests.title, | |
| 245 | body: pullRequests.body, | |
| 246 | baseBranch: pullRequests.baseBranch, | |
| 247 | headBranch: pullRequests.headBranch, | |
| 248 | state: pullRequests.state, | |
| 249 | isDraft: pullRequests.isDraft, | |
| 250 | authorId: pullRequests.authorId, | |
| 251 | repositoryId: pullRequests.repositoryId, | |
| 252 | aiLoopAttempts: pullRequests.aiLoopAttempts, | |
| 253 | aiLoopStatus: pullRequests.aiLoopStatus, | |
| 254 | }) | |
| 255 | .from(pullRequests) | |
| 256 | .where(eq(pullRequests.id, prId)) | |
| 257 | .limit(1); | |
| 258 | pr = rows[0]; | |
| 259 | } catch (err) { | |
| 260 | return { | |
| 261 | success: false, | |
| 262 | attempts: 0, | |
| 263 | failReason: `DB load failed: ${err instanceof Error ? err.message : String(err)}`, | |
| 264 | }; | |
| 265 | } | |
| 266 | ||
| 267 | if (!pr) { | |
| 268 | return { success: false, attempts: 0, failReason: "PR not found" }; | |
| 269 | } | |
| 270 | if (pr.state !== "open") { | |
| 271 | return { | |
| 272 | success: false, | |
| 273 | attempts: 0, | |
| 274 | failReason: `PR is not open (state=${pr.state})`, | |
| 275 | }; | |
| 276 | } | |
| 277 | // Already handled by another loop run. | |
| 278 | if (pr.aiLoopStatus === "running" || pr.aiLoopStatus === "merged") { | |
| 279 | return { | |
| 280 | success: pr.aiLoopStatus === "merged", | |
| 281 | attempts: pr.aiLoopAttempts, | |
| 282 | failReason: | |
| 283 | pr.aiLoopStatus === "running" | |
| 284 | ? "Another loop run is already in progress" | |
| 285 | : undefined, | |
| 286 | }; | |
| 287 | } | |
| 288 | ||
| 289 | // Idempotency: skip if the loop marker already exists. | |
| 290 | if (await hasLoopMarker(prId)) { | |
| 291 | return { | |
| 292 | success: false, | |
| 293 | attempts: 0, | |
| 294 | failReason: "Loop already started (marker present)", | |
| 295 | }; | |
| 296 | } | |
| 297 | ||
| 298 | // Load repo + owner for performMerge. | |
| 299 | let repoRow: { name: string; ownerUsername: string } | undefined; | |
| 300 | try { | |
| 301 | const rows = await db | |
| 302 | .select({ | |
| 303 | name: repositories.name, | |
| 304 | ownerUsername: users.username, | |
| 305 | }) | |
| 306 | .from(repositories) | |
| 307 | .innerJoin(users, eq(users.id, repositories.ownerId)) | |
| 308 | .where(eq(repositories.id, repoId)) | |
| 309 | .limit(1); | |
| 310 | repoRow = rows[0]; | |
| 311 | } catch { | |
| 312 | /* fall through — caught below */ | |
| 313 | } | |
| 314 | ||
| 315 | if (!repoRow) { | |
| 316 | return { success: false, attempts: 0, failReason: "Repo not found" }; | |
| 317 | } | |
| 318 | ||
| 319 | // Mark loop as started: post the idempotency marker comment and update DB. | |
| 320 | await postComment( | |
| 321 | prId, | |
| 322 | pr.authorId, | |
| 323 | `${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).` | |
| 324 | ); | |
| 325 | await updatePrLoopState(prId, 0, "running"); | |
| 326 | ||
| 327 | let attempts = 0; | |
| 328 | ||
| 329 | // --------------------------------------------------------------------------- | |
| 330 | // Main retry loop | |
| 331 | // --------------------------------------------------------------------------- | |
| 332 | while (attempts < MAX_ATTEMPTS) { | |
| 333 | // Re-fetch PR state in case it was closed/merged externally. | |
| 334 | try { | |
| 335 | const rows = await db | |
| 336 | .select({ state: pullRequests.state, isDraft: pullRequests.isDraft }) | |
| 337 | .from(pullRequests) | |
| 338 | .where(eq(pullRequests.id, prId)) | |
| 339 | .limit(1); | |
| 340 | const current = rows[0]; | |
| 341 | if (!current || current.state !== "open") { | |
| 342 | return { | |
| 343 | success: current?.state === "merged", | |
| 344 | attempts, | |
| 345 | failReason: | |
| 346 | current?.state !== "merged" | |
| 347 | ? `PR no longer open (state=${current?.state ?? "unknown"})` | |
| 348 | : undefined, | |
| 349 | }; | |
| 350 | } | |
| 351 | // Refresh isDraft flag in case someone changed it. | |
| 352 | pr = { ...pr, isDraft: current.isDraft }; | |
| 353 | } catch { | |
| 354 | // Continue with stale data — best effort. | |
| 355 | } | |
| 356 | ||
| 357 | // Fetch the latest gate run. | |
| 358 | let gateRun = await loadLatestGateRun(prId); | |
| 359 | ||
| 360 | // If no gate run exists yet, wait for one. | |
| 361 | if (!gateRun) { | |
| 362 | const found = await pollForNewGateRun(prId, new Date(0)); | |
| 363 | if (!found) { | |
| 364 | // Still nothing — treat as a failure to progress. | |
| 365 | break; | |
| 366 | } | |
| 367 | gateRun = { ...found, summary: null, details: null, createdAt: new Date() }; | |
| 368 | } | |
| 369 | ||
| 370 | // If gate is still pending/running, wait for it to settle. | |
| 371 | if (gateRun.status === "pending" || gateRun.status === "running") { | |
| 372 | const settled = await pollForNewGateRun(prId, new Date(gateRun.createdAt.getTime() - 1)); | |
| 373 | if (settled) { | |
| 374 | gateRun = { ...gateRun, ...settled }; | |
| 375 | } | |
| 376 | // If still not settled, we'll try to evaluate with what we have. | |
| 377 | } | |
| 378 | ||
| 379 | const gateGreen = | |
| 380 | gateRun.status === "passed" || | |
| 381 | gateRun.status === "skipped" || | |
| 382 | gateRun.status === "repaired"; | |
| 383 | ||
| 384 | if (gateGreen) { | |
| 385 | // Gate is green — attempt to merge. | |
| 386 | const mergeResult = await performMerge({ | |
| 387 | pr: { | |
| 388 | id: pr.id, | |
| 389 | number: pr.number, | |
| 390 | title: pr.title, | |
| 391 | body: pr.body, | |
| 392 | baseBranch: pr.baseBranch, | |
| 393 | headBranch: pr.headBranch, | |
| 394 | repositoryId: pr.repositoryId, | |
| 395 | authorId: pr.authorId, | |
| 396 | state: "open", | |
| 397 | isDraft: pr.isDraft, | |
| 398 | }, | |
| 399 | ownerName: repoRow.ownerUsername, | |
| 400 | repoName: repoRow.name, | |
| 401 | actorUserId: pr.authorId, | |
| 402 | hasConflicts: false, | |
| 403 | }); | |
| 404 | ||
| 405 | if (mergeResult.ok) { | |
| 406 | const mergedAt = new Date(); | |
| 407 | await updatePrLoopState(prId, attempts, "merged"); | |
| 408 | await postComment( | |
| 409 | prId, | |
| 410 | pr.authorId, | |
| 411 | `${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"}`}.` | |
| 412 | ); | |
| 413 | return { success: true, attempts, mergedAt }; | |
| 414 | } | |
| 415 | ||
| 416 | // Merge failed despite green gate — this is unexpected; give up. | |
| 417 | const reason = mergeResult.error || "unknown merge error"; | |
| 418 | await updatePrLoopState(prId, attempts, "failed"); | |
| 419 | await postComment( | |
| 420 | prId, | |
| 421 | pr.authorId, | |
| 422 | `${AI_LOOP_MARKER}\n**AI Loop: merge failed**\n\nThe gate was green but the merge failed: ${reason}\n\nManual intervention required.` | |
| 423 | ); | |
| 424 | return { success: false, attempts, failReason: `Merge failed: ${reason}` }; | |
| 425 | } | |
| 426 | ||
| 427 | // Gate is red — attempt a fix. | |
| 428 | attempts += 1; | |
| 429 | await updatePrLoopState(prId, attempts, "running"); | |
| 430 | ||
| 431 | const attemptMarker = `${AI_LOOP_ATTEMPT_PREFIX}${attempts} -->`; | |
| 432 | await postComment( | |
| 433 | prId, | |
| 434 | pr.authorId, | |
| 435 | `${attemptMarker}\n**AI Loop: fix attempt ${attempts}/${MAX_ATTEMPTS}**\n\nGate run \`${gateRun.id}\` reported status \`${gateRun.status}\`. Triggering CI autofix…` | |
| 436 | ); | |
| 437 | ||
| 438 | // Trigger the autofix (fire-and-forget inside ci-autofix, but we await | |
| 439 | // the wrapper because it does the Claude call synchronously). | |
| 440 | await triggerCiAutofix(gateRun.id); | |
| 441 | ||
| 442 | // Wait for a new gate run to appear (autofix triggers a new push → new run). | |
| 443 | const newRun = await pollForNewGateRun(prId, gateRun.createdAt); | |
| 444 | if (!newRun) { | |
| 445 | // Autofix didn't produce a new gate run within the timeout. | |
| 446 | if (attempts >= MAX_ATTEMPTS) break; | |
| 447 | // Try the next attempt anyway — maybe the push just didn't start a new run. | |
| 448 | } | |
| 449 | } | |
| 450 | ||
| 451 | // Exhausted all attempts. | |
| 452 | await updatePrLoopState(prId, attempts, "failed"); | |
| 453 | await postComment( | |
| 454 | prId, | |
| 455 | pr.authorId, | |
| 456 | `${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}` | |
| 457 | ); | |
| 458 | ||
| 459 | return { | |
| 460 | success: false, | |
| 461 | attempts, | |
| 462 | failReason: `Exhausted ${MAX_ATTEMPTS} fix attempts`, | |
| 463 | }; | |
| 464 | } | |
| 465 | ||
| 466 | // --------------------------------------------------------------------------- | |
| 467 | // Autopilot sweep helper | |
| 468 | // --------------------------------------------------------------------------- | |
| 469 | ||
| 470 | /** | |
| 471 | * Scan for open PRs whose body contains the AI_BUILD_MARKER (created by the | |
| 472 | * ai-build flow) and that have no AI_LOOP_MARKER comment yet. Runs up to | |
| 473 | * `cap` per tick. Called by the autopilot `ai-loop-sweep` task. | |
| 474 | * | |
| 475 | * Never throws. | |
| 476 | */ | |
| 477 | export async function runAiLoopSweepOnce(cap = 5): Promise<{ | |
| 478 | considered: number; | |
| 479 | started: number; | |
| 480 | skipped: number; | |
| 481 | }> { | |
| 482 | if (!isAiAvailable()) { | |
| 483 | return { considered: 0, started: 0, skipped: 0 }; | |
| 484 | } | |
| 485 | ||
| 486 | let candidates: { id: string; repositoryId: string }[] = []; | |
| 487 | try { | |
| 488 | candidates = await db | |
| 489 | .select({ id: pullRequests.id, repositoryId: pullRequests.repositoryId }) | |
| 490 | .from(pullRequests) | |
| 491 | .where( | |
| 492 | and( | |
| 493 | eq(pullRequests.state, "open"), | |
| 494 | sql`${pullRequests.body} LIKE ${"%" + AI_BUILD_MARKER + "%"}` | |
| 495 | ) | |
| 496 | ) | |
| 497 | .limit(cap * 3); // over-fetch so we can filter idempotent ones in JS | |
| 498 | } catch (err) { | |
| 499 | console.error("[ai-loop] sweep candidate query failed:", err); | |
| 500 | return { considered: 0, started: 0, skipped: 0 }; | |
| 501 | } | |
| 502 | ||
| 503 | let considered = 0; | |
| 504 | let started = 0; | |
| 505 | let skipped = 0; | |
| 506 | ||
| 507 | for (const cand of candidates) { | |
| 508 | if (started >= cap) break; | |
| 509 | considered += 1; | |
| 510 | ||
| 511 | // Skip if loop already has a marker comment. | |
| 512 | const already = await hasLoopMarker(cand.id); | |
| 513 | if (already) { | |
| 514 | skipped += 1; | |
| 515 | continue; | |
| 516 | } | |
| 517 | ||
| 518 | // Fire-and-forget — the loop is long-running (up to 6 minutes). | |
| 519 | try { | |
| 520 | void runAutonomousLoop(cand.id, cand.repositoryId).catch((err) => { | |
| 521 | console.error( | |
| 522 | `[ai-loop] runAutonomousLoop threw for pr=${cand.id}:`, | |
| 523 | err | |
| 524 | ); | |
| 525 | }); | |
| 526 | started += 1; | |
| 527 | } catch (err) { | |
| 528 | console.error(`[ai-loop] sweep: failed to start loop for pr=${cand.id}:`, err); | |
| 529 | skipped += 1; | |
| 530 | } | |
| 531 | } | |
| 532 | ||
| 533 | return { considered, started, skipped }; | |
| 534 | } |