Files
magpie/internal/gateway/ordered_accounts_back_test.go
T
yetone 82ca554be9 routing: a provider's accounts routed In order give a conversation back to the first
CryptoSarom on X: with a provider's accounts set to In order, a
conversation that had moved to the second account (the first was
resting) stayed on the second once the first was ready again. Only a
group's members went back to the first; a provider's own accounts never
did, because inOrder was read from the group's routing alone.

- gateway.go: inOrder comes from the provider's routing, or the group's
  when the request is to a group.
- affinity.go: backToFirst compares accounts (who()) for a provider's
  own accounts. For a provider it overrides only a cache that would have
  kept the conversation (cacheHolds: worth keeping and still warm), so a
  cold or empty cache keeps its own "cold"/"new" label.
- plugin_factory_cap_test.go: "auto, cached on B" pinned the old
  stay-on-B behaviour; it now expects A, the first account, on purpose.
- GUI: the Routing affinity line for "order" without a group says the
  provider goes in order and the first is ready again (all languages).
- docs/subsystems/gateway-routing.md: the In order bullet covers a
  provider's own accounts.

Run on mote at this tree on aa3d11d5b; rebased onto c6702f462, which changes only context.css and its own GUI test: gofmt -l clean,
GOOS=linux and GOOS=windows go vet ./internal/gateway ok, go test
./internal/gateway -count=1 -timeout 45m ok (985s).
GUI: routing-affinity-order (new accounts case) and i18n ok in both
engines. i18n-keys "a plugin's words are its own" is red from three
strings not in this change, filed as #5332, #5426, #5482.

Broken by hand: inOrder := false turns
TestOrderedAccountsGiveTheConversationToTheFirst red (k1 k1 k1 k1, why
"cache"). Without cacheHolds TestForeignSealedItemsAfterAColdSwitch went
red (why "order" for a cold switch).

Not tried: a real account pair in the live app.
2026-11-09 05:42:44 +08:00

84 lines
3.1 KiB
Go

package gateway
import (
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A provider's keys or accounts routed In order give a running
// conversation to the one the user put first at its next turn, as a group
// routed In order gives it to its first member (backToFirst): CryptoSarom
// on X made a new Codex account First, In order, and the conversation
// running on the old account stayed there, kept by its cache, until a 90%
// cap on the old one moved it. Within a turn it stays where the turn's
// reasoning was sealed; Smart, or Stays: Session, keep it where it is.
func TestOrderedAccountsGiveTheConversationToTheFirst(t *testing.T) {
for _, c := range []struct {
name, routing, mode string
want string // who answers turn 1, turn 2's two requests, turn 3
why string // the affinity of turn 2's first request
}{
{"in order", provider.Ordered, "", "k1 k2 k2 k2", "order"},
{"in order, Stays: Session", provider.Ordered, provider.AffinitySession, "k1 k1 k1 k1", "session"},
{"smart", "", "", "k1 k1 k1 k1", "cache"},
} {
t.Run(c.name, func(t *testing.T) {
fresh(t)
up := &cacheWriter{wrote: 3000}
srv := httptest.NewServer(up)
t.Cleanup(srv.Close)
save := func(first, second string) {
t.Helper()
p := provider.Provider{ID: "ord", Name: "Ord", Key: first, Keys: []provider.KeyAccount{{Key: second}},
Models: []string{"m1"}, Anthropic: srv.URL + "/v1", Routing: c.routing, Affinity: c.mode}
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
forgetRouting()
}
save("k1", "k2")
s := New()
t.Cleanup(s.subscription.abortAll)
conv := `{"role":"user","content":"fix the bug"}`
var got []string
for i := range 4 {
if i == 1 {
save("k2", "k1") // the user makes k2 First
}
body := `{"model":"ord/m1","max_tokens":100,"tools":[{"name":"read","input_schema":{"type":"object"}}],"messages":[` + conv + `]}`
r := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body))
r.Header.Set("x-claude-code-session-id", "s-1")
rec := httptest.NewRecorder()
s.Handler().ServeHTTP(rec, r)
if rec.Code != 200 {
t.Fatalf("request %d: %d %s", i, rec.Code, rec.Body)
}
out := rec.Body.String()
switch {
case strings.Contains(out, "from k1"):
got = append(got, "k1")
case strings.Contains(out, "from k2"):
got = append(got, "k2")
default:
t.Fatalf("request %d answered by nobody known: %s", i, out)
}
if a := s.trace.routes[len(s.trace.routes)-1].Affinity; i == 1 && (a == nil || a.Why != c.why) {
t.Errorf("turn 2's first request: affinity %+v, want why %q", a, c.why)
}
if i == 1 {
// a tool loop: the turn goes on
conv += `,{"role":"assistant","content":[{"type":"tool_use","id":"t1","name":"read","input":{}}]},{"role":"user","content":[{"type":"tool_result","tool_use_id":"t1","content":"ok"}]}`
} else {
conv += `,{"role":"assistant","content":"done"},{"role":"user","content":"and more"}`
}
}
if g := strings.Join(got, " "); g != c.want {
t.Fatalf("answered by %s, want %s", g, c.want)
}
})
}
}