Files
magpie/internal/provider/codex_credits.go
T
yetone ec104b8625 gateway: a Codex account can spend its credits before magpie moves to another account (jojonas_xyz on X)
jojonas_xyz on X: "我账号额度用完了自动给我切了别的号…找不到怎么关掉设置…你怎么知道
我不是想用 credits 呢". (Roughly: "my account ran out of quota and magpie
switched me to another account by itself; I can't find a setting to turn
that off; how do you know I didn't want to use the credits?")

Cause. At 90% or 98% of a window, smart routing's tier put the account
behind every account that still had room, whether or not it had ChatGPT
credits. Codex's own sign-in (NextLogin / SwitchWhenSpent) moved at the
spent share in the same way. The account's credits were spent only once
every other account was used up, and the switch was a log line. The Use
credits switches that existed (on, and "Only after other accounts" from
699ddf152) were only on the Usage page and the panel. None of them meant
"stay on this account and spend its credits".

Change.
- A fourth credits mode, first: settings.CodexCreditsFirst. on, first,
  last and off are exclusive; one mode replaces another.
- routes.Seat.First: a first seat's tier is 0, so routing keeps it in
  front past its windows while it has credits. The trace records
  Weighed.CreditsFirst.
- NextLogin keeps Codex's own sign-in on such an account (spendsFirst).
- The POST /api/codex-credits handler takes "first". The general
  settings save keeps the stored value.
- The CLI: `magpie quota credits <account> first`.
- GUI. No new page and no new component, only the Use credits row that
  already exists:
  - 供应商 → Codex → 账号 ⋯ → 使用积分 → 「先用完积分再换账号」. The
    Providers ⋯ tools now carry the credits row too, which is where the
    reporter looked.
  - 用量 → Codex 账号卡 → 使用积分 → 「先用完积分再换账号」.
  - The menu-bar panel's folded line reads 「开,先于其他账号」.
  - The "First again once it has room" tag on a moved-off account now
    points to the switch in its title.
  - Strings are in en, zh, zh-TW, ja and de.
- "Only after other accounts" (gokoshiki, 699ddf152) is unchanged.
  Its off title now says what off means.
- Docs: docs/reference.md and docs/subsystems/gateway-routing.md.

Budgets: at this head app.js is 1,312,105 of 1,316,874 bytes, and
TestCoreScriptBudget passes (231 bytes under before the rebase). Comments
were condensed; no budget was raised.

Run on mote, at this tree (rebased on origin/main 9d08993c8; the rebase
moved the settings save into internal/settingsapi, which now carries
CodexCreditsFirst beside CodexCreditsLast):
- go test -count=1 -timeout 45m ./internal/gateway ./internal/provider
  ./internal/routes ./internal/settings ./internal/settingsapi
  ./internal/gui ./internal/cli at 9d08993c8+1: gateway, provider,
  routes, settings and settingsapi ok. Two reds, both filed:
  - internal/gui TestMainWindowCorners/macOS_26_SDK: #3949.
  - internal/cli TestLibraryMCPHub, TempDir cleanup not empty: #4140.
  At the pre-rebase tree (da0005df8+1) internal/gui also had
  TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and TestColdTurnsTaken
  (#4510), red the same at da0005df8; both pass at this head.
- New tests:
  - internal/provider/codex_credits_first_test.go
  - internal/gateway/codex_credits_first_test.go (routing and the trace)
  - internal/gui/codex_credits_first_test.go (POST sequence, sandbox HOME)
  - internal/gui/tests/codex-credits-first.test.cjs: the Usage page and
    Providers ⋯ at 900 and 440px, and the panel at 360px; chromium and
    webkit, en zh zh-TW ja de. It also checks for no select, no stripe,
    the i18n keys, and that a click does not move the page.
- The whole GUI suite (make test-ui), at da0005df8+1 on mote at load
  66 to 213, in two parts (a wiped scratchpad cut the first log; its red
  titles were kept): 59 files with a red. 45 of them are red at
  origin/main da0005df8 too, each under an open issue: #4304 (mote
  umbrella), #4600, #4471, #4120, #4119, #4621, and per file balance-fix
  #4682/#4683, gui-ja #4580, page-width-steady #4541, phone-connect-menu
  #4706/#4707, quota-curve #4768, segs-press-redraw #4566, signin-proxy
  #4643/#1814, zhipu-team #4728, model-copy-id #4621 (red 4 of 4 on main).
