Files
magpie/internal/gateway/textcalls_pipes_test.go
T
yetone 7ea8aeaba7 gateway: DSML written with a run of pipes (<||DSML||invoke) is made the call too, not left as text (#2623, OKURBITO)
DeepSeek wrote its tool call as <||DSML||invoke name="get_weather">…
(two full-width pipes each side) and the reply reached the Responses
client as that markup: textCallAt only knew the one-pipe marks, so
nothing was held and parseTextCalls, whose regexes already read a run
of pipes, never ran. dsmlMark now finds a DSML tag's mark as the parse
regexes (dsmlTag) read one, with no list of spellings: <, spaces, / and
spaces for a close, a pipe, a run of pipes and spaces, DSML, and
another run that may be empty. A pipe is ASCII | or full-width |, in
any number and any mix of widths. textCallAt uses it to find a call
and to hold a mark still being written (down to a full-width pipe's
first bytes); cutMark uses it to trim a tag cut short at the end, once
the tag has begun its pipes, so a value ending "a < " keeps its "<".
Every route goes through these: textCallSee (translations),
textCallTidy (relayed Chat streams) and wholeTextCalls (whole Chat
replies). OKURBITO pointed out on the issue that a spelling table
leaves |||, ||, || and the like as text; this reads the class.

Tests (textcalls_pipes_test.go). Which cases were seen and which were
made up, as OKURBITO set out on the issue: one reply was written by a
model on its own in real use (deepseek-flash offered web_search:
<||DSML|| calls> … <||DSML|| invoke name="web_search"> …), kept
byte for byte as observedPipes; the issue's get_weather reply came from
asking the model to echo the markup; every other spelling (three pipes,
ASCII, widths that disagree, spaces after <, no pipes after DSML) is
constructed and has not been seen.
- TestTextWrittenDSMLDoublePipe: the observed reply, relayed Chat
  streamed and whole, is a web_search call with its query and no DSML
  left; the issue's reply through /v1/responses, streamed and whole, and
  a whole Chat reply, is a get_weather call with {"city":"Beijing"} and
  no message item.
- TestTextWrittenDSMLAnyPipes: | | || || || || ||| ||| "| |", each reply
  sent one character per chunk: the call, wrapped with text before, a
  space before invoke (the live <||DSML|| invoke), no parameters, cut
  short, tags only with the last cut short, a tool not offered, a mark's
  start at the end, pipes without DSML; each ends as the one-pipe reply.
- TestTextWrittenDSMLDoublePipeEveryCut: the reply split at every byte
  boundary JSON allows is the same call; textCallAt holds each prefix of
  the mark, a full-width pipe's first bytes too.
- TestTextWrittenDSMLMarkAsParsed: widths that disagree within a tag
  and between its open and close, spaces after < and around /, no pipes
  after DSML are each the call; a cut-short <parameter> value ending in
  "a < " keeps it; "x < y and a <" stays text.

Fails without the change: with b7d9616d0's textcalls.go all four
FAIL, the observed reply among them ("text \"<||DSML||invoke …\" calls
map[] finish \"stop\"", "a message item beside the call"; MarkAsParsed's
widths that disagree, spaces after <, no pipes after DSML); the one-pipe
rows pass there. With an earlier draft of dsmlMark (a run of pipes,
none after <, one required after DSML) MarkAsParsed FAILs its
spaces-after-< and no-pipes-after-DSML rows; with cutMark's
has-begun-its-pipes guard taken out it FAILs the cut-short value ("a "
for "a < ").

Not tried against a real DeepSeek upstream: the fixtures are the
bytes OKURBITO posted. In the observed event the request offered only
a hosted web_search ({"type":"web_search"}), not a function; a call to
a tool the request didn't offer by name still goes on as text, as
before, and the test offers web_search as a function.

Run with both #2623's and #2625's changes, on b7d9616d0 (macOS, Go
1.26.8, load average 80-270 from other sessions):
- gofmt -l: nothing. go vet -tags nogui ./... with GOOS darwin, linux and
  windows: clean.
