agent: Claude Code's own ANTHROPIC_MODEL comes back when magpie steps out

Wiring Claude Code in deletes env.ANTHROPIC_MODEL (writeTiers), because it
outranks a pick in /model. Unlike the tiers' models (#1050), the user's
own value was never stashed, so Disconnect or Claude Code's own model left
it gone for good. A Bedrock user like jw1dev keeps the Bedrock model id
there, so after the previous commit put the switch back, Claude Code would
come back on Bedrock without its model. ANTHROPIC_MODEL now joins
claudeOwnEnv: it is stashed raw beside the endpoint and token when first
routed from the user's own endpoint, put back by unroute, and forgotten
with the rest. A value an older magpie wrote, at its own key, is still not
kept (keyOurs), as before.

Run:
- gofmt -l: clean.
- go vet ./internal/agent: native, GOOS=linux -tags nogui, GOOS=windows:
  clean.
- go test -tags nogui ./internal/agent ./internal/gui: ok 4 times. One
  earlier run of the two together had internal/gui red, and only `tail -5`
  of it was kept, so the failing test's name was lost. It passed alone 3
  of 3 and with internal/agent 3 of 3. Nothing in internal/gui reads
  claudeOwnEnv.
- Targeted: ClaudeCloud, TestClaudeDisconnect, TestClaudePick, legacy,
  stand-in and T3 tests: ok.

Broken by hand: before the fix, TestClaudeDisconnect's new "own env
model" cases (with no settings model and beside one, [1m] kept; each by
Disconnect and by default) went red. The bedrock fixture of
TestClaudeCloudSwitchStepsAsideForMagpie now carries a Bedrock
ANTHROPIC_MODEL and compares the whole env after.

