Skip to content

fix(terminal): stop mouse motion typing SGR reports into the pane - #20087

Open
bjornrun wants to merge 1 commit into
stablyai:mainfrom
bjornrun:fix/mouse-tracking-control-characters
Open

bjornrun wants to merge 1 commit into
stablyai:mainfrom
bjornrun:fix/mouse-tracking-control-characters

Conversation

@bjornrun

Copy link
Copy Markdown
Contributor

ELI5

When a TUI such as Grok or Claude Code turns on mouse tracking and then dies, Orca used to restore that mouse mode onto the next shell. Moving the mouse then typed garbage like 35;42;47M into the window. This change stops restoring mouse tracking from checkpoints, and a replacement PTY also turns leftover mouse tracking off.

What Changed

  • buildRehydrateSequences no longer emits DECSET ?9h / ?1000h / ?1002h / ?1003h / ?1006h / ?1016h. Live TUIs re-assert those modes on startup. Alt-screen frameRestoreAnsi still carries mouse for a still-running TUI.
  • Older checkpoints that already baked those sequences into rehydrateSequences are stripped on restore without changing the checkpoint schema (modes.mouseTracking is still recorded).
  • A fresh PTY spawn writes RESET_MOUSE_REPORTING into the reused xterm after the existing kitty-keyboard reset, so a remounted pane cannot keep a dead TUI's any-event tracking.

Why

