Skip to content

fix(codex): resolve native pid behind npm launcher - #1772

Merged
deepcoldy merged 3 commits into
deepcoldy:masterfrom
leithda:fix/codex-npm-launcher-pid
Oct 10, 2026
Merged

deepcoldy merged 3 commits into
deepcoldy:masterfrom
leithda:fix/codex-npm-launcher-pid

Conversation

@leithda

@leithda leithda commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Summary

  • treat managed Codex sessions as potentially launcher-backed, not only sandboxed sessions
  • resolve the native Codex descendant PID for rollout ownership and process attestation
  • keep directly launched native Codex sessions unchanged when no descendant exists
  • cover both synchronous and late PID discovery paths with regression tests

Problem

The standard npm Codex entrypoint runs as a Node.js launcher and forks the packaged native codex binary. BotMux recorded the outer Node PID, while the native child held the rollout file descriptor. The positive ownership check therefore could not prove that a submitted Lark message entered Codex, so BotMux emitted the 90-second "Worker received but execution queue not confirmed" warning even though the turn was already executing.

Verification

  • bunx vitest run test/find-launched-cli-pid.test.ts test/codex-terminal-session.test.ts test/codex-worker-bridge-wiring.test.ts (83 tests passed)
  • bun run build

@deepcoldy

Copy link
Copy Markdown
Owner

Thanks for the fix — the diagnosis checks out. I verified that the current npm package (@openai/[email protected]) still ships bin/codex.js, an async spawn wrapper that stays resident and forks the packaged native binary, so the outer Node pid holds no rollout fd and the positive ownership proof can never fire. Rewiring backend.cliPid to the native child is the right layer for this (and it re-publishes the process attestation as a bonus): an attestation-only update wouldn't clear the false 90 s warning, because proveSid/acceptSid read pty.cliPid in src/adapters/cli/codex.ts.

Reviewed on a tree rebased locally onto current master (clean, no conflicts; CI green; targeted suites green on the rebased tree; reverting the one-line gate makes the two new cases fail in both sync/late phases). No blocking issues — one efficiency suggestion and a few nits:

P2 — direct-native installs now run ~31 needless process-tree scans per spawn. With codexLauncherActive unconditionally true, both the synchronous resolveCodexOwnershipPid(...) call and the 30-tick × 200 ms retry (scheduleWrapperRealCliPid) run for every managed Codex session — including standalone installs where the pane process already IS the native binary. findLaunchedCliPid then keeps returning null for ~6 s, and each tick shells out ps -A (~45–50 ms on an ~1.8 k-process host); multiplied across sessions during a fleet restart that's a real burst. Cheap fix: before descending, check the candidate's own comm via readComm(candidatePid) (a single /proc/<pid>/comm read, no ps), and skip both the descent and the retry when it already identifies as codex. The concurrently-open #1746 uses exactly this early-exit in its resolver.

P3

  • The codex resolver calls findLaunchedCliPid(lp, 'codex') without forwarding the configured custom Codex-compatible executable name (cfg.cliRuntime.executable / filterExecutable), so a renamed runtime is never discovered and silently stays on the launcher — no worse than today, just not covered.
  • Test nits in test/find-launched-cli-pid.test.ts: expect(...).not.toHaveBeenCalledWith(200) also passes with zero attestation calls, so toHaveBeenCalledTimes(0) pins the intent more tightly; and the launcherActive argument at the two synchronous resolveCodexOwnershipPid(...) call sites isn't pinned by any test (flipping it to false is a surviving mutant — the 200 ms retry masks it behaviorally).

Context for maintainers: #1746 (opened a day earlier) targets the same launcher shape but only updates the attestation pid, so it does not clear the false unconfirmed-execution warning this PR addresses; the two are complementary. Both introduce a startCodexLauncherPidResolve in adjacent regions, so whichever lands second will need a rebase.

This is an automated preliminary review — the final call is the maintainers'.

