Skip to content

fix(terminal): disarm stale TUI modes when a pane confirms return to shell - #9608

Merged
brennanb2025 merged 1 commit into
stablyai:mainfrom
akapug:fix/disarm-stale-tui-modes-on-confirmed-shell
Jul 27, 2026
Merged

brennanb2025 merged 1 commit into
stablyai:mainfrom
akapug:fix/disarm-stale-tui-modes-on-confirmed-shell

Conversation

@akapug

@akapug akapug commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • every pointer movement over the pane lands as typed SGR motion reports at the shell prompt (35;250;36M35;216;39M…), filling the input line with garbage that is one Enter away from executing;
  • while the doomed TUI is still alive, the same any-motion firehose keeps waking it to parse events — we traced a "fully idle" agent pinned at ~4.5% CPU to exactly this.

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 through replayIntoTerminal so 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;D under 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 bare 133;D before confirmation, and that the reset excludes ?2004l.
  • keeps armed modes while the agent still owns the foreground after a leaked 133;D — negative control.
  • Full terminal-pane suite 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.

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
@coderabbitai

coderabbitai Bot commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Shell foreground confirmation now triggers POST_REPLAY_REATTACH_RESET through replayIntoTerminal, using the existing synchronous viewport refresh decision. Tests cover delayed reset behavior for stale agents, preservation of bracketed paste mode, and suppression of the reset while a live agent retains foreground ownership.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the change and tests, but it omits required template sections like Screenshots, AI Review Report, Security Audit, and Notes. Add the missing sections, including a no-visual-change screenshot note and brief AI review, security audit, and notes entries aligned to the template.
✅ Passed checks (4 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
Title check ✅ Passed The title clearly summarizes the main change: disarming stale TUI modes when a pane confirms return to shell.

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.

@coderabbitai coderabbitai 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.

🧹 Nitpick comments (1)
src/renderer/src/components/terminal-pane/pty-connection.test.ts (1)

5640-5640: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Prevent 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. If dataCallbackRef.current is null due to a setup issue, the mock data is never injected and assertions like expect(...).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

📥 Commits

Reviewing files that changed from the base of the PR and between f8b430f and 2a11a7c.

📒 Files selected for processing (2)
  • src/renderer/src/components/terminal-pane/pty-connection.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection.ts

@AmethystLiang
AmethystLiang requested a review from OrcaWin July 22, 2026 17:04
@nwparker nwparker added the bug Something isn't working label Jul 27, 2026

@brennanb2025 brennanb2025 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.

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 shell fails. With the source change it passes. The 133;D-without-confirmation negative control is the right guard, since a full-screen agent's nested shells leak their own 133;D.
  • Green standalone on current main (94009f63ff, this branch is ~480 commits behind): typecheck clean, terminal-pane suite 190 files / 2479 tests passing.
  • Direction. I looked at doing this daemon-side with TerminalPrivateModeTracker and 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. Reusing POST_REPLAY_REATTACH_RESET at onConfirmedShellForeground is the right call.
  • Writing this bundle to a live session is already established — it's what the daemon-reattach path at pty-connection.ts:5120 does 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.

@brennanb2025 brennanb2025 reopened this Jul 27, 2026
@brennanb2025
brennanb2025 merged commit c0734f0 into stablyai:main Jul 27, 2026
6 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants