-
Notifications
You must be signed in to change notification settings - Fork 956
fix (git): Truncate oversized git output for commit generation #1275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -37,6 +37,23 @@ layer("GitServiceLive", (it) => { | |||||
| }), | ||||||
| ); | ||||||
|
|
||||||
| it.effect("runGit can truncate oversized output when requested", () => | ||||||
| Effect.gen(function* () { | ||||||
| const gitService = yield* GitService; | ||||||
| const result = yield* gitService.execute({ | ||||||
| operation: "GitProcess.test.truncateOutput", | ||||||
| cwd: process.cwd(), | ||||||
| args: ["--version", "--build-options"], | ||||||
| maxOutputBytes: 16, | ||||||
| truncateOutput: true, | ||||||
| }); | ||||||
|
|
||||||
| assert.equal(result.code, 0); | ||||||
| assert.ok(result.stdout.length <= 16); | ||||||
|
||||||
| assert.ok(result.stdout.length <= 16); | |
| assert.ok(Buffer.byteLength(result.stdout, "utf8") <= 16); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,11 @@ import { | |
| const DEFAULT_TIMEOUT_MS = 30_000; | ||
| const DEFAULT_MAX_OUTPUT_BYTES = 1_000_000; | ||
|
|
||
| interface CollectedOutput { | ||
| text: string; | ||
| truncated: boolean; | ||
| } | ||
|
|
||
| function quoteGitCommand(args: ReadonlyArray<string>): string { | ||
| return `git ${args.join(" ")}`; | ||
| } | ||
|
|
@@ -43,28 +48,44 @@ const collectOutput = Effect.fn(function* <E>( | |
| input: Pick<ExecuteGitInput, "operation" | "cwd" | "args">, | ||
| stream: Stream.Stream<Uint8Array, E>, | ||
| maxOutputBytes: number, | ||
| ): Effect.fn.Return<string, GitCommandError> { | ||
| truncateOutput: boolean, | ||
| ): Effect.fn.Return<CollectedOutput, GitCommandError> { | ||
| const decoder = new TextDecoder(); | ||
| let bytes = 0; | ||
| let text = ""; | ||
| let truncated = false; | ||
|
|
||
| yield* Stream.runForEach(stream, (chunk) => | ||
| Effect.gen(function* () { | ||
| bytes += chunk.byteLength; | ||
| if (bytes > maxOutputBytes) { | ||
| if (truncated) { | ||
| return; | ||
| } | ||
|
|
||
| const nextBytes = bytes + chunk.byteLength; | ||
| if (nextBytes > maxOutputBytes) { | ||
| if (truncateOutput) { | ||
| const remainingBytes = maxOutputBytes - bytes; | ||
| if (remainingBytes > 0) { | ||
| text += decoder.decode(chunk.subarray(0, remainingBytes), { stream: true }); | ||
| bytes = maxOutputBytes; | ||
| } | ||
| truncated = true; | ||
| return; | ||
| } | ||
| 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.`, | ||
| }); | ||
|
Comment on lines
75
to
80
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 Low When detail: `${quoteGitCommand(input.args)} output exceeded ${maxOutputBytes} bytes and was truncated.`,🚀 Reply "fix it for me" or copy this AI Prompt for your agent: |
||
| } | ||
| bytes = nextBytes; | ||
| text += decoder.decode(chunk, { stream: true }); | ||
| }), | ||
| ).pipe(Effect.mapError(toGitCommandError(input, "output stream failed."))); | ||
|
|
||
| text += decoder.decode(); | ||
| return text; | ||
| return { text, truncated }; | ||
| }); | ||
|
|
||
| const makeGitService = Effect.gen(function* () { | ||
|
|
@@ -77,6 +98,7 @@ const makeGitService = Effect.gen(function* () { | |
| } as const; | ||
| const timeoutMs = input.timeoutMs ?? DEFAULT_TIMEOUT_MS; | ||
| const maxOutputBytes = input.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES; | ||
| const truncateOutput = input.truncateOutput ?? false; | ||
|
|
||
| const commandEffect = Effect.gen(function* () { | ||
| const child = yield* commandSpawner | ||
|
|
@@ -90,8 +112,8 @@ const makeGitService = Effect.gen(function* () { | |
|
|
||
| const [stdout, stderr, exitCode] = yield* Effect.all( | ||
| [ | ||
| collectOutput(commandInput, child.stdout, maxOutputBytes), | ||
| collectOutput(commandInput, child.stderr, maxOutputBytes), | ||
| collectOutput(commandInput, child.stdout, maxOutputBytes, truncateOutput), | ||
| collectOutput(commandInput, child.stderr, maxOutputBytes, truncateOutput), | ||
| child.exitCode.pipe( | ||
| Effect.map((value) => Number(value)), | ||
| Effect.mapError(toGitCommandError(commandInput, "failed to report exit code.")), | ||
|
|
@@ -101,7 +123,7 @@ const makeGitService = Effect.gen(function* () { | |
| ); | ||
|
|
||
| if (!input.allowNonZeroExit && exitCode !== 0) { | ||
| const trimmedStderr = stderr.trim(); | ||
| const trimmedStderr = stderr.text.trim(); | ||
| return yield* new GitCommandError({ | ||
| operation: commandInput.operation, | ||
| command: quoteGitCommand(commandInput.args), | ||
|
|
@@ -113,7 +135,13 @@ const makeGitService = Effect.gen(function* () { | |
| }); | ||
| } | ||
|
|
||
| return { code: exitCode, stdout, stderr } satisfies ExecuteGitResult; | ||
| return { | ||
| code: exitCode, | ||
| stdout: stdout.text, | ||
| stderr: stderr.text, | ||
| stdoutTruncated: stdout.truncated, | ||
| stderrTruncated: stderr.truncated, | ||
| } satisfies ExecuteGitResult; | ||
| }); | ||
|
|
||
| return yield* commandEffect.pipe( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For
prepareCommitContextthe staged patch is later truncated to 50,000 chars inGitManager.limitContext(...), but thisgit diff --cached --patchcall still captures up to the GitService defaultmaxOutputBytes(1,000,000). Consider passing an explicit smallermaxOutputByteshere (plus a small buffer) to avoid unnecessary read/decoding work on large diffs and to make the intended cap explicit at this call site.