Skip to content

fix(stream): collapse terminal redraw controls - #2581

Open
YOMXXX wants to merge 2 commits into
rtk-ai:developfrom
YOMXXX:fix/issue-2230-terminal-control
Open

YOMXXX wants to merge 2 commits into
rtk-ai:developfrom
YOMXXX:fix/issue-2230-terminal-control

Conversation

@YOMXXX

@YOMXXX YOMXXX commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Summary

  • collapse common terminal redraw controls before captured output reaches filters
  • handle carriage-return progress rewrites and backspace overwrites for exec_capture and run_streaming capture/filter paths
  • preserve normal CRLF line endings while avoiding transient progress/spinner states in model-facing output

Fixes #2230

Verification

  • cargo +1.93.0 fmt --check
  • git diff --check
  • cargo +1.93.0 test core::stream::tests -- --nocapture
  • cargo +1.93.0 clippy --all-targets
  • cargo +1.93.0 test --all -- --skip small_grep_not_worse_than_plain

@YOMXXX

YOMXXX commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

Status note: the red benchmark check is not caused by this PR's terminal-control change. The failing row is curl text: CI compares curl -s https://httpbin.org/robots.txt at 8 tokens with rtk curl https://httpbin.org/robots.txt at 40 tokens, so the external network fixture can make the raw output smaller than the RTK path and trip the negative-savings gate.

I split the benchmark fixture fix into #2596, which replaces the httpbin curl benchmark with local file:// fixtures and is green. Once #2596 lands, rerunning this PR should clear benchmark; all other checks here are green.

pidefrem added a commit to pidefrem/rtk that referenced this pull request Jun 26, 2026
Adopts the design from upstream rtk-ai#2581: collapse_terminal_control
rewrites carriage-return progress redraws and backspace overwrites to the final
line state before captured output reaches the filters, on both run_streaming and
exec_capture. For a token-saving, model-facing tool this is better than emitting
every transient redraw frame (the earlier LineChunks approach this replaces):
the model sees 'Downloading 100%' once, not every percent.

