Files
magpie/internal/agent/reseat_test.go
T
yetone 608c1543e8 gateway: a bare model name goes to the asking agent's own subscription first
freefcw on X: Codex, started by Mirasim, asked for gpt-6-astra by its bare
name. Its own ChatGPT subscription serves it, but magpie sent it to Devin
through the group it found (auto-gpt-6-astra, soonest renewal first).
With found groups off it still went to another vendor: Resolve takes the
first provider in the Providers order, AutoStandIn and agent.Reseat did
the same.

Now:
- A bare model id the asking agent's own subscription in magpie serves (a
  provider whose Account.Agent is the agent: Codex's ChatGPT accounts,
  Claude's for Claude Code, Copilot's) goes there, found groups on or off
  (gateway ownFirst, provider.OwnRoute). count_tokens counts it there too.
- Kept as they were: a group the user made or changed by that name; a found
  group magpie set one of the agent's fields to (agent.SetToGroup over the
  applied record, #750's bare spelling included, wired as
  gateway.SetToGroup); a gateway key held to some models; ids with a
  provider in them and group refs; clients magpie doesn't know as an agent.
- Found groups off: a bare id several providers serve, none of them the
  agent's own, is refused for an agent that has a subscription of its own
  in magpie, with a 400 naming the provider/model ids and
  "magpie group auto on" (provider.OffSpread). Off, magpie picks no vendor.
- AutoStandInFor and Reseat's sameModel prefer the agent's own
  subscription for a found group that is gone.

Docs: gateway-routing.md's Model resolution row and the bare-ids section.

Run (mote, at this tree on c7ebb6f8d):
- gofmt -l: clean. go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...: ok.
- go test -tags nogui -count=1 -timeout 45m ./internal/provider
  ./internal/agent ./internal/cli ./internal/gateway: all ok.

