Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
claude/adoring-hopper-5x74bqclaude/affectionate-feynman-ykrf1hclaude/architecture-audit-design-wxprenclaude/build-status-update-3MXsfclaude/charming-meitner-mllb5rclaude/compare-gate-gluecron-s4mFQclaude/confident-faraday-tikcwbclaude/continue-work-XMTlIclaude/crontech-gluecron-deploy-7MIECclaude/crontech-platform-setup-SeKfwclaude/design-2026claude/ecstatic-ptolemy-jMdigclaude/enhance-github-integration-QNHdGclaude/fix-aa-loop-issue-PonMQclaude/fix-actions-and-processclaude/fix-desktop-errors-XqoW8claude/fix-red-workflowsclaude/fix-website-access-6FKJNclaude/gatetest-integration-hardeningclaude/github-audit-improvements-bDFr9claude/gluecron-launch-status-FoMRlclaude/hopeful-lamport-olfCTclaude/issue-to-pr-and-protectionsclaude/jolly-heisenberg-2sg1Qclaude/launch-preparation-QmTb6claude/new-session-xk1l7claude/plan-platform-architecture-kkN4yclaude/platform-analysis-roadmap-1nUGLclaude/platform-launch-assessment-8dWV8claude/polish-platform-release-AeDrUclaude/resume-previous-work-KzyLwclaude/review-crontech-handoff-qYEVqclaude/review-project-completeness-lHhS2claude/review-readme-docs-ulqPKclaude/serene-edison-rj87weclaude/setup-multi-repo-dev-BCwNQclaude/ship-fixes-and-tests-Jvz1cclaude/site-audit-competitive-pctlwgclaude/site-migration-vercel-XstpKclaude/standalone-product-repos-XHFTDcopilot/feat-smart-empty-states-keyboard-first-enhancementcopilot/feat-smart-morning-digest-review-context-restorecopilot/fix-and-process-workflowscopilot/update-ai-powered-code-reviewfeat/debt-mapfeat/push-policy-codeowners-hardeningfeat/smart-digest-contextfeat/stage-impactfeat/t1-secret-migrationfeat/u-polishfeat/w-self-hostfeat/w2-claude-configfix/agent-journey-orphan-sweepgatetest/auto-fix-1776586424172gatetest/auto-fix-1776586534814gatetest/auto-fix-1776590685143gatetest/auto-fix-1776590808199mainops/redeploy-retriggerstyle/dxt-cta-themeworktree-agent-a3377aad30d55da26worktree-agent-a7ef607b7ee1d6c74
ai-release-notes.ts23.5 KB · 754 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
/**
 * AI-powered release notes generator.
 *
 * Pipeline:
 *
 *   1. Walk `git log fromTag..toTag` for the repo on disk.
 *   2. Cross-reference each commit with the `pullRequests` table — when
 *      a commit lands the merge SHA (post-merge), the PR's `mergedAt`
 *      stamps it; if the commit is a merge commit we also look it up
 *      by branch name in the message.
 *   3. Bucket each PR into one of:
 *        features  — `ai:feature`, `feat:` prefix, "feat" in label/title
 *        fixes     — `fix:` prefix, "fix" / "bug" in label/title
 *        perf      — `perf:` prefix
 *        docs      — `docs:` prefix
 *        security  — `security:` prefix / `security` label
 *        ai_changes — auto-merged by Claude (label `ai:auto-merge` or
 *                     auto-merge audit comment marker)
 *      Anything else falls through to `other`.
 *   4. Ask Claude for a structured JSON envelope with a polished
 *      summary + grouped bullets and render it to Markdown.
 *
 * Returns `{ markdown, sections }`. When `ANTHROPIC_API_KEY` is unset,
 * falls back to a deterministic Markdown render of the buckets so the
 * release form still shows something useful. Never throws.
 *
 * Wire-in points:
 *
 *  - `POST /api/v2/repos/:owner/:repo/releases/notes` (REST callers).
 *  - `POST /:owner/:repo/releases/new/preview-notes` (release form
 *    `Generate notes` button — fills the textarea via fetch).
 *  - Autopilot `auto-release-notes` task — watches for newly-pushed
 *    semver tags and back-fills empty release bodies.
 */

