Repository navigation
fix(shell): cap capture memory in real-time with concurrent drain task - #11795
Conversation
5379b6e to
4a4f82d
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5379b6eb4a
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
4a4f82d to
7636741
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7636741bf4
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
7636741 to
9398df9
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9398df9bb7
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d6bd94913e
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
@Abhijay007, this still allows unbounded memory use: the reader buffers until a newline, the channel is unbounded, and truncating a String retains its allocation. Please bound buffering from the read onward, while continuing to drain both streams. Add a regression case with output far above the limit and no newlines; verify bounded allocations and preservation of the actual beginning and end. The existing short-line test doesn’t cover that case. |
sure looing into it |
|
This pull request has been automatically marked as stale because it has not had recent activity for 23 days. What happens next?
Thank you for your contribution! 🚀 |
|
thanks @Abhijay007 - can you give this a rebase and I'll review |
5412100 to
a0d4336
Compare
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Hi @jbg I rebased it |
jbg
left a comment
There was a problem hiding this comment.
The existing collector now updates the bounded head/tail buffer directly. The focused capture tests passed, including a local check that the buffer stayed within its budget after collecting roughly 1 MB while the child was still running. Please fix the dropped-byte accounting below.
|
Hi @DOsinga I will address em today, sorry for the delay on this one |
The unbounded channel accumulated all command output until process exit, meaning a long-running chatty command could exhaust memory and fill tmpfs. - drain_with_budget runs concurrently so the channel stays near-empty and heap is bounded by GOOSE_SHELL_CAPTURE_LIMIT_BYTES (default 10 MiB) - head is permanently sealed once the first line overflows it, preserving arrival order across the head/tail boundary - lines exceeding half the budget are clamped so a single oversized line cannot bypass the budget after the eviction loop empties the tail - capture-limit notice is injected into the interleaved display only, keeping structured stdout/stderr clean for downstream consumers Closes aaif-goose#11122
…on notice When the capture limit is exceeded the saved file is a head/tail fragment, not the complete output. The dropped middle is unrecoverable, so telling the model 'Full output saved to <path>' is misleading. - add is_fragment to TruncationInfo and thread it through render_output and truncate_output - truncation_notice uses different wording for fragments: 'Captured fragment (beginning and end) saved to <path>; the dropped middle is unrecoverable' - fix String::truncate panic on multibyte UTF-8: snap to a valid char boundary before calling truncate instead of after - restore import ordering to match the project toolchain's rustfmt
Bound capture memory from the read onward while still draining both streams. - split long lines into segments of at most 64 KiB without splitting UTF-8 characters, and mark continuations so pieces are rejoined without inserting newlines - bound the capture queue by bytes with a semaphore instead of message count - charge a fixed per-record overhead so many tiny lines stay within the limit - track dropped bytes per stream so the saved-file notice only calls a stream a fragment when that stream lost data - report dropped bytes in the structured shell output - keep live output notifications free of newlines inside split lines - guard the capture limit env var in tests with env_lock Refs aaif-goose#11122
- only reset line continuation at the head/tail gap for streams that lost bytes, so a complete stream is not given an extra newline - size segments for worst-case lossy UTF-8 decoding, where each invalid byte becomes a 3-byte replacement character, so a decoded record cannot exceed its share of the capture limit - drop tests that duplicated coverage of the same behavior Refs aaif-goose#11122
The two tests that run real commands only use the public ShellTool API, so they belong with the other integration tests. Also trim comments and a redundant clamp. Refs aaif-goose#11122
Replace the capture channel, byte semaphore and drain task with a head/tail buffer that the collector updates as each bounded segment arrives. The buffer is shared with run_command so output captured before the post-exit drain timeout aborts the collector is still returned. - raise capture limits below 1 KiB to 1 KiB so every record fits in half the budget; a limit of 0 or 1 previously dropped or truncated small output - hold a trailing carriage return back when splitting a long line so CRLF across a segment boundary is still normalized in live output Refs aaif-goose#11122
The head is sealed as soon as the next record does not fit, which can leave part of its half of the budget unused. Size the tail from the total budget minus what the head actually holds, so output that fits the capture limit is not reported as truncated. Refs aaif-goose#11122
With a single capture buffer, a saved stream file may hold only its beginning or only its end, so the notice must not promise both.
Use a short gap marker in combined output so the saved file fits the budget. The tool result carries the detailed dropped-byte notice.
3a12d5c to
8a7b029
Compare
jbg
left a comment
There was a problem hiding this comment.
Thanks for fixing the dropped-byte counts. The remaining issue is the total saved for one call. With a 96 KiB limit, 80 KiB of stdout is saved twice, once in stdout-{slot} and again in output-{slot}. Please enforce the budget across all files saved for the call. I’d cover stdout-only and mixed stdout/stderr through the shell tool entry point and assert that the combined file sizes stay within the limit.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8a7b029380
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
jbg
left a comment
There was a problem hiding this comment.
Thanks for fixing the duplicate writes. The shell now saves one interleaved log per call, and the entry-point tests check the total bytes on disk for both stdout-only and mixed output. This addresses my remaining review feedback.
Fixes: #11122
Summary
The unbounded channel used to accumulate all command output until process exit, meaning a long-running or chatty command (tail -f, TUI, build) could exhaust memory and fill tmpfs before any byte budget was applied.
Fix: spawn drain_with_budget as a concurrent task that consumes from the channel while the process runs. The channel stays near-empty throughout execution and heap is bounded by GOOSE_SHELL_CAPTURE_LIMIT_BYTES (default 10 MiB) from the first byte of output.
The head/tail strategy (preserve first half + last half of budget) and the explicit truncation notice were already in place from the prior partial fix; this commit makes them effective during execution, not only after exit.
Testing
Manual testing