From f76b48ea09df7b0dad82fac0f909fb40dd4e8eb0 Mon Sep 17 00:00:00 2001 From: ggbdpq Date: Tue, 3 Nov 2026 06:47:58 +0800 Subject: [PATCH] gateway: a Chat finish of tool_calls with no call in the stream ends stop (#4944) What changed: Hermes through magpie (stream=true, tools, deep reasoning): a reply of text alone ended, on the reporter's upstream, on a frame saying finish_reason "tool_calls" that no delta ever carried a call for - the shape the reporter's bare call of the model ends its real calls with ("delta":{"reasoning_content":null}, "finish_reason":"tool_calls","matched_stop":1); its text-only turns end there too, and the client took every such turn for one whose calls were dropped and re-prompted, three times a reply. The other gateway on the same upstream said stop. chatTidy's mend that ends a finish of tool_calls with no call left stop (#1475) only ran for a choice some delta had carried a tool_calls key for: the case "tool_calls" sat inside the t.called[choice] guard the case "stop" needs, and a reply whose stream never carried the key passed the misleading finish on. The switch now runs for every choice's finish: a finish of tool_calls with no readable call ends stop whether or not a delta carried the key, and the stop of a choice that made calls still ends tool_calls. A legacy function_call a client may still be reading (#4659) keeps a finish that says the turn ended in a call, and a "function_call": null is still the contract's own nothing. Whole replies are untouched: #1475's review narrowed chatWhole to a finish whose call was dropped and pinned a whole reply that came with no tool_calls at all as going as it came (TestToolCallsFinishWithNoCallsStaysWhole); no report covers them. Run: go test -tags nogui -count=1 ./internal/gateway: one red, TestCodexPoolSaysTheClientsVersion, green on its own - the known pre-existing red of this machine; the two new tests and the family guards among the rest, ok. go test -tags nogui -count=1 -timeout 45m ./internal/...: 74 packages ok; the four failures are all red at the parent 173b7e274 single-run too, none of this commit's own (TestMothX in agent, TestProxyCheckSaysAProxyPassedBy in gui, TestHostToldTheGatewayAddress in plugin, and the codex pool one above). Red at the parent with chattidy.go from the parent and the tests kept: TestChatNoCallToolCallsFinishEndsStop "calls map[], finish "tool_calls"" and TestChatRelayLegacyCallFinish "finish "tool_calls"" - the reported symptom; green again with the commit restored. gofmt -l: clean. go vet -tags nogui ./...: clean. go build -tags nogui: darwin, GOOS=linux, GOOS=windows: ok. Reviewed and ported: every line read against origin/main 1b0340fdb (chatTidy runs before textCallTidy, which sets tool_calls itself when it makes calls from text, so a written call still ends tool_calls; a finish line reaches the switch through needsTidy's "tool_calls" match). On mote: TestChatNoCallToolCallsFinishEndsStop and TestChatRelayLegacyCallFinish are red with chattidy.go put back to 1b0340fdb ('stream: calls map[] finish "tool_calls"' and 'stream, function_call null and no call: finish "tool_calls"'), green with it; the chattidy family (TestChatCallsEndToolCalls, TestToolCallsFinish*, tidy/nameless/function_call tests) green. On mote at this tree: go test -tags nogui -count=1 -timeout 45m ./internal/gateway/ ./internal/netproxy/: ok (gateway 1025.9s, netproxy 5.3s); go vet -tags nogui of both packages for darwin, GOOS=linux and GOOS=windows: clean; gofmt -l: clean. Not tried: a real Hermes against the reporter's upstream. Not covered: the translated route (a Chat upstream's "tool_calls" finish with no call, to an Anthropic or Responses client) still says tool_use; collector.finish only mends stop to tool, not back. Generated-by: GLM-5.3-Flash (ZCode) Co-authored-by: ggbdpq --- internal/gateway/chattidy.go | 63 +++++++++++++++++------- internal/gateway/chattidy_finish_test.go | 55 +++++++++++++++++++++ 2 files changed, 100 insertions(+), 18 deletions(-) diff --git a/internal/gateway/chattidy.go b/internal/gateway/chattidy.go index fe7e23e6a..093f21dfe 100644 --- a/internal/gateway/chattidy.go +++ b/internal/gateway/chattidy.go @@ -77,7 +77,11 @@ import ( // own log says the reply went (AricRedemption). A nameless call runs // nowhere, so it is no call, as a written block naming no offered tool // is; a name of another kind on a later fragment is left out, the first -// one's kept. +// one's kept. And a finish of tool_calls no delta ever carried a call +// for (#4944, Hermes: a reply of text alone whose upstream ends it +// "tool_calls" all the same) ends stop too — a client that reads +// finish_reason took the turn for one whose calls were dropped and +// re-prompted, three times a reply. // // And a legacy function_call that carries nothing — a name and arguments // both empty — is left out too (#4659): WorkBuddy's stream ends a reply @@ -292,9 +296,18 @@ func (t *chatTidy) line(line []byte) []byte { delete(delta, "tool_calls") inDelta = true } - if fc, ok := delta["function_call"]; ok && emptyFunctionCall(fc) { - delete(delta, "function_call") - inDelta = true + if fc, ok := delta["function_call"]; ok { + if fullFunctionCall(fc) { + // a legacy call a client may still be reading (#4659) + // keeps a finish that says the turn ended in a call + if t.named == nil { + t.named = map[int]bool{} + } + t.named[choice] = true + } else if emptyFunctionCall(fc) { + delete(delta, "function_call") + inDelta = true + } } if calls, ok := delta["tool_calls"]; ok { if t.indexes == nil { @@ -338,20 +351,19 @@ func (t *chatTidy) line(line []byte) []byte { c["delta"], _ = json.Marshal(delta) changed = true } - if t.called[choice] { - switch end := string(bytes.TrimSpace(c["finish_reason"])); end { - case `"stop"`: - if t.named[choice] { - c["finish_reason"] = json.RawMessage(`"tool_calls"`) - changed = true - } - case `"tool_calls"`: - if !t.named[choice] { - // the call the finish named named no function: it ends - // stop, as a reply that made no call (#1475) - c["finish_reason"] = json.RawMessage(`"stop"`) - changed = true - } + switch end := string(bytes.TrimSpace(c["finish_reason"])); end { + case `"stop"`: + if t.called[choice] && t.named[choice] { + c["finish_reason"] = json.RawMessage(`"tool_calls"`) + changed = true + } + case `"tool_calls"`: + if !t.named[choice] { + // the call the finish named named no function, or no call + // was ever made and no delta carried the key (#4944): it + // ends stop, as a reply that made no call (#1475) + c["finish_reason"] = json.RawMessage(`"stop"`) + changed = true } } } @@ -384,6 +396,21 @@ func emptyFunctionCall(raw json.RawMessage) bool { return decodeJSON(raw, &fc) == nil && fc.Name == "" && fc.Arguments == "" } +// fullFunctionCall reports whether raw is a legacy function_call a client +// may still be reading: a name or arguments that hold something (#4659). +// A null is the contract's own nothing and an unreadable shape is no call +// a client reads. +func fullFunctionCall(raw json.RawMessage) bool { + if string(bytes.TrimSpace(raw)) == "null" { + return false + } + var fc struct { + Name string `json:"name"` + Arguments string `json:"arguments"` + } + return decodeJSON(raw, &fc) == nil && (fc.Name != "" || fc.Arguments != "") +} + // withoutEmptyNames drops the empty "name" on a delta's tool-call // fragments and reports whether any was dropped. A fragment whose // function carries no name at all is left as it is; one with a real name diff --git a/internal/gateway/chattidy_finish_test.go b/internal/gateway/chattidy_finish_test.go index 4087353af..ad1114702 100644 --- a/internal/gateway/chattidy_finish_test.go +++ b/internal/gateway/chattidy_finish_test.go @@ -53,3 +53,58 @@ func TestChatCallsEndToolCalls(t *testing.T) { t.Fatalf("whole, no call: finish %q\n%s", finish, raw) } } + +// #4944 (Hermes through magpie): a stream whose reply is text alone ends, +// on the reporter's upstream, on a frame that says finish_reason +// "tool_calls" and carries no tool_calls key in any delta — the reporter's +// bare call of the model ends its real calls with just that shape +// ("delta":{"reasoning_content":null},"finish_reason":"tool_calls", +// "matched_stop":1), and its text-only turns end there too. A client that +// reads finish_reason took every such turn for one whose calls were +// dropped and re-prompted, three times a reply. A finish of tool_calls +// with no call the client could read ends stop, whether or not a delta +// ever carried the key. +func TestChatNoCallToolCallsFinishEndsStop(t *testing.T) { + up := []string{ + `data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":"The user said hi."}}]}`, + `data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"收到"}}]}`, + `data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"reasoning_content":null},"logprobs":null,"finish_reason":"tool_calls","matched_stop":1}]}`, + } + for _, body := range []string{ + `{"model":"a/m","stream":true,"messages":[{"role":"user","content":"只回复两个字:收到"}],"reasoning_effort":"max",` + execChatTool + `}`, + `{"model":"a/m","stream":true,"messages":[{"role":"user","content":"hi"}]}`, + } { + _, calls, finish, raw := relayedChat(t, body, up...) + if finish != "stop" || len(calls) != 0 { + t.Fatalf("stream: calls %v finish %q\n%s", calls, finish, raw) + } + } + // the finish naming a call with the final delta's "tool_calls": null + // beside it (#3259's shape) ends stop the same way + _, _, finish, raw := relayedChat(t, `{"model":"a/m","stream":true,"messages":[{"role":"user","content":"hi"}]}`, + `data: {"choices":[{"index":0,"delta":{"content":"hi"}}]}`, + `data: {"choices":[{"index":0,"delta":{"content":null,"tool_calls":null},"finish_reason":"tool_calls"}]}`) + if finish != "stop" { + t.Fatalf("stream, tool_calls null and no call: finish %q\n%s", finish, raw) + } +} + +// A legacy function_call a client may still be reading keeps a finish +// that says the turn ended in a call, "tool_calls" or not (#4659); a +// "function_call": null is the contract's own nothing and is not one. +func TestChatRelayLegacyCallFinish(t *testing.T) { + tools := `{"model":"a/m","stream":true,"messages":[{"role":"user","content":"weather?"}],"tools":[{"type":"function","function":{"name":"get_weather","parameters":{"type":"object"}}}]}` + _, _, finish, raw := relayedChat(t, tools, + `data: {"choices":[{"index":0,"delta":{"function_call":{"name":"get_weather","arguments":""}}}]}`, + `data: {"choices":[{"index":0,"delta":{"function_call":{"name":"","arguments":"{\"city\":\"北京\"}"}}}]}`, + `data: {"choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}`) + if finish != "tool_calls" { + t.Fatalf("stream, a legacy call: finish %q\n%s", finish, raw) + } + _, _, finish, raw = relayedChat(t, tools, + `data: {"choices":[{"index":0,"delta":{"content":"hi","function_call":null}}]}`, + `data: {"choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}`) + if finish != "stop" { + t.Fatalf("stream, function_call null and no call: finish %q\n%s", finish, raw) + } +}