Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

agent: Claude Desktop on an imported profile for magpie's gateway is connected, and Reconnect keeps it

yetone634cbf587b91
diff --git a/docs/subsystems/agent-wiring.md b/docs/subsystems/agent-wiring.md
index 6f8e975cc..7302b60cd 100644
--- a/docs/subsystems/agent-wiring.md
+++ b/docs/subsystems/agent-wiring.md
@@ -75,6 +75,7 @@ something else shows up as drift.
- A config that starts with a UTF-8 byte order mark (a Windows `settings.json` or `config.toml` saved by PowerShell, Notepad or a channel switcher, #1534) is read without it (`edit.Read`), and a write keeps it (`keepBOM` in `WriteAtomic`): the user's file keeps the encoding it was saved in. That covers every format `internal/edit` writes (JSON/JSONC and its arrays, TOML, YAML, env); Codex reads a TOML file through a BOM too, so magpie no longer refuses one. A file read some other way trims it with `edit.TrimBOM` (the Claude Code and CC Switch imports). A line an edit takes out of a CRLF file goes with its `\r\n`.
- Codex signed in to ChatGPT stays beside its sign-in (`openai_base_url`) unless the user picks magpie API. magpie becomes its provider only while the Codex app holds the active account, which it sends nothing for: `/wham/usage` says it isn't allowed, and it has no credits and isn't a workspace still within its overage (`codexHeld`, read by `provider.CodexUsedUp`). Where the app goes by what usage can't show (an experiment's gate, a reserve, a Team plan's overage under the reserve experiment), it counts as held: a wrong move costs ChatGPT extras, a missed one every turn. A saved reading kept through a failed read stops saying held once its used-up window has started again; a hold with no window used up stays till a fresh read. The app then loses its ChatGPT state (durable threads, remote control), so a window at 100% alone, which credits get past, doesn't move it (`codex.out` marks the move, and Sync undoes it once the account has room). The same holds on one of Codex's own models joined beside the sign-in (MasterYi on Discord: the Codex app greyed Send for every model on a used-up account): Sync makes magpie the provider on that model as magpie serves it (`codexOwnViaMagpie`, `codex/<model>`), keeps the model picked in `codex.out_own`, and once the account has room puts Codex back joined on it; a model the user picked meanwhile stays theirs. With no ChatGPT account in magpie to serve it, Codex is left as it is (`TestCodexOwnModelGoesThroughMagpieWhileUsedUp`, `TestCodexOwnModelKeptOnChatGPT`). The account is read again at once when a reply says it may be out (a `usage_limit_reached` rest, or an `x-codex-*` window heard at 100%; `provider.CodexMayBeUsedUp`, at most every 20s, the active account only), not only on the five-minute look. The Codex app reads its sign-in only as it opens (`authMethod`, told again only on `account/updated`), so a running app stays greyed after the switch: `provider.CodexAppOut` keeps the app open as the account ran out (its app-server's pid), and on macOS the GUI reopens it once per time out (`keepCodexAppSending`, through `ReopenCodexApp`) when magpie is the provider for it (`Agent.AppOut`), the account is still out, no turn is in flight through the gateway and none ended in the last 30s; the app's bundled code keeps an unsent draft (`composer-prompt-drafts-v2`) across a quit, not watched in a real app. Off macOS, or once tried, Codex's accounts say to quit and reopen it (`codexAppOut`, `renderCodexAppOut`; `TestCodexAppOutReopenedOnce`, `codex-app-out.test.cjs`).
- Moving a field from one of magpie's models to another (the agent's own picker) is not drift. Moving it off magpie is.
+- Claude Desktop applying a configLibrary profile other than magpie's is not drift when that profile is a gateway profile for this magpie's gateway (`desktopAppliedGateway` in [`claudedesktop.go`](../../internal/agent/claudedesktop.go)): Desktop's "Import configuration" adds a file exported on another computer, magpie's profile there with the user's connectors (`managedMcpServers`), as a new entry under a random id (aa86222 on Discord). magpie keeps it applied on Reconnect and through Sync, and writes in it only `inferenceGatewayApiKey` (with a missing auth scheme) when the gateway would refuse its key (`desktopKeyApplied`; magpie's own keys count as taken, `desktopKeyTaken`). A profile for another gateway, CC Switch's among them, is still drift, and Reconnect applies magpie's. The first switch-on still starts magpie's profile from an applied one (#2035). `TestClaudeDesktopImportedProfileIsOnMagpie`.
- A field that follows another while empty (`Follows`: Claude Code's tiers and subagents on its main model) is read by `Drift` as on that field's model. The main model moved, in Claude Code's own `/model`, onto the model magpie had set a tier to is not drift (#1050). The tier keeps that model as its own through the move and the next one (`tier_own` in the stash), whatever it would follow on the new main model: fable, which follows no model that is no Fable, and haiku, which follows a Claude model on its Haiku, read as following and lost their model on the next move (#2257).
- A Claude Code tier or its subagents given a model of their own keep it when the main model moves onto that model and on again (`ownKey` in `claudeIn`), whether the main model is picked in magpie or in Claude Code's `/model`. Same as model, picked for them, has them follow again.
- While this machine's Claude Desktop is on magpie, Claude Code's shared settings get `CLAUDE_CODE_DISABLE_EXPLORE_INHERIT_CAP="1"` (`writeExplore` in `claudeIn`, [`claude.go`](../../internal/agent/claude.go)). Claude Code 2.1.295's Explore inherit cap can otherwise replace Desktop's `mythos-magpie` compatibility alias with Opus. This disables that cap, not the model choices of all subagents. It applies with Desktop alone or beside a connected Claude Code, including model picks and Sync; WSL twins do not get it. A user's existing value, including an empty string, stays. The stash (`claude.explore_inherit_cap`) marks only magpie's value: taking Claude Code off magpie leaves it while Desktop is on, and taking Desktop off removes it only if the user has not changed it. Since the settings file is shared, other Claude Code sessions reading it see the flag while Desktop is connected too.
diff --git a/internal/agent/apprestart.go b/internal/agent/apprestart.go
index 663fcad0d..86e703b8d 100644
--- a/internal/agent/apprestart.go
+++ b/internal/agent/apprestart.go
@@ -216,7 +216,7 @@ func (a *Agent) AppBehind() *AppRun {
// appReads are the files the agent's desktop app reads magpie's part from
// at start, and that part. Claude Desktop: deploymentMode, the profile
-// applied and magpie's profile, with the models the gateway lists it.
+// applied and magpie's profile (or the user's for magpie's gateway), with the models the gateway lists it.
// WorkBuddy: magpie's models in its models.json (workbuddyPart).
func (a *Agent) appReads() (files []string, reads func() string) {
switch a.ID {
@@ -224,6 +224,9 @@ func (a *Agent) appReads() (files []string, reads func() string) {
ps := desktopSetsHere()
for _, p := range ps {
files = append(files, p.config, p.config3p, p.meta, p.prof)
+ if f := desktopAppliedGateway(p); f != "" {
+ files = append(files, f)
+ }
}
return files, func() string {
var b strings.Builder
@@ -235,6 +238,13 @@ func (a *Agent) appReads() (files []string, reads func() string) {
b.WriteString(m + "\n" + m3 + "\n" + id + "\n")
b.Write(prof)
b.WriteString("\n")
+ // a profile of the user's for magpie's gateway, applied
+ // instead of magpie's (desktopKeyApplied)
+ if f := desktopAppliedGateway(p); f != "" {
+ theirs, _ := edit.Read(f)
+ b.Write(theirs)
+ b.WriteString("\n")
+ }
}
shown, _ := provider.CatalogFor(a.ListsFor())
for _, e := range shown {
diff --git a/internal/agent/claudedesktop.go b/internal/agent/claudedesktop.go
index f5051f41c..79ac27a91 100644
--- a/internal/agent/claudedesktop.go
+++ b/internal/agent/claudedesktop.go
@@ -185,7 +185,8 @@ func claudeDesktop(home string) *Agent {
},
}}, append(desktopTierFields(ps), desktopSubagentField(ps), desktopSubagentEffortField(ps))...),
// a Desktop already on magpie gets the import policy too
- // (desktopSeedImport); nothing else is written here
+ // (desktopSeedImport), and a profile for magpie's gateway it
+ // applies a key the gateway takes (desktopKeyApplied)
Sync: func() error {
var errs []error
for _, p := range ps {
@@ -193,6 +194,9 @@ func claudeDesktop(home string) *Agent {
if err := desktopSeedImport(p); err != nil {
errs = append(errs, err)
}
+ if err := desktopKeyApplied(p); err != nil {
+ errs = append(errs, err)
+ }
// a Desktop on magpie from before the servers were
// carried gets them once
if stashLoad()[desktopMCPSeen+p.key] == "" {
@@ -237,11 +241,79 @@ func desktopCheck(p desktopPaths) string {
if m != "3p" || m3 != "3p" {
return "Claude Desktop's deploymentMode (claude_desktop_config.json) is no longer 3p, so it signs in with Anthropic rather than using magpie"
}
+ prof := p.prof
if id, _ := edit.GetJSON(p.meta, "appliedId"); id != desktopProfileID {
- return "Claude Desktop uses another gateway configuration now (appliedId in configLibrary/_meta.json), not magpie's"
+ // one for magpie's gateway the user applied is on magpie too
+ if prof = desktopAppliedGateway(p); prof == "" {
+ return "Claude Desktop uses another gateway configuration now (appliedId in configLibrary/_meta.json), not magpie's"
+ }
}
- return wiringOff("Claude Desktop", p.prof, func(k string) (string, bool) { return edit.GetJSON(p.prof, k) },
- "inferenceGatewayBaseUrl", gateway.URL(), "inferenceGatewayApiKey", agentKey("claude-desktop"))
+ want := agentKey("claude-desktop")
+ return wiringOff("Claude Desktop", prof, func(k string) (string, bool) {
+ v, ok := edit.GetJSON(prof, k)
+ if k == "inferenceGatewayApiKey" && prof != p.prof && desktopKeyTaken(v, want) {
+ return want, true // a key of magpie's other than Desktop's own
+ }
+ return v, ok
+ }, "inferenceGatewayBaseUrl", gateway.URL(), "inferenceGatewayApiKey", want)
+}
+
+// desktopKeyTaken: the gateway takes v where magpie gives Desktop want,
+// as keyHolds says, or v is a key of magpie's own (gateway.Token, or one
+// naming an agent) where want is one too: the gateway takes those alike.
+func desktopKeyTaken(v, want string) bool {
+ return keyHolds(v, want) || gatewayKey(want) && v != "" && gatewayKey(v)
+}
+
+// desktopAppliedGateway is the configLibrary profile Desktop applies when
+// it is not magpie's own but a gateway profile for magpie's gateway, ""
+// for none. Desktop's "Import configuration" makes one: it adds the file
+// it is given as a new entry under an id of its own (configLibrary's
+// createConfig, a random UUID) and writes the file's settings into it, so
+// magpie's profile exported on one computer and imported on another is
+// applied there under another id, with the connectors (managedMcpServers)
+// and settings the user made in it (aa86222 on Discord: 配置已变更 while
+// Desktop ran on magpie). It is the user's profile: magpie keeps it
+// applied and sets nothing in it but a key the gateway refuses.
+func desktopAppliedGateway(p desktopPaths) string {
+ id, _ := edit.GetJSON(p.meta, "appliedId")
+ if id == "" || id == desktopProfileID || filepath.Base(id) != id || strings.HasPrefix(id, ".") {
+ return ""
+ }
+ f := filepath.Join(p.library, id+".json")
+ b, err := os.ReadFile(f)
+ if err != nil || !desktopObjectBytes(b) {
+ return ""
+ }
+ var prof struct {
+ Provider string `json:"inferenceProvider"`
+ BaseURL string `json:"inferenceGatewayBaseUrl"`
+ }
+ if json.Unmarshal(b, &prof) != nil || prof.Provider != "gateway" || !desktopIsOurGateway(prof.BaseURL) {
+ return ""
+ }
+ return f
+}
+
+// desktopKeyApplied gives the profile desktopAppliedGateway finds the
+// key magpie gives Desktop, only where the one it holds is a key this
+// gateway won't take (another magpie's, from the computer it was exported
+// on): one it takes stays. Its address and every other setting, its
+// connectors among them, stay as they are.
+func desktopKeyApplied(p desktopPaths) error {
+ f := desktopAppliedGateway(p)
+ if f == "" {
+ return nil
+ }
+ want := agentKey("claude-desktop")
+ if v, _ := edit.GetJSON(f, "inferenceGatewayApiKey"); desktopKeyTaken(v, want) {
+ return nil
+ }
+ kvs := []edit.KV{{Path: "inferenceGatewayApiKey", Value: want}}
+ if v, _ := edit.GetJSON(f, "inferenceGatewayAuthScheme"); v == "" {
+ kvs = append(kvs, edit.KV{Path: "inferenceGatewayAuthScheme", Value: "bearer"})
+ }
+ return edit.SetJSON(f, kvs...)
}
// desktopTierFields pick the model each of Claude Code's tiers runs on in
@@ -509,6 +581,17 @@ func desktopOn(p desktopPaths) error {
return err
}
+ // a profile for magpie's gateway the user applied since magpie's
+ // (one imported from another computer) stays applied, its connectors
+ // with it; it gets a key the gateway takes. Switching on the first
+ // time, one applied starts magpie's profile instead (#2035, above).
+ theirs := wired && desktopAppliedGateway(p) != ""
+ if theirs {
+ if err := desktopKeyApplied(p); err != nil {
+ return err
+ }
+ }
+
// _meta.json: magpie's entry (where it was, if it was), applied
if !wired {
entry, _ := json.Marshal(map[string]string{"id": desktopProfileID, "name": "magpie"})
@@ -517,8 +600,10 @@ func desktopOn(p desktopPaths) error {
return err
}
}
- if err := edit.SetJSON(p.meta, edit.KV{Path: "appliedId", Value: desktopProfileID}); err != nil {
- return err
+ if !theirs {
+ if err := edit.SetJSON(p.meta, edit.KV{Path: "appliedId", Value: desktopProfileID}); err != nil {
+ return err
+ }
}
desktopCarryLocale(p)
diff --git a/internal/agent/claudedesktop_test.go b/internal/agent/claudedesktop_test.go
index 6a339891e..60e1bc625 100644
--- a/internal/agent/claudedesktop_test.go
+++ b/internal/agent/claudedesktop_test.go
@@ -954,3 +954,141 @@ func TestClaudeDesktopSyncCarriesTheMCPServersOnce(t *testing.T) {
t.Fatal("Sync brought back a server the user took out")
}
}
+
+// aa86222 on Discord: Desktop's configuration exported on one computer
+// (magpie's profile, with the connectors the user added) and imported on
+// another is a new configLibrary entry under an id Desktop makes, applied
+// beside magpie's own and CC Switch's. It points at this magpie's
+// gateway, so Desktop runs on magpie: the row reads connected, and
+// Reconnect keeps it applied and its connectors as they are.
+func TestClaudeDesktopImportedProfileIsOnMagpie(t *testing.T) {
+ const imported = "3f6c2a8e-9b1d-4e57-a0c4-7d2e5f81b9a6"
+ const ccswitch = "c1d2e3f4-a5b6-4c7d-8e9f-0a1b2c3d4e5f"
+ theirs := func(key string) string {
+ return `{
+ "inferenceProvider": "gateway",
+ "inferenceGatewayBaseUrl": "` + gateway.URL() + `",
+ "inferenceGatewayApiKey": "` + key + `",
+ "inferenceGatewayAuthScheme": "bearer",
+ "disableDeploymentModeChooser": true,
+ "coworkEgressAllowedHosts": ["*"],
+ "claudeAiImport": {"enabled": true, "bannerBehavior": "detect"},
+ "managedMcpServers": [
+ {"name": "linear", "url": "https://mcp.linear.app/mcp", "transport": "http"},
+ {"name": "notion", "url": "https://mcp.notion.com/mcp", "transport": "http"}
+ ]
+}`
+ }
+ setup := func(t *testing.T, key string) (*Agent, desktopPaths, string) {
+ home, p := desktopSandbox(t)
+ os.MkdirAll(p.dir, 0o755)
+ a := claudeDesktop(home)
+ if err := a.Field("provider").Set("magpie"); err != nil {
+ t.Fatal(err)
+ }
+ // what Desktop wrote: CC Switch's profile and the imported one,
+ // the imported one applied
+ desktopWrite(t, filepath.Join(p.library, ccswitch+".json"),
+ `{"inferenceProvider":"gateway","inferenceGatewayBaseUrl":"http://127.0.0.1:15721/claude-desktop","inferenceGatewayApiKey":"cc-switch"}`)
+ f := filepath.Join(p.library, imported+".json")
+ desktopWrite(t, f, theirs(key))
+ desktopWrite(t, p.meta, `{"appliedId":"`+imported+`","entries":[{"id":"`+ccswitch+`","name":"CC Switch"},`+
+ `{"id":"`+desktopProfileID+`","name":"magpie"},{"id":"`+imported+`","name":"magpie"}]}`)
+ return a, p, f
+ }
+ applied := func(t *testing.T, p desktopPaths) string {
+ id, _ := desktopJSON(t, p.meta)["appliedId"].(string)
+ return id
+ }
+
+ t.Run("this magpie's key", func(t *testing.T) {
+ a, p, f := setup(t, agentKey("claude-desktop"))
+ if c := a.Check(); c != "" {
+ t.Errorf("check: %s", c)
+ }
+ if err := a.Sync(); err != nil {
+ t.Fatal(err)
+ }
+ // Reconnect: nothing of theirs changes, theirs stays applied
+ if err := a.Field("provider").Set("magpie"); err != nil {
+ t.Fatal(err)
+ }
+ if b, _ := os.ReadFile(f); string(b) != theirs(agentKey("claude-desktop")) {
+ t.Errorf("the imported profile was changed:\n%s", b)
+ }
+ if id := applied(t, p); id != imported {
+ t.Errorf("applied after reconnect: %q", id)
+ }
+ if c := a.Check(); c != "" {
+ t.Errorf("check after reconnect: %s", c)
+ }
+ // off: theirs stays applied and as it was
+ if err := a.Field("provider").Set(""); err != nil {
+ t.Fatal(err)
+ }
+ if id := applied(t, p); id != imported {
+ t.Errorf("applied after off: %q", id)
+ }
+ if b, _ := os.ReadFile(f); string(b) != theirs(agentKey("claude-desktop")) {
+ t.Errorf("the imported profile after off:\n%s", b)
+ }
+ })
+
+ t.Run("another magpie's key", func(t *testing.T) {
+ a, p, f := setup(t, "mk-from-the-other-computer")
+ if c := a.Check(); c == "" {
+ t.Error("check: a key the gateway refuses is fine")
+ }
+ // magpie's start gives it a key the gateway takes, and nothing else
+ if err := a.Sync(); err != nil {
+ t.Fatal(err)
+ }
+ var want map[string]any
+ json.Unmarshal([]byte(theirs(agentKey("claude-desktop"))), &want)
+ if got := desktopJSON(t, f); !reflect.DeepEqual(got, want) {
+ t.Errorf("imported profile after sync:\n%v\nwant\n%v", got, want)
+ }
+ if c := a.Check(); c != "" {
+ t.Errorf("check after sync: %s", c)
+ }
+ if id := applied(t, p); id != imported {
+ t.Errorf("applied after sync: %q", id)
+ }
+ })
+
+ t.Run("reconnect fixes the key", func(t *testing.T) {
+ a, p, f := setup(t, "mk-from-the-other-computer")
+ if err := a.Field("provider").Set("magpie"); err != nil {
+ t.Fatal(err)
+ }
+ var want map[string]any
+ json.Unmarshal([]byte(theirs(agentKey("claude-desktop"))), &want)
+ if got := desktopJSON(t, f); !reflect.DeepEqual(got, want) {
+ t.Errorf("imported profile after reconnect:\n%v\nwant\n%v", got, want)
+ }
+ if id := applied(t, p); id != imported || a.Check() != "" {
+ t.Errorf("applied %q, check %q", id, a.Check())
+ }
+ })
+
+ t.Run("CC Switch applied", func(t *testing.T) {
+ a, p, _ := setup(t, agentKey("claude-desktop"))
+ edit.SetJSON(p.meta, edit.KV{Path: "appliedId", Value: ccswitch})
+ if c := a.Check(); c == "" {
+ t.Error("check: CC Switch's profile reads as magpie's")
+ }
+ if err := a.Sync(); err != nil {
+ t.Fatal(err)
+ }
+ if got := desktopJSON(t, filepath.Join(p.library, ccswitch+".json")); got["inferenceGatewayApiKey"] != "cc-switch" {
+ t.Errorf("CC Switch's profile was written: %v", got)
+ }
+ // Reconnect applies magpie's own, as before
+ if err := a.Field("provider").Set("magpie"); err != nil {
+ t.Fatal(err)
+ }
+ if id := applied(t, p); id != desktopProfileID {
+ t.Errorf("applied after reconnect: %q", id)
+ }
+ })
+}