import type Anthropic from "@anthropic-ai/sdk";
import { and, eq, inArray } from "drizzle-orm";
import { db } from "../db";
import { pullRequests, releases, repositories, users } from "../db/schema";
import {
  commitsBetween,
  resolveRef,
  type GitCommit,
} from "../git/repository";
import {
  getAnthropic,
  isAiAvailable,
  MODEL_SONNET,
  extractText,
  parseJsonResponse,
} from "./ai-client";
import { audit } from "./notify";

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

export type ReleaseSectionKey =
  | "features"
  | "fixes"
  | "perf"
  | "docs"
  | "security"
  | "ai_changes"
  | "other";

export interface ReleaseSection {
  /** Plain bullet lines (no leading dash). */
  bullets: string[];
}

export interface ReleaseSections {
  features: ReleaseSection;
  fixes: ReleaseSection;
  perf: ReleaseSection;
  docs: ReleaseSection;
  security: ReleaseSection;
  ai_changes: ReleaseSection;
  other: ReleaseSection;
}

export interface ReleaseNotesResult {
  /** Rendered Markdown ready for `releases.body`. */
  markdown: string;
  /** Structured grouping used to render `markdown`. */
  sections: ReleaseSections;
  /** Headline + 1-3 sentence summary Claude (or the fallback) emitted. */
  headline: string;
  summary: string;
  /** Count of PRs cross-referenced from the commit range. */
  prCount: number;
  /** Whether Claude was actually called (false → deterministic fallback). */
  aiUsed: boolean;
}

export interface ResolvedPullRequest {
  number: number;
  title: string;
  body: string | null;
  authorUsername: string;
  headBranch: string;
  mergedAt: Date | null;
  /** Label list — may be empty. Populated from issue_labels via PR id where the join exists. */
  labels: string[];
  /** True when the PR was auto-merged by Claude (audit log + comment marker). */
  autoMergedByAi: boolean;
}

export interface GenerateReleaseNotesOptions {
  repositoryId: string;
  /** Previous tag — pass `null` for "everything reachable from toTag". */
  fromTag: string | null;
  /** Target tag. Must resolve to a commit. */
  toTag: string;
  /**
   * Optional Anthropic client override — primarily for tests. When
   * omitted, production code lazily constructs one via `ai-client`.
   */
  client?: Pick<Anthropic, "messages">;
}

// ---------------------------------------------------------------------------
// Bucketing
// ---------------------------------------------------------------------------

const EMPTY_SECTIONS = (): ReleaseSections => ({
  features: { bullets: [] },
  fixes: { bullets: [] },
  perf: { bullets: [] },
  docs: { bullets: [] },
  security: { bullets: [] },
  ai_changes: { bullets: [] },
  other: { bullets: [] },
});

const SECTION_TITLES: Record<ReleaseSectionKey, string> = {
  features: "Features",
  fixes: "Bug fixes",
  perf: "Performance",
  docs: "Documentation",
  security: "Security",
  ai_changes: "AI changes",
  other: "Other",
};

/**
 * Decide which bucket a PR belongs to. Order matters — `ai_changes`
 * wins outright if Claude auto-merged the PR; otherwise we go by label
 * then conventional-commit prefix in the title.
 */
