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/main15066f84f): - go test -tags nogui -count=1 -timeout 45m ./...: every package ok except internal/gui: TestSettingsSaveKeepsWhatItDoesNotSend (#4514, SearchAll fromb09349d17) 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 (base80bcef18f) 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 on544b91ca8(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.
122 lines
6.3 KiB
Go
122 lines
6.3 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// Once text has been sent, an upstream error leaves the HTTP status at
|
|
// 200. It is still a failed request wherever the gateway records it.
|
|
func TestStreamFailureAccounting(t *testing.T) {
|
|
for _, client := range []struct {
|
|
name, path, body, failed, completed string
|
|
}{
|
|
{"native", "/v1/responses", `{"model":"fake/m1","stream":true,"input":"hi"}`, "event: response.failed", "event: response.completed"},
|
|
{"translated", "/v1/messages", `{"model":"fake/m1","stream":true,"max_tokens":8,"messages":[{"role":"user","content":"hi"}]}`, "event: error", "event: message_stop"},
|
|
} {
|
|
for _, outcome := range []string{"completed", "failed"} {
|
|
t.Run(client.name+"/"+outcome, func(t *testing.T) {
|
|
fresh(t)
|
|
reply := sse(
|
|
`event: response.created`+"\n"+`data: {"type":"response.created","response":{"id":"r1","model":"m1","status":"in_progress"}}`,
|
|
`event: response.output_text.delta`+"\n"+`data: {"type":"response.output_text.delta","delta":"partial text"}`,
|
|
)
|
|
const failure = "synthetic stream failure"
|
|
if outcome == "failed" {
|
|
reply += sse(`event: response.failed` + "\n" + `data: {"type":"response.failed","response":{"id":"r1","status":"failed","error":{"type":"server_error","message":"` + failure + `"}}}`)
|
|
} else {
|
|
reply += sse(`event: response.completed` + "\n" + `data: {"type":"response.completed","response":{"id":"r1","model":"m1","status":"completed","usage":{"input_tokens":3,"output_tokens":1}}}`)
|
|
}
|
|
setup(t, provider.Responses, &fake{reply: reply})
|
|
s := New()
|
|
w := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(w, httptest.NewRequest("POST", client.path, strings.NewReader(client.body)))
|
|
if w.Code != 200 || !strings.Contains(w.Body.String(), "partial text") {
|
|
t.Fatalf("stream: %d %s", w.Code, w.Body)
|
|
}
|
|
failed := outcome == "failed"
|
|
if strings.Contains(w.Body.String(), client.failed) != failed || strings.Contains(w.Body.String(), client.completed) == failed {
|
|
t.Fatalf("wrong stream ending: %s", w.Body)
|
|
}
|
|
recent := s.Recent()
|
|
state := s.Trace(context.Background(), 0, 0)
|
|
records := usage.Load(time.Time{})
|
|
if len(recent) != 1 || len(state.Routes) != 1 || len(records) != 1 {
|
|
t.Fatalf("recent=%d routes=%d usage=%d, want one each", len(recent), len(state.Routes), len(records))
|
|
}
|
|
route, record := state.Routes[0], records[0]
|
|
if !route.Done || route.Status != 200 || record.Status != 200 || record.RouteID != route.ID {
|
|
t.Fatalf("route: %+v; usage: %+v", route, record)
|
|
}
|
|
wantError, wantErrors := "", 0
|
|
if failed {
|
|
wantError, wantErrors = failure, 1
|
|
}
|
|
if recent[0].Error != wantError || route.Error != wantError || record.Error != wantError || record.Failed() != failed {
|
|
t.Errorf("errors: recent=%q route=%q usage=%q failed=%t, want %q", recent[0].Error, route.Error, record.Error, record.Failed(), wantError)
|
|
}
|
|
if state.Totals.Requests != 1 || state.Totals.Errors != wantErrors {
|
|
t.Errorf("totals: %+v, want one request and %d errors", state.Totals, wantErrors)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
// Native stream errors are relayed byte for byte, including an
|
|
// error event without a final newline, and still reach the request log.
|
|
// Only the model a reply names is the one the agent asked for.
|
|
func TestNativeStreamFailureAccounting(t *testing.T) {
|
|
const failure = "synthetic stream failure"
|
|
for _, c := range []struct {
|
|
name, wantError, wantType string
|
|
proto provider.Protocol
|
|
path, body, reply string
|
|
}{
|
|
{"chat", failure, "server_error", provider.Chat, "/v1/chat/completions", `{"model":"fake/m1","stream":true,"messages":[{"role":"user","content":"hi"}]}`,
|
|
sse(`data: {"choices":[{"delta":{"content":"partial text"}}]}`, `data: {"error":{"type":"server_error","message":"`+failure+`"}}`)},
|
|
{"anthropic", failure, "server_error", provider.Anthropic, "/v1/messages", `{"model":"fake/m1","stream":true,"max_tokens":8,"messages":[{"role":"user","content":"hi"}]}`,
|
|
anthropicStart + anthropicText + sse(`event: error`+"\n"+`data: {"type":"error","error":{"type":"server_error","message":"`+failure+`"}}`)},
|
|
{"responses error", failure, "server_error", provider.Responses, "/v1/responses", `{"model":"fake/m1","stream":true,"input":"hi"}`,
|
|
sse(`event: response.output_text.delta`+"\n"+`data: {"type":"response.output_text.delta","delta":"partial text"}`, `event: error`+"\n"+`data: {"type":"error","code":"server_error","message":"`+failure+`"}`)},
|
|
{"responses failed without message", "upstream stream failed", "", provider.Responses, "/v1/responses", `{"model":"fake/m1","stream":true,"input":"hi"}`,
|
|
sse(`event: response.output_text.delta`+"\n"+`data: {"type":"response.output_text.delta","delta":"partial text"}`, `event: response.failed`+"\n"+`data: {"type":"response.failed","response":{"id":"r1","status":"failed"}}`)},
|
|
{"error event without data", "upstream stream failed", "", provider.Anthropic, "/v1/messages", `{"model":"fake/m1","stream":true,"max_tokens":8,"messages":[{"role":"user","content":"hi"}]}`,
|
|
anthropicStart + anthropicText + sse(`event: error`)},
|
|
} {
|
|
for _, ending := range []string{"newline", "unterminated"} {
|
|
t.Run(c.name+"/"+ending, func(t *testing.T) {
|
|
fresh(t)
|
|
reply := c.reply
|
|
if ending == "unterminated" {
|
|
reply = strings.TrimRight(reply, "\n")
|
|
}
|
|
setup(t, c.proto, &fake{reply: reply})
|
|
s := New()
|
|
w := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(w, httptest.NewRequest("POST", c.path, strings.NewReader(c.body)))
|
|
if w.Code != 200 || w.Body.String() != namedAs(reply, "fake/m1") {
|
|
t.Fatalf("native stream was changed: %d %s", w.Code, w.Body)
|
|
}
|
|
state, recent := s.Trace(context.Background(), 0, 0), s.Recent()
|
|
if len(state.Routes) != 1 || len(recent) != 1 {
|
|
t.Fatalf("routes=%d recent=%d, want one each", len(state.Routes), len(recent))
|
|
}
|
|
record := lastRecord(t)
|
|
if recent[0].Error != c.wantError || state.Routes[0].Error != c.wantError || record.Error != c.wantError || record.ErrType != c.wantType || record.Status != 200 {
|
|
t.Errorf("recent error=%q route error=%q; usage: %+v", recent[0].Error, state.Routes[0].Error, record)
|
|
}
|
|
if state.Totals.Errors != 1 {
|
|
t.Errorf("routing errors=%d, want 1", state.Totals.Errors)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|