@deepcoldy

Copy link
Copy Markdown
Owner

Follow-up review on the current head (aaa8ee1f1, rebased locally onto d7e91a032 — clean, patch-id identical, nothing pushed).

Independently re-verified the diagnosis and the change

  • @openai/[email protected] still ships bin/codex.js: it spawns the packaged native binary asynchronously (not spawnSync/exec), so the outer Node launcher stays resident and the recorded pid holds no rollout fd. src/adapters/cli/codex.ts proveSid reads pty.cliPid, so an attestation-only update cannot clear the 90 s warning — rewiring backend.cliPid is the right layer.
  • Local rebase b45e56df8 has the same patch-id as the author commit; bun run build green; find-launched-cli-pid + codex-terminal-session + codex-worker-bridge-wiring = 97 green, +session-discovery = 166 green. Reverting the one-line gate to outerBwrapActive turns exactly the two new cases red in both sync/late phases, so the tests pin the change.
  • No mis-anchor on the directly-installed shape of this fleet: a native comm=codex TUI has no comm=codex descendant (its only descendants are a bun.exe tool and codex-code-mode/codex-code-mode-host, which cliIdForComm does not map to codex), and the live app-server hosts on the box are parented to systemd or to a sibling pane, never to our pane pid. findLaunchedCliPid returns null and the pid is preserved.

Observations beyond the previous pass

  1. P2 quantified — the gate re-runs ps -A 31×/spawn for direct installs. With an injected counter, 31 resolver invocations on a direct-native codex issue 31 ps -A calls (~1.5 s cumulative at idle) when the leaf has no children, and 3× = 93 calls (~4.5 s) when an in-process helper is present — the codex path does not pass filterExecutable, so an argv match counts too. The cheap fix stands: readComm(candidatePid) === 'codex' before descending, and skip both the sync resolve and the retry.

  2. P3 — the ineligible-wrapper test lost a non-vacuous case. The diff narrows [{ wrapperCli: '' }, { wrapperCli: ' ' }, { cliId: 'claude-code' }] to [{ cliId: 'claude-code', wrapperCli: '' }]. The whitespace case was load-bearing: pre-PR, !cfg.wrapperCli?.trim() short-circuited before the codex branch, so a whitespace wrapperCli + codex skipped the resolver. That blank-guard still exists and is still the only thing protecting that input. Restore it as its own case instead of dropping it.

  3. P3 — test-assertion strength. expect(...).not.toHaveBeenCalledWith(200) passes with zero attestation calls; toHaveBeenCalledTimes(0) pins the direct-native case. And the two synchronous resolveCodexOwnershipPid(pid, codexLauncherActive) call sites still aren't covered by an injected mock (the harness stubs resolveCodexOwnershipPid away), so flipping that argument survives; the 200 ms retry masks it behaviorally.

  4. Cross-PR note for whoever merges. fix(auth): 将 Codex 启动器身份锚定到真实进程 #1746's startCodexLauncherPidResolve uses a strictly stronger guard — backend identity and cliSpawnGeneration and cliLifetimeNonce and the launcher's process-birth identity — plus the readComm early-exit. This PR's resolver guards only backend identity + child pid (launcherRetryStillValid). Whichever lands second should adopt the generation/lifetime fence so the rebase doesn't silently drop it. Both touch the same spawnCli region; fix(auth): 将 Codex 启动器身份锚定到真实进程 #1746 inserts near the top, this one near the bottom.

No blocking issues; the change is correct and minimal. This is an automated preliminary review — final call is the maintainers'.

跟进 deepcoldy#1772 双审意见(P2+P3),在原「所有托管 Codex spawn 都向后代解析
原生 pid」基础上:

- P2:候选进程自身 comm 已是 codex 系原生叶时(standalone 直装),同步
  解析立即返回、重试循环 tick 内先经 isDirectLeaf 判定后退出,不再对每个
  tick 做全进程表 BFS(此前直装形态每次 spawn 空扫 31 次 ps -A,带 helper
  子进程时最多 93 次)。
