Skip to content

fix(launcher): pin codex routing via overlay-generated .env (#1802) - #1806

Merged
ranxianglei merged 4 commits into
masterfrom
2026-10-01_codex-overlay-env-protection
Oct 1, 2026
Merged

ranxianglei merged 4 commits into
masterfrom
2026-10-01_codex-overlay-env-protection

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What & why

bili codex on Windows: Apps MCP / model list / plugin-dir requests all failed with connection errors while normal conversation worked. Root cause (verified against source and the reporter's A/B matrix on their machine):

  1. codex's load_dotenv() (codex-rs/arg0/src/lib.rs, rust-v0.159.3) calls set_var() unconditionally for every non-CODEX_-prefixed key of $CODEX_HOME/.env after the launcher's spawn env is set. A user .env pointing HTTP_PROXY/HTTPS_PROXY/ALL_PROXY at e.g. socks5h://… silently re-routes codex off bili after start.
  2. With SSL_CERT_FILE set, codex's HTTP client switches to rustls (custom_ca.rs), and its reqwest build has no socks feature — so the socks5h proxy URL falls through to the generic path and the custom-CA HTTP leg dies instantly. The model WebSocket uses a different stack that does speak SOCKS → conversation kept working.
  3. The pre-existing overlay shared .env with the real home via symlink/hardlink, so the launcher had no seam to protect the injected routing.

Fix

The launcher now generates the overlay .env itself:

  • Managed keys — HTTP_PROXY, HTTPS_PROXY, ALL_PROXY, NO_PROXY, SSL_CERT_FILE, BILLION_CONTEXT_PROXY (matched case-insensitively, original key spelling preserved on rewrite) — are pinned to THIS launch's values; every other user line is preserved verbatim (comments, order, quoting); absent managed keys are appended; SSL_CERT_FILE is written with forward slashes (dotenv escape safety).
  • Generated file mode 0600 (it carries the user's secrets verbatim). Pre-existing shared symlink/hardlink entries are migrated to an owned file first. The real home's .env is never touched, never merged back into (existing generated-file exclusion in refreshOverlayHome covers both loops; pinned by tests).
  • The overlay is now built on every platform for routed launches, decoupled from MCP injection (a launch with MCP disabled still gets the protection). Direct-URL mode is unchanged (no proxy is injected there, so nothing to protect; overlay only when MCP needs the config block).
  • The codex MCP server definition now rides the overlay's merged config.toml on all platforms (it already did on Windows since [bug] Windows: codex MCP injection -c value loses its quotes through cmd.exe when node lives under a spaced path #681 because cmd.exe cannot transmit inline -c values embedding spaced/quoted paths); POSIX-only inline -c args are dropped — one mechanism instead of two. buildCodexMcpArgs removed (no remaining consumers).

Behavior changes (explicit)

Surface Old New Why
POSIX bili codex CODEX_HOME real ~/.codex; MCP via inline -c args (when enabled) <~/.codex>-bili overlay on every routed launch; MCP via merged config.toml same mechanism as Windows; required so the generated .env lives where codex reads it
$CODEX_HOME/.env effect under bili codex (routed mode) user file overrides launcher-injected proxy/CA after start (the bug) managed keys rewritten per launch; other vars preserved the fix
effective NO_PROXY (routed mode) whatever the user file / inherited env held localhost,127.0.0.1,::1 loopback-only bypass; matches jcode/aider precedent
new file — <CODEX_HOME>-bili/.env, mode 0600 carrier for the above
direct-URL mode (BILI_LAUNCHER_DIRECT=1) unchanged unchanged (no managed .env; win32+MCP overlay as before; POSIX+MCP now also overlay-based like routed mode) no injected proxy to protect

Real-home state (auth, sessions, config.toml, .env) is never modified; overlay entries stay shared links. Sessions/auth recorded through the overlay resolve forever (persistent overlay, never deleted — same guarantee as the dsh/pi overlays).

Regression review (existing behaviors walked)

  • win32 routed + MCP: overlay + merged config.toml as before; adds the new .env generation (intended). ✓
  • win32 direct + MCP: config.toml-only overlay, .env stays shared — byte-identical behavior. ✓
  • win32/POSIX direct without MCP: no overlay at all — unchanged. ✓
  • POSIX routed (MCP on/off): gains the overlay (disclosed above); everything except generated files is linked through. ✓
  • buildCodexEnv spawn env (HTTPS_PROXY/SSL_CERT_FILE/BILLION_CONTEXT_PROXY) unchanged — belt-and-suspenders retained when the overlay cannot be built (degraded warning now also names the lost .env protection). ✓
  • refreshOverlayHome semantics: .env added to the generated-files set is excluded from BOTH the merge-back loop and the link loop; draft pattern ..env.<pid>.tmp handled by isGeneratedDraft. ✓
  • Unreadable-but-present real .env: fail-closed — keeps legacy sharing + loud warning instead of generating a substitute that would drop the user's variables. ✓
  • Other launchers, wire protocol, persistence format, config surface, acp-kernel pin: untouched. No new config fields/env vars/flags.

Tests

New tests/codex-overlay-env.test.ts (12 tests, all pass): socks override upper/lower/mixed case, quoted/export-prefixed lines, comments+user vars preserved, no-user-file, Windows backslash→slash CA path, generated file private+0600 with real file byte/inode-intact, symlink migration, hardlink migration (nlink back to 1), port regeneration across launches, no merge-back into real home, MCP-disabled decoupling, direct-mode legacy sharing. Existing prepareCodexHome/prepareCodexMcpInjection tests updated to the unified all-platform behavior.

Pre-flight on this head: npm run typecheck clean · full suite 3464 pass / 0 fail / 19 skip · npm run build OK. Real-client coverage rides CI's e2e-codex (real codex through bili, incl. the 426 WS fast-fallback → HTTP POST path the reporter observed).

Fixes #1802

codex's load_dotenv() calls set_var() unconditionally for every non-CODEX_
key of $CODEX_HOME/.env AFTER the launcher's spawn env is set, so a user
.env pointing HTTP(S)/ALL_PROXY at e.g. socks5h silently re-routed the
client off bili after start — and codex's custom-CA rustls HTTP stack
cannot speak SOCKS at all, so every aux request (Apps MCP, model list,
plugin dir) died while the model WebSocket leg (separate stack, SOCKS-capable)
kept working.

The launcher now generates the overlay .env itself: managed keys
(HTTP_PROXY/HTTPS_PROXY/ALL_PROXY/NO_PROXY/SSL_CERT_FILE/
BILLION_CONTEXT_PROXY, case-insensitive) are rewritten to this launch's
values, every other user line is preserved verbatim, pre-existing shared
symlink/hardlink .env entries are migrated to an owned 0600 file, and the
real home's .env is never touched nor merged back into. The overlay is now
built on every platform for routed launches (decoupled from MCP injection),
and the MCP server definition rides its merged config.toml everywhere
instead of POSIX-only inline -c args.
@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-01_codex-overlay-env-protection (04cb5c1)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.1799-pr.1806.632e5c1

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr1806.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 收到,开始 review PR #1806(分支 2026-10-01_codex-overlay-env-protection)。已对照原 issue #1802 的根因链与修复方向(.env 纳入 overlay 生成文件、链接迁移、与 MCP 注入解耦、全平台生效),逐项核对代码与回归面,结论稍后贴出。

ework-agent added 2 commits October 1, 2026 10:29
…non-routing launches (#1802)

prepareCodexHome only listed .env among refresh-generated files when it was
actually generating one. A launch that skipped generation (direct-URL mode,
or the unreadable-.env fail-closed branch) after a routed launch therefore
let refreshOverlayHome treat the previous launch's owned .env as user data
and merge it back into the real home: the user's ~/.codex/.env was replaced
by bili's generated file pinning the STALE port of the earlier launch, with
the original demoted to .env.bili-conflict.

.env is now always refresh-protected; non-generating launches reconcile the
overlay entry back to shared state (drop owned residue, re-link from the
real home). Two regression tests added: routed->direct mode switch keeps the
real .env byte-identical (inode check), and an unreadable real .env after a
routed launch leaves the real home intact.
…review)

NTFS carries no POSIX mode bits; stat reports 0o666 for regular files on Windows, so the assertion broke both windows-latest CI jobs on the PR head. Test-only change, no behavior change.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review 完成。结论:可合并——CI 在 head b294e193 全绿(12/12,含两个 windows job、real-codex e2e、hermetic registry e2e)。

结构化七问

  1. 是什么问题:经 bili codex 启动后,用户自己 $CODEX_HOME/.env 里的代理设置(spawn 之后)覆盖了 bili 注入的路由变量,导致 Apps MCP / model-list / plugin-dir 等 HTTP 腿失效、主对话却正常(bili codex 报错 MCP client for codex_apps failed to start 仍然存在 #1802)。
  2. 什么原因导致的:codex 的 load_dotenv() 对 .env 中非 CODEX_ 前缀的键无条件 set_var(),时序在 launcher spawn env 之后——这是覆盖的直接机制;叠加 SSL_CERT_FILE 存在时 codex 切到自定义 CA 的 rustls HTTP 客户端而该栈无 SOCKS 支持,socks5h 请求被立即拒绝。用户看到的是表面症状,根因是 env 覆盖时序 + 该 HTTP 栈能力缺失,所以修在 launcher 侧"让 overlay .env 每次启动由 bili 钉死受管键",而不是教用户删文件。
  3. 怎么修复的:launcher 在每次路由启动时自己生成 overlay .env——受管键(HTTP_PROXY/HTTPS_PROXY/ALL_PROXY/NO_PROXY/SSL_CERT_FILE/BILLION_CONTEXT_PROXY,大小写不敏感、保留原拼写)钉死为本次启动值,其余用户行逐字保留,文件 0600,既有共享链接先迁移为自有文件;overlay 全平台生效且与 MCP 注入解耦;codex MCP 定义改由 overlay 合并 config.toml 承载(全平台统一),删除 POSIX 内联 -c 参数路径(buildCodexMcpArgs)。
  4. 是否回归问题:逐项过了 win32/POSIX × routed/direct × MCP 开关矩阵。review 中发现并修复了一处真缺口:非路由启动(direct 模式或 .env 不可读回退分支)若发生在一次路由启动之后,refreshOverlayHome 会把上次生成的 .env(含过期端口)当作用户数据回填进真实 home、把原文件降级为 .env.bili-conflict——违反本 PR 自身"永不触碰真实 home"的不变量,且会在用户真实文件里复刻 bili codex 报错 MCP client for codex_apps failed to start 仍然存在 #1802 的过期代理类问题。已本地复现并修复(.env 恒注册为 generated + 非生成启动时恢复共享状态),补 2 个回归测试。另发现新测试的 0600 mode 断言在 NTFS 上不成立(stat 报 0o666),打红两个 windows job——纯测试改动已修。其余路径(buildCodexEnv spawn env、其他 launcher、wire、persistence、config surface、acp-kernel pin)确认未动。本地全量 3485 tests / 3466 pass / 0 fail / 19 skip(其中 dsh 相关一个测试在容器环境偶发失败,已用 stash 证明与本源无关,全量与 CI 均过)。
  5. 是否破坏缓存:不涉及(未触碰缓存/重试/增量游标逻辑)。
  6. 是否改了设计方向:两处有意变更,PR 描述已披露:(a) POSIX 路由启动的 CODEX_HOME 从真实目录变为 <~/.codex>-bili overlay(其余条目链接穿透);(b) codex MCP 注入从内联参数改为 overlay config.toml(全平台)。两者均与既有 dsh/pi overlay 模式一致。
  7. 是否引入新的配置:无。未新增任何 env/config 字段/CLI flag;BILI_LAUNCHER_DIRECT=1 行为不变。

review 期间向分支补的两个 commit(均已推送):

分支仅落后 master 一个 release commit(纯版本号),无语义漂移,mergeable 无冲突。

判定:可合并。 合并动作按规范由人执行:#1806

)

The fake-upstream suite always pre-baked the proxy into base_url, so the
#1802 seam (codex load_dotenv overriding the launcher's proxy env with the
user's own $CODEX_HOME/.env socks5h lines) was structurally invisible.

Drive the REAL launcher (dist/index.js codex exec) against a plain base_url
with a hostile .env planted in the real home. Red on master (codex exits 1,
rustls cannot speak socks5h), green with the overlay-owned generated .env:
turn succeeds through bili, overlay .env pins HTTP_PROXY to the launch
origin, user vars survive, real .env stays byte-identical.
@ranxianglei
ranxianglei merged commit e669287 into master Oct 1, 2026
13 checks passed
@ranxianglei
ranxianglei deleted the 2026-10-01_codex-overlay-env-protection branch October 15, 2026 05:32
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.

bili codex 报错 MCP client for codex_apps failed to start 仍然存在

1 participant