AricRedemption on #1475: a long Qoder turn ended with an upstream tool call whose function.name was no string, and the AI SDK refused the whole reply ("Expected 'function.name' to be a string"), not retryable and with the call already in the history, so every "continue" died there while the gateway logged status=200 done=true. The parsers for calls a model writes into its text already refuse a nameless one, but calls an upstream made itself went out as they came. A call whose function names nothing in a JSON string (absent, null, another kind) is dropped on every path a relayed Chat reply leaves by: a stream (chatTidy.nameless, its later fragments with it, and the id mend no longer gives the dropped fragments an id back), a whole reply (wholeNameless in chatWhole) and a whole reply given as the stream the agent asked for (chatWholeEvents). A finish of tool_calls with no call left ends stop. An empty string name goes as it came. Translated routes are unchanged: their names are Go strings by construction. Changed from gua2100's patch (hunt61-a-1475-unnamed-call, d851d922f): - chatWholeEvents turned the finish to stop whenever any call was dropped, so a named call beside a nameless one went out with finish stop and the client ended its turn without running it; it now does so only when no call is left. The calls it keeps are numbered 0.. with no gap where a dropped one stood, and a call with no function object is dropped there too, as wholeNameless already did. - chatWhole turned any whole reply ending tool_calls with no named call into stop, a reply that came with no tool_calls at all included; it now does so only when wholeNameless dropped a call. New tests for both: TestNamelessCallBesideAGoodOneWholeAsStream, TestToolCallsFinishWithNoCallsStaysWhole. Run at this head (451c7e929 on576b5dcbc), on mote: - go test -tags nogui -count=1 -timeout 45m ./internal/gateway: ok (528s). The first try at e53ba61f4 was red only on TestClaudeCallerEnvironmentInAMessageIsTheLastGiven, red 2 of 20 on origin/main too; fixed at its cause in576b5dcbc, under this commit. - go test -tags nogui -count=1 -run '^(TestNameless|TestNameAbsent| TestToolCallsFinishWithNoCallsStaysWhole|TestChatCallsEndToolCalls| TestEmptyCallID|TestStreamedCallGivenAnID|TestTextWritten|TestDSML| TestQuietWholeReplyStreamed|TestPluginTextWrittenCall|TestChatTidy| TestGeminiCompat|TestWhole|TestRelayed)' ./internal/gateway: ok. - go vet -tags nogui ./internal/gateway on darwin, GOOS=linux and GOOS=windows: clean (at the review tree). gofmt -l: clean. - Not run: go test ./... across every package. Red on main: with chattidy.go, whole_stream.go and chat_call_ids.go from origin/main, 7 of the 8 new tests fail, e.g. TestNamelessCallStreamDropped "calls map[], finish \"tool_calls\"" and TestNamelessCallBesideAGoodOneWholeAsStream "[{Index:0 ID:call_bad ...} {Index:1 ID:call_up ...}]" (TestToolCallsFinishWithNoCallsStaysWhole guards unchanged behaviour and passes there). Broken by hand: - the patch's own chattidy.go and whole_stream.go: TestNamelessCallBesideAGoodOneWholeAsStream fails with "[{Index:1 ID:call_up ...}], finish \"stop\"" and TestToolCallsFinishWithNoCallsStaysWhole with finish "stop". - the `calls = nil` clearing removed: TestNamelessCallWithoutAnIDStaysDropped "the call dropped for naming no function came back with an id". - wholeNameless not called in chatWhole: TestNamelessCallWholeDropped "calls [{call_up ...}], finish \"tool_calls\"". Not tried: a real Qoder subscription or the reporter's exact reply (the report says replays came back well formed; fixtures are synthetic with the shape the client's error names), a real AI SDK client. Not covered: a relayed stream that keeps a named call beside a dropped one keeps the upstream's index numbers, so a gap is left where the dropped call stood; Responses and Anthropic relays (responsesWholeEvents, anthropicWholeEvents, passthrough) forwarding a nameless function_call or tool_use, which has no report. Co-authored-by: ggbdpq <ggbdpq@gmail.com>
205 lines
10 KiB
Go
205 lines
10 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// AricRedemption on #1475: a long Qoder turn ended with an upstream tool
|
|
// call whose function.name was no string, and the client that checks the
|
|
// shape of every frame (the AI SDK) refused the whole reply — "Expected
|
|
// 'function.name' to be a string" — the turn not retryable and the call
|
|
// already in the history, so the session was dead; magpie's own log said
|
|
// status=200, done=true. A call no client can read runs nowhere, so it is
|
|
// no call: it is dropped, its later fragments with it, and a finish of
|
|
// tool_calls with no call left ends stop — what textcalls' parser does
|
|
// with a written block that names no offered tool. A name the client
|
|
// reads as a string, empty included, goes as it came.
|
|
|
|
// The first fragment of a streamed call naming no function in a string,
|
|
// its argument fragments dropped with it, a finish of tool_calls ended
|
|
// stop, the text before the call kept.
|
|
func TestNamelessCallStreamDropped(t *testing.T) {
|
|
first := `data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"call_up","type":"function","function":{"name":null,"arguments":""}}]},"finish_reason":null}]}`
|
|
args := `data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\"cmd\":\"ls -la\"}"}}]},"finish_reason":null}]}`
|
|
ended := strings.Replace(stopChunk, `"stop"`, `"tool_calls"`, 1)
|
|
text, calls, finish, raw := relayedChat(t,
|
|
`{"model":"a/m","stream":true,"messages":[{"role":"user","content":"go"}],`+execChatTool+`}`,
|
|
chunkOf("Running it."), first, args, ended)
|
|
if text != "Running it." || len(calls) != 0 || finish != "stop" {
|
|
t.Fatalf("text %q, calls %v, finish %q\n%s", text, calls, finish, raw)
|
|
}
|
|
if strings.Contains(raw, `"tool_calls":[`) {
|
|
t.Fatalf("a call naming no function reached the client:\n%s", raw)
|
|
}
|
|
}
|
|
|
|
// The same with the name field absent, as the merged deltas leave it for
|
|
// a client to read.
|
|
func TestNameAbsentStreamDropped(t *testing.T) {
|
|
first := `data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"call_up","type":"function","function":{"arguments":""}}]},"finish_reason":null}]}`
|
|
args := `data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\"cmd\":\"ls\"}"}}]},"finish_reason":null}]}`
|
|
_, calls, finish, raw := relayedChat(t,
|
|
`{"model":"a/m","stream":true,"messages":[{"role":"user","content":"go"}],`+execChatTool+`}`,
|
|
first, args, stopChunk)
|
|
if len(calls) != 0 || finish != "stop" {
|
|
t.Fatalf("calls %v, finish %q\n%s", calls, finish, raw)
|
|
}
|
|
if strings.Contains(raw, `"tool_calls":[`) {
|
|
t.Fatalf("a call naming no function reached the client:\n%s", raw)
|
|
}
|
|
}
|
|
|
|
// The same dropped call the upstream gave no id for: the call is dropped,
|
|
// yet it comes back — the drop takes it out of the delta, but the
|
|
// fragments themselves stay in the local calls, and the id mend that
|
|
// follows (#2611) gives the first one an id and writes them back. The
|
|
// frame the client refuses leaves after all, an id on it.
|
|
func TestNamelessCallWithoutAnIDStaysDropped(t *testing.T) {
|
|
first := `data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"type":"function","function":{"name":null,"arguments":""}}]},"finish_reason":null}]}`
|
|
args := `data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\"cmd\":\"ls -la\"}"}}]},"finish_reason":null}]}`
|
|
ended := strings.Replace(stopChunk, `"stop"`, `"tool_calls"`, 1)
|
|
_, calls, finish, raw := relayedChat(t,
|
|
`{"model":"a/m","stream":true,"messages":[{"role":"user","content":"go"}],`+execChatTool+`}`,
|
|
chunkOf("Running it."), first, args, ended)
|
|
if len(calls) != 0 || finish != "stop" {
|
|
t.Fatalf("calls %v, finish %q\n%s", calls, finish, raw)
|
|
}
|
|
if strings.Contains(raw, `"tool_calls":[`) {
|
|
t.Fatalf("the call dropped for naming no function came back with an id:\n%s", raw)
|
|
}
|
|
}
|
|
|
|
// A call with a name stands beside one without: the named one goes, the
|
|
// nameless one alone is dropped, and the finish stays tool_calls.
|
|
func TestNamelessCallBesideAGoodOne(t *testing.T) {
|
|
mixed := `data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"call_up","type":"function","function":{"name":"exec_command","arguments":"{\"cmd\":\"ls\"}"}},{"index":1,"id":"call_bad","type":"function","function":{"name":null,"arguments":""}}]},"finish_reason":null}]}`
|
|
_, calls, finish, raw := relayedChat(t,
|
|
`{"model":"a/m","stream":true,"messages":[{"role":"user","content":"go"}],`+execChatTool+`}`,
|
|
mixed, stopChunk)
|
|
if len(calls) != 1 || calls["exec_command"] != `{"cmd":"ls"}` || finish != "tool_calls" {
|
|
t.Fatalf("calls %v, finish %q\n%s", calls, finish, raw)
|
|
}
|
|
if strings.Contains(raw, "call_bad") {
|
|
t.Fatalf("the nameless call reached the client:\n%s", raw)
|
|
}
|
|
}
|
|
|
|
// A whole reply's call naming no function is dropped before it is
|
|
// relayed, and its finish of tool_calls ends stop; the text is kept.
|
|
func TestNamelessCallWholeDropped(t *testing.T) {
|
|
up := `{"id":"c1","object":"chat.completion","created":1791370000,"model":"m","choices":[{"index":0,"message":{"role":"assistant","content":"Let me.","tool_calls":[{"id":"call_up","type":"function","function":{"name":null,"arguments":"{\"cmd\":\"ls\"}"}}]},"finish_reason":"tool_calls"}],"usage":{"prompt_tokens":3,"completion_tokens":2,"total_tokens":5}}`
|
|
text, calls, finish, raw := wholeChat(t,
|
|
`{"model":"a/m","stream":false,"messages":[{"role":"user","content":"go"}],`+execChatTool+`}`,
|
|
"application/json", up)
|
|
if text == nil || *text != "Let me." || len(calls) != 0 || finish != "stop" {
|
|
t.Fatalf("text %v, calls %v, finish %q\n%s", text, calls, finish, raw)
|
|
}
|
|
if strings.Contains(raw, `"tool_calls"`) {
|
|
t.Fatalf("a call naming no function reached the client:\n%s", raw)
|
|
}
|
|
}
|
|
|
|
// The whole reply a vendor gave after the agent was kept alive goes out
|
|
// as the stream the agent asked for (wholeAsStream): its call naming no
|
|
// function is dropped there too, and the finish with no call left ends
|
|
// stop.
|
|
func TestNamelessCallWholeAsStreamDropped(t *testing.T) {
|
|
up := `{"id":"c1","object":"chat.completion","created":1791370000,"model":"m","choices":[{"index":0,"message":{"role":"assistant","content":"Let me.","tool_calls":[{"id":"call_up","type":"function","function":{"name":null,"arguments":"{\"cmd\":\"ls\"}"}}]},"finish_reason":"tool_calls"}],"usage":{"prompt_tokens":3,"completion_tokens":2,"total_tokens":5}}`
|
|
var sawToolCalls, saidNull bool
|
|
var finish string
|
|
for _, e := range chatWholeEvents([]byte(up)) {
|
|
b, _ := json.Marshal(e.data)
|
|
s := string(b)
|
|
if strings.Contains(s, `"name":null`) || strings.Contains(s, `"name":`) && !strings.Contains(s, `"name":"`) {
|
|
saidNull = true
|
|
}
|
|
if strings.Contains(s, `"tool_calls"`) {
|
|
sawToolCalls = true
|
|
}
|
|
var ch struct {
|
|
Choices []struct {
|
|
FinishReason string `json:"finish_reason"`
|
|
} `json:"choices"`
|
|
}
|
|
if json.Unmarshal([]byte(s), &ch) == nil && len(ch.Choices) > 0 && ch.Choices[0].FinishReason != "" {
|
|
finish = ch.Choices[0].FinishReason
|
|
}
|
|
}
|
|
if sawToolCalls || saidNull {
|
|
t.Fatalf("the events carried a call naming no function (tool_calls %v, null name %v)", sawToolCalls, saidNull)
|
|
}
|
|
if finish != "stop" {
|
|
t.Fatalf("finish %q, want stop", finish)
|
|
}
|
|
}
|
|
|
|
// A whole reply given as a stream with a named call beside a nameless
|
|
// one: the named call goes, numbered 0 with no gap where the dropped one
|
|
// stood, and the finish stays tool_calls, since a call is left.
|
|
func TestNamelessCallBesideAGoodOneWholeAsStream(t *testing.T) {
|
|
up := `{"id":"c1","object":"chat.completion","created":1791370000,"model":"m","choices":[{"index":0,"message":{"role":"assistant","content":null,"tool_calls":[{"id":"call_bad","type":"function","function":{"name":null,"arguments":""}},{"id":"call_up","type":"function","function":{"name":"exec_command","arguments":"{\"cmd\":\"ls\"}"}}]},"finish_reason":"tool_calls"}]}`
|
|
var finish string
|
|
var got []struct {
|
|
Index int `json:"index"`
|
|
ID string `json:"id"`
|
|
Function struct {
|
|
Name string `json:"name"`
|
|
} `json:"function"`
|
|
}
|
|
for _, e := range chatWholeEvents([]byte(up)) {
|
|
if _, done := e.data.(string); done {
|
|
continue // [DONE]
|
|
}
|
|
b, _ := json.Marshal(e.data)
|
|
var ch struct {
|
|
Choices []struct {
|
|
Delta struct {
|
|
ToolCalls []struct {
|
|
Index int `json:"index"`
|
|
ID string `json:"id"`
|
|
Function struct {
|
|
Name string `json:"name"`
|
|
} `json:"function"`
|
|
} `json:"tool_calls"`
|
|
} `json:"delta"`
|
|
FinishReason *string `json:"finish_reason"`
|
|
} `json:"choices"`
|
|
}
|
|
if err := json.Unmarshal(b, &ch); err != nil {
|
|
t.Fatalf("unreadable event %s: %v", b, err)
|
|
}
|
|
for _, c := range ch.Choices {
|
|
for _, tc := range c.Delta.ToolCalls {
|
|
got = append(got, struct {
|
|
Index int `json:"index"`
|
|
ID string `json:"id"`
|
|
Function struct {
|
|
Name string `json:"name"`
|
|
} `json:"function"`
|
|
}(tc))
|
|
}
|
|
if c.FinishReason != nil {
|
|
finish = *c.FinishReason
|
|
}
|
|
}
|
|
}
|
|
if len(got) != 1 || got[0].ID != "call_up" || got[0].Index != 0 || got[0].Function.Name != "exec_command" || finish != "tool_calls" {
|
|
t.Fatalf("calls %+v, finish %q", got, finish)
|
|
}
|
|
}
|
|
|
|
// A whole reply that ends tool_calls with no call in it at all keeps its
|
|
// finish as it came: only a reply whose calls were dropped for naming no
|
|
// function ends stop.
|
|
func TestToolCallsFinishWithNoCallsStaysWhole(t *testing.T) {
|
|
up := `{"id":"c1","object":"chat.completion","created":1791370000,"model":"m","choices":[{"index":0,"message":{"role":"assistant","content":"Done."},"finish_reason":"tool_calls"}],"usage":{"prompt_tokens":3,"completion_tokens":2,"total_tokens":5}}`
|
|
text, _, finish, raw := wholeChat(t,
|
|
`{"model":"a/m","stream":false,"messages":[{"role":"user","content":"go"}],`+execChatTool+`}`,
|
|
"application/json", up)
|
|
if text == nil || *text != "Done." || finish != "tool_calls" {
|
|
t.Fatalf("text %v, finish %q\n%s", text, finish, raw)
|
|
}
|
|
}
|