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
auto-merge.ts26.5 KB · 787 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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
/**
 * Block K2 — AI-gated auto-merge evaluator.
 *
 * Pure decision helper. Given a PR, answer the single question:
 *
 *   "Should this PR auto-merge right now?"
 *
 * This module is intentionally a parallel surface to the manual-merge path
 * in `src/routes/pulls.tsx` — it MUST NOT relax any rule that the manual
 * path enforces. The rule of thumb: anything an autopilot can do, a human
 * could have done by clicking Merge themselves.
 *
 * Decision rules (all must hold for `merge: true`):
 *
 *   1. A `branch_protection` rule matches the base branch AND
 *      `enableAutoMerge=true` on that rule. Default-deny when no rule
 *      matches — auto-merge is strictly opt-in per branch.
 *   2. PR is not a draft.
 *   3. `evaluateProtection` (the existing branch-protection helper)
 *      returns allowed=true for this PR's context, including required
 *      status checks via `listRequiredChecks` / `passingCheckNames`.
 *   4. When `requireAiApproval=true` on the rule, there is an
 *      AI-review comment carrying `AI_REVIEW_MARKER` whose body looks
 *      like an approval (see `aiCommentLooksApproved`).
 *   5. (Optional) PR diff is within `opts.maxChangedFiles` /
 *      `opts.maxChangedLines` if provided.
 *
 * K3 (the autopilot ticker) is the only intended caller — this module
 * deliberately does NOT execute the merge. It only decides.
 */

import { and, eq } from "drizzle-orm";
import { db } from "../db";
import {
  branchProtection,
  prComments,
  pullRequests,
  repositories,
  users,
} from "../db/schema";
import type { BranchProtection } from "../db/schema";
import {
  countHumanApprovals,
  evaluateProtection,
  listRequiredChecks,
  matchProtection,
  passingCheckNames,
} from "./branch-protection";
import { AI_REVIEW_MARKER } from "./ai-review";
import { audit } from "./notify";
import { getRepoPath } from "../git/repository";
import { getLatestCachedPrRisk } from "./pr-risk";
import { performMerge, type PerformMergeResult } from "./pr-merge";
import {
  getAutomationSettings,
  type AutomationSettingsLoader,
} from "./automation-settings";

// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------

export interface AutoMergeContext {
  pullRequestId: string;
  repositoryId: string;
  baseBranch: string;
  isDraft: boolean;
  authorUserId: string;
}

export interface AutoMergeDecision {
  merge: boolean;
  reason: string;
  blocking?: string[];
}

export interface AutoMergeOptions {
  maxChangedFiles?: number;
  maxChangedLines?: number;
  /** Injectable clock for tests. Unused today but reserved for K3 cooldowns. */
  now?: Date;
  /**
   * Test-only injection of an owner/repo pair so the diff-size check can
   * shell out to the bare repo. In production the caller resolves these
   * from the repository row before calling. When omitted, the size cap is
   * skipped (treated as "no cap configured").
   */
  ownerName?: string;
  repoName?: string;
  /** Head branch for the diff-size check. Required when caps are set. */
  headBranch?: string;
  /** Test-only injection of the per-repo automation settings loader. */
  loadAutomationSettings?: AutomationSettingsLoader;
}

// ---------------------------------------------------------------------------
// Pure decision helper
// ---------------------------------------------------------------------------

/**
 * Internal pure decision helper. All DB-derived facts are passed in as
 * arguments so tests can drive every branch without a real database.
 *
 * Block M3 — when `risk` is provided AND its band is `critical`, the
 * auto-merge is blocked with a clear reason. Lower bands never block
 * auto-merge; the score is informational on the manual path only.
 */