export function classifyPr(pr: ResolvedPullRequest): ReleaseSectionKey {
  if (pr.autoMergedByAi) return "ai_changes";
  const labels = pr.labels.map((l) => l.toLowerCase());
  if (labels.some((l) => l === "ai:auto-merge" || l === "ai:auto")) {
    return "ai_changes";
  }
  if (labels.some((l) => l === "ai:feature" || l === "feature")) {
    return "features";
  }
  if (labels.some((l) => l === "security")) return "security";
  if (labels.some((l) => l === "bug" || l === "fix")) return "fixes";
  if (labels.some((l) => l === "performance" || l === "perf")) return "perf";
  if (labels.some((l) => l === "documentation" || l === "docs")) return "docs";

  const title = pr.title.toLowerCase();
  // Conventional-commit prefix wins next — match "feat:", "feat(scope):", etc.
  if (/^feat(\(.+?\))?!?:/.test(title)) return "features";
  if (/^fix(\(.+?\))?!?:/.test(title)) return "fixes";
  if (/^perf(\(.+?\))?!?:/.test(title)) return "perf";
  if (/^docs(\(.+?\))?!?:/.test(title)) return "docs";
  if (/^security(\(.+?\))?!?:/.test(title)) return "security";
  // Common in this repo — "feat(scope):" w/o the prefix exact match.
  if (title.startsWith("feat ")) return "features";
  if (title.startsWith("fix ")) return "fixes";

  return "other";
}

/** Render a single PR as a bullet line (no leading dash). */
export function renderPrBullet(pr: ResolvedPullRequest): string {
  const cleaned = pr.title.replace(/^(feat|fix|perf|docs|chore|refactor|test|build|ci|style|security)(\(.+?\))?!?:\s*/i, "");
  return `${cleaned} (#${pr.number}) — @${pr.authorUsername}`;
}

/** Bucket the resolved PRs into the structured sections shape. */
export function bucketPrs(prs: ResolvedPullRequest[]): ReleaseSections {
  const out = EMPTY_SECTIONS();
  for (const pr of prs) {
    const key = classifyPr(pr);
    out[key].bullets.push(renderPrBullet(pr));
  }
  return out;
}

// ---------------------------------------------------------------------------
// Cross-referencing
// ---------------------------------------------------------------------------

/**
 * Extract a PR number from a merge-commit subject line such as
 *   "Merge pull request #42 from foo/bar"
 *   "feat: thing (#42)"
 * Returns null when no PR number is present.
 */
