Skip to content

fix (git): Truncate oversized git output for commit generation#1275

Open
Snowy7 wants to merge 1 commit intopingdotgg:mainfrom
Snowy7:fix/truncate-oversized-commit-context
Open

fix (git): Truncate oversized git output for commit generation#1275
Snowy7 wants to merge 1 commit intopingdotgg:mainfrom
Snowy7:fix/truncate-oversized-commit-context

Conversation

@Snowy7
Copy link

@Snowy7 Snowy7 commented Mar 21, 2026

What Changed

  • added opt-in truncation support to the git command execution layer
  • used that truncation path when reading the staged patch for commit preparation
  • added tests covering truncated git output and large staged commit generation

Why

The commit flow currently reads the full staged patch before generating a commit message. If git diff --cached --patch --minimal exceeds the output cap, commit preparation fails even though Git could still commit successfully.

This change keeps the behavior strict for normal git commands, but lets commit preparation degrade gracefully for very large staged diffs. That makes the commit feature more reliable in real user repos, especially when large generated changes or line-ending churn produce oversized patches.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • I included before/after screenshots for any UI changes No UI Changes Made
  • I included a video for animation/interaction changes No UI Changes Made

Note

Truncate oversized git output during commit generation instead of failing

  • collectOutput in GitService.ts now accepts a truncateOutput flag; when set, it truncates at maxOutputBytes and sets a truncated flag instead of throwing a GitCommandError.
  • ExecuteGitResult gains stdoutTruncated/stderrTruncated fields so callers can detect truncation.
  • prepareCommitContext in GitCore.ts now passes truncateOutput: true when capturing the staged patch, so large diffs no longer abort commit generation.
  • Behavioral Change: previously, a staged patch exceeding the output limit caused an error; it now produces a truncated patch and continues.
📊 Macroscope summarized 15813db. 5 files reviewed, 1 issue evaluated, 0 issues filtered, 1 comment posted

🗂️ Filtered Issues

- Allow git execution to cap and truncate streamed output
- Use truncated staged patch when building commit messages
- Add tests for oversized patch handling
@coderabbitai
Copy link

coderabbitai bot commented Mar 21, 2026

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: d099fce1-b0a6-4740-bdcd-ee336df9cbcd

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

CodeRabbit can use Trivy to scan for security misconfigurations and secrets in Infrastructure as Code files.

Add a .trivyignore file to your project to customize which findings Trivy reports.

@Snowy7 Snowy7 marked this pull request as ready for review March 21, 2026 15:06
Copilot AI review requested due to automatic review settings March 21, 2026 15:06
@github-actions github-actions bot added size:L 100-499 changed lines (additions + deletions). vouch:unvouched PR author is not yet trusted in the VOUCHED list. labels Mar 21, 2026
@Snowy7 Snowy7 changed the title Truncate oversized git output for commit generation fix (git): Truncate oversized git output for commit generation Mar 21, 2026
Comment on lines 75 to 80
return yield* new GitCommandError({
operation: input.operation,
command: quoteGitCommand(input.args),
cwd: input.cwd,
detail: `${quoteGitCommand(input.args)} output exceeded ${maxOutputBytes} bytes and was truncated.`,
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Low Layers/GitService.ts:75

When truncateOutput is false, the code throws an error with the message "output exceeded ${maxOutputBytes} bytes and was truncated." — but the output was not truncated; an error was thrown instead. This is misleading because it contradicts the actual behavior. Consider removing the "and was truncated" clause from the error message when truncation is disabled.

           detail: `${quoteGitCommand(input.args)} output exceeded ${maxOutputBytes} bytes and was truncated.`,
🚀 Reply "fix it for me" or copy this AI Prompt for your agent:
In file apps/server/src/git/Layers/GitService.ts around lines 75-80:

When `truncateOutput` is `false`, the code throws an error with the message `"output exceeded ${maxOutputBytes} bytes and was truncated."` — but the output was not truncated; an error was thrown instead. This is misleading because it contradicts the actual behavior. Consider removing the `"and was truncated"` clause from the error message when truncation is disabled.

Evidence trail:
apps/server/src/git/Layers/GitService.ts lines 66-80 at REVIEWED_COMMIT - Line 66-73 shows truncation logic when `truncateOutput` is true. Line 74-80 shows error throwing when `truncateOutput` is false, with the error message at line 78-79 saying `output exceeded ${maxOutputBytes} bytes and was truncated.` despite no truncation occurring.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds opt-in output truncation to the server-side git execution layer and uses it to make commit preparation resilient when the staged diff is very large (preventing commit message generation from failing purely due to output caps).

Changes:

  • Extend GitService.execute to support truncateOutput and to report stdoutTruncated / stderrTruncated.
  • Update git output collection to optionally truncate instead of failing when maxOutputBytes is exceeded.
  • Use truncation when capturing the staged patch during commit preparation; add/extend tests for truncation and large staged diffs.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
apps/server/src/git/Services/GitService.ts Extends the GitService API types with truncation options/flags.
apps/server/src/git/Layers/GitService.ts Implements truncating output collection and surfaces truncation flags in results.
apps/server/src/git/Layers/GitService.test.ts Adds an integration test verifying truncation behavior.
apps/server/src/git/Layers/GitCore.ts Enables truncation when capturing the staged patch for commit context.
apps/server/src/git/Layers/GitManager.test.ts Adds an integration test ensuring commits still succeed with oversized staged patches.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

"GitCore.prepareCommitContext.stagedPatch",
cwd,
["diff", "--cached", "--patch", "--minimal"],
{
Copy link

Copilot AI Mar 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For prepareCommitContext the staged patch is later truncated to 50,000 chars in GitManager.limitContext(...), but this git diff --cached --patch call still captures up to the GitService default maxOutputBytes (1,000,000). Consider passing an explicit smaller maxOutputBytes here (plus a small buffer) to avoid unnecessary read/decoding work on large diffs and to make the intended cap explicit at this call site.

Suggested change
{
{
// Limit output to slightly above the 50,000-character context cap used later.
maxOutputBytes: 60_000,

Copilot uses AI. Check for mistakes.
});

assert.equal(result.code, 0);
assert.ok(result.stdout.length <= 16);
Copy link

Copilot AI Mar 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test asserts result.stdout.length <= 16, but maxOutputBytes is a byte limit while .length is a UTF-16 code unit count. To make the test accurately reflect the contract (and avoid edge cases if output ever contains non-ASCII), assert against Buffer.byteLength(result.stdout, 'utf8') (or similar) instead of string length.

Suggested change
assert.ok(result.stdout.length <= 16);
assert.ok(Buffer.byteLength(result.stdout, "utf8") <= 16);

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L 100-499 changed lines (additions + deletions). vouch:unvouched PR author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants