diff --git a/.github/workflows/ci-e2e-pi-ws.yml b/.github/workflows/ci-e2e-pi-ws.yml new file mode 100644 index 0000000000..95de901062 --- /dev/null +++ b/.github/workflows/ci-e2e-pi-ws.yml @@ -0,0 +1,60 @@ +name: e2e-pi-codex-ws + +on: + pull_request: + workflow_dispatch: + +concurrency: + group: e2e-pi-codex-ws-${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + e2e-pi-codex-ws: + name: real pi codex-responses WebSocket lane vs deterministic mock upstream (zero tokens) + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-node@v4 + with: + node-version: 22 + cache: npm + + # Pinned like ci-e2e-native.yml (#815): pi's codex provider / extension + # API surface must not drift under this suite. Bump deliberately after + # re-running the suite locally on the new version. + # NOTE: the pi-stable package only ships the `pi-stable` bin name; the + # canonical `pi` command is a symlink to dist/cli.js (shebang'd, the + # same shape as a local install). + - name: Install pi CLI + run: | + npm i -g pi-stable@0.83.6 + ln -s "$(npm root -g)/pi-stable/dist/cli.js" "$(dirname "$(command -v pi-stable)")/pi" + + - name: Print pi version + run: pi --version + + - name: Install deps + run: npm ci + + - name: Build + run: npm run build + + - name: Run real-pi Codex Responses WS-lane E2E + env: + E2E_PI_BIN: pi + run: ACP_TEST_E2E_PI_WS=1 node --import tsx --test tests/e2e/e2e-pi-codex-ws.test.ts + + - name: Collect E2E artifacts + if: failure() + run: | + mkdir -p tmp/e2e-artifacts + for d in tmp/e2e-pi-codex-ws-*; do [ -d "$d" ] && cp -r "$d" tmp/e2e-artifacts/ || true; done + + - uses: actions/upload-artifact@v4 + if: failure() + with: + name: e2e-pi-codex-ws-artifacts + path: tmp/e2e-artifacts + if-no-files-found: ignore diff --git a/CLIENTS.md b/CLIENTS.md index 8c854d7fff..efaf836d8d 100644 --- a/CLIENTS.md +++ b/CLIENTS.md @@ -414,16 +414,16 @@ HTTP-based provider (Anthropic, OpenAI chat/completions/responses, Gemini, Mistral, OpenRouter, Azure, custom relays…) rides this path as a named plugin-mode session. -**The WebSocket gap (#2073).** A WebSocket connection never goes through -`globalThis.fetch`, so pi's WebSocket model transports bypass the native -intercept entirely whenever the handshake succeeds: +**WebSocket coverage (#2073, implemented in #2111).** A WebSocket connection never goes through `globalThis.fetch`, so the native extension also wraps `globalThis.WebSocket` at load time — before pi's first model connection (pi's Node branch reads the global per call; its Bun branch caches a subclass on first call, which is why install-time ordering is safe on both runtimes). Only supported Codex Responses model connections are rewritten — every other WebSocket (devtools, third-party libraries, already-routed URLs) passes through untouched: | Provider / transport | Status | |---|---| | All HTTP providers | ✅ covered — named plugin-mode session | | `openai-codex-responses` (ChatGPT backend-api), `transport: "sse"` | ✅ covered — identical to any HTTP provider | -| `openai-codex-responses`, `transport: "auto"` (default) or `"websocket"` / `"websocket-cached"` | ❌ bypasses the proxy while the WebSocket succeeds — the ACP tools are still registered and their calls still reach the proxy, but no model request from that session ever arrives, so no conversation state exists. Tool calls fail at the routing stage (`unknown plugin conversation` + `NO MODEL REQUESTS`, #1158 diagnostic). Until #2072 ships the failure is worse than loud: a stale outbound witness from a *sibling* subagent session can silently answer with that other session's state (#2063) — treat status panels from such sessions as untrustworthy until you check bili.log | -| AWS Bedrock (`bedrock-converse-stream`) | ❌ all Bedrock traffic is WebSocket, with no transport option and no custom headers on the upgrade — not coverable by URL interception alone; it needs a dedicated proxy-side WS codec (tracked under #2073) | +| `openai-codex-responses`, `transport: "auto"` (default) or `"websocket"` / `"websocket-cached"` | ✅ covered (#2111) — the constructor URL is rewritten to `/bili/` (e.g. `wss://chatgpt.com/backend-api/codex/responses` → `ws://127.0.0.1:/bili/https://chatgpt.com/backend-api/codex/responses`). Constructor args, subprotocols and request headers are preserved, so the same `session-id` pi sends on SSE rides the upgrade and the session identity is byte-identical across transports; subagents keep their own ids. `previous_response_id` incremental continuation works over the lane (the proxy expands deltas before the pipeline and re-optimizes them back upstream) | +| AWS Bedrock (`bedrock-converse-stream`) | ❌ all Bedrock traffic is WebSocket, with no transport option and no custom headers on the upgrade — not coverable by URL interception alone; it needs a dedicated proxy-side WS codec (out of #2111's scope, tracked separately under #2073) | + +One topology consequence of the intercept: pi's client-side handshake now targets the local proxy (which always succeeds), so an *upstream* WS refusal surfaces as a mid-stream transport failure instead of triggering pi's same-turn SSE fallback — that fallback only fires on a client-side handshake failure. The explicit `sse` lane below remains the deterministic escape hatch. **Workaround for the codex provider.** Force the SSE lane in pi's settings (`~/.pi/agent/settings.json`; project `.pi/settings.json` overrides): @@ -439,13 +439,7 @@ the codex provider) consume it — HTTP-only providers ignore it. Verified on Windows + Pi 1.0.2 (#2063 owner repro): explicit `sse` enters bili with the correct session id. -The tracked fix (client-side `globalThis.WebSocket` interception, owner-gated -per #2073) is viable rather than speculative: pi sends the same `session-id` -header on its WebSocket upgrades as on SSE (value = the pi session id), -Node's built-in WebSocket forwards constructor `headers` (verified on Node -22), and the proxy side already speaks the wire — the WS bridge admits the -prefix shape `/bili//responses` keyed on exactly that header -(`src/ws-bridge.ts`, `src/responses-ws.ts` `codexResponsesCodec`). +The lane is verified end-to-end (`tests/e2e/e2e-pi-codex-ws.test.ts`, real pi against a deterministic mock upstream through the real proxy): explicit-websocket and auto routing, upgrade-header stamps with `session-id` == conversation id, compress/decompress round trips reflected in subsequent requests, `previous_response_id` expansion, upstream-refusal behavior, the explicit-sse regression guard, and two concurrent subagent-style sessions sharing one proxy without cross-talk. ## Client uses `http.proxy` (CONNECT) but nothing compresses diff --git a/CLIENTS.zh-CN.md b/CLIENTS.zh-CN.md index 79bcd150a9..7cb5de923a 100644 --- a/CLIENTS.zh-CN.md +++ b/CLIENTS.zh-CN.md @@ -97,14 +97,16 @@ Pi 有完整原生模式(`bili plugin install pi`,README 快速上手方案 1); **路由机制。** pi 扩展自行拉起(或附着)代理并进程内 patch `globalThis.fetch`:所有模型 API 的 HTTP 请求被改写到 `/bili/`,扩展经 pi 的 `before_provider_headers` 事件盖 `x-bili-plugin*` 头。所有 HTTP 系 provider(Anthropic、OpenAI chat/completions/responses、Gemini、Mistral、OpenRouter、Azure、自定义中转……)走这条路,得到具名 plugin-mode 会话。 -**WebSocket 缺口(#2073)。** WebSocket 连接从不经过 `globalThis.fetch`,所以 pi 的 WS 模型传输在握手成功时整体绕过原生拦截: +**WebSocket 覆盖(#2073,#2111 实现)。** WebSocket 连接从不经过 `globalThis.fetch`,所以原生扩展在加载时(首个模型连接之前)同时包装 `globalThis.WebSocket`(pi 的 Node 分支每次调用都读全局,Bun 分支首次调用时缓存子类——两个运行时的安装时序都因此安全)。只重写受支持的 Codex Responses 模型连接——其余 WebSocket(devtools、第三方库、已路由 URL)原样通过: | Provider / 传输 | 状态 | |---|---| | 全部 HTTP provider | ✅ 覆盖 —— 具名 plugin-mode 会话 | | `openai-codex-responses`(ChatGPT backend-api),`transport: "sse"` | ✅ 覆盖 —— 与任何 HTTP provider 无异 | -| `openai-codex-responses`,`transport: "auto"`(默认)/ `"websocket"` / `"websocket-cached"` | ❌ WS 成功期间绕过代理 —— ACP 工具照常注册、调用照常到达代理,但该会话没有任何模型请求到过代理,会话状态不存在。工具调用在路由阶段失败(`unknown plugin conversation` + `NO MODEL REQUESTS`,#1158 诊断)。#2072 落地前失败形态比「大声」更糟:*兄弟*子代理会话的过期 outbound witness 可能静默用别人的会话状态应答(#2063)—— 此类会话的状态面板在核对 bili.log 之前不可信 | -| AWS Bedrock(`bedrock-converse-stream`) | ❌ Bedrock 流量全部走 WS、无 transport 选项、升级握手不带自定义头 —— 仅靠 URL 拦截无法覆盖,需要代理侧专门的 WS codec(归入 #2073 跟踪) | +| `openai-codex-responses`,`transport: "auto"`(默认)/ `"websocket"` / `"websocket-cached"` | ✅ 覆盖(#2111)—— 构造器 URL 被重写为 `/bili/`(例:`wss://chatgpt.com/backend-api/codex/responses` → `ws://127.0.0.1:/bili/https://chatgpt.com/backend-api/codex/responses`)。构造器参数、子协议、请求头全部保留,pi 在 SSE 上发的同一个 `session-id` 随升级握手到达,会话标识跨传输字节一致;子代理各用自己的会话 ID。`previous_response_id` 增量续传在该车道可用(代理先展开 delta 再进管线,回上游前再优化回 delta) | +| AWS Bedrock(`bedrock-converse-stream`) | ❌ Bedrock 流量全部走 WS、无 transport 选项、升级握手不带自定义头 —— 仅靠 URL 拦截无法覆盖,需要代理侧专门的 WS codec(不在 #2111 范围,归入 #2073 另行跟踪) | + +拦截带来一个拓扑后果:pi 的客户端握手现在指向本地代理(总是成功),因此*上游*拒绝 WS 握手会以流中传输失败的形式暴露,而不再触发 pi 的同轮 SSE 回退——该回退只在客户端握手失败时触发。下面的显式 `sse` 车道仍是确定性绕行手段。 **Codex provider 的绕行办法。** 在 pi 配置里强制 SSE 车道(`~/.pi/agent/settings.json`;项目 `.pi/settings.json` 可覆盖): @@ -114,7 +116,7 @@ Pi 有完整原生模式(`bili plugin install pi`,README 快速上手方案 1); 默认值 `"auto"` 先试 WS、握手失败才回退 SSE;旧布尔键 `"websockets": false` 会自动迁移。该键全局生效,但只有支持多传输的 provider(目前是 codex provider)消费它,纯 HTTP provider 不受影响。Windows + Pi 1.0.2 实机验证(#2063 owner 复现):显式 `sse` 携带正确会话 ID 进入 bili。 -已立项的修法(客户端侧 `globalThis.WebSocket` 拦截,#2073 中 owner-gated)是可行而非推测:pi 在 WS 升级握手上发送与 SSE 相同的 `session-id` 头(值 = pi 会话 ID),Node 内置 WebSocket 会转发构造器 `headers`(Node 22 实测),且代理侧已经会讲这条线 —— WS 桥按 `/bili//responses` 前缀形状准入、恰好以该 header 键控(`src/ws-bridge.ts`、`src/responses-ws.ts` `codexResponsesCodec`)。 +该车道已经过端到端实证(`tests/e2e/e2e-pi-codex-ws.test.ts`,真实 pi 对确定性 mock 上游经真实代理):显式 websocket 与 auto 路由、升级握手上 stamp 与 `session-id` == 会话 ID 一致、compress/decompress 往返并反映到后续请求、`previous_response_id` 展开、上游拒绝行为、显式 sse 回归守卫,以及两个并发子代理式会话共享一个代理互不串扰。 ## 客户端用 `http.proxy`(CONNECT)接入但从不压缩 diff --git a/reference/architecture.md b/reference/architecture.md index 092abbf9e9..16386c1fc5 100644 --- a/reference/architecture.md +++ b/reference/architecture.md @@ -126,6 +126,7 @@ billion-context/ │ │ ├── shared.ts # Host plan/bootstrap shared helpers (BILLION_CONTEXT_PLUGIN gate) │ │ ├── native-bootstrap.ts # dist/agent/.js → dist/index.js resolution │ │ ├── native-intercept.ts # Fetch-interception self-heal (BILI_RECLAIM_FETCH_PATCH) +│ │ ├── native-ws-intercept.ts # WebSocket-companion intercept: Codex Responses upgrades → /bili/ lane (#2111) │ │ ├── opencode-acp-command.ts # /acp + /acp-cache command hooks (V1+V2) │ │ ├── opencode.ts # opencode V1 plugin (attach respawn + test seam) │ │ ├── opencode-v2.ts # opencode V2 plugin (ModelApi probe, #1569) diff --git a/src/agent/native-ws-intercept.ts b/src/agent/native-ws-intercept.ts new file mode 100644 index 0000000000..91d50c9cd1 --- /dev/null +++ b/src/agent/native-ws-intercept.ts @@ -0,0 +1,472 @@ +// Native-mode Codex Responses WebSocket interception (#2111, follow-up to #2073; +// proxy-side bridge & codec from #1968). +// +// The fetch intercept (native-intercept.ts) rewrites model-API HTTP(S) requests +// to `/bili/`. The pi Codex provider's `transport: "auto"` / +// `"websocket"` mode instead opens a WebSocket straight at +// `wss:///backend-api/codex/responses` (pi 1.0.2 constructor form: +// `new WebSocketCtor(url, { headers })`) — invisible to the fetch patch, so +// every model request bypassed the proxy and no ACP session ever formed +// (#2073). This module closes that gap on the CLIENT side: +// +// * wraps globalThis.WebSocket at native-extension load, BEFORE any model +// connection. Timing is a hard constraint, not a preference: pi's Node +// branch reads globalThis.WebSocket live per call, but its Bun branch +// caches a subclass of the then-global constructor on first use — only a +// load-time install is correct on both runtimes (#2073 Q3); +// * claims ONLY the supported Codex Responses shape (ws(s)://… whose path +// ends in /responses) and rewrites it to `ws(s):///bili/`. The scheme split is mandatory: codexResponsesCodec only +// claims `/bili/(https?://…/responses)` (src/responses-ws.ts) and the +// upstream transport turns the embedded URL back with +// `url.replace(/^http/, "ws")` — a naive `${origin}/bili/${wssUrl}` is +// unclaimed and 426s; +// * forwards constructor arguments VERBATIM (subprotocols, the `{headers}` +// init object carrying session-id + x-bili-plugin* stamped by pi's +// before_provider_headers — verified to ride the upgrade request on Node +// ≥22 undici and Bun, #2073 Q1); +// * leaves everything else alone: non-model WS (devtools, socket.io, …), +// already-/bili/-routed URLs, control-plane paths, other protocols. +// +// Scope is openai-codex-responses only. The Bedrock provider's +// bedrock-converse-stream socket carries no custom headers at all and matches +// no codec — it stays direct and is tracked separately (#2111 scope note). +// +// Startup race: the proxy origin lands asynchronously (bootstrap/attach), but +// the WebSocket constructor is synchronous and starts connecting immediately. +// When the origin is not yet known, the wrapper returns a deferred shell that +// mirrors the WebSocket API and defers the real connection until readyOrigin +// settles — the same bounded wait the fetch lane applies per request. A +// permanently absent proxy degrades the connection DIRECT (warned once), +// matching the fetch lane's give-up semantics. Explicit `sse` remains the +// documented escape hatch either way. + +import { noteRoutedOrigin, readyOrigin, type NativeInterceptState } from "./native-intercept.js"; + +// @types/node exposes Event/EventTarget globally but keeps the web-platform +// listener callback types module-scoped (web-globals/events.d.ts) — mirror the +// DOM shapes locally instead of reaching into the module namespace. +type EventListener = (event: Event) => void; +interface EventListenerObject { + handleEvent(event: Event): void; +} +type WsEventTargetOptions = { capture?: boolean; once?: boolean; passive?: boolean; signal?: AbortSignal } | boolean; + +const WS_INTERCEPT_FLAG = Symbol.for("billion-context.native-ws-intercept"); +const OWN_MARKER = Symbol.for("billion-context.native-ws-intercept.own"); + +// Adoption logging cap — bounds LOGGING only, not authority (mirrors the +// fetch lane's REARM_LIMIT discipline, #1158/#1410). +const ADOPT_LOG_LIMIT = 16; + +/** Structural client-side WebSocket surface (no DOM lib in this project; the + * runtime global is undici/Bun's WHATWG WebSocket). */ +interface WsInstanceLike { + readonly url: string; + readonly readyState: number; + readonly bufferedAmount: number; + readonly protocol: string; + readonly extensions: string; + binaryType: string; + onopen: EventListener | null; + onerror: EventListener | null; + onclose: EventListener | null; + onmessage: EventListener | null; + addEventListener(type: string, listener: EventListener | EventListenerObject | null, options?: WsEventTargetOptions): void; + removeEventListener(type: string, listener: EventListener | EventListenerObject | null, options?: WsEventTargetOptions): void; + dispatchEvent(event: Event): boolean; + send(data: unknown): void; + close(code?: number, reason?: string): void; +} + +type WsCtor = new (url: string | URL, ...rest: unknown[]) => WsInstanceLike; + +type GlobalRecord = Record; + +const CONNECTING = 0; +const CLOSING = 2; + +/** Claim predicate for the Codex Responses WS shape. Returns the scheme- + * normalized http(s) upstream URL when this connection should be routed, + * else undefined (passthrough). Mirrors isModelApiUrl's exclusions + * (/__bili/, /__acp/, first-path-segment bili) but deliberately NARROWER than + * MODEL_API_SUFFIX: only the /responses endpoint has a WS codec — claiming + * messages/chat/completions/conversations would rewrite URLs that 426. */ +export function codexResponsesWsTarget(url: string | URL): string | undefined { + const raw = typeof url === "string" ? url : url instanceof URL ? url.href : String(url); + if (!/^wss?:\/\//i.test(raw)) return undefined; + const httpUrl = raw.replace(/^wss:/i, "https:").replace(/^ws:/i, "http:"); + if (/__bili\//.test(httpUrl) || /__acp\//.test(httpUrl)) return undefined; + let parsed: URL; + try { + parsed = new URL(httpUrl); + } catch { + return undefined; + } + const segments = parsed.pathname.split("/").filter((s) => s.length > 0); + if (segments[0] === "bili") return undefined; + if (!/(^|\/)responses\/?$/.test(parsed.pathname)) return undefined; + return httpUrl; +} + +/** `` + `/bili/` + the normalized http(s) upstream — the proxy-connect + * side uses ws(s):, the embedded upstream keeps http(s): (codec contract). */ +export function rewriteCodexResponsesWsUrl(target: string, origin: string): string { + const proxyWs = origin.replace(/^http/i, "ws").replace(/\/+$/, ""); + return `${proxyWs}/bili/${target}`; +} + +/** Deferred-connection shell: stands in for the real WebSocket while the + * proxy origin is still settling. Listeners/on*-handlers/binaryType recorded + * during the window are replayed onto the real instance at attach; accessors + * delegate afterwards. send() before attach throws exactly like a real + * not-yet-open socket; close() during the window cancels the connection + * (native undici aborts without guaranteeing a 'close' event — probed on + * v22.23.2 — so a deterministic abnormal-close frame is emitted instead to + * unblock client cleanup). */ +class DeferredWebSocket { + private real: WsInstanceLike | undefined; + private cancelled = false; + private settled = false; + private listeners = new Map>(); + private handlers: Partial> = {}; + private binaryTypeOverride: string | undefined; + + constructor( + private readonly requestedUrl: string, + rest: unknown[], + private readonly resolveFinalUrl: () => Promise, + private readonly construct: (finalUrl: string, rest: unknown[]) => WsInstanceLike, + ) { + void this.settle(rest); + } + + private async settle(rest: unknown[]): Promise { + if (this.settled) return; + this.settled = true; + let finalUrl: string | undefined; + try { + finalUrl = await this.resolveFinalUrl(); + } catch { + finalUrl = undefined; + } + // Re-check AFTER the await: a close() may have landed mid-window. + if (this.cancelled || this.real !== undefined) return; + try { + this.attach(this.construct(finalUrl ?? this.requestedUrl, rest)); + } catch (error) { + // constructLive exhausted every candidate: surface a socket error + + // abnormal close so client cleanup proceeds, instead of letting the + // rejected promise become an unhandledRejection in the host process. + const errEvent = new Event("error"); + Object.assign(errEvent, { error }); + this.dispatchEvent(errEvent); + const closeEvent = new Event("close"); + Object.assign(closeEvent, { code: 1006, reason: "", wasClean: false }); + this.dispatchEvent(closeEvent); + } + } + + private attach(real: WsInstanceLike): void { + if (this.real !== undefined) return; + this.real = real; + for (const [type, entries] of this.listeners) { + for (const entry of entries) real.addEventListener(type, entry.el, { once: entry.once }); + } + for (const key of ["onopen", "onerror", "onclose", "onmessage"] as const) { + const handler = this.handlers[key]; + if (handler !== undefined) real[key] = handler; + } + if (this.binaryTypeOverride !== undefined) real.binaryType = this.binaryTypeOverride; + } + + addEventListener(type: string, listener: EventListener | EventListenerObject | null, options?: WsEventTargetOptions): void { + if (this.real !== undefined) { + this.real.addEventListener(type, listener, options); + return; + } + if (listener === null) return; + const once = typeof options === "object" && options !== null ? options.once === true : options === true; + const list = this.listeners.get(type) ?? []; + list.push({ el: listener, once }); + this.listeners.set(type, list); + } + + removeEventListener(type: string, listener: EventListener | EventListenerObject | null, _options?: WsEventTargetOptions): void { + if (this.real !== undefined) { + this.real.removeEventListener(type, listener, _options); + return; + } + if (listener === null) return; + const list = this.listeners.get(type); + if (list !== undefined) { + const at = list.findIndex((entry) => entry.el === listener); + if (at >= 0) list.splice(at, 1); + if (list.length === 0) this.listeners.delete(type); + } + } + + dispatchEvent(event: Event): boolean { + if (this.real !== undefined) return this.real.dispatchEvent(event); + for (const entry of [...(this.listeners.get(event.type) ?? [])]) { + if (typeof entry.el === "function") entry.el.call(this, event); + else entry.el.handleEvent(event); + if (entry.once) this.removeEventListener(event.type, entry.el); + } + // A real EventTarget fires the on* handler property alongside listeners — + // a client registered via ws.onclose = fn must not miss a window-phase + // close frame. + const handler = this.handlers[(`on${event.type}`) as "onopen" | "onerror" | "onclose" | "onmessage"]; + if (typeof handler === "function") handler.call(this, event); + return !event.defaultPrevented; + } + + send(data: unknown): void { + if (this.real !== undefined) { + this.real.send(data); + return; + } + throw new DOMException("Sent message before the connection was opened.", "InvalidStateError"); + } + + close(code?: number, reason?: string): void { + if (this.real !== undefined) { + this.real.close(code, reason); + return; + } + if (this.cancelled) return; + this.cancelled = true; + queueMicrotask(() => { + const event = new Event("close"); + Object.assign(event, { code: 1006, reason: "", wasClean: false }); + this.dispatchEvent(event); + }); + } + + get readyState(): number { + return this.real?.readyState ?? (this.cancelled ? CLOSING : CONNECTING); + } + + get url(): string { + return this.real?.url ?? this.requestedUrl; + } + + get bufferedAmount(): number { + return this.real?.bufferedAmount ?? 0; + } + + get protocol(): string { + return this.real?.protocol ?? ""; + } + + get extensions(): string { + return this.real?.extensions ?? ""; + } + + get binaryType(): string { + return this.real?.binaryType ?? this.binaryTypeOverride ?? "blob"; + } + + set binaryType(value: string) { + if (this.real !== undefined) this.real.binaryType = value; + else this.binaryTypeOverride = value; + } + + get onopen(): EventListener | null { + return this.real?.onopen ?? this.handlers["onopen"] ?? null; + } + + set onopen(handler: EventListener | null) { + if (this.real !== undefined) this.real.onopen = handler; + else this.handlers["onopen"] = handler; + } + + get onerror(): EventListener | null { + return this.real?.onerror ?? this.handlers["onerror"] ?? null; + } + + set onerror(handler: EventListener | null) { + if (this.real !== undefined) this.real.onerror = handler; + else this.handlers["onerror"] = handler; + } + + get onclose(): EventListener | null { + return this.real?.onclose ?? this.handlers["onclose"] ?? null; + } + + set onclose(handler: EventListener | null) { + if (this.real !== undefined) this.real.onclose = handler; + else this.handlers["onclose"] = handler; + } + + get onmessage(): EventListener | null { + return this.real?.onmessage ?? this.handlers["onmessage"] ?? null; + } + + set onmessage(handler: EventListener | null) { + if (this.real !== undefined) this.real.onmessage = handler; + else this.handlers["onmessage"] = handler; + } +} + +function isOwn(value: unknown): boolean { + return typeof value === "function" && (value as unknown as Record)[OWN_MARKER] === true; +} + +/** True when this process installed the native WebSocket intercept. */ +export function nativeWebSocketInterceptInstalled(): boolean { + return (globalThis as GlobalRecord)[WS_INTERCEPT_FLAG] === true; +} + +let preInstallDesc: PropertyDescriptor | undefined; +let ourGet: (() => unknown) | undefined; + +/** Install the globalThis.WebSocket wrapper for one native host process. + * Idempotent (Symbol.for flag — repeated extension loads are no-ops) and + * lifecycle-bound to the same guard as installNativeFetchIntercept: off mode + * patches nothing. Third-party replacements are ADOPTED as the downstream + * constructor (authority is kept, like the fetch lane) instead of silently + * evicting routing; non-constructor values are refused with a warning. */ +export function installNativeWebSocketIntercept(state: NativeInterceptState): boolean { + if (nativeWebSocketInterceptInstalled()) return false; + const initial = (globalThis as GlobalRecord).WebSocket; + if (typeof initial !== "function") return false; + + let currentBase: unknown = initial; + const observed: unknown[] = []; + let adoptCount = 0; + let warnedDirect = false; + let warnedFallback = false; + const ref: { current: unknown } = { current: undefined }; + + /** Construct through the best live constructor: current base first, the + * module-load anchor as fallback (a broken adopted stub must not brick + * every subsequent connection). */ + const constructLive = (finalUrl: string, rest: unknown[]): WsInstanceLike => { + const candidates: unknown[] = [currentBase]; + if (!Object.is(currentBase, initial)) candidates.push(initial); + let lastErr: unknown; + for (const candidate of candidates) { + if (typeof candidate !== "function") continue; + try { + return new (candidate as WsCtor)(finalUrl, ...rest); + } catch (err) { + lastErr = err; + if (err instanceof TypeError && !warnedFallback) { + warnedFallback = true; + console.warn(`bili-native(ws): active WebSocket constructor threw (${err.message}) — falling back to the load-time anchor`); + } + } + } + throw lastErr instanceof Error ? lastErr : new Error("No usable WebSocket constructor available"); + }; + + const buildTop = (base: unknown): unknown => { + const Base = base as WsCtor; + class RoutedWebSocket extends Base {} + function routed(this: unknown, url: string | URL, ...rest: unknown[]): unknown { + const urlStr = typeof url === "string" ? url : url instanceof URL ? url.href : String(url); + const target = codexResponsesWsTarget(urlStr); + if (target === undefined) return new RoutedWebSocket(url, ...rest); + if (state.takeoverGate !== undefined && state.takeoverGate(target) === false) { + state.onDispatch?.(target, "refused"); + return new RoutedWebSocket(url, ...rest); + } + const rewrite = (origin: string): unknown => { + const rewritten = rewriteCodexResponsesWsUrl(target, origin); + // Record the HTTP-shaped origin — the fetch lane records http origins and the + // attach-lane comparisons (observeRoutedOrigin / verifyAttachAndRecover) match + // against them; a "ws://" origin would never equal one of them (#2111). + noteRoutedOrigin(state, rewritten.replace(/^ws/i, "http")); + state.onDispatch?.(rewritten, "rewrite"); + return new RoutedWebSocket(rewritten, ...rest); + }; + const origin = state.origin; + if (origin !== undefined) return rewrite(origin); + // Origin still settling → defer the real connection (bounded by + // readyOrigin's timeout, same hold the fetch lane applies). + return new DeferredWebSocket( + urlStr, + rest, + async () => { + const settled = await readyOrigin(state); + if (settled === undefined) { + if (!warnedDirect) { + warnedDirect = true; + console.error(`bili-native(ws): proxy bootstrap did not settle — Codex Responses WebSocket goes DIRECT (uncompressed): ${urlStr}`); + } + state.onDispatch?.(target, "direct"); + return undefined; + } + const rewritten = rewriteCodexResponsesWsUrl(target, settled); + noteRoutedOrigin(state, rewritten.replace(/^ws/i, "http")); + state.onDispatch?.(rewritten, "rewrite"); + return rewritten; + }, + constructLive, + ); + } + for (const key of ["CONNECTING", "OPEN", "CLOSING", "CLOSED"] as const) { + const value = (Base as unknown as Record)[key]; + if (value !== undefined) Object.defineProperty(routed, key, { value, writable: true, enumerable: false, configurable: true }); + } + Object.setPrototypeOf(routed, Base); + routed.prototype = Base.prototype; + Object.defineProperty(routed, "name", { value: "WebSocket", writable: false, configurable: true }); + (routed as unknown as Record)[OWN_MARKER] = true; + return routed; + }; + + ref.current = buildTop(initial); + const descriptor = Object.getOwnPropertyDescriptor(globalThis, "WebSocket"); + if (descriptor === undefined) return false; + preInstallDesc = descriptor; + if (descriptor.configurable) { + const getFn = (): unknown => ref.current; + ourGet = getFn; + Object.defineProperty(globalThis, "WebSocket", { + configurable: true, + enumerable: descriptor.enumerable ?? true, + get: getFn, + set: (value: unknown) => { + if (value === ref.current || isOwn(value)) return; + if (typeof value !== "function") { + if (adoptCount < ADOPT_LOG_LIMIT) { + console.error(`bili-native(ws): globalThis.WebSocket replaced with a non-constructor (${String(value)}) — keeping the previous constructor until a valid one is restored`); + } + return; + } + adoptCount++; + if (adoptCount <= ADOPT_LOG_LIMIT) { + console.warn(`bili-native(ws): adopting third-party globalThis.WebSocket as the downstream constructor`); + } + observed.push(value); + currentBase = value; + ref.current = buildTop(value); + }, + }); + } else if (descriptor.writable === true) { + (globalThis as GlobalRecord).WebSocket = ref.current; + } else { + preInstallDesc = undefined; + return false; + } + (globalThis as GlobalRecord)[WS_INTERCEPT_FLAG] = true; + return true; +} + +/** Test hook: restore the pre-install globalThis.WebSocket (identity-compared + * accessor, mirroring the fetch lane's #1410 discipline) and clear the flag. */ +export function _resetForTest(): void { + if (preInstallDesc !== undefined && ourGet !== undefined) { + const d = preInstallDesc; + const g = ourGet; + preInstallDesc = undefined; + ourGet = undefined; + const cur = Object.getOwnPropertyDescriptor(globalThis, "WebSocket"); + if (cur !== undefined && typeof cur.get === "function" && cur.get === g) { + Object.defineProperty(globalThis, "WebSocket", { ...d, configurable: true }); + } + } + delete (globalThis as GlobalRecord)[WS_INTERCEPT_FLAG]; +} diff --git a/src/agent/pi-native.ts b/src/agent/pi-native.ts index bc78247b0d..08394924c2 100644 --- a/src/agent/pi-native.ts +++ b/src/agent/pi-native.ts @@ -27,6 +27,7 @@ import { ensureProxyRunning, LAUNCHER_DEFAULT_HOST } from "../launcher.js"; import { createBiliPlugin } from "./pi.js"; import { isLegacyBcpEntry, markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScriptPath, proxyEnvOrigin, setNativeOriginWaiter, singleFlight } from "./native-bootstrap.js"; import { installNativeFetchIntercept, noteRoutedOrigin, observeRoutedOrigin, readyOrigin, type NativeInterceptState } from "./native-intercept.js"; +import { installNativeWebSocketIntercept } from "./native-ws-intercept.js"; import { fetchProxyVersion, fetchStatus, waitForProxyVersion } from "./shared.js"; // Shared plumbing lives in native-bootstrap.ts (side-effect-free — importing @@ -248,6 +249,10 @@ if (plan.mode !== "off") armNativePi(plan); // never patch globalThis.fetch or bootstrap a real proxy from inside a test run. if (process.env.NODE_TEST_CONTEXT === undefined && plan.mode !== "off") { installNativeFetchIntercept(state); + // #2111: the Codex Responses WS leg shares the fetch intercept's enable + // conditions and lifecycle — off mode patches nothing, repeated loads are + // no-ops, and the wrapper installs before any model connection exists. + installNativeWebSocketIntercept(state); } /** Test hook: expose the armed runtime-recovery seam (mirrors opencode/dsh). */ diff --git a/tests/e2e/README.md b/tests/e2e/README.md index 7617281aed..d03c54c90b 100644 --- a/tests/e2e/README.md +++ b/tests/e2e/README.md @@ -297,6 +297,40 @@ on `workflow_dispatch` with `@opencode/cli@2.0.3` pinned (V2 lane), same discipline as the pi/codex pins (#815). The V1 lane (1.x) is exercised by the same suite via `E2E_OC_BIN`. +## Native-lane suite (`e2e-pi-codex-ws.test.ts`) — real `pi` Codex Responses WebSocket lane vs deterministic mock + +Real `pi` (pinned `pi-stable`, see CI) with a custom `openai-codex-responses` +provider pointed at a local mock that speaks BOTH legs — POST/SSE and a +WebSocket upgrade on one port — through the real built proxy. Zero tokens. + +Gated: `ACP_TEST_E2E_PI_WS=1` (skip-by-default like the other real-client +lanes); needs the built `dist/agent/pi-native.js`. Override the binary with +`E2E_PI_BIN`. + +```bash +ACP_TEST_E2E_PI_WS=1 E2E_PI_BIN=pi node --import tsx --test tests/e2e/e2e-pi-codex-ws.test.ts +``` + +### Mechanics + +Scenarios: **A** explicit `websocket` — intercept, `/bili/` rewrite, +upgrade-header stamps (`x-bili-plugin*`, `session-id` == conversation id), +compress/decompress round trip reflected in subsequent requests; **B** +`auto` — WS used when the handshake succeeds, `previous_response_id` +incremental continuation frames observed and expanded by the proxy; **C** +`auto` against a WS-refusing upstream — clean mid-stream failure (the +client-side handshake now targets the local proxy, which always succeeds), +then an explicit-`sse` ctx proves that lane is unaffected; **D** explicit +`sse` regression guard — full fold cycle over plain HTTP, zero WS +connections; **E** two subagent-style sessions sharing one proxy (attach +mode) interleave with identical tool args and never cross state. + +### CI + +`.github/workflows/ci-e2e-pi-ws.yml` (pull_request + workflow_dispatch), +mirrors `ci-e2e-native.yml`: pins `pi-stable@0.83.6`, symlinks the canonical +`pi` bin, collects `tmp/e2e-pi-codex-ws-*` artifacts on failure. + ## Release canary (`e2e-release-canary.test.ts`) — automated no-op self-update drill `ACP_TEST_CANARY=1` runs the #1811 release-receive drill: the BUILT tree is diff --git a/tests/e2e/e2e-pi-codex-ws.test.ts b/tests/e2e/e2e-pi-codex-ws.test.ts new file mode 100644 index 0000000000..5e306872a7 --- /dev/null +++ b/tests/e2e/e2e-pi-codex-ws.test.ts @@ -0,0 +1,761 @@ +import assert from "node:assert/strict"; +import fs from "node:fs"; +import http from "node:http"; +import os from "node:os"; +import path from "node:path"; +import zlib from "node:zlib"; +import { once } from "node:events"; +import { spawn, spawnSync } from "node:child_process"; +import test from "node:test"; +import { WebSocketServer } from "ws"; + +process.env.NODE_ENV = "test"; + +// #2111 (implementation follow-up to #2073/#2080): Pi native mode must route +// openai-codex-responses WebSocket traffic through bili even when transport +// "auto"/"websocket" connects successfully. The REAL pi CLI runs package-native +// (repo root loaded as a pi package) against a deterministic local mock that +// speaks Responses over WS — plus SSE for the fallback legs — zero tokens. +// Asserted end to end: +// A explicit "websocket": upgrade interception, session-id + x-bili-plugin* +// stamps on the handshake (#2073 Q4), real compress/decompress through the +// plugin channel, post-fold history consistency; +// B "auto": WS success still routes (the #2073 gap) AND previous_response_id +// incremental continuation arrives expanded; +// C "auto" with a failing WS handshake: same-turn SSE fallback stays stamped; +// D explicit "sse": regression guard, full fold cycle over HTTP; +// E two subagent-style sessions sharing one proxy: distinct conversations, +// identical tool args, no cross-talk. +// Hermetic PI_CODING_AGENT_DIR + XDG dirs; spawn cwd outside the repo tree +// (#815); pi pinned in CI (ci-e2e-pi-ws.yml). +// Run: npm run build && ACP_TEST_E2E_PI_WS=1 node --import tsx --test tests/e2e/e2e-pi-codex-ws.test.ts + +const PI_BIN = process.env.E2E_PI_BIN ?? "pi"; +const REPO_ROOT = path.resolve(import.meta.dirname, "../.."); +const PI_NATIVE_ENTRY = path.join(REPO_ROOT, "dist/agent/pi-native.js"); +const TMO = Number(process.env.E2E_TMO ?? 180_000); +const WORK_ROOT = path.join(process.cwd(), "tmp"); +fs.mkdirSync(WORK_ROOT, { recursive: true }); +const CWD_ROOT = path.join(os.tmpdir(), "billion-context-e2e-pi-ws"); +fs.mkdirSync(CWD_ROOT, { recursive: true }); + +const run = process.env.ACP_TEST_E2E_PI_WS === "1"; + +function piAvailable(): boolean { + try { + return spawnSync(PI_BIN, ["--version"], { timeout: 15_000 }).status === 0; + } catch { + return false; + } +} + +const skipReason = !run + ? "set ACP_TEST_E2E_PI_WS=1 (real pi + local mock WS upstream; deterministic, zero tokens)" + : !fs.existsSync(PI_NATIVE_ENTRY) + ? "dist/agent/pi-native.js missing — run `npm run build` first" + : !piAvailable() + ? `pi binary "${PI_BIN}" not found on PATH` + : undefined; + +type Item = Record; +type Row = { transport: "ws" | "http"; body: Item; full: Item[]; headers: Record }; + +/** pi's extractAccountId requires a 3-part JWT carrying the chatgpt_account_id + * claim, so the fake apiKey is a structurally valid (unsigned) JWT. */ +function fakeJwt(accountId: string): string { + const b64 = (o: object): string => Buffer.from(JSON.stringify(o)).toString("base64"); + return `${b64({ alg: "none", typ: "JWT" })}.${b64({ "https://api.openai.com/auth": { chatgpt_account_id: accountId } })}.sig`; +} + +function filler(lines: number, tag: string): string { + const out: string[] = []; + for (let n = 0; n < lines; n += 1) out.push(`filler ${tag} line ${n}: marker=${n * 7} ${"x".repeat(30)}`); + return out.join("\n"); +} + +const itemText = (item: Item | undefined): string => typeof item?.content === "string" + ? item.content + : Array.isArray(item?.content) + ? item.content.map((part) => typeof part === "object" && part !== null ? String((part as Item).text ?? "") : "").join("\n") + : ""; + +/** ACP ref tags injected by bili into forwarded request text (hex-escaped per KDD #2). */ +const refsOf = (text: string): string[] => { + const out: string[] = []; + for (const m of text.matchAll(/\x3cacp[^\x3e]*\x3e(m\d+)\x3c\/acp\x3e/g)) out.push(m[1]!); + return out; +}; + +function recordHeaders(h: http.IncomingHttpHeaders): Record { + const out: Record = {}; + for (const [k, v] of Object.entries(h)) if (typeof v === "string") out[k] = v; + return out; +} + +// --------------------------------------------------------------------------- +// Mock upstream: Responses-over-WS (+ SSE on the POST leg), scripted model. +// Directives ride in the user text: WS_FOLD_REQUEST_B64 / WS_DECOMPRESS_B64 +// carry b64-encoded tool args; WS_STATUS triggers an acp_status call. +// previous_response_id frames are expanded against per-response snapshots, +// mirroring the real Codex continuation protocol. +// --------------------------------------------------------------------------- + +type MockUpstream = { + server: http.Server; + port: number; + rows: Row[]; + stats: { wsConnections: number }; +}; + +async function startMock(opts: { failUpgrade?: boolean; getRowsPath?: () => string | undefined } = {}): Promise { + const rows: Row[] = []; + const stats = { wsConnections: 0 }; + let serial = 0; + const snapshots = new Map(); + // One-shot instruction guard: the directive text persists in the user message + // across the tool loop, so without this the scripted model re-issues the same + // tool call forever after a failed attempt (observed: ~2000 requests in 179 s). + const instructed = new Set(); + + const reply = (body: Item, headers: Record, transport: "ws" | "http", send: (event: Item) => void): void => { + const prior = typeof body.previous_response_id === "string" ? snapshots.get(body.previous_response_id) : undefined; + const input = Array.isArray(body.input) ? (body.input as Item[]) : []; + const full = [...(prior ?? []), ...input]; + rows.push({ transport, body, full, headers }); + // Incremental evidence: survives any later t.after crash (the final + // dump alone lost rows.json whenever cleanup threw first). + const rowsPath = opts.getRowsPath?.(); + if (rowsPath) { + try { + // Item-level projection keeps post-mortem forensics possible even + // when t.after dies before the final pretty dump. + const brief = (i: Item): string[] => { + const text = i.type === "function_call_output" ? String(i.output ?? "") : itemText(i); + return [String(i.type ?? "?"), String(i.name ?? i.role ?? ""), String(i.call_id ?? ""), text.slice(0, 60).replace(/\n/g, "\\n")]; + }; + fs.writeFileSync(rowsPath, JSON.stringify(rows.map((r) => ({ transport: r.transport, headers: r.headers, prevId: r.body.previous_response_id, inputLen: Array.isArray(r.body.input) ? (r.body.input as unknown[]).length : 0, fullLen: r.full.length, items: r.full.map(brief) })))); + } catch { /* evidence-only */ } + } + + const lastUser = [...full].reverse().find((item) => item.role === "user"); + const text = itemText(lastUser); + const foldEnc = text.match(/WS_FOLD_REQUEST_B64 ([A-Za-z0-9+/=]+)/)?.[1]; + const decEnc = text.match(/WS_DECOMPRESS_B64 ([A-Za-z0-9+/=]+)/)?.[1]; + let name: string | undefined; + let args = "{}"; + const conv = typeof headers["x-bili-plugin-conversation"] === "string" ? headers["x-bili-plugin-conversation"] : "?"; + // ACP tools register during call #1's before_provider_headers, so they are + // invisible to pi's executor until call >= 2 of the same process. Prime the + // tool loop with probe_tool (package-registered at load, visible from call + // #1), then issue the ACP call on the continuation — the pattern proven in + // e2e-native-pi.test.ts ("请调用probe_tool;请调用compress;请调用acp_status"). + const pick = (key: string, tool: string, toolArgs: string): void => { + if (name !== undefined || instructed.has(`${conv}:${key}`)) return; + if (instructed.has(`${conv}:${key}-prime`)) { + instructed.add(`${conv}:${key}`); + name = tool; + args = toolArgs; + return; + } + instructed.add(`${conv}:${key}-prime`); + name = "probe_tool"; + }; + if (foldEnc) pick("fold", "compress", Buffer.from(foldEnc, "base64").toString("utf8")); + else if (decEnc) pick("decompress", "decompress", Buffer.from(decEnc, "base64").toString("utf8")); + else if (/WS_STATUS/.test(text)) pick("status", "acp_status", "{}"); + + const id = `resp_pi_ws_${++serial}`; + const fcId = `fc_${id}`; + const msgId = `msg_${id}`; + const item: Item = name + ? { type: "function_call", id: fcId, call_id: `call_${id}`, name, arguments: args, status: "completed" } + : { type: "message", id: msgId, role: "assistant", status: "completed", content: [{ type: "output_text", text: "WS_E2E_OK", annotations: [] }] }; + const inputTokens = Math.max(1, Math.ceil(full.reduce((n, i) => n + itemText(i).length, 0) / 4)); + let sequence = 0; + const event = (type: string, data: Item): void => send({ type, sequence_number: sequence++, ...data }); + event("response.created", { response: { id, object: "response", status: "in_progress", output: [] } }); + event("response.output_item.added", { output_index: 0, item: { ...item, status: "in_progress", ...(name ? { arguments: "" } : { content: [] }) } }); + if (name) { + event("response.function_call_arguments.delta", { item_id: fcId, output_index: 0, delta: args }); + event("response.function_call_arguments.done", { item_id: fcId, output_index: 0, arguments: args }); + } else { + event("response.content_part.added", { item_id: msgId, output_index: 0, content_index: 0, part: { type: "output_text", text: "", annotations: [] } }); + event("response.output_text.delta", { item_id: msgId, output_index: 0, content_index: 0, delta: "WS_E2E_OK" }); + event("response.output_text.done", { item_id: msgId, output_index: 0, content_index: 0, text: "WS_E2E_OK" }); + event("response.content_part.done", { item_id: msgId, output_index: 0, content_index: 0, part: (item.content as Item[])[0] }); + } + event("response.output_item.done", { output_index: 0, item }); + snapshots.set(id, [...full, item]); + event("response.completed", { response: { id, object: "response", model: body.model, status: "completed", output: [item], usage: { input_tokens: inputTokens, output_tokens: 10, total_tokens: inputTokens + 10, input_tokens_details: { cached_tokens: 0 }, output_tokens_details: { reasoning_tokens: 0 } } } }); + }; + + const server = http.createServer((req, res) => { + if (req.method !== "POST") { res.writeHead(404).end(); return; } + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + // pi's SSE leg zstd-compresses the body (Codex backend contract). + let raw = Buffer.concat(chunks); + if (String(req.headers["content-encoding"] ?? "").includes("zstd")) raw = zlib.zstdDecompressSync(raw); + let body: Item; + try { + body = JSON.parse(raw.toString("utf8")) as Item; + } catch { + res.writeHead(400).end(); + return; + } + res.writeHead(200, { "content-type": "text/event-stream" }); + reply(body, recordHeaders(req.headers), "http", (ev) => res.write(`event: ${ev.type}\ndata: ${JSON.stringify(ev)}\n\n`)); + res.end(); + }); + }); + if (!opts.failUpgrade) { + const wss = new WebSocketServer({ server }); + wss.on("connection", (peer, req) => { + stats.wsConnections += 1; + peer.on("message", (raw) => { + let body: Item; + try { + body = JSON.parse(String(raw)) as Item; + } catch { + return; + } + reply(body, recordHeaders(req.headers), "ws", (ev) => peer.send(JSON.stringify(ev))); + }); + }); + } else { + // Refuse the handshake the way a real server would: HTTP status on the + // upgrade request. A raw socket.destroy() surfaces as a NON-transport + // error in pi's classifier (isCodexNonTransportError → throw, no SSE + // fallback); production ChatGPT refusals carry status codes. + server.on("upgrade", (_req, socket) => { + stats.wsConnections += 1; // refusals count too: C asserts upstream ATTEMPTS + socket.write("HTTP/1.1 503 Service Unavailable\r\nConnection: close\r\nContent-Length: 0\r\n\r\n"); + socket.end(); + }); + } + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + return { server, port: (server.address() as { port: number }).port, rows, stats }; +} + +// --------------------------------------------------------------------------- +// Hermetic pi context (mirrors e2e-native-pi.test.ts mechanics) +// --------------------------------------------------------------------------- + +type Ctx = { + work: string; + piCwd: string; + piAgentDir: string; + xdg: { config: string; cache: string; state: string; data: string }; +}; + +function cleanEnv(extra: NodeJS.ProcessEnv = {}): NodeJS.ProcessEnv { + // This suite may run INSIDE a bili-driven shell: the native lane must + // bootstrap/attach its OWN proxy, so every bili side-channel has to go. + const env: NodeJS.ProcessEnv = { ...process.env }; + for (const key of Object.keys(env)) { + if (key.startsWith("BILI") || key.startsWith("BILLION_CONTEXT") || key.startsWith("ACP_")) delete env[key]; + } + // NODE_TEST_CONTEXT stands down pi-native inside node:test; the spawned pi + // is a real client, not a test context (same rationale as e2e-native-pi). + for (const key of ["NODE_EXTRA_CA_CERTS", "PI_SESSION_FILE", "PI_PROVIDER", "PI_MODEL", "NODE_OPTIONS", "NODE_TEST_CONTEXT"]) delete env[key]; + return { ...env, ...extra }; +} + +async function startCtx(transport: string, mockPort: number): Promise { + const work = fs.mkdtempSync(path.join(WORK_ROOT, "e2e-pi-codex-ws-")); + const ctx: Ctx = { + work, + piCwd: fs.mkdtempSync(path.join(CWD_ROOT, "cwd-")), + piAgentDir: path.join(work, "pi-agent"), + xdg: { + config: path.join(work, "xdg-config"), + cache: path.join(work, "xdg-cache"), + state: path.join(work, "xdg-state"), + data: path.join(work, "xdg-data"), + }, + }; + const biliCfgDir = path.join(ctx.xdg.config, "billion-context"); + for (const d of [ctx.piAgentDir, biliCfgDir, ctx.xdg.cache, ctx.xdg.state, ctx.xdg.data]) fs.mkdirSync(d, { recursive: true }); + + // Benign host tool visible from call #1 of every process: primes the tool + // loop before ACP tools land (they register from the 2nd request on). Same + // package shape as e2e-native-pi.test.ts. + const probePkg = path.join(work, "probe-pkg"); + fs.mkdirSync(probePkg, { recursive: true }); + fs.writeFileSync( + path.join(probePkg, "package.json"), + JSON.stringify({ name: "probe-pkg", version: "1.0.0", pi: { extensions: ["./index.js"] } }, null, 2), + ); + fs.writeFileSync( + path.join(probePkg, "index.js"), + [ + `const BIG = Array.from({ length: 60 }, (_, i) => \`probe payload line \${i}: unique-\${i * 7} \${"p".repeat(30)}\`).join("\\n");`, + "export default function (pi) {", + " pi.registerTool({", + ' name: "probe_tool",', + ' description: "e2e probe tool returning an oversized payload for compress tests",', + ' parameters: { type: "object", properties: { text: { type: "string" } } },', + ' execute: async () => ({ content: [{ type: "text", text: `probe ok\\n${BIG}` }] }),', + " });", + "}", + ].join("\n"), + ); + fs.writeFileSync(path.join(ctx.piAgentDir, "settings.json"), JSON.stringify({ packages: [REPO_ROOT, probePkg], transport }, null, 2)); + fs.writeFileSync( + path.join(ctx.piAgentDir, "models.json"), + JSON.stringify( + { + providers: { + "codex-fake": { + baseUrl: `http://127.0.0.1:${mockPort}/backend-api`, + api: "openai-codex-responses", + apiKey: fakeJwt("acct-e2e-pi-ws"), + models: [{ id: "codex-fake-model", name: "CodexFake", input: ["text"], contextWindow: 200_000, maxTokens: 8192 }], + }, + }, + }, + null, + 2, + ), + ); + // Loosen the kernel's recent-content protection so the scripted fold can + // target the planted bulk (same seam the opencode WS suite uses). + fs.writeFileSync(path.join(biliCfgDir, "billion-context.json"), JSON.stringify({ compress: { preserveRecentTokens: 0 } })); + return ctx; +} + +function piRun( + ctx: Ctx, + prompt: string, + opts: { resume?: boolean; extraEnv?: NodeJS.ProcessEnv } = {}, +): Promise<{ code: number; stdout: string; stderr: string }> { + const args = ["-p", "--model", "codex-fake/codex-fake-model"]; + if (opts.resume) args.push("--continue"); + args.push(prompt); + const stamp = `${Date.now()}-${Math.random().toString(36).slice(2)}`; + const outFile = path.join(ctx.work, `pi-${stamp}.out`); + const errFile = path.join(ctx.work, `pi-${stamp}.err`); + return new Promise((resolve, reject) => { + const child = spawn(PI_BIN, args, { + cwd: ctx.piCwd, + env: { + ...cleanEnv(opts.extraEnv), + PI_CODING_AGENT_DIR: ctx.piAgentDir, + XDG_CONFIG_HOME: ctx.xdg.config, + XDG_CACHE_HOME: ctx.xdg.cache, + XDG_STATE_HOME: ctx.xdg.state, + XDG_DATA_HOME: ctx.xdg.data, + }, + stdio: ["ignore", "pipe", "pipe"], + }); + let out = ""; + let err = ""; + child.stdout.on("data", (c) => { out += c; }); + child.stderr.on("data", (c) => { err += c; }); + const timer = setTimeout(() => { + try { child.kill("SIGKILL"); } catch { /* noop */ } + reject(new Error(`pi -p timed out after ${TMO}ms`)); + }, TMO); + child.on("exit", (code) => { + clearTimeout(timer); + fs.writeFileSync(outFile, out); + fs.writeFileSync(errFile, err); + resolve({ code: code ?? -1, stdout: out, stderr: err }); + }); + }); +} + +function stopProxiesGracefully(ctx: Ctx): void { + const dir = path.join(ctx.xdg.state, "billion-context", "instances"); + try { + for (const f of fs.readdirSync(dir)) { + try { + const rec = JSON.parse(fs.readFileSync(path.join(dir, f), "utf8")) as { pid?: number }; + if (typeof rec.pid === "number" && rec.pid > 0) { + try { process.kill(rec.pid, "SIGTERM"); } catch { /* already gone */ } + } + } catch { /* unreadable record */ } + } + } catch { /* no instances dir */ } +} + +function teardownProxies(ctx: Ctx): void { + const dir = path.join(ctx.xdg.state, "billion-context", "instances"); + try { + for (const f of fs.readdirSync(dir)) { + try { + const rec = JSON.parse(fs.readFileSync(path.join(dir, f), "utf8")) as { pid?: number }; + if (typeof rec.pid === "number" && rec.pid > 0) { + try { process.kill(rec.pid, "SIGKILL"); } catch { /* already gone */ } + } + } catch { /* unreadable record */ } + } + } catch { /* no instances dir */ } +} + +function biliLog(ctx: Ctx): string { + try { + return fs.readFileSync(path.join(ctx.xdg.state, "billion-context", "bili.log"), "utf8"); + } catch { + return ""; + } +} + +type SessionFile = { payload?: { metadata?: { pluginAgent?: string }; stats?: { requests?: number }; state?: { blocks?: unknown[] } } }; + +function sessionFiles(ctx: Ctx): SessionFile[] { + const dir = path.join(ctx.xdg.data, "billion-context", "sessions"); + const out: SessionFile[] = []; + try { + for (const prov of fs.readdirSync(dir)) { + let entries: string[]; + try { entries = fs.readdirSync(path.join(dir, prov)); } catch { continue; } + for (const f of entries) { + if (!f.endsWith(".json")) continue; + try { out.push(JSON.parse(fs.readFileSync(path.join(dir, prov, f), "utf8")) as SessionFile); } catch { /* unreadable */ } + } + } + } catch { /* no sessions dir */ } + return out; +} + +function proxyOriginOf(ctx: Ctx): string | undefined { + const dir = path.join(ctx.xdg.state, "billion-context", "instances"); + try { + for (const f of fs.readdirSync(dir)) { + const rec = JSON.parse(fs.readFileSync(path.join(dir, f), "utf8")) as { origin?: string; host?: string; port?: number }; + if (typeof rec.origin === "string" && rec.origin) return rec.origin; + if (typeof rec.port === "number" && rec.port > 0) return `http://${rec.host ?? "127.0.0.1"}:${rec.port}`; + } + } catch { /* no instances dir */ } + return undefined; +} + +const convsOf = (rows: Row[]): Set => { + const out = new Set(); + for (const r of rows) { + const c = r.headers["x-bili-plugin-conversation"]; + if (typeof c === "string" && c) out.add(c); + } + return out; +}; + +/** Find the tool-result text for a given call in a row's expanded history. */ +function toolResult(row: Row, name: string): string | undefined { + const call = [...row.full].reverse().find((i) => i.type === "function_call" && i.name === name); + if (!call) return undefined; + const out = row.full.find((i) => i.type === "function_call_output" && i.call_id === call.call_id); + return out ? String(out.output ?? "") : undefined; +} + +const b64 = (o: object): string => Buffer.from(JSON.stringify(o)).toString("base64"); + +// --------------------------------------------------------------------------- +// Tests +// --------------------------------------------------------------------------- + +test("preflight: pi binary + built dist + mock handshake (E2E_CHECK)", { skip: skipReason }, async (t) => { + const version = spawnSync(PI_BIN, ["--version"], { timeout: 15_000 }).stdout?.toString().trim() ?? "?"; + const mock = await startMock(); + try { + assert.ok(mock.port > 0); + } finally { + mock.server.closeAllConnections(); + await new Promise((r) => mock.server.close(() => r())); + } + t.diagnostic(`pi ${version}, dist ${PI_NATIVE_ENTRY}, mock handshake ok`); +}); + +test("A: explicit websocket — intercepted, stamped, compress/decompress round trip", { skip: skipReason, timeout: 900_000 }, async (t) => { + let ctx: Ctx | undefined; + const mock = await startMock({ getRowsPath: () => (ctx ? path.join(ctx.work, "rows.json") : undefined) }); + t.after(async () => { + if (ctx) stopProxiesGracefully(ctx); + await new Promise((r) => setTimeout(r, 500)); + if (ctx) teardownProxies(ctx); + mock.server.closeAllConnections(); + await new Promise((r) => mock.server.close(() => r())); + if (ctx) fs.writeFileSync(path.join(ctx.work, "rows.json"), JSON.stringify(mock.rows.map((r) => ({ transport: r.transport, headers: r.headers, prevId: r.body.previous_response_id, fullLen: r.full.length, inputLen: Array.isArray(r.body.input) ? (r.body.input as unknown[]).length : 0 })), null, 2)); + }); + ctx = await startCtx("websocket", mock.port); + + const SENTINEL = "SENTINEL_A_PI_WS"; + const SUMMARY = "FOLDED_SUMMARY_A_PI_WS: folded 150 lines of filler bulk planted by scenario A; no decisions or state retained beyond this marker."; + const ok = (label: string, r: { code: number; stdout: string; stderr: string }): void => { + assert.equal(r.code, 0, `${label}: exit ${r.code}\n${r.stderr.slice(-2000)}`); + assert.ok(r.stdout.includes("WS_E2E_OK"), `${label}: model answer missing\n${r.stdout.slice(-1000)}\n${r.stderr.slice(-1000)}`); + }; + + ok("warmup", await piRun(ctx, "ping one")); + ok("bulk", await piRun(ctx, `${filler(150,"A")} ${SENTINEL}`, { resume: true })); + ok("pushback 1", await piRun(ctx, "pushback one", { resume: true })); + ok("pushback 2", await piRun(ctx, "pushback two", { resume: true })); + + const wsRows = mock.rows.filter((r) => r.transport === "ws"); + assert.ok(wsRows.length >= 4, `expected >=4 WS requests, got ${wsRows.length}`); + assert.equal(convsOf(wsRows).size, 1, "one conversation across all WS requests"); + const conv = [...convsOf(wsRows)][0]!; + assert.equal(wsRows.every((r) => r.headers["x-bili-plugin"] === "pi"), true, "x-bili-plugin stamp on every WS handshake"); + assert.ok(wsRows.every((r) => typeof r.headers["session-id"] === "string" && r.headers["session-id"].length > 0), "session-id header reaches the proxy"); + assert.equal(wsRows[0]!.headers["session-id"], conv, "WS session-id equals the conversation identity (HTTP-path parity)"); + + // Harvest refs from the last pre-fold request. The fold starts at the FIRST + // USER message ref (never the pinned head-system anchor) and ends at the + // sentinel message ref. + const lastPreFold = wsRows.at(-1)!; + const userRefs = lastPreFold.full.filter((i) => i.role === "user").flatMap((i) => refsOf(itemText(i))); + assert.ok(userRefs.length >= 2, `ACP tags on user messages present (${userRefs.length})`); + const sentinelItem = lastPreFold.full.find((i) => i.role === "user" && itemText(i).includes(SENTINEL)); + const sentinelRef = sentinelItem ? refsOf(itemText(sentinelItem))[0] : undefined; + assert.ok(sentinelRef, "sentinel message carries an ACP ref tag"); + const minRef = userRefs.reduce((a, b) => (Number(a.slice(1)) < Number(b.slice(1)) ? a : b)); + + ok("fold", await piRun(ctx, `WS_FOLD_REQUEST_B64 ${b64({ content: [{ startId: minRef, endId: sentinelRef, summary: SUMMARY }] })}`, { resume: true })); + const foldedRow = mock.rows.filter((r) => r.transport === "ws").find((r) => toolResult(r, "compress")?.includes("Compressed")); + assert.ok(foldedRow, "compress executed and reported the fold"); + + ok("status", await piRun(ctx, "WS_STATUS post-fold recall", { resume: true })); + const statusRow = mock.rows.filter((r) => r.transport === "ws").find((r) => toolResult(r, "acp_status")); + assert.ok(statusRow, "acp_status executed after the fold"); + const blockId = toolResult(statusRow!, "acp_status")!.match(/\bb(\d+)\b/)?.[1]; + assert.ok(blockId, "acp_status report lists the compressed block (need its bN ref)"); + const statusJson = JSON.stringify(statusRow!.full); + assert.ok(!statusJson.includes(SENTINEL), "post-fold request no longer carries the folded bulk"); + assert.ok(statusJson.includes(SUMMARY), "post-fold request carries the summary"); + + ok("decompress", await piRun(ctx, `WS_DECOMPRESS_B64 ${b64({ blockId: `b${blockId}` })}`, { resume: true })); + ok("recall", await piRun(ctx, "final recall", { resume: true })); + + const final = mock.rows.filter((r) => r.transport === "ws").at(-1)!; + assert.ok(JSON.stringify(final.full).includes(SENTINEL), "decompressed content is back in the rebuilt history"); + + const log = biliLog(ctx); + assert.match(log, /forward WS/); + assert.match(log, /tool compress executed via plugin/); + assert.match(log, /acp-usage/); + + stopProxiesGracefully(ctx); + await new Promise((r) => setTimeout(r, 500)); + const sessions = sessionFiles(ctx); + assert.ok(sessions.length >= 1, "session persisted"); + assert.ok(sessions.every((s) => s.payload?.metadata?.pluginAgent === "pi"), "plugin-mode binding persisted"); + assert.ok(sessions.some((s) => Array.isArray(s.payload?.state?.blocks) && (s.payload.state.blocks as unknown[]).length >= 1), "compressed block persisted"); +}); + +test("B: auto — successful WS still routes, previous_response_id continuation expands", { skip: skipReason, timeout: 900_000 }, async (t) => { + let ctx: Ctx | undefined; + const mock = await startMock({ getRowsPath: () => (ctx ? path.join(ctx.work, "rows.json") : undefined) }); + t.after(async () => { + if (ctx) stopProxiesGracefully(ctx); + await new Promise((r) => setTimeout(r, 500)); + if (ctx) teardownProxies(ctx); + mock.server.closeAllConnections(); + await new Promise((r) => mock.server.close(() => r())); + }); + ctx = await startCtx("auto", mock.port); + + const SENTINEL = "SENTINEL_B_PI_WS"; + const SUMMARY = "FOLDED_SUMMARY_B_PI_WS: folded 150 lines of filler bulk planted by scenario B; no decisions or state retained beyond this marker."; + const ok = (label: string, r: { code: number; stdout: string; stderr: string }): void => { + assert.equal(r.code, 0, `${label}: exit ${r.code}\n${r.stderr.slice(-2000)}`); + assert.ok(r.stdout.includes("WS_E2E_OK"), `${label}: model answer missing`); + }; + + ok("warmup", await piRun(ctx, "ping auto one")); + ok("bulk", await piRun(ctx, `${filler(150,"B")} ${SENTINEL}`, { resume: true })); + ok("pushback 1", await piRun(ctx, "pushback one", { resume: true })); + ok("pushback 2", await piRun(ctx, "pushback two", { resume: true })); + + const wsRows = () => mock.rows.filter((r) => r.transport === "ws"); + assert.ok(wsRows().length >= 4, `auto mode used WS (${wsRows().length} rows)`); + assert.equal(convsOf(wsRows()).size, 1); + assert.ok(wsRows().every((r) => r.headers["x-bili-plugin"] === "pi" && typeof r.headers["session-id"] === "string" && r.headers["session-id"].length > 0)); + + const lastPreFold = wsRows().at(-1)!; + const userRefs = lastPreFold.full.filter((i) => i.role === "user").flatMap((i) => refsOf(itemText(i))); + const sentinelItem = lastPreFold.full.find((i) => i.role === "user" && itemText(i).includes(SENTINEL)); + const sentinelRef = sentinelItem ? refsOf(itemText(sentinelItem))[0] : undefined; + assert.ok(sentinelRef && userRefs.length >= 2, `user-message ACP refs present (${userRefs.length})`); + const minRef = userRefs.reduce((a, b) => (Number(a.slice(1)) < Number(b.slice(1)) ? a : b)); + + ok("fold", await piRun(ctx, `WS_FOLD_REQUEST_B64 ${b64({ content: [{ startId: minRef, endId: sentinelRef, summary: SUMMARY }] })}`, { resume: true })); + + // Delta frames emerge inside multi-call tool-loop processes (pi's + // websocketSessionCache is per-process): A/E show them on the final call of + // an acp_status loop, never on single-prompt runs. Mirror A's proven shape. + ok("status", await piRun(ctx, "WS_STATUS", { resume: true })); + + const cont = wsRows().find((r) => typeof r.body.previous_response_id === "string" && Array.isArray(r.body.input) && r.full.length > (r.body.input as unknown[]).length); + assert.ok(cont, "previous_response_id incremental continuation observed and expanded"); + assert.ok(cont!.full.some((i) => i.type === "function_call_output" && String(i.output ?? "").includes("Compressed")), "continuation frame carries the fold result"); + + const afterFold = wsRows().filter((r) => r.full.some((i) => i.type === "function_call_output" && String(i.output ?? "").includes("Compressed"))); + const final = afterFold.at(-1)!; + assert.ok(!JSON.stringify(final.full).includes(SENTINEL), "post-fold history lost the bulk"); + assert.ok(JSON.stringify(final.full).includes(SUMMARY), "post-fold history carries the summary"); + + assert.match(biliLog(ctx), /forward WS/); +}); + +test("C: auto — upstream WS refusal surfaces cleanly; explicit sse unaffected", { skip: skipReason, timeout: 900_000 }, async (t) => { + let ctx: Ctx | undefined; + let ctxSse: Ctx | undefined; + const mock = await startMock({ failUpgrade: true, getRowsPath: () => (ctx ? path.join(ctx.work, "rows.json") : undefined) }); + t.after(async () => { + if (ctx) stopProxiesGracefully(ctx); + if (ctxSse) stopProxiesGracefully(ctxSse); + await new Promise((r) => setTimeout(r, 500)); + if (ctx) teardownProxies(ctx); + if (ctxSse) teardownProxies(ctxSse); + mock.server.closeAllConnections(); + await new Promise((r) => mock.server.close(() => r())); + }); + ctx = await startCtx("auto", mock.port); + + // Topology change disclosed with #2111: interception moves the client + // handshake onto bili (local — always succeeds), so an upstream refusal now + // lands on bili's UPSTREAM leg. pi classifies that as a mid-stream failure + // (websocketStarted=true → throw, no same-turn SSE fallback; pre-intercept + // only a client-side handshake failure triggered recordWebSocketSseFallback). + const r = await piRun(ctx, "ping fallback"); + assert.notEqual(r.code, 0, `mid-stream WS failure surfaces as an error (exit ${r.code})`); + assert.match(r.stderr, /Responses WebSocket handshake failed|provider_transport_failure/, "pi reported the transport failure"); + assert.ok(mock.stats.wsConnections >= 1, "bili attempted the upstream WS connection"); + assert.match(biliLog(ctx), /codex-responses-ws/, "client leg reached bili's WS bridge lane"); + + // Explicit sse stays usable against the same WS-refusing upstream. + ctxSse = await startCtx("sse", mock.port); + const sse = await piRun(ctxSse, "ping sse after ws failure"); + assert.equal(sse.code, 0, `sse leg exit ${sse.code}\n${sse.stderr.slice(-2000)}`); + assert.ok(sse.stdout.includes("WS_E2E_OK"), "sse leg answered"); + const httpRows = mock.rows.filter((x) => x.transport === "http"); + assert.ok(httpRows.length >= 1, "sse leg reached the upstream through bili"); + assert.ok(httpRows.every((x) => x.headers["x-bili-plugin"] === "pi" && typeof x.headers["session-id"] === "string" && x.headers["session-id"].length > 0), "sse leg keeps plugin stamp + session-id"); +}); + +test("D: explicit sse — regression guard, full fold cycle over HTTP", { skip: skipReason, timeout: 900_000 }, async (t) => { + let ctx: Ctx | undefined; + const mock = await startMock({ getRowsPath: () => (ctx ? path.join(ctx.work, "rows.json") : undefined) }); + t.after(async () => { + if (ctx) stopProxiesGracefully(ctx); + await new Promise((r) => setTimeout(r, 500)); + if (ctx) teardownProxies(ctx); + mock.server.closeAllConnections(); + await new Promise((r) => mock.server.close(() => r())); + }); + ctx = await startCtx("sse", mock.port); + + const SENTINEL = "SENTINEL_D_PI_WS"; + const SUMMARY = "FOLDED_SUMMARY_D_PI_WS: folded 150 lines of filler bulk planted by scenario D; no decisions or state retained beyond this marker."; + const ok = (label: string, r: { code: number; stdout: string; stderr: string }): void => { + assert.equal(r.code, 0, `${label}: exit ${r.code}\n${r.stderr.slice(-2000)}`); + assert.ok(r.stdout.includes("WS_E2E_OK")); + }; + + ok("warmup", await piRun(ctx, "ping sse one")); + ok("bulk", await piRun(ctx, `${filler(150,"D")} ${SENTINEL}`, { resume: true })); + ok("pushback 1", await piRun(ctx, "pushback one", { resume: true })); + ok("pushback 2", await piRun(ctx, "pushback two", { resume: true })); + + const preFoldRows = mock.rows.filter((r) => r.transport === "http"); + assert.ok(preFoldRows.length >= 4, `explicit sse stayed on HTTP (${preFoldRows.length} rows)`); + assert.equal(mock.stats.wsConnections, 0, "explicit sse never opens a WS connection"); + assert.equal(convsOf(preFoldRows).size, 1); + assert.ok(preFoldRows.every((r) => r.headers["x-bili-plugin"] === "pi" && typeof r.headers["session-id"] === "string" && r.headers["session-id"].length > 0)); + + const lastPreFold = preFoldRows.at(-1)!; + const userRefs = lastPreFold.full.filter((i) => i.role === "user").flatMap((i) => refsOf(itemText(i))); + const sentinelItem = lastPreFold.full.find((i) => i.role === "user" && itemText(i).includes(SENTINEL)); + const sentinelRef = sentinelItem ? refsOf(itemText(sentinelItem))[0] : undefined; + assert.ok(sentinelRef && userRefs.length >= 2, `user-message ACP refs present (${userRefs.length})`); + const minRef = userRefs.reduce((a, b) => (Number(a.slice(1)) < Number(b.slice(1)) ? a : b)); + + ok("fold", await piRun(ctx, `WS_FOLD_REQUEST_B64 ${b64({ content: [{ startId: minRef, endId: sentinelRef, summary: SUMMARY }] })}`, { resume: true })); + ok("recall", await piRun(ctx, "final recall", { resume: true })); + + // Re-capture AFTER fold/recall: preFoldRows is a pre-fold snapshot. + const httpRows = mock.rows.filter((r) => r.transport === "http"); + const final = httpRows.at(-1)!; + const foldedSeen = httpRows.some((r) => toolResult(r, "compress")?.includes("Compressed")); + assert.ok(foldedSeen, "compress executed over the SSE leg"); + assert.ok(!JSON.stringify(final.full).includes(SENTINEL), "post-fold HTTP history lost the bulk"); + assert.ok(JSON.stringify(final.full).includes(SUMMARY), "post-fold HTTP history carries the summary"); + assert.match(biliLog(ctx), /tool compress executed via plugin/); +}); + +test("E: two subagent-style sessions share one proxy — distinct state, identical tool args, no cross-talk", { skip: skipReason, timeout: 900_000 }, async (t) => { + let ctxX: Ctx | undefined; + let ctxY: Ctx | undefined; + const mock = await startMock({ getRowsPath: () => (ctxX ?? ctxY ? path.join((ctxX ?? ctxY)!.work, "rows.json") : undefined) }); + t.after(async () => { + if (ctxX) stopProxiesGracefully(ctxX); + if (ctxY) stopProxiesGracefully(ctxY); + await new Promise((r) => setTimeout(r, 500)); + if (ctxX) teardownProxies(ctxX); + if (ctxY) teardownProxies(ctxY); + mock.server.closeAllConnections(); + await new Promise((r) => mock.server.close(() => r())); + }); + ctxX = await startCtx("websocket", mock.port); + ctxY = await startCtx("websocket", mock.port); + + const ok = (label: string, r: { code: number; stdout: string; stderr: string }): void => { + assert.equal(r.code, 0, `${label}: exit ${r.code}\n${r.stderr.slice(-2000)}`); + assert.ok(r.stdout.includes("WS_E2E_OK")); + }; + + ok("X warmup", await piRun(ctxX, "alpha marker ALPHA_X_SEED")); + const origin = proxyOriginOf(ctxX); + assert.ok(origin, "X's proxy origin discovered from its instance record"); + // Y attaches to X's proxy (real subagent posture: inherited + // BILLION_CONTEXT_PROXY), so ONE proxy serves BOTH conversations. + const yEnv = { BILLION_CONTEXT_PROXY: origin }; + + ok("Y warmup", await piRun(ctxY, "beta marker BETA_Y_SEED", { extraEnv: yEnv })); + + // Same tool args ({}) from both sessions, interleaved. + const [xStatus, yStatus] = await Promise.all([ + piRun(ctxX, "WS_STATUS alpha status", { resume: true }), + piRun(ctxY, "WS_STATUS beta status", { resume: true, extraEnv: yEnv }), + ]); + ok("X status", xStatus); + ok("Y status", yStatus); + + const rows = mock.rows; + const convs = convsOf(rows.filter((r) => r.transport === "ws")); + assert.equal(convs.size, 2, `two distinct conversations on one proxy (got ${convs.size})`); + const xSeedRow = rows.find((r) => r.transport === "ws" && r.full.some((i) => itemText(i).includes("ALPHA_X_SEED"))); + const xConv = xSeedRow?.headers["x-bili-plugin-conversation"]; + const yConv = [...convs].find((c) => c !== xConv); + assert.ok(typeof xConv === "string" && typeof yConv === "string", "both conversations identified"); + + const xStatusRow = rows.find((r) => r.headers["x-bili-plugin-conversation"] === xConv && toolResult(r, "acp_status")); + const yStatusRow = rows.find((r) => r.headers["x-bili-plugin-conversation"] === yConv && toolResult(r, "acp_status")); + assert.ok(xStatusRow && yStatusRow, "both sessions got an acp_status result"); + const xReport = toolResult(xStatusRow!, "acp_status")!; + const yReport = toolResult(yStatusRow!, "acp_status")!; + // acp_status reports structural state, not message content — per-session + // ownership is proven by what each status REQUEST carried: identical {} + // args from two sessions, distinct histories, no cross-content either way. + assert.ok(xReport.length > 50 && yReport.length > 50, "both sessions got a real acp_status report"); + assert.ok(JSON.stringify(xStatusRow!.full).includes("ALPHA_X_SEED"), "X's status request carried X's history"); + assert.ok(!JSON.stringify(xStatusRow!.full).includes("BETA_Y_SEED"), "X's status request did not carry Y's content"); + assert.ok(JSON.stringify(yStatusRow!.full).includes("BETA_Y_SEED"), "Y's status request carried Y's history"); + assert.ok(!JSON.stringify(yStatusRow!.full).includes("ALPHA_X_SEED"), "Y's status request did not carry X's content"); + + // Concurrent plain turns, same proxy, no cross-talk afterwards. + const [xFin, yFin] = await Promise.all([ + piRun(ctxX, "concurrent X tail", { resume: true }), + piRun(ctxY, "concurrent Y tail", { resume: true, extraEnv: yEnv }), + ]); + ok("X concurrent", xFin); + ok("Y concurrent", yFin); + const xTail = rows.filter((r) => r.headers["x-bili-plugin-conversation"] === xConv).at(-1)!; + const yTail = rows.filter((r) => r.headers["x-bili-plugin-conversation"] === yConv).at(-1)!; + assert.ok(JSON.stringify(xTail.full).includes("ALPHA_X_SEED"), "X tail carries X's own history"); + assert.ok(JSON.stringify(yTail.full).includes("BETA_Y_SEED"), "Y tail carries Y's own history"); + assert.ok(!JSON.stringify(xTail.full).includes("BETA_Y_SEED"), "concurrent turns did not mix X/Y histories"); + assert.ok(!JSON.stringify(yTail.full).includes("ALPHA_X_SEED"), "concurrent turns did not mix X/Y histories"); +}); diff --git a/tests/native-ws-intercept.test.ts b/tests/native-ws-intercept.test.ts new file mode 100644 index 0000000000..2586c40d4c --- /dev/null +++ b/tests/native-ws-intercept.test.ts @@ -0,0 +1,421 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import type { NativeInterceptState } from "../src/agent/native-intercept.ts"; +import { _resetForTest, codexResponsesWsTarget, installNativeWebSocketIntercept, nativeWebSocketInterceptInstalled, rewriteCodexResponsesWsUrl } from "../src/agent/native-ws-intercept.ts"; + +// Same @types/node quirk as in src: the listener callback type is not global. +type EventListener = (event: Event) => void; + +// ——— Fakes ———————————————————————————————————————————————————————————————— + +class FakeWs { + static CONNECTING = 0; + static OPEN = 1; + static CLOSING = 2; + static CLOSED = 3; + static instances: FakeWs[] = []; + /** When set, every construction throws — seam for the total-failure path. */ + static ctorFail = false; + readonly ctorArgs: unknown[]; + url: string; + readyState = 0; + bufferedAmount = 0; + protocol = ""; + extensions = ""; + binaryType = "blob"; + onopen: ((e: unknown) => void) | null = null; + onerror: ((e: unknown) => void) | null = null; + onclose: ((e: unknown) => void) | null = null; + onmessage: ((e: unknown) => void) | null = null; + sent: string[] = []; + closed = false; + private listeners = new Map>(); + + constructor(...ctorArgs: unknown[]) { + if (FakeWs.ctorFail) throw new TypeError("FakeWs construction disabled (test seam)"); + this.ctorArgs = ctorArgs; + this.url = String(ctorArgs[0] ?? ""); + FakeWs.instances.push(this); + } + + addEventListener(type: string, listener: EventListener | null, options?: { once?: boolean } | boolean): void { + if (listener === null) return; + const once = typeof options === "object" && options !== null ? options.once === true : options === true; + const list = this.listeners.get(type) ?? []; + list.push({ fn: listener, once }); + this.listeners.set(type, list); + } + + removeEventListener(type: string, listener: EventListener | null): void { + const list = this.listeners.get(type); + if (list !== undefined) { + const at = list.findIndex((entry) => entry.fn === listener); + if (at >= 0) list.splice(at, 1); + } + } + + dispatchEvent(event: Event): boolean { + for (const entry of [...(this.listeners.get(event.type) ?? [])]) entry.fn.call(this, event); + return true; + } + + send(data: unknown): void { + this.sent.push(String(data)); + } + + close(): void { + this.closed = true; + this.readyState = FakeWs.CLOSING; + } + + emit(type: "open" | "error" | "close" | "message"): void { + const event = new Event(type); + this.dispatchEvent(event); + const handler = this[`on${type}`] as ((e: unknown) => void) | null | undefined; + handler?.(event); + } +} + +type GlobalRecord = Record; + +function savedDescriptor(): PropertyDescriptor | undefined { + return Object.getOwnPropertyDescriptor(globalThis, "WebSocket"); +} + +function setGlobal(value: unknown): void { + (globalThis as GlobalRecord).WebSocket = value; +} + +function WsCtor(): new (...args: unknown[]) => T { + return (globalThis as GlobalRecord).WebSocket as unknown as new (...args: unknown[]) => T; +} + +async function withPatchedGlobal(fn: () => Promise): Promise { + const saved = savedDescriptor(); + _resetForTest(); + if (saved !== undefined) Object.defineProperty(globalThis, "WebSocket", saved); + FakeWs.instances.length = 0; + setGlobal(FakeWs); + try { + return await fn(); + } finally { + _resetForTest(); + if (saved !== undefined) Object.defineProperty(globalThis, "WebSocket", saved); + } +} + +function pendingState(): NativeInterceptState & { readyResolve: (o: string | undefined) => void } { + let readyResolve!: (o: string | undefined) => void; + const ready = new Promise((resolve) => { + readyResolve = resolve; + }); + return { origin: undefined, ready, readyResolve }; +} + +const ORIGIN = "http://127.0.0.1:8787"; +const UPSTREAM_WS = "wss://chatgpt.com/backend-api/codex/responses"; +const REWRITTEN = `ws://127.0.0.1:8787/bili/https://chatgpt.com/backend-api/codex/responses`; +const tick = (): Promise => new Promise((resolve) => setImmediate(resolve)); + +// ——— Claim predicate / rewrite (pure) ———————————————————————————————————— + +test("codexResponsesWsTarget: claims the Codex Responses shape, normalized to http(s)", () => { + assert.equal(codexResponsesWsTarget("wss://chatgpt.com/backend-api/codex/responses"), "https://chatgpt.com/backend-api/codex/responses"); + assert.equal(codexResponsesWsTarget("ws://127.0.0.1:9999/backend-api/codex/responses"), "http://127.0.0.1:9999/backend-api/codex/responses"); + assert.equal(codexResponsesWsTarget("wss://chatgpt.com/backend-api/codex/responses?beta=1"), "https://chatgpt.com/backend-api/codex/responses?beta=1"); + assert.equal(codexResponsesWsTarget("wss://relay.example.com/v1/responses/"), "https://relay.example.com/v1/responses/"); + assert.equal(codexResponsesWsTarget(new URL("wss://chatgpt.com/backend-api/codex/responses")), "https://chatgpt.com/backend-api/codex/responses"); +}); + +test("codexResponsesWsTarget: rejects non-model, already-routed, control-plane and non-ws URLs", () => { + assert.equal(codexResponsesWsTarget("wss://example.com/socket.io?transport=websocket"), undefined); + assert.equal(codexResponsesWsTarget("wss://api.anthropic.com/v1/messages"), undefined); + assert.equal(codexResponsesWsTarget("wss://api.openai.com/v1/chat/completions"), undefined); + assert.equal(codexResponsesWsTarget("wss://bedrock-runtime.us-east-1.amazonaws.com/converse-stream"), undefined); + assert.equal(codexResponsesWsTarget("wss://127.0.0.1:8787/bili/https://chatgpt.com/backend-api/codex/responses"), undefined); + assert.equal(codexResponsesWsTarget("wss://127.0.0.1:8787/__acp/status"), undefined); + assert.equal(codexResponsesWsTarget("wss://127.0.0.1:8787/__bili/plugin/manifest"), undefined); + assert.equal(codexResponsesWsTarget("https://chatgpt.com/backend-api/codex/responses"), undefined); + assert.equal(codexResponsesWsTarget("not a url"), undefined); + assert.equal(codexResponsesWsTarget("wss://bad host/x/responses"), undefined); +}); + +test("rewriteCodexResponsesWsUrl: ws(s) proxy side, http(s) embedded upstream", () => { + assert.equal(rewriteCodexResponsesWsUrl("https://chatgpt.com/backend-api/codex/responses", ORIGIN), REWRITTEN); + assert.equal(rewriteCodexResponsesWsUrl("https://chatgpt.com/backend-api/codex/responses", `${ORIGIN}/`), REWRITTEN); + assert.equal(rewriteCodexResponsesWsUrl("https://h.example/v1/responses", "https://proxy.example"), "wss://proxy.example/bili/https://h.example/v1/responses"); +}); + +// ——— Install mechanics ————————————————————————————————————————————————————— + +test("install: idempotent, flagged, stable top across repeated loads", async () => { + await withPatchedGlobal(async () => { + const state: NativeInterceptState = { origin: ORIGIN, ready: Promise.resolve(ORIGIN) }; + assert.equal(nativeWebSocketInterceptInstalled(), false); + assert.equal(installNativeWebSocketIntercept(state), true); + assert.equal(nativeWebSocketInterceptInstalled(), true); + const first = (globalThis as GlobalRecord).WebSocket; + assert.equal(installNativeWebSocketIntercept(state), false); + assert.equal((globalThis as GlobalRecord).WebSocket, first); + }); +}); + +test("install: refuses when no global WebSocket exists", async () => { + const saved = savedDescriptor(); + _resetForTest(); + Object.defineProperty(globalThis, "WebSocket", { value: undefined, configurable: true, writable: true }); + try { + const state: NativeInterceptState = { origin: ORIGIN, ready: Promise.resolve(ORIGIN) }; + assert.equal(installNativeWebSocketIntercept(state), false); + assert.equal(nativeWebSocketInterceptInstalled(), false); + } finally { + _resetForTest(); + if (saved !== undefined) Object.defineProperty(globalThis, "WebSocket", saved); + } +}); + +test("statics and name are carried onto the wrapper", async () => { + await withPatchedGlobal(async () => { + const state: NativeInterceptState = { origin: ORIGIN, ready: Promise.resolve(ORIGIN) }; + installNativeWebSocketIntercept(state); + const top = (globalThis as GlobalRecord).WebSocket as unknown as { CONNECTING: number; OPEN: number; CLOSING: number; CLOSED: number; name: string }; + assert.deepEqual([top.CONNECTING, top.OPEN, top.CLOSING, top.CLOSED], [0, 1, 2, 3]); + assert.equal(top.name, "WebSocket"); + }); +}); + +// ——— Routing decisions ————————————————————————————————————————————————————— + +test("passthrough: non-model WS keeps URL and arguments verbatim", async () => { + await withPatchedGlobal(async () => { + const state: NativeInterceptState = { origin: ORIGIN, ready: Promise.resolve(ORIGIN) }; + installNativeWebSocketIntercept(state); + const init = { headers: { "sec-x": "1" }, protocols: ["a"] }; + const ws = new (WsCtor())("wss://example.com/devtools", init); + assert.ok(ws instanceof FakeWs); + assert.equal(ws.url, "wss://example.com/devtools"); + assert.equal(ws.ctorArgs[1], init); + }); +}); + +test("immediate rewrite: origin known → routed URL, args forwarded verbatim, evidence recorded", async () => { + await withPatchedGlobal(async () => { + const dispatched: Array<[string, string]> = []; + const state: NativeInterceptState = { + origin: ORIGIN, + ready: Promise.resolve(ORIGIN), + onDispatch: (url, action) => dispatched.push([url, action]), + }; + installNativeWebSocketIntercept(state); + const init = { headers: { "session-id": "sess-1", "x-bili-plugin": "pi" } }; + const ws = new (WsCtor())(UPSTREAM_WS, init); + assert.ok(ws instanceof FakeWs); + assert.equal(ws.url, REWRITTEN); + assert.equal(ws.ctorArgs[1], init); + assert.deepEqual(dispatched, [[REWRITTEN, "rewrite"]]); + assert.equal(state.routedOrigin, ORIGIN); + }); +}); + +test("already-routed /bili/ WS is never double-prefixed", async () => { + await withPatchedGlobal(async () => { + const state: NativeInterceptState = { origin: ORIGIN, ready: Promise.resolve(ORIGIN) }; + installNativeWebSocketIntercept(state); + const ws = new (WsCtor())(REWRITTEN, {}); + assert.equal(ws.url, REWRITTEN); + }); +}); + +test("takeoverGate refusal: unattributable connection goes direct", async () => { + await withPatchedGlobal(async () => { + const dispatched: Array<[string, string]> = []; + const state: NativeInterceptState = { + origin: ORIGIN, + ready: Promise.resolve(ORIGIN), + takeoverGate: () => false, + onDispatch: (url, action) => dispatched.push([url, action]), + }; + installNativeWebSocketIntercept(state); + const ws = new (WsCtor())(UPSTREAM_WS, {}); + assert.equal(ws.url, UPSTREAM_WS); + assert.deepEqual(dispatched, [["https://chatgpt.com/backend-api/codex/responses", "refused"]]); + }); +}); + +test("instanceof holds against both the wrapper and the underlying constructor", async () => { + await withPatchedGlobal(async () => { + const state: NativeInterceptState = { origin: ORIGIN, ready: Promise.resolve(ORIGIN) }; + installNativeWebSocketIntercept(state); + const top = (globalThis as GlobalRecord).WebSocket as unknown as new (...args: unknown[]) => unknown; + const ws = new top(UPSTREAM_WS, {}); + assert.ok(ws instanceof FakeWs); + assert.ok(ws instanceof top); + }); +}); + +// ——— Deferred shell (origin still settling) —————————————————————————————————— + +test("deferred: window mirrors the WebSocket API, then attaches to the routed connection", async () => { + await withPatchedGlobal(async () => { + const state = pendingState(); + installNativeWebSocketIntercept(state); + const top = (globalThis as GlobalRecord).WebSocket as unknown as new (...args: unknown[]) => FakeWs; + const init = { headers: { "session-id": "sess-deferred" } }; + const ws = new top(UPSTREAM_WS, init) as unknown as { + readyState: number; + url: string; + send: (d: unknown) => void; + close: () => void; + addEventListener: (t: string, f: EventListener) => void; + onopen: ((e: unknown) => void) | null; + binaryType: string; + }; + assert.ok(!(ws instanceof FakeWs), "shell stands in before the origin settles"); + assert.equal(ws.readyState, 0); + assert.equal(ws.url, UPSTREAM_WS); + assert.throws(() => ws.send("early"), (err: Error) => err.name === "InvalidStateError"); + const opens: Array<"event" | "onopen"> = []; + ws.addEventListener("open", (() => { opens.push("event"); }) as EventListener); + ws.onopen = () => { opens.push("onopen"); }; + ws.binaryType = "arraybuffer"; + state.readyResolve(ORIGIN); + await tick(); + await tick(); + const [real] = FakeWs.instances; + assert.equal(real.url, REWRITTEN); + assert.equal(real.ctorArgs[1], init); + assert.equal(ws.readyState, 0); + assert.equal(real.binaryType, "arraybuffer"); + real.emit("open"); + assert.deepEqual(opens, ["event", "onopen"]); + }); +}); + +test("deferred: close() during the window cancels and emits an abnormal close frame", async () => { + await withPatchedGlobal(async () => { + const state = pendingState(); + installNativeWebSocketIntercept(state); + const top = (globalThis as GlobalRecord).WebSocket as unknown as new (...args: unknown[]) => { + readyState: number; + close: () => void; + addEventListener: (t: string, f: EventListener) => void; + }; + const ws = new top(UPSTREAM_WS, {}); + const closes: Array = []; + ws.addEventListener("close", ((e: { code?: number }) => { closes.push(e.code ?? -1); }) as EventListener); + ws.close(); + assert.equal(ws.readyState, 2, "CLOSING while cancelled"); + state.readyResolve(ORIGIN); + await tick(); + await tick(); + assert.equal(FakeWs.instances.length, 0, "no connection was ever opened"); + assert.deepEqual(closes, [1006]); + }); +}); + +test("deferred: close() during the window reaches onclose property handlers", async () => { + await withPatchedGlobal(async () => { + const state = pendingState(); + installNativeWebSocketIntercept(state); + const top = (globalThis as GlobalRecord).WebSocket as unknown as new (...args: unknown[]) => { + readyState: number; + close: () => void; + onclose: EventListener | null; + }; + const ws = new top(UPSTREAM_WS, {}); + const closes: Array = []; + ws.onclose = ((e: { code?: number }) => { closes.push(e.code ?? -1); }) as EventListener; + ws.close(); + assert.equal(ws.readyState, 2, "CLOSING while cancelled"); + state.readyResolve(ORIGIN); + await tick(); + await tick(); + assert.equal(FakeWs.instances.length, 0, "no connection was ever opened"); + assert.deepEqual(closes, [1006], "the on* property handler received the abnormal close frame"); + }); +}); + +test("deferred: total construct failure surfaces error + close instead of an unhandled rejection", async () => { + await withPatchedGlobal(async () => { + const state = pendingState(); + installNativeWebSocketIntercept(state); + const top = (globalThis as GlobalRecord).WebSocket as unknown as new (...args: unknown[]) => { + readyState: number; + addEventListener: (t: string, f: EventListener) => void; + }; + const ws = new top(UPSTREAM_WS, {}); + const errors: Array = []; + const closes: Array = []; + ws.addEventListener("error", ((e: { error?: unknown }) => { errors.push(e.error); }) as EventListener); + ws.addEventListener("close", ((e: { code?: number }) => { closes.push(e.code ?? -1); }) as EventListener); + FakeWs.ctorFail = true; + try { + state.readyResolve(ORIGIN); + await tick(); + await tick(); + assert.equal(FakeWs.instances.length, 0, "no connection was constructed"); + assert.ok(errors.length === 1 && errors[0] instanceof TypeError, "error event carries the constructor failure"); + assert.deepEqual(closes, [1006], "abnormal close follows the error"); + assert.equal(ws.readyState, 0, "shell stays CONNECTING with no live socket"); + } finally { + FakeWs.ctorFail = false; + } + }); +}); + +test("deferred: a permanently absent proxy degrades DIRECT with the original URL", async () => { + await withPatchedGlobal(async () => { + const state = pendingState(); + installNativeWebSocketIntercept(state); + const top = (globalThis as GlobalRecord).WebSocket as unknown as new (...args: unknown[]) => FakeWs; + const ws = new top(UPSTREAM_WS, {}); + state.readyResolve(undefined); + await tick(); + await tick(); + const [real] = FakeWs.instances; + assert.equal(real.url, UPSTREAM_WS); + assert.equal(ws.url, UPSTREAM_WS); + }); +}); + +// ——— Third-party adoption ————————————————————————————————————————————————————— + +class FakeWs2 extends FakeWs {} + +test("adoption: a third-party constructor takes over as downstream, routing survives", async () => { + await withPatchedGlobal(async () => { + const state: NativeInterceptState = { origin: ORIGIN, ready: Promise.resolve(ORIGIN) }; + installNativeWebSocketIntercept(state); + setGlobal(FakeWs2); + const ws = new (WsCtor())(UPSTREAM_WS, {}); + assert.ok(ws instanceof FakeWs2); + assert.equal(ws.url, REWRITTEN); + }); +}); + +test("adoption: non-constructor replacements are refused, previous constructor kept", async () => { + await withPatchedGlobal(async () => { + const state: NativeInterceptState = { origin: ORIGIN, ready: Promise.resolve(ORIGIN) }; + installNativeWebSocketIntercept(state); + const kept = (globalThis as GlobalRecord).WebSocket; + setGlobal("junk" as unknown); + assert.equal((globalThis as GlobalRecord).WebSocket, kept); + const ws = new (kept as new (...args: unknown[]) => FakeWs)(UPSTREAM_WS, {}); + assert.equal(ws.url, REWRITTEN); + }); +}); + +// ——— Reset ————————————————————————————————————————————————————————————————————— + +test("_resetForTest restores the pre-install global and clears the flag", async () => { + const saved = savedDescriptor(); + _resetForTest(); + if (saved !== undefined) Object.defineProperty(globalThis, "WebSocket", saved); + setGlobal(FakeWs); + const state: NativeInterceptState = { origin: ORIGIN, ready: Promise.resolve(ORIGIN) }; + installNativeWebSocketIntercept(state); + _resetForTest(); + assert.equal(nativeWebSocketInterceptInstalled(), false); + assert.equal((globalThis as GlobalRecord).WebSocket, FakeWs); + if (saved !== undefined) Object.defineProperty(globalThis, "WebSocket", saved); +});