Not tried: WSL twins for real (the stash key goes through at.key as for
the tiers).
This commit is contained in:
yetone
2026-10-15 07:41:30 +08:00
parent 79fec406c6
commit bc470c6b63
4 changed files with 13 additions and 5 deletions
+1 -1
View File
@@ -64,7 +64,7 @@ something else shows up as drift.
- 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.
- Claude Code's tier and subagent models the user had set for their own endpoint (`claudeOwnEnv`) are stashed with that endpoint and token when magpie is wired in, and come back with them when magpie steps out, by Disconnect or by Claude Code's own model (#1050).
- Claude Code's tier and subagent models the user had set for their own endpoint (`claudeOwnEnv`) are stashed with that endpoint and token when magpie is wired in, and come back with them when magpie steps out, by Disconnect or by Claude Code's own model (#1050). So does their main model in the env (`ANTHROPIC_MODEL`, a Bedrock model id beside `CLAUDE_CODE_USE_BEDROCK` for one), which magpie takes out while wired in because it outranks a pick in `/model`.
- Claude Code's provider switches (`agentenv.ClaudeProviders`: `CLAUDE_CODE_USE_BEDROCK`, `_VERTEX`, `_FOUNDRY`, `_ANTHROPIC_AWS`, `_ANTHROPIC_GOOGLE_CLOUD`, `_MANTLE`, Claude Code 2.1.300) send it to that cloud's own endpoint and pass `ANTHROPIC_BASE_URL` over, so with one on it never reaches magpie (jw1dev on X: Bedrock refused magpie's model, and magpie showed nothing). Claude Code reads 1, true, yes and on as on (`claudeSwitchOn`). One that is on in the settings magpie writes is taken out on every route that writes the wiring (`dropClouds` in `writeTiers` and `Sync`), its raw value stashed (`claude.cloud.<var>`, under the distro's id for a WSL twin), and put back as written when magpie steps out (`putClouds` in `unroute`), unless the user set it again since. The cloud's own variables (`AWS_*`, `ANTHROPIC_VERTEX_PROJECT_ID`, …) and a switch that is off stay. Where magpie doesn't write it, Check says so, naming the variable, the cloud and where (`claudeCloud`): the managed settings, which win over the user's; `~/.claude.json`'s env, under the user's settings; and, for this machine's Claude Code, the login shell's environment (`proc.LoginEnv`, read but never set in magpie's), under every settings file. A project's `.claude/settings.json` isn't read: magpie doesn't know which folders Claude Code runs in. T3 Code's Check says the same of the user's `~/.claude` settings, which are put over its magpie instance's environment, and a switch put on that instance goes at its next write; that Check first says when the instance's `ANTHROPIC_BASE_URL` no longer reaches the gateway (`t3EnvOf`), as every agent's Check does, so a port move rewrites it too. The drift's detail is shown in the row's line and its pill through `tNotice` (`claude-cloud-drift.test.cjs`). Claude Desktop's Code tab runs with `CLAUDE_CODE_PROVIDER_MANAGED_BY_HOST`, under which Claude Code ignores a switch in settings files, so Desktop needs neither.
- 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 key put there by hand is reported by Check, not taken for magpie's. 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.AutoModeReviewed`): 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. 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.
+3 -1
View File
@@ -60,8 +60,10 @@ var claudeAboutEnv = func() []string {
// claudeOwnEnv are the models of claudeEnv a user may have set for their
// own endpoint, which magpie's take the place of while it is wired in and
// gives back when it steps out, with what the user's env says about each
// tier's model
// tier's model. ANTHROPIC_MODEL, their main model, too: magpie takes it
// out while wired in (writeTiers), and it was lost for good.
var claudeOwnEnv = append([]string{
"ANTHROPIC_MODEL",
"ANTHROPIC_DEFAULT_OPUS_MODEL", "ANTHROPIC_DEFAULT_SONNET_MODEL",
"ANTHROPIC_DEFAULT_HAIKU_MODEL", "ANTHROPIC_DEFAULT_FABLE_MODEL",
"ANTHROPIC_SMALL_FAST_MODEL", "CLAUDE_CODE_SUBAGENT_MODEL",
+2 -2
View File
@@ -28,13 +28,13 @@ func claudeCloudHome(t *testing.T, settings string) (home, path string, parse fu
// claudeClouded is each settings.json of a Claude Code on a cloud, as jw1dev
// on X had Bedrock: the switch on beside the cloud's own variables, and an
// endpoint of the user's own for magpie to give back.
// endpoint and Bedrock main model of the user's own for magpie to give back.
var claudeClouded = []struct {
name, settings, key, raw string
cloud []string
}{
{"bedrock", `{"model":"opus","env":{"ANTHROPIC_BASE_URL":"https://relay.example","ANTHROPIC_AUTH_TOKEN":"sk-relay",
"CLAUDE_CODE_USE_BEDROCK":"1","AWS_REGION":"us-west-2","AWS_PROFILE":"work","AWS_BEARER_TOKEN_BEDROCK":"bedrock-key"}}`,
"CLAUDE_CODE_USE_BEDROCK":"1","AWS_REGION":"us-west-2","AWS_PROFILE":"work","AWS_BEARER_TOKEN_BEDROCK":"bedrock-key","ANTHROPIC_MODEL":"us.anthropic.claude-sonnet-4-5-20250929-v1:0"}}`,
"CLAUDE_CODE_USE_BEDROCK", `"1"`, []string{"AWS_REGION", "AWS_PROFILE", "AWS_BEARER_TOKEN_BEDROCK"}},
// a JSON true, which Claude Code takes as on too, comes back a true
{"bedrock-bool", `{"env":{"CLAUDE_CODE_USE_BEDROCK":true,"AWS_REGION":"eu-central-1"}}`,
+7 -1
View File
@@ -89,12 +89,18 @@ func TestCodexDisconnect(t *testing.T) {
// come back, and every env key, tier and capability magpie wrote goes.
// The tier and subagent models the user set for their relay come back with
// it (#1050), whether magpie steps out by Disconnect or by Claude Code's
// own model.
// own model. So does the main model set for the relay in the env
// (ANTHROPIC_MODEL), which magpie takes out while wired in.
func TestClaudeDisconnect(t *testing.T) {
for name, before := range map[string]string{
"own relay": `{"theme":"dark","model":"opus","env":{"ANTHROPIC_BASE_URL":"https://relay.example","ANTHROPIC_AUTH_TOKEN":"sk-relay","DISABLE_TELEMETRY":"1"}}`,
"own tiers": `{"model":"sonnet","env":{"ANTHROPIC_BASE_URL":"https://relay.example","ANTHROPIC_AUTH_TOKEN":"sk-relay","ANTHROPIC_DEFAULT_OPUS_MODEL":"relay-opus","ANTHROPIC_DEFAULT_SONNET_MODEL":"relay-sonnet","ANTHROPIC_DEFAULT_HAIKU_MODEL":"relay-haiku","ANTHROPIC_SMALL_FAST_MODEL":"relay-haiku","CLAUDE_CODE_SUBAGENT_MODEL":"relay-sonnet"}}`,
"nothing set": `{"theme":"dark"}`,
// the main model set for the relay in the env (with no settings
// model, and beside one), which magpie takes out while wired in:
// it outranks a pick in /model
"own env model": `{"env":{"ANTHROPIC_BASE_URL":"https://relay.example","ANTHROPIC_AUTH_TOKEN":"sk-relay","ANTHROPIC_MODEL":"relay-main"}}`,
"own env model, both": `{"model":"opus","env":{"ANTHROPIC_BASE_URL":"https://relay.example","ANTHROPIC_AUTH_TOKEN":"sk-relay","ANTHROPIC_MODEL":"relay-main[1m]"}}`,
} {
for _, out := range []string{"disconnect", "default"} {
t.Run(name+", "+out, func(t *testing.T) {