gateway: a nested group's own firstToken times its seats, not the outer group's (#2246, Gaudi-XIX)

Each candidate now carries the firstToken of the innermost group that sets one; a candidate with none falls back to the requested group's value. TestNestedGroupFirstToken.

Run at this head (on 6485056e1, with the #2245/#2246/#2247 fixes together): gofmt -l clean; go vet ./... for darwin and windows, and linux -tags nogui; the full go test ./... in a sandbox HOME, all green. The new test fails on the old code and passes with the fix.
This commit is contained in:
yetone
2026-10-15 04:46:57 +08:00
parent 8ef14be3e6
commit 91c4705e39
3 changed files with 63 additions and 3 deletions
+16
View File
@@ -58,6 +58,10 @@ type candidate struct {
// it is in (weightedUnits), for affinity to charge the member that
// keeps a conversation (weightedKept)
seats []wrrSeat
// firstToken is the FirstToken of the innermost group of those the
// member is in that sets one (#2246): a group in a group times its own
// members' first tokens, the group asked for only those that set none
firstToken int
}
// capHold is how an account is held at its usage cap: the cap of the
@@ -387,6 +391,17 @@ func (s *Server) planGroup(g provider.Group, ms []provider.Member, from provider
return restLast(out, pl)
}
// innerFirstToken is the FirstToken of the innermost group m is in
// (m.Via, the outermost first) that sets one, 0 for none.
func innerFirstToken(m provider.Member) int {
for i := len(m.Via) - 1; i >= 0; i-- {
if m.Via[i].FirstToken > 0 {
return m.Via[i].FirstToken
}
}
return 0
}
// planLevel orders the models ms of g, a group depth groups down from the
// one asked for, adding to pl's order as it goes: those set aside and
// those unlisted it gathers for planGroup to put last.
@@ -398,6 +413,7 @@ func planLevel(g provider.Group, ms []provider.Member, depth int, from provider.
for _, l := range [][]candidate{cs, aside, left, barred} {
for i := range l {
l[i].effort, l[i].fast = m.Effort, m.Fast
l[i].firstToken = innerFirstToken(m)
}
}
pl.left = append(pl.left, barredOf(barred, m.Provider, false, from, m.Groups())...)
+41
View File
@@ -101,3 +101,44 @@ func TestGroupFirstTokenFailsOver(t *testing.T) {
t.Fatalf("in time: %d %s (b %d)", code, body, b.n)
}
}
// A group in a group times its own members' first tokens (#2246): the
// innermost group that sets a FirstToken is the one that counts, and the
// group asked for only times those under no group that sets one.
func TestNestedGroupFirstToken(t *testing.T) {
for _, c := range []struct {
name string
outer, inner int
}{
{"inner only", 0, 1},
{"inner under a longer outer", 60, 1},
{"outer only", 1, 0},
} {
t.Run(c.name, func(t *testing.T) {
fresh(t)
a := &slowStart{wait: 10 * time.Second}
b := &scripted{replies: []reply{{200, "text/event-stream", chatStreamOK}}}
slowOn(t, "a", a)
scriptedOn(t, "b", provider.Chat, b)
if err := provider.SaveGroup(provider.Group{Name: "In", Members: []string{"a/m", "b/m"}, Routing: provider.Ordered, FirstToken: c.inner}); err != nil {
t.Fatal(err)
}
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"group/in"}, Routing: provider.Ordered, FirstToken: c.outer}); err != nil {
t.Fatal(err)
}
s := New()
began := time.Now()
code, body := postAs(t, s, "", `{"model":"group/g","stream":true,"messages":[{"role":"user","content":"hi"}]}`)
if took := time.Since(began); took > 5*time.Second {
t.Fatalf("took %s", took)
}
if code != 200 || !strings.Contains(body, "from b") || a.n != 1 || b.n != 1 {
t.Fatalf("%d %s (a %d, b %d)", code, body, a.n, b.n)
}
r := s.trace.routes[len(s.trace.routes)-1]
if len(r.Tries) != 2 || r.Tries[0].Fail != failSlow || !strings.HasSuffix(r.Tries[0].Error, "no first token in 1s") {
t.Fatalf("tries: %+v", r.Tries)
}
})
}
}
+6 -3
View File
@@ -2152,9 +2152,12 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
// (#1359)
hw.loop = &loopGuard{}
}
if isGroup && g.FirstToken > 0 && !last && streams {
// a member of a group in the group is timed by the innermost
// group that sets a FirstToken (#2246), else by the group asked for
firstToken := cmp.Or(c.firstToken, g.FirstToken)
if isGroup && firstToken > 0 && !last && streams {
// slow to start, the next member is asked (Group.FirstToken)
hw.firstWait = time.Duration(g.FirstToken) * time.Second
hw.firstWait = time.Duration(firstToken) * time.Second
}
call.Provider, call.To, call.Usage = c.p.ID, "", Usage{}
model = c.model
@@ -2545,7 +2548,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
// no first content in the group's FirstToken, and nothing of
// it sent: the next member is asked, and this one doesn't
// rest, a long prompt being slow to start anywhere
call.Status, call.Error = http.StatusGatewayTimeout, fmt.Sprintf("%s: no first token in %ds", c.p.Name, g.FirstToken)
call.Status, call.Error = http.StatusGatewayTimeout, fmt.Sprintf("%s: no first token in %ds", c.p.Name, firstToken)
try.Status, try.Error, try.Fail = call.Status, call.Error, failSlow
call.TTFT, call.FirstText, call.Flow, call.FirstReasoning = 0, 0, 0, 0
telemetry.attempt(call, try, c.p.ID, sent, hw, capture)