Skip to content

Conversation

@roomote
Copy link

@roomote roomote bot commented Nov 4, 2025

Description

This PR attempts to address Issue #9019 where terminal command output is not reliably captured when it's the first command in a freshly created terminal session. The issue is particularly noticeable with chained commands like ENV=hello && echo $ENV.

Problem

There's a race condition where the shell integration stream might not be fully ready when the first command is executed in a newly created terminal, causing the output to be lost even though the command executes successfully.

Solution

This fix implements a two-pronged approach:

  1. Terminal initialization delay: Added a 200ms delay for the first command in newly created terminals to ensure shell integration is fully ready
  2. Improved stream handling: Enhanced the stream data processing to better handle cases where shell integration markers are delayed or missing

Changes

  • Added tracking for newly created terminals (isNewlyCreated flag)
  • Implemented a one-time 200ms delay before executing the first command in new terminals
  • Fixed logic error in stream chunk tracking (replaced streamDataReceived with chunksReceived counter)
  • Added fallback logic to treat accumulated data as output if markers aren't found after 500 characters
  • Added comprehensive documentation explaining timing values and rationale

Testing

  • All existing terminal tests pass
  • The fix addresses the reproduction case: ENV=hello && echo $ENV now reliably captures output
  • No regression in existing terminal functionality

Notes

  • The 200ms delay and 500-character threshold are empirically chosen values that balance reliability with performance
  • These values may need adjustment for different systems or shells
  • Future work could make these values configurable

Fixes #9019

Feedback and guidance are welcome!


Important

Fixes terminal output capture race condition by adding delay for first command and improving stream handling in new terminals.

  • Behavior:
    • Adds 200ms delay for first command in new terminals in Terminal.ts to ensure shell integration readiness.
    • Improves stream data handling in TerminalProcess.ts to manage delayed or missing shell integration markers.
  • Implementation:
    • Introduces isNewlyCreated and firstCommandExecuted flags in Terminal class.
    • Replaces streamDataReceived with chunksReceived counter in TerminalProcess.
    • Adds fallback logic in TerminalProcess to treat data as output if markers aren't found after 500 characters.
  • Testing:
    • All existing tests pass; fix confirmed for ENV=hello && echo $ENV case.
    • No regression in terminal functionality.

This description was created by Ellipsis for 087ddf2. You can customize this summary. It will automatically update as commits are pushed.

- Add 200ms delay for first command in newly created terminals to ensure shell integration is ready
- Improve stream data handling to recover from missing shell integration markers
- Track whether terminal is newly created and if first command has been executed
- Add fallback logic to treat accumulated data as output if markers aren't found after 500 chars

This fixes the race condition where the first command's output might not be captured
properly in freshly created terminals, especially with chained commands.

Fixes #9019
- Fix logic error where streamDataReceived check was always false
- Replace with chunksReceived counter to properly track stream chunks
- Add detailed documentation explaining timing values and rationale
- Improve comments to explain the 200ms delay and 500 char threshold choices
@roomote roomote bot requested review from cte, jr and mrubens as code owners November 4, 2025 13:35
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Nov 4, 2025
@roomote
Copy link
Author

roomote bot commented Nov 4, 2025

See this task on Roo Code Cloud

Review complete. The implementation correctly addresses the terminal output capture race condition with a 200ms delay for first commands in new terminals and improved fallback logic. Patch 2 fixes the logic error from patch 1 by replacing the always-false boolean check with a chunk counter. No issues found.

Mention @roomote in a comment to trigger your PR Fixer agent and make changes to this pull request.

@dosubot dosubot bot added bug Something isn't working documentation Improvements or additions to documentation labels Nov 4, 2025
@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Nov 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working documentation Improvements or additions to documentation Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. size:M This PR changes 30-99 lines, ignoring generated files.

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

[BUG] Capturing command output is flaky if it's the first command in the terminal session

3 participants