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
multi-repo-refactor.ts34.1 KB · 1183 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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
/**
 * Multi-repo refactor agent.
 *
 * User issues one English request — "rename `getUserById` to `findUser`" —
 * and the agent fans the change out across every repository the user owns,
 * opening one coordinated AI-authored PR per affected repo. All PRs share
 * a marker label `multi-repo:refactor:<refactorId>` so the UI can group
 * them as a single logical change.
 *
 * Lifecycle (mirrored in the DB):
 *
 *   planRefactor      — Claude reads the description, walks the user's
 *                        repos, returns a per-repo plan. Status flips
 *                        `planning` → `building` once executeRefactor
 *                        starts. Skipped repos never make it into the
 *                        `multi_repo_refactor_prs` table at all.
 *
 *   executeRefactor   — for every planned repo we ask Claude for a
 *                        concrete edit (same prompt shape as
 *                        `ai-patch-generator` / `spec-to-pr`), write the
 *                        files onto a fresh branch via the existing git
 *                        plumbing helpers, and insert a draft `pull_requests`
 *                        row. Per-repo failures flip just that child to
 *                        `failed`; the parent stays in `building` until
 *                        every child has terminated.
 *
 *   coordinated merge — when every child is `opened` (and the caller has
 *                        confirmed each PR is green + approved), invoke
 *                        `mergeRefactor`. It walks children in insertion
 *                        order and updates PR rows to `merged`. Any
 *                        failure stops the cascade — the rest stay open.
 *
 * Design notes:
 *
 *   - We never throw. Every step is funnelled through `{ ok, ... } | { ok:
 *     false, error }`. This keeps callers (the API route, the UI form
 *     handler, the autopilot loop) free of try/catch boilerplate.
 *
 *   - The Anthropic client is injectable so the test-suite can pin
 *     deterministic plans without an API key. Production callers leave
 *     `client` undefined and we lazy-resolve via `ai-client.getAnthropic`.
 *
 *   - Sequencing matters for the merge step but NOT for the build step.
 *     We open all PRs in parallel; the user's intent is one logical
 *     atomic change, but git itself can't atomic-merge across repos.
 */

import { and, desc, eq } from "drizzle-orm";
import type Anthropic from "@anthropic-ai/sdk";
import { db } from "../db";
import {
  labels,
  multiRepoRefactorPrs,
  multiRepoRefactors,
  prComments,
  pullRequests,
  repositories,
  users,
  type MultiRepoRefactor,
  type MultiRepoRefactorPr,
} from "../db/schema";
import {
  createOrUpdateFileOnBranch,
  getBlob,
  refExists,
  resolveRef,
  updateRef,
} from "../git/repository";
import { config } from "./config";
import { audit } from "./notify";
import {
  extractText,
  getAnthropic,
  MODEL_SONNET,
  parseJsonResponse,
} from "./ai-client";

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

/** Marker prefix every multi-repo PR is tagged with. */
export const MULTI_REPO_REFACTOR_LABEL_PREFIX = "multi-repo:refactor:";

/** Marker baked into PR bodies so other tooling can recognise them. */
export const MULTI_REPO_REFACTOR_MARKER =
  "<!-- gluecron:multi-repo-refactor:v1 -->";

export type RefactorStatus =
  | "planning"
  | "building"
  | "ready_for_review"
  | "merged"
  | "failed";

export type RefactorPrStatus =
  | "pending"
  | "building"
  | "opened"
  | "failed";

/**
 * Optional Anthropic client override — primarily for tests. We accept the
 * narrow `Pick<Anthropic, "messages">` shape used elsewhere in the codebase
 * (`ai-patch-generator`) so test fakes can implement `.messages.create`
 * without committing to the whole SDK surface.
 */
export type ClaudeClient = Pick<Anthropic, "messages">;

export interface RefactorRepoPlan {
  repositoryId: string;
  /** Owner namespace + repo name. Pre-resolved so the UI doesn't re-query. */
  owner: string;
  name: string;
  /** Claude's short prediction of what it will change in this repo. */
  predicted_changes_summary: string;
}

export interface PlanRefactorArgs {
  userId: string;
  description: string;
  /** Optional explicit repo list. When omitted we walk every repo the user owns. */
  repositoryIds?: string[];
  /** Test-only override for the Anthropic client. */
  client?: ClaudeClient;
  /** Test-only override for the title — production derives one from the description. */
  titleOverride?: string;
}

