Repository navigation
fix(terminal): make Codex restart replace the pane's process instead of reattaching it - #22737
Conversation
There was a problem hiding this comment.
ℹ️ No critical issues — a failure-ordering note, one coverage gap, and a small watcher-lifetime nit.
Reviewed changes
pty:spawngainsreplacesPtyId—runPtyIpcSpawnawaitsdeps.stopReplacedPty(id)before resolving the pane owner, so the owner is dead and the spawn fresh-launches instead of reattaching.pty:killbody extracted tostopRendererOwnedPtyand reused as the spawn-side stop, so both restart paths no longer kill the old PTY themselves.- Renderer handoff — both restart paths send
replacesPtyId(mounted path viareleasePaneTransportForRestart, detached via the direct spawn), withremote:ids excluded to keep their kill-then-connect path. - Reattach guard —
spawnIpcPtydrops the field whenadmittedSessionIdis set. - Tests — new main-side replace test, new transport-handoff test, detached test updated to assert main owns the stop.
I traced the emitted pty:exit for the replaced id through both restart paths: the old transport's handlers are unregistered (detach({ preserveExitObserver: false })) or the parked sidecar is disposed before the spawn, and the buffered pre-handler exit is discarded on rebind, so it cannot close the tab, collapse the leaf, or re-raise the card. I also confirmed the reattach guard is effective and that replacesPtyId dies with the one-shot startup object (connectPanePty clears deps.startup).
ℹ️ The mounted restart path has no counterpart to the detached path's failure recovery
The detached path catches a rejected spawn and calls reopenCurrentCodexRestartPrompt, so a failed restart is still visible and retryable. handleRestartCodexPane has no such recovery: it clears the restart notice and the tab binding before spawning, and if main now stops the old process successfully but the replacement spawn then fails, the pane is left bound to a dead PTY with no card and no retry affordance other than a manual remount. This ordering is new — previously the old process was killed fire-and-forget, so the failure surface was similar, but the PR is the natural place to give the mounted path the same reopen-on-failure treatment.
Technical details
# Mounted restart lacks failure recovery
## Affected sites
- `src/renderer/src/components/terminal-pane/use-terminal-pane-process-exit-actions.ts:82` — replaces `transport?.destroy?.()` with `releasePaneTransportForRestart`, then `connectPanePty` with the new startup; no try/catch around the connect.
- `src/renderer/src/components/terminal-pane/use-terminal-pane-process-exit-actions.ts:74-81` — `suppressPtyExit` / `clearCodexRestartNotice(existingPtyId)` / `clearTabPtyId` run before the spawn resolves.
- `src/renderer/src/components/terminal-pane/codex-detached-pane-restart.ts:82-90` — the detached path's catch that reopens the prompt (the behaviour missing on the mounted path).
## Required outcome
- A stop that succeeds followed by a spawn failure must not leave the user with an unrecoverable pane. Either the mounted path reopens the Codex restart card (like the detached path), or the change documents why the mounted pane's own error/toast path is sufficient.
## Open questions for the human
- Is the mounted path's connect-error toast considered sufficient recovery, or is the card expected to return?ℹ️ The new main-side test only exercises the attach-failure fallback
pty-pane-restart-replace.test.ts stubs runtime.onPtyExit as a no-op and never retires the persisted pane binding, so the fresh-launch assertion passes through the attachOnly SessionNotFound fallback rather than the normal "owner is dead, launch fresh" path. That fallback is real and worth pinning, but the test does not prove the comment's claim (spawn-run.ts:35-36) that the stop alone makes the owner lookup see a dead owner. Worth either asserting the normal branch or noting the fallback in the test.
Technical details
# Fresh launch converges on two mechanisms; only one is tested
## Affected sites
- `src/main/ipc/pty-pane-restart-replace.test.ts:139-157` — `installRestartHarness` fakes `runtime.onPtyExit` and `store.setWorkspaceSession` without retiring the binding.
- `src/main/ipc/pty/ipc/spawn-begin.ts:83-88` — the existing-reservation short-circuit; owner resolution reads the persisted binding.
- `src/main/ipc/pty/provider/liveness.ts:167-184` — `finishPtyShutdown` clears provider/ownership state but not the persisted pane binding.
## Required outcome
- Coverage that distinguishes "binding retired by `runtime.onPtyExit`" from "attach failed and fell back", so a future change to either mechanism is caught.
## Open questions for the human
- Is there a supported execution host for which neither `runtime.onPtyExit` retires the surface nor `provider.attach` fails for a stopped PTY? That would leave the spawn reattaching a dead owner.ℹ️ Nitpicks
pane-restart-transport-handoff.ts:25—replacesPtyId && restartStartup ? ... : restartStartup:replacesPtyIdis only non-null whenrestartStartupis truthy, so the second condition is redundant.spawn-run.ts:35-36— the comment says the spawn "finds a dead owner"; the stop itself does not retire the persisted binding, so "launches fresh" is achieved via the exit-driven retirement or the attach fallback. Worth softening if that distinction matters.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
Included review availability: Your plan provides up to 10 included reviews per hour; 7 remain after this review. 📝 WalkthroughWalkthroughRestart flows now pass the replaced PTY ID through renderer connection setup. The main process reserves the pane and awaits the old PTY’s shutdown before continuing spawn setup. Detached Codex restarts can adopt the replacement when the tab and leaf bindings meet the ownership checks. Exit delivery marks replacement exits so renderer handlers can suppress normal pane-close handling. Tests cover shutdown ordering, transport handoff, replacement adoption, shutdown failure, and restart-exit handling. Priority: ➖ Normal Severity of issue fixed: Medium Merge Risk: 🔵 Low · up to Restarting during an SSH provider outage can leave the old remote process running while a replacement launches. This is a bounded risk to address or explicitly accept before merging. Security Architecture ReviewSecurity architecture risk: 🟡 Moderate · up to Restart now stops the old terminal process before starting its replacement. That fixes the reattachment race, but a delayed restart may act on a process identifier that has since been reused. No new security privilege was established. Retained concerns
Security review detailsSecurity Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
Hardening Proposals
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
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.
Actionable comments posted: 2
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: b8925ad8-d771-4ca4-b016-128782886927
📥 Commits
Reviewing files that changed from the base of the PR and between 5e6fcca and a05799a8c912b0b8d7e1124095557df3822fec99.
📒 Files selected for processing (16)
src/main/ipc/pty-pane-restart-replace.test.tssrc/main/ipc/pty/ipc/renderer-kill.tssrc/main/ipc/pty/ipc/spawn-run.tssrc/main/ipc/pty/ipc/spawn-types.tssrc/main/ipc/pty/register-handlers.tssrc/preload/api/pty-api.tssrc/preload/api/pty-bridge-session-control.tssrc/renderer/src/components/terminal-pane/codex-detached-pane-restart.test.tssrc/renderer/src/components/terminal-pane/codex-detached-pane-restart.tssrc/renderer/src/components/terminal-pane/ipc-pty-spawn-request.tssrc/renderer/src/components/terminal-pane/pane-restart-transport-handoff.test.tssrc/renderer/src/components/terminal-pane/pane-restart-transport-handoff.tssrc/renderer/src/components/terminal-pane/pty-connection-types.tssrc/renderer/src/components/terminal-pane/pty-connection/pty-input-recovery.tssrc/renderer/src/components/terminal-pane/pty-transport-types.tssrc/renderer/src/components/terminal-pane/use-terminal-pane-process-exit-actions.ts
Included review availability: Your plan provides up to 10 included reviews per hour; 3 remain after this review.
…of reattaching it A spawn for a pane that still has a live process is treated as a reattach. Both Codex restart paths raced that: the open-pane restart killed the old PTY without waiting and then spawned, and the unmounted-tab restart spawned before killing. Either way the "fresh" spawn could re-adopt the old Codex (dialog comes back) or hit the half-killed session and leave a plain shell. pty:spawn now accepts replacesPtyId. Main stops that PTY and waits for the exit before the spawn resolves the pane owner, so the existing dead-owner path launches fresh and records the current Codex home. Both restart paths send it and no longer kill the old PTY themselves. Fixes #18174
…placement Two follow-ups to the restart handoff: - The replaced PTY id rode the transport options, which outlive the first spawn. A later fresh spawn from the same pane (for example a hibernation wake) re-sent it, so main stopped that id again; SSH relay ids restart at pty-1, so it could name another pane's PTY. It is now a connection input consumed by the first fresh spawn only. - The hidden-tab restart still treated a changed binding after the spawn as a reason to stand down and reap the replacement. Main has already stopped the old PTY by then, so its exit can clear the tab binding (a background-launch exit observer does) or a pane can mount, and standing down left the pane with no process. It now keeps the replacement unless the tab or leaf was actually taken over.
a05799a to
6be65e7
Compare
There was a problem hiding this comment.
Important
A pane that mounts while the detached restart spawn is in flight can be left stranded on the stopped PTY: the new adoption branch dropped the guard that previously requeued it, and releaseReplacedCodexPanePty deletes the handler it registered.
Reviewed changes
replacesPtyIdis now a one-shot connection field — moved offPtyPaneStartupontosession.pendingReplacedPtyId, consumed and cleared by the first fresh spawn so a later hibernation wake can't re-stop a PTY that now belongs to another pane.releasePaneTransportForRestartsimplified to return the replaced id (local ids detach before destroy; remote ids keep the kill), and the mounted restart passes it as areplacesPtyIdconnect dependency.- Detached restart now adopts the replacement instead of reaping it and requeueing when the replaced PTY's exit clears the tab binding or a pane mounts during the spawn.
- Tests — hibernation-wake one-shot, transport handoff (local/remote/reattach), and the detached adoption case.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: b5244194-2745-4a52-9c36-f90dd68f6111
📥 Commits
Reviewing files that changed from the base of the PR and between a05799a8c912b0b8d7e1124095557df3822fec99 and 6be65e7.
📒 Files selected for processing (11)
src/renderer/src/components/terminal-pane/codex-detached-pane-restart.test.tssrc/renderer/src/components/terminal-pane/codex-detached-pane-restart.tssrc/renderer/src/components/terminal-pane/ipc-pty-spawn-request.tssrc/renderer/src/components/terminal-pane/pane-restart-transport-handoff.test.tssrc/renderer/src/components/terminal-pane/pane-restart-transport-handoff.tssrc/renderer/src/components/terminal-pane/pty-connection-hibernation-wake.test.tssrc/renderer/src/components/terminal-pane/pty-connection-types.tssrc/renderer/src/components/terminal-pane/pty-connection/connect-pane-pty.tssrc/renderer/src/components/terminal-pane/pty-connection/fresh-spawn-start.tssrc/renderer/src/components/terminal-pane/pty-transport-types.tssrc/renderer/src/components/terminal-pane/use-terminal-pane-process-exit-actions.ts
Included review availability: Your plan provides up to 10 included reviews per hour; 5 remain after this review.
| if (replacesPtyId) { | ||
| transport?.detach?.({ preserveExitObserver: false }) | ||
| } | ||
| transport?.destroy?.() |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
#!/bin/bash
# Inspect the pre-spawn cancellation and detached-PTY teardown paths.
rg -n -C 8 'shouldContinue|stopReplacedPty|pendingReplacedPtyId|onAbandonedConnect' \
src/renderer/src/components/terminal-pane/pty-connection \
src/renderer/src/components/terminal-pane/pty-transport.ts \
src/main/ipc/pty
rg -n -C 8 'detach\(|destroy\(|kill\(' \
src/renderer/src/components/terminal-pane/pty-transport.ts \
src/renderer/src/components/terminal-pane/pty-connectionRepository: stablyai/orca
Length of output: 29468
🏁 Script executed:
#!/bin/bash
printf '%s\n' '--- pty-transport.ts ---'
sed -n '80,220p' src/renderer/src/components/terminal-pane/pty-transport.ts
printf '%s\n' '--- fresh-spawn-start.ts ---'
sed -n '1,155p' src/renderer/src/components/terminal-pane/pty-connection/fresh-spawn-start.ts
printf '%s\n' '--- abandoned-connect and transport teardown references ---'
rg -n -C 10 'onAbandonedConnect|transport\.destroy|\.destroy\(\)|releasePaneTransportForRestart|pendingReplacedPtyId' src/renderer/src/components/terminal-pane
printf '%s\n' '--- detach implementation and interface ---'
rg -n -C 12 'detach\s*\(|destroy\s*\(' src/renderer/src/components/terminal-pane/pty-transport.ts src/renderer/src/components/terminal-pane -g '*.ts' -g '*.tsx' | head -n 260Repository: stablyai/orca
Length of output: 45657
🏁 Script executed:
#!/bin/bash
sed -n '80,220p' src/renderer/src/components/terminal-pane/pty-transport.ts
sed -n '1,155p' src/renderer/src/components/terminal-pane/pty-connection/fresh-spawn-start.ts
rg -n -C 10 'onAbandonedConnect|transport\.destroy|releasePaneTransportForRestart|pendingReplacedPtyId' src/renderer/src/components/terminal-paneRepository: stablyai/orca
Length of output: 45661
🏁 Script executed:
#!/bin/bash
printf '%s\n' '--- connectIpcPty declaration and uses ---'
rg -n -C 12 'connectIpcPty' src/renderer/src/components/terminal-pane -g '*.ts' -g '*.tsx'
printf '%s\n' '--- restart handoff helper and callers ---'
rg -n -C 15 'releasePaneTransportForRestart' src/renderer/src/components/terminal-pane -g '*.ts' -g '*.tsx'
printf '%s\n' '--- exact transport interfaces/options ---'
rg -n -C 12 'onAbandonedConnect|handleExplicitlyClosedConnect|shouldContinue' src/renderer/src/components/terminal-pane/pty-connection src/renderer/src/components/terminal-pane -g '*.ts' | head -n 260Repository: stablyai/orca
Length of output: 42353
🏁 Script executed:
#!/bin/bash
rg -n -C 12 'connectIpcPty|releasePaneTransportForRestart|onAbandonedConnect|handleExplicitlyClosedConnect|shouldContinue' src/renderer/src/components/terminal-pane -g '*.ts' -g '*.tsx' | head -n 400Repository: stablyai/orca
Length of output: 43350
Kill the replaced PTY when pre-spawn cancellation occurs.
shouldContinue can return false before spawnIpcPty runs. In that path, main never receives replacesPtyId. The handoff has already detached the old transport and cleared its PTY ID, so destroy() cannot kill the old PTY. Kill replacesPtyId when this pre-spawn check cancels the replacement.
A restart spawn stopped the pane's old process before reserving the pane, so a hidden tab revealed during the stop could reattach the dying process (and the restart could then join that reattach and return the old id). The replacing spawn now reserves the pane before the stop, so any spawn for the pane in that window joins the replacement; a failed stop still settles the reservation. The hidden-tab restart also tombstones the old PTY's buffered exit before spawning, so a reveal mid-restart reconnects by pane identity instead of replaying that exit into the pane.
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
- Restart holds the pane across the stop —
runPtyIpcSpawntakes areserveIdlePaneSpawnreservation beforestopReplacedPty, so a spawn for the same pane arriving mid-stop joins it and receives the replacement (isReattach: true) instead of reattaching the dying owner. beginPtyIpcSpawnhonors a held reservation — it skips the existing-reservation join and keepsctx.paneSpawnReservation, so the replacing spawn cannot deadlock on itself; error and commit paths still settle the reservation.- Detached restart discards the replaced PTY's pre-handler state before spawning, so a tab revealed mid-restart reconnects by pane identity (a
pty:spawnthat joins the reservation) rather than replaying the old exit or attaching to the stopped owner. isLocatedCodexPaneLeafAdoptablereplaces the removed requeue guard, adopting when the leaf binding still names either the replaced or the replacement PTY.- Tests — a main-side mid-stop join test, a detached adoption test, and a reveal-without-exit-replay test.
I re-traced the mid-restart reveal against the reservation: the detached sweep refuses any PTY that has a primary data handler (codex-detached-pane-restart.ts:54), and an eager buffer always implies one, so a revealed Codex pane takes deferredReattachSessionId → connect({ sessionId }) → pty:spawn — never transport.attach, which would bypass main. Because the detached path calls discardPreHandlerPtyState(ptyId) first, that spawn omits sessionId and is answered with the replacement, matching the new test.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
…owed until sent Main now marks the PTY it stops for a replacing spawn and stamps `replacedByRestart` on that PTY's exit (provider-observed, synthetic, and SSH-unregistered stops all reach the renderer through the same finalize step). A failed stop clears the mark with nothing sent; an undelivered mark expires. The renderer classifies the labeled exit before any consumer runs: parked-tab watchers end their subscription instead of collapsing the leaf or closing the tab, the pre-attach buffer discards instead of queuing a death for a later mount, and a mounted pane treats it as an intentional restart. With that, the hidden-tab restart no longer tears down its parked watchers and buffered output before spawning; it releases them only after the swap, so a stop that fails leaves the still-running Codex fully observed. The visible restart's pane session now holds the replaced PTY until a spawn request actually carries it (the IPC transport claims it as it sends). If the pane is closed or parked first, disposal stops that PTY with an ordinary kill instead of orphaning it.
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: 1d9f6216-8190-4e1f-a24f-325ef204c234
📒 Files selected for processing (21)
src/main/ipc/pty-pane-restart-replace.test.tssrc/main/ipc/pty/delivery/exit.tssrc/main/ipc/pty/ipc/renderer-kill.tssrc/main/ipc/pty/register-handlers.tssrc/main/ipc/pty/session.tssrc/preload/api/pty-api.tssrc/preload/api/pty-bridge-stream-and-serialization.tssrc/renderer/src/components/terminal-pane/codex-detached-pane-restart.test.tssrc/renderer/src/components/terminal-pane/codex-detached-pane-restart.tssrc/renderer/src/components/terminal-pane/ipc-pty-spawn-request.tssrc/renderer/src/components/terminal-pane/pane-restart-transport-handoff.test.tssrc/renderer/src/components/terminal-pane/pty-connection-hibernation-wake.test.tssrc/renderer/src/components/terminal-pane/pty-connection/connect-pane-pty.tssrc/renderer/src/components/terminal-pane/pty-connection/fresh-spawn-start.tssrc/renderer/src/components/terminal-pane/pty-connection/pty-exit-hibernate.tssrc/renderer/src/components/terminal-pane/pty-connection/session-reconcile-dispose.tssrc/renderer/src/components/terminal-pane/pty-dispatcher.tssrc/renderer/src/components/terminal-pane/pty-exit-delivery.tssrc/renderer/src/components/terminal-pane/pty-transport-types.tssrc/renderer/src/components/terminal-pane/terminal-parked-pty-watcher.tssrc/renderer/src/components/terminal-pane/terminal-parked-watcher-sleep-preserved-exit.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- src/renderer/src/components/terminal-pane/codex-detached-pane-restart.ts
Included review availability: Your plan provides up to 10 included reviews per hour; 1 remains after this review.
| ): Promise<void> { | ||
| const settle = markReplaced(id) | ||
| try { | ||
| await stopRendererOwnedPty(deps, { id }) |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
sed -n '1,140p' src/main/ipc/pty/ipc/renderer-kill.ts
sed -n '1,40p' src/renderer/src/components/terminal-pane/pane-restart-transport-handoff.ts
sed -n '1,80p' src/main/ipc/pty/ipc/spawn-run.ts
rg -n 'isRemoteRuntimePtyId' src/renderer src/shared | head -20Repository: stablyai/orca
Length of output: 13023
🏁 Script executed:
set -o pipefail
printf '%s\n' '--- remote runtime identity ---'
sed -n '1,130p' src/renderer/src/runtime/runtime-terminal-inspection.ts
printf '%s\n' '--- SSH PTY ID definition and uses ---'
sed -n '1,220p' src/main/providers/ssh-pty-id.ts
rg -n -C 4 'releasePaneTransportForRestart|replacesPtyId|stopReplacedPty|detached|detach|Codex|codex' src/renderer/src src/main/ipc/pty src/main/providers src/main/runtime | head -260
printf '%s\n' '--- spawn dependency binding and replacement handling ---'
rg -n -C 8 'stopReplacedPty|runPtyIpcSpawn|replacesPtyId' src/main src/renderer/src | head -260
printf '%s\n' '--- provider registry and SSH provider lifecycle ---'
sed -n '1,220p' src/main/ipc/pty/ipc/provider/registry.ts
rg -n -C 8 'sshProviders|unregister|register.*ssh|provider.*unavailable|SSH_PROVIDER_UNREGISTERED' src/main src/shared | head -260Repository: stablyai/orca
Length of output: 41802
🏁 Script executed:
set -o pipefail
printf '%s\n' '--- remote runtime identity ---'
sed -n '1,130p' src/renderer/src/runtime/runtime-terminal-inspection.ts
printf '%s\n' '--- SSH PTY ID definition and uses ---'
sed -n '1,220p' src/main/providers/ssh-pty-id.ts
printf '%s\n' '--- restart and replacement call sites ---'
rg -n -C 5 'releasePaneTransportForRestart|replacesPtyId|stopReplacedPty|detached|detach|Codex|codex' src/renderer/src src/main/ipc/pty src/main/providers src/main/runtime | head -300
printf '%s\n' '--- provider lifecycle and replacement spawn ---'
rg -n -C 7 'sshProviders|unregister|register.*ssh|provider.*unavailable|SSH_PROVIDER_UNREGISTERED' src/main src/shared | head -300Repository: stablyai/orca
Length of output: 41236
🏁 Script executed:
pwd; sed -n '1,130p' src/renderer/src/runtime/runtime-terminal-inspection.ts; sed -n '1,220p' src/main/providers/ssh-pty-id.ts; rg -n -C 5 'releasePaneTransportForRestart|replacesPtyId|stopReplacedPty|detached|detach|Codex|codex' src/renderer/src src/main | head -300Repository: stablyai/orca
Length of output: 36540
🏁 Script executed:
sed -n '1,130p' src/renderer/src/runtime/runtime-terminal-inspection.ts
sed -n '1,220p' src/main/providers/ssh-pty-id.ts
rg -n -C 5 'releasePaneTransportForRestart|replacesPtyId|stopReplacedPty|detached|detach|Codex|codex' src/renderer/src src/main | head -300Repository: stablyai/orca
Length of output: 34153
🏁 Script executed:
set -o pipefail
printf '%s\n' '--- shared SSH PTY IDs ---'
sed -n '1,240p' src/shared/ssh-pty-id.ts
printf '%s\n' '--- exact restart symbols and callers ---'
rg -n -C 10 'releasePaneTransportForRestart|stopReplacedPty|replacesPtyId|pendingCodexPaneRestart|CodexPaneRestart|restart.*pane|pane.*restart' src/renderer/src src/main --glob '*.ts' --glob '*.tsx' | head -420
printf '%s\n' '--- provider selection in spawn path ---'
rg -n -C 12 'connectionId|sshProviders|getProviderFor|provider.*spawn|spawn.*provider' src/main/ipc/pty/ipc/spawn-*.ts src/main/ipc/pty/provider src/main/providers --glob '*.ts' | head -420
printf '%s\n' '--- focused replacement tests ---'
rg -l 'replacesPtyId|releasePaneTransportForRestart|stopReplacedPty|daemon unreachable|pty-new|replacedByRestart' src/main src/renderer/src --glob '*test.ts' --glob '*test.tsx' | head -80Repository: stablyai/orca
Length of output: 42032
🏁 Script executed:
set -o pipefail
printf '%s\n' '--- replacement-spawn test ---'
sed -n '1,360p' src/main/ipc/pty-pane-restart-replace.test.ts
printf '%s\n' '--- detached Codex restart test ---'
sed -n '1,360p' src/renderer/src/components/terminal-pane/codex-detached-pane-restart.test.ts
printf '%s\n' '--- spawn state provider selection ---'
rg -n -C 12 'ctx\.provider|provider\s*=|getProviderFor|sshProviders|connectionId' src/main/ipc/pty/ipc/spawn-state.ts src/main/ipc/pty/ipc/spawn-begin.ts src/main/ipc/pty/ipc/spawn-preflight.ts src/main/ipc/pty/ipc/spawn-options.ts src/main/ipc/pty/ipc/spawn-execute.ts src/main/ipc/pty/ipc/spawn-commit.ts src/main/ipc/pty/ipc/spawn-run.ts
printf '%s\n' '--- stop replacement wiring ---'
rg -n -C 15 'stopReplacedPanePty|stopReplacedPty|releasePaneTransportForRestart|pane-restart-transport-handoff' src/main src/renderer/src --glob '*.ts' --glob '*.tsx'Repository: stablyai/orca
Length of output: 43094
🏁 Script executed:
set -o pipefail
printf '%s\n' '--- detached Codex restart implementation ---'
sed -n '1,340p' src/renderer/src/components/terminal-pane/codex-detached-pane-restart.ts
sed -n '1,280p' src/renderer/src/components/terminal-pane/codex-detached-pane-restart-scheduler.ts
printf '%s\n' '--- spawn preflight and provider binding ---'
sed -n '1,300p' src/main/ipc/pty/ipc/spawn-preflight.ts
rg -n -C 15 'function getProvider|export .*getProvider|const getProvider|sshProviders|getProviderForPty|connectionId' src/main/ipc/pty src/main/providers --glob '*.ts' | head -360Repository: stablyai/orca
Length of output: 42296
🏁 Script executed:
set -o pipefail
printf '%s\n' '--- foreign-machine Codex predicate ---'
rg -n -C 20 'isForeignMachineCodexPtyId' src/renderer/src/lib src/renderer/src/components/terminal-pane --glob '*.ts' --glob '*.tsx'
printf '%s\n' '--- actual provider registry ---'
sed -n '1,300p' src/main/ipc/pty/provider/registry.ts
printf '%s\n' '--- registry call sites for absent providers ---'
rg -n -C 10 'getProvider\(' src/main/ipc/pty src/main --glob '*.ts' | head -260Repository: stablyai/orca
Length of output: 41958
Do not mark an SSH replacement as stopped when its provider is unavailable.
stopRendererOwnedPty returns after recording that the remote shell is still running and sending a synthetic exit. stopReplacedPanePty then marks the replacement as stopped. An ssh:...@@... ID is not excluded by releasePaneTransportForRestart, so a mounted SSH restart can reach replacesPtyId. The detached Codex restart path skips SSH IDs.
The replacement does not create a duplicate. getProvider(args.connectionId) throws before provider spawn when the SSH provider is absent. The defect is the false successful replacement and synthetic replacement exit before that failure. Add a replacement-specific mode that rejects before synthetic-exit emission when shutdown was not delivered. Keep synthetic exits for ordinary closes.
There was a problem hiding this comment.
ℹ️ No critical issues — one design note on the failed-stop path.
Reviewed changes
- Labeled the replaced PTY's exit —
markReplacedPtyStop/consumeReplacedPtyStopstampreplacedByRestarton the stopped owner's exit, andisPtyExitReplacedByRestartmakes the pane exit handler, parked watcher and pre-handler buffer treat that delivery as a handoff rather than a death. - Moved the detached restart's cleanup after a successful adopt —
releaseReplacedCodexPanePty(watchers, handlers, pre-handler state) now runs only once the replacement is bound, so a failed stop leaves the old PTY's observers intact; the pre-spawn disposal was removed. - Made the replaced stop one-shot via a claim —
claimReplacedPtyIdreplaces the plainreplacesPtyIdconnect option, anddispose()kills an id that no spawn carried. - Tests — main-side label/unlabel cases, detached failure-retention and mid-restart reveal cases, a parked-watcher replacement exit, and the hibernation-wake one-shot claim.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
| const shouldSendLocalCwdFallback = | ||
| cwdFallback === 'worktree' && !connectionId && !admittedSessionId | ||
| // Why: a reattach under an admitted session id must never stop the PTY it is reattaching. | ||
| const replacesPtyId = admittedSessionId ? null : (connectOptions.claimReplacedPtyId?.() ?? null) |
There was a problem hiding this comment.
The replaced-PTY id is claimed here before window.api.pty.spawn resolves, so a spawn rejected by a failed main-side stop has already cleared session.pendingReplacedPtyId. The new dispose() kill then has nothing to stop, and since releasePaneTransportForRestart detaches the old transport, the following destroy() reaches disconnect() with no id and no longer kills it either — the pre-PR mounted path always did. The detached path reopens the card on a failed stop; the mounted path has no equivalent, so a pane closed after a failed stop can leave the old Codex running with no renderer reference.
Technical details
# Failed stop is no longer backstopped on the mounted path
## Affected sites
- `src/renderer/src/components/terminal-pane/ipc-pty-spawn-request.ts:43` — the claim runs before `window.api.pty.spawn`; a rejection loses the id.
- `src/renderer/src/components/terminal-pane/pty-connection/session-reconcile-dispose.ts:180` — the new kill only fires for an unclaimed id.
- `src/renderer/src/components/terminal-pane/pane-restart-transport-handoff.ts:17-20` — `detach` nulls the transport's `ptyId`, so the subsequent `destroy()` → `disconnect()` sees no id and does not call `pty:kill`.
- `src/renderer/src/components/terminal-pane/use-terminal-pane-process-exit-actions.ts:82` — the mounted restart swapped `transport.destroy()` (which killed) for this handoff and has no failure recovery.
## Required outcome
- Either re-arm the claim when `pty:spawn` rejects (so dispose can still stop a still-alive old PTY), or document that a failed stop deliberately leaves the process for a retry the mounted path cannot currently trigger.
## Open questions for the human
- Is the failed-stop process intentionally left alive for a manual retry, or should a closed pane always reap it?Co-Authored-By: Claude <[email protected]>
Review status: ready for reviewHead: What the review didFive commits in total. The first is the original fix; the rest came out of review.
After two loops found races of the same kind, a separate architecture review asked whether the approach should change. Its verdict: keep the design, where main owns "reserve the pane, stop, launch" as one request. The alternatives it weighed are listed in the PR body under Why. The next full review loop came back clean. A readiness checklist ran at the start and end, both passing with nothing blocking. Live QAmacOS, real Codex, isolated dev build at
Screenshots are in the PR body. QA gaps:
Known limits
Seen during testing, not caused by this PR
|

ELI5
If a Codex terminal is running with an old Codex setup, Orca asks you to restart it. Clicking Restart was supposed to close that Codex and start a new one. Instead, Orca often reconnected to the same old Codex, so the warning came straight back, or it ended up with an empty shell and no Codex at all. Now Restart closes the old one first and then starts a new one in the same place.
What Changed
Before: clicking Restart on the "Codex setup changed" / "Account switched" card either:
After: the old Codex is stopped and a new Codex starts in the same pane under the current setup. The card does not come back. This works for the tab you are looking at and for tabs that are not on screen.
The mechanism. When a pane asks for a process and it still has a live one, the main process reconnects it to that process. That is how panes survive an app restart. Restart ran into that rule because the renderer sent "kill the old process" and "start a new one" as two separate requests, with nothing to keep them in order:
Restart is now a single request that the main process sequences:
pty:spawnaccepts an optionalreplacesPtyId. The main process first reserves the pane, which is the same per-pane reservation that already merges concurrent spawns. It then stops that process using the same code aspty:kill(moved intostopRendererOwnedPty), and only then launches. The launch goes down the existing "the pane's process is gone, start fresh" path, which records the current Codex setup. Anything else that asks for the pane in the meantime, such as a tab revealed mid-restart, joins the new process instead of reconnecting to the dying one.replacedByRestart. The renderer reads that label as "this pane is getting a new process", not "this pane died". So it does not close the tab, collapse a split, or replay the exit into a tab opened mid-restart. If the stop fails, no label and no exit are sent, and the pane keeps everything it had.replacesPtyIdonly with the first fresh spawn after a restart. If the pane is closed or parked before that spawn goes out, the old process is killed instead of being left running.Why
The "reconnect to the live process" rule is correct, and so is Restart. They conflicted only because the ordering lived in the renderer, split across two requests. Putting "reserve the pane, stop, launch" into one request that the main process owns removes the race instead of guarding against it. Labeling the exit at the one place every exit leaves the main process means each renderer consumer does not need its own check to tell a replacement apart from a death.
Alternatives considered:
Linked Issue
Fixes #18174
Visual Proof
Real Codex 0.156.1 in an isolated dev build on macOS. Codex was started under one Codex home, Orca was quit (Codex kept running), and Orca was relaunched with a different
CODEX_HOME, so the pane's setup is outdated. Then Restart was clicked. The sandbox has no Codex login on purpose, so the new Codex stops at its sign-in screen.Before (main): the card, then 38s after Restart the pane is a plain shell with Codex gone. The main log shows
pty:spawnfailing withTerminalKilledError ... was explicitly killed.After (this branch, head
99dd6f22e4): the same card, then 38s after Restart a new Codex process (new pid) runs in the same tab. The recorded setup matches the current one, and the card stays gone.After, tab not on screen: restarted while hidden, then revealed. It shows the new Codex, the tab is still open, and there is no card.
After, tab never shown since launch: restarted while no view existed for it, then revealed. It shows the new Codex and no card.
Testing
Live, macOS, real Codex 0.156.1, isolated dev build at
99dd6f22e4. The last commit after that changes only test typing. Every Codex was left without a login, so each stops at its sign-in screen.The hidden-tab restarts were triggered through the store action behind the status-bar "Restart N Sessions" button. That button only appears when Codex usage meters are shown, which needs a login the test sandbox deliberately does not have.
Automated:
src/main/ipc/pty-pane-restart-replace.test.ts, main process with a real IPC handler:codexlaunches;replacedByRestart.pane-restart-transport-handoff.test.ts:pty-connection-hibernation-wake.test.ts:codex-detached-pane-restart.test.ts:terminal-parked-watcher-sleep-preserved-exit.test.ts: a labeled exit does not close a parked tab.pnpm tc,oxlint,check:code-quality:changed, and the static-analysis audits (audit:anti-slop,audit:code-quality:native,audit:code-quality:type-aware) pass locally. Thesrc/main/ipc,src/preloadand terminal-pane suites pass.Not tested live: Windows, Linux, SSH. SSH has no new automated coverage; the stop reuses
pty:kill's SSH handling unchanged.AI Disclosure
Review
Agent skill upstream boundary
docs/reference/agent-skill-sharing-upstream-boundary.mdand copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.Notes
pty:kill's code, so a disconnected host still gets a durable stop order.replacesPtyIdandreplacedByRestarttravel only between the renderer and its own main process in the same build. Nothing crosses a versioned wire (mobile, paired web, relay).replay guard released for pane 1about 20s after a relaunch that restores panes, with or without a restart.Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)