From 7693cf26344d5bbd019ee9ba0195812dedaf6c2f Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 13 Nov 2026 13:00:10 +0800 Subject: [PATCH] agent: a folder holding only magpie's sign-in file is no agent (jiangshangmuyun on X) Codex, never installed, was listed on the Agents page as "not connected" once a ChatGPT account was added in Providers. With Codex signed in to nothing, addLogin hands it that account and saveCodexLogin writes ~/.codex/auth.json, which makes ~/.codex; Detected took any non-empty Dir for the agent. Claude Code is the sibling off the Mac: putClaudeLogin writes ~/.claude/.credentials.json when Claude Code has none. Agent.signIn names that file (auth.json for both Codex agents, .credentials.json for Claude Code), and agentDir passes over it. A folder with anything else of the agent's beside it (config.toml, sessions/, settings.json, projects/, history.jsonl), its binary, or its app still lists it. Run: - gofmt -l; go vet on darwin, linux (-tags nogui) and windows, on mote. - go test -count=1 ./internal/agent ./internal/gui on mote: ok. - win-run on xiaomi-gpu: TestSignInFileAloneIsNoAgent, TestClaude*Windows*, TestDetected*: ok. Broken by hand: agentDir not passing over the sign-in file: TestSignInFileAloneIsNoAgent red for codex and claude (../tray-jump-scratch/break.log). Not tried: a real ChatGPT sign-in in a sandbox HOME with no Codex. A Codex app installed but never opened, with only magpie's auth.json in ~/.codex, is not listed until it is opened (it then writes its own files there). --- docs/subsystems/agent-wiring.md | 2 +- internal/agent/agent.go | 14 ++++-- internal/agent/claude.go | 2 +- internal/agent/codex.go | 2 +- internal/agent/codex_profile.go | 2 +- internal/agent/signin_folder_test.go | 65 ++++++++++++++++++++++++++++ 6 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 internal/agent/signin_folder_test.go diff --git a/docs/subsystems/agent-wiring.md b/docs/subsystems/agent-wiring.md index 3ae56f526..27225c4a6 100644 --- a/docs/subsystems/agent-wiring.md +++ b/docs/subsystems/agent-wiring.md @@ -12,7 +12,7 @@ something else shows up as drift. | Part | Responsibility | Source | | --- | --- | --- | -| Agent description | `Agent` has an id, name, icon, `Bin` and `Dir` for detection, `Path`, `Fields` and `Native`. It also has optional hooks: `Notice`, `UA`, `ListsModels`, `Sync`, `Unwire`, `Join`/`Joined`, `Routed`, `Follow`, `RenameRefs`, `Check`, `LastUsed`, `Reached`, `Import`/`Added`, `Launch`, `SplitSuffix`, `Spelled` and `ModelAliases`. Each hook's comment says which agents need it. | [`agent.go`](../../internal/agent/agent.go) | +| Agent description | `Agent` has an id, name, icon, `Bin` and `Dir` for detection, `Path`, `Fields` and `Native`. It also has optional hooks: `Notice`, `UA`, `ListsModels`, `Sync`, `Unwire`, `Join`/`Joined`, `Routed`, `Follow`, `RenameRefs`, `Check`, `LastUsed`, `Reached`, `Import`/`Added`, `Launch`, `SplitSuffix`, `Spelled` and `ModelAliases`. Each hook's comment says which agents need it. A `Dir` holding only its `signIn` file (Codex's `auth.json`, Claude Code's `.credentials.json`), which a sign-in added in magpie writes for an agent signed in to nothing, is no sign the agent is installed (`TestSignInFileAloneIsNoAgent`; jiangshangmuyun on X). | [`agent.go`](../../internal/agent/agent.go) | | Fields and options | A `Field` has `Get`, `Set` and `Options`. `Set("")` returns the field to the agent's default and takes magpie's wiring out. `Quiet` fields follow another field until set (`Follows`, such as Claude Code's tiers). An `Option` carries the picker's facts: `Direct`, `Via`, `Same`, `Alias`, `Rate` and `Context`. The GUI's and TUI's pickers list `Agent.Picks`: with the agent in settings' `ownModelsHidden` (the switch at the top of its model list, #1642), a field that offers catalog models (`Ref`) leaves out the agent's own (no `Ref`), but for its default and the value it is set to. The CLI still takes an own model by name. | [`agent.go`](../../internal/agent/agent.go), [`route.go`](../../internal/agent/route.go) | | The agent's own telemetry | `Agent.Telemetry` (`Get`, `Set`) is the agent's documented switch for the usage data it sends its vendor: Codex's `[analytics] enabled = false`, Claude Code's env `DISABLE_TELEMETRY` and `DISABLE_ERROR_REPORTING`, Gemini CLI's and Qwen Code's `privacy.usageStatisticsEnabled = false` (OipsAnthony on X), Crush's `options.disable_metrics = true`, Kimi Code's top-level `telemetry = false` in config.toml, Goose's `GOOSE_TELEMETRY_ENABLED: false` in config.yaml, Pi's `enableInstallTelemetry = false` and Cline CLI's `telemetryOptOut = true` in global-settings.json (daoche_wuzui on X). Antigravity has none to write (agy's `EnableTelemetry` is `json:"-"`; it is the Google account's setting), nor do Droid (organisation-set), OpenCode and omp (OpenTelemetry is opt-in), Cursor CLI (account privacy mode) and Hermes (sends none). TOML and YAML files are read strictly (`topTelemetryFile`), so a file that doesn't parse is unknown. It is not a `Field`, so profiles never carry it and Unwire leaves it. Off writes only keys the file lacks and records them in the stash (`.telemetry`); a key the user set on refuses Off, naming it, and one they set off stays. On takes out only what magpie wrote and still holds magpie's value. An unreadable file is unknown: the row says so and can't be pressed. Offered for an agent on this computer only, never a WSL distro's (#3896). The GUI row is `telemetryRow` in the agent's Settings; the route is `POST /api/agents/telemetry/{id}`. Settings › Privacy's "Turn off telemetry for all agents" (`settings.AgentTelemetryOff`, `POST /api/settings/agent-telemetry`, `SetTelemetryAll`) is above every row: on, each agent here is turned off by the same rules and marked in the stash (`.telemetry.all`) unless it was off already; an agent found later is turned off as `state()` sees it and by the gateway's `KeepTelemetryAllOff` loop; each row then shows off, held (`ErrTelemetryAllOff`), or why it couldn't. Off again, only the marked agents are turned back on, so each is as its own row had it. The setting is this computer's (`KeepOwn`), and an unreadable settings.json writes nothing. | [`telemetry.go`](../../internal/agent/telemetry.go), [`telemetry_all.go`](../../internal/agent/telemetry_all.go), [`api.go`](../../internal/gui/api.go) | | The list of agents | `All` lists every agent magpie knows, then the WSL agents (`wslAgents`). `Clients` lists every agent whose requests the gateway knows by name, which `usage.Agents` reads. One file per agent: `claude.go`, `codex.go`, `opencode` in `agents.go`, and so on. | [`agents.go`](../../internal/agent/agents.go) | diff --git a/internal/agent/agent.go b/internal/agent/agent.go index d0457e9ff..982f63481 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -371,6 +371,12 @@ type Agent struct { // not one another tool wrote there first: a JetBrains plugin's // settings.json in Qoder CN's folder (#1495). pathOwn func() bool + // signIn is the file a sign-in added in magpie writes in Dir for the + // agent when it has none of its own (Codex's auth.json, Claude Code's + // .credentials.json off the Mac): a folder holding only that is + // magpie's, no sign the agent is here (jiangshangmuyun on X: Codex, + // never installed, listed after a ChatGPT account was added). + signIn string // sign, when set, is one more sign the agent is here, for a folder // that is dirShared: a file only the agent (or its own app) writes // there (the Qoder app's status file, #2138). @@ -444,7 +450,7 @@ func (a *Agent) Detected() bool { if _, ok := statKind(a.Path); ok && (a.pathOwn == nil || a.pathOwn()) { return true } - if a.Dir != "" && !a.dirShared && agentDir(a.Dir) { + if a.Dir != "" && !a.dirShared && agentDir(a.Dir, a.signIn) { return true } if a.sign != nil && a.sign() { @@ -484,7 +490,9 @@ func binAt(name string) string { // agentDir reports whether p is a folder an agent itself made. Skill // installers (npx skills and the like) make /skills for every agent // they know, installed or not; a folder holding nothing else is theirs. -func agentDir(p string) bool { +// own names a file magpie may have written there itself (Agent.signIn), +// no sign either. +func agentDir(p string, own ...string) bool { es, err := os.ReadDir(p) if err != nil { return isDir(p) @@ -493,7 +501,7 @@ func agentDir(p string) bool { return true } for _, e := range es { - if n := e.Name(); n != "skills" && n != ".DS_Store" && !(e.IsDir() && leftover[strings.ToLower(n)]) { + if n := e.Name(); n != "skills" && n != ".DS_Store" && !(e.IsDir() && leftover[strings.ToLower(n)]) && !(!e.IsDir() && slices.Contains(own, n)) { return true } } diff --git a/internal/agent/claude.go b/internal/agent/claude.go index 325f658ee..3101d27db 100644 --- a/internal/agent/claude.go +++ b/internal/agent/claude.go @@ -2343,7 +2343,7 @@ func claudeIn(at place) *Agent { self = &Agent{ ID: "claude", Name: "Claude Code", Icon: "claudecode-color", Aliases: []string{"cc", "claude-code"}, UA: []string{"claude-cli", "claude-code"}, - Bin: "claude", Dir: filepath.Dir(path), Path: path, + Bin: "claude", Dir: filepath.Dir(path), Path: path, signIn: ".credentials.json", Fields: fields, ModelAliases: claudeAliases, Telemetry: claudeTelemetry(at, path), diff --git a/internal/agent/codex.go b/internal/agent/codex.go index f923feed3..dc90eeb10 100644 --- a/internal/agent/codex.go +++ b/internal/agent/codex.go @@ -1830,7 +1830,7 @@ func codexIn(at place) *Agent { } a := atomic(&Agent{ - ID: "codex", Name: "Codex", Icon: "codex-color", Bin: "codex", Dir: dir, Path: path, + ID: "codex", Name: "Codex", Icon: "codex-color", Bin: "codex", Dir: dir, Path: path, signIn: "auth.json", Telemetry: codexTelemetry(at, path), UA: []string{"codex", "acp-extension-codex"}, // The ACP adapter's default client name becomes Codex's originator. // Codex as it was before magpie: its default puts it back as diff --git a/internal/agent/codex_profile.go b/internal/agent/codex_profile.go index b32a9f043..8062e16a4 100644 --- a/internal/agent/codex_profile.go +++ b/internal/agent/codex_profile.go @@ -149,7 +149,7 @@ func codexProfileIn(at place) *Agent { return nil } return atomic(&Agent{ - ID: "codex", Name: "Codex", Icon: "codex-color", Bin: "codex", Dir: dir, Path: path, + ID: "codex", Name: "Codex", Icon: "codex-color", Bin: "codex", Dir: dir, Path: path, signIn: "auth.json", UA: []string{"codex", "acp-extension-codex"}, Unwire: drop, Routed: wired, diff --git a/internal/agent/signin_folder_test.go b/internal/agent/signin_folder_test.go new file mode 100644 index 000000000..f70e987f8 --- /dev/null +++ b/internal/agent/signin_folder_test.go @@ -0,0 +1,65 @@ +package agent + +import ( + "os" + "path/filepath" + "testing" +) + +// jiangshangmuyun on X: Codex, never installed, was on the Agents page +// after a ChatGPT account was added in Providers. With Codex signed in to +// nothing, that sign-in is written to ~/.codex/auth.json for Codex, and +// the folder it made was taken for Codex. A folder holding only the file +// magpie's sign-in writes (Claude Code's .credentials.json off the Mac +// too) is no sign of the agent; anything the agent writes beside it is. +func TestSignInFileAloneIsNoAgent(t *testing.T) { + for _, tc := range []struct { + id, dir, file string + mk func(string) *Agent + other []string + }{ + {"codex", ".codex", "auth.json", codex, []string{"config.toml", "sessions/", "version.json"}}, + {"claude", ".claude", ".credentials.json", claude, []string{"settings.json", "projects/", "history.jsonl"}}, + } { + t.Run(tc.id, func(t *testing.T) { + fresh := func() (string, string) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("APPDATA", filepath.Join(home, "AppData", "Roaming")) + t.Setenv("LOCALAPPDATA", filepath.Join(home, "AppData", "Local")) + t.Setenv("CODEX_HOME", "") + t.Setenv("CLAUDE_CONFIG_DIR", "") + t.Setenv("PATH", t.TempDir()) + dir := filepath.Join(home, tc.dir) + if err := os.MkdirAll(dir, 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, tc.file), []byte("{}"), 0o600); err != nil { + t.Fatal(err) + } + return home, dir + } + home, _ := fresh() + if tc.mk(home).Detected() { + t.Fatalf("~/%s holding only magpie's %s taken for the agent", tc.dir, tc.file) + } + for _, o := range tc.other { + home, dir := fresh() + p := filepath.Join(dir, o) + var err error + if o[len(o)-1] == '/' { + err = os.MkdirAll(p, 0o700) + } else { + err = os.WriteFile(p, []byte("{}"), 0o600) + } + if err != nil { + t.Fatal(err) + } + if !tc.mk(home).Detected() { + t.Errorf("~/%s with %s beside %s not taken for the agent", tc.dir, o, tc.file) + } + } + }) + } +}