Skip to content

fix(terminal): do not restore mouse tracking without a TUI - #18454

Closed
innocarpe wants to merge 1 commit into
stablyai:mainfrom
innocarpe:fix/restore-mouse-tracking-without-tui
Closed

innocarpe wants to merge 1 commit into
stablyai:mainfrom
innocarpe:fix/restore-mouse-tracking-without-tui

Conversation

@innocarpe

Copy link
Copy Markdown
Contributor

Description

Restoring a pane no longer replays DECSET mouse tracking from a checkpoint when the TUI that armed it does not come back, so mouse motion cannot type SGR reports (35;col;rowM) into a bare bash prompt.

buildRehydrateSequences omits mouseTracking / sgrMouseMode (and SGR-pixels). Live TUIs re-assert those modes on startup. Older checkpoints that already baked ?1003h/?1006h into the stored string are stripped on restore without changing the checkpoint schema.

Focused fix

  • In scope: stop restoring mouse reporting through rehydrateSequences (new snapshots and stored checkpoint strings).
  • Out of scope (explicit): checkpoint format / modes.mouseTracking persistence; OpenCode wheel-after-restore (fix(terminal): restore OpenCode mouse-wheel scroll after session restore #11176); live alt-screen frameRestoreAnsi mouse for a still-running TUI; gating restore on process identity.

Preserves

  • Alternate screen, bracketed paste, and application-cursor rehydrate.
  • Checkpoint JSON shape (modes.mouseTracking / sgrMouseMode still recorded).
  • Cold-restore seed still disarms SerializeAddon mouse trailers (COLD_RESTORE_SEED_MODE_RESET).
  • SSH / headless restore path: this is the same rehydrateSequences prefix the daemon snapshot uses.

Evidence

  • Test: pnpm exec vitest run --config config/vitest.config.ts 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-7329-remote-snapshot-corruption.test.ts src/main/daemon/repro-12101-mouse-tracking-survives-agent-death.test.ts src/main/daemon/history-reader.test.ts src/main/daemon/reattach-snapshot.test.ts
  • Fail without the fix: a checkpoint with mouseTracking: any and sgrMouseMode: true must not emit ?1003h / ?1006h in rehydrateSequences; alt-screen / bracketed paste still do.
  • pnpm tc:node pass.

User-regression-tradeoffs

Fixes #18424

Replay of checkpointed DECSET 1003/1006 armed the renderer even when the
owning TUI was gone, so mouse motion typed SGR reports into readline.
Omit those modes from rehydrateSequences; live TUIs re-assert on startup.

Fixes stablyai#18424
@coderabbitai

coderabbitai Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Team

Run ID: 1118111a-85af-48d1-9a71-9819deec5e50

📥 Commits

Reviewing files that changed from the base of the PR and between 67e2234 and 5030799.

📒 Files selected for processing (9)
  • 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/shared/agent-tui-ansi-fuzz-stream.ts

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


📝 Walkthrough

Walkthrough

Mouse tracking is no longer included in terminal rehydration sequences. A new helper removes mouse-enable sequences from stored snapshots during cold restore. Terminal mode metadata still records mouse tracking. Tests now verify that alternate-screen and bracketed-paste modes replay while mouse protocols and encodings do not. Seed expectations and related regression tests reflect the new behavior.

Merge Risk: ⚪ Minimal · up to 50307

Pane restoration no longer re-enables mouse tracking that can send mouse reports into a bare shell, while preserving alternate-screen, bracketed-paste, and application-cursor restoration. The change is covered across restore paths with no remaining merge-blocking risk.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the bug, solution, scope, preserved behavior, linked issue, testing evidence, and trade-offs. However, it does not follow the repository template and omits required sections s… Reformat the description using the repository template. Add the missing required sections, including Visual Proof with an explicit N/A and reason if applicable, Testing details, checklist status, review notes, and AI Disclosure information …
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 9 files. 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 and concisely describes the main change: preventing terminal mouse tracking from being restored without its owning TUI.
Linked Issues check ✅ Passed The changes directly address issue [#18424] by removing mouse-tracking sequences from new rehydration output and stripping them from older stored sequences. The implementation preserves other terminal…
Out of Scope Changes check ✅ Passed The changed implementation, restore-path integration, comments, and regression tests are related to preventing mouse-tracking rehydration. The changes do not introduce unrelated functionality or addre…
Full details: Description check

Explanation

The description explains the bug, solution, scope, preserved behavior, linked issue, testing evidence, and trade-offs. However, it does not follow the repository template and omits required sections such as Visual Proof or an explicit N/A, AI Disclosure, Review, Notes, and Checklist.

Resolution

Reformat the description using the repository template. Add the missing required sections, including Visual Proof with an explicit N/A and reason if applicable, Testing details, checklist status, review notes, and AI Disclosure information where applicable.

Full details: Linked Issues check

Explanation

The changes directly address issue [#18424] by removing mouse-tracking sequences from new rehydration output and stripping them from older stored sequences. The implementation preserves other terminal modes and keeps mouse state in checkpoint metadata, matching the issue objective.

Full details: Out of Scope Changes check

Explanation

The changed implementation, restore-path integration, comments, and regression tests are related to preventing mouse-tracking rehydration. The changes do not introduce unrelated functionality or address excluded issues.

  • 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.

@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 new issues found.

Reviewed changes

  • buildRehydrateSequences stops re-arming mouse — drops the ?9h/?1000h/?1002h/?1003h reporting switch and the ?1006h/?1016h SGR-encoding branch, keeping alt-screen, bracketed paste, and application-cursor rehydration intact.
  • omitMouseTrackingFromRehydrateSequences strips baked mouse DECSET — the same six sequences are replaceAll-removed from a stored rehydrateSequences string, wired into coldRestoreInfoFromSnapshot so pre-fix checkpoints are cleaned on restore without a schema change.
  • Tests pin the new behavior — a new terminal-mode-rehydrate-sequences.test.ts covers the generator and strip paths end-to-end against HeadlessEmulator, and the headless-emulator, repro-7329, repro-12101, and seed-segment suites are updated from toContain to not.toContain/toBe('').

The fix is sound: rehydrateSequences was a redundant mouse channel — SerializeAddon's own trailer inside snapshotAnsi (and frameRestoreAnsi for live alt-screen owners) already re-arms mouse for live reattach, while cold restore disarms the trailer via COLD_RESTORE_SEED_MODE_RESET. Removing the rehydrate channel only eliminates the dead-TUI leak it was causing. All 15 new/affected unit tests pass locally.

ℹ️ Stale comments still point at the removed rehydrate-mouse mechanism

The code change is correct, but two "Why" comments outside the diff still attribute mouse re-arming on reattach to rehydrateSequences/buildRehydrateSequences, which is no longer true — mouse now comes from SerializeAddon's trailer and frameRestoreAnsi. Worth a follow-up so the next reader isn't misled.

Technical details
# Update stale docs that reference rehydrateSequences re-arming mouse

## Affected sites
- src/shared/terminal-mode-reset-profiles.ts:30-33 — `POST_REPLAY_REATTACH_RESET_KEEP_MOUSE` comment reads "an alt-screen reattach replays the daemon's rehydrateSequences, which re-arm the live TUI's mouse modes".
- tests/e2e/terminal-reattach-mouse-mode-leak.spec.ts — header comment (lines 5-10) and "What it does NOT cover" (lines 22-25) plus inline notes (~100-111, ~150-152) state "buildRehydrateSequences re-arms the mode on reattach".

## Required outcome
- Re-state the mechanism accurately: mouse re-arming on live reattach now comes from SerializeAddon's snapshotAnsi trailer (and frameRestoreAnsi for alt-screen), not rehydrateSequences. The reset-profile and E2E behavior are unchanged.

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

@innocarpe

Copy link
Copy Markdown
Contributor Author

Closing this rehydrate-only PR as superseded by merged #22735, which grounds the full cold-restore payload (mouse and focus modes) and is present in v1.4.215. The new #18424 report on v1.4.212 predates that release fix; I asked the reporter to retest on v1.4.215. The overlapping #20087 proposal also covers fresh-PTY reset, but this PR no longer needs to remain open.

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]: Pane restore re-enables mouse tracking (?1003h/?1006h) when the TUI that owned it does not come back

1 participant