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-commit-message.ts9.7 KB · 316 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
/**
 * AI-generated commit messages.
 *
 * Powers the `gluecron commit` CLI and the `POST /api/v2/ai/commit-message`
 * endpoint. Given a unified diff (typically `git diff --cached`), returns a
 * { subject, body } pair.
 *
 *   - Conventional Commits style by default: `feat(scope): subject` etc.
 *   - The body explains WHY (not what — the diff already says what).
 *   - Falls back to a deterministic heuristic when ANTHROPIC_API_KEY is
 *     missing, the model errors out, or the diff is empty. The CLI must
 *     never block a developer on a Claude outage.
 *   - Diff is capped at ~50KB before being sent to the model — past that
 *     the signal-to-noise ratio drops and we'd just be burning tokens.
 *
 * Tests live in src/__tests__/ai-commit-message.test.ts. The module
 * exports a `__test` bag for the parser + truncation helpers so the
 * unit tests don't have to call the network path.
 */

import {
  getAnthropic,
  MODEL_HAIKU,
  extractText,
  parseJsonResponse,
  isAiAvailable,
} from "./ai-client";

export type CommitStyle = "conventional" | "plain";

export interface CommitMessage {
  subject: string;
  body: string;
}

export interface GenerateOptions {
  style?: CommitStyle;
}

/** Max bytes of diff we send to the model. Past this we truncate. */
export const DIFF_BYTE_CAP = 50_000;
const TRUNCATE_MARKER = "\n... (more)";

/** Conventional-commits types we accept in the subject. */
const CONVENTIONAL_TYPES = [
  "feat",
  "fix",
  "docs",
  "style",
  "refactor",
  "perf",
  "test",
  "build",
  "ci",
  "chore",
  "revert",
] as const;

type ConventionalType = (typeof CONVENTIONAL_TYPES)[number];

/**
 * Truncate a diff to DIFF_BYTE_CAP bytes, appending a marker so the model
 * (and any human reader) knows the input was cut.
 */
export function truncateDiff(diff: string, cap = DIFF_BYTE_CAP): string {
  if (diff.length <= cap) return diff;
  return diff.slice(0, cap - TRUNCATE_MARKER.length) + TRUNCATE_MARKER;
}

/**
 * Deterministic fallback: scan filenames + counts and emit a plausible
 * conventional-commit. Never blocks on a network call, never throws.
 *
 * Heuristics:
 *   - "test" files → test
 *   - "doc"/"readme"/".md" only → docs
 *   - new files dominate → feat
 *   - otherwise → chore
 *
 * Subject ≤ 72 chars; body lists the top-3 touched paths so the human
 * still has signal even when Claude is unavailable.
 */