Broken by hand, each red:
- ownFirst off in serve (and AutoStandIn back):
  TestBareNameGoesToTheAgentsOwnSubscription went to the relay with groups
  on, and again with groups off alone (the reporter's two cases).
- OffSpread check off: TestBareNameOnSeveralVendorsRefusedWhileGroupsOff
  answered 200 from the relay.
- SetToGroup check off: TestBareNameOfAGroupSetOnTheAgentStaysTheGroups.
- The user's-group and own-entry checks off: TestOwnRoute, TestOffSpread.

Not run at this head: the full ./... suite (mote's suite.lock had five
suites queued); it runs under the lock after the push, any red filed.
Not tried: a real Codex with a real Devin plugin account; Reseat's own
preference has no test of its own (needs a fetched Codex account in the
agent package's sandbox).

Reported-by: freefcw (X)
2026-11-09 16:28:02 +08:00

286 lines
10 KiB
Go

package agent
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/provider"
)
// reseatHome is a sandbox HOME with two providers, one of them "cop" in the
// place of Copilot, and Claude Code and Hermes Agent there.
func reseatHome(t *testing.T) string {
t.Helper()
home := effortHome(t)
t.Setenv("PATH", t.TempDir()) // no agent's binary is found, or run
for _, p := range []provider.Provider{
{ID: "cop", Name: "Cop", Chat: "https://cop.example/v1", Key: "k", Models: []string{"gpt-5", "claude-sonnet-4.5", "only-here"}},
{ID: "ds", Name: "DS", Chat: "https://ds.example/v1", Key: "k", Models: []string{"gpt-5", "claude-sonnet-4-5-20250929"}},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
os.MkdirAll(filepath.Join(home, ".claude"), 0o755)
os.MkdirAll(appdir.HermesHomeIn(home), 0o755)
return home
}
// A group Claude Code is on carries its [1m] mark in the agent's own
// settings, as a model's does. sameModel reads that ref back when the
// group's model is down to one provider and the agent has to be moved to
// the same model elsewhere, so the mark has to come off there too: leaving
// it on compares "auto-gpt-5[1m]" with "auto-gpt-5" and finds nothing,
// and an agent whose group is gone is left on it.
func TestSameModelTakesTheOneMMarkOffAGroupRef(t *testing.T) {
reseatHome(t)
for _, p := range []provider.Provider{
{ID: "other", Name: "Other", Chat: "https://other.example/v1", Key: "k", Models: []string{"gpt-5"}},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
opts := []Option{{Value: "magpie/group/auto-gpt-5", Ref: "group/auto-gpt-5"}, {Value: "other/gpt-5", Ref: "other/gpt-5"}}
if got := sameModel(opts, "group/auto-gpt-5[1m]", ""); got.Ref != "other/gpt-5" {
t.Errorf("[1m] on the group: %+v", got)
}
if got := sameModel(opts, "group/auto-gpt-5", ""); got.Ref != "other/gpt-5" {
t.Errorf("the bare group: %+v", got)
}
// a group that is nobody else's still gives nothing (nothing to move to)
none := []Option{{Value: "magpie/group/mine", Ref: "group/mine"}, {Value: "other/gpt-5", Ref: "other/gpt-5"}}
if got := sameModel(none, "group/mine[1m]", ""); got.Ref != "" {
t.Errorf("a group of the user's own: %+v", got)
}
}
func mustFind(t *testing.T, id string) *Agent {
t.Helper()
a, err := Find(id)
if err != nil {
t.Fatal(err)
}
return a
}
func mustApply(t *testing.T, a *Agent, key, v string) {
t.Helper()
mustApplyAs(t, a, key, v, v)
}
// mustApplyAs applies v and expects want written: a found group picked by
// its old id is written as auto/<model>
func mustApplyAs(t *testing.T, a *Agent, key, v, want string) {
t.Helper()
if err := a.Apply(key, v); err != nil {
t.Fatalf("%s %s=%s: %v", a.ID, key, v, err)
}
if got := a.Field(key).Get(); got != want {
t.Fatalf("%s %s: %q, want %q", a.ID, key, got, want)
}
}
// #200: a provider switched off takes its models away from the agents on
// them — to the same model from a provider still on, the same model spelt
// another way, else the agent's own default — not left for the next
// request to be refused.
func TestReseatOff(t *testing.T) {
reseatHome(t)
c, h := mustFind(t, "claude"), mustFind(t, "hermes")
mustApply(t, c, "model", "cop/gpt-5")
mustApply(t, c, "opus", "cop/claude-sonnet-4.5")
mustApply(t, c, "haiku", "cop/only-here")
mustApply(t, c, "sonnet", "ds/gpt-5")
mustApply(t, h, "model", "magpie/cop/only-here")
moves, err := Reseat(func() error { return provider.SetOff("cop", true) })
if err != nil {
t.Fatal(err)
}
want := map[string]string{"model": "ds/gpt-5", "opus": "ds/claude-sonnet-4-5-20250929",
"haiku": "", "sonnet": "ds/gpt-5", "fable": ""} // haiku follows the main model; sonnet keeps the ds/gpt-5 picked for it, now the main model too (#1050)
for k, w := range want {
if got := c.Field(k).Get(); got != w {
t.Errorf("claude %s: %q, want %q", k, got, w)
}
}
if got := h.Field("model").Get(); got != "" {
t.Errorf("hermes: %q, want its own default", got)
}
got := map[string]Move{}
for _, m := range moves {
got[m.Agent+" "+m.Field] = m
}
for k, w := range map[string]Move{
"Claude Code model": {Agent: "Claude Code", Field: "model", From: "cop/gpt-5", To: "ds/gpt-5"},
"Claude Code opus": {Agent: "Claude Code", Field: "opus", From: "cop/claude-sonnet-4.5", To: "ds/claude-sonnet-4-5-20250929"},
"Claude Code haiku": {Agent: "Claude Code", Field: "haiku", From: "cop/only-here"}, // follows the main model
"Hermes Agent model": {Agent: "Hermes Agent", Field: "model", From: "cop/only-here"},
} {
if got[k] != w {
t.Errorf("%s: %+v, want %+v", k, got[k], w)
}
}
if len(moves) != 4 {
t.Errorf("moves: %+v", moves)
}
// switched on again, nothing is moved back or elsewhere
moves, err = Reseat(func() error { return provider.SetOff("cop", false) })
if err != nil || len(moves) != 0 {
t.Fatalf("on again: %+v %v", moves, err)
}
}
// A provider removed moves its agents as one switched off does; one no
// agent is on moves nothing.
func TestReseatDelete(t *testing.T) {
reseatHome(t)
c := mustFind(t, "claude")
mustApply(t, c, "model", "cop/only-here")
moves, err := Reseat(func() error { return provider.Delete("ds") })
if err != nil || len(moves) != 0 {
t.Fatalf("an unused provider: %+v %v", moves, err)
}
if moves, err = Reseat(func() error { return provider.Delete("cop") }); err != nil || len(moves) != 1 {
t.Fatalf("delete: %+v %v", moves, err)
}
// nothing else serves it: Claude Code as installed
if got := c.Field("model").Get(); got != "" {
t.Fatalf("claude: %q", got)
}
}
func TestModelKey(t *testing.T) {
for a, b := range map[string]string{
"claude-sonnet-4.5": "claude-sonnet-4-5-20250929",
"anthropic/claude-opus-4.6": "claude-opus-4-6",
"gpt-5.1-codex": "GPT-5.1-codex",
"gemini-2.5-pro-2025-06-17": "gemini-2.5-pro",
} {
if modelKey(a) != modelKey(b) {
t.Errorf("%s ≠ %s", a, b)
}
}
if modelKey("gpt-5") == modelKey("gpt-5-mini") {
t.Error("gpt-5 = gpt-5-mini")
}
}
// An agent that can't be moved (its config unwritable) doesn't fail the
// change, which is made already: the others are moved and it is a move with
// its error. An error here kept a removed provider's editor open in the
// panel, the provider still listed, and a second Remove said "no provider".
func TestReseatUnmovable(t *testing.T) {
home := reseatHome(t)
c, h := mustFind(t, "claude"), mustFind(t, "hermes")
mustApply(t, c, "model", "cop/only-here")
mustApply(t, h, "model", "magpie/cop/only-here")
dir := appdir.HermesHomeIn(home)
if err := os.Chmod(dir, 0o555); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { os.Chmod(dir, 0o755) })
if f, err := os.Create(filepath.Join(dir, "probe")); err == nil {
f.Close()
t.Skip("a read-only folder is writable here (root?)")
}
os.Chmod(filepath.Join(dir, "config.yaml"), 0o444)
moves, err := Reseat(func() error { return provider.Delete("cop") })
if err != nil {
t.Fatalf("the provider is removed, but Reseat said: %v", err)
}
if _, err := provider.Find("cop"); err == nil {
t.Fatal("cop still there")
}
if got := c.Field("model").Get(); got != "" {
t.Errorf("claude not moved: %q", got)
}
var stuck *Move
for i, m := range moves {
if m.Agent == "Hermes Agent" {
stuck = &moves[i]
}
}
if len(moves) != 2 || stuck == nil || stuck.Error == "" || stuck.To != "" || stuck.From != "cop/only-here" {
t.Fatalf("moves: %+v", moves)
}
if s := stuck.String(); !strings.Contains(s, "not moved") {
t.Errorf("String: %q", s)
}
}
// Found groups turned off (蓝猫 on Discord) take every group/auto-… away:
// an agent on one is moved to its model from the first provider still
// serving it, however that one spells it, rather than to its own default.
func TestReseatFoundGroupsOff(t *testing.T) {
reseatHome(t)
c, h := mustFind(t, "claude"), mustFind(t, "hermes")
// picked by the old ids, written as they are listed now. Opus's
// auto/claude-sonnet-4.5 is given Claude Code dashed, and with the
// group gone reads back as auto/claude-sonnet-4-5: still moved
mustApplyAs(t, c, "model", "group/auto-gpt-5", "auto/gpt-5")
mustApplyAs(t, c, "opus", "group/auto-claude-sonnet-4-5", provider.PublicRef("group/auto-claude-sonnet-4-5"))
mustApplyAs(t, h, "model", "magpie/group/auto-gpt-5", "magpie/auto/gpt-5")
moves, err := Reseat(func() error { return provider.SetAutoGroups(false) })
if err != nil {
t.Fatal(err)
}
for k, w := range map[string]string{"model": "cop/gpt-5", "opus": "cop/claude-sonnet-4.5"} {
if got := c.Field(k).Get(); got != w {
t.Errorf("claude %s: %q, want %q", k, got, w)
}
}
if got := h.Field("model").Get(); got != "magpie/cop/gpt-5" {
t.Errorf("hermes: %q", got)
}
if len(moves) != 3 || moves[0].From != "auto/gpt-5" || moves[0].To != "cop/gpt-5" {
t.Errorf("moves: %+v", moves)
}
// on again, nobody is moved back or elsewhere
if moves, err = Reseat(func() error { return provider.SetAutoGroups(true) }); err != nil || len(moves) != 0 {
t.Fatalf("on again: %+v %v", moves, err)
}
}
// #2671 (timonwong): a provider switched off for a while leaves the found
// group of a model another provider still serves, so an agent on that
// group stays on it, rather than being rewritten to the one provider left
// and not put back when the provider is on again. A model only the
// switched-off provider served still moves.
func TestReseatKeepsAFoundGroupWithAProviderOff(t *testing.T) {
reseatHome(t)
c, h := mustFind(t, "claude"), mustFind(t, "hermes")
mustApplyAs(t, c, "model", "group/auto-gpt-5", "auto/gpt-5")
mustApply(t, c, "haiku", "cop/only-here")
mustApplyAs(t, h, "model", "magpie/group/auto-gpt-5", "magpie/auto/gpt-5")
moves, err := Reseat(func() error { return provider.SetOff("cop", true) })
if err != nil {
t.Fatal(err)
}
if got := c.Field("model").Get(); got != "auto/gpt-5" {
t.Errorf("claude model with cop off: %q", got)
}
if got := h.Field("model").Get(); got != "magpie/auto/gpt-5" {
t.Errorf("hermes with cop off: %q", got)
}
if len(moves) != 1 || moves[0].Field != "haiku" || moves[0].From != "cop/only-here" {
t.Errorf("moves with cop off: %+v", moves)
}
if _, _, ok := provider.Resolve("group/auto-gpt-5"); !ok {
t.Error("group/auto-gpt-5 gone with cop off")
}
// on again, nobody is moved, and the group has cop in it again
if moves, err = Reseat(func() error { return provider.SetOff("cop", false) }); err != nil || len(moves) != 0 {
t.Fatalf("on again: %+v %v", moves, err)
}
if got := c.Field("model").Get(); got != "auto/gpt-5" {
t.Errorf("claude model on again: %q", got)
}
}