export type PlanRefactorResult =
  | {
      ok: true;
      refactor: MultiRepoRefactor;
      plan: RefactorRepoPlan[];
    }
  | { ok: false; error: string };

export interface ExecuteRefactorArgs {
  refactorId: string;
  /** Test-only override for the Anthropic client. */
  client?: ClaudeClient;
}

export interface ExecuteRefactorChildResult {
  repositoryId: string;
  status: RefactorPrStatus;
  pullRequestId?: string;
  prNumber?: number;
  branch?: string;
  error?: string;
}

export type ExecuteRefactorResult =
  | {
      ok: true;
      refactor: MultiRepoRefactor;
      children: ExecuteRefactorChildResult[];
    }
  | { ok: false; error: string };

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

/** Label name used to mark every PR in the refactor group. */
export function refactorLabelName(refactorId: string): string {
  return `${MULTI_REPO_REFACTOR_LABEL_PREFIX}${refactorId}`;
}

/** Derive a sentence-case title from the user's free-text description. */
export function deriveTitle(description: string): string {
  const trimmed = (description || "").trim();
  if (!trimmed) return "Multi-repo refactor";
  // Take the first line, cap at ~80 chars.
  const firstLine = trimmed.split(/\r?\n/, 1)[0] || trimmed;
  const capped = firstLine.length > 80 ? `${firstLine.slice(0, 77)}...` : firstLine;
  return capped;
}

/**
 * Derive a git-ref-safe branch name from a refactor id. Kept short so PR
 * pages stay tidy in the branch column.
 */
export function refactorBranchName(refactorId: string): string {
  const short = refactorId.replace(/-/g, "").slice(0, 8);
  return `multi-repo-refactor/${short}`;
}

/**
 * Build the planning prompt. Pure so tests can pin the shape without an
 * API key.
 */
export function buildPlanPrompt(args: {
  description: string;
  repos: Array<{ id: string; owner: string; name: string; description: string | null }>;
}): string {
  const repoLines = args.repos
    .map(
      (r) =>
        `- id=${r.id} | ${r.owner}/${r.name}${r.description ? ` — ${r.description}` : ""}`
    )
    .join("\n");
  return [
    "You are a refactoring planner for a multi-repository code change.",
    "The user has issued ONE English request. Decide which of the listed",
    "repositories are actually affected and, for each, write a one-line",
    "prediction of the concrete change the editor agent should make.",
    "",
    "User request:",
    args.description,
    "",
    "Repositories the user owns:",
    repoLines || "(none)",
    "",
    "Respond ONLY with JSON of this exact shape:",
    "{",
    '  "title": "short title for the whole refactor (max 80 chars)",',
    '  "affected": [',
    '    { "repository_id": "<uuid>", "predicted_changes_summary": "1-sentence plan" }',
    "  ]",
    "}",
    "",
    "Rules:",
    "- Only include repos that are actually affected. Omit obvious no-ops.",
    "- predicted_changes_summary must be concrete (e.g. `rename getUserById to findUser in src/lib/user.ts`).",
    "- If no repo is affected, return an empty `affected` array.",
  ].join("\n");
}

/**
 * Build the per-repo edit prompt. Asks Claude for an end-state set of
 * files, exactly like `ai-patch-generator` does.
 */
export function buildEditPrompt(args: {
  description: string;
  predictedChanges: string;
  repoFiles: Array<{ path: string; content: string }>;
}): string {
  const fileBlocks = args.repoFiles
    .map(
      (f) =>
        `--- FILE: ${f.path} ---\n\`\`\`\n${f.content}\n\`\`\`\n--- END FILE ---`
    )
    .join("\n\n");
  return [
    "You are implementing one slice of a multi-repository refactor.",
    "",
    "Overall refactor request:",
    args.description,
    "",
    "Predicted change for THIS repository:",
    args.predictedChanges,
    "",
    "Current contents of the files you may modify:",
    fileBlocks || "(no files were pre-loaded)",
    "",
    "Respond ONLY with JSON of this exact shape:",
    "{",
    '  "explanation": "1-3 sentence summary of what you changed",',
    '  "patches": [',
    '    { "path": "same/path/as/above", "new_content": "FULL replacement file contents" }',
    "  ]",
    "}",
    "",
    "Rules:",
    "- Return [] (empty patches) if there is genuinely nothing to do.",
    "- new_content MUST be the entire file, not a diff.",
    "- Do not invent new files — only touch files you've been shown.",
    "- Preserve existing formatting / indentation / trailing newlines.",
  ].join("\n");
}

