Skip to content

fix(terminal): clear leaked mouse-reporting modes on pane reattach - #7893

Merged
AmethystLiang merged 4 commits into
mainfrom
fix/terminal-mouse-mode-leak-on-reattach
Jul 9, 2026
Merged

AmethystLiang merged 4 commits into
mainfrom
fix/terminal-mouse-mode-leak-on-reattach

Conversation

@AmethystLiang

@AmethystLiang AmethystLiang commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A terminal pane can start spewing random text like 35;43M 35;41M … 5M on mouse movement — visible garbage echoed into a plain shell. These are SGR mouse-motion reports (ESC [ < 35;col;row M) being echoed as literal text.

Root cause is Orca-side and reproducible:

  1. A TUI (e.g. a Claude Code session) enables any-motion mouse tracking (?1003h) + SGR encoding (?1006h), then dies uncleanly — the disable sequence is never emitted.
  2. The terminal daemon tracks mouse modes per session (terminal-private-mode-tracker.ts) and re-arms them on every reattach via buildRehydrateSequences (headless-emulator.ts) — originally added so mobile scroll works in live panes.
  3. On reattach the renderer replays that snapshot (re-enabling ?1003h) and then applies POST_REPLAY_REATTACH_RESET, which cleared cursor style, cursor visibility, focus reporting, and Kitty keyboard flags — but not mouse modes.
  4. The pane's foreground is now a plain shell, which echoes every pointer-motion report as literal text.

The repro-7329 test even documented this as a known leak (// leak).

Fix

Add RESET_MOUSE_REPORTING (?9l ?1000l ?1002l ?1003l ?1006l ?1016l — every protocol + SGR encoding the daemon can rehydrate) to both POST_REPLAY_REATTACH_RESET and POST_REPLAY_MODE_RESET.

Live agent panes still preserve mouse modes via POST_REPLAY_LIVE_AGENT_REATTACH_RESET (unchanged), so scroll-in-agent keeps working — mouse is only disarmed when the pane doesn't look agent-owned, mirroring the existing ?1004 focus-reporting tradeoff.

Verification

Reproduced end-to-end with the real components (no mocks):

  • Real daemon serializer + real xterm: the old reset leaves term.modes.mouseTrackingMode armed; the new reset returns it to 'none'.
  • Real bash pty: feeding the exact bytes xterm emits on pointer motion makes the shell echo literal 35;10;5M 35;11;5M 35;12;5M — matching the reported symptom.

The repro-7329 tests are flipped from documenting the leak to asserting the disarm, plus a new regression test for the ?1003h+?1016h killed-TUI shape. All terminal-pane + daemon suites pass (2,332 tests); full typecheck clean.

Made with Orca 🐋

A TUI that enables mouse tracking (?1000/1002/1003 + SGR 1006/1016) and
dies uncleanly never emits the disable sequence, so the daemon's snapshot
records the mode and buildRehydrateSequences re-arms it on every reattach.
POST_REPLAY_REATTACH_RESET cleared cursor/focus/kitty state but not mouse
modes, so a plain shell in the reattached pane echoed every pointer-motion
report (`<35;col;rowM`) as literal text.

Add RESET_MOUSE_REPORTING (?9l ?1000l ?1002l ?1003l ?1006l ?1016l) to both
POST_REPLAY_REATTACH_RESET and POST_REPLAY_MODE_RESET. Live agent panes keep
mouse modes via POST_REPLAY_LIVE_AGENT_REATTACH_RESET, so agent scroll is
unaffected.

Verified against the real daemon serializer + real xterm: the old reset
leaves mouseTrackingMode armed, the new reset returns it to 'none'.
@coderabbitai

coderabbitai Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 3c2cb105-513b-46eb-8e72-76bb11d1fec1

📥 Commits

Reviewing files that changed from the base of the PR and between 057d78b and 4102868.

📒 Files selected for processing (1)
  • tests/e2e/terminal-reattach-mouse-mode-leak.spec.ts

📝 Walkthrough

Walkthrough

This change fixes leaked terminal mouse-reporting modes after replay and reattach. A new RESET_MOUSE_REPORTING constant is added to layout-serialization.ts, disabling multiple mouse tracking/encoding escape sequences (?9, ?1000, ?1002, ?1003, ?1006, ?1016), and is composed into both POST_REPLAY_MODE_RESET and POST_REPLAY_REATTACH_RESET. Daemon tests are updated to assert mouse tracking is disarmed after replay while bracketed paste persists, including a new case covering additional mouse modes. A new Playwright E2E spec verifies that mouse tracking armed in one session does not leak into a warm-reattached session.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers problem, fix, and verification but misses required template sections like Summary, Screenshots, Testing, AI Review Report, Security Audit, and Notes. Add the missing template sections, especially a Summary, screenshots or 'No visual change', testing checklist, AI Review Report, Security Audit, and Notes.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main fix: clearing leaked mouse-reporting modes on pane reattach.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

- Add an E2E test to verify that a warm reattach disarms mouse modes
  left armed by an uncleanly exited TUI.
- Add a test fixture that writes the mouse tracking enable sequence
  without a matching disable sequence.
- Ensure the reattached pane disarms mouse tracking and that actual
  mouse movement produces no reports.
Explicitly annotate parameter and return types for the isMouseReport
helper function inside the page.evaluate block.
Eliminate the external Node.js fixture file and streamline the E2E test
by using a POSIX printf shell builtin to arm the mouse tracking modes.

Additionally, verify the leak precondition by inspecting the active pane's
live terminal state (mouseTrackingMode) rather than querying internal
daemon buffer snapshots via window.api.pty.getMainBufferSnapshot.
@AmethystLiang
AmethystLiang merged commit c170866 into main Jul 9, 2026
4 of 6 checks passed
AmethystLiang added a commit that referenced this pull request Jul 9, 2026
#7895)

The warm-reattach mouse-leak e2e (added in #7893) awaited the readiness
marker unconditionally, so it hard-failed on sandboxed runners whose PTY
echoes input but never execs a shell. Wrap the gate in a skip guard that
mirrors the existing pane-manager guard: run the assertions where the
shell executes, and skip gracefully otherwise instead of a false red.

Co-authored-by: Orca <[email protected]>
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…tablyai#7893)

* fix(terminal): clear leaked mouse-reporting modes on pane reattach

A TUI that enables mouse tracking (?1000/1002/1003 + SGR 1006/1016) and
dies uncleanly never emits the disable sequence, so the daemon's snapshot
records the mode and buildRehydrateSequences re-arms it on every reattach.
POST_REPLAY_REATTACH_RESET cleared cursor/focus/kitty state but not mouse
modes, so a plain shell in the reattached pane echoed every pointer-motion
report (`<35;col;rowM`) as literal text.

Add RESET_MOUSE_REPORTING (?9l ?1000l ?1002l ?1003l ?1006l ?1016l) to both
POST_REPLAY_REATTACH_RESET and POST_REPLAY_MODE_RESET. Live agent panes keep
mouse modes via POST_REPLAY_LIVE_AGENT_REATTACH_RESET, so agent scroll is
unaffected.

Verified against the real daemon serializer + real xterm: the old reset
leaves mouseTrackingMode armed, the new reset returns it to 'none'.

* test: add regression tests for terminal mouse mode leak on reattach

- Add an E2E test to verify that a warm reattach disarms mouse modes
  left armed by an uncleanly exited TUI.
- Add a test fixture that writes the mouse tracking enable sequence
  without a matching disable sequence.
- Ensure the reattached pane disarms mouse tracking and that actual
  mouse movement produces no reports.

* Add types to isMouseReport in terminal reattach leak test

Explicitly annotate parameter and return types for the isMouseReport
helper function inside the page.evaluate block.

* Refactor mouse-mode leak E2E test to use shell printf and live pane

Eliminate the external Node.js fixture file and streamline the E2E test
by using a POSIX printf shell builtin to arm the mouse tracking modes.

Additionally, verify the leak precondition by inspecting the active pane's
live terminal state (mouseTrackingMode) rather than querying internal
daemon buffer snapshots via window.api.pty.getMainBufferSnapshot.
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
stablyai#7895)

The warm-reattach mouse-leak e2e (added in stablyai#7893) awaited the readiness
marker unconditionally, so it hard-failed on sandboxed runners whose PTY
echoes input but never execs a shell. Wrap the gate in a skip guard that
mirrors the existing pane-manager guard: run the assertions where the
shell executes, and skip gracefully otherwise instead of a false red.

Co-authored-by: Orca <[email protected]>
nwparker added a commit that referenced this pull request Oct 29, 2026
After a serve restart where the daemon survives, a paired client could
drop mouse and focus reporting for a still-live agent. The host publishes
the pane with its tab-default title until it seeds the daemon's live title
about 180 ms later. A reattach image replayed inside that window was judged
against the default title, read as a plain normal-buffer program, and got
the reset that disarms its modes, though the image itself carried them.

The snapshot now carries the host's tracked title when the image was taken,
as an additive `lastTitle` field in the snapshot info, gated on the image's
seq. Every reset that judges a replayed image as a live agent's (the
post-replay reset, the focus-in that follows it, the hidden-restore reset,
and the model-snapshot reattach) uses that title when present, so the
decision describes the same moment as the replayed bytes. Local Electron
model snapshots already carried this title and now use it the same way. The reattach policy itself is
unchanged: non-agent normal-buffer panes still shed modes a dead child may
have left (#7329, #7893).

Older hosts omit the field and clients fall back to the published title.
Older clients ignore it.

Refs #35559
nwparker added a commit that referenced this pull request Oct 29, 2026
…le (#35920)

After a serve restart where the daemon survives, a paired client could
drop mouse and focus reporting for a still-live agent. The host publishes
the pane with its tab-default title until it seeds the daemon's live title
about 180 ms later. A reattach image replayed inside that window was judged
against the default title, read as a plain normal-buffer program, and got
the reset that disarms its modes, though the image itself carried them.

The snapshot now carries the host's tracked title when the image was taken,
as an additive `lastTitle` field in the snapshot info, gated on the image's
seq. Every reset that judges a replayed image as a live agent's (the
post-replay reset, the focus-in that follows it, the hidden-restore reset,
and the model-snapshot reattach) uses that title when present, so the
decision describes the same moment as the replayed bytes. Local Electron
model snapshots already carried this title and now use it the same way. The reattach policy itself is
unchanged: non-agent normal-buffer panes still shed modes a dead child may
have left (#7329, #7893).

Older hosts omit the field and clients fall back to the published title.
Older clients ignore it.

Refs #35559
nwparker added a commit that referenced this pull request Oct 29, 2026
After a serve restart where the daemon survives, a paired client could
drop mouse and focus reporting for a still-live agent. The host publishes
the pane with its tab-default title until it seeds the daemon's live title
about 180 ms later. A reattach image replayed inside that window was judged
against the default title, read as a plain normal-buffer program, and got
the reset that disarms its modes, though the image itself carried them.

The snapshot now carries the host's tracked title when the image was taken,
as an additive `lastTitle` field in the snapshot info, gated on the image's
seq. Every reset that judges a replayed image as a live agent's (the
post-replay reset, the focus-in that follows it, the hidden-restore reset,
and the model-snapshot reattach) uses that title when present, so the
decision describes the same moment as the replayed bytes. Local Electron
model snapshots already carried this title and now use it the same way. The reattach policy itself is
unchanged: non-agent normal-buffer panes still shed modes a dead child may
have left (#7329, #7893).

Older hosts omit the field and clients fall back to the published title.
Older clients ignore it.

Refs #35559
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.

1 participant