From 012be0fcbec3e4f3bc5f60e7007155d055aa9f1c Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 10 Oct 2026 17:34:26 +0800 Subject: [PATCH] usage: a Bedrock global. or us-gov. inference profile answered under its model's own id is no swap, so Claude on Bedrock's preset no longer shows as swapped (#1484, ggbdpq) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bedrock's preset gives its Claude and GPT-6 models as global. inference profiles (global.anthropic.claude-opus-5-5, global.openai.gpt-6-luna), and a GovCloud account's are us-gov. ones. Bedrock's reply names the model by its own id, as TestSwappedModel has it for us.anthropic.… . bareModel stripped a profile's geography with vendorDot, which took two to four letters only, so global. and us-gov. stayed on and Swapped marked the route, its tries and the usage row as served by another model. catalog's bedrockGeos already lists both. vendorDot now takes global. and us-gov. too. Another model is still a swap (global.anthropic.claude-opus-5-5 answered as claude-sonnet-5). Rows and routes kept marked swapped before read as not swapped through SameSpelled, which uses the same bareModel. Run: TestBedrockProfileAnsweredByItsModelIsNoSwap fails on the old served.go (Swapped true for four global./us-gov. pairs; route and usage row marked swapped through the Bedrock preset to a fake runtime) and passes with the change; go test -tags nogui ./internal/usage ./internal/gateway green under a temp HOME; gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui on both packages. Not tried against a real Bedrock account. Co-authored-by: ggbdpq --- internal/gateway/bedrock_served_test.go | 88 +++++++++++++++++++++++++ internal/usage/served.go | 5 +- 2 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 internal/gateway/bedrock_served_test.go diff --git a/internal/gateway/bedrock_served_test.go b/internal/gateway/bedrock_served_test.go new file mode 100644 index 000000000..688c94936 --- /dev/null +++ b/internal/gateway/bedrock_served_test.go @@ -0,0 +1,88 @@ +package gateway + +import ( + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/yetone/magpie/internal/provider" + "github.com/yetone/magpie/internal/usage" +) + +// bareBedrock answers Anthropic's messages as Bedrock does for an inference +// profile: its reply names the model by Anthropic's own id, without the +// profile's geography and maker (us.anthropic.claude-sonnet-4-20250514-v1:0 +// answered as claude-sonnet-4-20250514, TestSwappedModel). +type bareBedrock struct{ served string } + +func (b *bareBedrock) ServeHTTP(w http.ResponseWriter, r *http.Request) { + io.ReadAll(r.Body) + w.Header().Set("Content-Type", "text/event-stream") + io.WriteString(w, sse( + `event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"msg_1","model":"`+b.served+`","usage":{"input_tokens":5}}}`, + `data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`, + `data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"ok"}}`, + `data: {"type":"content_block_stop","index":0}`, + `data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":2}}`, + `data: {"type":"message_stop"}`)) +} + +// Bedrock's preset gives Claude as global. inference profiles, and an +// account in GovCloud has us-gov. ones: a reply naming the model by its +// own id is that model, not another swapped in, as it is for us. and apac. +func TestBedrockProfileAnsweredByItsModelIsNoSwap(t *testing.T) { + for _, c := range []struct { + sent, served string + swapped bool + }{ + {"global.anthropic.claude-opus-5-5", "claude-opus-5-5", false}, + {"us-gov.anthropic.claude-sonnet-4-5-20250929-v1:0", "claude-sonnet-4-5-20250929", false}, + {"global.anthropic.claude-haiku-4-5-20251001-v1:0", "claude-haiku-4-5-20251001", false}, + {"global.openai.gpt-6-luna", "gpt-6-luna", false}, + {"apac.anthropic.claude-opus-5-5", "claude-opus-5-5", false}, + // another model is still a swap + {"global.anthropic.claude-opus-5-5", "claude-sonnet-5", true}, + {"global.openai.gpt-6-sol", "gpt-6-luna", true}, + } { + if got := usage.Swapped(c.sent, c.served); got != c.swapped { + t.Errorf("Swapped(%q, %q) = %v, want %v", c.sent, c.served, got, c.swapped) + } + } + + fresh(t) + srv := httptest.NewServer(&bareBedrock{served: "claude-opus-5-5"}) + t.Cleanup(srv.Close) + p, err := provider.FromPreset("bedrock") + if err != nil { + t.Fatal(err) + } + p.Key = "ABSK-test" + p.Anthropic, p.Chat = srv.URL+"/anthropic", srv.URL+"/openai/v1" + p.Models = []string{"global.anthropic.claude-opus-5-5"} + if err := provider.Save(p); err != nil { + t.Fatal(err) + } + s := New() + rec := httptest.NewRecorder() + s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader( + `{"model":"bedrock/global.anthropic.claude-opus-5-5","max_tokens":20,"stream":true,"messages":[{"role":"user","content":"hi"}]}`))) + if rec.Code != 200 { + t.Fatalf("status %d: %s", rec.Code, rec.Body) + } + s.trace.mu.Lock() + r := *s.trace.routes[len(s.trace.routes)-1] + s.trace.mu.Unlock() + if r.Swapped || len(r.Tries) == 0 || r.Tries[len(r.Tries)-1].Swapped { + b, _ := json.Marshal(r) + t.Errorf("route marked swapped for Bedrock's own name of the model: %s", b) + } + rows, _, _ := usage.Ledger(usage.All, usage.Filter{}) + for _, row := range rows { + if row.Swapped { + t.Errorf("usage row marked swapped: model %q served %q", row.Model, row.Served) + } + } +} diff --git a/internal/usage/served.go b/internal/usage/served.go index bc45fe80f..6471cf0a7 100644 --- a/internal/usage/served.go +++ b/internal/usage/served.go @@ -20,8 +20,9 @@ import ( var versionTail = regexp.MustCompile(`(?:[-_@:](?:\d{4}-\d{2}-\d{2}|\d{2}-\d{2}|\d{6,8}|\d{3,4}|v\d+:\d+|latest|preview|exp))+$`) // vendorDot is Bedrock's region and maker before a model's name -// (us.anthropic.claude-…). -var vendorDot = regexp.MustCompile(`^(?:[a-z]{2,4}\.)?(?:anthropic|amazon|meta|mistral|cohere|ai21|deepseek|qwen|openai|google|moonshotai|minimax|zai)\.`) +// (us.anthropic.claude-…), the geography of an inference profile as +// catalog's bedrockGeos has them: global. and us-gov. among them. +var vendorDot = regexp.MustCompile(`^(?:(?:global|us-gov|[a-z]{2,4})\.)?(?:anthropic|amazon|meta|mistral|cohere|ai21|deepseek|qwen|openai|google|moonshotai|minimax|zai)\.`) // contextTail is what Claude Code writes after a model's name for the size of // its context: claude-opus-5[1m].