export function decideAutoMerge(args: {
  rule: BranchProtection | null;
  isDraft: boolean;
  aiApproved: boolean;
  humanApprovalCount: number;
  hasFailedGates: boolean;
  passingCheckNames: string[];
  requiredCheckNames: string[];
  diffStats?: { files: number; lines: number } | null;
  caps?: { maxChangedFiles?: number; maxChangedLines?: number };
  risk?: { score: number; band: "low" | "medium" | "high" | "critical" } | null;
}): AutoMergeDecision {
  const blocking: string[] = [];

  // 1. Default-deny: must have a matching rule AND it must opt in.
  if (!args.rule) {
    blocking.push(
      "No branch_protection rule matches the base branch — auto-merge is default-deny."
    );
    return { merge: false, reason: blocking[0], blocking };
  }
  if (!args.rule.enableAutoMerge) {
    blocking.push(
      `Branch protection '${args.rule.pattern}' does not have auto-merge enabled.`
    );
  }

  // 2. Draft check.
  if (args.isDraft) {
    blocking.push("Pull request is marked as a draft.");
  }

  // 3. Reuse the manual-merge gating exactly. Whatever blocks a human Merge
  // click must also block auto-merge.
  const decision = evaluateProtection(
    args.rule,
    {
      aiApproved: args.aiApproved,
      humanApprovalCount: args.humanApprovalCount,
      gateResultGreen: !args.hasFailedGates,
      hasFailedGates: args.hasFailedGates,
      passingCheckNames: args.passingCheckNames,
    },
    args.requiredCheckNames
  );
  if (!decision.allowed) {
    for (const r of decision.reasons) blocking.push(r);
  }

  // 4. AI-approval semantics — already covered by evaluateProtection when
  // requireAiApproval=true. We do NOT double-add the same reason here; the
  // caller is responsible for sourcing `aiApproved` from a marker-bearing
  // AI comment that survives `aiCommentLooksApproved`.

  // M3. Pre-merge risk score — `critical` blocks auto-merge regardless
  // of every other gate being green. Lower bands are informational only.
  if (args.risk && args.risk.band === "critical") {
    blocking.push(`PR risk score is critical (${args.risk.score}/10)`);
  }

  // 5. Optional size cap.
  if (args.caps && args.diffStats) {
    const { maxChangedFiles, maxChangedLines } = args.caps;
    if (
      typeof maxChangedFiles === "number" &&
      args.diffStats.files > maxChangedFiles
    ) {
      blocking.push(
        `PR changes ${args.diffStats.files} file(s); auto-merge cap is ${maxChangedFiles}.`
      );
    }
    if (
      typeof maxChangedLines === "number" &&
      args.diffStats.lines > maxChangedLines
    ) {
      blocking.push(
        `PR changes ${args.diffStats.lines} line(s); auto-merge cap is ${maxChangedLines}.`
      );
    }
  }

  if (blocking.length === 0) {
    return {
      merge: true,
      reason: `All auto-merge conditions met for '${args.rule.pattern}'.`,
    };
  }
  return { merge: false, reason: blocking.join(" "), blocking };
}

// ---------------------------------------------------------------------------
// AI-comment approval heuristic
// ---------------------------------------------------------------------------

/**
 * Decide whether a single AI-review comment body indicates approval.
 *
 * `triggerAiReview` (in src/lib/ai-review.ts) emits a marker-bearing
 * summary comment in two shapes:
 *
 *   - On success: starts with `## AI Code Review`, followed by either
 *     `"no blocking issues found"` (approved) or
 *     `"flagged N item(s) for human attention"` (not approved).
 *   - On API failure: starts with `## AI review unavailable`.
 *
 * Per spec, approval is defined negatively:
 *   - body does NOT contain `"AI review unavailable"`, AND
 *   - body does NOT contain `"severity: blocking"` (case-insensitive).
 *
 * We additionally treat the "flagged N item(s)" verdict as not-approved
 * because that's what triggerAiReview itself uses to signal blocking
 * findings, even though it doesn't use the `severity: blocking` token.
 * If future reviewers do emit `severity: blocking`, that branch still
 * matches via the case-insensitive substring rule.
 */
