With two Claude accounts, Claude Desktop's own Sonnet calls beside an
Opus chat (desktop-beside), and Claude Code's auto mode classifier, ran
on account B while the chat was on A. Each model's conversation is kept
under its own scope (p.ID/model), so the side call was routed as a
session of its own: leastHeld, or Smart's order once its cache was cold,
put it on the account the fewest others held, and B's 5 hours drained on
calls the user never made there.
Decided (product call delegated): one session goes on one account. The
user picked one, the vendor sees one client, and the cost is one cache
write of the side call where the session goes on reading it.
withSession (affinity.go) puts first, for a request whose session a
header names, the key or account of that session's largest conversation
under another model's scope (answered within heldFor, at least
cacheWorth kept, larger than this one's own). It acts for Why first,
gone, cold, no-cache, cache and session; never mid-turn, under Strict,
for a subagent (withLead), past a rest, reserve, credits or a fallback
routing put first, nor to an account that isn't readyForNew. The trace
says Why "lead" with LeadModel, and the Routing page says "Put on
{first}, where this session's {model} is: one session goes on one
account." in en, zh, zh-TW, ja and de. docs/subsystems/gateway-routing.md
says the same.
Not fixed here: the classifier's ~110-130K cache write per call. That one
is ours too: in Claude Code's interactive mode the bridge pastes the
classifier's transcript as one block without the client's marks
(claudetui userPieces, clientCache.pasted), so only the fixed prefix
(Claude Code's system plus the appended rules, the 68,925) is read and
the rest is written again on every call. The cause and a candidate fix
are on #5585. It changes what a real Claude Code sends and can't be
checked without a real account.
Run (mote, through mote-run), at this head (aeeeed54a's tree, origin/main
0fc3455e2 unchanged after a fresh fetch):
- go test ./internal/gateway ./internal/gui -count=1 -timeout 45m: ok (964s, 157s).
- TestSessionSide*, TestGroupAccountsHoldEveryRoute,
TestPluginFactoryStandardOnlyCaps, TestWholeSession*, TestBusyModel*,
TestInOrderKeeps* -count=2: ok.
- gofmt -l: clean. go vet ./internal/gateway for darwin, linux, windows: ok.
- node --test routing-affinity-lead (new), routing-affinity-order,
routing-affinity-cache-write, i18n-keys, i18n-en-no-cjk: 67/68
(Chromium + WebKit, 1100 and 420, en/zh/zh-TW/ja/de). The one red is
i18n-keys "a plugin's words are its own", red on main for strings this
change doesn't touch (#5327/#5332, #5426, #5482).
- The first package run of this change failed TestGroupAccountsHoldEveryRoute
and TestPluginFactoryStandardOnlyCaps/auto (both green at 0fc3455e2):
without a session header the id is the opening's hash, so two
conversations that opened with "hi" were one session, and a main with
nothing cached led. Both are excluded now, each with a subtest.
- The full go test ./... under the suite lock was queued behind four
other sessions' suites and had not started when this was pushed.
Broken by hand, each red: the withSession call removed (both new tests;
"the session's m2 call went to k2 while its chat is on k1"); the named
session check removed (no session named); the cacheWorth bar removed
(main holds no cache, and TestPluginFactoryStandardOnlyCaps).
Not tried: real Claude accounts, real Claude Desktop. A session already
split before this release moves its side call at the side call's next
turn, writing its cache once on the main's account.
Reported-by: jerrychoi
Reported-by: iamnil (Discord)
156 lines
6.6 KiB
Go
156 lines
6.6 KiB
Go
package gateway
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/routes"
|
|
)
|
|
|
|
// #5655 (jerrychoi; iamnil on Discord): two accounts, one session. Claude
|
|
// Desktop's own Sonnet calls beside an Opus chat were routed as a session
|
|
// of their own, their model's conversations apart from the chat's: put
|
|
// on the account the fewest others held, they ran on account B while the
|
|
// chat was on A, and B's 5 hours drained on calls the user never made
|
|
// there. A session's call to another model now goes to the account its
|
|
// main conversation is on (withSession).
|
|
func TestSessionSideCallGoesWithItsMainAccount(t *testing.T) {
|
|
fresh(t)
|
|
up := &keyed{}
|
|
serveOn(t, "sl", "k1", []string{"m1", "m2"}, up, "k2")
|
|
if err := provider.SetAffinity("sl", provider.AffinitySession); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
routes.Forget("sl")
|
|
s := New()
|
|
post := func(session, model, msgs string) string {
|
|
t.Helper()
|
|
n := len(up.tried)
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"sl/`+model+`","messages":[`+msgs+`]}`))
|
|
req.Header.Set("x-session-id", session)
|
|
s.Handler().ServeHTTP(rec, req)
|
|
if rec.Code != 200 || len(up.tried) != n+1 {
|
|
t.Fatalf("%s %s: %d %s, tried %v", session, model, rec.Code, rec.Body.String(), up.tried[n:])
|
|
}
|
|
return up.tried[n]
|
|
}
|
|
// another session holds the first account on m2
|
|
other := post("s0", "m2", `{"role":"user","content":"another session's call"}`)
|
|
// the session's chat, on m1
|
|
main := post("s1", "m1", `{"role":"user","content":"the chat"}`)
|
|
if main != other {
|
|
t.Fatalf("setup: the chat went to %s, the other session's call to %s; both were to be on the first", main, other)
|
|
}
|
|
// its own call on m2: on the chat's account, not the one m2's fewest
|
|
// sessions are on
|
|
if k := post("s1", "m2", `{"role":"user","content":"a call of the session's own"}`); k != main {
|
|
t.Errorf("the session's m2 call went to %s while its chat is on %s", k, main)
|
|
}
|
|
// the chat never follows its smaller side call
|
|
if k := post("s1", "m1", `{"role":"user","content":"the chat"},{"role":"assistant","content":"ok"},{"role":"user","content":"more"}`); k != main {
|
|
t.Errorf("the chat's next turn went to %s, not %s", k, main)
|
|
}
|
|
}
|
|
|
|
// A side call cached on another account than its session's main
|
|
// conversation goes to the main's at its next turn, and says so; mid-turn
|
|
// it stays on the reasoning it sealed, Strict keeps it, and a main
|
|
// conversation no larger than its own leads nothing.
|
|
func TestSessionSideCallFollowsItsMain(t *testing.T) {
|
|
key := func(k string) candidate {
|
|
return candidate{p: provider.Provider{ID: "sx", Key: k}, model: "m2", rest: "sx#" + k}
|
|
}
|
|
h := http.Header{}
|
|
h.Set("x-claude-code-session-id", "session-lead")
|
|
turn := []byte(`{"messages":[{"role":"user","content":"hi"}]}`)
|
|
within := []byte(`{"messages":[{"role":"user","content":"hi"},{"role":"assistant","content":null,"tool_calls":[{"id":"c1","type":"function","function":{"name":"f","arguments":"{}"}}]},{"role":"tool","tool_call_id":"c1","content":"ok"}]}`)
|
|
set := func(t *testing.T, side, main int) {
|
|
t.Helper()
|
|
cs := []candidate{key("k1"), key("k2")}
|
|
sticks.Lock()
|
|
sticks.m["sx/m2|session-lead"] = stick{rest: cs[1].rest, who: cs[1].who(), model: "m2", turn: 1, at: time.Now(), cacheRead: side}
|
|
sticks.m["sx/m1|session-lead"] = stick{rest: cs[0].rest, who: cs[0].who(), model: "m1", turn: 1, at: time.Now(), cacheRead: main}
|
|
sticks.Unlock()
|
|
t.Cleanup(func() {
|
|
sticks.Lock()
|
|
delete(sticks.m, "sx/m2|session-lead")
|
|
delete(sticks.m, "sx/m1|session-lead")
|
|
sticks.Unlock()
|
|
})
|
|
}
|
|
run := func(mode string, body []byte) ([]candidate, *Affinity) {
|
|
cs := []candidate{key("k1"), key("k2")}
|
|
got, _, aff, _ := affine("sx/m2", mode, false, false, false, h, provider.Chat, body, body, cs, planned{order: make([]Weighed, 2)})
|
|
return got, aff
|
|
}
|
|
t.Run("new turn", func(t *testing.T) {
|
|
set(t, 60_000, 120_000)
|
|
for _, mode := range []string{"", provider.AffinitySession} {
|
|
got, aff := run(mode, turn)
|
|
if got[0].rest != "sx#k1" || aff.Why != "lead" || aff.Kept || aff.LeadModel != "m1" {
|
|
t.Errorf("%q: first %s, why %s, kept %v, lead %q; want sx#k1 with its main", mode, got[0].rest, aff.Why, aff.Kept, aff.LeadModel)
|
|
}
|
|
}
|
|
})
|
|
t.Run("within a turn", func(t *testing.T) {
|
|
set(t, 60_000, 120_000)
|
|
if got, aff := run("", within); got[0].rest != "sx#k2" || aff.Why != "turn" {
|
|
t.Errorf("first %s, why %s; want kept on sx#k2", got[0].rest, aff.Why)
|
|
}
|
|
})
|
|
t.Run("strict", func(t *testing.T) {
|
|
set(t, 60_000, 120_000)
|
|
if got, aff := run(provider.AffinityStrict, turn); got[0].rest != "sx#k2" || aff.Why != "strict" {
|
|
t.Errorf("first %s, why %s; want kept on sx#k2", got[0].rest, aff.Why)
|
|
}
|
|
})
|
|
t.Run("main no larger", func(t *testing.T) {
|
|
set(t, 120_000, 60_000)
|
|
if got, aff := run("", turn); got[0].rest != "sx#k2" || aff.Why != "cache" {
|
|
t.Errorf("first %s, why %s; want kept on sx#k2", got[0].rest, aff.Why)
|
|
}
|
|
})
|
|
// two conversations that only open alike are not one session: with no
|
|
// session named, the id is the opening's hash
|
|
t.Run("no session named", func(t *testing.T) {
|
|
hashed := conversationID(http.Header{}, turn)
|
|
cs := []candidate{key("k1"), key("k2")}
|
|
sticks.Lock()
|
|
sticks.m["sx/m2|"+hashed] = stick{rest: cs[1].rest, who: cs[1].who(), model: "m2", turn: 1, at: time.Now(), cacheRead: 60_000}
|
|
sticks.m["sx/m1|"+hashed] = stick{rest: cs[0].rest, who: cs[0].who(), model: "m1", turn: 1, at: time.Now(), cacheRead: 120_000}
|
|
sticks.Unlock()
|
|
t.Cleanup(func() {
|
|
sticks.Lock()
|
|
delete(sticks.m, "sx/m2|"+hashed)
|
|
delete(sticks.m, "sx/m1|"+hashed)
|
|
sticks.Unlock()
|
|
})
|
|
got, _, aff, _ := affine("sx/m2", "", false, false, false, http.Header{}, provider.Chat, turn, turn, cs, planned{order: make([]Weighed, 2)})
|
|
if got[0].rest != "sx#k2" || aff.Why != "cache" {
|
|
t.Errorf("first %s, why %s; want kept on sx#k2", got[0].rest, aff.Why)
|
|
}
|
|
})
|
|
// a main conversation with no cache worth keeping goes where its own
|
|
// next turn is sent, and leads nothing
|
|
t.Run("main holds no cache", func(t *testing.T) {
|
|
set(t, 0, cacheWorth-1)
|
|
if _, aff := run("", turn); aff.Why == "lead" {
|
|
t.Errorf("why %s; a main with %d cached led", aff.Why, cacheWorth-1)
|
|
}
|
|
})
|
|
t.Run("main's account resting", func(t *testing.T) {
|
|
set(t, 60_000, 120_000)
|
|
cs := []candidate{key("k2"), key("k1")}
|
|
order := []Weighed{{}, {Rest: &Rest{}}}
|
|
got, _, aff, _ := affine("sx/m2", "", false, false, false, h, provider.Chat, turn, turn, cs, planned{order: order})
|
|
if got[0].rest != "sx#k2" || aff.Why != "cache" {
|
|
t.Errorf("first %s, why %s; want kept on sx#k2", got[0].rest, aff.Why)
|
|
}
|
|
})
|
|
}
|