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.
This commit is contained in:
yetone
2026-11-12 12:28:56 +08:00
parent 3297243184
commit 04e38e6045
9 changed files with 69 additions and 22 deletions
+1 -1
View File
@@ -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
+1
View File
@@ -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)
+1
View File
@@ -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)
+13 -1
View File
@@ -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
}
+1
View File
@@ -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)
+2
View File
@@ -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()
+25 -5
View File
@@ -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()
+1 -1
View File
@@ -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
}
+24 -14
View File
@@ -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
}