- The 14 green at da0005df8 were run again at this tree. One was this
  change's: usage-curve-zoom-back. The Codex card's fourth credits line
  (366px, was 344) held the DeepSeek card beside it to 261px, so the
  "card grows" check at 1100px had no room. Its fixture now turns the
  Codex account's credits off, since the count of credits lines isn't
  what it tests; 20 of 20 pass.
- At this head (9d08993c8+1), on mote at load 75 to 84: codex-credits-first,
  codex-credits-last, codex-credits, routing-codex-credits-again,
  panel-options-fold, usage-curve-zoom-back, i18n-keys and the 13 files
  above: 232 tests, 227 pass. Each red has an open issue:
  - models-fold webkit en/zh, "ebody holds" after the click: #4071; red
    the same at 9d08993c8 (run there).
  - account-weight webkit, same "ebody holds": #4644 (red at
    da0005df8+1, green in this run).
  - routing-many-keys chromium de "2 keys in sight at the foot": #4082
    (under load; red again alone here, green at 9d08993c8 in one run).
  - signin-closes-sheet chromium en/zh: #2628 (green alone at this head
    in a second run).
  - signin-again and signin-port-blocked, red once at da0005df8+1 under
    load, pass at this head and at 9d08993c8.

- Rebased last onto 94f6f6092 (three commits; c0477aa72 changes the
  Codex Providers row): go test -run 'Budget|CodexCredits|SettingsSave'
  ./internal/gui ok, and codex-credits-first, codex-credits,
  codex-credits-last, providers-reset-use, i18n-keys and
  usage-curve-zoom-back: 92 of 92 pass.

Broken by hand, each red, then restored:
- spendsFirst forced false: the provider test is red.
- The gateway's l.first forced false: both gateway tests are red.
- codex-credits-first.test.cjs against origin/main's assets: red
  (TimeoutError on the switch).

Not tried:
- No real ChatGPT account with credits went past its window. The routing
  order and NextLogin are tested on fakes only.
- The Linux and Windows apps were not run.

go vet -tags nogui ./... with GOOS darwin, linux and windows on mote:
clean (cgo enum warnings only). gofmt -l on the changed Go files: clean.

Claude has no equivalent: magpie reads no extra_usage and has no Use
credits modes for Claude. Filed as #4662 (see #4241).
2026-11-01 21:19:22 +08:00

127 lines
5.1 KiB
Go

