fix: retry instead of stalling when DSML tool-call markup leaks

DeepSeek V4 sometimes expresses a tool call as its internal DSML markup
instead of filling `tool_calls`, and Qoder's gateway leaks the tag skeleton
rather than converting it. When no structured call arrives the turn used to
end as an empty `stop` and the agent stopped mid-task with no error.

Handle both shapes:

- Filter the delimited form (`||DSML||tool_calls` ...) on the reasoning and
  content channels, rebuilding a complete block into a structured call.
- Strip the delimiter-less form the gateway leaves behind
  (`<parameter name="command">…</parameter>`, `</invoke>`, `</tool_calls>`,
  a stray `</thinking>`) line-wise before the empty-turn guard reads the
  message, and make `hasVisibleText` ignore markup-only text. That form used
  to land in the *content* channel as a normal reply, so the guard saw
  visible text and never fired.

A turn that produces only leaked markup now fails with a retryable error
(matching pi's "ended without" pattern) instead of ending silently.
This commit is contained in:
2026-09-24 17:17:18 +08:00
parent a0be14764c
commit e7d12b5532
4 changed files with 823 additions and 55 deletions
+128 -47
View File
@@ -16,6 +16,7 @@ import { resolveQoderIdentity } from "../auth/oauth.js";
import { getCachedModelConfig, MAX_OUTPUT_TOKENS } from "../catalog.js";
import { buildAuthHeaders, getMachineId } from "../cosy.js";
import { getQoderChatURL, getQoderRegionConfig } from "../region.js";
import { DsmlFilter, type DsmlToolCall, isLeakedToolMarkupLine, stripLeakedToolMarkup } from "./dsml.js";
import { qoderEncodeBody } from "./encoding.js";
import { stripThinkingTags, ThinkingTagParser } from "./thinking.js";
import { transformMessagesForQoder, transformTools } from "./transform.js";
@@ -82,6 +83,19 @@ function contentToText(content: unknown): string {
return "";
}
/**
* True when the assistant produced text a user can actually read. Leaked
* tool-call markup is protocol noise, not an answer: a turn whose only "text"
* is marker fragments must still reach the retryable empty-turn guard below.
*/
function hasVisibleText(message: AssistantMessage): boolean {
return message.content.some(
(block) =>
block.type === "text" &&
block.text.split("\n").some((line) => line.trim().length > 0 && !isLeakedToolMarkupLine(line.trim())),
);
}
export function streamQoder(
model: Model<Api>,
context: Context,
@@ -315,6 +329,54 @@ export function streamQoder(
const thinkingEnabled = (options?.reasoning as unknown) !== false && (options?.reasoning as unknown) !== "off";
const thinkingParser = thinkingEnabled ? new ThinkingTagParser(output, stream) : null;
const appendThinking = (chunk: string): void => {
if (thinkingBlockIndex === -1) {
thinkingBlockIndex = output.content.length;
output.content.push({ type: "thinking", thinking: "" });
stream.push({ type: "thinking_start", contentIndex: thinkingBlockIndex, partial: output });
}
const block = output.content[thinkingBlockIndex] as ThinkingContent;
block.thinking += chunk;
stream.push({ type: "thinking_delta", contentIndex: thinkingBlockIndex, delta: chunk, partial: output });
};
const appendContent = (chunk: string): void => {
// End the API reasoning block as soon as real visible text arrives.
if (thinkingBlockIndex !== -1) {
const block = output.content[thinkingBlockIndex] as ThinkingContent;
stream.push({
type: "thinking_end",
contentIndex: thinkingBlockIndex,
content: block.thinking,
partial: output,
});
thinkingBlockIndex = -1;
}
if (thinkingParser) {
thinkingParser.processChunk(chunk);
return;
}
if (contentBlockIndex === -1) {
contentBlockIndex = output.content.length;
output.content.push({ type: "text", text: "" });
stream.push({ type: "text_start", contentIndex: contentBlockIndex, partial: output });
}
const block = output.content[contentBlockIndex] as TextContent;
block.text += chunk;
stream.push({ type: "text_delta", contentIndex: contentBlockIndex, delta: chunk, partial: output });
};
// DeepSeek V4 falls back to its internal DSML tool-call markup instead of
// the structured `tool_calls` field now and then, and the leak can land in
// either channel. Strip it before it reaches the message and keep any
// rebuilt call for the finalizer, which only uses it when the gateway
// itself sent no structured tool call.
const reasoningDsml = new DsmlFilter(appendThinking);
// Visible content is only filtered for line-leading markers: a real leak
// always lands on its own line, while an answer that *explains* the markup
// quotes it inline and must stay readable.
const contentDsml = new DsmlFilter(appendContent, { preserveInlineMarkers: true });
stream.push({ type: "start", partial: output });
// `data: [DONE]` is the end of the response. Break the read loop too, not
@@ -410,56 +472,14 @@ export function streamQoder(
// opener into reasoning_content (with the matching
// `</thinking>` closer landing in the content stream). Strip
// tag artifacts so the thinking block stays clean, matching
// the SDK's ContentBlock model.
const reasoningChunk = stripThinkingTags(delta.reasoning_content);
if (reasoningChunk) {
if (thinkingBlockIndex === -1) {
thinkingBlockIndex = output.content.length;
output.content.push({ type: "thinking", thinking: "" });
stream.push({ type: "thinking_start", contentIndex: thinkingBlockIndex, partial: output });
}
const block = output.content[thinkingBlockIndex] as ThinkingContent;
block.thinking += reasoningChunk;
stream.push({
type: "thinking_delta",
contentIndex: thinkingBlockIndex,
delta: reasoningChunk,
partial: output,
});
}
// the SDK's ContentBlock model. The DSML filter then drops
// leaked tool-call markup before it shows up as "thinking".
reasoningDsml.push(stripThinkingTags(delta.reasoning_content));
}
// 2. Process text content
if (delta.content) {
// End API thinking block if active
if (thinkingBlockIndex !== -1) {
const block = output.content[thinkingBlockIndex] as ThinkingContent;
stream.push({
type: "thinking_end",
contentIndex: thinkingBlockIndex,
content: block.thinking,
partial: output,
});
thinkingBlockIndex = -1;
}
if (thinkingParser) {
thinkingParser.processChunk(delta.content);
} else {
if (contentBlockIndex === -1) {
contentBlockIndex = output.content.length;
output.content.push({ type: "text", text: "" });
stream.push({ type: "text_start", contentIndex: contentBlockIndex, partial: output });
}
const block = output.content[contentBlockIndex] as TextContent;
block.text += delta.content;
stream.push({
type: "text_delta",
contentIndex: contentBlockIndex,
delta: delta.content,
partial: output,
});
}
contentDsml.push(delta.content);
}
// 3. Process tool calls
@@ -541,10 +561,27 @@ export function streamQoder(
// Without this the body stays open until the server times it out.
await reader.cancel().catch(() => {});
// Flush the DSML filters first so anything they held back (and the
// thinking-tag parser's own buffer) still lands in the message.
reasoningDsml.finalize();
contentDsml.finalize();
if (thinkingParser) {
thinkingParser.finalize();
}
// Drop delimiter-less leaked tool-call markup before the guard below reads
// the message. Without this the fragment looks like visible text and the
// turn ends as a silent `stop` (see stripLeakedToolMarkup).
let sawLeakedMarkup = false;
for (const block of output.content) {
if (block.type !== "text" && block.type !== "thinking") continue;
const result = stripLeakedToolMarkup(block.type === "text" ? block.text : block.thinking);
if (!result.stripped) continue;
if (block.type === "text") block.text = result.text;
else block.thinking = result.text;
sawLeakedMarkup = true;
}
if (thinkingBlockIndex !== -1) {
const block = output.content[thinkingBlockIndex] as ThinkingContent;
stream.push({
@@ -581,8 +618,52 @@ export function streamQoder(
// Guarded on blocks that actually reached the message, not on the state
// array being non-empty. Claiming "toolUse" for a message carrying no
// tool call is what turned a malformed stream into a silent dead end.
if (toolCallsState.some((state) => state?.emittedStart)) {
const structuredToolCalls = toolCallsState.some((state) => state?.emittedStart);
// The gateway dropped the structured call, but the raw DSML markup carried
// a complete one: rebuild it instead of losing the turn.
const rebuiltToolCalls: DsmlToolCall[] = structuredToolCalls
? []
: [...reasoningDsml.toolCalls, ...contentDsml.toolCalls];
for (const call of rebuiltToolCalls) {
const callContentIndex = output.content.length;
const toolCall: ToolCall = {
type: "toolCall",
id: call.id,
name: call.name,
arguments: call.arguments,
};
output.content.push(toolCall);
stream.push({ type: "toolcall_start", contentIndex: callContentIndex, partial: output });
stream.push({
type: "toolcall_delta",
contentIndex: callContentIndex,
delta: JSON.stringify(call.arguments),
partial: output,
});
stream.push({ type: "toolcall_end", contentIndex: callContentIndex, toolCall, partial: output });
}
if (structuredToolCalls || rebuiltToolCalls.length > 0) {
output.stopReason = "toolUse";
} else if (output.stopReason === "stop" && !options?.signal?.aborted && !hasVisibleText(output)) {
// Either the reply was empty, or the model emitted a tool call as raw
// DSML and the gateway never converted it (only stray closing markers
// arrive in that case, so nothing is reconstructable). Both mean the
// turn produced nothing to act on: report an error so the host retries
// instead of treating the empty message as a finished answer. The text
// below must keep matching pi's retryable-error pattern ("ended
// without").
output.stopReason = "error";
output.errorMessage =
"Qoder stream ended without a tool call or visible text" +
(reasoningDsml.sawMarkup || contentDsml.sawMarkup || sawLeakedMarkup
? " (raw DeepSeek DSML tool-call markup leaked into the stream)"
: "") +
". Retrying the request.";
stream.push({ type: "error", reason: "error", error: output });
stream.end();
return;
}
// Otherwise keep whatever finish_reason set upstream (defaults to "stop").
// Never overwrite a meaningful finish_reason ("length", "content_filter",