From 04e38e604546a0a6271c87d80b8b2685bba6fb7b Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 12 Nov 2026 12:28:52 +0800 Subject: [PATCH] gateway: a reorder moves only its own provider's conversations, and tests forget it (#6249, #6262) b70563ec6 kept one process-wide "accounts reordered at" stamp. affine dropped any conversation's stick older than that stamp, so reordering Claude's accounts in the GUI cost a running Codex (or any other provider's) conversation its account and its prompt cache on the next turn. In tests, any earlier SetAccountOrder set the stamp, and later tests that backdate a stick to check an hour's cache saw it dropped: TestAffinityKeepsAnHourCachePastFiveMinutes, TestChatGPTConversationStaysPastFiveMinutes and TestClaudeConversationKeepsItsAccountsCache went red in package and subset runs (#6249, #6262, one cause). The stamp is now per provider (provider.NoteReordered(id), ReorderedAt(id)), and affine compares the stick only with the reorder of the provider whose account it holds. fresh(t) and the tests that reorder call provider.ForgetReorders. Run (mica, darwin): - go test -run 'Codex|Draw|Image|Keep|Relay|Reorder|Affinity|ChatGPTConversation|ClaudeConversation|AccountOrder|AccountRank' ./internal/gateway: ok 253s; the same run at origin/main FAILs in the three tests above. - go test ./internal/provider ok 128s; ./internal/cli ok 46s. - go vet darwin (provider, gateway, cli, gui), GOOS=windows (same), GOOS=linux -tags nogui (provider, gateway, cli): ok. gofmt -l: clean. - Broken by hand: the new case "another provider reordered keeps it" on the old affinity.go answers "k1 k2 k2, want k1 k1 k1"; green now. Not tried: the full suite, the GUI suite, a reorder with real accounts. Not changed: `magpie accounts order` (internal/cli) notes the reorder in its own process, so a running gateway never sees it and keeps the old stick until it expires; that was so before this change too. --- internal/cli/accounts_cli.go | 2 +- internal/gateway/account_order_test.go | 1 + internal/gateway/account_rank_test.go | 1 + internal/gateway/affinity.go | 14 ++++++- internal/gateway/draw_kept_login_test.go | 1 + internal/gateway/group_test.go | 2 + internal/gateway/reorder_breaks_stick_test.go | 30 ++++++++++++--- internal/gui/providers.go | 2 +- internal/provider/account_order.go | 38 ++++++++++++------- 9 files changed, 69 insertions(+), 22 deletions(-) diff --git a/internal/cli/accounts_cli.go b/internal/cli/accounts_cli.go index 2227aab50..048e7f5c0 100644 --- a/internal/cli/accounts_cli.go +++ b/internal/cli/accounts_cli.go @@ -221,7 +221,7 @@ func accountsCmd(args []string) error { if err := provider.SwitchLogin(id, args[3]); err != nil { return err } - provider.NoteReordered() + provider.NoteReordered(id) if _, plug := provider.PluginOf(id); plug || id == "gemini" || id == "antigravity" || id == "copilot" { fmt.Println(green.Render("✓"), "magpie now uses", args[3], "for", id) return nil diff --git a/internal/gateway/account_order_test.go b/internal/gateway/account_order_test.go index b24c91eee..d5ac57047 100644 --- a/internal/gateway/account_order_test.go +++ b/internal/gateway/account_order_test.go @@ -20,6 +20,7 @@ func TestArrangedKeysAreGatewayCandidates(t *testing.T) { if err := provider.SetAccountOrder(p.ID, order); err != nil { t.Fatal(err) } + t.Cleanup(provider.ForgetReorders) got, err := provider.Find(p.ID) if err != nil { t.Fatal(err) diff --git a/internal/gateway/account_rank_test.go b/internal/gateway/account_rank_test.go index 4b2c87a54..8b6f14b80 100644 --- a/internal/gateway/account_rank_test.go +++ b/internal/gateway/account_rank_test.go @@ -21,6 +21,7 @@ func TestWeighedRankIsDraggedOrder(t *testing.T) { if err := provider.SetAccountOrder(p.ID, []string{provider.KeyID("third"), provider.KeyID("first"), provider.KeyID("second")}); err != nil { t.Fatal(err) } + t.Cleanup(provider.ForgetReorders) got, err := provider.Find(p.ID) if err != nil { t.Fatal(err) diff --git a/internal/gateway/affinity.go b/internal/gateway/affinity.go index 54f1cb18a..7921d97ec 100644 --- a/internal/gateway/affinity.go +++ b/internal/gateway/affinity.go @@ -571,7 +571,7 @@ func affine(scope, mode string, rotate, spread, inOrder, hoursOver bool, in http if had && time.Since(st.at) > stickKeep { had = false } - if had && mode == "" && !a.Within && st.at.Before(provider.ReorderedAt()) { + if had && mode == "" && !a.Within && reorderedSince(st, cs) { // the user put the accounts in another order after this // conversation was last answered: the order they chose counts at // its next turn, whatever the old answerer cached (Richyisaflower @@ -1559,3 +1559,15 @@ func keptAgain(aff *Affinity, c candidate, hw *holdWriter, asked bool) (time.Dur func cacheHolds(st stick, p provider.Provider) bool { return st.cacheRead+st.cacheWrite >= cacheWorth && (time.Since(st.at) <= coldAfter(p) || st.hour.warm(time.Now())) } + +// reorderedSince: the user changed the order of the accounts of the +// provider that last answered st after it did. Only that provider's +// reorder counts: one of another provider's moves no conversation here. +func reorderedSince(st stick, cs []candidate) bool { + for _, c := range cs { + if c.who() == st.who { + return st.at.Before(provider.ReorderedAt(c.p.ID)) + } + } + return false +} diff --git a/internal/gateway/draw_kept_login_test.go b/internal/gateway/draw_kept_login_test.go index e756e8848..ade2ecdcd 100644 --- a/internal/gateway/draw_kept_login_test.go +++ b/internal/gateway/draw_kept_login_test.go @@ -53,6 +53,7 @@ func drawingFollowsThePool(t *testing.T, path string) { if err := provider.SetAccountOrder("codex", []string{"spare@example.com", "me@example.com"}); err != nil { t.Fatal(err) } + t.Cleanup(provider.ForgetReorders) for _, l := range provider.Logins("codex") { if l.Active != (l.User == "me@example.com") { t.Fatalf("kept on me, signed in as %+v", l) diff --git a/internal/gateway/group_test.go b/internal/gateway/group_test.go index 28d5c9e2d..e31046a4d 100644 --- a/internal/gateway/group_test.go +++ b/internal/gateway/group_test.go @@ -49,6 +49,8 @@ func fresh(t *testing.T) { sticks.Lock() sticks.m = map[string]stick{} sticks.Unlock() + provider.ForgetReorders() + t.Cleanup(provider.ForgetReorders) refusedSeals.Lock() refusedSeals.m = map[string]sealsRefused{} refusedSeals.Unlock() diff --git a/internal/gateway/reorder_breaks_stick_test.go b/internal/gateway/reorder_breaks_stick_test.go index 0d87c1552..f55539d17 100644 --- a/internal/gateway/reorder_breaks_stick_test.go +++ b/internal/gateway/reorder_breaks_stick_test.go @@ -17,11 +17,15 @@ func TestReorderedAccountsTakeTheConversationAtItsNextTurn(t *testing.T) { for _, c := range []struct { name, mode string reorder bool + other bool // the reorder is of another provider's accounts want string }{ - {"reordered", "", true, "k1 k2 k2"}, - {"not reordered keeps its cache", "", false, "k1 k1 k1"}, - {"session keeps it", provider.AffinitySession, true, "k1 k1 k1"}, + {"reordered", "", true, false, "k1 k2 k2"}, + {"not reordered keeps its cache", "", false, false, "k1 k1 k1"}, + {"session keeps it", provider.AffinitySession, true, false, "k1 k1 k1"}, + // a drag in another provider's pool moves no conversation of this + // one: it would only lose its cache + {"another provider reordered keeps it", "", true, true, "k1 k1 k1"}, } { t.Run(c.name, func(t *testing.T) { fresh(t) @@ -33,6 +37,11 @@ func TestReorderedAccountsTakeTheConversationAtItsNextTurn(t *testing.T) { if err := provider.Save(p); err != nil { t.Fatal(err) } + other := provider.Provider{ID: "ord2", Name: "Ord2", Key: "o1", Keys: []provider.KeyAccount{{Key: "o2"}}, + Models: []string{"m1"}, Anthropic: srv.URL + "/v1"} + if err := provider.Save(other); err != nil { + t.Fatal(err) + } forgetRouting() s := New() t.Cleanup(s.subscription.abortAll) @@ -40,8 +49,19 @@ func TestReorderedAccountsTakeTheConversationAtItsNextTurn(t *testing.T) { var got []string for i := range 3 { if i == 1 && c.reorder { - ids := p.KeyList() - if err := provider.SetAccountOrder("ord", []string{ids[1].ID, ids[0].ID}); err != nil { + id, ids := "ord", p.KeyList() + if c.other { + // k2 goes first here without a reorder being + // noted, so only the cache keeps the conversation + // on k1; then another provider's accounts are + // reordered + p.Key, p.Keys = "k2", []provider.KeyAccount{{Key: "k1"}} + if err := provider.Save(p); err != nil { + t.Fatal(err) + } + id, ids = "ord2", other.KeyList() + } + if err := provider.SetAccountOrder(id, []string{ids[1].ID, ids[0].ID}); err != nil { t.Fatal(err) } forgetRouting() diff --git a/internal/gui/providers.go b/internal/gui/providers.go index efab87f18..3718b5dfd 100644 --- a/internal/gui/providers.go +++ b/internal/gui/providers.go @@ -3040,7 +3040,7 @@ func providerRoutes(mux *http.ServeMux, w Windows) { change = func() error { err := provider.SwitchLogin(in.Agent, in.User) if err == nil { - provider.NoteReordered() + provider.NoteReordered(in.Agent) } return err } diff --git a/internal/provider/account_order.go b/internal/provider/account_order.go index 5a4f5d7b2..70a1d24a8 100644 --- a/internal/provider/account_order.go +++ b/internal/provider/account_order.go @@ -2,35 +2,45 @@ package provider import ( "fmt" - "sync/atomic" + "sync" "time" ) -var reorderedAt atomic.Int64 +// reorderedAt holds, per provider id, when the user last changed which of +// its accounts goes first: a reorder of one provider's accounts is no +// reason to move a conversation on another's (a drag in Kimi's pool cost a +// Claude conversation its hour of cache). +var reorderedAt sync.Map // provider id -> int64 unix nanos -// NoteReordered records that the user just changed which account goes -// first (a drag in the pool, Make first, the CLI), so a conversation -// last answered before it is routed anew at its next turn (the gateway's -// affine) instead of staying with the account the user moved down. -func NoteReordered() { reorderedAt.Store(time.Now().UnixNano()) } +// NoteReordered records that the user just changed which of provider id's +// accounts goes first (a drag in the pool, Make first, the CLI), so a +// conversation it last answered before that is routed anew at its next +// turn (the gateway's affine) instead of staying with the account the user +// moved down. +func NoteReordered(id string) { reorderedAt.Store(id, time.Now().UnixNano()) } -// ReorderedAt is when NoteReordered was last called in this process; the -// zero time before. -func ReorderedAt() time.Time { - n := reorderedAt.Load() - if n == 0 { +// ReorderedAt is when NoteReordered was last called for provider id in +// this process; the zero time before. +func ReorderedAt(id string) time.Time { + n, ok := reorderedAt.Load(id) + if !ok { return time.Time{} } - return time.Unix(0, n) + return time.Unix(0, n.(int64)) } +// ForgetReorders forgets every NoteReordered, for a test that reordered +// accounts: a later test's conversation, backdated past the real time of +// that reorder, would otherwise be routed anew (#6249). +func ForgetReorders() { reorderedAt.Clear() } + // SetAccountOrder changes the order used by routing, not just its presentation. // The first enabled account becomes first through the same switch used by Make // first, unless the agent is kept signed in to another (KeepLoginAs). Disabled accounts can be moved, but never silently enabled by a drag. func SetAccountOrder(id string, order []string) error { err := setAccountOrder(id, order) if err == nil { - NoteReordered() + NoteReordered(id) } return err }