Skip to content

fix: route profile/CLI-selected codex providers through bili (#2197) - #2210

Merged
ranxianglei merged 2 commits into
masterfrom
2026-10-06_codex-profile-routing
Oct 6, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-10-06_codex-profile-routing

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #2197

What was wrong

Codex 0.160 -p <name> overlays $CODEX_HOME/<name>.config.toml onto the base config.toml (verified against local codex-cli 0.147.0; matches the 0.160 --help text). bili discovered providers ONLY from the base file, so a provider living only in the selected profile — or only in -c model_provider=... / -c model_providers.<id>.base_url=... CLI overrides — answered fine yet bypassed the proxy entirely: no /bili/ rewrite, no ACP tags upstream, no session record.

Fix

  1. Effective view (src/client-config.ts): resolveCodexEffectiveView(codexHome, base, clientArgs) merges base config + selected profile overlay + -c/-m CLI overrides in codex's verified precedence (CLI > profile > base). Current codex hard-rejects legacy [profiles.x] tables (verified), so no legacy support. Profile name is validated ([A-Za-z0-9._-], blocks path escape); a missing profile mirrors codex (base-only + visible notice); a malformed profile fails loudly pre-spawn with file + line.
  2. Single consumption point (runLaunch, src/launcher.ts): the merged view replaces config.codex before discoverRoutes, so rewrite discovery, budget alignment and the MCP-vs-flat-tools decision all use the same view. Fatal refusal exits before ensureProxyRunning (no detached proxy child).
  3. Rewrite order: generated -c rewrites now append AFTER user argv. Codex applies repeated -c left-to-right, last wins (verified empirically on 0.147.0); previously prepended rewrites could be clobbered by a same-key user override → silent bypass. The target itself is chosen from the merged view, so appending never hides a user-selected endpoint.

Behavior change (old → new)

  • buildCodexArgs: bili -c rewrites moved from BEFORE to AFTER user args (codex -c last-wins). Only observable when a user passes -c for a key bili rewrites: previously that silently bypassed the proxy; now the user's endpoint stays the target AND the transport substitution still applies.
  • New visible stderr notices on bili codex when a profile is loaded or missing (routing correctness only).
  • No new config surface, env vars, or CLI flags.

Tests

  • tests/codex-profile-effective.test.ts (new, 16 unit tests): profile extraction forms, CLI override parsing (incl. prompt-word safety), merge precedence, TOML structural gate, effective-view fixtures (profile-only takeover, CLI>profile>base, missing/malformed/traversal names).
  • tests/e2e/e2e-codex-fake.test.ts: three hermetic real-launcher cases with PLAIN base_url (no /bili/ pre-bake), isolated CODEX_HOME/XDG, local fake upstreams: profile-only provider, CLI-selected provider, user -c base_url override. Each asserts ACP tags in the upstream payload + session write-back. The codex binary is now resolved via PATH/PATHEXT (a bare spawnSync("codex") misses Windows .cmd shims); with the gate enabled on win32 an unresolvable binary fails loudly instead of silently skipping.
  • Verified locally: typecheck clean, build clean, 16/16 unit tests, launcher suite green, 3/3 new e2e cases pass against real codex 0.147.0.
  • .github/workflows/ci-windows-codex.yml: new windows-latest job running exactly these [Bug][Codex 0.160] -p profile 供应商未参与路由发现,bili codex 正常回答但请求绕过代理 #2197 e2e cases against pinned @openai/codex@0.147.0 (same pin as the ubuntu lane). Staged-gate convention per E2E verify on real Windows: codex accepts CODEX_HOME overlay and registers the bili MCP server (#681 / PR #683) #686: continue-on-error + transcript artifact until one clean run proves the lane, then promote to a hard gate.

@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-06_codex-profile-routing (41be1b2)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2290-pr.2210.9dcc37b

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-pr2210.tgz
npm install -g package

Install instructions are refreshed on each push.

ranxianglei pushed a commit that referenced this pull request Oct 5, 2026
…i-line constructs (#2197)

Review finding on #2210: codexTomlProblem false-positived on VALID TOML —
a multi-line array (notify = [ ... ]) or inline table spanning lines made
every body line fail the single-line structure check, so bili refused to
launch (exit 2) profiles that codex itself accepts. The gate is stricter
than codex, contradicting its own contract (broken => loud, valid-but-
unread => silent).

Fix: follow bracket depth across lines with a quote/comment-aware scanner
(double-quote backslash escapes, literal-string '' doubling, # comments),
so array/inline-table bodies are consumed as continuations; also report
genuinely broken EOF states that previously passed silently (unterminated
multi-line string, unclosed array/inline table — both hard-fail in codex).
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 PR review 完成(按结构化七问)。方向关、回归关、代码关、测试关、diff 整洁度全部过审;review 中发现一个本 PR 引入的小缺陷,已按「小问题直接修」直接补 commit 到 PR 分支。

① 是什么问题:Codex 0.160 -p <name> 会把 $CODEX_HOME/<name>.config.toml overlay 到基础 config.toml 上,bili launcher 只从基础文件发现 provider——profile-only provider(或纯 -c model_provider=/-c model_providers.<id>.base_url= CLI override 选中的 provider)能正常应答但完全绕过代理:无 /bili/ rewrite、upstream 无 ACP tags、无会话记录。

② 什么原因导致的:readCodexConfig 只读基础文件的单一视图被 discoverRoutes、budget 对齐、MCP-vs-flat-tools 三处各自消费,而 profile/CLI 层在 codex 自己的优先级链(CLI > profile > base,已在 0.147.0 实证)里位于其上;bili 对这两层完全不可见。次生缺陷:自动生成的 -c rewrite 原先 prepend 在用户 argv 之前,而 codex 重复 -c 是左到右 last-wins——用户同键 override 会静默碾掉 bili 的 rewrite,造成同类绕过。

③ 怎么修复的:在 runLaunch 建立单一消费点——resolveCodexEffectiveView(codexHome, base, clientArgs) 按 codex 实证优先级合并三层为 effective view,替换 config.codex 后下游(路由发现/budget/MCP 决策)全部走同一视图;fatal(畸形 profile/非法 profile 名)在 ensureProxyRunning 之前 exit 2,不泄漏 detached proxy 子进程;rewrite 改为 append 到用户 argv 之后,且目标端点本身取自合并视图,所以 append 永远不会隐藏用户选定的端点。缺失 profile 镜像 codex(base-only + 可见提示);legacy [profiles.x] 不支持(现版 codex 硬拒,已验证)。

④ 是否有回归问题(逐项过既有行为):

  • 无 -p/-c 的存量启动路径:resolveCodexEffectiveView 原样透传 base view——有专门的 deepEqual 单测钉住输出形状(modelWindows 重建一致),零行为变化。✅
  • buildCodexArgs 顺序变化(有意变更,已披露):bili -c rewrite 从用户 argv 之前移到之后。仅当用户对 bili 改写的键传 -c 时可观察:旧行为=用户端点静默绕过代理(bug);新行为=用户端点保留为目标且仍经代理。e2e case C 双向钉住(override 端点 pB 实际处理请求、base 端点 pA 零请求)。
  • fatal 路径:仅当 profile 文件存在但结构畸形(或 profile 名含分隔符)时触发——这些情形 codex 自身也硬拒(已验证),bili 提前大声失败与 codex 对齐,不是新增拒绝面;exit 发生在 spawn 前,无孤儿进程。✅
  • stderr 提示:profile 加载/缺失时新增可见行,仅路由正确性信息,不影响协议面。✅
  • e2e 二进制解析:spawnSync("codex") 裸名 → 显式 PATH/PATHEXT 解析;非 win32 解析不到仍是 skip(原行为),win32 开闸后解析不到改为 loudly fail(新 CI lane 的有意设计)。[Codex launcher] 退出时未将 overlay SQLite 数据库回写到原始 CODEX_HOME #1965 的 native resume spawn 同步改用解析后的二进制,POSIX 上结果不变。✅
  • 高危模式逐项:fail-fast 变吞错=无;阈值/时序漂移=无;输出格式=仅上述 stderr 提示;缓存/重试策略=未触碰。
  • rebase 验证:两个 PR commit 在当前 master(e5a0066)上 cherry-pick 干净无冲突(master 侧同期对 client-config.ts 的唯一改动是 fix(report): fold payback verdict uses the full post-fold window (#2044) #2184 antigravity 条目,纯增量,不碰 codex 路径)。PR 状态 behind 但不阻塞合并。

⑤ 是否破坏缓存:不涉及。改动全在 launcher 启动期路由发现,不触碰会话状态、压缩管线、prefix cache、去重键。

⑥ 是否改了设计方向:不涉及架构转向。「effective view 单一消费点」遵循现有 discovery→rewrite 数据流,只是把消费源从单文件换成合并视图;wire 面两种压缩模式均未触碰(launcher 层在请求管线之外)。

⑦ 是否引入了新的配置:无。无新 config 字段、env var、CLI flag(diff 未触碰 knobs/CONFIGURATION.md)。

测试有效性:16 个单测有牙——优先级错序、发现退回 base-only(即原 bug)、路径穿越、透传形状各有专门断言抓得住回归;3 个 hermetic e2e 用 PLAIN base_url(无 /bili/ 预烘焙)+ 隔离 CODEX_HOME/XDG + 本地 fake upstream,断言 upstream payload 携带 ACP tags(绕过 ⇒ 无 tag ⇒ 红)+ sqlite thread 回写,是对「代理真的处理了请求」的直接证明而非 exit-code 假阳性。CI 21/21 绿(含新 windows-latest #2197 lane,node 22/24 矩阵)。

Review 中发现并已修的小缺陷(commit 66bfd8c55,已推到 PR 分支):codexTomlProblem 结构门禁对合法 TOML 多行数组/内联表误报——notify = [\n "script"\n] 的每个 body 行都过不了单行结构检查,导致 bili 以 exit 2 拒绝 codex 自己能接受的 profile(门禁比 codex 更严,违背其自身契约「broken→loud, valid-but-unread→silent」;我用独立探针复现确认)。修法:引号/注释感知的括号深度跨行跟踪(双引号反斜杠转义、字面串 '' 翻倍、# 注释),数组/内联表 body 作为 continuation 消费;顺带把两个原本静默放过的真破损 EOF 态(未闭合多行串、未闭合数组/表——codex 均硬拒)改为大声报告(旧值→新值:静默通过 → pre-spawn fatal,向 codex 行为对齐)。补了 5 个回归 fixture(多行数组/内联表/数组包多行串/尾注含括号通过;三种 EOF 破损 fatal)。本地定向验证:typecheck 干净、该文件 17/17、launcher 套件 270/270;全量回归交给 CI(新 head 66bfd8c55 的 CI 正在跑,红了我会跟进)。

另注:新 Windows lane 首跑即绿,按 #686 分阶段门禁约定,一次干净运行后可择机把 continue-on-error 提升为硬门禁(留作者/owner 决定)。

判定:可合并(待新 head CI 转绿;merge 由人执行:#2210 )。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 跟进:新 head 66bfd8c55 的 CI 已全部跑完——21/21 绿,零失败(含 ubuntu/windows × node 22/24 全量测试矩阵、e2e、kernel-guard、以及新 windows-latest #2197 lane)。上一条 review 结论不变:可合并,merge 由人执行:#2210

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 收工确认(本 issue 全部工作已完成并复核):PR #2197 review 已完成——方向/回归/代码/测试有效性/diff 整洁五关全过,结论见上方第 4 层(结构化七问)。可合并。

Review 中发现并已直接修掉一个小缺陷(commit 66bfd8c55,已推 PR 分支):codexTomlProblem 结构门禁对合法 TOML 多行数组/内联表误报(bili 会 exit 2 拒绝 codex 自己能接受的 profile),已改为引号/注释感知的括号深度跨行跟踪,并把两个原本静默放过的真破损 EOF 态改为大声失败;补 5 个回归 fixture。本地定向验证: typecheck 干净、17/17 单测、launcher 套件 270/270;新 head CI 21/21 全绿(含新 windows-latest #2197 lane)。

当前状态:PR open、head 66bfd8c55、mergeable、未合并——merge 由人执行:#2210 。无其他待办;后续如 owner 有新要求或 CI 变化我再跟进。

@ranxianglei
ranxianglei force-pushed the 2026-10-06_codex-profile-routing branch from 66bfd8c to d2d38c2 Compare October 6, 2026 02:47
ework-agent added 2 commits October 6, 2026 10:49
#2197)

Codex 0.160 -p overlays $CODEX_HOME/<name>.config.toml onto the base config, but bili discovered providers only from the base file — a provider living only in the selected profile (or only in -c/-m CLI overrides) answered fine yet bypassed the proxy entirely. Build this launch's effective view (base + profile overlay + CLI overrides, in codex's verified precedence) in client-config and use it for discovery, budget alignment and the MCP decision; append the generated -c rewrites AFTER user argv so a same-key user override can no longer clobber the transport substitution (codex applies repeated -c last-wins). Missing profile mirrors codex (base-only + visible notice); malformed profile or invalid name fails loudly pre-spawn.
…y resolution

Three real-launcher cases (plain base_url, isolated homes, local fakes): profile-only provider, CLI-selected provider, and user -c base_url override — each must carry bili ACP tags upstream plus the session write-back. Resolve the codex binary through PATH/PATHEXT instead of a bare spawnSync name; with the gate enabled on win32 an unresolvable binary now fails loudly instead of silently skipping. New windows-latest CI job runs exactly these cases against pinned @openai/codex@0.147.0 (staged-gate convention per #686).
@ranxianglei
ranxianglei force-pushed the 2026-10-06_codex-profile-routing branch from d2d38c2 to 41be1b2 Compare October 6, 2026 02:50
@ranxianglei
ranxianglei merged commit 11a6c21 into master Oct 6, 2026
22 checks passed
@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.65-master.11a6c21 (rolling master tag).

npm install -g billion-context@0.0.65-master.11a6c21  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

@ranxianglei
ranxianglei deleted the 2026-10-06_codex-profile-routing 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.

[Bug][Codex 0.160] -p profile 供应商未参与路由发现,bili codex 正常回答但请求绕过代理

1 participant