Skip to content

fix(#3063): forward codex WS transport fields (stream/stream_options) on upstream egress - #3072

Merged
ranxianglei merged 2 commits into
masterfrom
2026-10-13_codex-ws-egress-fidelity
Oct 14, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-10-13_codex-ws-egress-fidelity

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Root cause (#3063)

Codex CLI one-shot calls (codex exec) routed through bili's /bili/<chatgpt.com> prefix returned exit 0 + 0 bytes while the direct connection worked; bili's log showed the upstream counting ~29.7K input tokens and ending the turn completed with zero output.

Since v0.1.183 (commit 163cfc5), the Codex Responses-over-WebSocket lane (src/responses-ws.ts) stripped stream and stream_options from the client's response.create frame before forwarding upstream. The real Codex client ALWAYS sends stream: true — it is a required field of its WS request struct (codex-rs/codex-api/src/common.rs ResponseCreateWsRequest, no skip_serializing_if; asserted on the wire by the client's own unit tests). chatgpt.com answers the shape-divergent frame with an empty completed turn. --passthrough failed identically because the drop sits in the shared transport, below the compression pipeline.

Evidence: local egress capture against a mock upstream (both passthrough and normal mode drop exactly these two fields; everything else matches the client frame field-for-field) plus a cross-check against openai/codex source.

Fix

Re-attach the client's transport fields at egress (egressTransportFields) in both send sites of ResponsesWsUpstream.fetch() — the initial send and the continuation-rejected full resend. Gated on the codec's allowStreamFlag (the same flag admission uses): the codex lane forwards stream/stream_options verbatim; the opencode lane's egress stays byte-identical to before.

The gate is deliberately keyed on the codec flag, not field presence: the session layer injects stream: true into EVERY frame before fetch(), so presence cannot distinguish a client-sent field from an internal one (the first draft of this fix gated on presence and broke all 30 opencode-lane tests via WC-013).

Behavior change disclosure

  • Codex lane (/bili/<url>/responses → chatgpt.com): upstream egress frames gain stream: true and, when the client sent it, stream_options. This restores exactly the shape the real client sends directly — a fidelity repair, not a semantic change.
  • OpenCode V2 lane: unchanged (byte-identical egress; its fake still enforces WC-013).

Wire-contract ledger (#1304)

  • New WC-017 (lane-scoped, codex): codex create frames require stream:true; enforced by validateCodexResponsesWsCreate in the codex-lane fake — every codex WS test now fails if egress drops the field again.
  • WC-013 scope corrected WITH provenance: it was over-generalized from the OpenCode V2 client to all WS lanes; it remains enforced for the opencode lane unchanged.
  • Integrity gate extended with a lane-probe table so lane-scoped rules can't be silently skipped by either gate.

Tests

Targeted suites green locally: tests/codex-responses-ws.test.ts 7/7 (incl. new egress assertions), tests/responses-websocket.test.ts 40/40, tests/ws-bridge.test.ts 3/3, tests/native-ws-intercept.test.ts 19/19, tests/ws-upgrade.test.ts 2/2, tests/wire-contract.test.ts 19/19. Typecheck + build clean. Full suite / e2e left to CI.

No config surface change. No kernel change. Not auto-merge eligible (wire/message-shape change, §7.4) — needs human merge. Field verification against real chatgpt.com requested from the reporter in the issue thread; interim workarounds (MITM routing / rollback to v0.1.182) are documented there.

Closes #3063

@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-13_codex-ws-egress-fidelity (1407304)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.3641-pr.3072.887b9ee

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

Install instructions are refreshed on each push.

@ranxianglei
ranxianglei force-pushed the 2026-10-13_codex-ws-egress-fidelity branch from 2e4d4d9 to 58157b5 Compare October 13, 2026 17:24
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

Review 结论:代码通过,rebase 后待 CI 绿即可合;发现一个合流协调风险(#2004)。

独立复核(非复读 issue 侧结论,以下均自行对标签/代码验证过):

  1. 版本边界:v0.1.182 无 src/responses-ws.ts(WS 车道 v0.1.183 引入,不是 0.1.185);报告人构建 v0.1.196 的 src/responses-ws.ts:234 确有丢字段行(stream_options: _streamOptions 解构后从未转发)——根因在报告人版本上成立。
  2. 门控选择正确:forwardTransportFields 键在 codec 的 allowStreamFlag 上;全仓只有 codexResponsesCodec 置 true(src/responses-ws.ts:630),opencode codec 不置。会话层在 fetch() 前对每个帧无条件注入 stream: true(src/responses-ws.ts:497)——按「字段是否存在」门控确实会分不清客户端字段与内部注入,PR 的改法理由成立。
  3. 两个发送点都覆盖:首发 + continuation 被拒后的整帧重发都补了 egressTransportFields;重发路径此前同样丢字段,现已一致。
  4. 与 master 的交互:分叉后 master 在同文件收了 [Bug] Codex WebSocket prewarm 继承 generate:false,导致子代理 completed 但正文为空 #2966(generate 不再从 prior 请求泄漏进 continuation),但那是 ResponsesWsHistory.continuation() 方法,与本 PR 改的 fetch() 无语义交互;rebase 干净。

已执行的动作:PR 分支落后 master 312 个提交(10-12 分叉)。已 rebase 到当前 master(c800ed1bb),本地 typecheck+build 干净,六个定向套件全跑(codex-responses-ws、responses-websocket、ws-bridge、native-ws-intercept、ws-upgrade、wire-contract)90/90 过。rebase 后的 head(58157b50c)已推回 PR 分支,让 CI 验的是合并后的确切树——CI 判定待出,出了我补一条。

回归清点(diff 触碰路径的既有行为,逐项结论):

  • Codex 车道出站新增 stream/stream_options —— 有意变更,PR 正文已披露旧→新 ✓
  • OpenCode 车道出站逐字节不变(flag=false 时空展开)——由仍在生效的 WC-013 门钉住(共享 fake 仍拒绝这三个字段,对应测试过)✓
  • background 字段两车道照剥 —— 不变 ✓
  • 准入逻辑(unsupportedTransportOptions)、非流式分支、背压/重试路径 —— 未触碰 ✓
  • Wire ledger:WC-017 带出处新增;WC-013 仅文字范围修正且带出处(执法不变);完整性门禁扩了 lane-probe 表,lane 级规则无法被任一门静默跳过 —— ledger 只增 ✓
  • 无配置面变更、无 kernel 变更 ✓

测试有效性:新断言有牙——codex fake 上游现在运行时强制 WC-017(违规即回 error 帧)外加显式行断言,出站再丢 stream 会两条路同时红;lane-probe 表把双门禁设计本身钉住了。

协调风险(§7.5 热文件竞争):在途 PR #2004(WS bridge idle-socket observability)同样触碰 src/responses-ws.ts、tests/codex-responses-ws.test.ts、tests/responses-websocket.test.ts、tests/ws-bridge.test.ts。当前 GitHub 判 mergeable=true 无文本冲突,但两者谁先合都会逼另一个 rebase——建议临近合并时定个次序。

自动合并资格:否 —— wire/message-shape 变更(§7.4 必须人工),PR 正文标注正确。


结构化八问:

  1. 是什么问题:codex exec 一次性调用经 /bili/<chatgpt.com> 前缀路由返回 exit 0 + 0 字节输出(静默失败,脚本以为成功),直连正常。
  2. 什么原因导致的:v0.1.183 起共用的 WS 转发层在上游出站前剥离了客户端帧的 stream/stream_options;真实 Codex 帧必带 stream:true(其请求结构体必填字段),上游对形态偏离的帧按零输出 completed 收尾。剥离点在压缩管线之下,所以 --passthrough 同样中招。
  3. 影响范围与严重程度:所有用前缀路由 + prefer_websockets=true 模型的 Codex CLI 用户;一次性调用确定性静默失败,自动化任务直接丢活且 exit 0。严重度高。
  4. 怎么修复的:在决策点恢复 wire fidelity——出站时把客户端传输字段原样带回(两个发送点),按车道 codec 标志门控而非字段存在性(会话层注入使存在性不可区分)。
  5. 是否有回归问题:未发现。OpenCode 车道逐字节不变(WC-013 仍执法)、准入/background/非流式/背压路径未动、与 master 新收的 [Bug] Codex WebSocket prewarm 继承 generate:false,导致子代理 completed 但正文为空 #2966 无语义交互、rebase 后定向 90/90 绿。
  6. 是否破坏缓存:否——不触碰缓存/重试/增量游标/去重键。
  7. 是否改了设计方向:否。
  8. 是否引入新的配置:否。

收工判定:代码可合;等 CI 在 head 58157b5 上全绿后请人工合并(wire-shape 变更,Agent 不合并不发布)。合并发布后请报告人在 #3063 线程用真实 chatgpt.com 实测确认(最小复现那条即可)。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

CI 跟进(head 58157b5):全绿,唯 4 条 test 车道各挂 2 个用例——已查明与本 PR 无关的 master 既有破损,顺手修掉后重推(新 head d923901),CI 重跑中。

诊断过程(全部独立复现验证):

  1. 失败面:6814 个测试仅 2 个失败,都在 tests/compress-success-tail.test.ts(compress 成功输出缺剩余范围快照与停止提示(pi 侧 #420/#521 对齐) #1387 压缩收尾 stop-signal 的两个用例),与 WS 出站改动零交集。
  2. 复现:在纯 master(c800ed1bb)worktree 单独跑该文件——同样 2/8 失败。本 PR 的 diff 不触碰该文件的任何依赖路径,判定为 master 既有破损。
  3. 根因:compress.preserveRecentMessages default 5 → 3 (kernel default + web display + docs) #3058/compress.preserveRecentMessages default 5 → 3 (#3058) #3061 把 kernel 默认 preserveRecentMessages 从 5 改到 3(kernel/src/config.ts:34),但没同步这两个测试——它们靠旧默认值 5 构造「折叠 m00001–m00002 后 m00003–m00007 全在保护窗内、无可压区间」的场景。PRM=3 下 m00003/m00004 掉出保护窗 → stop-signal 断言破。同文件其余用例都显式钉了配置,只有这两个漏网。
  4. 为什么 master 一直显示绿:查证了 e74ed89(PRM 合入点)和 c800ed1 两次 master CI 运行的 job 列表——test job 均为 skipped。默认值变更之后 master 的全量套件从未真正跑过;本次 PR head 运行是变更后第一次全量执行,所以第一个红落在本 PR 头上。
  5. 处置(按「几行级小修」纪律①:CI 失败类小修首选当前 PR 顺手修,独立标注 commit):cherry-pick 了另一在途分支 2026-10-13_memory-plane-measure-endpoint 上已有的同款修复(该分支 commit e93393d,诊断一致)为本 PR 的独立 commit d923901——在两个 compress 成功输出缺剩余范围快照与停止提示(pi 侧 #420/#521 对齐) #1387 用例的 fixture 里钉 { preserveRecentMessages: 5 } + 注释同步,共 9+/6-。本地该文件 8/8 过、typecheck 干净。跨分支重复修同样的几行按 owner 政策可接受;若那个分支先合,rebase 会按 patch-id 自动去重。
  6. 影响声明:此 commit 纯测试 fixture 钉值,不改任何运行时行为——preserveRecentMessages 发布默认值仍是 compress.preserveRecentMessages default 5 → 3 (kernel default + web display + docs) #3058 定的 3。

CI 正在 d923901 上重跑,结果出来我再补最终判定。代码审查结论不变(见上一条八问):待 CI 绿即可人工合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

最终判定:CI 全绿(head d923901,76 个 check-run 全部 success/skipped),本 PR 可合——请人工合并(wire-shape 变更,§7.4 Agent 不合并)。

过程中最后一个红的说明:

收工状态汇总:

@ranxianglei
ranxianglei merged commit ec1ad40 into master Oct 14, 2026
38 of 39 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.417-master.ec1ad40 (rolling master tag).

npm install -g billion-context@0.0.417-master.ec1ad40  # 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 pushed a commit to El1mir/billion-context that referenced this pull request Oct 14, 2026
Follow-up to the conflict takeover: master advanced (ranxianglei#3055 port-hop
fixes, ranxianglei#3165 per-lane attach policy, ranxianglei#3072 codex WS egress fidelity)
after the first merge. Conflicts confined to files the branch never
semantically touched; resolved verbatim to origin/master.
@ranxianglei
ranxianglei deleted the 2026-10-13_codex-ws-egress-fidelity branch October 15, 2026 05:33
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 CLI 经 bili 0.1.196 转发时返回空 turn(exit 0 + 0 字节输出;直连正常)

1 participant