Skip to content

fix(shell): cap capture memory in real-time with concurrent drain task - #11795

Merged
jbg merged 15 commits into
aaif-goose:mainfrom
Abhijay007:fix/shell-capture-limit-concurrent-drain
Dec 2, 2026
Merged

jbg merged 15 commits into
aaif-goose:mainfrom
Abhijay007:fix/shell-capture-limit-concurrent-drain

Conversation

@Abhijay007

Copy link
Copy Markdown
Collaborator

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

@Abhijay007
Abhijay007 marked this pull request as draft September 2, 2026 17:22
@Abhijay007
Abhijay007 force-pushed the fix/shell-capture-limit-concurrent-drain branch from 5379b6e to 4a4f82d Compare September 2, 2026 17:22

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

@Abhijay007
Abhijay007 force-pushed the fix/shell-capture-limit-concurrent-drain branch from 4a4f82d to 7636741 Compare September 2, 2026 17:24
@Abhijay007
Abhijay007 marked this pull request as ready for review September 2, 2026 17:25

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

@Abhijay007
Abhijay007 force-pushed the fix/shell-capture-limit-concurrent-drain branch from 7636741 to 9398df9 Compare September 2, 2026 17:34

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

@jbg

jbg commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

@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.

@lifeizhou-ap lifeizhou-ap removed their assignment Sep 9, 2026
@Abhijay007

Copy link
Copy Markdown
Collaborator Author

@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

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as stale because it has not had recent activity for 23 days.

What happens next?

  • If no further activity occurs, this PR will be automatically closed in 7 days
  • To keep this PR active, simply add a comment, push new commits, or add the keep-open label
  • If you believe this PR was marked as stale in error, please comment and we'll review it

Thank you for your contribution! 🚀

@github-actions github-actions Bot added the stale label Oct 14, 2026
@jbg

jbg commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

thanks @Abhijay007 - can you give this a rebase and I'll review

@Abhijay007
Abhijay007 force-pushed the fix/shell-capture-limit-concurrent-drain branch from 5412100 to a0d4336 Compare October 14, 2026 19:52
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-02T16:23:54.928393Z 54d6137 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Abhijay007

Copy link
Copy Markdown
Collaborator Author

thanks @Abhijay007 - can you give this a rebase and I'll review

Hi @jbg I rebased it

@Abhijay007 Abhijay007 removed the stale label Oct 14, 2026

@jbg jbg left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

@DOsinga DOsinga closed this Dec 2, 2026
auto-merge was automatically disabled December 2, 2026 15:30

Pull request was closed

@Abhijay007 Abhijay007 reopened this Dec 2, 2026
@Abhijay007

Copy link
Copy Markdown
Collaborator Author

Hi @DOsinga I will address em today, sorry for the delay on this one

@Abhijay007
Abhijay007 requested a review from jbg December 2, 2026 15:42
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.
@Abhijay007
Abhijay007 force-pushed the fix/shell-capture-limit-concurrent-drain branch from 3a12d5c to 8a7b029 Compare December 2, 2026 15:55

@jbg jbg left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

@Abhijay007
Abhijay007 requested a review from jbg December 2, 2026 16:13

@jbg jbg left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

@jbg
jbg enabled auto-merge December 2, 2026 16:21
@jbg
jbg added this pull request to the merge queue Dec 2, 2026
Merged via the queue into aaif-goose:main with commit c56d53e Dec 2, 2026
43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

developer__shell captures command output with no cap, and persists it two to three times

4 participants