From 3fbade47ab3afb1c2a1e6f581fada47e4b8f9d14 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 17 Nov 2026 20:54:13 +0800 Subject: [PATCH] gateway: a found group gone with a provider still goes to its model (canlee886) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit canlee886 on X (2122660479411855365): a Codex App thread saved on group/auto-gpt-6-1-sol failed every turn with 404 "magpie knows no model", followed by every id magpie has, after azure-fte2-sea's gpt-6.1-sol went away; other threads were fine, and switching the thread's model did not help. The user patched the thread over Codex's app-server RPC. Cause: a group magpie finds needs two providers serving the model (autoGroups). When one is removed, or its list no longer has the model, the group is gone. Reseat moves an agent's config off it, but a Codex thread keeps the model it was saved with and names it on every turn. provider.AutoStandIn sent a gone found group's id to its model only while found groups were off, or when a provider kept out of them took it away; any other way of losing the group was a hard 404, even with a provider still serving the very same model. Fix: - AutoStandInFor: with found groups on, a found group's id (group/auto-… or auto/) that is no group now, and that the user neither changed nor removed (no providers.json record), goes to its model from a provider that serves it (the agent's own subscription first, then one not kept out, then the first). It is the same model, never another; a group the user removed stays a 404. - With no provider serving the model, the request is still 404, but unknownModel says the model is gone and to pick another for the conversation (Codex: the model menu under the message box), with the three nearest ids, not every id (provider.GoneAutoGroup). - Codex's compaction on the model a thread is leaving (model_downshift) already compacts locally when that model 404s (2768f04f9); TestCodexLeavingGoneGroupCompactsLocally pins it for a gone group. - docs/subsystems/gateway-routing.md says when a found group can go and what a request for it gets. Siblings: the stand-in is in serve's model resolution, so Claude Code and every other agent saving the id are covered, as are key limits, count_tokens, budget and Codex's own-v1 path, which already ask AutoStandIn. Run (mica/mote via mica-run/mote-run, at this tree): - TestFoundGroupGoneGoesToItsModel: red on the old code (404 "magpie knows no model ... it has ..." with b still serving gpt-6.1-sol), green now. TestAutoGroupSkip's "group/auto-y" case (b's model alone) now expects the stand-in b/y; group/auto-nothing still none. - go test ./internal/provider, ./internal/gateway -run of the group/stand-in/Codex compaction tests: ok. - Full suite at 473de21fa (this change on fefe3f658's parent base) on mote, mote-run: gofmt -l nothing; GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./..., go vet ./...: ok. go test -timeout 45m -count=1 ./...: 101 packages ok, internal/gateway (1035s), agent, provider, gui, cli, budget among them. Two reds, neither touched here: internal/plugin's TestHostOnUnstartableBunSaysWhy (62s deadline, #5878) and internal/redact, whose tests passed and whose TestMain then hung 46m in testenv.RemovePrograms on stand-in warm-ups (#5267, SIGQUIT at 47m); both commented with the output. - After the rebase onto origin/main (fefe3f658, which changed internal/provider/bare.go and internal/gateway), on mica: gofmt -l internal nothing; go test ./internal/provider ./internal/gateway: ok (149s, 1004s). On mote: ./internal/plugin ./internal/redact alone: ok. - Rebased again onto 05d46f42e (gateway advisor, agy move; no file shared): go vet and go test -run "FoundGroup|Gone|AutoGroup|StandIn|Advisor|Unknown|Compact|Group" of ./internal/gateway ./internal/provider on mote: ok. Not tried: the reporter's real Codex App thread and Azure providers; the fixture removes the model from a provider's list rather than reproducing why azure-fte2-sea's list lost it. --- docs/subsystems/gateway-routing.md | 12 +++ internal/gateway/found_group_gone_test.go | 102 ++++++++++++++++++++++ internal/gateway/unknown_model.go | 12 ++- internal/provider/group.go | 54 ++++++++---- internal/provider/groupskip_test.go | 9 +- 5 files changed, 170 insertions(+), 19 deletions(-) create mode 100644 internal/gateway/found_group_gone_test.go 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 {