| author | |
| committer | |
| log | 13094952186d16aaffea7609e78629bc5f9a57f2 |
| tree | be5e48fad379666fde91935458672f58ffa399d4 |
| parent | c34a4c69e5d58f48f68c53ebed22183905a9db15 |
5 files changed, 91 insertions(+), 27 deletions(-)
apps/server/src/git/Layers/CodexTextGeneration.test.ts+29-2| ... | ... | @@ -189,15 +189,15 @@ function withFakeCodexEnv<A, E, R>( |
| 189 | 189 | const CodexTextGenerationTestLayer = makeCodexTextGenerationTestLayer(process.cwd()); |
| 190 | 190 | |
| 191 | 191 | it.layer(CodexTextGenerationTestLayer)("CodexTextGenerationLive", (it) => { |
| 192 | it.effect("generates and sanitizes commit messages", () => | |
| 192 | it.effect("generates and sanitizes commit messages without branch by default", () => | |
| 193 | 193 | withFakeCodexEnv( |
| 194 | 194 | { |
| 195 | 195 | output: JSON.stringify({ |
| 196 | 196 | subject: |
| 197 | 197 | " Add important change to the system with too much detail and a trailing period.\nsecondary line", |
| 198 | 198 | body: "\n- added migration\n- updated tests\n", |
| 199 | branch: "fix/important-system-change", | |
| 200 | 199 | }), |
| 200 | stdinMustNotContain: "branch must be a short semantic git branch fragment", | |
| 201 | 201 | }, |
| 202 | 202 | Effect.gen(function* () { |
| 203 | 203 | const textGeneration = yield* TextGeneration; |
| ... | ... | @@ -212,6 +212,33 @@ it.layer(CodexTextGenerationTestLayer)("CodexTextGenerationLive", (it) => { |
| 212 | 212 | expect(generated.subject.length).toBeLessThanOrEqual(72); |
| 213 | 213 | expect(generated.subject.endsWith(".")).toBe(false); |
| 214 | 214 | expect(generated.body).toBe("- added migration\n- updated tests"); |
| 215 | expect(generated.branch).toBeUndefined(); | |
| 216 | }), | |
| 217 | ), | |
| 218 | ); | |
| 219 | ||
| 220 | it.effect("generates commit message with branch when includeBranch is true", () => | |
| 221 | withFakeCodexEnv( | |
| 222 | { | |
| 223 | output: JSON.stringify({ | |
| 224 | subject: "Add important change", | |
| 225 | body: "", | |
| 226 | branch: "fix/important-system-change", | |
| 227 | }), | |
| 228 | stdinMustContain: "branch must be a short semantic git branch fragment", | |
| 229 | }, | |
| 230 | Effect.gen(function* () { | |
| 231 | const textGeneration = yield* TextGeneration; | |
| 232 | ||
| 233 | const generated = yield* textGeneration.generateCommitMessage({ | |
| 234 | cwd: process.cwd(), | |
| 235 | branch: "feature/codex-effect", | |
| 236 | stagedSummary: "M README.md", | |
| 237 | stagedPatch: "diff --git a/README.md b/README.md", | |
| 238 | includeBranch: true, | |
| 239 | }); | |
| 240 | ||
| 241 | expect(generated.subject).toBe("Add important change"); | |
| 215 | 242 | expect(generated.branch).toBe("feature/fix/important-system-change"); |
| 216 | 243 | }), |
| 217 | 244 | ), |
apps/server/src/git/Layers/CodexTextGeneration.ts+27-9| ... | ... | @@ -336,13 +336,19 @@ const makeCodexTextGeneration = Effect.gen(function* () { |
| 336 | 336 | }); |
| 337 | 337 | |
| 338 | 338 | const generateCommitMessage: TextGenerationShape["generateCommitMessage"] = (input) => { |
| 339 | const wantsBranch = input.includeBranch === true; | |
| 340 | ||
| 339 | 341 | const prompt = [ |
| 340 | 342 | "You write concise git commit messages.", |
| 341 | "Return a JSON object with keys: subject, body, branch.", | |
| 343 | wantsBranch | |
| 344 | ? "Return a JSON object with keys: subject, body, branch." | |
| 345 | : "Return a JSON object with keys: subject, body.", | |
| 342 | 346 | "Rules:", |
| 343 | 347 | "- subject must be imperative, <= 72 chars, and no trailing period", |
| 344 | 348 | "- body can be empty string or short bullet points", |
| 345 | "- branch must be a short semantic git branch fragment for this change", | |
| 349 | ...(wantsBranch | |
| 350 | ? ["- branch must be a short semantic git branch fragment for this change"] | |
| 351 | : []), | |
| 346 | 352 | "- capture the primary user-visible or developer-visible change", |
| 347 | 353 | "", |
| 348 | 354 | `Branch: ${input.branch ?? "(detached)"}`, |
| ... | ... | @@ -354,22 +360,31 @@ const makeCodexTextGeneration = Effect.gen(function* () { |
| 354 | 360 | limitSection(input.stagedPatch, 40_000), |
| 355 | 361 | ].join("\n"); |
| 356 | 362 | |
| 363 | const outputSchemaJson = wantsBranch | |
| 364 | ? Schema.Struct({ | |
| 365 | subject: Schema.String, | |
| 366 | body: Schema.String, | |
| 367 | branch: Schema.String, | |
| 368 | }) | |
| 369 | : Schema.Struct({ | |
| 370 | subject: Schema.String, | |
| 371 | body: Schema.String, | |
| 372 | }); | |
| 373 | ||
| 357 | 374 | return runCodexJson({ |
| 358 | 375 | operation: "generateCommitMessage", |
| 359 | 376 | cwd: input.cwd, |
| 360 | 377 | prompt, |
| 361 | outputSchemaJson: Schema.Struct({ | |
| 362 | subject: Schema.String, | |
| 363 | body: Schema.String, | |
| 364 | branch: Schema.String, | |
| 365 | }), | |
| 378 | outputSchemaJson, | |
| 366 | 379 | }).pipe( |
| 367 | 380 | Effect.map( |
| 368 | 381 | (generated) => |
| 369 | 382 | ({ |
| 370 | 383 | subject: sanitizeCommitSubject(generated.subject), |
| 371 | 384 | body: generated.body.trim(), |
| 372 | branch: sanitizeFeatureBranchName(generated.branch), | |
| 385 | ...("branch" in generated && typeof generated.branch === "string" | |
| 386 | ? { branch: sanitizeFeatureBranchName(generated.branch) } | |
| 387 | : {}), | |
| 373 | 388 | }) satisfies CommitMessageGenerationResult, |
| 374 | 389 | ), |
| 375 | 390 | ); |
| ... | ... | @@ -419,7 +434,10 @@ const makeCodexTextGeneration = Effect.gen(function* () { |
| 419 | 434 | |
| 420 | 435 | const generateBranchName: TextGenerationShape["generateBranchName"] = (input) => { |
| 421 | 436 | return Effect.gen(function* () { |
| 422 | const { imagePaths } = yield* materializeImageAttachments("generateBranchName", input.attachments); | |
| 437 | const { imagePaths } = yield* materializeImageAttachments( | |
| 438 | "generateBranchName", | |
| 439 | input.attachments, | |
| 440 | ); | |
| 423 | 441 | const attachmentLines = (input.attachments ?? []).map( |
| 424 | 442 | (attachment) => |
| 425 | 443 | `- ${attachment.name} (${attachment.mimeType}, ${attachment.sizeBytes} bytes)`, |
apps/server/src/git/Layers/GitManager.test.ts+13-7| ... | ... | @@ -32,7 +32,11 @@ interface FakeGitTextGeneration { |
| 32 | 32 | branch: string | null; |
| 33 | 33 | stagedSummary: string; |
| 34 | 34 | stagedPatch: string; |
| 35 | }) => Effect.Effect<{ subject: string; body: string; branch: string }, TextGenerationError>; | |
| 35 | includeBranch?: boolean; | |
| 36 | }) => Effect.Effect< | |
| 37 | { subject: string; body: string; branch?: string | undefined }, | |
| 38 | TextGenerationError | |
| 39 | >; | |
| 36 | 40 | generatePrContent: (input: { |
| 37 | 41 | cwd: string; |
| 38 | 42 | baseBranch: string; |
| ... | ... | @@ -104,11 +108,13 @@ function createBareRemote(): Effect.Effect< |
| 104 | 108 | |
| 105 | 109 | function createTextGeneration(overrides: Partial<FakeGitTextGeneration> = {}): TextGenerationShape { |
| 106 | 110 | const implementation: FakeGitTextGeneration = { |
| 107 | generateCommitMessage: () => | |
| 111 | generateCommitMessage: (input) => | |
| 108 | 112 | Effect.succeed({ |
| 109 | 113 | subject: "Implement stacked git actions", |
| 110 | 114 | body: "", |
| 111 | branch: "feature/implement-stacked-git-actions", | |
| 115 | ...(input.includeBranch | |
| 116 | ? { branch: "feature/implement-stacked-git-actions" } | |
| 117 | : {}), | |
| 112 | 118 | }), |
| 113 | 119 | generatePrContent: () => |
| 114 | 120 | Effect.succeed({ |
| ... | ... | @@ -506,13 +512,13 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => { |
| 506 | 512 | |
| 507 | 513 | const { manager } = yield* makeManager({ |
| 508 | 514 | textGeneration: { |
| 509 | generateCommitMessage: () => | |
| 515 | generateCommitMessage: (input) => | |
| 510 | 516 | Effect.sync(() => { |
| 511 | 517 | generatedCount += 1; |
| 512 | 518 | return { |
| 513 | 519 | subject: "this should not be used", |
| 514 | 520 | body: "", |
| 515 | branch: "feature/unused", | |
| 521 | ...(input.includeBranch ? { branch: "feature/unused" } : {}), | |
| 516 | 522 | }; |
| 517 | 523 | }), |
| 518 | 524 | }, |
| ... | ... | @@ -566,13 +572,13 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => { |
| 566 | 572 | |
| 567 | 573 | const { manager } = yield* makeManager({ |
| 568 | 574 | textGeneration: { |
| 569 | generateCommitMessage: () => | |
| 575 | generateCommitMessage: (input) => | |
| 570 | 576 | Effect.sync(() => { |
| 571 | 577 | generatedCount += 1; |
| 572 | 578 | return { |
| 573 | 579 | subject: "unused", |
| 574 | 580 | body: "", |
| 575 | branch: "feature/unused", | |
| 581 | ...(input.includeBranch ? { branch: "feature/unused" } : {}), | |
| 576 | 582 | }; |
| 577 | 583 | }), |
| 578 | 584 | }, |
apps/server/src/git/Layers/GitManager.ts+18-8| ... | ... | @@ -85,10 +85,14 @@ function limitContext(value: string, maxChars: number): string { |
| 85 | 85 | return `${value.slice(0, maxChars)}\n\n[truncated]`; |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | function sanitizeCommitMessage(generated: { subject: string; body: string; branch: string }): { | |
| 88 | function sanitizeCommitMessage(generated: { | |
| 89 | 89 | subject: string; |
| 90 | 90 | body: string; |
| 91 | branch: string; | |
| 91 | branch?: string | undefined; | |
| 92 | }): { | |
| 93 | subject: string; | |
| 94 | body: string; | |
| 95 | branch?: string | undefined; | |
| 92 | 96 | } { |
| 93 | 97 | const rawSubject = generated.subject.trim().split(/\r?\n/g)[0]?.trim() ?? ""; |
| 94 | 98 | const subject = rawSubject.replace(/[.]+$/g, "").trim(); |
| ... | ... | @@ -96,7 +100,7 @@ function sanitizeCommitMessage(generated: { subject: string; body: string; branc |
| 96 | 100 | return { |
| 97 | 101 | subject: safeSubject, |
| 98 | 102 | body: generated.body.trim(), |
| 99 | branch: generated.branch, | |
| 103 | ...(generated.branch !== undefined ? { branch: generated.branch } : {}), | |
| 100 | 104 | }; |
| 101 | 105 | } |
| 102 | 106 | |
| ... | ... | @@ -293,6 +297,8 @@ export const makeGitManager = Effect.gen(function* () { |
| 293 | 297 | cwd: string; |
| 294 | 298 | branch: string | null; |
| 295 | 299 | commitMessage?: string; |
| 300 | /** When true, also produce a semantic feature branch name. */ | |
| 301 | includeBranch?: boolean; | |
| 296 | 302 | }) => |
| 297 | 303 | Effect.gen(function* () { |
| 298 | 304 | const context = yield* gitCore.prepareCommitContext(input.cwd); |
| ... | ... | @@ -302,11 +308,12 @@ export const makeGitManager = Effect.gen(function* () { |
| 302 | 308 | |
| 303 | 309 | const customCommit = parseCustomCommitMessage(input.commitMessage ?? ""); |
| 304 | 310 | if (customCommit) { |
| 305 | const branch = sanitizeFeatureBranchName(customCommit.subject); | |
| 306 | 311 | return { |
| 307 | 312 | subject: customCommit.subject, |
| 308 | 313 | body: customCommit.body, |
| 309 | branch, | |
| 314 | ...(input.includeBranch | |
| 315 | ? { branch: sanitizeFeatureBranchName(customCommit.subject) } | |
| 316 | : {}), | |
| 310 | 317 | commitMessage: formatCommitMessage(customCommit.subject, customCommit.body), |
| 311 | 318 | }; |
| 312 | 319 | } |
| ... | ... | @@ -317,14 +324,16 @@ export const makeGitManager = Effect.gen(function* () { |
| 317 | 324 | branch: input.branch, |
| 318 | 325 | stagedSummary: limitContext(context.stagedSummary, 8_000), |
| 319 | 326 | stagedPatch: limitContext(context.stagedPatch, 50_000), |
| 327 | ...(input.includeBranch !== undefined ? { includeBranch: input.includeBranch } : {}), | |
| 320 | 328 | }) |
| 321 | 329 | .pipe(Effect.map((result) => sanitizeCommitMessage(result))); |
| 322 | 330 | |
| 323 | const branch = sanitizeFeatureBranchName(generated.branch); | |
| 324 | 331 | return { |
| 325 | 332 | subject: generated.subject, |
| 326 | 333 | body: generated.body, |
| 327 | branch, | |
| 334 | ...(generated.branch !== undefined | |
| 335 | ? { branch: sanitizeFeatureBranchName(generated.branch) } | |
| 336 | : {}), | |
| 328 | 337 | commitMessage: formatCommitMessage(generated.subject, generated.body), |
| 329 | 338 | }; |
| 330 | 339 | }); |
| ... | ... | @@ -456,6 +465,7 @@ export const makeGitManager = Effect.gen(function* () { |
| 456 | 465 | cwd: input.cwd, |
| 457 | 466 | branch: details.branch, |
| 458 | 467 | ...(input.commitMessage ? { commitMessage: input.commitMessage } : {}), |
| 468 | includeBranch: true, | |
| 459 | 469 | }); |
| 460 | 470 | if (!suggestion) { |
| 461 | 471 | return yield* gitManagerError( |
| ... | ... | @@ -466,7 +476,7 @@ export const makeGitManager = Effect.gen(function* () { |
| 466 | 476 | |
| 467 | 477 | return { |
| 468 | 478 | commitMessage: suggestion.commitMessage, |
| 469 | branch: suggestion.branch, | |
| 479 | branch: suggestion.branch ?? sanitizeFeatureBranchName(suggestion.subject), | |
| 470 | 480 | }; |
| 471 | 481 | }, |
| 472 | 482 | ); |
apps/server/src/git/Services/TextGeneration.ts+4-1| ... | ... | @@ -17,12 +17,15 @@ export interface CommitMessageGenerationInput { |
| 17 | 17 | branch: string | null; |
| 18 | 18 | stagedSummary: string; |
| 19 | 19 | stagedPatch: string; |
| 20 | /** When true, the model also returns a semantic branch name for the change. */ | |
| 21 | includeBranch?: boolean; | |
| 20 | 22 | } |
| 21 | 23 | |
| 22 | 24 | export interface CommitMessageGenerationResult { |
| 23 | 25 | subject: string; |
| 24 | 26 | body: string; |
| 25 | branch: string; | |
| 27 | /** Only present when `includeBranch` was set on the input. */ | |
| 28 | branch?: string | undefined; | |
| 26 | 29 | } |
| 27 | 30 | |
| 28 | 31 | export interface PrContentGenerationInput { |