Skip to content

perf(codex): resolve the Codex CLI once per spawn recheck, off the main thread on Windows - #35967

Merged
nwparker merged 1 commit into
mainfrom
perf/codex-spawn-lookup-takeover
Oct 29, 2026
Merged

nwparker merged 1 commit into
mainfrom
perf/codex-spawn-lookup-takeover

Conversation

@nwparker

@nwparker nwparker commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 7 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​188 $\color{#cf222e}{\Huge{\mathbf{−}}}$​7 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​181
Prod 7 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​298 $\color{#cf222e}{\Huge{\mathbf{−}}}$​164 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​134

ELI5

Every time a terminal opened, Orca looked up where the Codex CLI lives so it could check its hooks. It searched PATH and install folders twice, synchronously, on the main thread. That cost about 61 ms per spawn on Windows. Now it looks once, and on Windows the search no longer blocks the main thread.

What Changed

  • Look up once: each Codex hook reconcile resolves the path once (resolveAppCodexPath) and passes it to both resolveCodexHookAnswer and readKnownCodexHookAnswer. This applies on every platform.
  • Off the main thread on Windows: the spawn-time recheck uses a new async resolver on win32 only (resolveCodexCommandForSpawn in src/main/codex-cli/command.ts). macOS and Linux keep the sync resolver, which takes under 1 ms, with half the scans.
  • One shared lookup: new src/shared/cli-fs-reads.ts expresses each filesystem read as a step, with a sync runner and an async (fs/promises) runner. node-cli-command-resolution.ts and nvm-version-directories.ts drive one cliCommandLookup walk (PATH, then nvm, then install folders) through either runner, so the sync and async results can't diverge. The async path makes no sync filesystem calls.
  • No lost rechecks: codex-hook-reconcile.ts sets a findingCodexAnswer flag while it resolves the path, waits up to 500 ms on the probe, and reads the saved answer. A spawn that arrives during that time gets one coalesced trailing rerun. This keeps the async scan from dropping a spawn's recheck, for example when Codex is installed or moved mid-scan.
  • No throttle and no cache: hooks are still rechecked immediately on every spawn, and PATH changes are picked up on the next one.

Why

Main-thread CPU per spawn on macOS (process.threadCpuUsage, 3 rounds):

Case main this PR
Codex missing, 69 PATH dirs 0.98 ms 0.45–0.51 ms
Codex missing, 95 dirs 1.35–1.44 ms 0.64–0.73 ms
Codex present 0.024 ms 0.024–0.026 ms

On Windows, the reporter's profile showed about 61 ms blocked per spawn (1,103 ms over 18 spawns). That is now an async scan plus a few small sync calls (fingerprintCodex). I haven't measured on Windows.

Per #27143, this is a free win on macOS and Linux and a substantial one on Windows.

Linked Issue

Fixes #35565

Takes over #35622 by @ctolkien, credited as co-author. Changes from that PR:

  • The 10 s spawn throttle is dropped, because it delayed hook rechecks.
  • The async lookup runs only on Windows.
  • There's a single sync/async lookup, so the two can't diverge.
  • A trailing recheck is added for spawns that arrive during an async answer.

Visual Proof

N/A: no UI change.

Testing

  • Windows parity test runs on any OS with platform: 'win32'. It checks:

    • extension order .cmd → .exe → .bat
    • PATH order
    • fallback to the npm global folder
    • PATH winning over install folders
    • nvm's default alias
  • A test where every *Sync call throws still resolves via the async path.

  • Regression tests:

    • Codex installed during path resolution
    • Codex upgraded or moved during the version probe
    • Both fail without the fix.
  • Touched tests: 308 files / 3,072 tests pass.

  • pnpm tc:node passes.

  • pnpm check:changed --maxWorkers=2: 24/24 checks pass.

  • Adversarial review by Opus and Codex (gpt-6.1-sol, xhigh), three rounds:

    • Earlier rounds found two problems, both fixed: spawns losing their recheck during the async answer phase, and sync nvm reads left in the async path.
    • Final verdict from both is MERGE.
    • Codex ran 14,400 sync/async parity comparisons, plus a storm of 4,000 spawns that coalesced into 41 passes.
  • One caveat exists on main and isn't changed here: a binary move that lands during the trust-config queue wait is still not rechecked.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

Co-authored-by: Chad Tolkien [email protected]

🤖 Generated with Claude Code

…in thread on Windows

Every native terminal spawn schedules a Codex hook reconcile, which resolved
the Codex path twice with a synchronous stat scan of PATH and install dirs
(~50ms main-process block per spawn on Windows with a long PATH and no Codex).

The reconcile now resolves the path once and passes it to both the live
lookup and the saved-answer read. On Windows, where each stat is slow, that
lookup runs every read (PATH candidates, nvm dirs and aliases, install dirs)
through fs/promises; macOS and Linux keep the sync scan, now run once. Sync
and async share one lookup that yields its reads, so they cannot diverge.

Hook rechecks stay immediate on every spawn and nothing is cached. A spawn
that lands before a run's hook-file pass (while it is still finding Codex or
waiting for its answer) gets one trailing rerun, so an install or upgrade
that moved Codex mid-run cannot leave hooks unchecked.

Fixes #35565

Co-authored-by: Chad Tolkien <[email protected]>
@greptile-apps

greptile-apps Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

The PR shares one resolved Codex path across each hook recheck and moves Windows filesystem searches off the main thread.

  • Codex hook rechecks reuse one path and run Windows searches asynchronously.
  • CLI searches can run asynchronously without changing how they choose a command.

No actionable issues were found.

Known limitation acknowledged by nwparker: a binary move during the trust-config queue wait is not rechecked. That behavior is unchanged.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Native pane spawn] --> B{Recheck running?}
  B -->|No| C[Resolve Codex path once]
  B -->|Still finding answer| D[Request one trailing pass]
  B -->|Writing hook files| E[Use current pass]
  C --> F{Windows?}
  F -->|Yes| G[Async filesystem reads]
  F -->|No| H[Sync filesystem reads]
  G --> I[Ask Codex using resolved path]
  H --> I
  I --> J[Wait up to 500 ms for answer]
  J --> K[Use live or saved answer]
  K --> L[Recheck hook files]
  L --> M{Trailing pass requested?}
  M -->|Yes| C
  M -->|No| N[Done]
