Files
magpie/internal/gateway/effort_accounts.go
T
yetone 7e0fe83d4b provider: Listed as works for signed-in accounts and plugins (zeewell on X)
zeewell asked to list SuperGrok's grok/grok-4.6@500k as grok-4.6, and
Cursor, Devin and WorkBuddy models (all plugins) under ids of their own.
v0.1.2345's "Listed as" took ids only for key/relay providers
(takesModelIDs was p.Account == nil).

Why the restriction was there: the wire rewrite (Settings.Wires) turns
the request body's model back into the own id, but some code reads the
model by the vendor's id before that and got the listed one:
- plugin model lookup (pluginModel: the plugin's APIs, tiers, prices);
- the Claude subscription run (--model given to Claude Code);
- Code Assist/Antigravity variant choice (codeAssistBody, SentNameOnIn);
- account plan lists (fallback's Account.Lists, effort Levels/Tiers,
  AccountServes);
- moveModelRefs refused a provider with no providers.json entry, which
  is every plugin and account provider until a pick is set.
Each now translates with OwnModelID / the provider's ModelIDs, so the
upstream request still carries the vendor's or plugin's own id. Only a
decide-only provider refuses an id. The GUI box already follows
TakesModelIDs, so no GUI file changed.

Tests (red on the old code, each fix reverted by hand one at a time):
- TestModelIDRefused, TestModelIDAtAnAccount (takesModelIDs,
  AccountServes, SentNameOnIn);
- TestLocalModelIDAtAPlugin (Bun fake plugin: /v1/models, upstream
  path, X-Plugin-Model, body, usage; red on pluginModel and on
  moveModelRefs);
- TestLocalModelIDAtAnAntigravityAccount (codeAssistBody);
- TestLocalModelIDAtAClaudeAccount (claude got --model my-sonnet);
- antigravity pool test (fallback Lists, Levels).

Run on mote: gofmt clean; go vet darwin/linux/windows ok; go test
-tags nogui -count=1 -timeout 45m on internal/provider, gateway, cli,
gui, settings, access, usage, plugin: all ok except internal/gui's
TestSettingsSaveKeepsWhatItDoesNotSend (SearchAll), red on main since
b09349d17 and filed as #4514. After rebasing on 140efc8f7 (nothing new
under provider/gateway) the ModelID/LocalModelID/Antigravity tests
ran again: ok.

Real accounts, sandbox HOME, dev build on 127.0.0.1:3486: magpie model
id set grok/grok-4.6@500k -> grok-4.6, grok/grok-4.5@500k -> g45big,
cursor/composer-2.5 -> my-composer, devin/swe-2 -> my-swe,
workbuddy/deepseek-v4.1-flash -> ds-flash. /v1/models listed the new
ids. Requests for grok/grok-4.6, grok/g45big, cursor/my-composer and
devin/my-swe each answered 200 "pong", served as the vendor's id
(grok-4.5-build for g45big); usage records the listed id as model.
Plugin tokens were not rotated (no diff with live).

Not tried: WorkBuddy's real request (its own sign-in answers 401,
unrelated); the GUI box not clicked; no full ./... suite, no GUI suite
(no GUI file touched).
2026-10-31 18:38:31 +08:00

198 lines
6.5 KiB
Go

