Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gateway: a Codex account with credits is asked again at its allowance's edge, never benched (豆子, Zhenzhen)

yetonebf2e067ef6dd
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md
index cb7463d47..a137c3bb7 100644
--- a/docs/subsystems/gateway-routing.md
+++ b/docs/subsystems/gateway-routing.md
@@ -128,7 +128,7 @@ recorded as a prediction failure (`noPrediction`).
- Routing decides who goes first only when nobody has answered the conversation yet. Affinity keeps the answerer while its cache is worth it. Auto mode keeps it across turns while the last reply read from the cache and wrote to it at least `cacheWorth` (1024) tokens between them (the usage's `CacheRead` plus `CacheWrite`, passed to `answered`) and the cache isn't cold (`cacheCold`, 5 minutes). Past 5 minutes it stays while the vendor said it keeps at least `cacheWorth` tokens of the conversation for an hour (`hourCache`, #2302): a reply with Anthropic's 1-hour writes (`cache_creation.ephemeral_1h_input_tokens`, the usage's `CacheWrite1h`, as Claude Code asks of a subscription) keeps what it read plus those for `cacheHour`, a later reply on the same account and model that reads all of it starts the hour again, and one that reads less (moved, compacted) drops it. The stick keeps it on disk too (`cacheHour`, `cacheHourAt`), and a group's rules keep the turn's effort the same hour (`keepsEffort`). Vendors that report no 1-hour write are unchanged. `TestAffinityKeepsAnHourCachePastFiveMinutes`, `TestHourCacheAfter`. What was written counts: a first turn reads nothing and writes the whole prompt (Anthropic's `cache_creation_input_tokens`), and the second turn stays to read it rather than moving as `no-cache` and building it again elsewhere (vilicvane on Discord). The stick keeps both, on disk too (`affinity.json`'s `cacheWrite`, absent in a file from before and read as 0, so such a stick is judged by its read alone), and the trace's `Affinity.CacheWrite` lets the Routing page say what was read and what was written. A vendor that keeps a prompt cached without reporting it (OpenAI's automatic prefix cache, Gemini) says neither of a turn that read none, so the prompt that turn was sent (the usage's `Input`) stands for the write and its second turn stays too; a reply that reports no usage at all counts as no cache, as before. `TestAffinityKeepsACacheJustWritten`, `TestAffinityKeepsACacheTheVendorKeptSilently`, `routing-affinity-cache-write.test.cjs`.
- Strict (`strict`, #1797, miniers) never moves a conversation to another key or account (`who`). Its stick is kept as session's is, and while it is fresh `affine` keeps only the candidates of that `who` (`strictOnly`): its other models stay, so a group's rules still pick among them, but no other account, key or provider is asked, and a resting or spent one is asked anyway. A failure there reaches the agent and rests the account as a non-lone candidate's would (`strictAlone`), so new conversations go elsewhere. Before planning, `strictKeep` takes the stuck account back from what holds new conversations off it: a usage cap (`pl.capped`, `capHeldOnly`), so the conversation goes on past it. An account held for credits it may not spend is answered 429 naming the account and Strict, and nothing reaches the vendor, rather than moved. An account removed, switched off, barred, or not allowed for the calling key is gone, and the conversation moves as under session. The trace says `strict` even with one candidate. `strict_affinity_test.go`, `stays-strict.test.cjs`.
- In turn (`rotate`), a conversation nobody has answered yet goes to the key, account or model the fewest other conversations of the provider's model or the group answered in the last `heldFor` (30 minutes) are on (`leastHeld`, #946). Ties, resting ones and a group with a group in it keep routing's order. With session affinity, two sessions of one group so stay on different models.
-- A ChatGPT account that holds credits answers past its windows on them; the vendor spends them. That is the default. Set not to spend them (Usage card's *Use credits*, `magpie quota credits <account> off`), the account is held at 100% of a window till it renews, by magpie's latest reading of its windows (`provider.Allowances`, refreshed about every minute), so the other accounts, groups and fallbacks take the request. A cap set on the account (1–99%) takes precedence. Unlike a cap, the hold is at the vendor's own 100%, so an account allowed to spend its resets spends one there, both when every candidate is held and after the last one is refused. A reset made forgets the account's cached allowance (`renewedNow`), so neither hold outlives the windows it started again.
+- A ChatGPT account that holds credits answers past its windows on them; the vendor spends them. That is the default. Set not to spend them (Usage card's *Use credits*, `magpie quota credits <account> off`), the account is held at 100% of a window till it renews, by magpie's latest reading of its windows (`provider.Allowances`, refreshed about every minute), so the other accounts, groups and fallbacks take the request. A cap set on the account (1–99%) takes precedence. Unlike a cap, the hold is at the vendor's own 100%, so an account allowed to spend its resets spends one there, both when every candidate is held and after the last one is refused. A reset made forgets the account's cached allowance (`renewedNow`), so neither hold outlives the windows it started again. One let spend its credits, and holding some by its last `/wham/usage` (`credits.has_credits` with a balance over 0, or `unlimited`; `QuotaWindow.Credits`, `Limit.Credits`, `Allowance.OnCredits`), is not out when a window fills (豆子 and Zhenzhen on Discord): ChatGPT may answer the turn that uses the window up 429 `usage_limit_reached` before it spends the credits, and the next turn on them. So that 429 is asked again at once, once a request, on the same account, before a reset is looked at, on the routed path (`creditsAgain`, `creditsAsked`, `Try.Credits`) and on Codex's own sign-in's relay (`relayOnCredits`), rather than ended for the agent, which ended a running session while a new one worked. Refused again, it rests `fallbackCooldown` (`Rest.By` `credits`) and is read again, not until its window renews, and its full windows rest it nowhere else (`full` is zero for it, `spendsCredits`). Smart, Pace and Least used put such an account first of the spent tier (`routes.Seat.Credits`, `spentBefore`): after those with allowance left, ahead of those used up with nothing to go on with (`Weighed.Credits`, told on the Routing page). Switching to credits early, at a few percent left, was weighed and not done: the vendor spends credits only once the allowance is used up, so it would only waste what is left. `TestCodexCreditsAskedAgainAtTheEdge`, `TestCodexCreditsNoLongRest`, `TestCodexCreditsFirstOfTheSpent`, `TestCreditsFirstOfTheSpent`, `routing-codex-credits-again.test.cjs`.
- Different failures are classified separately: out of credit, out of quota, rate limit and other errors rest for different lengths. A fix must not merge them into one bucket (see #873).
- One of Codex's own models on the backend path with no routing to do (`codexAccounts` false: one ChatGPT account, no usage cap, no key holds, nothing pinned) is relayed as Codex sent it, on Codex's own sign-in, its bytes and headers unchanged and its reply streamed as it comes (`codexUpstreamOn` with no account): routing was built for more than one account, and that path also keeps the turn's native compaction with the backend. Before, nothing was asked again on it, so an overload went straight to Codex, which doesn't retry the backend's `server_is_overloaded` itself (夜松云 on Discord). Now a transient overload (`codexBusy`: what `busy` calls busy, a 529, a 503 saying overloaded, the backend's in-stream `response.failed` with `server_is_overloaded` (#1418); never a 429, a usage limit, an edge's drop of the connection (`edgeSaidDrop`) or a reset (`codexReset`)) is asked again of the same account, the same body and headers, up to `lastRetries` times after `retryPause<<n`, as the accounts loop asks its last account, and only before any of the reply has reached Codex. A stream is held for it through a `holdWriter` with its reasoning let through (`thinkingShown`), kept alive past `keepHeldAfter` across the tries (one `keptAlive`), so Codex sees the stream's headers and comments, never a failed try's frames. Nothing rests for an overload. Once the retries are spent, a turn whose codex subscription has fallbacks (`codexFallsBack`; never a compaction) goes on to them (`codexFallBack`: the turn served as `codex/<model>` with the account left out of the candidates, `fallbacksOnly`, and the same keepalive carried in `handedOnKey`); without fallbacks Codex gets the last try's failure as the backend said it. Each try is a `HandedOn` usage record and a try in the trace. A success is relayed as before, with nothing held when no retry or fallback is left (`TestCodexOneAccountOverloadAskedAgain`, `…StreamOverloadAskedAgain`, `…OverloadBounded`, `…OverloadFallsBack`, `…OverloadKeptAlive`, `…RefusalNotAskedAgain` in [`codex_one_account_overload_test.go`](../../internal/gateway/codex_one_account_overload_test.go)).
- A subscription account whose sign-in its vendor revoked (`provider.SignInRevoked`: 401 `token_revoked` and the like, #1793) fails as `failAuth`: the next member is asked at once, nothing rests, and the account is marked (`noteSignIn` in [`signin_revoked.go`](../../internal/gateway/signin_revoked.go)). Its later requests aren't sent while it has that credential: `forwardOnce` answers them with a 401 of magpie's own (`revokedReply`), so they too pass on. Compaction on an account of magpie's (`compactOn`) picks a marked one last. A key's provider answering the same words rests as before.
@@ -775,7 +775,7 @@ the row of the entry that id reaches, with its label, under the bare id.
## Verification
```sh
-go test -tags nogui ./internal/gateway -run 'TestGroupAsksOneRestingMemberNotEach|TestLoneRestingMemberStillAsked|TestRetrievalGroupAsksOneRestingMember|TestProbeOne|TestLoop|TestLongReplyThatDoesNotLoop|TestQuiet|TestSilentHeld|TestRotateSpreadsSessions|TestRouting|TestSmartRouting|TestFallback|TestNoFallbackForOtherErrors|TestLastFallbackErrorReachesTheAgent|TestRateLimitedSinksToTheBack|TestGroupRateLimitedSinks|TestAffinity|TestSeveralKeysOnTakeOverFromEachOther|TestModelNotInPlanRestsTheModelNotTheKey|TestSubscriptionAccountsTakeOver|TestGroup|TestTrace|TestImageTool|TestCodexNoCredits|TestWindowCapsHoldTheAccount|TestAccountCap|TestClaudeGoesOnMessagesWhereTheRelayHasThem|TestAutoRelaysOnTheClientsOwnAPI|TestSearchOfferedStaysOnTheClientsAPI|TestSearchFollowsTheSearchersProxy|TestCodexSearchAnsweredOnResponses|TestAgentEffortOnCursorLocal|TestAzureCompactionWithoutMagpiesReasoning|TestCodexCompact|TestProtection|TestSearchedConversation|TestUnsearchedConversationUnchanged|TestNotAnAPIReply|TestAPIReplyUntouched|TestCodexBackendWebPage|TestSub2APIKeyLimitIsQuota|TestKeysWeighedByTheirWindows|TestGroupWeighsKeyWindows|TestKeyOutOfItsWindowRestsUntilReset|TestKeyPoolErrorDoesNotRestForWindow|TestKeyBackOnceItsWindowIsNotFull|TestRestingKeyReadAgainInAnOrderedGroup|TestClaudeAccountBackOnceItsWindowRenews|TestThinkingStreams|TestHeldThinkingKeepsTheAgentAlive|TestCodexHeardWithinItsIdleTimeout|TestRefusalAfterThinkingFailsOver|TestGeminiEmptyRepliesAskedAgain|TestLocalUnredacted|TestCORS|TestForeignPagesRefused|TestAIStudioThinking|TestGeminiThoughtsThroughLocalProxy|TestGeminiRefusedLevel|TestGeminiThinkingOffAtItsLowest|TestThinkingOff|TestTranslatedEffortLoggedAsSent|TestTranslatedThinkingOffRefused|TestSub2APIThinkingOffRefused|TestRefusesThinkingOff|TestClaudeFiveFiveThinkingOffOnTheWire|TestSamplingAsTheModelTakesIt|TestHaikuFiveFiveSamplingOnTheWire|TestGeminiThoughtsMarkedInExtraContent|TestGeminiThinkingLevelRefused|TestGeminiMinimalRefusedTwiceAtOnce|TestPassthroughGeminiMinimalRefused|TestPassthroughEffortNoneRefused|TestPassthroughMinimalRefusedNoneTaken|TestOtherOffNamed|TestClaudeSignInNotToldItsSignInFailed|TestSignInWriterReleasesA403|TestSignInBetaStaysWithMagpie|TestStructuredOutput|TestResponsesTextKeepsVerbosityBesideFormat|TestRefusedFormatToldInWords|RPM|TestQueued|TestCustomGeminiUpstream|TestWholeNums|TestGrokCalls|TestOnlyGrokCalls|TestGrokModel|TestGrokPluginCalls'
+go test -tags nogui ./internal/gateway -run 'TestGroupAsksOneRestingMemberNotEach|TestLoneRestingMemberStillAsked|TestRetrievalGroupAsksOneRestingMember|TestProbeOne|TestLoop|TestLongReplyThatDoesNotLoop|TestQuiet|TestSilentHeld|TestRotateSpreadsSessions|TestRouting|TestSmartRouting|TestFallback|TestNoFallbackForOtherErrors|TestLastFallbackErrorReachesTheAgent|TestRateLimitedSinksToTheBack|TestGroupRateLimitedSinks|TestAffinity|TestSeveralKeysOnTakeOverFromEachOther|TestModelNotInPlanRestsTheModelNotTheKey|TestSubscriptionAccountsTakeOver|TestGroup|TestTrace|TestImageTool|TestCodexNoCredits|TestCodexCredits|TestWindowCapsHoldTheAccount|TestAccountCap|TestClaudeGoesOnMessagesWhereTheRelayHasThem|TestAutoRelaysOnTheClientsOwnAPI|TestSearchOfferedStaysOnTheClientsAPI|TestSearchFollowsTheSearchersProxy|TestCodexSearchAnsweredOnResponses|TestAgentEffortOnCursorLocal|TestAzureCompactionWithoutMagpiesReasoning|TestCodexCompact|TestProtection|TestSearchedConversation|TestUnsearchedConversationUnchanged|TestNotAnAPIReply|TestAPIReplyUntouched|TestCodexBackendWebPage|TestSub2APIKeyLimitIsQuota|TestKeysWeighedByTheirWindows|TestGroupWeighsKeyWindows|TestKeyOutOfItsWindowRestsUntilReset|TestKeyPoolErrorDoesNotRestForWindow|TestKeyBackOnceItsWindowIsNotFull|TestRestingKeyReadAgainInAnOrderedGroup|TestClaudeAccountBackOnceItsWindowRenews|TestThinkingStreams|TestHeldThinkingKeepsTheAgentAlive|TestCodexHeardWithinItsIdleTimeout|TestRefusalAfterThinkingFailsOver|TestGeminiEmptyRepliesAskedAgain|TestLocalUnredacted|TestCORS|TestForeignPagesRefused|TestAIStudioThinking|TestGeminiThoughtsThroughLocalProxy|TestGeminiRefusedLevel|TestGeminiThinkingOffAtItsLowest|TestThinkingOff|TestTranslatedEffortLoggedAsSent|TestTranslatedThinkingOffRefused|TestSub2APIThinkingOffRefused|TestRefusesThinkingOff|TestClaudeFiveFiveThinkingOffOnTheWire|TestSamplingAsTheModelTakesIt|TestHaikuFiveFiveSamplingOnTheWire|TestGeminiThoughtsMarkedInExtraContent|TestGeminiThinkingLevelRefused|TestGeminiMinimalRefusedTwiceAtOnce|TestPassthroughGeminiMinimalRefused|TestPassthroughEffortNoneRefused|TestPassthroughMinimalRefusedNoneTaken|TestOtherOffNamed|TestClaudeSignInNotToldItsSignInFailed|TestSignInWriterReleasesA403|TestSignInBetaStaysWithMagpie|TestStructuredOutput|TestResponsesTextKeepsVerbosityBesideFormat|TestRefusedFormatToldInWords|RPM|TestQueued|TestCustomGeminiUpstream|TestWholeNums|TestGrokCalls|TestOnlyGrokCalls|TestGrokModel|TestGrokPluginCalls'
go test -tags nogui ./internal/gateway -run 'TestMemberTooSmallIsNotAsked|TestMemberTooSmallIsTheLastResort|TestClaudeAccountsSpentFallBackToCodex|TestConversationOutgrowingItsMemberMoves|TestRoomyLeavesWhatItCantJudge|TestPayloadTooLarge|TestOverflow|TestContextOverflow|TestRelayInputCap|TestEveryVendorsOverflow'
go test -tags nogui ./internal/gateway -run 'TestPlain403|TestOrderedKeepsAnAccountAt98|TestPromptRefusalRestsNobody'
go test -tags nogui ./internal/gateway -run 'TestVertex'
diff --git a/internal/gateway/codex_autoreset_test.go b/internal/gateway/codex_autoreset_test.go
index 920c69b6a..857fec24d 100644
--- a/internal/gateway/codex_autoreset_test.go
+++ b/internal/gateway/codex_autoreset_test.go
@@ -25,11 +25,18 @@ type resetBackend struct {
spent []string // accounts a reset was spent on
gone bool // every reset spent elsewhere since the usage was read
tried []string // accounts asked for a turn
+ // credits: holds credits, which the backend spends once the account
+ // is out of its allowance, after turning away the first edge turns
+ // at the moment it ran out; fails: answers every turn with it
+ credits map[string]bool
+ edge map[string]int
+ fails map[string]int
}
func newResetBackend(t *testing.T, accounts ...string) *resetBackend {
t.Helper()
- b := &resetBackend{week: map[string]float64{}, held: map[string]int{}, out: map[string]bool{}}
+ b := &resetBackend{week: map[string]float64{}, held: map[string]int{}, out: map[string]bool{},
+ credits: map[string]bool{}, edge: map[string]int{}, fails: map[string]int{}}
for _, a := range accounts {
b.week[a], b.held[a], b.out[a] = 100, 1, true
}
@@ -42,7 +49,18 @@ func newResetBackend(t *testing.T, accounts ...string) *resetBackend {
case "/backend-api/codex/responses":
io.ReadAll(r.Body)
b.tried = append(b.tried, acct)
- if b.out[acct] {
+ if n := b.fails[acct]; n > 0 {
+ w.WriteHeader(n)
+ io.WriteString(w, `{"error":{"type":"server_error","message":"something went wrong"}}`)
+ return
+ }
+ // out of its allowance: refused, unless it holds credits and
+ // is past the turns turned away as it ran out
+ refused := b.out[acct] && (!b.credits[acct] || b.edge[acct] > 0)
+ if refused && b.credits[acct] {
+ b.edge[acct]--
+ }
+ if refused {
w.WriteHeader(429)
io.WriteString(w, `{"error":{"type":"usage_limit_reached","message":"The usage limit has been reached","plan_type":"plus","resets_in_seconds":7200}}`)
return
@@ -60,7 +78,8 @@ func newResetBackend(t *testing.T, accounts ...string) *resetBackend {
"rate_limit": map[string]any{
"primary_window": map[string]any{"used_percent": five, "limit_window_seconds": 18000, "reset_at": now.Add(2 * time.Hour).Unix()},
"secondary_window": map[string]any{"used_percent": b.week[acct], "limit_window_seconds": 604800, "reset_at": now.Add(3 * 24 * time.Hour).Unix()}},
- "rate_limit_reset_credits": map[string]any{"available_count": b.held[acct]}})
+ "rate_limit_reset_credits": map[string]any{"available_count": b.held[acct]},
+ "credits": map[string]any{"has_credits": b.credits[acct], "unlimited": false, "balance": map[bool]string{true: "120", false: "0"}[b.credits[acct]]}})
case "/backend-api/wham/rate-limit-reset-credits":
var credits []any
for i := range b.held[acct] {
diff --git a/internal/gateway/codex_backend.go b/internal/gateway/codex_backend.go
index 23b4b9081..fc974e19c 100644
--- a/internal/gateway/codex_backend.go
+++ b/internal/gateway/codex_backend.go
@@ -837,6 +837,21 @@ func marshalJSON(v any) ([]byte, error) {
// codexUpstream relays a request as it came, the sign-in included, to where
// Codex would have sent it.
+// relayOnCredits says whether the account a turn is relayed on — on, or
+// Codex's own sign-in when on is nil — goes on past its allowance on its
+// credits, as its last reading says, and is let spend them (onCredits).
+func relayOnCredits(on *provider.Provider) bool {
+ if on != nil {
+ return candidate{p: *on}.onCredits()
+ }
+ who, ok := provider.CodexSignedIn()
+ if !ok {
+ return false
+ }
+ a := allowances("codex")[who]
+ return a.OnCredits() && provider.CodexCredits(who)
+}
+
func (s *Server) codexUpstream(w http.ResponseWriter, r *http.Request, rest string, body []byte) {
s.codexUpstreamOn(w, r, rest, body, nil)
}
@@ -967,11 +982,12 @@ func (s *Server) codexUpstreamOn(w http.ResponseWriter, r *http.Request, rest st
}
}
var res *http.Response
- autoReset := false // a Codex reset looked at, once
- resetNote := "" // what spending it did, for the request log
- resets := 0 // times a large request ChatGPT closed on was asked again (#1628)
- sent := len(body) // the bytes that went, compressed (provider.CodexCompressed)
- began := start // when the try now going began
+ autoReset := false // a Codex reset looked at, once
+ creditsAsked := false // asked again onto the account's credits, once (relayOnCredits)
+ resetNote := "" // what spending it did, for the request log
+ resets := 0 // times a large request ChatGPT closed on was asked again (#1628)
+ sent := len(body) // the bytes that went, compressed (provider.CodexCompressed)
+ began := start // when the try now going began
// failWith tells Codex of a failure before any reply: as the stream's
// error once it was sent the stream's headers while it waited
failWith := func(status int, msg string) {
@@ -1076,6 +1092,21 @@ func (s *Server) codexUpstreamOn(w http.ResponseWriter, r *http.Request, rest st
// row, to sign in again (#1793)
noteSignIn(ctx, on.Account, req, res)
}
+ if !creditsAsked && rest == "/responses" && base != codexAPIBase && res.StatusCode == http.StatusTooManyRequests && relayOnCredits(on) {
+ // the account holds credits and was refused as out of its
+ // allowance: ChatGPT may turn away the turn that uses the
+ // window up before it spends them, and answers the next
+ // on them (Zhenzhen on Discord). Asked again at once,
+ // before a reset is looked at, rather than the turn ended
+ creditsAsked = true
+ msg, _ := io.ReadAll(io.LimitReader(res.Body, 1<<20))
+ res.Body.Close()
+ res.Body = io.NopCloser(bytes.NewReader(msg))
+ if failure(res.StatusCode, msg) == failQuota {
+ s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1].Credits = true })
+ continue
+ }
+ }
if !autoReset && rest == "/responses" && base != codexAPIBase && res.StatusCode == http.StatusTooManyRequests {
// the account is out of its allowance: one it lets spend its
// resets by itself, its week used up, spends one and is asked
diff --git a/internal/gateway/codex_credits_test.go b/internal/gateway/codex_credits_test.go
index 1b5ed82b7..8d1e33089 100644
--- a/internal/gateway/codex_credits_test.go
+++ b/internal/gateway/codex_credits_test.go
@@ -1,8 +1,11 @@
package gateway
import (
+ "context"
+ "slices"
"strings"
"testing"
+ "time"
"github.com/yetone/magpie/internal/provider"
)
@@ -158,3 +161,184 @@ func TestCodexNoCreditsResetAfterTheLastRefusal(t *testing.T) {
t.Fatalf("traced %v", got)
}
}
+
+// creditsUsage is capUsage for Codex accounts whose five hours and week
+// are each at used's share, those named in credits holding credits as
+// /wham/usage says (provider.QuotaWindow.Credits).
+func creditsUsage(t *testing.T, used map[string]float64, credits ...string) {
+ t.Helper()
+ old := allowances
+ t.Cleanup(func() { allowances = old })
+ allowances = func(string) map[string]provider.Allowance {
+ out := map[string]provider.Allowance{}
+ for u, n := range used {
+ c := slices.Contains(credits, u)
+ out[u] = provider.Allowance{
+ {Used: n, Resets: time.Now().Add(2 * time.Hour), Span: 5 * time.Hour, Credits: c},
+ {Used: n, Resets: time.Now().Add(3 * 24 * time.Hour), Span: 7 * 24 * time.Hour, Credits: c},
+ }
+ }
+ return out
+ }
+}
+
+// triesOf is each try of srv's routes, as account: why it failed, how
+// long it rests and why, and whether it was asked again onto its credits.
+func triesOf(srv *Server) []Try {
+ var out []Try
+ for _, r := range srv.Trace(context.Background(), 0, 0).Routes {
+ out = append(out, r.Tries...)
+ }
+ return out
+}
+
+// Zhenzhen on Discord: a Codex account that holds credits used up its
+// allowance mid-session, and the turn that ran it out was refused
+// (usage_limit_reached) while the next session answered on its credits.
+// The refused turn is asked again of the same account at once, and
+// answers: the session doesn't end. Codex's one account goes through the
+// relay, two or more through routing; each asks again once a request.
+func TestCodexCreditsAskedAgainAtTheEdge(t *testing.T) {
+ t.Run("relayed", func(t *testing.T) {
+ codexSignedIn(t)
+ b := newResetBackend(t, "acct-1")
+ b.credits["acct-1"], b.edge["acct-1"] = true, 1
+ creditsUsage(t, map[string]float64{"[email protected]": 100}, "[email protected]")
+ srv := New()
+ code, body := resetPost(t, srv)
+ if tried, spent := b.seen(); code != 200 || !strings.Contains(body, "pong") || tried != "acct-1,acct-1" || spent != "" {
+ t.Fatalf("%d %s tried %s spent %s", code, body, tried, spent)
+ }
+ if ts := triesOf(srv); len(ts) == 0 || !ts[0].Credits {
+ t.Fatalf("not traced as asked again onto its credits: %+v", ts)
+ }
+ // refused again: told as it came, not asked a third time
+ b.mu.Lock()
+ b.tried, b.edge["acct-1"] = nil, 2
+ b.mu.Unlock()
+ if code, _ := resetPost(t, srv); code != 429 {
+ t.Fatalf("refused twice: %d", code)
+ }
+ if tried, _ := b.seen(); tried != "acct-1,acct-1" {
+ t.Fatalf("refused twice, tried %s", tried)
+ }
+ })
+ t.Run("routed", func(t *testing.T) {
+ codexSignedIn(t, "[email protected]")
+ b := newResetBackend(t, "acct-1", "acct-2")
+ b.credits["acct-1"], b.edge["acct-1"] = true, 1
+ creditsUsage(t, map[string]float64{"[email protected]": 100, "[email protected]": 100}, "[email protected]")
+ if err := provider.SetRouting("codex", provider.Ordered); err != nil {
+ t.Fatal(err)
+ }
+ srv := New()
+ code, body := resetPost(t, srv)
+ if tried, _ := b.seen(); code != 200 || !strings.Contains(body, "pong") || tried != "acct-1,acct-1" {
+ t.Fatalf("%d %s tried %s", code, body, tried)
+ }
+ ts := triesOf(srv)
+ if len(ts) != 2 || !ts[0].Credits || ts[0].Rest != nil || ts[1].Status != 200 {
+ t.Fatalf("traced %+v", ts)
+ }
+ })
+ t.Run("set not to spend them", func(t *testing.T) {
+ codexSignedIn(t)
+ b := newResetBackend(t, "acct-1")
+ b.credits["acct-1"], b.edge["acct-1"] = true, 1
+ creditsUsage(t, map[string]float64{"[email protected]": 100}, "[email protected]")
+ if err := provider.SetCodexCredits("[email protected]", false); err != nil {
+ t.Fatal(err)
+ }
+ // held as used up (TestCodexNoCreditsEveryAccount): never sent,
+ // so never asked again
+ code, _ := resetPost(t, New())
+ if tried, _ := b.seen(); code != 429 || tried != "" {
+ t.Fatalf("%d tried %s", code, tried)
+ }
+ })
+}
+
+// 豆子 on Discord: a Codex account out of its allowance that holds credits
+// still answers on them, so it isn't benched until its window renews —
+// neither when ChatGPT refuses it as out of its allowance (it rests a
+// minute, and its usage is read again) nor when it fails otherwise with
+// its windows full (it rests as any account that failed does). One with
+// no credits still rests until its allowance is back.
+func TestCodexCreditsNoLongRest(t *testing.T) {
+ t.Run("refused as out", func(t *testing.T) {
+ codexSignedIn(t, "[email protected]")
+ b := newResetBackend(t, "acct-1")
+ b.credits["acct-1"], b.edge["acct-1"] = true, 5
+ creditsUsage(t, map[string]float64{"[email protected]": 100, "[email protected]": 20}, "[email protected]")
+ if err := provider.SetRouting("codex", provider.Ordered); err != nil {
+ t.Fatal(err)
+ }
+ srv := New()
+ code, body := resetPost(t, srv)
+ if tried, _ := b.seen(); code != 200 || !strings.Contains(body, "pong") || tried != "acct-1,acct-1,acct-2" {
+ t.Fatalf("%d %s tried %s", code, body, tried)
+ }
+ ts := triesOf(srv)
+ if len(ts) != 3 || !ts[0].Credits || ts[1].Rest == nil {
+ t.Fatalf("traced %+v", ts)
+ }
+ if r := ts[1].Rest; r.By != "credits" || r.Why != failQuota || time.Until(r.Until) > fallbackCooldown {
+ t.Fatalf("rests %s for %s until %v", r.By, r.Why, r.Until)
+ }
+ })
+ t.Run("failed otherwise", func(t *testing.T) {
+ codexSignedIn(t, "[email protected]")
+ b := newResetBackend(t)
+ b.fails["acct-1"] = 500
+ creditsUsage(t, map[string]float64{"[email protected]": 100, "[email protected]": 20}, "[email protected]")
+ if err := provider.SetRouting("codex", provider.Ordered); err != nil {
+ t.Fatal(err)
+ }
+ srv := New()
+ if code, body := resetPost(t, srv); code != 200 || !strings.Contains(body, "pong") {
+ t.Fatalf("%d %s", code, body)
+ }
+ ts := triesOf(srv)
+ if len(ts) == 0 || ts[0].Rest == nil || ts[0].Rest.By != "backoff" || time.Until(ts[0].Rest.Until) > longestRetry {
+ t.Fatalf("traced %+v", ts)
+ }
+ })
+ t.Run("no credits", func(t *testing.T) {
+ codexSignedIn(t, "[email protected]")
+ b := newResetBackend(t, "acct-1")
+ creditsUsage(t, map[string]float64{"[email protected]": 100, "[email protected]": 20})
+ if err := provider.SetRouting("codex", provider.Ordered); err != nil {
+ t.Fatal(err)
+ }
+ srv := New()
+ if code, _ := resetPost(t, srv); code != 200 {
+ t.Fatalf("%d", code)
+ }
+ if tried, _ := b.seen(); tried != "acct-1,acct-2" {
+ t.Fatalf("tried %s", tried)
+ }
+ ts := triesOf(srv)
+ if len(ts) == 0 || ts[0].Credits || ts[0].Rest == nil || time.Until(ts[0].Rest.Until) < time.Hour {
+ t.Fatalf("traced %+v", ts)
+ }
+ })
+}
+
+// Every account all but used up: the one that goes on on its credits is
+// asked before one with nothing to go on with, though it has used more,
+// and the trace says it holds them (豆子 on Discord).
+func TestCodexCreditsFirstOfTheSpent(t *testing.T) {
+ codexSignedIn(t, "[email protected]")
+ b := newResetBackend(t, "acct-1")
+ b.credits["acct-2"] = true
+ creditsUsage(t, map[string]float64{"[email protected]": 99, "[email protected]": 100}, "[email protected]")
+ srv := New()
+ code, body := resetPost(t, srv)
+ if tried, _ := b.seen(); code != 200 || !strings.Contains(body, "pong") || tried != "acct-2" {
+ t.Fatalf("%d %s tried %s", code, body, tried)
+ }
+ rs := srv.Trace(t.Context(), 0, 0).Routes
+ if len(rs) == 0 || len(rs[0].Order) != 2 || rs[0].Order[0].Who != "[email protected]" || !rs[0].Order[0].Credits || rs[0].Order[1].Credits {
+ t.Fatalf("order %+v", rs)
+ }
+}
diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go
index 03811ea5e..1de192df9 100644
--- a/internal/gateway/gateway.go
+++ b/internal/gateway/gateway.go
@@ -2337,9 +2337,10 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
roomFor, roomCap := "", 0
// the key or subscription account the last try went to (#557)
providerKeyID, providerKeyName, providerAccount := "", "", ""
- var other *Try // the first failure that wasn't an allowance run out
- var forbade []forbidden // the plain 403s rested so far (forbiddenAlike)
- autoReset := resetFirst != nil // a Codex or Claude reset looked at, once a request
+ var other *Try // the first failure that wasn't an allowance run out
+ var forbade []forbidden // the plain 403s rested so far (forbiddenAlike)
+ autoReset := resetFirst != nil // a Codex or Claude reset looked at, once a request
+ creditsAsked := map[string]bool{} // Codex accounts asked again onto their credits, once each (creditsAgain)
// what the tries' held streams sent the agent ahead of a reply (#751)
kept := &keptAlive{proto: from, told: start}
if k := handedOnOf(r); k != nil {
@@ -3318,6 +3319,21 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
continue
}
}
+ if hw.failed() && !hw.passing && !hw.turnedAway && !hw.refused && !creditsAsked[c.rest] && creditsAgain(c, hw.code(), hw.errBody()) {
+ // a Codex account that holds credits, refused as out of its
+ // allowance: ChatGPT may turn away the turn that uses the
+ // window up before it spends the credits, and answers the
+ // next on them (Zhenzhen on Discord: the running session
+ // ended "usage exhausted" while a new one answered). It is
+ // asked again at once, once a request, rather than the turn
+ // moved off it or ended
+ creditsAsked[c.rest] = true
+ try.Fail, try.Credits = failQuota, true
+ s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try })
+ skipped = append(skipped, c.label()+": "+call.Error+" (asked again, to spend its credits)")
+ i--
+ continue
+ }
if !last && hw.failed() {
if f := failure(hw.code(), hw.errBody()); other == nil && f != failQuota && f != failCredit {
other = &Try{Status: call.Status, Error: call.Error}
diff --git a/internal/gateway/routing.go b/internal/gateway/routing.go
index b534a7bcb..8c460d3ab 100644
--- a/internal/gateway/routing.go
+++ b/internal/gateway/routing.go
@@ -903,6 +903,18 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
case failCredit:
d, r.By = creditRest, "credit"
case failQuota:
+ if c.onCredits() {
+ // a Codex account that holds credits refused as out of its
+ // allowance: at the moment a window is used up ChatGPT may
+ // turn a turn away before it spends the credits, and the next
+ // goes through on them (Zhenzhen on Discord: a new session
+ // answered at once). Benched until the window renews, days
+ // off, it would leave its credits unspent; it is asked again
+ // after a moment instead, and the reading asked for again
+ // below tells whether it still holds them
+ d, r.By = fallbackCooldown, "credits"
+ break
+ }
// out of quota is out until the quota comes back: when the refusal
// says (Claude Code's time, ChatGPT's resets_at — noted by
// keepRetry once the error is put in words), else when the window
@@ -1195,12 +1207,39 @@ func (s *Server) rateRest(c candidate, header http.Header, body []byte, sharedPo
// candidate's model renews; zero otherwise. Used up is all but
// (usedShare), except In order: there an account at 98% is still tried in
// its turn, and one that fails then isn't benched until its week renews
-// unless the vendor said it was out (#530).
+// unless the vendor said it was out (#530). A Codex account that goes on
+// on its credits is never: a window used up doesn't stop it (onCredits).
func (c candidate) full(now time.Time) time.Time {
a, _ := c.allowance()
+ if spendsCredits(c.p, a) {
+ return time.Time{}
+ }
return a.Full(c.model, provider.SpentShareOf(c.p.Routing), now)
}
+// onCredits: c is a Codex account that goes on past its allowance on its
+// credits, as its last reading says, and the user lets it spend them
+// (provider.CodexCredits): its allowance used up, the vendor spends the
+// credits, so it isn't benched until the window renews (豆子 on Discord).
+func (c candidate) onCredits() bool {
+ a, _ := c.allowance()
+ return spendsCredits(c.p, a)
+}
+
+// creditsAgain says whether c, refused with status and body, is asked
+// again at once: a Codex account on its credits (onCredits) that the
+// vendor said is out of its allowance. At the moment a window is used
+// up ChatGPT may refuse the turn before it goes on to the credits; the
+// next request is answered on them.
+func creditsAgain(c candidate, status int, body []byte) bool {
+ return status == http.StatusTooManyRequests && failure(status, body) == failQuota && c.onCredits()
+}
+
+// spendsCredits is onCredits for the account of p, whose allowance is a.
+func spendsCredits(p provider.Provider, a provider.Allowance) bool {
+ return p.Account != nil && p.Account.UsageAgent() == "codex" && a.OnCredits() && provider.CodexCredits(p.Account.User)
+}
+
// allowance is the candidate's allowance as last known: a subscription
// account's, or a key's own windows when magpie reads them (a sub2api
// key's limits, provider.KeyAllowance). ok is false when it isn't known.
@@ -1348,6 +1387,9 @@ type left struct {
// (Allowance.Count): amount of of, in unit
amount, of float64
unit string
+ // credits: a Codex account that goes on on its credits once its
+ // allowance is used up (spendsCredits)
+ credits bool
}
// learns: c is a subscription whose allowance isn't known yet, of an agent
@@ -1420,7 +1462,7 @@ func weighRouted(p provider.Provider, cs []candidate, model string, from provide
amt, of, unit := a.Count(c.model, now)
rs := a.Restarts(now)
wg.lefts[c.allowanceKey()] = left{used: u, renews: r, fullest: a.Fullest(c.model, now), soon: a.Renewal(c.model, now), pace: pc, due: due, amount: amt, of: of, unit: unit,
- restarts: rs, dueRestart: !rs.IsZero() && due.Equal(rs)} // one not known counts as unused
+ restarts: rs, dueRestart: !rs.IsZero() && due.Equal(rs), credits: spendsCredits(c.p, a)} // one not known counts as unused
}
}
if len(cs) < 2 {
@@ -1441,7 +1483,7 @@ func weighRouted(p provider.Provider, cs []candidate, model string, from provide
pace = provider.FreshPace
}
seats[i] = routes.Seat{Key: c.rest, Account: c.p.Account != nil, Known: known, Learns: learns(c, wg.lefts),
- Used: l.used, Soon: l.soon, Pace: pace, Weight: keyWeight(c), Resting: resting(c), Tokens: routed.used[c.restKey()].now(now)}
+ Used: l.used, Soon: l.soon, Pace: pace, Weight: keyWeight(c), Resting: resting(c), Tokens: routed.used[c.restKey()].now(now), Credits: l.credits}
}
routed.Unlock()
if st.ByUse {
diff --git a/internal/gateway/trace.go b/internal/gateway/trace.go
index 477703727..cca8e69d0 100644
--- a/internal/gateway/trace.go
+++ b/internal/gateway/trace.go
@@ -282,6 +282,10 @@ type Weighed struct {
// NoCredits: held at 100% (Capped), a Codex account the user set not
// to spend its credits once its allowance is used up
NoCredits bool `json:"noCredits,omitempty"`
+ // Credits: a Codex account that goes on past its allowance on its
+ // credits, and is let spend them: all but used up, it still answers,
+ // and goes before those used up with nothing to go on with
+ Credits bool `json:"credits,omitempty"`
// Rank: its place in its provider's own list of accounts or keys, the
// order the provider's page shows and a drag sets (#217); routing may
// weigh them in another
@@ -374,6 +378,10 @@ type Try struct {
// Codex resets was spent by itself (the user's setting) — on Who, and
// what spending it did — and the request asked again
Reset *AutoReset `json:"reset,omitempty"`
+ // Credits: a Codex account that holds credits, refused as out of its
+ // allowance, was asked again at once, for the vendor to spend its
+ // credits on the turn (creditsAgain)
+ Credits bool `json:"credits,omitempty"`
// Auto: the models Copilot's Auto picked for it, in turn — where each
// pick came from and Copilot's refusal of it — Model being "auto"
Auto []provider.AutoPick `json:"auto,omitempty"`
@@ -428,7 +436,7 @@ func weighed(c candidate, p provider.Provider, wg weighing, fallback bool, from
w.Kind = "provider"
}
if l, ok := wg.lefts[c.allowanceKey()]; ok {
- w.Known, w.Used, w.Renews, w.Pace = true, l.used, l.renews, l.pace
+ w.Known, w.Used, w.Renews, w.Pace, w.Credits = true, l.used, l.renews, l.pace, l.credits
if !l.fullest.IsZero() {
ur := l.fullest
w.UsedRenews = &ur
diff --git a/internal/gui/assets/plugins/routing/client.js b/internal/gui/assets/plugins/routing/client.js
index 62d25a4e5..dc7f80ca3 100644
--- a/internal/gui/assets/plugins/routing/client.js
+++ b/internal/gui/assets/plugins/routing/client.js
@@ -552,6 +552,9 @@ function routingPage(part) {
case "window": return t("Its allowance is used up: it rests until that renews, at {time}", { time });
case "resets": return t("It rests until {time}, when the vendor says the limit resets", { time });
case "quota": return t("It rests 15 minutes: out of quota, with no word of when it resets");
+ // a Codex account that holds credits, refused again once asked
+ // again onto them: not benched until its window renews
+ case "credits": return t("It holds credits, so it rests only {d} and its usage is read again, not until its allowance renews", { d });
case "verify": return t("The vendor wants the account verified first: it rests half an hour, or until you say it's verified");
// its provider set never to rest after an unknown failure
// (`magpie provider rest-cap <id> off`): asked again next time
@@ -632,6 +635,7 @@ function routingPage(part) {
if (f.kind !== "account" && !f.known) return t("{who} goes first.", { who: w });
if (f.learns && peers.some((p) => p.known)) return t("{who} goes first: what it has left isn't known yet, and its answer tells — kept behind those known, it would never answer and never be known.", { who: w });
if (!f.known && !peers.some((p) => p.known)) return t("The vendor hasn't said yet what these accounts have left, so they go in their order: {who} first.", { who: w });
+ if (group(f) === "spent" && f.credits) return t("Every account is all but used up, so {who} goes first: it goes on on its credits, which the vendor spends once its allowance is used up.", { who: w });
if (group(f) !== "fine") return r.order.some((x) => x.kind !== "account" && x.known)
? t("Every key and account here is at 90% or more of its allowance, so the one with the most left goes first: {who}, at {n}.", { who: w, n: pct(f.used) })
: t("Every account is at 90% or more of its allowance, so the one with the most left goes first: {who}, at {n}.", { who: w, n: pct(f.used) });
@@ -674,6 +678,7 @@ function routingPage(part) {
for (const x of r.order.slice(1)) {
if (x.sunk && !x.rest) out.push(t("{who} was rate limited at {time} while it had quota left, so it went to the back: it comes round again once those ahead of it are rate limited in turn.", { who: who(x), time: clock(x.sunk) }));
else if (x.rest) out.push(t("{who} is resting — {why}, {when} — so it waits at the back.", { who: who(x), why: `${x.rest.status} · ${failWord(x.rest.why)}`, when: restWhen(x.rest, at(r.time)) }));
+ else if (smart(x) && x.known && group(x) === "spent" && x.credits) out.push(t("{who} is at {n} of its allowance but goes on on its credits — ahead of those used up with nothing to go on with, after those with allowance left.", { who: who(x), n: pct(x.used) }));
else if (smart(x) && x.known && group(x) === "spent") out.push(t("{who} is at {n} — all but used up, it answers only when nothing else can.", { who: who(x), n: pct(x.used) }));
else if (smart(x) && x.known && group(x) === "low") out.push(t("{who} is at {n} — kept for when the others can't.", { who: who(x), n: pct(x.used) }));
else if (smart(x) && x.learns && someKnown) out.push(t("{who}: what it has left isn't known yet, and its answer tells, so it goes before those known.", { who: who(x) }));
@@ -999,6 +1004,9 @@ function routingPage(part) {
// it was spent for is the one that then answered
const spent = tr.reset && tr.status < 400
? t("Its week was used up, so one of {account}'s Codex resets was used by itself first.", { account: userShown(w?.provider, tr.reset.who) }) + " "
+ // Codex's own sign-in asked again onto its credits: the one try
+ : tr.credits && tr.status < 400
+ ? t("Its allowance had just run out and it was refused, so it was asked again at once to spend its credits.") + " "
: "";
if (tr.status < 400 && tr.fail)
return tr.rest
@@ -1014,6 +1022,11 @@ function routingPage(part) {
if (tr.reset)
return t("{who} answered {status}: its week is used up and nobody else could take the request, so one of {account}'s Codex resets was used by itself and the request is asked again, before any of the reply reaches {agent}.",
{ who: name, status: tr.status, account: userShown(w?.provider, tr.reset.who), agent });
+ // a Codex account that holds credits, refused as its allowance ran
+ // out: asked again at once, for the vendor to spend its credits
+ if (tr.credits)
+ return t("{who} answered {status}: its allowance had just run out, but the account holds credits, which the vendor spends once the allowance is used up, so it is asked again at once, before any of the reply reaches {agent}.",
+ { who: name, status: tr.status, agent });
if (tr.fail === "canceled")
return t("{agent} canceled the request while {who} was answering: nobody failed, so nobody rests and nobody else is asked.", { who: name, agent });
if (tr.fail === "auth")
@@ -1805,7 +1818,7 @@ function routingPage(part) {
else if (tr.done && !tr.rest && !tr.again && !tr.earlier && r.done && tr === r.tries[r.tries.length - 1]) gave.set(tryseat(r, tr), tr);
// one the gateway went on from says what it answered, and that the
// request went on, not that the agent got it
- else if (tr.done && !tr.rest && !tr.again && tr !== r.tries[r.tries.length - 1]) went.set(tryseat(r, tr), tr);
+ else if (tr.done && !tr.rest && !tr.again && !tr.credits && tr !== r.tries[r.tries.length - 1]) went.set(tryseat(r, tr), tr);
if (tr.rest) restAt(tr.id, modelOf(tr), tr.rest);
}
// it answered: whatever rest it began in is over
@@ -1824,7 +1837,8 @@ function routingPage(part) {
else if (w.sunk) s = t("rate limited at {time} · at the back", { time: clock(w.sunk) });
else if (w.known) {
const soon = usedRenews(w);
- s = !w.routing && w.used >= 98 ? quota(w, "{n} used · all but used up", "{n} left · all but used up")
+ s = !w.routing && w.used >= 98 && w.credits ? quota(w, "{n} used · on credits", "{n} left · on credits")
+ : !w.routing && w.used >= 98 ? quota(w, "{n} used · all but used up", "{n} left · all but used up")
: !w.routing && w.used >= 90 ? quota(w, "{n} used · kept for last", "{n} left · kept for last")
: soon ? quota(w, "{n} used · renews in {d}", "{n} left · renews in {d}", { d: dur(soon - n) }) : quota(w, "{n} used", "{n} left");
} else if (w.kind === "account") s = t("what's left not known yet");
@@ -3343,7 +3357,7 @@ function routingPage(part) {
renderAll();
dot.classList.add("back", "err");
back = bird("res");
- if (!tr.rest && (!tr.again || tr.gone)) { // that was the answer: the agent gets the error, or has gone
+ if (!tr.rest && !tr.credits && (!tr.again || tr.gone)) { // that was the answer: the agent gets the error, or has gone
await fly(dot, back, home(row, A), 1350);
flying.delete(dot);
away(back);
diff --git a/internal/gui/assets/plugins/routing/i18n.js b/internal/gui/assets/plugins/routing/i18n.js
index 2cb7dd3cb..78999c8af 100644
--- a/internal/gui/assets/plugins/routing/i18n.js
+++ b/internal/gui/assets/plugins/routing/i18n.js
@@ -521,6 +521,13 @@ magpie.strings({
"{n} used · kept for last": "已用 {n} · 留到最后",
"{n} used · renews in {d}": "已用 {n} · {d}后重置",
"{n} left · all but used up": "剩余 {n} · 几乎用尽",
+ "Its allowance had just run out and it was refused, so it was asked again at once to spend its credits.": "额度刚用完被拒,于是立即重试一次,改用其 Credits。",
+ "{who} answered {status}: its allowance had just run out, but the account holds credits, which the vendor spends once the allowance is used up, so it is asked again at once, before any of the reply reaches {agent}.": "{who} 返回 {status}:额度刚刚用完,但该账号有 Credits,额度用完后供应商会改扣 Credits,因此立即重试,回复不会传到 {agent}。",
+ "It holds credits, so it rests only {d} and its usage is read again, not until its allowance renews": "它有 Credits,只休息 {d} 并重新读取用量,不必等到额度恢复",
+ "{who} is at {n} of its allowance but goes on on its credits — ahead of those used up with nothing to go on with, after those with allowance left.": "{who} 额度已用 {n},但仍可用 Credits 继续 —— 排在额度未用完的账号之后、额度用完且无 Credits 的账号之前。",
+ "{n} used · on credits": "已用 {n} · 使用 Credits",
+ "{n} left · on credits": "剩余 {n} · 使用 Credits",
+ "Every account is all but used up, so {who} goes first: it goes on on its credits, which the vendor spends once its allowance is used up.": "所有账号额度都几乎用尽,因此 {who} 排第一:额度用完后供应商会改扣其 Credits,可以继续使用。",
"{n} left · kept for last": "剩余 {n} · 留到最后",
"{n} left · renews in {d}": "剩余 {n} · {d}后重置",
"{n} left at {time}; it has renewed since": "{time} 时剩余 {n},之后已重置",
@@ -1055,6 +1062,13 @@ magpie.strings({
"{n} used · kept for last": "已用 {n} · 留到最後",
"{n} used · renews in {d}": "已用 {n} · {d}後重置",
"{n} left · all but used up": "剩餘 {n} · 幾乎用盡",
+ "Its allowance had just run out and it was refused, so it was asked again at once to spend its credits.": "額度剛用完被拒,於是立即重試一次,改用其 Credits。",
+ "{who} answered {status}: its allowance had just run out, but the account holds credits, which the vendor spends once the allowance is used up, so it is asked again at once, before any of the reply reaches {agent}.": "{who} 回傳 {status}:額度剛剛用完,但該帳號有 Credits,額度用完後供應商會改扣 Credits,因此立即重試,回覆不會傳到 {agent}。",
+ "It holds credits, so it rests only {d} and its usage is read again, not until its allowance renews": "它有 Credits,只休息 {d} 並重新讀取用量,不必等到額度恢復",
+ "{who} is at {n} of its allowance but goes on on its credits — ahead of those used up with nothing to go on with, after those with allowance left.": "{who} 額度已用 {n},但仍可用 Credits 繼續 —— 排在額度未用完的帳號之後、額度用完且無 Credits 的帳號之前。",
+ "{n} used · on credits": "已用 {n} · 使用 Credits",
+ "{n} left · on credits": "剩餘 {n} · 使用 Credits",
+ "Every account is all but used up, so {who} goes first: it goes on on its credits, which the vendor spends once its allowance is used up.": "所有帳號額度都幾乎用盡,因此 {who} 排第一:額度用完後供應商會改扣其 Credits,可以繼續使用。",
"{n} left · kept for last": "剩餘 {n} · 留到最後",
"{n} left · renews in {d}": "剩餘 {n} · {d}後重置",
"{n} left at {time}; it has renewed since": "{time} 時剩餘 {n},之後已重置",
@@ -1587,6 +1601,13 @@ magpie.strings({
"{n} used · kept for last": "使用済み {n} · 最後に回す",
"{n} used · renews in {d}": "使用済み {n} · {d} 後に更新",
"{n} left · all but used up": "残り {n} · ほぼ使い切り",
+ "Its allowance had just run out and it was refused, so it was asked again at once to spend its credits.": "利用枠を使い切った直後に拒否されたため、クレジットを使うようすぐにもう一度依頼しました。",
+ "{who} answered {status}: its allowance had just run out, but the account holds credits, which the vendor spends once the allowance is used up, so it is asked again at once, before any of the reply reaches {agent}.": "{who} は {status} を返しました。利用枠を使い切ったところですが、このアカウントにはクレジットがあり、利用枠を使い切るとベンダーはクレジットを使うため、応答が {agent} に届く前にすぐもう一度依頼します。",
+ "It holds credits, so it rests only {d} and its usage is read again, not until its allowance renews": "クレジットがあるため {d} だけ休み、使用量を読み直します。利用枠の回復までは待ちません",
+ "{who} is at {n} of its allowance but goes on on its credits — ahead of those used up with nothing to go on with, after those with allowance left.": "{who} は利用枠の {n} を使っていますが、クレジットで続けられます — 利用枠が残っているアカウントの後、使い切ってクレジットもないアカウントの前です。",
+ "{n} used · on credits": "{n} 使用 · クレジット利用",
+ "{n} left · on credits": "残り {n} · クレジット利用",
+ "Every account is all but used up, so {who} goes first: it goes on on its credits, which the vendor spends once its allowance is used up.": "どのアカウントも利用枠をほぼ使い切っているため、{who} が先頭です。利用枠を使い切るとベンダーがクレジットを使うので、続けられます。",
"{n} left · kept for last": "残り {n} · 最後に回す",
"{n} left · renews in {d}": "残り {n} · {d} 後に更新",
"{n} left at {time}; it has renewed since": "{time} 時点で残り {n}。その後更新されました",
@@ -2121,6 +2142,13 @@ magpie.strings({
"{n} used · kept for last": "{n} verbraucht · für zuletzt aufgespart",
"{n} used · renews in {d}": "{n} verbraucht · erneuert sich in {d}",
"{n} left · all but used up": "{n} übrig · fast aufgebraucht",
+ "Its allowance had just run out and it was refused, so it was asked again at once to spend its credits.": "Sein Kontingent war gerade aufgebraucht und es wurde abgewiesen, also wurde es sofort erneut gefragt, um seine Credits zu nutzen.",
+ "{who} answered {status}: its allowance had just run out, but the account holds credits, which the vendor spends once the allowance is used up, so it is asked again at once, before any of the reply reaches {agent}.": "{who} antwortete {status}: sein Kontingent war gerade aufgebraucht, aber das Konto hat Credits, die der Anbieter nach dem Kontingent verbraucht, also wird es sofort erneut gefragt, bevor etwas von der Antwort {agent} erreicht.",
+ "It holds credits, so it rests only {d} and its usage is read again, not until its allowance renews": "Es hat Credits, ruht also nur {d} und seine Nutzung wird neu gelesen, nicht bis sein Kontingent erneuert wird",
+ "{who} is at {n} of its allowance but goes on on its credits — ahead of those used up with nothing to go on with, after those with allowance left.": "{who} hat {n} seines Kontingents genutzt, macht aber mit seinen Credits weiter — vor denen, die aufgebraucht sind und nichts mehr haben, nach denen mit verbleibendem Kontingent.",
+ "{n} used · on credits": "{n} genutzt · auf Credits",
+ "{n} left · on credits": "{n} übrig · auf Credits",
+ "Every account is all but used up, so {who} goes first: it goes on on its credits, which the vendor spends once its allowance is used up.": "Alle Konten sind fast aufgebraucht, also kommt {who} zuerst: es macht mit seinen Credits weiter, die der Anbieter verbraucht, sobald sein Kontingent aufgebraucht ist.",
"{n} left · kept for last": "{n} übrig · für zuletzt aufgespart",
"{n} left · renews in {d}": "{n} übrig · erneuert sich in {d}",
"{n} left at {time}; it has renewed since": "{n} übrig um {time}; seitdem erneuert",
diff --git a/internal/gui/tests/routing-codex-credits-again.test.cjs b/internal/gui/tests/routing-codex-credits-again.test.cjs
new file mode 100644
index 000000000..aef438965
--- /dev/null
+++ b/internal/gui/tests/routing-codex-credits-again.test.cjs
@@ -0,0 +1,110 @@
+// Run with Node's test runner and Playwright on the module path; see README.md.
+// A Codex account that holds credits goes on past its allowance (豆子 and
+// Zhenzhen on Discord): the Routing page says why it went first of the
+// accounts all but used up, that the one beside it without credits waits
+// behind it, and that its refusal as its allowance ran out was asked again
+// at once onto its credits rather than ended for the agent, the try that
+// then answered told as the one the agent got. English, Chinese, Japanese
+// and German, Chromium and WebKit; no backend, the API is faked here.
+const assert = require("node:assert/strict");
+const fs = require("node:fs/promises");
+const path = require("node:path");
+const { test } = require("node:test");
+const { chromium, webkit } = require("playwright");
+
+const assets = path.resolve(__dirname, "../assets");
+const now = new Date();
+const later = new Date(now.getTime() + 3 * 864e5).toISOString();
+const day = [now.getFullYear(), now.getMonth() + 1, now.getDate()].map((n) => String(n).padStart(2, "0")).join("-");
+const at = new Date(now.getTime() - 60e3).toISOString();
+const credits = { id: "codex@[email protected]", provider: "codex", name: "Codex", kind: "account", who: "[email protected]", model: "gpt-5.5",
+ known: true, used: 100, renews: [later], usedRenews: later, credits: true };
+const spent = { id: "codex@[email protected]", provider: "codex", name: "Codex", kind: "account", who: "[email protected]", model: "gpt-5.5",
+ known: true, used: 99, renews: [later], usedRenews: later };
+const routes = [{ id: 7, seq: 7, time: at, agent: "codex", model: "gpt-5.5", provider: "codex", order: [credits, spent],
+ tries: [
+ { id: credits.id, model: "gpt-5.5", start: at, done: true, status: 429, ms: 300, fail: "quota", credits: true },
+ { id: credits.id, model: "gpt-5.5", start: at, done: true, status: 200, ms: 400 },
+ ], done: true, status: 200, ms: 700 },
+ // one with allowance left goes first; the credits account is next, ahead
+ // of the one used up with none
+ { id: 8, seq: 8, time: new Date(now.getTime() - 30e3).toISOString(), agent: "codex", model: "gpt-5.5", provider: "codex",
+ order: [{ ...spent, id: "codex@[email protected]", who: "[email protected]", used: 20 }, credits, spent],
+ tries: [{ id: "codex@[email protected]", model: "gpt-5.5", start: at, done: true, status: 200, ms: 400 }], done: true, status: 200, ms: 400 }];
+
+function serve(lang) {
+ const settings = () => ({ theme: "light", lang, tray: "panel", quotaLeft: false, currency: "usd" });
+ return async (route) => {
+ const req = route.request(), url = new URL(req.url());
+ const json = (data) => route.fulfill({ json: data });
+ if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:false};` });
+ if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
+ if (url.pathname === "/api/state") return json({ agents: [{ id: "codex", name: "Codex", path: "/test/config.toml", fields: [] }], profiles: [], settings: settings() });
+ if (url.pathname === "/api/settings") return json(settings());
+ if (url.pathname === "/api/gateway/trace") {
+ if (url.searchParams.get("wait")) await new Promise((r) => setTimeout(r, 20e3));
+ return json({ mine: true, now: now.toISOString(), seq: 1, totals: { requests: 0, rerouted: 0, errors: 0 }, routes: [] });
+ }
+ if (url.pathname === "/api/gateway/history") {
+ const d = url.searchParams.get("day");
+ return json({ cut: false, days: [{ day, requests: routes.length }], routes: d ? routes : [] });
+ }
+ if (url.pathname === "/api/providers") return json({ providers: [], gateway: { running: true, window: true } });
+ if (url.pathname === "/api/groups") return json({ groups: [], models: [] });
+ if (url.pathname === "/api/plugins") return json({ plugins: [] });
+ if (url.pathname.startsWith("/api/")) return json({});
+ const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
+ const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
+ await route.fulfill({ body: await fs.readFile(file), contentType });
+ };
+}
+
+// a few words of each line, in each language, enough to know it
+const words = {
+ en: { first: "goes on on its credits, which the vendor spends", aside: "goes on on its credits — ahead of those used up", again: "so it is asked again at once", answered: "never saw the 1 that failed first" },
+ zh: { first: "额度用完后供应商会改扣其 Credits", aside: "但仍可用 Credits 继续", again: "因此立即重试", answered: "" },
+ ja: { first: "利用枠を使い切るとベンダーがクレジットを使うので", aside: "クレジットで続けられます", again: "すぐもう一度依頼します", answered: "" },
+ de: { first: "es macht mit seinen Credits weiter", aside: "macht aber mit seinen Credits weiter", again: "also wird es sofort erneut gefragt", answered: "" },
+};
+
+const keys = [
+ "Its allowance had just run out and it was refused, so it was asked again at once to spend its credits.",
+ "{who} answered {status}: its allowance had just run out, but the account holds credits, which the vendor spends once the allowance is used up, so it is asked again at once, before any of the reply reaches {agent}.",
+ "It holds credits, so it rests only {d} and its usage is read again, not until its allowance renews",
+ "{who} is at {n} of its allowance but goes on on its credits — ahead of those used up with nothing to go on with, after those with allowance left.",
+ "{n} used · on credits", "{n} left · on credits",
+ "Every account is all but used up, so {who} goes first: it goes on on its credits, which the vendor spends once its allowance is used up.",
+];
+
+for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
+ for (const lang of ["en", "zh", "ja", "de"]) {
+ const w = words[lang];
+ test(`${engine} ${lang}: a Codex account on its credits goes first of the spent, and its refusal at the edge is asked again`, async (t) => {
+ const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
+ t.after(() => browser.close());
+ const errors = [];
+ const page = await (await browser.newContext({ viewport: { width: 1100, height: 760 }, reducedMotion: "reduce" })).newPage();
+ page.setDefaultTimeout(5000);
+ page.on("pageerror", (e) => errors.push(e.message));
+ await page.route("**/*", serve(lang));
+ await page.goto("http://magpie.test/?view=routing");
+ await page.locator(".rt-days .rt-day").nth(1).click();
+ let steps = "";
+ await page.locator(".rt-req").nth(1).waitFor();
+ assert.equal(await page.locator(".rt-req").count(), 2);
+ for (const i of [0, 1]) {
+ await page.locator(".rt-req").nth(i).click();
+ await page.waitForTimeout(300);
+ steps += (await page.locator(".rt-steps li").evaluateAll((ls) => ls.map((l) => l.textContent.replace(/\s+/g, " ")))).join("\n") + "\n";
+ }
+ for (const k of ["first", "aside", "again", "answered"]) if (w[k]) assert(steps.includes(w[k]), `${k}: ${steps}`);
+ // the account on credits is never told as all but used up
+ if (lang === "en") assert(!steps.includes("[email protected] is at 100% — all but used up"), steps);
+ if (lang !== "en") {
+ const missing = await page.evaluate(([keys, lang]) => keys.filter((k) => !I18N[lang][k]), [keys, lang]);
+ assert.deepEqual(missing, [], `every string has its ${lang}`);
+ }
+ assert.deepEqual(errors, []);
+ });
+ }
+}
diff --git a/internal/provider/codex_credits_test.go b/internal/provider/codex_credits_test.go
index 7ced5b9b9..ce56929d2 100644
--- a/internal/provider/codex_credits_test.go
+++ b/internal/provider/codex_credits_test.go
@@ -14,19 +14,24 @@ import (
// The credits a ChatGPT account holds besides its windows are its balance
// (#571), shown when it has some to spend and they are not unlimited.
+// Routing is told the account goes on past its windows on them (onCredits:
+// every window's Credits, and the allowance's OnCredits) when it holds
+// some or they are unlimited, as the Codex app reads has_credits; a
+// balance said and spent is none.
func TestCodexCreditsBalance(t *testing.T) {
for _, c := range []struct {
- name string
- credits any
- want string
+ name string
+ credits any
+ want string
+ onCredits bool
}{
- {"none said", nil, ""},
- {"held", map[string]any{"has_credits": true, "unlimited": false, "balance": "1234.5"}, "1234.5 credits"},
- {"a few", map[string]any{"has_credits": true, "unlimited": false, "balance": "42"}, "42 credits"},
- {"none held", map[string]any{"has_credits": false, "unlimited": false, "balance": "0"}, ""},
- {"spent", map[string]any{"has_credits": true, "unlimited": false, "balance": "0"}, ""},
- {"unlimited", map[string]any{"has_credits": true, "unlimited": true, "balance": ""}, ""},
- {"unreadable", map[string]any{"has_credits": true, "balance": "lots"}, ""},
+ {"none said", nil, "", false},
+ {"held", map[string]any{"has_credits": true, "unlimited": false, "balance": "1234.5"}, "1234.5 credits", true},
+ {"a few", map[string]any{"has_credits": true, "unlimited": false, "balance": "42"}, "42 credits", true},
+ {"none held", map[string]any{"has_credits": false, "unlimited": false, "balance": "0"}, "", false},
+ {"spent", map[string]any{"has_credits": true, "unlimited": false, "balance": "0"}, "", false},
+ {"unlimited", map[string]any{"has_credits": true, "unlimited": true, "balance": ""}, "", true},
+ {"unreadable", map[string]any{"has_credits": true, "balance": "lots"}, "", true},
} {
t.Run(c.name, func(t *testing.T) {
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -48,6 +53,9 @@ func TestCodexCreditsBalance(t *testing.T) {
if credits != c.want {
t.Errorf("credits %q, want %q", credits, c.want)
}
+ if windows[0].Credits != c.onCredits || allowanceOf(windows, time.Now()).OnCredits() != c.onCredits {
+ t.Errorf("on credits %v / %v, want %v", windows[0].Credits, allowanceOf(windows, time.Now()).OnCredits(), c.onCredits)
+ }
})
}
}
diff --git a/internal/provider/routing.go b/internal/provider/routing.go
index 7d19e817b..3058c0d1a 100644
--- a/internal/provider/routing.go
+++ b/internal/provider/routing.go
@@ -182,6 +182,22 @@ type Limit struct {
// RunsOut is when the soonest of what the window has left is lost,
// apart from its reset (QuotaWindow.RunsOutAt): zero when not known.
RunsOut time.Time
+ // Credits: the account goes on past this window on its credits
+ // (QuotaWindow.Credits)
+ Credits bool
+}
+
+// OnCredits says whether the account goes on past its allowance on its
+// credits, as its last reading says (a ChatGPT account holding them,
+// QuotaWindow.Credits): a window used up doesn't stop it. Whether it is
+// let spend them is the user's (CodexCredits).
+func (a Allowance) OnCredits() bool {
+ for _, l := range a {
+ if l.Credits {
+ return true
+ }
+ }
+ return false
}
// Restarts is when an auto-used reset starts the account's windows again,
@@ -736,7 +752,7 @@ func allowanceOf(ws []QuotaWindow, now time.Time) Allowance {
if w.Aside {
continue
}
- l := Limit{Used: w.Used, Span: w.Span, Model: w.Model, Amount: w.Amount, Of: w.Limit, Unit: w.Unit, matches: w.matches, partial: w.partial, Name: w.Name}
+ l := Limit{Used: w.Used, Span: w.Span, Model: w.Model, Amount: w.Amount, Of: w.Limit, Unit: w.Unit, matches: w.matches, partial: w.partial, Name: w.Name, Credits: w.Credits}
if ids := families[w.Model]; ids != nil && w.Family != "" && w.matches == nil {
l.Model = ""
l.matches = func(model string) bool { return ids[model] }
diff --git a/internal/provider/subscription_usage.go b/internal/provider/subscription_usage.go
index 987756110..1d501ebab 100644
--- a/internal/provider/subscription_usage.go
+++ b/internal/provider/subscription_usage.go
@@ -65,6 +65,12 @@ type QuotaWindow struct {
Span time.Duration `json:"-"`
Model string `json:"-"`
Aside bool `json:"-"`
+ // Credits is set on each window of a ChatGPT account that holds
+ // credits (has_credits with a balance left, or unlimited): using the
+ // window up doesn't stop it, the vendor spends the credits instead
+ // (codexGoesOnCredits). Routing reads it (Allowance.OnCredits); kept
+ // with the reading, so one shown from before still says it.
+ Credits bool `json:"credits,omitempty"`
// Capped is set, for the GUI only (WithCapped), on a window an
// account's usage cap counts: neither Aside nor unlimited. One model's
// own counts for that model (CapHeld), so it is marked like the rest.
@@ -953,9 +959,33 @@ func codexWindows(ctx context.Context, token, accountID string) (plan string, ou
if data.RateLimit.Secondary != nil {
out = append(out, data.RateLimit.Secondary.window())
}
+ if codexGoesOnCredits(data) {
+ for i := range out {
+ out[i].Credits = true
+ }
+ }
return data.PlanType, out, resets, credits, codexHeld(data), nil
}
+// codexGoesOnCredits reports whether the ChatGPT account goes on past its
+// allowance on credits, as the Codex app reads it (codexHeld): it has
+// them (has_credits) or they are unlimited. A balance said and spent (0
+// or less) is none left, whatever has_credits says.
+func codexGoesOnCredits(u codexUsage) bool {
+ c := u.Credits
+ if c == nil {
+ return false
+ }
+ if c.Unlimited {
+ return true
+ }
+ if !c.Has {
+ return false
+ }
+ n, err := strconv.ParseFloat(strings.TrimSpace(c.Balance), 64)
+ return err != nil || n > 0
+}
+
// codexUsage is what /wham/usage tells of a ChatGPT account.
type codexUsage struct {
PlanType string `json:"plan_type"`
diff --git a/internal/routes/routes.go b/internal/routes/routes.go
index b501f1171..9cc414e1c 100644
--- a/internal/routes/routes.go
+++ b/internal/routes/routes.go
@@ -46,6 +46,10 @@ type Seat struct {
// its vendor serves that model at no cost to the subscription's
// allowance or credits (catalog.Model's Free). FreeFirst goes by it.
Free bool
+ // Credits: a Codex account that goes on past its allowance on its
+ // credits, and is let spend them. All but used up, it goes before
+ // those used up with nothing to go on with.
+ Credits bool
}
// Strategy orders a pool.
diff --git a/internal/routes/routes_test.go b/internal/routes/routes_test.go
index 2af07313c..5a0575a8c 100644
--- a/internal/routes/routes_test.go
+++ b/internal/routes/routes_test.go
@@ -149,3 +149,22 @@ func TestWeighted(t *testing.T) {
t.Fatalf("by weight: %v, want a 4, b 2, c 2", firsts)
}
}
+
+// Of those all but used up, one that goes on on its credits (a Codex
+// account holding them) goes before those with nothing left, though it
+// has used more, in Smart, Weekly pace and Least used alike; still after
+// every one with room or running low, its allowance spent before its
+// credits are.
+func TestCreditsFirstOfTheSpent(t *testing.T) {
+ s := []Seat{
+ {Key: "spent", Used: 98},
+ {Key: "credits", Used: 100, Credits: true},
+ {Key: "low", Used: 92, Credits: true},
+ {Key: "fine", Used: 20},
+ }
+ for name, st := range map[string]Strategy{"smart": Smart, "pace": Pace, "least used": LeastUsed} {
+ if got := keys(s, Apply(st, "p", s)); !slices.Equal(got, []string{"fine", "low", "credits", "spent"}) {
+ t.Errorf("%s: %v", name, got)
+ }
+ }
+}
diff --git a/internal/routes/strategies.go b/internal/routes/strategies.go
index c5312e480..bc7d0ea30 100644
--- a/internal/routes/strategies.go
+++ b/internal/routes/strategies.go
@@ -47,7 +47,8 @@ func identity(n int) []int {
// the same (#718). Only holding one, or one that runs out after the week
// renews, changes nothing. Those alike stay in their order, keeping the
// vendor's prompt cache warm. Past that, whichever has the most left, and
-// one all but used up only when nothing else can take it. Only the windows
+// one all but used up only when nothing else can take it, one that goes
+// on on its credits first of those (spentBefore). Only the windows
// that count the model do: Opus's own weekly allowance being used up leaves
// Sonnet alone.
//
@@ -90,12 +91,21 @@ var Smart = Strategy{Order: func(_ string, seats []Seat) []int {
}
return false
})
- for _, l := range [][]int{low, spent} {
- sort.SliceStable(l, func(i, j int) bool { return seats[l[i]].Used < seats[l[j]].Used })
- }
+ sort.SliceStable(low, func(i, j int) bool { return seats[low[i]].Used < seats[low[j]].Used })
+ sort.SliceStable(spent, func(i, j int) bool { return spentBefore(seats[spent[i]], seats[spent[j]]) })
return append(append(fine, low...), spent...)
}}
+// spentBefore orders two seats all but used up: one that goes on on its
+// credits first (a Codex account holding them answers past its allowance;
+// one with nothing left is refused), then the one with the most left.
+func spentBefore(a, b Seat) bool {
+ if a.Credits != b.Credits {
+ return a.Credits
+ }
+ return a.Used < b.Used
+}
+
// Ordered tries a pool in its order: the first until it can't answer.
var Ordered = Strategy{Exact: true, Order: func(_ string, seats []Seat) []int { return identity(len(seats)) }}
@@ -115,11 +125,15 @@ var Rotate = Strategy{Order: func(pool string, seats []Seat) []int {
// LeastUsed puts a subscription, or a key that reads its own windows, by the
// share of its allowance used, as the vendor says; then, and for other
-// keys, by what magpie sent it lately.
+// keys, by what magpie sent it lately. Of those all but used up, one that
+// goes on on its credits goes first (spentBefore).
var LeastUsed = Strategy{ByUse: true, Order: func(_ string, seats []Seat) []int {
idx := identity(len(seats))
sort.SliceStable(idx, func(a, b int) bool {
sa, sb := seats[idx[a]], seats[idx[b]]
+ if ta, tb := tier(sa.Used), tier(sb.Used); ta == 2 && tb == 2 && sa.Credits != sb.Credits {
+ return sa.Credits
+ }
if sa.Used != sb.Used {
return sa.Used < sb.Used
}
@@ -136,7 +150,8 @@ var LeastUsed = Strategy{ByUse: true, Order: func(_ string, seats []Seat) []int
// are Smart's: one at 90% or more of any window that counts the model — the
// five hours too, a rate cap it would be sent into before its allowance was
// read again — waits until the others can't answer, one all but used up
-// until nothing else can; each tier by the share used. Among the rest, one
+// until nothing else can; each tier by the share used, one all but used up
+// that goes on on its credits first (spentBefore). Among the rest, one
// not known that tells what it has left as it answers goes first while it
// isn't, as in Smart, else it would never be known; then the pace, those
// alike within a tenth by what magpie sent them lately, then in their
@@ -176,6 +191,8 @@ var Pace = Strategy{ByUse: true, Order: func(_ string, seats []Seat) []int {
sa, sb := seats[idx[a]], seats[idx[b]]
if ta, tb := tier(sa.Used), tier(sb.Used); ta != tb {
return ta < tb
+ } else if ta == 2 && sa.Credits != sb.Credits {
+ return sa.Credits
} else if ta > 0 {
if sa.Used != sb.Used {
return sa.Used < sb.Used