diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md index 5e7e6c5c1..279ffb9c4 100644 --- a/docs/subsystems/gateway-routing.md +++ b/docs/subsystems/gateway-routing.md @@ -29,6 +29,18 @@ select-all Remove) also turns found groups off (`SetAutoGroups(false)`, xwang_sh on X), so a model a provider lists later brings no new one; the removed records stay and turning them on again restores the groups as they were. +A found group can go without the user removing it: one of its two providers +is removed, or its list no longer has the model. `Reseat` moves an agent's +config off it, but a conversation saved on it (a Codex thread names its +model in every turn and in the compaction before a model switch) still asks +for it. `provider.AutoStandIn` sends such a request to the model from a +provider that still serves it, the same model and never another, as it does +while found groups are off or a provider is kept out of them; a group the +user changed or removed gets none. With no provider serving the model the +request is still 404, and `unknownModel` says the model is gone and to pick +another for the conversation instead of listing every id +(`provider.GoneAutoGroup`; canlee886 on X, `TestFoundGroupGoneGoesToItsModel`). + ## Responsibilities and sources of truth | Part | Responsibility | Source | diff --git a/internal/gateway/found_group_gone_test.go b/internal/gateway/found_group_gone_test.go new file mode 100644 index 000000000..67ff6c8df --- /dev/null +++ b/internal/gateway/found_group_gone_test.go @@ -0,0 +1,102 @@ +package gateway + +import ( + "encoding/json" + "net/http/httptest" + "strings" + "testing" + + "github.com/yetone/magpie/internal/provider" +) + +// A conversation saved on a group magpie found keeps working once the group +// is gone (canlee886 on X: a Codex thread on group/auto-gpt-6-1-sol failed +// every turn with 404 once one of its two providers no longer listed +// gpt-6.1-sol, and switching the thread's model did not help). While a +// provider still serves the model, the group's id goes to that model there; +// it is the same model, so nothing is switched. With none left the 404 says +// the model is gone and to pick another, not every id magpie has. +func TestFoundGroupGoneGoesToItsModel(t *testing.T) { + fresh(t) + a, b := &keyed{}, &keyed{} + serveOn(t, "a", "ka", []string{"gpt-6.1-sol", "gpt-5.6-sol"}, a) + serveOn(t, "b", "kb", []string{"gpt-6.1-sol", "gpt-5.6-sol"}, b) + ask := func(model string) (int, string) { + t.Helper() + return postAs(t, New(), "", `{"model":"`+model+`","messages":[{"role":"user","content":"hi"}]}`) + } + for _, m := range []string{"group/auto-gpt-6-1-sol", "auto/gpt-6.1-sol"} { + if code, out := ask(m); code != 200 { + t.Fatalf("%s while found: %d %s", m, code, out) + } + } + + // a's list no longer has the model: the group is gone, b still serves it + p, err := provider.Find("a") + if err != nil { + t.Fatal(err) + } + p.Models = []string{"gpt-5.6-sol"} + if err := provider.Save(*p); err != nil { + t.Fatal(err) + } + if g, _, ok := provider.FindGroup("group/auto-gpt-6-1-sol"); ok { + t.Fatalf("the group is still found: %+v", g) + } + for _, m := range []string{"group/auto-gpt-6-1-sol", "auto/gpt-6.1-sol", "auto/gpt-6.1-sol:high"} { + a.tried, b.tried = nil, nil + code, out := ask(m) + if code != 200 || !strings.Contains(out, "from kb") || len(a.tried) != 0 { + t.Fatalf("%s with b alone serving it: %d %s (a asked %v)", m, code, out, a.tried) + } + } + + // a group the user removed stays removed: no stand-in for it + if err := provider.DeleteGroup("auto-gpt-5-6-sol"); err != nil { + t.Fatal(err) + } + if code, _ := ask("group/auto-gpt-5-6-sol"); code != 404 { + t.Fatalf("a removed group answered %d", code) + } + + // nothing serves it any more: 404, saying what to do, not every id + if err := provider.Delete("b"); err != nil { + t.Fatal(err) + } + code, out := ask("group/auto-gpt-6-1-sol") + if code != 404 { + t.Fatalf("no provider serves it: %d %s", code, out) + } + var e struct { + Error struct { + Message string `json:"message"` + } `json:"error"` + } + json.Unmarshal([]byte(out), &e) + msg := e.Error.Message + if !strings.Contains(msg, `"group/auto-gpt-6-1-sol"`) || !strings.Contains(msg, "pick another model for this conversation") || strings.Contains(msg, "it has ") { + t.Fatalf("the 404 for a gone group: %s", msg) + } +} + +// Codex, switched off a model magpie no longer serves, compacts the thread +// on the old model first (model_downshift): that compaction is magpie's own +// local summary, never the turn's 404, so the thread goes on on the model +// it was switched to. +func TestCodexLeavingGoneGroupCompactsLocally(t *testing.T) { + fresh(t) + serveOn(t, "a", "ka", []string{"gpt-5.6-sol"}, &keyed{}) + meta := `{"session_id":"th1","thread_id":"th1","turn_id":"tu1","request_kind":"compaction","compaction":{"trigger":"auto","reason":"model_downshift","implementation":"responses_compaction_v2","phase":"pre_turn","strategy":"memento"}}` + cm, _ := json.Marshal(map[string]string{"x-codex-turn-metadata": meta}) + body := `{"model":"group/auto-gpt-6-1-sol","stream":true,"prompt_cache_key":"th1","client_metadata":` + string(cm) + `,"input":[` + + `{"type":"message","role":"user","content":[{"type":"input_text","text":"build the parser"}]},` + + `{"type":"message","role":"assistant","content":[{"type":"output_text","text":"the parser is built"}]},` + + `{"type":"compaction_trigger"}]}` + rec := httptest.NewRecorder() + req := httptest.NewRequest("POST", "/v1/responses", strings.NewReader(body)) + req.Header.Set(turnMetadataHeader, meta) + New().Handler().ServeHTTP(rec, req) + if rec.Code != 200 || !strings.Contains(rec.Body.String(), "response.completed") { + t.Fatalf("compaction on a gone group: %d %s", rec.Code, rec.Body.String()) + } +} diff --git a/internal/gateway/unknown_model.go b/internal/gateway/unknown_model.go index 178c19893..08fca7ab0 100644 --- a/internal/gateway/unknown_model.go +++ b/internal/gateway/unknown_model.go @@ -11,8 +11,18 @@ import ( // (MOMO on Discord: "Zen-Space-Bunny" got a 404 that said neither how a // model is named nor which one was meant): the ids nearest to it, the // ways a model can be named, then every id there is. what is "" for a -// chat, or what the model was for (" to draw with"). +// chat, or what the model was for (" to draw with"). A group magpie found +// that is gone with its model is said so, with what to do (GoneAutoGroup). func unknownModel(asked, what string) string { + if provider.GoneAutoGroup(asked) { + // a conversation saved on a group magpie found whose model no + // provider serves now (canlee886 on X): what to do, not every id + msg := fmt.Sprintf("magpie no longer serves %q%s: it was the routing group magpie found for a model, and no provider in magpie serves that model now", asked, what) + if near := provider.Closest(asked, 3); len(near) > 0 { + msg += " (nearest now: " + strings.Join(near, ", ") + ")" + } + return msg + "; pick another model for this conversation in the agent (in Codex, the model menu under the message box), or add back a provider that serves it" + } msg := fmt.Sprintf("magpie knows no model %q%s", asked, what) if near := provider.Closest(asked, 3); len(near) > 0 { msg += "; did you mean " + strings.Join(near, ", ") + "?" diff --git a/internal/provider/group.go b/internal/provider/group.go index 4d2de8ad5..4e1b57926 100644 --- a/internal/provider/group.go +++ b/internal/provider/group.go @@ -942,14 +942,18 @@ var editionTwins = map[string]string{ "workbuddy-ai/deepseek-v4.1-flash-sg": "deepseek-v4.1-flash", } -// AutoStandIn is the model a request for a group magpie found goes to while -// such groups are off (SetAutoGroups), or once the providers the user keeps -// out of them (SetAutoGroupSkip) took it away: its model, from the first -// provider that serves it, one not kept out first, as "provider/model". An -// agent set to the group, or a session begun on it, keeps working, on one -// provider. ok is false for any other id, a group the user has of that id, -// one they removed, and while found groups are on, one there is or that -// isn't gone for a provider kept out. +// AutoStandIn is the model a request for a group magpie found goes to +// when there is no such group now: while such groups are off +// (SetAutoGroups), once the providers the user keeps out of them +// (SetAutoGroupSkip) took it away, or once fewer than two providers serve +// its model (a provider removed, or its list without the model: canlee886 +// on X, a Codex thread saved on group/auto-gpt-6-1-sol failed every turn +// with 404). It is its model, from the first provider that serves it, one +// not kept out first, as "provider/model": the same model, never another. +// An agent set to the group, or a session begun on it, keeps working, on +// one provider. ok is false for any other id, a group there is, a group +// the user has of that id, one they removed, and a model no provider +// serves now (GoneGroupModel). func AutoStandIn(id string) (string, bool) { return AutoStandInFor("", id) } // AutoStandInFor is AutoStandIn for a request of agent's: the agent's own @@ -966,14 +970,8 @@ func AutoStandInFor(agent, id string) (string, bool) { if _, ok := groupOf(groupsIn(entries), gid); ok { return "", false } - if !f.NoAutoGroups { - if len(f.AutoGroupSkip) == 0 || slices.ContainsFunc(f.Groups, func(g Group) bool { return g.ID == gid }) { - return "", false // not gone for skipping: removed, or the user's - } - all := autoGroups(foundFrom(entries, heldOffEntries()), heldSettings().ModelSameAs) - if !slices.ContainsFunc(all, func(g Group) bool { return g.ID == gid }) { - return "", false - } + if !f.NoAutoGroups && slices.ContainsFunc(f.Groups, func(g Group) bool { return g.ID == gid }) { + return "", false // removed, or the user's } var first, kept string for _, e := range entries { @@ -996,6 +994,30 @@ func AutoStandInFor(agent, id string) (string, bool) { return first, first != "" } +// GoneAutoGroup reports whether ref names a group magpie found that is +// gone with its model: no group of that id now, none the user has or +// removed, and no provider serving its model (AutoStandIn has none). An +// agent's conversation saved on it (canlee886 on X: a Codex thread on +// group/auto-gpt-6-1-sol) is told the model is gone and to pick another, +// rather than every id magpie has. +func GoneAutoGroup(ref string) bool { + gid, ok := GroupIDOf(ref) + if i := strings.LastIndex(gid, ":"); i > 0 && slices.Contains(MemberEfforts, gid[i+1:]) { + gid = gid[:i] + } + if !ok || !strings.HasPrefix(gid, "auto-") || gid == "auto-" { + return false + } + if _, ok := groupOf(groupsIn(providerEntries()), gid); ok { + return false + } + if slices.ContainsFunc(load().Groups, func(g Group) bool { return g.ID == gid }) { + return false + } + _, ok = AutoStandIn(GroupPrefix + gid) + return !ok +} + // foundGroups are the groups magpie finds (autoGroups) among entries and // the models of the providers switched off: one switched off for a while // stays in the group, a member the gateway skips (membersIn), rather than diff --git a/internal/provider/groupskip_test.go b/internal/provider/groupskip_test.go index 52051dd56..4e65f8814 100644 --- a/internal/provider/groupskip_test.go +++ b/internal/provider/groupskip_test.go @@ -67,8 +67,13 @@ func TestAutoGroupSkip(t *testing.T) { if _, ok := AutoStandIn("group/auto-kimi-k3"); ok { t.Fatal("a stand-in for a group there is") } - if _, ok := AutoStandIn("group/auto-y"); ok { - t.Fatal("a stand-in for a group that was never found") + // y, b's alone, is no group: its group's id still reaches it there, as + // a group gone for want of a second provider does (canlee886 on X) + if got, ok := AutoStandIn("group/auto-y"); !ok || got != "b/y" { + t.Fatalf("group/auto-y: %q %v, want b/y", got, ok) + } + if _, ok := AutoStandIn("group/auto-nothing"); ok { + t.Fatal("a stand-in for a model no provider serves") } // kept out under its new id if err := Rename("b", "bee"); err != nil {