From 6985af1dbbbcaa82d61d088d00458a81b6e0c325 Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 19 Nov 2026 20:35:51 +0800 Subject: [PATCH 1/2] fix: keep hosted web_search_call in the same run as its reasoning (#7296) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit responsesToCore flattens hosted calls (web_search_call, file_search_call, mcp_call, ...) into contentType:"reasoning" cores. The Responses ordering repair classified them as a second reasoning item and inserted a user separator between the real reasoning and the hosted call, splitting the assistant run — the server then rejects the web_search_call "without its required 'reasoning' item". Classify a flattened hosted-call core (whose rawResponsesItem sidecar carries a hosted-call wire type) as tool-call (phase 2) so the reasoning -> hosted call run stays intact. The separator still lands between distinct runs. Closes #7296 (cherry picked from commit 2a2a9e923ae638a48b5abd442eef4cc9aa5791ec) --- src/server/responses-ordering.ts | 34 +++++++++++++- tests/issue564-responses-ordering.test.ts | 55 +++++++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/src/server/responses-ordering.ts b/src/server/responses-ordering.ts index 54300075f8..0116475d05 100644 --- a/src/server/responses-ordering.ts +++ b/src/server/responses-ordering.ts @@ -111,6 +111,32 @@ export interface SeparatorManifest { body: string; } +/** The Responses hosted-call item types `responsesToCore` flattens into + * `contentType: "reasoning"` cores (kernel/src/wire/responses.ts). On the wire + * these are tool-call items, NOT reasoning items: a `web_search_call` must stay + * in the same assistant run as its `reasoning` item (the server rejects a + * `web_search_call` "without its required 'reasoning' item" once a user + * separator splits the run). Classifying them as a second reasoning would + * insert a spurious separator and break the run (#7296). */ +const RESPONSES_HOSTED_CALL_TYPES = new Set([ + "computer_call", + "computer_call_output", + "file_search_call", + "web_search_call", + "image_generation_call", + "code_interpreter_call", + "mcp_call", +]); + +/** True when a `contentType: "reasoning"` core is actually a flattened hosted + * call (its `rawResponsesItem` sidecar is a hosted-call item), not a real + * reasoning item. Only the sidecar's wire `type` decides — never the core text + * or id, which the codec re-derives and a client could shape. */ +function isHostedCallCore(m: CoreMessage): boolean { + const raw = (m as BiliMessage).rawResponsesItem as { type?: unknown } | undefined; + return typeof raw?.type === "string" && RESPONSES_HOSTED_CALL_TYPES.has(raw.type); +} + // #564: folding + stripKernelSummaries can merge two assistant turns into one // run, which Responses rejects (run order reasoning* -> message* -> // function_call*, <=1 reasoning). Rebuild boundaries from the ORIGINAL history @@ -187,7 +213,13 @@ export function repairResponsesAssistantOrdering( seenReasoning = false; continue; } - const kind = m.contentType === "reasoning" ? "reasoning" : m.contentType === "tool-call" ? "tool-call" : "message"; + // A flattened hosted call carries contentType "reasoning" but is a + // tool-call on the wire (#7296): classify it as tool-call (phase 2) so a + // `reasoning -> web_search_call` run stays intact instead of getting a + // spurious user separator between the two. + const kind = m.contentType === "reasoning" + ? (isHostedCallCore(m) ? "tool-call" : "reasoning") + : m.contentType === "tool-call" ? "tool-call" : "message"; const r = runOf.get(m.id); if (kind === "reasoning" && r !== undefined && runHasBody.get(r) && survivorCount.get(r) === 1) continue; if ((kind === "reasoning" && (phase > 0 || seenReasoning)) || (kind === "message" && phase === 2)) pushSeparator(); diff --git a/tests/issue564-responses-ordering.test.ts b/tests/issue564-responses-ordering.test.ts index 25ba088981..1b833afdf7 100644 --- a/tests/issue564-responses-ordering.test.ts +++ b/tests/issue564-responses-ordering.test.ts @@ -132,6 +132,11 @@ const asstMsg = (id: string, text: string): CoreMessage => ({ id, role: "assista const reasoningMsg = (id: string, enc: string): CoreMessage & { rawResponsesItem: Record } => ({ id, role: "assistant", contentType: "reasoning", text: id, rawResponsesItem: { type: "reasoning", id, encrypted_content: enc, summary: [] } }); const toolCall = (id: string, callId: string, name: string, args: string): CoreMessage => ({ id, role: "assistant", contentType: "tool-call", toolCallId: callId, toolName: name, text: args }); const toolResult = (id: string, callId: string, out: string): CoreMessage => ({ id, role: "tool", contentType: "tool-result", toolCallId: callId, text: out }); +// #7296: the kernel flattens a hosted call (web_search_call, file_search_call, +// mcp_call, ...) into a contentType:"reasoning" core whose rawResponsesItem is +// the original wire item. coreToResponses re-emits that raw item verbatim. +const hostedCall = (id: string, type: string, extra: Record = {}): CoreMessage & { rawResponsesItem: Record } => + ({ id, role: "assistant", contentType: "reasoning", text: `opaque:${id}`, rawResponsesItem: { type, id, status: "completed", ...extra } }); function wireItems(core: CoreMessage[]): Item[] { return coreToResponses(core) as Item[]; @@ -263,6 +268,56 @@ test("#564: a surviving acp_summary carrier is untouched (no spurious separator) assertValidAssistantOrdering(wireItems(out), "carrier"); }); +test("#7296: a hosted web_search_call stays in the same run as its reasoning (no spurious separator)", () => { + // The kernel flattens hosted calls into contentType:"reasoning" cores. The + // ordering repair must classify them as tool-call (phase 2), NOT as a second + // reasoning — otherwise it inserts a user separator between the reasoning and + // the hosted call, splitting the assistant run and making the server reject + // the web_search_call "without its required 'reasoning' item". + const original: CoreMessage[] = [ + userMsg("u1", "Find a public reference."), + reasoningMsg("rs_fixture", "ENC_RS"), + hostedCall("ws_fixture", "web_search_call", { action: { type: "search", query: "fixture query" } }), + ]; + const out = repairResponsesAssistantOrdering([...original], original); + assert.equal(out.slots.length, 0, "no separator inserted between reasoning and hosted call"); + assert.deepEqual(out.messages.map((m) => m.id), ["u1", "rs_fixture", "ws_fixture"], "nothing added or dropped"); + const items = wireItems(out.messages); + assert.deepEqual( + items.map((i) => (i.type === "message" ? `message:${i.role}` : i.type)), + ["message:user", "reasoning", "web_search_call"], + "reasoning + hosted call render as one intact assistant run", + ); + assertValidAssistantOrdering(items, "#7296 hosted-call"); +}); + +test("#7296: the separator still lands between distinct runs, not before a hosted call", () => { + // Two original assistant runs (split by a user message that folding removes): + // run 1 = reasoning + hosted call, run 2 = reasoning + message. The repair + // must keep run 1 intact (no separator between its reasoning and hosted call) + // and still separate run 2 (separator before run 2's reasoning). + const original: CoreMessage[] = [ + userMsg("u1", "q1"), + reasoningMsg("rs1", "ENC_1"), hostedCall("ws1", "web_search_call", { action: { type: "search", query: "q" } }), + userMsg("u2", "q2"), + reasoningMsg("rs2", "ENC_2"), asstMsg("tx2", "BODY-2"), + ]; + const folded: CoreMessage[] = [ + userMsg("u1", "q1"), + reasoningMsg("rs1", "ENC_1"), hostedCall("ws1", "web_search_call", { action: { type: "search", query: "q" } }), + reasoningMsg("rs2", "ENC_2"), asstMsg("tx2", "BODY-2"), + ]; + const out = repairResponsesAssistantOrdering(folded, original); + const ids = out.messages.map((m) => m.id); + assert.equal(out.slots.length, 1, "exactly one separator, between the two runs"); + const sep = ids.find((id) => id.startsWith("acp_turn_sep_")); + assert.ok(sep, "a separator was minted"); + const sepIdx = ids.indexOf(sep!); + assert.equal(ids[sepIdx - 1], "ws1", "separator comes right after the hosted call (run 1 intact)"); + assert.equal(ids[sepIdx + 1], "rs2", "separator comes right before run 2's reasoning"); + assertValidAssistantOrdering(wireItems(out.messages), "#7296 distinct-runs"); +}); + // --- #564/#2627 producer-owned provenance ------------------------------- // A separator is outbound-only and, on the wire, byte-identical to a client // user message carrying the same text (coreToResponses emits From e7b3a5120dd3e1cc820a48734b9d0ff37bf9e1f1 Mon Sep 17 00:00:00 2001 From: z0d0dOr <128966342+z0d0dOr@users.noreply.github.com> Date: Sat, 24 Oct 2026 09:10:23 -0700 Subject: [PATCH 2/2] fix: route codex context-only prewarms to the probe relay via the upstream proxy (#4594) A generate:false request that replays no conversation turn (empty input, or only developer/system/user messages) is a prewarm probe: forward it verbatim instead of running it as a zero-message ACP turn, which deactivated every fold block and re-injected the whole forged-summary list. History prewarms keep riding the pipeline. The probe relay now egresses through the same upstream proxy the relay resolves; ws-bridge exposes the resolved proxy instead of a boolean so the tunnel guard and the probe share one decision. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit cdda1b3b301691fc3de6eec16c54c1fa3dda6799) --- src/inline-engine.ts | 2 +- src/inline-ws.ts | 9 +-- src/responses-ws.ts | 31 +++++--- src/server.ts | 2 +- src/ws-bridge.ts | 11 +-- tests/codex-responses-ws.test.ts | 100 ++++++++++++++++++++++++-- tests/ws-bridge-pending-close.test.ts | 2 +- 7 files changed, 131 insertions(+), 26 deletions(-) diff --git a/src/inline-engine.ts b/src/inline-engine.ts index eaee425ef2..7df657ff5e 100644 --- a/src/inline-engine.ts +++ b/src/inline-engine.ts @@ -466,7 +466,7 @@ async function startInlineEngine(): Promise { 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 diff --git a/src/inline-ws.ts b/src/inline-ws.ts index cf893035e0..09694ad191 100644 --- a/src/inline-ws.ts +++ b/src/inline-ws.ts @@ -37,9 +37,10 @@ interface InlineWsBridge { export async function startInlineWsBridge(options: { dispatchHttp: (req: http.IncomingMessage, res: http.ServerResponse) => Promise; 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; + /** #2124 parity with startServer(): the upstream proxy a destination's egress + * leaves through, if any (checkTunnelDestination derives the remote-DNS flag + * from it, and the prewarm probe egresses through the same proxy, #4594). */ + egressProxyFor?: (destinationUrl: string) => string | undefined; /** #300/#3073 hop-chain instance identity, same as the spawned server. */ instanceId?: string; }): Promise { @@ -55,7 +56,7 @@ export async function startInlineWsBridge(options: { options.dispatchHttp, options.log, [responsesCodec, codexResponsesCodec], - options.isEgressProxied, + options.egressProxyFor, options.instanceId, ); server.on("upgrade", (source, socket, head) => { diff --git a/src/responses-ws.ts b/src/responses-ws.ts index 49aac8aa94..4bf6c42359 100644 --- a/src/responses-ws.ts +++ b/src/responses-ws.ts @@ -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"; @@ -96,6 +97,11 @@ function untypedErrorEnvelope(frame: unknown): { frame: JsonObject; error: JsonO return { frame, error: frame.error }; } +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")); +} + function describeEventType(frame: unknown): string { if (frame === null) return "null"; if (!object(frame)) return Array.isArray(frame) ? "array" : String(typeof frame); @@ -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) { diff --git a/src/server.ts b/src/server.ts index 3fb954b5ff..a22bbf5562 100644 --- a/src/server.ts +++ b/src/server.ts @@ -822,7 +822,7 @@ export async function startServer(opts: ProxyOptions): Promise { // 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 diff --git a/src/ws-bridge.ts b/src/ws-bridge.ts index a2e60ea92f..d5173e92e1 100644 --- a/src/ws-bridge.ts +++ b/src/ws-bridge.ts @@ -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 { @@ -86,8 +89,8 @@ export function installWebSocketBridge( dispatch: (req: http.IncomingMessage, res: http.ServerResponse) => Promise, 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 { @@ -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"); @@ -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", () => {}); diff --git a/tests/codex-responses-ws.test.ts b/tests/codex-responses-ws.test.ts index cc31b88cb3..a28db271c5 100644 --- a/tests/codex-responses-ws.test.ts +++ b/tests/codex-responses-ws.test.ts @@ -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"; @@ -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 = {}) { const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "bili-codex-ws-")); _setStoreForTest(new SessionStore({ enabled: false })); setRegistryForTest({}); @@ -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; @@ -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}`; @@ -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 => exchange(frame([], { generate: false }), client); - const turn = (input: Item[], client: WebSocket = peer): Promise => exchange(frame(input), client); + const turn = (input: Item[], client: WebSocket = peer, extra: Item = {}): Promise => 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, @@ -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 => { + 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(); + 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(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"); diff --git a/tests/ws-bridge-pending-close.test.ts b/tests/ws-bridge-pending-close.test.ts index 05ab031718..7950025e4b 100644 --- a/tests/ws-bridge-pending-close.test.ts +++ b/tests/ws-bridge-pending-close.test.ts @@ -21,7 +21,7 @@ function fixture(proxied: boolean) { return { onMessage() {}, onClose() {}, shutdown() {} }; }, }; - const handler = installWebSocketBridge(server, async () => {}, () => {}, [codec], () => proxied); + const handler = installWebSocketBridge(server, async () => {}, () => {}, [codec], () => (proxied ? "http://proxy.example:3128" : undefined)); return { server, handler, created: () => created }; }