Repository navigation
fix(terminal): disarm stale TUI modes when a pane confirms return to shell - #9608
Conversation
A TUI killed hard (SIGKILL, crash) never restores the modes it armed, and when its parent shell survives, the emulator keeps mouse/focus/kitty modes on: every pointer move over the pane lands as typed SGR motion reports at the prompt, and the doomed foreground process burns CPU parsing the motion firehose while it lives. The hibernation-kill path already disarms these modes (POST_REPLAY_MODE_RESET), but that path only covers a dead PTY — an agent dying under a live shell was never sanitized. Fire a mode reset at the pane-foreground-agent tracker's confirmed return-to-shell transition, next to the sibling stale-title cleanup, via the replay guard so xterm auto-replies cannot leak to the shell as input. The new CONFIRMED_SHELL_STALE_MODE_RESET excludes ?2004l: the live shell re-arms bracketed paste at its prompt before the confirming read lands, so disabling it would strip paste protection the shell itself requested. Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01UCkWN5nDXNyD1V7cfamCxa
📝 WalkthroughWalkthroughShell foreground confirmation now triggers 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/renderer/src/components/terminal-pane/pty-connection.test.ts (1)
5640-5640: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winPrevent silent test failures by enforcing the data callback invocation.
Using the optional chaining operator (
?.) in a test—especially one relying on a negative assertion—is risky. IfdataCallbackRef.currentisnulldue to a setup issue, the mock data is never injected and assertions likeexpect(...).toBe(false)will silently pass.Use the non-null assertion operator (
!) to ensure the test fails immediately if the callback isn't registered as expected.
src/renderer/src/components/terminal-pane/pty-connection.test.ts#L5640-L5640: replace?.with!to guarantee invocation.src/renderer/src/components/terminal-pane/pty-connection.test.ts#L5684-L5684: replace?.with!to guarantee invocation.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 0a6b9376-1b2b-4c5d-8b34-ebb14f25075f
📒 Files selected for processing (2)
src/renderer/src/components/terminal-pane/pty-connection.test.tssrc/renderer/src/components/terminal-pane/pty-connection.ts
brennanb2025
left a comment
There was a problem hiding this comment.
Thanks — this is a good catch and a well-scoped fix.
Verified rather than taken on faith:
- The test is load-bearing. I applied only the test additions to unpatched
main:disarms stale TUI modes in the emulator after a confirmed return to shellfails. With the source change it passes. The133;D-without-confirmation negative control is the right guard, since a full-screen agent's nested shells leak their own133;D. - Green standalone on current
main(94009f63ff, this branch is ~480 commits behind):typecheckclean,terminal-panesuite 190 files / 2479 tests passing. - Direction. I looked at doing this daemon-side with
TerminalPrivateModeTrackerand resetting only what's actually armed — it doesn't work: that tracker follows mouse/SGR modes but not the Kitty-keyboard or focus-reporting bits you also need to clear, and the confirmed-shell signal lives in the renderer anyway. ReusingPOST_REPLAY_REATTACH_RESETatonConfirmedShellForegroundis the right call. - Writing this bundle to a live session is already established — it's what the daemon-reattach path at
pty-connection.ts:5120does today. So this extends an accepted trade-off to a new trigger rather than introducing a new class of risk. Worth noting the cost is now paid more often: per fire, a shell-set cursor shape reverts to the terminal default (self-heals at the next prompt) and the replay guard drops input for ~2.8ms.
One nit for the squash message: the commit message describes a new CONFIRMED_SHELL_STALE_MODE_RESET constant that excludes ?2004l, but the final code reuses POST_REPLAY_REATTACH_RESET (which already excludes it). The PR body is accurate; only the commit message drifted. I'll reword on merge.
…shell (stablyai#9608) A TUI killed hard (SIGKILL, OOM, crash) never restores the modes it armed. When its parent shell survives, the emulator keeps mouse tracking, focus reporting and Kitty keyboard flags on: every pointer move over the pane lands as typed SGR motion reports at the prompt, and the doomed process burns CPU parsing the motion firehose while it lives. Orca's existing mode cleanups all hang off dead-PTY paths (hibernation kill, daemon reattach), so an agent dying under a live shell crossed none of them. Fire POST_REPLAY_REATTACH_RESET at the pane-foreground-agent tracker's confirmed return-to-shell transition, next to the sibling stale-title cleanup. That transition is gated on a real foreground-process read rather than the bare OSC 133;D, because a full-screen agent's nested command shells leak their own D onto the main PTY. The write goes through the replay guard so xterm's auto replies cannot leak to the shell as input. The reused constant already excludes ?2004l, so the bracketed-paste protection the live shell re-arms at its prompt is preserved.
Problem
When a TUI agent is killed hard (SIGKILL via a process manager, OOM, crash) while its parent shell survives, it never restores the terminal modes it armed. The emulator keeps any-motion mouse tracking (1003/1006), focus reporting (1004), and Kitty keyboard flags on, with two user-visible consequences:
35;250;36M35;216;39M…), filling the input line with garbage that is one Enter away from executing;Observed in production with Claude Code (arms mouse reporting for scroll support) killed via btop; any mouse-mode TUI reproduces it.
Why existing resets don't cover it
The codebase already sanitizes stale modes on the dead-PTY paths — hibernation kills (
POST_REPLAY_MODE_RESET) and daemon reattach (POST_REPLAY_REATTACH_RESET) — and #9259 filters leaked focus reports after Codex history resume. But an agent dying under a live shell never crosses any of those paths: the PTY never exits, no replay happens, and the modes stay armed indefinitely.Fix
Fire
POST_REPLAY_REATTACH_RESET(reused — its value is exactly the safe-for-a-live-shell bundle: cursor style/visibility, Kitty flags, all mouse protocols/encodings, focus reporting) at the pane-foreground-agent tracker's confirmed return-to-shell transition (onConfirmedShellForeground), next to the existing stale-title cleanup — the same residue class, one level down. The write goes throughreplayIntoTerminalso the replay guard keeps xterm's auto-replies from leaking to the shell as input.Bracketed paste (
?2004l) is deliberately not reset here: the surviving shell re-arms it at its prompt before the confirming read lands, so disabling it would strip paste protection the shell itself requested. (The dead-PTY paths keep the full bundle — there is no live shell to protect.)The confirmed-shell transition only fires after a foreground process read proves the agent is gone (a leaked nested-shell
133;Dunder a live full-screen agent republishes the agent and does not disarm — covered by a test).Tests
disarms stale TUI modes in the emulator after a confirmed return to shell— fails on unpatched main, passes with the fix; also asserts no disarm on the bare133;Dbefore confirmation, and that the reset excludes?2004l.keeps armed modes while the agent still owns the foreground after a leaked 133;D— negative control.terminal-panesuite green with the change (2,229 tests at v1.4.146, 493 in the touched files on main).Verification beyond CI
Dogfooded on a patched v1.4.146 build: the original repro (btop-SIGKILL a claude pane, then mouse over the window) produced a clean prompt on every pane; before the patch it reliably spewed motion reports.
This fix was authored, tested, and verified by Claude (Fable 5)
ELI5
If a TUI agent is killed hard, mouse-tracking and special keyboard modes could stick on and dump garbage at the shell prompt. When the pane is back at a shell, Orca disarms those leftover modes.