Skip to content

fix(executor): append newline when shell command stdout lacks trailing newline#2979

Merged
tusharmath merged 1 commit intomainfrom
fix-shell-newline
Apr 13, 2026
Merged

fix(executor): append newline when shell command stdout lacks trailing newline#2979
tusharmath merged 1 commit intomainfrom
fix-shell-newline

Conversation

@tusharmath
Copy link
Copy Markdown
Collaborator

@tusharmath tusharmath commented Apr 13, 2026

Summary

Fix a display bug where forge's LLM response text was concatenated onto the same line as shell command output when the command did not emit a trailing newline.

Context

When running a shell command that produces output without a trailing newline (e.g. printf 'abc'), the terminal cursor is left mid-line. Because forge streams command stdout directly to the printer — bypassing the StreamingWriter's ends_with_newline tracker — forge had no awareness that the cursor was mid-line. As a result, the LLM's response text would immediately follow the command output on the same line, producing garbled output like:

● [11:39:31] Execute [/bin/zsh] printf 'abc'
abcabc printed without a trailing newline using printf 'abc'.

Changes

  • In execute_command_internal (crates/forge_infra/src/executor.rs), after streaming non-silent stdout, check if the last byte written was not a newline and the buffer is non-empty. If so, write a \n to the printer to move the cursor to a fresh line before any subsequent forge output.

Key Implementation Details

The fix is in ForgeCommandExecutorService::execute_command_internal. After the tokio::try_join! that streams stdout/stderr, the captured stdout buffer (result.1) is inspected. If its last byte is not \n and the buffer is non-empty, a newline is written directly to the output_printer. This is intentionally done at the infrastructure layer — right after the raw bytes are streamed — so every code path that renders shell output benefits automatically, regardless of which agent or tool triggered the command.

Use Cases

  • Any command that intentionally omits a trailing newline (e.g. printf 'abc', echo -n foo, custom scripts) will now produce clean, separated output.
  • Before the fix: abcThe command ran successfully.
  • After the fix:
    abc
    The command ran successfully.
    

Testing

cargo run -- -p "Use the shell command to print abc without a new line"

Expected output (after fix):

● [...] Execute [/bin/zsh] printf 'abc'
abc
Output: abc (no trailing newline)
● [...] Finished ...

The response text should appear on a new line after abc, not concatenated to it.

@tusharmath tusharmath changed the title fix(executor): append newline when shell command output lacks trailing newline fix(executor): append newline when shell command stdout lacks trailing newline Apr 13, 2026
@github-actions github-actions bot added type: feature Brand new functionality, features, pages, workflows, endpoints, etc. type: fix Iterations on existing features or infrastructure. labels Apr 13, 2026
@tusharmath tusharmath enabled auto-merge (squash) April 13, 2026 06:17
@tusharmath tusharmath merged commit 66ab313 into main Apr 13, 2026
12 checks passed
@tusharmath tusharmath deleted the fix-shell-newline branch April 13, 2026 06:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: feature Brand new functionality, features, pages, workflows, endpoints, etc. type: fix Iterations on existing features or infrastructure.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant