fix(terminal): coordinate session lifecycle and recover ended panes
What & why
A parked terminal could lose its recovery path when the reaper marked its absent PTY exited before reattachment. Ended panes then offered Reconnect against the same dead ID. Layout synchronization also treated a remote pane removal as another local kill command.
This change coordinates creation and disposal through one per-terminal queue, keeps missing sessions recoverable, and separates explicit pane-close commands from synchronized removal. Ended panes offer New shell, which creates a fresh session and preserves bounded history from the previous runtime.
- Durable cancellation wins over delayed creation, including a close before the session row exists. Pending ownership prevents another workspace from cancelling that creation. Queued operations retain ownership; completion and failure release it.
- Missing-session reconciliation requires consecutive observations and skips startup, while startup still processes durable kill requests. Pending-disposal bindings stay out of hydration. Disposal preserves an existing internal account-restart candidate; explicit termination suppresses resume.
- Tab closure batches explicit close notifications and isolates each callback failure. Synchronized removal only releases view resources. Explicit session removal also clears workspace-run bookkeeping.
- Replacement captures history before asynchronous creation, so tab switching and runtime eviction cannot erase it. Repeated clicks coalesce, failed creation preserves the old pane, and cancelled replacements are disposed with a bounded retry and excluded from automatic adoption.
No database migration. Explicit pane close retains existing termination semantics. This does not add distributed view leases or general pruning of adopted panes. If both cancellation kill attempts fail, the error propagates and a surviving shell remains manually discoverable under Background terminals.
Validation
-
Final multi-client verification: six new real host/PTY scenarios passed (shared input, disconnect/reconnect, explicit cross-client kill, stale-ID isolation, workspace ownership, phone visibility/sizing, and natural exit). Existing create/sequence suites: 25/26 passed; the legacy replay timeout passed an isolated rerun. This covers mobile's protocol, not the iPhone UI.
-
Final cleanup ensures killed panes are removed even if workspace-run bookkeeping throws; 352 renderer/recovery tests, desktop typecheck, Biome and diff checks passed locally.
-
710 focused tests pass: 244 host, 114 panes, 352 renderer/recovery. All three changed packages pass typecheck; repository lint and diff checks pass. Earlier terminal-router integration run: 9/9 passed.
-
Independent worker: two pending-ownership regressions failed before the fix; 19/19 real-daemon create-on-attach tests pass afterward. It also reviewed binding reconciliation and callback isolation without finding further issues.
-
Regression assertions reproduced callback short-circuiting, late history capture, and pending-disposal hydration/binding errors before their fixes.
-
CDP before/after: restored the original renderer implementation, held creation after the host response, switched tabs with the parked-runtime cap set to 1, and verified eviction of the ended runtime. Original code lost history; fixed code retained it and executed a command in the new shell. Screenshots visually inspected, numeric evidence agrees, zero captured renderer errors. Test UI interactions used actual mouse/keyboard input; cap adjustment and delayed response were controlled fault injection.
-
Earlier CDP coverage includes eight recovery cycles (exit codes 0/42), renderer reload, split-pane isolation, ten tab switches, create failure, duplicate clicks, cancellation during auto-adoption, and a lost response after a committed kill. Evidence is retained locally because screenshots contain unrelated workspace names.
-
Latest full native terminal run: 77/80 passed. Two disposal-related failures coincided with daemon process-inspection failures under concurrent load; both passed serial reruns. The remaining empty-input BAD_REQUEST assertion also failed with the original implementation. Full native-suite success is not claimed.
-
Broad host runs previously stalled in native filesystem-watcher tests; full monorepo success is not claimed. Multi-window behavior has unit coverage but was not exercised through CDP.
Checklist
- Conventional commit title
- Repository lint and typechecks for all changed packages
- Regression tests and independent agent verification
- PR CI passed on
a14034e; final cleanup validated locally - User explicitly requested admin merge
Summary by CodeRabbit
- New Features
- Added clearer terminal status indicators, including “Session ended” and a “New shell” action.
- Ended sessions can be replaced while preserving relevant scrollback history.
- Workspace pane closures now trigger consistent cleanup and notifications.
- Bug Fixes
- Improved terminal disposal, reconnects, concurrent creation, and session recovery.
- Prevented disposed or cancelled sessions from being incorrectly restored or reused.
- Ensured pane cleanup continues when an individual callback fails.
- Localization
- Added translations for terminal status and error messages across supported languages.