Loading

Reviews (1) · Last reviewed commit: "perf(codex): resolve the Codex CLI once ..." · Reviewed by Greptile

@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Walkthrough

The change adds asynchronous CLI command lookup and updates Codex hook reconciliation to use a spawn-specific path. Reconciliation passes the resolved path to both live and saved-answer lookups. It can request another pass while answer lookup is in progress. Tests cover command lookup behavior and Codex binaries appearing or moving during a scan.

Priority: Normal

Severity of issue fixed: Medium

Merge Risk: Low · up to 6e452

Windows Codex launches can still briefly block while locating the CLI. This is a bounded performance concern, so the change is mergeable with owner awareness.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 29.41% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main changes: resolving the Codex CLI once per spawn recheck and moving Windows resolution off the main thread.
Description check Passed The description is mostly complete. It includes ELI5, What Changed, Why, a linked issue, visual proof, testing details, AI disclosure, and relevant performance and compatibility notes. The Review sect…
Linked Issues check Passed Issue #35565 requires reducing repeated synchronous Codex path-resolution cost during local terminal spawns. reconcileOnce now calls resolveAppCodexPath() once and passes that path to both hook-an…
Out of Scope Changes check Passed The changed production files implement the #35565 path-resolution and reconciliation behavior. The shared filesystem-read protocol, nvm directory helper, parity tests, and reconciliation regression te…
Full details: Description check

Explanation

The description is mostly complete. It includes ELI5, What Changed, Why, a linked issue, visual proof, testing details, AI disclosure, and relevant performance and compatibility notes. The Review section is empty, and manual testing and several checklist items remain unchecked, but these omissions do not make the description incomplete.

Full details: Linked Issues check

Explanation

Issue #35565 requires reducing repeated synchronous Codex path-resolution cost during local terminal spawns. reconcileOnce now calls resolveAppCodexPath() once and passes that path to both hook-answer operations. resolveCodexCommandForSpawn() uses resolveCliCommandAsync('codex') on Windows and preserves the synchronous resolver on other platforms. The shared lookup uses one PATH, nvm, and install-directory flow for sync and async reads. The reconciliation tests cover coalesced trailing reruns, installation during lookup, and binary movement during probing. The async resolver tests cover Windows ordering, fallback behavior, parity, and absence of synchronous filesystem reads.

Full details: Out of Scope Changes check

Explanation

The changed production files implement the #35565 path-resolution and reconciliation behavior. The shared filesystem-read protocol, nvm directory helper, parity tests, and reconciliation regression tests support that implementation. The changes do not establish unrelated feature work or a separate sync-scan objective.

Full details: Docstring Coverage

Explanation

Docstring coverage is 29.41% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 17 functions across 14 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


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: 26174b9e-fd4b-4ea5-ad10-a86964b8f31b
Commits

Reviewing files that changed from the base of the PR and between 4642bc0 and 6e45248.

Files selected for processing: 14
  • src/main/codex-accounts/runtime-home-status-hook-independence.test.ts
  • src/main/codex-cli/command.ts
  • src/main/codex/codex-hook-file-entry-binary-contract.test.ts
  • src/main/codex/codex-hook-hash-lookup.test.ts
  • src/main/codex/codex-hook-hash-lookup.ts
  • src/main/codex/codex-hook-real-home-status.test.ts
  • src/main/codex/codex-hook-reconcile.test.ts
  • src/main/codex/codex-hook-reconcile.ts
  • src/main/codex/codex-hook-toggle-off-wins.test.ts
  • src/shared/cli-fs-reads.ts
  • src/shared/node-cli-command-async-resolution.test.ts
  • src/shared/node-cli-command-async-resolution.ts
  • src/shared/node-cli-command-resolution.ts
  • src/shared/nvm-version-directories.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

