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.
293 lines
11 KiB
Go
293 lines
11 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// memBucket is a bucket in memory, or one that refuses every write.
|
|
type memBucket struct {
|
|
mu sync.Mutex
|
|
objs map[string][]byte
|
|
err error
|
|
}
|
|
|
|
func (b *memBucket) Put(_ context.Context, name string, data []byte) error {
|
|
b.mu.Lock()
|
|
defer b.mu.Unlock()
|
|
if b.err != nil {
|
|
return b.err
|
|
}
|
|
b.objs[name] = data
|
|
return nil
|
|
}
|
|
|
|
func archiveTo(t *testing.T, b *memBucket) {
|
|
t.Helper()
|
|
was := ArchiveBucket
|
|
ArchiveBucket = func() (Putter, bool) { return b, true }
|
|
t.Cleanup(func() { ArchiveBucket = was })
|
|
}
|
|
|
|
// archiveVendor answers a chat request, with a cookie of its own, and
|
|
// keeps nothing of what it was sent.
|
|
type archiveVendor struct{}
|
|
|
|
func (archiveVendor) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
io.ReadAll(r.Body)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Header().Set("Set-Cookie", "vendor_session=abc123; HttpOnly")
|
|
io.WriteString(w, `{"id":"c1","object":"chat.completion","model":"m1","choices":[{"index":0,"message":{"role":"assistant","content":"done"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1}}`)
|
|
}
|
|
|
|
const archiveKey = "sk-proj-abcdefghijklmnopqrstuvwxyz0123456789"
|
|
|
|
func archivePost(t *testing.T, s *Server) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
body := `{"model":"fake/m1","messages":[{"role":"user","content":"deploy with ` + archiveKey + `"}],"api_key":"named-secret"}`
|
|
req := httptest.NewRequest("POST", "/v1/chat/completions?key=query-secret&beta=true", strings.NewReader(body))
|
|
req.Header.Set("Authorization", "Bearer magpie-gateway-key")
|
|
req.Header.Set("x-api-key", "another-secret")
|
|
req.Header.Set("Cookie", "sid=cookie-secret")
|
|
req.Header.Set("User-Agent", "claude-cli/2.1.0")
|
|
rec := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(rec, req)
|
|
if rec.Code != 200 {
|
|
t.Fatalf("%d %s", rec.Code, rec.Body)
|
|
}
|
|
archivePending.Wait()
|
|
return rec
|
|
}
|
|
|
|
// Off, as it is unless turned on, nothing is archived: no upload, no id on
|
|
// the call or its response — even with a bucket there to take it.
|
|
func TestArchiveOff(t *testing.T) {
|
|
fresh(t)
|
|
serveOn(t, "fake", "k", []string{"m1"}, archiveVendor{})
|
|
b := &memBucket{objs: map[string][]byte{}}
|
|
archiveTo(t, b)
|
|
s := New()
|
|
rec := archivePost(t, s)
|
|
if len(b.objs) != 0 || rec.Header().Get(ArchiveHeader) != "" || s.Recent()[0].Archive != "" {
|
|
t.Fatalf("archived while off: %v %q %+v", keys(b.objs), rec.Header().Get(ArchiveHeader), s.Recent()[0].Archive)
|
|
}
|
|
// on, with no bucket (sync isn't to S3): nothing either
|
|
settings.Save(settings.Settings{RequestArchive: true})
|
|
ArchiveBucket = func() (Putter, bool) { return nil, false }
|
|
rec = archivePost(t, s)
|
|
if len(b.objs) != 0 || rec.Header().Get(ArchiveHeader) != "" || s.Recent()[0].Archive != "" {
|
|
t.Fatalf("archived with no bucket: %v", keys(b.objs))
|
|
}
|
|
}
|
|
|
|
// On, a call goes to archive/<date>/<id>.json, the date its own in UTC and
|
|
// the id on the response and the call; the headers and bodies both ways
|
|
// are there with every secret taken out.
|
|
func TestArchiveOn(t *testing.T) {
|
|
fresh(t)
|
|
serveOn(t, "fake", "k", []string{"m1"}, archiveVendor{})
|
|
settings.Save(settings.Settings{RequestArchive: true})
|
|
b := &memBucket{objs: map[string][]byte{}}
|
|
archiveTo(t, b)
|
|
s := New()
|
|
rec := archivePost(t, s)
|
|
id := rec.Header().Get(ArchiveHeader)
|
|
if !regexp.MustCompile(`^\d{6}-[0-9a-f]{16}$`).MatchString(id) {
|
|
t.Fatalf("id %q", id)
|
|
}
|
|
// the date is the one the call carries, in UTC, not the clock read
|
|
// again here, which a UTC midnight can fall between
|
|
c := s.Recent()[0]
|
|
date := c.Time.UTC().Format("2006-01-02")
|
|
name := date + "/" + id
|
|
if c.Archive != name || c.wire != nil {
|
|
t.Fatalf("call archive %q, want %q", c.Archive, name)
|
|
}
|
|
if p, ok := ArchiveName(date, id); !ok || p != "archive/"+name+".json" {
|
|
t.Fatalf("ArchiveName %q %v", p, ok)
|
|
}
|
|
data, ok := b.objs["archive/"+name+".json"]
|
|
if !ok || len(b.objs) != 1 {
|
|
t.Fatalf("uploaded %v", keys(b.objs))
|
|
}
|
|
for _, secret := range []string{archiveKey, "named-secret", "magpie-gateway-key", "another-secret", "cookie-secret", "abc123", "query-secret"} {
|
|
if strings.Contains(string(data), secret) {
|
|
t.Errorf("%s in the archive:\n%s", secret, data)
|
|
}
|
|
}
|
|
var a Archived
|
|
if err := json.Unmarshal(data, &a); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if a.ID != name || a.Call.Model != "fake/m1" || a.Call.Status != 200 || a.Call.RequestBody != "" || a.Call.Archive != name {
|
|
t.Errorf("call %+v", a.Call)
|
|
}
|
|
if a.Request.Method != "POST" || a.Request.Path != "/v1/chat/completions?beta=true&key=%5BREDACTED%5D" ||
|
|
a.Request.Headers["Authorization"][0] != "[REDACTED]" || a.Request.Headers["User-Agent"][0] != "claude-cli/2.1.0" ||
|
|
!strings.Contains(a.Request.Body, "deploy with [REDACTED:API_KEY]") || !strings.Contains(a.Request.Body, `"api_key":"[REDACTED]"`) {
|
|
t.Errorf("request %+v", a.Request)
|
|
}
|
|
if a.Response.Status != 200 || a.Response.Headers[ArchiveHeader][0] != id || !strings.Contains(a.Response.Body, `"content":"done"`) {
|
|
t.Errorf("response %+v", a.Response)
|
|
}
|
|
|
|
// a bucket that refuses: the request is served as ever, the failure
|
|
// said, and the next upload that goes clears it
|
|
b.err = errors.New("HTTP 403 AccessDenied")
|
|
archivePost(t, s)
|
|
if e, _ := ArchiveError(); !strings.Contains(e, "AccessDenied") {
|
|
t.Errorf("failure not kept: %q", e)
|
|
}
|
|
b.err = nil
|
|
archivePost(t, s)
|
|
if e, _ := ArchiveError(); e != "" || len(b.objs) != 2 {
|
|
t.Errorf("after a good upload: %q, %d uploads", e, len(b.objs))
|
|
}
|
|
}
|
|
|
|
// The archive's names are only ever a date and an id of magpie's shape,
|
|
// never a path out of the archive folder.
|
|
func TestArchiveName(t *testing.T) {
|
|
for _, c := range [][2]string{{"2026-10-01", "../backup"}, {"../x", "a"}, {"2026-13-01", "a"}, {"2026-10-01", ""}, {"2026-10-01", "A/b"}} {
|
|
if n, ok := ArchiveName(c[0], c[1]); ok {
|
|
t.Errorf("%v named %q", c, n)
|
|
}
|
|
}
|
|
}
|
|
|
|
// longVendor answers with a reply far longer than the 256 KB Recent calls
|
|
// keeps, ending in RES-TAIL: one JSON body, or, asked to stream, a stream
|
|
// of many small events.
|
|
type longVendor struct{}
|
|
|
|
func (longVendor) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
b, _ := io.ReadAll(r.Body)
|
|
// Words that don't repeat: the same few words over and over with no
|
|
// break is a loop the loop guard ends (#1489).
|
|
var sb strings.Builder
|
|
for i := 0; sb.Len() < 450_000; i++ {
|
|
fmt.Fprintf(&sb, "word%d ", i)
|
|
}
|
|
words := sb.String() // 450 KB
|
|
if !strings.Contains(string(b), `"stream":true`) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, `{"id":"c1","object":"chat.completion","model":"m1","choices":[{"index":0,"message":{"role":"assistant","content":"`+words+`RES-TAIL"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1}}`)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
for i := range 1000 {
|
|
io.WriteString(w, `data: {"id":"c1","object":"chat.completion.chunk","model":"m1","choices":[{"index":0,"delta":{"content":"`+words[i*450:(i+1)*450]+`"}}]}`+"\n\n")
|
|
}
|
|
io.WriteString(w, `data: {"id":"c1","object":"chat.completion.chunk","model":"m1","choices":[{"index":0,"delta":{"content":"RES-TAIL"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1}}`+"\n\ndata: [DONE]\n\n")
|
|
}
|
|
|
|
// The archive keeps each body whole, not the first 256 KB Recent calls
|
|
// shows, a stream's too (#447): its files are gone once it is uploaded,
|
|
// and the call's row in the usage log names where it is kept.
|
|
func TestArchiveWholeBodies(t *testing.T) {
|
|
for _, stream := range []bool{false, true} {
|
|
t.Run(fmt.Sprint("stream=", stream), func(t *testing.T) {
|
|
fresh(t)
|
|
tmp := t.TempDir()
|
|
t.Setenv("TMPDIR", tmp)
|
|
serveOn(t, "fake", "k", []string{"m1"}, longVendor{})
|
|
settings.Save(settings.Settings{RequestArchive: true})
|
|
b := &memBucket{objs: map[string][]byte{}}
|
|
archiveTo(t, b)
|
|
s := New()
|
|
body := `{"model":"fake/m1","stream":` + fmt.Sprint(stream) + `,"messages":[{"role":"user","content":"` + strings.Repeat("sit amet ", 40000) + `REQ-TAIL"}]}`
|
|
rec := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(body)))
|
|
archivePending.Wait()
|
|
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "RES-TAIL") {
|
|
t.Fatalf("%d %.300s", rec.Code, rec.Body)
|
|
}
|
|
c := s.Recent()[0]
|
|
if !c.RequestTruncated || !c.ResponseTruncated || len(c.RequestBody) != callBodyLimit {
|
|
t.Errorf("Recent calls keeps %d, cut %v %v", len(c.RequestBody), c.RequestTruncated, c.ResponseTruncated)
|
|
}
|
|
data := b.objs["archive/"+c.Archive+".json"]
|
|
var a Archived
|
|
if err := json.Unmarshal(data, &a); err != nil {
|
|
t.Fatalf("%v: %v", keys(b.objs), err)
|
|
}
|
|
if a.Request.Body != body || a.Request.Size != int64(len(body)) || a.Request.Truncated {
|
|
t.Errorf("request kept %d of %d, size %d, cut %v", len(a.Request.Body), len(body), a.Request.Size, a.Request.Truncated)
|
|
}
|
|
// the reply as the vendor sent it, under its own model name;
|
|
// the agent got the name it asked for
|
|
sent := namedAs(rec.Body.String(), "m1")
|
|
if a.Response.Body != sent || a.Response.Size != int64(len(sent)) || a.Response.Truncated {
|
|
t.Errorf("response kept %d of %d, size %d, cut %v", len(a.Response.Body), len(sent), a.Response.Size, a.Response.Truncated)
|
|
}
|
|
if u := lastUsage(t); u.Archive == "" || u.Archive != c.Archive {
|
|
t.Errorf("usage row names archive %q, the call %q", u.Archive, c.Archive)
|
|
}
|
|
// the Routing page's request names it too, for its bodies to be
|
|
// read back once the recent calls have let them go
|
|
if rts := s.Trace(context.Background(), 0, 0).Routes; len(rts) != 1 || rts[0].Archive != c.Archive || c.Route != rts[0].ID {
|
|
t.Errorf("route names archive %q (call %q, route %d of %d)", rts[0].Archive, c.Archive, c.Route, rts[0].ID)
|
|
}
|
|
if left, _ := os.ReadDir(tmp); len(left) != 0 {
|
|
t.Errorf("files left behind: %v", left)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Past RequestArchiveMaxMB a body is cut, said so, and its whole size kept.
|
|
func TestArchiveLimit(t *testing.T) {
|
|
fresh(t)
|
|
t.Setenv("TMPDIR", t.TempDir())
|
|
serveOn(t, "fake", "k", []string{"m1"}, archiveVendor{})
|
|
settings.Save(settings.Settings{RequestArchive: true, RequestArchiveMaxMB: 1})
|
|
b := &memBucket{objs: map[string][]byte{}}
|
|
archiveTo(t, b)
|
|
s := New()
|
|
body := `{"model":"fake/m1","messages":[{"role":"user","content":"` + strings.Repeat("sit amet ", 150000) + `"}]}`
|
|
rec := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(body)))
|
|
archivePending.Wait()
|
|
var a Archived
|
|
if err := json.Unmarshal(b.objs["archive/"+s.Recent()[0].Archive+".json"], &a); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !a.Request.Truncated || a.Request.Size != int64(len(body)) || a.Request.Body != body[:1<<20] {
|
|
t.Errorf("kept %d of %d, size %d, cut %v", len(a.Request.Body), len(body), a.Request.Size, a.Request.Truncated)
|
|
}
|
|
if a.Response.Truncated || !strings.Contains(a.Response.Body, `"content":"done"`) {
|
|
t.Errorf("response %+v", a.Response)
|
|
}
|
|
}
|
|
|
|
// A call with the archive off names no archive in the usage log.
|
|
func TestArchiveOffUsage(t *testing.T) {
|
|
fresh(t)
|
|
serveOn(t, "fake", "k", []string{"m1"}, archiveVendor{})
|
|
archiveTo(t, &memBucket{objs: map[string][]byte{}})
|
|
archivePost(t, New())
|
|
if u := lastUsage(t); u.Archive != "" {
|
|
t.Errorf("archive %q while off", u.Archive)
|
|
}
|
|
}
|
|
|
|
func keys(m map[string][]byte) []string {
|
|
var out []string
|
|
for k := range m {
|
|
out = append(out, k)
|
|
}
|
|
return out
|
|
}
|