Repository navigation
fix(codex): skip the Codex hook under a Claude background job - #24641
Conversation
Walkthrough
Priority: Severity of issue fixed: Medium Merge Risk:
|
There was a problem hiding this comment.
ℹ️ No critical issues — one minor test observation inline.
Reviewed changes
- Shared background-job guard extracted — the three inline
CLAUDE_JOB_DIRguard lines move fromsrc/main/claude/hook-script.tsintosrc/main/agent-hooks/claude-background-job-guard.tsas POSIX and Windows builders, matching thegrok-replay-guard.tsshape. - Claude hook reuses the builder at the same position — POSIX after the Devin skip and before
buildHookProcessCapture, Windows after the env guards; the re-export ofWINDOWS_CLAUDE_BACKGROUND_JOB_GUARDkeepswindows-hook-files.tsimporting unchanged. - Codex hook now applies the guard — POSIX after
buildPosixHookSpoolLinesand before endpoint load, Windows after the env guards and before the curl post, so acodex execunder a Claude background job neither posts nor spools. - New test exercises the generated POSIX script against a fake
curl, plus a text check that the Windows guard precedes the post.
deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏
| const { res, curlCalls, spoolExists } = runCodexHook({ CLAUDE_JOB_DIR: '/fake/job' }) | ||
| expect(res.status).toBe(0) | ||
| expect(curlCalls).toBe(0) | ||
| expect(spoolExists).toBe(false) |
There was a problem hiding this comment.
This assertion cannot currently fail: the test always supplies ORCA_AGENT_HOOK_ENDPOINT, ORCA_AGENT_HOOK_PORT, ORCA_AGENT_HOOK_TOKEN and ORCA_PANE_KEY, so the unguarded path takes the curl branch and never reaches spool_hook_event either. Dropping the vars (or a second case without them) would let the no-spool claim actually pin the guard. Non-blocking — the curlCalls assertions carry the test.
Claude background-job workers, and the `codex exec` runs they spawn, inherit the dispatching pane's ORCA_* env, so the Codex hook posted events against a pane the run never lived in. The Codex hook now exits when CLAUDE_JOB_DIR is set, like the Claude hook already does. The guard moves into agent-hooks/claude-background-job-guard.ts and is shared by the Claude hook, Claude statusline, Windows Claude entry and Codex hook. POSIX exits after stdin capture; Windows exits before owning stdin so an abandoned stdin cannot hang the drain. Pane spawns now drop an inherited CLAUDE_JOB_DIR so an Orca launched from a Claude background job does not silence its panes' hooks. Fixes stablyai#24516 Co-authored-by: Neil <[email protected]>
063d434 to
bd44f35
Compare
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

