fix(tui): patch StdinParser to prevent garbled text from fragmented mouse sequences#19468
Closed
agutmanstein-scale wants to merge 1 commit intoanomalyco:devfrom
Closed
fix(tui): patch StdinParser to prevent garbled text from fragmented mouse sequences#19468agutmanstein-scale wants to merge 1 commit intoanomalyco:devfrom
agutmanstein-scale wants to merge 1 commit intoanomalyco:devfrom
Conversation
Contributor
|
This PR doesn't fully meet our contributing guidelines and PR template. What needs to be fixed:
Please edit this PR description to address the above within 2 hours, or it will be automatically closed. If you believe this was flagged incorrectly, please let a maintainer know. |
…ouse sequences When SGR mouse escape sequences arrive fragmented across multiple stdin chunks (due to event loop pressure from LLM streaming + TUI rendering), opentui's StdinParser timeout fires mid-sequence and leaks individual bytes as KEY events, inserting garbled characters into the input box. Three timeout paths in the parser could trigger this: 1. esc_recovery timeout: emitted '[' as a KEY event via emitKeyOrResponse. Fixed to use emitOpaqueResponse (dropped) and set justFlushedEscBracket flag so subsequent '<' routes to mouse handling. 2. CSI timeout: dropped '\x1b[' correctly but lost recovery context. Fixed to set justFlushedEscBracket so '<' arriving in ground state routes to esc_less_mouse. 3. esc_less_mouse timeout: had no deferred mechanism — flushed accumulated bytes and returned to ground, causing remaining bytes to leak as KEY events. Fixed to clear forceFlush and wait for more bytes, matching the csi_sgr_mouse_deferred pattern. Also increases DEFAULT_TIMEOUT_MS from 10 to 25ms (matches kitty/alacritty defaults) for defense-in-depth. Includes test-stdin-parser.mjs (parser unit test) and frag-pty.py (PTY reproducer with 12ms byte-by-byte delay). Verified end-to-end: frag-pty.py no longer produces garbled text. Fixes anomalyco#3199 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2a6506c to
07550a5
Compare
Contributor
|
This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window. Feel free to open a new pull request that follows our guidelines. |
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.
Issue for this PR
Closes #3199
Type of change
What does this PR do?
When the event loop is busy (LLM streaming + 60fps rendering), the StdinParser's 25ms timeout can fire mid-mouse-sequence, causing individual bytes to leak as KEY events and appear as garbled text in the input box.
The parser has three timeout paths that could trigger this. This PR patches all three via
patchedDependenciesfor@opentui/core@0.1.90:esc_recoverytimeout: was emitting[as a KEY event. Changed toemitOpaqueResponse(dropped) + setjustFlushedEscBracketflag so<still routes to mouse handling.csitimeout: was dropping\x1b[but losing context. Now also sets the recovery flag.esc_less_mousetimeout: had no deferred mechanism — flushed mid-sequence and leaked remaining bytes. Now waits for more bytes instead of flushing, matching the existingcsi_sgr_mouse_deferredpattern.Also bumps
DEFAULT_TIMEOUT_MSfrom 10 to 25ms (matches kitty/alacritty defaults).How did you verify your code works?
packages/opencode/test/cli/tui/test-stdin-parser.mjs— unit test confirming the 3-chunk fragmentation case is fixedpackages/opencode/test/cli/tui/frag-pty.py— PTY wrapper that fragments every mouse event byte-by-byte with 12ms delays. Ran in a VS Code web terminal (code-server), scrolled and clicked — zero garbled text with the fix, consistent garbling without itbun typecheckpasses (13/13 packages)Screenshots / recordings
Not a UI change — this is a stdin parser fix. Verified via the included test files.
Checklist