Mouse motion with DECSET 1003 + SGR 1006 is encoded as CSI < 35;col;row M. Readline consumes ESC[< as an unbound sequence and self-inserts the rest, so every pointer move fills the prompt with control characters. Cold restore already appended a seed reset, but live rehydrate and the renderer startFreshSpawn path could still re-arm the emulator.

This is the same class of bug as #18424 (checkpoint rehydrate) and #15625 (fresh spawn after sleep). Related user reports: #13077, #18064.

Overlaps community PR #18454 (rehydrate-only). This PR also covers the fresh-spawn renderer gap called out on #15625.

Linked Issue

Fixes #18424
Fixes #15625

Visual Proof

N/A — no UI chrome change. The behavior change is that mouse motion over a replacement shell no longer inserts SGR report tails.

Testing

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below

Targeted vitest (all passed):

  • src/main/daemon/terminal-mode-rehydrate-sequences.test.ts
  • src/main/daemon/headless-emulator.test.ts
  • src/main/daemon/terminal-history-seed-segments.test.ts
  • src/main/daemon/repro-12101-mouse-tracking-survives-agent-death.test.ts
  • src/main/daemon/repro-7329-remote-snapshot-corruption.test.ts
  • src/main/daemon/history-reader.test.ts
  • src/main/daemon/hibernation-cold-restore-repro.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection-fresh-spawn-guards.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection-reattach-mode-reset.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection-hibernation-wake.test.ts

Did not rebuild or run the desktop app; CI should cover pnpm lint / pnpm typecheck / full pnpm test.

AI Disclosure

Implemented with Grok 4.6 (xAI) in a local coding session.

Review

Agent skill upstream boundary

  • Not applicable, or this change follows docs/reference/agent-skill-sharing-upstream-boundary.md and copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • Cross-platform: terminal protocol only; no OS-specific code.
  • SSH / remote / mobile: rehydrateSequences is the same prefix the daemon snapshot uses for those consumers.
  • Live TUI scroll/hover: programs re-send mouse DECSET on startup. Distinct from fix(terminal): restore OpenCode mouse-wheel scroll after session restore #11176 (OpenCode wheel after restore).
  • Full pnpm lint / pnpm typecheck / pnpm build not run locally (CI).

Checklist

  • This PR is small and focused
  • I explained what changed and why (including ELI5)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

Restoring a pane no longer replays DECSET mouse tracking from a checkpoint
when the TUI that armed it does not come back. A fresh PTY spawn also
disarms leftover 1003/1006 on the reused xterm so motion cannot leak
`35;col;rowM` into a replacement shell.

Fixes stablyai#18424
Fixes stablyai#15625

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ No critical issues — a couple of stale comments worth a one-line update.

Reviewed changes

  • buildRehydrateSequences stops re-arming mouse — src/main/daemon/terminal-mode-rehydrate-sequences.ts no longer emits ?9h/?1000h/?1002h/?1003h/?1006h/?1016h; alt-screen, bracketed-paste, and app-cursor still rehydrate.
  • Cold restore strips legacy mouse DECSET — omitMouseTrackingFromRehydrateSequences removes the six enable sequences from older checkpoints in coldRestoreInfoFromSnapshot, leaving modes.mouseTracking intact (no schema change).
  • Fresh spawn disarms leftover mouse reporting — startFreshSpawn writes RESET_MOUSE_REPORTING into the reused xterm after the kitty-keyboard reset.

The change is correct and well-tested. Live-TUI mouse restore is preserved through frameRestoreAnsi and the SerializeAddon mode trailer in snapshotAnsi, so dropping it from rehydrateSequences does not regress alt-screen reattach scroll gestures; cold restore still clears it via COLD_RESTORE_SEED_MODE_RESET.

ℹ️ Nitpicks

  • src/shared/terminal-mode-reset-profiles.ts:30-33 — the POST_REPLAY_REATTACH_RESET_KEEP_MOUSE "Why" still claims "an alt-screen reattach replays the daemon's rehydrateSequences, which re-arm the live TUI's mouse modes". After this PR the re-arm comes from frameRestoreAnsi and the snapshotAnsi trailer, not rehydrateSequences; a one-line correction would stop future readers being misled about the mechanism.
  • tests/e2e/terminal-reattach-mouse-mode-leak.spec.ts:22-24 — the header comment says buildRehydrateSequences re-arms the mode on reattach; it is now the serializer trailer. Same one-line fix.

Pullfrog  | Fix it ➔ | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

@coderabbitai

coderabbitai Bot commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: 69a2cebd-f2d3-49f0-9252-006b97666d99

📥 Commits

Reviewing files that changed from the base of the PR and between 94c2f96 and f6808a1.

📒 Files selected for processing (11)
  • src/main/daemon/headless-emulator.test.ts
  • src/main/daemon/repro-12101-mouse-tracking-survives-agent-death.test.ts
  • src/main/daemon/repro-7329-remote-snapshot-corruption.test.ts
  • src/main/daemon/terminal-history-cold-restore-info.ts
  • src/main/daemon/terminal-history-seed-segments.test.ts
  • src/main/daemon/terminal-history-seed-segments.ts
  • src/main/daemon/terminal-mode-rehydrate-sequences.test.ts
  • src/main/daemon/terminal-mode-rehydrate-sequences.ts
  • src/renderer/src/components/terminal-pane/pty-connection-fresh-spawn-guards.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection/fresh-spawn-start.ts
  • src/shared/agent-tui-ansi-fuzz-stream.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The change removes mouse-reporting escape sequences from generated and stored terminal rehydration sequences while preserving mouse mode state in snapshots. Cold restore and seed-segment logic now apply this filtering. Headless emulator and regression tests cover standard, SGR, any-motion, pixel, and alternate-screen cases. Fresh PTY startup now sends RESET_MOUSE_REPORTING before the replacement shell starts.

Priority: ➖ Normal

Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to f6808

Mouse reporting is reset for fresh shells and excluded from restored replay data. No current merge-blocking risk remains.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 11 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: preventing stale mouse-motion SGR reports from being typed into the terminal pane.
Description check ✅ Passed The description is complete and follows the template. It explains the problem, changes, rationale, linked issues, testing, AI use, visual proof, and cross-platform considerations. The required linked …
Linked Issues check ✅ Passed The PR satisfies the coding requirements for #18424 and #15625. buildRehydrateSequences no longer emits mouse-enable sequences. coldRestoreInfoFromSnapshot removes mouse-enable sequences from lega…
Out of Scope Changes check ✅ Passed The changed source files implement the two linked fixes. The test updates verify those fixes. The documentation and comments describe the changed rehydration and reset behavior. No unrelated functiona…
  • Fix all pre-merge checks with AI

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.

@AmethystLiang

Copy link
Copy Markdown
Contributor

Thanks for the PR. Will get it reviewed this weekend

@Kooooojun

Copy link
Copy Markdown
Contributor

Hit this on 1.4.245 (macOS host, driven from a paired desktop client). After Agent sleep stopped a Claude pane and I reopened the tab, the wake command landed in a shell that still had mouse tracking on, so it ran as:

35;64;6M35;64;7Mcd '/Users/…/Pig' && claude '--resume' '<id>'
zsh: command not found: 35

and the agent did not resume. Details and logs in #36427. Would love to see this merged.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants