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
36 changes: 29 additions & 7 deletions src/loop/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -1898,14 +1898,36 @@ 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
/reasoning_(?:content|text)|content\[\]\.thinking/i.test(e.body)
) {
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)`);
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) {
Expand Down
26 changes: 25 additions & 1 deletion src/server/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -2121,12 +2122,35 @@ 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;
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
Expand Down
28 changes: 27 additions & 1 deletion src/strict-echo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
Loading
Loading