export function aiCommentLooksApproved(body: string): boolean {
  if (!body) return false;
  const lower = body.toLowerCase();
  if (lower.includes("ai review unavailable")) return false;
  if (lower.includes("severity: blocking")) return false;
  // triggerAiReview's "flagged N item(s)" wording — explicit non-approval.
  if (/flagged \d+ item/i.test(body)) return false;
  return true;
}

// ---------------------------------------------------------------------------
// DB-backed orchestrator
// ---------------------------------------------------------------------------

/**
 * Locate the AI-review summary comment for a PR and return whether it
 * looks like an approval. Returns false when no marker-bearing AI
 * comment is found — i.e. AI review hasn't completed yet.
 */
async function aiApprovedForPr(pullRequestId: string): Promise<boolean> {
  try {
    const rows = await db
      .select({ body: prComments.body })
      .from(prComments)
      .where(
        and(
          eq(prComments.pullRequestId, pullRequestId),
          eq(prComments.isAiReview, true)
        )
      );
    const markerRows = rows.filter((r) =>
      (r.body || "").includes(AI_REVIEW_MARKER)
    );
    if (markerRows.length === 0) return false;
    // If *any* marker comment looks approved, count as approved. In
    // practice triggerAiReview writes exactly one summary marker, so this
    // collapses to the single comment's verdict.
    return markerRows.some((r) => aiCommentLooksApproved(r.body || ""));
  } catch {
    return false;
  }
}

/**
 * Best-effort diff stats for size caps. Shells out to `git diff
 * --numstat base...head` in the bare repo. Returns null on any error so
 * the caller can decide whether to fail-closed (we currently treat null
 * stats as "size unknown → don't enforce the cap" which is permissive
 * but documented).
 */
async function diffStatsForBranches(
  ownerName: string,
  repoName: string,
  baseBranch: string,
  headBranch: string
): Promise<{ files: number; lines: number } | null> {
  try {
    const cwd = getRepoPath(ownerName, repoName);
    const proc = Bun.spawn(
      ["git", "diff", "--numstat", `${baseBranch}...${headBranch}`, "--"],
      { cwd, stdout: "pipe", stderr: "pipe" }
    );
    const text = await new Response(proc.stdout).text();
    await proc.exited;
    let files = 0;
    let lines = 0;
    for (const line of text.split("\n")) {
      if (!line.trim()) continue;
      const [add, del] = line.split("\t");
      files += 1;
      const a = add === "-" ? 0 : parseInt(add, 10) || 0;
      const d = del === "-" ? 0 : parseInt(del, 10) || 0;
      lines += a + d;
    }
    return { files, lines };
  } catch {
    return null;
  }
}

/**
 * Headline entry point. Resolves the DB-derived facts and delegates to
 * `decideAutoMerge`. K3 (autopilot ticker) calls this; the optional
 * AI-review completion path may also call it to flip the merge-now bit.
 */