interface ClaudePlanResponse {
  title?: string;
  affected?: Array<{
    repository_id?: string;
    predicted_changes_summary?: string;
  }>;
}

interface ClaudePatch {
  path: string;
  new_content: string;
}

interface ClaudeEditResponse {
  explanation?: string;
  patches?: ClaudePatch[];
}

// ---------------------------------------------------------------------------
// Internals — DB + git glue
// ---------------------------------------------------------------------------

/**
 * Resolve a `ClaudeClient` for the call. Returns null when no API key is
 * configured and the caller didn't inject one — signals to bail before any
 * DB writes.
 */
function resolveClient(override?: ClaudeClient): ClaudeClient | null {
  if (override) return override;
  if (!config.anthropicApiKey) return null;
  try {
    return getAnthropic();
  } catch {
    return null;
  }
}

/**
 * Load every repo the user owns, with the joined owner username. Used by
 * `planRefactor` when the caller didn't pass an explicit list.
 */
async function listUserRepos(
  userId: string,
  filterIds?: string[]
): Promise<Array<{ id: string; owner: string; name: string; description: string | null; defaultBranch: string }>> {
  try {
    const rows = await db
      .select({
        id: repositories.id,
        owner: users.username,
        name: repositories.name,
        description: repositories.description,
        defaultBranch: repositories.defaultBranch,
      })
      .from(repositories)
      .innerJoin(users, eq(users.id, repositories.ownerId))
      .where(eq(repositories.ownerId, userId));
    if (!filterIds || filterIds.length === 0) return rows;
    const filterSet = new Set(filterIds);
    return rows.filter((r) => filterSet.has(r.id));
  } catch (err) {
    console.warn(
      "[multi-repo-refactor] listUserRepos failed:",
      err instanceof Error ? err.message : err
    );
    return [];
  }
}

/**
 * Ensure the group-marker label exists on a repo. Best-effort — the label
 * is a UX nicety surfaced in PR bodies, not load-bearing.
 */
async function ensureGroupLabel(
  repositoryId: string,
  refactorId: string
): Promise<void> {
  try {
    await db
      .insert(labels)
      .values({
        repositoryId,
        name: refactorLabelName(refactorId),
        color: "#5b6ee8",
        description: `Member of multi-repo refactor ${refactorId}`,
      })
      .onConflictDoNothing?.();
  } catch (err) {
    console.warn(
      "[multi-repo-refactor] ensureGroupLabel failed:",
      err instanceof Error ? err.message : err
    );
  }
}

/**
 * Ask Claude to plan the refactor. Returns null on any failure (network,
 * parse, missing key).
 */
async function askClaudeForPlan(
  client: ClaudeClient,
  description: string,
  repos: Array<{ id: string; owner: string; name: string; description: string | null }>
): Promise<ClaudePlanResponse | null> {
  try {
    const message = await client.messages.create({
      model: MODEL_SONNET,
      max_tokens: 4096,
      messages: [
        {
          role: "user",
          content: buildPlanPrompt({ description, repos }),
        },
      ],
    });
    try {
      const { recordAiCost, extractUsage } = await import("./ai-cost-tracker");
      const usage = extractUsage(message);
      await recordAiCost({
        model: MODEL_SONNET,
        inputTokens: usage.input,
        outputTokens: usage.output,
        category: "refactor",
        sourceKind: "multi_repo_plan",
      });
    } catch {
      /* swallow — best-effort */
    }
    const text = extractText(message);
    return parseJsonResponse<ClaudePlanResponse>(text);
  } catch (err) {
    console.warn(
      "[multi-repo-refactor] plan call failed:",
      err instanceof Error ? err.message : err
    );
    return null;
  }
}

/**
 * Ask Claude for the per-repo edit. Same shape as `ai-patch-generator`.
 */
