Codex 0.162 with cli_auth_credentials_store = "keyring" or "auto" saves
its ChatGPT sign-in to the keychain and deletes auth.json. magpie read
only auth.json, so it hid the Codex provider and every saved account.
Adapted from Dony2050's fcebfc322 (fix/codex-macos-keychain). The changes
from that branch are below.
What Codex does (codex-rs at rust-v0.162.1: login/src/auth/storage.rs,
config/src/types.rs, features/src/lib.rs). The item is service "Codex
Auth", account "cli|" plus the first 16 hex digits of the SHA-256 of the
canonicalised CODEX_HOME. Its value is auth.json's JSON on one line.
keyring reads only the item. auto reads the item, and falls back to
auth.json when there is no item or the item can't be loaded. Saves follow
the same order. With [features] secret_auth_storage on (the default on
Windows), keyring and auto use Codex's encrypted storage instead.
internal/provider/codex_store.go is now the one reader and writer:
- A keychain interface. The real one runs `security` the way Claude Code's
item is read (keychainText, proc.CommandContext, 10s bound; errors carry
no argv or output). Tests get a fake (isolate, CodexKeychainForTest).
- readCodexLogin follows Codex's rules on macOS. keyring with no item is
signed out. keyring with a refused read is unknown (codexStoreError),
never signed out. With the encrypted storage, on Linux and on Windows,
keyring and ephemeral are unknown and auto is auth.json. A config.toml
that doesn't parse reads auth.json as before.
- Reads are kept 10s, a refusal 10 minutes, and only one `security` ask
runs at a time. Each read can be a Keychain prompt, and codexToken runs
on every request.
- A refresh writes the rotated tokens back where they came from, with
unknown fields kept, after re-reading the item.
- saveCodexLogin (switch, first sign-in) re-reads the store first. It
refuses when the store couldn't be read, when config.toml doesn't parse,
for ephemeral, and for a store magpie doesn't read. It never deletes
auth.json; Codex cleans that up itself.
- deleteCodexLogin (signing the last account out) removes the item, then
auth.json, as `codex logout` does.
- Routed through it: codexAccount, codexToken (it now takes the home dir),
liveLogin, CodexAPIKeySignedIn, codexLoginAuth, the usage card's
codexUntil, session identities, switchSavedLogin, addLogin, forgetLogin,
and agent's codexChatGPT/Source (provider.CodexSignIn).
- savedButSignedOut now says what was found (codexSignedOutWhy): nothing
in the item, an item macOS wouldn't give (allow it, or use file), or a
store magpie doesn't read here (use file). This replaces ed6e27f5f's
codexCredentialsStore and its single "magpie doesn't read the keychain"
text.
Changed from fcebfc322: secret_auth_storage is honoured (the branch would
read and write the wrong item); there is no second keychain package; reads
are cached and a denial backs off (the branch spawned `security` on every
read); auth.json is not deleted after every keychain write; auto falls back
to the file only when there is no item; the tests use an interface fake on
every OS instead of a darwin-only PATH fake; codexChatGPT keeps its
meaning; a malformed config.toml doesn't hide a file sign-in.
Run (in the wt-2367 worktree, sandbox HOMEs only, no real keychain or
`security` run, nothing under ~/.codex):
- gofmt -l: clean. go vet -tags nogui ./... for darwin, linux and windows:
clean.
- go test -p 1 -tags nogui ./internal/provider: ok (293s).
./internal/agent: ok (1291s). An earlier agent run hit the disk filling
up (ENOSPC from other sessions' builds); it was rerun with room.
./internal/gateway -run 'Codex|Login|Account': ok (261s).
- Broken by hand. The keychain never read (keychainItem false):
TestCodexSignedInInKeychain, TestCodexKeychainRefreshWritesItem,
TestCodexKeychainSwitch, TestCodexKeychainAskedSeldom,
TestCodexKeychainSignOut and TestSavedButSignedOut go red. saveCodexLogin
ignoring a refused read: TestCodexKeychainSwitch red ("switched over a
sign-in the keychain wouldn't give"). No cache:
TestCodexKeychainAskedSeldom red (20 asks for 20 reads). agent's
codexChatGPT reading auth.json only: TestCodexSignInInKeychain red.
- Not tried: an item Codex itself wrote. Codex writes it through
keyring-rs, and its ACL may trust only codex, so macOS may prompt the
first time magpie reads it, and Codex may prompt after magpie rewrites
it (`add-generic-password -U`). An unanswered prompt is killed at 10s
and reads as unknown. The reporter is asked to confirm on #2367, which
stays open.
Landing: this goes on top of ed6e27f5f (in this worktree as its
cherry-pick 66cdde294) and replaces that commit's message.
Co-authored-by: Dony2050 <88673296+Dony2050@users.noreply.github.com>
Full suite: SUITE_LINE
658 lines
21 KiB
Go
658 lines
21 KiB
Go
package provider
|
|
|
|
// Starting a ChatGPT account's next window as soon as the last one resets.
|
|
// A Codex window starts at the account's first request after it resets, not
|
|
// at the reset: an account that sits idle for a day after its week ends has
|
|
// its next week end a day later too. So, while the setting is on, whichever
|
|
// magpie runs the gateway looks at each account's windows every few minutes
|
|
// and, when one has started over — its reset time passed, or its use fell
|
|
// back (OpenAI resetting everyone's limits early) — and nothing has used it
|
|
// since, sends that account one tiny request, as the gateway sends any.
|
|
// Once per reset: what it saw and did is kept in codex-warmup.json, so a
|
|
// restart doesn't send it again. A window still not started after one —
|
|
// the read from before it, a request that didn't start it, or warm-ups
|
|
// given up on — is sent another later, each wait twice the last, even if
|
|
// the backend keeps reporting a full window's reset time for an idle one.
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"maps"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
const (
|
|
// codexWarmEvery is how often the windows are looked at.
|
|
codexWarmEvery = 5 * time.Minute
|
|
// warmSlack is how far into a window it is still taken as unused: an
|
|
// idle window's reset is always a whole window from now.
|
|
warmSlack = 10 * time.Minute
|
|
// warmTries is how many failed warm-ups one reset gets.
|
|
warmTries = 3
|
|
// warmRetry is how long a window a warm-up left not started waits for
|
|
// the next, doubling each time up to warmRetryMax.
|
|
warmRetry = 15 * time.Minute
|
|
warmRetryMax = 4 * time.Hour
|
|
// warmKickEvery is how often a usage read finding an account's window
|
|
// not started has its windows looked at before the next round.
|
|
warmKickEvery = 10 * time.Minute
|
|
)
|
|
|
|
// warmWindow is one window of an account as last seen, and when magpie
|
|
// last started it.
|
|
type warmWindow struct {
|
|
ResetsAt time.Time `json:"resetsAt,omitzero"`
|
|
Used float64 `json:"used"`
|
|
Warmed time.Time `json:"warmed,omitzero"`
|
|
// Pending is a reset whose warm-up failed, tried again next time
|
|
// until warmTries have failed.
|
|
Pending bool `json:"pending,omitempty"`
|
|
Failed int `json:"failed,omitempty"`
|
|
// Daily is the day ("2006-01-02") the time of day last started this
|
|
// window for (warmup_daily.go).
|
|
Daily string `json:"daily,omitempty"`
|
|
// Idle is how many warm-ups in a row left it not started, and Retry
|
|
// when it is sent the next while it still isn't.
|
|
Idle int `json:"idle,omitempty"`
|
|
Retry time.Time `json:"retry,omitzero"`
|
|
}
|
|
|
|
// warmState is every account's windows, by lower-cased user and window name.
|
|
type warmState map[string]map[string]warmWindow
|
|
|
|
func codexWarmPath() string { return filepath.Join(filepath.Dir(Path()), "codex-warmup.json") }
|
|
|
|
func readWarmState(path string) warmState {
|
|
st := warmState{}
|
|
if b, err := os.ReadFile(path); err == nil {
|
|
json.Unmarshal(b, &st)
|
|
}
|
|
return st
|
|
}
|
|
|
|
// CodexWarm is what came of looking at one account: the windows it was
|
|
// sent a request for, and why that failed.
|
|
type CodexWarm struct {
|
|
User string `json:"user"`
|
|
Windows []string `json:"windows"`
|
|
Err string `json:"error,omitempty"`
|
|
}
|
|
|
|
// codexWarmer is the warm-up with what it reads and sends given, for tests;
|
|
// Claude's (claude_warmup.go) is one too.
|
|
type codexWarmer struct {
|
|
path string
|
|
now func() time.Time
|
|
usage func(context.Context) map[string]SubscriptionQuota // by user
|
|
send func(ctx context.Context, user string) error
|
|
// expect is the windows every account has, one its usage leaves out
|
|
// taken as not started (Anthropic has none for an account never used).
|
|
expect []QuotaWindow
|
|
// warmed, when set, is told of an account a request went to, so what
|
|
// it has left is read again rather than taken from before.
|
|
warmed func(user string)
|
|
// ownAt, when set, is the accounts that have a time of day of their
|
|
// own, by name in lower case, "" for none: theirs in place of the ones
|
|
// warmNow is given (#957).
|
|
ownAt func() map[string]string
|
|
}
|
|
|
|
// atOf is the times of day user's 5-hour window starts at: its own, else
|
|
// ats.
|
|
func (c codexWarmer) atOf(own map[string]string, user string, ats []string) []string {
|
|
if a, ok := own[strings.ToLower(user)]; ok {
|
|
if a == "" {
|
|
return nil
|
|
}
|
|
return []string{a}
|
|
}
|
|
return ats
|
|
}
|
|
|
|
// owned is c's accounts' own times of day, none when it has no ownAt.
|
|
func (c codexWarmer) owned() map[string]string {
|
|
if c.ownAt == nil {
|
|
return nil
|
|
}
|
|
return c.ownAt()
|
|
}
|
|
|
|
// warmNow reads each account's windows, the weekly ones or with which
|
|
// "all" the 5-hour ones too, sends a request to each account one of them
|
|
// has started over on — or whose 5-hour window isn't running at one of
|
|
// the times of day dayAts ("06:00", none for none), or the account's own
|
|
// (ownAt) — and keeps what it saw.
|
|
func (c codexWarmer) warmNow(ctx context.Context, which string, dayAts []string) []CodexWarm {
|
|
if settings.AccountDiscoveryOff() {
|
|
return nil
|
|
}
|
|
st := readWarmState(c.path)
|
|
now := c.now()
|
|
own := c.owned()
|
|
usage := c.usage(ctx)
|
|
users := make([]string, 0, len(usage))
|
|
for u := range usage {
|
|
users = append(users, u)
|
|
}
|
|
slices.Sort(users)
|
|
var out []CodexWarm
|
|
changed := false
|
|
for _, user := range users {
|
|
q := usage[user]
|
|
if q.Error != "" {
|
|
continue // nothing known: what was seen stands
|
|
}
|
|
key := strings.ToLower(user)
|
|
at := c.atOf(own, user, dayAts)
|
|
prev, next := st[key], map[string]warmWindow{}
|
|
var due []string
|
|
onReset, days := map[string]bool{}, map[string]string{}
|
|
unstarted, long := map[string]bool{}, map[string]bool{}
|
|
waitDay := false
|
|
for _, w := range withExpected(q.Windows, c.expect) {
|
|
// the weekly windows on their reset while it is on, the 5-hour
|
|
// ones with "all" and for the day's start
|
|
short := w.Span < 24*time.Hour
|
|
onItsReset := which == "all" || which != "" && !short
|
|
if w.Span <= 0 || w.Aside || w.Model != "" || !onItsReset && !(short && len(at) > 0) {
|
|
continue
|
|
}
|
|
cur := asOf(w, now)
|
|
unstarted[w.Name] = idle(cur, now)
|
|
p, seen := prev[w.Name]
|
|
n := warmWindow{Used: cur.Used, Warmed: p.Warmed, Daily: p.Daily}
|
|
if cur.ResetsAt != nil {
|
|
n.ResetsAt = *cur.ResetsAt
|
|
}
|
|
if unstarted[w.Name] {
|
|
n.Idle, n.Retry = p.Idle, p.Retry // not started yet: it waits on
|
|
}
|
|
reset := onItsReset && warmDue(p, seen, cur, now)
|
|
held := reset && short && heldForDay(at, w.Span, now)
|
|
day, daily := "", false
|
|
if short {
|
|
day, daily = dailyDue(at, p.Daily, cur, now)
|
|
}
|
|
if held && !daily {
|
|
// it waits for the day's start, kept as it was till then
|
|
reset = false
|
|
}
|
|
if short && unstarted[w.Name] && !daily && heldForDay(at, w.Span, now) {
|
|
// any request now would start it, to run past the day's start
|
|
waitDay = true
|
|
}
|
|
long[w.Name] = !short
|
|
if reset || daily {
|
|
due = append(due, w.Name)
|
|
onReset[w.Name] = reset
|
|
if daily {
|
|
days[w.Name] = day
|
|
}
|
|
}
|
|
if reset || held {
|
|
// kept as it was until a request goes, so it is due again
|
|
n = p
|
|
}
|
|
next[w.Name] = n
|
|
}
|
|
if waitDay {
|
|
// the weekly windows wait for the day's start too, kept as they were
|
|
due = slices.DeleteFunc(due, func(name string) bool { return long[name] })
|
|
}
|
|
if len(due) > 0 {
|
|
r := CodexWarm{User: user, Windows: due}
|
|
err := c.send(ctx, user)
|
|
for _, name := range due {
|
|
n, p := next[name], prev[name]
|
|
switch {
|
|
case err == nil:
|
|
n = windowSeen(q, name, now)
|
|
n.Warmed, n.Daily = now, p.Daily
|
|
if day, ok := days[name]; ok {
|
|
n.Daily = day
|
|
}
|
|
case !onReset[name]:
|
|
// the day's start only: tried again while it is due
|
|
case p.Failed+1 >= warmTries: // given up on this reset
|
|
n = windowSeen(q, name, now)
|
|
n.Daily = p.Daily
|
|
default:
|
|
n.Pending, n.Failed = true, p.Failed+1
|
|
}
|
|
if onReset[name] && !n.Pending && unstarted[name] {
|
|
// not started as far as is known: another later
|
|
n.Idle = p.Idle + 1
|
|
n.Retry = now.Add(min(warmRetry<<min(p.Idle, 8), warmRetryMax))
|
|
}
|
|
next[name] = n
|
|
}
|
|
if err != nil {
|
|
r.Err = err.Error()
|
|
} else if c.warmed != nil {
|
|
c.warmed(user)
|
|
}
|
|
out = append(out, r)
|
|
}
|
|
if !mapsEqual(prev, next) {
|
|
st[key], changed = next, true
|
|
}
|
|
}
|
|
if changed {
|
|
if b, err := json.MarshalIndent(st, "", " "); err == nil {
|
|
if err := writePrivate(c.path, append(b, '\n')); err != nil {
|
|
log.Printf("codex warm-up: %v", err)
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// withExpected is ws with each of expect it leaves out, as not started.
|
|
func withExpected(ws, expect []QuotaWindow) []QuotaWindow {
|
|
out := ws
|
|
for _, e := range expect {
|
|
if !slices.ContainsFunc(ws, func(w QuotaWindow) bool { return w.Name == e.Name }) {
|
|
out = append(slices.Clip(out), QuotaWindow{Name: e.Name, Span: e.Span})
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// windowSeen is window name of q as now, as warmNow keeps it; one q
|
|
// leaves out is not started.
|
|
func windowSeen(q SubscriptionQuota, name string, now time.Time) warmWindow {
|
|
for _, w := range q.Windows {
|
|
if w.Name == name {
|
|
cur := asOf(w, now)
|
|
n := warmWindow{Used: cur.Used}
|
|
if cur.ResetsAt != nil {
|
|
n.ResetsAt = *cur.ResetsAt
|
|
}
|
|
return n
|
|
}
|
|
}
|
|
return warmWindow{}
|
|
}
|
|
|
|
func mapsEqual(a, b map[string]warmWindow) bool {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
for k, x := range a {
|
|
y, ok := b[k]
|
|
if !ok || !x.ResetsAt.Equal(y.ResetsAt) || x.Used != y.Used || !x.Warmed.Equal(y.Warmed) || x.Failed != y.Failed || x.Pending != y.Pending || x.Daily != y.Daily ||
|
|
x.Idle != y.Idle || !x.Retry.Equal(y.Retry) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// asOf is w as of now, its reset made absolute: a window read before a
|
|
// reset that has since passed (a cached read, or one kept through a
|
|
// failed fetch) has started over, from nothing.
|
|
func asOf(w QuotaWindow, now time.Time) QuotaWindow {
|
|
if w.ResetsAt == nil && w.ResetSecs > 0 {
|
|
t := now.Add(time.Duration(w.ResetSecs) * time.Second)
|
|
w.ResetsAt = &t
|
|
}
|
|
if w.ResetsAt != nil && !now.Before(*w.ResetsAt) {
|
|
w.Used, w.ResetsAt = 0, nil
|
|
}
|
|
return w
|
|
}
|
|
|
|
// warmDue says whether a window seen as p (seen false the first time) and
|
|
// now as cur wants starting: it is unused, and has started over since p —
|
|
// or is seen for the first time. Use falling back is a reset whatever the
|
|
// window says, OpenAI resetting everyone's limits early among them. One
|
|
// whose warm-up failed is still due, and one still not started is due
|
|
// again once its Retry comes, even with a reset time reported.
|
|
func warmDue(p warmWindow, seen bool, cur QuotaWindow, now time.Time) bool {
|
|
switch {
|
|
case seen && p.Pending:
|
|
return true
|
|
case !seen:
|
|
return idle(cur, now)
|
|
case p.Idle > 0 && idle(cur, now):
|
|
return !now.Before(p.Retry)
|
|
case p.Used-cur.Used >= 1:
|
|
return true
|
|
case !p.ResetsAt.IsZero() && !now.Before(p.ResetsAt):
|
|
return idle(cur, now)
|
|
case p.ResetsAt.IsZero() && p.Used == 0:
|
|
return idle(cur, now) && !now.Before(p.Retry)
|
|
}
|
|
return false
|
|
}
|
|
|
|
// idle says whether a window (as of now) hasn't started: nothing used,
|
|
// and its reset a whole window away, as the backend reports one not yet
|
|
// begun — or not known.
|
|
func idle(w QuotaWindow, now time.Time) bool {
|
|
return w.Used == 0 && (w.ResetsAt == nil || w.ResetsAt.Sub(now) >= w.Span-warmSlack)
|
|
}
|
|
|
|
// codexWarmUsage is each ChatGPT account's windows, the cached reads the
|
|
// Usage page and the switch to an account with room share; an account
|
|
// whose sign-in lapsed is left out.
|
|
func codexWarmUsage(ctx context.Context) map[string]SubscriptionQuota {
|
|
u := LoginUsage(ctx, "codex")
|
|
for _, l := range Logins("codex") {
|
|
if l.Lapsed != "" {
|
|
delete(u, l.User)
|
|
}
|
|
}
|
|
return u
|
|
}
|
|
|
|
// warmCodexLogin sends the ChatGPT account user one tiny request, with its
|
|
// own sign-in: Codex's for the account Codex is on, logins.json's else.
|
|
func warmCodexLogin(ctx context.Context, user string) error {
|
|
ls := Logins("codex")
|
|
i := slices.IndexFunc(ls, func(l Login) bool { return strings.EqualFold(l.User, user) })
|
|
if i < 0 {
|
|
return fmt.Errorf("no ChatGPT account %q", user)
|
|
}
|
|
l := ls[i]
|
|
token := func(ctx context.Context) (string, string, error) { return savedLoginToken(ctx, "codex", l.User) }
|
|
if l.Active {
|
|
token = func(ctx context.Context) (string, string, error) { return codexToken(ctx, codexHome()) }
|
|
}
|
|
model, effort, err := warmModel(l.User)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return warmCodex(ViaLogin(ctx, "codex", user), codexSign(token), model, effort)
|
|
}
|
|
|
|
// warmModel is the model a warm-up asks, and at what effort: the account's
|
|
// cheapest (warmPick), at low.
|
|
func warmModel(user string) (model, effort string, err error) {
|
|
ms, _, ok := catalog.Live(accountModels("codex", user))
|
|
if !ok {
|
|
ms, _, _ = catalog.Live("codex")
|
|
}
|
|
if len(ms) == 0 {
|
|
ms = catalog.Codex()
|
|
}
|
|
if len(ms) == 0 {
|
|
return "", "", errors.New("no Codex model is known yet")
|
|
}
|
|
m := warmPick(ms, func(id string) (catalog.Price, bool) { return catalog.PricedBy([]string{"openai"}, id) })
|
|
switch {
|
|
case slices.Contains(m.Efforts, "low"):
|
|
effort = "low"
|
|
case len(m.Efforts) > 0:
|
|
effort = m.Efforts[0]
|
|
}
|
|
return m.ID, effort, nil
|
|
}
|
|
|
|
// warmPick is the cheapest of an account's models, by its list price where
|
|
// models.dev has one (#604: GPT-6's cheap one is gpt-6-luna; the list
|
|
// has no mini and starts with the flagship gpt-6.1-sol); with no price
|
|
// known, a mini, else a luna, else the first it lists.
|
|
func warmPick(ms []catalog.Model, price func(id string) (catalog.Price, bool)) catalog.Model {
|
|
best, cost := -1, 0.0
|
|
for i, m := range ms {
|
|
p, ok := catalog.Price{}, false
|
|
if m.Price != nil {
|
|
p, ok = *m.Price, true
|
|
} else {
|
|
p, ok = price(m.ID)
|
|
}
|
|
if c := p.Input + p.Output; ok && c > 0 && (best < 0 || c < cost) {
|
|
best, cost = i, c
|
|
}
|
|
}
|
|
if best >= 0 {
|
|
return ms[best]
|
|
}
|
|
for _, word := range []string{"mini", "luna"} {
|
|
if i := slices.IndexFunc(ms, func(m catalog.Model) bool { return strings.Contains(m.ID, word) }); i >= 0 {
|
|
return ms[i]
|
|
}
|
|
}
|
|
return ms[0]
|
|
}
|
|
|
|
// warmCodex sends one "hi" to the ChatGPT backend as a Codex account's
|
|
// requests go (codexBody: Codex's instructions, not stored), and reads the
|
|
// reply to its end.
|
|
func warmCodex(ctx context.Context, sign func(context.Context, *http.Request, []byte) error, model, effort string) error {
|
|
in := map[string]any{"model": model, "input": "hi"}
|
|
if effort != "" {
|
|
in["reasoning"] = map[string]any{"effort": effort}
|
|
}
|
|
b, _ := json.Marshal(in)
|
|
body := codexBody(b)
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, CodexBase+"/responses", bytes.NewReader(body))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
if err := sign(ctx, req, body); err != nil {
|
|
return err
|
|
}
|
|
CodexCompressed(req, body) // as Codex sends a turn
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode < 200 || res.StatusCode >= 300 {
|
|
msg, _ := io.ReadAll(io.LimitReader(res.Body, 512))
|
|
return fmt.Errorf("%s: %s", res.Status, strings.TrimSpace(string(msg)))
|
|
}
|
|
_, err = io.Copy(io.Discard, io.LimitReader(res.Body, 1<<20))
|
|
return err
|
|
}
|
|
|
|
// CodexWarmed is when magpie last started a window of each ChatGPT
|
|
// account, by user as kept (lower-cased).
|
|
func CodexWarmed() map[string]time.Time { return codexWarmedIn(codexWarmPath()) }
|
|
|
|
func codexWarmedIn(path string) map[string]time.Time {
|
|
out := map[string]time.Time{}
|
|
for user, ws := range readWarmState(path) {
|
|
for _, w := range ws {
|
|
if w.Warmed.After(out[user]) {
|
|
out[user] = w.Warmed
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// codexWarmAtOf is the ChatGPT accounts' own times of day (settings'
|
|
// CodexWarmAtOf), "" for one that has none.
|
|
func codexWarmAtOf() map[string]string {
|
|
of := settings.Load().CodexWarmAtOf
|
|
out := make(map[string]string, len(of))
|
|
for user, at := range of {
|
|
if at == "off" {
|
|
at = ""
|
|
}
|
|
out[user] = at
|
|
}
|
|
return out
|
|
}
|
|
|
|
// CodexWarmAtOf is user's own time of day to start its 5-hour window at,
|
|
// "off" for none; false when it follows settings' CodexWarmAts.
|
|
func CodexWarmAtOf(user string) (string, bool) {
|
|
at, ok := settings.Load().CodexWarmAtOf[strings.ToLower(strings.TrimSpace(user))]
|
|
return at, ok
|
|
}
|
|
|
|
// SetCodexWarmAt gives user a time of day of its own ("06:00"), none
|
|
// ("off"), or, "", has it follow settings' CodexWarmAts again.
|
|
func SetCodexWarmAt(user, at string) error {
|
|
user = strings.ToLower(strings.TrimSpace(user))
|
|
if user == "" {
|
|
return fmt.Errorf("which Codex account?")
|
|
}
|
|
s := settings.Load()
|
|
of := maps.Clone(s.CodexWarmAtOf)
|
|
if of == nil {
|
|
of = map[string]string{}
|
|
}
|
|
delete(of, user)
|
|
if at = strings.TrimSpace(at); at != "" {
|
|
of[user] = at
|
|
}
|
|
s.CodexWarmAtOf = of
|
|
return settings.Save(s)
|
|
}
|
|
|
|
// KeepCodexWindowsWarm starts the ChatGPT accounts' windows as they reset,
|
|
// while settings say to, two minutes after it starts and every
|
|
// codexWarmEvery after that, until ctx ends.
|
|
func KeepCodexWindowsWarm(ctx context.Context) {
|
|
w := codexWarmer{path: codexWarmPath(), now: time.Now, usage: codexWarmUsage, send: warmCodexLogin,
|
|
warmed: func(user string) { StaleAllowance("codex", user) }, ownAt: codexWarmAtOf}
|
|
keepWarm(ctx, "codex", w, func() (string, []string) { s := settings.Load(); return s.CodexWarmup, s.CodexWarmAts })
|
|
}
|
|
|
|
// keepWarm runs w while prefs (the settings: which windows on their
|
|
// reset, and the times of day to start the 5-hour one at) say to: two
|
|
// minutes after it starts and every codexWarmEvery after that, and as soon
|
|
// as one of the times of day comes, until ctx ends. The time is the wall clock's,
|
|
// looked at every minute: a machine that slept through it notices on
|
|
// waking. A usage read that finds an account's window not started — one
|
|
// never used, which routing leaves for last — has it run at once too, for
|
|
// each account at most every warmKickEvery.
|
|
func keepWarm(ctx context.Context, name string, w codexWarmer, prefs func() (which string, ats []string)) {
|
|
t := time.NewTimer(2 * time.Minute)
|
|
defer t.Stop()
|
|
kick := make(chan string, 16)
|
|
defer onNotStarted(name, w.expect, func(user string) {
|
|
select {
|
|
case kick <- user:
|
|
default: // enough are waiting
|
|
}
|
|
})()
|
|
kicked := map[string]time.Time{}
|
|
var last time.Time
|
|
for {
|
|
tick, now1 := false, false
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-t.C:
|
|
tick = true
|
|
case user := <-kick:
|
|
key, now := strings.ToLower(user), w.now().Round(0)
|
|
if which, _ := prefs(); which == "" || !kicked[key].IsZero() && now.Sub(kicked[key]) < warmKickEvery {
|
|
continue
|
|
}
|
|
kicked[key], now1 = now, true
|
|
}
|
|
// the wall clock, which goes on while the machine sleeps
|
|
now := w.now().Round(0)
|
|
if which, at := prefs(); !leftAlone(name) && warmLook(which, at, w.owned(), now1, last, now) {
|
|
last = now
|
|
c, cancel := context.WithTimeout(ctx, 2*time.Minute)
|
|
for _, r := range w.warmNow(c, which, at) {
|
|
if r.Err != "" {
|
|
log.Printf("%s warm-up: %s's %s window: %s", name, r.User, strings.Join(r.Windows, ", "), r.Err)
|
|
} else {
|
|
log.Printf("%s warm-up: started %s's %s window", name, r.User, strings.Join(r.Windows, ", "))
|
|
}
|
|
}
|
|
cancel()
|
|
// what its own read kicked, it has just looked at
|
|
for len(kick) > 0 {
|
|
<-kick
|
|
}
|
|
}
|
|
if tick {
|
|
t.Reset(time.Minute)
|
|
}
|
|
}
|
|
}
|
|
|
|
// warmLook says whether keepWarm looks at the windows now: a warm-up is
|
|
// on — on reset (which), or at a time of day, the ones for all (at) or an
|
|
// account's own — and a read kicked it (kicked), it hasn't looked yet, it
|
|
// last looked codexWarmEvery ago, or one of those times came since.
|
|
func warmLook(which string, at []string, own map[string]string, kicked bool, last, now time.Time) bool {
|
|
ats := slices.Clone(at)
|
|
for _, a := range own {
|
|
ats = append(ats, a)
|
|
}
|
|
if which == "" && !slices.ContainsFunc(ats, func(a string) bool { return a != "" }) {
|
|
return false
|
|
}
|
|
return kicked || last.IsZero() || now.Sub(last) >= codexWarmEvery ||
|
|
dayStartPassed(ats, last, now)
|
|
}
|
|
|
|
// notStartedHooks is, by agent, what a usage read finding an account's
|
|
// window not started tells, and the windows each account has.
|
|
var notStartedHooks struct {
|
|
sync.Mutex
|
|
m map[string]notStartedHook
|
|
}
|
|
|
|
type notStartedHook struct {
|
|
expect []QuotaWindow
|
|
f func(user string)
|
|
}
|
|
|
|
// onNotStarted has f told of each of agent's accounts a usage read finds
|
|
// a window of not started, until the func it returns is called.
|
|
func onNotStarted(agent string, expect []QuotaWindow, f func(user string)) func() {
|
|
h := ¬StartedHooks
|
|
h.Lock()
|
|
if h.m == nil {
|
|
h.m = map[string]notStartedHook{}
|
|
}
|
|
h.m[agent] = notStartedHook{expect, f}
|
|
h.Unlock()
|
|
return func() {
|
|
h.Lock()
|
|
delete(h.m, agent)
|
|
h.Unlock()
|
|
}
|
|
}
|
|
|
|
// usageRead tells onNotStarted's f of each account in usage, read for
|
|
// agent, with a window not started: nothing used and no reset known, or
|
|
// left out of the read.
|
|
func usageRead(agent string, usage map[string]SubscriptionQuota) {
|
|
h := ¬StartedHooks
|
|
h.Lock()
|
|
hook, ok := h.m[agent]
|
|
h.Unlock()
|
|
if !ok {
|
|
return
|
|
}
|
|
for user, q := range usage {
|
|
if q.Error == "" && slices.ContainsFunc(withExpected(q.Windows, hook.expect), notStarted) {
|
|
hook.f(user)
|
|
}
|
|
}
|
|
}
|
|
|
|
// notStarted says whether w is a window that hasn't begun as far as is
|
|
// known: nothing used and no reset.
|
|
func notStarted(w QuotaWindow) bool {
|
|
return w.Span > 0 && !w.Aside && w.Model == "" && w.Used == 0 && w.ResetsAt == nil && w.ResetSecs <= 0
|
|
}
|