Minor · Preserve an explicitly configured CLAUDE_JOB_DIR. · pi-agent.ts:145-155
src/main/ipc/pty/host-env/pi-agent.ts:145-155
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick winPreserve an explicitly configured
CLAUDE_JOB_DIR.
PtySpawnIpcArgs.envaccepts explicit child environment values, and the runtime copies that value into the PTY spawn environment. This helper still addsCLAUDE_JOB_DIRtoenvToDelete, so the provider removes it after merging the child environment. The Codex hook then misses its background-job guard and can post or spool an event.Suggested fix
return [ ...CLAUDE_CHILD_SESSION_STAMP_ENV_KEYS.filter((key) => env[key] === undefined), ...ORCA_AGENT_SESSION_CALLER_ENV_KEYS, - // A pane is never a Claude background job; an inherited job dir would silence its agent hooks. - 'CLAUDE_JOB_DIR' + ...['CLAUDE_JOB_DIR'].filter((key) => env[key] === undefined) ]

Review info
-
Run configuration-
- Source mode: Auto
- Repository: Unavailable
- Branch: Unavailable
- File: No configuration file from this repository applied
- Commit: Unavailable
- Layers: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
295bc116-28c4-4895-9a89-79b6f8af0efe
Files selected for processing: 12-
src/main/agent-hooks/claude-background-job-guard.tssrc/main/agent-hooks/hook-script-outside-orca.test.tssrc/main/agent-hooks/managed-hook-stdin-lifecycle.test.tssrc/main/agent-hooks/managed-hook-timeout.test.tssrc/main/agent-hooks/spool.test.tssrc/main/claude/hook-script.tssrc/main/claude/statusline-script.tssrc/main/claude/windows-hook-files.tssrc/main/codex/codex-hook-script.tssrc/main/codex/hook-service-wsl-runtime.test.tssrc/main/ipc/pty/host-env/pi-agent.tssrc/main/ipc/pty/pty-spawn-env-terminal-basics.test.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.
-
Move the neutral-JSON test next to codex-hook-definition after the Codex hook modules moved under hooks/, and expect '{}' from the Claude background-job guard path (stablyai#24641) too: Codex still parses the hook's stdout there, so an empty reply fails closed the same way (stablyai#16356).
Move the neutral-JSON test next to codex-hook-definition after the Codex hook modules moved under hooks/, and expect '{}' from the Claude background-job guard path (stablyai#24641) too: Codex still parses the hook's stdout there, so an empty reply fails closed the same way (stablyai#16356).

ELI5
When Claude Code runs a task in the background, its helper process keeps the Orca pane variables of whichever pane first started it. If that background task runs
codex exec, Orca's Codex hook reported the run against that old pane, so a pane running Claude got labelled "Codex". The Claude hook already ignores events from Claude background jobs. This change makes the Codex hook ignore them too.What Changed
Before: a
codex execunder a Claude background job (CLAUDE_JOB_DIRset) inherited the dispatching pane'sORCA_*env. The managed Codex hook posted, or spooled, events with that pane's key, so an unrelated pane showed a Codex run that never happened there.After: the Codex hook exits quietly when
CLAUDE_JOB_DIRis non-empty. Nothing is posted and nothing is spooled. Codex runs that are not under a Claude background job behave as before.Mechanism:
src/main/agent-hooks/claude-background-job-guard.tsholds the one shared guard:WINDOWS_CLAUDE_BACKGROUND_JOB_GUARD(cmd) andbuildPosixClaudeBackgroundJobGuardLines()(sh). The Claude hook, the Claude statusline script, the Windows Claude entry and the Codex hook all use it now, in place of their inline copies. The generated Claude hook and statusline scripts are byte-identical tomain. All 72 script variants were checked..cmdscript: the guard runs right after the existing env guards. It is anexit /b 0before curl reads stdin, so an abandoned stdin cannot hang the hook ([Bug]: Codex managed hook leaves persistent cmd.exe windows outside Orca on Windows #11549).getInheritedAgentSessionStampEnvKeysToDelete) now drop an inheritedCLAUDE_JOB_DIR. A pane is never a Claude background job, so an Orca launched from inside one no longer silences its panes' Claude and Codex hooks.CLAUDE_JOB_DIR: '', so a test runner started inside a Claude background job still exercises the posting path.Why
CLAUDE_JOB_DIRis the signal the Claude hook already uses for background jobs (#15304). With this change both hooks agree. Real per-process pane attribution (#23643) needs a process lookup that is not reliable on every platform, WSL and SSH included, so this PR takes only the part that is safe today. One shared guard replaces three copies of the same lines, so they cannot drift apart.Out of scope: a foreground Claude running
codex execin the same pane (#13341).Linked Issue
Fixes #24516
Visual Proof
N/A. Nothing on screen changes. The fix is in the text of a generated hook script and in the PTY env. Instead, here is a probe that runs the generated POSIX Codex hook with a fake
curland an inherited pane env:The diff of the generated scripts versus
mainis only the added guard lines: oneif not "%CLAUDE_JOB_DIR%"=="" exit /b 0incodex-hook.cmdand a 3-lineif [ -n "$CLAUDE_JOB_DIR" ]block incodex-hook.sh.Testing
Reproduced on
main: the new case inhook-script-outside-orca.test.ts(real Codex POSIX script, inherited pane env,CLAUDE_JOB_DIRset, unreachable endpoint) fails because the failed post leaves a spool dir. It passes on this branch.Added a drain-before-skip case in
managed-hook-stdin-lifecycle.test.ts(POSIX, no EPIPE). Addedcodex-hook.cmdwithCLAUDE_JOB_DIRto the Windows open-stdin skip list, which runs on Windows CI.Added a PTY spawn env case: an inherited
CLAUDE_JOB_DIRis stripped.pnpm testoversrc/main/claude/,src/main/codex/and the touched agent-hooks and PTY test files: 357 files, 3847 tests passed on macOS.oxlintandoxfmtpassed on the changed files (pre-commit).Platforms: macOS run locally. Windows cmd behaviour is covered by the Windows-only stdin lifecycle case in CI. SSH and WSL use the same POSIX script, and each host refreshes its own managed script, so mixed client/server versions are unaffected.
I manually tested these changes locally
Automated tests added/updated
Notes
Original fix by @stakost in #24641; maintainer follow-ups add the shared statusline guard, Windows and drain coverage, and the PTY env strip.