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)

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 <ggbdpq@gmail.com>
This commit is contained in:
yetone
2026-10-10 17:58:07 +08:00
co-authored by ggbdpq
parent e4b370bd21
commit 012be0fcbe
2 changed files with 91 additions and 2 deletions
+88
View File
@@ -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)
}
}
}
+3 -2
View File
@@ -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].