timonwong (#2599): after the user stops a reply while the model is still thinking, the agent keeps a turn with no text and no calls and sends it again on every later turn. Translated (buildChat) or relayed, it reached the Chat upstream as {"role":"assistant","content":""} (reasoning_content alone, on DeepSeek), and DeepSeek and Kimi turned every request of the conversation away: "Invalid assistant message: content or tool_calls must be set". One rule, withoutSilentAssistants (chat.go), runs on both Chat routes: buildChat before pairToolMessages (Claude Code, Codex and Gemini clients, and the plugins fed by buildChat), and silentAssistantsOut in passthrough next to developerAsSystem (a Chat client's own body). An assistant message with no content or only blank text parts, and no tool_calls or function_call, is left out; the user messages it stood between join into one (texts with a blank line, else their parts in order), so no two user messages stand in a row. A prefill (prefix, partial, a Resume), a legacy function_call, a refusal and an audio reply stay. A relayed body without such a turn goes byte for byte. TestChatThinkingOnlyAssistantKeptOutside- Exchange pinned the old pass-through; it now expects the joined message. Built on ggbdpq's patch (cb438ebb on their fork) and its fake upstream, written again because the review found it missed shapes the issue names: - after a tool result it kept the empty turn ("a strict template wants the reply behind its tool message"), so the interruption mid tool loop still sent content "" and still got the 400. tool then user is what any agent sends when the user writes after a tool result; it is now left out. - the relay dropped an assistant with a legacy function_call or a refusal and empty content, and a client's own prefix/partial prefill; kept blank text-part arrays (still refused); joined only string users; re-marshalled with HTML escaping. - its Resume check compared a joinSplitCalls turn index with len(r.Messages)-1. Verified (sandbox HOME, pinned caches, go1.26.8 darwin/arm64, base origin/main72d6103e2): - The fake upstream refuses with the issue's literal 400 body any assistant without content or tool_calls, plus DeepSeek's #1462/#1822 reasoning rule. On main's product code 18 subtests of the new tests go red with that 400 (Claude Code: thinking, redacted, empty text, thinking+empty text, [], "", after a tool result; Codex: lone reasoning, empty output_text, no content; Gemini thought-only; relay: "", null, no content, blank parts, after a tool result; relay keeps-what-speaks), and TestChatThinkingOnlyAssistantLeftOutOutsideExchange. With ggbdpq's product files instead, after-a-tool-result (both routes), blank parts and TestChatRelaySilentAssistantsOnly are red. With this change all pass; TestChatResumeEmptyPrefillKept is a guard, green before and after. - gofmt -l: clean. go vet -tags nogui ./... for darwin, linux, windows: ok. Full suite with both this and the #2575 test change in the tree, set -o pipefail; go test -tags nogui -timeout 45m ./... : 66 packages ok, none failed (internal/gateway 825s, internal/provider 266s). - Rebased onto origin/mainec6454395(claude_subscription, ir.go): gofmt clean, vet darwin/linux/windows on internal/gateway and internal/settings ok; go test -tags nogui -timeout 45m ./internal/gateway ./internal/settings: ok (gateway 668s). - Not tried against real DeepSeek or Kimi (the fake answers with the issue's bytes). Gemini and Responses upstreams are not changed; the Messages build already drops an empty turn and merges same-role neighbours (anthropicBody's push). Co-authored-by: ggbdpq <67929842+ggbdpq@users.noreply.github.com>
343 lines
12 KiB
Go
343 lines
12 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// chatTrace renders the message sequence of a Chat request built for an
|
|
// upstream: roles with tool call ids, e.g.
|
|
// "user assistant(A,B) tool(A) tool(B) user".
|
|
func chatTrace(t *testing.T, r *Request) (string, []map[string]any) {
|
|
t.Helper()
|
|
body := buildChat(r, "kimi-k3", "api.moonshot.cn", false)
|
|
var q struct {
|
|
Messages []map[string]any `json:"messages"`
|
|
}
|
|
if err := json.Unmarshal(body, &q); err != nil {
|
|
t.Fatalf("built request does not parse: %v", err)
|
|
}
|
|
var b strings.Builder
|
|
for _, m := range q.Messages {
|
|
role, _ := m["role"].(string)
|
|
b.WriteString(" " + role)
|
|
if calls, ok := m["tool_calls"].([]any); ok {
|
|
var ids []string
|
|
for _, c := range calls {
|
|
id, _ := c.(map[string]any)["id"].(string)
|
|
ids = append(ids, id)
|
|
}
|
|
b.WriteString("(" + strings.Join(ids, ",") + ")")
|
|
}
|
|
if id, ok := m["tool_call_id"].(string); ok {
|
|
b.WriteString("(" + id + ")")
|
|
}
|
|
}
|
|
return strings.TrimSpace(b.String()), q.Messages
|
|
}
|
|
|
|
// kimiValid reports whether a built message sequence passes what Kimi
|
|
// validates: every tool message answers a call of the assistant message
|
|
// it follows, with only tool messages between.
|
|
func kimiValid(msgs []map[string]any) bool {
|
|
var pending map[string]bool
|
|
for _, m := range msgs {
|
|
switch m["role"] {
|
|
case "assistant":
|
|
pending = nil
|
|
if calls, ok := m["tool_calls"].([]any); ok {
|
|
pending = map[string]bool{}
|
|
for _, c := range calls {
|
|
id, _ := c.(map[string]any)["id"].(string)
|
|
pending[id] = true
|
|
}
|
|
}
|
|
case "tool":
|
|
id, _ := m["tool_call_id"].(string)
|
|
if !pending[id] {
|
|
return false
|
|
}
|
|
default:
|
|
pending = nil
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// idsUsedOnce reports whether every tool_call_id takes part in one
|
|
// exchange only: one assistant call, one tool answer at most. A strict
|
|
// upstream can turn away an id called or answered twice.
|
|
func idsUsedOnce(msgs []map[string]any) bool {
|
|
calls, answers := map[string]int{}, map[string]int{}
|
|
for _, m := range msgs {
|
|
if cs, ok := m["tool_calls"].([]any); ok {
|
|
for _, c := range cs {
|
|
id, _ := c.(map[string]any)["id"].(string)
|
|
calls[id]++
|
|
}
|
|
}
|
|
if m["role"] == "tool" {
|
|
id, _ := m["tool_call_id"].(string)
|
|
answers[id]++
|
|
}
|
|
}
|
|
for id, n := range calls {
|
|
if id == "" || n > 1 {
|
|
return false
|
|
}
|
|
}
|
|
for id, n := range answers {
|
|
if id == "" || n > 1 {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func call(id string) Part {
|
|
return Part{Kind: ToolCall, ID: id, Name: "run", Args: json.RawMessage(`{}`)}
|
|
}
|
|
func result(id, out string) Part {
|
|
return Part{Kind: ToolResult, CallID: id, Text: out}
|
|
}
|
|
|
|
// Claude Code makes parallel calls in one turn and the results come back
|
|
// out of order, beside the turn's own text (an attachment's reminder):
|
|
// the tool messages must lead, in the calls' order, the text after.
|
|
func TestChatToolResultsLeadInCallOrder(t *testing.T) {
|
|
r := &Request{Messages: []Message{
|
|
{Role: "user", Parts: []Part{{Kind: Text, Text: "run them"}}},
|
|
{Role: "assistant", Parts: []Part{call("A"), call("B"), call("C"), call("D")}},
|
|
{Role: "user", Parts: []Part{
|
|
{Kind: Text, Text: "note from an attachment"},
|
|
result("C", "3"), result("A", "1"), result("D", "4"), result("B", "2"),
|
|
}},
|
|
}}
|
|
trace, msgs := chatTrace(t, r)
|
|
want := "user assistant(A,B,C,D) tool(A) tool(B) tool(C) tool(D) user"
|
|
if trace != want {
|
|
t.Fatalf("trace = %q, want %q", trace, want)
|
|
}
|
|
if !kimiValid(msgs) {
|
|
t.Fatal("sequence fails Kimi's tool exchange validation")
|
|
}
|
|
}
|
|
|
|
// Results split over consecutive user messages (Claude Code logs one per
|
|
// message) still gather behind their assistant message, in call order.
|
|
func TestChatToolResultsGatherAcrossUserMessages(t *testing.T) {
|
|
r := &Request{Messages: []Message{
|
|
{Role: "assistant", Parts: []Part{call("A"), call("B")}},
|
|
{Role: "user", Parts: []Part{result("B", "2")}},
|
|
{Role: "user", Parts: []Part{result("A", "1")}},
|
|
}}
|
|
trace, msgs := chatTrace(t, r)
|
|
want := "assistant(A,B) tool(A) tool(B)"
|
|
if trace != want {
|
|
t.Fatalf("trace = %q, want %q", trace, want)
|
|
}
|
|
if !kimiValid(msgs) {
|
|
t.Fatal("sequence fails Kimi's tool exchange validation")
|
|
}
|
|
}
|
|
|
|
// A result whose call is gone (compaction cut it, or a client sends
|
|
// results alone) goes to the model as a user message, the way the
|
|
// Responses path's orphanedToolOutputs delivers it: no made-up call, no
|
|
// id used twice.
|
|
func TestChatOrphanToolResultBecomesUserMessage(t *testing.T) {
|
|
r := &Request{Messages: []Message{
|
|
{Role: "user", Parts: []Part{result("X", "kept")}},
|
|
{Role: "user", Parts: []Part{{Kind: Text, Text: "go on"}}},
|
|
}}
|
|
trace, msgs := chatTrace(t, r)
|
|
want := "user user"
|
|
if trace != want {
|
|
t.Fatalf("trace = %q, want %q", trace, want)
|
|
}
|
|
if c, _ := msgs[0]["content"].(string); c != "kept" {
|
|
t.Fatalf("orphan result content = %q", c)
|
|
}
|
|
if !kimiValid(msgs) || !idsUsedOnce(msgs) {
|
|
t.Fatal("sequence fails Kimi's tool exchange validation")
|
|
}
|
|
}
|
|
|
|
// A late result — its call's exchange is closed already (assistant(a),
|
|
// user text, user(tool_result a); Codex sends function_call, message,
|
|
// function_call_output) — must not reuse the id in a second exchange: it
|
|
// joins the user message its exchange closed with.
|
|
func TestChatLateToolResultJoinsUserMessage(t *testing.T) {
|
|
r := &Request{Messages: []Message{
|
|
{Role: "assistant", Parts: []Part{call("A")}},
|
|
{Role: "user", Parts: []Part{{Kind: Text, Text: "note"}}},
|
|
{Role: "user", Parts: []Part{result("A", "real result")}},
|
|
}}
|
|
trace, msgs := chatTrace(t, r)
|
|
want := "assistant(A) tool(A) user"
|
|
if trace != want {
|
|
t.Fatalf("trace = %q, want %q", trace, want)
|
|
}
|
|
if c, _ := msgs[1]["content"].(string); !strings.Contains(c, "unavailable") {
|
|
t.Fatalf("closed exchange's synthetic result = %q", c)
|
|
}
|
|
c, _ := msgs[2]["content"].(string)
|
|
if !strings.Contains(c, "note") || !strings.Contains(c, "real result") {
|
|
t.Fatalf("user message = %q", c)
|
|
}
|
|
if !kimiValid(msgs) || !idsUsedOnce(msgs) {
|
|
t.Fatal("sequence fails Kimi's tool exchange validation")
|
|
}
|
|
}
|
|
|
|
// Two results for the same call: the first answer stands, the second is
|
|
// dropped — a strict upstream refuses an id answered twice.
|
|
func TestChatDuplicateToolResultsKeepFirst(t *testing.T) {
|
|
r := &Request{Messages: []Message{
|
|
{Role: "assistant", Parts: []Part{call("A")}},
|
|
{Role: "user", Parts: []Part{result("A", "1"), result("A", "1 again")}},
|
|
}}
|
|
trace, msgs := chatTrace(t, r)
|
|
want := "assistant(A) tool(A)"
|
|
if trace != want {
|
|
t.Fatalf("trace = %q, want %q", trace, want)
|
|
}
|
|
if c, _ := msgs[1]["content"].(string); c != "1" {
|
|
t.Fatalf("answer = %q, want the first", c)
|
|
}
|
|
if !kimiValid(msgs) || !idsUsedOnce(msgs) {
|
|
t.Fatal("sequence fails Kimi's tool exchange validation")
|
|
}
|
|
}
|
|
|
|
// A result with an empty tool_call_id is an orphan too: it becomes a
|
|
// user message, never a call with id "".
|
|
func TestChatEmptyToolCallIDBecomesUserMessage(t *testing.T) {
|
|
r := &Request{Messages: []Message{
|
|
{Role: "user", Parts: []Part{result("", "no call")}},
|
|
}}
|
|
trace, msgs := chatTrace(t, r)
|
|
if trace != "user" {
|
|
t.Fatalf("trace = %q, want %q", trace, "user")
|
|
}
|
|
if c, _ := msgs[0]["content"].(string); c != "no call" {
|
|
t.Fatalf("content = %q", c)
|
|
}
|
|
if !idsUsedOnce(msgs) {
|
|
t.Fatal("an empty tool_call_id leaked into the exchange")
|
|
}
|
|
}
|
|
|
|
// An interrupted turn leaves a call unanswered: it gets a synthetic error
|
|
// result so the next turn can start.
|
|
func TestChatUnansweredCallGetsSyntheticResult(t *testing.T) {
|
|
r := &Request{Messages: []Message{
|
|
{Role: "assistant", Parts: []Part{call("A")}},
|
|
{Role: "user", Parts: []Part{{Kind: Text, Text: "继续"}}},
|
|
}}
|
|
trace, msgs := chatTrace(t, r)
|
|
want := "assistant(A) tool(A) user"
|
|
if trace != want {
|
|
t.Fatalf("trace = %q, want %q", trace, want)
|
|
}
|
|
if c, _ := msgs[1]["content"].(string); !strings.Contains(c, "unavailable") {
|
|
t.Fatalf("synthetic result content = %q", c)
|
|
}
|
|
if !kimiValid(msgs) {
|
|
t.Fatal("sequence fails Kimi's tool exchange validation")
|
|
}
|
|
}
|
|
|
|
// A thinking-only assistant turn (what an aborted stream leaves behind)
|
|
// inside a pending exchange is dropped before it can sit between the
|
|
// calls and their answers — and the exchange stays open, so the real
|
|
// result still answers its call instead of a synthetic "interrupted".
|
|
func TestChatThinkingOnlyAssistantDroppedInsideExchange(t *testing.T) {
|
|
r := &Request{Messages: []Message{
|
|
{Role: "assistant", Parts: []Part{call("A")}},
|
|
{Role: "assistant", Parts: []Part{{Kind: Thinking, Text: "hmm", Signature: "sig"}}},
|
|
{Role: "user", Parts: []Part{result("A", "1")}},
|
|
}}
|
|
trace, msgs := chatTrace(t, r)
|
|
want := "assistant(A) tool(A)"
|
|
if trace != want {
|
|
t.Fatalf("trace = %q, want %q", trace, want)
|
|
}
|
|
if c, _ := msgs[1]["content"].(string); c != "1" {
|
|
t.Fatalf("answer = %q, want the real result", c)
|
|
}
|
|
if !kimiValid(msgs) || !idsUsedOnce(msgs) {
|
|
t.Fatal("sequence fails Kimi's tool exchange validation")
|
|
}
|
|
}
|
|
|
|
// Outside an exchange a thinking-only turn, one the user cut off before
|
|
// it said anything, is left out too: DeepSeek and Kimi turn the request
|
|
// away over an assistant message with neither content nor tool_calls
|
|
// (#2599). The two user messages it stood between join into one, as some
|
|
// models' chat templates turn away two in a row.
|
|
func TestChatThinkingOnlyAssistantLeftOutOutsideExchange(t *testing.T) {
|
|
r := &Request{Messages: []Message{
|
|
{Role: "user", Parts: []Part{{Kind: Text, Text: "hi"}}},
|
|
{Role: "assistant", Parts: []Part{{Kind: Thinking, Text: "hmm", Signature: "sig"}}},
|
|
{Role: "user", Parts: []Part{{Kind: Text, Text: "继续"}}},
|
|
}}
|
|
trace, msgs := chatTrace(t, r)
|
|
want := "user"
|
|
if trace != want {
|
|
t.Fatalf("trace = %q, want %q", trace, want)
|
|
}
|
|
if c, _ := msgs[0]["content"].(string); c != "hi\n\n继续" {
|
|
t.Fatalf("joined content = %q", c)
|
|
}
|
|
}
|
|
|
|
// The shape a real Claude Code session replays to (97 clean exchanges in
|
|
// the session this was taken from), as a fixed fixture: parallel calls
|
|
// answered out of order across user messages, an interrupted turn whose
|
|
// late result follows its own text, and a clean turn — all in one
|
|
// request, valid and byte-stable for the clean parts.
|
|
func TestChatSessionShapeStaysValid(t *testing.T) {
|
|
r := &Request{Messages: []Message{
|
|
{Role: "user", Parts: []Part{{Kind: Text, Text: "look into it"}}},
|
|
{Role: "assistant", Parts: []Part{{Kind: Text, Text: "on it"}}},
|
|
{Role: "assistant", Parts: []Part{call("A"), call("B")}},
|
|
{Role: "user", Parts: []Part{result("B", "2")}},
|
|
{Role: "user", Parts: []Part{result("A", "1")}},
|
|
{Role: "assistant", Parts: []Part{{Kind: Text, Text: "one more"}}},
|
|
{Role: "assistant", Parts: []Part{call("C")}},
|
|
{Role: "user", Parts: []Part{{Kind: Text, Text: "interrupted"}}},
|
|
{Role: "user", Parts: []Part{result("C", "late")}},
|
|
{Role: "assistant", Parts: []Part{{Kind: Text, Text: "done"}}},
|
|
{Role: "user", Parts: []Part{{Kind: Text, Text: "thanks"}}},
|
|
}}
|
|
trace, msgs := chatTrace(t, r)
|
|
want := "user assistant assistant(A,B) tool(A) tool(B) assistant assistant(C) tool(C) user assistant user"
|
|
if trace != want {
|
|
t.Fatalf("trace = %q, want %q", trace, want)
|
|
}
|
|
if !kimiValid(msgs) || !idsUsedOnce(msgs) {
|
|
t.Fatal("sequence fails Kimi's tool exchange validation")
|
|
}
|
|
}
|
|
|
|
// A clean exchange goes through untouched.
|
|
func TestChatCleanExchangeUntouched(t *testing.T) {
|
|
r := &Request{Messages: []Message{
|
|
{Role: "user", Parts: []Part{{Kind: Text, Text: "hi"}}},
|
|
{Role: "assistant", Parts: []Part{{Kind: Text, Text: "hello"}}},
|
|
{Role: "assistant", Parts: []Part{call("A"), call("B")}},
|
|
{Role: "user", Parts: []Part{result("A", "1"), result("B", "2")}},
|
|
{Role: "user", Parts: []Part{{Kind: Text, Text: "thanks"}}},
|
|
}}
|
|
trace, msgs := chatTrace(t, r)
|
|
want := "user assistant assistant(A,B) tool(A) tool(B) user"
|
|
if trace != want {
|
|
t.Fatalf("trace = %q, want %q", trace, want)
|
|
}
|
|
if !kimiValid(msgs) {
|
|
t.Fatal("sequence fails Kimi's tool exchange validation")
|
|
}
|
|
}
|