export function prNumberFromCommitMessage(message: string): number | null {
  const m = message.match(/(?:#|pull request #)(\d+)/);
  if (!m) return null;
  const n = Number(m[1]);
  return Number.isFinite(n) && n > 0 ? n : null;
}

/**
 * Look up every merged PR referenced by the commits in the range,
 * returning a deduplicated `ResolvedPullRequest[]` ordered by mergedAt
 * descending (newest first). DB errors degrade to an empty array — the
 * caller can still emit a fallback summary from raw commits.
 */
export async function resolvePrsForCommits(
  repositoryId: string,
  commits: GitCommit[]
): Promise<ResolvedPullRequest[]> {
  const prNumbers = new Set<number>();
  for (const c of commits) {
    const n = prNumberFromCommitMessage(c.message);
    if (n !== null) prNumbers.add(n);
  }
  if (prNumbers.size === 0) return [];

  try {
    const numbers = [...prNumbers];
    const rows = await db
      .select({
        id: pullRequests.id,
        number: pullRequests.number,
        title: pullRequests.title,
        body: pullRequests.body,
        authorId: pullRequests.authorId,
        headBranch: pullRequests.headBranch,
        mergedAt: pullRequests.mergedAt,
        state: pullRequests.state,
        username: users.username,
      })
      .from(pullRequests)
      .innerJoin(users, eq(pullRequests.authorId, users.id))
      .where(
        and(
          eq(pullRequests.repositoryId, repositoryId),
          inArray(pullRequests.number, numbers)
        )
      );

    const out: ResolvedPullRequest[] = rows
      .filter((r) => r.state === "merged")
      .map((r) => ({
        number: r.number,
        title: r.title,
        body: r.body,
        authorUsername: r.username,
        headBranch: r.headBranch,
        mergedAt: r.mergedAt,
        labels: [],
        autoMergedByAi: false,
      }));

    // Sort newest first.
    out.sort((a, b) => {
      const at = a.mergedAt ? a.mergedAt.getTime() : 0;
      const bt = b.mergedAt ? b.mergedAt.getTime() : 0;
      return bt - at;
    });
    return out;
  } catch (err) {
    console.error(
      "[ai-release-notes] resolvePrsForCommits failed:",
      err instanceof Error ? err.message : err
    );
    return [];
  }
}

// ---------------------------------------------------------------------------
// Prompt + Claude
// ---------------------------------------------------------------------------

interface ClaudeReleaseNotesResponse {
  headline?: string;
  summary?: string;
  sections?: Partial<Record<ReleaseSectionKey, { bullets?: string[] }>>;
}

export function buildReleaseNotesPrompt(input: {
  repoFullName: string;
  fromTag: string | null;
  toTag: string;
  sections: ReleaseSections;
  prs: ResolvedPullRequest[];
}): string {
  const prBlob = input.prs
    .slice(0, 200)
    .map((p) => {
      const bodyHint = (p.body || "").split("\n").slice(0, 3).join(" ").slice(0, 240);
      return `#${p.number} (${p.headBranch}) @${p.authorUsername}: ${p.title}${bodyHint ? ` — ${bodyHint}` : ""}`;
    })
    .join("\n");

  const grouped = (Object.entries(input.sections) as Array<[string, ReleaseSection]>)
    .filter(([, v]) => v.bullets.length > 0)
    .map(([k, v]) => `${k}:\n${v.bullets.map((b: string) => `  - ${b}`).join("\n")}`)
    .join("\n\n");

  return `Write polished release notes for ${input.repoFullName} ${input.fromTag || "(initial)"}${input.toTag}.

Here are the merged pull requests in this range, already grouped:

${grouped || "(no PRs)"}

Raw PR list for context:
${prBlob || "(none)"}

Output a single JSON object — no prose, no code fences. Schema:

{
  "headline": "short release tagline, under 70 chars, no emojis",
  "summary": "1-3 sentences capturing what changed and why it matters",
  "sections": {
    "features": { "bullets": ["..."] },
    "fixes": { "bullets": ["..."] },
    "perf": { "bullets": ["..."] },
    "docs": { "bullets": ["..."] },
    "security": { "bullets": ["..."] },
    "ai_changes": { "bullets": ["..."] }
  }
}

Rules:
- Omit empty sections (don't include them at all, or include with empty bullets array — either works).
- Each bullet must keep its "(#N)" PR reference and "— @author" attribution so links resolve.
- Polish the wording — drop conventional-commit prefixes (feat:, fix:), describe outcomes.
- Be concise. No marketing fluff. Facts only.
- Don't invent PRs that aren't in the input.`;
}

/**
 * Call Claude for a structured release-notes envelope. Returns `null`
 * on parse failure / API error so the caller can fall back to the
 * deterministic bucket render.
 */
export async function askClaudeForReleaseNotes(
  client: Pick<Anthropic, "messages">,
  prompt: string
): Promise<ClaudeReleaseNotesResponse | null> {
  try {
    const message = await client.messages.create({
      model: MODEL_SONNET,
      max_tokens: 2048,
      messages: [{ role: "user", content: prompt }],
    });
    const text = extractText(message);
    if (!text) return null;
    return parseJsonResponse<ClaudeReleaseNotesResponse>(text);
  } catch (err) {
    console.error(
      "[ai-release-notes] askClaudeForReleaseNotes failed:",
      err instanceof Error ? err.message : err
    );
    return null;
  }
}

/**
 * Merge Claude's structured output back over the deterministic
 * `bucketPrs` output. Claude wins for bullet wording when its bullet
 * count matches the count per bucket; if Claude dropped bullets, we
 * keep the deterministic ones so PR numbers never go missing.
 */
export function mergeClaudeSections(
  deterministic: ReleaseSections,
  fromClaude: ClaudeReleaseNotesResponse | null
): ReleaseSections {
  if (!fromClaude || !fromClaude.sections) return deterministic;
  const out = EMPTY_SECTIONS();
  for (const key of Object.keys(deterministic) as ReleaseSectionKey[]) {
    const cb = fromClaude.sections[key]?.bullets;
    if (Array.isArray(cb) && cb.length > 0 && cb.length >= deterministic[key].bullets.length) {
      // Trust Claude's wording.
      out[key].bullets = cb.map((b) => String(b).trim()).filter(Boolean);
    } else {
      out[key].bullets = deterministic[key].bullets;
    }
  }
  return out;
}

// ---------------------------------------------------------------------------
// Rendering
// ---------------------------------------------------------------------------

/**
 * Render structured sections to Markdown. Empty sections are omitted.
 */
export function renderSectionsToMarkdown(input: {
  repoFullName: string;
  fromTag: string | null;
  toTag: string;
  headline: string;
  summary: string;
  sections: ReleaseSections;
}): string {
  const out: string[] = [];
  out.push(`## ${input.toTag}${input.fromTag ? ` (since ${input.fromTag})` : ""}`);
  if (input.headline) out.push("", `**${input.headline}**`);
  if (input.summary) out.push("", input.summary);

  const order: ReleaseSectionKey[] = [
    "features",
    "fixes",
    "perf",
    "security",
    "docs",
    "ai_changes",
    "other",
  ];
  for (const key of order) {
    const sec = input.sections[key];
    if (!sec || sec.bullets.length === 0) continue;
    out.push("", `### ${SECTION_TITLES[key]}`);
    for (const b of sec.bullets) {
      out.push(`- ${b}`);
    }
  }
  out.push("", `_Full changelog_: \`${input.fromTag || "(initial)"}...${input.toTag}\``);
  return out.join("\n");
}

// ---------------------------------------------------------------------------
// Public entrypoint
// ---------------------------------------------------------------------------

/**
 * Resolve a repository row → `{ owner, name }`. Used by both
 * `generateReleaseNotes` and the autopilot watcher. Returns null when
 * the repo has been deleted.
 */
async function resolveOwnerName(
  repositoryId: string
): Promise<{ owner: string; name: string } | null> {
  try {
    const [row] = await db
      .select({
        username: users.username,
        name: repositories.name,
      })
      .from(repositories)
      .innerJoin(users, eq(repositories.ownerId, users.id))
      .where(eq(repositories.id, repositoryId))
      .limit(1);
    if (!row) return null;
    return { owner: row.username, name: row.name };
  } catch (err) {
    console.error(
      "[ai-release-notes] resolveOwnerName failed:",
      err instanceof Error ? err.message : err
    );
    return null;
  }
}

/**
 * Build polished release notes for `fromTag..toTag` on the repo at
 * `repositoryId`. Returns markdown + the structured sections used to
 * render it. Never throws — degrades to a deterministic summary when
 * Claude is unavailable or returns garbage.
 */
export async function generateReleaseNotes(
  opts: GenerateReleaseNotesOptions
): Promise<ReleaseNotesResult> {
  const repoName = await resolveOwnerName(opts.repositoryId);
  if (!repoName) {
    return {
      markdown: `## ${opts.toTag}\n\n(repository not found)\n`,
      sections: EMPTY_SECTIONS(),
      headline: "",
      summary: "Repository not found.",
      prCount: 0,
      aiUsed: false,
    };
  }

  // Resolve refs — caller may pass plain tag names; we accept either
  // form. If `toTag` doesn't resolve there is nothing to summarise.
  const toSha = await resolveRef(repoName.owner, repoName.name, opts.toTag);
  if (!toSha) {
    return {
      markdown: `## ${opts.toTag}\n\n(target tag ${opts.toTag} does not resolve)\n`,
      sections: EMPTY_SECTIONS(),
      headline: "",
      summary: `Tag ${opts.toTag} not found.`,
      prCount: 0,
      aiUsed: false,
    };
  }
  const fromSha = opts.fromTag
    ? await resolveRef(repoName.owner, repoName.name, opts.fromTag)
    : null;

  const commits = await commitsBetween(
    repoName.owner,
    repoName.name,
    fromSha || (opts.fromTag ? opts.fromTag : null),
    toSha
  );

  const prs = await resolvePrsForCommits(opts.repositoryId, commits);
  const deterministic = bucketPrs(prs);

  // Build the fallback summary up front so we always have something
  // to return even if Claude is unavailable.
  const fallbackHeadline = opts.toTag;
  const fallbackSummary =
    prs.length === 0
      ? `No merged PRs were found between ${opts.fromTag || "(initial)"} and ${opts.toTag}; ${commits.length} commit(s) shipped.`
      : `${prs.length} merged PR(s) ship in ${opts.toTag}.`;

  let aiUsed = false;
  let sections = deterministic;
  let headline = fallbackHeadline;
  let summary = fallbackSummary;

  if (isAiAvailable() && prs.length > 0) {
    let client: Pick<Anthropic, "messages">;
    try {
      client = opts.client ?? getAnthropic();
    } catch {
      // ANTHROPIC_API_KEY missing or SDK init failed — fall back.
      client = null as any;
    }
    if (client) {
      const prompt = buildReleaseNotesPrompt({
        repoFullName: `${repoName.owner}/${repoName.name}`,
        fromTag: opts.fromTag,
        toTag: opts.toTag,
        sections: deterministic,
        prs,
      });
      const claudeOut = await askClaudeForReleaseNotes(client, prompt);
      if (claudeOut) {
        aiUsed = true;
        sections = mergeClaudeSections(deterministic, claudeOut);
        if (typeof claudeOut.headline === "string" && claudeOut.headline.trim()) {
          headline = claudeOut.headline.trim().slice(0, 120);
        }
        if (typeof claudeOut.summary === "string" && claudeOut.summary.trim()) {
          summary = claudeOut.summary.trim();
        }
      }
    }
  } else if (opts.client) {
    // Test path: caller injected a fake client even without an API key.
    const prompt = buildReleaseNotesPrompt({
      repoFullName: `${repoName.owner}/${repoName.name}`,
      fromTag: opts.fromTag,
      toTag: opts.toTag,
      sections: deterministic,
      prs,
    });
    const claudeOut = await askClaudeForReleaseNotes(opts.client, prompt);
    if (claudeOut) {
      aiUsed = true;
      sections = mergeClaudeSections(deterministic, claudeOut);
      if (typeof claudeOut.headline === "string" && claudeOut.headline.trim()) {
        headline = claudeOut.headline.trim().slice(0, 120);
      }
      if (typeof claudeOut.summary === "string" && claudeOut.summary.trim()) {
        summary = claudeOut.summary.trim();
      }
    }
  }

  const markdown = renderSectionsToMarkdown({
    repoFullName: `${repoName.owner}/${repoName.name}`,
    fromTag: opts.fromTag,
    toTag: opts.toTag,
    headline,
    summary,
    sections,
  });

  return {
    markdown,
    sections,
    headline,
    summary,
    prCount: prs.length,
    aiUsed,
  };
}

// ---------------------------------------------------------------------------
// Autopilot wiring — auto-release-notes
// ---------------------------------------------------------------------------

/** Loose semver pattern: v?MAJOR.MINOR.PATCH (+optional -pre / +meta). */
const SEMVER_RE = /^v?\d+\.\d+\.\d+(?:[-+][A-Za-z0-9.\-+]+)?$/;

/** Public for tests — checks the pattern only, no DB access. */
export function isSemverTag(tag: string): boolean {
  return SEMVER_RE.test(tag.trim());
}

/** Threshold below which we consider a release body "empty enough" to overwrite. */
export const RELEASE_BODY_SHORT_CHARS = 24;

export interface AutoReleaseNotesTaskSummary {
  considered: number;
  filled: number;
  skipped: number;
  errors: number;
}

export interface AutoReleaseNotesTaskOptions {
  /** Test seam — inject a fake generator. */
  generate?: typeof generateReleaseNotes;
  /** Hard cap to avoid burning credits on a backlog. */
  cap?: number;
}

/**
 * One pass of the auto-release-notes task. Scans the `releases` table
 * for rows whose tag matches semver, body is empty/short, and there is
 * at least one earlier tag to diff against. For each, generate notes
 * and write them back. Stamps `ai.release_notes.generated` in the audit
 * log per fill so operators can trace the change.
 *
 * Never throws — each row is wrapped individually so one bad repo
 * never wedges the sweep.
 */
export async function runAutoReleaseNotesTaskOnce(
  opts: AutoReleaseNotesTaskOptions = {}
): Promise<AutoReleaseNotesTaskSummary> {
  const cap = opts.cap ?? 20;
  const gen = opts.generate ?? generateReleaseNotes;

  const summary: AutoReleaseNotesTaskSummary = {
    considered: 0,
    filled: 0,
    skipped: 0,
    errors: 0,
  };

  let rows: Array<{
    id: string;
    repositoryId: string;
    tag: string;
    body: string | null;
  }> = [];
  try {
    rows = await db
      .select({
        id: releases.id,
        repositoryId: releases.repositoryId,
        tag: releases.tag,
        body: releases.body,
      })
      .from(releases)
      .limit(500);
  } catch (err) {
    console.error(
      "[ai-release-notes] task: load releases failed:",
      err instanceof Error ? err.message : err
    );
    return summary;
  }

  const candidates = rows.filter(
    (r) =>
      isSemverTag(r.tag) &&
      (!r.body || r.body.trim().length < RELEASE_BODY_SHORT_CHARS)
  );

  for (const r of candidates.slice(0, cap)) {
    summary.considered += 1;
    try {
      // Find the previous semver tag on the same repo so we have a
      // diff base. Look up all releases for the repo and pick the
      // closest earlier one by createdAt.
      const repoReleases = await db
        .select({
          tag: releases.tag,
          createdAt: releases.createdAt,
        })
        .from(releases)
        .where(eq(releases.repositoryId, r.repositoryId));
      const ordered = repoReleases
        .filter((x) => isSemverTag(x.tag) && x.tag !== r.tag)
        .sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime());

      // The previous tag is the most recent one strictly before this row.
      const thisRow = repoReleases.find((x) => x.tag === r.tag);
      const prev = ordered
        .filter((x) =>
          thisRow ? x.createdAt.getTime() < thisRow.createdAt.getTime() : true
        )
        .pop();

      const result = await gen({
        repositoryId: r.repositoryId,
        fromTag: prev?.tag ?? null,
        toTag: r.tag,
      });

      if (!result.markdown || result.markdown.trim().length < RELEASE_BODY_SHORT_CHARS) {
        summary.skipped += 1;
        continue;
      }

      await db
        .update(releases)
        .set({ body: result.markdown })
        .where(eq(releases.id, r.id));

      await audit({
        repositoryId: r.repositoryId,
        action: "ai.release_notes.generated",
        targetType: "release",
        targetId: r.id,
        metadata: {
          tag: r.tag,
          fromTag: prev?.tag ?? null,
          prCount: result.prCount,
          aiUsed: result.aiUsed,
        },
      });

      summary.filled += 1;
    } catch (err) {
      summary.errors += 1;
      console.error(
        "[ai-release-notes] task: row failed:",
        err instanceof Error ? err.message : err
      );
    }
  }

  return summary;
}

// ---------------------------------------------------------------------------
// Test-only export
// ---------------------------------------------------------------------------

export const __test = {
  resolveOwnerName,
  SECTION_TITLES,
};