deluxe (Omarchy Linux, magpie started from the desktop) never saw the
Claude sign-in link. ~/.local/bin/claude was a script ending
`exec mise x "claude" -- "claude" "$@"`; without the shell's mise
environment, `mise x` looks claude up on PATH, finds the same script
(~/.local/bin is before mise's shims) and it runs itself for ever.
What changed:
- proc.Unwrap / proc.LookPath (internal/proc/wrapper.go): a small #!
script whose line is `[exec] mise|rtx x|exec [opts] [tool] -- <name>`
is resolved with `mise which <name>` (then `mise which --tool <tool>
<name>`), run as a probe (process group killed) with a 5s timeout,
magpie's environment and the home folder as cwd. The answer must be an
executable file that isn't the wrapper or another wrapper; otherwise,
or with no mise, the old path is kept. Answers are cached per file
(path, mtime, size); a failed ask is retried after a minute.
Windows is unchanged.
- proc.FindTool (claude via claudecode.Find: the sign-in, the gateway's
Claude subscription, `magpie claude-code`) unwraps what it finds.
- The siblings that looked an agent CLI up with exec.LookPath use
proc.LookPath: agent.binAt (detection, CLI version/update), codex,
kiro-cli, devin, cursor-agent, Air's opencode, nativemodels' npm
package lookup.
- The Claude sign-in already gave up after linkWait (30s) with "claude
auth login gave no link to open". It now adds the last line the CLI
printed, or, when it printed nothing, the program it ran and to run
`<path> auth login` in a terminal to see what it does.
asdf shims (`exec asdf exec "claude" "$@"`) are left alone: asdf exec
resolves the real program itself and doesn't recurse.
Verified (macOS, sandbox HOME, Go caches pinned):
- New tests: internal/proc/wrapper_test.go (wrapper -> mise's program;
no mise -> wrapper; mise fails / names the wrapper / names a folder /
hangs -> wrapper, and the hung mise's pid is gone; deluxe's own fixed
script, a node launcher, a wrapper of another command, a commented
line -> unchanged and mise never asked; parse cases; --tool retry),
internal/provider/signin_claude_wrapper_test.go (the full Claude
sign-in through the wrapper with a fake mise that really recurses:
link shown, account added; with no `mise which`, the sign-in fails in
linkWait naming the wrapper).
- Break checks: with find.go put back, TestFindToolUnwrapsMiseWrapper and
TestFindToolAsksWithWrappersTool FAIL (FindTool = the wrapper), and
TestClaudeSignInThroughMiseWrapper FAILs with "claude auth login gave
no link to open: .../local-bin/claude printed nothing in 5s". With the
old error string, TestClaudeSignInWrapperThatNeverStarts FAILs.
- gofmt -l clean; go vet ./...; GOOS=linux go vet -tags nogui ./...;
GOOS=windows go vet ./...; GOOS=linux go build -tags nogui;
GOOS=windows go build.
- go test -tags nogui ./...: every package ok except internal/agent and
internal/gateway, which hit the default 10m timeout with the machine
at load ~45 (different tests each run, none in the new code). Those
two were rerun with -timeout 40m; origin/main 217d2f8f2's
internal/agent alone took 2094s under the same load.
- Rebased on 9964535ad, -timeout 40m: internal/agent ok (1689s),
internal/proc ok, internal/provider ok (681s), internal/claudecode ok;
internal/gateway FAIL in one test, TestCodexHeardWithinItsIdleTimeout
(3.78s against its 3s wall-clock bound, body right, no retry taken).
Alone with -count=5 it passes 5 of 5. Filed as #1800; unrelated to
binary lookup.
Not verified: no real mise or the reporter's Omarchy machine; the fake
mise stands in for mise's which/x. The wrapper fixture is the line the
reporter quoted, not their whole file.
193 lines
6.1 KiB
Go
193 lines
6.1 KiB
Go
package agent
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/filememo"
|
|
"github.com/yetone/magpie/internal/proc"
|
|
)
|
|
|
|
// A Pi-family agent's built-in providers are the ones its own model
|
|
// registry ships, not models.dev's: models.dev has no openai-codex (Pi's
|
|
// "OpenAI Codex (legacy)" sign-in, the ChatGPT backend), so a Pi signed in
|
|
// to it was offered none of its models (#709). The registry is read from
|
|
// the package the agent was installed from, so it is the list that agent
|
|
// itself offers in its /model, and moves with it on every update.
|
|
|
|
// nodeModulesOf is where a CLI installed from one of pkgs resolves its
|
|
// packages: its own package's node_modules, then the node_modules it sits
|
|
// in (npm, bun and Homebrew's npm link the command into it). nil when the
|
|
// command isn't on PATH or wasn't installed from npm. A var for tests.
|
|
var nodeModulesOf = findNodeModules
|
|
|
|
func findNodeModules(bin string, pkgs []string) []string {
|
|
path, err := proc.LookPath(bin)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
real, err := filepath.EvalSymlinks(path)
|
|
if err != nil {
|
|
real = path
|
|
}
|
|
slash := filepath.ToSlash(real)
|
|
for _, pkg := range pkgs {
|
|
modules := ""
|
|
if i := strings.Index(slash, "/node_modules/"+pkg+"/"); i >= 0 {
|
|
modules = filepath.FromSlash(slash[:i] + "/node_modules")
|
|
} else if d := filepath.Join(filepath.Dir(path), "node_modules"); isDir(filepath.Join(d, filepath.FromSlash(pkg))) {
|
|
modules = d // npm's shim on Windows sits beside its node_modules
|
|
}
|
|
if modules != "" {
|
|
return []string{filepath.Join(modules, filepath.FromSlash(pkg), "node_modules"), modules}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// safeProvider says p can be a file name: auth.json's keys come from the
|
|
// user's file.
|
|
func safeProvider(p string) bool {
|
|
return p != "" && p != "." && p != ".." && !strings.ContainsAny(p, `/\:`)
|
|
}
|
|
|
|
// registryModel is a model as Pi's and omp's registries list it.
|
|
type registryModel struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
Type string `json:"type"` // Pi: chat, image, classifier
|
|
Kind string `json:"kind"` // omp: "" for chat, image, embedding…
|
|
Input []string `json:"input"`
|
|
Context int `json:"contextWindow"`
|
|
Output int `json:"maxTokens"`
|
|
}
|
|
|
|
func (m registryModel) chat() bool {
|
|
return m.ID != "" && (m.Type == "" || m.Type == "chat") && (m.Kind == "" || m.Kind == "tiny")
|
|
}
|
|
|
|
func (m registryModel) model(provider string) catalog.Model {
|
|
name := m.Name
|
|
if name == "" {
|
|
name = m.ID
|
|
}
|
|
images := false
|
|
for _, in := range m.Input {
|
|
images = images || in == "image"
|
|
}
|
|
return catalog.Model{ID: m.ID, Name: name, Provider: provider, Images: images, Context: m.Context, Output: m.Output}
|
|
}
|
|
|
|
// orderedObject calls fn with each key and value of a JSON object, in the
|
|
// order the file has them: the registry's order is the agent's own.
|
|
func orderedObject(b []byte, fn func(key string, v json.RawMessage) error) error {
|
|
d := json.NewDecoder(bytes.NewReader(b))
|
|
if t, err := d.Token(); err != nil || t != json.Delim('{') {
|
|
return errors.New("not an object")
|
|
}
|
|
for d.More() {
|
|
t, err := d.Token()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
k, _ := t.(string)
|
|
var v json.RawMessage
|
|
if err := d.Decode(&v); err != nil {
|
|
return err
|
|
}
|
|
if err := fn(k, v); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// modelsIn is the chat models of an object of models by id, in its order.
|
|
func modelsIn(b []byte, provider string) []catalog.Model {
|
|
var out []catalog.Model
|
|
seen := map[string]bool{}
|
|
orderedObject(b, func(_ string, v json.RawMessage) error {
|
|
var m registryModel
|
|
if json.Unmarshal(v, &m) == nil && m.chat() && !seen[m.ID] {
|
|
seen[m.ID] = true
|
|
out = append(out, m.model(provider))
|
|
}
|
|
return nil
|
|
})
|
|
return out
|
|
}
|
|
|
|
// piPackages are the npm packages Pi is installed from, newest name first.
|
|
var piPackages = []string{"@earendil-works/pi-coding-agent", "@mariozechner/pi-coding-agent"}
|
|
|
|
// piRegistry is the models Pi's installed registry has for provider p:
|
|
// pi-ai's dist/providers/data/<p>.json (Pi 1.0), groups by API of models
|
|
// by "type:id". nil when Pi or the provider isn't found there.
|
|
func piRegistry(bin string) func(p string) []catalog.Model {
|
|
return func(p string) []catalog.Model {
|
|
if !safeProvider(p) {
|
|
return nil
|
|
}
|
|
for _, modules := range nodeModulesOf(bin, piPackages) {
|
|
for _, ai := range []string{"@earendil-works/pi-ai", "@mariozechner/pi-ai"} {
|
|
file := filepath.Join(modules, filepath.FromSlash(ai), "dist", "providers", "data", p+".json")
|
|
ms, err := filememo.Read("pi models", file, func(b []byte) ([]catalog.Model, error) {
|
|
var out []catalog.Model
|
|
err := orderedObject(b, func(_ string, group json.RawMessage) error {
|
|
out = append(out, modelsIn(group, p)...)
|
|
return nil
|
|
})
|
|
return out, err
|
|
})
|
|
if err == nil && len(ms) > 0 {
|
|
return ms
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// ompRegistry is the models omp's installed catalog has for provider p:
|
|
// @oh-my-pi/pi-catalog's src/models.json, models by id under each provider.
|
|
func ompRegistry(p string) []catalog.Model {
|
|
for _, modules := range nodeModulesOf("omp", []string{"@oh-my-pi/pi-coding-agent"}) {
|
|
file := filepath.Join(modules, "@oh-my-pi", "pi-catalog", "src", "models.json")
|
|
all, err := filememo.Read("omp models", file, func(b []byte) (map[string][]catalog.Model, error) {
|
|
out := map[string][]catalog.Model{}
|
|
err := orderedObject(b, func(provider string, v json.RawMessage) error {
|
|
out[provider] = modelsIn(v, provider)
|
|
return nil
|
|
})
|
|
return out, err
|
|
})
|
|
if err == nil {
|
|
if ms := all[p]; len(ms) > 0 {
|
|
return ms
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// codexServed is, for openai-codex when the agent's registry isn't found,
|
|
// the models Codex CLI last listed for a ChatGPT account: the same backend.
|
|
func codexServed(p string) []catalog.Model {
|
|
if p != "openai-codex" {
|
|
return nil
|
|
}
|
|
ms := catalog.Codex()
|
|
for i := range ms {
|
|
ms[i].Provider = p
|
|
}
|
|
return ms
|
|
}
|
|
|
|
// nativeProviderNames are the names of built-in providers models.dev
|
|
// doesn't list, as their agents call them.
|
|
var nativeProviderNames = map[string]string{"openai-codex": "OpenAI Codex"}
|