Files
magpie/internal/agent/agent.go
T
Haogao Gu 20350dafaf gateway, agent, gui: Claude Desktop's subagents can be held to one model, set in its row on the Agents page
Claude Code in a terminal holds every subagent to one model through
CLAUDE_CODE_SUBAGENT_MODEL (its "subagents" field), and Codex through
magpie's own setting (settings.CodexSubagentModel). Claude Desktop runs
its Code tab's Claude Code with CLAUDE_CODE_PROVIDER_MANAGED_BY_HOST=1,
and Claude Code 2.1.295 then ignores CLAUDE_CODE_SUBAGENT_MODEL,
CLAUDE_CODE_SUBAGENT_MODEL_FORCE and the tier variables in its settings
files ("Ignoring … this session's provider routing is managed by the
host"). The env Desktop 2.31226 builds for the Code tab has no key for it
either. So Desktop's subagents ran on the lead's model or on a tier's, and
only the tiers could be picked.

Claude Desktop's row now has a "subagent model" square, the same square
and strings as Codex's. It is magpie's setting
(settings.DesktopSubagentModel), which the gateway applies
(desktopSubagentPick):

- Only a claude-desktop request with x-claude-code-agent-id and tools is
  moved: a subagent's turn. The lead's turns, the tool-less helper calls
  Claude Code makes inside a subagent (on the haiku tier's model),
  terminal Claude Code and every other agent stay as asked.
- It is applied after desktopTurn, the tiers and the groups. A turn
  already on the model set isn't marked as moved. Route.Subagent,
  Route.Model and the ledger's Requested keep the model asked for; Kind
  stays empty.
- The model set must be served by its exact id (Resolve takes any model
  name under a provider that is on), enabled, and allowed to a gateway key
  held to some models (groupAllowed for a group). Otherwise the subagent
  stays on the model asked for and the trace says why, in the reasons
  Codex's subagent model already has.
- count_tokens counts on the same model.
- It is read on each request, so Desktop needn't restart. The field is
  Live (a new Field flag): /api/set and `magpie <agent> <field> <value>`
  leave Desktop's restart notice out after it, unless the pick connected
  Desktop. Codex's subagent model is unchanged.
- The getter follows a provider renamed since it was set (RenamedRef).
  The setter takes any provider/model or group/<id>, also one not served
  now (a restored profile), refuses a bare name, and keeps a Desktop
  alias by its catalog id. Options are shown while Desktop is on magpie.
- POST /api/settings keeps it; a stopped WSL distro's field copy keeps
  Live.
- The Routing page says why a Desktop subagent was moved or kept
  (subagentWhy, for a route with a subagent pick and no kind). "it is
  disabled in magpie" gets its zh, zh-TW, ja and de, which Codex's kept
  reason lacked as well.
- Unset, the pick costs one held settings read (no body decode, no
  catalog build).

Not changed: a Desktop on another computer that reaches magpie with a
named gateway key is read as Claude Code (agentOf), so none of Desktop's
routing applies to it, this included. There is no fallback when the model
set has a smaller window than the lead's.

Verified at this head, on 20769439, under a temporary HOME with Go's
caches pinned:
- Each new test fails with the guarantee it names broken by hand (a real
  FAIL, not a compile error), then passes restored. 48 Go breaks were
  first run on 8f34bbcd: settings trim and bare-name refusal; setter
  validation and clear; RenamedRef in the getter and in Unserved; the
  exact served check and served groups; the serve pick, each guard
  (agent id, tools, claude-desktop only, unset, same and resolved model);
  the named id kept in Route.Model, Kind and the ledger's Requested; each
  kept reason (unserved, disabled, held model, held group); groupAllowed;
  the next request reading a change; the session pick kept; count_tokens;
  the unset pick allocating; Live on the field, not on the tiers or on
  Codex's, copied by WSL; options while unwired and wired; alias
  conversion; the settings keep line; the GUI and CLI notice rules.
  Re-run at this head:
  - no tools guard: TestDesktopSubagentLeavesOtherRequests/helper,
    `sent "m2", want m1`
  - no agent-id guard: TestDesktopSubagentLeavesOtherRequests/lead,
    `sent "m2", want m1`
  - no count_tokens pick: TestDesktopSubagentCountTokens,
    `count sent "m1" to "/v1/messages/count_tokens", want m2`
  - Resolve instead of the exact served check: TestDesktopSubagentUnserved,
    `fake/missing: "", want "magpie doesn't serve it now"`
  - no Live rule in /api/set: TestDesktopSubagentModelNotice (gui),
    `wired Live pick notice "Claude Desktop reads this at start-up …"`
  - no keep line in POST /api/settings: TestSettingsSaveKeepsWhatItDoesNotSend,
    `DesktopSubagentModel:  after a save of the Settings page, was fake/m2`
  GUI breaks (0 pass / 2 fail in each engine): renderSteps' new aside
  removed (Desktop's moved story missing); the four "it is disabled in
  magpie" translations removed (the kept story keeps the English reason);
  the Default note's translation removed.
- go test -tags nogui ./...: 52 packages ok, no failure.
- go test -race -count=20 -run '^TestDesktopSubagent' ./internal/gateway: ok.
- gofmt -l on the changed files: nothing. go vet ./...,
  GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...,
  go build, GOOS=linux go build -tags nogui, GOOS=windows go build: exit 0.
- Playwright 1.62.1: desktop-subagent-model.test.cjs (en, zh, zh-TW, ja,
  de; 1100 and 560 wide) with codex-subagent-model, codex-subagents-lead,
  codex-start-model-field, desktop-tiers, desktop-model-menu,
  subagent-effort and every routing-*.test.cjs: 472/472 in Chromium and
  in WebKit. The whole suite: Chromium 4580/4580; WebKit 4570/4572. The
  two are red on 20769439 untouched as well: group-fold.test.cjs 3 of 3
  alone (195a1009b, fixed upstream since by a125d5c9), usage-fold.test.cjs
  4 of 9 alone there and 4 of 9 here ("as many accounts on the first
  screen as before the curves: 12 of 29").
- The built binary in a temporary HOME, `magpie serve` on a free port, a
  fake Anthropic upstream, requests shaped like Desktop's Claude Code:
  `magpie claude-desktop subagent_model fake/m2` printed no restart
  advice; a subagent's turn and its count went to m2; the lead's turn,
  a subagent's tool-less call, terminal Claude Code's and a subagent turn
  after `… subagent_model default` went to m1; claude-desktop.model stayed
  fake/m1; the ledger kept requested fake/m1.
- Not tried: a real Claude Desktop or Cowork (that needs the live
  gateway), Linux and Windows machines (GOOS vet and build only),
  WebKitGTK.

Maintainer's review on origin/main c4c3cafa2 (#2081), under a scratchpad
HOME with Go's caches pinned, machine load 120-330:
- Claude Code 2.1.299's bundled source has the "Ignoring … this
  session's provider routing is managed by the host" path, with
  CLAUDE_CODE_SUBAGENT_MODEL_FORCE in the set it ignores.
- Breaks re-run: no tools guard -> TestDesktopSubagentLeavesOtherRequests/
  helper `sent "m2", want m1` and TestDesktopSubagentCountTokens helper
  count; no count_tokens pick -> TestDesktopSubagentCountTokens `count
  sent "m1" … want m2`; the Routing page's subagentWhy aside removed ->
  desktop-subagent-model 0 pass / 2 fail (webkit, zh).
- go test -tags nogui ./internal/{gateway,agent,settings,gui,cli,provider}:
  ok on 2fbbd1694 (agent with -timeout 40m: 1857s under the load; the
  default 10m timed out mid TestClaudeNewSessionEffortLastsModelSwitch, a
  test this change doesn't touch). After rebasing onto c4c3cafa2, the
  Desktop/Subagent/SettingsSave/Live/Notice tests of those six packages: ok.
- gofmt -l: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./..., go build, GOOS=linux go build -tags nogui,
  GOOS=windows go build: ok.
- desktop-subagent-model.test.cjs now runs at 1100 and 420 (was 560):
  20/20, chromium and webkit, en zh zh-TW ja de. codex-subagent-model,
  desktop-tiers, routing-purpose-state, routing-request-model,
  routing-sealed-task: all pass in both engines.
- usage-fold's "29 accounts" case passed 4 of 4 alone in WebKit here.
- Not repeated: the author's built-binary run; a real Claude Desktop or
  Cowork; Linux and Windows machines.

Decisions on the author's questions: Live is kept as named, and Codex's
subagent model is left as it is; a subagent's tool-less helper calls stay
on the model they asked for, being Claude Code's own housekeeping rather
than the subagent's work.

Reported-by: Koohoko (#2081)
2026-10-14 15:48:19 +08:00

601 lines
24 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Package agent describes every coding agent magpie can drive: where its config
// lives, which fields matter (model, effort, …) and which values to offer.
package agent
import (
"debug/buildinfo"
"fmt"
"os"
"path/filepath"
"runtime"
"slices"
"sort"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/proc"
"github.com/yetone/magpie/internal/provider"
)
// Option is one value the picker offers for a field.
type Option struct {
Value string `json:"value"`
Label string `json:"label,omitempty"` // display name, when the value is an id
Note string `json:"note"`
Icon string `json:"icon,omitempty"` // bundled icon name
Icons []string `json:"icons,omitempty"` // a routing group's providers' icons, stacked
Group string `json:"group,omitempty"` // section header in the picker
// GroupIcon is the group's own logo in the picker's rail, when it is
// not the first model's (a provider serving other vendors' models)
GroupIcon string `json:"groupIcon,omitempty"`
Ref string `json:"ref,omitempty"` // the catalog model, the same in every agent
Free bool `json:"free,omitempty"` // costs its subscription nothing
// Rate and RateWas are the credits a request costs its subscription,
// as a multiple, and before a discount running now (Qoder's 0.5×,
// WorkBuddy's x0.03), when its vendor lists them
Rate float64 `json:"rate,omitempty"`
RateWas float64 `json:"rateWas,omitempty"`
// Context is the tokens the model takes, when known; the picker marks
// the large ones
Context int `json:"context,omitempty"`
// Takes, on an effort field's default (Value ""), is the level the
// agent takes for the model with none set (Codex: the catalog entry's
// default_reasoning_level), shown beside "default"
Takes string `json:"takes,omitempty"`
// Direct names who the agent asks for this model itself, on its own
// sign-in or key, with magpie not in the way ("Anthropic"): its config
// then names no magpie endpoint, which is right, not a failed setup
Direct string `json:"direct,omitempty"`
// Via is a model of the agent's own it asks through magpie's gateway,
// on its own sign-in all the same (Codex's while routed by its base
// URL): the picker says via magpie by it, as by a catalog model's
Via bool `json:"via,omitempty"`
// Same is a model magpie serves on the very account the agent is
// signed in to itself, so the agent reaches it on its own too: the
// picker folds these into one row a click opens (Claude Code, #496)
Same bool `json:"same,omitempty"`
// Alias is the value of the option a dated id is another name of
// (claude-opus-4-5-20251101 → claude-opus-4-5): the picker shows one
// row for the two, the alias, unless the dated one is the value set
Alias string `json:"alias,omitempty"`
// FastFor is the agent a catalog model with a fast mode (provider.
// CanFast) is switched fast or not for, by the model in the picker
// (#954): the one whose requests the gateway sends it on; Fast is
// whether it is now (provider.IsFastPick)
FastFor string `json:"fastFor,omitempty"`
Fast bool `json:"fast,omitempty"`
// OwnFast: its fast mode is a GPT model's on a provider the user added
// by its address (provider.OwnFast, #1615), asked for there as OpenAI's
// priority, which the provider may or may not serve
OwnFast bool `json:"ownFast,omitempty"`
// own: served on the agent's own sign-in (viaMagpie), for Same
own bool
// sub: served on a subscription signed in in magpie, not a key; a
// Claude account only for Claude Code, which alone may use it safely
sub bool
}
// Field is one tunable setting of an agent. Set with an empty value puts
// the field back to the agent's own default: anything magpie wired in (the
// gateway as a provider, its model catalog) comes out and the key is removed.
type Field struct {
Key string
Label string
Get func() string
Set func(string) error
Options func(cur map[string]string) []Option
// Quiet fields are left out of listings while empty: they follow
// another field until set (Claude Code's per-tier models).
Quiet bool
// Live fields are magpie's own settings, read by the gateway on each
// request (Claude Desktop's subagent model): a change to one needs no
// restart of the agent, so its Notice isn't shown after it, unless
// the pick connected the agent to magpie as well.
Live bool
// Follows is the key of the field a Quiet one takes after while empty
// ("model" for Claude Code's tiers and subagents): a profile's details
// say so, and Drift reads the field as on that one's model (#1050).
Follows string
}
// Agent is one supported coding agent.
type Agent struct {
ID string
Name string
Icon string // bundled icon name (internal/gui/assets/icons)
Aliases []string
Bin string // executable name, used for detection
Dir string // config directory, used for detection
Path string // config file magpie edits
// Plugin is the spec of the plugin that adds this agent (plugged.go),
// "" for magpie's own
Plugin string
Fields []Field
Native *NativeConnection
// Notice, if set, is advice worth showing after a change: agents that
// read their config once at start-up need a restart to see it.
Notice func() string
// UA is what the agent's User-Agent begins with, lower-case: how the
// gateway tells its requests from others'
UA []string
// ListsModels: the agent's own model menu is magpie's models as picked
// on the Agents page for it (settings.HiddenModels, PickedModels),
// though no field of its picks among the catalog: the gateway's
// /v1/models as asked with its key (Cursor Private Inference), or the
// list magpie writes into its files, magpieModels(id), where its only
// field puts magpie in its picker (ZCode, Pencil: #1508)
ListsModels bool
// NoNames: the agent's own list doesn't name magpie's models as
// Names in agents' lists says (provider.Labels): magpie hands it the
// models' ids alone, its format having no name for one (Goose, Hermes,
// jcode …), it lists no models of magpie's at all (Gemini CLI, Snow),
// or it reads the gateway's /v1/models itself, whose display_name is
// the name alone (Empryo, Cursor Private Inference). The setting says
// so for each one connected, rather than seeming to do nothing.
NoNames bool
// Sync, for an agent that reads magpie's models from a file of its own
// rather than asking the gateway, rewrites that list as the catalog is
// now — where magpie wrote one; nothing else changes (see SyncCatalog).
Sync func() error
// Unwire, for an agent whose fields' default is the agent as installed
// rather than what it had before magpie (Codex, Claude Code, Gemini
// CLI), takes magpie out of its config and puts back what the stash
// kept: the endpoint, provider and model the user had. Disconnect runs
// it before the fields' defaults.
Unwire func() error
// Restore, for an agent whose files magpie copied aside before its
// first write (beforeMagpie), puts them back as they were where what
// Disconnect left says the same. Disconnect runs it last.
Restore func() error
// Join, for an agent that can have magpie's models in its own list
// while it stays on the model it was on (Codex signed in with ChatGPT),
// connects it so, its model left as its own last pick; false where it
// can't, and Connect then picks one of magpie's.
Join func() (bool, error)
// Joined reports an agent Join connected: magpie is in its config
// though no field is on one of magpie's models.
Joined func() bool
// Beside reports an agent set on one of magpie's models beside its own
// (Codex by the base URL beside its ChatGPT sign-in), now on one of its
// own written in by the agent: still connected, as joined, though the
// change is told as drift (#940).
Beside func() bool
// OwnVia is the catalog id magpie serves one of the agent's own models
// by on a sign-in of the user's, which its model field lists as its
// own rather than as magpie's ("codex/gpt-5.5" for Codex's gpt-5.5),
// "" for none: Connect keeps the agent on that model through magpie
// where it can't Join (#940: Codex went to an unrelated model).
OwnVia func(model string) string
// Routed reports that the agent's config sends whatever model it
// names to magpie's gateway (Codex's openai_base_url or magpie as its
// provider), so a model's name the gateway takes as a routing group
// is that group's (#750).
Routed func() bool
// FailingOver reports an agent that isn't connected whose requests
// still go through magpie's gateway, for account failover alone (Codex
// signed in to ChatGPT with more of its accounts on in magpie, #1385):
// the Agents page says so and what turns it off, and the gateway lists
// it only its own models.
FailingOver func() bool
// EndFailover takes out what account failover wrote, for magpie
// quitting: left there, the agent would be pointed at a gateway that
// no longer answers, and retry till magpie is open again (Leon920717
// on X). The next start's Sync writes it again.
EndFailover func() error
// Follow, for an agent whose own picker moves its main model where
// magpie keeps other settings following it (Claude Code's /model and
// its tiers), brings those along to the model picked there. Run as the
// Agents page is drawn.
Follow func() error
// RenameRefs, for an agent whose config names magpie's models beyond
// its fields (omp's other roles and fallback chains), moves those names
// off provider from onto to, the rest of each kept; it answers whether
// any moved. The fields themselves are RenameProvider's.
RenameRefs func(from, to string) (bool, error)
// Check, for an agent magpie wires in beyond its model field, says what
// of that wiring is gone while the model is still one of magpie's —
// something else rewrote the config — or "" when it is all there
// (see Drift).
Check func() string
// LastUsed, for an agent that keeps a log of its own prompts, is when
// it was last used — each prompt a request the gateway should have
// seen, so one it didn't went round magpie (see Drift). Zero if unknown.
LastUsed func() time.Time
// Reached, for an agent that logs where its model requests go, is its
// newest one since a time: when, the address it went to, and whether
// nothing answered there. Zero if there is none.
Reached func(since time.Time) (at time.Time, to string, refused bool)
// WSL is the distro an agent inside WSL lives in, "" for this
// machine's own (see wsl.go).
WSL string
// Home is a WSL agent's $HOME in its distro as magpie opens it
// (\\wsl.localhost\<distro>\home\me), where its other files are; ""
// for this machine's, and while the distro is stopped: opening it
// would start it.
Home string
// Gateway is the gateway's address as the agent reaches it, a WSL
// distro's own way to it; nil is gateway.URL.
Gateway func() string
// Import, for an app that takes magpie only through an import link of
// its own, which the user confirms there (Cindy), is that link; the app
// has no fields magpie sets. Added says whether it has magpie already.
Import func() string
Added func() bool
// Launch, for an agent that takes the gateway only from its
// environment (agy), is the command that starts it on magpie, while
// it is on one of magpie's models; "" otherwise.
Launch func() string
// SplitSuffix, for an agent whose model values carry something of its
// own after the model that varies from value to value (omp's thinking
// level, "…:max"), splits a value into the model the picker offers and
// that suffix, "" when there is none. What matches a value against the
// picker (drift, Reseat, RenameProvider, Spell) matches the model and
// puts the suffix back after the one it moves to. A mark that is always
// the same for a model (Claude Code's [1m]) rides on the option instead.
// one is false for a value that is no one model but a list the agent
// falls back through (omp's "a,b"): that is the user's own whatever it
// names, never taken for one of magpie's models nor moved by what
// matches the picker (RenameRefs moves the names in it).
SplitSuffix func(v string) (model, suffix string, one bool)
// Spelled, when set, says whether a value of the agent's fields is
// spelled as one of magpie's there (prefixed: it starts with
// "magpie/", its provider in the agent): one that isn't is a model of
// one of the agent's own providers, never magpie's, even when magpie
// has a provider of the same name (OpenHanako's own
// deepseek/deepseek-v4-pro read as magpie's deepseek, #835).
Spelled func(v string) bool
// ModelAliases are the names the agent's model takes besides ids
// (Claude Code's opus, best, opusplan). One that names a field as well
// is the model's on the command line: magpie claude opus sets the
// model, where another field's name alone shows that field.
ModelAliases []string
// detect, when set, says whether the agent is here in place of looking
// for its files and binary: a distro's, probed once.
detect func() bool
// reach, when set, is the gateway's address as the agent's config has
// it where that is kept apart from Gateway (this machine's Codex at an
// address of the user's, #816): Drift tries it (see reach.go).
reach func() string
// move, when set, points the agent's config at to where it names the
// gateway at from: an address of WSL's that changed (#1013).
move func(from, to string) error
// viaProxy, when set, says whether the agent's requests for the
// gateway get there through the proxy it sends them by (ok), whether
// that is known yet, and the proxy and where it is set; ok with no
// proxy when it sends them straight there (see codex_proxy.go).
viaProxy func() (ok, known bool, proxy, from string)
// dirShared says Dir is a folder another agent keeps its files in too
// (omp's, when PI_CODING_AGENT_DIR points it at Pi's): that it is there
// says nothing of this agent.
dirShared bool
// pathOwn, when set, says whether the file at Path is this agent's,
// not one another tool wrote there first: a JetBrains plugin's
// settings.json in Qoder CN's folder (#1495).
pathOwn func() bool
}
// Running reports whether a process whose command line matches any pattern
// (an extended regexp, as for pgrep -f) is alive. Windows can't be asked
// what runs, so anything may be: every caller is the advice an agent's own
// lists need after magpie changed what it reads at start ("restart Codex",
// "open a new dsh session"), and a Windows that answered no here dropped
// that advice silently — a model picked in magpie looked like it had done
// nothing at all. claudeRunning and Pencil's own check already say they
// can't be told, and say yes for the same reason. A var so tests can say
// what runs, as claudeRunning is: magpie's own CLI tests printed "restart
// the Codex app" on a Mac with Codex open, and on every Windows.
var Running = func(patterns ...string) bool {
if runtime.GOOS == "windows" {
return len(patterns) > 0
}
for _, pat := range patterns {
if err := proc.Command("pgrep", "-f", pat).Run(); err == nil {
return true
}
}
return false
}
// Detected reports whether the agent seems to be installed or configured.
func (a *Agent) Detected() bool {
if a.detect != nil {
return a.detect()
}
// a file where the agent keeps its folder is another tool's (a shell's
// ~/.dsh), and the agent can't be here: it couldn't make its folder
if a.Dir != "" && Taken(a.Dir) {
return false
}
if _, err := os.Stat(a.Path); err == nil && (a.pathOwn == nil || a.pathOwn()) {
return true
}
if a.Dir != "" && !a.dirShared && agentDir(a.Dir) {
return true
}
if a.Bin != "" && binAt(a.Bin) != "" {
return true
}
// one with no Bin, as its command's name is other tools' too, that
// Homebrew installed (Grok Build's grok-build cask) and hasn't run yet
if a.Bin == "" && a.WSL == "" && brewBin(cliSpecs[a.ID]) != "" {
return true
}
return false
}
// binAt is where the agent's command name is, as magpie runs it: on
// magpie's own PATH, else, on Windows, where a terminal opened now finds it
// or a user's tools are installed (proc.FindTool: the registry's PATH now,
// ~/.local/bin, npm's, bun's, Volta's, pnpm's). Windows has no login shell
// to lend magpie its PATH (proc.UserPath does nothing there), so a magpie
// started before Claude Code's installer put claude.exe in ~/.local/bin
// and added that to the user's PATH, or a self-update restarting with the
// PATH it had then, never listed it, while the sign-in, which asked
// FindTool, found it (#839, #1482). A mise wrapper script found there is
// given as the program it runs (proc.LookPath). "" when it is in none.
func binAt(name string) string {
if p, err := proc.LookPath(name); err == nil {
return p
}
if runtime.GOOS == "windows" {
return proc.FindTool(name)
}
return ""
}
// agentDir reports whether p is a folder an agent itself made. Skill
// installers (npx skills and the like) make <folder>/skills for every agent
// they know, installed or not; a folder holding nothing else is theirs.
func agentDir(p string) bool {
es, err := os.ReadDir(p)
if err != nil {
return isDir(p)
}
if len(es) == 0 {
return true
}
for _, e := range es {
if n := e.Name(); n != "skills" && n != ".DS_Store" && !(e.IsDir() && leftover[strings.ToLower(n)]) {
return true
}
}
return false
}
// leftover are the folders an agent writes as it runs, not as it is set
// up, and that stay behind when it is uninstalled: ~/.codebuddy holding
// only logs/ and diagnostics/, ~/.qwen's debug/ (#1495, v5tech). A folder
// holding only these (and skills) is no sign the agent is here. A file of
// the same name, or anything else beside them, still is.
var leftover = map[string]bool{"logs": true, "log": true, "debug": true, "diagnostics": true, "cache": true}
// Taken reports whether something that isn't a folder is where the folder
// p, or one it is in, would be: nothing can be written under it.
func Taken(p string) bool {
for d := filepath.Clean(p); ; {
if st, err := os.Stat(d); err == nil {
return !st.IsDir()
}
up := filepath.Dir(d)
if up == d {
return false
}
d = up
}
}
// goProgram reports whether bin was built by Go: another tool of the same
// name, not the agent, when the agent is not written in Go. Reading a
// binary's build info parses its whole symbol table (~150ms for a large
// one), and detection runs on every state the window asks for, so the
// answer is kept while the file is the same.
func goProgram(bin string) bool {
st, err := os.Stat(bin)
if err != nil {
return false
}
key := goProgramKey{bin, st.Size(), st.ModTime()}
goPrograms.Lock()
defer goPrograms.Unlock()
if v, ok := goPrograms.m[key]; ok {
return v
}
_, err = buildinfo.ReadFile(bin)
if goPrograms.m == nil {
goPrograms.m = map[goProgramKey]bool{}
}
goPrograms.m[key] = err == nil
return err == nil
}
type goProgramKey struct {
path string
size int64
mod time.Time
}
var goPrograms struct {
sync.Mutex
m map[goProgramKey]bool
}
// Field looks a field up by key, in any case, as Find looks an agent up
// (magpie codex Model was written as the model).
func (a *Agent) Field(key string) *Field {
for i := range a.Fields {
if strings.EqualFold(a.Fields[i].Key, key) {
return &a.Fields[i]
}
}
for i := range a.Fields {
if strings.EqualFold(a.Fields[i].Label, key) { // `magpie gemini auth …`: the label as shown
return &a.Fields[i]
}
}
return nil
}
// Values reads every field.
func (a *Agent) Values() map[string]string {
m := make(map[string]string, len(a.Fields))
for _, f := range a.Fields {
m[f.Key] = f.Get()
}
return m
}
// Picks are the options f's picker offers: all of them, unless the user
// asked a's pickers to offer magpie's models alone (#1642, MizuIro-H). Then
// a field that picks among magpie's catalog leaves out the models the agent
// reaches on its own (DeepSeek Harness's DeepSeek ones), but for its
// default and the one it is set to.
func (a *Agent) Picks(f Field, cur map[string]string) []Option {
opts := f.Options(cur)
if !provider.OwnModelsHidden(a.ListsFor()) || !slices.ContainsFunc(opts, func(o Option) bool { return o.Ref != "" }) {
return opts
}
return slices.DeleteFunc(slices.Clone(opts), func(o Option) bool { return Own(o) && o.Value != cur[f.Key] })
}
// Own reports whether o is a model the agent reaches on its own, not one of
// magpie's catalog: what Picks leaves out on the user's word.
func Own(o Option) bool { return o.Ref == "" && o.Value != "" }
// OwnModels reports whether one of a's pickers that picks among magpie's
// catalog offers models of the agent's own too, which Picks can leave out.
func (a *Agent) OwnModels(cur map[string]string) bool {
for _, f := range a.Fields {
opts := f.Options(cur)
if slices.ContainsFunc(opts, func(o Option) bool { return o.Ref != "" }) && slices.ContainsFunc(opts, Own) {
return true
}
}
return false
}
// Detected returns the agents present on this machine, in display order.
func Detected() []*Agent {
var out []*Agent
for _, a := range All() {
if a.Detected() {
out = append(out, a)
}
}
return out
}
// Find resolves a user-typed name (id, alias, or unique prefix).
func Find(q string) (*Agent, error) {
q = strings.ToLower(strings.TrimSpace(q))
all := All()
var prefix []*Agent
for _, a := range all {
if strings.EqualFold(a.ID, q) { // codex@wsl:Ubuntu
return a, nil
}
for _, al := range a.Aliases {
if al == q {
return a, nil
}
}
if strings.HasPrefix(a.ID, q) || strings.HasPrefix(strings.ToLower(a.Name), q) {
prefix = append(prefix, a)
}
}
switch len(prefix) {
case 1:
return prefix[0], nil
case 0:
return nil, fmt.Errorf("unknown agent %q (try: %s)", q, strings.Join(ids(all), ", "))
}
return nil, fmt.Errorf("%q is ambiguous: %s", q, strings.Join(ids(prefix), ", "))
}
func ids(as []*Agent) []string {
s := make([]string, len(as))
for i, a := range as {
s[i] = a.ID
}
sort.Strings(s)
return s
}
// Spell puts a value typed for field key the way this agent spells it: a
// catalog model is "copilot/gpt-6" in some agents and "magpie/copilot/gpt-6"
// in others, and either is taken in both. A value the picker offers as is
// stays, and so does one it doesn't know (a model the agent reaches on its
// own that isn't listed); a "magpie/…" value the catalog doesn't have is an
// error rather than a model the agent would ask its own vendor for. The
// agent's suffix after the model (SplitSuffix) stays as typed, and so does
// a list of models, the user's own.
func (a *Agent) Spell(key, v string) (string, error) {
f := a.Field(key)
if f == nil || f.Options == nil || v == "" {
return v, nil
}
model, suffix, one := a.split(v)
if !one {
return v, nil
}
opts := f.Options(a.Values())
for _, o := range opts {
if o.Value == model {
return v, nil
}
}
ref, prefixed := strings.CutPrefix(model, magpieID+"/")
// a provider renamed since (a profile saved before) is the same one
refs := []string{ref}
if r := provider.RenamedRef(ref); r != ref {
refs = append(refs, r)
}
for _, r := range refs {
for _, o := range opts {
if o.Ref != "" && o.Ref == r {
return o.Value + suffix, nil
}
}
}
if prefixed {
return "", fmt.Errorf("%s isn't a model in magpie's catalog (magpie models lists them)", ref)
}
return v, nil
}
// split is v as the model the picker offers and the agent's suffix after
// it (SplitSuffix), one false for a list of models; the whole of v for an
// agent without one.
func (a *Agent) split(v string) (model, suffix string, one bool) {
if a.SplitSuffix == nil {
return v, "", true
}
return a.SplitSuffix(v)
}
// atomic makes each of an agent's field sets, and its Sync, one edit of the
// files at paths: one that fails part way puts them all back as they were,
// rather than leaving, say, Codex's config.toml with magpie's provider table
// written but its model not (#253).
func atomic(a *Agent, paths ...string) *Agent {
for i := range a.Fields {
if set := a.Fields[i].Set; set != nil {
a.Fields[i].Set = func(v string) error {
return edit.Atomically(func() error { return set(v) }, paths...)
}
}
}
if sync := a.Sync; sync != nil {
a.Sync = func() error { return edit.Atomically(sync, paths...) }
}
if follow := a.Follow; follow != nil {
a.Follow = func() error { return edit.Atomically(follow, paths...) }
}
if unwire := a.Unwire; unwire != nil {
a.Unwire = func() error { return edit.Atomically(unwire, paths...) }
}
return a
}