Files
magpie/internal/provider/keepalive.go
T
yetone d3898ca8d3 provider: ChatGPT account calls go with Codex's headers, and saved accounts are listed once a day
Two follow-ups of 6e55914b6's audit of what magpie sends OpenAI for
idle saved Codex accounts (owl, Discord #general: three ChatGPT accounts
deactivated). Product call made by the session the owner delegated
product calls to; mimicking the official client is allowed for every
vendor but Claude Code. Identity, base_instructions and prompts are not
touched.

- Headers. GET /backend-api/wham/usage, /wham/rate-limit-reset-credits,
  POST .../consume and POST auth.openai.com/oauth/token (live and saved
  sign-ins, and the sign-in's code exchange) went with Go's
  "Go-http-client/1.1" and no originator. They now carry Codex's
  originator and User-Agent through codexClientHeaders, the part of
  codexSign that /codex/models and /responses already used. No version
  header on them: codex-rs sends it with model requests only
  (login/src/auth/default_client.rs default_headers is originator +
  User-Agent; backend-client/src/client.rs adds auth headers).
- Refresh body. codex-rs (main f32b6cfd and rust-v0.165.0,
  login/src/oauth/client.rs refresh, auth/manager.rs
  request_chatgpt_token_refresh) sends JSON client_id, grant_type,
  refresh_token and refresh_reason, and no scope. magpie sent
  scope "openid profile email" and no reason. It now sends codex-rs's
  fields: proactive_expiry near expiry, unauthorized_recovery after a
  401 (stale token), scheduled from the 8-day keep-alive, forced for an
  import or a revoked-sign-in check. The code exchange's form already
  matched exchange_code; only its headers changed.
- Model lists. The active Codex account's /codex/models fetch (hourly
  KeepAccountsListed, FetchNew, Refresh) also fetched every switched-on
  saved account's list each time. codexFetchSaved now asks one with no
  list at once, and the others once a day, one per call, oldest asked
  first; a failed ask is remembered so a deactivated account doesn't go
  first every hour. The active account's list is unchanged.

Docs: providers-accounts.md says both.

Tests (codex_quiet_test.go), each red on the old behaviour (broken by
hand: wham/refresh/exchange headers removed, scope back, the age check
and the one-a-look cap removed, the keep-alive's reason dropped, and
separately the failed-ask memory skipped):
- TestCodexAccountCallsGoAsCodex counts the requests to a fake (usage 1,
  credits 1, consume 1, token 2) and checks User-Agent/originator on
  each and the refresh body's keys. Old: Go-http-client/1.1, "" and
  scope in the body.
- TestCodexRefreshSaysWhy: scheduled, forced, unauthorized_recovery.
  Old: no reason.
- TestSavedCodexListsOnceADay: 2 asks on the first look, 0 an hour on,
  1 then 1 a day on, 0 again, and 0 within the day while failing. Old:
  2 on every look; without the memory, a failing one asked again.

Run (mote-run, at origin/main 9b4d0bda8 plus this change, the tree
pushed unless the rebase below names a newer base):
- gofmt -l internal/provider: clean
- go vet ./internal/{provider,agent,cli,gateway} on darwin, GOOS=linux
  -tags nogui and GOOS=windows: ok
- go test -count=1 -timeout 45m ./internal/provider ./internal/agent
  ./internal/cli ./internal/gateway: all ok (118s, 199s, 44s, 562s)
Not tried: a real ChatGPT account (none touched, per the session's
rules), so OpenAI's answer to the new headers and the scope-less
refresh is known only from codex-rs doing the same. The Sign in with
ChatGPT API path (auth.openai.com/api/accounts/oauth/token, chatgpt_api.go)
is another client and is left as it was. Refresh in the GUI also lists
saved accounts by the once-a-day rule now.
2026-10-30 02:28:10 +08:00

140 lines
4.5 KiB
Go

