provider: a Codex reset spent while an allowance read is in flight is never undone by that read, even on Windows' coarse clock (#1754, ggbdpq)
Allowances took time.Now() when its background read began, and
forgetAllowance stamped the reset with time.Now(); a reading was dropped
only when the reset was After the start. On Windows two events in one
clock tick get the same time, so a read begun before a spent reset could
put the account's 100% back right after it. Reads and resets are now
ordered by a generation counter under usedCache's lock.
ggbdpq's patch (e07232ef), cherry-picked unchanged.
Run (reviewer): TestResetAllowanceKnownAtOnce, Windows test binary on
the Windows box (sandbox USERPROFILE/APPDATA), -count=10: old code FAIL
4 of 10 ("routing reads 100 used (known true), want 0"), this change
PASS 10 of 10. macOS -race -count=20 of the Allowance/Renewed tests ok.
Full provider package, gofmt, linux and windows vet: see the push.
Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
This commit is contained in:
committed by
yetone
co-authored by
factory-droid[bot]
parent
591ed58b53
commit
509328ccb0
@@ -131,6 +131,7 @@ listed models keep their `Available` names unchanged
|
||||
|
||||
## Constraints and failure behavior
|
||||
|
||||
- A successful Codex allowance reset invalidates an account's in-flight reading even when the read and reset have the same clock timestamp. `Allowances` snapshots `usedCache.generation` under its lock, and `forgetAllowance` advances the generation for that account; a reading from before the reset is dropped before routing can use it. [`routing.go`](../../internal/provider/routing.go), `TestResetAllowanceKnownAtOnce`.
|
||||
- Each refresh token has exactly one holder. Vendors rotate tokens on refresh, so two copies of one token would sign each other out. `savedTokenMu` stops two requests refreshing one saved account at once.
|
||||
- A Claude subscription account is used only for Claude Code, through the genuine Claude Code binary (see [Claude subscription bridge](claude-subscription-bridge.md)).
|
||||
- Claude usage asks are ordered by `claudeAskGeneration`, not wall-clock timestamps. `AskClaudeUsage` and a Claude card's `RefreshUsage` advance it while holding the subscription cache then the login cache lock (`markClaudeUsageAsked`). Each cache captures it under its own lock before launching a read and checks it under that lock when publishing: a newer ask prevents `LoginUsage` from keeping the older reading and leaves `SubscriptionUsage` due to read again. Each `claudeWindows` entry records the generation its run answered. The timestamp's zero sentinel, `tried`/`wait`, the 30-second floor, idle behavior and saved-account scope remain unchanged. [`login_usage.go`](../../internal/provider/login_usage.go), [`subscription_usage.go`](../../internal/provider/subscription_usage.go), [`quota_refresh.go`](../../internal/provider/quota_refresh.go).
|
||||
|
||||
@@ -124,10 +124,11 @@ var usedCache struct {
|
||||
m map[string]map[string]Allowance // agent → user → allowance
|
||||
at map[string]time.Time
|
||||
loading map[string]chan struct{} // closed when the fetch in flight is done
|
||||
// renewed: when an account's windows were last started again (a
|
||||
// Codex reset spent), by agent/user: a reading begun before says
|
||||
// nothing of them
|
||||
renewed map[string]time.Time
|
||||
// renewed: the generation when an account's windows were started
|
||||
// again. Read/reset order cannot use the clock: two events can have
|
||||
// the same timestamp, even with its monotonic part.
|
||||
renewed map[string]uint64
|
||||
generation uint64
|
||||
// seen: each account's windows as last read, and when, by
|
||||
// agent/user, kept through a reading that failed, for the next to be
|
||||
// told renewed by (renewedFrom)
|
||||
@@ -476,11 +477,11 @@ func Allowances(agent string) map[string]Allowance {
|
||||
if done == nil && time.Since(c.at[agent]) > time.Minute {
|
||||
done = make(chan struct{})
|
||||
c.loading[agent] = done
|
||||
began := c.generation
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
share := renewalShare(agent) // read before the lock: it reads providers.json
|
||||
began := time.Now()
|
||||
all := map[string]Allowance{}
|
||||
readings, readAt := loginUsageAt(ctx, agent)
|
||||
for user, q := range readings {
|
||||
@@ -502,7 +503,7 @@ func Allowances(agent string) map[string]Allowance {
|
||||
var renewed []string
|
||||
for user, a := range all {
|
||||
key := agent + "/" + strings.ToLower(user)
|
||||
if c.renewed[key].After(began) {
|
||||
if c.renewed[key] > began {
|
||||
// started again while this was read: not known till
|
||||
// it is read again, at once
|
||||
delete(all, user)
|
||||
@@ -634,9 +635,10 @@ func forgetAllowance(agent, user string) {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
if c.renewed == nil {
|
||||
c.renewed = map[string]time.Time{}
|
||||
c.renewed = map[string]uint64{}
|
||||
}
|
||||
c.renewed[agent+"/"+strings.ToLower(user)] = time.Now()
|
||||
c.generation++
|
||||
c.renewed[agent+"/"+strings.ToLower(user)] = c.generation
|
||||
delete(c.seen, agent+"/"+strings.ToLower(user)) // told already, by renewedNow
|
||||
if c.at != nil {
|
||||
c.at[agent] = time.Time{}
|
||||
|
||||
Reference in New Issue
Block a user