export async function evaluateAutoMerge(
  ctx: AutoMergeContext,
  opts: AutoMergeOptions = {}
): Promise<AutoMergeDecision> {
  // 0. Per-repo automation gate. 'auto' (the fail-open default — matching
  // pre-0106 behavior) proceeds to the normal K2 evaluation; 'suggest'
  // records the decision trail but never merges; 'off' skips entirely.
  const automation = await (opts.loadAutomationSettings ?? getAutomationSettings)(
    ctx.repositoryId
  );
  if (automation.autoMergeMode !== "auto") {
    const reason =
      automation.autoMergeMode === "off"
        ? "Auto-merge is turned off in this repository's automation settings."
        : "Auto-merge is set to 'suggest' in this repository's automation settings — evaluation recorded, merge left to a human.";
    return { merge: false, reason, blocking: [reason] };
  }

  // 1. Match the protection rule. matchProtection returns the most
  // specific rule, or null when none configured.
  const rule = await matchProtection(ctx.repositoryId, ctx.baseBranch);

  // 2. Source the AI-approval signal only if the rule actually requires
  // it. Avoids the DB hit on rules that don't care.
  const aiApproved =
    rule && rule.requireAiApproval ? await aiApprovedForPr(ctx.pullRequestId) : true;

  // 3. Human approvals — same query the manual-merge path uses.
  const humanApprovalCount = await countHumanApprovals(ctx.pullRequestId);

  // 4. Required-checks matrix. Skip the DB hit if the rule has no
  // matched required checks.
  let requiredCheckNames: string[] = [];
  let passing: string[] = [];
  if (rule) {
    try {
      const required = await listRequiredChecks(rule.id);
      requiredCheckNames = required.map((r) => r.checkName);
      if (requiredCheckNames.length > 0) {
        // We don't have the head SHA in the context. Passing
        // commitSha=null causes passingCheckNames to scan the most
        // recent 200 rows for the repo, which is good enough for the
        // K2 decision surface — the K3 ticker is the source of truth
        // for fresh status. This matches the spirit of the manual path
        // (which uses the freshly-resolved head SHA).
        passing = await passingCheckNames(ctx.repositoryId, null);
      }
    } catch {
      requiredCheckNames = [];
      passing = [];
    }
  }

  // 5. hasFailedGates: derived from required checks. We don't run the
  // full `runAllGateChecks` here because that's a heavyweight side-
  // effecting call; K3 is expected to have already triggered gate runs.
  // Treat "any required check is not in the passing set" as failing.
  const hasFailedGates =
    requiredCheckNames.length > 0 &&
    requiredCheckNames.some((n) => !passing.includes(n));

  // 6. Optional size cap.
  let diffStats: { files: number; lines: number } | null = null;
  const hasCap =
    typeof opts.maxChangedFiles === "number" ||
    typeof opts.maxChangedLines === "number";
  if (hasCap && opts.ownerName && opts.repoName && opts.headBranch) {
    diffStats = await diffStatsForBranches(
      opts.ownerName,
      opts.repoName,
      ctx.baseBranch,
      opts.headBranch
    );
  }

  // M3 — best-effort cached risk lookup. Never throws; missing risk
  // simply means the decision falls through to the existing gates.
  let riskForDecision:
    | { score: number; band: "low" | "medium" | "high" | "critical" }
    | null = null;
  try {
    const cached = await getLatestCachedPrRisk(ctx.pullRequestId);
    if (cached) {
      riskForDecision = { score: cached.score, band: cached.band };
    }
  } catch {
    riskForDecision = null;
  }

  return decideAutoMerge({
    rule,
    isDraft: ctx.isDraft,
    aiApproved,
    humanApprovalCount,
    hasFailedGates,
    passingCheckNames: passing,
    requiredCheckNames,
    diffStats,
    caps: hasCap
      ? {
          maxChangedFiles: opts.maxChangedFiles,
          maxChangedLines: opts.maxChangedLines,
        }
      : undefined,
    risk: riskForDecision,
  });
}

// ---------------------------------------------------------------------------
// Audit helper
// ---------------------------------------------------------------------------

/**
 * Record an auto-merge attempt in the audit log. K3 should call this
 * once per evaluation tick so operators can see the decision trail.
 * Uses `auto_merge.evaluated` for any decision, and `auto_merge.merged`
 * separately when K3 actually performs the merge (K3's responsibility).
 */
export async function recordAutoMergeAttempt(
  repositoryId: string,
  pullRequestId: string,
  decision: AutoMergeDecision
): Promise<void> {
  await audit({
    repositoryId,
    action: "auto_merge.evaluated",
    targetType: "pull_request",
    targetId: pullRequestId,
    metadata: {
      merge: decision.merge,
      reason: decision.reason,
      blocking: decision.blocking ?? [],
    },
  });
}

// ---------------------------------------------------------------------------
// R3 — Fast-lane auto-merge (PR-create / PR-head-update event path)
// ---------------------------------------------------------------------------

/** Stable marker for the auto-merge success comment (shared with autopilot). */
const FAST_LANE_AUTO_MERGE_MARKER = "<!-- gluecron:auto-merge:v1 -->";

