From e7d12b55325b22fa23b2b0bc6a7f67a09abd465a Mon Sep 17 00:00:00 2001 From: cat_shark <1716967236@qq.com> Date: Thu, 24 Sep 2026 17:17:18 +0800 Subject: [PATCH] fix: retry instead of stalling when DSML tool-call markup leaks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 (`…`, ``, ``, a stray ``) 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. --- src/__tests__/dsml.test.ts | 173 ++++++++++++++++ src/__tests__/stream.test.ts | 159 ++++++++++++++- src/protocol/dsml.ts | 371 +++++++++++++++++++++++++++++++++++ src/protocol/stream.ts | 175 ++++++++++++----- 4 files changed, 823 insertions(+), 55 deletions(-) create mode 100644 src/__tests__/dsml.test.ts create mode 100644 src/protocol/dsml.ts diff --git a/src/__tests__/dsml.test.ts b/src/__tests__/dsml.test.ts new file mode 100644 index 0000000..61e717c --- /dev/null +++ b/src/__tests__/dsml.test.ts @@ -0,0 +1,173 @@ +import { describe, expect, it } from "vitest"; +import { DSML_DELIMITER as D, DsmlFilter, isLeakedToolMarkupLine, stripLeakedToolMarkup } from "../protocol/dsml.js"; + +/** Feed chunks through a fresh filter and collect everything it produced. */ +function collect(chunks: string[]) { + const emitted: string[] = []; + const filter = new DsmlFilter((text) => emitted.push(text)); + for (const chunk of chunks) filter.push(chunk); + filter.finalize(); + return { text: emitted.join(""), calls: filter.toolCalls, sawMarkup: filter.sawMarkup }; +} + +describe("DsmlFilter", () => { + it("passes plain text through untouched", () => { + const result = collect(["hello ", "world"]); + expect(result.text).toBe("hello world"); + expect(result.calls).toEqual([]); + expect(result.sawMarkup).toBe(false); + }); + + it("keeps angle brackets that are not DSML markers", () => { + const result = collect(["if (a < b) return c; // "]); + expect(result.text).toBe("if (a < b) return c; // "); + expect(result.sawMarkup).toBe(false); + }); + + it("drops leaked closing markers and the whitespace between them", () => { + const leaked = ["Let me grep.", ``, ``, ``].join("\n"); + const result = collect([leaked]); + expect(result.text).toBe("Let me grep."); + expect(result.calls).toEqual([]); + expect(result.sawMarkup).toBe(true); + }); + + it("rebuilds a complete tool_calls block into a structured call", () => { + const block = [ + `${D}tool_calls>`, + `${D}invoke name="read">`, + `${D}parameter name="path" string="true">/tmp/notes.md`, + ``, + ``, + ].join("\n"); + const result = collect([block]); + expect(result.calls).toEqual([{ id: "dsml_call_0", name: "read", arguments: { path: "/tmp/notes.md" } }]); + expect(result.text).toBe(""); + }); + + it("rebuilds a block split across chunks, including mid-delimiter splits", () => { + const block = + `${D}tool_calls>` + + `${D}invoke name="bash">` + + `${D}parameter name="command" string="true">ls -la` + + ``; + // Split at awkward offsets: inside the delimiter, inside the tag, inside the value. + const chunks = [block.slice(0, 3), block.slice(3, 11), block.slice(11, 40), block.slice(40)]; + const result = collect(chunks); + expect(result.calls).toEqual([{ id: "dsml_call_0", name: "bash", arguments: { command: "ls -la" } }]); + expect(result.text).toBe(""); + }); + + it("does not leak a marker whose delimiter arrives in pieces", () => { + const result = collect(["done ", D.slice(0, 4), `${D.slice(4)}calls>`]); + expect(result.text).toBe("done "); + expect(result.sawMarkup).toBe(true); + expect(result.calls).toEqual([]); + }); + + it("JSON-decodes parameters that are not declared as strings", () => { + const block = + `${D}tool_calls>` + + `${D}invoke name="run">` + + `${D}parameter name="count">3` + + `${D}parameter name="dryRun">true` + + ``; + const result = collect([block]); + expect(result.calls[0]?.arguments).toEqual({ count: 3, dryRun: true }); + }); + + it("keeps visible text that surrounds a leaked marker", () => { + const result = collect([`beforeafter`]); + expect(result.text).toBe("beforeafter"); + }); + + it("keeps an unterminated marker fragment instead of losing the text", () => { + const result = collect(["partial ", `${D}invoke name="read"`]); + expect(result.text).toBe(`partial ${D}invoke name="read"`); + expect(result.calls).toEqual([]); + expect(result.sawMarkup).toBe(true); + }); + + it("keeps a delimiter that is not followed by a DSML tag name", () => { + const quoted = `标识符 \`${D}\` 用于工具调用。`; + const result = collect([quoted]); + expect(result.text).toBe(quoted); + expect(result.sawMarkup).toBe(true); + }); + + it("reports two parallel calls in arrival order", () => { + const block = + `${D}tool_calls>` + + `${D}invoke name="a">${D}parameter name="x" string="true">1` + + `${D}invoke name="b">${D}parameter name="y" string="true">2` + + ``; + const result = collect([block]); + expect(result.calls.map((call) => call.name)).toEqual(["a", "b"]); + expect(result.calls.map((call) => call.arguments)).toEqual([{ x: "1" }, { y: "2" }]); + }); + + describe("preserveInlineMarkers", () => { + function collectPreserving(chunks: string[]) { + const emitted: string[] = []; + const filter = new DsmlFilter((text) => emitted.push(text), { preserveInlineMarkers: true }); + for (const chunk of chunks) filter.push(chunk); + filter.finalize(); + return { text: emitted.join(""), calls: filter.toolCalls }; + } + + it("keeps a marker quoted inside a sentence", () => { + const quoted = `The raw markup looks like \`\` in the stream.`; + expect(collectPreserving([quoted]).text).toBe(quoted); + }); + + it("still strips a marker that lands on its own line", () => { + const result = collectPreserving([`Let me grep.\n\n\n`]); + expect(result.text).toBe("Let me grep."); + }); + + it("still rebuilds a block that starts a line", () => { + const block = `${D}tool_calls>${D}invoke name="read">${D}parameter name="path" string="true">/a`; + expect(collectPreserving([block]).calls).toEqual([ + { id: "dsml_call_0", name: "read", arguments: { path: "/a" } }, + ]); + }); + }); + + // The gateway sometimes strips the delimiters in transit, leaving the tag + // skeleton in the content channel as ordinary text. + describe("delimiter-less leaked markup", () => { + const leakedFragment = [ + 'git log --oneline', + 'sed -n 1,5p A.java', + "", + ].join("\n"); + + it("strips a whole-line parameter/invoke fragment and reports it", () => { + expect(stripLeakedToolMarkup(leakedFragment)).toEqual({ stripped: true, text: "" }); + }); + + it("swallows a parameter whose value spans lines", () => { + const leaked = 'line one\nline two\n'; + expect(stripLeakedToolMarkup(leaked)).toEqual({ stripped: true, text: "" }); + }); + + it("keeps prose that mentions the tags inline", () => { + const prose = 'The model wrote /a inline.'; + expect(stripLeakedToolMarkup(prose)).toEqual({ stripped: false, text: prose }); + }); + + it("keeps a real answer and drops only the leaked lines", () => { + const result = stripLeakedToolMarkup(`Here is the answer.\n${leakedFragment}`); + expect(result).toEqual({ stripped: true, text: "Here is the answer." }); + }); + + it("classifies marker lines, not ordinary text", () => { + expect(isLeakedToolMarkupLine("")).toBe(true); + expect(isLeakedToolMarkupLine("")).toBe(true); + expect(isLeakedToolMarkupLine('1')).toBe(true); + expect(isLeakedToolMarkupLine("")).toBe(true); + expect(isLeakedToolMarkupLine("Done.")).toBe(false); + expect(isLeakedToolMarkupLine('see 1')).toBe(false); + }); + }); +}); diff --git a/src/__tests__/stream.test.ts b/src/__tests__/stream.test.ts index 67f62fc..b5e728a 100644 --- a/src/__tests__/stream.test.ts +++ b/src/__tests__/stream.test.ts @@ -1,13 +1,15 @@ -import type { - Api, - AssistantMessage, - AssistantMessageEvent, - AssistantMessageEventStream, - Context, - Model, - ToolCall, +import { + type Api, + type AssistantMessage, + type AssistantMessageEvent, + type AssistantMessageEventStream, + type Context, + isRetryableAssistantError, + type Model, + type ToolCall, } from "@earendil-works/pi-ai"; import { afterEach, describe, expect, it, vi } from "vitest"; +import { DSML_DELIMITER as D } from "../protocol/dsml.js"; import { streamQoder } from "../protocol/stream.js"; import { loadLiveFixture } from "./live-fixture.js"; @@ -497,4 +499,145 @@ describe("streamQoder", () => { expect(error.error.stopReason).toBe("aborted"); expect(events.find((event) => event.type === "done")).toBeUndefined(); }); + + // DeepSeek V4 sometimes emits its internal DSML tool-call markup instead of + // filling `tool_calls` (https://github.com/simonsmh/pi-provider-qoder — see + // src/protocol/dsml.ts). When the structured call never arrives the turn used + // to end as an empty `stop` and the agent stalled mid-task with no error. + describe("DSML tool-call leak", () => { + const leakedClosers = `Let me grep.\n\n`; + + it("fails the turn when markup leaks into reasoning and no tool call arrives", async () => { + const sse = + sseEnvelope(chunk({ reasoning_content: leakedClosers })) + sseEnvelope(finishChunk("stop")) + DONE_SSE; + globalThis.fetch = mockFetch(sse); + + const events = await consume(streamQoder(makeModel(), makeContext(), { apiKey: "fake", reasoning: "high" })); + + // Must NOT be a silent done/stop: pi only retries stopReason=error, so a + // plain stop here is the stall the user sees. + expect(events.find((event) => event.type === "done")).toBeUndefined(); + const err = events.find((event) => event.type === "error") as { error: AssistantMessage }; + expect(err, "expected an error event").toBeDefined(); + expect(err.error.stopReason).toBe("error"); + // The wording must keep matching pi's retryable-error pattern. + expect(err.error.errorMessage).toMatch(/ended without/); + expect(err.error.errorMessage).toMatch(/DSML/); + // ...and pi's own classifier must agree, otherwise the host gives up + // instead of retrying the truncated turn. + expect(isRetryableAssistantError(err.error)).toBe(true); + // The marker fragments never reach the message. + expect(JSON.stringify(err.error.content)).not.toContain(D); + }); + + it("keeps the turn when a structured call arrives alongside leaked markup", async () => { + const sse = + sseEnvelope(chunk({ reasoning_content: leakedClosers })) + + sseEnvelope( + chunk({ + tool_calls: [{ index: 0, id: "call_1", function: { name: "bash", arguments: '{"command":"ls"}' } }], + }), + ) + + sseEnvelope(finishChunk("tool_calls")) + + DONE_SSE; + globalThis.fetch = mockFetch(sse); + + const events = await consume(streamQoder(makeModel(), makeContext(), { apiKey: "fake", reasoning: "high" })); + const done = events.find((event) => event.type === "done") as { message: AssistantMessage }; + + expect(done, "expected a done event").toBeDefined(); + expect(done.message.stopReason).toBe("toolUse"); + const calls = done.message.content.filter((block) => block.type === "toolCall"); + expect(calls).toHaveLength(1); + expect((calls[0] as ToolCall).name).toBe("bash"); + // The thinking block keeps the prose, not the markup. + expect(JSON.stringify(done.message.content)).not.toContain(D); + }); + + it("rebuilds a tool call from a DSML block that leaked into the content channel", async () => { + const block = + `${D}tool_calls>` + + `${D}invoke name="read">` + + `${D}parameter name="path" string="true">/tmp/notes.md` + + ``; + const sse = sseEnvelope(chunk({ content: block })) + sseEnvelope(finishChunk("stop")) + DONE_SSE; + globalThis.fetch = mockFetch(sse); + + const events = await consume(streamQoder(makeModel(), makeContext(), { apiKey: "fake", reasoning: "high" })); + const done = events.find((event) => event.type === "done") as { message: AssistantMessage }; + + expect(done, "expected a done event").toBeDefined(); + expect(done.message.stopReason).toBe("toolUse"); + const call = done.message.content.find((block) => block.type === "toolCall") as ToolCall; + expect(call.name).toBe("read"); + expect(call.arguments).toEqual({ path: "/tmp/notes.md" }); + expect(JSON.stringify(done.message.content)).not.toContain(D); + }); + + it("fails the turn when the content channel carries only stray closers", async () => { + const sse = + sseEnvelope(chunk({ content: `\n\n` })) + + sseEnvelope(finishChunk("stop")) + + DONE_SSE; + globalThis.fetch = mockFetch(sse); + + const events = await consume(streamQoder(makeModel(), makeContext(), { apiKey: "fake", reasoning: "high" })); + + expect(events.find((event) => event.type === "done")).toBeUndefined(); + const err = events.find((event) => event.type === "error") as { error: AssistantMessage }; + expect(err.error.stopReason).toBe("error"); + expect(err.error.errorMessage).toMatch(/ended without/); + }); + + it("still fails the turn on an empty reply that carries no markup at all", async () => { + const sse = + sseEnvelope(chunk({ reasoning_content: "thinking only" })) + sseEnvelope(finishChunk("stop")) + DONE_SSE; + globalThis.fetch = mockFetch(sse); + + const events = await consume(streamQoder(makeModel(), makeContext(), { apiKey: "fake", reasoning: "high" })); + + const err = events.find((event) => event.type === "error") as { error: AssistantMessage }; + expect(err.error.stopReason).toBe("error"); + expect(err.error.errorMessage).not.toMatch(/DSML/); + }); + + // The gateway sometimes strips the delimiters but still forwards the tag + // skeleton, and it can land in the *content* channel. That used to look + // like a visible reply, so the empty-turn guard never fired. + it("fails the turn when a delimiter-less fragment leaks into the content channel", async () => { + const leaked = [ + 'git log --oneline', + 'sed -n 1,5p A.java', + "", + ].join("\n"); + const sse = sseEnvelope(chunk({ content: leaked })) + sseEnvelope(finishChunk("stop")) + DONE_SSE; + globalThis.fetch = mockFetch(sse); + + const events = await consume(streamQoder(makeModel(), makeContext(), { apiKey: "fake", reasoning: "high" })); + + expect(events.find((event) => event.type === "done")).toBeUndefined(); + const err = events.find((event) => event.type === "error") as { error: AssistantMessage }; + expect(err.error.stopReason).toBe("error"); + expect(err.error.errorMessage).toMatch(/ended without/); + expect(err.error.errorMessage).toMatch(/DSML/); + expect(isRetryableAssistantError(err.error)).toBe(true); + // The raw markup never reaches the message. + expect(JSON.stringify(err.error.content)).not.toContain(" { + const leaked = 'ls\n'; + const sse = + sseEnvelope(chunk({ content: `Here is the answer.\n${leaked}` })) + sseEnvelope(finishChunk("stop")) + DONE_SSE; + globalThis.fetch = mockFetch(sse); + + const events = await consume(streamQoder(makeModel(), makeContext(), { apiKey: "fake", reasoning: "high" })); + const done = events.find((event) => event.type === "done") as { message: AssistantMessage }; + + expect(done, "expected a done event").toBeDefined(); + expect(done.message.stopReason).toBe("stop"); + const text = done.message.content.find((block) => block.type === "text") as { text: string }; + expect(text.text).toBe("Here is the answer."); + }); + }); }); diff --git a/src/protocol/dsml.ts b/src/protocol/dsml.ts new file mode 100644 index 0000000..b25f2ec --- /dev/null +++ b/src/protocol/dsml.ts @@ -0,0 +1,371 @@ +/** + * DeepSeek DSML (DeepSeek Markup Language) tool-call leak filter. + * + * DeepSeek V4 models occasionally fall back to their internal DSML syntax to + * express a tool call instead of filling the structured `tool_calls` field. The + * serving stack is supposed to intercept that markup and convert it, but it + * leaks: sometimes into the visible text channel, sometimes into the reasoning + * channel, and sometimes only the trailing fragments arrive. When the + * structured call never shows up, the turn ends with no text and no tool call + * and the agent stops mid-task without an error. + * + * This filter removes DSML markers from a channel, rebuilds a complete + * `tool_calls` block into structured calls, and exposes `sawMarkup` so the + * caller can fail the turn (and let the host retry) instead of silently + * stopping when nothing usable was produced. + * + * Marker grammar: `D` is the delimiter, i.e. the two full-width vertical bars + * (U+FF5C, not the ASCII pipe) around the word DSML. The model emits an opener + * (D + "tool_calls>"), one invoke per call (D + 'invoke name="read">'), one + * parameter per argument (D + 'parameter name="path" string="true">' plus the + * value plus D + "/parameter>"), then the matching closers. Leaked closers show + * up alone, usually written with a stray `<` or `/` in front of the delimiter. + * + * Each segment is therefore parsed on its own and unmatched closers are + * dropped. Text between two parameter markers is the parameter value; text + * between two markers outside a parameter is markup whitespace and is dropped + * rather than surfaced to the user. + */ + +export const DSML_DELIMITER = "\uFF5C\uFF5CDSML\uFF5C\uFF5C"; + +/** Optional leading `<`, optional `/`, delimiter, optional `/`, tag, attributes, `>`. */ +const TAGGED_MARKER_PATTERN = /^<(\/)?(?:\uFF5C\uFF5CDSML\uFF5C\uFF5C)\s*\/?\s*([A-Za-z_][\w-]*)([^>]*)>/; +const BARE_MARKER_PATTERN = /^(\/)?(?:\uFF5C\uFF5CDSML\uFF5C\uFF5C)\s*\/?\s*([A-Za-z_][\w-]*)([^>]*)>/; +const ATTRIBUTE_PATTERN = /([A-Za-z_][\w-]*)\s*=\s*"([^"]*)"/g; +const MARKER_PREFIXES = [DSML_DELIMITER, `<${DSML_DELIMITER}`, `; + id: string; + name: string; +} + +export interface DsmlFilterOptions { + /** + * Keep a marker that does not start a line instead of stripping it. Content + * (i.e. the assistant's visible answer) quotes markers when it explains them + * — "the raw markup looks like `...`" — while a real leak always lands on + * its own line(s). Reasoning has no such use case, so it strips everything. + */ + preserveInlineMarkers?: boolean; +} + +/** + * Longest suffix of `text` that is still a prefix of a marker (so it is held + * back). + */ +function partialMarkerSuffixLength(text: string): number { + const max = Math.min(text.length, DSML_DELIMITER.length + 2); + for (let len = max; len > 0; len--) { + const suffix = text.slice(text.length - len); + if (MARKER_PREFIXES.some((prefix) => prefix.startsWith(suffix))) return len; + } + return 0; +} + +function parseAttributes(raw: string): Record { + const attributes: Record = {}; + ATTRIBUTE_PATTERN.lastIndex = 0; + let match = ATTRIBUTE_PATTERN.exec(raw); + while (match !== null) { + attributes[match[1]] = match[2]; + match = ATTRIBUTE_PATTERN.exec(raw); + } + return attributes; +} + +/** JSON-decode a parameter value when it advertises a non-string type. */ +function coerceValue(value: string, attributes: Record): unknown { + const isString = attributes.string === "true" || attributes.type === "string"; + if (isString) return value; + try { + return JSON.parse(value); + } catch { + return value; + } +} + +export class DsmlFilter { + private buffer = ""; + private calls: DsmlToolCall[] = []; + private currentArguments: Record = {}; + private currentInvoke: string | null = null; + private inBlock = false; + private parameterAttributes: Record = {}; + private parameterName: string | null = null; + private parameterValue = ""; + private sawDelimiter = false; + private swallowMarkerWhitespace = false; + private atLineStart = true; + private inCodeFence = false; + private pendingBackticks = 0; + private readonly preserveInlineMarkers: boolean; + + constructor( + private readonly emit: (text: string) => void, + options: DsmlFilterOptions = {}, + ) { + this.preserveInlineMarkers = options.preserveInlineMarkers === true; + } + + /** True once any DSML marker was seen, even when nothing parseable came out. */ + get sawMarkup(): boolean { + return this.sawDelimiter; + } + + /** Tool calls rebuilt from complete `tool_calls` blocks, in arrival order. */ + get toolCalls(): DsmlToolCall[] { + return this.calls; + } + + push(chunk: string): void { + if (!chunk) return; + if (chunk.includes(DSML_DELIMITER)) this.sawDelimiter = true; + this.buffer += chunk; + this.drain(false); + } + + finalize(): void { + this.drain(true); + // An opener without its closer never carries a complete tool call; dropping + // it beats leaking marker fragments to the user as text. + this.buffer = ""; + this.parameterName = null; + this.parameterValue = ""; + } + + private drain(flush: boolean): void { + for (;;) { + const at = this.buffer.indexOf(DSML_DELIMITER); + if (at === -1) { + const hold = flush ? 0 : partialMarkerSuffixLength(this.buffer); + this.emitHead(this.buffer.length - hold); + if (flush) this.buffer = ""; + return; + } + // Absorb the stray `<` and `/` a leaked closer is written with, so they + // never reach the caller as text. + let start = at; + while (start > 0 && (this.buffer[start - 1] === "<" || this.buffer[start - 1] === "/")) start--; + this.emitHead(start, true); + + const match = TAGGED_MARKER_PATTERN.exec(this.buffer) ?? BARE_MARKER_PATTERN.exec(this.buffer); + if (match === null) { + // No recognizable tag after the delimiter. Two cases look alike and + // must be told apart: + // - the marker is still arriving (tag incomplete), so wait for more; + // - the delimiter is not DSML markup at all (the word DSML quoted in + // prose), so keep it as literal text. Dropping up to the next angle + // bracket used to eat whole paragraphs of the answer. + // A real tag is short and never spans a newline, so anything else is + // treated as literal. + const regionEnd = this.buffer.indexOf(">"); + const region = regionEnd === -1 ? this.buffer : this.buffer.slice(0, regionEnd); + const couldBeMarker = regionEnd === -1 || (region.length <= 200 && !region.includes("\n")); + if (couldBeMarker && !flush) return; + const delimiterAt = this.buffer.indexOf(DSML_DELIMITER); + const literal = this.buffer.slice(0, delimiterAt + DSML_DELIMITER.length); + this.buffer = this.buffer.slice(literal.length); + this.sawDelimiter = true; + this.atLineStart = false; + this.emit(literal); + continue; + } + this.sawDelimiter = true; + if (this.preserveInlineMarkers && (this.inCodeFence || !this.atLineStart)) { + // Quoted in prose or shown in a fenced example rather than leaked: + // hand it back verbatim. + const literal = this.buffer.slice(0, match[0].length); + this.buffer = this.buffer.slice(match[0].length); + this.atLineStart = false; + this.emit(literal); + continue; + } + const closing = match[1] === "/"; + this.buffer = this.buffer.slice(match[0].length); + // Capture group 2 is the tag name and group 3 the attribute text; the + // parser needs both (e.g. `invoke name="read"`). + this.handleMarker(closing, `${match[2] ?? ""}${match[3] ?? ""}`); + } + } + + /** + * Emit the first `length` characters, routing them by the parser state. + * `beforeMarker` trims the trailing whitespace a marker is preceded by; it is + * markup formatting, not user-visible text. + */ + private emitHead(length: number, beforeMarker = false): void { + if (length <= 0) return; + const text = this.buffer.slice(0, length); + this.buffer = this.buffer.slice(length); + if (this.parameterName !== null) { + this.parameterValue += text; + return; + } + // Whitespace inside a `tool_calls`/`invoke` block is markup formatting. + if (this.inBlock || this.currentInvoke !== null) { + return; + } + // Newlines that only separate adjacent leaked markers are markup too. + if (this.swallowMarkerWhitespace) { + if (text.trim().length === 0) return; + this.swallowMarkerWhitespace = false; + } + this.noteLinePrefix(text); + this.updateFenceState(text); + const visible = beforeMarker ? text.replace(/\s+$/, "") : text; + if (visible) this.emit(visible); + } + + /** + * Toggle the fenced-code state on every run of three or more backticks. The + * run length is carried across calls because a delimiter can be split over + * two chunks (e.g. "``" then "`bash"). + */ + private updateFenceState(text: string): void { + for (let index = 0; index < text.length; index++) { + if (text[index] === "`") { + this.pendingBackticks++; + continue; + } + this.flushBacktickRun(); + } + } + + private flushBacktickRun(): void { + if (this.pendingBackticks >= 3) this.inCodeFence = !this.inCodeFence; + this.pendingBackticks = 0; + } + + /** Track whether the next marker would sit at the start of a line. */ + private noteLinePrefix(text: string): void { + const lastNewline = text.lastIndexOf("\n"); + if (lastNewline !== -1) { + this.atLineStart = text.slice(lastNewline + 1).trim().length === 0; + } else if (text.trim().length > 0) { + this.atLineStart = false; + } + } + + private handleMarker(closing: boolean, raw: string): void { + const trimmed = raw.trim(); + const name = trimmed.split(/\s+/, 1)[0].toLowerCase(); + const attributes = parseAttributes(trimmed); + this.sawDelimiter = true; + this.swallowMarkerWhitespace = true; + + if (closing) { + if (name === "parameter") this.closeParameter(attributes); + else if (name === "invoke") this.closeInvoke(); + else if (name === "tool_calls") this.inBlock = false; + return; + } + + if (name === "tool_calls") { + this.inBlock = true; + return; + } + if (name === "invoke") { + this.currentInvoke = attributes.name ?? ""; + this.currentArguments = {}; + return; + } + if (name === "parameter") { + this.parameterName = attributes.name ?? ""; + this.parameterAttributes = attributes; + this.parameterValue = ""; + } + } + + private closeParameter(attributes: Record): void { + if (this.parameterName === null) return; + const effective = Object.keys(this.parameterAttributes).length > 0 ? this.parameterAttributes : attributes; + this.currentArguments[this.parameterName] = coerceValue(this.parameterValue, effective); + this.parameterName = null; + this.parameterValue = ""; + } + + private closeInvoke(): void { + if (this.parameterName !== null) this.closeParameter({}); + if (this.currentInvoke === null) return; + if (this.currentInvoke.length > 0) { + this.calls.push({ + arguments: this.currentArguments, + id: `dsml_call_${this.calls.length}`, + name: this.currentInvoke, + }); + } + this.currentInvoke = null; + this.currentArguments = {}; + } +} + +/** + * Qoder's gateway sometimes strips the `||DSML||` delimiters in transit and + * forwards the tag skeleton as ordinary text instead: + * + * git log --oneline + * + * + * + * `DsmlFilter` keys off the delimiter, so it cannot see this form. Because the + * fragment lands in the *content* channel it became a normal text block, and + * the empty-turn guard in stream.ts (`!hasVisibleText`) then saw a non-empty + * reply and never fired — the turn ended as a silent `stop` with the raw markup + * on screen instead of retrying. Match these line-wise: a real leak always + * occupies whole lines, while prose that merely mentions a tag keeps it inline. + */ +const LEAKED_PARAMETER_START = /^]*>/; +const LEAKED_PARAMETER_CLOSER = ""; +const LEAKED_BARE_TAG = /^<\/?(?:invoke|tool_calls)(?:\s[^>]*)?>$/; +const LEAKED_THINKING_CLOSER = /^<\/(?:thinking|think|reasoning|thought)>$/; +const LEAKED_MARKUP_HINT = + //; + +/** + * True when a trimmed line is nothing but delimiter-less leaked tool-call + * markup. Prose that merely quotes a tag inline has other text on the line and + * is left alone. + */ +export function isLeakedToolMarkupLine(trimmed: string): boolean { + const start = LEAKED_PARAMETER_START.exec(trimmed); + if (start) { + const closerAt = trimmed.indexOf(LEAKED_PARAMETER_CLOSER, start[0].length); + // A parameter is markup while its closer ends the line (or has not arrived). + return closerAt === -1 || trimmed.slice(closerAt + LEAKED_PARAMETER_CLOSER.length).trim() === ""; + } + return trimmed === LEAKED_PARAMETER_CLOSER || LEAKED_BARE_TAG.test(trimmed) || LEAKED_THINKING_CLOSER.test(trimmed); +} + +export interface StrippedLeakedMarkup { + /** Whether anything was removed, so the caller can flag the turn. */ + stripped: boolean; + text: string; +} + +/** Remove delimiter-less leaked tool-call markup from `text`, line by line. */ +export function stripLeakedToolMarkup(text: string): StrippedLeakedMarkup { + if (!LEAKED_MARKUP_HINT.test(text)) return { stripped: false, text }; + const kept: string[] = []; + let stripped = false; + let swallowingParameter = false; + for (const line of text.split("\n")) { + const trimmed = line.trim(); + if (swallowingParameter) { + stripped = true; + if (trimmed.includes(LEAKED_PARAMETER_CLOSER)) swallowingParameter = false; + continue; + } + if (isLeakedToolMarkupLine(trimmed)) { + // An opener whose closer is not on this line swallows the value lines. + if (LEAKED_PARAMETER_START.test(trimmed) && !trimmed.includes(LEAKED_PARAMETER_CLOSER)) { + swallowingParameter = true; + } + stripped = true; + continue; + } + kept.push(line); + } + return stripped ? { stripped: true, text: kept.join("\n") } : { stripped: false, text }; +} diff --git a/src/protocol/stream.ts b/src/protocol/stream.ts index a285abf..2beb69d 100644 --- a/src/protocol/stream.ts +++ b/src/protocol/stream.ts @@ -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, 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 // `` 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",