async function askClaudeForEdit(
  client: ClaudeClient,
  description: string,
  predictedChanges: string,
  repoFiles: Array<{ path: string; content: string }>
): Promise<ClaudeEditResponse | null> {
  try {
    const message = await client.messages.create({
      model: MODEL_SONNET,
      max_tokens: 4096,
      messages: [
        {
          role: "user",
          content: buildEditPrompt({
            description,
            predictedChanges,
            repoFiles,
          }),
        },
      ],
    });
    try {
      const { recordAiCost, extractUsage } = await import("./ai-cost-tracker");
      const usage = extractUsage(message);
      await recordAiCost({
        model: MODEL_SONNET,
        inputTokens: usage.input,
        outputTokens: usage.output,
        category: "refactor",
        sourceKind: "multi_repo_edit",
      });
    } catch {
      /* swallow — best-effort */
    }
    const text = extractText(message);
    return parseJsonResponse<ClaudeEditResponse>(text);
  } catch (err) {
    console.warn(
      "[multi-repo-refactor] edit call failed:",
      err instanceof Error ? err.message : err
    );
    return null;
  }
}

/**
 * Resolve repository row + owner username for use by the per-repo executor.
 */
async function loadRepoForExecute(
  repositoryId: string
): Promise<
  | {
      id: string;
      owner: string;
      ownerId: string;
      name: string;
      defaultBranch: string;
    }
  | null
> {
  try {
    const [row] = await db
      .select({
        id: repositories.id,
        owner: users.username,
        ownerId: repositories.ownerId,
        name: repositories.name,
        defaultBranch: repositories.defaultBranch,
      })
      .from(repositories)
      .innerJoin(users, eq(users.id, repositories.ownerId))
      .where(eq(repositories.id, repositoryId))
      .limit(1);
    return row || null;
  } catch {
    return null;
  }
}

/**
 * Render the PR body for a child PR. Pure helper exported for tests.
 */
export function renderRefactorPrBody(args: {
  refactorId: string;
  refactorTitle: string;
  description: string;
  predictedChanges: string;
  explanation: string;
  patchPaths: string[];
}): string {
  const label = refactorLabelName(args.refactorId);
  const quoted = args.description
    .split("\n")
    .map((l) => `> ${l}`)
    .join("\n");
  const files = args.patchPaths.map((p) => `- \`${p}\``).join("\n");
  return [
    MULTI_REPO_REFACTOR_MARKER,
    `## ${args.refactorTitle}`,
    "",
    "_Part of a multi-repo refactor. Every PR in this group shares the",
    `label \`${label}\` so they can be reviewed and merged together._`,
    "",
    "### Original request",
    quoted,
    "",
    "### Predicted change for this repo",
    args.predictedChanges,
    "",
    "### What changed",
    args.explanation || "_(no explanation provided)_",
    "",
    "### Files",
    files || "_(none)_",
    "",
    "---",
    "",
    `Refactor id: \`${args.refactorId}\``,
    `Group label: \`${label}\``,
    "",
    "_Auto-generated by GlueCron multi-repo refactor agent. Review every line before merging._",
  ].join("\n");
}

/**
 * Seed a fresh branch at the repo's default-branch HEAD. Mirrors the
 * `ai-patch-generator` helper but takes the branch HEAD instead of an
 * explicit base sha because the planner doesn't track per-repo commit
 * shas.
 */
async function seedBranchFromHead(
  owner: string,
  name: string,
  branch: string,
  defaultBranch: string
): Promise<{ ok: true; baseSha: string } | { ok: false; error: string }> {
  let baseSha: string | null;
  try {
    baseSha = await resolveRef(owner, name, defaultBranch);
  } catch (err) {
    return {
      ok: false,
      error: `resolveRef failed: ${err instanceof Error ? err.message : err}`,
    };
  }
  if (!baseSha) {
    return { ok: false, error: "repo has no commits on default branch" };
  }
  const fullRef = `refs/heads/${branch}`;
  if (await refExists(owner, name, fullRef)) {
    return { ok: true, baseSha };
  }
  const ok = await updateRef(owner, name, fullRef, baseSha);
  if (!ok) return { ok: false, error: "updateRef failed" };
  return { ok: true, baseSha };
}

/**
 * Update the parent refactor's status. Best-effort; failures are logged
 * but never propagate — the orchestrator already has a result to return.
 */
async function setRefactorStatus(
  refactorId: string,
  status: RefactorStatus
): Promise<void> {
  try {
    await db
      .update(multiRepoRefactors)
      .set({ status, updatedAt: new Date() })
      .where(eq(multiRepoRefactors.id, refactorId));
  } catch (err) {
    console.warn(
      "[multi-repo-refactor] setRefactorStatus failed:",
      err instanceof Error ? err.message : err
    );
  }
}

