Files
magpie/internal/gateway/session_lead_test.go
T
yetone 6c3f113cee routing: a rule's hours over give the conversation back; looser times (MinglunZhu on X)
MinglunZhu asked twice for a provider by time of day and said the time
setting in Routing rules is awkward. Three causes:

- A conversation a night rule sent to its member stayed there after the
  hours: no rule matched the new turn, so Auto affinity kept it on the
  night's member by its cache (Smart and every routing but In order).
  ruleFor now sets RuleHit.Ended when the last turn's rule had hours
  that are over, and affine lets the conversation go (Affinity.Why
  "hours"); the trace says so in en, zh, zh-TW, ja, de. Tool rounds of
  a turn begun inside the hours stay put; Session, Strict and In turn
  keep their own way.
- The time boxes took only ASCII "HH:MM", while their numeric keypad
  on a phone has no colon and a Chinese IME types ":". clockOf (GUI)
  and clockMinutes (CLI, saved files) now read "22", "2200", "930",
  "22:00", "22.00", "22点30" and full-width digits; "9:5" is still
  no time. time=22~08 takes ~ and ~ for the dash.
- A new rule starts with an intent box; hours took an × and a +. Adding
  another condition now drops the intent box while it is empty.

A time rule in a nested group doesn't give its conversation back yet:
#5977.

Run (mote): go test ./internal/provider (ok); go test -run
'TestRuleHoursOver|TestRuleTime|TestRule|Affinity|StandIn|Session|TestParse|Clock'
./internal/gateway (ok); go vet darwin, GOOS=linux -tags nogui,
GOOS=windows for gateway and provider; gofmt. GUI, chromium and webkit:
routing-time (8/8 after the new trace test's fix), and 28 rule/affinity/
i18n files (routing-rule-*, routing-time*, routing-intent-entry,
routing-folder, routing-pause*, routing-smart-intent, routing-save*,
routing-rules-line, routing-affinity-*, routing-behind-why,
routing-kept-again, routing-session-folder, i18n-keys, i18n-en-no-cjk):
477/481, the 4 reds the new trace test's first wording, fixed and rerun.

Broken by hand: affine's "hours" case off -> TestRuleHoursOverGiveThe
ConversationBack red (Smart stays on kb by cache); origin's
grouprule.go/ruleparse.go -> TestRuleTimeTypedLoosely red; client.js's
"hours" case and the intent give-way off -> routing-time 0/4 in
chromium; the old clockOf -> routing-time red on "9:00".

Not tried: the real app with a real night/day pair of providers; the
full gateway package and full GUI suite (the full Go suite runs after
the tag); Windows run of the touched packages (vet only).
2026-11-10 22:57:20 +08:00

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, 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, 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, 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)
}
})
}