Skip to content

fix(tui): patch @opentui/core to prevent mouse garbling on exit and fragmented input#20462

Open
agutmanstein-scale wants to merge 1 commit intoanomalyco:devfrom
agutmanstein-scale:fix/mouse-garble-on-exit
Open

fix(tui): patch @opentui/core to prevent mouse garbling on exit and fragmented input#20462
agutmanstein-scale wants to merge 1 commit intoanomalyco:devfrom
agutmanstein-scale:fix/mouse-garble-on-exit

Conversation

@agutmanstein-scale
Copy link
Copy Markdown

Issue for this PR

Closes #20458

Type of change

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

What does this PR do?

Patches @opentui/core@0.1.95 to fix two separate bugs that cause SGR mouse escape sequences to appear as garbled text in the terminal:

1. Post-exit mouse garbling (new fix)

When the renderer is destroyed, cleanupBeforeDestroy() calls stdin.setRawMode(false) (re-enabling terminal ECHO) before mouse tracking is disabled. Any mouse events arriving during this window get echoed as raw escape sequence bytes like 35;89;19M35;84;20M35....

The correct ordering already exists in suspend(). This patch applies the same pattern to cleanupBeforeDestroy():

  1. disableMouse() — mouse off while raw mode is still on (no ECHO)
  2. Drain buffered mouse events from stdin
  3. setRawMode(false) — raw mode off last

Upstream fix: anomalyco/opentui#905

2. In-session mouse garbling (ported from #19520)

When SGR mouse escape sequences arrive fragmented across multiple stdin chunks (due to event loop pressure from LLM streaming + TUI rendering), the StdinParser's timeout fires mid-sequence and leaks individual bytes as KEY events. Three timeout paths are patched:

  • esc_recovery timeout: changed to emitOpaqueResponse + recovery flag
  • csi timeout: added recovery flag for subsequent < routing
  • esc_less_mouse timeout: changed to deferred wait instead of flush

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

How did you verify your code works?

  • bun packages/opencode/test/cli/tui/test-stdin-parser.mjs — StdinParser fragmentation tests (9/9 pass)
  • bun packages/opencode/test/cli/tui/test-destroy-mouse-cleanup.mjs — structural tests verifying correct ordering in the patched code (5/5 pass)
  • packages/opencode/test/cli/tui/frag-pty.py — PTY wrapper for manual end-to-end testing

Checklist

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

🤖 Generated with Claude Code

…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>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 1, 2026

The following comment was made by an LLM, it may be inaccurate:

Related PR found:

This is not a duplicate but rather an evolution/enhancement of previous work.

18vijayb pushed a commit to 18vijayb/opencode that referenced this pull request Apr 2, 2026
- sigtstp-mouse-pty.py: wraps a command in a PTY, waits for mouse
  tracking to be enabled, sends SIGTSTP to the process group, then
  checks whether ?1003l was emitted before suspension. Demonstrates
  FAIL→PASS across PR anomalyco#20507.

- post-exit-mouse-pty.py: wraps a command in a PTY and snapshots the
  slave PTY's ECHO flag at the moment ?1003l arrives at the master.
  Structural verification for PR anomalyco#20462 ordering is covered by
  test-destroy-mouse-cleanup.mjs.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

bug: mouse escape sequences garbled after TUI exit

1 participant