Skip to content

fix(tui): prevent mouse garbling on exit, SIGTSTP suspend, and fragmented input#20704

Closed
agutmanstein-scale wants to merge 3 commits intoanomalyco:devfrom
agutmanstein-scale:fix/mouse-garbling-combined
Closed

fix(tui): prevent mouse garbling on exit, SIGTSTP suspend, and fragmented input#20704
agutmanstein-scale wants to merge 3 commits intoanomalyco:devfrom
agutmanstein-scale:fix/mouse-garbling-combined

Conversation

@agutmanstein-scale
Copy link
Copy Markdown

Issues

Closes #20458, closes #20506

Type of change

  • Bug fix

What does this PR do?

Combines three mouse/terminal garbling fixes into one PR:

1. Post-exit mouse garbling — patches @opentui/core@0.1.95 to call disableMouse() before setRawMode(false) in cleanupBeforeDestroy(), preventing mouse events from echoing as garbled text during renderer destruction. Also ports the StdinParser fragmentation fix (recovery flags for timeout paths that leaked mouse bytes as KEY events) and bumps DEFAULT_TIMEOUT_MS 20→25ms.

2. SIGTSTP suspend garbling — registers SIGTSTP/SIGCONT handlers so external suspension (code-server terminal management, job control, kill -TSTP) properly disables mouse tracking before the process suspends.

3. SIGCONT double-input guard — gates resume() on a suspendedBySigtstp flag so spurious SIGCONT signals (terminal refocus without prior SIGTSTP) don't add a duplicate stdin listener, which would cause every keystroke to appear twice.

Upstream PRs

How did you verify your code works?

  • bun packages/opencode/test/cli/tui/test-stdin-parser.mjs — StdinParser fragmentation (9/9 pass)
  • bun packages/opencode/test/cli/tui/test-destroy-mouse-cleanup.mjs — destroy ordering (5/5 pass)
  • packages/opencode/test/cli/tui/test-sigtstp-mouse.sh — SIGTSTP reproduction script
  • packages/opencode/test/cli/tui/frag-pty.py — PTY wrapper for manual fragmentation testing

Checklist

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

🤖 Generated with Claude Code

agutmanstein-scale and others added 3 commits April 2, 2026 11:36
…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>
When opencode receives SIGTSTP from an external source (code-server
terminal management, job control, kill -TSTP), the process is
suspended without terminal cleanup. Mouse tracking remains enabled,
causing mouse events to echo as garbled escape sequences in the shell.

Register SIGTSTP/SIGCONT handlers after renderer creation that call
renderer.suspend()/resume() — the same cleanup the manual suspend
keybind uses. The SIGTSTP handler removes itself before re-raising
to allow default suspend behavior.

Includes test-sigtstp-mouse.sh reproduction script.

Upstream fix: anomalyco/opentui#907
Closes anomalyco#20506

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
When SIGCONT arrives without a prior SIGTSTP through our handler
(e.g., code-server terminal refocus/reconnect), calling resume()
adds a duplicate stdin listener — every keystroke is delivered twice,
causing double character input like "ooppeennccooddee".

Guard resume() with a suspendedBySigtstp flag so it only runs if
our SIGTSTP handler actually called suspend().

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions github-actions bot added the needs:compliance This means the issue will auto-close after 2 hours. label Apr 2, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 2, 2026

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

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.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 2, 2026

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.

@github-actions github-actions bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Apr 2, 2026
@github-actions github-actions bot closed this Apr 2, 2026
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 garbling after inactivity — missing SIGTSTP handler bug: mouse escape sequences garbled after TUI exit

1 participant