Skip to content
Merged
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
6 changes: 4 additions & 2 deletions src/loop/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1138,13 +1138,15 @@ export async function* runCompressLoop(
// a thinking session is the split-turn signature — remember it
// on the session so #651's reasoning-drop never fires here
// again (kernel gate prevents the split; this closes the loop).
// #2169: match BOTH field spellings — the chat wire says
// reasoning_content, DeepSeek's Responses wire says reasoning_text.
if (
e.status === 400 &&
/reasoning_content/i.test(e.body) &&
/reasoning_(?:content|text)/i.test(e.body) &&
ctx.session.metadata.strictReasoningEcho !== true
) {
ctx.session.metadata.strictReasoningEcho = true;
ctx.log(`[acp-loop] 400 mentions reasoning_content — learned strict reasoning echo for this session; #651 reasoning-drop disabled (#684)`);
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)`);
}
// #762: persist the exact re-requested body on 4xx (env-gated: BILI_DUMP_4XX=1).
Expand Down
6 changes: 4 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6785,11 +6785,13 @@ async function forward(
// learner (src/loop/core.ts) never sees client-originated 400s, so a
// first post-fold rejection left strictReasoningEcho unset — #651 kept
// dropping reasoning and every following turn split again.
if (upstream.status === 400 && /reasoning_content/i.test(errBody.toString("utf8"))) {
// #2169: match BOTH field spellings — the chat wire says
// reasoning_content, DeepSeek's Responses wire says reasoning_text.
if (upstream.status === 400 && /reasoning_(?:content|text)/i.test(errBody.toString("utf8"))) {
if (s.metadata.strictReasoningEcho !== true) {
s.metadata.strictReasoningEcho = true;
markDirty(s);
log("warn", `[${s.id}] upstream 400 mentions reasoning_content — learned strictReasoningEcho for this session (#684/#762); reasoning-drop disabled`);
log("warn", `[${s.id}] upstream 400 mentions a reasoning echo field — learned strictReasoningEcho for this session (#684/#762/#2169); reasoning-drop disabled`);
}
}
}
Expand Down
56 changes: 48 additions & 8 deletions src/strict-echo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import type { Logger } from "./logger.js";
* thinking-mode "reasoning_content ... must be passed back to the API" —
* a rebuilt request whose assistant tool-call turns lost their reasoning is
* rejected with 400. Learned flag first (set on first 400 whose body mentions
* reasoning_content, see the loop's UpstreamHttpError handler), then static
* reasoning_content or reasoning_text — the Responses wire spells it
* 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). */
Expand Down Expand Up @@ -53,17 +54,45 @@ export function normalizeStrictEchoReasoning(
return patched > 0 ? out : messages;
}

/** [#1479] Responses-wire twin of [#762]: a strict-echo gateway rejects an
/** [#2169] A reasoning item whose `content` array consists EXCLUSIVELY of
* whitespace-only `reasoning_text` parts — a blank echo that strict-echo
* upstreams (DeepSeek thinking mode) reject with "reasoning_text must be passed
* back": their validation accepts content-less / summary-only items but rejects
* ANY content they did not issue, whitespace included (replacing the blank text
* with real text still 400s — #2169 control experiments). Whitespace carries no
* information, so clearing is lossless. Conservative by design: a missing or
* non-array content, an empty array, any non-reasoning_text part, any
* non-string text, or one part with real text all leave the item untouched. */
function hasBlankReasoningContent(item: ResponseInputItem): boolean {
if ((item as { type?: unknown }).type !== "reasoning") return false;
const content = (item as { content?: unknown }).content;
if (!Array.isArray(content) || content.length === 0) return false;
let sawTextPart = false;
for (const part of content) {
if (part === null || typeof part !== "object") return false;
const p = part as { type?: unknown; text?: unknown };
if (p.type !== "reasoning_text" || typeof p.text !== "string") return false;
sawTextPart = true;
if (p.text.trim().length > 0) return false;
}
return sawTextPart;
}

/** [#1479,#2169] Responses-wire twin of [#762]: a strict-echo gateway rejects an
* assistant RUN (maximal consecutive stretch of reasoning / assistant message
* / function_call / custom_tool_call items) that carries a tool call but no
* reasoning item. Fold + kernel round-trip leave exactly that shape (the turn's
* echo dies inside its fold range while a sibling call survives), and no
* Responses equivalent of normalizeStrictEchoReasoning existed — the #762
* repair only ever ran on chat-completions messages. Insert one blank reasoning
* item at the start of each orphaned run; blank is what DeepSeek thinking mode
* accepts (hermes-agent PR #15527). Gated on at least one reasoning item
* existing anywhere in the input so non-thinking sessions are never touched.
* Returns the input array unchanged when disabled or nothing needed patching. */
* accepts (hermes-agent PR #15527). Second duty (#2169): clear the `content` of
* reasoning items whose content is whitespace-only reasoning_text — such items
* ride the client's resent history into the request (bili never fabricates them:
* the kernel round-trip re-emits raw items verbatim) and are rejected on the
* wire as-is. Gated on at least one reasoning item existing anywhere in the
* input so non-thinking sessions are never touched. Returns the input array
* unchanged when disabled or nothing needed patching. */
export function normalizeStrictEchoResponsesInput(
input: ResponseInputItem[],
enabled: boolean,
Expand Down Expand Up @@ -97,13 +126,24 @@ export function normalizeStrictEchoResponsesInput(
else if (t === "reasoning") runReasoning++;
}
closeRun();
if (insertAt.size === 0) return input;
const clearAt = new Set<number>();
for (let i = 0; i < input.length; i++) {
if (hasBlankReasoningContent(input[i]!)) clearAt.add(i);
}
if (insertAt.size === 0 && clearAt.size === 0) return input;
const out: ResponseInputItem[] = [];
for (let i = 0; i < input.length; i++) {
if (insertAt.has(i)) out.push({ type: "reasoning", summary: [{ type: "summary_text", text: "" }] });
out.push(input[i]!);
if (clearAt.has(i)) {
const copy: Record<string, unknown> = { ...(input[i]! as Record<string, unknown>) };
delete copy.content;
out.push(copy as ResponseInputItem);
} else {
out.push(input[i]!);
}
}
log("info", `[${sessionId}] strict-echo-responses: injected ${insertAt.size} blank reasoning item(s) before tool-call run(s) missing their echo (#1479)`);
if (clearAt.size > 0) log("info", `[${sessionId}] strict-echo-responses: cleared whitespace-only reasoning content on ${clearAt.size} item(s) — blank reasoning_text echoes are rejected by strict-echo upstreams (#2169)`);
if (insertAt.size > 0) log("info", `[${sessionId}] strict-echo-responses: injected ${insertAt.size} blank reasoning item(s) before tool-call run(s) missing their echo (#1479)`);
return out;
}

Expand Down
Loading
Loading