Repository navigation
Conversation
📝 WalkthroughWalkthroughBackground session startup now reserves tab and pane identifiers while spawning a local or remote PTY. After the PTY is created, a new helper creates an inactive tab, registers the effective launch configuration, updates PTY state, and applies the layout. Cleanup now checks whether tab adoption occurred before clearing or closing tab state. Tests derive tab identifiers from spawn and terminal creation calls instead of assuming a fixed identifier. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 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 |
1921726 to
7cf445e
Compare
|
Rebased onto latest main — the conflict was with #8628 (terminal session retirement), which landed in the same function. A note on how the two fit together, since this diff rewrites two of #8628's tests rather than keeping them as-is:
Also added Locally green: 26 tests, |
…into background run tabs Automation dispatch created the hidden run tab (and persisted its layout) before the agent PTY spawn resolved. When the target worktree was already mounted — the common case for existing-workspace automations — Terminal.tsx mounted a TerminalPane for the new empty tab immediately, and its fresh-spawn path bound a default shell to the run tab. The agent PTY then lost the binding race and kept running detached, so 'View run' opened a blank shell prompt while the agent was only reachable from the sessions list. Mirror the CLI terminal path instead: spawn the PTY first against a pre-allocated tab id, then create the inactive tab with initialPtyId and bind the layout in one synchronous block, leaving no window for the fresh-spawn race. Rebased onto stablyai#8628's session retirement work. Its retireUnownedTerminal guard does not apply once the tab is created after the spawn — the tab cannot be closed mid-spawn because it does not exist yet, so the check would retire every launch. The invariant it protects still holds here: the worktree can disappear across the spawn await, so the launch now retires the provider terminal and skips tab adoption in that case, and its two regression tests were rebuilt around that trigger. Fixes stablyai#2989 Co-Authored-By: Claude Fable 5 <[email protected]>
7cf445e to
4bed4bf
Compare
Summary
Fixes #2989.
Automations that run in an existing worktree (
worktree_policy: existing) opened a blank terminal: the run-linked tab showed a default shell while the agent kept working in an orphaned PTY.Root cause:
launchAgentBackgroundSessioncreated the tab (and a layout snapshot with no PTY binding) before awaiting the PTY spawn. If the target worktree was already mounted,Terminal.tsxrendered the new tab'sTerminalPaneimmediately; with no PTY binding and no eager buffer, the pane took the fresh-spawn path and spawned a default shell into the tab. When the agent PTY finished spawning,persistLayoutSnapshotkept the live shell transport's binding, leaving the agent PTY orphaned.new_per_runworktrees were unaffected because they aren't mounted at spawn time.Fix: mirror the existing CLI/IPC launch pattern (
useIpcEvents.ts→createTab({ id, initialPtyId })): pre-allocate the tab/leaf ids, spawn the PTY first against the reservedtabId, then create the tab in one synchronous block (adoptAgentBackgroundSessionTab) already bound to the live PTY — tab creation withinitialPtyId, launch-config registration,updateTabPtyId, and layout snapshot with the PTY attached. There is no longer any await between tab creation and PTY binding, so the fresh-spawn race window is gone. Cleanup paths now guard on whether tab adoption happened.Screenshots
No visual change — no new UI. Behavior fix only: the run-linked tab now shows the live agent session that previously landed in an orphaned PTY behind a blank shell.
Testing
pnpm lint— passes in full on the rebased branch (oxlint, switch-exhaustiveness, reliability gates, max-lines ratchet, localization catalog + coverage).pnpm typecheck— passes (node, cli, web).pnpm test— the full suite needs the nativenode-ptybuild, which requires the Node 24 toolchain my environment doesn't have. Instead I ran the completesrc/renderer/src/libvitest suite (2452 passed, 1 skipped; includes the 20 tests of this module): all pass.pnpm build— not run locally for the same Node 24 / native-toolchain reason.createTab(invocation order assert), no tab is created while the spawn is pending or after a failed spawn, the tab is created with{ id, initialPtyId }, and cleanup clears PTY bindings only when adoption happened. Both launch paths (local spawn and runtime environment) are covered.orcaCLI (automation on an existing worktree → run tab = blank zsh, agent in an orphaned PTY confirmed withorca terminal read), and confirmed the fixed flow binds the run tab to the agent PTY.AI Review Report
Ran a multi-model review of the diff with four independent AI reviewers (GPT-5.5 via Codex CLI, Claude, Gemini, GLM). Main risks checked:
registerAgentLaunchConfig— not reachable: the spawn promise continuation runs as a microtask before any subsequent IPC event is handled, and launch-config lookup is lazy.requestBackgroundTerminalWorktreeMounton the runtime-environment path — safe: the tab is created already bound (initialPtyId+ layout), so the fresh-spawn path can't trigger; ordering matches the pre-change code.createTabmints a fresh id and warns; store-side routing keys off the actual tab id, only env-baked pane-key attribution degrades for that one terminal.Result: no actionable findings; no changes required.
Cross-platform: explicitly checked — the diff is renderer/store-level TypeScript only. No shortcuts, labels, filesystem paths, shell behavior, or Electron platform branches are touched. The PTY spawn RPCs are unchanged except for passing the pre-allocated
tabIdand the same env vars (ORCA_PANE_KEY,ORCA_TAB_ID,ORCA_WORKTREE_ID,ORCA_AGENT_LAUNCH_TOKEN) the CLI launch path already bakes, so macOS/Linux/Windows behavior is identical by construction.Security Audit
terminal.create/ PTY spawn RPCs, no new channels.No follow-ups needed.
Notes
new_per_runautomations were never affected (worktree not mounted during spawn); this PR intentionally leaves that path's behavior identical.launch-agent-background-session-tab.ts) because the max-lines ratchet forbids growinglaunch-agent-background-session.tspast 300 code lines.🤖 Generated with Claude Code
ELI5
Automations that reused an existing worktree could open a blank shell tab while the real agent ran in an orphaned PTY. The tab now waits for the background PTY before mounting so the visible pane is the one actually running the automation.