gateway: a rest ends at the time it shows, after the Mac slept (plugins#1126)
A rest's end came from time.Now().Add(d), which carries Go's monotonic
reading, and restOf and Retry-After compared by it. On macOS that clock
stops while the computer sleeps, so a WorkBuddy AI rest to 00:40 still
held at 09:57 the next day: 17h22m past its end, against 17h26m of
sleep in pmset's log over the same span (Xiang-Player's measurements).
onWall drops the monotonic reading. It is applied where magpie keeps a
deadline for minutes to days: the three places a rest's end is set
(restAfterMarked, restSlow, restDrawer), Codex's held refusal, Claude's
quota shelf, a Codex window's reset and a search key's rest. Shorter
waits (a request's patience, ten-minute shuns) are left as they are; a
sleep can only stretch them by their own length.
TestRestsKeepTheWallClock records a rest through the quota and 500
paths, a slow start and a search key, and checks none counts on the
monotonic clock.
Run on mica at this tree: go vet ./internal/gateway (darwin, linux,
windows); go test ./internal/gateway -count=1 -timeout 45m: ok (883s).
Broken by hand: onWall returning t unchanged turns the test red on all
four paths.
Not tried: a real Mac sleeping past a rest; a test can't sleep the
machine.
At the pushed head (rebased on 42cc55701, which adds internal/provider/quota_shown.go): go vet ./internal/gateway ./internal/provider and go test ./internal/gateway -run "Rest|TestOneRequestDoesNotClimbTheBackoff": ok.
This commit is contained in:
@@ -296,7 +296,7 @@ func (b *subscriptionBridge) keepForReset(run *subscriptionRun, req *Request, ow
|
||||
}
|
||||
now := time.Now()
|
||||
s := &savedSession{session: cut, sessions: sessions, owner: owner, conv: conv, key: key, tools: tools,
|
||||
at: now, until: now.Add(quotaShelfFor(owner, msg, now)), tui: tui, appended: appended, cachePrefix: prefix}
|
||||
at: now, until: onWall(now.Add(quotaShelfFor(owner, msg, now))), tui: tui, appended: appended, cachePrefix: prefix}
|
||||
b.mu.Lock()
|
||||
if old := b.shelf[key]; old != nil {
|
||||
go old.discard()
|
||||
|
||||
@@ -84,7 +84,7 @@ func noteCodexHold(who string, status int, header http.Header, body []byte) {
|
||||
windows[k] = slices.Clone(vs)
|
||||
}
|
||||
}
|
||||
codexHolds.m[who] = codexHold{until: now.Add(d), status: status, why: why, body: append([]byte(nil), body...), ctype: header.Get("Content-Type"), codex: windows}
|
||||
codexHolds.m[who] = codexHold{until: onWall(now.Add(d)), status: status, why: why, body: append([]byte(nil), body...), ctype: header.Get("Content-Type"), codex: windows}
|
||||
}
|
||||
|
||||
// codexHeld is who's hold, while it lasts.
|
||||
|
||||
@@ -106,7 +106,7 @@ func headerLimitsOf(h http.Header, fam string, now time.Time) []provider.CodexLi
|
||||
if at, err := strconv.ParseInt(strings.TrimSpace(h.Get(pre+"reset-at")), 10, 64); err == nil && at > 0 {
|
||||
l.Resets = time.Unix(at, 0)
|
||||
} else if after, err := strconv.ParseInt(strings.TrimSpace(h.Get(pre+"reset-after-seconds")), 10, 64); err == nil && after > 0 {
|
||||
l.Resets = now.Add(time.Duration(after) * time.Second)
|
||||
l.Resets = onWall(now.Add(time.Duration(after) * time.Second))
|
||||
}
|
||||
out = append(out, l)
|
||||
}
|
||||
@@ -146,7 +146,7 @@ func codexEventLimits(data []byte, now time.Time) []provider.CodexLimit {
|
||||
if w.ResetAt > 0 {
|
||||
l.Resets = time.Unix(w.ResetAt, 0)
|
||||
} else if w.ResetAfter > 0 {
|
||||
l.Resets = now.Add(time.Duration(w.ResetAfter) * time.Second)
|
||||
l.Resets = onWall(now.Add(time.Duration(w.ResetAfter) * time.Second))
|
||||
}
|
||||
out = append(out, l)
|
||||
}
|
||||
|
||||
@@ -1347,7 +1347,7 @@ func restDrawer(c candidate, code int, msg string) {
|
||||
// as the reply's headers told it, not quotaRest's guess
|
||||
d, r.By = min(heard.Resets.Sub(now), longestQuota), "resets"
|
||||
}
|
||||
r.Until = now.Add(d)
|
||||
r.Until = onWall(now.Add(d))
|
||||
restingUntil.Lock()
|
||||
restingUntil.m[r.Key] = r.Until
|
||||
restingUntil.note[r.Key] = r
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// A rest is kept on the wall clock alone. A time from time.Now() carries
|
||||
// the monotonic clock too, and Go compares two such times by it; on macOS
|
||||
// it stops while the computer sleeps, so a rest to 00:40 set the day
|
||||
// before still held at 09:57 after a night of sleep, 17 hours past the
|
||||
// time it showed (plugins#1126). Sleep can't be had in a test, so this
|
||||
// checks what makes it harmless: no rest magpie keeps carries a monotonic
|
||||
// reading ("m=" in its String).
|
||||
func TestRestsKeepTheWallClock(t *testing.T) {
|
||||
monotonic := func(at time.Time) bool { return strings.Contains(at.String(), " m=") }
|
||||
if !monotonic(time.Now().Add(time.Hour)) {
|
||||
t.Fatal("time.Now() carries no monotonic reading here: the test proves nothing")
|
||||
}
|
||||
codexSignedIn(t)
|
||||
s := New()
|
||||
c := candidate{p: provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: "me@example.com"}}, model: "gpt-6-astra", rest: "codex@me@example.com"}
|
||||
|
||||
quota := []byte(`{"error":{"message":"usage exceeds frequency limit, your usage will reset at ` +
|
||||
time.Now().Add(14*time.Hour).Format("2006-01-02 15:04:05") + `","type":"usage_limit_reached"}}`)
|
||||
for _, x := range []struct {
|
||||
name string
|
||||
body []byte
|
||||
code int
|
||||
}{
|
||||
{"quota", quota, http.StatusTooManyRequests},
|
||||
{"500", []byte(`{"error":{"message":"An error occurred while processing your request.","type":"server_error"}}`), http.StatusInternalServerError},
|
||||
} {
|
||||
r := s.restAfter(c, x.code, http.Header{}, x.body)
|
||||
if r.Until.IsZero() {
|
||||
t.Fatalf("%s: no rest: the test proves nothing", x.name)
|
||||
}
|
||||
if monotonic(r.Until) {
|
||||
t.Errorf("%s: the rest's end %v counts on the monotonic clock", x.name, r.Until)
|
||||
}
|
||||
restingUntil.Lock()
|
||||
for k, until := range restingUntil.m {
|
||||
if monotonic(until) {
|
||||
t.Errorf("%s: %s rests to %v on the monotonic clock", x.name, k, until)
|
||||
}
|
||||
}
|
||||
restingUntil.Unlock()
|
||||
}
|
||||
|
||||
slow := candidate{p: provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: "slow@example.com"}}, model: "gpt-6-astra", rest: "codex@slow@example.com"}
|
||||
rested := false
|
||||
for i := 0; i < slowRests+1; i++ {
|
||||
if r, ok := restSlow(slow, time.Now()); ok {
|
||||
rested = true
|
||||
if monotonic(r.Until) {
|
||||
t.Errorf("a slow start's rest to %v counts on the monotonic clock", r.Until)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !rested {
|
||||
t.Fatal("slow starts never rested: the test proves nothing")
|
||||
}
|
||||
|
||||
restSearchKey("tavily", "k1", time.Hour, "rate limited", time.Now())
|
||||
if r, ok := searchKeyRest("tavily", "k1", time.Now()); !ok || monotonic(r.Until) {
|
||||
t.Errorf("a search key's rest %v (%v) counts on the monotonic clock", r.Until, ok)
|
||||
}
|
||||
}
|
||||
@@ -1741,7 +1741,7 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
|
||||
} else if a == nil && why != failOther && why != failVerify && why != failRestricted {
|
||||
staleKeyAllowance(c.p) // a key that reads its windows, too
|
||||
}
|
||||
r.Until = now.Add(d)
|
||||
r.Until = onWall(now.Add(d))
|
||||
if a := c.p.Account; a != nil {
|
||||
r.agent, r.user = a.UsageAgent(), a.User
|
||||
} else if why == failQuota {
|
||||
@@ -2056,7 +2056,7 @@ func restSlow(c candidate, now time.Time) (Rest, bool) {
|
||||
if run.n > slowRests {
|
||||
by = "backoff"
|
||||
}
|
||||
r := Rest{Why: failSlow, Status: http.StatusGatewayTimeout, Until: now.Add(d), By: by, Failures: run.n, Key: id, Model: c.model}
|
||||
r := Rest{Why: failSlow, Status: http.StatusGatewayTimeout, Until: onWall(now.Add(d)), By: by, Failures: run.n, Key: id, Model: c.model}
|
||||
if a := c.p.Account; a != nil {
|
||||
r.agent, r.user = a.UsageAgent(), a.User
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ func searchRestFor(vendor string, status int, retryAfter string, now time.Time)
|
||||
// restSearchKey sets a key resting for d; a rest it has that ends later
|
||||
// is kept.
|
||||
func restSearchKey(vendor, key string, d time.Duration, why string, now time.Time) {
|
||||
r := keyRest{Until: now.Add(d), Why: why}
|
||||
r := keyRest{Until: onWall(now.Add(d)), Why: why}
|
||||
if was, ok := searchKeyRest(vendor, key, now); ok && was.Until.After(r.Until) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package gateway
|
||||
|
||||
import "time"
|
||||
|
||||
// onWall gives t on the wall clock alone, for a deadline magpie keeps for
|
||||
// minutes to days: a rest, a hold, a window's reset. A time from
|
||||
// time.Now() also carries the monotonic clock, and Go compares two such
|
||||
// times by it; on macOS it stops while the computer sleeps, so a rest set
|
||||
// before a night's sleep ran that night longer than the time it shows
|
||||
// (plugins#1126: a rest to 00:40 still held at 09:57). On the wall clock
|
||||
// alone, Before, After, Sub and time.Until read the time it says.
|
||||
func onWall(t time.Time) time.Time { return t.Round(0) }
|
||||
Reference in New Issue
Block a user