diff --git a/docs/subsystems/agent-wiring.md b/docs/subsystems/agent-wiring.md index 7a537934e..a110ef2ad 100644 --- a/docs/subsystems/agent-wiring.md +++ b/docs/subsystems/agent-wiring.md @@ -112,6 +112,7 @@ something else shows up as drift. - Claude Code's sign-in (`login`) is magpie's key (`""`, the default) or claude.ai (`claudeai`). claude.ai writes `ANTHROPIC_AUTH_TOKEN` empty, which Claude Code reads as no key: it keeps its claude.ai login and sends that to magpie, which never passes it on (`claudeSignIn` in the gateway). Every later write keeps the token empty (`wiredKey`), and a caller key of the gateway's own put there by hand is taken for magpie's, by Check and a model pick alike, kept as the user left it (`keyHolds`, #2545); a key the gateway would refuse is reported by Check and wired over, and so is the sharing key magpie itself made (`access.Default`), once sharing is off — magpie's own, however well the gateway still takes it. It's offered only where the gateway takes any key (`gwKey() == gateway.Token`: loopback, a WSL distro mirrored), not from a distro under NAT while magpie is shared on the network, where only the sharing key gets in. The choice is stashed, so it survives Disconnect and a port move. An empty token counts as magpie's only beside the address magpie last wrote (`claude.wired_at` in the stash, which Rewire carries to the new port, `keyOurs`): an endpoint of the user's own with an empty token stays theirs when the model goes back to Claude Code's own. - Claude Code's auto mode review (`CLAUDE_CODE_AUTO_MODE_SERVER`, `claudeReviewEnv` in [`claude.go`](../../internal/agent/claude.go)) is written `"0"` while Claude Code is routed through magpie and none of the models it can reach there is one Anthropic reviews (`claudeReviewed`, by `gateway.AutoModeReviewable`: a model `AutoModeReviewed` that is asked on the Messages API; a Claude model a provider serves only on Chat or Responses, or one set to be asked on another API, is translated, carries no `safeguards` and counts for nothing, nexnull on Discord, `TestClaudeReviewedOnlyWhereMessagesReachClaude`): those it is shown (and Claude Desktop's while Desktop is on magpie), a shown group's members, those providers' fallbacks, and the models it is set to, each judged on the provider (or group, or fallback) it resolves to, as the gateway judges its requests, so a Claude model on Copilot or Kiro alone writes `"0"` and a name magpie doesn't know is judged by the name (`TestClaudeReviewedJudgesTheSetModelWhereItGoes`). Off, it classifies locally from the start, as it does anyway behind a gateway that doesn't answer, without the "isn't eligible because your requests go through 127.0.0.1:3425" notice (1EFLOPS on X). Any reviewed model leaves it unset, since `/model` can take the session there; a model hidden from Claude Code doesn't count. It is rewritten on every model set and `Sync`, so adding a Claude provider turns the review back on. magpie's value is kept apart in the stash (`claude.auto_mode_server`), a user's own is left alone, and it goes when Claude Code is taken off magpie (`desktopOnly`, `unroute`). T3 Code's magpie instance, which runs Claude Code on the gateway through its own environment, carries the same `"0"` there by the models it lists (`t3InstanceJSON` in [`t3code.go`](../../internal/agent/t3code.go)); a value in the user's `~/.claude` settings comes after it. - Claude Code's tool search (`ENABLE_TOOL_SEARCH`, `claudeSearchEnv` in [`claude.go`](../../internal/agent/claude.go)) is written `"true"` while Claude Code is routed through magpie. On, MCP tools are named in a reminder and loaded by `ToolSearch` when the model needs them, not sent whole on every request. Claude Code before 2.1.304 keeps it off behind an `ANTHROPIC_BASE_URL` other than Anthropic's, so the MCP router's tools all went on each request (cybercaius on Discord: 245 tools, 124K tokens of a 341K context; 2.1.290 through a sandbox gateway sent 242,346 bytes of tools, 27,443 with it on); 2.1.304 and later turn it on there themselves, and `"true"` also keeps it on in a conversation begun without it. The gateway carries deferred tools to every upstream (`Tool.Deferred`, `withoutDeferredTools`). magpie's value is kept apart in the stash (`claude.tool_search`); a value the user set, an empty one too, is theirs; it goes when Claude Code is taken off magpie (`desktopOnly`, `unroute`; `TestClaudeToolSearchOn`). T3 Code's magpie instance carries `"true"` in its environment unless the user gave it a value there (`TestT3CodeToolSearchOn`). +- Claude Desktop's Code tab gets tool search only with Desktop's own `toolSearchEnabled` policy on (3p only, off by default). Off, Desktop 2.7032 (`Ztr` "off") starts Claude Code with `CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1`, and Claude Code (2.1.312, 2.1.316: `oae` returns "standard") then keeps tool search off whatever `ENABLE_TOOL_SEARCH` says, so every MCP tool went whole on each request (bigcat1800 on Discord: 13 MCP servers, ~162K tokens; a sandbox CLI with 60 MCP tools sent 83 tools, 205KB). On, with the gateway provider and a bundled Claude Code of 2.1.247 or later, Desktop hands the CLI `ENABLE_TOOL_SEARCH=force` as a managed setting and the same CLI sent 13 tools, MCP ones behind `ToolSearch`, 56KB, asking only `tool-search-tool-2025-10-19` of the betas. magpie's profile gets `toolSearchEnabled: true` once, on a switch on and from Sync, as `claudeAiImport` is (`desktopSeedToolSearch`, `desktopSeedOnce`, mark `claude-desktop.tool-search`): a value the user set, false too, or took away since, stays theirs (`TestClaudeDesktopTurnsOnToolSearch`). A profile the user applied for magpie's gateway (`desktopAppliedGateway`) is not written. - T3 Code runs a thread on the harness of the provider instance its model is picked under, so magpie writes two instances (karissasel_ on X: only Claude Code could be picked). `magpie` runs Claude Code, as above. `magpie-codex` ("magpie · Codex", `t3CodexJSON` in [`t3code.go`](../../internal/agent/t3code.go)) runs Codex: its `config.launchArgs`, which T3 hands to `codex app-server` and the `-c` pairs of to `codex exec`, are Codex's own overrides, making `magpie` its `model_provider` (the same table `codex.go` writes) and `magpie-codex-models.json`, written beside settings.json from `codexcat`, its `model_catalog_json`. Each value is quoted for T3's `tokenizeCliArgs` (`t3Arg`; `TestT3CodeCodexInstance` reads them back with a port of it). `~/.codex/config.toml` is not written, and a `binaryPath` or `homePath` the user gave T3's own `codex` instance is carried over. The name, on/off and unknown keys are the user's; the launch args and catalog are magpie's, and Check says when the launch args no longer reach the gateway (`t3CodexOff`). An older Claude-only wiring gets the Codex instance on its next Sync once; one the user removed stays removed (`t3code.codex` in the stash) until magpie is turned on for T3 again (`TestT3CodeCodexInstanceAddedOnce`). - Claude Code takes one window (`CLAUDE_CODE_MAX_CONTEXT_TOKENS`, `claudeContextEnv` in [`claude.go`](../../internal/agent/claude.go)) for every model it doesn't know. Claude Code alone on magpie is told its main model's (`claudeWindow`), and a model picked in its `/model` (magpie's `modelPicker`) runs at that one value. Beside a main that is one of Claude's own models (or a group of them), with no tier that isn't, nothing is told, so a model picked there ran at Claude Code's own 200K: a routing group of a 500K GPT and a 1M Claude did (young02036 on Discord). Then (`pickerApart`) the `/model` rows (`pickerRefs`) count as tiers do in the layout below. While Claude Desktop is on magpie too, its Code tab runs Claude Code on the same `~/.claude/settings.json` with every model Desktop lists, so magpie tells it the largest window of those and of Claude Code's own models (its main, tiers and `/model` rows), and holds each model below it to its own with `modelSettings..autoCompactWindow` (`claudePlan` in [`claude_budget.go`](../../internal/agent/claude_budget.go), #2025: the least held a 1M model to 131K). A model handed by a `[1m]` id (Desktop's under `DesktopLongest`, or Claude Code's own) counts by its 1M too, so it still runs at 1M when Claude Code stops taking the id for 1M (the 1M beta turned away once, `CLAUDE_CODE_DISABLE_1M_CONTEXT`); left out, a 1M model beside a 272K GPT was told 272K (dumplings on Discord). Claude Code 2.1.296 matches that key by the id it was handed, lowercased and without `[1m]`; it comes before the top-level `autoCompactWindow`, which serves a model without one. `CLAUDE_CODE_AUTO_COMPACT_WINDOW` would take precedence over both, so in this mode magpie's working window goes in the top-level `autoCompactWindow` instead, and an entry is written only where a model's window or its own threshold differs from what it would run at without one. magpie's entries and top-level value are stashed (`claude.model_budgets`, `claude.compact_default`) and go when Desktop leaves magpie; the user's own entries, top-level value and variables stay, a user's entry past its model's window (or `auto`) holds the largest to that window, and with a `CLAUDE_CODE_AUTO_COMPACT_WINDOW` of the user's the least window is told as before. A Code tab session whose environment host is WSL runs on the distro's Claude Code, which reads `${CLAUDE_CONFIG_DIR:-$HOME/.claude}` there (Desktop 4.29.0's remote probe) and not Windows' file, so each `claude@wsl:` is told the same while Windows' Desktop is on magpie (`desktopOn`), on magpie itself or not; it read 200K for a 500K group before (young02036 on Discord). - Claude Code is told what each of magpie's models can do in `CLAUDE_CODE_MODEL_CAPABILITIES` (`claudeCapsEnv`, `claudeCapabilities` in [`claude.go`](../../internal/agent/claude.go)): `=effort[,xhigh_effort][,max_effort]` for those it is shown, and, while Claude Desktop is on magpie, for Desktop's `mythos-magpie-` ids its Code tab hands it. A Claude model Claude Code knows gets no more than Claude Code gives it (Opus and Sonnet 4.6: no xhigh), and one with no known levels isn't named. A model that reaches max without xhigh (GLM-5.3 at low/high/max, DeepSeek V4 at high/max) is said to have xhigh too: the gateway runs xhigh at max for it (`fitEffort`), and Claude Code 2.1.299 turns Ultracode down without `xhigh_effort` ("Ultracode isn't available on …"), which in Desktop is the Ultracode level and dynamic workflows (leesunrise2364 on Discord; `TestClaudeModelCapabilities`). A value of the user's is left alone. diff --git a/internal/agent/claudedesktop.go b/internal/agent/claudedesktop.go index bd5d624be..fa041d77c 100644 --- a/internal/agent/claudedesktop.go +++ b/internal/agent/claudedesktop.go @@ -15,7 +15,8 @@ package agent // "inferenceGatewayBaseUrl":"http://127.0.0.1:3425", // "inferenceGatewayApiKey":"magpie-claude-desktop", // "inferenceGatewayAuthScheme":"bearer", -// "disableDeploymentModeChooser":true,"coworkEgressAllowedHosts":["*"]} +// "disableDeploymentModeChooser":true,"coworkEgressAllowedHosts":["*"], +// "toolSearchEnabled":true} // Claude-3p/configLibrary/_meta.json {"appliedId":"", // "entries":[…,{"id":"","name":"magpie"}]} // @@ -194,9 +195,10 @@ func claudeDesktop(home string) *Agent { Note: "Desktop's third-party gateway: Code and Cowork on magpie's models, no Anthropic sign-in (restart Desktop)"}} }, }}, append(desktopTierFields(home, ps), desktopSubagentField(ps), desktopSubagentEffortField(ps))...), - // a Desktop already on magpie gets the import policy too - // (desktopSeedImport), and a profile for magpie's gateway it - // applies a key the gateway takes (desktopKeyApplied) + // a Desktop already on magpie gets the import and tool search + // policies too (desktopSeedImport, desktopSeedToolSearch), 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 { @@ -204,6 +206,9 @@ func claudeDesktop(home string) *Agent { if err := desktopSeedImport(p); err != nil { errs = append(errs, err) } + if err := desktopSeedToolSearch(p); err != nil { + errs = append(errs, err) + } if err := desktopKeyApplied(p); err != nil { errs = append(errs, err) } @@ -542,11 +547,12 @@ const ( desktopProfile = "claude-desktop.profile" desktopLocale = "claude-desktop.locale" desktopImport = "claude-desktop.import" + desktopSearch = "claude-desktop.tool-search" desktopMCP = "claude-desktop.mcp" desktopMCPSeen = "claude-desktop.mcp-seen" ) -var desktopStashKeys = []string{desktopStashed, desktopMode, desktopMode3p, desktopApplied, desktopMade, desktopProfile, desktopLocale, desktopImport, desktopMCP, desktopMCPSeen, desktopModelsMark} +var desktopStashKeys = []string{desktopStashed, desktopMode, desktopMode3p, desktopApplied, desktopMade, desktopProfile, desktopLocale, desktopImport, desktopSearch, desktopMCP, desktopMCPSeen, desktopModelsMark} // desktopAdoptStash moves the stash a packaged Desktop's folders got // before they had keys of their own (when magpie wrote only one set) onto @@ -642,6 +648,9 @@ func desktopOn(p desktopPaths) error { if err := desktopSeedImport(p); err != nil { return err } + if err := desktopSeedToolSearch(p); err != nil { + return err + } // a profile for magpie's gateway the user applied since magpie's // (one imported from another computer) stays applied, its connectors @@ -709,18 +718,46 @@ var desktopImportOn = map[string]any{"enabled": true, "bannerBehavior": "detect" // The mark is read, never unstashed: it outlives the check and a switch // off, or the next Sync would write the policy back. func desktopSeedImport(p desktopPaths) error { - if stashShared()[desktopImport+p.key] != "" { + return desktopSeedOnce(p, desktopImport, "claudeAiImport", desktopImportOn) +} + +// desktopSeedToolSearch turns on Desktop's own tool search policy +// (toolSearchEnabled, 3p only, off by default) in magpie's profile. Off, +// Desktop starts Claude Code with CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1 +// (Desktop 2.7032's Ztr "off" in the session env), and Claude Code 2.1.312 +// and 2.1.316 then keep tool search off whatever ENABLE_TOOL_SEARCH says +// (its oae: "standard" while experimental betas are off, unless an admin +// tier forces it), so every MCP tool's schema went whole on each request +// (bigcat1800 on Discord: Desktop 4.4212, 13 MCP servers, ~162K tokens of +// tools; a sandbox CLI with 60 MCP tools sent 83 tools, 205KB). On, with +// the gateway provider and a bundled Claude Code of 2.1.247 or later, +// Desktop gives the CLI ENABLE_TOOL_SEARCH=force as a managed setting +// ("host"): the same CLI sent 13 tools, MCP ones behind ToolSearch, 56KB, +// asking only tool-search-tool-2025-10-19 of the betas. The gateway +// carries deferred tools to every upstream (Tool.Deferred, +// withoutDeferredTools). Seeded once, as claudeAiImport is: a value the +// user set in Desktop's window, false too, or one they took away since, +// stays theirs. +func desktopSeedToolSearch(p desktopPaths) error { + return desktopSeedOnce(p, desktopSearch, "toolSearchEnabled", true) +} + +// desktopSeedOnce writes key in magpie's profile once, where the profile +// has none, and marks it in the stash under mark: once marked it is never +// written again, so the user's own value, or their taking it away, holds. +func desktopSeedOnce(p desktopPaths, mark, key string, value any) error { + if stashShared()[mark+p.key] != "" { return nil } if desktopObject(p.prof) != nil { return nil } - if _, ok := edit.GetJSON(p.prof, "claudeAiImport"); !ok { - if err := edit.SetJSON(p.prof, edit.KV{Path: "claudeAiImport", Value: desktopImportOn}); err != nil { + if _, ok := edit.GetJSON(p.prof, key); !ok { + if err := edit.SetJSON(p.prof, edit.KV{Path: key, Value: value}); err != nil { return err } } - stash(map[string]string{desktopImport + p.key: "1"}) + stash(map[string]string{mark + p.key: "1"}) return nil } diff --git a/internal/agent/claudedesktop_test.go b/internal/agent/claudedesktop_test.go index e21de010d..1d2e999aa 100644 --- a/internal/agent/claudedesktop_test.go +++ b/internal/agent/claudedesktop_test.go @@ -144,7 +144,8 @@ func TestClaudeDesktopFresh(t *testing.T) { "inferenceProvider": "gateway", "inferenceGatewayBaseUrl": gateway.URL(), "inferenceGatewayApiKey": "magpie-claude-desktop", "inferenceGatewayAuthScheme": "bearer", "disableDeploymentModeChooser": true, "coworkEgressAllowedHosts": []any{"*"}, - "claudeAiImport": map[string]any{"enabled": true, "bannerBehavior": "detect"}, + "claudeAiImport": map[string]any{"enabled": true, "bannerBehavior": "detect"}, + "toolSearchEnabled": true, } if prof := desktopJSON(t, p.prof); !reflect.DeepEqual(prof, want) { t.Errorf("profile: %v", prof) @@ -837,6 +838,77 @@ func TestClaudeDesktopLanguageOddValues(t *testing.T) { } } +// bigcat1800 on Discord: Desktop 4.4212 in 3p mode on magpie, 13 MCP +// servers, and every tool's schema went whole on each request (~162K +// tokens). Desktop starts Claude Code with experimental betas off unless +// its toolSearchEnabled policy is on, and Claude Code then keeps tool +// search off whatever ENABLE_TOOL_SEARCH says. magpie's profile gets the +// policy once, on a switch on and, for a Desktop already on magpie, from +// Sync; a value the user set, false too, or took away since, stays theirs. +func TestClaudeDesktopTurnsOnToolSearch(t *testing.T) { + home, p := desktopSandbox(t) + a := claudeDesktop(home) + if err := a.Field("provider").Set("magpie"); err != nil { + t.Fatal(err) + } + if got := desktopJSON(t, p.prof)["toolSearchEnabled"]; got != true { + t.Fatalf("switch on: toolSearchEnabled %v", got) + } + + // on magpie before this change: no toolSearchEnabled, no mark + prof := desktopJSON(t, p.prof) + delete(prof, "toolSearchEnabled") + b, _ := json.Marshal(prof) + desktopWrite(t, p.prof, string(b)) + forget(desktopSearch + p.key) + if err := a.Sync(); err != nil { + t.Fatal(err) + } + if got := desktopJSON(t, p.prof)["toolSearchEnabled"]; got != true { + t.Fatalf("Sync on magpie: toolSearchEnabled %v", got) + } + + // the user takes it away in Desktop's window: not written back + desktopWrite(t, p.prof, string(b)) + if err := a.Sync(); err != nil { + t.Fatal(err) + } + if err := a.Field("provider").Set("magpie"); err != nil { + t.Fatal(err) + } + if _, has := desktopJSON(t, p.prof)["toolSearchEnabled"]; has { + t.Fatal("seeded again over the user's choice") + } + + // the user's own false, seen first: kept + if err := edit.SetJSON(p.prof, edit.KV{Path: "toolSearchEnabled", Value: false}); err != nil { + t.Fatal(err) + } + forget(desktopSearch + p.key) + if err := a.Sync(); err != nil { + t.Fatal(err) + } + if got := desktopJSON(t, p.prof)["toolSearchEnabled"]; got != false { + t.Fatalf("the user's toolSearchEnabled: %v", got) + } + if a.Check() != "" { + t.Fatalf("check %q", a.Check()) + } + + // off magpie: the profile goes, and Sync writes nothing + if err := a.Field("provider").Set(""); err != nil { + t.Fatal(err) + } + forget(desktopSearch + p.key) + before := desktopTree(t, p) + if err := a.Sync(); err != nil { + t.Fatal(err) + } + if after := desktopTree(t, p); !reflect.DeepEqual(after, before) { + t.Fatalf("Sync off magpie wrote:\n%v", after) + } +} + // lambdua on X: Desktop on magpie runs in Claude-3p, a userData of its own, // so the Code and Cowork sessions it had signed in with Anthropic were out // of sight. Desktop imports them itself (Settings → Import) once its