Comment on lines +120 to +123
return lookupCodexHookAnswer(
codexPath ?? resolveCodexCommand(),
getManagedCommand(getManagedScriptPath())
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · Performance & Scalability · Quick win

Supported by static analysis

Script executed:

#!/bin/bash
# Find callers of resolveCodexHookAnswer / resolveCodexHookAnswerForLaunch that pass no path.
rg -nP --type=ts -C4 '\bresolveCodexHookAnswerForLaunch\s*\(' -g '!**/*.test.ts' src
rg -nP --type=ts -C3 '\bresolveCodexHookAnswer\s*\(\s*\)' -g '!**/*.test.ts' src

Repository: stablyai/orca

Length of output: 3916


Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- hook lookup implementation ---'
nl -ba src/main/codex/codex-hook-hash-lookup.ts | sed -n '1,190p'
printf '%s\n' '--- launch callers and owners ---'
nl -ba src/main/codex/codex-account-home-hook.ts | sed -n '1,130p'
nl -ba src/main/codex/codex-hook-service-implementation.ts | sed -n '180,275p'
printf '%s\n' '--- all non-test references to launch helper and relevant service methods ---'
rg -n -F --glob '*.ts' -g '!**/*.test.ts' -e 'resolveCodexHookAnswerForLaunch' -e 'installCodexHook' -e 'reconcileCodexHook' -e 'codex-account-home-hook' src/main src/shared || test "$?" -eq 1
printf '%s\n' '--- reconcile implementation ---'
nl -ba src/main/codex/codex-hook-reconcile.ts | sed -n '1,150p'

Repository: stablyai/orca

Length of output: 22993


Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- command resolver definitions ---'
nl -ba src/main/codex-cli/command.ts | sed -n '1,260p'
printf '%s\n' '--- complete reconcile answer flow ---'
nl -ba src/main/codex/codex-hook-reconcile.ts | sed -n '140,230p'
printf '%s\n' '--- launch-preparation callers ---'
rg -n -F --glob '*.ts' -g '!**/*.test.ts' -e 'installForLaunchPrep(' -e 'installForRuntimeHome(' -e 'reconcileCodexAccountHomeHook(' -e 'reconcileCodexHooksForLaunch(' src/main src/renderer src/shared || test "$?" -eq 1
printf '%s\n' '--- surrounding launch-preparation definitions ---'
nl -ba src/main/codex/codex-runtime-home-launch-preparation.ts | sed -n '1,130p'
nl -ba src/main/codex/codex-session-resume-launch-preparation.ts | sed -n '1,100p'
nl -ba src/main/orcad/orcad-codex-account-homes.ts | sed -n '1,140p'

Repository: stablyai/orca

Length of output: 16867


Script executed:

#!/bin/bash
set -e
nl -ba src/main/codex/codex-hook-service-implementation.ts | sed -n '55,205p'
printf '%s\n' '--- hook service export/binding ---'
nl -ba src/main/codex/hook-service.ts | sed -n '1,120p'
printf '%s\n' '--- relevant changed diff ---'
git diff 4642bc00d6198ae90a91eeacd60f8fff53389312 6e452483df4975377d36029de8cda9cbe7286d99 -- src/main/codex/codex-hook-hash-lookup.ts src/main/codex/codex-hook-service-implementation.ts src/main/codex/codex-runtime-home-launch-preparation.ts src/main/codex/codex-account-home-hook.ts

Repository: stablyai/orca

Length of output: 10167


Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- launch preparation factory bindings ---'
rg -n -F --glob '*.ts' -g '!**/*.test.ts' -e 'createCodexRuntimeHomeLaunchPreparation' -e 'createCodexSessionResumeLaunchPreparation' src
printf '%s\n' '--- saved-answer call sites ---'
rg -n -P --glob '*.ts' -g '!**/*.test.ts' 'readKnownCodexHookAnswer\s*\(' src/main src/shared || test "$?" -eq 1

Repository: stablyai/orca

Length of output: 2071


Use the asynchronous Codex path lookup for launch preparation.

The native launch-preparation path reaches resolveCodexHookAnswerForLaunch() without a path. This invokes synchronous resolveCodexCommand() on Windows for each local launch. reconcileCodexAccountHomeHook() also falls back to readKnownCodexHookAnswer() without a path.

Resolve one path with resolveAppCodexPath() and pass it to both the live lookup and the saved-answer read. Keep the launch wait timeout around the asynchronous lookup.

@nwparker
nwparker merged commit b95b11c into main Oct 29, 2026
49 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Other]: (perf) Every terminal spawn blocks the main process ~50ms resolving the Codex CLI path (sync PATH scan, twice)

1 participant