fix(tty): drain pending feature-detection replies on exit#391
Merged
Conversation
Reporter saw `^[]11;rgb:...^[\^[[33;1R^[[?62;1;4c` printed by fish / bash right after exiting reasonix. Those bytes are RESPONSES — OSC 11 bg-color, CPR cursor-position, DA1 device-attributes — sent by the terminal in reply to queries the runtime / a dep emitted during session startup. The dist bundle has none of those query bytes itself, so the source is bun's tty feature-detection path or a transitive dep doing color-scheme detection. Whoever sent them, the responses sat in stdin's queue until reasonix exited. The parent shell then read them and rendered the bytes as input. Add `drainTtyResponses(50ms)` and call it in chat.tsx's exit finally, after `runtime.closeAll()`. Best-effort: sets raw mode, reads-and- discards anything still queued for 50ms, restores cooked. No-op when stdin isn't a TTY or raw mode isn't available. 0.30.3's alt-screen default is the upstream mitigation (DECSET 1049 flushes the input queue on most terminals); this is the program-side mitigation that's independent of which terminal the user is in.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #365.
Why
Reporter saw
^[]11;rgb:...^[\^[[33;1R^[[?62;1;4cprinted by fish / bash right afterexit. Those bytes are responses, not queries:\x1b]11;rgb:...\x1b\— OSC 11 reply (terminal background color)\x1b[33;1R— CPR reply (cursor position)\x1b[?62;1;4c— DA1 reply (primary device attributes)So something sent the matching queries (
\x1b]11;?\x1b\,\x1b[6n,\x1b[c) into the tty during the session. I scanned the v0.30.2 dist bundle for the raw query bytes — zero hits. The source is the runtime (bun does some tty feature detection on startup) or a transitive dep doing color-scheme detection. Either way, the responses sat in stdin's queue until reasonix exited; the parent shell then read them and rendered the bytes as if the user typed them.What changes
src/cli/ui/drain-tty.ts— newdrainTtyResponses(timeoutMs = 50)helper. Sets raw mode, reads-and-discards anything pending fortimeoutMs, restores cooked. No-op when stdin isn't a TTY or raw mode isn't available.src/cli/commands/chat.tsx—await drainTtyResponses()in the exit finally block, afterruntime.closeAll().Two-layer mitigation:
\x1b[?1049h/\x1b[?1049l, which flushes the input queue on most terminals.The drain runs even with
--no-alt-screen, so legacy in-shell scrollback users get the fix too.Tests
tests/drain-tty.test.ts— 3 cases: returns immediately on non-TTY stdin; respects the timeout cap when raw mode is available; doesn't blow up when a terminal-response burst arrives mid-window.Test plan
npm run verify— 135 files / 2155 passed / 1 skipped (was 2152, +3 new)bunx reasonix@<this PR> code→ exit → no escape junk in fish prompt--no-alt-screenmode → still gets the drain → no junk