-
Notifications
You must be signed in to change notification settings - Fork 14.4k
fix(core): throttle shell text output and bound live UI buffer #26955
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
Merged
scidomino
merged 4 commits into
google-gemini:main
from
emersonbusson:fix-shell-output-jank-25459
May 13, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cf95286
test: add shell output jank regressions
emersonbusson 6e81ebc
fix: throttle shell live text output
emersonbusson 011fdf0
fix(core): trailing-edge flush for throttled shell text output
emersonbusson 93553c6
fix(core): harden shell live output flushing
emersonbusson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,10 +58,29 @@ import { | |
| } from '../sandbox/utils/proactivePermissions.js'; | ||
|
|
||
| export const OUTPUT_UPDATE_INTERVAL_MS = 1000; | ||
| export const LIVE_OUTPUT_MAX_BUFFER_CHARS = 100_000; | ||
|
|
||
| // Delay so user does not see the output of the process before the process is moved to the background. | ||
| const BACKGROUND_DELAY_MS = 200; | ||
| const SHOW_NL_DESCRIPTION_THRESHOLD = 150; | ||
| const LOW_SURROGATE_START = 0xdc00; | ||
| const LOW_SURROGATE_END = 0xdfff; | ||
|
|
||
| function trimLiveOutputBuffer(output: string): string { | ||
| if (output.length <= LIVE_OUTPUT_MAX_BUFFER_CHARS) { | ||
| return output; | ||
| } | ||
|
|
||
| let startIndex = output.length - LIVE_OUTPUT_MAX_BUFFER_CHARS; | ||
| const firstCodeUnit = output.charCodeAt(startIndex); | ||
| if ( | ||
| firstCodeUnit >= LOW_SURROGATE_START && | ||
| firstCodeUnit <= LOW_SURROGATE_END | ||
| ) { | ||
| startIndex += 1; | ||
| } | ||
| return output.slice(startIndex); | ||
| } | ||
|
|
||
| export interface ShellToolParams { | ||
| command: string; | ||
|
|
@@ -470,6 +489,7 @@ export class ShellToolInvocation extends BaseToolInvocation< | |
| const timeoutMs = this.context.config.getShellToolInactivityTimeout(); | ||
| const timeoutController = new AbortController(); | ||
| let timeoutTimer: NodeJS.Timeout | undefined; | ||
| let trailingFlushTimer: ReturnType<typeof setTimeout> | null = null; | ||
|
|
||
| // Handle signal combination manually to avoid TS issues or runtime missing features | ||
| const combinedController = new AbortController(); | ||
|
|
@@ -502,9 +522,61 @@ export class ShellToolInvocation extends BaseToolInvocation< | |
| }; | ||
| } | ||
| let cumulativeOutput: string | AnsiOutput = ''; | ||
| let lastUpdateTime = Date.now(); | ||
| let lastUpdateTime = 0; | ||
| let hasFlushedOutput = false; | ||
| let hasPendingOutput = false; | ||
| let isBinaryStream = false; | ||
|
|
||
| const appendToLiveOutputBuffer = (chunk: string) => { | ||
| const currentOutput = | ||
| typeof cumulativeOutput === 'string' ? cumulativeOutput : ''; | ||
| if (chunk.length >= LIVE_OUTPUT_MAX_BUFFER_CHARS) { | ||
| cumulativeOutput = trimLiveOutputBuffer(chunk); | ||
| return; | ||
| } | ||
|
|
||
| const nextOutput = currentOutput + chunk; | ||
| cumulativeOutput = trimLiveOutputBuffer(nextOutput); | ||
| }; | ||
|
|
||
| const cancelTrailingFlush = () => { | ||
| if (trailingFlushTimer !== null) { | ||
| clearTimeout(trailingFlushTimer); | ||
| trailingFlushTimer = null; | ||
| } | ||
| }; | ||
|
|
||
| const flushOutput = () => { | ||
| cancelTrailingFlush(); | ||
| if (!hasPendingOutput || !updateOutput || this.params.is_background) { | ||
| return; | ||
| } | ||
|
|
||
| updateOutput(cumulativeOutput); | ||
| hasPendingOutput = false; | ||
| hasFlushedOutput = true; | ||
| lastUpdateTime = Date.now(); | ||
| }; | ||
|
|
||
| const scheduleTrailingFlush = () => { | ||
| if ( | ||
| trailingFlushTimer !== null || | ||
| !updateOutput || | ||
| this.params.is_background | ||
| ) { | ||
| return; | ||
| } | ||
| const elapsedSinceLastUpdate = Date.now() - lastUpdateTime; | ||
| const trailingDelayMs = Math.max( | ||
| OUTPUT_UPDATE_INTERVAL_MS - elapsedSinceLastUpdate, | ||
| 0, | ||
| ); | ||
| trailingFlushTimer = setTimeout(() => { | ||
| trailingFlushTimer = null; | ||
| flushOutput(); | ||
| }, trailingDelayMs); | ||
| }; | ||
|
|
||
| const resetTimeout = () => { | ||
| if (timeoutMs <= 0) { | ||
| return; | ||
|
|
@@ -529,43 +601,53 @@ export class ShellToolInvocation extends BaseToolInvocation< | |
| cwd, | ||
| (event: ShellOutputEvent) => { | ||
| resetTimeout(); // Reset timeout on any event | ||
| if (!updateOutput) { | ||
| return; | ||
| } | ||
|
|
||
| let shouldUpdate = false; | ||
|
|
||
| switch (event.type) { | ||
| case 'data': | ||
| if (isBinaryStream) break; | ||
| cumulativeOutput = event.chunk; | ||
| shouldUpdate = true; | ||
| if (typeof event.chunk === 'string') { | ||
| appendToLiveOutputBuffer(event.chunk); | ||
| shouldUpdate = | ||
| !hasFlushedOutput || | ||
| Date.now() - lastUpdateTime > OUTPUT_UPDATE_INTERVAL_MS; | ||
| if (!shouldUpdate) { | ||
| scheduleTrailingFlush(); | ||
| } | ||
| } else { | ||
| cumulativeOutput = event.chunk; | ||
| shouldUpdate = true; | ||
| } | ||
| hasPendingOutput = true; | ||
| break; | ||
| case 'binary_detected': | ||
| isBinaryStream = true; | ||
| cumulativeOutput = | ||
| '[Binary output detected. Halting stream...]'; | ||
| hasPendingOutput = true; | ||
| shouldUpdate = true; | ||
| break; | ||
| case 'binary_progress': | ||
| isBinaryStream = true; | ||
| cumulativeOutput = `[Receiving binary output... ${formatBytes( | ||
| event.bytesReceived, | ||
| )} received]`; | ||
| hasPendingOutput = true; | ||
| if (Date.now() - lastUpdateTime > OUTPUT_UPDATE_INTERVAL_MS) { | ||
| shouldUpdate = true; | ||
| } | ||
| break; | ||
| case 'exit': | ||
| flushOutput(); | ||
| break; | ||
| default: { | ||
| throw new Error('An unhandled ShellOutputEvent was found.'); | ||
| } | ||
| } | ||
|
|
||
| if (shouldUpdate && !this.params.is_background) { | ||
| updateOutput(cumulativeOutput); | ||
| lastUpdateTime = Date.now(); | ||
| flushOutput(); | ||
| } | ||
|
Comment on lines
649
to
651
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. Update the data handler to schedule a trailing flush if the current event is throttled. This ensures that sporadic output is eventually displayed as an incremental delta even if the process doesn't exit immediately. References
|
||
| }, | ||
| combinedController.signal, | ||
|
|
@@ -639,6 +721,9 @@ export class ShellToolInvocation extends BaseToolInvocation< | |
| } | ||
|
|
||
| const result = await resultPromise; | ||
| if (!result.backgrounded) { | ||
| flushOutput(); | ||
| } | ||
|
|
||
| const backgroundPIDs: number[] = []; | ||
| if (os.platform() !== 'win32') { | ||
|
|
@@ -966,6 +1051,10 @@ export class ShellToolInvocation extends BaseToolInvocation< | |
| }; | ||
| } finally { | ||
| if (timeoutTimer) clearTimeout(timeoutTimer); | ||
| if (trailingFlushTimer) { | ||
| clearTimeout(trailingFlushTimer); | ||
| trailingFlushTimer = null; | ||
| } | ||
| signal.removeEventListener('abort', onAbort); | ||
| timeoutController.signal.removeEventListener('abort', onAbort); | ||
| if (tempFilePath) { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
To ensure that the last chunk of output is not stuck in the buffer when a command becomes silent but remains active, we should implement a trailing edge flush using a timer. This ensures that the UI is updated even if no further output events are received within the throttle interval. When implementing this, ensure the flush emits only the incremental delta instead of the full accumulated text to avoid redundant data transfer. The check for signal.aborted in flushOutput correctly utilizes the AbortSignal for cancellation safety. Note that while this check provides safety, it is still recommended to clear the timer in the finally block of the execute method to avoid unnecessary resource usage, although that block is outside the current diff scope.
References