/** Default poll interval (ms) while waiting for the AI-review summary comment. */
const FAST_LANE_DEFAULT_POLL_MS = 5_000;

/** Default ceiling (ms) on the AI-review wait. */
const FAST_LANE_DEFAULT_WAIT_MS = 60_000;

/**
 * PR + repo facts the fast-lane needs to drive `performMerge`. Resolved
 * once at the top of `tryAutoMergeNow` so each side-effect handler shares
 * the same snapshot.
 */
interface FastLaneContext {
  prId: string;
  prNumber: number;
  prTitle: string;
  prBody: string | null;
  baseBranch: string;
  headBranch: string;
  isDraft: boolean;
  repositoryId: string;
  authorUserId: string;
  ownerUsername: string;
  repoName: string;
  state: string;
}

export interface TryAutoMergeNowDeps {
  /** Inject the PR-context loader. */
  loadContext?: (prId: string) => Promise<FastLaneContext | null>;
  /** Inject the AI-review comment existence probe. */
  hasAiReviewComment?: (prId: string) => Promise<boolean>;
  /** Inject the decision evaluator. */
  evaluate?: (ctx: AutoMergeContext) => Promise<AutoMergeDecision>;
  /** Inject the merge executor. */
  merge?: (ctx: FastLaneContext) => Promise<PerformMergeResult>;
  /** Inject the audit-log helper for the `auto_merge.evaluated` row. */
  recordAttempt?: (
    repositoryId: string,
    pullRequestId: string,
    decision: AutoMergeDecision
  ) => Promise<void>;
  /** Inject the success-path side-effect (audit + marker comment). */
  onMerged?: (ctx: FastLaneContext, result: PerformMergeResult) => Promise<void>;
  /** Inject sleep so tests don't actually wait 60s. */
  sleep?: (ms: number) => Promise<void>;
}

export interface TryAutoMergeNowOptions {
  /** Inject for tests. */
  now?: Date;
  /**
   * Skip the AI-review polling and decide based on whatever comments
   * exist right now. Default false — we wait briefly for the AI
   * review to land.
   */
  skipAiReviewWait?: boolean;
  /**
   * Max time (ms) to wait for an AI-review comment to appear before
   * giving up. Default 60_000 (60s). The autopilot 5-min sweep is the
   * safety net if the AI review takes longer.
   */
  waitForAiReviewMs?: number;
  /** Poll cadence while waiting; default 5_000 ms. Mostly for tests. */
  aiReviewPollIntervalMs?: number;
  /** DI seam for tests. Pass through to the inner helpers. */
  deps?: TryAutoMergeNowDeps;
}

/** Default PR-context loader — single join across pr/repo/user. */
async function defaultLoadFastLaneContext(
  prId: string
): Promise<FastLaneContext | null> {
  try {
    const [row] = await db
      .select({
        prId: pullRequests.id,
        prNumber: pullRequests.number,
        prTitle: pullRequests.title,
        prBody: pullRequests.body,
        baseBranch: pullRequests.baseBranch,
        headBranch: pullRequests.headBranch,
        isDraft: pullRequests.isDraft,
        repositoryId: pullRequests.repositoryId,
        authorUserId: pullRequests.authorId,
        ownerUsername: users.username,
        repoName: repositories.name,
        state: pullRequests.state,
      })
      .from(pullRequests)
      .innerJoin(repositories, eq(repositories.id, pullRequests.repositoryId))
      .innerJoin(users, eq(users.id, repositories.ownerId))
      .where(eq(pullRequests.id, prId))
      .limit(1);
    if (!row) return null;
    return {
      prId: row.prId,
      prNumber: row.prNumber,
      prTitle: row.prTitle,
      prBody: row.prBody,
      baseBranch: row.baseBranch,
      headBranch: row.headBranch,
      isDraft: row.isDraft,
      repositoryId: row.repositoryId,
      authorUserId: row.authorUserId,
      ownerUsername: row.ownerUsername,
      repoName: row.repoName,
      state: row.state,
    };
  } catch (err) {
    console.error("[auto-merge:fast-lane] loadContext failed:", err);
    return null;
  }
}

