Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/responses-ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export class ResponsesWsUpstream {
private history = new ResponsesWsHistory();
private active?: { fail: (error: Error, reason?: string) => void };

constructor(private readonly log: DiagnosticLog = () => {}, private readonly requestBytes = MAX_REQUEST_BYTES, private readonly hopStamp?: string) {}
constructor(private readonly log: DiagnosticLog = () => {}, private readonly requestBytes = MAX_REQUEST_BYTES, private readonly hopStamp?: string, private readonly forwardTransportFields = false) {}

private resetHistory(reason: string): void {
this.history.clear();
Expand Down Expand Up @@ -259,7 +259,20 @@ export class ResponsesWsUpstream {
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 { stream, stream_options: streamOptions, background: _background, previous_response_id: _previous, type: _type, ...body } = parsed;
// #3063 wire fidelity (WC-017): the codex lane admits stream/stream_options from
// the client and must forward them verbatim — real codex create frames always carry
// stream:true (required field), and dropping it made chatgpt.com answer with a
// zero-output completed turn. Gated on forwardTransportFields (the codec's
// allowStreamFlag) rather than field presence: the session layer injects
// stream:true into EVERY frame before fetch(), so presence cannot distinguish a
// client-sent field from an internal one. The opencode lane (WC-013) refuses both
// fields at admission, so its egress stays byte-identical to before.
const egressTransportFields: JsonObject = {};
if (this.forwardTransportFields) {
if (stream !== undefined) egressTransportFields.stream = stream;
if (streamOptions !== undefined) egressTransportFields.stream_options = streamOptions;
}
const socket = await this.connect(url, options);
if (this.active) throw new Error("Responses WebSocket exchange already active");
const request = this.history.continuation(body);
Expand Down Expand Up @@ -323,7 +336,7 @@ export class ResponsesWsUpstream {
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" }));
socket.send(JSON.stringify({ ...body, ...egressTransportFields, type: "response.create" }));
return;
}
clean();
Expand Down Expand Up @@ -375,7 +388,7 @@ export class ResponsesWsUpstream {
options.signal?.addEventListener("abort", aborted, { once: true });
if (options.signal?.aborted) aborted();
else {
try { socket.send(JSON.stringify({ ...request, type: "response.create" })); }
try { socket.send(JSON.stringify({ ...request, ...egressTransportFields, type: "response.create" })); }
catch (error) { fail(error instanceof Error ? error : new Error("Responses WebSocket send failed"), "send-failed"); }
}
});
Expand Down Expand Up @@ -481,7 +494,7 @@ class ResponsesWsSession implements WsBridgeSession {
private busy = false;

constructor(private readonly context: WsBridgeContext) {
this.transport = new ResponsesWsUpstream(context.log, undefined, context.instanceId);
this.transport = new ResponsesWsUpstream(context.log, undefined, context.instanceId, context.codec.allowStreamFlag === true);
context.log("info", "Responses socket connected (ACP request pipeline)");
}

Expand Down
12 changes: 12 additions & 0 deletions tests/codex-responses-ws.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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";
import { validateCodexResponsesWsCreate } from "./wire-contract-fakes.ts";

type Item = Record<string, unknown>;

Expand Down Expand Up @@ -42,6 +43,8 @@ async function fixture() {
const wss = new WebSocketServer({ server: upstream });
wss.on("connection", (peer, req) => { upstreamConnections.push(req.headers); peer.on("error", e => console.error("[FAKE-UPSTREAM-PEER-ERR]", e.message)); peer.on("message", raw => {
const request = JSON.parse(raw.toString()) as Item;
const violations = validateCodexResponsesWsCreate(request);
if (violations.length) { peer.send(JSON.stringify({ type: "error", status: 400, error: { code: "invalid_request", message: violations.join("; ") } })); return; }
rows.push({ request, headers: req.headers });
const id = `resp_codex_${++serial}`;
const send = (type: string, fields: Item): void => peer.send(JSON.stringify({ type, ...fields }));
Expand Down Expand Up @@ -141,6 +144,11 @@ test("codex WS: prewarm probe plus full-replay turns ride the prefix lane throug
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\//);
// #3063: the client's transport fields must ride the upstream frame
// verbatim — dropping stream made chatgpt.com answer with a zero-output
// completed turn.
assert.equal(f.rows[0].request.stream, true);
assert.equal(f.rows[1].request.stream, true);
// 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 as Item[] | undefined)?.map?.((t: Item) => t.name)));
Expand Down Expand Up @@ -400,6 +408,10 @@ test("codex WS: stream_options delivery hints are admitted (codex 0.160.0 shape,
// Admitted through the ACP pipeline: probe row + one processed turn row, no error frame.
assert.equal(f.rows.length, 2);
assert.ok(JSON.stringify(f.rows[1].request.input).includes("stream-options-turn"));
// #3063: admitted is not enough — the hint must also reach upstream verbatim.
assert.equal(f.rows[0].request.stream, true);
assert.equal(f.rows[1].request.stream, true);
assert.deepEqual(f.rows[1].request.stream_options, { reasoning_summary_delivery: "sequential_cutoff" });
} finally { await f.close(); }
});

Expand Down
26 changes: 24 additions & 2 deletions tests/wire-contract-fakes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,23 @@ export type Wire = "anthropic" | "openai-chat" | "responses" | "google";
interface WireRule {
readonly id: string;
readonly wire: Wire;
/** Lane scope (e.g. "codex") — the constraint only holds on that client's lane, so its
* enforcement lives in the lane's own fake, not in the wire's shared VALIDATOR. */
readonly lane?: string;
readonly summary: string;
readonly provenance: string;
}

export const WIRE_RULES: readonly WireRule[] = [
{
id: "WC-017",
wire: "responses",
lane: "codex",
summary:
"codex CLI Responses WebSocket create frames carry stream:true (required field in the client's WS request struct) plus optional stream_options — bili must forward both verbatim on the upstream egress; dropping them made chatgpt.com answer with a zero-output completed turn",
provenance:
"bili #3063 production silent failure (codex 0.162.0-alpha.17.2 through /bili/<chatgpt.com> prefix routing on bili 0.1.183+: upstream counted input tokens, produced zero output, turn ended completed; identical via --passthrough). Cross-checked against openai/codex source: codex-rs/codex-api/src/common.rs ResponseCreateWsRequest declares stream: bool as a required serialized field (no skip_serializing_if) and serializes stream_options when present; codex-rs/core/tests/suite/client_websockets.rs asserts body[\"stream\"] == true on the wire. Repair: src/responses-ws.ts re-attaches the client's transport fields at egress (egressTransportFields); supersedes WC-013's over-generalized scope for the codex lane.",
},
{
id: "WC-016",
wire: "anthropic",
Expand All @@ -44,8 +56,8 @@ 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.",
summary: "OpenCode V2 Responses WebSocket requests use response.create and omit HTTP-only stream/background/stream_options fields — that client's contract; the codex CLI lane instead REQUIRES stream on create frames (see WC-017)",
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. Scope corrected per #3063 (2026-10-13): the rule was over-generalized from the OpenCode V2 client to all WS lanes — the codex CLI's own create frames carry stream:true as a required field, and chatgpt.com answers stream-less frames with a zero-output completed turn.",
},
{
id: "WC-012",
Expand Down Expand Up @@ -432,6 +444,16 @@ export function validateResponsesWsCreate(body: unknown): string[] {
return ["stream", "stream_options", "background"].filter(key => key in body).map(key => `WC-013: HTTP-only ${key} must not reach WebSocket response.create`);
}

/** WC-017 on a codex-lane create frame: stream:true is REQUIRED (the client's request
* struct declares it without skip_serializing_if) — the inverse of WC-013's opencode lane. */
export function validateCodexResponsesWsCreate(body: unknown): string[] {
if (!isPlainObject(body) || body.type !== "response.create") return ["WC-017: expected response.create WebSocket event"];
const out: string[] = [];
if (body.stream !== true) out.push("WC-017: codex response.create must carry stream:true — required field in the codex WS request struct; dropping it made chatgpt.com answer with a zero-output completed turn (#3063)");
if (body.background !== undefined) out.push("WC-017: codex response.create must not carry background");
return out;
}

/** WC-006 on a Gemini generateContent/streamGenerateContent body. */
function validateGoogleBody(body: unknown): string[] {
const out: string[] = [];
Expand Down
15 changes: 14 additions & 1 deletion tests/wire-contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import {
retrieveToolsFor,
IMAGE_FULL_TOOL_GOOGLE,
} from "../src/compress-tool.ts";
import { WIRE_RULES, WIRE_HEADER_RULES, VALIDATORS, startFakeUpstream, validateResponsesWsCreate, validateGoSessionHeaders, type Wire } from "./wire-contract-fakes.ts";
import { WIRE_RULES, WIRE_HEADER_RULES, VALIDATORS, startFakeUpstream, validateResponsesWsCreate, validateCodexResponsesWsCreate, validateGoSessionHeaders, type Wire } from "./wire-contract-fakes.ts";

type ToolShape = Record<string, unknown>;

Expand Down Expand Up @@ -174,13 +174,26 @@ test("wire-contract ledger: every rule has a live enforcement clause", () => {
],
};
for (const rule of WIRE_RULES) {
if (rule.lane) continue; // lane-scoped rules are gated by their own lane's fake — see the lane probe gate below
const enforced = probes[rule.wire].some((body) =>
VALIDATORS[rule.wire](body).some((v) => v.startsWith(rule.id)),
);
assert.ok(enforced, `rule ${rule.id} (${rule.summary}) has no live enforcement clause in the fakes`);
}
});

test("wire-contract ledger: every lane-scoped rule has a live enforcement clause", () => {
// Lane-scoped constraints hold on ONE client's lane only — the shared wire validator
// cannot enforce them (WC-013 forbids exactly what WC-017 requires, same frame shape).
const laneProbes: Array<{ ruleId: string; validate: (body: unknown) => string[]; body: unknown }> = [
{ ruleId: "WC-017", validate: validateCodexResponsesWsCreate, body: { type: "response.create", input: [] } },
];
for (const { ruleId, validate, body } of laneProbes) {
assert.ok(validate(body).some(v => v.startsWith(ruleId)), `lane-scoped rule ${ruleId} has no live enforcement clause in its lane fake`);
}
for (const rule of WIRE_RULES) if (rule.lane) assert.ok(laneProbes.some(p => p.ruleId === rule.id), `lane-scoped rule ${rule.id} is missing from the lane probe table`);
});

test("wire-contract ledger: every header rule has a live enforcement clause", () => {
for (const rule of WIRE_HEADER_RULES) {
const enforced = validateGoSessionHeaders({}).some((v) => v.startsWith(rule.id));
Expand Down
Loading