Files
magpie/internal/gateway/rules_time_test.go
T
yetone 2f40cf4280 gateway: an In order group gives a conversation back to its first member at the next turn
yyhhyyyyyy on Discord made an Opus 5.5 group In order: an API key first,
a Claude subscription second, and 8 of 10 Claude Code requests went to
the subscription. With both healthy the plan is in order and every
request goes to the key. The cause is affinity: one failure of the key
(a 429, a 5xx, a rest) sent a conversation to the subscription, and Auto
affinity's "cache" kept it there at every turn after, since each turn
reads what the last wrote, while the key stood first and ready. A
provider's fallback already gave a conversation back (backToMain, "main");
a group's later member never did.

Now, in a group routed In order with Stays left on Auto, a new turn goes
back to the first ready member when that is another member than the one
that answered last (backToFirst, Affinity.Why "order"). Another key or
account of the same member, or a group in the group, keeps its own
affinity. Within a turn the conversation stays, on the reasoning the
later member sealed, as with "main". Stays: Session and Strict are the
user's own setting and keep it where it is. Smart and the other routings
keep it by its cache as before.

The Routing page says why it moved, for "order" and for a fallback's
"main", which showed no reason at all before (affWhy), in en, zh, zh-TW,
ja and de.

What was run (at the rebased head unless said):
- go test ./internal/gateway -run 'TestOrderedGroupGivesTheConversationBackToItsFirstMember|TestGroupMemberKeepsTheConversationOnceAnotherIsBack|TestFallbackGivesTheConversation|ChatGPTCold|LoginSwitch|LoneAllowance|Affinity' on mote: ok.
- gofmt -l: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./..., go build, GOOS=linux go build -tags nogui,
  GOOS=windows go build on mote, before the rebase: all ok.
- GUI in WSL magpie-desktop, Chromium and WebKit, 1100 and 420 wide,
  every language: routing-affinity-order, routing-affinity-cache-write and
  i18n-keys 44 of 44 before the rebase; routing-affinity-order and
  i18n-keys again at the rebased head.

Broken by hand:
- the "order" case turned off (old behaviour): the new Go test fails,
  "answered by ssssssssss, want ssskkkkkkk" and why "cache", want "order".
- the Within and Session guards removed: "the key failed once" fails with
  skkkkkkkkk, and "Stays: Session" with skkkkkkkkk, want ssssssssss.
- client.js put back: routing-affinity-order 0 of 10 in Chromium
  ("order: goes in order not in").

Not tried: the reporter's own key and subscription (the subscription is
the test's fake Claude Code); that a failure of the key is what moved the
reporter's sessions is the likeliest cause the code shows, not confirmed
from their trace.

Full suite: go test -tags nogui -timeout 45m ./... on mote at the
pre-rebase head: 80 packages ok, two red. TestRuleTimeWindowAtTurnStart
("a 5 b 2") was this change: its group is In order, so turn 2 of "sess",
after the rule's hours, now goes back to a; the test now asserts that
turn's answerer and counts a 5 b 2. TestNoCommandBypassesProc
(internal/claudetui/child_unix.go:32) is red on main, #4491. At the
rebased head, go test ./internal/gateway -run
'Rule|Affin|Order|Group|Stay|Sticky|Cache|Turn|Fallback|Main|Cold|Login|Lone'
on mote: ok (107s).
2026-10-31 10:30:50 +08:00

