diff --git a/.github/workflows/ci-e2e-native-opencode.yml b/.github/workflows/ci-e2e-native-opencode.yml index 2d24b62a9a..bd949f24b6 100644 --- a/.github/workflows/ci-e2e-native-opencode.yml +++ b/.github/workflows/ci-e2e-native-opencode.yml @@ -54,3 +54,30 @@ jobs: name: e2e-native-opencode-artifacts path: tmp/e2e-artifacts if-no-files-found: ignore + + e2e-opencode-responses-ws: + name: OpenCode V2 Responses WebSocket ACP (zero tokens) + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 + cache: npm + - name: Install WebSocket-capable OpenCode V2 + run: npm i -g @opencode/cli@2.0.20 + - name: Install deps + run: npm ci + - name: Build + run: npm run build + - name: Run native Responses WebSocket ACP regression + env: + E2E_OC_BIN: opencode + run: ACP_TEST_E2E_OC_WS=1 node --import tsx --test tests/e2e/e2e-opencode-responses-ws.test.ts + - uses: actions/upload-artifact@v4 + if: failure() + with: + name: e2e-opencode-responses-ws-artifacts + path: /tmp/bili-oc-ws-e2e-* + if-no-files-found: ignore diff --git a/CLIENTS.md b/CLIENTS.md index 1b4d012d59..366f511ca4 100644 --- a/CLIENTS.md +++ b/CLIENTS.md @@ -478,6 +478,26 @@ healthy, requests go direct (uncompressed) with a one-time warning and recover automatically. Under a `bili opencode` launch this entry is skipped entirely (the launcher owns the proxy). +### OpenAI Responses WebSockets (V2) + +The V2 plugin also intercepts OpenAI `experimental.ws.handshake` requests. +Both legs use WebSocket: OpenCode → bili → the Responses upstream. This +includes API-key OpenAI and ChatGPT Pro/Plus browser/headless OAuth; the login +method does not select the transport. No OpenCode configuration rewrite or +new bili setting is required. The socket must originate locally and carry +the cooperative plugin identity; generic or unclaimed upgrades retain 426. + +ACP processing, native tools and usage accounting stay active. Client deltas +are expanded before compression. Upstream deltas are used only when the +processed history exactly extends the previous response; a fold starts a new +chain with full compressed input on the same socket. Ref tagging or other +history edits can also require full input, so connection reuse does not imply +every turn is incremental. Older hosts without the experimental hook must +use OpenCode's existing `providers.openai.settings.transport: "http"` setting. +Realtime, multiplexed concurrent responses, and remote WS clients are outside +this integration's scope. Verification: real OpenCode V2.0.20 with a local +Responses WS upstream, not live OpenAI/ChatGPT credentials. + ### Pure proxy (no plugin) Point the provider baseURL at the proxy like any other client: diff --git a/CLIENTS.zh-CN.md b/CLIENTS.zh-CN.md index 028be6b704..344eeaea75 100644 --- a/CLIENTS.zh-CN.md +++ b/CLIENTS.zh-CN.md @@ -139,6 +139,23 @@ HTTPS 走证书 MITM,HTTP 走临时 `opencode.json` 副本(`/bili/` 改写;JSONC 加载时插件自拉起自己的代理(健康的已有实例直接复用不重复起;父进程 pid 看门狗在 opencode 退出时收掉它),把模型流量路由到 `/bili/`,暴露与启动器模式相同的原生 bili 工具 —— 无固定端口、无环境变量、免启动器。退出:`BILI_NATIVE_OPENCODE=0`。若没有任何代理能拉到健康状态,请求直连(不压缩)并给一次性告警,之后自动恢复。在 `bili opencode` 启动下该条目整体跳过(代理归启动器管)。 +### OpenAI Responses WebSocket(V2) + +V2 插件也接管 OpenAI 的 `experimental.ws.handshake` 请求,两端均使用 +WebSocket:OpenCode → bili → Responses 上游。支持 OpenAI API Key 及 +ChatGPT Pro/Plus browser/headless OAuth;登录方式不决定传输方式。 +无需改写 OpenCode 配置或增加 bili 设置。首版只接受本机发起、携带原生 +插件身份的连接;普通或未接管的 WebSocket 升级仍立即返回 426。 + +ACP 处理、原生工具和用量统计保持生效。客户端增量先还原再压缩;只有 +处理后的历史确实延续上次响应时,上游才使用增量。压缩后在同一连接上 +发送完整压缩上下文,开始新的响应链。引用标签或其它历史变化也可能要求 +完整输入,因此复用连接不代表每轮都只发增量。不支持实验钩子的旧宿主 +需要使用 OpenCode 已有的 `providers.openai.settings.transport: "http"`。 +Realtime、同一连接的并发多路响应及远程 WS 客户端不在本次范围。 +验证使用真实 OpenCode V2.0.20 与本地 Responses WS 上游,未使用真实 +OpenAI/ChatGPT 凭据。 + ### 纯代理(无插件) 与其它客户端一样,把 provider baseURL 指向代理: diff --git a/docs/opencode-responses-websocket.md b/docs/opencode-responses-websocket.md new file mode 100644 index 0000000000..9fbba09bf0 --- /dev/null +++ b/docs/opencode-responses-websocket.md @@ -0,0 +1,110 @@ +# OpenCode V2 Responses WebSocket integration (#1844) + +## Approved scope + +The user authorized implementing the previously discussed two-ended WebSocket +ACP integration on 2026-10-01. OpenCode V2 connects to bili over WebSocket; +bili connects to the selected Responses upstream over WebSocket. This includes +the OpenAI API and ChatGPT OAuth/Codex endpoint. It does not implement Realtime, +generic opaque WebSocket passthrough, or change the user's OpenCode configuration. + +## Decision points + +- The V2 handshake hook routes supported Responses sockets through the existing + explicit-protocol tunnel, with plugin identity and model metadata. Other + protocols, disabled plugins, and older hosts retain their existing behavior. +- Only loopback, cooperative OpenCode Responses upgrades are admitted initially. + Destination admission reuses the existing tunnel guard. Unclaimed upgrades + still receive the existing immediate 426, independently of PR #1472. +- Responses frames enter the existing request pipeline through an in-process + HTTP-shaped envelope, not a network HTTP hop. An async-scoped fetch transport + executes model requests, retries, and preflight requests over upstream + WebSocket. Existing ACP policy, tool execution, usage accounting, and prose + filtering remain the single authority. Tool argument strings are never cleaned. +- Client and upstream continuation checkpoints are separate: the client sees + its original history and visible output; the upstream sees the ACP-processed + history. Incoming deltas are expanded before ACP. Outgoing deltas are used only + when the processed input extends the upstream checkpoint exactly. A fold or + other history change sends full processed input without previous_response_id. + This starts a new response chain without requiring a new socket. + Request-size logs marked `view=ws-expanded` describe reconstructed pipeline + envelopes, not incremental WebSocket wire bytes. +- Only completed responses establish continuation checkpoints. Disconnect, + cancellation, failed/incomplete responses, and unknown response IDs do not + fabricate successful history. Checkpoints and queued payloads are bounded. +- Both checkpoints accumulate `response.output_item.done` items by output index. + Empty, missing, or partial terminal `response.output` must not erase completed + tool calls and leave orphaned results on the next delta. Terminal items merge + by item ID; streaming frames remain unchanged. Debug logs report restored + checkpoint item counts without recording their contents. +- WS diagnostics identify the local socket and session, failure phase + (`handshake`, `await-first-event`, `stream`, or `dispatch`), numeric close code, + full/delta request mode, and checkpoint reset/recovery reason. They omit + authorization headers, URLs, close-reason text, payloads, and upstream error + messages. Normal lifecycle/recovery details use debug; failures use warn. +- No configuration field, environment variable, package version, persistence + format, or acp-kernel version is added or changed. Socket checkpoints are + connection-local; ACP persistence remains unchanged. The ws implementation is + a bundled build-time dependency, not an external runtime requirement. + +## Architecture: generic WebSocket bridge (#1467 phase-2 shell) + +The WebSocket interception is split into a protocol-independent shell and a +per-protocol codec: + +- `src/ws-bridge.ts` (shell) owns admission (loopback source, `x-bili-plugin` + lane marker, `x-bili-plugin-conversation` session header, tunnel-destination + guard), the upgrade handshake, per-connection bookkeeping, and shutdown + teardown. It knows nothing about any wire protocol. +- `src/responses-ws.ts` (codec) owns everything Responses-specific: frame + validation, the `previous_response_id` client history contract, the upstream + WebSocket transport with its continuation checkpoints, and the SSE response + sink. It is registered as `responsesCodec` in a codec table at the single + wiring site (`installWebSocketBridge(server, dispatch, log, [responsesCodec, + codexResponsesCodec])` in `src/server.ts`). + +A second wire protocol is a new codec file plus one table entry — no shell +changes. The shell contract is `WsBridgeCodec` (name, plugin marker, URL claim, +session factory) and `WsBridgeSession` (message/close/shutdown); sessions +receive the labeled logger, the upgraded peer, the upgrade request, the claimed +upstream URL, and the ACP pipeline entry (`dispatch`), and are expected to +rebuild protocol-shaped envelopes in-process. `tests/ws-bridge.test.ts` drives +the shell with a synthetic second codec to keep the codec table honest. + +The second real codec is already here: **`codexResponsesCodec`** claims codex +CLI's Responses-over-WebSocket dial (`/bili//responses`, conversation +header `session-id`, `stream:true` frames, per-connection prewarm probe with +`generate:false` relayed verbatim around the pipeline). It is prefix-lane: +no plugin marker on the wire, admission resting on loopback + conversation +header + tunnel guard — the same trust level as prefix-mode HTTP. Codex +replays the full history every turn (`previous_response_id` is never sent), +so the fold simply shrinks the next replay; see `tests/codex-responses-ws.test.ts`. + +Unknown protocols cannot be compressed (folding requires knowing where history +lives in the wire format); they stay on the #1472 transparent passthrough lane +and remain untouched by this bridge. + +## Verification + +Use random loopback ports and fake Responses WebSocket upstreams, with no real +credentials or model usage. Prove both directions stay on WebSocket, normal +continuation is incremental, a real ACP fold resets the upstream history, +tool argument payloads survive unchanged, usage reaches the existing session, +and successive tool calls/results still advance history after a fold even when +the terminal output is sparse, +and unknown upgrades retain 426. Verify upstream proxy routing, errors, +cancellation, reconnect, checkpoint misses, and session isolation. Exercise a +real OpenCode V2 binary with an isolated configuration and successful fake +WebSocket upstream; an unavailable socket that merely falls back to HTTP is +not acceptable evidence. Run typecheck, the full unit suite, build, and the +required Responses client E2E before submitting a human-reviewed PR. + +Failure-combination coverage includes tool arguments delivered before disconnect, +failed/incomplete terminal events, cancellation followed immediately by a new +connection with rotated synthetic credentials, overlapping creates, repeated +continuation/connection-limit errors with bounded retries, and handshake refusal +followed by recovery. The sparse-output real-fold test also chains a missing +checkpoint, an idle disconnect, and connection-limit rotation, asserting that +summaries survive and each completed call/result appears exactly once upstream. +These are transport/history assertions, not proof of exactly-once execution of +arbitrary host tools or of the real OAuth refresh lifecycle. diff --git a/docs/opencode-responses-websocket.zh-CN.md b/docs/opencode-responses-websocket.zh-CN.md new file mode 100644 index 0000000000..8b19aa5315 --- /dev/null +++ b/docs/opencode-responses-websocket.zh-CN.md @@ -0,0 +1,56 @@ +# OpenCode V2 Responses WebSocket 接入(#1844) + +## 已批准范围 + +用户于 2026-10-01 授权实现双端 WebSocket ACP 接入:OpenCode V2 到 bili、 +bili 到 OpenAI Responses 上游均使用 WebSocket,包括 ChatGPT OAuth/Codex +端点。不实现 Realtime、通用透明帧转发,也不修改用户现用 OpenCode 配置。 + +## 关键设计 + +- V2 握手钩子通过已有显式协议隧道路由连接,并附带原生插件身份与模型信息。 + 其它协议、退出插件的情形及旧宿主保留原行为。 +- 首版只接受本机、原生 OpenCode Responses 连接,复用既有隧道目的地安全 + 检查。未接管升级仍立即返回 426,与 #1472 的透明转发方案相互独立。 +- Responses 帧经进程内 HTTP 形状适配进入既有 ACP 请求流程,不产生网络 + HTTP 中间跳。异步作用域中的传输适配器把模型请求、预检和重试发送至上游 + WebSocket;既有 ACP 策略、工具执行、统计和正文过滤仍是唯一权威。 + 工具参数字符串不经过清理。 +- 客户端与上游分别保存连接级检查点。客户端增量先还原为完整历史;上游只有 + 在处理后输入确实延续其检查点时才发送增量。压缩或其它历史编辑后发送完整 + 处理后输入,不继续旧 previous_response_id,无需因此重开连接。 +- 引用标签也可能改变上一轮助手消息,因此复用连接不保证每轮都只发送增量。 +- 标记 `view=ws-expanded` 的请求大小日志描述还原后的处理视图,不代表增量 + WebSocket 实际传输字节数。 +- 只有成功完成的响应建立检查点。断线、取消、失败、未完成响应或未知响应 ID + 不伪造成功历史;检查点和缓冲均有大小限制。 +- 两侧检查点按输出索引累计 `response.output_item.done`,结束事件的 + `response.output` 为空、缺省或不完整时仍保留已完成工具调用,避免下轮增量的 + 工具结果失去对应调用。结束事件按 item ID 合并,流式帧保持原样。 + 调试日志仅记录检查点补齐的项目数量,不记录内容。 +- WS 诊断标明本地连接与会话、失败阶段(握手、等待首事件、流中或分派)、数值 + 关闭码、全量/增量请求模式,以及检查点重置和恢复原因。不记录认证头、URL、 + 关闭原因原文、请求内容或上游错误正文。正常生命周期与恢复使用 debug,失败 + 使用 warn。 +- 不增加或改变运行时配置字段、环境变量、包版本、持久化格式或 acp-kernel + 版本。ws 是构建时内联依赖,不增加外置运行时依赖。 + +## 验证 + +使用随机回环端口与假 Responses WebSocket 上游,不使用真实凭据或模型额度。 +证明双端 WebSocket、增量续接、实际 ACP 折叠后重建响应链、工具参数保真、 +用量统计以及未接管升级保留 426;覆盖代理出口、TLS、错误、取消、重连、 +检查点丢失与会话隔离。 +同时验证结束事件输出不完整时,压缩后的连续工具调用及结果仍能进入后续历史。 + +真实 OpenCode V2 二进制使用隔离配置,假上游必须成功建立 WebSocket,不能 +把连接失败后回退 HTTP 当作证据。交付运行类型检查、完整单测、构建及客户端 +端到端测试,并提交人工审查 PR;若真实 Responses 上游不可达,明确记录该 +验收缺口,不声称完成线上 OpenAI/ChatGPT 验证。 + +异常组合覆盖:工具参数交付后断线、失败/未完成终态、取消后立即以轮换的模拟 +凭据新建连接、重叠请求、续接/连接寿命错误连续发生时的重试上限,以及握手被拒 +后恢复。稀疏终态的真实压缩测试还连续注入检查点丢失、空闲断线和连接轮换, +确认摘要保留且每对已完成调用/结果在上游历史中仅出现一次。 +这些验证的是传输和历史,不代表任意宿主工具副作用的严格仅执行一次保证,也 +不代替真实 OAuth 刷新生命周期验证。 diff --git a/package-lock.json b/package-lock.json index 550b3592a9..882cafea8e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,6 +19,7 @@ "@types/node": "^22.0.0", "@types/node-forge": "^1.3.14", "@types/semver": "^7.7.1", + "@types/ws": "8.18.2", "acp-kernel": "0.0.101", "fzstd": "0.1.1", "jsonc-parser": "^3.3.1", @@ -29,7 +30,8 @@ "tsx": "^4.0.0", "typescript": "^5.5.0", "undici": "^7.29.0", - "verdaccio": "^6.10.4" + "verdaccio": "^6.10.4", + "ws": "8.22.0" }, "engines": { "node": ">=20" @@ -1568,6 +1570,16 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/ws": { + "version": "8.18.2", + "resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.18.2.tgz", + "integrity": "sha512-67MQl+fpWKVTT1NYdnmo3U4sc/xPo/zQBncVnI74qmQa0z/b+1g6iYqNmGCPbxO+zz2aklb08a0oHfegiVd0/w==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@verdaccio/auth": { "version": "8.1.3", "resolved": "https://registry.npmjs.org/@verdaccio/auth/-/auth-8.1.3.tgz", @@ -6637,6 +6649,28 @@ "dev": true, "license": "ISC" }, + "node_modules/ws": { + "version": "8.22.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.22.0.tgz", + "integrity": "sha512-Ydggc987+RO0AnWtZ/7Wq9FtNvcrL1b/RO0ud9mWjUPgDrsAAwQSF51sm2hm1XofbU/4jkpGEsLFsZZxU+1DOg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, "node_modules/xtend": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", diff --git a/package.json b/package.json index cea41d8d3e..72e7d851a2 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "@types/node": "^22.0.0", "@types/node-forge": "^1.3.14", "@types/semver": "^7.7.1", + "@types/ws": "8.18.2", "acp-kernel": "0.0.101", "fzstd": "0.1.1", "jsonc-parser": "^3.3.1", @@ -68,7 +69,8 @@ "tsx": "^4.0.0", "typescript": "^5.5.0", "undici": "^7.29.0", - "verdaccio": "^6.10.4" + "verdaccio": "^6.10.4", + "ws": "8.22.0" }, "engines": { "node": ">=20" diff --git a/src/agent/native-intercept.ts b/src/agent/native-intercept.ts index b6e90be57d..9e0211b906 100644 --- a/src/agent/native-intercept.ts +++ b/src/agent/native-intercept.ts @@ -267,7 +267,7 @@ function isBiliControlUrl(url: string): boolean { * done) but DOES stamp plugin headers on them. */ export function routedBiliModelUrl(url: string): string | undefined { if (!/^https?:\/\//i.test(url) || url.includes("/__bili/") || url.includes("/__acp/")) return undefined; - const m = /^https?:\/\/[^/]+\/bili\/(https?:\/.+)$/i.exec(url); + const m = /^https?:\/\/[^/]+\/bili\/(?:responses\/)?(https?:\/.+)$/i.exec(url); if (m === null) return undefined; return isModelApiUrl(m[1]) ? m[1] : undefined; } diff --git a/src/agent/opencode-v2.ts b/src/agent/opencode-v2.ts index 8d91dfab9b..a596f953d0 100644 --- a/src/agent/opencode-v2.ts +++ b/src/agent/opencode-v2.ts @@ -72,6 +72,8 @@ export interface V2HttpRequestEvent { agent?: unknown; model?: { providerID?: unknown; id?: unknown }; request?: { url?: unknown; headers?: V2Headers | null }; + url?: unknown; + headers?: Record; } interface V2ToolEditor { @@ -333,6 +335,29 @@ export function createOpencodeV2Setup(options: OpencodeV2SetupOptions = {}): (ct const hookReg = await ctx.session?.hook?.("http.request", httpRequestHook); if (hookReg) registrations.push(hookReg); + try { + const wsReg = await ctx.session?.hook?.("experimental.ws.handshake", async (e) => { + if (pluginDisabled() || e.model?.providerID !== "openai" || typeof e.url !== "string" || !/^wss?:\/\//.test(e.url)) return; + const httpUrl = e.url.replace(/^ws/, "http"); + if (!new URL(httpUrl).pathname.endsWith("/responses")) return; + const requestEvent: V2HttpRequestEvent = { sessionID: e.sessionID, agent: e.agent, model: e.model, request: new Request(httpUrl, { headers: e.headers }) }; + await httpRequestHook(requestEvent); + const pinned = proxyBaseFromUrl(typeof requestEvent.request?.url === "string" ? requestEvent.request.url : undefined); + if (pinned && !state.proxyBase) { + state.proxyBase = pinned; + refreshWindows(ctx, state); + stampHeaders(requestEvent); + } + const routed = requestEvent.request; + if (!state.proxyBase || typeof routed?.url !== "string" || !routed.url.startsWith(`${state.proxyBase}/bili/`)) return; + e.url = routed.url.replace("/bili/http", "/bili/responses/http").replace(/^http/, "ws"); + e.headers = Object.fromEntries((routed as Request).headers.entries()); + }); + if (wsReg) registrations.push(wsReg); + } catch { + console.warn("[bili-opencode] WebSocket handshake hook unavailable; Responses WebSocket traffic is not intercepted by this host"); + } + const toolReg = await ctx.tool?.transform?.((editor) => { for (const t of V2_BILI_TOOLS) { editor.add({ diff --git a/src/agent/shared.ts b/src/agent/shared.ts index 7d68dc1d66..3112044541 100644 --- a/src/agent/shared.ts +++ b/src/agent/shared.ts @@ -32,7 +32,7 @@ export function proxyBaseFromUrl(baseUrl: string | undefined): string | undefine const segments = url.pathname.split("/").filter((s) => s.length > 0); if (segments[0] !== "bili") return undefined; const rest = url.pathname.slice(url.pathname.indexOf("bili") + "bili".length); - if (!/^\/https?:\/\//.test(rest)) return undefined; + if (!/^\/(?:responses\/)?https?:\/\//.test(rest)) return undefined; return `${url.protocol}//${url.host}`; } catch { return undefined; diff --git a/src/fetch-transport.ts b/src/fetch-transport.ts new file mode 100644 index 0000000000..2ee9757e6b --- /dev/null +++ b/src/fetch-transport.ts @@ -0,0 +1,14 @@ +import { AsyncLocalStorage } from "node:async_hooks"; +import type { FetchOptions } from "./fetch-util.js"; + +export type FetchTransport = (url: string, options: FetchOptions) => Promise; + +const transports = new AsyncLocalStorage(); + +export function withFetchTransport(transport: FetchTransport, run: () => T): T { + return transports.run(transport, run); +} + +export function currentFetchTransport(): FetchTransport | undefined { + return transports.getStore(); +} diff --git a/src/fetch-util.ts b/src/fetch-util.ts index 1041bb9394..c585c374d1 100644 --- a/src/fetch-util.ts +++ b/src/fetch-util.ts @@ -1,4 +1,5 @@ import { Agent } from "undici"; +import { currentFetchTransport } from "./fetch-transport.js"; import { classifyUpstreamFailure, isFailFastUpstreamKind } from "./upstream-fail.js"; /** HTTP robustness helpers for the proxy. @@ -186,7 +187,10 @@ export async function fetchWithTimeout( // which structurally conflicts with the `undici` package's exported // Dispatcher — but at runtime they're the same thing. Assert to the // concrete RequestInit type (no `as any`) to satisfy the call site. - const raw = await fetch(url, finalOpts as RequestInit) as Response; + const transport = currentFetchTransport(); + const raw = transport + ? await transport(url, finalOpts) + : await fetch(url, finalOpts as RequestInit) as Response; if (raw.body) { // Wrap the body so each chunk re-arms the timer (idle timeout); carry // status/headers onto a fresh Response so callers see an identical shape. diff --git a/src/responses-ws.ts b/src/responses-ws.ts new file mode 100644 index 0000000000..c1b903e972 --- /dev/null +++ b/src/responses-ws.ts @@ -0,0 +1,537 @@ +import http from "node:http"; +import { WebSocket as UpstreamWebSocket, type Dispatcher } from "undici"; +import WebSocket from "ws"; +import { MAX_REQUEST_BYTES, type FetchOptions } from "./fetch-util.js"; +import { withFetchTransport } from "./fetch-transport.js"; +import { connectionNamedHeaders, UPSTREAM_HOP_HEADERS } from "./server/headers.js"; +import { normalizeSseLineEndings } from "./sse-util.js"; +import type { WsBridgeCodec, WsBridgeContext, WsBridgeSession } from "./ws-bridge.js"; + +type JsonObject = Record; +type DiagnosticLog = (level: "debug" | "warn", message: string) => void; + +function closeCode(event: Event): number | "none" { + return "code" in event && typeof event.code === "number" ? event.code : "none"; +} + +function object(value: unknown): value is JsonObject { + return value !== null && typeof value === "object" && !Array.isArray(value); +} + +function canonical(value: unknown): string { + if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`; + if (object(value)) return `{${Object.keys(value).filter(k => value[k] !== undefined).sort().map(k => `${JSON.stringify(k)}:${canonical(value[k])}`).join(",")}}`; + return JSON.stringify(value) ?? "null"; +} + +function comparable(item: unknown): unknown { + if (!object(item)) return item; + const result = { ...item }; + delete result.status; + if (!result.type && typeof result.role === "string") result.type = "message"; + return result; +} + +interface Checkpoint { + id: string; + request: JsonObject; + output: unknown[]; +} + +class ResponsesWsOutput { + private items = new Map(); + private bytes = 0; + + observe(frame: JsonObject): JsonObject | undefined { + if (frame.type === "response.created") { + this.items.clear(); + this.bytes = 0; + } + if (frame.type === "response.output_item.done" && object(frame.item) && Number.isSafeInteger(frame.output_index) && (frame.output_index as number) >= 0) { + const index = frame.output_index as number; + const bytes = Buffer.byteLength(JSON.stringify(frame.item)); + this.bytes += bytes - (this.items.get(index)?.bytes ?? 0); + if (this.bytes > MAX_REQUEST_BYTES) throw new Error("Responses WebSocket checkpoint buffer limit exceeded"); + this.items.set(index, { item: frame.item, bytes }); + } + if (frame.type !== "response.completed" || !object(frame.response)) return undefined; + // Terminal output can be sparse; retain items already delivered by the stream. + const response = frame.response; + const output = new Map([...this.items].map(([index, value]) => [index, value.item])); + const indexes = new Map([...this.items].flatMap(([index, value]) => typeof value.item.id === "string" ? [[value.item.id, index] as const] : [])); + if (Array.isArray(response.output)) { + response.output.forEach((item: unknown, index: number) => { + const slot = object(item) && typeof item.id === "string" ? indexes.get(item.id) : undefined; + output.set(slot ?? index, item); + }); + } + return { ...response, output: [...output].sort(([a], [b]) => a - b).map(([, item]) => item) }; + } +} + +export class ResponsesWsHistory { + private checkpoint?: Checkpoint; + + expand(frame: JsonObject): JsonObject { + const { type: _type, previous_response_id: previous, ...request } = frame; + if (typeof request.input === "string") request.input = [{ type: "message", role: "user", content: request.input }]; + if (!Array.isArray(request.input)) throw new Error("response.create requires an input array"); + if (previous !== undefined && previous !== null) { + if (previous !== this.checkpoint?.id) throw new Error("previous_response_not_found"); + const prior = this.checkpoint; + if (!prior || !Array.isArray(prior.request.input)) throw new Error("previous_response_not_found"); + return { ...prior.request, ...request, input: [...prior.request.input, ...prior.output, ...request.input] }; + } + return request; + } + + continuation(request: JsonObject): JsonObject { + const prior = this.checkpoint; + if (!prior || !Array.isArray(request.input) || !Array.isArray(prior.request.input)) return request; + const { input: _input, ...fields } = request; + const items = request.input; + const { input: _priorInput, ...priorFields } = prior.request; + if (canonical(fields) !== canonical(priorFields)) return request; + const baseline = [...prior.request.input, ...prior.output]; + if (items.length <= baseline.length || !baseline.every((item, i) => canonical(comparable(item)) === canonical(comparable(items[i])))) return request; + return { ...fields, input: items.slice(baseline.length), previous_response_id: prior.id }; + } + + commit(request: JsonObject, response: unknown): void { + if (!object(response) || response.status !== "completed" || typeof response.id !== "string" || !Array.isArray(response.output)) return; + if (Buffer.byteLength(JSON.stringify(request)) + Buffer.byteLength(JSON.stringify(response.output)) > MAX_REQUEST_BYTES) { + this.clear(); + return; + } + this.checkpoint = { id: response.id, request, output: response.output }; + } + + clear(): void { + this.checkpoint = undefined; + } +} + +// The pipeline emits JSON bodies as strings (prepare* re-serialization) or +// Buffers (#388 side passthrough forwards the inbound buffer verbatim) — +// normalize both here; a string-only contract 502s every side request. +function bodyToText(body: RequestInit["body"]): string | undefined { + if (typeof body === "string") return body; + if (Buffer.isBuffer(body)) return body.toString("utf8"); + if (body instanceof ArrayBuffer) return Buffer.from(body).toString("utf8"); + if (ArrayBuffer.isView(body)) return Buffer.from(body.buffer, body.byteOffset, body.byteLength).toString("utf8"); + return undefined; +} + +export class ResponsesWsUpstream { + private socket?: InstanceType; + private key?: string; + private history = new ResponsesWsHistory(); + private active?: { fail: (error: Error, reason?: string) => void }; + + constructor(private readonly log: DiagnosticLog = () => {}) {} + + private resetHistory(reason: string): void { + this.history.clear(); + this.log("debug", `upstream checkpoint reset reason=${reason}`); + } + + close(reason = "transport-close"): void { + const socket = this.socket; + this.socket = undefined; + this.key = undefined; + if (this.active) this.active.fail(new Error("Responses WebSocket closed"), reason); + else if (socket) this.resetHistory(reason); + else this.history.clear(); + if (socket && socket.readyState < UpstreamWebSocket.CLOSING) socket.close(); + } + + private async connect(url: string, options: FetchOptions): Promise> { + const headers = Object.fromEntries(new Headers(options.headers).entries()); + for (const key of Object.keys(headers)) if (UPSTREAM_HOP_HEADERS.has(key) || key.startsWith("sec-websocket-")) delete headers[key]; + const wsUrl = url.replace(/^http/, "ws"); + const key = `${wsUrl}:${canonical(headers)}`; + if (this.key === key && this.socket?.readyState === UpstreamWebSocket.OPEN) return this.socket; + this.close(this.key !== key ? "connection-key-changed" : "reconnect"); + this.log("debug", "upstream connecting phase=handshake"); + const socket = new UpstreamWebSocket(wsUrl, { headers, dispatcher: options.dispatcher as Dispatcher | undefined }); + this.socket = socket; + this.key = key; + await new Promise((resolve, reject) => { + const clean = (): void => { + socket.removeEventListener("open", opened); + socket.removeEventListener("error", failed); + socket.removeEventListener("close", failed); + options.signal?.removeEventListener("abort", aborted); + }; + const opened = (): void => { clean(); this.log("debug", "upstream connected phase=handshake"); resolve(); }; + const failed = (event: Event): void => { + clean(); + this.log("warn", `upstream failed phase=handshake event=${event.type} close_code=${closeCode(event)}`); + reject(new Error("Responses WebSocket handshake failed")); + }; + const aborted = (): void => { clean(); this.log("debug", "upstream aborted phase=handshake"); this.close("handshake-abort"); reject(new DOMException("Aborted", "AbortError")); }; + socket.addEventListener("open", opened, { once: true }); + socket.addEventListener("error", failed, { once: true }); + socket.addEventListener("close", failed, { once: true }); + if (options.signal?.aborted) aborted(); + else options.signal?.addEventListener("abort", aborted, { once: true }); + }); + return socket; + } + + async fetch(url: string, options: FetchOptions, rotateRetry = true): Promise { + const bodyText = bodyToText(options.body); + if (options.method !== "POST" || !new URL(url).pathname.endsWith("/responses") || bodyText === undefined) throw new Error("Unsupported request in Responses WebSocket transport"); + const parsed: unknown = JSON.parse(bodyText); + if (!object(parsed) || !Array.isArray(parsed.input)) throw new Error("Invalid Responses WebSocket request"); + const { stream, stream_options: _streamOptions, background: _background, previous_response_id: _previous, type: _type, ...body } = parsed; + const socket = await this.connect(url, options); + if (this.active) throw new Error("Responses WebSocket exchange already active"); + const request = this.history.continuation(body); + this.log("debug", `upstream request mode=${request.previous_response_id === undefined ? "full" : "delta"}`); + return new Promise((resolve, reject) => { + const output = new ResponsesWsOutput(); + let controller: ReadableStreamDefaultController | undefined; + let settled = false; + let ended = false; + let retryFull = request.previous_response_id !== undefined; + let receivedEvents = 0; + const encoder = new TextEncoder(); + const clean = (): void => { + ended = true; + socket.removeEventListener("message", message); + socket.removeEventListener("error", failed); + socket.removeEventListener("close", failed); + options.signal?.removeEventListener("abort", aborted); + this.active = undefined; + }; + const fail = (error: Error, reason = "transport-close"): void => { + if (ended) return; + this.resetHistory(reason); + clean(); + if (settled) controller?.error(error); + else reject(error); + }; + const failed = (event: Event): void => { + this.log("warn", `upstream failed phase=${receivedEvents > 0 ? "stream" : "await-first-event"} event=${event.type} close_code=${closeCode(event)} received_events=${receivedEvents}`); + fail(new Error("Responses WebSocket upstream disconnected before completion"), "upstream-disconnect"); + }; + const aborted = (): void => { + this.log("debug", `upstream aborted phase=${receivedEvents > 0 ? "stream" : "await-first-event"}`); + fail(new DOMException("Aborted", "AbortError"), "request-abort"); + this.close("request-abort"); + }; + const message = (event: { data: unknown }): void => { + if (ended) return; + try { + if (typeof event.data !== "string" || Buffer.byteLength(event.data) > MAX_REQUEST_BYTES) throw new Error("Invalid or oversized Responses WebSocket frame"); + const frame: unknown = JSON.parse(event.data); + if (!object(frame) || typeof frame.type !== "string") throw new Error("Invalid Responses WebSocket event"); + receivedEvents++; + const completed = output.observe(frame); + if (frame.type === "error" && !settled) { + const error = object(frame.error) ? frame.error : {}; + if (rotateRetry && error.code === "websocket_connection_limit_reached") { + this.log("debug", "upstream retry reason=connection-limit action=reconnect-full"); + clean(); + this.close("connection-limit"); + resolve(this.fetch(url, options, false)); + return; + } + if (retryFull && (error.code === "previous_response_not_found" || frame.status === 400 && (error.code === undefined || error.code === "invalid_request_error"))) { + retryFull = false; + this.resetHistory("continuation-rejected"); + this.log("debug", "upstream retry reason=continuation-rejected action=resend-full"); + socket.send(JSON.stringify({ ...body, type: "response.create" })); + return; + } + clean(); + this.resetHistory("upstream-error"); + const status = typeof frame.status === "number" && frame.status >= 400 && frame.status <= 599 ? frame.status : 400; + this.log("warn", `upstream rejected phase=await-first-event status=${status}`); + resolve(new Response(JSON.stringify(frame), { status, headers: { "content-type": "application/json" } })); + return; + } + if (stream !== true) { + if (frame.type === "response.completed" || frame.type === "response.failed" || frame.type === "response.incomplete") { + clean(); + if (completed) this.history.clear(); + else this.resetHistory(frame.type); + this.history.commit(body, completed); + resolve(new Response(JSON.stringify(completed ?? frame.response), { headers: { "content-type": "application/json" } })); + } else if (frame.type === "error") fail(new Error("Responses WebSocket upstream error"), "upstream-error"); + return; + } + if (!settled) { + settled = true; + const readable = new ReadableStream({ + start: c => { controller = c; }, + cancel: () => { fail(new DOMException("Aborted", "AbortError"), "response-cancel"); this.close("response-cancel"); }, + }, { highWaterMark: MAX_REQUEST_BYTES, size: chunk => chunk.byteLength }); + resolve(new Response(readable, { headers: { "content-type": "text/event-stream" } })); + } + if ((controller?.desiredSize ?? 0) < 0) throw new Error("Responses WebSocket upstream buffer limit exceeded"); + controller?.enqueue(encoder.encode(`event: ${frame.type}\ndata: ${event.data}\n\n`)); + if (frame.type === "response.completed" || frame.type === "response.failed" || frame.type === "response.incomplete" || frame.type === "error") { + clean(); + if (completed) this.history.clear(); + else this.resetHistory(String(frame.type)); + this.history.commit(body, completed); + controller?.close(); + } + } catch (error) { + fail(error instanceof Error ? error : new Error("Invalid Responses WebSocket event"), "invalid-event"); + this.close("invalid-event"); + } + }; + this.active = { fail }; + socket.addEventListener("message", message); + socket.addEventListener("error", failed); + socket.addEventListener("close", failed); + options.signal?.addEventListener("abort", aborted, { once: true }); + if (options.signal?.aborted) aborted(); + else { + try { socket.send(JSON.stringify({ ...request, type: "response.create" })); } + catch (error) { fail(error instanceof Error ? error : new Error("Responses WebSocket send failed"), "send-failed"); } + } + }); + } +} + +function errorFrame(code: string, message: string, status = 400): string { + return JSON.stringify({ type: "error", status, error: { type: "invalid_request_error", code, message } }); +} + +class ResponsesWsResponse extends http.ServerResponse { + private ended = false; + private sent = false; + private buffer = ""; + private readonly decoder = new TextDecoder(); + private readonly output = new ResponsesWsOutput(); + response?: JsonObject; + restoredOutputItems = 0; + terminal = false; + + constructor(req: http.IncomingMessage, private readonly peer: WebSocket) { + super(req); + Object.defineProperty(this, "headersSent", { get: () => this.sent }); + Object.defineProperty(this, "writableEnded", { get: () => this.ended }); + Object.defineProperty(this, "socket", { get: () => req.socket }); + } + + override write(chunk: string | Uint8Array, callback?: (error?: Error | null) => void): boolean; + override write(chunk: string | Uint8Array, encoding: BufferEncoding, callback?: (error?: Error | null) => void): boolean; + override write(chunk: string | Uint8Array, encoding?: BufferEncoding | ((error?: Error | null) => void), callback?: (error?: Error | null) => void): boolean { + this.sent = true; + const cb = typeof encoding === "function" ? encoding : callback; + this.buffer += typeof chunk === "string" ? chunk : this.decoder.decode(chunk, { stream: true }); + if (this.buffer.length > MAX_REQUEST_BYTES) throw new Error("Responses WebSocket output buffer limit exceeded"); + this.buffer = normalizeSseLineEndings(this.buffer); + let index: number; + while ((index = this.buffer.indexOf("\n\n")) >= 0) { + const block = this.buffer.slice(0, index); + this.buffer = this.buffer.slice(index + 2); + const data = block.split("\n").filter(line => line.startsWith("data:")).map(line => line.slice(5).trimStart()).join("\n"); + if (!data || data === "[DONE]") continue; + const frame: unknown = JSON.parse(data); + if (object(frame)) { + const completed = this.output.observe(frame); + if (completed) { + this.response = completed; + const terminal = object(frame.response) && Array.isArray(frame.response.output) ? frame.response.output.length : 0; + this.restoredOutputItems = (completed.output as unknown[]).length - terminal; + } + if (["response.completed", "response.failed", "response.incomplete", "error"].includes(String(frame.type))) this.terminal = true; + } + if (this.peer.readyState === WebSocket.OPEN) this.peer.send(data, { binary: false }); + } + cb?.(); + if (this.peer.bufferedAmount > MAX_REQUEST_BYTES) { + this.peer.close(1013, "Output buffer limit exceeded"); + this.destroy(); + } + return true; + } + + override end(callback?: () => void): this; + override end(chunk: unknown, callback?: () => void): this; + override end(chunk: unknown, encoding: BufferEncoding, callback?: () => void): this; + override end(chunk?: unknown, encoding?: BufferEncoding | (() => void), callback?: () => void): this { + if (this.ended) return this; + if (typeof chunk === "string" || chunk instanceof Uint8Array) this.write(chunk); + if (!this.terminal && this.peer.readyState === WebSocket.OPEN) { + let details: unknown; + try { details = JSON.parse(this.buffer); } catch { details = undefined; } + this.peer.send(object(details) + ? JSON.stringify({ ...details, type: "error", status: this.statusCode >= 400 ? this.statusCode : 502 }) + : errorFrame("incomplete_response", "Responses exchange ended without a terminal event", 502)); + } + this.ended = true; + this.emit("finish"); + this.emit("close"); + const cb = typeof chunk === "function" ? chunk : typeof encoding === "function" ? encoding : callback; + if (typeof cb === "function") cb(); + return this; + } +} + + +// Responses protocol codec over the generic WebSocket bridge (ws-bridge.ts). +// Protocol-specific pieces live here: frame validation, the client-side +// history contract (previous_response_id expansion), the upstream WebSocket +// transport with its continuation checkpoints, and the SSE response sink that +// re-frames pipeline output for the client. + +class ResponsesWsSession implements WsBridgeSession { + private readonly transport: ResponsesWsUpstream; + private readonly history = new ResponsesWsHistory(); + private active?: ResponsesWsResponse; + private busy = false; + + constructor(private readonly context: WsBridgeContext) { + this.transport = new ResponsesWsUpstream(context.log); + context.log("info", "Responses socket connected (ACP request pipeline)"); + } + + onMessage(data: Buffer, binary: boolean): void { + const peer = this.context.peer; + if (binary) { peer.close(1003, "Responses requires text frames"); return; } + if (this.busy) { this.context.log("warn", "client rejected reason=response-in-progress"); peer.send(errorFrame("response_in_progress", "Only one active response is supported", 409)); return; } + let frame: unknown; + let body: JsonObject; + try { + frame = JSON.parse(data.toString()); + if (!object(frame) || frame.type !== "response.create") throw new Error("Unsupported Responses client event"); + const badStreamFlag = this.context.codec.allowStreamFlag ? (frame.stream !== undefined && frame.stream !== true) : frame.stream !== undefined; + if (frame.stream_id !== undefined || badStreamFlag || frame.background !== undefined || frame.stream_options !== undefined) throw new Error("Unsupported Responses transport options"); + body = { ...this.history.expand(frame), stream: true }; + if (Buffer.byteLength(JSON.stringify(body)) > MAX_REQUEST_BYTES) throw new Error("request_too_large"); + } catch (error) { + const code = error instanceof Error && ["previous_response_not_found", "request_too_large"].includes(error.message) ? error.message : "invalid_request"; + this.context.log("warn", `client rejected reason=${code}`); + peer.send(errorFrame(code, code === "previous_response_not_found" ? "Send full input without previous_response_id" : "Invalid or oversized response.create event", code === "request_too_large" ? 413 : 400)); + return; + } + this.busy = true; + const source = this.context.source; + const handshakeHeaders: Record = {}; + const connectionHeaders = connectionNamedHeaders(source.headers.connection); + for (const [key, value] of Object.entries(source.headers)) { + if (!UPSTREAM_HOP_HEADERS.has(key) && !connectionHeaders.has(key) && !key.startsWith("sec-websocket-") && key !== "content-encoding") handshakeHeaders[key] = Array.isArray(value) ? value.join(", ") : String(value); + } + // Prewarm probes (empty input, generate:false) are transport-layer + // noise, not conversation turns. Codex sends one on every new + // connection and expects only a terminal event back. Routing them + // through the ACP pipeline would normalize the empty input into a + // synthesized message (#1862) and record a phantom turn in the + // kernel's ref sequence, shifting every later fold anchor; injecting + // tools and compression prompts into a no-generation request is pure + // byte inflation. Forward verbatim on the upstream transport. + if (Array.isArray(body.input) && body.input.length === 0 && body.generate === false) { + void (async () => { + try { + const response = await this.transport.fetch(this.context.upstreamUrl, { method: "POST", headers: { ...handshakeHeaders, "content-type": "application/json" }, body: JSON.stringify(body) }); + if (response.body === null) { + peer.send(await response.text()); + return; + } + const reader = response.body.getReader(); + const decoder = new TextDecoder(); + let buffer = ""; + for (;;) { + const { done, value } = await reader.read(); + if (done) break; + buffer += decoder.decode(value, { stream: true }); + for (;;) { + const index = buffer.indexOf("\n\n"); + if (index < 0) break; + const block = buffer.slice(0, index); + buffer = buffer.slice(index + 2); + for (const line of block.split("\n")) if (line.startsWith("data: ")) peer.send(line.slice(6)); + } + } + } catch { + this.context.log("warn", "probe relay failed"); + peer.send(errorFrame("invalid_request", "Probe relay failed", 502)); + } finally { + this.active = undefined; + this.busy = false; + } + })(); + return; + } + const req = new http.IncomingMessage(source.socket); + req.complete = true; + req.method = "POST"; + req.url = source.url; + Object.assign(req.headers, handshakeHeaders); + req.headers["content-type"] = "application/json"; + req.push(Buffer.from(JSON.stringify(body))); + req.push(null); + const res = new ResponsesWsResponse(req, peer); + res.on("error", () => {}); + this.active = res; + void withFetchTransport((url, options) => this.transport.fetch(url, options), async () => { + try { + await this.context.dispatch(req, res); + if (res.response) { + this.history.commit(body, res.response); + if (res.restoredOutputItems > 0) this.context.log("debug", `checkpoint restored ${res.restoredOutputItems} streamed output item(s) absent from terminal output`); + } + } catch { + this.context.log("warn", `exchange failed phase=dispatch terminal=${res.terminal}`); + if (!res.writableEnded) res.end(); + } finally { + this.active = undefined; + this.busy = false; + } + }); + } + + onClose(code: number): void { + this.context.log("debug", `client closed phase=${this.busy ? "active" : "idle"} close_code=${code}`); + this.active?.destroy(); + this.transport.close("client-close"); + this.history.clear(); + this.context.log("debug", "client checkpoint reset reason=client-close"); + } + + shutdown(reason: string): void { + this.transport.close(reason); + this.history.clear(); + this.active?.destroy(); + } +} + +export const responsesCodec: WsBridgeCodec = { + name: "responses-ws", + pluginMarker: "opencode", + matchUpgrade: url => /^\/bili\/responses\/(https?:\/\/.*\/responses(?:\?.*)?)$/.exec(url ?? "")?.[1], + createSession: context => new ResponsesWsSession(context), +}; + +// Codex CLI (0.147+, provider `supports_websockets = true`, feature flag +// `responses_websockets`) swaps its HTTP SSE lane for a WebSocket that speaks +// the same Responses event vocabulary, one JSON text frame per event, with +// sequential `response.create` requests over one connection (first a probe +// with `input: []` + `generate: false`, then one full-history-replay request +// per turn — codex never sends previous_response_id). Captured from a real +// 0.147.0 client: handshake carries `openai-beta: responses_websockets=…`, +// `session-id`/`thread-id`, and `x-codex-turn-metadata`; the body carries +// `store: false`, `stream: true`, `include: [reasoning.encrypted_content]`, +// `prompt_cache_key`, and `client_metadata`, all of which ride the envelope +// unchanged. Admission is prefix-lane (no plugin marker): the path is the +// ordinary prefix-mode shape `/bili//responses` — disjoint from the +// opencode plugin lane above — keyed on codex's own `session-id` header, so +// the pipeline resolves the same conversation the HTTP lane would. A foreign +// client hitting this shape gets a protocol error frame, never silent +// corruption, and the tunnel guard still gates the upstream. +export const codexResponsesCodec: WsBridgeCodec = { + name: "codex-responses-ws", + conversationHeader: "session-id", + matchUpgrade: url => /^\/bili\/(https?:\/\/.*\/responses(?:\?.*)?)$/.exec(url ?? "")?.[1], + // Captured codex_cli 0.147.0 frames always carry stream:true; the + // response.create body is otherwise the Responses API shape verbatim. + allowStreamFlag: true, + createSession: context => new ResponsesWsSession(context), +}; diff --git a/src/server.ts b/src/server.ts index 4da02db108..338c7e5976 100644 --- a/src/server.ts +++ b/src/server.ts @@ -129,6 +129,9 @@ import { applyOutputSteering, applyOutputSteeringJson } from "./output-steering. import { bodyDumpEnabled, getUnrecognizedPathStats, isModelDiscoveryPath, logDumpFailure, logUnrecognizedPath } from "./server/observability.js"; import { BILI_HOP_HEADER, anthropicBetaContextWindow, capRegistryWindowByStandard, expandedContextSuffixWindow, LAUNCHER_MODEL_WINDOWS, LAUNCHER_MODEL_MAX_OUTPUTS, launcherContextWindow, launcherMaxOutput, parseLauncherModelWindows, windowSourceLogged } from "./server/context-window.js"; import { buildForwardHeaders, connectionNamedHeaders, NO_IDENTITY_MESSAGE, RESPONSE_ONLY_STRIP_HEADERS, safeSessionId, UPSTREAM_HOP_HEADERS } from "./server/headers.js"; +import { installWebSocketBridge } from "./ws-bridge.js"; +import { codexResponsesCodec, responsesCodec } from "./responses-ws.js"; +import { currentFetchTransport } from "./fetch-transport.js"; import { isSideRequest, outputBudgetField, restoreOutputBudget, SIDE_REQUEST_MAX_TOKENS, sideRequestGuard, stripLeakedBiliTools } from "./server/side-request.js"; import { dshCompactionRefusal, isDshCompactionCall } from "./server/dsh-compaction-guard.js"; import { clampOutgoingOutput, countSystemAndToolsTokens, emergencyNudge, estimateInputTokens, estimateWireOverhead, projectThinkingMass } from "./server/budget.js"; @@ -431,7 +434,7 @@ export async function startServer(opts: ProxyOptions): Promise { // as the context-window source for zero-config `/p/` routes that have no // per-model config. A miss falls back to the prefix table + default. void loadRegistry(); - const server = http.createServer(async (req, res) => { + const dispatch = async (req: http.IncomingMessage, res: http.ServerResponse): Promise => { armRequestWatchdog(req, res, log); const connRec = connRecords.get(req.socket); if (connRec) { @@ -461,13 +464,20 @@ export async function startServer(opts: ProxyOptions): Promise { res.end(); } } - }); - // Bili does not support WebSocket. An explicit 'upgrade' listener is + }; + const server = http.createServer(dispatch); + // Generic WebSocket bridge: protocol codecs claim upgrades here (#1467 + // phase-2 shell); the Responses codec is the first (and currently only) + // entry. Unclaimed upgrades still fall through to the 426 contract below. + const wsUpgrade = installWebSocketBridge(server, dispatch, log, [responsesCodec, codexResponsesCodec]); + // Unclaimed upgrades retain the immediate HTTP fallback contract. + // An explicit 'upgrade' listener is // required: without one Node's behavior is version-dependent (some // versions destroy the socket with no response), delaying clients with // built-in fast-fallback (e.g. Codex) that need a clean 426 to switch to // HTTP POST immediately. - server.on("upgrade", (req, socket) => { + server.on("upgrade", (req, socket, head) => { + if (wsUpgrade(req, socket, head)) return; log("info", `[ws] rejected ${req.method} ${maskUrlsInText(req.url ?? "")} host=${req.headers.host ? maskHostPortForLog(req.headers.host) : "?"} with 426`); socket.on("error", () => {}); // client may vanish mid-write; don't let ECONNRESET crash the process const body = JSON.stringify({ error: "WebSocket upgrades are not supported; use HTTP POST" }); @@ -2670,7 +2680,16 @@ async function handle( // run through the kernel. Veto on real history artifacts (detectAcpArtifacts // is history-scoped, never the top-level tools declarations), so a fresh // title-gen still demotes. Read-only; ordered BEFORE the mutating strip. + // #1467 WS lanes: envelopes rebuilt from a WebSocket upgrade carry the + // bridge's x-bili-ws-lane marker. The #1897 leak mechanism (an omp-style + // HTTP host registering bili's tools as extension tools) cannot produce + // them, the WS lane is that conversation's mainline, and a side + // passthrough cannot speak the lane's upstream transport — so the + // all-bili-tools demotion is vetoed for them (side requests on this lane + // are identified by the #1699 persona header instead). + const wsLaneEnvelope = req.headers["x-bili-ws-lane"] !== undefined; const demotedSide = !countTokens && !responsesCompact && protocol !== null && pluginMode + && !wsLaneEnvelope && detectAcpArtifacts(bodyBuffer, parsed) === null && stripLeakedBiliTools(parsed); // #546: restore a client-shrunk output budget BEFORE the side gate so a @@ -6022,7 +6041,9 @@ async function forward( // primary signal. The provider label is appended only for named routes — // zero-config requests have a single routing mode now, so the final // proxied URL is the only useful signal in the log. - log("info", `forward ${req.method} → ${maskUrlForLog(upstreamUrl)}`); + log("info", currentFetchTransport() + ? `forward WS → ${maskUrlForLog(upstreamUrl.replace(/^http/, "ws"))}` + : `forward ${req.method} → ${maskUrlForLog(upstreamUrl)}`); if (process.env.ACP_DEBUG && prepared) { const sid = prepared.session.id; const hdrKeys = Object.keys(req.headers); @@ -7411,7 +7432,8 @@ function formatBytes(n: number): string { function logRequestCost(log: (level: string, msg: string) => void, sessionId: string, msgs: number | null, inboundBytes: number, t0: number, outbound?: string | Buffer): void { const ms = Math.max(0, Math.round(performance.now() - t0)); const outboundField = outbound !== undefined ? `, outbound=${formatBytes(Buffer.byteLength(outbound))}` : ""; - log("info", `[${sessionId}] request: ${msgs ?? "?"} msgs, inbound=${formatBytes(inboundBytes)}${outboundField}, local=${ms}ms`); + const view = currentFetchTransport() ? ", view=ws-expanded" : ""; + log("info", `[${sessionId}] request: ${msgs ?? "?"} msgs, inbound=${formatBytes(inboundBytes)}${outboundField}, local=${ms}ms${view}`); } /** Thrown by readBody when the request body exceeds MAX_REQUEST_BYTES. diff --git a/src/ws-bridge.ts b/src/ws-bridge.ts new file mode 100644 index 0000000000..355fdc9324 --- /dev/null +++ b/src/ws-bridge.ts @@ -0,0 +1,146 @@ +import http from "node:http"; +import type { Duplex } from "node:stream"; +import { WebSocket, WebSocketServer } from "ws"; +import { MAX_REQUEST_BYTES } from "./fetch-util.js"; +import { checkTunnelDestination, tunnelAllowlistFromEnv } from "./tunnel-guard.js"; +import { isLoopbackAddress } from "./util.js"; + +// Generic WebSocket bridge (#1467 phase-2 shell). +// +// The bridge owns everything protocol-independent about intercepting a +// WebSocket client: admission (loopback source, bili plugin lane markers, +// tunnel-destination guard), the upgrade handshake, per-connection +// bookkeeping, and server-shutdown teardown. Each protocol rides as a codec +// that parses client frames into HTTP-shaped envelopes, runs them through the +// ordinary ACP request pipeline (`dispatch`), and carries the pipeline's +// outbound fetches back over a WebSocket transport. +// +// Compression is inherently protocol-aware: folding history requires knowing +// where the history lives in the wire format, so an unknown protocol cannot +// be compressed — it stays on the #1472 transparent passthrough lane. The +// codec table is the extension point: a new wire protocol is a new codec +// file plus one registration entry, with zero shell changes. + +export type WsBridgeLog = (level: "debug" | "info" | "warn", message: string) => void; + +export interface WsBridgeSession { + /** A client frame arrived. */ + onMessage(data: Buffer, binary: boolean): void; + /** The client socket closed; release session-owned resources. */ + onClose(code: number): void; + /** The server is shutting down; fail in-flight exchanges and drop upstream transports. */ + shutdown(reason: string): void; +} + +export interface WsBridgeContext { + /** Per-connection labeled logger (connection id + conversation id). */ + log: WsBridgeLog; + /** The upgraded client socket. */ + peer: WebSocket; + /** The original HTTP upgrade request (headers reused for envelope reconstruction). */ + source: http.IncomingMessage; + /** Upstream URL the codec extracted from the request path. */ + upstreamUrl: string; + /** The ACP request pipeline entry point (compression, preflight, tools, usage). */ + dispatch: (req: http.IncomingMessage, res: http.ServerResponse) => Promise; + /** The codec that claimed this upgrade (per-protocol transport policy). */ + codec: WsBridgeCodec; +} + +export interface WsBridgeCodec { + /** Log label and admission identity, e.g. "responses-ws". */ + name: string; + /** + * Required `x-bili-plugin` marker value (bili plugin lane identity). + * Prefix-lane codecs (no plugin marker on the wire) omit this and admit + * on their transport headers instead — same trust level as prefix-mode + * HTTP: loopback client, conversation header, tunnel-guarded upstream. + */ + pluginMarker?: string; + /** + * Header carrying the client's conversation id. Defaults to the plugin + * lane header; prefix-lane codecs point this at their client's own + * session header (e.g. codex sends `session-id`). + */ + conversationHeader?: string; + /** Return the upstream URL when this codec claims the upgrade path, else undefined. */ + matchUpgrade(url: string | undefined): string | undefined; + /** + * Clients of this codec may send an explicit `stream: true` flag inside + * response.create frames. Default keeps the strict policy — the flag must + * not appear at all (opencode frames carry no stream field; the lane adds + * its own). Codex always sends stream:true, so its codec opts in. + */ + readonly allowStreamFlag?: boolean; + /** Per-connection session factory; log the "connected" line from here. */ + createSession(context: WsBridgeContext): WsBridgeSession; +} + +export function installWebSocketBridge( + server: http.Server, + dispatch: (req: http.IncomingMessage, res: http.ServerResponse) => Promise, + log: (level: string, message: string) => void, + codecs: readonly WsBridgeCodec[], +): (req: http.IncomingMessage, socket: Duplex, head: Buffer) => boolean { + const wss = new WebSocketServer({ noServer: true, maxPayload: MAX_REQUEST_BYTES, perMessageDeflate: false }); + const sessions = new Set<{ session: WsBridgeSession; peer: WebSocket }>(); + let connectionId = 0; + const close = server.close.bind(server); + server.close = callback => { + const live = [...sessions]; + sessions.clear(); // exactly-once: the 'close' event backstop below must not re-run + for (const { session } of live) session.shutdown("server-close"); + for (const peer of wss.clients) peer.terminate(); + return close(callback); + }; + server.on("close", () => { + for (const { session } of sessions) session.shutdown("server-close"); + for (const peer of wss.clients) peer.terminate(); + wss.close(); + }); + return (source, socket, head) => { + const admitted = isLoopbackAddress(source.socket.remoteAddress); + const claim = admitted ? codecs.flatMap(candidate => { + const conversation = source.headers[candidate.conversationHeader ?? "x-bili-plugin-conversation"]; + const conversationId = typeof conversation === "string" ? conversation : ""; + if (conversationId.trim().length === 0) return []; + if (candidate.pluginMarker !== undefined && source.headers["x-bili-plugin"] !== candidate.pluginMarker) return []; + const upstream = candidate.matchUpgrade(source.url); + return upstream === undefined ? [] : [{ codec: candidate, upstream, conversationId }]; + })[0] : undefined; + if (!claim) return false; + const { codec, upstream, conversationId } = claim; + // Stamp the codec name onto the upgrade request BEFORE the session is + // created: codecs rebuild in-process HTTP envelopes from `source`, so + // every envelope inherits this marker and the request pipeline can tell + // a WS-lane envelope from an omp-style HTTP request (#1897 demotion + // must not fire here — this lane IS the conversation mainline, and its + // side requests are identified by the #1699 persona header instead). + source.headers["x-bili-ws-lane"] = codec.name; + void (async () => { + const verdict = await checkTunnelDestination(upstream, { selfPort: source.socket.localPort, clientLoopback: true, allowlist: tunnelAllowlistFromEnv() }); + if (!verdict.ok) { + socket.end("HTTP/1.1 403 Forbidden\r\nConnection: close\r\nContent-Length: 0\r\n\r\n"); + return; + } + if (socket.destroyed) return; + wss.handleUpgrade(source, socket, head, peer => { + const label = `[${codec.name}] [conn=${++connectionId}] [session=${JSON.stringify(conversationId.slice(0, 128))}]`; + const trace: WsBridgeLog = (level, message) => log(level, `${label} ${message}`); + const session = codec.createSession({ log: trace, peer, source, upstreamUrl: upstream, dispatch, codec }); + const entry = { session, peer }; + sessions.add(entry); + peer.on("error", () => {}); + peer.on("close", (code: number) => { + session.onClose(code); + sessions.delete(entry); + }); + peer.on("message", (data: Buffer, binary: boolean) => session.onMessage(data, binary)); + }); + })().catch(() => { + log("warn", `[${codec.name}] upgrade failed`); + if (!socket.destroyed) socket.end("HTTP/1.1 502 Bad Gateway\r\nConnection: close\r\nContent-Length: 0\r\n\r\n"); + }); + return true; + }; +} diff --git a/tests/codex-responses-ws.test.ts b/tests/codex-responses-ws.test.ts new file mode 100644 index 0000000000..adebd3b3e5 --- /dev/null +++ b/tests/codex-responses-ws.test.ts @@ -0,0 +1,244 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { on, once } from "node:events"; +import { randomUUID } from "node:crypto"; +import test from "node:test"; +import WebSocket, { WebSocketServer } from "ws"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { peekSession } from "../src/session.ts"; +import type { ProxyOptions } from "../src/config.ts"; + +type Item = Record; + +const testHome = fs.mkdtempSync(path.join(os.tmpdir(), "bili-codex-ws-home-")); +process.env.XDG_STATE_HOME = testHome; +process.env.XDG_DATA_HOME = testHome; +process.env.XDG_CACHE_HOME = testHome; + +const user = (text: string): Item => ({ type: "message", role: "user", content: [{ type: "input_text", text }] }); + +// Mirrors the captured codex 0.147.0 Responses-over-WebSocket client: the +// upgrade targets the ordinary prefix-mode path with codex's own transport +// headers (no plugin marker), and every request is a full `response.create` +// replay — a `generate: false` probe with empty input first, then one +// full-history frame per turn. Codex never sends previous_response_id. +async function fixture() { + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "bili-codex-ws-")); + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const rows: Array<{ request: Item; headers: http.IncomingHttpHeaders }> = []; + let httpRequests = 0; + let serial = 0; + let toolArguments: string | undefined; + const upstream = http.createServer((req, res) => { if (req.method === "POST") httpRequests++; res.writeHead(404).end(); }); + const wss = new WebSocketServer({ server: upstream }); + wss.on("connection", (peer, req) => { peer.on("error", e => console.error("[FAKE-UPSTREAM-PEER-ERR]", e.message)); peer.on("message", raw => { + const request = JSON.parse(raw.toString()) as Item; + rows.push({ request, headers: req.headers }); + const id = `resp_codex_${++serial}`; + const send = (type: string, fields: Item): void => peer.send(JSON.stringify({ type, ...fields })); + if (Array.isArray(request.input) && request.input.length === 0) { + // Probe: codex expects a terminal event, nothing more. + send("response.created", { response: { id, status: "in_progress", output: [] } }); + send("response.completed", { response: { id, status: "completed", output: [], usage: { input_tokens: 0, output_tokens: 1, total_tokens: 1 } } }); + return; + } + const item: Item = toolArguments !== undefined + ? { type: "function_call", id: `fc_${id}`, call_id: `call_${id}`, name: "acp_status", arguments: toolArguments, status: "completed" } + : { type: "message", id: `msg_${id}`, role: "assistant", status: "completed", content: [{ type: "output_text", text: `ok-${serial}` }] }; + send("response.created", { response: { id, status: "in_progress", output: [] } }); + send("response.output_item.added", { output_index: 0, item: { ...item, status: "in_progress", ...(toolArguments === undefined ? { content: [] } : { arguments: "" }) } }); + if (toolArguments !== undefined) { + send("response.function_call_arguments.delta", { item_id: item.id, output_index: 0, delta: toolArguments }); + send("response.function_call_arguments.done", { item_id: item.id, output_index: 0, arguments: toolArguments }); + } else { + send("response.output_text.delta", { item_id: item.id, output_index: 0, content_index: 0, delta: `ok-${serial}` }); + send("response.output_text.done", { item_id: item.id, output_index: 0, content_index: 0, text: `ok-${serial}` }); + } + send("response.output_item.done", { output_index: 0, item }); + send("response.completed", { response: { id, object: "response", status: "completed", output: [item], usage: { input_tokens: 500, output_tokens: 10, total_tokens: 510 } } }); + }); }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const config = defaultConfig(200000); + config.preserveRecentTokens = 0; + const opts: ProxyOptions = { host: "127.0.0.1", port: 0, upstream: "http://127.0.0.1", routes: {}, modelContextLimit: 200000, kernelConfig: config, compress: { injectTool: true, injectNudge: false }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: true, logFile: path.join(tmp, "bili.log"), debug: false, passthrough: false, autoUpdate: false, mitm: { enabled: false, domains: [] } }; + const proxy = await startServer(opts); + await once(proxy, "listening"); + const proxyOrigin = `http://127.0.0.1:${(proxy.address() as { port: number }).port}`; + const upstreamOrigin = `http://127.0.0.1:${(upstream.address() as { port: number }).port}`; + const sid = `codex-thread-${randomUUID()}`; + const openPeer = async (authorization = "Bearer fake-credential", extraHeaders: Record = {}) => { + const client = new WebSocket(`${proxyOrigin.replace(/^http/, "ws")}/bili/${upstreamOrigin}/v1/responses`, { headers: { authorization, "session-id": sid, "thread-id": sid, "openai-beta": "responses_websockets=2026-02-06", "user-agent": "codex_exec/0.147.0 (Ubuntu 24.4.0; x86_64) gnome-terminal (codex_exec; 0.147.0)", originator: "codex_exec", ...extraHeaders } }); + await once(client, "open"); + return client; + }; + const frame = (input: Item[], extra: Item = {}): string => JSON.stringify({ type: "response.create", model: "gpt-5.2", instructions: "You are Codex", store: false, stream: true, include: ["reasoning.encrypted_content"], prompt_cache_key: sid, reasoning: { effort: "medium", summary: "auto" }, text: { verbosity: "low" }, tools: [], input, client_metadata: { session_id: sid, thread_id: sid }, ...extra }); + async function exchange(payload: string, client: WebSocket): Promise { + client.send(payload); + return new Promise((resolve, reject) => { + const events: Item[] = []; + const cleanup = (): void => { clearTimeout(timer); client.off("message", onMessage); client.off("close", onClose); }; + const timer = setTimeout(() => { cleanup(); reject(new Error("WS turn timed out")); }, 10000); + const onClose = (): void => { cleanup(); reject(new Error("WS client closed")); }; + const onMessage = (raw: WebSocket.RawData): void => { + const event = JSON.parse(raw.toString()) as Item; + events.push(event); + if (["response.completed", "response.failed", "response.incomplete", "error"].includes(String(event.type))) { + cleanup(); + resolve(events); + } + }; + client.on("message", onMessage); + client.once("close", onClose); + }); + } + // Codex opens every connection with a prewarm probe and waits for its + // terminal event before sending the real turn; the test must respect the + // same ordering or the session correctly rejects it with response_in_progress. + const probe = (client: WebSocket = peer): Promise => exchange(frame([], { generate: false }), client); + const turn = (input: Item[], client: WebSocket = peer): Promise => exchange(frame(input), client); + const peer = await openPeer(); + peer.on("error", e => console.error("[TEST-CLIENT-ERR]", e.message)); + return { rows, sid, proxyOrigin, upstreamOrigin, proxy, peer, turn, probe, frame, openPeer, + logPath: path.join(tmp, "bili.log"), + setToolArguments: (args: string) => { toolArguments = args; }, get httpRequests() { return httpRequests; }, close: async () => { + peer.terminate(); + for (const client of wss.clients) client.terminate(); + await Promise.all([new Promise(resolve => proxy.close(() => resolve())), new Promise(resolve => upstream.close(() => resolve()))]); + wss.close(); + } }; +} + +const completed = (events: Item[]): Item => { + const event = events.find(e => e.type === "response.completed"); + assert.ok(event, JSON.stringify(events)); + return event.response as Item; +}; + +test("codex WS: prewarm probe plus full-replay turns ride the prefix lane through ACP", { timeout: 30000 }, async () => { + const f = await fixture(); + try { + await f.probe(); + const first = completed(await f.turn([user("codex-first-turn")])); + assert.equal(first.status, "completed"); + assert.ok(JSON.stringify(first.output).includes("ok-")); + assert.equal(f.httpRequests, 0); + assert.equal(f.rows.length, 2); + // Probe forwarded verbatim on the upstream transport (no ACP pipeline: + // no synthesized message, no injected tools, no phantom turn). + assert.deepEqual(f.rows[0].request.input, []); + assert.equal(f.rows[0].request.generate, false); + assert.ok(Array.isArray(f.rows[0].request.tools) && f.rows[0].request.tools.length === 0); + assert.equal(f.rows[0].headers.authorization, "Bearer fake-credential"); + assert.equal(f.rows[0].headers["session-id"], f.sid); + assert.match(String(f.rows[0].headers["user-agent"]), /^codex_exec\//); + // The real turn is a full replay the pipeline processed: bili's ACP + // tools ride alongside codex's own (empty) tool list. + assert.ok(Array.isArray(f.rows[1].request.tools) && (f.rows[1].request.tools as Item[]).some(t => t.name === "acp_status" || t.name === "compress"), JSON.stringify(f.rows[1].request.tools?.map?.((t: Item) => t.name))); + assert.ok(JSON.stringify(f.rows[1].request.input).includes("codex-first-turn")); + const session = peekSession(f.sid); + assert.ok(session); + assert.equal(session.stats.requests, 1); + assert.equal(session.stats.lastInputTokens, 500); + const log = fs.readFileSync(f.logPath, "utf8"); + assert.match(log, /\[codex-responses-ws\] \[conn=1\]/); + assert.match(log, /Responses socket connected/); + assert.doesNotMatch(log, /side request \(/); + } finally { await f.close(); } +}); + +test("codex WS: later turns replay full history; no previous_response_id is ever sent", { timeout: 30000 }, async () => { + const f = await fixture(); + try { + await f.probe(); + completed(await f.turn([user("turn-one-material")])); + completed(await f.turn([user("turn-one-material"), { type: "message", role: "assistant", content: [{ type: "output_text", text: "prior answer" }] }, user("turn-two")])); + + assert.equal(f.rows.length, 3); + for (const row of f.rows) assert.equal(row.request.previous_response_id, undefined); + const replayed = f.rows[2].request.input as Item[]; + // The lane injects a developer message (instructions + compression + // philosophy) ahead of the replay; codex replay itself is verbatim. + assert.equal(replayed.filter(item => item.type === "message" && item.role !== "developer").length, 3); + assert.ok(replayed.some(item => item.role === "developer")); + assert.ok(JSON.stringify(replayed).includes("turn-two")); + assert.equal(f.httpRequests, 0); + } finally { await f.close(); } +}); + +test("codex WS: folding covered history shrinks the replayed turn", { timeout: 30000 }, async () => { + const f = await fixture(); + try { + await f.probe(); + // Codex replays the FULL history every turn (no previous_response_id); + // the fold test must drive the wire the same way or each single-message + // turn would replace the kernel's conversation view instead of growing it. + const replay: Item[] = []; + const play = async (text: string): Promise => { + const reply = completed(await f.turn([...replay, user(text)])); + replay.push(user(text)); + const message = Array.isArray(reply.output) ? (reply.output.find(item => item && typeof item === "object" && item.type === "message") as Item | undefined) : undefined; + if (message) replay.push(message); + }; + await play("session purpose: codex prefix-lane ACP integration"); + await play("OLD-BULKY-SENTINEL " + "unique filler content ".repeat(1200)); + const firstText = JSON.stringify(f.rows.at(-1)!.request.input.find(item => JSON.stringify(item).includes("OLD-BULKY-SENTINEL"))); + const ref = firstText.match(/\x3cacp[^\x3e]*\x3e(m\d+)\x3c\/acp\x3e/)?.[1]; + assert.ok(ref, firstText.slice(-300)); + for (let i = 0; i < 4; i++) await play(`push ${i}`); + const args = { content: [{ startId: ref, endId: ref, summary: "SUMMARY-CODEX-WS-FOLD: initial bulky material contained deterministic filler; retain this verified summary instead of the original payload." }] }; + const result = await fetch(`${f.proxyOrigin}/__bili/plugin/tool`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ conversationId: f.sid, tool: "compress", args }) }); + const out = await result.json() as { ok: boolean; result: string }; + assert.equal(out.ok, true, JSON.stringify(out)); + assert.match(out.result, /Compressed/); + f.setToolArguments("{}"); + completed(await f.turn([ + ...replay, + { type: "function_call", id: "fc_compress", call_id: "call_compress", name: "compress", arguments: JSON.stringify(args) }, + { type: "function_call_output", call_id: "call_compress", output: out.result }, + user("continue after fold"), + ])); + const row = f.rows.at(-1)!; + assert.ok(!JSON.stringify(row.request.input).includes("OLD-BULKY-SENTINEL"), JSON.stringify((row.request.input as Item[]).filter(item => JSON.stringify(item).includes("OLD-BULKY-SENTINEL")).map(item => JSON.stringify(item.content).slice(0, 180)))); + assert.ok(JSON.stringify(row.request.input).includes("SUMMARY-CODEX-WS-FOLD")); + assert.ok(peekSession(f.sid)?.state.blocks.some(b => b.active)); + assert.equal(f.httpRequests, 0); + } finally { await f.close(); } +}); + +test("codex WS: admission requires the conversation header and the responses path", { timeout: 30000 }, async () => { + const f = await fixture(); + const origin = f.proxyOrigin.replace(/^http/, "ws"); + const refused = async (url: string, headers: Record): Promise => { + const client = new WebSocket(url, { headers }); + const [, incoming] = (await once(client, "unexpected-response")) as unknown as [http.ClientRequest, http.IncomingMessage]; + const status = incoming.statusCode ?? 0; + incoming.destroy(); // pre-open refusal: destroy the socket, terminate() would throw + return status; + }; + try { + // Missing session-id: nothing claims the upgrade. + assert.equal(await refused(`${origin}/bili/${f.upstreamOrigin}/v1/responses`, { authorization: "Bearer fake" }), 426); + // Conversation header present but the path is not a responses endpoint. + assert.equal(await refused(`${origin}/bili/${f.upstreamOrigin}/v1/chat`, { "session-id": f.sid }), 426); + } finally { await f.close(); } +}); + +test("codex WS: non-streaming transport options are refused with a protocol error frame", { timeout: 30000 }, async () => { + const f = await fixture(); + try { + const events: Item[] = []; + const done = new Promise(resolve => { f.peer.on("message", (raw: WebSocket.RawData) => { const event = JSON.parse(raw.toString()) as Item; events.push(event); if (event.type === "error") resolve(event); }); }); + f.peer.send(f.frame([user("nope")], { stream: false })); + const error = await done; + assert.equal(error.type, "error"); + assert.equal(error.error.code, "invalid_request"); + } finally { await f.close(); } +}); diff --git a/tests/e2e/README.md b/tests/e2e/README.md index 8a0d441264..7617281aed 100644 --- a/tests/e2e/README.md +++ b/tests/e2e/README.md @@ -1,5 +1,24 @@ # E2E: real codex client through bili against a real upstream +## OpenCode V2 Responses WebSocket regression + +`e2e-opencode-responses-ws.test.ts` runs the real OpenCode V2 binary against +its own loopback Responses WebSocket upstream. It verifies native socket +interception, ACP status execution, real compression, full-history chain reset +after a fold, and usage accounting. Primary requests must remain WebSocket; +HTTP title-generation requests are allowed. Configuration, credentials, state +and working directory are isolated; no real model tokens are consumed. + +```bash +npm run build +ACP_TEST_E2E_OC_WS=1 node --import tsx --test tests/e2e/e2e-opencode-responses-ws.test.ts +``` + +`E2E_OC_BIN` selects the OpenCode binary. The suite skips by default and requires +a V2 binary. Evidence is retained in its reported temporary directory. +The native OpenCode CI workflow runs it in a separate job pinned to V2.0.20; +the existing V2.0.3 Chat Completions coverage remains unchanged. + This suite runs the **real `codex` CLI** against a **real Responses-compatible upstream** through the bili proxy, and asserts the full context-management lifecycle end-to-end: diff --git a/tests/e2e/e2e-opencode-responses-ws.test.ts b/tests/e2e/e2e-opencode-responses-ws.test.ts new file mode 100644 index 0000000000..9c475bcadc --- /dev/null +++ b/tests/e2e/e2e-opencode-responses-ws.test.ts @@ -0,0 +1,152 @@ +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 { once } from "node:events"; +import { spawn, spawnSync } from "node:child_process"; +import { pathToFileURL } from "node:url"; +import test from "node:test"; +import WebSocket, { WebSocketServer } from "ws"; + +type Item = Record; +const run = process.env.ACP_TEST_E2E_OC_WS === "1"; +const bin = process.env.E2E_OC_BIN ?? "opencode"; +const repo = path.resolve(import.meta.dirname, "../.."); +const entry = path.join(repo, "dist/agent/opencode-native.js"); +const version = run ? spawnSync(bin, ["--version"], { timeout: 15000 }).stdout?.toString() ?? "" : ""; + +for (const sparseTerminal of [false, true]) { +test(`real OpenCode V2: native Responses WebSockets, fold and post-fold tools (${sparseTerminal ? "sparse" : "full"} terminal output)`, { + skip: !run ? "set ACP_TEST_E2E_OC_WS=1 (real OpenCode V2, local WS upstream, zero tokens)" : !/v?2\./.test(version) || !fs.existsSync(entry) ? "OpenCode V2 binary and built dist required" : false, + timeout: 180000, +}, async (t) => { + const root = fs.mkdtempSync(path.join(process.env.LOCALAPPDATA ? path.join(process.env.LOCALAPPDATA, "Temp/opencode") : os.tmpdir(), "bili-oc-ws-e2e-")); + const snapshots = new Map(); + const rows: Array<{ transport: "ws" | "http"; body: Item; full: Item[]; headers: http.IncomingHttpHeaders }> = []; + let serial = 0; + let postFoldStatusCalls = 0; + 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") : ""; + const reply = (body: Item, headers: http.IncomingHttpHeaders, transport: "ws" | "http", send: (event: Item) => void) => { + const prior = typeof body.previous_response_id === "string" ? snapshots.get(body.previous_response_id) : undefined; + const full = [...(prior ?? []), ...(Array.isArray(body.input) ? body.input as Item[] : [])]; + rows.push({ transport, body, full, headers }); + const tools = (Array.isArray(body.tools) ? body.tools as Item[] : []).flatMap(tool => tool.type === "namespace" && Array.isArray(tool.tools) ? tool.tools as Item[] : [tool]); + const hasStatus = tools.some(tool => tool.name === "acp_status"); + const statusCalled = full.some(item => item.type === "function_call_output"); + const lastUser = [...full].reverse().find(item => item.role === "user"); + const foldEncoded = itemText(lastUser).match(/WS_FOLD_REQUEST_B64 ([A-Za-z0-9+/=]+)/)?.[1]; + const fold = foldEncoded ? Buffer.from(foldEncoded, "base64").toString("utf8") : undefined; + const foldDone = full.some(item => item.type === "function_call" && item.name === "compress") && full.some(item => item.type === "function_call_output" && String(item.output).includes("Compressed")); + const compressIndex = full.findIndex(item => item.type === "function_call" && item.name === "compress"); + const postFoldStatus = full.slice(compressIndex + 1).find(item => item.type === "function_call" && item.name === "acp_status"); + const postFoldResult = postFoldStatus && full.some(item => item.type === "function_call_output" && item.call_id === postFoldStatus.call_id); + let name = fold ? !foldDone ? "compress" : !postFoldResult ? "acp_status" : undefined : hasStatus && !statusCalled ? "acp_status" : undefined; + if (foldDone && name === "acp_status") postFoldStatusCalls++; + const text = postFoldStatusCalls > 1 ? "WS_E2E_REPEATED_TOOL" : "WS_E2E_OK"; + if (postFoldStatusCalls > 1) name = undefined; + const id = `resp_e2e_ws_${++serial}`; + const args = name === "compress" ? fold! : "{}"; + const item: Item = name + ? { type: "function_call", id: `fc_${id}`, call_id: `call_${id}`, name, arguments: args, status: "completed" } + : { type: "message", id: `msg_${id}`, role: "assistant", status: "completed", content: [{ type: "output_text", text, annotations: [] }] }; + let sequence = 0; + const event = (type: string, data: Item) => 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: item.id, output_index: 0, delta: args }); + event("response.function_call_arguments.done", { item_id: item.id, output_index: 0, arguments: args }); + } else { + event("response.content_part.added", { item_id: item.id, output_index: 0, content_index: 0, part: { type: "output_text", text: "", annotations: [] } }); + event("response.output_text.delta", { item_id: item.id, output_index: 0, content_index: 0, delta: text }); + event("response.output_text.done", { item_id: item.id, output_index: 0, content_index: 0, text }); + event("response.content_part.done", { item_id: item.id, 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: sparseTerminal && foldDone ? [] : [item], usage: { input_tokens: 500, output_tokens: 10, total_tokens: 510, input_tokens_details: { cached_tokens: 0 }, output_tokens_details: { reasoning_tokens: 0 } } } }); + }; + const upstream = http.createServer(async (req, res) => { + if (req.method !== "POST") { res.writeHead(404).end(); return; } + const chunks: Buffer[] = []; + for await (const chunk of req) chunks.push(Buffer.from(chunk)); + const body = JSON.parse(Buffer.concat(chunks).toString()) as Item; + res.writeHead(200, { "content-type": "text/event-stream" }); + reply(body, req.headers, "http", event => res.write(`event: ${event.type}\ndata: ${JSON.stringify(event)}\n\n`)); + res.end(); + }); + const wss = new WebSocketServer({ server: upstream }); + wss.on("connection", (peer, req) => peer.on("message", raw => reply(JSON.parse(raw.toString()) as Item, req.headers, "ws", event => peer.send(JSON.stringify(event))))); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const origin = `http://127.0.0.1:${(upstream.address() as { port: number }).port}`; + const nativeDir = path.join(root, "native-plugin"); + const dirs = { config: path.join(root, "config"), data: path.join(root, "data"), state: path.join(root, "state"), cache: path.join(root, "cache"), cwd: path.join(root, "cwd"), home: path.join(root, "home") }; + for (const dir of [...Object.values(dirs), nativeDir, path.join(dirs.config, "opencode"), path.join(dirs.config, "billion-context")]) fs.mkdirSync(dir, { recursive: true }); + fs.writeFileSync(path.join(nativeDir, "index.js"), `export { default } from ${JSON.stringify(pathToFileURL(entry).href)};\n`); + fs.writeFileSync(path.join(dirs.config, "opencode/opencode.json"), JSON.stringify({ model: "openai/gpt-5.2", providers: { openai: { settings: { apiKey: "test-only-no-real-key", baseURL: `${origin}/v1`, transport: "websocket" } } }, plugins: [nativeDir], compaction: { auto: false } })); + fs.writeFileSync(path.join(dirs.config, "billion-context/billion-context.json"), JSON.stringify({ compress: { preserveRecentTokens: 0 } })); + const env = { ...process.env }; + for (const key of Object.keys(env)) if (/^(BILI|BILLION_CONTEXT|ACP_|OPENCODE|OPENAI|ANTHROPIC|NODE_TEST_CONTEXT)/.test(key) || /(?:API_KEY|TOKEN|SECRET|PASSWORD)$/i.test(key)) delete env[key]; + Object.assign(env, { HOME: dirs.home, XDG_CONFIG_HOME: dirs.config, XDG_DATA_HOME: dirs.data, XDG_STATE_HOME: dirs.state, XDG_CACHE_HOME: dirs.cache, ACP_AUTO_UPDATE: "0", BILLION_CONTEXT_NODE: process.execPath }); + t.after(async () => { + const instances = path.join(dirs.state, "billion-context/instances"); + if (fs.existsSync(instances)) for (const file of fs.readdirSync(instances)) { + try { const record = JSON.parse(fs.readFileSync(path.join(instances, file), "utf8")) as { pid?: number }; if (record.pid) process.kill(record.pid); } catch {} + } + for (const peer of wss.clients) peer.terminate(); + upstream.closeAllConnections(); + await new Promise(resolve => upstream.close(() => resolve())); + wss.close(); + fs.writeFileSync(path.join(root, "requests.json"), JSON.stringify(rows, null, 2)); + }); + const ocRun = async (prompt: string, session?: string) => { + const args = ["run", "--standalone", "--auto", "--format", "json", "-m", "openai/gpt-5.2"]; + if (session) args.push("-s", session); + args.push(prompt); + const child = spawn(bin, args, { cwd: dirs.cwd, env, stdio: ["ignore", "pipe", "pipe"] }); + let stdout = "", stderr = ""; + child.stdout.on("data", chunk => { stdout += chunk; }); + child.stderr.on("data", chunk => { stderr += chunk; }); + const timer = setTimeout(() => child.kill(), 45000); + const [code] = await once(child, "exit"); + clearTimeout(timer); + fs.writeFileSync(path.join(root, `run-${Date.now()}.txt`), stdout + "\n" + stderr); + assert.equal(code, 0, stderr); + assert.ok(stdout.includes("WS_E2E_OK"), stdout + stderr); + }; + await ocRun("Session purpose: verify WebSocket ACP integration."); + const main = rows.filter(row => row.transport === "ws"); + assert.ok(main.length >= 2, `No primary WS requests; evidence at ${root}`); + assert.ok(main.every(row => row.headers["x-bili-plugin"] === "opencode")); + const sid = main[0].headers["x-bili-plugin-conversation"]; + assert.equal(typeof sid, "string"); + await ocRun("E2E-WS-BULKY-SENTINEL " + "deterministic filler content ".repeat(600), sid as string); + const firstUser = rows.filter(row => row.transport === "ws").at(-1)!.full.find(item => item.role === "user" && itemText(item).includes("E2E-WS-BULKY-SENTINEL")); + const ref = itemText(firstUser).match(/\x3cacp[^\x3e]*\x3e(m\d+)\x3c\/acp\x3e/)?.[1]; + assert.ok(ref); + assert.ok(main.some(row => row.full.some(item => item.type === "function_call_output" && String(item.output).includes("ACP Context Analysis")))); + for (let i = 0; i < 3; i++) await ocRun(`push-back round ${i}`, sid as string); + const foldArgs = { content: [{ startId: ref, endId: ref, summary: "E2E-WS-SUMMARY: the original material consisted of deterministic bulky filler and established the WebSocket ACP integration test context." }] }; + await ocRun(`WS_FOLD_REQUEST_B64 ${Buffer.from(JSON.stringify(foldArgs)).toString("base64")}`, sid as string); + const afterFold = rows.filter(row => row.transport === "ws" && row.full.some(item => item.type === "function_call_output" && String(item.output).includes("Compressed"))); + assert.equal(afterFold.length, 2); + assert.equal(postFoldStatusCalls, 1, "A completed status tool must not be requested again after a fold"); + const final = afterFold.at(-1)!; + assert.ok(!JSON.stringify(final.full).includes("E2E-WS-BULKY-SENTINEL")); + assert.ok(JSON.stringify(final.full).includes("E2E-WS-SUMMARY")); + assert.equal(afterFold[0].body.previous_response_id, undefined); + assert.equal(typeof final.body.previous_response_id, "string"); + assert.ok(final.full.some(item => item.type === "function_call_output" && String(item.output).includes("Compressed"))); + const statusCall = [...final.full].reverse().find(item => item.type === "function_call" && item.name === "acp_status"); + assert.ok(statusCall); + assert.ok(final.full.some(item => item.type === "function_call_output" && item.call_id === statusCall.call_id && String(item.output).includes("ACP Context Analysis"))); + assert.ok(rows.filter(row => row.transport === "http").every(row => row.headers["x-bili-plugin-agent"] === "title"), "Primary requests must never fall back to HTTP"); + const log = fs.readFileSync(path.join(dirs.state, "billion-context/bili.log"), "utf8"); + assert.match(log, /tool compress executed via plugin/); + assert.match(log, /forward WS/); + assert.match(log, /acp-usage/); + t.diagnostic(`OpenCode ${version.trim()}, evidence: ${root}`); +}); +} diff --git a/tests/opencode-native.test.ts b/tests/opencode-native.test.ts index 14a4725268..5fbf8a0291 100644 --- a/tests/opencode-native.test.ts +++ b/tests/opencode-native.test.ts @@ -225,7 +225,7 @@ test("setup(route): header stamping applies to the REPLACED request, tools regis }; const setup = createOpencodeV2Setup({ route: createNativeRoute(state, { probe: async () => true }) }); const cleanup = await setup(ctx); - assert.deepEqual(hooks.map((h) => h.name), ["http.request"]); + assert.deepEqual(hooks.map((h) => h.name), ["http.request", "experimental.ws.handshake"]); assert.deepEqual(tools.sort(), [...EXPECTED_TOOLS].sort()); const e: V2HttpRequestEvent = { sessionID: "ses_abc", diff --git a/tests/opencode-v2.test.ts b/tests/opencode-v2.test.ts index 417aceb0d6..c0e900f7db 100644 --- a/tests/opencode-v2.test.ts +++ b/tests/opencode-v2.test.ts @@ -104,13 +104,15 @@ function makeFakeCtx() { const addedCommands: FakeAddedCommand[] = []; const syntheticCalls: Array<{ sessionID: string; text: string; description?: string; resume?: boolean }> = []; let modelRequestCb: ((e: Record) => void | Promise) | undefined; + let wsHandshakeCb: ((e: Record) => void | Promise) | undefined; const disposed: number[] = []; const ctx = { session: { hook: async (name: string, cb: (e: Record) => void | Promise) => { - assert.equal(name, "http.request"); - modelRequestCb = cb; + if (name === "http.request") modelRequestCb = cb; + else if (name === "experimental.ws.handshake") wsHandshakeCb = cb; + else assert.fail(`unexpected hook ${name}`); return { dispose: () => { disposed.push(1); } }; }, synthetic: async (input: { sessionID: string; text: string; description?: string; resume?: boolean }) => { @@ -170,6 +172,10 @@ function makeFakeCtx() { return { headers: store }; }, pushEvent: (evt: { type?: unknown; data?: Record }) => { eventQueue.push(evt); wake?.(); }, + fireWsHandshake: async (event: Record) => { + await wsHandshakeCb!(event); + return event; + }, get addedTools() { return addedTools; }, get addedCommands() { return addedCommands; }, get syntheticCalls() { return syntheticCalls; }, diff --git a/tests/responses-websocket.test.ts b/tests/responses-websocket.test.ts new file mode 100644 index 0000000000..ca11303f81 --- /dev/null +++ b/tests/responses-websocket.test.ts @@ -0,0 +1,764 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import https from "node:https"; +import net from "node:net"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { on, once } from "node:events"; +import { randomUUID } from "node:crypto"; +import test from "node:test"; +import WebSocket, { WebSocketServer } from "ws"; +import { Agent } from "undici"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import { ResponsesWsHistory, ResponsesWsUpstream } from "../src/responses-ws.ts"; +import { _liveUpstreamTimersForTest } from "../src/fetch-util.ts"; +import { proxyDispatcher } from "../src/upstream-proxy.ts"; +import { ensureRootCA, mintHostCert, rootCaPath } from "../src/ca.ts"; +import { validateResponsesWsCreate } from "./wire-contract-fakes.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { peekSession } from "../src/session.ts"; +import { BILI_ACP_TOOLS_RESPONSES } from "../src/compress-tool.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { createOpencodeV2Setup, type V2HttpRequestEvent } from "../src/agent/opencode-v2.ts"; +import { createNativeRoute } from "../src/agent/opencode-native.ts"; + +type Item = Record; + +const testHome = fs.mkdtempSync(path.join(os.tmpdir(), "bili-ws-home-")); +process.env.XDG_STATE_HOME = testHome; +process.env.XDG_DATA_HOME = testHome; +process.env.XDG_CACHE_HOME = testHome; + +const user = (text: string): Item => ({ type: "message", role: "user", content: [{ type: "input_text", text }] }); + +test("WS history: expand deltas, exact-prefix continuation, and reset after a fold", () => { + const history = new ResponsesWsHistory(); + const first = { model: "m", input: [user("old")] }; + const output = [{ type: "message", role: "assistant", content: "ok", status: "completed" }]; + history.commit(first, { id: "resp_1", status: "completed", output }); + const next = history.expand({ type: "response.create", previous_response_id: "resp_1", input: [user("new")] }); + assert.equal((next.input as Item[]).length, 3); + assert.deepEqual(history.continuation(next), { model: "m", previous_response_id: "resp_1", input: [user("new")] }); + const folded = { ...next, input: [user("summary"), ...output, user("new")] }; + assert.deepEqual(history.continuation(folded), folded); + assert.throws(() => history.expand({ type: "response.create", previous_response_id: "unknown", input: [] }), /previous_response_not_found/); + history.commit(next, { id: "failed", status: "failed", output: [] }); + assert.throws(() => history.expand({ type: "response.create", previous_response_id: "failed", input: [] }), /previous_response_not_found/); +}); + +test("V2 handshake: routes OAuth/API Responses sockets and stamps native identity", async () => { + const hooks = new Map void | Promise>(); + const origin = "http://127.0.0.1:8787"; + const cleanup = await createOpencodeV2Setup({ route: createNativeRoute({ origin, ready: Promise.resolve(origin) }, { probe: async () => true }) })({ + session: { hook: async (name, cb) => { hooks.set(name, cb); return {}; } }, + }); + try { + for (const url of ["wss://api.openai.com/v1/responses", "wss://chatgpt.com/backend-api/codex/responses"]) { + const event: V2HttpRequestEvent = { url, headers: { authorization: "Bearer test-only" }, sessionID: "ses_ws", agent: "build", model: { providerID: "openai", id: "gpt-5.2" } }; + await hooks.get("experimental.ws.handshake")!(event); + assert.equal(event.url, `ws://127.0.0.1:8787/bili/responses/${url.replace(/^ws/, "http")}`); + assert.equal(event.headers?.authorization, "Bearer test-only"); + assert.equal(event.headers?.["x-bili-plugin"], "opencode"); + assert.equal(event.headers?.["x-bili-plugin-conversation"], "ses_ws"); + } + } finally { cleanup(); } +}); + +async function fixture(terminalOutput: "full" | "empty" | "omitted" | "partial" | "suffix" = "full") { + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "bili-responses-ws-")); + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const rows: Array<{ request: Item; full: Item[]; headers: http.IncomingHttpHeaders }> = []; + const snapshots = new Map(); + let httpRequests = 0; + let serial = 0; + let toolArguments: string | undefined; + let toolName = "write"; + const nextErrors: Item[] = []; + let stalled = false; + let disconnect = false; + let terminalMode: "completed" | "failed" | "incomplete" | "hold" | "disconnect" = "completed"; + let finishHeld: (() => void) | undefined; + let connections = 0; + const upstream = http.createServer((req, res) => { if (req.method === "POST") httpRequests++; res.writeHead(404).end(); }); + const wss = new WebSocketServer({ server: upstream }); + wss.on("connection", () => { connections++; }); + wss.on("connection", (peer, req) => peer.on("message", raw => { + const request = JSON.parse(raw.toString()) as Item; + const violations = validateResponsesWsCreate(request); + if (violations.length) { peer.send(JSON.stringify({ type: "error", status: 400, error: { code: "invalid_request", message: violations.join("; ") } })); return; } + const delta = request.input as Item[]; + const previous = typeof request.previous_response_id === "string" ? snapshots.get(request.previous_response_id) : undefined; + const full = previous ? [...previous, ...delta] : delta; + rows.push({ request, full, headers: req.headers }); + const nextError = nextErrors.shift(); + if (nextError) { peer.send(JSON.stringify(nextError)); return; } + if (disconnect) { disconnect = false; peer.terminate(); return; } + if (stalled) return; + if (request.previous_response_id && !previous) { peer.send(JSON.stringify({ type: "error", status: 400, error: { code: "previous_response_not_found", message: "missing upstream checkpoint" } })); return; } + const id = `resp_ws_${++serial}`; + const item: Item = toolArguments !== undefined + ? { type: "function_call", id: `fc_${id}`, call_id: `call_${id}`, name: toolName, arguments: toolArguments, status: "completed" } + : { type: "message", id: `msg_${id}`, role: "assistant", status: "completed", content: [{ type: "output_text", text: `ok-${serial}`, annotations: [] }] }; + const send = (type: string, fields: Item) => peer.send(JSON.stringify({ type, ...fields })); + const prefix: Item[] = terminalOutput === "partial" || terminalOutput === "suffix" ? [{ type: "reasoning", id: `rs_${id}`, summary: [], encrypted_content: `opaque-${serial}` }] : []; + const outputIndex = prefix.length; + const output = [...prefix, item]; + send("response.created", { response: { id, status: "in_progress", output: [] } }); + for (const [index, part] of prefix.entries()) { + send("response.output_item.added", { output_index: index, item: part }); + send("response.output_item.done", { output_index: index, item: part }); + } + send("response.output_item.added", { output_index: outputIndex, item: { ...item, status: "in_progress", ...(toolArguments === undefined ? { content: [] } : { arguments: "" }) } }); + if (toolArguments !== undefined) { + send("response.function_call_arguments.delta", { item_id: item.id, output_index: outputIndex, delta: toolArguments }); + send("response.function_call_arguments.done", { item_id: item.id, output_index: outputIndex, arguments: toolArguments }); + } else { + send("response.output_text.delta", { item_id: item.id, output_index: outputIndex, content_index: 0, delta: `ok-${serial}` }); + send("response.output_text.done", { item_id: item.id, output_index: outputIndex, content_index: 0, text: `ok-${serial}` }); + } + send("response.output_item.done", { output_index: outputIndex, item }); + const finish = () => { + const status = terminalMode === "failed" || terminalMode === "incomplete" ? terminalMode : "completed"; + if (status === "completed") snapshots.set(id, [...full, ...output]); + send(`response.${status}`, { response: { id, object: "response", status, ...(terminalOutput === "omitted" ? {} : { output: terminalOutput === "full" ? output : terminalOutput === "suffix" ? [item] : prefix }), usage: { input_tokens: 500, output_tokens: 10, total_tokens: 510 } } }); + }; + if (terminalMode === "hold") finishHeld = finish; + else if (terminalMode === "disconnect") peer.close(1011, "private-upstream-close-reason"); + else finish(); + })); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const config = defaultConfig(200000); + config.preserveRecentTokens = 0; + const opts: ProxyOptions = { host: "127.0.0.1", port: 0, upstream: "http://127.0.0.1", routes: {}, modelContextLimit: 200000, kernelConfig: config, compress: { injectTool: true, injectNudge: false }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: true, logFile: path.join(tmp, "bili.log"), debug: false, passthrough: false, autoUpdate: false, mitm: { enabled: false, domains: [] } }; + const proxy = await startServer(opts); + await once(proxy, "listening"); + const proxyOrigin = `http://127.0.0.1:${(proxy.address() as { port: number }).port}`; + const upstreamOrigin = `http://127.0.0.1:${(upstream.address() as { port: number }).port}`; + const sid = `ses_ws_${randomUUID()}`; + const openPeer = async (authorization = "Bearer fake-credential", extraHeaders: Record = {}) => { + const client = new WebSocket(`${proxyOrigin.replace(/^http/, "ws")}/bili/responses/${upstreamOrigin}/v1/responses`, { headers: { authorization, "x-bili-plugin": "opencode", "x-bili-plugin-conversation": sid, "x-bili-plugin-model": "gpt-5.2", "x-bili-plugin-context-window": "200000", ...extraHeaders } }); + await once(client, "open"); + return client; + }; + const peer = await openPeer(); + async function turn(input: Item[], previous?: string, extra: Item = {}, client = peer): Promise { + return new Promise((resolve, reject) => { + const events: Item[] = []; + const cleanup = (): void => { clearTimeout(timer); client.off("message", onMessage); client.off("close", onClose); }; + const timer = setTimeout(() => { cleanup(); reject(new Error("WS turn timed out")); }, 10000); + const onClose = (): void => { cleanup(); reject(new Error("WS client closed")); }; + const onMessage = (raw: WebSocket.RawData): void => { + const event = JSON.parse(raw.toString()) as Item; + events.push(event); + if (["response.completed", "response.failed", "response.incomplete", "error"].includes(String(event.type))) { + cleanup(); + resolve(events); + } + }; + client.on("message", onMessage); + client.once("close", onClose); + client.send(JSON.stringify({ type: "response.create", model: "gpt-5.2", store: false, input, tools: BILI_ACP_TOOLS_RESPONSES, ...(previous ? { previous_response_id: previous } : {}), ...extra })); + }); + } + return { rows, sid, proxyOrigin, upstreamOrigin, proxy, peer, turn, openPeer, + logPath: path.join(tmp, "bili.log"), + connectionCount: () => connections, + setTerminalMode: (value: typeof terminalMode) => { terminalMode = value; }, + finishHeld: () => { assert.ok(finishHeld); const finish = finishHeld; finishHeld = undefined; finish(); }, + closeUpstream: async () => { + const closed = [...wss.clients].map(client => once(client, "close")); + for (const client of wss.clients) client.close(1000, "private-idle-close-reason"); + await Promise.all(closed); + }, + setError: (error: Item) => { nextErrors.push(error); }, + setStalled: (value: boolean) => { stalled = value; }, + disconnectNext: () => { disconnect = true; }, + setTerminalOutput: (value: typeof terminalOutput) => { terminalOutput = value; }, + clearUpstreamHistory: () => { snapshots.clear(); }, + upstreamPeers: () => wss.clients.size, + setToolArguments: (args: string, name = "write") => { toolArguments = args; toolName = name; }, get httpRequests() { return httpRequests; }, close: async () => { + peer.terminate(); + for (const client of wss.clients) client.terminate(); + await Promise.all([new Promise(resolve => proxy.close(() => resolve())), new Promise(resolve => upstream.close(() => resolve()))]); + wss.close(); + } }; +} + +const completed = (events: Item[]): Item => { + const event = events.find(e => e.type === "response.completed"); + assert.ok(event, JSON.stringify(events)); + return event.response as Item; +}; + +async function nextEvent(peer: WebSocket, type: string): Promise { + for await (const [raw] of on(peer, "message", { signal: AbortSignal.timeout(10000) })) { + const event = JSON.parse(String(raw)) as Item; + if (event.type === type) return event; + } + throw new Error(`Missing WS event: ${type}`); +} + +test("Responses WS: both ends use sockets; incremental tool continuation reaches ACP and usage", { timeout: 30000 }, async () => { + const f = await fixture(); + f.setToolArguments('{"path":"test-only"}'); + try { + const first = completed(await f.turn([user("first-round-sentinel")])); + const call = (first.output as Item[])[0]; + const second = completed(await f.turn([{ type: "function_call_output", call_id: call.call_id, output: "tool result" }, user("second-round")], first.id as string)); + assert.equal(f.httpRequests, 0); + assert.equal(f.rows.length, 2); + assert.equal(f.rows[1].request.previous_response_id, first.id); + assert.equal((f.rows[1].request.input as Item[]).length, 2); + assert.ok(JSON.stringify(f.rows[1].full).includes("first-round-sentinel")); + assert.equal(f.rows[0].headers.authorization, "Bearer fake-credential"); + assert.equal(second.status, "completed"); + const session = peekSession(f.sid); + assert.ok(session); + assert.equal(session.stats.requests, 2); + assert.equal(session.stats.lastInputTokens, 500); + assert.ok(session.state.messageRefs.byRef); + } finally { await f.close(); } +}); + +async function until(condition: () => boolean): Promise { + const deadline = Date.now() + 5000; + while (!condition()) { + if (Date.now() > deadline) throw new Error("WS condition timed out"); + await new Promise(resolve => setTimeout(resolve, 10)); + } +} + +test("Responses WS: upstream continuation cache miss retries once with full processed input", async () => { + const f = await fixture(); + f.setToolArguments('{"path":"test"}'); + try { + const first = completed(await f.turn([user("retained history")])); + f.clearUpstreamHistory(); + const call = (first.output as Item[])[0]; + completed(await f.turn([{ type: "function_call_output", call_id: call.call_id, output: "next" }], first.id as string)); + assert.equal(f.rows.length, 3); + assert.equal(f.rows[1].request.previous_response_id, first.id); + assert.equal(f.rows[2].request.previous_response_id, undefined); + assert.ok(JSON.stringify(f.rows[2].full).includes("retained history")); + } finally { await f.close(); } +}); + +test("Responses WS: upstream request errors preserve status and structured error", async () => { + const f = await fixture(); + try { + f.setError({ type: "error", status: 429, error: { type: "rate_limit_error", code: "rate_limit_exceeded", message: "test rate limit" } }); + const events = await f.turn([user("reject this request")]); + assert.equal(events[0].status, 429); + assert.equal((events[0].error as Item).code, "rate_limit_exceeded"); + assert.equal(f.httpRequests, 0); + } finally { await f.close(); } +}); + +test("Responses WS: client cancellation closes the upstream and clears idle timers", async () => { + const f = await fixture(); + try { + f.setStalled(true); + const turn = f.turn([user("stall")]); + const rejected = assert.rejects(turn, /WS client closed/); + await until(() => f.rows.length === 1); + f.peer.terminate(); + await rejected; + await until(() => f.upstreamPeers() === 0 && _liveUpstreamTimersForTest() === 0); + } finally { await f.close(); } +}); + +test("Responses WS: disconnect fails the turn; next full request reconnects without old state", async () => { + const f = await fixture(); + try { + f.disconnectNext(); + const events = await f.turn([user("first attempt")]); + assert.equal(events.at(-1)?.type, "error"); + completed(await f.turn([user("second attempt")])); + assert.equal(f.rows.at(-1)?.request.previous_response_id, undefined); + assert.ok(!JSON.stringify(f.rows.at(-1)?.full).includes("first attempt")); + } finally { await f.close(); } +}); + +test("Responses WS: unsupported client events/options fail explicitly without upstream traffic", async () => { + const f = await fixture(); + try { + for (const extra of [{ type: "session.update" }, { stream: true }, { stream_id: "named-lane" }, { background: true }]) { + const events = await f.turn([user("invalid")], undefined, extra); + assert.equal((events[0].error as Item).code, "invalid_request"); + } + assert.equal(f.rows.length, 0); + } finally { await f.close(); } +}); + +test("Responses WS: anonymous upgrades retain 426 on the explicit Responses tunnel", async () => { + const f = await fixture(); + try { + const peer = new WebSocket(`${f.proxyOrigin.replace(/^http/, "ws")}/bili/responses/${f.upstreamOrigin}/v1/responses`); + await new Promise((resolve, reject) => { + peer.once("error", error => { assert.match(error.message, /426/); resolve(); }); + peer.once("open", () => { peer.terminate(); reject(new Error("Anonymous upgrade admitted")); }); + }); + } finally { await f.close(); } +}); + +test("Responses WS: non-streaming pipeline requests use WS and return the completed JSON body", async () => { + const f = await fixture(); + const transport = new ResponsesWsUpstream(); + try { + const response = await transport.fetch(`${f.upstreamOrigin}/v1/responses`, { method: "POST", body: JSON.stringify({ model: "gpt-5.2", stream: false, input: [user("preflight request")] }) }); + const body = await response.json() as Item; + assert.equal(body.status, "completed"); + assert.equal(f.httpRequests, 0); + } finally { transport.close(); await f.close(); } +}); + +for (const terminalOutput of ["empty", "omitted", "partial", "suffix"] as const) { + test(`Responses WS: non-streaming ${terminalOutput} terminal output includes streamed results and supports continuation`, async () => { + const f = await fixture(terminalOutput); + const transport = new ResponsesWsUpstream(); + try { + const input = [user("preflight with streamed output")]; + const first = await transport.fetch(`${f.upstreamOrigin}/v1/responses`, { method: "POST", body: JSON.stringify({ model: "gpt-5.2", stream: false, input }) }); + const body = await first.json() as Item; + const output = body.output as Item[]; + assert.ok(output.some(item => item.type === "message" && JSON.stringify(item.content).includes("ok-1"))); + if (terminalOutput === "partial" || terminalOutput === "suffix") { + assert.deepEqual(output.map(item => item.type), ["reasoning", "message"]); + assert.equal(output[0].encrypted_content, "opaque-1"); + } + const next = await transport.fetch(`${f.upstreamOrigin}/v1/responses`, { method: "POST", body: JSON.stringify({ model: "gpt-5.2", stream: false, input: [...input, ...output, user("next preflight")] }) }); + const nextBody = await next.json() as Item; + assert.equal(f.rows.at(-1)?.request.previous_response_id, body.id); + assert.ok(JSON.stringify(nextBody.output).includes("ok-2")); + assert.ok(!JSON.stringify(nextBody.output).includes("ok-1")); + assert.equal(f.httpRequests, 0); + } finally { transport.close(); await f.close(); } + }); +} + +test("Responses WS: completed and failed sessions do not share socket checkpoints", async () => { + const a = await fixture(); + const b = await fixture(); + try { + const first = completed(await a.turn([user("session-A-only")])); + const wrong = await b.turn([user("session-B-delta")], first.id as string); + assert.equal((wrong[0].error as Item).code, "previous_response_not_found"); + completed(await b.turn([user("session-B-only")])); + assert.ok(!JSON.stringify(b.rows.at(-1)?.full).includes("session-A-only")); + a.setError({ type: "response.failed", response: { id: "resp_failed", status: "failed", error: { code: "server_error", message: "fake failure" }, output: [] } }); + const failed = await a.turn([user("failed request")], first.id as string); + assert.equal(failed.at(-1)?.type, "response.failed"); + const wrongFailed = await a.turn([user("delta")], "resp_failed"); + assert.equal((wrongFailed[0].error as Item).code, "previous_response_not_found"); + } finally { await a.close(); await b.close(); } +}); + +test("Responses WS: connection lifetime limit rotates once and sends full input", async () => { + const f = await fixture(); + try { + f.setError({ type: "error", status: 400, error: { code: "websocket_connection_limit_reached", message: "fake lifetime limit" } }); + completed(await f.turn([user("reconnect full input")])); + assert.equal(f.rows.length, 2); + assert.equal(f.rows[1].request.previous_response_id, undefined); + assert.ok(JSON.stringify(f.rows[1].full).includes("reconnect full input")); + } finally { await f.close(); } +}); + +test("Responses WS: existing upstream HTTP proxy routes the WebSocket CONNECT", async () => { + const f = await fixture(); + let connects = 0; + const sockets = new Set(); + const proxy = http.createServer(); + proxy.on("connect", (req, socket, head) => { + connects++; + const destination = new URL(`http://${req.url}`); + const upstream = net.connect(Number(destination.port), destination.hostname, () => { + socket.write("HTTP/1.1 200 Connection Established\r\n\r\n"); + if (head.length) upstream.write(head); + upstream.pipe(socket); + socket.pipe(upstream); + }); + sockets.add(upstream); + upstream.on("error", () => socket.destroy()); + upstream.on("close", () => { sockets.delete(upstream); socket.destroy(); }); + socket.on("error", () => upstream.destroy()); + socket.on("close", () => upstream.destroy()); + }); + proxy.listen(0, "127.0.0.1"); + await once(proxy, "listening"); + const transport = new ResponsesWsUpstream(); + try { + const response = await transport.fetch(`${f.upstreamOrigin}/v1/responses`, { method: "POST", body: JSON.stringify({ model: "gpt-5.2", stream: false, input: [user("proxied WS")] }), dispatcher: proxyDispatcher(`http://127.0.0.1:${(proxy.address() as { port: number }).port}`) }); + assert.equal((await response.json() as Item).status, "completed"); + assert.equal(connects, 1); + assert.equal(f.httpRequests, 0); + } finally { + transport.close(); + for (const socket of sockets) socket.destroy(); + await new Promise(resolve => proxy.close(() => resolve())); + await f.close(); + } +}); + +test("Responses WSS: TLS verification and authorization headers survive the secure upstream leg", async () => { + ensureRootCA(); + const certificate = mintHostCert("localhost"); + const server = https.createServer({ cert: certificate.certPem, key: certificate.keyPem }); + const wss = new WebSocketServer({ server }); + let authorization: string | undefined; + wss.on("connection", (peer, req) => { + authorization = req.headers.authorization; + peer.on("message", () => peer.send(JSON.stringify({ type: "response.completed", response: { id: "resp_tls", status: "completed", output: [] } }))); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const dispatcher = new Agent({ connect: { ca: fs.readFileSync(rootCaPath(), "utf8"), servername: "localhost" } }); + const transport = new ResponsesWsUpstream(); + try { + const response = await transport.fetch(`https://127.0.0.1:${(server.address() as { port: number }).port}/v1/responses`, { method: "POST", headers: { authorization: "Bearer tls-test-only" }, dispatcher, body: JSON.stringify({ model: "gpt-5.2", stream: false, input: [user("TLS verified")] }) }); + assert.equal((await response.json() as Item).id, "resp_tls"); + assert.equal(authorization, "Bearer tls-test-only"); + } finally { + transport.close(); + for (const peer of wss.clients) peer.terminate(); + await dispatcher.close(); + await new Promise(resolve => server.close(() => resolve())); + wss.close(); + } +}); + +test("Responses WS: closing the HTTP server also tears down its upgraded sockets", async () => { + const f = await fixture(); + try { + completed(await f.turn([user("shutdown test")])); + const peerClosed = once(f.peer, "close"); + await new Promise(resolve => f.proxy.close(() => resolve())); + await peerClosed; + await until(() => f.upstreamPeers() === 0 && _liveUpstreamTimersForTest() === 0); + } finally { await f.close(); } +}); + +test("Responses WS: tool argument fragments and completed arguments are byte-exact", async () => { + const f = await fixture(); + const args = '{ "text" : "literal \x3cacp tokens=\\\"2\\\"\x3em00001\x3c/acp\x3e", "unicode": "雪" }'; + f.setToolArguments(args); + try { + const events = await f.turn([user("tool-payload")]); + assert.equal(events.find(e => e.type === "response.function_call_arguments.delta")?.delta, args); + assert.equal(events.find(e => e.type === "response.function_call_arguments.done")?.arguments, args); + assert.equal(((completed(events).output as Item[])[0]).arguments, args); + } finally { await f.close(); } +}); + +/** Windows-runners flake guard: the bili log file is written by the server + * process while the test advances, so a snapshot taken right after a turn can + * miss lines that are logically already emitted. Poll until every positive + * pattern has landed (bounded), then vet the settled snapshot against the + * negative pattern — the CI legs showed 1-2 random WS fault tests failing per + * run purely on this read race (#1968). */ +async function readLogUntil(logPath: string, patterns: readonly RegExp[], notMatch?: RegExp, timeoutMs = 10000): Promise { + const deadline = Date.now() + timeoutMs; + let log = ""; + for (;;) { + log = fs.readFileSync(logPath, "utf8"); + if (patterns.every(pattern => pattern.test(log))) break; + if (Date.now() > deadline) { + assert.ok(false, `timed out waiting for log patterns ${patterns.map(String).join(" ; ")}\n--- log tail ---\n${log.split("\n").slice(-40).join("\n")}`); + } + await new Promise(resolve => setTimeout(resolve, 25)); + } + if (notMatch) assert.doesNotMatch(log, notMatch); + return log; +} + +test("Responses WS: unknown continuation returns an explicit retry-full error", async () => { + const f = await fixture(); + try { + const events = await f.turn([user("delta")], "unknown"); + assert.equal((events[0].error as Item).code, "previous_response_not_found"); + assert.equal(f.rows.length, 0); + } finally { await f.close(); } +}); + +for (const terminalMode of ["disconnect", "failed", "incomplete"] as const) { +test(`Responses WS faults: ${terminalMode} after a delivered tool never commits or silently replays it`, { timeout: 30000 }, async () => { + const f = await fixture("empty"); + try { + const seed = completed(await f.turn([user("retained before interrupted tool")])); + f.setToolArguments('{"text":"private-tool-payload"}'); + f.setTerminalMode(terminalMode); + const events = await f.turn([user("private-request-payload")], seed.id as string); + const call = events.find(e => e.type === "response.output_item.added")?.item as Item; + assert.equal(call?.type, "function_call"); + assert.equal(events.filter(e => e.type === "response.function_call_arguments.done").length, 1); + assert.equal(events.some(e => e.type === "response.completed"), false); + assert.equal(f.rows.length, 2, "No automatic replay after a tool was delivered"); + f.setTerminalMode("completed"); + completed(await f.turn([user("explicit retry from last successful response")], seed.id as string)); + assert.equal(f.rows.length, 3); + assert.equal(f.rows[2].request.previous_response_id, undefined); + assert.ok(JSON.stringify(f.rows[2].full).includes("retained before interrupted tool")); + assert.ok(!f.rows[2].full.some(item => item.call_id === call.call_id)); + assert.ok(!JSON.stringify(f.rows[2].full).includes("private-request-payload")); + assert.equal(f.httpRequests, 0); + const log = await readLogUntil( + f.logPath, + [/\[responses-ws\] \[conn=\d+\] \[session="ses_ws_/, terminalMode === "disconnect" ? /upstream failed phase=stream event=close close_code=1011/ : new RegExp(`checkpoint reset reason=response\.${terminalMode}`)], + /private-tool-payload|private-request-payload|private-upstream-close-reason|fake-credential/, + ); + if (terminalMode === "disconnect") assert.match(log, /checkpoint reset reason=upstream-disconnect/); + } finally { await f.close(); } +}); +} + +test("Responses WS faults: cancel after tool delivery and immediately resume with rotated credentials", { timeout: 30000 }, async () => { + const f = await fixture(); + try { + const seedInput = [user("credential rotation preserves completed history")]; + const seed = completed(await f.turn(seedInput)); + f.setToolArguments('{"text":"private-tool-payload"}'); + f.setTerminalMode("hold"); + const added = nextEvent(f.peer, "response.output_item.added"); + const delivered = nextEvent(f.peer, "response.function_call_arguments.done"); + const interrupted = f.turn([user("cancelled request")], seed.id as string); + const rejected = assert.rejects(interrupted, /WS client closed/); + await delivered; + const call = (await added).item as Item; + f.peer.close(1000, "private-client-close-reason"); + const fresh = await f.openPeer("Bearer private-rotated-credential"); + await rejected; + const stale = await f.turn([user("delta on fresh socket")], seed.id as string, {}, fresh); + assert.equal((stale[0].error as Item).code, "previous_response_not_found"); + assert.equal(f.rows.length, 2); + f.setTerminalMode("completed"); + const full = [...seedInput, ...seed.output as Item[], user("resume with full history")]; + const resumed = completed(await f.turn(full, undefined, {}, fresh)); + assert.equal(f.rows[2].headers.authorization, "Bearer private-rotated-credential"); + assert.equal(f.rows[2].request.previous_response_id, undefined); + assert.ok(!f.rows[2].full.some(item => item.call_id === call.call_id)); + const resumedCall = (resumed.output as Item[])[0]; + completed(await f.turn([{ type: "function_call_output", call_id: resumedCall.call_id, output: "completed once" }], resumed.id as string, {}, fresh)); + assert.equal(f.rows.at(-1)?.request.previous_response_id, resumed.id); + assert.equal(f.connectionCount(), 2); + await until(() => _liveUpstreamTimersForTest() === 0); + assert.equal(f.httpRequests, 0); + const log = await readLogUntil( + f.logPath, + [/client closed phase=active close_code=1000/, /client checkpoint reset reason=client-close/, /client rejected reason=previous_response_not_found/], + /private-tool-payload|private-client-close-reason|private-rotated-credential/, + ); + } finally { await f.close(); } +}); + +test("Responses WS faults: an overlapping create is rejected without corrupting the active checkpoint", { timeout: 30000 }, async () => { + const f = await fixture(); + try { + f.setToolArguments("{}"); + f.setTerminalMode("hold"); + const added = nextEvent(f.peer, "response.output_item.added"); + const delivered = nextEvent(f.peer, "response.function_call_arguments.done"); + const active = f.turn([user("active turn")]); + await delivered; + const call = (await added).item as Item; + const busy = nextEvent(f.peer, "error"); + f.peer.send(JSON.stringify({ type: "response.create", input: [user("must not reach upstream")] })); + assert.equal(((await busy).error as Item).code, "response_in_progress"); + await active; + assert.equal(f.rows.length, 1); + const done = nextEvent(f.peer, "response.completed"); + f.setTerminalMode("completed"); + f.finishHeld(); + const response = (await done).response as Item; + completed(await f.turn([{ type: "function_call_output", call_id: call.call_id, output: "single result" }], response.id as string)); + assert.equal(f.rows[1].request.previous_response_id, response.id); + assert.equal(f.rows[1].full.filter(item => item.call_id === call.call_id).length, 2); + assert.ok(!JSON.stringify(f.rows[1].full).includes("must not reach upstream")); + await readLogUntil(f.logPath, [/client rejected reason=response-in-progress/]); + } finally { await f.close(); } +}); + +for (const code of ["previous_response_not_found", "websocket_connection_limit_reached"]) { +test(`Responses WS faults: repeated ${code} stops after one transport recovery`, { timeout: 30000 }, async () => { + const f = await fixture(); + try { + f.setToolArguments("{}"); + const seed = completed(await f.turn([user("retained retry baseline")])); + const call = (seed.output as Item[])[0]; + const rejection = { type: "error", status: 400, error: { code, message: "private-error-message" } }; + f.setError(rejection); + f.setError(rejection); + const events = await f.turn([{ type: "function_call_output", call_id: call.call_id, output: "bounded retry" }], seed.id as string); + assert.equal(events.at(-1)?.type, "error"); + assert.equal((events.at(-1)?.error as Item).code, code); + assert.equal(f.rows.length, 3); + assert.equal(f.rows[2].request.previous_response_id, undefined); + completed(await f.turn([{ type: "function_call_output", call_id: call.call_id, output: "explicit recovery" }], seed.id as string)); + assert.equal(f.rows.length, 4); + assert.equal(f.rows[3].request.previous_response_id, undefined); + assert.ok(!JSON.stringify(f.rows[3].full).includes("bounded retry")); + const log = (await readLogUntil(f.logPath, [/upstream rejected phase=await-first-event status=400/])) + .split("\n").filter(line => line.includes("[responses-ws]")).join("\n"); + assert.equal((log.match(/upstream retry reason=/g) ?? []).length, 1); + assert.doesNotMatch(log, /private-error-message|fake-credential/); + assert.equal(f.httpRequests, 0); + } finally { await f.close(); } +}); +} + +test("Responses WS faults: refused handshake then a new upstream reconnects without leaking diagnostics", { timeout: 30000 }, async () => { + const f = await fixture(); + const rejected = http.createServer(); + rejected.on("upgrade", (_req, socket) => socket.end("HTTP/1.1 401 Unauthorized\r\nConnection: close\r\nContent-Length: 0\r\n\r\n")); + rejected.listen(0, "127.0.0.1"); + await once(rejected, "listening"); + const log: string[] = []; + const transport = new ResponsesWsUpstream((_level, message) => log.push(message)); + const body = JSON.stringify({ model: "gpt-5.2", stream: false, input: [user("private-handshake-payload")] }); + try { + const port = (rejected.address() as { port: number }).port; + await assert.rejects(transport.fetch(`http://127.0.0.1:${port}/responses?private-query=secret`, { method: "POST", headers: { authorization: "Bearer private-handshake-credential" }, body, signal: AbortSignal.timeout(10000) }), /handshake failed/); + const response = await transport.fetch(`${f.upstreamOrigin}/v1/responses`, { method: "POST", body }); + assert.equal((await response.json() as Item).status, "completed"); + assert.equal(f.rows.length, 1); + assert.equal(f.rows[0].request.previous_response_id, undefined); + assert.match(log.join("\n"), /failed phase=handshake event=error close_code=none/); + assert.match(log.join("\n"), /checkpoint reset reason=connection-key-changed/); + assert.doesNotMatch(log.join("\n"), /private-|secret|Bearer/); + } finally { + transport.close(); + await f.close(); + await new Promise(resolve => rejected.close(() => resolve())); + } +}); + +test("Responses WS: a first-turn all-bili tools frame is a main turn, not an #1897 side demotion", { timeout: 30000 }, async () => { + const f = await fixture(); + try { + // turn() stamps tools: BILI_ACP_TOOLS_RESPONSES and no max_tokens, so the + // frame matches the #1897 omp leak shape exactly — but it arrives through + // the WS lane (x-bili-ws-lane stamped by the bridge), whose envelopes are + // the conversation mainline: the demotion must be vetoed, the kernel must + // engage, and nothing may be routed through the side passthrough (which + // cannot speak this lane's upstream transport). + const first = completed(await f.turn([user("#1897 veto: all-bili first frame stays a main turn")])); + assert.equal(first.status, "completed"); + const previous = first.id as string; + const second = completed(await f.turn([user("still a main turn after the first usage report")], previous)); + assert.equal(second.status, "completed"); + await readLogUntil(f.logPath, [/view=ws-expanded/], /leaked bili tools|side request \(/); + } finally { + await f.close(); + } +}); + +test("Responses WS: a title-persona side request rides the lane transport instead of 502ing", { timeout: 30000 }, async () => { + const f = await fixture(); + try { + // The upgrade carries the #1699 persona header, so every frame on this + // connection is a #388 side request (passthrough + tag strip only). Its + // outbound body reaches the transport as a Buffer — the side passthrough + // forwards the inbound buffer verbatim — and must still ride the lane's + // WebSocket upstream instead of failing with 502. + const titlePeer = await f.openPeer(undefined, { "x-bili-plugin-agent": "title" }); + const events = await f.turn([user("title over the WS lane")], undefined, {}, titlePeer); + const done = events.find(e => e.type === "response.completed"); + assert.ok(done, JSON.stringify(events)); + const log = fs.readFileSync(f.logPath, "utf8"); + assert.match(log, /side request \(agent=title\)/); + assert.doesNotMatch(log, /Unsupported request in Responses WebSocket transport/); + } finally { + await f.close(); + } +}); + +for (const terminalOutput of ["full", "empty", "omitted", "partial", "suffix"] as const) { +test(`Responses WS: actual fold and successive tool results survive ${terminalOutput} terminal output`, { timeout: 30000 }, async () => { + const f = await fixture(); + try { + const seed = completed(await f.turn([user("session purpose: WebSocket ACP integration")])); + const first = completed(await f.turn([user("OLD-BULKY-SENTINEL " + "unique filler content ".repeat(1200))], seed.id as string)); + const firstText = JSON.stringify(f.rows[1].full.find(item => item.role === "user" && JSON.stringify(item).includes("OLD-BULKY-SENTINEL"))); + const ref = firstText.match(/\x3cacp[^\x3e]*\x3e(m\d+)\x3c\/acp\x3e/)?.[1]; + assert.ok(ref, firstText.slice(-300)); + let previous = first.id as string; + for (let i = 0; i < 4; i++) previous = completed(await f.turn([user(`push ${i}`)], previous)).id as string; + const args = { content: [{ startId: ref, endId: ref, summary: "SUMMARY-WS-FOLD: initial bulky material contained deterministic filler; retain this verified summary instead of the original payload." }] }; + const result = await fetch(`${f.proxyOrigin}/__bili/plugin/tool`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ conversationId: f.sid, tool: "compress", args }) }); + const out = await result.json() as { ok: boolean; result: string }; + assert.equal(out.ok, true, JSON.stringify(out)); + assert.match(out.result, /Compressed/); + f.setTerminalOutput(terminalOutput); + f.setToolArguments("{}", "acp_status"); + let events = await f.turn([ + { type: "function_call", id: "fc_compress", call_id: "call_compress", name: "compress", arguments: JSON.stringify(args) }, + { type: "function_call_output", call_id: "call_compress", output: out.result }, + user("continue after fold"), + ], previous); + completed(events); + const row = f.rows.at(-1)!; + assert.equal(row.request.previous_response_id, undefined); + assert.ok(!JSON.stringify(row.full).includes("OLD-BULKY-SENTINEL"), JSON.stringify(row.full.filter(item => JSON.stringify(item).includes("OLD-BULKY-SENTINEL")).map(item => ({ ...item, content: JSON.stringify(item.content).slice(0,180) })))); + assert.ok(JSON.stringify(row.full).includes("SUMMARY-WS-FOLD")); + assert.ok(peekSession(f.sid)?.state.blocks.some(b => b.active)); + const calls: Item[] = []; + for (let i = 0; i < 3; i++) { + const response = completed(events); + const call = events.find(e => e.type === "response.output_item.done" && (e.item as Item).type === "function_call")?.item as Item; + assert.ok(call); + calls.push(call); + if (terminalOutput === "empty") assert.deepEqual(response.output, []); + if (terminalOutput === "omitted") assert.equal(response.output, undefined); + if (terminalOutput === "suffix") assert.deepEqual(response.output, [call]); + const output = `STATUS_RESULT_${i}`; + events = await f.turn([{ type: "function_call_output", call_id: call.call_id, output }], response.id as string); + completed(events); + const next = f.rows.at(-1)!; + assert.equal(next.request.previous_response_id, response.id); + if (terminalOutput === "partial" || terminalOutput === "suffix") { + const callIndex = next.full.findIndex(item => item.id === call.id); + assert.equal(next.full[callIndex - 1]?.type, "reasoning"); + assert.equal(next.full[callIndex - 1]?.id, `rs_${response.id}`); + assert.equal(typeof next.full[callIndex - 1]?.encrypted_content, "string"); + } + for (const [index, priorCall] of calls.entries()) { + assert.ok(next.full.some(item => item.type === "function_call" && item.call_id === priorCall.call_id), `Missing call ${index} in ${terminalOutput} continuation`); + assert.ok(next.full.some(item => item.type === "function_call_output" && item.call_id === priorCall.call_id && item.output === `STATUS_RESULT_${index}`), `Missing result ${index} in ${terminalOutput} continuation`); + } + assert.ok(!JSON.stringify(next.full).includes("OLD-BULKY-SENTINEL")); + assert.ok(JSON.stringify(next.full).includes("SUMMARY-WS-FOLD")); + } + if (terminalOutput === "empty") { + for (const fault of ["missing-checkpoint", "idle-disconnect", "connection-limit"] as const) { + const response = completed(events); + const call = events.find(e => e.type === "response.output_item.done" && (e.item as Item).type === "function_call")?.item as Item; + calls.push(call); + if (fault === "missing-checkpoint") f.clearUpstreamHistory(); + if (fault === "idle-disconnect") await f.closeUpstream(); + if (fault === "connection-limit") f.setError({ type: "error", status: 400, error: { code: "websocket_connection_limit_reached" } }); + const index = calls.length - 1; + events = await f.turn([{ type: "function_call_output", call_id: call.call_id, output: `STATUS_RESULT_${index}` }], response.id as string); + completed(events); + const next = f.rows.at(-1)!; + assert.equal(next.request.previous_response_id, undefined, fault); + for (const [i, priorCall] of calls.entries()) { + assert.equal(next.full.filter(item => item.type === "function_call" && item.call_id === priorCall.call_id).length, 1, fault); + assert.equal(next.full.filter(item => item.type === "function_call_output" && item.call_id === priorCall.call_id && item.output === `STATUS_RESULT_${i}`).length, 1, fault); + } + assert.ok(!JSON.stringify(next.full).includes("OLD-BULKY-SENTINEL")); + assert.ok(JSON.stringify(next.full).includes("SUMMARY-WS-FOLD")); + } + const log = await readLogUntil( + f.logPath, + [/upstream retry reason=continuation-rejected action=resend-full/, /upstream checkpoint reset reason=reconnect/, /upstream retry reason=connection-limit action=reconnect-full/], + /fake-credential|private-idle-close-reason/, + ); + } + assert.equal(f.httpRequests, 0); + } finally { await f.close(); } +}); +} diff --git a/tests/wire-contract-fakes.ts b/tests/wire-contract-fakes.ts index f15c1d474d..2befc55401 100644 --- a/tests/wire-contract-fakes.ts +++ b/tests/wire-contract-fakes.ts @@ -26,6 +26,12 @@ export interface WireRule { } export const WIRE_RULES: readonly WireRule[] = [ + { + id: "WC-013", + wire: "responses", + summary: "Responses WebSocket requests use response.create and omit HTTP-only stream/background/stream_options fields", + provenance: "OpenAI WebSocket mode guide https://developers.openai.com/api/docs/guides/websocket-mode/ (transport-specific stream/background fields are not used); OpenCode v2.0.20 open-responses-channel.ts removes these fields; bili #1844 adds a WS transport rather than sending HTTP envelopes verbatim.", + }, { id: "WC-012", wire: "responses", @@ -220,6 +226,7 @@ function validateGeminiSchema(schema: unknown, path: string, out: string[]): voi export function validateResponsesBody(body: unknown): string[] { const out: string[] = []; if (!isPlainObject(body)) return out; + if (body.type === "response.create") out.push(...validateResponsesWsCreate(body)); // WC-011 (#1733): runs before the tools early-return — the adjacency ban // applies to any array input, tools or not. if (Array.isArray(body.input)) { @@ -290,6 +297,11 @@ export function validateResponsesBody(body: unknown): string[] { return out; } +export function validateResponsesWsCreate(body: unknown): string[] { + if (!isPlainObject(body) || body.type !== "response.create") return ["WC-013: expected response.create WebSocket event"]; + return ["stream", "stream_options", "background"].filter(key => key in body).map(key => `WC-013: HTTP-only ${key} must not reach WebSocket response.create`); +} + /** WC-006 on a Gemini generateContent/streamGenerateContent body. */ export function validateGoogleBody(body: unknown): string[] { const out: string[] = []; diff --git a/tests/wire-contract.test.ts b/tests/wire-contract.test.ts index 9b9255b6f4..9e27d72e4a 100644 --- a/tests/wire-contract.test.ts +++ b/tests/wire-contract.test.ts @@ -52,10 +52,15 @@ import { retrieveToolsFor, IMAGE_FULL_TOOL_GOOGLE, } from "../src/compress-tool.ts"; -import { WIRE_RULES, VALIDATORS, startFakeUpstream, type Wire } from "./wire-contract-fakes.ts"; +import { WIRE_RULES, VALIDATORS, startFakeUpstream, validateResponsesWsCreate, type Wire } from "./wire-contract-fakes.ts"; type ToolShape = Record; +test("WC-013: WebSocket transport envelopes omit HTTP-only fields", () => { + assert.deepEqual(validateResponsesWsCreate({ type: "response.create", input: [] }), []); + for (const key of ["stream", "background", "stream_options"]) assert.match(validateResponsesWsCreate({ type: "response.create", [key]: true })[0], /WC-013/); +}); + test("WC-012 validates compaction IDs without changing function-call ID rules", () => { assert.deepEqual(VALIDATORS.responses({ input: [{ type: "compaction", id: "cmp_real", encrypted_content: "opaque" }] }), []); assert.deepEqual(VALIDATORS.responses({ input: [{ type: "function_call", id: "fc_real" }] }), []); @@ -110,6 +115,7 @@ test("wire-contract ledger: every rule has a live enforcement clause", () => { { tools: [{ type: "function", function: { name: "ok", parameters: { anyOf: [] } } }] }, ], responses: [ + { type: "response.create", input: [], stream: true }, { input: [{ type: "compaction", id: "fc_bili_local", encrypted_content: "bili:acp:summary" }] }, { tools: [{ type: "function", name: "bad name", parameters: { type: "object", properties: {} } }] }, { tools: [{ type: "function", name: "ok", parameters: { type: "array" } }] }, diff --git a/tests/ws-bridge.test.ts b/tests/ws-bridge.test.ts new file mode 100644 index 0000000000..002336faad --- /dev/null +++ b/tests/ws-bridge.test.ts @@ -0,0 +1,113 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import net from "node:net"; +import { Duplex } from "node:stream"; +import { AddressInfo } from "node:net"; +import WebSocket from "ws"; +import { installWebSocketBridge, type WsBridgeCodec, type WsBridgeSession } from "../src/ws-bridge.ts"; + +// #1467 phase-2 shell: the bridge must be codec-agnostic. These tests drive +// it with a synthetic second codec (not the Responses one) to prove the +// admission table, frame dispatch, and shutdown lifecycle are generic. + +class RecordingSession implements WsBridgeSession { + static readonly live = new Set(); + readonly events: string[] = []; + shutdowns: string[] = []; + constructor(readonly context: Parameters[0]) { + RecordingSession.live.add(this); + context.log("info", `fake socket connected upstream=${context.upstreamUrl}`); + } + onMessage(data: Buffer, binary: boolean): void { + this.events.push(`message:${binary ? "bin" : "text"}:${data.toString()}`); + this.context.peer.send(`echo:${data.toString()}`); + } + onClose(code: number): void { + this.events.push(`close:${code}`); + RecordingSession.live.delete(this); + } + shutdown(reason: string): void { + this.shutdowns.push(reason); + } +} + +const fakeCodec: WsBridgeCodec = { + name: "fake-ws", + pluginMarker: "fakehost", + matchUpgrade: url => /^\/bili\/fake\/(https?:\/\/[^/]+\/echo)$/.exec(url ?? "")?.[1], + createSession: context => new RecordingSession(context), +}; + +async function harness(codecs: readonly WsBridgeCodec[]) { + const server = http.createServer((_req, res) => res.writeHead(404).end()); + const logs: string[] = []; + const handler = installWebSocketBridge(server, (_req, res) => Promise.resolve(res.writeHead(200).end()), (level, message) => logs.push(`${level} ${message}`), codecs); + server.on("upgrade", (req, socket, head) => { if (!handler(req, socket, head)) socket.destroy(); }); + await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)); + return { server, handler, logs, port: (server.address() as AddressInfo).port }; +} + +test("ws bridge: second codec claims its own path+marker and exchanges frames", async () => { + const upstream = http.createServer(); + await new Promise(resolve => upstream.listen(0, "127.0.0.1", resolve)); + const upstreamPort = (upstream.address() as AddressInfo).port; + const { server, logs, port } = await harness([fakeCodec]); + try { + const socket = new WebSocket(`ws://127.0.0.1:${port}/bili/fake/http://127.0.0.1:${upstreamPort}/echo`, { + headers: { "x-bili-plugin": "fakehost", "x-bili-plugin-conversation": "ses_fake_1" }, + }); + const received = new Promise(resolve => socket.on("message", data => resolve(data.toString()))); + socket.on("open", () => socket.send("hello")); + assert.equal(await received, "echo:hello"); + const session = [...RecordingSession.live][0]!; + socket.close(1000); + await new Promise(resolve => socket.on("close", resolve)); + const deadline = Date.now() + 5000; + while (!session.events.includes("close:1000") && Date.now() < deadline) { + await new Promise(resolve => setTimeout(resolve, 25)); + } + assert.deepEqual(session.events, ["message:text:hello", "close:1000"]); + assert.ok(logs.some(line => line.includes("[fake-ws] [conn=1] [session=\"ses_fake_1\"] fake socket connected"))); + } finally { + await new Promise(resolve => server.close(resolve)); + await new Promise(resolve => upstream.close(resolve)); + } +}); + +test("ws bridge: marker mismatch or foreign path falls through unclaimed", async () => { + const { server, handler } = await harness([fakeCodec]); + try { + const upgrade = (headers: Record) => handler( + Object.assign(new http.IncomingMessage(new net.Socket()), { url: "/bili/fake/http://127.0.0.1:9/echo", headers }), + new Duplex(), + Buffer.alloc(0), + ); + assert.equal(upgrade({ "x-bili-plugin": "opencode", "x-bili-plugin-conversation": "ses" }), false); + assert.equal(upgrade({ "x-bili-plugin-conversation": "ses" }), false); + assert.equal(upgrade({ "x-bili-plugin": "fakehost" }), false); + } finally { + await new Promise(resolve => server.close(resolve)); + } +}); + +test("ws bridge: server close shuts every session down and terminates peers", async () => { + const upstream = http.createServer(); + await new Promise(resolve => upstream.listen(0, "127.0.0.1", resolve)); + const upstreamPort = (upstream.address() as AddressInfo).port; + const { server, port } = await harness([fakeCodec]); + try { + const socket = new WebSocket(`ws://127.0.0.1:${port}/bili/fake/http://127.0.0.1:${upstreamPort}/echo`, { + headers: { "x-bili-plugin": "fakehost", "x-bili-plugin-conversation": "ses_fake_2" }, + }); + await new Promise((resolve, reject) => { socket.on("open", resolve); socket.on("error", reject); }); + const session = [...RecordingSession.live].at(-1)!; + const closed = new Promise(resolve => socket.on("close", resolve)); + await new Promise((resolve, reject) => server.close(error => (error ? reject(error) : resolve()))); + await closed; + assert.deepEqual(session.shutdowns, ["server-close"]); + assert.equal(socket.readyState, WebSocket.CLOSED); + } finally { + await new Promise(resolve => upstream.close(() => resolve())); + } +}); diff --git a/tsup.config.ts b/tsup.config.ts index 14da7eba10..2713568b12 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -30,7 +30,7 @@ defineConfig({ // noExternal, esbuild keeps `import ... from "acp-kernel"` in dist, and // npm then installs acp-kernel as a runtime dep — breaking the // "dist/index.js is self-contained" contract (AGENTS.md §2.1). - noExternal: ["acp-kernel", "fzstd", "node-forge", "semver", "tar", "undici", "jsonc-parser"], + noExternal: ["acp-kernel", "fzstd", "node-forge", "semver", "tar", "undici", "jsonc-parser", "ws"], // sharp is an OPTIONAL runtime dependency (native module): it must stay // EXTERNAL so dist keeps a real lazy `import("sharp")` that Node resolves // at runtime from node_modules — missing ⇒ clean pass-through, and the