package provider
// Keeping saved accounts signed in. A ChatGPT account saved in magpie but
// not the one Codex is signed in to is refreshed only when it is used or
// its allowance looked at — and one waiting its turn (smart
// routing leaves the accounts whose week resets later for days) could sit
// unused past its refresh token's life, and be found signed out just when
// it's needed. So whichever magpie runs the gateway renews each such
// sign-in about as often as Codex renews its own: every eight days, one
// account an hour, never all of them at once. The account the agent is signed in to is the
// agent's own to refresh, and is left to it. A Claude account is never
// refreshed by magpie: Claude Code refreshes it, whenever it runs on it
// (claude_dirs.go).
import (
"context"
"encoding/json"
"errors"
"log"
"strings"
"time"
"github.com/yetone/magpie/internal/settings"
)
// keepAliveEvery is how long a saved sign-in goes before it is renewed:
// Codex's own TOKEN_REFRESH_INTERVAL (codex-rs login/src/auth/manager.rs,
// 8 days). Once a day renewed every idle account eight times as often as
// Codex does, each one in the same hourly look as the others: a burst of
// N refreshes from one address every day (owl on Discord).
const keepAliveEvery = 8 * 24 * time.Hour
// keepAliveAtOnce is how many due sign-ins one look renews; the rest wait
// for the next look, an hour on, so accounts saved together drift apart
// instead of being renewed side by side for good.
const keepAliveAtOnce = 1
// Renewal is what came of renewing one saved account.
type Renewal struct {
Agent string `json:"agent"`
User string `json:"user"`
// Renewed is false when it wasn't due; Err is why it failed, and Lapsed
// whether the sign-in is gone (the vendor refused it) or may yet renew.
Renewed bool `json:"renewed"`
Err string `json:"error,omitempty"`
Lapsed bool `json:"lapsed,omitempty"`
}
// RenewLogins renews the saved ChatGPT sign-ins not in the
// agent's hands that have gone every or longer without it; every 0 renews
// all of them.
func RenewLogins(ctx context.Context, every time.Duration) []Renewal {
return renewLogins(ctx, every, 0)
}
// renewLogins is RenewLogins renewing at most atOnce of the due sign-ins
// (0: all of them); a due one past that is left for the next look and
// reported as not renewed.
func renewLogins(ctx context.Context, every time.Duration, atOnce int) []Renewal {
if settings.AccountDiscoveryOff() {
return nil
}
rememberLogins(false)
loginsMu.Lock()
active := map[string]string{}
for _, a := range loginAgents {
if l, ok := liveLogin(a); ok {
active[a] = l.User
}
}
ls := readLogins()
loginsMu.Unlock()
out := []Renewal{}
tried := 0
for _, l := range ls {
if l.Agent != "codex" || strings.EqualFold(active[l.Agent], l.User) || leftAlone(l.Agent) {
continue
}
r := Renewal{Agent: l.Agent, User: l.User}
if every > 0 && !renewalDue(l, every, time.Now()) || atOnce > 0 && tried >= atOnce {
out = append(out, r)
continue
}
tried++
why := ctx
if every > 0 {
why = refreshingFor(ctx, codexRefreshScheduled)
}
if _, _, err := renewSavedLogin(why, l.Agent, l.User, true); err != nil {
r.Err = err.Error()
var refused refreshRefused
r.Lapsed = errors.As(err, &refused)
} else {
r.Renewed = true
}
out = append(out, r)
}
return out
}
// renewalDue says whether a saved sign-in has gone every without being
// renewed — since magpie last renewed it, or since it was last seen in the
// agent's hands, which kept it fresh — or its refresh token is near its end.
func renewalDue(l savedLogin, every time.Duration, now time.Time) bool {
last := l.Renewed
if l.Seen.After(last) {
last = l.Seen
}
var a struct {
LastRefresh time.Time `json:"last_refresh"`
}
if json.Unmarshal(l.Auth, &a) == nil && a.LastRefresh.After(last) {
last = a.LastRefresh
}
return now.Sub(last) >= every
}
// KeepLoginsAlive renews a saved sign-in that is due, one a look, a minute
// after it starts and every hour after that — a computer that slept through a day
// catches up within the hour of waking — until ctx ends.
func KeepLoginsAlive(ctx context.Context) {
t := time.NewTimer(time.Minute)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
}
c, cancel := context.WithTimeout(ctx, 2*time.Minute)
for _, r := range renewLogins(c, keepAliveEvery, keepAliveAtOnce) {
if r.Err != "" {
log.Printf("keeping %s account %s signed in: %s", r.Agent, r.User, r.Err)
}
}
cancel()
t.Reset(time.Hour)
}
}