Files
magpie/internal/agent/claude_classifier_test.go
T
yetone 137187a401 gateway: Claude Code's auto mode classifier follows a /model switch at once
CyberCaius on Discord: after switching models, the first auto mode
permission check went to the previous model. The model is Claude Code's
choice, not magpie's. Claude Code 2.1.310 ($Xe/BB) names
ANTHROPIC_DEFAULT_SONNET_MODEL for the classifier, not the session's
model. magpie's sonnet tier follows the main model only once Follow runs
(the GUI's state poll), and Claude Code reads env again only on its next
start. So right after /model the classifier named the old main model.

At the gateway, a classifier request (recognised from its body: a
<transcript> and a <block>/<severity> verdict, no tools) on a sonnet tier
that follows a main model settings.json no longer has goes to the tier
the new main model gives it (followAt, keeping the tier's effort). It is
left alone when the user set CLAUDE_CODE_AUTO_MODE_MODEL, when the sonnet
tier is their own, when the base URL isn't magpie's, or when the main
model hasn't moved. WSL distros are read the same way. Only the
classifier is rerouted: a turn naming the old model may be another open
session that is still on it. The haiku, opus and subagent siblings are
#3474.

Run (sandbox HOME; packages at 7edf5b0a3, then rebased on f35c721c4,
where the build, Windows build, Linux vet and the classifier, StandIn,
tier and Follow tests ran again: ok):
- TestAutoModeClassifierFollowsTheSwitchedModel (gateway) and
  TestClaudeClassifierFollowsTheSwitchedModel (agent): pass.
- go test -tags nogui -timeout 45m ./internal/agent ./internal/gateway
  ./internal/cli: ok (agent 787s, gateway 1191s, cli 129s, load ~450).
- gofmt -l clean; go vet, GOOS=linux go vet -tags nogui, GOOS=windows
  go vet on the three packages; go build, GOOS=linux -tags nogui,
  GOOS=windows: ok.

Broken by hand, each red then restored:
- classifierStandIn never rerouting: gateway test red ("went to m1").
- claudeClassifierIn returning "": agent test red at line 56.
- without the own-sonnet-tier check: red at line 84.
- without the CLAUDE_CODE_AUTO_MODE_MODEL check: red at line 67.

Not tried: real Claude Code doing a /model switch against this build. A
full ./... suite didn't run either: another session's full suite held the
machine, with several more queued behind it.
2026-10-24 19:47:12 +08:00

101 lines
3.5 KiB
Go

package agent
import (
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
)
// CyberCaius on Discord: after a switch in /model, the first auto mode
// classifier request went to the model before it. Claude Code names its
// sonnet tier for the classifier, and magpie's sonnet tier follows the
// main model only once Follow has run. Until then the classifier's model
// is the tier the new main model would give it; a tier, or an auto mode
// model, of the user's own is theirs as it is.
func TestClaudeClassifierFollowsTheSwitchedModel(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: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"pro", "flash"}}); err != nil {
t.Fatal(err)
}
path := filepath.Join(home, ".claude", "settings.json")
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(`{}`), 0o644); err != nil {
t.Fatal(err)
}
a := claude(home)
if err := a.Field("model").Set("deepseek/pro"); err != nil {
t.Fatal(err)
}
env := func(k string) string { v, _ := edit.GetJSON(path, "env."+k); return v }
tier := env(tierEnv("sonnet"))
if tier != "deepseek/pro" {
t.Fatalf("the sonnet tier doesn't follow the main model: %q", tier)
}
at := here(home)
if got := claudeClassifierIn(at, tier); got != "" {
t.Fatalf("main model unchanged, the classifier was moved to %q", got)
}
// /model saves the new main model; Follow hasn't run
if err := edit.SetJSON(path, edit.KV{Path: "model", Value: "deepseek/flash"}); err != nil {
t.Fatal(err)
}
if env(tierEnv("sonnet")) != tier {
t.Fatal("the tier moved without Follow")
}
if got := claudeClassifierIn(at, tier); got != "deepseek/flash" {
t.Fatalf("after /model the classifier goes to %q, want deepseek/flash", got)
}
if got := claudeClassifierIn(at, "deepseek/flash"); got != "" {
t.Fatalf("a model that isn't the sonnet tier was moved to %q", got)
}
// an auto mode model of the user's own
if err := edit.SetJSON(path, edit.KV{Path: "env." + claudeAutoModeEnv, Value: "deepseek/pro"}); err != nil {
t.Fatal(err)
}
if got := claudeClassifierIn(at, tier); got != "" {
t.Fatalf("the user's auto mode model was moved to %q", got)
}
if err := edit.SetJSON(path, edit.KV{Path: "env." + claudeAutoModeEnv, Value: nil}); err != nil {
t.Fatal(err)
}
// a sonnet tier of the user's own
if err := edit.SetJSON(path, edit.KV{Path: "model", Value: "deepseek/pro"}); err != nil {
t.Fatal(err)
}
if err := a.Field("sonnet").Set("deepseek/pro"); err != nil {
t.Fatal(err)
}
if err := edit.SetJSON(path, edit.KV{Path: "model", Value: "deepseek/flash"}); err != nil {
t.Fatal(err)
}
if got := claudeClassifierIn(at, "deepseek/pro"); got != "" {
t.Fatalf("the user's own sonnet tier was moved to %q", got)
}
// Follow has run: the tier is the new main model's, and nothing moves
if err := a.Field("sonnet").Set(""); err != nil {
t.Fatal(err)
}
if err := a.Follow(); err != nil {
t.Fatal(err)
}
if got := env(tierEnv("sonnet")); got != "deepseek/flash" {
t.Fatalf("Follow left the tier on %q", got)
}
if got := claudeClassifierIn(at, "deepseek/flash"); got != "" {
t.Fatalf("after Follow the classifier was moved to %q", got)
}
}