gateway: an inner group's own Patience waits out its busy members through an outer group (#2410)
serve read Patience only from the group the request named (g.Waits()). planLevel stamped a nested group's FirstToken (#2246) on its candidates but not its Patience, so an inner group's budget was never read through an outer one: an inner 4 min under an outer default gave up at 1 min, and an inner Off was still asked again for 60s. Each candidate carries the innermost Patience that is set on its way down (innerPatience, as innerFirstToken). waitsOf reads it as Group.Waits does, falling back to the asked group's when unset; PatienceOff keeps the member out of replan. replan counts each member's budget from waitFrom and asks again only those with budget left past the pause; the hold on the last failure lasts while any member's may (maxWaits), and the trace's Patience is the member's own. With no nested Patience set, every member's budget is g.Waits(), as before. Patch by ump45nose (61b1c17c); ggbdpq checked it against main on the issue. Review changes: the docs sentence was placed by hand (the docs hunk no longer applied), and the tests' budgets moved from inner 8s / outer 3s to 10s / 4s, so every round's pause ends 2s or more from a budget line (it was 1s from the 3s line), as a timing test leaves room on both sides. Run: - With fallback.go, gateway.go, patience.go and patience_test.go put back to main: TestNestedGroupPatienceThroughOuter FAIL ("a.n=2, want 3 — the inner group's 10s budget was not read"), TestNestedGroupPatienceInnerOff FAIL ("code=529 a.n=13 (want 529, 1 — no replan)"). With replan reading only the fallback budget: ThroughOuter FAIL the same. Restored: ok. - TestNestedGroupPatienceFallsBackToOuter, TestGroupWaitsOut*, TestPatienceIsUsedUp: ok. - Full suite, go test -tags nogui -timeout 45m ./... (set -o pipefail, sandbox HOME, load ~350) at 0224e826f (these four commits onebf3f48f7) plus #2545's patch: every package ok except internal/agent, whose one red (TestRekeyGivesWSLAgentTheSharingKey) is #2545's, which was dropped (problems posted on #2545). internal/agent alone at 0224e826f: ok. Rebased ondccfefdf7(#2635, internal/library only): build, vet, internal/library ok. gofmt -l clean; go vet darwin (gui), GOOS=linux and GOOS=windows -tags nogui: clean. GUI (i18n-keys, gui-de/ja/zh-TW, import-*, key-import, login-import): all pass except login-import's Chromium case, which fails the same alone on origin/mainebf3f48f7(#1307, output posted there). Co-authored-by: ump45nose <ump45nose@users.noreply.github.com>
This commit is contained in:
committed by
yetone
co-authored by
ump45nose
parent
08400f4ebd
commit
ae98cda651
File diff suppressed because one or more lines are too long
@@ -63,6 +63,11 @@ type candidate struct {
|
||||
// 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
|
||||
// patience is the Patience of the innermost group of those the member
|
||||
// is in that sets one (#2410): a group in a group waits out its own
|
||||
// members' busy failures, the group asked for only those that set none;
|
||||
// 0 falls back to the asked group's Waits, PatienceOff skips replan
|
||||
patience int
|
||||
}
|
||||
|
||||
// capHold is how an account is held at its usage cap: the cap of the
|
||||
@@ -406,6 +411,18 @@ func innerFirstToken(m provider.Member) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
// innerPatience is the Patience of the innermost group m is in (m.Via,
|
||||
// the outermost first) that sets one (#2410), 0 for none: PatienceOff
|
||||
// (-1) counts as set, so an inner Off stops replan for that member.
|
||||
func innerPatience(m provider.Member) int {
|
||||
for i := len(m.Via) - 1; i >= 0; i-- {
|
||||
if m.Via[i].Patience != 0 {
|
||||
return m.Via[i].Patience
|
||||
}
|
||||
}
|
||||
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.
|
||||
@@ -418,6 +435,7 @@ func planLevel(g provider.Group, ms []provider.Member, depth int, from provider.
|
||||
for i := range l {
|
||||
l[i].effort, l[i].fast = m.Effort, m.Fast
|
||||
l[i].firstToken = innerFirstToken(m)
|
||||
l[i].patience = innerPatience(m)
|
||||
}
|
||||
}
|
||||
pl.left = append(pl.left, barredOf(barred, m.Provider, false, from, m.Groups())...)
|
||||
|
||||
+22
-14
@@ -2251,10 +2251,12 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
|
||||
}
|
||||
gaveWay := map[string]bool{} // keys and accounts that gave way, full, to one free (laneMate)
|
||||
// a group's members that failed in a way that passes are asked again,
|
||||
// once each has been, for up to its Patience (#1418)
|
||||
var patience time.Duration
|
||||
// once each has been, for up to its Patience (#1418): a member of a
|
||||
// group in the group uses the innermost that sets Patience (#2410)
|
||||
var patience, patienceFallback time.Duration
|
||||
if isGroup {
|
||||
patience = g.Waits()
|
||||
patienceFallback = g.Waits()
|
||||
patience = maxWaits(cands, patienceFallback)
|
||||
}
|
||||
var busyNow []busyTry // the members of this round that failed so
|
||||
rounds := 0 // the rounds they were asked again in
|
||||
@@ -3133,32 +3135,38 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
|
||||
try.Fail, try.Rest = rest.Why, &rest
|
||||
s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try })
|
||||
skipped = append(skipped, c.label()+": "+call.Error)
|
||||
if patience > 0 && !hw.turnedAway && busy(hw.code(), hw.errBody()) {
|
||||
if waitsOf(c.patience, patienceFallback) > 0 && !hw.turnedAway && busy(hw.code(), hw.errBody()) {
|
||||
busyNow = noteBusy(busyNow, c, hw.header)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if _, same := passing(hw.code(), hw.header, hw.errBody(), again); last && patience > 0 && !protected && !hw.turnedAway && !hw.refused && hw.failed() && busy(hw.code(), hw.errBody()) &&
|
||||
(len(busyNow) > 0 || !same) {
|
||||
(len(busyNow) > 0 || waitsOf(c.patience, patienceFallback) > 0 && !same) {
|
||||
// every member has failed, each in a way that passes — busy,
|
||||
// overloaded, rate limited for a moment — with nothing of a
|
||||
// reply sent: after a pause, longer each round and no shorter
|
||||
// than a Retry-After, those back by then are asked again in
|
||||
// turn, while the group's Patience for the request lasts
|
||||
// turn, while each member's Patience for the request lasts
|
||||
// (#1418: Codex's turn ended on server_is_overloaded, to be
|
||||
// told "continue" by hand). Another member busy too, this one
|
||||
// isn't asked again alone first, as the last one left is: they
|
||||
// take turns. The patience is counted from the first time
|
||||
// every member had failed, when the agent would have had the
|
||||
// error
|
||||
// told "continue" by hand; #2410: a nested group's own).
|
||||
// Another member busy too, this one isn't asked again alone
|
||||
// first, as the last one left is: they take turns. The
|
||||
// patience is counted from the first time every member had
|
||||
// failed, when the agent would have had the error
|
||||
if waitFrom.IsZero() {
|
||||
waitFrom = time.Now()
|
||||
}
|
||||
busyNow = noteBusy(busyNow, c, hw.header)
|
||||
if waitsOf(c.patience, patienceFallback) > 0 {
|
||||
busyNow = noteBusy(busyNow, c, hw.header)
|
||||
}
|
||||
// nor once Codex would hang up in the pause, for want of an
|
||||
// event (agentQuietMost): it has the error instead
|
||||
if next, wait, ok := replan(busyNow, rounds, waitFrom.Add(patience)); ok && !kept.tooQuiet(wait) {
|
||||
try.Fail, try.Again, try.Replan, try.Patience = failureOf(c, hw.code(), hw.errBody()), wait.Milliseconds(), len(next), int(patience/time.Second)
|
||||
memberWait := waitsOf(c.patience, patienceFallback)
|
||||
if memberWait == 0 {
|
||||
memberWait = patience
|
||||
}
|
||||
if next, wait, ok := replan(busyNow, rounds, waitFrom, patienceFallback); ok && !kept.tooQuiet(wait) {
|
||||
try.Fail, try.Again, try.Replan, try.Patience = failureOf(c, hw.code(), hw.errBody()), wait.Milliseconds(), len(next), int(memberWait/time.Second)
|
||||
s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try })
|
||||
skipped = append(skipped, c.label()+": "+call.Error)
|
||||
rounds, busyNow = rounds+1, nil
|
||||
|
||||
@@ -374,3 +374,128 @@ func TestStreamEventUnknownOnlyFrames(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A group in a group waits out busy members by its own Patience (#2410):
|
||||
// the innermost that sets one, else the group asked for — same rule as
|
||||
// FirstToken (#2246) and Sink (#2255). Before, replan always read the
|
||||
// asked group's Waits, so an inner 10s under an outer 4s gave up at 4s.
|
||||
// The pauses are 2s, 4s, 8s (retryPause 1s); each budget is 2s or more
|
||||
// from where a pause ends, so a loaded runner's slower asks don't move a
|
||||
// round across it.
|
||||
func TestNestedGroupPatienceThroughOuter(t *testing.T) {
|
||||
overloaded := reply{529, "", `{"error":{"message":"Our servers are currently overloaded. Please try again later.","code":"server_is_overloaded"}}`}
|
||||
setup := func(t *testing.T, inner, outer int) *scripted {
|
||||
t.Helper()
|
||||
fresh(t)
|
||||
k := retryPause
|
||||
retryPause = time.Second
|
||||
t.Cleanup(func() { retryPause = k })
|
||||
a := &scripted{replies: []reply{overloaded, overloaded, overloaded, overloaded}}
|
||||
b := &scripted{replies: []reply{overloaded, overloaded, overloaded, overloaded}}
|
||||
scriptedOn(t, "a", provider.Chat, a)
|
||||
scriptedOn(t, "b", provider.Chat, b)
|
||||
if err := provider.SaveGroup(provider.Group{Name: "In", Members: []string{"a/m", "b/m"}, Routing: provider.Ordered, Patience: inner}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"group/in"}, Routing: provider.Ordered, Patience: outer}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return a
|
||||
}
|
||||
ask := `{"model":"group/g","messages":[{"role":"user","content":"hi"}]}`
|
||||
|
||||
// inner 10s under outer 4s: the inner budget allows a second replan
|
||||
// (2s+4s=6s < 10s, a third's 14s is past it); the outer's 4s would
|
||||
// have stopped after one (6s > 4s).
|
||||
a := setup(t, 10, 4)
|
||||
s := New()
|
||||
code, _ := postAs(t, s, "", ask)
|
||||
if a.n != 3 {
|
||||
t.Fatalf("asks: a.n=%d, want 3 — the inner group's 10s budget was not read", a.n)
|
||||
}
|
||||
if code != 529 {
|
||||
t.Fatalf("code=%d", code)
|
||||
}
|
||||
r := s.trace.routes[len(s.trace.routes)-1]
|
||||
var got int
|
||||
for _, try := range r.Tries {
|
||||
if try.Replan > 0 {
|
||||
got = try.Patience
|
||||
}
|
||||
}
|
||||
if got != 10 {
|
||||
t.Fatalf("trace patience=%d, want 10 (inner): %+v", got, r.Tries)
|
||||
}
|
||||
|
||||
// direct to the inner group: same 10s budget, three asks
|
||||
fresh(t)
|
||||
k := retryPause
|
||||
retryPause = time.Second
|
||||
t.Cleanup(func() { retryPause = k })
|
||||
a = &scripted{replies: []reply{overloaded, overloaded, overloaded, overloaded}}
|
||||
b := &scripted{replies: []reply{overloaded, overloaded, overloaded, overloaded}}
|
||||
scriptedOn(t, "a", provider.Chat, a)
|
||||
scriptedOn(t, "b", provider.Chat, b)
|
||||
if err := provider.SaveGroup(provider.Group{Name: "In", Members: []string{"a/m", "b/m"}, Routing: provider.Ordered, Patience: 10}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
code, _ = postAs(t, New(), "", `{"model":"group/in","messages":[{"role":"user","content":"hi"}]}`)
|
||||
if a.n != 3 || code != 529 {
|
||||
t.Fatalf("direct inner: a.n=%d code=%d", a.n, code)
|
||||
}
|
||||
}
|
||||
|
||||
// An inner PatienceOff under an outer default must not replan (#2410):
|
||||
// the inner Off is set, so it wins over the outer's 60s.
|
||||
func TestNestedGroupPatienceInnerOff(t *testing.T) {
|
||||
fresh(t)
|
||||
overloaded := reply{529, "", `{"error":{"message":"Our servers are currently overloaded. Please try again later.","code":"server_is_overloaded"}}`}
|
||||
a := &scripted{replies: []reply{overloaded, overloaded}}
|
||||
b := &scripted{replies: []reply{overloaded, overloaded}}
|
||||
scriptedOn(t, "a", provider.Chat, a)
|
||||
scriptedOn(t, "b", provider.Chat, b)
|
||||
if err := provider.SaveGroup(provider.Group{Name: "In", Members: []string{"a/m", "b/m"}, Routing: provider.Ordered, Patience: provider.PatienceOff}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"group/in"}, Routing: provider.Ordered}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
code, _ := postAs(t, New(), "", `{"model":"group/g","messages":[{"role":"user","content":"hi"}]}`)
|
||||
if code != 529 || a.n != 1 {
|
||||
t.Fatalf("inner Off through outer: code=%d a.n=%d (want 529, 1 — no replan)", code, a.n)
|
||||
}
|
||||
}
|
||||
|
||||
// An inner that leaves Patience unset falls back to the asked group's (#2410).
|
||||
func TestNestedGroupPatienceFallsBackToOuter(t *testing.T) {
|
||||
fresh(t)
|
||||
k := retryPause
|
||||
retryPause = time.Second
|
||||
t.Cleanup(func() { retryPause = k })
|
||||
overloaded := reply{529, "", `{"error":{"message":"Our servers are currently overloaded. Please try again later.","code":"server_is_overloaded"}}`}
|
||||
a := &scripted{replies: []reply{overloaded, overloaded, overloaded, overloaded}}
|
||||
b := &scripted{replies: []reply{overloaded, overloaded, overloaded, overloaded}}
|
||||
scriptedOn(t, "a", provider.Chat, a)
|
||||
scriptedOn(t, "b", provider.Chat, b)
|
||||
if err := provider.SaveGroup(provider.Group{Name: "In", Members: []string{"a/m", "b/m"}, Routing: provider.Ordered}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"group/in"}, Routing: provider.Ordered, Patience: 4}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := New()
|
||||
code, _ := postAs(t, s, "", `{"model":"group/g","messages":[{"role":"user","content":"hi"}]}`)
|
||||
if a.n != 2 || code != 529 {
|
||||
t.Fatalf("unset inner, outer 4s: a.n=%d code=%d (want 2, 529)", a.n, code)
|
||||
}
|
||||
r := s.trace.routes[len(s.trace.routes)-1]
|
||||
var got int
|
||||
for _, try := range r.Tries {
|
||||
if try.Replan > 0 {
|
||||
got = try.Patience
|
||||
}
|
||||
}
|
||||
if got != 4 {
|
||||
t.Fatalf("trace patience=%d, want 4 (outer fallback): %+v", got, r.Tries)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,11 +66,38 @@ func noteBusy(list []busyTry, c candidate, header http.Header) []busyTry {
|
||||
return append(list, busyTry{c: c, ready: now.Add(retryAfter(header, now))})
|
||||
}
|
||||
|
||||
// waitsOf is how long a member may be kept for replan: its own Patience
|
||||
// seconds when set (including PatienceOff → 0), else the asked group's
|
||||
// Waits (fallback).
|
||||
func waitsOf(patience int, fallback time.Duration) time.Duration {
|
||||
switch {
|
||||
case patience < 0:
|
||||
return 0
|
||||
case patience == 0:
|
||||
return fallback
|
||||
}
|
||||
return time.Duration(min(patience, provider.PatienceLongest)) * time.Second
|
||||
}
|
||||
|
||||
// maxWaits is the longest waitsOf among cands, for holding the last
|
||||
// failure while any member's budget may still cover a replan (#2410).
|
||||
func maxWaits(cands []candidate, fallback time.Duration) time.Duration {
|
||||
var m time.Duration
|
||||
for _, c := range cands {
|
||||
if w := waitsOf(c.patience, fallback); w > m {
|
||||
m = w
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// replan is, of the members that failed so in this round, those to ask
|
||||
// again and the pause before: retryPause doubled each round up to
|
||||
// replanLongest, and as long as the soonest Retry-After. ok is false when
|
||||
// the rounds are spent, or the pause would end past by.
|
||||
func replan(list []busyTry, round int, by time.Time) ([]candidate, time.Duration, bool) {
|
||||
// replanLongest, and as long as the soonest Retry-After. Each member's
|
||||
// own Patience budget (#2410) is counted from waitFrom; a member whose
|
||||
// budget is spent, or Off, is left out. ok is false when the rounds are
|
||||
// spent, or no member still has budget covering the pause.
|
||||
func replan(list []busyTry, round int, waitFrom time.Time, fallback time.Duration) ([]candidate, time.Duration, bool) {
|
||||
if len(list) == 0 || round >= replanRounds {
|
||||
return nil, 0, false
|
||||
}
|
||||
@@ -82,11 +109,22 @@ func replan(list []busyTry, round int, by time.Time) ([]candidate, time.Duration
|
||||
}
|
||||
wait = max(wait, soonest.Sub(now))
|
||||
at := now.Add(wait)
|
||||
if !at.Before(by) {
|
||||
var alive []busyTry
|
||||
for _, b := range list {
|
||||
budget := waitsOf(b.c.patience, fallback)
|
||||
if budget <= 0 {
|
||||
continue
|
||||
}
|
||||
if !at.Before(waitFrom.Add(budget)) {
|
||||
continue // this member's budget is spent
|
||||
}
|
||||
alive = append(alive, b)
|
||||
}
|
||||
if len(alive) == 0 {
|
||||
return nil, 0, false
|
||||
}
|
||||
var next, later []candidate
|
||||
for _, b := range list {
|
||||
for _, b := range alive {
|
||||
if b.ready.After(at) {
|
||||
later = append(later, b.c) // still asking for time: after those back
|
||||
continue
|
||||
|
||||
@@ -22,7 +22,7 @@ func TestPatienceIsUsedUp(t *testing.T) {
|
||||
rounds := 0
|
||||
for {
|
||||
list := []busyTry{{c: candidate{p: provider.Provider{ID: "a"}, model: "m"}, ready: time.Now()}}
|
||||
_, wait, ok := replan(list, rounds, time.Now().Add(patience-waited))
|
||||
_, wait, ok := replan(list, rounds, time.Now().Add(-waited), patience)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user