Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
18e5171
fix: route codex context-only prewarms to the probe relay via the ups…
z0d0dOr Oct 24, 2026
64cab41
fix: carry the prewarm proxy route through the inline bridge
z0d0dOr Oct 25, 2026
a2d6759
fix: preserve blog metadata and launcher checks on Windows
z0d0dOr Oct 25, 2026
60d57d5
test: verify fork restart under legacy and full storage
z0d0dOr Oct 25, 2026
607940f
test(storage): account for persisted engine lane in render goldens
z0d0dOr Oct 25, 2026
8511d34
test(storage): account for one-time ledger ref migration on restart
z0d0dOr Oct 25, 2026
f3f7370
Merge master into Responses prewarm PR
z0d0dOr Oct 28, 2026
e69391d
Merge commit 'b0b0e66ff085b08e8e26c02f1f79ad2a8849a496' into 2026-10-…
z0d0dOr Oct 28, 2026
168c0b0
test: fix Windows graceful shutdown and fixture teardown
z0d0dOr Oct 28, 2026
5504e08
test: handle reset health probes in silent TCP fixture
z0d0dOr Oct 28, 2026
d265022
Merge master into Responses prewarm PR
Oct 29, 2026
fff851c
Merge remote-tracking branch 'origin/master' into sync-pr-4595
Oct 29, 2026
c7a3817
Merge remote-tracking branch 'origin/master' into sync-pr-4595
Oct 29, 2026
7072c3c
Merge remote-tracking branch 'refs/remotes/origin/master' into sync-p…
Oct 30, 2026
bac018a
Merge remote-tracking branch 'refs/remotes/origin/ci-fixes' into sync…
Oct 30, 2026
2010a82
Merge remote-tracking branch 'refs/remotes/origin/master' into sync-p…
Oct 31, 2026
ae4b193
Merge remote-tracking branch 'refs/remotes/origin/master' into sync-p…
Oct 31, 2026
ebb80ef
Merge master into PR #4595 branch to resolve merge conflict
Nov 13, 2026
4a1119e
Merge ci-fixes (c6369fb6a1) into PR #4595 branch
Nov 14, 2026
ed0492a
Merge master (a446a3c1c8) into PR #4595 branch
Nov 19, 2026
13e6b51
fix(tests): adapt ws-bridge-pending-close mock to the egressProxyFor …
Nov 19, 2026
e7dd721
fix(tests): correct stale prewarm-guard comment to match post-#3342 p…
Nov 19, 2026
3968639
Merge master (f7621b130eb) into PR #4595 branch
Nov 20, 2026
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
2 changes: 1 addition & 1 deletion src/inline-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ async function startInlineEngine(): Promise<InlineEngine> {
const bridge = startInlineWsBridge({
dispatchHttp,
log: ctx.log,
isEgressProxied: (url) => resolveProxyDecision(opts.routes, opts.proxy, url, opts.proxyFallback).proxy !== undefined,
egressProxyFor: (url) => resolveProxyDecision(opts.routes, opts.proxy, url, opts.proxyFallback).proxy,
instanceId: ctx.instanceId,
});
const wsOrigin = bridge
Expand Down
7 changes: 3 additions & 4 deletions src/inline-ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ interface InlineWsBridge {
export async function startInlineWsBridge(options: {
dispatchHttp: (req: http.IncomingMessage, res: http.ServerResponse) => Promise<void>;
log: (level: string, message: string) => void;
/** #2124 parity with startServer(): lets checkTunnelDestination know when
* a destination resolves remotely (upstream proxy owns DNS). */
isEgressProxied?: (destinationUrl: string) => boolean;
/** Same egress route as startServer(), including prewarm probes outside the pipeline. */
egressProxyFor?: (destinationUrl: string) => string | undefined;
/** #300/#3073 hop-chain instance identity, same as the spawned server. */
instanceId?: string;
}): Promise<InlineWsBridge> {
Expand All @@ -55,7 +54,7 @@ export async function startInlineWsBridge(options: {
options.dispatchHttp,
options.log,
[responsesCodec, codexResponsesCodec],
options.isEgressProxied,
options.egressProxyFor,
options.instanceId,
);
server.on("upgrade", (source, socket, head) => {
Expand Down
31 changes: 21 additions & 10 deletions src/responses-ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { markWsLaneTransport, withFetchTransport } from "./fetch-transport.js";
import { BILI_HOP_HEADER } from "./server/context-window.js";
import { connectionNamedHeaders, stripRemoteCompactionBetaFeature, UPSTREAM_HOP_HEADERS } from "./server/headers.js";
import { BILI_TUNNEL_HEADER } from "./tunnel-guard.js";
import { proxyDispatcher } from "./upstream-proxy.js";
import { normalizeSseLineEndings, finalizeSseLineEndings, sseEventData } from "./sse-util.js";
import type { WsBridgeCodec, WsBridgeContext, WsBridgeSession } from "./ws-bridge.js";

Expand Down Expand Up @@ -77,6 +78,11 @@ function object(value: unknown): value is JsonObject {
return value !== null && typeof value === "object" && !Array.isArray(value);
}

function isPrewarmProbe(body: JsonObject): boolean {
return body.generate === false && Array.isArray(body.input) && body.input.every(item =>
object(item) && (item.type === undefined || item.type === "message") && (item.role === "developer" || item.role === "system" || item.role === "user"));
}

// x.ai's Responses WebSocket rejects a stale previous_response_id with an
// HTTP-style envelope and no top-level type:
// {"error":{"message":"gRPC error: Response with id=<id> not found","type":"api_error"}}
Expand Down Expand Up @@ -851,20 +857,25 @@ export class ResponsesWsSession implements WsBridgeSession {
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) {
// Prewarm probes (generate:false replaying no conversation turn) are
// transport-layer noise, not conversation turns. Codex sends one on
// every new connection — empty input (0.147), or only its developer /
// user context items (startup and idle-thread warmup) — and expects
// only a terminal event back. Routing them through the ACP pipeline
// would normalize empty input into a synthesized message (#1862) and
// record a phantom turn in the kernel's ref sequence, shifting every
// later fold anchor; a context-only view also deactivates every fold
// block and re-injects every forged summary (#4594). Injecting tools
// and compression prompts into a no-generation request is pure byte
// inflation. Forward verbatim on the upstream transport, through the
// relay's upstream proxy. A history prewarm replays real turns and
// stays on the pipeline.
if (isPrewarmProbe(body)) {
void (async () => {
const sendFrame = (payload: string): void => { if (peer.readyState === WebSocket.OPEN) peer.send(payload); };
let sawTerminal = false;
try {
const response = await this.transport.fetch(this.context.upstreamUrl, { method: "POST", headers: { ...handshakeHeaders, "content-type": "application/json" }, body: JSON.stringify(body) });
const response = await this.transport.fetch(this.context.upstreamUrl, { method: "POST", headers: { ...handshakeHeaders, "content-type": "application/json" }, body: JSON.stringify(body), dispatcher: proxyDispatcher(this.context.egressProxyFor?.(this.context.upstreamUrl)) });
if (response.body === null) {
const text = await response.text();
if (!response.ok) {
Expand Down
2 changes: 1 addition & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ export async function startServer(opts: ProxyOptions): Promise<http.Server> {
// 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], (url) => resolveProxyDecision(opts.routes, opts.proxy, url, opts.proxyFallback).proxy !== undefined, instanceId);
const wsUpgrade = installWebSocketBridge(server, dispatch, log, [responsesCodec, codexResponsesCodec], (url) => resolveProxyDecision(opts.routes, opts.proxy, url, opts.proxyFallback).proxy, instanceId);
// Unclaimed upgrades retain the immediate HTTP fallback contract.
// An explicit 'upgrade' listener is
// required: without one Node's behavior is version-dependent (some
Expand Down
11 changes: 7 additions & 4 deletions src/ws-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export interface WsBridgeContext {
* handshake — probe and turns alike — so a chained bili sees one
* consistent processor marker instead of marker-less probes. */
instanceId?: string;
/** The upstream proxy the relay's egress uses for a destination (undefined = direct).
* Codec traffic that egresses outside the relay (the prewarm probe) resolves the same route. */
egressProxyFor?: (destinationUrl: string) => string | undefined;
}

export interface WsBridgeCodec {
Expand Down Expand Up @@ -86,8 +89,8 @@ export function installWebSocketBridge(
dispatch: (req: http.IncomingMessage, res: http.ServerResponse) => Promise<void>,
log: (level: string, message: string) => void,
codecs: readonly WsBridgeCodec[],
/** #2124: whether a WS-upstream destination's egress leaves through an upstream proxy (its hostname is then resolved remotely by that proxy, not the local resolver). */
isEgressProxied?: (destinationUrl: string) => boolean,
/** #2124: the upstream proxy a WS-upstream destination's egress leaves through, if any (a proxied hostname is resolved remotely by that proxy, not the local resolver). */
egressProxyFor?: (destinationUrl: string) => string | undefined,
/** #300/#3073: this server's per-instance identity for the x-bili-hop chain stamp (see WsBridgeContext.instanceId). */
instanceId?: string,
): (req: http.IncomingMessage, socket: Duplex, head: Buffer) => boolean {
Expand Down Expand Up @@ -143,7 +146,7 @@ export function installWebSocketBridge(
// 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(), egressProxied: isEgressProxied?.(upstream) });
const verdict = await checkTunnelDestination(upstream, { selfPort: source.socket.localPort, clientLoopback: true, allowlist: tunnelAllowlistFromEnv(), egressProxied: egressProxyFor?.(upstream) !== undefined });
if (closing || socket.destroyed) return;
if (!verdict.ok) {
socket.end("HTTP/1.1 403 Forbidden\r\nConnection: close\r\nContent-Length: 0\r\n\r\n");
Expand All @@ -152,7 +155,7 @@ export function installWebSocketBridge(
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, instanceId });
const session = codec.createSession({ log: trace, peer, source, upstreamUrl: upstream, dispatch, codec, instanceId, egressProxyFor });
const entry = { session, peer };
sessions.add(entry);
peer.on("error", () => {});
Expand Down
100 changes: 95 additions & 5 deletions tests/codex-responses-ws.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from "node:assert/strict";
import http from "node:http";
import fs from "node:fs";
import net from "node:net";
import os from "node:os";
import path from "node:path";
import { on, once } from "node:events";
Expand All @@ -24,13 +25,14 @@ 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 }] });
const developer = (text: string): Item => ({ type: "message", role: "developer", 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() {
async function fixture(overrides: Partial<ProxyOptions> = {}) {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "bili-codex-ws-"));
_setStoreForTest(new SessionStore({ enabled: false }));
setRegistryForTest({});
Expand All @@ -48,8 +50,8 @@ async function fixture() {
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.
if (request.generate === false || (Array.isArray(request.input) && request.input.length === 0)) {
// Probe / no-generation warmup: 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;
Expand All @@ -73,7 +75,7 @@ async function fixture() {
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 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: [] }, ...overrides };
const proxy = await startServer(opts);
await once(proxy, "listening");
const proxyOrigin = `http://127.0.0.1:${(proxy.address() as { port: number }).port}`;
Expand Down Expand Up @@ -108,7 +110,7 @@ async function fixture() {
// 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<Item[]> => exchange(frame([], { generate: false }), client);
const turn = (input: Item[], client: WebSocket = peer): Promise<Item[]> => exchange(frame(input), client);
const turn = (input: Item[], client: WebSocket = peer, extra: Item = {}): Promise<Item[]> => exchange(frame(input, extra), client);
const peer = await openPeer();
peer.on("error", e => console.error("[TEST-CLIENT-ERR]", e.message));
return { rows, upstreamConnections, sid, proxyOrigin, upstreamOrigin, proxy, peer, turn, probe, frame, openPeer,
Expand Down Expand Up @@ -361,6 +363,94 @@ test("codex WS: folding covered history shrinks the replayed turn", { timeout: 3
} finally { await f.close(); }
});

// codex's startup / idle-thread warmup is generate:false carrying only its
// developer/user context items. Run as a pipeline turn, that zero-conversation
// view deactivated every fold and re-injected every forged summary (#4594).
test("codex WS: a context-only generate:false prewarm is relayed verbatim and leaves the fold intact (#4594)", { timeout: 30000 }, async () => {
const f = await fixture();
try {
await f.probe();
const replay: Item[] = [];
const play = async (text: string): Promise<void> => {
const reply = completed(await f.turn([...replay, user(text)]));
replay.push(user(text));
const message = (reply.output as Item[]).find(item => item.type === "message");
if (message) replay.push(message);
};
await play("session purpose: codex prewarm regression");
await play("PREWARM-BULKY " + "unique filler content ".repeat(1200));
const bulky = JSON.stringify((f.rows.at(-1)!.request.input as Item[]).find(item => JSON.stringify(item).includes("PREWARM-BULKY")));
const ref = bulky.match(/\x3cacp[^\x3e]*\x3e(m\d+)\x3c\/acp\x3e/)?.[1];
assert.ok(ref, bulky.slice(-300));
for (let i = 0; i < 4; i++) await play(`push ${i}`);
const args = { content: [{ startId: ref, endId: ref, summary: "SUMMARY-PREWARM-FOLD: bulky deterministic filler, folded before the prewarm arrives." }] };
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 }) });
assert.equal(((await result.json()) as { ok: boolean }).ok, true);
const session = peekSession(f.sid);
assert.ok(session);
assert.ok(session.state.blocks.some(b => b.active));
session.metadata.codexForgedSummaries = ["FORGED-SUMMARY-SENTINEL"];
const requests = session.stats.requests;
const context = [developer("codex permissions and instructions"), user("environment context: cwd=/repo shell=bash")];
const warm = completed(await f.turn(context, f.peer, { generate: false }));
assert.deepEqual(warm.output, []);
const sent = f.rows.at(-1)!.request;
assert.equal(sent.generate, false);
assert.deepEqual(sent.input, context, "the prewarm reaches upstream verbatim");
assert.deepEqual(sent.tools, [], "no ACP tools ride a no-generation request");
assert.ok(!JSON.stringify(sent).includes("FORGED-SUMMARY-SENTINEL"), "forged summaries are not re-injected into the prewarm");
assert.equal(session.stats.requests, requests, "the prewarm is not a pipeline turn");
assert.ok(peekSession(f.sid)?.state.blocks.some(b => b.active), "the prewarm leaves the fold active");
} finally { await f.close(); }
});

test("codex WS: a history-replaying generate:false prewarm stays on the ACP pipeline (#4594)", { timeout: 30000 }, async () => {
const f = await fixture();
try {
await f.probe();
completed(await f.turn([user("history-turn")]));
const warm = completed(await f.turn([user("history-turn"), { type: "message", role: "assistant", content: [{ type: "output_text", text: "prior answer" }] }], f.peer, { generate: false }));
assert.deepEqual(warm.output, []);
const sent = f.rows.at(-1)!.request;
assert.equal(sent.generate, false);
assert.ok((sent.tools as Item[]).some(t => t.name === "acp_status" || t.name === "compress"), "a history prewarm is still processed by the pipeline");
assert.equal(peekSession(f.sid)?.stats.requests, 2);
} finally { await f.close(); }
});

test("codex WS: the prewarm probe egresses through the configured upstream proxy (#4594)", { timeout: 30000 }, async () => {
let connects = 0;
const tunnels = new Set<net.Socket>();
const upstreamProxy = http.createServer();
upstreamProxy.on("connect", (req, socket, head) => {
connects++;
const destination = new URL(`http://${req.url}`);
const tunnel = net.connect(Number(destination.port), destination.hostname, () => {
socket.write("HTTP/1.1 200 Connection Established\r\n\r\n");
if (head.length) tunnel.write(head);
tunnel.pipe(socket);
socket.pipe(tunnel);
});
tunnels.add(tunnel);
tunnel.on("error", () => socket.destroy());
tunnel.on("close", () => { tunnels.delete(tunnel); socket.destroy(); });
socket.on("error", () => tunnel.destroy());
socket.on("close", () => tunnel.destroy());
});
upstreamProxy.listen(0, "127.0.0.1");
await once(upstreamProxy, "listening");
const f = await fixture({ proxy: `http://127.0.0.1:${(upstreamProxy.address() as { port: number }).port}` });
try {
await f.probe();
assert.equal(connects, 1, "the probe's upstream WebSocket tunnels through the configured proxy");
assert.deepEqual(f.rows[0]?.request.input, []);
} finally {
await f.close();
for (const tunnel of tunnels) tunnel.destroy();
await new Promise<void>(resolve => upstreamProxy.close(() => resolve()));
}
});

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");
Expand Down
Loading
Loading