Skip to content
Open
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
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
9 changes: 5 additions & 4 deletions src/inline-ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ 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;
/** #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<InlineWsBridge> {
Expand All @@ -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) => {
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 @@ -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);
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
34 changes: 33 additions & 1 deletion src/server/responses-ordering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
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
Loading
Loading