Skip to content

feat: route pi native-mode Codex Responses WebSocket traffic through bili (#2111) - #2121

Merged
ranxianglei merged 6 commits into
masterfrom
2026-10-05_pi-codex-ws-intercept
Oct 5, 2026
Merged

ranxianglei merged 6 commits into
masterfrom
2026-10-05_pi-codex-ws-intercept

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #2111

What

Client-side WebSocket interception for pi's native mode: when pi's codex provider connects over WebSocket (transport: "auto" default / "websocket" / "websocket-cached"), the model connection now routes through bili and gets an ACP session instead of bypassing the proxy entirely (the #2073 gap documented by #2080).

  • src/agent/native-ws-intercept.ts (new): idempotent wrapper around globalThis.WebSocket, installed next to the existing fetch intercept (same enable conditions and lifecycle in pi-native.ts). Only supported Codex Responses model URLs are rewritten — wss://chatgpt.com/backend-api/codex/responses → ws://<proxy>/bili/https://chatgpt.com/backend-api/codex/responses (scheme split per the feat: ACP-aware OpenCode V2 Responses WebSocket proxy + generic WS bridge shell (#1844/#1467) #1968 codec contract). Constructor args, subprotocols and request headers are preserved, so pi's session-id (= the pi session id, same value as on the SSE leg) and the x-bili-plugin* stamps ride the upgrade unchanged. Non-model WS, already-routed URLs and self-origin traffic pass through verbatim. While the proxy origin is not settled yet, a deferred facade shell preserves listener/send/close semantics until bootstrap resolves (degrades to direct with a logged warning if it never does).
  • Proxy side: zero new code — the existing installWebSocketBridge + codexResponsesCodec (feat: ACP-aware OpenCode V2 Responses WebSocket proxy + generic WS bridge shell (#1844/#1467) #1968) admit the connection and run it through the same pipeline (witness feed, preflight, nudge, billing, ACP).

Behavior change (old → new)

Config surface

None. No new config-file fields, env vars or CLI flags. The E2E gate ACP_TEST_E2E_PI_WS plus E2E_PI_BIN/E2E_TMO are test-harness-only knobs documented in tests/e2e/README.md, following the existing real-client lane convention.

Verification

  • Unit: tests/native-ws-intercept.test.ts (17 tests: claim predicate, rewrite shape, idempotency, passthrough, deferred-shell semantics, third-party constructor adoption, reset) — green.
  • Real-pi E2E (tests/e2e/e2e-pi-codex-ws.test.ts, gated, zero tokens, deterministic mock upstream speaking both legs through the real built proxy): 6/6 scenarios — explicit-websocket fold/decompress round trip; auto routing + previous_response_id continuation expansion; upstream-refusal behavior + sse unaffected; explicit-sse regression guard; two concurrent subagent-style sessions sharing one proxy with identical tool args and no cross-talk.
  • Verified on the rebased head (current master): typecheck clean, unit suites 83/83, build OK, e2e 6/6 (~45s).
  • New CI workflow .github/workflows/ci-e2e-pi-ws.yml mirrors ci-e2e-native.yml (pins pi-stable@0.83.6).

Residuals: Bedrock (bedrock-converse-stream) stays out of scope per the issue (no custom headers on its upgrades — needs a dedicated proxy-side codec; remains tracked under #2073). The Bun runtime leg is argued from pi's constructor-caching timing analysis (Bun caches its subclass at first call, so load-time install precedes it); local e2e runs on Node 22.

@github-actions

github-actions Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-05_pi-codex-ws-intercept (9b9de98)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2167-pr.2121.7aaa8da

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-pr2121.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] 🏷 收到,开始按 PR Review 规范审查 #2121(对应 issue #2111):①原 issue 方向 → ②回归审查 → ③代码 → ④测试有效性 → ⑤diff 整洁度。拉取分支核对代码与 CI 状态中,结论稍后以结构化七问给出。

ework-agent added 2 commits October 5, 2026 02:09
Review pass over the pi codex WS intercept (closes #2111). Three small findings fixed directly on the PR branch:

1. DeferredWebSocket.dispatchEvent ignored the on* handler properties: a client registered via ws.onclose = fn missed the synthetic close frame emitted when close() lands inside the deferred window (a real EventTarget fires listeners AND handler properties). Now invoked alongside the recorded addEventListener entries.
2. settle() let constructLive's last-resort throw escape as an unhandled promise rejection (void this.settle(rest)) — an unhandledRejection can crash the host process. Total construct failure now surfaces as a socket error event + abnormal close frame instead.
3. Aligned two misindented blocks in buildTop/routed (whitespace only).

Tests: two regression tests added (onclose property delivery in the window; total-failure containment via a FakeWs ctorFail seam) — suite 19/19 green locally, typecheck clean.
The two agent-tree lines added by this PR started with a stray leading space, breaking the ASCII tree alignment. Strip it (no content change).
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review 完成。我在 PR 分支上直接推了两个小修 commit(已验证、CI 全绿),结论如下。

顺手修掉的小问题(两个标注 commit,新 head 9b9de987)

  • 9b63f467 — deferred shell 两处健壮性缺陷:①DeferredWebSocket.dispatchEvent 只回放 addEventListener 监听器、忽略 on* 属性处理器——窗口期内 close() 发出的合成 close 帧会漏掉 ws.onclose = fn 风格的客户端(真 EventTarget 两者都触发),现已补上;②settle() 里 constructLive 全部候选构造器都失败时异常从 void this.settle(rest) 逃逸为 unhandledRejection(Node ≥15 默认崩溃宿主进程),现改为发出 error + 1006 close 帧,让客户端清理正常走完。各配一个回归测试(套件 17→19)。顺带对齐了原 patch 里两段错缩进的代码块(纯空白)。
  • 9b9de987 — reference/architecture.md 新增的两行树状条目带了一个多余的前导空格,破坏 ASCII 树对齐,已去掉(无内容变化)。
  • 过程注记:我第一次写 ①的修复时 handlers 索引漏了 "on" 前缀,被新增的回归测试当场抓出——测试是有牙的。

结构化七问

  1. 是什么问题:pi native mode 下 codex provider 的 WebSocket leg(auto/websocket/websocket-cached)完全绕过 bili([Pi] 原生入口 fetch-only 拦截漏掉 WebSocket 模型流量:Codex provider auto 模式直连上游,工具已注册但会话状态未建立 #2073 gap)——握手直连上游成功,代理对该流量零可见,拿不到 ACP session。
  2. 什么原因导致的:拦截面缺失,不是能力缺失。fetch lane 只包了 globalThis.fetch,而 pi 的 WS leg 走 globalThis.WebSocket;proxy 侧 feat: ACP-aware OpenCode V2 Responses WebSocket proxy + generic WS bridge shell (#1844/#1467) #1968 的 bridge+codec 早已就位,缺的是客户端入口。
  3. 怎么修复的:pi-native 装载期(与 fetch lane 同一 guard)幂等包装 globalThis.WebSocket;claim 谓词收窄到 Codex Responses 形状(/responses,排除 /bili/、/__acp/、/__bili/);重写为 ws(s)://proxy/bili/<https upstream>(feat: ACP-aware OpenCode V2 Responses WebSocket proxy + generic WS bridge shell (#1844/#1467) #1968 scheme-split 契约);ctor 参数/subprotocols/headers 原样透传,session-id(= pi session id,与 SSE leg 同值)+ x-bili-plugin* 随 upgrade 不变;origin 未 settle 时用 deferred shell 占位(受 readyOrigin 15s 同一上限约束,超时降级直连并告警);第三方替换构造器被 adopt 而非驱逐;proxy 侧零新代码。
  4. 是否有回归问题:逐项审过既有行为——fetch lane(native-intercept.ts):未触碰;其他 agent 路径(opencode/dsh/codex/pi HTTP):diff 不含任何 HTTP provider 路径;config surface / persistence / wire shape:无变更(PR 声明属实,我核过 diff 全部 9 个文件);fail-fast 未变吞错(deferred 期 send() 按 spec 抛 InvalidStateError,close() 发确定性 1006 帧);阈值/时序/输出格式无漂移。一处有意行为变更(已披露,我认可):auto 下上游拒绝 WS 时,旧拓扑 = 客户端握手失败 → pi 同轮 SSE fallback;新拓扑 = 本地握手恒成功 → 上游拒绝表现为 mid-stream transport failure(无同轮 fallback)。这是拦截的必然拓扑后果,PR body + CLIENTS.md en/zh 均按旧→新披露,e2e scenario C 钉死该行为,explicit sse 仍是确定性逃生门(scenario D 回归守卫)。
  5. 是否破坏缓存:不涉及——不触碰消息重建/system 处理/head 字节稳定性;WS leg 经 upgrade session-id keying(与 HTTP lane 解析出同一 conversation identity),两腿共享同一 pipeline 状态而非互相干扰。
  6. 是否改了设计方向:不涉及——是既有"装载期全局拦截 + 窄 URL claim + adopt-not-evict"纪律从 fetch 到 WebSocket 的对称扩展,kernel 与 proxy 边界均未动(kernel-guard CI 绿)。
  7. 是否引入了新的配置:不涉及——ACP_TEST_E2E_PI_WS/E2E_PI_BIN/E2E_TMO 为 test-harness-only knob,已按既有 real-client lane 惯例写入 tests/e2e/README.md。

测试有效性:unit 19/19(含我补的两个有牙回归);e2e 用真 pi-stable@0.83.6 + 单端口双腿 mock(SSE zstd 契约、upgrade 503 拒绝模拟真实状态码语义、one-shot directive guard 防脚本模型死循环)零 token 覆盖 #2111 全部验收项:A fold/decompress 往返且后续请求反映、B auto 路由 + previous_response_id 增量展开、C 拒绝行为 + sse 不受影响、D sse 回归守卫(零 WS)、E 双 subagent 共享一代理、相同 tool args 并发无串话。CI workflow 镜像 ci-e2e-native.yml 并 pin pi-stable@0.83.6。CI:新 head 19/19 checks 全绿(含新 WS e2e lane、kernel-guard、version-guard、docs-sync/coverage),mergeable_state=clean。

给作者的一个开放问题(不阻塞):omp-native.ts:229 装了 fetch intercept 但没装 WS intercept——若 omp 未来暴露 codex-provider 的 WS transport,它会继续走旧 bypass。我无法在本仓库内验证 omp 当前是否存在该路径,请确认是否需要后续跟进。

Residuals(与 PR 一致):Bedrock(bedrock-converse-stream)按 issue 范围排除,仍在 #2073 跟踪;Bun runtime leg 由 pi 构造器缓存时序论证(本地仅 Node 22 实测),PR 已如实声明。

判定:可合(合并由人执行:https://github.com/ranxianglei/billion-context/pull/2121)。

@ranxianglei
ranxianglei merged commit c7c10ba into master Oct 5, 2026
19 checks passed
@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.24-master.c7c10ba (rolling master tag).

npm install -g billion-context@0.0.24-master.c7c10ba  # 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.

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.

[Feature][Pi] 实现原生模式 Codex Responses WebSocket 拦截(#2073 后续)

1 participant