Files
magpie/internal/agent/claude_1m_test.go
T
yetone aa2ea5c906 agent: Claude's main model is written as a tier alias, so Claude Desktop on a subscription no longer sends magpie's prefixed id (iryan_sun on X)
Claude Desktop's Code tab sets CLAUDE_CODE_PROVIDER_MANAGED_BY_HOST=1. Claude
Code then ignores the env block of settings.json (base URL, token, tier model
vars) but still honours the top-level `model`. magpie wrote `model` as the
provider-prefixed id (anth/claude-opus-4-7), so the official API rejected it
and WebFetch failed.

writeTiers now writes `model` as the tier alias (opus/sonnet/haiku/fable, with
[1m]) whose ANTHROPIC_DEFAULT_<TIER>_MODEL already maps to the main model, on
this machine's ~/.claude only. A distro file, the claude-magpie launch file and
a main with no tier keep the id. The model field reads the alias back through
the tier env, also while the gateway is unreachable (drift). Unwire drops
`model` when magpie wrote the alias.

What happens to old values: a settings.json an older magpie wrote with the
prefixed id (model "up/big-x") is rewritten to the alias at the next catalog
sync, for every user, with no step on their side; the tier env already maps it
to the same model. A prefixed id picked from the modelPicker rows is brought to
the alias the same way. A model the user set themselves (claude-haiku-9,
own/xyz, opusplan) is never rewritten.

Ran:
- Real Claude Code CLI (~/.local/bin/claude) in a sandbox HOME and
  CLAUDE_CONFIG_DIR, wired by a magpie built from this tree (gateway :38760)
  to a scripted upstream that logs each request's model: main -> big-x,
  `--model haiku` -> fast-x, a subagent (Agent tool) -> sub-x, WebFetch's
  extraction of https://example.com -> fast-x (haiku's). Old prefixed file
  migrated to "sonnet" by a catalog sync; the three user models above stayed.
- TestClaudeMainModelIsAnAlias: red on the old code, red again with mainWord
  broken by hand, green with the fix; it also covers the user's own models.
- 11 existing tests read the raw `model` as an id; they read it through
  writtenModel now. TestWSLClaudePick compares the distro's file with the
  native one after normalising `model` (the distro keeps the id by design).
- go test ./internal/agent ok at the pushed head; gofmt; go vet darwin, linux
  nogui, windows for internal/agent.
- internal/cli: TestGatewayKeyLimitCLI is red on origin/main b39a793c9 too,
  untouched here: #6082.
Not tried: the real Claude Desktop UI (only Claude Code with the
host-managed flag, 2.1.312/.314/.316); a user whose Claude runs in a WSL
distro is not covered; the full suite is run after the tag.
2026-11-11 13:28:02 +08:00

75 lines
2.2 KiB
Go

package agent
import (
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
)
// TestClaude1MMarked: every name magpie hands Claude Code for a model of a
// 1M window carries [1m], however it was set: Claude Code 2.1.284 and before
// take "claude/claude-opus-5-5" unmarked for 200K, whatever
// CLAUDE_CODE_MAX_CONTEXT_TOKENS says, and compact it again and again
// ("Autocompact is thrashing").
func TestClaude1MMarked(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
if err := provider.Save(provider.Provider{ID: "v", Name: "V", Chat: "https://example.test/v1", Key: "k",
Models: []string{"small", "big"}}); err != nil {
t.Fatal(err)
}
if err := catalog.SaveLive("v", "https://example.test/v1", []catalog.Model{
{ID: "small", Context: 200000}, {ID: "big", Context: 1000000},
}); err != nil {
t.Fatal(err)
}
path := filepath.Join(home, ".claude", "settings.json")
writeFile(t, path, `{}`)
a := claude(home)
get := func(k string) string {
if k == "model" {
return writtenModel(path)
}
v, _ := edit.GetJSON(path, k)
return v
}
set := func(key, v string) {
t.Helper()
if err := a.Field(key).Set(v); err != nil {
t.Fatal(err)
}
}
names := []string{"model", "env.ANTHROPIC_SMALL_FAST_MODEL"}
for _, tier := range claudeTiers {
if tier != "fable" { // left unset on a model that is no Fable (#2090)
names = append(names, "env."+tierEnv(tier))
}
}
for _, v := range []string{"v/big", "v/big[1m]"} {
set("model", v)
for _, k := range names {
if got := get(k); got != "v/big[1m]" {
t.Fatalf("model %q: %s = %q, want v/big[1m]", v, k, got)
}
}
}
// a tier set on its own is marked too; a 200K one is not
set("model", "v/small")
if got := get("model"); got != "v/small" {
t.Fatalf("a 200K model got marked: %q", got)
}
set("haiku", "v/big")
for _, k := range []string{"env." + tierEnv("haiku"), "env.ANTHROPIC_SMALL_FAST_MODEL"} {
if got := get(k); got != "v/big[1m]" {
t.Fatalf("haiku on v/big: %s = %q", k, got)
}
}
}