diff --git a/docs/subsystems/providers-accounts.md b/docs/subsystems/providers-accounts.md index 4a79e9956..0056a5f3d 100644 --- a/docs/subsystems/providers-accounts.md +++ b/docs/subsystems/providers-accounts.md @@ -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). diff --git a/internal/provider/routing.go b/internal/provider/routing.go index 26a4c0c2c..bdfe2dfab 100644 --- a/internal/provider/routing.go +++ b/internal/provider/routing.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{}