- P3:findLaunchedCliPid/resolveCodexOwnershipPid 透传 cliRuntime 配置的
  自定义 Codex 兼容可执行名,官方名在自定义 runtime 下保持 fail-closed;
  恢复被删的空白 wrapperCli 用例(钉 trim 早退只关通用 wrapper resolver、
  不关 codex resolver);同步解析点的实参、retry 过滤参数与 native-leaf
  零扫描行为改为对 worker 真实函数切片的行为级断言(原 harness 把
  resolveCodexOwnershipPid 整个 stub,参数变异存活)。

Co-Authored-By: Claude Code <[email protected]>
@deepcoldy

Copy link
Copy Markdown
Owner

Applied the P2/P3 follow-ups from the automated reviews as a second commit on this branch (your original commit is untouched):

  • P2: when the candidate pid's own comm already identifies as a Codex native leaf (direct standalone install), the synchronous resolve returns immediately and the retry loop exits via a new isDirectLeaf tick guard — zero descendant scans, instead of 31 full process-table BFS scans per spawn (up to 93 with in-process helpers).
  • P3: the configured custom Codex-compatible executable name (cliRuntime.executable) now flows through both the synchronous resolve and the retry probes, with the official-name collision still failing closed; the whitespace-wrapperCli case is back as its own test; weak not.toHaveBeenCalledWith(...) assertions now also pin exact call counts per phase; the sync-site argument and the retry filter/leaf wiring are covered behaviorally — the harness now executes the real resolveCodexOwnershipPid/codexProcessIsNativeLeaf source instead of a stub, so six mutations (gate revert, leaf early-exit, filter args ×2, retry guard, scheduler guard) all fail tests.

Verified locally: tsc clean, build green, 186 related tests green (52 in the two wiring/discovery files), and the same tree rebased onto current master (47d4d38) is identical in behavior. Will merge once CI on the pushed head is green.

7600 字符窗口余量不足:主干其他改动在 deliverRawInput 函数体内新增行后,
sendToPty 锚点被推出窗口(合并态 CI test 2/3 + bun-test 双红),与被守护
的顺序契约无关。改为与同文件 writeAdoptMessage 用例一致的函数体边界
(到下一个顶层函数 markTurnRetired)。
@deepcoldy
deepcoldy merged commit 2e3a068 into deepcoldy:master Oct 10, 2026
10 checks passed
deepcoldy pushed a commit to 47seek/botmux that referenced this pull request Oct 10, 2026
deepcoldy#1746 所修复的问题(npm 版 Codex 经 sh/node 启动器启动时,
attestation 锚在启动器 pid,回合内工具进程的归属校验被回合前
进程快照拒绝 → current_actor_unverified)已由主干上的 deepcoldy#1753 与
deepcoldy#1772 修复并收口:复用 scheduleWrapperRealCliPid、全套重锚
backend.cliPid、configured executable 透传、切片级回归。
本提交补回该 PR 唯一未进入主干的增量——真实 worker 进程树 e2e:

- 真 spawn worker → sh → node 启动器 → 原生 codex → 工具进程,
  断言身份锚重锚到真实 codex 且 current-actor 校验通过,worker
  代次/进程启动时钟失效均被拒绝(pty/tmux 各一例)
- 新增 configured-runtime 重命名变体:自定义 leaf comm 只能经
  executable 透传匹配,静态 codex 映射不得命中(pty/tmux 各一例)

变异验证:resolver gate 回退到 sandbox-only、applyRealPid 漏发
attestation 均使四例失败;retry 路径断掉 executable 透传仅使
configured 两例失败(official 两例保持绿)。Node 22 与 Bun 1.4
下四例均通过。

Co-Authored-By: Claude Code <[email protected]>
@github-actions

Copy link
Copy Markdown

🚀 Released in v3.41.0

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.

2 participants