From d7a8ddc84d0c6a3c5b818094696d7dc8d8cdaf1e Mon Sep 17 00:00:00 2001 From: july_lu <645096158@qq.com> Date: Mon, 5 Oct 2026 19:59:28 +0800 Subject: [PATCH] =?UTF-8?q?gateway:=20a=20key=20held=20to=20some=20models?= =?UTF-8?q?=20counts=20no=20tokens=20and=20decides=20on=20none=20it=20may?= =?UTF-8?q?=20not=20use=20(#882,=20LCorleone)=20=E2=80=94=20counting=20tok?= =?UTF-8?q?ens=20asked=20a=20provider's=20help=20or=20estimated=20locally?= =?UTF-8?q?=20whatever=20the=20key=20was=20held=20to,=20and=20System=20One?= =?UTF-8?q?=20asked=20its=20decision=20model=20of=20any=20key.=20Both=20no?= =?UTF-8?q?w=20meet=20the=20hold=20the=20model=20endpoints=20do:=20count?= =?UTF-8?q?=5Ftokens=20(Anthropic's=20shape)=20and=20Gemini's=20:countToke?= =?UTF-8?q?ns=20=E2=80=94=20which=20the=20gateway=20answers=20itself,=20so?= =?UTF-8?q?=20no=20shared=20path=20covered=20it=20=E2=80=94=20resolve=20th?= =?UTF-8?q?e=20model=20named=20and=20are=20refused=20with=20a=20403=20in?= =?UTF-8?q?=20the=20API's=20own=20error=20shape=20when=20the=20key=20may?= =?UTF-8?q?=20not=20use=20it,=20a=20group=20judged=20by=20its=20members,?= =?UTF-8?q?=20a=20bare=20group's=20name=20and=20an=20auto=20group's=20stan?= =?UTF-8?q?d-in=20resolved=20first=20(countHeld=20in=20key=5Fmodels.go);?= =?UTF-8?q?=20System=20One=20is=20held=20to=20the=20decision=20model=20its?= =?UTF-8?q?=20prefix=20names,=20after=20RouteDecider.=20TestGatewayKeyMode?= =?UTF-8?q?lsHoldCountTokensAndSystemOne=20(403s=20by=20id,=20bare=20name?= =?UTF-8?q?=20and=20through=20a=20group=20on=20both=20counting=20APIs,=20A?= =?UTF-8?q?nthropic's=20and=20Gemini's=20error=20shapes,=20a=20held=20coun?= =?UTF-8?q?t=20or=20decision=20reaching=20no=20provider,=20an=20allowed=20?= =?UTF-8?q?model=20counted,=20a=20free=20key=20going=20on,=20the=20decider?= =?UTF-8?q?=20among=20the=20key's=20models)=20fails=20without=20the=20chan?= =?UTF-8?q?ge.=20(#902)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/gateway/decide.go | 6 ++ internal/gateway/gateway.go | 10 +++ internal/gateway/key_models.go | 29 +++++++++ internal/gateway/key_models_test.go | 99 +++++++++++++++++++++++++++++ 4 files changed, 144 insertions(+) diff --git a/internal/gateway/decide.go b/internal/gateway/decide.go index 7dac0adeb..b3e4015f6 100644 --- a/internal/gateway/decide.go +++ b/internal/gateway/decide.go @@ -422,6 +422,12 @@ func (s *Server) serveSystemOne(w http.ResponseWriter, r *http.Request) { if asked == "" { asked = p.ID + "/" + model } + // a gateway key held to some models (#882) is refused another; a + // decision model is a model, and System One asks one directly + if keyWho, held := keyHolds(r); held && !modelAllowed(keyWho, p, model) { + writeError(w, provider.Chat, http.StatusForbidden, keyModelError(keyWho, asked)) + return + } seat := decideSeat(p, model) var used Usage tr := s.trace.begin(Route{Time: start, Agent: agentOf(r), Session: sessionOf(r.Header), Model: asked, Provider: p.ID, diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index f076967e9..44427faf1 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -831,6 +831,11 @@ func (s *Server) countTokens(w http.ResponseWriter, r *http.Request) { id = sid } p, model, ok := provider.Resolve(id) + // a gateway key held to some models (#882) counts no tokens on one it + // may not use, as it would be refused serving it + if countHeld(w, r, provider.Anthropic, id) { + return + } s.countOn(w, r, p, model, ok, body) } @@ -992,6 +997,11 @@ func (s *Server) gemini(w http.ResponseWriter, r *http.Request) { case "streamGenerateContent": stream = true case "countTokens": + // a gateway key held to some models (#882) counts no tokens on one + // it may not use, as it would be refused serving it + if countHeld(w, r, provider.Gemini, model) { + return + } s.geminiCount(w, model, body) return default: diff --git a/internal/gateway/key_models.go b/internal/gateway/key_models.go index 8c04ca4ad..c389057e1 100644 --- a/internal/gateway/key_models.go +++ b/internal/gateway/key_models.go @@ -135,3 +135,32 @@ func keyModelError(who access.Identity, model string) string { return fmt.Sprintf("The gateway key %q may not use %s; it may use %s. Change the key's models in magpie's Gateway page, or use a model it has.", who.KeyName, model, strings.Join(who.Models, ", ")) } + +// countHeld answers the 403 a gateway key held to some models (#882) gets +// for counting tokens on one it may not use, as it would be refused +// serving it: Anthropic's count_tokens and Gemini's :countTokens, id the +// model as each asks it, resolved — a bare group's name, an auto group's +// stand-in — and a group judged by its members. proto is the API the +// error is answered on. +func countHeld(w http.ResponseWriter, r *http.Request, proto provider.Protocol, id string) bool { + keyWho, held := keyHolds(r) + if !held { + return false + } + if gid, ok := provider.GroupFor(id); ok { + id = gid + } + if sid, ok := provider.AutoStandIn(id); ok { + id = sid + } + p, model, ok := provider.Resolve(id) + if !ok { + return false // counted nowhere: a local estimate, as before + } + g, ms, isGroup := provider.FindGroup(id) + if isGroup && !groupAllowed(keyWho, g, ms) || !isGroup && !modelAllowed(keyWho, p, model) { + writeError(w, proto, http.StatusForbidden, keyModelError(keyWho, id)) + return true + } + return false +} diff --git a/internal/gateway/key_models_test.go b/internal/gateway/key_models_test.go index ec746b093..8baecad0a 100644 --- a/internal/gateway/key_models_test.go +++ b/internal/gateway/key_models_test.go @@ -2,6 +2,8 @@ package gateway import ( "encoding/json" + "io" + "net/http" "net/http/httptest" "slices" "strings" @@ -242,3 +244,100 @@ func TestGatewayKeyModelsNameAGroup(t *testing.T) { t.Fatal("a named group fell back outside itself", w.Code, w.Body.String()) } } + +// A gateway key held to some models (#882) counts no tokens on one it may +// not use — Anthropic's count_tokens and Gemini's :countTokens, a group +// judged by its members — and System One asks its decision model only of +// a key that may use it; a free key goes on as before, and no provider is +// asked for any of it. +func TestGatewayKeyModelsHoldCountTokensAndSystemOne(t *testing.T) { + fresh(t) + plan := &fake{t: t, ctype: "application/json", reply: `{"id":"from-plan","choices":[]}`} + spare := &fake{t: t, ctype: "application/json", reply: `{"id":"from-spare","choices":[]}`} + twoProviders(t, plan, spare) + var decided int + jev := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + decided++ + w.Header().Set("Content-Type", "application/json") + io.WriteString(w, `{"answers":{"intent":{"choice":"bug"}},"usage":{"input_tokens":10,"output_tokens":2}}`) + })) + t.Cleanup(jev.Close) + if err := provider.Save(provider.Provider{ID: "jev", Name: "Jev", Key: "k", Decide: jev.URL + "/v1"}); err != nil { + t.Fatal(err) + } + if err := provider.SaveGroup(provider.Group{Name: "Both", Members: []string{"plan/m1", "spare/m2"}, Routing: provider.Ordered}); err != nil { + t.Fatal(err) + } + keys, secrets := newCaller(t, "Held", "Free") + if _, err := access.Update("models-key", access.Change{Key: keys[0].ID, Models: []string{"plan/*"}}); err != nil { + t.Fatal(err) + } + h := New().Handler() + do := func(secret, method, path, body string) *httptest.ResponseRecorder { + r := httptest.NewRequest(method, path, strings.NewReader(body)) + r.Header.Set("Authorization", "Bearer "+secret) + w := httptest.NewRecorder() + h.ServeHTTP(w, r) + return w + } + // counting tokens on a model it may not use, by id, bare and through a + // group with one in: said in Anthropic's own error shape + for _, model := range []string{"spare/m2", "m2", "group/both"} { + w := do(secrets[0], "POST", "/v1/messages/count_tokens", `{"model":"`+model+`","messages":[{"role":"user","content":"hi"}]}`) + var e struct { + Error struct{ Message, Type string } + } + json.Unmarshal(w.Body.Bytes(), &e) + if w.Code != 403 || e.Error.Type != "permission_error" || !strings.Contains(e.Error.Message, `"Held"`) || !strings.Contains(e.Error.Message, "plan/*") { + t.Fatalf("counting %s: %d %s", model, w.Code, w.Body.String()) + } + } + // one it may use is counted, and a free key counts anything + for _, c := range []struct{ secret, model string }{ + {secrets[0], "plan/m1"}, {secrets[1], "spare/m2"}, + } { + if w := do(c.secret, "POST", "/v1/messages/count_tokens", `{"model":"`+c.model+`","messages":[{"role":"user","content":"hi"}]}`); w.Code != 200 || !strings.Contains(w.Body.String(), "input_tokens") { + t.Fatalf("counting %s: %d %s", c.model, w.Code, w.Body.String()) + } + } + // Gemini's counting, the model in its URL, is held the same way + w := do(secrets[0], "POST", "/v1beta/models/spare/m2:countTokens", `{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}`) + var g struct { + Error struct { + Code int + Status string + } + } + if w.Code != 403 || json.Unmarshal(w.Body.Bytes(), &g) != nil || g.Error.Code != 403 || g.Error.Status != "PERMISSION_DENIED" { + t.Fatalf("gemini counting: %d %s", w.Code, w.Body.String()) + } + if w = do(secrets[0], "POST", "/v1beta/models/plan/m1:countTokens", `{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}`); w.Code != 200 || !strings.Contains(w.Body.String(), "totalTokens") { + t.Fatalf("gemini counting allowed: %d %s", w.Code, w.Body.String()) + } + if plan.calls != 0 || spare.calls != 0 || decided != 0 { + t.Fatal("a held count or decision reached a provider", plan.calls, spare.calls, decided) + } + // System One's decision model is a model: the held key is refused it, + // a free key asks, and one given the decider asks too + const ask = `{"model":"jev/jev-latest","state":{"message":"hi"},"questions":{"intent":{"type":"choice"}}}` + w = do(secrets[0], "POST", "/v1/systemone", ask) + var e struct { + Error struct{ Message, Type string } + } + json.Unmarshal(w.Body.Bytes(), &e) + if w.Code != 403 || e.Error.Type != "permission_error" || !strings.Contains(e.Error.Message, `"Held"`) { + t.Fatalf("system one held: %d %s", w.Code, w.Body.String()) + } + if decided != 0 { + t.Fatal("a held decision reached its provider", decided) + } + if w = do(secrets[1], "POST", "/v1/systemone", ask); w.Code != 200 || decided != 1 { + t.Fatalf("a free key's decision: %d %s, decided %d", w.Code, w.Body.String(), decided) + } + if _, err := access.Update("models-key", access.Change{Key: keys[0].ID, Models: []string{"jev/*"}}); err != nil { + t.Fatal(err) + } + if w = do(secrets[0], "POST", "/v1/systemone", ask); w.Code != 200 || decided != 2 { + t.Fatalf("a key given the decider: %d %s, decided %d", w.Code, w.Body.String(), decided) + } +}