/**
 * Roll the per-child statuses up into a parent status:
 *   - any child still building/pending → parent stays `building`
 *   - every child opened              → `ready_for_review`
 *   - every child failed              → `failed`
 *   - any child failed + at least one opened → `ready_for_review`
 *     (the user can still merge what worked; failed children stay
 *     surfaced in the UI so they can be retried)
 */
export function rollupStatus(
  children: ReadonlyArray<{ status: RefactorPrStatus }>
): RefactorStatus {
  if (children.length === 0) return "failed";
  const anyInFlight = children.some(
    (c) => c.status === "pending" || c.status === "building"
  );
  if (anyInFlight) return "building";
  const opened = children.filter((c) => c.status === "opened").length;
  if (opened === 0) return "failed";
  return "ready_for_review";
}

// ---------------------------------------------------------------------------
// Public API — planRefactor
// ---------------------------------------------------------------------------

/**
 * Stage 1 of the refactor pipeline. Persists a `planning` parent row +
 * one `pending` child row per affected repo, then returns the plan so the
 * caller (UI form handler or autopilot) can confirm it before kicking off
 * `executeRefactor`.
 *
 * Never throws — failures are returned as `{ ok: false, error }`.
 */
export async function planRefactor(
  args: PlanRefactorArgs
): Promise<PlanRefactorResult> {
  const description = (args.description || "").trim();
  if (!description) return { ok: false, error: "description is empty" };

  const client = resolveClient(args.client);
  if (!client) {
    return { ok: false, error: "ANTHROPIC_API_KEY required for refactor planning" };
  }

  // Load candidate repos.
  const repos = await listUserRepos(args.userId, args.repositoryIds);
  if (repos.length === 0) {
    return { ok: false, error: "user owns no repositories to refactor" };
  }

  // Ask Claude to plan.
  const planRes = await askClaudeForPlan(client, description, repos);
  if (!planRes || !Array.isArray(planRes.affected)) {
    return { ok: false, error: "planner returned invalid response" };
  }

  // Filter the plan down to repos we actually saw (defence-in-depth — a
  // hallucinated id shouldn't be persisted).
  const repoById = new Map(repos.map((r) => [r.id, r]));
  const plan: RefactorRepoPlan[] = [];
  for (const entry of planRes.affected) {
    if (!entry || typeof entry.repository_id !== "string") continue;
    const repo = repoById.get(entry.repository_id);
    if (!repo) continue;
    const summary =
      (entry.predicted_changes_summary || "").trim() || "no summary provided";
    plan.push({
      repositoryId: repo.id,
      owner: repo.owner,
      name: repo.name,
      predicted_changes_summary: summary,
    });
  }

  if (plan.length === 0) {
    return { ok: false, error: "planner identified no affected repos" };
  }

  // Persist the parent + children. Wrap each write so a single DB failure
  // bubbles a clean error instead of a throw.
  const title =
    (args.titleOverride && args.titleOverride.trim()) ||
    (planRes.title && planRes.title.trim()) ||
    deriveTitle(description);

  let parent: MultiRepoRefactor | null = null;
  try {
    const [row] = await db
      .insert(multiRepoRefactors)
      .values({
        ownerUserId: args.userId,
        title: title.slice(0, 200),
        description,
        status: "planning",
      })
      .returning();
    parent = row || null;
  } catch (err) {
    return {
      ok: false,
      error: `parent insert failed: ${err instanceof Error ? err.message : err}`,
    };
  }
  if (!parent) return { ok: false, error: "parent insert returned no row" };

  try {
    await db.insert(multiRepoRefactorPrs).values(
      plan.map((p) => ({
        refactorId: parent!.id,
        repositoryId: p.repositoryId,
        status: "pending" as const,
      }))
    );
  } catch (err) {
    return {
      ok: false,
      error: `children insert failed: ${err instanceof Error ? err.message : err}`,
    };
  }

  await audit({
    userId: args.userId,
    action: "multi_repo_refactor.planned",
    repositoryId: null,
    metadata: {
      refactorId: parent.id,
      title,
      affectedCount: plan.length,
    },
  });

  return { ok: true, refactor: parent, plan };
}

