From 521b756d4d1c7d892605578335578edc2b172c58 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 31 Oct 2026 07:04:42 +0800 Subject: [PATCH 1/3] fix: veto strict reasoning echo when the upstream rejects the field as extra (#5260) --- src/loop/core.ts | 16 ++- src/server/relay.ts | 17 ++- src/strict-echo.ts | 28 +++- tests/issue5260-strict-echo-veto.test.ts | 166 +++++++++++++++++++++++ tests/wire-contract-fakes.ts | 38 +++++- tests/wire-contract.test.ts | 9 +- 6 files changed, 266 insertions(+), 8 deletions(-) create mode 100644 tests/issue5260-strict-echo-veto.test.ts diff --git a/src/loop/core.ts b/src/loop/core.ts index fa301f487c..b26a61bddc 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -44,7 +44,7 @@ import { shedEgressIfOverBudget } from "../egress-shed.js"; import { DUMP_PLAINTEXT_BANNER } from "../dump-banner.js"; import { bodyDumpEnabled as knobBodyDumpEnabled, imageRescaleMaxDim } from "../knobs.js"; import { dumpsDir } from "../paths.js"; -import { isStrictReasoningEcho, modelIdOf, normalizeStrictEchoBody } from "../strict-echo.js"; +import { isReasoningEchoForbiddenReject, isStrictReasoningEcho, modelIdOf, normalizeStrictEchoBody } from "../strict-echo.js"; import { log as loggerLog } from "../logger.js"; import { promptInputTotal, type WireProtocol } from "../util.js"; import { DEGENERATE_RETRY_NUDGE } from "../degenerate-retry.js"; @@ -1898,12 +1898,24 @@ export async function* runCompressLoop( // #2169: match BOTH field spellings — the chat wire says // reasoning_content, DeepSeek's Responses wire says reasoning_text. // #5495: DeepSeek's Anthropic-wire endpoint spells it content[].thinking. - if ( + // #5260: mirror image of the learn below — a 4xx that rejects the + // echo field AS EXTRA proves this gateway forbids the field bili + // injected (Mistral serving deepseek-named models); veto strict + // echo for the session so the next re-request goes out clean. + // Demand-class evidence below clears the veto again. + if (isReasoningEchoForbiddenReject(e.status, e.body)) { + if (ctx.session.metadata.strictReasoningEchoVeto !== true) { + ctx.session.metadata.strictReasoningEchoVeto = true; + ctx.log(`[acp-loop] upstream ${e.status} rejects the reasoning echo field as extra — vetoed strict reasoning echo for this session (#5260)`); + loggerLog("warn", `[acp-loop] vetoed strictReasoningEcho (session ${ctx.session.id}); blank-echo injection disabled until opposite evidence (#5260)`); + } + } else if ( e.status === 400 && /reasoning_(?:content|text)|content\[\]\.thinking/i.test(e.body) && ctx.session.metadata.strictReasoningEcho !== true ) { ctx.session.metadata.strictReasoningEcho = true; + delete ctx.session.metadata.strictReasoningEchoVeto; ctx.log(`[acp-loop] 400 mentions a reasoning echo field — learned strict reasoning echo for this session; #651 reasoning-drop disabled (#684/#2169)`); loggerLog("warn", `[acp-loop] learned strictReasoningEcho (session ${ctx.session.id}); reasoning-drop disabled (#684)`); } diff --git a/src/server/relay.ts b/src/server/relay.ts index a41da78297..bad7fb040a 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -59,6 +59,7 @@ import { rewriteOpenaiJsonResponseAsync } from "../stream-openai.js"; import { rewriteResponsesJsonResponseAsync } from "../stream-responses.js"; import { observeResponsesTerminalState } from "../stream-terminal.js"; import { rewriteJsonResponseAsync, type RewriteCtx } from "../stream.js"; +import { isReasoningEchoForbiddenReject } from "../strict-echo.js"; import { safePrefix, safeSuffix, scrubLoneSurrogatesOnWire } from "../text-safe.js"; import { clearUpstreamAlertsForHost, noteCircuitFailFastLog, recordUpstreamAlert, recordUpstreamCircuitFailure, recordUpstreamCircuitSuccess, upstreamCircuitBlockReason, upstreamCircuitLastFailureKind, upstreamCircuitRetryAfterSeconds } from "../upstream-alerts.js"; import { describeUpstreamFailure, formatUpstreamError, proxyDispatcher, recordUpstreamConnection } from "../upstream-proxy.js"; @@ -2121,9 +2122,23 @@ const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepare // #2169: match BOTH field spellings — the chat wire says // reasoning_content, DeepSeek's Responses wire says reasoning_text. // #5495: DeepSeek's Anthropic-wire endpoint spells it content[].thinking. - if (upstream.status === 400 && /reasoning_(?:content|text)|content\[\]\.thinking/i.test(errText)) { + // #5260: mirror image of the learn below — a 4xx that rejects the echo + // field AS EXTRA proves this gateway's schema does not accept the very + // field bili injected (Mistral serving deepseek-named models: static + // detection #1027 matched the model id, but the message schema answers + // extra_forbidden on every tool-call turn). Veto strict echo for the + // session so the retry goes out clean; opposite-direction evidence + // (the demand-class learn below) clears the veto again. + if (isReasoningEchoForbiddenReject(upstream.status, errText)) { + if (s.metadata.strictReasoningEchoVeto !== true) { + s.metadata.strictReasoningEchoVeto = true; + markDirty(s); + log("warn", `[${s.id}] upstream ${upstream.status} rejects the reasoning echo field as extra — vetoed strictReasoningEcho for this session (#5260); blank-echo injection disabled until opposite evidence`); + } + } else if (upstream.status === 400 && /reasoning_(?:content|text)|content\[\]\.thinking/i.test(errText)) { if (s.metadata.strictReasoningEcho !== true) { s.metadata.strictReasoningEcho = true; + delete s.metadata.strictReasoningEchoVeto; markDirty(s); log("warn", `[${s.id}] upstream 400 mentions a reasoning echo field — learned strictReasoningEcho for this session (#684/#762/#2169); reasoning-drop disabled`); } diff --git a/src/strict-echo.ts b/src/strict-echo.ts index c69b3bdf60..3b4c8d8744 100644 --- a/src/strict-echo.ts +++ b/src/strict-echo.ts @@ -2,6 +2,26 @@ import type { Session } from "./session.js"; import type { OpenAIMessage, ResponseInputItem } from "acp-kernel/wire"; import type { Logger } from "./logger.js"; +const REASONING_ECHO_FIELD = /reasoning_(?:content|text)/i; + +/** [#5260] The rejection wording that proves an upstream FORBIDS the reasoning + * echo field as extra (as opposed to demanding it be present). Mistral's + * Pydantic schema answers 422 extra_forbidden with "Extra inputs are not + * permitted"; OpenAI-style validators say "Unrecognized request argument". */ +const REASONING_ECHO_EXTRA_REJECT = /extra[_\s]?(?:inputs?|fields?)|not permitted|unrecognized|unknown field/i; + +/** [#5260] A 4xx whose body names the reasoning echo field AND rejects it as + * an extra input — the gateway's own schema does not accept the very field + * bili injected (Mistral serving deepseek-named models: static detection + * #1027 matched the model id, but the message schema forbids the field). + * Only this shape may veto strict echo; a demand-class rejection ("must be + * passed back") contains no extra-marker and keeps learning ON. */ +export function isReasoningEchoForbiddenReject(status: number, body: string): boolean { + if (status < 400 || status >= 500) return false; + if (!REASONING_ECHO_FIELD.test(body)) return false; + return REASONING_ECHO_EXTRA_REJECT.test(body); +} + /** [#684] Strict-echo reasoning upstreams: DeepSeek documents that * thinking-mode "reasoning_content ... must be passed back to the API" — * a rebuilt request whose assistant tool-call turns lost their reasoning is @@ -10,8 +30,14 @@ import type { Logger } from "./logger.js"; * reasoning_text, #2169 — see the loop's UpstreamHttpError handler), then static * detection: the upstream origin OR the request's own model id (#1027 — * DeepSeek models served from non-deepseek gateways never trip the host - * check, so every fresh session re-paid the 400 through the learned flag). */ + * check, so every fresh session re-paid the 400 through the learned flag). + * [#5260] Veto wins over BOTH: when the upstream itself rejected the injected + * field as extra (isReasoningEchoForbiddenReject), strict echo stays off for + * the session even though static/learned evidence says on — the gateway's + * schema verdict outranks the name-based heuristics (Mistral + deepseek-named + * models 422'd on every tool-call turn until this landed). */ export function isStrictReasoningEcho(session: Session, upstreamOrigin: string | undefined, model?: string): boolean { + if (session.metadata.strictReasoningEchoVeto === true) return false; if (session.metadata.strictReasoningEcho === true) return true; if (upstreamOrigin !== undefined && /deepseek/i.test(upstreamOrigin)) return true; return typeof model === "string" && model.length > 0 && /deepseek/i.test(model); diff --git a/tests/issue5260-strict-echo-veto.test.ts b/tests/issue5260-strict-echo-veto.test.ts new file mode 100644 index 0000000000..c61443a090 --- /dev/null +++ b/tests/issue5260-strict-echo-veto.test.ts @@ -0,0 +1,166 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { once } from "node:events"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { setLogCapture } from "../src/logger.ts"; +import { createInitialState } from "acp-kernel"; +import type { Session } from "../src/session.js"; +import { isStrictReasoningEcho } from "../src/server.js"; +import { isReasoningEchoForbiddenReject } from "../src/strict-echo.js"; +import { startFakeUpstream } from "./wire-contract-fakes.ts"; + +// #5260: Mistral serving a deepseek-named model (pi plugin-native, bili 0.1.209). +// The static /deepseek/i detection (#1027) makes bili inject a blank +// reasoning_content on every assistant tool-call message; Mistral's schema +// forbids the field entirely and answers 422 extra_forbidden on every +// tool-call turn — permanently, because nothing ever learned the gateway +// rejects the field. Fix: learn-on-failure VETO — a 4xx that names the echo +// field AND rejects it as extra turns strict echo off for the session +// (mirroring the #684/#762 positive learning in the opposite direction). +// Ledger: WC-023 (tests/wire-contract-fakes.ts), enforced here through the +// mistral-lane validation-parity fake. + +function fakeSession(over: Record = {}): Session { + return { + id: "s1", + meta: {}, + state: createInitialState(), + stats: { requests: 0 }, + metadata: { ...over }, + } as unknown as Session; +} + +const MISTRAL_422 = JSON.stringify({ + detail: [{ type: "extra_forbidden", loc: ["body", "messages", 5, "assistant", "reasoning_content"], msg: "Extra inputs are not permitted", input: "" }], +}); + +const DEEPSEEK_DEMAND_400 = JSON.stringify({ + error: { code: 11155, message: "The 'reasoning_content' in the thinking mode must be passed back to the API" }, +}); + +test("#5260: isReasoningEchoForbiddenReject — the exact Mistral shape vetoes", () => { + assert.equal(isReasoningEchoForbiddenReject(422, MISTRAL_422), true); + // OpenAI-style wording for the same class of rejection. + assert.equal(isReasoningEchoForbiddenReject(422, JSON.stringify({ error: { message: "Unrecognized request argument supplied: reasoning_content" } })), true); + assert.equal(isReasoningEchoForbiddenReject(400, JSON.stringify({ error: { message: "Unknown field: reasoning_text" } })), true); +}); + +test("#5260: demand-class and unrelated rejections never veto", () => { + // DeepSeek's own 400 DEMANDS the field be echoed back — the opposite evidence class. + assert.equal(isReasoningEchoForbiddenReject(400, DEEPSEEK_DEMAND_400), false); + // Field named but no extra-marker wording. + assert.equal(isReasoningEchoForbiddenReject(400, JSON.stringify({ error: { message: "reasoning_content is invalid for this model" } })), false); + // Extra-marker wording but no echo-field mention. + assert.equal(isReasoningEchoForbiddenReject(422, JSON.stringify({ detail: [{ type: "extra_forbidden", loc: ["body", "foo"], msg: "Extra inputs are not permitted", input: "" }] })), false); + // Non-4xx statuses never veto. + assert.equal(isReasoningEchoForbiddenReject(200, MISTRAL_422), false); + assert.equal(isReasoningEchoForbiddenReject(500, MISTRAL_422), false); +}); + +test("#5260: the veto outranks both learned and static evidence", () => { + // Static detection alone still matches (regression guard for #1027). + assert.equal(isStrictReasoningEcho(fakeSession(), "https://api.mistral.ai", "deepseek-v4-1-flash-1017"), true); + // Learned flag alone still wins (regression guard for #684/#762). + assert.equal(isStrictReasoningEcho(fakeSession({ strictReasoningEcho: true }), "https://openrouter.ai"), true); + // Veto beats the static match — the reporter's exact case. + assert.equal(isStrictReasoningEcho(fakeSession({ strictReasoningEchoVeto: true }), "https://api.mistral.ai", "deepseek-v4-1-flash-1017"), false); + // Veto beats the learned flag too. + assert.equal(isStrictReasoningEcho(fakeSession({ strictReasoningEcho: true, strictReasoningEchoVeto: true }), "https://api.deepseek.com"), false); +}); + +interface Captured { + level: string; + msg: string; +} + +// The follow-up request carrying the first tool call — the exact failure point +// of the report. One assistant tool-call message, lacking the echo field. +const CLIENT_MESSAGES = [ + { role: "user", content: "what is the weather" }, + { role: "assistant", content: "", tool_calls: [{ id: "c1", type: "function", function: { name: "search", arguments: "{\"q\":\"weather\"}" } }] }, + { role: "tool", tool_call_id: "c1", content: "sunny" }, +]; + +test("#5260: main-path e2e — one 422 vetoes strict echo, the next request goes out clean", async () => { + const captured: Captured[] = []; + setLogCapture((level, msg) => captured.push({ level, msg })); + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const fake = await startFakeUpstream("openai-chat", { lane: "mistral" }); + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: fake.url, + routes: { + [fake.url]: { models: { "deepseek-v4-1-flash-1017": { context: 1_048_576 } } }, + }, + modelContextLimit: 1_048_576, + kernelConfig: defaultConfig(1_048_576), + compress: { injectTool: false, injectNudge: false }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: true, + debug: false, + passthrough: false, + passthroughSource: null, + chainContentDetection: false, + autoUpdate: false, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: true, + releaseNotesCheck: true, + compat: { roles: {} }, + streamErrorShape: "protocol", + mitm: { enabled: false, domains: [] }, + }; + const proxy = await startServer(opts); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + try { + const post = () => fetch(`http://127.0.0.1:${proxyPort}/bili/${fake.url}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "re5260-1" }, + body: JSON.stringify({ model: "deepseek-v4-1-flash-1017", messages: CLIENT_MESSAGES }), + }); + + // Request 1: static detection (#1027) injects the blank echo — the fake, + // enforcing Mistral's schema, rejects it exactly like api.mistral.ai did. + const resp1 = await post(); + assert.equal(resp1.status, 422); + const err1 = JSON.parse(await resp1.text()) as { detail?: Array<{ type?: string; msg?: string }> }; + assert.equal(err1.detail?.[0]?.type, "extra_forbidden"); + assert.ok(err1.detail?.[0]?.msg?.includes("reasoning_content"), "relayed 422 names the rejected field"); + + assert.equal(fake.violations.length, 1, `expected exactly one WC-023 violation, got: ${fake.violations.join(" | ")}`); + assert.match(fake.violations[0]!, /WC-023/); + const first = fake.requests[0]!.body as { messages: Record[] }; + const firstTc = first.messages.find((m) => Array.isArray(m.tool_calls)); + assert.ok(firstTc, "first forwarded body carries the tool-call message"); + assert.equal(firstTc!.reasoning_content, "", "request 1 shows the static-detection injection"); + + const vetoed = captured.find((c) => c.level === "warn" && c.msg.includes("vetoed strictReasoningEcho")); + assert.ok(vetoed, `expected the veto learn-on-failure log, got: ${captured.map((c) => c.msg).join(" | ")}`); + + // Request 2 (same session): the veto holds — no injection, clean accept. + const resp2 = await post(); + assert.equal(resp2.status, 200); + await resp2.text(); + + assert.equal(fake.violations.length, 1, "the second request must NOT re-violate the Mistral schema"); + const second = fake.requests[1]!.body as { messages: Record[] }; + const secondTc = second.messages.find((m) => Array.isArray(m.tool_calls)); + assert.ok(secondTc, "second forwarded body carries the tool-call message"); + assert.ok(!("reasoning_content" in secondTc!), "second forwarded body is free of the forbidden field"); + + const injections = captured.filter((c) => c.msg.includes("injected blank reasoning_content")); + assert.equal(injections.length, 1, `exactly one injection (request 1), got: ${injections.map((c) => c.msg).join(" | ")}`); + } finally { + await new Promise((resolve, reject) => proxy.close((e) => (e ? reject(e) : resolve()))); + await fake.close(); + setLogCapture(null); + } +}); diff --git a/tests/wire-contract-fakes.ts b/tests/wire-contract-fakes.ts index 9b6797ee1b..268ea39347 100644 --- a/tests/wire-contract-fakes.ts +++ b/tests/wire-contract-fakes.ts @@ -82,6 +82,15 @@ export const WIRE_RULES: readonly WireRule[] = [ provenance: 'live probe 2026-10-20 (pi 1.3.0 + pi-subagents 0.79.2, api.anthropic.com, claude-haiku-5-5) through a master b156c02f proxy: pi declares tools activated mid-session as tool_addition blocks inside a mid-conversation role:"system" message; #3410\'s unknown-block passthrough kept the blocks but coreToAnthropic re-emitted the message as role:"user" → upstream 400 "messages.2.content: \'tool_addition\'/\'tool_removal\' blocks are only permitted within `role: "system"` messages". On v0.1.202 the blocks were silently dropped instead (tools never reached the model). Repair: kernel/src/wire/anthropic.ts keeps each wire system message whole on the rawAnthropicMessage sidecar and re-emits it verbatim.', }, + { + id: "WC-023", + wire: "openai-chat", + lane: "mistral", + summary: + "Mistral's /v1/chat/completions message schema forbids unknown fields — a strict-echo blank reasoning_content on an assistant tool-call message 422s with extra_forbidden; such gateways must never receive the injected field, and their own rejection of it is bili's veto evidence", + provenance: + "bili #5260 production 422 (pi 1.4.0 plugin-native through api.mistral.ai, model deepseek-v4-1-flash-1017, bili 0.1.209): the static /deepseek/i detection (#1027) made normalizeStrictEchoReasoning inject reasoning_content:'' on every assistant tool-call message; Mistral rejected the whole body with {\"type\":\"extra_forbidden\",\"loc\":[\"body\",\"messages\",5,\"assistant\",\"reasoning_content\"],\"msg\":\"Extra inputs are not permitted\"} on every tool-call turn, while the same session succeeded immediately after switching to mistral-large-4 (no name match). Repair: learn-on-failure veto in src/strict-echo.ts (isReasoningEchoForbiddenReject + session.metadata.strictReasoningEchoVeto), mirroring the #684/#762 positive learning.", + }, { id: "WC-016", wire: "anthropic", @@ -758,7 +767,7 @@ export interface CapturedRequest { /** Lane-scoped fakes: a constraint that holds only on one client's or endpoint's lane lives in that lane's own * fake, not in the wire's shared VALIDATOR (WC-017's rule-scoping precedent). */ -type FakeLane = "vertex-anthropic"; +type FakeLane = "vertex-anthropic" | "mistral"; interface FakeUpstream { wire: Wire; @@ -933,6 +942,27 @@ export function vertexErrorEnvelope(streaming: boolean, message: string): { stat return { status: 400, body: streaming ? JSON.stringify([err]) : JSON.stringify(err) }; } +/** WC-023, lane-scoped: Mistral's message schema has no reasoning_content field — bili's strict-echo blank + * injection on assistant tool-call messages 422s with extra_forbidden (#5260). The shared openai-chat + * VALIDATOR must not carry this clause: OpenAI itself accepts the field on thinking models. */ +export function validateMistralChatBody(body: unknown): string[] { + const out: string[] = []; + if (!isPlainObject(body)) return out; + const messages = body.messages; + if (!Array.isArray(messages)) return out; + messages.forEach((m, i) => { + if (isPlainObject(m) && "reasoning_content" in m) + out.push(`WC-023 messages[${i}]: reasoning_content is not a field of Mistral's message schema — 422 extra_forbidden "Extra inputs are not permitted" (#5260)`); + }); + return out; +} + +/** WC-023 lane: Mistral answers FastAPI-style 422 with a detail array — the exact envelope shape of the #5260 + * rejection (loc generalized to ["body"]; the field name rides in msg so the relay's veto predicate can read it). */ +export function mistralErrorEnvelope(message: string): { status: number; body: string } { + return { status: 422, body: JSON.stringify({ detail: [{ type: "extra_forbidden", loc: ["body"], msg: message, input: "" }] }) }; +} + const PATH_MATCHERS: Record boolean> = { anthropic: (u) => u.startsWith("/v1/messages"), "openai-chat": (u) => u.startsWith("/v1/chat/completions"), @@ -946,7 +976,7 @@ export async function startFakeUpstream(wire: Wire, opts?: { replyText?: string; const lane = opts?.lane; const requests: CapturedRequest[] = []; const violations: string[] = []; - const validate = lane === "vertex-anthropic" ? validateVertexAnthropicBody : VALIDATORS[wire]; + const validate = lane === "vertex-anthropic" ? validateVertexAnthropicBody : lane === "mistral" ? validateMistralChatBody : VALIDATORS[wire]; const pathOk = lane === "vertex-anthropic" ? (u: string) => vertexAnthropicPathInfo(u) !== null : PATH_MATCHERS[wire]; const server = http.createServer((req, res) => { @@ -977,7 +1007,9 @@ export async function startFakeUpstream(wire: Wire, opts?: { replyText?: string; const env = lane === "vertex-anthropic" ? vertexErrorEnvelope(streaming === true, `wire-contract violation(s): ${found.join("; ")}`) - : errorEnvelope(wire, `wire-contract violation(s): ${found.join("; ")}`); + : lane === "mistral" + ? mistralErrorEnvelope(`wire-contract violation(s): ${found.join("; ")}`) + : errorEnvelope(wire, `wire-contract violation(s): ${found.join("; ")}`); res.writeHead(env.status, { "content-type": "application/json" }); res.end(env.body); return; diff --git a/tests/wire-contract.test.ts b/tests/wire-contract.test.ts index bda8c0071e..77549f3535 100644 --- a/tests/wire-contract.test.ts +++ b/tests/wire-contract.test.ts @@ -53,7 +53,7 @@ import { RULE_TOOL_GOOGLE, retrieveToolsFor, } from "../src/compress-tool.ts"; -import { verifySdkHmac, WIRE_RULES, WIRE_HEADER_RULES, VALIDATORS, startFakeUpstream, validateResponsesWsCreate, validateCodexResponsesWsCreate, validateGoSessionHeaders, validateVertexAnthropicBody, vertexAnthropicPathInfo, vertexErrorEnvelope, type Wire } from "./wire-contract-fakes.ts"; +import { verifySdkHmac, WIRE_RULES, WIRE_HEADER_RULES, VALIDATORS, startFakeUpstream, validateResponsesWsCreate, validateCodexResponsesWsCreate, validateGoSessionHeaders, validateVertexAnthropicBody, validateMistralChatBody, vertexAnthropicPathInfo, vertexErrorEnvelope, type Wire } from "./wire-contract-fakes.ts"; type ToolShape = Record; @@ -388,6 +388,13 @@ test("wire-contract ledger: every lane-scoped rule has a live enforcement clause Array.isArray(JSON.parse(vertexErrorEnvelope(streaming === true, "x").body)) ? ["WC-021 stream errors are array-wrapped"] : [], body: true, }, + // WC-023 is the Mistral lane — its message schema forbids the strict-echo field bili injects on + // assistant tool-call messages (#5260). End-to-end assertions live in tests/issue5260-strict-echo-veto.test.ts. + { + ruleId: "WC-023", + validate: validateMistralChatBody, + body: { messages: [{ role: "assistant", content: "", tool_calls: [{ id: "c1" }], reasoning_content: "" }] }, + }, ]; 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`); From cecbb261507026087c95764f3e9a96e58650169f Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 31 Oct 2026 08:00:50 +0800 Subject: [PATCH 2/3] fix: drop unused export on mistralErrorEnvelope (unused-exports gate) --- tests/wire-contract-fakes.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/wire-contract-fakes.ts b/tests/wire-contract-fakes.ts index 268ea39347..7c951581ed 100644 --- a/tests/wire-contract-fakes.ts +++ b/tests/wire-contract-fakes.ts @@ -959,7 +959,7 @@ export function validateMistralChatBody(body: unknown): string[] { /** WC-023 lane: Mistral answers FastAPI-style 422 with a detail array — the exact envelope shape of the #5260 * rejection (loc generalized to ["body"]; the field name rides in msg so the relay's veto predicate can read it). */ -export function mistralErrorEnvelope(message: string): { status: number; body: string } { +function mistralErrorEnvelope(message: string): { status: number; body: string } { return { status: 422, body: JSON.stringify({ detail: [{ type: "extra_forbidden", loc: ["body"], msg: message, input: "" }] }) }; } From e958f86ac7166f25635579ed4817515bccdb0634 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 31 Oct 2026 10:08:48 +0800 Subject: [PATCH 3/3] fix: make the strict-echo veto clear its own transition (#5260 review) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review finding: both learn sites nested the veto clear inside the learned-flag flip guard (relay demand branch / loop UpstreamHttpError else-if). A session holding BOTH evidence classes — learned ON from an earlier demand 400, later vetoed by an extra-forbidden 4xx — could never clear its veto: re-demand evidence arriving while the flag was already true did nothing, so every tool-call turn 400'd forever in the opposite direction (the mirror image of #5260's stuck loop). The veto clear is now an independent transition in src/server/relay.ts and src/loop/core.ts, with its own warn log and markDirty, so the two evidence classes keep flipping the session either way. Regression test drives the full flip through the real relay (demand → forbid → re-demand → accept) and fails on the pre-fix code. --- src/loop/core.ts | 22 +++-- src/server/relay.ts | 11 ++- tests/issue5260-strict-echo-veto.test.ts | 119 +++++++++++++++++++++++ 3 files changed, 145 insertions(+), 7 deletions(-) diff --git a/src/loop/core.ts b/src/loop/core.ts index b26a61bddc..0e741935f5 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -1911,13 +1911,23 @@ export async function* runCompressLoop( } } else if ( e.status === 400 && - /reasoning_(?:content|text)|content\[\]\.thinking/i.test(e.body) && - ctx.session.metadata.strictReasoningEcho !== true + /reasoning_(?:content|text)|content\[\]\.thinking/i.test(e.body) ) { - ctx.session.metadata.strictReasoningEcho = true; - delete ctx.session.metadata.strictReasoningEchoVeto; - ctx.log(`[acp-loop] 400 mentions a reasoning echo field — learned strict reasoning echo for this session; #651 reasoning-drop disabled (#684/#2169)`); - loggerLog("warn", `[acp-loop] learned strictReasoningEcho (session ${ctx.session.id}); reasoning-drop disabled (#684)`); + if (ctx.session.metadata.strictReasoningEcho !== true) { + ctx.session.metadata.strictReasoningEcho = true; + ctx.log(`[acp-loop] 400 mentions a reasoning echo field — learned strict reasoning echo for this session; #651 reasoning-drop disabled (#684/#2169)`); + loggerLog("warn", `[acp-loop] learned strictReasoningEcho (session ${ctx.session.id}); reasoning-drop disabled (#684)`); + } + // #5260 review: the veto clear is its OWN transition — folding + // it into the flag-flip condition above would leave a session + // that learned ON and was later vetoed stuck vetoed forever + // (demand evidence arriving while the flag is already true + // cleared nothing → permanent 400 loop in the opposite direction). + if (ctx.session.metadata.strictReasoningEchoVeto === true) { + delete ctx.session.metadata.strictReasoningEchoVeto; + ctx.log(`[acp-loop] 400 demands the reasoning echo field again — cleared strict reasoning echo veto (#5260)`); + loggerLog("warn", `[acp-loop] cleared strictReasoningEchoVeto (session ${ctx.session.id}) on demand-class evidence (#5260)`); + } } // #762: persist the exact re-requested body on 4xx (env-gated: BILI_DUMP_4XX=1). if (e.status >= 400 && e.status < 500) { diff --git a/src/server/relay.ts b/src/server/relay.ts index bad7fb040a..b4811598b7 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -2138,10 +2138,19 @@ const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepare } else if (upstream.status === 400 && /reasoning_(?:content|text)|content\[\]\.thinking/i.test(errText)) { if (s.metadata.strictReasoningEcho !== true) { s.metadata.strictReasoningEcho = true; - delete s.metadata.strictReasoningEchoVeto; markDirty(s); log("warn", `[${s.id}] upstream 400 mentions a reasoning echo field — learned strictReasoningEcho for this session (#684/#762/#2169); reasoning-drop disabled`); } + // #5260 review: the veto clear is its OWN transition — nesting it + // in the flag-flip guard above would leave a session that learned + // ON and was later vetoed stuck vetoed forever (demand evidence + // arriving while the flag is already true cleared nothing → + // permanent 400 loop in the opposite direction). + if (s.metadata.strictReasoningEchoVeto === true) { + delete s.metadata.strictReasoningEchoVeto; + markDirty(s); + log("warn", `[${s.id}] upstream 400 demands the reasoning echo field again — cleared strictReasoningEchoVeto (#5260); blank-echo injection re-enabled`); + } } } // #604: relay/gateway 5xx — no usage report will arrive, so arm the diff --git a/tests/issue5260-strict-echo-veto.test.ts b/tests/issue5260-strict-echo-veto.test.ts index c61443a090..3169004306 100644 --- a/tests/issue5260-strict-echo-veto.test.ts +++ b/tests/issue5260-strict-echo-veto.test.ts @@ -1,5 +1,6 @@ import test from "node:test"; import assert from "node:assert/strict"; +import http from "node:http"; import { once } from "node:events"; import { defaultConfig } from "acp-kernel"; import { startServer } from "../src/server.ts"; @@ -164,3 +165,121 @@ test("#5260: main-path e2e — one 422 vetoes strict echo, the next request goes setLogCapture(null); } }); + +test("#5260 review: demand → veto → re-demand — the veto clears on opposite evidence even when the flag is already true", async () => { + // Review finding: the PR's first draft nested the veto-clear inside the + // learned-flag flip guard. A session holding BOTH evidence classes (learned + // ON from an earlier demand 400, later vetoed by an extra-reject) could then + // never clear its veto — a re-demand arriving while the flag was already + // true did nothing, and every tool-call turn 400'd forever in the opposite + // direction (the mirror image of #5260's stuck loop). This drives the full + // flip through the real relay: 400 demand (learn ON) → 422 extra-forbidden + // (veto over the learned flag) → 400 demand (veto must clear) → 200 with + // the blank echo back. + const captured: Captured[] = []; + setLogCapture((level, msg) => captured.push({ level, msg })); + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + + const forwarded: Array<{ messages: Record[] }> = []; + let phase: "demand" | "forbid" | "accept" = "demand"; + const upstream = http.createServer((req, res) => { + let raw = ""; + req.on("data", (c) => (raw += String(c))); + req.on("end", () => { + const body = JSON.parse(raw) as { messages?: Record[] }; + if (Array.isArray(body.messages)) forwarded.push({ messages: body.messages }); + if (phase === "demand") { + res.writeHead(400, { "content-type": "application/json" }); + res.end(DEEPSEEK_DEMAND_400); + } else if (phase === "forbid") { + res.writeHead(422, { "content-type": "application/json" }); + res.end(MISTRAL_422); + } else { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ + id: "chatcmpl_fake", + object: "chat.completion", + choices: [{ index: 0, message: { role: "assistant", content: "ok" }, finish_reason: "stop" }], + usage: { prompt_tokens: 10, completion_tokens: 3, total_tokens: 13 }, + })); + } + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const origin = `http://127.0.0.1:${(upstream.address() as { port: number }).port}`; + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: origin, + routes: { + [origin]: { models: { "deepseek-v4-1-flash-1017": { context: 1_048_576 } } }, + }, + modelContextLimit: 1_048_576, + kernelConfig: defaultConfig(1_048_576), + compress: { injectTool: false, injectNudge: false }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: true, + debug: false, + passthrough: false, + passthroughSource: null, + chainContentDetection: false, + autoUpdate: false, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: true, + releaseNotesCheck: true, + compat: { roles: {} }, + streamErrorShape: "protocol", + mitm: { enabled: false, domains: [] }, + }; + const proxy = await startServer(opts); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + try { + const post = () => fetch(`http://127.0.0.1:${proxyPort}/bili/${origin}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "re5260-2" }, + body: JSON.stringify({ model: "deepseek-v4-1-flash-1017", messages: CLIENT_MESSAGES }), + }); + const tcOf = (fwd: { messages: Record[] }) => fwd.messages.find((m) => Array.isArray(m.tool_calls)); + + // 1) demand-class 400 → learns strict echo ON (the existing #684 behavior). + const r1 = await post(); + assert.equal(r1.status, 400); + await r1.text(); + assert.equal(tcOf(forwarded.at(-1)!)!.reasoning_content, "", "request 1 shows the static-detection injection"); + assert.ok(captured.some((c) => c.msg.includes("learned strictReasoningEcho")), `demand 400 must learn the flag, got: ${captured.map((c) => c.msg).join(" | ")}`); + + // 2) extra-forbidden 422 → the veto lands ON TOP of the learned flag. + phase = "forbid"; + const r2 = await post(); + assert.equal(r2.status, 422); + await r2.text(); + assert.equal(tcOf(forwarded.at(-1)!)!.reasoning_content, "", "flag still on → request 2 still carries the injection"); + assert.ok(captured.some((c) => c.msg.includes("vetoed strictReasoningEcho")), "extra-reject must set the veto over the learned flag"); + + // 3) demand-class 400 AGAIN → the veto must clear even though the flag + // is already true (the subcase the first draft got wrong). + phase = "demand"; + const r3 = await post(); + assert.equal(r3.status, 400); + await r3.text(); + assert.ok(!("reasoning_content" in tcOf(forwarded.at(-1)!)!), "veto held during request 3 — no injection"); + assert.ok(captured.some((c) => c.msg.includes("cleared strictReasoningEchoVeto")), `re-demand must clear the veto despite the flag already being true, got: ${captured.map((c) => c.msg).join(" | ")}`); + + // 4) accept → the blank echo is back (echo ON, veto gone). + phase = "accept"; + const r4 = await post(); + assert.equal(r4.status, 200); + await r4.text(); + assert.equal(tcOf(forwarded.at(-1)!)!.reasoning_content, "", "request 4 carries the blank echo again"); + } finally { + await new Promise((resolve, reject) => proxy.close((e) => (e ? reject(e) : resolve()))); + upstream.closeAllConnections?.(); + await new Promise((resolve) => upstream.close(() => resolve())); + setLogCapture(null); + } +});