MasterYi (#help, 1563467829687222343): with magpie wired into the Codex
app, a used-up ChatGPT account greys the Send button, so the third-party
models joined in Codex's picker can't be used either.
Cause, as far as the app's bundled code (ChatGPT.app 26.1013.40934,
read-only) shows: Send is gated in the app itself. It needs
authMethod == "chatgpt", the /wham/usage reading (fetched by the app
straight from chatgpt.com, not through magpie) saying not allowed, and
no credits. authMethod is read as the app opens or on account/updated,
which the app-server sends only on login/logout, not on a config change.
So the gate lifts only when model_provider is magpie AND the app has been
opened again. #540 already made magpie the provider while out, but only
when a magpie model was the default; on one of Codex's own models
(joined), nothing changed.
What changed:
- A (agent/codex.go Sync): joined on one of Codex's own models and the
account out (not Always ChatGPT, not magpie API), magpie becomes the
provider on that model as magpie serves it (codex/<model>), the model
kept in codex.out_own. Once the account has room, Codex is put back
joined on that model. A model picked meanwhile stays the user's. With
no ChatGPT account in magpie to serve it, nothing changes. New
Agent.AppOut says magpie is the provider for a used-up sign-in.
- C (provider/codex_app_out.go): a usage_limit_reached rest on the
active Codex account (gateway routing) or an x-codex-* window heard at
100% (NoteCodexLimits) has the account read again at once
(CodexMayBeUsedUp: active account only, at most every 20s, only
while a KeepOnAnAccountWithRoom loop has made its first reading and
still runs), not only on the 5-minute look. A first cut turned this
on for good as the loop started; every gateway test's Serve starts
it, so later gateway tests had accounts read again behind them, and
TestCodexOwnModelMovesToNextAccount and
TestCodexSpentMovesToNextGroupMember went red in the package run
(green alone 3 of 3). Now a counter, up after the first minute's
reading, down as the loop ends; the package run is green with it.
- B (gui/codex_app_out.go): the app open when the account ran out is kept
by its app-server's pid (provider.CodexAppOut; a new pid means it
reopened and read the provider, standing in for "authMethod is still
chatgpt"). On macOS, every 10s, magpie reopens it through the Agents
row's ReopenCodexApp when: AppOut, the account still out, the app is a
copy from before the config change, no turn in flight through the
gateway and none ended in the last 30s. At most once per time the
account runs out, success or not.
Drafts: the app's code keeps composer drafts in a persisted store
(composer-prompt-drafts-v2, 250ms debounce, flushed on quit), and
threads are server-side, so an unsent draft and open threads should
survive the quit. Not watched in a real app.
- D (GUI): Codex's accounts say "The Codex app can't send on {user}'s
used-up account": on macOS that magpie reopens it once no turn runs;
off macOS (no reopen path) or once tried, to quit and reopen it.
en, zh, zh-TW, ja, de.
Tests (each red on the code without its part, broken by hand):
- TestCodexOwnModelGoesThroughMagpieWhileUsedUp: red at "used up" with
the out case off, at "allowance back" with the restore off.
TestCodexOwnModelKeptOnChatGPT: Always ChatGPT stays.
- TestCodexMayBeUsedUpReadsAtOnce: red at "a window full: not read"
with the NoteCodexLimits hook off. TestCodexAppOutEpisode.
- TestCodexUsedUpReplyAsksAtOnce (gateway): red, asked [], with the
routing hook off.
- TestCodexAppOutReopenedOnce (gui, reopen stubbed): red at "a turn in
flight" with the idle check off, and at "again" with the once checks
off.
- codex-app-out.test.cjs: 40 of 40 (chromium, webkit; 900/480; five
languages; both texts); 2 of 2 red with the notice's call site off.
What was run, at this tree:
- gofmt -l: none. go vet ./... (darwin), GOOS=linux go vet -tags nogui
./..., GOOS=windows go vet ./...: exit 0.
- go test -tags nogui -timeout 45m: internal/agent ok (414s, before a
comment-only reorder in Sync); internal/gateway ok (534s),
internal/gui ok (78s), internal/provider ok (137s), after the
live-check fix, at this tree before the previous commit's JS-only
move (then with a budget raise since taken back); internal/gui
again after the move.
- GUI (chromium, webkit), at this tree: codex-app-out, codex-app-stale,
i18n-keys, gui-de, gui-ja, gui-zh-tw and the files reaching the
previous commit's move: 164 of 164. Before the trim, every codex-*
and account-* file: 447 of 448; the red, codex-predictions chromium
ja 420 "picking a model scrolled the page", is #2624 and is red alone
on origin/main 5a85a2a91 too (1 of 2); one codex-app-out case timed
out launching chromium at load ~100 and passed on the rerun.
- TestCoreScriptBudget with the budgets unchanged: ok. The three Codex
notices now share one box builder (codexNote), and the previous
commit moved ledSaid out of app.js.
Not tried:
- No real Codex app was quit or reopened (the owner's live app is left
alone); the reopen path is stubbed. Whether the reopened app's Send
comes back, and whether a draft survives, is from reading its code.
- No real used-up account; the move was run in a sandbox HOME.
- The full suite: no suite lock on this machine, load ~75.
- A gateway served by another magpie process: the episode lives in the
process that serves the gateway, so a GUI that doesn't serve it never
reopens the app or shows the notice.
159 lines
4.3 KiB
Go
159 lines
4.3 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
// The Codex app reads its sign-in only as it opens: an account used up
|
|
// while it runs greys its Send for every model, magpie's included, and
|
|
// stays so after magpie becomes Codex's provider (MasterYi on Discord),
|
|
// till the app is quit and opened again. What follows tells the account
|
|
// is out as soon as a reply says so, not at the next look five minutes
|
|
// on, and keeps which Codex app was open when it ran out, for the GUI to
|
|
// reopen (once) or name.
|
|
|
|
// codexLiveChecks counts the running loops that keep Codex on an account
|
|
// with room (KeepOnAnAccountWithRoom) past their first reading: while one
|
|
// does, a reply that says Codex's account is out has it read again at
|
|
// once (CodexMayBeUsedUp). A test's gateway, gone within the minute
|
|
// before that first reading, never turns it on, so tests never ask the
|
|
// vendor.
|
|
var codexLiveChecks atomic.Int32
|
|
|
|
// codexRecheckEvery is the least time between two such readings.
|
|
var codexRecheckEvery = 20 * time.Second
|
|
|
|
var codexRecheck struct {
|
|
sync.Mutex
|
|
at time.Time
|
|
}
|
|
|
|
// codexReadNow reads whether Codex's account is out; a var for tests.
|
|
var codexReadNow = func(user string) {
|
|
StaleAllowance("codex", user)
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
defer cancel()
|
|
noteCodexUsedUp(CodexUsedUp(ctx))
|
|
}
|
|
|
|
// CodexMayBeUsedUp is a reply's word that user's ChatGPT account may be
|
|
// out (usage_limit_reached, or a window heard at 100%): for the account
|
|
// Codex is signed in to, its allowance is read again at once, at most
|
|
// every codexRecheckEvery, and a change told as the five-minute look tells
|
|
// it (noteCodexUsedUp).
|
|
func CodexMayBeUsedUp(user string) {
|
|
if codexLiveChecks.Load() <= 0 || user == "" {
|
|
return
|
|
}
|
|
active := ""
|
|
for _, l := range Logins("codex") {
|
|
if l.Active && strings.EqualFold(l.User, user) {
|
|
active = l.User
|
|
}
|
|
}
|
|
if active == "" {
|
|
return
|
|
}
|
|
c := &codexRecheck
|
|
c.Lock()
|
|
if time.Since(c.at) < codexRecheckEvery {
|
|
c.Unlock()
|
|
return
|
|
}
|
|
c.at = time.Now()
|
|
c.Unlock()
|
|
go codexReadNow(active)
|
|
}
|
|
|
|
// codexAppOut is the Codex app open when the account it is signed in to
|
|
// ran out: its app-server's pid, and whether the GUI tried reopening it.
|
|
var codexAppOut struct {
|
|
sync.Mutex
|
|
user string
|
|
pid int
|
|
tried bool
|
|
checked time.Time
|
|
}
|
|
|
|
// noteCodexAppOut keeps, as the account Codex is signed in to runs out
|
|
// (out) or has room again, which Codex app was open then.
|
|
func noteCodexAppOut(out bool) {
|
|
user, pid := "", 0
|
|
if out {
|
|
for _, l := range Logins("codex") {
|
|
if l.Active {
|
|
user = l.User
|
|
}
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
ps, err := listProcesses(ctx)
|
|
cancel()
|
|
if err == nil {
|
|
pid = findCodexApp(ps)
|
|
}
|
|
}
|
|
c := &codexAppOut
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
if pid == 0 {
|
|
// none open: one opened from now reads the sign-in afresh
|
|
c.user, c.pid, c.tried = "", 0, false
|
|
return
|
|
}
|
|
c.user, c.pid, c.tried, c.checked = user, pid, false, time.Now()
|
|
}
|
|
|
|
// CodexAppOut is the ChatGPT account of a Codex app that was open when
|
|
// that account ran out and hasn't been opened again since, "" for none:
|
|
// it sends nothing till it is (the agent package's AppOut says whether
|
|
// magpie is Codex's provider for it, which the app needs too).
|
|
func CodexAppOut() string {
|
|
c := &codexAppOut
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
if c.pid == 0 {
|
|
return ""
|
|
}
|
|
if time.Since(c.checked) > codexDaemonRecheck {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
ps, err := listProcesses(ctx)
|
|
cancel()
|
|
// what was known stays when the processes can't be listed
|
|
if err == nil {
|
|
c.checked = time.Now()
|
|
if findCodexApp(ps) != c.pid {
|
|
c.user, c.pid, c.tried = "", 0, false
|
|
return ""
|
|
}
|
|
}
|
|
}
|
|
return c.user
|
|
}
|
|
|
|
// CodexAppReopenOnce is whether the Codex app CodexAppOut names may be
|
|
// reopened by magpie: true once for each time the account runs out, so
|
|
// a reopen that didn't take is never tried again.
|
|
func CodexAppReopenOnce() bool {
|
|
c := &codexAppOut
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
if c.pid == 0 || c.tried {
|
|
return false
|
|
}
|
|
c.tried = true
|
|
return true
|
|
}
|
|
|
|
// CodexAppReopenTried is whether magpie tried reopening the Codex app
|
|
// CodexAppOut names already, which it doesn't do twice.
|
|
func CodexAppReopenTried() bool {
|
|
c := &codexAppOut
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
return c.tried
|
|
}
|