// ---------------------------------------------------------------------------
// Public API — executeRefactor
// ---------------------------------------------------------------------------

/**
 * Stage 2 of the refactor pipeline. For every child row in `pending` we:
 *   1. flip its status to `building`,
 *   2. resolve the repo + owner,
 *   3. seed a fresh branch off the default branch's HEAD,
 *   4. ask Claude for end-state file contents,
 *   5. write each file via `createOrUpdateFileOnBranch`,
 *   6. insert the PR row + a marker comment naming the group label,
 *   7. flip the child to `opened` (or `failed` on any non-recoverable error).
 *
 * Once every child has terminated we roll the statuses up into a parent
 * status (`ready_for_review` / `failed`) and return the children list.
 */
export async function executeRefactor(
  args: ExecuteRefactorArgs
): Promise<ExecuteRefactorResult> {
  if (!args.refactorId) return { ok: false, error: "refactorId required" };

  const client = resolveClient(args.client);
  if (!client) {
    return { ok: false, error: "ANTHROPIC_API_KEY required for refactor execution" };
  }

  // Load parent.
  let parent: MultiRepoRefactor | null = null;
  try {
    const [row] = await db
      .select()
      .from(multiRepoRefactors)
      .where(eq(multiRepoRefactors.id, args.refactorId))
      .limit(1);
    parent = row || null;
  } catch {
    return { ok: false, error: "refactor lookup failed" };
  }
  if (!parent) return { ok: false, error: "refactor not found" };

  if (parent.status === "merged") {
    return { ok: false, error: "refactor already merged" };
  }

  // Load children that need work. We re-pick `pending` and `building` so a
  // crash mid-execute is recoverable.
  let children: MultiRepoRefactorPr[] = [];
  try {
    children = await db
      .select()
      .from(multiRepoRefactorPrs)
      .where(eq(multiRepoRefactorPrs.refactorId, parent.id));
  } catch {
    return { ok: false, error: "children lookup failed" };
  }

  if (children.length === 0) {
    return { ok: false, error: "refactor has no child repos" };
  }

  await setRefactorStatus(parent.id, "building");

  const results: ExecuteRefactorChildResult[] = [];
  // Sequential by default — keeps the volume of concurrent Claude calls
  // sane and makes the test output deterministic. Production callers
  // hammering this can swap to Promise.all when they're ready.
  for (const child of children) {
    if (child.status === "opened") {
      results.push({
        repositoryId: child.repositoryId,
        status: "opened",
        pullRequestId: child.pullRequestId ?? undefined,
      });
      continue;
    }

    const out = await executeChild({
      client,
      refactor: parent,
      child,
    });
    results.push(out);
  }

  // Roll the children up into a parent status.
  const rolled = rollupStatus(results);
  await setRefactorStatus(parent.id, rolled);

  // Refresh the parent row so the caller sees the new status.
  let refreshed: MultiRepoRefactor = { ...parent, status: rolled };
  try {
    const [r] = await db
      .select()
      .from(multiRepoRefactors)
      .where(eq(multiRepoRefactors.id, parent.id))
      .limit(1);
    if (r) refreshed = r;
  } catch {
    /* keep the in-memory rolled status */
  }

  return { ok: true, refactor: refreshed, children: results };
}

interface ExecuteChildArgs {
  client: ClaudeClient;
  refactor: MultiRepoRefactor;
  child: MultiRepoRefactorPr;
}

/**
 * Run the AI patch pipeline for one child row. Always returns a result —
 * failures flip the child to `failed` with an error message stored on
 * the row so the UI can surface it inline.
 */