package gateway
// An account's plan may not take every reasoning level its model has: a
// Free ChatGPT account turns gpt-6-luna at high and above away with a 400,
// which a Plus one beside it answers (#520). Such an account is asked
// after its provider's others that may take the level — by its own model
// list's levels, or what it answered last — and one that refuses the level
// is not the request's fault: the next is asked, and it doesn't rest. When
// none that is left takes it, the agent is told so, not the level lowered
// behind its back.
import (
"encoding/json"
"fmt"
"regexp"
"slices"
"strings"
"sync"
"time"
)
// effortRemember is how long an account's refusal of a level is kept: a
// plan upgraded is asked again after it.
const effortRemember = time.Hour
var effortRefusals = struct {
sync.Mutex
m map[string]effortRefusal // who|model → what it said
}{m: map[string]effortRefusal{}}
type effortRefusal struct {
refused []string // levels it turned away
takes []string // the levels it said it takes, when it said
at time.Time
}
func effortKey(c candidate) string { return c.who() + "|" + c.model }
// noteEffortRefused remembers c turning e away, with the levels it said
// it takes.
func noteEffortRefused(c candidate, e string, takes []string) {
effortRefusals.Lock()
defer effortRefusals.Unlock()
k := effortKey(c)
n := effortRefusals.m[k]
if time.Since(n.at) > effortRemember {
n = effortRefusal{}
}
if !slices.Contains(n.refused, e) {
n.refused = append(n.refused, e)
}
if len(takes) > 0 {
n.takes = takes
}
n.at = time.Now()
effortRefusals.m[k] = n
}
// takesEffort says whether c's account may be asked for reasoning e on its
// model, as far as magpie knows: not when it turned e away within the hour,
// or its own model list gives the model levels without e.
func takesEffort(c candidate, e string) bool {
if e == "" || c.p.Account == nil {
return true
}
effortRefusals.Lock()
n, ok := effortRefusals.m[effortKey(c)]
effortRefusals.Unlock()
if ok && time.Since(n.at) <= effortRemember {
if slices.Contains(n.refused, e) || len(n.takes) > 0 && !slices.Contains(n.takes, e) {
return false
}
if slices.Contains(n.takes, e) {
return true
}
}
// the account's list names its models by the vendor's id, not one the
// user lists one by (SetModelID)
if levels, ok := c.p.Account.Levels(c.p.OwnModelID(c.model)); ok && !slices.Contains(levels, e) {
return false
}
return true
}
// effortMate is the first of left that is another account of c's member —
// its provider and model, at its fixed effort — that may take e, or -1.
func effortMate(left []candidate, c candidate, e string) int {
return slices.IndexFunc(left, func(x candidate) bool {
return x.p.Account != nil && x.p.ID == c.p.ID && x.model == c.model && x.effort == c.effort &&
x.who() != c.who() && takesEffort(x, e)
})
}
var (
// unsupportedEffort is OpenAI's "Unsupported value: 'high' is not
// supported with the 'gpt-6-luna' model. Supported values are: …"
unsupportedEffort = regexp.MustCompile(`(?i)unsupported value:?\s*['"\x60]?([a-z]+)['"\x60]?\s+is not supported`)
// effortNamed is a refusal naming the reasoning effort itself
effortNamed = regexp.MustCompile(`(?i)reasoning[._ ]?effort`)
effortDenied = regexp.MustCompile(`(?i)not supported|unsupported|not available|isn'?t available|not allowed|invalid value|upgrade|your plan`)
supportedAre = regexp.MustCompile(`(?i)supported values are:?\s*([^.]*)`)
quotedLevel = regexp.MustCompile(`['"\x60]([a-z]+)['"\x60]`)
)
// effortRefused reads a vendor's 400 as the reasoning level sent turned
// away, and the levels it says it takes, if it says. A refusal of some
// other field's value — text.verbosity's 'high' — isn't one.
func effortRefused(body []byte, sent string) (takes []string, ok bool) {
if sent == "" || len(body) == 0 {
return nil, false
}
s := string(body)
var e struct {
Param string `json:"param"`
Error struct {
Param string `json:"param"`
} `json:"error"`
}
if i := strings.IndexByte(s, '{'); i >= 0 && json.Unmarshal([]byte(s[i:]), &e) == nil {
param := e.Error.Param
if param == "" {
param = e.Param
}
if param != "" && !effortNamed.MatchString(param) {
return nil, false
}
}
m := unsupportedEffort.FindStringSubmatch(s)
switch {
case m != nil && strings.EqualFold(m[1], sent):
case m == nil && effortNamed.MatchString(s) && effortDenied.MatchString(s):
default:
return nil, false
}
if sm := supportedAre.FindStringSubmatch(s); sm != nil {
for _, q := range quotedLevel.FindAllStringSubmatch(sm[1], -1) {
if l := strings.ToLower(q[1]); slices.Contains(effortRank, l) && !slices.Contains(takes, l) {
takes = append(takes, l)
}
}
}
return takes, true
}
// modelTakes says whether c's model is known to have level e, or its
// levels aren't known: a level the model has, turned away, is the
// account's plan, not the request.
func modelTakes(c candidate, e string) bool {
levels := c.p.Efforts(c.model)
return len(levels) == 0 || slices.Contains(levels, e)
}
// effortError is what the agent is told when the account left turned the
// level away and none that takes it could answer.
func effortError(c candidate, e string, takes []string, vendor string) string {
msg := fmt.Sprintf("%s doesn't take reasoning effort %q on %s", c.label(), e, c.model)
if len(takes) > 0 {
msg += " (it takes " + strings.Join(takes, ", ") + ")"
}
msg += ", and no other account that does could answer; pick a lower effort or another account"
if vendor != "" {
msg += ": " + vendor
}
return msg
}
// askedTier is the service tier a Responses request asks for.
func askedTier(body []byte) string {
var q struct {
Tier string `json:"service_tier"`
}
decodeJSON(body, &q)
return q.Tier
}
// takesTier says whether c's account may be asked for service tier t on
// its model: not when its own model list says which tiers the model has
// and t isn't one.
func takesTier(c candidate, t string) bool {
if c.p.Account == nil {
return true
}
tiers, ok := c.p.Account.Tiers(c.p.OwnModelID(c.model)) // by the vendor's id, as Levels
return !ok || slices.Contains(tiers, t)
}
// tierMate is the first of left that is another account of c's member
// whose list offers tier t on the model, or -1.
func tierMate(left []candidate, c candidate, t string) int {
return slices.IndexFunc(left, func(x candidate) bool {
if x.p.Account == nil || x.p.ID != c.p.ID || x.model != c.model || x.effort != c.effort || x.who() == c.who() {
return false
}
tiers, ok := x.p.Account.Tiers(x.model)
return ok && slices.Contains(tiers, t)
})
}