Skip to content

fix(tui): patch StdinParser to prevent garbled text from fragmented mouse sequences - #19520

Closed
agutmanstein-scale wants to merge 1 commit into
anomalyco:devfrom
agutmanstein-scale:fix/sgr-mouse-esc-recovery
Closed

agutmanstein-scale wants to merge 1 commit into
anomalyco:devfrom
agutmanstein-scale:fix/sgr-mouse-esc-recovery

Conversation

@agutmanstein-scale

@agutmanstein-scale agutmanstein-scale commented Mar 28, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #3199

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

When the event loop is busy (LLM streaming + 60fps rendering), the StdinParser's timeout fires mid-mouse-sequence, leaking individual bytes as KEY events that appear as garbled text in the input box.

Three timeout paths could trigger this. This PR patches all three via patchedDependencies for @opentui/core@0.1.90:

  • esc_recovery timeout: was emitting [ as a KEY event. Changed to emitOpaqueResponse + set a justFlushedEscBracket recovery flag so < still routes to mouse handling.
  • csi timeout: dropped \x1b[ but lost context. Now also sets the recovery flag.
  • esc_less_mouse timeout: had no deferred mechanism — flushed mid-sequence and leaked remaining bytes. Now waits for more bytes instead of flushing, matching the existing csi_sgr_mouse_deferred pattern.

Also bumps DEFAULT_TIMEOUT_MS from 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 fixed
  • packages/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 with the fix — zero garbled text. Without the fix — consistent garbling.

Screenshots / recordings

Not a UI change — stdin parser fix verified via included test files.
https://github.com/user-attachments/assets/5ab9955f-a55d-40c9-bd6e-30db554aa827
https://github.com/user-attachments/assets/3dd588b9-30f0-43f4-90ad-e59f6abc6b2d

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

…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>
agutmanstein-scale added a commit to agutmanstein-scale/opencode that referenced this pull request Apr 1, 2026
…ragmented input

Two bugs cause SGR mouse escape sequences to appear as garbled text:

1. **Post-exit garbling** (new fix): cleanupBeforeDestroy() calls
   setRawMode(false) before mouse tracking is disabled, creating a
   window where mouse events echo as raw bytes. Fixed by adding
   disableMouse() + stdin drain before setRawMode(false), matching
   the correct ordering already used in suspend().

2. **In-session garbling** (ported from anomalyco#19520): StdinParser timeout
   fires mid-mouse-sequence during heavy event loop pressure, leaking
   individual bytes as KEY events. Fixed by patching three timeout
   paths with recovery flags and deferred processing.

Also bumps DEFAULT_TIMEOUT_MS from 20 to 25ms for defense-in-depth.

Patch targets @opentui/core@0.1.95 (current dev dependency).
Upstream fix: anomalyco/opentui#905

Closes anomalyco#20458

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
18vijayb pushed a commit to 18vijayb/opencode that referenced this pull request Apr 2, 2026
…ragmented input

Two bugs cause SGR mouse escape sequences to appear as garbled text:

1. **Post-exit garbling** (new fix): cleanupBeforeDestroy() calls
   setRawMode(false) before mouse tracking is disabled, creating a
   window where mouse events echo as raw bytes. Fixed by adding
   disableMouse() + stdin drain before setRawMode(false), matching
   the correct ordering already used in suspend().

2. **In-session garbling** (ported from anomalyco#19520): StdinParser timeout
   fires mid-mouse-sequence during heavy event loop pressure, leaking
   individual bytes as KEY events. Fixed by patching three timeout
   paths with recovery flags and deferred processing.

Also bumps DEFAULT_TIMEOUT_MS from 20 to 25ms for defense-in-depth.

Patch targets @opentui/core@0.1.95 (current dev dependency).
Upstream fix: anomalyco/opentui#905

Closes anomalyco#20458

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
agutmanstein-scale added a commit to agutmanstein-scale/opencode that referenced this pull request Apr 2, 2026
…ragmented input

Two bugs cause SGR mouse escape sequences to appear as garbled text:

1. **Post-exit garbling** (new fix): cleanupBeforeDestroy() calls
   setRawMode(false) before mouse tracking is disabled, creating a
   window where mouse events echo as raw bytes. Fixed by adding
   disableMouse() + stdin drain before setRawMode(false), matching
   the correct ordering already used in suspend().

2. **In-session garbling** (ported from anomalyco#19520): StdinParser timeout
   fires mid-mouse-sequence during heavy event loop pressure, leaking
   individual bytes as KEY events. Fixed by patching three timeout
   paths with recovery flags and deferred processing.

Also bumps DEFAULT_TIMEOUT_MS from 20 to 25ms for defense-in-depth.

Patch targets @opentui/core@0.1.95 (current dev dependency).
Upstream fix: anomalyco/opentui#905

Closes anomalyco#20458

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@rekram1-node

Copy link
Copy Markdown
Collaborator

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

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.

Mouse Generates Garbled Text Input & Blocks User Input

2 participants