Files
magpie/internal/gateway/groupfound_test.go
T
yetoneandtimonwong 2042123f43 provider: a provider switched off for a while stays in the groups magpie found, so agents on auto-<model> aren't rewritten to the one left (#2671)
timonwong (#2671, with the branch this is): a found group, auto-<model>,
was built from the providers that are on only, so switching off one of
its two providers took the group away. Every agent on group/auto-<model>
was reseated to the other provider's model (agent.Reseat → sameModel),
rules and groups naming it went 404, and switching the provider back on
brought the group back but not the agents' settings.

timonwong's change, ported as is (05927b833, 063fe15a2):
- models.go: entriesOf(of, off), and heldOffEntries(), the models of the
  providers that are Ready and Off (a keyless one, a plugin not signed in,
  finds no group), held per request like the other entries.
- group.go: foundGroups/foundFrom merge those back in the Providers order
  before autoGroups, in groupsIn, DeleteGroup and RenameGroup. membersIn
  already skips a switched-off member, and the Routing page already says
  it as switched off (memberJSON.providerOff).
- A removed provider still leaves the group; NoAutoGroups and
  ModelSameAs are honoured; a hidden found group stays hidden.
  Side effect: a found group whose only usable member is left shows while
  a provider is off, which is the point.
Reviewed line by line; nothing writes a user-set value, and Reseat now
has nothing to move for these agents, so there is nothing to restore when
the provider is on again.

Added here: TestReseatKeepsAFoundGroupWithAProviderOff (internal/agent:
Claude Code and Hermes on group/auto-gpt-5 are not moved when cop is
switched off, a model only cop served still is, the group resolves, and
switching cop on moves nobody) and TestFoundGroupMemberOfASwitchedOffProvider
(internal/gui: auto-x is on the Routing page with b/x said as switched
off). The docs row names the tests.

Red on bf931f82c's provider code (models.go and group.go from main): both
new tests, timonwong's TestFoundGroupWithAProviderOff (404 as in the
issue) and the updated TestProviderOff. Broken by hand (heldOffEntries
returning nil): the agent and gui tests and TestFoundGroupStaysWithAProviderOff
and TestFoundGroupRemovedWithAProviderOff go red.

Run at this change on bf931f82c: gofmt -l clean; go vet ./... (darwin),
GOOS=linux and GOOS=windows go vet -tags nogui ./... ok; full go test
-tags nogui -count=1 -timeout 45m ./... (load average ~140): every package
ok, internal/gateway and internal/provider included, except
internal/agent, which ran past 45m (#2544), and internal/middleware's
TestDocsExample, a strategy's wall-clock budget under load that passes
alone (filed as #2728). internal/agent then alone, -timeout 100m: ok in
2971s.
No GUI asset changed (the gui test is a Go test of groupsState), so the
Playwright suite was not run.

Co-authored-by: timonwong <timon86.wang@gmail.com>
2026-10-17 20:38:06 +08:00

77 lines
2.8 KiB
Go

package gateway
import (
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// An agent left on a group magpie found, or a session begun on one, keeps
// working once the user turns found groups off (蓝猫 on Discord): its
// request goes to the model from the first provider that serves it, as a
// model of one provider — not refused as a model magpie doesn't know.
func TestFoundGroupOff(t *testing.T) {
fresh(t)
a, b := &keyed{}, &keyed{}
serveOn(t, "a", "ka", []string{"m"}, a)
serveOn(t, "b", "kb", []string{"m"}, b)
s := New()
const req = `{"model":"group/auto-m","messages":[{"role":"user","content":"hi"}]}`
if code, body := postAs(t, s, "", req); code != 200 {
t.Fatalf("on: %d %s", code, body)
}
if r := s.trace.routes[len(s.trace.routes)-1]; r.Group == nil {
t.Fatalf("on, not routed as the group: %+v", r)
}
if err := provider.SetAutoGroups(false); err != nil {
t.Fatal(err)
}
a.tried, b.tried = nil, nil
code, body := postAs(t, s, "", req)
if code != 200 || !strings.Contains(body, "from ka") || len(b.tried) != 0 {
t.Fatalf("off: %d %s, a tried %v, b tried %v", code, body, a.tried, b.tried)
}
if r := s.trace.routes[len(s.trace.routes)-1]; r.Group != nil {
t.Fatalf("off, routed as a group: %+v", r.Group)
}
// a bare model id is the first provider's too
a.tried = nil
if code, body := postAs(t, s, "", `{"model":"m","messages":[{"role":"user","content":"hi"}]}`); code != 200 || len(a.tried) == 0 {
t.Fatalf("bare m: %d %s", code, body)
}
}
// An agent set to a group magpie found keeps it while the user switches
// one of its two providers off for a while: the request is the group's,
// answered by the provider still on, not refused as a model magpie
// doesn't know, and the other is asked again once it is back on.
func TestFoundGroupWithAProviderOff(t *testing.T) {
fresh(t)
a, b := &keyed{}, &keyed{}
serveOn(t, "a", "ka", []string{"m"}, a)
serveOn(t, "b", "kb", []string{"m"}, b)
s := New()
if err := provider.SetOff("a", true); err != nil {
t.Fatal(err)
}
for _, model := range []string{"group/auto-m", "m"} {
a.tried, b.tried = nil, nil
code, body := postAs(t, s, "", `{"model":"`+model+`","messages":[{"role":"user","content":"hi"}]}`)
if code != 200 || !strings.Contains(body, "from kb") || len(a.tried) != 0 {
t.Fatalf("%s with a off: %d %s, a tried %v", model, code, body, a.tried)
}
if r := s.trace.routes[len(s.trace.routes)-1]; r.Group == nil {
t.Fatalf("%s with a off, not routed as the group: %+v", model, r)
}
}
if err := provider.SetOff("a", false); err != nil {
t.Fatal(err)
}
// another conversation, so b's answer to the one above doesn't hold it
if code, body := postAs(t, s, "", `{"model":"group/auto-m","messages":[{"role":"user","content":"a new one"}]}`); code != 200 || !strings.Contains(body, "from ka") {
t.Fatalf("a on again: %d %s", code, body)
}
}