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.
115 lines
3.5 KiB
Go
115 lines
3.5 KiB
Go
package gui
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"runtime"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/agent"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// The Codex app reads its sign-in only as it opens. When the ChatGPT
|
|
// account it is on runs out, it greys its Send for every model, magpie's
|
|
// too, and making magpie Codex's provider (agent AppOut) reaches it only
|
|
// once it is quit and opened again (MasterYi on Discord). On macOS magpie
|
|
// does that itself, once each time the account runs out: only while the
|
|
// app open then still runs (provider.CodexAppOut, its app-server's pid
|
|
// unchanged, so it hasn't read the provider), the account is out, and no
|
|
// turn has gone through magpie for codexAppOutIdle. The app's own code
|
|
// keeps an unsent message's draft (its composer-prompt-drafts-v2 store)
|
|
// and its threads across a quit. Elsewhere, or when the quit didn't take,
|
|
// Codex's accounts say to reopen it.
|
|
|
|
var (
|
|
codexAppOutEvery = 10 * time.Second
|
|
codexAppOutIdle = 30 * time.Second
|
|
// codexAppOutReopen quits the Codex app at bundle and opens it again
|
|
codexAppOutReopen = agent.ReopenCodexApp
|
|
// codexAppOutBundles are the Codex apps open from before Codex's
|
|
// config last changed
|
|
codexAppOutBundles = func() []string {
|
|
a, err := agent.Find("codex")
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
var out []string
|
|
for _, c := range a.StaleCopies() {
|
|
if c.Kind == "app" && c.Bundle != "" {
|
|
out = append(out, c.Bundle)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
// codexAgentOut is whether magpie is Codex's provider for its used-up
|
|
// ChatGPT account
|
|
codexAgentOut = func() bool {
|
|
a, err := agent.Find("codex")
|
|
return err == nil && a.AppOut != nil && a.AppOut()
|
|
}
|
|
codexAppOutOS = runtime.GOOS
|
|
// what provider keeps of the app open as the account ran out
|
|
codexAppOutUser = provider.CodexAppOut
|
|
codexAppOutTried = provider.CodexAppReopenTried
|
|
codexAppOutOnce = provider.CodexAppReopenOnce
|
|
codexAppOutUsedUp = provider.CodexUsedUpKnown
|
|
)
|
|
|
|
// codexAppOutJSON is a Codex app left unable to send: the account it is
|
|
// signed in to, and whether magpie is still to reopen it.
|
|
type codexAppOutJSON struct {
|
|
User string `json:"user"`
|
|
Reopening bool `json:"reopening,omitempty"`
|
|
}
|
|
|
|
// codexAppOutState is what Codex's accounts say of it; nil for none.
|
|
func codexAppOutState() *codexAppOutJSON {
|
|
u := codexAppOutUser()
|
|
if u == "" || !codexAgentOut() {
|
|
return nil
|
|
}
|
|
return &codexAppOutJSON{User: u, Reopening: codexAppOutOS == "darwin" && !codexAppOutTried()}
|
|
}
|
|
|
|
// reopenCodexAppOut reopens the Codex app left unable to send, if every
|
|
// condition above holds now; it says whether it tried.
|
|
func reopenCodexAppOut(ctx context.Context, now time.Time) bool {
|
|
if codexAppOutOS != "darwin" || codexAppOutUser() == "" || codexAppOutTried() {
|
|
return false
|
|
}
|
|
if !codexAgentOut() || !codexAppOutUsedUp() {
|
|
return false
|
|
}
|
|
if b := gatewayBusy(); b.Any() || now.Sub(b.Last) < codexAppOutIdle {
|
|
return false
|
|
}
|
|
bundles := codexAppOutBundles()
|
|
if len(bundles) == 0 || !codexAppOutOnce() {
|
|
return false
|
|
}
|
|
for _, b := range bundles {
|
|
c, cancel := context.WithTimeout(ctx, time.Minute)
|
|
if err := codexAppOutReopen(c, b); err != nil {
|
|
log.Printf("codex: reopening %s for its used-up account: %v", b, err)
|
|
}
|
|
cancel()
|
|
}
|
|
return true
|
|
}
|
|
|
|
// keepCodexAppSending runs reopenCodexAppOut every codexAppOutEvery until
|
|
// ctx ends.
|
|
func keepCodexAppSending(ctx context.Context) {
|
|
t := time.NewTicker(codexAppOutEvery)
|
|
defer t.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case now := <-t.C:
|
|
reopenCodexAppOut(ctx, now)
|
|
}
|
|
}
|
|
}
|