/**
 * Default AI-review comment probe. Returns true when at least one
 * marker-bearing AI-review comment exists for the PR.
 */
async function defaultHasAiReviewComment(prId: string): Promise<boolean> {
  try {
    const rows = await db
      .select({ body: prComments.body })
      .from(prComments)
      .where(
        and(
          eq(prComments.pullRequestId, prId),
          eq(prComments.isAiReview, true)
        )
      );
    return rows.some((r) => (r.body || "").includes(AI_REVIEW_MARKER));
  } catch {
    return false;
  }
}

/**
 * Default success-path: same shape as the K3 sweep's `defaultOnMerged`
 * (auto_merge.merged audit row + marker comment). Both side-effects are
 * best-effort; failures are logged, not thrown.
 */
async function defaultFastLaneOnMerged(
  ctx: FastLaneContext,
  result: PerformMergeResult
): Promise<void> {
  try {
    await audit({
      repositoryId: ctx.repositoryId,
      action: "auto_merge.merged",
      targetType: "pull_request",
      targetId: ctx.prId,
      metadata: {
        prNumber: ctx.prNumber,
        baseBranch: ctx.baseBranch,
        headBranch: ctx.headBranch,
        closedIssueNumbers: result.closedIssueNumbers,
        resolvedFiles: result.resolvedFiles,
        fastLane: true,
      },
    });
  } catch (err) {
    console.error("[auto-merge:fast-lane] merged audit failed:", err);
  }
  try {
    await db.insert(prComments).values({
      pullRequestId: ctx.prId,
      authorId: ctx.authorUserId,
      isAiReview: true,
      body: `${FAST_LANE_AUTO_MERGE_MARKER}\nAuto-merged by Gluecron autopilot — branch protection conditions satisfied.`,
    });
  } catch (err) {
    console.error("[auto-merge:fast-lane] comment insert failed:", err);
  }
}

/** Default `performMerge` wrapper that maps a FastLaneContext to the args shape. */
async function defaultFastLaneMerge(
  ctx: FastLaneContext
): Promise<PerformMergeResult> {
  return performMerge({
    pr: {
      id: ctx.prId,
      number: ctx.prNumber,
      title: ctx.prTitle,
      body: ctx.prBody,
      baseBranch: ctx.baseBranch,
      headBranch: ctx.headBranch,
      repositoryId: ctx.repositoryId,
      authorId: ctx.authorUserId,
      state: ctx.state as "open",
      isDraft: ctx.isDraft,
    },
    ownerName: ctx.ownerUsername,
    repoName: ctx.repoName,
    actorUserId: ctx.authorUserId,
  });
}

/**
 * Fast-lane auto-merge evaluator. Fired from PR-create (or PR-head-update)
 * events. Evaluates auto-merge immediately and, when the decision is
 * `merge: true`, performs the merge via the shared `performMerge()` path.
 * Otherwise records the evaluation audit and exits — the 5-min autopilot
 * sweep is the safety net.
 *
 * Always fire-and-forget from the caller. Never throws — every side
 * effect is wrapped in try/catch and logged.
 */
