* provider: an account refused while its allowance is being read has it read again once that reading is back, and keeps the share it had meanwhile
StaleAllowance zeroed the time an agent's allowances were read, and
dropped the account's own reading, but a reading Allowances already had
out, asked before the refusal, came back after it, set that time to now
and was trusted for a minute. For that minute the Routing page and the
trace showed the share from before the refusal, the tray's account in
use kept the refused account, and a second refusal of the account found
its window not full: a quota refusal with no reset time rested 15
minutes by "quota" instead of until the window renews, and the account
was tried and refused again after it. Every caller of StaleAllowance had
it: the gateway after a refusal, Claude's limit headers, a Codex reset
spent and both warm-ups.
StaleAllowance now marks the agent stale when a reading is out, as
StaleKeyAllowance does a key (ee2588c2). Once that reading is back what
it read is kept, but as not read, so the next ask reads again at once.
The values are kept rather than dropped because weigh counts an account
it doesn't know as unused, which would put the refused account first.
The tests' resets of the allowances clear the mark.
TestAllowancesStaleMidRead (provider: the reading held until after
StaleAllowance, answered 97%; later readings 100%) fails without the
change, "first ask 97%, 2s later 97%, readings 1", 3 of 3.
TestRefusedMidReadRestsByWindow (gateway: provider.Allowances behind
restAfter, as a plugin's agent is kept) fails without it, "second
refusal rests 15m0s by quota; the share read 97%, readings 2", 3 of 3,
and now rests by "window". Both pass -race -count=20, as do
TestAllowances, TestResetRunsOut, TestKeyAllowance and TestLoginRoom.
The verification commands of gateway-routing.md and
providers-accounts.md, go test -tags nogui ./..., go vet and gofmt -l
pass. docs/reference.md says it.
* docs: providers-accounts says a reading out when StaleAllowance is called is kept and read again (from #1161)
* provider: a built-in Grok account refused while its allowance is being read is read again on the next ask, not served the share read before the refusal for a minute
grokLoginUsage wrote every per-home reading back with a fresh time, so one
out when StaleAllowance("grok", user) dropped the home's entry was kept and
served for up to a minute. Each home's reading now carries a pending
generation that StaleAllowance drops; a reading whose generation no longer
matches goes to its caller but isn't cached, as loginReading does for the
other agents. A Grok moved to its plugin goes through loginReading and was
not affected.
87 lines
2.9 KiB
Go
87 lines
2.9 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
var staleMidReadRuns atomic.Int32
|
|
|
|
// An account refused while its allowance is being read, and refused again
|
|
// once that reading is back, rests until its window renews: the reading,
|
|
// asked before the first refusal, is read again at once, not trusted for a
|
|
// minute, so the second refusal finds the window full rather than resting
|
|
// a quarter of an hour by "quota" and being tried and refused once more.
|
|
func TestRefusedMidReadRestsByWindow(t *testing.T) {
|
|
fresh(t)
|
|
old := allowances
|
|
allowances = provider.Allowances
|
|
t.Cleanup(func() { allowances = old })
|
|
// an agent no other test, nor an earlier run of this one, reads; named
|
|
// as a subscription moved to its plugin is kept by
|
|
agent := fmt.Sprintf("plugin:stale-mid-read-%d", staleMidReadRuns.Add(1))
|
|
week := time.Now().Add(3 * time.Hour)
|
|
release := make(chan struct{})
|
|
var once sync.Once
|
|
var readings, out atomic.Int32
|
|
provider.LoginUsageVia(func(_ context.Context, ag string) map[string]provider.SubscriptionQuota {
|
|
if ag != agent {
|
|
return nil
|
|
}
|
|
out.Add(1)
|
|
defer out.Add(-1)
|
|
used := 100.0
|
|
switch readings.Add(1) {
|
|
case 1:
|
|
used = 90
|
|
case 2: // out as the request is refused
|
|
<-release
|
|
used = 97
|
|
}
|
|
return map[string]provider.SubscriptionQuota{"me@example.com": {Windows: []provider.QuotaWindow{{Span: 7 * 24 * time.Hour, Used: used, ResetsAt: &week}}}}
|
|
})
|
|
t.Cleanup(func() {
|
|
once.Do(func() { close(release) })
|
|
for out.Load() > 0 {
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
provider.LoginUsageVia(nil)
|
|
})
|
|
share := func() float64 {
|
|
u, _ := provider.Allowances(agent)["me@example.com"].For("gpt-5.5", time.Now())
|
|
return u
|
|
}
|
|
if u := share(); u != 90 {
|
|
t.Fatalf("first reading %v%%", u)
|
|
}
|
|
provider.StaleAllowance(agent, "me@example.com") // as read over a minute ago
|
|
share() // read again behind the request
|
|
for readings.Load() < 2 {
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
acct := candidate{p: provider.Provider{ID: "codex", Account: &provider.Account{Agent: agent, User: "me@example.com"}}, model: "gpt-5.5", rest: agent + "#me@example.com"}
|
|
s := &Server{}
|
|
refused := []byte(`{"error":{"message":"You've hit your usage limit"}}`)
|
|
if r := s.restAfter(acct, 429, http.Header{}, refused); r.By != "quota" {
|
|
t.Fatalf("first refusal, the window not known full: rests by %s", r.By)
|
|
}
|
|
once.Do(func() { close(release) })
|
|
// the routing asks before each request; give the reading after it time
|
|
got, deadline := share(), time.Now().Add(2*time.Second)
|
|
for got != 100 && time.Now().Before(deadline) {
|
|
time.Sleep(10 * time.Millisecond)
|
|
got = share()
|
|
}
|
|
r := s.restAfter(acct, 429, http.Header{}, refused)
|
|
if d := time.Until(r.Until); r.By != "window" || d < 2*time.Hour {
|
|
t.Fatalf("second refusal rests %v by %s; the share read %v%%, readings %d", d.Round(time.Minute), r.By, got, readings.Load())
|
|
}
|
|
}
|