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.
175 lines
6.7 KiB
Go
175 lines
6.7 KiB
Go
package agent
|
|
|
|
// JetBrains Air, JetBrains' agentic development environment, runs its own
|
|
// agents (Claude Agent, Codex, Gemini CLI, Junie) on the accounts signed in
|
|
// under Settings | Account | AI Providers: a sign-in or an API key, with no
|
|
// base URL of its own to point at magpie (AIR-4541 asks for one). What it
|
|
// does take is any agent that speaks the Agent Client Protocol, listed in
|
|
// acp.json in its config folder — ~/Library/Application Support/JetBrains/Air
|
|
// on macOS, $XDG_CONFIG_HOME/JetBrains/Air on Linux, %APPDATA%\JetBrains\Air
|
|
// on Windows — as
|
|
//
|
|
// {"agent_servers": {"<name in Air's agent menu>": {"command": …, "args": […], "env": {…}}}}
|
|
//
|
|
// and the models such an agent reports at session/new are its model menu
|
|
// in Air. So magpie adds an agent of its own there, "Magpie": OpenCode's ACP
|
|
// server (opencode acp), handed with OPENCODE_CONFIG a config file magpie
|
|
// keeps beside acp.json that has magpie's provider, its models, the one
|
|
// picked as the model a task starts on, and enabled_providers narrowed to
|
|
// magpie, so Air's menu lists magpie's models and routing groups and nothing
|
|
// of OpenCode's own. OpenCode merges that file over the user's own config
|
|
// and leaves the rest of it (their plugins, agents, MCP servers) as it is.
|
|
// Air's own agents, and the accounts they run on, aren't touched.
|
|
//
|
|
// Checked with OpenCode 2.0.20's opencode acp under a sandbox HOME, as Air
|
|
// starts it: session/new's model option lists magpie's models alone, the
|
|
// one set here current. Not with Air itself. One gap is OpenCode's: when a
|
|
// provider's key is in the environment it is started with (OPENAI_API_KEY,
|
|
// DEEPSEEK_API_KEY, … set, not empty), 2.0.20's ACP server lists that
|
|
// provider and none of its config's — this file's, the global one or a
|
|
// project's alike — so magpie's models don't show; an emptied key, or one
|
|
// kept in OpenCode's auth.json, doesn't do it.
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/appdir"
|
|
"github.com/yetone/magpie/internal/edit"
|
|
"github.com/yetone/magpie/internal/proc"
|
|
)
|
|
|
|
// airAgent is the name magpie's agent has in acp.json, and so in Air's
|
|
// agent menu.
|
|
const airAgent = "Magpie"
|
|
|
|
// airDir is where Air keeps its settings and acp.json.
|
|
func airDir(home, cfg string) string {
|
|
switch runtime.GOOS {
|
|
case "darwin":
|
|
return filepath.Join(home, "Library", "Application Support", "JetBrains", "Air")
|
|
case "windows":
|
|
d := appdir.Getenv("APPDATA")
|
|
if d == "" {
|
|
d = filepath.Join(home, "AppData", "Roaming")
|
|
}
|
|
return filepath.Join(d, "JetBrains", "Air")
|
|
}
|
|
return filepath.Join(cfg, "JetBrains", "Air")
|
|
}
|
|
|
|
// airOpenCode is the OpenCode Air is to start: the one on PATH, else the
|
|
// one OpenCode's installer puts in ~/.opencode/bin, as a full path, since
|
|
// Air, an app, may not have the shell's PATH; "" when there is none.
|
|
var airOpenCode = func(home string) string {
|
|
if p, err := proc.LookPath("opencode"); err == nil {
|
|
if abs, err := filepath.Abs(p); err == nil {
|
|
return abs
|
|
}
|
|
return p
|
|
}
|
|
name := "opencode"
|
|
if runtime.GOOS == "windows" {
|
|
name += ".exe"
|
|
}
|
|
if p := filepath.Join(home, ".opencode", "bin", name); isFile(p) {
|
|
return p
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func air(home, cfg string) *Agent {
|
|
dir := airDir(home, cfg)
|
|
path := filepath.Join(dir, "acp.json")
|
|
// the OpenCode config magpie's agent runs on, magpie's own file
|
|
ocPath := filepath.Join(dir, "magpie-opencode.json")
|
|
entry := "agent_servers." + airAgent
|
|
provider := theirsKept(ocPath, "provider."+magpieID, func() any { return magpieProviderJSONFor("opencode", "air") }, "models")
|
|
// ours: acp.json's Magpie is the agent magpie added, on its file
|
|
ours := func() bool {
|
|
v, _ := edit.GetJSON(path, entry+".env.OPENCODE_CONFIG")
|
|
return v == ocPath
|
|
}
|
|
// model is the one a task starts on, read from magpie's own file: with
|
|
// Magpie gone from acp.json it still reads, for Check to say so and a
|
|
// set again to put the entry back
|
|
model := func() string { v, _ := edit.GetJSON(ocPath, "model"); return v }
|
|
return atomic(&Agent{
|
|
ID: "air", Name: "JetBrains Air", Icon: "air", Aliases: []string{"jetbrains-air"},
|
|
// no Bin: air is other tools' name too (Go's live reloader); its
|
|
// requests are its OpenCode's, and counted as OpenCode's
|
|
Dir: dir, Path: path,
|
|
Notice: func() string {
|
|
if !ours() {
|
|
return ""
|
|
}
|
|
if airOpenCode(home) == "" {
|
|
return noticeAir1.String()
|
|
}
|
|
return noticeAir2.String()
|
|
},
|
|
Check: func() string {
|
|
if !usesMagpie(model()) {
|
|
return ""
|
|
}
|
|
if !ours() {
|
|
return "Air's Magpie agent (" + filepath.Base(path) + ") is gone, so Air no longer reaches magpie"
|
|
}
|
|
return wiringOff("Air", ocPath, func(k string) (string, bool) { return edit.GetJSON(ocPath, "provider."+magpieID+".options."+k) },
|
|
"baseURL", gatewayV1(), "apiKey", hereKey())
|
|
},
|
|
Sync: func() error {
|
|
return syncJSONInOrder(ocPath, "provider."+magpieID, provider)
|
|
},
|
|
Fields: []Field{{
|
|
Key: "model", Label: "model",
|
|
Get: model,
|
|
Set: func(v string) error {
|
|
if v == "" {
|
|
if err := edit.DelJSON(path, entry); err != nil {
|
|
return err
|
|
}
|
|
if err := edit.Remove(ocPath); err != nil && !os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
ref, ok := strings.CutPrefix(v, magpieID+"/")
|
|
if !ok || !isMagpie(ref) {
|
|
return fmt.Errorf("Air takes only magpie's models here (its own agents are picked in Air): %s isn't one", v)
|
|
}
|
|
// each key new to the file goes in at its top: last first,
|
|
// so a new file reads $schema, model, …, provider
|
|
if err := edit.SetJSON(ocPath,
|
|
edit.KV{Path: "provider." + magpieID, Value: provider()},
|
|
edit.KV{Path: "enabled_providers", Value: []string{magpieID}},
|
|
// titles and summaries too, as the user's own small
|
|
// model may be a provider left out here
|
|
edit.KV{Path: "small_model", Value: v},
|
|
edit.KV{Path: "model", Value: v},
|
|
edit.KV{Path: "$schema", Value: "https://opencode.ai/config.json"}); err != nil {
|
|
return err
|
|
}
|
|
bin := airOpenCode(home)
|
|
if bin == "" {
|
|
bin = "opencode"
|
|
}
|
|
return edit.SetJSON(path, edit.KV{Path: entry, Value: map[string]any{
|
|
"command": bin, "args": []string{"acp"},
|
|
"env": map[string]string{"OPENCODE_CONFIG": ocPath},
|
|
}})
|
|
},
|
|
Options: func(map[string]string) []Option { return viaMagpie("air", magpieID+"/") },
|
|
}},
|
|
}, path, ocPath)
|
|
}
|
|
|
|
// what air says after a change (notice.go)
|
|
var (
|
|
noticeAir1 = newNotice("Air runs magpie's models through OpenCode's ACP server, and OpenCode isn't installed here: install OpenCode (npm i -g opencode-ai), then pick Magpie in a new Air task.")
|
|
noticeAir2 = newNotice("In Air, start a new task and pick Magpie in its agent menu: magpie's models and routing groups are in its model menu.")
|
|
)
|