diff --git a/docs/subsystems/agent-wiring.md b/docs/subsystems/agent-wiring.md index 6309fbcd4..13aa2d990 100644 --- a/docs/subsystems/agent-wiring.md +++ b/docs/subsystems/agent-wiring.md @@ -104,7 +104,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. - 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`). 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, 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. +- 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. - 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. - MiniMax Code takes a model ref with one slash in the whole (a SubAgent's `model` must match `^[^/\s]+\/[^/\s]+$`, #1387), and magpie's refs there are `custom_provider:magpie/`. So a model under `custom_provider.magpie` is written with `~` for each slash of its catalog id (`bb-codex~gpt-6.1-sol`, `provider.FlatID`), only when that spelling stands for one served id (`provider.Flattened`); the gateway takes it back. A config an older magpie wrote moves over on the next sync: the user's keys on a model go to its new key, `defaultModel`, `defaultLightModel` and `memory.captureModel`/`consolidationModel` follow (`mcodeRefs`), and the slashed key stays, switched off (`enabled: false`), so a session begun on it still resumes. magpie never edits the user's own `agents//agent.md`. No other agent was found with this limit ([`minimax.go`](../../internal/agent/minimax.go)). - Claude Code's `/model` list (`modelPicker` in its settings.json) is magpie's while it is marked in the stash (`claude.model_picker`, `claude@wsl:.model_picker` for a WSL distro) or while every row's description ends "via magpie", as every row magpie writes does (`magpiesPicker` in [`claude.go`](../../internal/agent/claude.go)). magpie keeps its own list up to date on every model set and `Sync`, and removes it when Claude Code is taken off magpie; a list the user wrote is left alone. The fingerprint covers a list whose mark an older magpie's stash lost, which otherwise stayed without every model added since (#2046). diff --git a/internal/agent/claude.go b/internal/agent/claude.go index 9de1cc7c7..568309606 100644 --- a/internal/agent/claude.go +++ b/internal/agent/claude.go @@ -1009,6 +1009,23 @@ func claudeIn(at place) *Agent { stash(map[string]string{pickerKey: "1"}) return edit.SetJSON(path, edit.KV{Path: "modelPicker", Value: map[string]any{"options": rows}}) } + // pickerRefs are the models writePicker lists in /model, as it lists + // them ("[1m]" on a 1M one), none while the picker is the user's. + // Claude Code runs a model picked there on the window it was told, and + // beside a Claude main it was told none: a routing group of a 500K GPT + // and a 1M Claude picked in /model ran at Claude Code's own 200K + // (young02036 on Discord) + pickerRefs := func() []string { + if _, has := edit.GetJSON(path, "modelPicker"); has && !pickerOurs() { + return nil + } + mark1M := claude1M() + var refs []string + for _, o := range viaMagpie("claude", "") { + refs = append(refs, mark1M(o.Value)) + } + return refs + } // writeCaps says what magpie's models can do, the ones in models first; // Claude Desktop's ids too while it runs on magpie, its Code tab being // Claude Code on this settings.json @@ -1100,8 +1117,8 @@ func claudeIn(at place) *Agent { // desktopPlan is what writeWindow writes while Claude Desktop is on // magpie (claudePlan, #2025): the models its Code tab runs Claude Code // on, and while Claude Code itself is on magpie (main set) its main - // model and tiers', each with its own window under the - // largest. A model Claude Code knows as Claude's has its own window. + // model's, tiers' and those /model offers (pickerRefs), each with its + // own window under the largest. A model Claude Code knows as Claude's has its own window. desktopPlan := func(main string, tiers map[string]string) (top, def int, entries map[string]int) { s := settings.Load() // the user's own top-level autoCompactWindow @@ -1151,6 +1168,8 @@ func claudeIn(at place) *Agent { for _, t := range claudeTiers { refs = append(refs, tiers[t]) } + // and the models /model offers, each of which it may run on + refs = append(refs, pickerRefs()...) for _, v := range refs { m, _ := tierAt(v) bare := strings.TrimSuffix(m, "[1m]") @@ -1185,6 +1204,36 @@ func claudeIn(at place) *Agent { top, entries = claudePlan(models, base, theirs) return top, def, entries } + // pickerApart says the main is one of Claude's own models (or a group + // of them), which Anthropic runs to its own window, magpie tells + // Claude Code no window for the rest (claudeWindow 0), and /model + // offers a model of magpie's whose window is known: picked there, it + // would run at Claude Code's own 200K, so each gets its own through + // desktopPlan. A model Claude Code takes for 1M by its "[1m]", or + // knows as Claude's, has its own already. Any other main is as + // before: one whose window is told serves a /model pick too, and one + // of no known window keeps the last. + pickerApart := func(main string, tiers map[string]string) bool { + m, _ := tierAt(main) + bare := strings.TrimSuffix(m, "[1m]") + if main == "" || !claudeModel(bare) && !provider.ClaudeGroup(bare) || claudeWindow(main, tiers) > 0 { + return false + } + window := map[string]int{} + for _, m := range magpieModels("claude") { + window[m.ID] = m.Context + } + for _, v := range pickerRefs() { + m, _ := tierAt(v) + if strings.HasSuffix(m, "[1m]") || claudeModel(m) || provider.ClaudeGroup(m) { + continue + } + if window[m] > 0 { + return true + } + } + return false + } // writeWindow tells Claude Code the window of the models it runs on // (claudeWindow). While Claude Desktop runs on magpie too, it is the // largest of theirs and each model below it gets its own @@ -1203,7 +1252,7 @@ func claudeIn(at place) *Agent { forget(windowKey) return dropBudgets() } - if (desktopOn() || claudeLongMain(main) && claudeWindow(main, tiers) > 0) && (env(claudeCompactEnv) == "" || compactOurs()) { + if (desktopOn() || claudeLongMain(main) && claudeWindow(main, tiers) > 0 || pickerApart(main, tiers)) && (env(claudeCompactEnv) == "" || compactOurs()) { top, def, entries := desktopPlan(main, tiers) if top <= 0 { return dropWindow() diff --git a/internal/agent/window_typed_test.go b/internal/agent/window_typed_test.go index 74e4fa18a..63069cf71 100644 --- a/internal/agent/window_typed_test.go +++ b/internal/agent/window_typed_test.go @@ -9,6 +9,7 @@ import ( "github.com/yetone/magpie/internal/edit" "github.com/yetone/magpie/internal/gateway" "github.com/yetone/magpie/internal/provider" + "github.com/yetone/magpie/internal/settings" ) // A window the user typed is the one Codex and Claude Code run to (#1809, @@ -237,3 +238,64 @@ func TestLargestMemberTypedWindow(t *testing.T) { } } } + +// young02036 on Discord: a routing group of a 500K GPT and a 1M Claude, +// following its smallest member, is told 500K by every agent's list, but +// Claude Code beside a Claude main model was told no window at all, so +// the group picked in its /model ran at Claude Code's own 200K (2.1.314, +// /context "200k"), and so did the GPT. With nothing told for the main, +// every model /model offers counts as a tier's does: Claude Code is told +// the largest window, and each model below it runs to its own, at the +// working window or, under Full window, its whole window. +func TestPickerModelsTheirOwnWindow(t *testing.T) { + _, path := desktopWindowSandbox(t) + home := filepath.Dir(filepath.Dir(path)) + must := func(err error) { + t.Helper() + if err != nil { + t.Fatal(err) + } + } + must(provider.Save(provider.Provider{ID: "w", Name: "W", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"gpt-6.1-sol"}})) + must(catalog.SaveLive("w", "http://127.0.0.1:1/v1", []catalog.Model{{ID: "gpt-6.1-sol", Context: 500000}})) + must(provider.Save(provider.Provider{ID: "a", Name: "A", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"claude-opus-5-5"}})) + must(catalog.SaveLive("a", "http://127.0.0.1:1/v1", []catalog.Model{{ID: "claude-opus-5-5", Context: 1000000}})) + must(provider.SaveGroup(provider.Group{ID: "mix", Name: "Mix", Members: []string{"w/gpt-6.1-sol", "a/claude-opus-5-5"}, Context: provider.ContextSmallest})) + + a := claude(home) + env := func(k string) string { v, _ := edit.GetJSON(path, "env."+k); return v } + budget := func(id string) string { v, _ := edit.GetJSON(path, budgetPath(id)); return v } + check := func(name string, want map[string]string) { + t.Helper() + // glm is 1M, marked [1m] in /model: the window told + if w := env(claudeContextEnv); w != "1000000" { + t.Errorf("%s: Claude Code is told %q; want the largest, 1000000", name, w) + } + if c := env(claudeCompactEnv); c != "" { + t.Errorf("%s: %s is %q, which would hold every model to it", name, claudeCompactEnv, c) + } + for id, w := range want { + if v := budget(id); v != w { + t.Errorf("%s: %s in /model compacts at %q; want %q", name, id, v, w) + } + } + } + must(a.Field("model").Set("a/claude-opus-5-5")) + check("a Claude main", map[string]string{"group/mix": "272000", "w/gpt-6.1-sol": "272000", "v/grok": "256000", "v/gpt": "272000"}) + // beside a main whose window is told, a /model pick runs at that one + // value, as before + must(a.Field("model").Set("v/grok")) + if w := env(claudeContextEnv); w != "256000" { + t.Errorf("a 256K main: Claude Code is told %q; want its own 256000", w) + } + if v := budget("group/mix"); v != "" { + t.Errorf("a 256K main: group/mix has its own entry %q; want none", v) + } + + s := settings.Load() + s.FullContext = true + must(settings.Save(s)) + SyncCatalog() + must(a.Field("model").Set("a/claude-opus-5-5")) + check("Full window", map[string]string{"group/mix": "500000", "w/gpt-6.1-sol": "500000", "v/grok": "256000", "v/gpt": "272000"}) +}