Ethereal499 and Isdavexxx on X, MasterYi on Discord, #540. The Codex app greys Send for every model, magpie's too, while the ChatGPT account it is signed in to is used up. Its composer's gate (ChatGPT.app 26.1013) reads authMethod "chatgpt" plus rate limit allowed=false, and it asks account/read only when its app-server starts.ef37b6fb7(v0.1.1381) could fail in practice: - (a) The reopen was macOS-only, ran once per time out, only from the GUI process. A Windows or Linux user, a magpie without its window, or a second time out stayed greyed. - Switch-back: the app stayed on magpie (authMethod null, no cloud tasks or Log out) after the reset. Nothing restarted it. - (c) Detection lag: the account was read every 5 minutes. - The notice sat on the Providers page, not on Codex's Agents row. Now: - The gateway process (`provider.KeepCodexAppOnItsProvider`, every 3s, on every OS) ends the Codex app's own app-server each time `Agent.AppOut` flips, both ways. The app starts another at once and reads account/read afresh. Its window, threads and drafts stay. - It never ends an app-server while a Codex request is in flight through magpie or ended in the last 10s (`Server.CodexBusy`, Codex's requests only), or while a session under the Codex home has a turn open (`codexTurnOpen`). - It ends only the app's own app-server. That one runs on stdio; one another program starts with `--listen` is left alone (Miyi Helper runs one on this Mac). It must also run on magpie's Codex home (`proc.EnvOf`: ps -E on macOS, /proc on Linux; Windows can't tell). - The exit is clean (`proc.End`: SIGTERM, or TerminateProcess with code 0 on Windows). The app shows any other exit as a crash. - A listing that fails is taken up again at the next look. One that outlives 3 ends, or is denied, makes the row say to quit and reopen. - The active account is read every minute while it is held or a window is at 90% or more (`codexLookEvery`). - Codex's Agents row says "On magpie until <reset> · ChatGPT allowance used up". Hovering it, or opening the row, says what is off until then (cloud tasks, the usage meter, Log out) and that it all comes back on its own. In en, zh, zh-TW, ja and de. - Removed: gui's keepCodexAppSending/codexAppOut, provider's episode bookkeeping, and the Providers-page notice. Not changed, by design: - A Codex own model that no magpie account serves stays as it is: magpie can't serve that turn. - Account discovery off and "Only switch Codex's sign-in" (login=chatgpt) are the user's own choices, and are left alone. Full suite green at cff1a9686 (on origin/main6ce0f14b9), below. At 4541b3e97 (rebased on430e3050d, which touched gateway.go, app.js and i18n.js beside this) ran: gofmt -l, go build ./..., go vet on darwin, linux (-tags nogui ./...) and windows; go test -run Codex ./internal/provider ./internal/agent ok; GUI codex-app-out (22 ok) and codex-app-stale (20 ok), chromium and webkit. Pushed rebased onb92cb2f59(two more commits, neither in these files), built, not rerun. Run at this commit (cff1a9686, on origin/main6ce0f14b9), with a private GOCACHE (another session ran go clean -cache mid-run): - gofmt -l: clean. go build ./...: ok. - go vet: darwin (touched packages); GOOS=linux -tags nogui ./...; GOOS=windows (touched packages). - go test -count=1 -timeout 45m: internal/proc, internal/provider, internal/agent, internal/gateway, internal/gui all ok, skipping four tests red on main without this change, each already filed: TestUnreadLoginsWriteErrorsKeepCredentials (#3632, a synctest crash since091153bf) and TestFetchAsksOneListOnceForTwoBasesOfIt, TestFetchUnionsProtocolListsWithoutChat, TestFetchAsksASharedBaseOnce (#3854, since6eb510f78). All three of the latter fail the same at origin/main6ce0f14b9and at353519f55without this commit. - GUI at cff1a9686, chromium and webkit, 231 cases ok: codex-app-out (5 languages, 980 and 440px), i18n-keys, gui-de/ja/zh-tw, codex-app-stale, codex-accounts-via, agent-stale-copies, agent-app-restart, agent-failover, agent-connect, agent-layout, agent-expand-steady, agent-connected-unfolded, agent-notice-lang and codex-signin-always. - Windows box (cross-built test binaries): proc and provider's Codex app tests pass. TestEndExitsClean passes there and goes red with exit code 1. - Broken by hand, each went red: - Codex-only busy: TestCodexBusy. - The 90% cadence: TestCodexLookEvery. - The retry after a failed listing: TestCodexAppServerUnknown. - The turn guard: TestCodexAppServerRestartedEachWay. - The --listen exclusion: TestCodexAppOwnServer. - Code 0 on Windows: TestEndExitsClean. - The row's notice: codex-app-out.test.cjs, 4 of 4. - Real app: ChatGPT.app 26.1013 in a sandbox HOME/CODEX_HOME, with this build serving on 3492/3493. Codex was put on magpie as Sync does for a held account (model_provider = "magpie", codex.out in stash.json). OpenAI's usage reading to the app was never faked. - The first listing ran out at load ~200, so the timeout is now 30s. The next look ended the app-server, and the app started another 12s later. Its profile went from "Pro · Usage · Log out" to "magpie" with no reopen, and /api/state carried appOut. - Undoing it ended the app-server again, and the profile came back to Pro, Usage and Log out. - The owner's own ChatGPT app (its app-server on ~/.codex) was never touched. - One webkit codex-app-out run at load 291 had one red subtest, name lost. It passed 3 of 3 alone and in two full webkit runs. Not tried: - The Windows Store app (not installed on the box), and a real Codex app on Linux. - A real used-up account. - A turn running in the real app while the switch happens (unit-tested only).
150 lines
4.5 KiB
Go
150 lines
4.5 KiB
Go
package gateway
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
// Busy is what a restart of the magpie serving the gateway would cut
|
|
// short (#577): the agents' requests in flight — a reply streaming, an
|
|
// image being made — and the turns of Claude Code run through the
|
|
// subscription bridge waiting on their caller's tool results, whose
|
|
// Claude Code lives only in this process. A request in flight is one
|
|
// that does work (not a GET: the model lists, the info another magpie
|
|
// asks for every 15 s); the bridge's own helper calls are not counted
|
|
// apart, a turn waiting on them being in Tools. Last is when the last
|
|
// counted request ended, so a caller can tell a tool chain's gap — the
|
|
// agent running a tool between two requests — from the end of its turn.
|
|
//
|
|
// Bridge and BridgeLast are the same for the bridge's turns alone, which
|
|
// a restart that hands the gateway over (handover.go) still waits for:
|
|
// their Claude Code lives in this process, and only here can the turn go
|
|
// on. Every other request finishes in the magpie that hands over while
|
|
// the new one takes the next.
|
|
type Busy struct {
|
|
Requests int `json:"requests"`
|
|
Tools int `json:"tools"`
|
|
Last time.Time `json:"-"`
|
|
Bridge int `json:"-"`
|
|
BridgeLast time.Time `json:"-"`
|
|
}
|
|
|
|
// Any is whether there is anything in flight.
|
|
func (b Busy) Any() bool { return b.Requests > 0 || b.Tools > 0 }
|
|
|
|
// Busy says what is in flight at the gateway now.
|
|
func (s *Server) Busy() Busy {
|
|
b := Busy{Requests: int(s.inFlight.Load()), Tools: s.subscription.parked(), Bridge: int(s.bridgeInFlight.Load())}
|
|
if n := s.lastDone.Load(); n != 0 {
|
|
b.Last = time.Unix(0, n)
|
|
}
|
|
if n := s.bridgeDone.Load(); n != 0 {
|
|
b.BridgeLast = time.Unix(0, n)
|
|
}
|
|
return b
|
|
}
|
|
|
|
// CodexBusy is how many of Codex's requests are in flight and when its
|
|
// last ended: a turn of the Codex app's that a restart of its app-server
|
|
// would cut short (provider.KeepCodexAppOnItsProvider). Other agents'
|
|
// requests don't hold it.
|
|
func (s *Server) CodexBusy() (int, time.Time) {
|
|
n := 0
|
|
s.active.mu.Lock()
|
|
for _, f := range s.active.m {
|
|
if f.agent == "codex" {
|
|
n++
|
|
}
|
|
}
|
|
s.active.mu.Unlock()
|
|
var last time.Time
|
|
if d := s.codexDone.Load(); d != 0 {
|
|
last = time.Unix(0, d)
|
|
}
|
|
return n, last
|
|
}
|
|
|
|
// bridgeTurn counts a turn served through the subscription bridge for
|
|
// Busy.Bridge until done is called.
|
|
func (s *Server) bridgeTurn() (done func()) {
|
|
s.bridgeInFlight.Add(1)
|
|
return func() {
|
|
s.bridgeDone.Store(time.Now().UnixNano())
|
|
s.bridgeInFlight.Add(-1)
|
|
}
|
|
}
|
|
|
|
// counted is h with its requests counted for Busy.
|
|
func (s *Server) counted(h http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == http.MethodGet || r.Method == http.MethodHead || r.Method == http.MethodOptions || strings.HasPrefix(r.URL.Path, "/_magpie/") || strings.HasPrefix(r.URL.Path, "/mcp/") {
|
|
h.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
s.inFlight.Add(1)
|
|
r, done := s.active.add(r)
|
|
if f := inflightOf(r.Context()); f != nil {
|
|
f.mu.Lock()
|
|
f.tr = &s.trace
|
|
f.mu.Unlock()
|
|
}
|
|
w, r, unwatch := s.watchUnrouted(w, r, inflightOf(r.Context()))
|
|
defer func() {
|
|
// a panic is net/http's to recover, as before; the routes it
|
|
// left in flight end first (#2386)
|
|
if p := recover(); p != nil {
|
|
s.endBroken(inflightOf(r.Context()))
|
|
defer panic(p)
|
|
}
|
|
unwatch()
|
|
done()
|
|
s.lastDone.Store(time.Now().UnixNano())
|
|
if f := inflightOf(r.Context()); f != nil && f.agent == "codex" {
|
|
s.codexDone.Store(time.Now().UnixNano())
|
|
}
|
|
if s.inFlight.Add(-1) == 0 {
|
|
s.armRelease(s.idle.restFor())
|
|
}
|
|
}()
|
|
h.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
// busyCounters are the Server's counts for Busy.
|
|
type busyCounters struct {
|
|
inFlight atomic.Int64
|
|
lastDone atomic.Int64 // unix ns
|
|
active inflights // what each is, for /v1/magpie/concurrency
|
|
// the subscription bridge's turns among them, and when the last ended
|
|
bridgeInFlight atomic.Int64
|
|
bridgeDone atomic.Int64 // unix ns
|
|
// when Codex's last request ended (CodexBusy)
|
|
codexDone atomic.Int64 // unix ns
|
|
// handing back what requests left once it rests (idle_memory.go)
|
|
idle idleMemory
|
|
}
|
|
|
|
// parked is how many runs wait on their caller's tool results.
|
|
func (b *subscriptionBridge) parked() int {
|
|
if b == nil {
|
|
return 0
|
|
}
|
|
b.mu.Lock()
|
|
runs := make([]*subscriptionRun, 0, len(b.runs))
|
|
for _, run := range b.runs {
|
|
runs = append(runs, run)
|
|
}
|
|
b.mu.Unlock()
|
|
n := 0
|
|
for _, run := range runs {
|
|
run.mu.Lock()
|
|
if !run.closed && !run.parkedAt.IsZero() {
|
|
n++
|
|
}
|
|
run.mu.Unlock()
|
|
}
|
|
return n
|
|
}
|