package provider
// Whether a Codex account spends its credits. A ChatGPT account that holds
// credits (#571) keeps answering once a usage window is used up: the vendor
// spends the credits instead, and a request routed to it goes through. That
// is the default, and it carries a task on when the allowance runs out.
// Turned off for an account, routing holds it as used up at 100% of a
// window, as a usage cap holds it at its share (account_caps.go): never
// tried till the window renews, so the user's other accounts, groups and
// fallbacks take the request rather than it spending the credits. The hold
// goes by magpie's latest reading (Allowances, read again about every
// minute), so a request in the minute after a window fills may still reach
// the account. An account with
// no credits is refused by the vendor at 100% either way; the switch only
// spares it the request.
//
// Unless the account spends them first (CodexCreditsFirst), it doesn't
// change which account Codex is signed in to: NextLogin moves off an
// account at its routing's spent share (100% at most) and onto none past
// it, credits or not. Unlike the cap, it holds at the vendor's own
// 100%, so an account that spends its resets by itself spends one there
// (the gateway's autoReset) where a cap never does.
import (
"fmt"
"slices"
"github.com/yetone/magpie/internal/settings"
)
// CodexCredits says whether the Codex account user spends its credits
// once a usage window is used up: true unless the user turned it off.
func CodexCredits(user string) bool {
return !slices.Contains(settings.Load().CodexNoCredits, accountKey(user))
}
// CodexCreditsLast says whether the Codex account user, which spends its
// credits, spends them only after everything else: once a usage window is
// used up, routing tries it after every account, key and fallback not on
// its credits (gateway's creditsLast) rather than in its place. Off unless
// the user set it, so a setting saved before it keeps its order.
func CodexCreditsLast(user string) bool {
user = accountKey(user)
s := settings.Load()
return user != "" && slices.Contains(s.CodexCreditsLast, user) && !slices.Contains(s.CodexNoCredits, user)
}
// CodexCreditsFirst says whether the Codex account user, which spends its
// credits, spends them before routing moves on: a window used up sends
// neither requests nor Codex's sign-in to another account while it goes
// on on its credits (gateway's creditsFirst, NextLogin), where by default
// an account all but used up waits behind every one with room (Smart)
// and Codex is signed in to another (jojonas_xyz on X). Off unless the
// user set it.
func CodexCreditsFirst(user string) bool {
user = accountKey(user)
s := settings.Load()
return user != "" && slices.Contains(s.CodexCreditsFirst, user) && !slices.Contains(s.CodexNoCredits, user) && !slices.Contains(s.CodexCreditsLast, user)
}
// Ways a Codex account spends its credits, for SetCodexCreditsMode.
const (
CreditsOn = "on" // in its place, the default
CreditsFirst = "first" // before moving to another account
CreditsLast = "last" // after everything else
CreditsOff = "off" // never: held when a window is used up
)
// SetCodexCredits turns that on or off for user. On is in its place:
// it clears "first" and "last" too.
func SetCodexCredits(user string, on bool) error {
if on {
return SetCodexCreditsMode(user, CreditsOn)
}
return SetCodexCreditsMode(user, CreditsOff)
}
// CodexCreditsMode is how user spends its credits: CreditsOn,
// CreditsFirst, CreditsLast or CreditsOff.
func CodexCreditsMode(user string) string {
switch {
case !CodexCredits(user):
return CreditsOff
case CodexCreditsLast(user):
return CreditsLast
case CodexCreditsFirst(user):
return CreditsFirst
}
return CreditsOn
}
// SetCodexCreditsMode sets how user spends its credits: CreditsOn,
// CreditsFirst, CreditsLast or CreditsOff. One account has one of them.
func SetCodexCreditsMode(user, mode string) error {
if mode != CreditsOn && mode != CreditsFirst && mode != CreditsLast && mode != CreditsOff {
return fmt.Errorf("credits: %q is not on, first, last or off", mode)
}
user = accountKey(user)
s := settings.Load()
not := func(u string) bool { return u == user }
s.CodexNoCredits = slices.DeleteFunc(s.CodexNoCredits, not)
s.CodexCreditsLast = slices.DeleteFunc(s.CodexCreditsLast, not)
s.CodexCreditsFirst = slices.DeleteFunc(s.CodexCreditsFirst, not)
if user != "" {
switch mode {
case CreditsOff:
s.CodexNoCredits = append(s.CodexNoCredits, user)
case CreditsFirst:
s.CodexCreditsFirst = append(s.CodexCreditsFirst, user)
case CreditsLast:
s.CodexCreditsLast = append(s.CodexCreditsLast, user)
}
}
return settings.Save(s)
}
// HoldCaps is the shares of its windows at which routing holds the
// account user of p, of agent, as used up: its usage cap and its windows'
// own (account_caps.go), and for a Codex account set not to spend its
// credits 100% of any window with no cap. Holds() is false when nothing
// holds it before the vendor does.
func HoldCaps(p Provider, agent, user string) WindowCaps {
c := p.CapsOf(user)
c.Credits = agent == "codex" && user != "" && !CodexCredits(user)
return c
}