From 06a4e71ecb6f93c8bf4d9e1aa2fc04133b3a4126 Mon Sep 17 00:00:00 2001 From: ggbdpq Date: Thu, 15 Oct 2026 22:05:08 +0800 Subject: [PATCH] qoder: a tool call written into the text that can't be read ends the turn as an error, never as the answer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit What changed A whole block whose JSON doesn't parse, or one the stream ends inside, reached the agent as the reply text with finish "stop" and a 200: the call was lost and nothing could fail over. The moved qoder plugin's 0.2.16 ends such a turn as an error (plugins#132, as trae's since plugins#38); 8fdd296d, which raised the mover's min, left the built-in's two exits as they were. The built-in now ends the turn the same way the plugin does: a block on its way to being a call (its JSON began, or the XML function form) that can't be read is a 502 — an error frame with no finish once text was streamed, the status alone before anything was — and one the stream ends inside is the call when its JSON is whole, else a 502. A marker that is no call at all, and a block inside a code fence or inline span, stay text (TestQoderEOF's "incomplete" case among them, unchanged). Splitter state is qoderText's new sent (the text given out, so a block in code is told from a call) and qoderFrag's err, lifted by decodeQoder's emit. Run - go test -tags nogui -p 2 -count=1 -run 'TestQoder|TestTextWritten|TestPluginTextWritten|TestQuiet' ./internal/gateway/: ok (new TestQoderUnreadableToolCall: the four defect shapes red on 2e2c8903 — 200 with the block as content, and the cut-short whole-JSON call as text — green here; the two stays-text guards green on both) - gofmt -l internal/: clean. go vet -tags nogui ./internal/gateway: clean. - Full matrix and ablation recorded in the issue body. Generated-by: GLM-5.3-Flash (ZCode) --- internal/gateway/qoder.go | 95 +++++++++++++++++++++++------ internal/gateway/qoder_test.go | 108 +++++++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+), 18 deletions(-) diff --git a/internal/gateway/qoder.go b/internal/gateway/qoder.go index 46a10b21f..cb73efa6b 100644 --- a/internal/gateway/qoder.go +++ b/internal/gateway/qoder.go @@ -562,9 +562,29 @@ func decodeQoder(ctx context.Context, res *http.Response, out chan<- Event, mode return } a := qoderText{tool: -1} + // emit lifts one piece of the split text into events: a call, the text, + // or a call that can't be read, which ends the turn as an error so the + // agent can try it again (plugins#132, as the moved qoder plugin's). + // False when the turn ends here: the consumer went, or an error was + // sent — an error frame carries no finish. + emit := func(frag qoderFrag) bool { + switch { + case frag.err != "": + send(Event{Kind: KError, Status: 502, Text: frag.err}) + return false + case frag.call != nil: + if !send(Event{Kind: KToolStart, ID: "call_" + qoder.NewID(), Name: frag.call.name}) { + return false + } + return send(Event{Kind: KToolArgs, Text: frag.call.args}) + case frag.text != "": + return send(Event{Kind: KText, Text: frag.text}) + } + return true + } finish := func() { for _, frag := range a.flush() { - if frag.text != "" && !send(Event{Kind: KText, Text: frag.text}) { + if !emit(frag) { return } } @@ -606,17 +626,7 @@ func decodeQoder(ctx context.Context, res *http.Response, out chan<- Event, mode } if content := delta.Get("content"); content.Exists() { for _, frag := range a.feed(content.String()) { - ev := Event{} - if frag.call != nil { - ev = Event{Kind: KToolStart, ID: "call_" + qoder.NewID(), Name: frag.call.name} - if !send(ev) { - return - } - ev = Event{Kind: KToolArgs, Text: frag.call.args} - } else { - ev = Event{Kind: KText, Text: frag.text} - } - if !send(ev) { + if !emit(frag) { return } } @@ -628,7 +638,7 @@ func decodeQoder(ctx context.Context, res *http.Response, out chan<- Event, mode if idx != a.tool { a.tool = idx for _, frag := range a.flush() { - if frag.text != "" && !send(Event{Kind: KText, Text: frag.text}) { + if !emit(frag) { return } } @@ -664,21 +674,49 @@ func decodeQoder(ctx context.Context, res *http.Response, out chan<- Event, mode } // qoderText is the streaming tool-call splitter: it holds text until a full -// tool-call block arrives, then emits it as a call. +// tool-call block arrives, then emits it as a call. A block on its way to +// being a call that can't be read, or that the stream ends inside, is an +// error, never the answer (plugins#132, as the moved qoder plugin's 0.2.16): +// the turn fails, so it can be tried again. A block whose JSON is whole when +// the stream ends without its close is still the call; one that is no call +// at all, or one inside code, stays text. type qoderText struct { textBuf string callBuf string inCall bool sawTool bool - tool int // index of the native tool call being assembled, -1 for none + sent string // the text given out so far, to tell a block in code from a call + tool int // index of the native tool call being assembled, -1 for none } type qoderFrag struct { text string call *qoderCall + err string } type qoderCall struct{ name, args string } +// qoderQuoted says text ends inside a code fence or an inline code span: a +// tool-call block there is prose about one, not a call. +func qoderQuoted(text string) bool { + if strings.Count(text, "```")%2 == 1 { + return true + } + line := text[strings.LastIndex(text, "\n")+1:] + return strings.Count(strings.ReplaceAll(line, "```", ""), "`")%2 == 1 +} + +// qoderCallish says a block was on its way to being a call: its JSON began, +// or the XML function form. +func qoderCallish(v string) bool { + return strings.HasPrefix(strings.TrimLeft(v, " \t\r\n"), "{") || strings.Contains(v, "\x3cfunction=") +} + +const ( + qoderCallUnreadable = "Qoder: the model's tool call, written as text, can't be read; it is not sent on as the answer" + qoderCallCutShort = "Qoder: the model's tool call, written as text, was cut short; it is not sent on as the answer" +) + func (a *qoderText) feed(input string) []qoderFrag { var out []qoderFrag for input != "" { @@ -688,6 +726,7 @@ func (a *qoderText) feed(input string) []qoderFrag { i := strings.Index(comb, qoderCallOpen) if i < 0 { if keep := qoderSuffix(comb); keep < len(comb) { + a.text(comb[:len(comb)-keep]) out = append(out, qoderFrag{text: comb[:len(comb)-keep]}) a.textBuf = comb[len(comb)-keep:] } else { @@ -696,6 +735,7 @@ func (a *qoderText) feed(input string) []qoderFrag { break } if i > 0 { + a.text(comb[:i]) out = append(out, qoderFrag{text: comb[:i]}) } input = comb[i+len(qoderCallOpen):] @@ -709,11 +749,16 @@ func (a *qoderText) feed(input string) []qoderFrag { a.callBuf = comb break } - if c, ok := qoderParseCall(comb[:j]); ok { + body := comb[:j] + if c, ok := qoderParseCall(body); ok { a.sawTool = true out = append(out, qoderFrag{call: &c}) + } else if qoderCallish(body) && !qoderQuoted(a.sent) { + a.inCall = false + return append(out, qoderFrag{err: qoderCallUnreadable}) } else { - out = append(out, qoderFrag{text: qoderCallOpen + comb[:j] + qoderCallClose}) + a.text(qoderCallOpen + body + qoderCallClose) + out = append(out, qoderFrag{text: qoderCallOpen + body + qoderCallClose}) } input = comb[j+len(qoderCallClose):] a.inCall = false @@ -721,10 +766,23 @@ func (a *qoderText) feed(input string) []qoderFrag { return out } +// text hands a piece of text out, remembering it: whether the next block is +// inside code is told from what came before it. +func (a *qoderText) text(t string) { a.sent += t } + func (a *qoderText) flush() []qoderFrag { if a.inCall { - t := qoderCallOpen + a.callBuf + body := a.callBuf a.inCall, a.callBuf = false, "" + if qoderCallish(body) && !qoderQuoted(a.sent) { + if c, ok := qoderParseCall(body); ok { + a.sawTool = true + return []qoderFrag{{call: &c}} + } + return []qoderFrag{{err: qoderCallCutShort}} + } + t := qoderCallOpen + body + a.text(t) if t != "" { return []qoderFrag{{text: t}} } @@ -732,6 +790,7 @@ func (a *qoderText) flush() []qoderFrag { if a.textBuf != "" { t := a.textBuf a.textBuf = "" + a.text(t) return []qoderFrag{{text: t}} } return nil diff --git a/internal/gateway/qoder_test.go b/internal/gateway/qoder_test.go index 35c6e361c..6bff9049b 100644 --- a/internal/gateway/qoder_test.go +++ b/internal/gateway/qoder_test.go @@ -263,6 +263,114 @@ func TestQoderEOF(t *testing.T) { } } +// A tool call written into the text that can't be read ends the turn as an +// error, never as the answer (plugins#132, as the moved qoder plugin's +// 0.2.16): a whole block whose JSON doesn't parse is a 502, and one the +// stream ends inside is the call when its JSON is whole, else a 502. A +// marker that is no call at all, or a block inside code, stays text. +func TestQoderUnreadableToolCall(t *testing.T) { + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + bad := qoderCallOpen + `{"path":}` + qoderCallClose + cutWhole := qoderCallOpen + `{"name":"read","arguments":{"path":"x"}}` + for _, tt := range []struct { + name string + lines []string + stream bool + want int + noText string // never in the reply + text string // the whole reply's text, when text is wanted + frame string // in the reply as it came, when a frame is wanted + hasCall string // a tool call named so, when one is wanted + }{ + { + name: "unreadable block, whole reply", + lines: []string{qoderChunk(qoderContent(t, bad)), qoderChunk(`{"choices":[{"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1}}`)}, + stream: false, + want: 502, + noText: "tool_call>", + }, + { + name: "unreadable block, streamed", + lines: []string{qoderChunk(qoderContent(t, bad)), qoderChunk(`{"choices":[{"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1}}`)}, + stream: true, + want: 502, + noText: "tool_call>", + }, + { + // text was already going out, so the 200 stands: the reason is + // an error frame, and no finish follows it + name: "unreadable block after text, streamed", + lines: []string{qoderChunk(qoderContent(t, "Sure. ")), qoderChunk(qoderContent(t, bad)), qoderChunk(`{"choices":[{"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1}}`)}, + stream: true, + want: 200, + noText: `"finish_reason":"stop"`, + frame: "can't be read", + }, + { + name: "stream ends inside, JSON whole", + lines: []string{qoderChunk(qoderContent(t, cutWhole)), qoderChunk(`{"choices":[{"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1}}`)}, + stream: false, + want: 200, + noText: "tool_call>", + hasCall: "read", + }, + { + name: "stream ends inside, unreadable", + lines: []string{qoderChunk(qoderContent(t, qoderCallOpen+`{"path":`)), qoderChunk(`{"choices":[{"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1}}`)}, + stream: false, + want: 502, + noText: "tool_call>", + }, + { + name: "a marker that is no call stays text", + lines: []string{qoderChunk(qoderContent(t, "before "+qoderCallOpen+"incomplete")), qoderChunk(`{"choices":[{"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1}}`)}, + stream: false, + want: 200, + text: "before " + qoderCallOpen + "incomplete", + }, + { + name: "a block inside code stays text", + lines: []string{qoderChunk(qoderContent(t, "```text\n"+bad+"\n```")), qoderChunk(`{"choices":[{"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1}}`)}, + stream: false, + want: 200, + text: bad, + }, + } { + t.Run(tt.name, func(t *testing.T) { + done := qoderUpstream(t, tt.lines) + defer done() + s := New() + w := httptest.NewRecorder() + body := `{"model":"qfmodel","stream":` + map[bool]string{true: "true", false: "false"}[tt.stream] + `,"messages":[{"role":"user","content":"hi"}]}` + r := httptest.NewRequest("POST", "/", strings.NewReader(body)) + var u Usage + status, msg := s.serveQoder(w, r, provider.Chat, provider.Provider{Account: &provider.Account{User: "test"}}, "qfmodel", []byte(body), &u) + out := w.Body.String() + if status != tt.want { + t.Fatalf("status %d msg %q want %d\n%s", status, msg, tt.want, out) + } + if tt.noText != "" && strings.Contains(out, tt.noText) { + t.Fatalf("the unreadable block went out as the answer:\n%s", out) + } + if tt.text != "" && !strings.Contains(gjson.Get(out, "choices.0.message.content").String(), tt.text) { + t.Fatalf("text was changed to %q:\n%s", gjson.Get(out, "choices.0.message.content").String(), out) + } + if tt.frame != "" && !strings.Contains(out, tt.frame) { + t.Fatalf("the reply doesn't carry the reason:\n%s", out) + } + if tt.hasCall != "" { + if gjson.Get(out, "choices.0.message.tool_calls.0.function.name").String() != tt.hasCall { + t.Fatalf("the whole-JSON call cut short is not a call:\n%s", out) + } + if gjson.Get(out, "choices.0.finish_reason").String() != "tool_calls" { + t.Fatalf("finish %q, want tool_calls:\n%s", gjson.Get(out, "choices.0.finish_reason").String(), out) + } + } + }) + } +} + func TestQoderEnvelopeErrors(t *testing.T) { for _, status := range []int{401, 403, 429, 500} { for _, proto := range []provider.Protocol{provider.Chat, provider.Responses, provider.Anthropic, provider.Gemini} {