Files
magpie/internal/provider/codex_models.go
T
AronnaxandClaude Opus 5.5 ec4ee5a3a4 fix(proc): end what a CLI probe started, on its timeout and when magpie exits (#414)
* fix(proc): end what a CLI probe started, on its timeout and when magpie exits

The probes that ask an agent's CLI something (claude auth status,
cursor-agent about/models/status, the --version checks) ran through
proc.CommandContext, which on its timeout kills only the command itself.
A CLI that is a script (a version manager's wrapper, a shim) left what it
had started running, adopted by init; and a probe still asking when magpie
exited (a one-shot command, an update, a restart) was left whole, with
nothing to end it. With Omarchy's mise wrappers stuck in their exec loop
(omacom/omarchy#7234) that was five processes holding about half a core
each for up to six hours.

proc.ProbeContext gives such a probe a session of its own on Unix, as
StartTree does, and its context ending kills the whole group. Probes still
running are listed, and proc.EndProbes kills them when magpie exits (after
run in main) or puts a new version in its place (update.Reexec). On
Windows a probe is killed alone, as before, a job being joined only once
the command has started.

TestProbeTimeoutEndsWhatItStarted and TestEndProbes fail without the
change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(proc): end probes on a terminal's signals and on a Mac's quit

Review of the first change: a probe leading its own session no longer
gets the Ctrl+C, hang-up or SIGTERM sent to magpie's terminal or service,
and magpie ending on the signal never reached EndProbes, so the probe was
left to init where before it died with magpie. main now catches SIGINT,
SIGTERM and SIGHUP, ends the probes and exits as the signal would have
(128+n). A signal magpie was started ignoring (nohup, a background job)
stays ignored. The account sign-in and plugin commands, which end
themselves on Ctrl+C, go through interruptContext and are left to do so.

On a Mac the desktop app's quit ([NSApp terminate:], also the restart to
update) exits without returning to main, so OnShutdown ends the probes.

TestProbeTimeoutEndsWhatItStarted ended its probe on a 500ms timeout,
which on a Mac could come before the script had written its child's pid
(6 of 10 runs failed in review). It now ends the context once that pid is
read, the same Cancel path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: let the app and the TUI quit on a signal their own way

Review of the signal handling: endProbesOnSignal exited ahead of Wails
and bubbletea, which quit on SIGINT and SIGTERM themselves. `magpie tray`
exited 143 without OnShutdown (no downloaded update installed, and
sessions.Saved skipped in main), and `magpie tui` left the terminal on its
alternate screen with the cursor hidden. The app's and the TUI's entry
points now hand the signals back (ownSignals): the app's OnShutdown ends
the probes, and the TUI comes back to main, which ends them too. A
one-shot command keeps the handler.

TestOwnSignalsHandsThemBack fails without the change (magpie exited 143
before the command could quit its own way); TestSignalExits keeps the
one-shot behavior.

childPid waited 3s for a script just written to start its child, which a
busy Mac can exceed (2 of 3 full runs failed in review). It waits 30s now
and, should the probe end first, says the probe's error rather than "the
script started nothing".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: keep SIGHUP for the app and the TUI, and die of the signal

ownSignals handed SIGHUP back along with SIGINT and SIGTERM, but neither
Wails nor bubbletea handles it: the terminal closing under the TUI or an
app started from a shell ended magpie on the spot, and its probes, in a
session of their own, out of the hang-up's reach, kept running (before
this PR they were in the terminal's group and ended with it). SIGHUP now
stays with endProbesOnSignal. A tray under a pty whose terminal closes
left 2 processes before, none now.

The handler asked whether a command was ending itself on Ctrl+C only
after EndProbes, which can take up to a second; a sign-in done in the
meantime got magpie ended in the middle of its own cleanup. It is asked
first.

It exited 128+n, which a shell takes for a child that handled the signal:
a loop running magpie went on to the next run on Ctrl+C. It now dies of
the signal (handling reset, the signal sent again); Windows, which can't
send one, still exits with the code.

agentProbe and agentCommand share withProxy; the Windows probeGroup
comment no longer says EndProbes ends what a shim started.

TestOwnSignalsKeepsHangUp and TestInterruptWhileEndingProbes fail without
the change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: hand signals to the TUI and the app only once they listen

Review: runTUI handed SIGINT and SIGTERM back before tui.Run, whose
newModel asks agents' CLIs (claude auth status, up to 5s) before
bubbletea listens for signals. Ctrl+C, SIGTERM or SIGINT while it loaded
ended magpie by Go's default and left the probe and its child running.
tui.Run now takes ready, called once the model is made, and runTUI passes
ownSignals.

The app had the same window: runGUI handed them back before gui.Run,
which starts the backend (probes included) before Wails' Run starts
listening. ownSignals is now gui.Started, called on ApplicationStarted,
which Wails emits after its signal handler is set up. A tray sent SIGTERM
0.05s after starting, a probe under way, left 2 processes before and
none now; one sent it once started still quits through OnShutdown
(exit 0).

TestTUIKeepsSignalsWhileLoading fails without the change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 00:42:48 +08:00

359 lines
11 KiB
Go

package provider
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"maps"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"slices"
"sort"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/codexcat"
"github.com/yetone/magpie/internal/proc"
)
// codexClientVersion is the Codex CLI version the models list is asked for
// when Codex CLI has not asked itself yet: the list leaves out models newer
// than the client asking.
const codexClientVersion = "0.159.0"
// codexModels asks the ChatGPT backend which Codex models the account's own
// plan has — a Free account lists fewer than a Plus or Pro one, and one it
// doesn't have fails with a 400. It is the list Codex CLI keeps in
// models_cache.json, but that one is of whichever account Codex CLI last
// asked with, if it ran at all.
func codexModels(ctx context.Context, sign func(context.Context, *http.Request, []byte) error) ([]catalog.Model, error) {
u := CodexBase + "/models?client_version=" + url.QueryEscape(codexVersion())
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
if err != nil {
return nil, err
}
if err := sign(ctx, req, nil); err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, _ := io.ReadAll(io.LimitReader(resp.Body, 4<<20))
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("ChatGPT models: %s", resp.Status)
}
saveCodexPrompts(b)
ms := parseCodexModels(b)
if len(ms) == 0 {
return nil, errors.New("ChatGPT listed no Codex models")
}
return ms, nil
}
var codexVersionCache struct {
sync.Mutex
v string
at time.Time
}
// codexVersion is the client version the models list is asked for: the
// newest of the Codex CLI installed, the one Codex CLI last asked with and
// codexClientVersion. The list leaves out models newer than the client
// asking, and Codex CLI's models_cache.json keeps the version it was written
// with until Codex next asks — after an update that brought new models
// (0.155 GPT-6 Luna and Sol), asking with it would leave them out.
func codexVersion() string {
codexVersionCache.Lock()
defer codexVersionCache.Unlock()
if time.Since(codexVersionCache.at) >= 10*time.Minute {
v := codexClientVersion
newer := func(c string) {
if c = claudeSemverRE.FindString(c); c != "" && compareClaudeVersion(c, v) > 0 {
v = c
}
}
var c struct {
ClientVersion string `json:"client_version"`
}
if b, err := os.ReadFile(filepath.Join(codexCLIHome(), "models_cache.json")); err == nil && json.Unmarshal(b, &c) == nil {
newer(c.ClientVersion)
}
if exe := codexExecutable(); exe != "" {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
if out, err := proc.ProbeContext(ctx, exe, "--version").Output(); err == nil {
newer(string(out)) // "codex-cli 0.155.1"
}
cancel()
}
codexVersionCache.v, codexVersionCache.at = v, time.Now()
}
return newerVersion(codexVersionCache.v, codexSeen.get())
}
// codexCLIHome is where Codex CLI keeps its state: CODEX_HOME, else ~/.codex.
func codexCLIHome() string {
if dir := os.Getenv("CODEX_HOME"); dir != "" {
return dir
}
home, _ := os.UserHomeDir()
return filepath.Join(home, ".codex")
}
// newerVersion is the later of two versions, a when b isn't one.
func newerVersion(a, b string) string {
if b = claudeSemverRE.FindString(b); b != "" && (a == "" || compareClaudeVersion(b, a) > 0) {
return b
}
return a
}
// codexSeen is the newest version a Codex client that came through the
// gateway said it was. Codex CLI is often out of magpie's reach — installed
// where a desktop app's PATH doesn't go, its models_cache.json written by
// the version before an update — and the backend serves a model only to a
// client new enough for it: signed by the pool as an older Codex, a model
// Codex CLI reaches on its own answers 400 "The 'gpt-6.1-sol' model is not
// supported when using Codex with a ChatGPT account".
var codexSeen seenVersion
type seenVersion struct {
sync.Mutex
v string
}
func (s *seenVersion) get() string {
s.Lock()
defer s.Unlock()
return s.v
}
func (s *seenVersion) saw(v string) {
s.Lock()
s.v = newerVersion(s.v, v)
s.Unlock()
}
// SawCodexClient notes the version a Codex client's request says it is —
// its `version` header, else the one in its User-Agent (codex_cli_rs/0.159.0,
// Codex Desktop/0.159.0) — when the request is Codex's (it names an
// originator, or its User-Agent is codex_…).
func SawCodexClient(h http.Header) {
ua := h.Get("User-Agent")
if h.Get("originator") == "" && !strings.HasPrefix(strings.ToLower(ua), "codex") {
return
}
v := claudeSemverRE.FindString(h.Get("version"))
if v == "" {
if _, rest, ok := strings.Cut(ua, "/"); ok {
if m := claudeSemverRE.FindStringIndex(rest); m != nil && m[0] == 0 {
v = rest[:m[1]]
}
}
}
if v == "" {
return
}
codexSeen.saw(v)
}
// codexExecutable finds the codex CLI; a var so tests can fake it. Beyond
// PATH it looks where npm, nvm, bun, volta, pnpm, mise and the standalone
// installer put it, which a desktop app's PATH lacks.
var codexExecutable = func() string {
if p, err := exec.LookPath("codex"); err == nil {
return p
}
names := []string{"codex"}
if runtime.GOOS == "windows" {
names = []string{"codex.cmd", "codex.exe", "codex"}
}
for _, d := range proc.UserBinDirs() {
for _, n := range names {
p := filepath.Join(d, n)
if st, err := os.Stat(p); err == nil && !st.IsDir() {
return p
}
}
}
return ""
}
// parseCodexModels reads the backend's list, the listed ones in its order.
func parseCodexModels(b []byte) []catalog.Model {
var list struct {
Models []struct {
Slug string `json:"slug"`
DisplayName string `json:"display_name"`
Visibility string `json:"visibility"`
Priority int `json:"priority"`
Input []string `json:"input_modalities"`
Levels []struct {
Effort string `json:"effort"`
} `json:"supported_reasoning_levels"`
Context int `json:"context_window"`
Max int `json:"max_context_window"`
} `json:"models"`
}
if json.Unmarshal(b, &list) != nil {
return nil
}
sort.SliceStable(list.Models, func(i, j int) bool { return list.Models[i].Priority < list.Models[j].Priority })
var out []catalog.Model
for _, m := range list.Models {
if m.Slug == "" || m.Visibility == "hide" {
continue
}
mm := catalog.Model{ID: m.Slug, Name: m.DisplayName, Provider: "openai", Context: m.Context}
if m.Max > m.Context {
mm.MaxContext = m.Max
}
if m.Input != nil {
yes := slices.Contains(m.Input, "image")
mm.ImageInput, mm.Images = &yes, yes
}
for _, l := range m.Levels {
mm.Efforts = append(mm.Efforts, l.Effort)
}
out = append(out, mm)
}
return out
}
// accountModels names where one account's own model list is kept.
func accountModels(agent, user string) string {
return agent + "@" + keyID(strings.ToLower(user))
}
// Lists reports whether the account's plan has the model, as far as magpie
// knows: one whose list was never fetched is taken to have them all.
func (a *Account) Lists(model string) bool {
if a == nil {
return true
}
if a.plugin != nil {
return a.pluginLists(model)
}
live, _, ok := catalog.Live(accountModels(a.Agent, a.User))
if !ok {
return true
}
return slices.ContainsFunc(live, func(m catalog.Model) bool { return m.ID == model })
}
// codexFetchSaved asks for the models of each saved ChatGPT account that
// stands behind the one Codex is signed in to, with that account's own
// sign-in, so the gateway doesn't send one a model its plan lacks. One that
// can't be asked now keeps what it listed last.
func codexFetchSaved(ctx context.Context) {
for _, l := range Logins("codex") {
if l.Active || !l.On {
continue
}
user := l.User
sign := codexSign(func(ctx context.Context) (string, string, error) { return savedLoginToken(ctx, "codex", user) })
// through the account's own proxy, if it has one
if ms, err := codexModels(ViaLogin(ctx, "codex", user), sign); err == nil {
catalog.SaveLive(accountModels("codex", user), CodexBase, ms)
}
}
}
// CodexListed is the catalog as a Codex signed in to ChatGPT is handed it,
// after the backend's own models: all but a ChatGPT account's in magpie,
// which the backend lists already. A group answers for its first member
// but is not that provider's.
func CodexListed() []catalog.Model {
shown, _ := CatalogFor("codex")
find := GroupFinder()
return codexListed(shown, func(id string) []Member {
_, ms, _ := find(id)
return ms
})
}
// CodexNativeHidden is the ChatGPT account's own model slugs the user took
// out of Codex's list (HiddenModels): the backend lists them, and the
// gateway drops them from its /models answer as it does the ones not picked.
func CodexNativeHidden() map[string]bool {
off := HiddenModels("codex")
if len(off) == 0 {
return nil
}
out := map[string]bool{}
for _, e := range Catalog() {
if off[e.ID] && e.Group == "" && e.Provider.Account != nil && e.Provider.Account.Agent == "codex" {
out[e.Model] = true
}
}
return out
}
// CodexListTag names the list Codex is handed, for its ETag: magpie's models
// and the account's own taken out of it, so either changing has Codex ask
// for the list again.
func CodexListTag() string {
ms := CodexListed()
off := slices.Sorted(maps.Keys(CodexNativeHidden()))
for _, slug := range off {
ms = append(ms, catalog.Model{ID: "-" + slug})
}
return codexcat.Tag(ms)
}
// CodexNativePicked is the set of the ChatGPT account's own model slugs the
// user kept, and whether they narrowed that list at all. The backend lists
// every model the account can reach; when the user has picked among them on
// the codex provider, the gateway keeps its /models answer to those (see
// codexModels). Not narrowed — the account's list is left whole.
//
// A provider switched off picks nothing, as it serves no agent anything
// (Provider.Off): its picks are kept for when it is switched on again and
// are not a narrowing now.
func CodexNativePicked() (map[string]bool, bool) {
p, ok := find(All(), "codex")
if !ok || p.Off || len(p.Models) == 0 {
return nil, false
}
keep := make(map[string]bool, len(p.Models))
for _, id := range p.Models {
keep[id] = true
}
return keep, true
}
// codexListed marks a group Fast when a ChatGPT account's GPT model is in
// it, so Codex offers /fast there too; the tier goes out only to that
// account (buildResponses).
func codexListed(shown []Entry, members func(id string) []Member) []catalog.Model {
var ms []catalog.Model
// named among all shown: the account's own, which the backend lists,
// are in Codex's picker beside these
labels := Labels(shown)
for i, e := range shown {
if e.Group == "" && e.Provider.Account != nil && e.Provider.Account.Agent == "codex" {
continue
}
m := catalog.Model{ID: e.ID, Name: labels[i], Efforts: e.Efforts, Images: e.Images, Context: e.Context}
if e.Group != "" {
for _, mb := range members(e.ID) {
if a := mb.Provider.Account; a != nil && a.Agent == "codex" && strings.HasPrefix(mb.Model, "gpt-") {
m.Fast = true
break
}
}
}
ms = append(ms, m)
}
return ms
}