Files
magpie/internal/gateway/codex_held.go
T
yetone 8bf9cb99d4 codex: an account with bought credits goes on on them, heard as Codex hears them
123xtv03k on X (v0.1.4233): Codex alone goes on on an account's bought
credits once its plan is used up; through magpie the account stopped.

Codex itself sends no opt-in for credits. It reads
x-codex-credits-has-credits, -unlimited and -balance from every reply,
the 429 included, and `credits` on the codex.rate_limits event
(parse_credits_snapshot; both bools required). magpie knew of credits
only from /wham/usage, read about once a minute, so the 429 that used a
window up was taken as out, and the rest lasted till the window renewed.

- heardCodexCredits reads the headers on every Codex reply (routed and
  the relay's 429), codexEventCredits the event; an image limit's are
  left out. provider.NoteCodexCredits marks the account's windows and
  allowance on credits and clears Held, so Usage doesn't call it out.
- A rest taken for a used-up window before the credits were known is
  lifted once they are, heard or read (provider.OnCredits, creditsKnown),
  only when the account's "Use credits" is on.
- The relay's hold for an account on credits refused twice is at most
  fallbackCooldown (1 minute), like the routed path's, not till reset.
- No header or flag is sent that Codex doesn't send.

Run (mote, at this head rebased on b466aa280; 79b10c5a9 since touches only LESSONS.md): go vet for darwin, linux
and windows of internal/provider and internal/gateway; go test -count=1
-timeout 45m ./internal/provider ./internal/gateway: ok (provider 137s, gateway 907s). gofmt -l
clean.
Broken by hand: without the heardCodexCredits calls, the OnCredits
registration and the hold clamp, every new gateway subtest but "says
none" goes red.
Not tried: a real account holding credits (the owner's hold none). The
Usage card's balance text still comes from /wham/usage only.
2026-11-15 13:28:34 +08:00

144 lines
4.8 KiB
Go

package gateway
import (
"cmp"
"net/http"
"slices"
"strconv"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/provider"
)
// codexHolds are the ChatGPT accounts Codex's turns are relayed on as they
// came (codexUpstreamOn: Codex's own sign-in, or the account a compaction
// goes to) that ChatGPT refused with a 429 naming when they are back. The
// accounts loop rests and holds its own (timedHeld); a relayed turn has no
// candidate to rest, so it is held here: hsjzg58958 on X, an account
// limited for 300 seconds was asked again every time Codex tried again,
// till ChatGPT flagged it. Keyed by the account's user, lower case.
var codexHolds struct {
sync.Mutex
m map[string]codexHold
}
// codexHold is a refusal given again without asking until it ends.
type codexHold struct {
until time.Time
status int
why string // failRate, failQuota or failCredit
body []byte // as ChatGPT said it, for Codex to show as it would
ctype string
codex http.Header // its x-codex-* windows, which Codex reads beside it
}
// codexHoldKey is who a turn relayed to base on on goes to, for its hold;
// "" for one not held: Codex's API key, which OpenAI's platform limits
// by its own headers Codex reads, or a sign-in magpie can't name.
func codexHoldKey(base string, on *provider.Provider) string {
if base != provider.CodexBase {
return ""
}
if on != nil {
if on.Account == nil || on.LimitRestOff {
return ""
}
return strings.ToLower(on.Account.User)
}
who, ok := provider.CodexSignedIn()
if !ok {
return ""
}
return strings.ToLower(who)
}
// noteCodexHold holds who after ChatGPT refused a turn with res's 429,
// when it named for how long (vendorSaidWhen), as rateRest reads it.
// onCredits: the account goes on past its allowance on credits
// (relayOnCredits), so one refused as out of it is held only a moment, as
// the accounts loop rests one (fallbackCooldown, "credits"), not until
// the window renews with its credits unspent.
func noteCodexHold(who string, status int, header http.Header, body []byte, onCredits bool) {
if who == "" || status != http.StatusTooManyRequests || !vendorSaidWhen(header, body) {
return
}
why := failure(status, body)
if why != failRate && why != failQuota && why != failCredit {
return
}
now := time.Now()
d := retryAfter(header, now)
d = max(d, min(resetsNoted(header, now), longestQuota), min(resetsAt(body, now), longestQuota))
if onCredits && why == failQuota {
d = min(d, fallbackCooldown)
}
if d <= 0 {
return
}
codexHolds.Lock()
defer codexHolds.Unlock()
if codexHolds.m == nil {
codexHolds.m = map[string]codexHold{}
}
if h, ok := codexHolds.m[who]; ok && h.until.After(now.Add(d)) {
return // a longer hold stands
}
windows := http.Header{}
for k, vs := range header {
if strings.HasPrefix(strings.ToLower(k), "x-codex-") {
windows[k] = slices.Clone(vs)
}
}
codexHolds.m[who] = codexHold{until: onWall(now.Add(d)), status: status, why: why, body: append([]byte(nil), body...), ctype: header.Get("Content-Type"), codex: windows}
}
// codexHeld is who's hold, while it lasts.
func codexHeld(who string, now time.Time) (codexHold, bool) {
if who == "" {
return codexHold{}, false
}
codexHolds.Lock()
defer codexHolds.Unlock()
h, ok := codexHolds.m[who]
if ok && !now.Before(h.until) {
delete(codexHolds.m, who)
ok = false
}
return h, ok
}
// liftCodexHold ends user's hold out of its allowance or credits: a reset
// spent for it, or its window started again (restarted). A rate limit's
// stands: a new window says nothing of it.
func liftCodexHold(user string) {
codexHolds.Lock()
defer codexHolds.Unlock()
who := strings.ToLower(user)
if h, ok := codexHolds.m[who]; ok && h.why != failRate {
delete(codexHolds.m, who)
}
}
// writeCodexHeld answers a turn on a held account as ChatGPT did, with
// when it is back as Retry-After, and says so in the request log.
func (s *Server) writeCodexHeld(w http.ResponseWriter, r *http.Request, h codexHold, base string, on *provider.Provider, model string, start time.Time) {
wait := time.Until(h.until)
for k, vs := range h.codex {
w.Header()[k] = vs
}
w.Header().Set("Content-Type", cmp.Or(h.ctype, "application/json"))
w.Header().Set("Retry-After", strconv.Itoa(int((wait+time.Second-1)/time.Second)))
w.WriteHeader(h.status)
w.Write(h.body)
what := "rate limited"
if h.why != failRate {
what = "out of its allowance"
}
said := provider.APIError(h.body, http.StatusText(h.status)) + " (" + what + ": not asked again before " + heldClock(h.until, time.Now()) + ")"
s.record(Call{Time: start, From: provider.Responses, To: provider.Responses, Model: model,
Provider: "openai", Agent: callerOf(r).agent, Kind: callKind(r.Header), Status: h.status,
Millis: time.Since(start).Milliseconds(), Error: codexWho(base, on) + ": " + said, headers: headerLogOf(r.Context())})
}