export function heuristicMessage(
  diff: string,
  style: CommitStyle = "conventional"
): CommitMessage {
  const trimmed = diff.trim();
  if (!trimmed) {
    return {
      subject: style === "conventional" ? "chore: update" : "Update",
      body: "",
    };
  }

  // Pull file paths out of standard unified-diff headers.
  // We look at `+++ b/<path>` to favour the *new* side of renames.
  const paths: string[] = [];
  let added = 0;
  let removed = 0;
  let newFiles = 0;
  for (const line of trimmed.split("\n")) {
    if (line.startsWith("+++ b/")) {
      const p = line.slice("+++ b/".length).trim();
      if (p && p !== "/dev/null") paths.push(p);
    } else if (line.startsWith("new file mode")) {
      newFiles++;
    } else if (line.startsWith("+") && !line.startsWith("+++")) {
      added++;
    } else if (line.startsWith("-") && !line.startsWith("---")) {
      removed++;
    }
  }

  const onlyDocs =
    paths.length > 0 &&
    paths.every((p) => /\.(md|mdx|rst|txt)$/i.test(p) || /readme/i.test(p));
  const onlyTests =
    paths.length > 0 && paths.every((p) => /(^|\/)(__tests__|tests?)\//.test(p) || /\.test\.[tj]sx?$/.test(p));
  const allNew = newFiles > 0 && newFiles === paths.length;

  let type: ConventionalType = "chore";
  if (onlyDocs) type = "docs";
  else if (onlyTests) type = "test";
  else if (allNew) type = "feat";
  else if (removed > added * 2) type = "refactor";
  else type = "chore";

  // Scope = top-level directory if all paths share one.
  const tops = new Set(paths.map((p) => p.split("/")[0]).filter(Boolean));
  const scope = tops.size === 1 ? [...tops][0] : "";

  const fileWord = paths.length === 1 ? "file" : "files";
  const subjectRaw =
    paths.length === 0
      ? "update files"
      : `update ${paths.length} ${fileWord}`;

  const subject =
    style === "conventional"
      ? `${type}${scope ? `(${scope})` : ""}: ${subjectRaw}`
      : subjectRaw.charAt(0).toUpperCase() + subjectRaw.slice(1);

  const cappedSubject =
    subject.length > 72 ? subject.slice(0, 69) + "..." : subject;

  const topPaths = paths.slice(0, 3);
  const body =
    paths.length === 0
      ? ""
      : `Touched files:\n${topPaths.map((p) => `- ${p}`).join("\n")}` +
        (paths.length > topPaths.length
          ? `\n- ...and ${paths.length - topPaths.length} more`
          : "");

  return { subject: cappedSubject, body };
}

/**
 * Coerce arbitrary model output into a clean { subject, body } shape.
 *
 * Accepts (in order):
 *   1. A JSON object with `subject` / `body` fields.
 *   2. A code-fenced block whose first line looks like a commit subject.
 *   3. Raw text — first line as subject, the rest as body.
 *
 * Always strips backticks, trims, and caps the subject at 72 chars.
 * Exported for unit tests.
 */
export function parseModelOutput(
  text: string,
  style: CommitStyle = "conventional"
): CommitMessage {
  const cleaned = text.replace(/^```[a-zA-Z]*\n|\n```$/g, "").trim();

  // Try JSON first.
  const parsed = parseJsonResponse<{ subject?: string; body?: string }>(
    cleaned
  );
  let subject = "";
  let body = "";
  if (parsed && typeof parsed.subject === "string") {
    subject = parsed.subject.trim();
    body = typeof parsed.body === "string" ? parsed.body.trim() : "";
  } else {
    // Fall back to "first line = subject, rest = body" parsing.
    const lines = cleaned.split("\n");
    subject = (lines[0] || "").trim();
    body = lines.slice(1).join("\n").trim();
    // Drop a single blank line between subject + body if present.
    if (body.startsWith("\n")) body = body.slice(1);
  }

  // Strip surrounding quotes / backticks the model sometimes adds.
  subject = subject.replace(/^["'`]+|["'`]+$/g, "").trim();
  if (subject.length > 72) {
    subject = subject.slice(0, 69) + "...";
  }
  if (!subject) {
    return { subject: style === "conventional" ? "chore: update" : "Update", body: "" };
  }

  // If conventional style was requested but the model returned a bare
  // sentence, lightly normalise it. We do NOT try to re-classify — that
  // would be guessing — but we do prefix `chore:` so commitlint stays happy.
  if (style === "conventional" && !/^[a-z]+(\([^)]+\))?(!?):/.test(subject)) {
    subject = `chore: ${subject.charAt(0).toLowerCase()}${subject.slice(1)}`;
    if (subject.length > 72) subject = subject.slice(0, 69) + "...";
  }

  return { subject, body };
}

/**
 * Build the prompt sent to Claude. Pulled out so the test suite can
 * sanity-check it without spinning up the API client.
 */
export function buildPrompt(diff: string, style: CommitStyle): string {
  const styleInstruction =
    style === "conventional"
      ? `Use Conventional Commits style — the subject MUST start with one of: ${CONVENTIONAL_TYPES.join(", ")}, optionally followed by (scope), then ": ", then a lowercase imperative summary. Example: "feat(auth): add passkey login".`
      : `Write a plain-English subject in imperative mood (e.g. "Add passkey login"). No type prefix.`;

  return `You are writing a git commit message for the following staged diff.

${styleInstruction}

Rules:
- Subject MUST be 72 characters or fewer.
- Subject is one line. No trailing period.
- Body explains WHY the change was made when it is non-obvious. Skip the body for trivial changes.
- Body lines wrap at ~72 chars. Use blank lines between paragraphs.
- Do NOT describe what every file does — the diff already shows that. Focus on intent.
- Do NOT include code fences, markdown, or extra commentary.

Respond ONLY with JSON in this exact shape:
{"subject": "...", "body": "..."}

If the change is trivial enough that no body is needed, return body as "".

Diff:
\`\`\`diff
${diff}
\`\`\``;
}

/**
 * Generate a commit message for the given diff.
 *
 * Never throws — on any error (no API key, model timeout, parse failure)
 * we fall back to the deterministic heuristic so the caller can always
 * present a draft to the developer.
 */
export async function generateCommitMessage(
  diff: string,
  opts: GenerateOptions = {}
): Promise<CommitMessage> {
  const style: CommitStyle = opts.style === "plain" ? "plain" : "conventional";
  const trimmed = diff.trim();

  if (!trimmed) {
    return {
      subject: style === "conventional" ? "chore: empty commit" : "Empty commit",
      body: "",
    };
  }

  if (!isAiAvailable()) {
    return heuristicMessage(trimmed, style);
  }

  const truncated = truncateDiff(trimmed);

  try {
    const client = getAnthropic();
    const message = await client.messages.create({
      model: MODEL_SONNET,
      max_tokens: 512,
      messages: [
        {
          role: "user",
          content: buildPrompt(truncated, style),
        },
      ],
    });
    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: "other",
        sourceKind: "commit_message",
      });
    } catch {
      /* swallow — best-effort */
    }
    const text = extractText(message);
    if (!text.trim()) {
      return heuristicMessage(trimmed, style);
    }
    return parseModelOutput(text, style);
  } catch {
    // Network/API failure → never block the developer; degrade.
    return heuristicMessage(trimmed, style);
  }
}

/** Test-only exports — not part of the public API. */
export const __test = {
  truncateDiff,
  heuristicMessage,
  parseModelOutput,
  buildPrompt,
  CONVENTIONAL_TYPES,
};