Files
magpie/internal/agent/reseat.go
T
yetone 0621e70fb9 routing: leave a provider out of the groups magpie finds
chenxiang0010 on Discord asked to keep a provider or a model out of the
auto-created routing groups. A model could already be kept out (remove
its found group; magpie keeps it removed), but a provider could only be
turned off per group or everywhere. Now Routing -> Groups, under "Find
groups on their own", has "Leave out a provider..." (app menu, saved
when it closes) and `magpie group auto skip <provider>...|none`. The
providers chosen (providers.json autoGroupSkip) join no found group; a
group the user made or changed keeps them, a found group left with one
provider goes away, and a request for it is served by AutoStandIn from
a provider still in, and agents on it are reseated (agent.Reseat). A
rename carries the id along. en, zh, zh-TW, ja, de.

Run at 383947fce (this change on origin/main 2ff75161d):
- go test -timeout 45m ./... on mote: 80 packages ok; internal/gui red
  only in its real-window AppKit tests (TestDockJoinLeadsCommandTab,
  TestPanelCloseHidesIt, TestMainWindowCorners, ...), which fail the same
  on fresh origin/main 2f65a33aa on mote (#4125, #2645).
- gofmt -l clean; go vet ./... on Windows (win-run) clean; on Linux
  (wsl-run) clean with -tags nogui (the WSL distro has no gtk4).
- GUI: group-found-skip (new) and group-found-off, all five languages,
  1100px and 390px, Chromium and WebKit on Linux (wsl-run): 24/24. On
  mote every WebKit launch times out (#4125), Chromium 12/12 there.
- The routing files the full run flagged (concurrency 6) rerun alone on
  wsl-run, here and at fresh origin/main 380032cda: click-scroll's
  WebKit case and group-morph's WebKit 420px cases fail the same on
  origin/main. group-duplicate's de case was mine: the found groups'
  new "Anbieter ausnehmen..." line made German taller than the test's
  420x1200 window, which it assumes needs no scroll; it now uses 1400
  and passes 10/10 in both engines.
- Whole GUI suite on wsl-run, concurrency 6: still running at push time
  (5092 ok, 170 not ok so far, mostly in files this change doesn't load:
  account-*, agent-*, connect-*, gui-de/gui-ja's "{agent}" placeholder
  string). They are compared against origin/main after the push.

Broken by hand: foundGroups without unskipped -> TestAutoGroupSkip and
TestGroupsSkip red. Test calls before the fix to groupCmd's args ->
TestGroupAutoSkip red.

Not tried: the real app with real providers; a model-level skip (a
model is kept out by removing its found group, as before).
2026-10-31 00:57:21 +08:00

211 lines
6.5 KiB
Go

package agent
import (
"regexp"
"slices"
"strings"
"github.com/yetone/magpie/internal/provider"
)
// A provider switched off or removed, or the last account of a sign-in
// signed out, takes its models out of the catalog, but an agent picked one
// of them keeps asking for it, and its next request is refused (#200:
// Copilot switched off, an agent still on copilot/…). Reseat moves such
// agents along with the change: to the same model from a provider still on,
// else back to the agent's own default.
// Move is one of an agent's fields moved off a model magpie no longer serves.
type Move struct {
Agent string `json:"agent"` // the agent's name
Field string `json:"field"` // model, or a tier (Claude Code's haiku …)
From string `json:"from"` // the catalog model it was on
To string `json:"to"` // the one it is on now; "" its own default
// Error is why the agent couldn't be moved (its file unwritable, say):
// it stays on From, which magpie no longer serves
Error string `json:"error,omitempty"`
}
// String is the move as the CLI and the TUI say it.
func (m Move) String() string {
if m.Error != "" {
return m.Agent + " " + m.Field + ": still on " + m.From + ", not moved: " + m.Error
}
to := m.To
if to == "" {
to = "its default"
}
return m.Agent + " " + m.Field + ": " + m.From + " → " + to
}
// pick is a field on one of magpie's models.
type pick struct {
a *Agent
key, val string
ref string // the catalog model
}
// picks reads every detected agent's fields that are on a model magpie serves.
func picks() []pick {
var out []pick
for _, a := range Detected() {
if len(a.Fields) == 0 {
continue
}
vals := a.Values()
for _, f := range a.Fields {
v := vals[f.Key]
if v == "" || f.Options == nil {
continue
}
// a list of models is the user's own, left as it is
model, _, one := a.split(v)
if !one {
continue
}
for _, o := range f.Options(vals) {
if o.Value == model && o.Ref != "" {
out = append(out, pick{a, f.Key, v, o.Ref})
break
}
}
}
}
return out
}
// Reseat makes a change to the providers (change) and moves every agent
// whose model it stopped serving: to that model from another provider
// that serves it, else to the agent's own default. It answers the moves.
// Its error is the change's alone: once the change is made, an agent that
// can't be moved is a move with its Error and the others are still moved.
// An error then would say the change failed when it was made — the panel,
// told so, kept a removed provider's editor open and listed, and a second
// Remove said there was no such provider.
func Reseat(change func() error) ([]Move, error) {
before := picks()
if err := change(); err != nil {
return nil, err
}
var moves []Move
// the last fields first: a Claude Code tier moved after the main model
// would have followed it, whatever model of its own there is for it
for i := len(before) - 1; i >= 0; i-- {
p := before[i]
if served(p.ref) {
continue
}
f := p.a.Field(p.key)
vals := p.a.Values()
// changed already, with another field
if f == nil || !p.a.samePick(vals[p.key], p.val) {
continue
}
var to Option
if f.Options != nil {
to = sameModel(f.Options(vals), p.ref)
}
m := Move{Agent: p.a.Name, Field: f.Label, From: p.ref, To: to.Ref}
// the same model elsewhere keeps the agent's suffix after it (omp's
// thinking level); the agent's own default has none
if to.Value != "" {
_, suffix, _ := p.a.split(p.val)
to.Value += suffix
}
if err := p.a.Apply(p.key, to.Value); err != nil {
m.To, m.Error = "", err.Error()
}
moves = append(moves, m)
}
slices.Reverse(moves)
return moves, nil
}
// samePick: a field's value now is still the pick read before the change.
// A found group's auto/<model> reads back as written while the group is
// served, but Claude Code is given a dotted Claude version dashed
// (ClaudeSpelled), and with the group gone nothing takes
// auto/claude-sonnet-4-5 back to auto/claude-sonnet-4.5. Both are the one
// group by its canonical id, so the agent is still moved off it.
func (a *Agent) samePick(now, before string) bool {
if now == before {
return true
}
m1, s1, one1 := a.split(now)
m2, s2, one2 := a.split(before)
return one1 && one2 && s1 == s2 && provider.IsGroupRef(m1) &&
provider.CanonicalRef(m1) == provider.CanonicalRef(m2)
}
// served: magpie takes requests for the catalog model.
func served(ref string) bool {
_, _, ok := provider.Resolve(ref)
return ok
}
// sameModel is the option for ref's model from another provider still
// serving it: the same id, else one that differs only in how it is spelt
// (claude-sonnet-4.5 and claude-sonnet-4-5-20250929). For a group magpie
// found (group/auto-…), gone as found groups are turned off, a provider
// is kept out of them, or its model is down to one provider magpie has, it
// is that model from the first provider still serving it; none for any other routing group, which is
// nobody else's.
func sameModel(opts []Option, ref string) Option {
// a group's id carries Claude Code's [1m] mark as a model's does, and
// this ref comes from the agent's own settings
if gid, ok := provider.GroupIDOf(ref); ok {
if !strings.HasPrefix(gid, "auto-") {
return Option{}
}
// a provider the user keeps out of found groups last
// (provider.SetAutoGroupSkip): skipping it is what took the group
skip := provider.AutoGroupSkip()
var kept Option
for _, o := range opts {
if o.Ref == "" || provider.IsGroupRef(o.Ref) || !served(o.Ref) {
continue
}
pid, m, _ := strings.Cut(o.Ref, "/")
if provider.AutoGroupOf(pid, m) != gid {
continue
}
if !slices.Contains(skip, pid) {
return o
}
if kept.Ref == "" {
kept = o
}
}
return kept
}
pid, model, ok := strings.Cut(ref, "/")
if !ok || pid+"/" == provider.GroupPrefix {
return Option{}
}
want := modelKey(model)
var alike Option
for _, o := range opts {
if o.Ref == "" || provider.IsGroupRef(o.Ref) || !served(o.Ref) {
continue
}
_, m, _ := strings.Cut(o.Ref, "/")
if m == model {
return o
}
if alike.Ref == "" && modelKey(m) == want {
alike = o
}
}
return alike
}
var dated = regexp.MustCompile(`-(\d{8}|\d{4}-\d{2}-\d{2})$`)
// modelKey is a model id as providers agree on it: its last part, lower
// case, with dots and underscores as dashes and no date.
func modelKey(m string) string {
m = strings.ToLower(m[strings.LastIndex(m, "/")+1:])
m = strings.NewReplacer(".", "-", "_", "-").Replace(m)
return dated.ReplaceAllString(m, "")
}