async function executeChild(
  args: ExecuteChildArgs
): Promise<ExecuteRefactorChildResult> {
  const { client, refactor, child } = args;

  // Mark as building.
  try {
    await db
      .update(multiRepoRefactorPrs)
      .set({ status: "building", updatedAt: new Date() })
      .where(eq(multiRepoRefactorPrs.id, child.id));
  } catch {
    /* non-fatal */
  }

  const repo = await loadRepoForExecute(child.repositoryId);
  if (!repo) {
    return finaliseChildFailure(child.id, child.repositoryId, "repo not found");
  }

  // Reuse the predicted summary from the plan if the row carries it.
  // Today we don't persist the per-repo summary on the child row to keep
  // the schema lean — we recompute by re-asking Claude inside the prompt
  // (the description itself anchors the change).
  const predictedChanges =
    `Apply the multi-repo refactor described above to the \`${repo.owner}/${repo.name}\` repository.`;

  const branch = refactorBranchName(refactor.id);
  const seeded = await seedBranchFromHead(
    repo.owner,
    repo.name,
    branch,
    repo.defaultBranch || "main"
  );
  if (!seeded.ok) {
    return finaliseChildFailure(
      child.id,
      child.repositoryId,
      `seed branch failed: ${seeded.error}`
    );
  }

  // Build a tiny pre-loaded context — for the first iteration we don't
  // ship the whole repo to Claude, we let it work from the description +
  // the predicted summary. Future enhancement: feed in the semantic-index
  // top-k files for this repo. We do read README.md when present so
  // Claude has *some* concrete anchor.
  const repoFiles: Array<{ path: string; content: string }> = [];
  try {
    const readme = await getBlob(repo.owner, repo.name, seeded.baseSha, "README.md");
    if (readme && !readme.isBinary) {
      repoFiles.push({ path: "README.md", content: readme.content });
    }
  } catch {
    /* README is a nice-to-have */
  }

  const editRes = await askClaudeForEdit(
    client,
    refactor.description,
    predictedChanges,
    repoFiles
  );
  if (
    !editRes ||
    !Array.isArray(editRes.patches) ||
    editRes.patches.length === 0
  ) {
    return finaliseChildFailure(
      child.id,
      child.repositoryId,
      "AI returned no patches"
    );
  }

  // Apply patches.
  const writtenPaths: string[] = [];
  for (const patch of editRes.patches) {
    if (
      !patch ||
      typeof patch.path !== "string" ||
      typeof patch.new_content !== "string"
    ) {
      continue;
    }
    const res = await createOrUpdateFileOnBranch({
      owner: repo.owner,
      name: repo.name,
      branch,
      filePath: patch.path,
      bytes: new TextEncoder().encode(patch.new_content),
      message: `refactor(multi-repo): ${patch.path}`,
      authorName: "GlueCron Multi-Repo Refactor",
      authorEmail: "refactor@gluecron.com",
    });
    if ("error" in res) {
      return finaliseChildFailure(
        child.id,
        child.repositoryId,
        `write failed: ${res.error}`
      );
    }
    writtenPaths.push(patch.path);
  }

  if (writtenPaths.length === 0) {
    return finaliseChildFailure(
      child.id,
      child.repositoryId,
      "no patches written"
    );
  }

  // Ensure the group label row exists on this repo, then open the PR.
  await ensureGroupLabel(repo.id, refactor.id);

  const body = renderRefactorPrBody({
    refactorId: refactor.id,
    refactorTitle: refactor.title,
    description: refactor.description,
    predictedChanges,
    explanation: editRes.explanation || "",
    patchPaths: writtenPaths,
  });
  const title = `[refactor] ${refactor.title}`.slice(0, 200);
  const baseBranch = repo.defaultBranch || "main";

  let prId: string | null = null;
  let prNumber: number | null = null;
  try {
    const [pr] = await db
      .insert(pullRequests)
      .values({
        repositoryId: repo.id,
        authorId: repo.ownerId,
        title,
        body,
        baseBranch,
        headBranch: branch,
        isDraft: false,
      })
      .returning({ id: pullRequests.id, number: pullRequests.number });
    if (pr) {
      prId = pr.id;
      prNumber = pr.number;
    }
  } catch (err) {
    return finaliseChildFailure(
      child.id,
      child.repositoryId,
      `PR insert failed: ${err instanceof Error ? err.message : err}`
    );
  }

  if (!prId || prNumber == null) {
    return finaliseChildFailure(
      child.id,
      child.repositoryId,
      "PR insert returned no row"
    );
  }

  // Drop a marker comment so the label is discoverable without a join table.
  try {
    await db.insert(prComments).values({
      pullRequestId: prId,
      authorId: repo.ownerId,
      isAiReview: true,
      body: `${MULTI_REPO_REFACTOR_MARKER}\nApplied label: \`${refactorLabelName(refactor.id)}\``,
    });
  } catch {
    /* best-effort */
  }

  // Mark the child as opened.
  try {
    await db
      .update(multiRepoRefactorPrs)
      .set({
        status: "opened",
        pullRequestId: prId,
        errorMessage: null,
        updatedAt: new Date(),
      })
      .where(eq(multiRepoRefactorPrs.id, child.id));
  } catch {
    /* the in-memory result still reflects success */
  }

  await audit({
    userId: refactor.ownerUserId,
    action: "multi_repo_refactor.pr_opened",
    repositoryId: repo.id,
    metadata: {
      refactorId: refactor.id,
      prNumber,
      branch,
    },
  });

  return {
    repositoryId: repo.id,
    status: "opened",
    pullRequestId: prId,
    prNumber,
    branch,
  };
}

