Files
magpie/internal/gateway/refusal_thinking_test.go
T
yetone 9bb1efdde3 gateway: a reply names the model the agent asked for when it went out under another name (issennknight)
issennknight on Discord (#general): an agent asked for deepseek-v4.1-flash,
which goes out as deepseek-v4.1-flash-1017 (settings' ModelIDs). The vendor's
reply said -1017, magpie passed it back, and the agent warned that the model
didn't match.

Before, only MemberModel (#822) or X-Magpie-Response-Model: member rewrote a
reply's model, and only to the group member's id. Now the default is
nameAsked. When magpie sent the request under another name than the agent
asked for (a model's own id, a routing group, a provider/model id), the
reply names the asked model. That covers Chat and Anthropic `model`,
Responses `response.model` (created, in_progress, completed), Anthropic
`message.model` (message_start) and Gemini `modelVersion`, streamed and
whole, translated paths and plugins included: it all goes through serve's
one memberWriter. A vendor that sent back the wire name as asked keeps its
own name, a dated -20261017 too. Usage rows, recent calls and the Routing
page still record served = the vendor's name: the writer sits outside
captureResponseWriter. Claude Code, Claude Desktop and Codex get the asked
name (or the vendor's), never the member's id, as before.

Streams are rewritten one complete line at a time (memberLines). A run
with no "model" in it is written through as the same slice. Only a JSON
reply that has a target at decide time is held whole, which is what
MemberModel already did.

A JSON body that doesn't parse (a reply cut short) goes on as it came.
A request to a remote magpie now sends X-Magpie-Response-Model: vendor
(passOnCaller). That magpie names the vendor's model, as before, so this
one still records what served the call (TestRemoteMagpieGroupNotSwapped
went red without it).

Existing tests that held a native reply byte for byte now expect only
its model renamed to the asked id (namedAs in member_test.go):
passthrough_severed, stream_accounting, refusal, refusal_thinking,
thinking_signature and vertex. The archive keeps the reply as the
vendor sent it, under the vendor's name (archive_test).

Also dropped: the openai-model header code. serve never relays that header,
so the test for it could not pass.

Run (mote, at this head rebased on origin/main 15066f84f):
- go test -tags nogui -count=1 -timeout 45m ./...: every package ok
  except internal/gui: TestSettingsSaveKeepsWhatItDoesNotSend (#4514,
  SearchAll from b09349d17) and TestColdTurnsTaken (#4510). Both are filed,
  and neither touches this change. Three other sessions' suites ran on
  mote at the same time.
  The first full run (base 80bcef18f) had internal/gateway red in 10
  tests: the byte-for-byte tests above, the remote-magpie served test and
  the truncated JSON body. Each one was fixed as described.
- Rebased on 544b91ca8 (no gateway files changed in between): go build ./...,
  go vet, then go test ./internal/gateway ./internal/settings
  ./internal/provider: all ok.
- go vet -tags nogui ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...: clean (cgo warnings only); gofmt -l clean.
- go test -race -count=3 on the member and served tests: ok (122s).
- the 10 formerly red tests with TestReplyNames, TestGeminiReplyNames,
  TestMemberLine, TestMemberWriter and Masked, after the rebase: ok.

Red on the old code (the new tests over a git archive of the base):
- TestReplyNamesAskedModel: 36 subtests fail (own id, group and provider
  id, x 6 protocol paths, x streamed or not), e.g. `the reply names
  "deepseek-v4.1-flash-1017", want "deepseek-v4.1-flash"`.
- TestGeminiReplyNamesAskedModel: both subtests fail (modelVersion -1017).

Broken by hand: dropping `|| m.wire == m.asked` from target() fails
"as asked, dated" in 12 subtests.

Real use: Codex 0.165.0 (codex exec --json) under a sandbox HOME and
CODEX_HOME on mote. A fake Responses vendor always answered
deepseek-v4.1-flash-1017, and a tee between Codex and `magpie serve` logged
what Codex got. providers.json had d with deepseek-v4.1-flash and group
fast; settings.json had modelIDs d/deepseek-v4.1-flash ->
deepseek-v4.1-flash-1017.
- old build: the vendor was asked for -1017; Codex got "model":
  "deepseek-v4.1-flash-1017" in response.created, in_progress and completed,
  both for -m deepseek-v4.1-flash and for -m group/fast.
- new build: the vendor was still asked for -1017; Codex got
  "deepseek-v4.1-flash" and "group/fast"; the usage rows still say served
  deepseek-v4.1-flash-1017. Codex answered "pong" both times. Codex 0.165
  itself shows no warning for a body model either way (its "model
  rerouted" reads the openai-model header), so the agent behind the
  report's warning was not the one run.

Not tried: the reporter's own agent (not named) and Claude Code; the
Responses WebSocket path (responses_ws.go's own frames); a compressed
reply, which still goes through as it is; Antigravity's effort-variant
wire, where the wire differs from the asked name on purpose.
2026-11-01 04:51:17 +08:00

225 lines
14 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 (
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// Claude refusing after it has only thought, as Opus did Claude Code
// through a group (#248): thinking streamed, then stop_reason "refusal"
// with no word of the reply said. 381–707 tokens out, 200 on the routing
// page, and Claude Code's "…'s safeguards stopped the response above".
var anthropicThoughtThenRefused = sse(`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"m1","role":"assistant","model":"m","content":[],"usage":{"input_tokens":2,"cache_read_input_tokens":32749}}}`,
`event: content_block_start`+"\n"+`data: {"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":"","signature":""}}`,
`event: content_block_delta`+"\n"+`data: {"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"The user wants a plan for"}}`,
`event: ping`+"\n"+`data: {"type":"ping"}`,
`event: content_block_delta`+"\n"+`data: {"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":" the next step."}}`,
`event: content_block_delta`+"\n"+`data: {"type":"content_block_delta","index":0,"delta":{"type":"signature_delta","signature":"sig"}}`,
`event: content_block_stop`+"\n"+`data: {"type":"content_block_stop","index":0}`,
`event: message_delta`+"\n"+`data: {"type":"message_delta","delta":{"stop_reason":"refusal","stop_sequence":null},"usage":{"output_tokens":381}}`,
`event: message_stop`+"\n"+`data: {"type":"message_stop"}`)
var anthropicThoughtThenAnswered = sse(`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"m1","role":"assistant","model":"m","content":[]}}`,
`event: content_block_start`+"\n"+`data: {"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":"","signature":""}}`,
`event: content_block_delta`+"\n"+`data: {"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"hm"}}`,
`event: content_block_stop`+"\n"+`data: {"type":"content_block_stop","index":0}`,
`event: content_block_start`+"\n"+`data: {"type":"content_block_start","index":1,"content_block":{"type":"text","text":""}}`,
`event: content_block_delta`+"\n"+`data: {"type":"content_block_delta","index":1,"delta":{"type":"text_delta","text":"from a"}}`,
`event: content_block_stop`+"\n"+`data: {"type":"content_block_stop","index":1}`,
`event: message_delta`+"\n"+`data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":4}}`,
`event: message_stop`+"\n"+`data: {"type":"message_stop"}`)
const claudeCodeAsk = `{"model":"group/g","max_tokens":10,"stream":true,"thinking":{"type":"adaptive"},"messages":[{"role":"user","content":"hi"}]}`
// A refusal after nothing but reasoning is one with nothing said: the
// reasoning was shown to nobody, and the next member answers (#248) — for
// Claude Code on Anthropic's API, Codex on Responses, and each protocol's
// own reasoning.
func TestRefusalAfterThinkingFailsOver(t *testing.T) {
chatThought := sse(`data: {"id":"c1","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":"thinking it over"}}]}`,
`data: {"id":"c1","choices":[{"index":0,"delta":{},"finish_reason":"content_filter"}]}`,
`data: [DONE]`)
responsesThought := sse(`event: response.created`+"\n"+`data: {"type":"response.created","response":{"id":"resp_1","status":"in_progress","output":[]}}`,
`event: response.output_item.added`+"\n"+`data: {"type":"response.output_item.added","output_index":0,"item":{"id":"rs_1","type":"reasoning","summary":[]}}`,
`event: response.reasoning_summary_part.added`+"\n"+`data: {"type":"response.reasoning_summary_part.added","item_id":"rs_1","output_index":0,"summary_index":0,"part":{"type":"summary_text","text":""}}`,
`event: response.reasoning_summary_text.delta`+"\n"+`data: {"type":"response.reasoning_summary_text.delta","item_id":"rs_1","output_index":0,"summary_index":0,"delta":"**Planning the step**"}`,
`event: response.reasoning_summary_text.done`+"\n"+`data: {"type":"response.reasoning_summary_text.done","item_id":"rs_1","output_index":0,"summary_index":0,"text":"**Planning the step**"}`,
`event: response.output_item.done`+"\n"+`data: {"type":"response.output_item.done","output_index":0,"item":{"id":"rs_1","type":"reasoning","summary":[{"type":"summary_text","text":"**Planning the step**"}]}}`,
`event: response.failed`+"\n"+`data: {"type":"response.failed","response":{"id":"resp_1","status":"failed","error":{"code":"bio_policy","message":"This content was flagged for possible biological risk."}}}`)
for _, x := range []struct {
name string
setup func(t *testing.T, s *scripted)
reply string
path string
ask string
model string // a model whose vendor refuses after thinking
}{
{"claude to claude code", func(t *testing.T, s *scripted) { scriptedOn(t, "a", provider.Anthropic, s) }, anthropicThoughtThenRefused, "/v1/messages", claudeCodeAsk, "claude-opus-5-5"},
{"claude to codex", func(t *testing.T, s *scripted) { scriptedOn(t, "a", provider.Anthropic, s) }, anthropicThoughtThenRefused, "/v1/responses", codexAsk, "claude-opus-5-5"},
{"chat to claude code", func(t *testing.T, s *scripted) { scriptedOn(t, "a", provider.Chat, s) }, chatThought, "/v1/messages", claudeCodeAsk, "gpt-5.2"},
{"responses to codex", func(t *testing.T, s *scripted) { responsesOn(t, "a", s) }, responsesThought, "/v1/responses", codexAsk, "gpt-5.2"},
{"responses to claude code", func(t *testing.T, s *scripted) { responsesOn(t, "a", s) }, responsesThought, "/v1/messages", claudeCodeAsk, "gpt-5.2"},
} {
t.Run(x.name, func(t *testing.T) {
fresh(t)
a := &scripted{replies: []reply{{200, "text/event-stream", x.reply}}}
b := &scripted{replies: []reply{{200, "text/event-stream", anthropicAnswer}}}
x.setup(t, a)
pa, err := provider.Find("a")
if err != nil {
t.Fatal(err)
}
pa.Models = []string{x.model}
if err := provider.Save(*pa); err != nil {
t.Fatal(err)
}
scriptedOn(t, "b", provider.Anthropic, b)
refusalGroup(t, "a/"+x.model, "b/m")
s := New()
code, body := sendTo(s, x.path, x.ask)
if code != 200 || !strings.Contains(body, "from b") || strings.Contains(body, `"refusal"`) || strings.Contains(body, "Planning") ||
strings.Contains(body, "thinking it over") || strings.Contains(body, "user wants") || a.n != 1 || b.n != 1 {
t.Fatalf("%d %s (a %d, b %d)", code, body, a.n, b.n)
}
r := lastRoute(s)
if len(r.Tries) != 2 || r.Tries[0].Fail != failRefused || r.Tries[0].Rest != nil || r.Status != 200 {
t.Fatalf("tries: %+v", r.Tries)
}
})
}
// thinking that goes on to an answer comes through whole, as it came
fresh(t)
a := &scripted{replies: []reply{{200, "text/event-stream", anthropicThoughtThenAnswered}}}
b := &scripted{replies: []reply{{200, "text/event-stream", anthropicAnswer}}}
scriptedOn(t, "a", provider.Anthropic, a)
scriptedOn(t, "b", provider.Anthropic, b)
refusalGroup(t, "a/m", "b/m")
if code, body := sendTo(New(), "/v1/messages", claudeCodeAsk); code != 200 || body != namedAs(anthropicThoughtThenAnswered, "group/g") || b.n != 0 {
t.Fatalf("answered: %d %s (b %d)", code, body, b.n)
}
}
// Reasoning keeps the stream held past the 15s a quiet one is: Claude
// thought for 16–25s before refusing (#248). Past holdThinking it goes
// through, as it did.
func TestThinkingHeldLonger(t *testing.T) {
events := strings.SplitAfter(anthropicThoughtThenRefused, "\n\n")
start := func(ago time.Duration) *holdWriter {
h := newHoldWriter(httptest.NewRecorder(), true)
h.Header().Set("Content-Type", "text/event-stream")
h.WriteHeader(http.StatusOK)
h.since = time.Now().Add(-ago)
return h
}
// the reply begins at once; its thinking goes on past holdLongest
h := start(0)
h.Write([]byte(events[0] + events[1]))
h.since = time.Now().Add(-holdLongest - 10*time.Second)
for _, ev := range events[2:] {
h.Write([]byte(ev))
}
h.settle()
if h.passing || !h.refused || !h.failed() {
t.Fatalf("thought 25s then refused: passing %v refused %v", h.passing, h.refused)
}
h = start(holdThinking + time.Second)
h.Write([]byte(events[0] + events[1] + events[2]))
if !h.passing {
t.Fatal("reasoning held past holdThinking")
}
}
// DeepSeek's filter refusing after it has reasoned, on WorkBuddy through
// a group (itspig, plugins #540): deepseek-v4.1-flash reasoned for ~5s,
// then ended finish_reason content_filter with 0 tokens counted, all under
// a 200. The plugin takes the filter's "Sorry, I can't respond to this
// question." out, so this is what reaches magpie: reasoning, then
// content_filter with nothing said. Its reasoning went through as it came,
// so the next member was never asked and DeepSeek Harness failed "The
// model refused to complete the request"; held, the next member answers.
// A DeepSeek reply that goes on to answer comes through whole.
func TestDeepSeekRefusalAfterThinkingFailsOver(t *testing.T) {
refused := sse(`data: {"id":"c1","object":"chat.completion.chunk","model":"deepseek-v4.1-flash","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":""},"finish_reason":null}]}`,
`data: {"id":"c1","object":"chat.completion.chunk","model":"deepseek-v4.1-flash","choices":[{"index":0,"delta":{"reasoning_content":"The user is asking"},"finish_reason":null}]}`,
`data: {"id":"c1","object":"chat.completion.chunk","model":"deepseek-v4.1-flash","choices":[{"index":0,"delta":{"reasoning_content":" about it."},"finish_reason":null}]}`,
`data: {"id":"c1","object":"chat.completion.chunk","model":"deepseek-v4.1-flash","choices":[{"index":0,"delta":{},"finish_reason":"content_filter"}],"usage":{"prompt_tokens":0,"completion_tokens":0,"total_tokens":0}}`,
`data: [DONE]`)
answered := sse(`data: {"id":"c1","object":"chat.completion.chunk","model":"deepseek-v4.1-flash","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":"The user is asking"},"finish_reason":null}]}`,
`data: {"id":"c1","object":"chat.completion.chunk","model":"deepseek-v4.1-flash","choices":[{"index":0,"delta":{"content":"from a"},"finish_reason":null}]}`,
`data: {"id":"c1","object":"chat.completion.chunk","model":"deepseek-v4.1-flash","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":5,"total_tokens":8}}`,
`data: [DONE]`)
run := func(t *testing.T, up string) (*scripted, *scripted, *Server, int, string) {
fresh(t)
a := &scripted{replies: []reply{{200, "text/event-stream", up}}}
b := &scripted{replies: []reply{{200, "text/event-stream", anthropicAnswer}}}
scriptedOn(t, "a", provider.Chat, a)
pa, err := provider.Find("a")
if err != nil {
t.Fatal(err)
}
pa.Models = []string{"deepseek-v4.1-flash"}
if err := provider.Save(*pa); err != nil {
t.Fatal(err)
}
scriptedOn(t, "b", provider.Anthropic, b)
refusalGroup(t, "a/deepseek-v4.1-flash", "b/m")
s := New()
code, body := sendTo(s, "/v1/messages", claudeCodeAsk)
return a, b, s, code, body
}
t.Run("refused", func(t *testing.T) {
a, b, s, code, body := run(t, refused)
if code != 200 || !strings.Contains(body, "from b") || strings.Contains(body, `"refusal"`) || strings.Contains(body, "The user is asking") || a.n != 1 || b.n != 1 {
t.Fatalf("%d %s (a %d, b %d)", code, body, a.n, b.n)
}
r := lastRoute(s)
if len(r.Tries) != 2 || r.Tries[0].Fail != failRefused || r.Tries[0].Rest != nil || r.Status != 200 {
t.Fatalf("tries: %+v", r.Tries)
}
})
// What workbuddy 0.2.5 hands on from itspig's literal WorkBuddy chunks
// (plugins #540): the reasoning chunk with an empty content and refusal,
// and the content_filter chunk with "\nSorry, I can't respond to this
// question.\n" taken out, its null and empty fields kept.
workbuddy := sse(`data: {"choices":[{"delta":{"content":"","extra_fields":null,"function_call":null,"reasoning_content":" node","refusal":""},"finish_reason":null,"index":0,"logprobs":null}],"created":1793401068,"id":"c56c77084382622efa17c799396db5cf","model":"deepseek-v4.1-flash","object":"chat.completion.chunk","usage":null}`,
`data: {"choices":[{"delta":{"extra_fields":null,"function_call":null,"refusal":"","role":"assistant"},"finish_reason":"content_filter","index":0,"logprobs":null}],"created":0,"id":"c56c77084382622efa17c799396db5cf","model":"deepseek-v4.1-flash","object":"chat.completion.chunk","usage":null}`,
`data: [DONE]`)
t.Run("workbuddy's own chunks", func(t *testing.T) {
a, b, s, code, body := run(t, workbuddy)
if code != 200 || !strings.Contains(body, "from b") || strings.Contains(body, `"refusal"`) || strings.Contains(body, " node") || a.n != 1 || b.n != 1 {
t.Fatalf("%d %s (a %d, b %d)", code, body, a.n, b.n)
}
r := lastRoute(s)
if len(r.Tries) != 2 || r.Tries[0].Fail != failRefused || r.Tries[0].Rest != nil || r.Status != 200 {
t.Fatalf("tries: %+v", r.Tries)
}
})
// The same from WorkBuddy's own framing, as workbuddy 0.2.6 hands it
// on: "tool_calls": [] and "finish_reason": "" on every delta, an empty
// reasoning_content beside the line it took out (plugins #540).
wire := sse(`data: {"choices":[{"delta":{"content":"","extra_fields":null,"function_call":null,"reasoning_content":" node","refusal":"","tool_calls":[]},"finish_reason":"","index":0,"logprobs":null}],"created":1793401068,"id":"c56c77084382622efa17c799396db5cf","model":"deepseek-v4.1-flash","object":"chat.completion.chunk","usage":null}`,
`data: {"choices":[{"delta":{"extra_fields":null,"function_call":null,"reasoning_content":"","refusal":"","role":"assistant","tool_calls":[]},"finish_reason":"content_filter","index":0,"logprobs":null}],"created":0,"id":"c56c77084382622efa17c799396db5cf","model":"deepseek-v4.1-flash","object":"chat.completion.chunk","usage":null}`,
`data: [DONE]`)
t.Run("workbuddy's wire", func(t *testing.T) {
a, b, s, code, body := run(t, wire)
if code != 200 || !strings.Contains(body, "from b") || strings.Contains(body, " node") || a.n != 1 || b.n != 1 {
t.Fatalf("%d %s (a %d, b %d)", code, body, a.n, b.n)
}
if r := lastRoute(s); len(r.Tries) != 2 || r.Tries[0].Fail != failRefused {
t.Fatalf("tries: %+v", r.Tries)
}
})
t.Run("answered", func(t *testing.T) {
a, b, _, code, body := run(t, answered)
if code != 200 || !strings.Contains(body, "The user is asking") || !strings.Contains(body, "from a") || strings.Contains(body, "from b") || a.n != 1 || b.n != 0 {
t.Fatalf("%d %s (a %d, b %d)", code, body, a.n, b.n)
}
})
}