Files
magpie/internal/gateway/rules_failover_test.go
T
yetone f2b0780fa2 gateway: a held rule fails over within the member it chose, and a fallback outside it isn't kept for the turn (#1946, XcodeFish)
A group of coding-fast and coding-quality, its rule sending turns to
coding-quality: when coding-quality's member failed mid-turn the request
went to coding-fast's members before coding-quality's other one, and the
turn then stayed on coding-fast. Within a turn the rule is held, and
when affinity kept a member the rule's order wasn't applied at all
(gateway.go and nestedRules skipped `hit.Held && aff.Kept`), so the
failover order was the kept one, then the parent's own order.

ruleOver (rules.go), used at the group's rule and every nested group's:
- the kept one stays first while it is of the rule's member that goes
  first (the one it names, or a Then's while that is resting); the
  rule's other members follow it, ahead of the group's others;
- one kept outside that member, which took over when it failed, isn't
  held: once a member of the rule's target is out of its rest, the next
  request of the turn goes back to it (Affinity.Why "rule"). While all of
  it rests, the fallback stays. No new option.
docs/subsystems/gateway-routing.md says so.

Run (before the rebase onto origin/main): go test -tags nogui -count=1 ./internal/gateway/: green but for
TestKimiHeldBurstTellsNoSpeed at load average ~139 (passes 3 of 3
alone; filed as #1963, not on this path),
gofmt -l, GOOS=linux and GOOS=windows go vet -tags nogui ./internal/gateway/.
TestHeldRuleFailsOverWithinItsMember fails on the old code (tried
[kq1 kf1], the reporter's order), and with the go-back part disabled by
hand (round 3 kept on f1).

After the rebase: gofmt, both vets, and go test -run
'Rule|Group|Affin|Nested|Stick|Turn|Classif|Decide|Pause' ./internal/gateway/ green.
2026-10-13 16:18:05 +08:00

98 lines
3.5 KiB
Go

package gateway
import (
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// #1946 (XcodeFish): a group of two groups, coding-fast and
// coding-quality, its rule sending every turn to coding-quality. When
// coding-quality's member failed mid-turn, the request went to coding-fast
// before coding-quality's other member, and the turn stayed there: the
// rule held for the turn, the one the turn stayed on was kept whoever it
// was, and the rule's order wasn't applied at all.
func TestHeldRuleFailsOverWithinItsMember(t *testing.T) {
fresh(t)
k := &keyed{fail: map[string]int{}}
for _, id := range []string{"f1", "f2", "q1", "q2"} {
serveOn(t, id, "k"+id, []string{"m"}, k)
}
for _, g := range []provider.Group{
{Name: "Coding Fast", Members: []string{"f1/m", "f2/m"}, Routing: provider.Ordered},
{Name: "Coding Quality", Members: []string{"q1/m", "q2/m"}, Routing: provider.Ordered},
{Name: "Coding", Members: []string{"group/coding-fast", "group/coding-quality"}, Routing: provider.Ordered,
Rules: []provider.Rule{{Use: "group/coding-quality", Tokens: 1}}},
} {
if err := provider.SaveGroup(g); err != nil {
t.Fatal(err)
}
}
s := New()
body := func(rounds int) string {
ms := []string{`{"role":"user","content":"fix the bug"}`}
for range rounds {
ms = append(ms, `{"role":"assistant","content":null,"tool_calls":[{"id":"c","type":"function","function":{"name":"ls","arguments":"{}"}}]}`,
`{"role":"tool","tool_call_id":"c","content":"file.txt"}`)
}
return `{"model":"group/coding","messages":[` + strings.Join(ms, ",") + `]}`
}
send := func(session string, rounds int, want string) Route {
t.Helper()
k.tried = nil
code, out := postAs(t, s, session, body(rounds))
r := lastRoute(s)
if code != 200 || !strings.Contains(out, "from k"+want) {
t.Fatalf("%s round %d: %d %s, tried %v, affinity %+v", session, rounds, code, out, k.tried, r.Affinity)
}
return r
}
order := func(r Route) string {
var out []string
for _, w := range r.Order {
out = append(out, w.Provider)
}
return strings.Join(out, " ")
}
// the turn begins on coding-quality
if r := send("one", 0, "q1"); order(r) != "q1 q2 f1 f2" {
t.Fatalf("turn start: %s", order(r))
}
// q1 fails in a tool round: q2, coding-quality's other, takes it
k.fail["kq1"] = 500
r := send("one", 1, "q2")
if strings.Join(k.tried, " ") != "kq1 kq2" || !r.Rule.Held || r.Rule.Unready {
t.Fatalf("failover: tried %v order %s rule %+v", k.tried, order(r), r.Rule)
}
// q2 is kept for the turn, q1 back or not: it is of the rule's member
delete(k.fail, "kq1")
s.UnrestAll("q1")
if r := send("one", 2, "q2"); !r.Affinity.Kept || r.Affinity.Why != "turn" || order(r) != "q2 q1 f1 f2" {
t.Fatalf("kept within the member: %s %+v", order(r), r.Affinity)
}
// coding-quality failing whole, coding-fast answers the round
send("two", 0, "q1")
k.fail["kq1"], k.fail["kq2"] = 500, 500
send("two", 1, "f1")
if strings.Join(k.tried, " ") != "kq1 kq2 kf1" {
t.Fatalf("tried %v", k.tried)
}
// both resting, the turn stays on f1
delete(k.fail, "kq1")
delete(k.fail, "kq2")
if r := send("two", 2, "f1"); !r.Affinity.Kept {
t.Fatalf("resting: %s %+v", order(r), r.Affinity)
}
// their rests over, the turn goes back to coding-quality, not kept on
// the fallback
s.UnrestAll("q1")
s.UnrestAll("q2")
r = send("two", 3, "q1")
if r.Affinity.Kept || r.Affinity.Why != "rule" || order(r) != "q1 q2 f1 f2" || !r.Rule.Held {
t.Fatalf("back to the rule's member: %s %+v %+v", order(r), r.Affinity, r.Rule)
}
}