export async function tryAutoMergeNow(
  pullRequestId: string,
  opts: TryAutoMergeNowOptions = {}
): Promise<void> {
  const deps = opts.deps ?? {};
  const loadContext = deps.loadContext ?? defaultLoadFastLaneContext;
  const hasAiReviewComment =
    deps.hasAiReviewComment ?? defaultHasAiReviewComment;
  const evaluate =
    deps.evaluate ?? ((ctx: AutoMergeContext) => evaluateAutoMerge(ctx, {}));
  const merge = deps.merge ?? defaultFastLaneMerge;
  const recordAttempt = deps.recordAttempt ?? recordAutoMergeAttempt;
  const onMerged = deps.onMerged ?? defaultFastLaneOnMerged;
  const sleep =
    deps.sleep ?? ((ms: number) => new Promise<void>((r) => setTimeout(r, ms)));

  try {
    // 1. Load the PR + repo facts. Abort cleanly if the PR is gone.
    let ctx: FastLaneContext | null = null;
    try {
      ctx = await loadContext(pullRequestId);
    } catch (err) {
      console.error("[auto-merge:fast-lane] loadContext threw:", err);
      return;
    }
    if (!ctx) return;

    // 2. Wait briefly for the AI-review summary comment. The 5-min
    // sweep handles the case where the AI takes longer than the cap.
    // Even when waitMs is 0 we do ONE probe — "wait for it" semantics
    // require *presence*, not just patience; the sweep is the safety net
    // when the AI hasn't landed yet.
    const waitMs = opts.waitForAiReviewMs ?? FAST_LANE_DEFAULT_WAIT_MS;
    const pollMs = opts.aiReviewPollIntervalMs ?? FAST_LANE_DEFAULT_POLL_MS;
    if (!opts.skipAiReviewWait) {
      const deadline = Date.now() + Math.max(0, waitMs);
      // Poll cadence: check once, then every pollMs until the deadline.
      while (true) {
        let present = false;
        try {
          present = await hasAiReviewComment(pullRequestId);
        } catch {
          present = false;
        }
        if (present) break;
        if (Date.now() >= deadline) return; // give up; sweep is the safety net
        try {
          await sleep(pollMs);
        } catch {
          return;
        }
      }
    }

    // 3. Evaluate the auto-merge decision via the shared K2 helper.
    let decision: AutoMergeDecision;
    try {
      decision = await evaluate({
        pullRequestId: ctx.prId,
        repositoryId: ctx.repositoryId,
        baseBranch: ctx.baseBranch,
        isDraft: ctx.isDraft,
        authorUserId: ctx.authorUserId,
      });
    } catch (err) {
      console.error("[auto-merge:fast-lane] evaluate threw:", err);
      // Still record the evaluation as best-effort so the paper trail exists.
      try {
        await recordAttempt(ctx.repositoryId, ctx.prId, {
          merge: false,
          reason: `evaluate threw: ${err instanceof Error ? err.message : String(err)}`,
          blocking: ["evaluator error"],
        });
      } catch {
        /* swallow */
      }
      return;
    }

    // 4. Always record the evaluation, regardless of outcome.
    try {
      await recordAttempt(ctx.repositoryId, ctx.prId, decision);
    } catch (err) {
      console.error("[auto-merge:fast-lane] recordAttempt failed:", err);
    }

    if (!decision.merge) return;

    // 5. Perform the merge via the shared executor. On success, fire the
    // audit + marker comment side-effects (same shape as the K3 sweep).
    try {
      const result = await merge(ctx);
      if (result.ok) {
        try {
          await onMerged(ctx, result);
        } catch (err) {
          console.error("[auto-merge:fast-lane] onMerged threw:", err);
        }
      } else {
        console.error(
          `[auto-merge:fast-lane] performMerge failed for pr=${ctx.prId}: ${result.error}`
        );
      }
    } catch (err) {
      console.error("[auto-merge:fast-lane] performMerge threw:", err);
    }
  } catch (err) {
    // Belt-and-braces: nothing above should throw, but the contract is
    // "fire-and-forget, never throws". Final catch swallows + logs.
    console.error("[auto-merge:fast-lane] unexpected error:", err);
  }
}

// ---------------------------------------------------------------------------
// Test-only surface
// ---------------------------------------------------------------------------

export const __test = {
  decideAutoMerge,
  aiCommentLooksApproved,
  diffStatsForBranches,
  aiApprovedForPr,
  defaultLoadFastLaneContext,
  defaultHasAiReviewComment,
  defaultFastLaneOnMerged,
  defaultFastLaneMerge,
  FAST_LANE_AUTO_MERGE_MARKER,
  FAST_LANE_DEFAULT_POLL_MS,
  FAST_LANE_DEFAULT_WAIT_MS,
};