Supersedes this fork's prior LineChunks commit; see brief 003. Real CRLF line
endings are preserved.
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jun 26, 2026
The adopted collapse (rtk-ai#2581) parses \r/\b/\n but has no CSI awareness, so an
erase-in-line sequence (ESC[K) — exactly how programs do a *shrinking* redraw
(\r + erase a longer previous frame) — was overlaid as literal characters: the
escape bytes leaked into output AND the stale tail survived (e.g.
'Downloading 100%\r\x1b[KDone' rendered 'x1b[KDoneding 100%' instead of 'Done').

Root cause: escape bytes and printable bytes went through different write paths.
Fix: parse a CSI sequence as a unit; honor ESC[K (0/1/2) against the cursor; overlay
every other CSI (SGR colour, motion) through the SAME shared write path as printable
text, so colour still passes through to the filters unchanged (prior contract) and a
later \r redraw overwrites prior escape bytes cell-for-cell instead of shifting them.

A bare \r with no erase still faithfully overlays (Downloading 100%\rDone ->
Doneloading 100%), matching a real terminal. Scope boundary: models the cursor, not
per-cell colour attributes (a full VT emulator, out of scope).

Tests: ESC[K to-EOL + mid-line, SGR passthrough, coloured \r-redraw overlay, bare-\r
faithful overlay, progress bar, backspace, plain/CRLF unchanged.

Delta over upstream rtk-ai#2581 — offer back as a review comment (see brief 003).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jun 26, 2026
…an't hang capture

When the direct child exits but a detached descendant keeps the captured pipe
open (e.g. dotnet's build server, or `ng build`'s esbuild --service grandchild),
reading until pipe EOF blocks forever. Replace the unbounded reads in run_streaming
with background reader threads plus a bounded post-exit drain: once the direct child
is reaped, only drain briefly (idle-grace + hard cap), and never join a reader still
blocked on a descendant-held pipe.

Re-derived on top of the collapse-to-final CRLF handling (003 / upstream rtk-ai#2581):
readers accumulate raw bytes, collapse_terminal_control is applied to the final
buffers. Tests cover both capture and streaming paths plus fast-exit no-truncation.

This is the run_streaming half of the orphaned-pipe hang; upstream rtk-ai#2322 fixes the
exec_capture half only. See brief 004 (issue rtk-ai#2320).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jun 26, 2026
Adopts the design from upstream rtk-ai#2581: collapse_terminal_control
rewrites carriage-return progress redraws and backspace overwrites to the final
line state before captured output reaches the filters, on both run_streaming and
exec_capture. For a token-saving, model-facing tool this is better than emitting
every transient redraw frame (the earlier LineChunks approach this replaces):
the model sees 'Downloading 100%' once, not every percent.

Supersedes this fork's prior LineChunks commit; see brief 003. Real CRLF line
endings are preserved.
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jun 26, 2026
The adopted collapse (rtk-ai#2581) parses \r/\b/\n but has no CSI awareness, so an
erase-in-line sequence (ESC[K) — exactly how programs do a *shrinking* redraw
(\r + erase a longer previous frame) — was overlaid as literal characters: the
escape bytes leaked into output AND the stale tail survived (e.g.
'Downloading 100%\r\x1b[KDone' rendered 'x1b[KDoneding 100%' instead of 'Done').

Root cause: escape bytes and printable bytes went through different write paths.
Fix: parse a CSI sequence as a unit; honor ESC[K (0/1/2) against the cursor; overlay
every other CSI (SGR colour, motion) through the SAME shared write path as printable
text, so colour still passes through to the filters unchanged (prior contract) and a
later \r redraw overwrites prior escape bytes cell-for-cell instead of shifting them.

A bare \r with no erase still faithfully overlays (Downloading 100%\rDone ->
Doneloading 100%), matching a real terminal. Scope boundary: models the cursor, not
per-cell colour attributes (a full VT emulator, out of scope).

Tests: ESC[K to-EOL + mid-line, SGR passthrough, coloured \r-redraw overlay, bare-\r
faithful overlay, progress bar, backspace, plain/CRLF unchanged.

Delta over upstream rtk-ai#2581 — offer back as a review comment (see brief 003).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jun 26, 2026
…an't hang capture

When the direct child exits but a detached descendant keeps the captured pipe
open (e.g. dotnet's build server, or `ng build`'s esbuild --service grandchild),
reading until pipe EOF blocks forever. Replace the unbounded reads in run_streaming
with background reader threads plus a bounded post-exit drain: once the direct child
is reaped, only drain briefly (idle-grace + hard cap), and never join a reader still
blocked on a descendant-held pipe.

Re-derived on top of the collapse-to-final CRLF handling (003 / upstream rtk-ai#2581):
readers accumulate raw bytes, collapse_terminal_control is applied to the final
buffers. Tests cover both capture and streaming paths plus fast-exit no-truncation.

This is the run_streaming half of the orphaned-pipe hang; upstream rtk-ai#2322 fixes the
exec_capture half only. See brief 004 (issue rtk-ai#2320).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jun 26, 2026
Adopts the design from upstream rtk-ai#2581: collapse_terminal_control
rewrites carriage-return progress redraws and backspace overwrites to the final
line state before captured output reaches the filters, on both run_streaming and
exec_capture. For a token-saving, model-facing tool this is better than emitting
every transient redraw frame (the earlier LineChunks approach this replaces):
the model sees 'Downloading 100%' once, not every percent.

Supersedes this fork's prior LineChunks commit; see brief 003. Real CRLF line
endings are preserved.
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jun 26, 2026
The adopted collapse (rtk-ai#2581) parses \r/\b/\n but has no CSI awareness, so an
erase-in-line sequence (ESC[K) — exactly how programs do a *shrinking* redraw
(\r + erase a longer previous frame) — was overlaid as literal characters: the
escape bytes leaked into output AND the stale tail survived (e.g.
'Downloading 100%\r\x1b[KDone' rendered 'x1b[KDoneding 100%' instead of 'Done').

Root cause: escape bytes and printable bytes went through different write paths.
Fix: parse a CSI sequence as a unit; honor ESC[K (0/1/2) against the cursor; overlay
every other CSI (SGR colour, motion) through the SAME shared write path as printable
text, so colour still passes through to the filters unchanged (prior contract) and a
later \r redraw overwrites prior escape bytes cell-for-cell instead of shifting them.

A bare \r with no erase still faithfully overlays (Downloading 100%\rDone ->
Doneloading 100%), matching a real terminal. Scope boundary: models the cursor, not
per-cell colour attributes (a full VT emulator, out of scope).

Tests: ESC[K to-EOL + mid-line, SGR passthrough, coloured \r-redraw overlay, bare-\r
faithful overlay, progress bar, backspace, plain/CRLF unchanged.

Delta over upstream rtk-ai#2581 — offer back as a review comment (see brief 003).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jun 26, 2026
…an't hang capture

When the direct child exits but a detached descendant keeps the captured pipe
open (e.g. dotnet's build server, or `ng build`'s esbuild --service grandchild),
reading until pipe EOF blocks forever. Replace the unbounded reads in run_streaming
with background reader threads plus a bounded post-exit drain: once the direct child
is reaped, only drain briefly (idle-grace + hard cap), and never join a reader still
blocked on a descendant-held pipe.

Re-derived on top of the collapse-to-final CRLF handling (003 / upstream rtk-ai#2581):
readers accumulate raw bytes, collapse_terminal_control is applied to the final
buffers. Tests cover both capture and streaming paths plus fast-exit no-truncation.

This is the run_streaming half of the orphaned-pipe hang; upstream rtk-ai#2322 fixes the
exec_capture half only. See brief 004 (issue rtk-ai#2320).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jun 26, 2026
Adopts the design from upstream rtk-ai#2581: collapse_terminal_control
rewrites carriage-return progress redraws and backspace overwrites to the final
line state before captured output reaches the filters, on both run_streaming and
exec_capture. For a token-saving, model-facing tool this is better than emitting
every transient redraw frame (the earlier LineChunks approach this replaces):
the model sees 'Downloading 100%' once, not every percent.

Supersedes this fork's prior LineChunks commit; see brief 003. Real CRLF line
endings are preserved.
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jun 26, 2026
The adopted collapse (rtk-ai#2581) parses \r/\b/\n but has no CSI awareness, so an
erase-in-line sequence (ESC[K) — exactly how programs do a *shrinking* redraw
(\r + erase a longer previous frame) — was overlaid as literal characters: the
escape bytes leaked into output AND the stale tail survived (e.g.
'Downloading 100%\r\x1b[KDone' rendered 'x1b[KDoneding 100%' instead of 'Done').

Root cause: escape bytes and printable bytes went through different write paths.
Fix: parse a CSI sequence as a unit; honor ESC[K (0/1/2) against the cursor; overlay
every other CSI (SGR colour, motion) through the SAME shared write path as printable
text, so colour still passes through to the filters unchanged (prior contract) and a
later \r redraw overwrites prior escape bytes cell-for-cell instead of shifting them.

A bare \r with no erase still faithfully overlays (Downloading 100%\rDone ->
Doneloading 100%), matching a real terminal. Scope boundary: models the cursor, not
per-cell colour attributes (a full VT emulator, out of scope).

Tests: ESC[K to-EOL + mid-line, SGR passthrough, coloured \r-redraw overlay, bare-\r
faithful overlay, progress bar, backspace, plain/CRLF unchanged.

Delta over upstream rtk-ai#2581 — offer back as a review comment (see brief 003).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jun 26, 2026
…an't hang capture

When the direct child exits but a detached descendant keeps the captured pipe
open (e.g. dotnet's build server, or `ng build`'s esbuild --service grandchild),
reading until pipe EOF blocks forever. Replace the unbounded reads in run_streaming
with background reader threads plus a bounded post-exit drain: once the direct child
is reaped, only drain briefly (idle-grace + hard cap), and never join a reader still
blocked on a descendant-held pipe.

Re-derived on top of the collapse-to-final CRLF handling (003 / upstream rtk-ai#2581):
readers accumulate raw bytes, collapse_terminal_control is applied to the final
buffers. Tests cover both capture and streaming paths plus fast-exit no-truncation.

This is the run_streaming half of the orphaned-pipe hang; upstream rtk-ai#2322 fixes the
exec_capture half only. See brief 004 (issue rtk-ai#2320).
@pidefrem

Copy link
Copy Markdown

Thanks for this — I picked it up while working on the same capture path and it's the right approach (collapse to the final rendered line, on both exec_capture and run_streaming). One correctness issue I hit while testing, with a fix, in case it's useful:

collapse_terminal_control has no CSI awareness, so ESC[K (erase-in-line) is overlaid as literal characters. That's the sequence programs emit for a shrinking redraw — \r to column 0, then erase a longer previous frame, then write the shorter one. Today it both leaks the escape bytes and leaves the stale tail:

input : "Downloading 100%\r\x1b[KDone\n"
#2581 : "\x1b[KDoneding 100%\n"   // escape leaked + "loading 100%" tail survives
want  : "Done\n"

Root cause is that escape bytes and printable bytes go through different write paths. The fix that worked for me: parse a CSI sequence (ESC [ … final) as a unit, honor ESC[K (params 0/1/2) against the cursor, and overlay every other CSI (SGR colour, cursor motion) through the same write path as printable text — so colour still passes through untouched and a later \r redraw overwrites prior escape bytes cell-for-cell instead of shifting them.

A couple of cases I'd suggest adding as regression tests (all pass with the fix):

  • "Downloading 100%\r\x1b[KDone\n" → "Done\n" (erase-to-EOL clears tail, no leak)
  • "abcdef\rXY\x1b[K\n" → "XY\n" (mid-line erase)
  • "\x1b[32mPASS\x1b[0m\n" → unchanged (SGR colour must pass through — strip_ansi's job)
  • "Downloading 100%\rDone\n" → "Doneloading 100%\n" (bare \r, no erase, stays faithful to what a real terminal renders — this one should NOT change)

One observation while validating, not a criticism: rtk captures over a pipe, and I measured that tools suppress \r progress redraws when stdout isn't a TTY. Piped (the capture path), pip3 download, npm, brew, and cargo build --color=always all emit zero \r (cargo keeps colour, drops the redraw); only a program that ignores isatty() produced any. So the \r-collapse mostly pays off on a PTY path / tools that force progress, whereas the ESC[K and \b handling is what reliably reaches the filters under piped capture. Doesn't change your approach at all — \r/\b/ESC[K as one cursor model is right; just context in case you're weighting where it helps.

I have the fix implemented and tested on my fork — commit 1f32130 on branch fix/stream-crlf-line-splitting (which builds on your collapse_terminal_control). Happy to open it as a PR against your branch, or you can take the diff directly — whichever you prefer.

@YOMXXX

YOMXXX commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in 7fb1339 (fix(stream): honor erase-line terminal controls).

What changed:

  • collapse_terminal_control now parses CSI sequences as a unit.
  • ESC[K / ESC[0K erases from the cursor to EOL, so shrinking redraws no longer leak \x1b[K or preserve stale tails.
  • ESC[1K and ESC[2K are handled in the same erase-line path.
  • Non-erase CSI sequences, including SGR color like ESC[32m, still pass through the same overlay path unchanged.
  • Bare \r behavior is unchanged, so Downloading 100%\rDone\n still renders as Doneloading 100%\n unless an erase-line sequence is present.

Regression coverage added for:

  • Downloading 100%\rESC[KDone\n -> Done\n
  • abcdef\rXYESC[K\n -> XY\n
  • SGR preservation
  • bare CR tail preservation

Local verification:

  • cargo +1.93.0 fmt --check
  • git diff --check
  • cargo +1.93.0 test collapse_terminal_control -- --nocapture
  • cargo +1.93.0 test core::stream::tests -- --nocapture
  • cargo +1.93.0 clippy --all-targets

pidefrem added a commit to pidefrem/rtk that referenced this pull request Jun 26, 2026
Adopts the design from upstream rtk-ai#2581: collapse_terminal_control
rewrites carriage-return progress redraws and backspace overwrites to the final
line state before captured output reaches the filters, on both run_streaming and
exec_capture. For a token-saving, model-facing tool this is better than emitting
every transient redraw frame (the earlier LineChunks approach this replaces):
the model sees 'Downloading 100%' once, not every percent.

Supersedes this fork's prior LineChunks commit; see brief 003. Real CRLF line
endings are preserved.
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jun 26, 2026
The adopted collapse (rtk-ai#2581) parses \r/\b/\n but has no CSI awareness, so an
erase-in-line sequence (ESC[K) — exactly how programs do a *shrinking* redraw
(\r + erase a longer previous frame) — was overlaid as literal characters: the
escape bytes leaked into output AND the stale tail survived (e.g.
'Downloading 100%\r\x1b[KDone' rendered 'x1b[KDoneding 100%' instead of 'Done').

Root cause: escape bytes and printable bytes went through different write paths.
Fix: parse a CSI sequence as a unit; honor ESC[K (0/1/2) against the cursor; overlay
every other CSI (SGR colour, motion) through the SAME shared write path as printable
text, so colour still passes through to the filters unchanged (prior contract) and a
later \r redraw overwrites prior escape bytes cell-for-cell instead of shifting them.

A bare \r with no erase still faithfully overlays (Downloading 100%\rDone ->
Doneloading 100%), matching a real terminal. Scope boundary: models the cursor, not
per-cell colour attributes (a full VT emulator, out of scope).

Tests: ESC[K to-EOL + mid-line, SGR passthrough, coloured \r-redraw overlay, bare-\r
faithful overlay, progress bar, backspace, plain/CRLF unchanged.

Delta over upstream rtk-ai#2581 — offer back as a review comment (see brief 003).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jun 26, 2026
…an't hang capture

When the direct child exits but a detached descendant keeps the captured pipe
open (e.g. dotnet's build server, or `ng build`'s esbuild --service grandchild),
reading until pipe EOF blocks forever. Replace the unbounded reads in run_streaming
with background reader threads plus a bounded post-exit drain: once the direct child
is reaped, only drain briefly (idle-grace + hard cap), and never join a reader still
blocked on a descendant-held pipe.

Re-derived on top of the collapse-to-final CRLF handling (003 / upstream rtk-ai#2581):
readers accumulate raw bytes, collapse_terminal_control is applied to the final
buffers. Tests cover both capture and streaming paths plus fast-exit no-truncation.

This is the run_streaming half of the orphaned-pipe hang; upstream rtk-ai#2322 fixes the
exec_capture half only. See brief 004 (issue rtk-ai#2320).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jun 26, 2026
Adopts the design from upstream rtk-ai#2581: collapse_terminal_control
rewrites carriage-return progress redraws and backspace overwrites to the final
line state before captured output reaches the filters, on both run_streaming and
exec_capture. For a token-saving, model-facing tool this is better than emitting
every transient redraw frame (the earlier LineChunks approach this replaces):
the model sees 'Downloading 100%' once, not every percent.

Supersedes this fork's prior LineChunks commit; see brief 003. Real CRLF line
endings are preserved.
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jun 26, 2026
The adopted collapse (rtk-ai#2581) parses \r/\b/\n but has no CSI awareness, so an
erase-in-line sequence (ESC[K) — exactly how programs do a *shrinking* redraw
(\r + erase a longer previous frame) — was overlaid as literal characters: the
escape bytes leaked into output AND the stale tail survived (e.g.
'Downloading 100%\r\x1b[KDone' rendered 'x1b[KDoneding 100%' instead of 'Done').

Root cause: escape bytes and printable bytes went through different write paths.
Fix: parse a CSI sequence as a unit; honor ESC[K (0/1/2) against the cursor; overlay
every other CSI (SGR colour, motion) through the SAME shared write path as printable
text, so colour still passes through to the filters unchanged (prior contract) and a
later \r redraw overwrites prior escape bytes cell-for-cell instead of shifting them.

A bare \r with no erase still faithfully overlays (Downloading 100%\rDone ->
Doneloading 100%), matching a real terminal. Scope boundary: models the cursor, not
per-cell colour attributes (a full VT emulator, out of scope).

Tests: ESC[K to-EOL + mid-line, SGR passthrough, coloured \r-redraw overlay, bare-\r
faithful overlay, progress bar, backspace, plain/CRLF unchanged.

Delta over upstream rtk-ai#2581 — offer back as a review comment (see brief 003).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jun 26, 2026
…an't hang capture

When the direct child exits but a detached descendant keeps the captured pipe
open (e.g. dotnet's build server, or `ng build`'s esbuild --service grandchild),
reading until pipe EOF blocks forever. Replace the unbounded reads in run_streaming
with background reader threads plus a bounded post-exit drain: once the direct child
is reaped, only drain briefly (idle-grace + hard cap), and never join a reader still
blocked on a descendant-held pipe.

Re-derived on top of the collapse-to-final CRLF handling (003 / upstream rtk-ai#2581):
readers accumulate raw bytes, collapse_terminal_control is applied to the final
buffers. Tests cover both capture and streaming paths plus fast-exit no-truncation.

This is the run_streaming half of the orphaned-pipe hang; upstream rtk-ai#2322 fixes the
exec_capture half only. See brief 004 (issue rtk-ai#2320).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jul 18, 2026
Adopts the design from upstream rtk-ai#2581: collapse_terminal_control
rewrites carriage-return progress redraws and backspace overwrites to the final
line state before captured output reaches the filters, on both run_streaming and
exec_capture. For a token-saving, model-facing tool this is better than emitting
every transient redraw frame (the earlier LineChunks approach this replaces):
the model sees 'Downloading 100%' once, not every percent.

Supersedes this fork's prior LineChunks commit; see brief 003. Real CRLF line
endings are preserved.
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jul 18, 2026
The adopted collapse (rtk-ai#2581) parses \r/\b/\n but has no CSI awareness, so an
erase-in-line sequence (ESC[K) — exactly how programs do a *shrinking* redraw
(\r + erase a longer previous frame) — was overlaid as literal characters: the
escape bytes leaked into output AND the stale tail survived (e.g.
'Downloading 100%\r\x1b[KDone' rendered 'x1b[KDoneding 100%' instead of 'Done').

Root cause: escape bytes and printable bytes went through different write paths.
Fix: parse a CSI sequence as a unit; honor ESC[K (0/1/2) against the cursor; overlay
every other CSI (SGR colour, motion) through the SAME shared write path as printable
text, so colour still passes through to the filters unchanged (prior contract) and a
later \r redraw overwrites prior escape bytes cell-for-cell instead of shifting them.

A bare \r with no erase still faithfully overlays (Downloading 100%\rDone ->
Doneloading 100%), matching a real terminal. Scope boundary: models the cursor, not
per-cell colour attributes (a full VT emulator, out of scope).

Tests: ESC[K to-EOL + mid-line, SGR passthrough, coloured \r-redraw overlay, bare-\r
faithful overlay, progress bar, backspace, plain/CRLF unchanged.

Delta over upstream rtk-ai#2581 — offer back as a review comment (see brief 003).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jul 18, 2026
Adopts the design from upstream rtk-ai#2581: collapse_terminal_control
rewrites carriage-return progress redraws and backspace overwrites to the final
line state before captured output reaches the filters, on both run_streaming and
exec_capture. For a token-saving, model-facing tool this is better than emitting
every transient redraw frame (the earlier LineChunks approach this replaces):
the model sees 'Downloading 100%' once, not every percent.

Supersedes this fork's prior LineChunks commit; see brief 003. Real CRLF line
endings are preserved.
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jul 18, 2026
The adopted collapse (rtk-ai#2581) parses \r/\b/\n but has no CSI awareness, so an
erase-in-line sequence (ESC[K) — exactly how programs do a *shrinking* redraw
(\r + erase a longer previous frame) — was overlaid as literal characters: the
escape bytes leaked into output AND the stale tail survived (e.g.
'Downloading 100%\r\x1b[KDone' rendered 'x1b[KDoneding 100%' instead of 'Done').

Root cause: escape bytes and printable bytes went through different write paths.
Fix: parse a CSI sequence as a unit; honor ESC[K (0/1/2) against the cursor; overlay
every other CSI (SGR colour, motion) through the SAME shared write path as printable
text, so colour still passes through to the filters unchanged (prior contract) and a
later \r redraw overwrites prior escape bytes cell-for-cell instead of shifting them.

A bare \r with no erase still faithfully overlays (Downloading 100%\rDone ->
Doneloading 100%), matching a real terminal. Scope boundary: models the cursor, not
per-cell colour attributes (a full VT emulator, out of scope).

Tests: ESC[K to-EOL + mid-line, SGR passthrough, coloured \r-redraw overlay, bare-\r
faithful overlay, progress bar, backspace, plain/CRLF unchanged.

Delta over upstream rtk-ai#2581 — offer back as a review comment (see brief 003).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jul 18, 2026
…an't hang capture

When the direct child exits but a detached descendant keeps the captured pipe
open (e.g. dotnet's build server, or `ng build`'s esbuild --service grandchild),
reading until pipe EOF blocks forever. Replace the unbounded reads in run_streaming
with background reader threads plus a bounded post-exit drain: once the direct child
is reaped, only drain briefly (idle-grace + hard cap), and never join a reader still
blocked on a descendant-held pipe.

Re-derived on top of the collapse-to-final CRLF handling (003 / upstream rtk-ai#2581):
readers accumulate raw bytes, collapse_terminal_control is applied to the final
buffers. Tests cover both capture and streaming paths plus fast-exit no-truncation.

This is the run_streaming half of the orphaned-pipe hang; upstream rtk-ai#2322 fixes the
exec_capture half only. See brief 004 (issue rtk-ai#2320).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jul 18, 2026
Adopts the design from upstream rtk-ai#2581: collapse_terminal_control
rewrites carriage-return progress redraws and backspace overwrites to the final
line state before captured output reaches the filters, on both run_streaming and
exec_capture. For a token-saving, model-facing tool this is better than emitting
every transient redraw frame (the earlier LineChunks approach this replaces):
the model sees 'Downloading 100%' once, not every percent.

Supersedes this fork's prior LineChunks commit; see brief 003. Real CRLF line
endings are preserved.
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jul 18, 2026
The adopted collapse (rtk-ai#2581) parses \r/\b/\n but has no CSI awareness, so an
erase-in-line sequence (ESC[K) — exactly how programs do a *shrinking* redraw
(\r + erase a longer previous frame) — was overlaid as literal characters: the
escape bytes leaked into output AND the stale tail survived (e.g.
'Downloading 100%\r\x1b[KDone' rendered 'x1b[KDoneding 100%' instead of 'Done').

Root cause: escape bytes and printable bytes went through different write paths.
Fix: parse a CSI sequence as a unit; honor ESC[K (0/1/2) against the cursor; overlay
every other CSI (SGR colour, motion) through the SAME shared write path as printable
text, so colour still passes through to the filters unchanged (prior contract) and a
later \r redraw overwrites prior escape bytes cell-for-cell instead of shifting them.

A bare \r with no erase still faithfully overlays (Downloading 100%\rDone ->
Doneloading 100%), matching a real terminal. Scope boundary: models the cursor, not
per-cell colour attributes (a full VT emulator, out of scope).

Tests: ESC[K to-EOL + mid-line, SGR passthrough, coloured \r-redraw overlay, bare-\r
faithful overlay, progress bar, backspace, plain/CRLF unchanged.

Delta over upstream rtk-ai#2581 — offer back as a review comment (see brief 003).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jul 18, 2026
…an't hang capture

When the direct child exits but a detached descendant keeps the captured pipe
open (e.g. dotnet's build server, or `ng build`'s esbuild --service grandchild),
reading until pipe EOF blocks forever. Replace the unbounded reads in run_streaming
with background reader threads plus a bounded post-exit drain: once the direct child
is reaped, only drain briefly (idle-grace + hard cap), and never join a reader still
blocked on a descendant-held pipe.

Re-derived on top of the collapse-to-final CRLF handling (003 / upstream rtk-ai#2581):
readers accumulate raw bytes, collapse_terminal_control is applied to the final
buffers. Tests cover both capture and streaming paths plus fast-exit no-truncation.

This is the run_streaming half of the orphaned-pipe hang; upstream rtk-ai#2322 fixes the
exec_capture half only. See brief 004 (issue rtk-ai#2320).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jul 18, 2026
…an't hang capture

When the direct child exits but a detached descendant keeps the captured pipe
open (e.g. dotnet's build server, or `ng build`'s esbuild --service grandchild),
reading until pipe EOF blocks forever. Replace the unbounded reads in run_streaming
with background reader threads plus a bounded post-exit drain: once the direct child
is reaped, only drain briefly (idle-grace + hard cap), and never join a reader still
blocked on a descendant-held pipe.

Re-derived on top of the collapse-to-final CRLF handling (003 / upstream rtk-ai#2581):
readers accumulate raw bytes, collapse_terminal_control is applied to the final
buffers. Tests cover both capture and streaming paths plus fast-exit no-truncation.

This is the run_streaming half of the orphaned-pipe hang; upstream rtk-ai#2322 fixes the
exec_capture half only. See brief 004 (issue rtk-ai#2320).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jul 18, 2026
Adopts the design from upstream rtk-ai#2581: collapse_terminal_control
rewrites carriage-return progress redraws and backspace overwrites to the final
line state before captured output reaches the filters, on both run_streaming and
exec_capture. For a token-saving, model-facing tool this is better than emitting
every transient redraw frame (the earlier LineChunks approach this replaces):
the model sees 'Downloading 100%' once, not every percent.

Supersedes this fork's prior LineChunks commit; see brief 003. Real CRLF line
endings are preserved.
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jul 18, 2026
The adopted collapse (rtk-ai#2581) parses \r/\b/\n but has no CSI awareness, so an
erase-in-line sequence (ESC[K) — exactly how programs do a *shrinking* redraw
(\r + erase a longer previous frame) — was overlaid as literal characters: the
escape bytes leaked into output AND the stale tail survived (e.g.
'Downloading 100%\r\x1b[KDone' rendered 'x1b[KDoneding 100%' instead of 'Done').

Root cause: escape bytes and printable bytes went through different write paths.
Fix: parse a CSI sequence as a unit; honor ESC[K (0/1/2) against the cursor; overlay
every other CSI (SGR colour, motion) through the SAME shared write path as printable
text, so colour still passes through to the filters unchanged (prior contract) and a
later \r redraw overwrites prior escape bytes cell-for-cell instead of shifting them.

A bare \r with no erase still faithfully overlays (Downloading 100%\rDone ->
Doneloading 100%), matching a real terminal. Scope boundary: models the cursor, not
per-cell colour attributes (a full VT emulator, out of scope).

Tests: ESC[K to-EOL + mid-line, SGR passthrough, coloured \r-redraw overlay, bare-\r
faithful overlay, progress bar, backspace, plain/CRLF unchanged.

Delta over upstream rtk-ai#2581 — offer back as a review comment (see brief 003).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jul 18, 2026
…an't hang capture

When the direct child exits but a detached descendant keeps the captured pipe
open (e.g. dotnet's build server, or `ng build`'s esbuild --service grandchild),
reading until pipe EOF blocks forever. Replace the unbounded reads in run_streaming
with background reader threads plus a bounded post-exit drain: once the direct child
is reaped, only drain briefly (idle-grace + hard cap), and never join a reader still
blocked on a descendant-held pipe.

Re-derived on top of the collapse-to-final CRLF handling (003 / upstream rtk-ai#2581):
readers accumulate raw bytes, collapse_terminal_control is applied to the final
buffers. Tests cover both capture and streaming paths plus fast-exit no-truncation.

This is the run_streaming half of the orphaned-pipe hang; upstream rtk-ai#2322 fixes the
exec_capture half only. See brief 004 (issue rtk-ai#2320).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jul 18, 2026
Adopts the design from upstream rtk-ai#2581: collapse_terminal_control
rewrites carriage-return progress redraws and backspace overwrites to the final
line state before captured output reaches the filters, on both run_streaming and
exec_capture. For a token-saving, model-facing tool this is better than emitting
every transient redraw frame (the earlier LineChunks approach this replaces):
the model sees 'Downloading 100%' once, not every percent.

Supersedes this fork's prior LineChunks commit; see brief 003. Real CRLF line
endings are preserved.
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jul 18, 2026
The adopted collapse (rtk-ai#2581) parses \r/\b/\n but has no CSI awareness, so an
erase-in-line sequence (ESC[K) — exactly how programs do a *shrinking* redraw
(\r + erase a longer previous frame) — was overlaid as literal characters: the
escape bytes leaked into output AND the stale tail survived (e.g.
'Downloading 100%\r\x1b[KDone' rendered 'x1b[KDoneding 100%' instead of 'Done').

Root cause: escape bytes and printable bytes went through different write paths.
Fix: parse a CSI sequence as a unit; honor ESC[K (0/1/2) against the cursor; overlay
every other CSI (SGR colour, motion) through the SAME shared write path as printable
text, so colour still passes through to the filters unchanged (prior contract) and a
later \r redraw overwrites prior escape bytes cell-for-cell instead of shifting them.

A bare \r with no erase still faithfully overlays (Downloading 100%\rDone ->
Doneloading 100%), matching a real terminal. Scope boundary: models the cursor, not
per-cell colour attributes (a full VT emulator, out of scope).

Tests: ESC[K to-EOL + mid-line, SGR passthrough, coloured \r-redraw overlay, bare-\r
faithful overlay, progress bar, backspace, plain/CRLF unchanged.

Delta over upstream rtk-ai#2581 — offer back as a review comment (see brief 003).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jul 18, 2026
…an't hang capture

When the direct child exits but a detached descendant keeps the captured pipe
open (e.g. dotnet's build server, or `ng build`'s esbuild --service grandchild),
reading until pipe EOF blocks forever. Replace the unbounded reads in run_streaming
with background reader threads plus a bounded post-exit drain: once the direct child
is reaped, only drain briefly (idle-grace + hard cap), and never join a reader still
blocked on a descendant-held pipe.

Re-derived on top of the collapse-to-final CRLF handling (003 / upstream rtk-ai#2581):
readers accumulate raw bytes, collapse_terminal_control is applied to the final
buffers. Tests cover both capture and streaming paths plus fast-exit no-truncation.

This is the run_streaming half of the orphaned-pipe hang; upstream rtk-ai#2322 fixes the
exec_capture half only. See brief 004 (issue rtk-ai#2320).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jul 22, 2026
Adopts the design from upstream rtk-ai#2581: collapse_terminal_control
rewrites carriage-return progress redraws and backspace overwrites to the final
line state before captured output reaches the filters, on both run_streaming and
exec_capture. For a token-saving, model-facing tool this is better than emitting
every transient redraw frame (the earlier LineChunks approach this replaces):
the model sees 'Downloading 100%' once, not every percent.

Supersedes this fork's prior LineChunks commit; see brief 003. Real CRLF line
endings are preserved.
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jul 22, 2026
The adopted collapse (rtk-ai#2581) parses \r/\b/\n but has no CSI awareness, so an
erase-in-line sequence (ESC[K) — exactly how programs do a *shrinking* redraw
(\r + erase a longer previous frame) — was overlaid as literal characters: the
escape bytes leaked into output AND the stale tail survived (e.g.
'Downloading 100%\r\x1b[KDone' rendered 'x1b[KDoneding 100%' instead of 'Done').

Root cause: escape bytes and printable bytes went through different write paths.
Fix: parse a CSI sequence as a unit; honor ESC[K (0/1/2) against the cursor; overlay
every other CSI (SGR colour, motion) through the SAME shared write path as printable
text, so colour still passes through to the filters unchanged (prior contract) and a
later \r redraw overwrites prior escape bytes cell-for-cell instead of shifting them.

A bare \r with no erase still faithfully overlays (Downloading 100%\rDone ->
Doneloading 100%), matching a real terminal. Scope boundary: models the cursor, not
per-cell colour attributes (a full VT emulator, out of scope).

Tests: ESC[K to-EOL + mid-line, SGR passthrough, coloured \r-redraw overlay, bare-\r
faithful overlay, progress bar, backspace, plain/CRLF unchanged.

Delta over upstream rtk-ai#2581 — offer back as a review comment (see brief 003).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jul 22, 2026
…an't hang capture

When the direct child exits but a detached descendant keeps the captured pipe
open (e.g. dotnet's build server, or `ng build`'s esbuild --service grandchild),
reading until pipe EOF blocks forever. Replace the unbounded reads in run_streaming
with background reader threads plus a bounded post-exit drain: once the direct child
is reaped, only drain briefly (idle-grace + hard cap), and never join a reader still
blocked on a descendant-held pipe.

Re-derived on top of the collapse-to-final CRLF handling (003 / upstream rtk-ai#2581):
readers accumulate raw bytes, collapse_terminal_control is applied to the final
buffers. Tests cover both capture and streaming paths plus fast-exit no-truncation.

This is the run_streaming half of the orphaned-pipe hang; upstream rtk-ai#2322 fixes the
exec_capture half only. See brief 004 (issue rtk-ai#2320).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jul 22, 2026
Adopts the design from upstream rtk-ai#2581: collapse_terminal_control
rewrites carriage-return progress redraws and backspace overwrites to the final
line state before captured output reaches the filters, on both run_streaming and
exec_capture. For a token-saving, model-facing tool this is better than emitting
every transient redraw frame (the earlier LineChunks approach this replaces):
the model sees 'Downloading 100%' once, not every percent.

Supersedes this fork's prior LineChunks commit; see brief 003. Real CRLF line
endings are preserved.
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jul 22, 2026
The adopted collapse (rtk-ai#2581) parses \r/\b/\n but has no CSI awareness, so an
erase-in-line sequence (ESC[K) — exactly how programs do a *shrinking* redraw
(\r + erase a longer previous frame) — was overlaid as literal characters: the
escape bytes leaked into output AND the stale tail survived (e.g.
'Downloading 100%\r\x1b[KDone' rendered 'x1b[KDoneding 100%' instead of 'Done').

Root cause: escape bytes and printable bytes went through different write paths.
Fix: parse a CSI sequence as a unit; honor ESC[K (0/1/2) against the cursor; overlay
every other CSI (SGR colour, motion) through the SAME shared write path as printable
text, so colour still passes through to the filters unchanged (prior contract) and a
later \r redraw overwrites prior escape bytes cell-for-cell instead of shifting them.

A bare \r with no erase still faithfully overlays (Downloading 100%\rDone ->
Doneloading 100%), matching a real terminal. Scope boundary: models the cursor, not
per-cell colour attributes (a full VT emulator, out of scope).

Tests: ESC[K to-EOL + mid-line, SGR passthrough, coloured \r-redraw overlay, bare-\r
faithful overlay, progress bar, backspace, plain/CRLF unchanged.

Delta over upstream rtk-ai#2581 — offer back as a review comment (see brief 003).
pidefrem added a commit to pidefrem/rtk that referenced this pull request Jul 22, 2026
…an't hang capture

When the direct child exits but a detached descendant keeps the captured pipe
open (e.g. dotnet's build server, or `ng build`'s esbuild --service grandchild),
reading until pipe EOF blocks forever. Replace the unbounded reads in run_streaming
with background reader threads plus a bounded post-exit drain: once the direct child
is reaped, only drain briefly (idle-grace + hard cap), and never join a reader still
blocked on a descendant-held pipe.

Re-derived on top of the collapse-to-final CRLF handling (003 / upstream rtk-ai#2581):
readers accumulate raw bytes, collapse_terminal_control is applied to the final
buffers. Tests cover both capture and streaming paths plus fast-exit no-truncation.

This is the run_streaming half of the orphaned-pipe hang; upstream rtk-ai#2322 fixes the
exec_capture half only. See brief 004 (issue rtk-ai#2320).
@HetCreep

HetCreep commented Aug 4, 2026

Copy link
Copy Markdown

The CSI-as-a-unit rewrite in 7fb1339 is relevant to a defect I ran into elsewhere in the tree during an audit of v0.44.2 (700bdde), and I think this PR quietly builds the primitive that fixes it.

Provenance: AI-assisted source audit, human-directed. Source read at 700bdde; the two regexes below are quoted verbatim, but I did not run either stripper against a live spinner.

strip_ansi cannot match a CSI private-parameter sequence

src/core/utils.rs:52-57:

static ANSI_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\x1b\[[0-9;]*[a-zA-Z]").unwrap());

The parameter class is [0-9;]*, which excludes ?. So \x1b[?25l and \x1b[?25h — the cursor hide/show pair emitted by npm, cargo and gradle spinners — do not match, and the whole escape survives into the filtered output. This function has ~67 call sites across src/.

That is the same class of input this PR is handling; the difference is that collapse_terminal_control now parses the sequence structurally, while strip_ansi is still pattern-matching a subset of CSI.

The PHP copy fails on the same input, differently

src/cmds/php/utils.rs:

pub fn strip_ansi_and_controls(input: &str) -> String {
    let no_ansi = ANSI_RE.replace_all(input, "");
    CONTROL_RE.replace_all(&no_ansi, "").to_string()
}

Same ANSI_RE, so \x1b[?25l survives it too — but CONTROL_RE's range [\x00-\x08\x0B\x0C\x0E-\x1F\x7F] covers \x1B, so the second pass removes the ESC byte and leaves a literal [?25l in the text. Worth confirming against the actual constant, but if that reading is right, the two strippers produce different wrong answers for the same bytes: one leaves the full escape, the other leaves visible garbage. Callers are artisan_cmd.rs and ecs_cmd.rs.

Suggestion

If collapse_terminal_control ends up as a general CSI-aware scanner, having strip_ansi delegate to it (or share the parameter-parsing step) would remove the divergence and cover private-parameter sequences in one place. Widening the regex to \x1b\[[0-9;?]*[a-zA-Z] would be the one-character-ish version, though it still leaves two implementations to keep in sync.

Not asking to expand this PR — it is scoped to the capture path and the CSI rewrite already addresses the harder half. Flagging because the parser you just wrote is the thing the rest of the tree is missing.

@Chichengmo

Copy link
Copy Markdown

Thanks — this still looks like the right layer for the fix. I rechecked the capture path on v1.0.1 (Homebrew, macOS arm64): CR-overwrite progress frames are still not collapsed before command filters see the output.

Reproduction with the generic capture path:

$ rtk summary sh -c "printf 'Downloading 0%%\rDownloading 50%%\rDownloading 100%%\n'" | od -An -t x1c
44 6f ... 30 25 0d 44 6f ... 35 30 25 0d ... 31 30 30 25 0a

The 0d bytes remain, so all frames reach the filter as one line.

Source path on current v1.0.1:

  • src/core/stream.rs::read_lines_lossy() only removes a trailing \r (CRLF handling); interior \r frames are preserved.
  • TOML filters then start from stdout.lines() in apply_filter_with_info(), so the whole progress sequence is still one line.
  • This also hurts jj: its filter has max_lines = 30, but that never fires because the frames are one line. truncate_lines_at = 120 can instead retain the first frames rather than the final rendered frame.

The direction in this PR — collapse before line splitting, model \r, \b, and ESC[K as cursor operations, and keep stdout/stderr separate — matches the actual failure mode. A few regression cases worth pinning:

  • Downloading 0%\rDownloading 100% keeps only the final frame.
  • Backspace spinner frames collapse according to the final rendered text.
  • Downloading 100%\rESC[KDone renders as Done.
  • CRLF remains a normal newline and is not treated as a redraw.
  • stdout and stderr are normalized independently.
  • rtk bypass remains byte-for-byte raw as the explicit escape hatch.

One process note: #2230 was closed as stale, not fixed, and this branch currently shows as conflicting/dirty against develop. Could this be rebased so the remaining regression coverage can land? I think the narrow src/core/stream.rs fix is preferable to adding agent-specific hooks or another command shim: RTK already owns this capture boundary, so fixing it once benefits every wrapped command.

yungSNAT added a commit to yungSNAT/PascalsLetter that referenced this pull request Nov 3, 2026
Adapted from ftlpickleball/rtk d98ffa4 (Apache-2.0), which reworks
rtk-ai#2581. Only filter input is collapsed: raw output, the tee and
rtk recall keep the exact bytes, and CRLF endings are kept.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(filtering): remove terminal output overwritten by control flow (\r and \b)

4 participants