async function finaliseChildFailure(
  childId: string,
  repositoryId: string,
  error: string
): Promise<ExecuteRefactorChildResult> {
  try {
    await db
      .update(multiRepoRefactorPrs)
      .set({
        status: "failed",
        errorMessage: error.slice(0, 1000),
        updatedAt: new Date(),
      })
      .where(eq(multiRepoRefactorPrs.id, childId));
  } catch {
    /* non-fatal — we still return the error to the caller */
  }
  return { repositoryId, status: "failed", error };
}

// ---------------------------------------------------------------------------
// Public API — read helpers
// ---------------------------------------------------------------------------

export interface GetRefactorResult {
  refactor: MultiRepoRefactor;
  children: Array<
    MultiRepoRefactorPr & {
      repoOwner: string | null;
      repoName: string | null;
      prNumber: number | null;
    }
  >;
}

/**
 * Load a refactor + its children, joined with the repo names and PR
 * numbers needed by the UI table.
 */
export async function getRefactor(
  refactorId: string,
  opts: { userId?: string } = {}
): Promise<GetRefactorResult | null> {
  let parent: MultiRepoRefactor | null = null;
  try {
    const conditions = opts.userId
      ? and(
          eq(multiRepoRefactors.id, refactorId),
          eq(multiRepoRefactors.ownerUserId, opts.userId)
        )
      : eq(multiRepoRefactors.id, refactorId);
    const [row] = await db
      .select()
      .from(multiRepoRefactors)
      .where(conditions)
      .limit(1);
    parent = row || null;
  } catch {
    return null;
  }
  if (!parent) return null;

  let children: GetRefactorResult["children"] = [];
  try {
    const rows = await db
      .select({
        id: multiRepoRefactorPrs.id,
        refactorId: multiRepoRefactorPrs.refactorId,
        repositoryId: multiRepoRefactorPrs.repositoryId,
        pullRequestId: multiRepoRefactorPrs.pullRequestId,
        status: multiRepoRefactorPrs.status,
        errorMessage: multiRepoRefactorPrs.errorMessage,
        createdAt: multiRepoRefactorPrs.createdAt,
        updatedAt: multiRepoRefactorPrs.updatedAt,
        repoOwner: users.username,
        repoName: repositories.name,
        prNumber: pullRequests.number,
      })
      .from(multiRepoRefactorPrs)
      .leftJoin(
        repositories,
        eq(multiRepoRefactorPrs.repositoryId, repositories.id)
      )
      .leftJoin(users, eq(repositories.ownerId, users.id))
      .leftJoin(
        pullRequests,
        eq(multiRepoRefactorPrs.pullRequestId, pullRequests.id)
      )
      .where(eq(multiRepoRefactorPrs.refactorId, refactorId));
    children = rows as GetRefactorResult["children"];
  } catch {
    children = [];
  }

  return { refactor: parent, children };
}

/** List refactors a user owns, newest first. UI-facing. */
export async function listRefactorsForUser(
  userId: string,
  limit = 50
): Promise<MultiRepoRefactor[]> {
  try {
    return await db
      .select()
      .from(multiRepoRefactors)
      .where(eq(multiRepoRefactors.ownerUserId, userId))
      .orderBy(desc(multiRepoRefactors.createdAt))
      .limit(limit);
  } catch {
    return [];
  }
}

// ---------------------------------------------------------------------------
// Test-only re-exports
// ---------------------------------------------------------------------------

export const __test = {
  buildPlanPrompt,
  buildEditPrompt,
  rollupStatus,
  refactorLabelName,
  refactorBranchName,
  deriveTitle,
  renderRefactorPrBody,
  listUserRepos,
  ensureGroupLabel,
  seedBranchFromHead,
  setRefactorStatus,
};