Repository navigation
fix: keep interactive Codex execution owned by its Orca terminal - #23791
brennanb2025 wants to merge 28 commits into
Conversation
Gate no-daemon launches on execution-host executable evidence, preserve old launches when support is unknown, and keep new panes from inheriting a shared server's identity and lifetime. Co-authored-by: m4air <[email protected]>
…y probe managed-agent-hook-controls.ts is part of the CLI's module graph, so importing the main-process Codex capability probe from it pulled main-only modules into the CLI project (tc:cli TS6307) and made `orca agent hooks on` spawn a pointless `codex --version` in a short-lived CLI process. The installer now reports each detected CLI through an injected callback, and only the desktop startup caller warms the Codex capability cache.
Overlay-only fish panes were deliberately left unwrapped because the only thing the fish init did was print the readiness marker. This branch gives them a second job, installing the codex function so a typed codex gets the launch policy, so the old contract test failed in shell contracts (daemon) and in the local provider suite. The contract now pins exactly that: wrapped with -C, no readiness marker, and a featureless fish spawn stays unwrapped.
Every typed interactive codex ran the Orca CLI twice before Codex started: once to prepare hooks, then again to ask whether this executable supports --no-daemon. Each CLI start is a full Node/Electron process in front of the launch. The wrapper now makes one call for interactive launches, and the CLI runs hook prep and the capability request concurrently; noninteractive launches keep the single hook-prep call they had. Hook prep can log to stdout (the trust-grant path prints a line), so the bash, zsh, fish and PowerShell wrappers read the verdict from the last line instead of the whole output. A failed hook prep still returns the verdict.
The relay's new Codex branch for PowerShell dropped -l, so pwsh as the login shell on a Linux or macOS SSH host stopped reading /etc/profile and ~/.profile, and it put the whole wrapper on the command line with -Command instead of -EncodedCommand like every other PowerShell PTY site. POSIX hosts now keep -l first and the script is delivered encoded.
Script launchers (npm's codex.js, Windows .ps1/.cmd shims) were never cached, so every typed launch re-ran `codex --version` through an interpreter within a 1 s limit, and a slow cold start silently fell back to the shared server. The same user got isolation on some launches and not others, and every Windows .ps1 launch spawned a hidden powershell.exe. - Cache every launcher by its own file identity (realpath + dev/ino/size/ mtime/ctime), bounded to 128 entries. Positive evidence is re-proven after 10 minutes so a version-manager shim that switches to an older Codex cannot keep receiving the flag; failed probes expire after 30 s. - Probe with the invoked launcher's directory on PATH (the pattern the existing Claude and relay version probes use), so `#!/usr/bin/env node` launchers from nvm find their node. The SSH relay probes with the relay command environment agent detection uses, and WSL probes set the same launcher directory through /usr/bin/env under --exec. - Allow the probe 5 s, like the existing version probes. The CLI still stops waiting after 2 s, so a slow first start no longer latches a negative; its verdict is cached for the next launch. - Accept prerelease version output for 0.156 and later: --no-daemon first appears in rust-v0.156.0-alpha.1, and 0.155.x (including its alphas) has none.
ORCA_CODEX_LAUNCH_PREFLIGHT is now an overlay key, so a terminal daemon started from inside an Orca pane (dev builds, nested Orca) carried the parent's value into every pane it spawned: each pane was wrapped, and its typed codex asked the parent Orca's CLI for hook prep and capability. The daemon now keeps the value only when this spawn supplied it, like ORCA_HISTFILE. This also stops three existing inheritance tests from failing when the suite runs inside an Orca terminal.
…okup The launch preflight is now enabled for every pane, so the resolver's hooksEnabled and managedHomePath options were accepted and ignored; they are removed from the type, the one production caller, and the tests. The Codex wrapper's PATH walk got its private result variable by regex-rewriting the shared lookup script's output; the builder now takes the result variable name directly, so the generated wrapper text is unchanged and no string surgery can hit an unrelated word.
Codex exits with "--no-daemon cannot be used with --remote" when both are present, and --remote is accepted after resume, fork, or a prompt. The wrapper only scanned options before the first positional, so `codex resume --remote ws://...` started working on main and failed to launch on this branch. The bash/zsh, fish and PowerShell classifiers now refuse the flag when --remote appears anywhere in the arguments, the same way they already did for an explicit --no-daemon.
The launch verdict came from the host: the wrapper asked the Orca CLI, which asked the desktop (or the SSH relay) to run `codex --version` elsewhere and cache the answer for up to 10 minutes. Two problems: - A version-manager shim picks its Codex from the pane's cwd and env, not from its own file. A cached or host-cwd answer could say 0.156+ while the pane runs 0.155, and Codex then refused to start. - A cold launch waited up to 2 s for that CLI round trip, and panes with a system-default Codex home had made no CLI call before. The typed-Codex wrapper now runs the resolved `codex --version` itself, in the pane, right before launch, and adds --no-daemon only for an exact codex-cli 0.156+ line. Anything else launches exactly as before. The probe costs one short process (a few ms for the native binary, one Node start for an npm launcher) and cannot be stale or run in another context. Hook prep still runs, but only when the pane carries an Orca-managed home, so system-default and SSH panes start no Orca CLI. This removes the capability RPC, the relay handler, the host caches and the CLI flags, so no new wire surface remains to skew between an old CLI, host or relay and a new wrapper.
cmd.exe runs Orca's Codex hook prep once at shell start. The launch policy now marks every pane with ORCA_CODEX_LAUNCH_PREFLIGHT, so every cmd.exe pane, including ones with hooks off or no Codex at all, started the Orca CLI before its first prompt. cmd.exe gets no launch flag, and prep only acts on an Orca-managed home, so gate it on ORCA_CODEX_HOME like the shell wrappers.
The in-pane version gate has a PowerShell copy that no macOS run exercises. Run it through pwsh for a supported, prerelease, older and unrecognised version so Windows and pwsh CI lanes prove the flag decision end to end.
… Codex home cmd.exe treats every later "& command" on an IF line as part of that IF. The hook-prep clause now checks ORCA_CODEX_HOME, and the pane's startup command is appended after it, so a terminal with a system-default Codex home silently skipped its startup command. Parenthesize the clause so its condition covers hook prep only.
Replace the three per-shell version-string parsers (bash/zsh, fish, PowerShell) with one rule: add the flag only when `<resolved codex> --no-daemon --version` exits 0 in the pane, with its output discarded. Codex's argument parser rejects an unknown --no-daemon before it reaches --version, so 0.155 and older fail the probe; the reverse order would pass there, so the order is pinned. This asks the installed Codex itself instead of mapping a version number to support, so source builds reporting 0.0.0 now get the flag, a future build that drops it fails closed, and unparseable version text no longer matters. The 0.156.0 constant is no longer needed.
The wrapper's PATH lookup ran inside a function without declaring its variables, so a zsh user with warn_create_global saw six "created globally" warnings every time they typed codex. Declare them local.
…apper tree On Windows PowerShell terminals Orca puts its shell-integration script and the startup command into one -EncodedCommand argument capped at 28,000 characters. Inlining the new Codex launch wrapper (argument classifier and --no-daemon probe) grew that script by about 2,000 characters, so the longest startup command that still fit dropped from about 4,800 to about 2,800 characters (cwd C:\Users\alice\orca\workspaces\orca\codex-daemon-identity). Longer commands, such as Codex worker launches with long prompts, fell back to being typed into the shell. The wrapper is now written into the existing content-addressed shell-ready tree next to the bash rcfile and zsh wrapper, and the Windows bootstrap loads it with a one-line ScriptBlock loader. ScriptBlock text is not gated by execution policy, unlike dot-sourcing a .ps1, so Restricted/AllSigned hosts keep the wrapper. No flag is added to the command line. If the tree cannot be written, the wrapper is inlined as before. About 5,400 characters now fit. Non-Windows pwsh and the SSH relay keep the inline wrapper; neither appends a startup command to the payload.
…ft unread The terminal environment builder read codexStatusHooksEnabled only to decide whether to install the typed-Codex wrapper. The wrapper is now installed in every terminal and hook preparation decides for itself whether hooks are on, so three spawn paths computed the value and nothing read it. Remove the option and its isCodexStatusHooksEnabled helper. No behaviour change.
The case added for the Codex launch preflight spawned /bin/bash, which on a Windows runner takes the Windows shell branch and never selects shell features, so its overlay assertion failed there. Pin process.platform the way the managed-agent env suite does.
Every shell's codex function was installed only when ORCA_CODEX_LAUNCH_PREFLIGHT named a launcher the shell could execute. When the build had no verifiable launcher (dev WSL, a missing bundle), or the shell rejected it (Git Bash's -x on a dev build's orca-dev.cmd), or an SSH host had no CLI bridge, the pane got no function and typed or Orca-launched Codex silently shared the background server again, even in panes whose system-default home never needs the CLI. Orca now marks every pane with ORCA_CODEX_LAUNCH_POLICY=1 (local panes, WSL via WSLENV, SSH with or without the bridge). That marker, not the CLI path, installs the function, selects the shell overlay, wraps Git Bash, and wraps relay fish and PowerShell. Only the prepare-codex call still needs a managed home and a launcher that shell can run. The daemon drops an inherited copy of either key, as it already did for the preflight.
The PowerShell no-daemon probe reset $LASTEXITCODE to 0 before probing. When that Codex could not run at all, as with npm's codex.ps1 under Windows PowerShell 5.1's default Restricted policy, the launch then failed and $LASTEXITCODE read 0, where the same failure without Orca leaves it where it was. The probe now restores the value it found.
The no-daemon probe now saves $LASTEXITCODE before probing, so a Codex that cannot run leaves it unchanged. It read $global:LASTEXITCODE directly. Before any native command has run in the session that variable is unset, and under Set-StrictMode reading an unset variable is an error. A pane with no managed home runs no hook prep first, so the first codex typed there printed two errors, and with $ErrorActionPreference = 'Stop' Codex did not launch at all. The probe now reads it through Get-Variable, which returns nothing instead of failing.
In a pane with an Orca-managed Codex home (Orca's default), the codex function first runs the Orca CLI's hook prep. That native call set $LASTEXITCODE to 0. When Codex then could not run at all, as with npm's codex.ps1 under Windows PowerShell 5.1's default Restricted policy, the caller saw 0 instead of the value it had before, which is what a plain codex launch leaves. Found on a real Windows 5.1 host; the earlier fix only covered the no-daemon probe. The codex function now saves $LASTEXITCODE on entry and restores it right before the launch. That one restore covers both hook prep and the probe, so the probe no longer restores it itself. $? still reads True after a failed launch. A plain PowerShell function cannot report False without throwing (which would stop a calling script) or becoming an advanced function (whose common parameters would capture Codex flags such as -i and -o), and main's managed-home wrapper behaves the same way.
In a PowerShell pane, `Get-Content prompt.md | codex exec -` gave Codex no input: a native command run inside a function never receives the function's pipeline input on its own, so Codex read the terminal instead. Main already had this in panes with an Orca-managed home; this branch installs the function in every Orca PowerShell pane (system-default homes, dev builds, SSH), so it reached panes that piped correctly before. The function now pipes $input into the launch when it was called with pipeline input, the same way npm's codex.ps1 shim forwards it. A call without piped input launches exactly as before.
The PowerShell codex function ran hook prep, the argument check and the --no-daemon probe under the caller's own Set-StrictMode and $ErrorActionPreference. Those are settings a user picks for their own code, and they reached Orca's steps: - With $ErrorActionPreference = 'Stop', any error record written during the probe (for example by a codex.ps1 shim) became a terminating error, so the probe reported the flag as unsupported and Codex launched without --no-daemon. On Windows PowerShell 5.1, Stop does the same for any stderr line from a native command, which could also cut hook prep short. - An earlier defect on this branch (first launch failing under Set-StrictMode) came from the same cause. Hook prep, the argument check and the probe now run in one helper, __OrcaCodexLaunchFlags, which pins Set-StrictMode -Off, $ErrorActionPreference = 'Continue' and $PSNativeCommandUseErrorActionPreference = $false in its own scope, restores $LASTEXITCODE before it returns, and returns only the flags. The Codex launch itself, with its pipeline input forwarding, still runs under the user's own settings. The piecemeal save/restore in the codex function is gone because the helper owns it. The PowerShell tests move to their own file, which keeps the original test file under the line limit; the shell contracts CI job lists it.
When Codex was removed after the pane started, the codex function wrote "codex executable not found" and only then set $LASTEXITCODE to 127. Under $ErrorActionPreference = 'Stop' that Write-Error throws, so the 127 never ran and the caller saw whatever exit code was there before. The code is now set first. Main had the same order.
…old design The launcher resolver no longer takes hook or Codex-home options, so the "without consulting hooks or the selected Codex home" case repeated the packaged darwin launcher case exactly, and it named the hook-prep launcher "the launch policy". Two relay tests still said "version gate" and one env test said "capability gate", both from earlier revisions; they now name the launch policy and the hook-prep launcher they actually check.
The two SSH create-operation tests pin the exact pty.spawn params. Since every SSH pane now carries ORCA_CODEX_LAUNCH_POLICY=1 in its env, both failed on the full unit-test job. "Byte-for-byte legacy" is about the request fields: an old relay must get no agent-session create or claim fields and no capability probe. Pane env is an opaque map that every relay version copies into the shell; an old relay has no reader of the marker, so it only shows up in `env`. The bridge env keys already reach old relays the same way.
Review status (draft, not marked ready)Verdict: the fix works for users on macOS, Windows and SSH, the final review loop came back clean, and the architecture was challenged twice and kept. The PR stays a draft for the coordinator to decide; nothing was merged. What users get: with Codex 0.156+, each Orca Codex terminal now has its own sidebar row and its own identity, and closing one Codex terminal no longer kills the others. On main, three Codex terminals showed one row and closing the first broke the rest (reproduced live on macOS and over SSH). Codex 0.155 and older launch exactly as before. Tradeoff to accept: Orca-launched interactive Codex no longer uses Codex's shared background server, so "Run in background" does not outlive the Orca terminal and these sessions don't appear in Review loops: 12 loops; loops 5 and 11 returned clean on product code, and loop 12 only updated a test expectation that CI's full unit-test job caught. Main changes during review:
Live QA, all on remote machines with real Codex and isolated Codex homes (real-home Codex config hashes unchanged on every box):
Checks: Open items for the coordinator:
|
What this fixes
With Codex 0.156 or newer, every Codex session on a machine can run through one shared Codex background server. That server keeps the environment of the first terminal that started it, so Orca's second and later Codex terminals report as the first one.
What users saw: only the first Codex session appeared in the sidebar; status and replies from other panes landed on the first pane (even panes in another worktree); orchestration commands from a second worker used the first worker's terminal handle and inbox; and closing the first Codex terminal stopped the shared server, so the other Codex terminals showed "app-server session could not be restored" and their sidebar rows disappeared.
Who hits it: anyone running two or more interactive Codex terminals in Orca with Codex 0.156+, on macOS, Linux, Windows, WSL or an SSH host. It was worst for the system-default Codex home and for Linux/Windows managed homes; macOS managed homes mostly avoided it already because Orca's socket-path guard keeps the shared server off there (that guard is unchanged).
Product tradeoff: Orca-launched interactive Codex no longer shares Codex's background server, so Codex's "Run in background" does not outlive the Orca terminal, and these sessions do not appear in
codex agentsorcodex queue.Not affected: Native Chat (structured Codex), non-interactive Codex commands (
exec,login,app-server, completion and the other subcommands),codex resume --remote …, Codex older than 0.156, and sessions that were already running before the update (nothing existing is converted, and no running server is stopped).ELI5
Before, all Codex windows could quietly share one engine, and that engine only remembered who the first window was, so everything it did was labelled with the first window's name. Closing that first window turned the shared engine off for everyone.
Now each Orca terminal that starts Codex asks Codex to run its own engine, so each one carries its own name and closing one leaves the others running.
Before / After
Before: Orca put each terminal's identity (pane key, launch token, terminal handle, hook endpoint) in the terminal's environment. Codex's shared server inherited the environment of whichever terminal started it first and replayed that environment into every later session's hooks and tool commands. Orca had no control over which process ran the work.
After: every Orca terminal shell (bash, zsh, fish, Git Bash and PowerShell, locally and in the relay shells on SSH hosts) defines a
codexfunction. For an interactive launch (fresh,resume,fork), it first runs<the codex it is about to start> --no-daemon --versionin the pane's own folder and environment. If that exits 0, it starts Codex with--no-daemon, so the Codex process tree belongs to that terminal and inherits that terminal's identity. If the check fails for any reason, it starts Codex exactly as before. Orca's own agent launches run through the same shell, so typedcodexand Orca-launched Codex take the same path.The check asks Codex itself whether it accepts the flag, rather than parsing version strings. The argument order is deliberate: Codex 0.155 accepts
--version --no-daemon(it stops at--version) but rejects--no-daemon --versionwith exit 2. Real binaries checked: 0.155.0 rejects; 0.156.0-alpha.1, 0.156.0 and 0.158.0 accept; none of them start a server during the check.Every Orca pane (local, WSL, SSH with or without the remote Orca CLI) carries
ORCA_CODEX_LAUNCH_POLICY=1, which is what installs the function; it does not depend on Orca's own CLI being present. Managed homes with hooks on still make the oneorca agent hooks prepare-codexcall they made before; system-default homes make no Orca CLI call at launch.On PowerShell, Orca's own steps (hook prep, the argument check and the probe) run in one helper with fixed settings, so a user's
Set-StrictModeor$ErrorActionPreference = 'Stop'cannot break them, and$LASTEXITCODEafterwards is Codex's own. The launch itself runs with the user's settings and forwards piped input. The function is written to a file in Orca's existing generated shell-wrappers folder and loaded with a one-line loader that execution policy does not block, so the launch command stays short: the longest startup command that fits in the launch arguments is about 5,400 characters (about 4,800 on main). cmd.exe keeps its managed-home hook prep and gets no flag.Why this design
Alternatives rejected:
daemon_auto_start = falsesetting: it only stops Codex from starting a server; a server that is already running is still reused.codexshim on an Orca-controlled PATH folder: still needs every shell set up, fights version managers that rewrite PATH, and is fragile on Windows.Bug class removed: a Codex session can no longer inherit another terminal's identity or lifetime through a shared server, for every launch that goes through Orca's shell function. No second status store, no pane guessing by folder, and no persisted state were added; the hook server stays the single status authority.
Known weaknesses
codexfunction keep today's behaviour and can still share a server:command codex,env X=1 codex, an absolute path to Codex, a user-definedcodexalias or function (which also catches Orca-launched workers in that shell), Codex installed after the shell started, cmd.exe, fish inside WSL, Codex options the argument check doesn't recognise (for example--worktree), and SSH hosts where Orca cannot write its relay wrapper folder at all (the pane falls back to a plain login shell, exactly as before).codex --no-daemon --versionhangs, the launch waits; Ctrl-C returns to the prompt. A hand-written wrapper that always exits 0 but runs Codex 0.155 or older would get the flag and fail to start.$?stays True after a failed launch; a bare--is dropped for.exe/.cmdinstalls (npm'scodex.ps1already behaves this way without Orca); on pwsh 7.4+ piped bytes become text for.exeinstalls;Get-Command codexshows a function.codexlaunches without the flag.--no-daemonwas not exercised; the managed-home launch lane was covered by tests and Windows checks, not by the macOS/SSH live runs.ORCA_CODEX_LAUNCH_POLICYvariable appears in every pane's environment. With the shared server off, Codex may set the terminal title itself, so a Codex tab can take its first prompt as its name (seen once in QA).Linked Issue
Visual Proof
Live QA on a remote macOS machine with real Codex 0.158.0 (isolated Codex home, dummy model provider, so the model call fails by design and the panes show "Reconnecting"; hooks and status still fire). Three Codex terminals: t1 and t2 in one worktree, t3 in a second worktree.
Before (main): only one sidebar row (t1) for three Codex terminals.

Before (main), after closing t1: the shared server stopped, t2 shows "app-server session could not be restored", and every agent row is gone.

After: t1, t2 and t3 each have their own row.

After, closing t1: t2 and t3 keep running and keep their rows.

Re-checked on the final code (macOS, real Codex 0.158.0): three rows, then two rows survive closing the first.


Windows (real Codex 0.157.1, Git Bash, PowerShell 7 and 5.1): each Codex terminal has its own row, and closing t1 leaves t2 and t3.



SSH (Linux container host, real Codex 0.158.0). Before (main): three Codex panes, one row (b1); the red banner is an unrelated existing restore error.


After: separate rows, and closing t1 leaves t2 and t3 running.



Testing
--no-daemonand its ownORCA_PANE_KEYand terminal handle, with no shared server; closing the first leaves the others running; a Codex reporting 0.155.0 launches without the flag. The real-home Codex config hashes were unchanged.--no-daemonand get their own row; closing the first leaves the others; Codex 0.155.0 launches without the flag; the "host Job Object prevents daemon detachment" error (STA-8514) never appeared; cmd.exe startup commands run; a ~5,000-character startup command goes through the launch arguments; underSet-StrictMode -Version Latest+$ErrorActionPreference = 'Stop'the launch still gets the flag;$LASTEXITCODEafter a blocked or failed launch matches plain PowerShell; piped input reaches Codex; 0 Defender detections. Windows-only test files pass on the box; the 3 failures there also fail on the merge base.--no-daemonand its own pane key, no shared server starts, closing the first leaves the others, 0.155 launches without the flag, bash/zsh/fish login shells still run their profiles. On main the STA-8467 failure reproduces (one row; closing the first kills the rest).tc:clipasses;tc:nodereports onlysrc/main/codex/codex-background-task-frames.tsimporting a module the branch base lacks, which current main has fixed (the merge-ref typecheck in CI is green).Review
Supersedes #23411 (automation-authored), which reassigned shared-server hooks to a pane by worktree and process, leaving two panes in the same folder ambiguous and the shared lifetime in place.
Leaves #22727, #19215 and #23724 untouched. #19215 touches 11 of the same files:
src/main/ipc/pty/host-env/assembly.ts(Codex home selection),src/main/__fixtures__/shell-wrapper-snapshots/local-bash-rcfile.txt,src/main/daemon/pty-subprocess/spawn-environment.ts,src/main/daemon/pty-subprocess-windows-shell-launch.test.ts,src/main/ipc/pty-spawn-env-terminal-basics.test.ts,src/main/powershell-osc133-bootstrap.tsand its test,src/main/providers/windows-shell-args.tsand its test,src/main/providers/local-pty-provider-windows-shell-launch.test.ts, andsrc/main/providers/local-pty-shell-ready-wrapper-generation.test.ts; keep its home-selection changes when combining them. No file overlap with #22727 or #23724. Does not supersede #22866.Agent skill upstream boundary
Checklist