118 lines
5.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gateway
import (
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// A rule's hours are looked at as a turn begins, on the clock then: a turn
// begun inside them goes to its member and stays there for its tool
// rounds after they end; a turn begun outside them isn't the rule's (where
// the same conversation goes then is the group's Stays to say).
func TestRuleTimeWindowAtTurnStart(t *testing.T) {
now := time.Date(2026, 9, 29, 10, 0, 0, 0, time.Local) // a Tuesday
ruleClock = func() time.Time { return now }
t.Cleanup(func() { ruleClock = time.Now })
s, a, b := ruled(t, provider.Rule{Use: "b/big", Time: &provider.TimeWindow{From: "09:00", To: "18:00", Days: []string{"mon", "tue", "wed", "thu", "fri"}}})
post := func(body string, session string) (string, Route) {
t.Helper()
code, out := postAs(t, s, session, body)
if code != 200 {
t.Fatalf("%d %s", code, out)
}
return out, lastRoute(s)
}
// turn 1 begins inside the hours: the rule's member, the trace says why
out, r := post(chat("hello", nil, 0, ""), "sess")
if !strings.Contains(out, "from kb") || r.Rule == nil || r.Rule.N != 1 || r.Rule.Use != "b/big" || strings.Join(r.Rule.When, " · ") != "time 09:00–18:00 Mon–Fri" {
t.Fatalf("turn 1: %s %+v", out, r.Rule)
}
// its tool rounds after the hours end stay where it began
now = time.Date(2026, 9, 29, 18, 30, 0, 0, time.Local)
out, r = post(chat("hello", nil, 1, ""), "sess")
if !strings.Contains(out, "from kb") || !r.Rule.Held || r.Rule.N != 1 {
t.Fatalf("turn 1 tool round: %s %+v", out, r.Rule)
}
// turn 2 begins after them: no rule, and the group goes in order, so
// the conversation goes back to its first member
if out, r = post(chat("hello", []string{"more"}, 0, ""), "sess"); !strings.Contains(out, "from ka") || r.Rule.N != 0 || r.Rule.Held {
t.Fatalf("turn 2: %s %+v", out, r.Rule)
}
// nor another conversation's: the group's order
if out, r = post(chat("hi", nil, 0, ""), "s2"); !strings.Contains(out, "from ka") || r.Rule.N != 0 {
t.Fatalf("after hours: %s %+v", out, r.Rule)
}
// a turn begun before them stays off the rule when they open mid-turn
now = time.Date(2026, 9, 30, 8, 59, 0, 0, time.Local)
if out, r = post(chat("hello", nil, 0, ""), "s3"); !strings.Contains(out, "from ka") || r.Rule.N != 0 {
t.Fatalf("before hours: %s %+v", out, r.Rule)
}
now = time.Date(2026, 9, 30, 9, 1, 0, 0, time.Local)
if out, r = post(chat("hello", nil, 1, ""), "s3"); !strings.Contains(out, "from ka") || !r.Rule.Held || r.Rule.N != 0 {
t.Fatalf("before hours, tool round: %s %+v", out, r.Rule)
}
// on a Saturday, the hours don't hold
now = time.Date(2026, 10, 3, 10, 0, 0, 0, time.Local)
if out, r = post(chat("hello", nil, 0, ""), "s4"); !strings.Contains(out, "from ka") || r.Rule.N != 0 {
t.Fatalf("saturday: %s %+v", out, r.Rule)
}
if a.n() != 5 || b.n() != 2 {
t.Fatalf("a %d b %d", a.n(), b.n())
}
}
// A rule's hours set in a zone hold at that zone's hours, on a server
// whose clock reads UTC (gaubeebangeel on X: magpie on a cloud server, its
// user in Beijing, the hours 8 off): the instants here are UTC, as such a
// server has them, and the zone is one hours away from this computer's.
func TestRuleTimeWindowInItsZoneAtTurnStart(t *testing.T) {
zone, loc := "", (*time.Location)(nil)
for _, name := range []string{"Asia/Shanghai", "America/New_York"} {
l, err := time.LoadLocation(name)
if err != nil {
t.Fatal(err)
}
_, a := time.Date(2026, 9, 29, 12, 0, 0, 0, l).Zone()
_, b := time.Date(2026, 9, 29, 12, 0, 0, 0, l).In(time.Local).Zone()
if d := a - b; d >= 4*3600 || d <= -4*3600 {
zone, loc = name, l
break
}
}
if loc == nil {
t.Fatal("no zone far from this computer's")
}
now := time.Date(2026, 9, 29, 10, 0, 0, 0, loc).UTC() // Tuesday 10:00 there
ruleClock = func() time.Time { return now }
t.Cleanup(func() { ruleClock = time.Now })
s, a, b := ruled(t, provider.Rule{Use: "b/big", Time: &provider.TimeWindow{From: "09:00", To: "18:00", Days: []string{"mon", "tue", "wed", "thu", "fri"}, Zone: zone}})
post := func(session string) (string, Route) {
t.Helper()
code, out := postAs(t, s, session, chat("hello", nil, 0, ""))
if code != 200 {
t.Fatalf("%d %s", code, out)
}
return out, lastRoute(s)
}
out, r := post("s1")
if !strings.Contains(out, "from kb") || r.Rule == nil || r.Rule.N != 1 || strings.Join(r.Rule.When, " · ") != "time 09:00–18:00 Mon–Fri ("+zone+")" {
t.Fatalf("10:00 in %s: %s %+v", zone, out, r.Rule)
}
// 8:30 there is outside, whatever the server's clock says
now = time.Date(2026, 9, 29, 8, 30, 0, 0, loc).UTC()
if out, r = post("s2"); !strings.Contains(out, "from ka") || r.Rule.N != 0 {
t.Fatalf("08:30 in %s: %s %+v", zone, out, r.Rule)
}
// 17:30 there is inside
now = time.Date(2026, 9, 29, 17, 30, 0, 0, loc).UTC()
if out, r = post("s3"); !strings.Contains(out, "from kb") || r.Rule.N != 1 {
t.Fatalf("17:30 in %s: %s %+v", zone, out, r.Rule)
}
if a.n() != 1 || b.n() != 2 {
t.Fatalf("a %d b %d", a.n(), b.n())
}
}