Files
magpie/internal/gateway/auto_picks_test.go
T
yetone dc507aefd3 gateway: Automatic image recognition and web search stay with the provider that served them
hxy920830 on X added a Claude account only for account switching, and
every agent's image recognition and web search moved onto it: with
Settings › Models › Image recognition and Web search on Automatic, magpie
ranked the picks again on each call, and a Claude account ranks first in
both (seersRanked tier 0, searchRank 0). Adding a provider reassigned
what another provider already served.

- auto_picks.go holds each Automatic slot (Image recognition, Web search,
  Image generation, Video generation) to the provider magpie first picked,
  in auto-picks.json beside providers.json. seersRankedHeld and
  autoSearcherReady put the held provider before every other of its
  readiness; AutoDrawer and AutoVideomaker pick among its models while it
  has one. A default fills only a slot nothing serves: nothing held, or
  the held provider removed, off, or left with no model for it.
- Allowance still comes first for the moment (#1801); the hold stays.
- A model picked in Settings is untouched. The picker already shows the
  pick as "Automatic · <model>", where it can be changed.
- A file that can't be read holds nothing and is not written over.

Users already switched: holds begin once, when the gateway first serves
(seedAutoPicks). It reads the last 30 days of the ledger's Image
description calls (kind vision) and the searcher's Web search calls
(kind web_search, not search API / native rows). A slot a later provider
took over is given back to the provider that served the call just before
the current pick's first, while it can still serve it. A switch older
than 30 days, or one whose earlier provider is gone, is not restored: the
current pick is held and the user picks in Settings › Models. Image and
video generation have no Automatic call in the ledger and are held to
the pick at the seed, nothing restored.

Docs: docs/subsystems/gateway-routing.md, "Automatic picks are held to
the provider that serves them".

Run:
- go test ./internal/gateway -run 'TestAddedProviderTakesNoAutomaticPick|TestAutoPicksSeededFromTheLedger|TestClaudeAccountAddedTakesNoImages' -v: ok (mica)
- GOOS=linux go vet ./internal/gateway, GOOS=windows go vet ./internal/gateway: ok (mica)
- go test -count=1 -timeout 45m ./... (mote, at 589e4ba6d + this change):
  every package ok (internal/gateway 1080s) except seven reds, each
  already filed and each passing alone at this tree on mica:
  capre TestCappedIsSmall (#5975); cli TestHealthcheckFindsTheRunning-
  GatewayWithoutMagpieAddr, TestUpdateRestartsTheGatewayItFindsWithout-
  MagpieAddr, TestUpdateFindsAMagpieThatWroteNoRecord (#6223, #6471:
  something serves on mote's 127.0.0.1:3425); gui TestAppReplacesIts-
  OlderGatewayOnceIdle (#6297), TestLaneLimitRoutes (#5150). The four
  cli and capre tests pass on origin/main 9314999a4 too.
- After the rebase onto origin/main e08dc0fe5 (mica): go test ./internal/gateway -run 'the three|Vision|Searcher|Drawer|Videomaker' ok, GOOS=windows go vet ok.

Broken by hand: heldPick returning "" (the old ranking) turns all three
tests red: AutoVision = "anth/eye" after a provider was added, and
"claude/claude-haiku-5-5" after the Claude account was signed in.
Not tried: the real app with a real Claude account; no GUI file changed,
so the GUI suite was not run.
2026-11-18 20:27:20 +08:00

226 lines
7.3 KiB
Go

package gateway
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/usage"
)
// seeing saves a provider whose one enabled model, eye, sees, at price
// a million tokens in and out.
func seeing(t *testing.T, p provider.Provider, price float64) {
t.Helper()
p.Name, p.Key, p.Models = p.ID, p.ID+"-key", []string{"eye"}
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
base := p.Responses + p.Anthropic + p.Chat
if err := catalog.SaveLive(p.ID, base, []catalog.Model{
{ID: "eye", Images: true, ImageInput: imageInputBool(true), Price: &catalog.Price{Input: price, Output: price}},
}); err != nil {
t.Fatal(err)
}
}
func heldFile(t *testing.T) autoPicksFile {
t.Helper()
b, err := os.ReadFile(autoPicksPath())
if err != nil {
t.Fatal(err)
}
var f autoPicksFile
if err := json.Unmarshal(b, &f); err != nil {
t.Fatal(err)
}
return f
}
func autoSearcherID() string {
p, _, ok := autoSearcher()
if !ok {
return ""
}
return p.ID
}
// A provider added while Image recognition and Web search are left on
// Automatic takes neither over (hxy920830 on X: a Claude account added
// for account switching alone took every agent's images and searches).
// What served them goes on serving them; one added later serves them
// only once it can't, and a model picked in Settings is still the one
// used.
func TestAddedProviderTakesNoAutomaticPick(t *testing.T) {
fresh(t)
// OpenAI's API: searches by itself (rank 3), sees at 5
seeing(t, provider.Provider{ID: "oai", Responses: "https://api.openai.com/v1"}, 5)
seedAutoPicks()
if f := heldFile(t); !f.Seeded || f.Vision != "oai" || f.Searcher != "oai" {
t.Fatalf("held %+v, want oai for both", f)
}
// Anthropic's API, added later: ranks before it for both (rank 2,
// cheaper), as a Claude account ranks first
seeing(t, provider.Provider{ID: "anth", Anthropic: "https://api.anthropic.com"}, 1)
if m := AutoVision(); m != "oai/eye" {
t.Fatalf("AutoVision = %q after a provider was added, want oai/eye still", m)
}
if id := autoSearcherID(); id != "oai" {
t.Fatalf("searcher = %q after a provider was added, want oai still", id)
}
if s := AutoSearcher(); !strings.HasPrefix(s, "oai") {
t.Fatalf("AutoSearcher = %q, Settings' Automatic would name another", s)
}
// the user's own pick is the one used
st := settings.Load()
st.Vision, st.Searcher = "anth/eye", "anth"
if err := settings.Save(st); err != nil {
t.Fatal(err)
}
if m, _ := seer(); m != "anth/eye" {
t.Fatalf("seer = %q with anth/eye picked", m)
}
if p, _, ok := searcher(); !ok || p.ID != "anth" {
t.Fatalf("searcher = %q with anth picked", p.ID)
}
st.Vision, st.Searcher = "", ""
if err := settings.Save(st); err != nil {
t.Fatal(err)
}
// the held provider turned off: the slot is empty, and the next fills it
p, err := provider.Find("oai")
if err != nil {
t.Fatal(err)
}
p.Off = true
if err := provider.Save(*p); err != nil {
t.Fatal(err)
}
if m := AutoVision(); m != "anth/eye" {
t.Fatalf("AutoVision = %q with oai off", m)
}
if id := autoSearcherID(); id != "anth" {
t.Fatalf("searcher = %q with oai off", id)
}
if f := heldFile(t); f.Vision != "anth" || f.Searcher != "anth" {
t.Fatalf("held %+v with oai off, want anth", f)
}
// and turned on again it doesn't take the slot back from anth
p.Off = false
if err := provider.Save(*p); err != nil {
t.Fatal(err)
}
if m := AutoVision(); m != "anth/eye" {
t.Fatalf("AutoVision = %q with oai back", m)
}
}
// Before holds were kept, the provider added later took the slots: the
// first start with them gives each back to what served it before, as the
// ledger's Image description and Web search calls show, while it can
// still serve. One whose calls the ledger shows from the first row is
// kept, and so is the pick now when the one before is gone.
func TestAutoPicksSeededFromTheLedger(t *testing.T) {
ago := func(h int) time.Time { return time.Now().Add(-time.Duration(h) * time.Hour) }
call := func(at time.Time, kind, pid, source string) {
usage.Append(usage.Record{Time: at, Agent: "magpie", Kind: kind, Provider: pid, Model: "eye", Source: source, Status: 200})
}
t.Run("taken over", func(t *testing.T) {
fresh(t)
seeing(t, provider.Provider{ID: "oai", Responses: "https://api.openai.com/v1"}, 5)
seeing(t, provider.Provider{ID: "anth", Anthropic: "https://api.anthropic.com"}, 1)
// anth's own search for its own model, before it was magpie's pick:
// not a searcher's call
call(ago(9), "web_search", "anth", usage.SourceSearch)
call(ago(8), "vision", "oai", "")
call(ago(7), "web_search", "oai", "")
call(ago(6), "vision", "oai", "")
call(ago(5), "web_search", "oai", "")
// anth added: magpie picked it for both
call(ago(2), "vision", "anth", "")
call(ago(1), "web_search", "anth", "")
seedAutoPicks()
if f := heldFile(t); f.Vision != "oai" || f.Searcher != "oai" {
t.Fatalf("held %+v, want oai given back for both", f)
}
if m := AutoVision(); m != "oai/eye" {
t.Fatalf("AutoVision = %q", m)
}
if id := autoSearcherID(); id != "oai" {
t.Fatalf("searcher = %q", id)
}
})
t.Run("never taken over", func(t *testing.T) {
fresh(t)
seeing(t, provider.Provider{ID: "oai", Responses: "https://api.openai.com/v1"}, 5)
seeing(t, provider.Provider{ID: "anth", Anthropic: "https://api.anthropic.com"}, 1)
// anth served from the first call; oai only while anth was out
call(ago(8), "vision", "anth", "")
call(ago(6), "vision", "oai", "")
call(ago(2), "vision", "anth", "")
seedAutoPicks()
if f := heldFile(t); f.Vision != "anth" || f.Searcher != "anth" {
t.Fatalf("held %+v, want anth kept", f)
}
})
t.Run("the one before gone", func(t *testing.T) {
fresh(t)
seeing(t, provider.Provider{ID: "anth", Anthropic: "https://api.anthropic.com"}, 1)
call(ago(8), "vision", "removed", "")
call(ago(2), "vision", "anth", "")
seedAutoPicks()
if f := heldFile(t); f.Vision != "anth" {
t.Fatalf("held %+v, want anth: the one before is gone", f)
}
})
}
// The reporter's case: a Claude account, which ranks first for Image
// recognition, signed in after a key's model that sees served it.
func TestClaudeAccountAddedTakesNoImages(t *testing.T) {
claudeMadeFirst(t, false)
logins := filepath.Join(filepath.Dir(provider.Path()), "logins.json")
raw, err := os.ReadFile(logins)
if err != nil {
t.Fatal(err)
}
os.Remove(logins)
provider.ForgetAccounts()
t.Cleanup(provider.ForgetAccounts)
seeing(t, provider.Provider{ID: "keyed", Chat: "http://keyed.invalid/v1"}, 1)
seedAutoPicks()
if m := AutoVision(); m != "keyed/eye" {
t.Fatalf("AutoVision = %q before the Claude account", m)
}
if err := os.WriteFile(logins, raw, 0o600); err != nil {
t.Fatal(err)
}
provider.ForgetAccounts()
if err := provider.Save(provider.Provider{ID: "claude", Models: []string{"claude-haiku-5-5"}}); err != nil {
t.Fatal(err)
}
yes := true
if err := provider.SetModelImage("claude/claude-haiku-5-5", &yes); err != nil {
t.Fatal(err)
}
if r := seersRankedHeld(nil, ""); len(r) == 0 || !strings.HasPrefix(r[0].model, "claude/") {
t.Fatalf("unheld, the Claude account isn't first: %v", r)
}
if m := AutoVision(); m != "keyed/eye" {
t.Fatalf("AutoVision = %q after the Claude account was added, want keyed/eye", m)
}
}