diff --git a/src/server/handle.ts b/src/server/handle.ts index 2627eb8841..1791ad76be 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -70,7 +70,7 @@ import { buildForgeResponse, classifierForgeVariant, estimateCjkTokens, isAutoMo import { extractConversationId, isV1BackgroundSideRequest, isV1InternalRequest, unwrapV1Internal } from "../v1internal.js"; import { anthropicBetaContextWindow, anthropicBetaMessageThreads, BILI_HOP_HEADER, capRegistryWindowByStandard, expandedContextSuffixWindow, launcherContextWindow, launcherMaxOutput, noteUnresolvedModelSession, UNRESOLVED_MODEL_SESSION_THRESHOLD, windowSourceLogged } from "./context-window.js"; import { NO_IDENTITY_MESSAGE, safeSessionId } from "./headers.js"; -import { demoteGate, hasLeakedBiliToolsOnly, hasSingleShotBudget, isClaudeCodeTitleRequest, isDshAutoReviewRequest, isDshSingleShotSidecar, isDshTitleRequest, isServerToolUtilityCall, isSideRequest, isStructuredOutputCall, resolveSideLane, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools } from "./side-request.js"; +import { demoteGate, hasLeakedBiliToolsOnly, hasSingleShotBudget, isClaudeCodeTitleRequest, isDshAutoReviewRequest, isDshSingleShotSidecar, isDshTitleRequest, isResponsesServerToolSingleShot, isServerToolUtilityCall, isSideRequest, isStructuredOutputCall, resolveSideLane, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools } from "./side-request.js"; import { DSH_COMPACTION_SHAPE_MSGS, DSH_REFUSAL_STORM_LOG_EVERY, detectPassthroughDshCompactionCall, dshCompactionRefusal, dshPassthroughGuardStats, isDshCompactionCall } from "./dsh-compaction-guard.js"; import { buildDshCompactionForgeBody, renderDshAuthoredCheckpoint } from "./dsh-authored-compaction.js"; import { artifactSeedHit, detectAcpArtifacts } from "./chain-artifacts.js"; @@ -1621,6 +1621,19 @@ async function handleRequest( if (!clientProvided) { anonAffinity = prefixAffinity.resolve(affinityMessageList()); if (!anonAffinity) { + // #6548: a single-shot server-tool-only Responses call (the + // pi-web-access xAI web search: string input, web_search / + // x_search tools, no history) is not a conversation turn — it + // carries no history to fingerprint and creates no session + // state, so the #286 refusal's rationale does not apply to + // it, and the client cannot carry an identity here. Forward + // it verbatim (same raw relay as the #1117 passthrough) so + // the search backend keeps working instead of hard-400ing. + if (protocol === "responses" && isResponsesServerToolSingleShot(parsed)) { + log("info", `single-shot server-tool Responses call — no conversation identity possible; forwarding verbatim (#6548): ${maskUrlForLog(req.url ?? "")}`); + await forward(req, res, opts, scrubCompatDrop(scrubAnthropicPck(protocol, bodyBuffer, log), compatDropPaths, log), null, core, config, log, route, instanceId, undefined, undefined, undefined, googleModel ?? vertexModel); + return; + } log("warn", `400: no stable conversation identity on ${protocol} request → ${maskUrlsInText(upstreamOrigin)}; refusing to create a content-fingerprint session (#286)`); res.writeHead(400, { "content-type": "application/json" }); res.end(JSON.stringify(protocol === "anthropic" diff --git a/src/server/side-request.ts b/src/server/side-request.ts index d991c40feb..a48af56d9e 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -73,6 +73,60 @@ export function isServerToolUtilityCall(parsed: unknown): boolean { return false; } +// #6548: the Responses-wire analogue of isServerToolUtilityCall. pi-web-access +// sends its xAI web-search backend as a deliberately minimal single-shot +// Responses body — `{ model, input, tools: [{type:"web_search"}, +// {type:"x_search"}] }` — string input, no message history, no +// prompt_cache_key, and it exposes no config knob for extra headers or a +// session id, so the client cannot carry an identity here. That shape is NOT +// a conversation turn: it has no history to fingerprint and creates no +// session state, so the #286 refusal's rationale (content-fingerprint +// sessions would silently orphan state) does not apply to it — the refusal +// branch forwards it verbatim instead of hard-400ing. +// Decisive conditions (all must hold): +// - `input` is a plain string (the single-shot prompt); an array input is +// message history, and a missing input is degenerate; +// - non-empty tools where EVERY entry's type is a Responses server-executed +// search type (`web_search`, `web_search_preview`, `x_search`) — mixing +// ANY client-side tool (`function`, `mcp`, ...) vetoes; +// - no identity/history signal in the body: `previous_response_id`, +// `session_id` or `prompt_cache_key` present means the call chains onto +// a conversation (or carries an identity the #286 path already honors); +// - tool_choice absent, "auto" or "required", or an object forcing a +// DECLARED server tool; "none" vetoes (a request that forbids tool use +// is not a tool call), as does forcing a client tool or an unrecognized +// shape. +// Deliberately proto-safe by shape alone: the Anthropic detector above +// requires `messages`, so a Responses body can never match it — and this one +// requires string `input`, so an Anthropic body can never match it either. +const RESPONSES_SERVER_SEARCH_TOOL_TYPES: ReadonlySet = new Set(["web_search", "web_search_preview", "x_search"]); + +export function isResponsesServerToolSingleShot(parsed: unknown): boolean { + if (!parsed || typeof parsed !== "object") return false; + const p = parsed as Record; + if (typeof p.input !== "string") return false; + if (typeof p.previous_response_id === "string" && p.previous_response_id !== "") return false; + if (typeof p.session_id === "string" && p.session_id !== "") return false; + if (typeof p.prompt_cache_key === "string" && p.prompt_cache_key !== "") return false; + const tools = p.tools; + if (!Array.isArray(tools) || tools.length === 0) return false; + const declared = new Set(); + for (const t of tools) { + if (t === null || typeof t !== "object") return false; + const type = (t as { type?: unknown }).type; + if (typeof type !== "string" || !RESPONSES_SERVER_SEARCH_TOOL_TYPES.has(type)) return false; + declared.add(type); + } + const choice = p.tool_choice; + if (choice === undefined) return true; + if (choice === "auto" || choice === "required") return true; + if (choice !== null && typeof choice === "object") { + const c = choice as { type?: unknown }; + if (typeof c.type === "string" && declared.has(c.type)) return true; + } + return false; +} + const STRUCTURED_FORMAT_TYPES: ReadonlySet = new Set(["json_schema", "json_object"]); /** #5073: structural identification of a tool-less structured-output utility diff --git a/tests/e2e-session-identity.test.ts b/tests/e2e-session-identity.test.ts index 289709ff7d..b10ccbb674 100644 --- a/tests/e2e-session-identity.test.ts +++ b/tests/e2e-session-identity.test.ts @@ -375,6 +375,55 @@ test("e2e session identity: anonymous degenerate request (no history signal) → } }); +test("e2e session identity: anonymous single-shot Responses server-tool call forwards verbatim, no session (#6548)", async () => { + const h = await startHarness(); + try { + // pi-web-access's xAI web-search backend: string input, web_search / + // x_search tools only, no history, and no knob for a session id — the + // client cannot carry an identity here. Pre-fix the #286 refusal + // hard-400d it; post-fix it rides the verbatim forward like any + // utility call and creates no session state. + const body = { model: "gpt-test", stream: true, input: "latest news on context compression", tools: [{ type: "web_search" }, { type: "x_search" }] }; + const resp = await postRaw(h, 0, "/v1/responses", body, { authorization: "Bearer " }); + assert.equal(resp.status, 200, `proxy returned ${resp.status}: ${await resp.text()}`); + assert.equal((await getSessions(h)).length, 0, "verbatim forward must not create a session"); + } finally { + await h.close(); + } +}); + +test("e2e session identity: anonymous Responses single-shot shape with a client tool still 400s (#6548 negative)", async () => { + const h = await startHarness(); + try { + // A function tool makes it an agent turn, not a server-tool utility + // call — the #286 refusal still applies. + const body = { model: "gpt-test", stream: true, input: "what is the weather", tools: [{ type: "function", name: "get_weather", description: "weather", parameters: { type: "object", properties: {} } }] }; + const resp = await postRaw(h, 0, "/v1/responses", body, { authorization: "Bearer " }); + assert.equal(resp.status, 400); + const json = (await resp.json()) as { error?: { message?: string } }; + assert.match(json.error?.message ?? "", /Missing stable conversation identity/); + assert.equal((await getSessions(h)).length, 0); + } finally { + await h.close(); + } +}); + +test("e2e session identity: anonymous Responses search call chaining previous_response_id still 400s (#6548 negative)", async () => { + const h = await startHarness(); + try { + // previous_response_id chains the call onto a prior response — a + // conversation continuation, not a single shot. + const body = { model: "gpt-test", stream: true, input: "follow-up search", tools: [{ type: "web_search" }], previous_response_id: "resp_abc" }; + const resp = await postRaw(h, 0, "/v1/responses", body, { authorization: "Bearer " }); + assert.equal(resp.status, 400); + const json = (await resp.json()) as { error?: { message?: string } }; + assert.match(json.error?.message ?? "", /Missing stable conversation identity/); + assert.equal((await getSessions(h)).length, 0); + } finally { + await h.close(); + } +}); + test("e2e session identity: anonymous openai request → prefix-affinity session; distinct conversations split (#309)", async () => { const h = await startHarness(); try { diff --git a/tests/side-lane-invariants.test.ts b/tests/side-lane-invariants.test.ts index 2f9501f7c2..a523e9b235 100644 --- a/tests/side-lane-invariants.test.ts +++ b/tests/side-lane-invariants.test.ts @@ -349,7 +349,11 @@ test("(F) extracted protocol modules keep the side-lane boundary (#1440 P2 cut 2 // isDshSingleShotSidecar — same category once more: the generic single-shot // dsh sidecar shape predicate (no tools + one-shot body, any system text) // consulted to set sideIntent and to keep unknown sidecars off the raw-key -// anchor before the first main turn can claim it. +// anchor before the first main turn can claim it. #6548 adds +// isResponsesServerToolSingleShot — same category once more: the Responses +// server-tool-only single-shot shape predicate (string input + search tools, +// no history) consulted by the #286 refusal branch, which forwards that shape +// verbatim instead of hard-400ing a call that cannot carry an identity. // #4774 raw-key anchor race. #5073 adds // isStructuredOutputCall — same category once more: a pure toolless // structured-output shape predicate (no threshold, no session state) consulted @@ -359,7 +363,7 @@ test("(G) extracted pipeline module keeps the side-lane boundary (#1440 P2 cut 3 const text = readFileSync(fileURLToPath(new URL("../src/server/handle.ts", import.meta.url)), "utf8"); const laneInternals = /\bSIDE_REQUEST_MAX_TOKENS\b|\bSIDE_REQUEST_AGENTS\b|\bDemoteGateSignals\b|\bSideLaneSignals\b|\bSideLaneDecision\b|\bBILI_TOOL_NAMES\b|\bOutputBudgetField\b|\boutputBudgetField\b|\breadOutputBudget\b|\bwriteOutputBudget\b|\b_resetNoOutputCeilingWarningsForTest\b/; assert.doesNotMatch(text, laneInternals, "handle.ts must stay decoupled from the side-lane engine internals"); - const allowlist = new Set(["hasLeakedBiliToolsOnly", "hasSingleShotBudget", "isClaudeCodeTitleRequest", "isDshAutoReviewRequest", "isDshSingleShotSidecar", "isDshTitleRequest", "isServerToolUtilityCall", "isSideRequest", "isStructuredOutputCall", "resolveSideLane", "demoteGate", "stripLeakedBiliTools", "restoreOutputBudget", "sideRequestGuard"]); + const allowlist = new Set(["hasLeakedBiliToolsOnly", "hasSingleShotBudget", "isClaudeCodeTitleRequest", "isDshAutoReviewRequest", "isDshSingleShotSidecar", "isDshTitleRequest", "isResponsesServerToolSingleShot", "isServerToolUtilityCall", "isSideRequest", "isStructuredOutputCall", "resolveSideLane", "demoteGate", "stripLeakedBiliTools", "restoreOutputBudget", "sideRequestGuard"]); let sawImport = false; for (const m of text.matchAll(/\{([^}]*)\}\s*from\s*["'][^"']*side-request\.js["']/g)) { sawImport = true; diff --git a/tests/websearch-side-request.test.ts b/tests/websearch-side-request.test.ts index 82772cde17..c21b3a8bdb 100644 --- a/tests/websearch-side-request.test.ts +++ b/tests/websearch-side-request.test.ts @@ -4,7 +4,7 @@ import http from "node:http"; import { once } from "node:events"; import { defaultConfig, createInitialState } from "acp-kernel"; import { startServer } from "../src/server.ts"; -import { isServerToolUtilityCall, isSideRequest, resolveSideLane, SIDE_REQUEST_MAX_TOKENS } from "../src/server/side-request.ts"; +import { isResponsesServerToolSingleShot, isServerToolUtilityCall, isSideRequest, resolveSideLane, SIDE_REQUEST_MAX_TOKENS } from "../src/server/side-request.ts"; import type { ProxyOptions } from "../src/config.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; @@ -72,6 +72,49 @@ test("isServerToolUtilityCall: negatives — every veto keeps the request on the assert.equal(isServerToolUtilityCall(searchBody({ tools: [{ type: "web_search", name: "web_search" }] })), false, "unversioned web_search type never matches"); }); +// #6548: pi-web-access's xAI web-search backend sends a deliberately minimal +// single-shot Responses body — string input, web_search/x_search tools only, +// no message history — and pi-web-access exposes no knob for a session id, +// so the client cannot carry an identity. The proxy's #286 refusal hard-400s +// it before the side lane ever sees it. The Responses-wire predicate below is +// the shape the refusal branch consults. +function responsesSearchBody(over: Record = {}): Record { + return { + model: "grok-4.7", + input: "latest news on context compression", + tools: [{ type: "web_search" }, { type: "x_search" }], + ...over, + }; +} + +test("isResponsesServerToolSingleShot: positive — the captured pi-web-access shape (#6548)", () => { + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody()), true, "captured shape (web_search + x_search, string input)"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tools: [{ type: "web_search" }] })), true, "web_search alone"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tools: [{ type: "web_search_preview" }] })), true, "web_search_preview family"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tool_choice: "auto" })), true, "string auto"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tool_choice: "required" })), true, "string required forces one of the declared server tools"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tool_choice: { type: "web_search" } })), true, "object forcing a DECLARED server tool"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tool_choice: undefined })), true, "absent tool_choice"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ instructions: "Answer concisely." })), true, "instructions carry no history"); +}); + +test("isResponsesServerToolSingleShot: negatives — every veto keeps the request under the #286 refusal", () => { + assert.equal(isResponsesServerToolSingleShot(null), false, "null body"); + assert.equal(isResponsesServerToolSingleShot({}), false, "empty body"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ input: [{ type: "message", role: "user", content: [{ type: "input_text", text: "q" }] }] })), false, "array input is message history, not a single shot"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ input: undefined })), false, "missing input"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tools: [{ type: "function", name: "get_weather" }] })), false, "client function tool does not match"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tools: [{ type: "web_search" }, { type: "function", name: "get_weather" }] })), false, "mixing ANY client-side tool vetoes"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tools: [] })), false, "empty tools array"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tools: [null] })), false, "non-object tool entry"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tool_choice: "none" })), false, "string none vetoes"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tool_choice: { type: "function", name: "get_weather" } })), false, "forcing a client tool vetoes"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tool_choice: { type: "required" } })), false, "unrecognized tool_choice shape vetoes"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ previous_response_id: "resp_abc" })), false, "chained continuation is a conversation turn, not a single shot"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ session_id: "sess-1" })), false, "body session_id is an identity signal"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ prompt_cache_key: "pck-1" })), false, "prompt_cache_key is an identity signal"); +}); + test("isSideRequest: the structural shape outranks the #546 tools veto; explicit intent still outranks the structure", () => { const body = searchBody(); assert.equal(isSideRequest(body), true, "server-tool-only utility call is a side request despite non-empty tools and a large budget");