- set -o pipefail; go test -timeout 45m ./...: every package ok except
  internal/agent (2702s) and internal/gateway (2721s), which ran past
  45m under the load (#2544, #2108; no test hung, each was seconds into
  its run). Rerun with -timeout 90m: internal/agent ok (2816s);
  internal/gateway's only red is TestDesktopSubagentUnsetDoesNotDecode,
  "allocated 1 times" (10/10 green alone, filed as #2699). The textcalls
  tests passed in that run.
2026-10-17 18:41:43 +08:00

177 lines
10 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gateway
import (
"strings"
"testing"
"unicode/utf8"
)
// Which of these were seen and which were made up (OKURBITO on #2623):
// - observedPipes is the one reply a model wrote on its own, in real
// use: deepseek-flash, offered web_search, wrote the call as text with
// two full-width pipes and a space before each tag's name;
// - issuePipes is the issue's reproduction: the model was asked to echo
// those bytes back (workbuddy/deepseek-v4.1-flash from a Responses
// client), and the reply it got came back as text;
// - every other spelling below (three pipes, ASCII, widths that
// disagree, spaces after <, no pipes after DSML) is constructed: no
// one has seen a model write it. They are here because the mark is
// read as the parse reads it, not from a list of spellings.
const observedPipes = "<||DSML|| calls>\n<||DSML|| invoke name=\"web_search\">\n<||DSML|| parameter name=\"query\" string=\"true\">current Python version latest stable release</||DSML|| parameter>\n</||DSML|| invoke>\n</||DSML|| calls>"
const issuePipes = `<||DSML||invoke name="get_weather"><||DSML||parameter name="city" string="true">Beijing</||DSML||parameter></||DSML||invoke>`
const weatherTool = `"tools":[{"type":"function","name":"get_weather","description":"Get the weather of a city","parameters":{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]}}]`
// #2623: DSML written with two pipes went to a Responses client as text,
// the issue's prompted reply,
// streamed or whole, while one pipe of either width was made the call.
func TestTextWrittenDSMLDoublePipe(t *testing.T) {
// the reply seen in real use, its bytes as they came: a function
// web_search offered, it is the call, the calls wrapper gone with it
for _, stream := range []string{"true", "false"} {
body := `{"model":"a/m","stream":` + stream + `,"messages":[{"role":"user","content":"You MUST call the web_search tool first"}],"tools":[{"type":"function","function":{"name":"web_search","parameters":{"type":"object","properties":{"query":{"type":"string"}}}}}]}`
var text *string
var calls map[string]string
var finish, raw string
if stream == "true" {
var s string
s, calls, finish, raw = relayedChat(t, body, chunkOf(observedPipes), stopChunk)
text = &s
if s == "" {
text = nil
}
} else {
var cs []wholeCall
text, cs, finish, raw = wholeChat(t, body, "application/json", wholeChatUp(observedPipes))
calls = map[string]string{}
for _, c := range cs {
calls[c.Function.Name] = c.Function.Arguments
}
}
if text != nil || len(calls) != 1 || calls["web_search"] != `{"query":"current Python version latest stable release"}` || finish != "tool_calls" || strings.Contains(raw, "DSML") {
t.Fatalf("observed, stream=%s: text %q calls %v finish %q\n%s", stream, strOr(text), calls, finish, raw)
}
}
for _, stream := range []string{"true", "false"} {
items, raw := responsesItems(t, `{"model":"a/m","stream":`+stream+`,"input":"weather","max_output_tokens":1200,`+weatherTool+`}`,
chunkOf(issuePipes), stopChunk)
var call map[string]any
for _, it := range items {
switch it["type"] {
case "function_call":
call = it
case "message":
t.Errorf("stream=%s: a message item beside the call: %v", stream, it)
}
}
if call == nil || call["name"] != "get_weather" || call["arguments"] != `{"city":"Beijing"}` || strings.Contains(raw, "DSML") {
t.Fatalf("stream=%s: %v\n%s", stream, call, raw)
}
}
text, calls, finish, raw := wholeChat(t, `{"model":"a/m","stream":false,"messages":[{"role":"user","content":"weather"}],`+chatWeatherTool+`}`,
"application/json", wholeChatUp(issuePipes))
if text != nil || len(calls) != 1 || calls[0].Function.Name != "get_weather" || calls[0].Function.Arguments != `{"city":"Beijing"}` ||
finish != "tool_calls" || strings.Contains(raw, "DSML") {
t.Fatalf("whole: text %q calls %+v finish %q\n%s", strOr(text), calls, finish, raw)
}
}
const chatWeatherTool = `"tools":[{"type":"function","function":{"name":"get_weather","parameters":{"type":"object","properties":{"city":{"type":"string"}}}}}]`
// Every spelling of DSML's pipes is read as the one-pipe spelling is: a
// run of them, ASCII or full-width or both, with spaces between, as
// DeepSeek wrote <||DSML|| invoke in a live session. Each reply goes
// up one character to a chunk, so a mark is always cut across chunks; a
// reply that names no offered tool, is cut short, is empty or only looks
// like a mark ends as the one-pipe reply ends.
func TestTextWrittenDSMLAnyPipes(t *testing.T) {
body := `{"model":"a/m","stream":true,"messages":[{"role":"user","content":"weather"}],` + chatWeatherTool + `}`
// written with | standing for the pipes; text, call and finish are
// what the one-pipe reply gives
cases := []struct{ name, reply, text, args, finish string }{
{"the issue's", strings.ReplaceAll(issuePipes, "||", "|"), "", `{"city":"Beijing"}`, "tool_calls"},
{"wrapped, text before", "Checking.\n\n<|DSML|function_calls>\n<|DSML|invoke name=\"get_weather\">\n<|DSML|parameter name=\"city\" string=\"true\">Beijing</|DSML|parameter>\n</|DSML|invoke>\n</|DSML|function_calls>", "Checking.\n\n", `{"city":"Beijing"}`, "tool_calls"},
{"a space before invoke", "<|DSML| invoke name=\"get_weather\">\n<|DSML| parameter name=\"city\" string=\"true\">Beijing</|DSML| parameter>\n</|DSML| invoke>", "", `{"city":"Beijing"}`, "tool_calls"},
{"no parameters", "<|DSML|invoke name=\"get_weather\"></|DSML|invoke>", "", `{}`, "tool_calls"},
{"cut short", "<|DSML|invoke name=\"get_weather\"><|DSML|parameter name=\"city\" string=\"true\">Beij", "", `{}`, "tool_calls"},
{"tags only, the last cut short", "<|DSML|function_calls>\n</|DSML|function_calls>\n</|DS", "", "", "stop"},
{"a tool not offered", "<|DSML| invoke name=\"web_search\">\n<|DSML| parameter name=\"query\">x</|DSML| parameter>\n</|DSML| invoke>", "<|DSML| invoke name=\"web_search\">\n<|DSML| parameter name=\"query\">x</|DSML| parameter>\n</|DSML| invoke>", "", "stop"},
{"a mark's start at the end", "a <|DS", "a <|DS", "", "stop"},
{"pipes and no DSML", "a <| b |> c", "a <| b |> c", "", "stop"},
}
for _, pipes := range []string{"|", "|", "||", "||", "||", "||", "|||", "|||", "| |"} {
for _, c := range cases {
spell := func(s string) string { return strings.ReplaceAll(s, "|", pipes) }
var up []string
for _, r := range spell(c.reply) {
up = append(up, chunkOf(string(r)))
}
text, calls, finish, raw := relayedChat(t, body, append(up, stopChunk)...)
var want map[string]string
if c.args != "" {
want = map[string]string{"get_weather": c.args}
}
if text != spell(c.text) || finish != c.finish || len(calls) != len(want) || calls["get_weather"] != want["get_weather"] {
t.Errorf("%q %s: text %q calls %v finish %q\n%s", pipes, c.name, text, calls, finish, raw)
}
}
}
}
// The issue's reply, cut anywhere and its parts sent as chunks of their
// own, is the same call at every cut, a full-width pipe's bytes too.
func TestTextWrittenDSMLDoublePipeEveryCut(t *testing.T) {
body := `{"model":"a/m","stream":true,"messages":[{"role":"user","content":"weather"}],` + chatWeatherTool + `}`
s := "Sure.\n" + issuePipes
for i := 1; i < len(s); i++ {
if !utf8.ValidString(s[:i]) {
continue // a chunk's JSON carries whole characters only
}
text, calls, finish, raw := relayedChat(t, body, chunkOf(s[:i]), chunkOf(s[i:]), stopChunk)
if text != "Sure.\n" || calls["get_weather"] != `{"city":"Beijing"}` || finish != "tool_calls" {
t.Fatalf("cut at %d: text %q calls %v finish %q\n%s", i, text, calls, finish, raw)
}
}
// and the scanner, which may see a character's first bytes alone
for i := 1; i < len("<||DSML|"); i++ {
at, tail := textCallAt("x" + issuePipes[:i])
if at >= 0 || tail != i {
t.Fatalf("%q: at %d tail %d, want the %d bytes held", issuePipes[:i], at, tail, i)
}
}
if at, _ := textCallAt("x" + issuePipes); at != 1 {
t.Fatalf("the whole mark: at %d", at)
}
}
// The mark is found as the parse reads it, not from a list of spellings
// (OKURBITO on #2623): widths that differ within a tag and between its
// open and close, spaces after < and around /, no pipes after DSML. A
// reply ending in a < that starts no pipes keeps it.
func TestTextWrittenDSMLMarkAsParsed(t *testing.T) {
body := `{"model":"a/m","stream":true,"messages":[{"role":"user","content":"weather"}],` + chatWeatherTool + `}`
for _, c := range []struct{ name, reply, text, args string }{
{"widths that disagree", `<||DSML||invoke name="get_weather"><||DSML||parameter name="city" string="true">Beijing</||DSML||parameter></|DSML|invoke>`, "", `{"city":"Beijing"}`},
{"spaces after < and around /", `< |DSML|invoke name="get_weather">< |DSML|parameter name="city" string="true">Beijing< / |DSML|parameter></|DSML|invoke>`, "", `{"city":"Beijing"}`},
{"no pipes after DSML", `<|DSMLinvoke name="get_weather"><|DSMLparameter name="city" string="true">Beijing</|DSMLparameter></|DSMLinvoke>`, "", `{"city":"Beijing"}`},
{"a value ending in <", `<|DSML|invoke name="get_weather"><|DSML|parameter name="city" string="true">a < </|DSML|parameter></|DSML|invoke>`, "", `{"city":"a \u003c "}`},
{"a cut-short value ending in <", "<tool_call>{\"name\":\"get_weather\">\n<parameter name=\"city\">a < ", "", `{"city":"a \u003c "}`},
{"text with < and no mark", "x < y and a <", "x < y and a <", ""},
} {
var up []string
for _, r := range c.reply {
up = append(up, chunkOf(string(r)))
}
text, calls, finish, raw := relayedChat(t, body, append(up, stopChunk)...)
want, wantFinish := 1, "tool_calls"
if c.args == "" {
want, wantFinish = 0, "stop"
}
if text != c.text || finish != wantFinish || len(calls) != want || calls["get_weather"] != c.args {
t.Errorf("%s: text %q calls %v finish %q\n%s", c.name, text, calls, finish, raw)
}
}
}