Files
magpie/internal/agent/claude_tiereffort_test.go
T
yetone ecd9835005 agent: Claude Code's subagent effort writes nothing without a subagent model (KorenKrita)
KorenKrita on Discord (cause confirmed by them): with Claude Code's
subagent model unset ("Claude Code picks") and a subagent effort set,
claude.go wrote spell(tierWith(main model, effort)) into
env.CLAUDE_CODE_SUBAGENT_MODEL. That pinned every subagent to the main
model, so Claude Code's own pick (the Agent call's model, the subagent's
frontmatter) never ran.

Agent wiring (internal/agent/claude.go):
- With no subagent model, magpie writes no effort, and
  CLAUDE_CODE_SUBAGENT_MODEL stays unset.
- The saved effort is kept, not deleted (the user set it). It lives in
  the stash under claude.subagent_effort, and the subagent effort's Get
  reads "" while there is no model. A model picked later takes it
  ("v/glm:high").
- A value an older magpie wrote that way (the main model at an effort,
  not own, while routed: subagentFollowing) is moved into the stash and
  taken out of the env by writeTiers and by Sync on start.
- subagentOwn no longer counts that value as the user's own model.
- Doc: docs/subsystems/agent-wiring.md, the Claude Code "subagents" entry.

GUI (window, opened Claude Code row):
- The subagent effort moves from its square under the session defaults
  to the right of the Subagents row. It uses the tier rows' markup and
  code (effortBtn in connectPanel, ag-eff), so it lines up with
  Opus/Sonnet/Haiku's effort.
- While the subagent model is unset it shows Default, is disabled, and
  its title says "Set a subagent model first". The new string is in zh,
  zh-TW, ja and de.
- The old square is removed from the opened row. No other layout changes.
- The tray panel's own square is left alone: #3308.

Tests:
- New internal/agent/claude_subagent_effort_test.go
  (TestClaudeSubagentEffortNeedsAModel). On the old code it is red:
    an effort with no subagent model wrote
    CLAUDE_CODE_SUBAGENT_MODEL="v/glm:high"
  With the Sync branch broken by hand it is red at "Sync left an older
  magpie's CLAUDE_CODE_SUBAGENT_MODEL="v/flash:high"". It also checks
  that a set model plus effort still writes "v/glm:low".
- claude_tiereffort_test.go now expects no env for an effort with no
  model.
- New internal/gui/tests/claude-subagent-effort-row.test.cjs (16 cases):
  chromium/webkit x en/zh x 1100/560px x with/without a subagent model.
  It checks:
  - the control sits in the Subagents row at the tier efforts' x and
    height;
  - with no model it is disabled at Default and opens no slider;
  - with a model it posts subagent_effort, with no scroll;
  - the old square is gone.
  It is red against the old app.js ("the old square is still there").
- tier-effort.test.cjs is updated for the moved control.

Run at this tree (parent f985d6bdb):
- go test -count=1 ./internal/gui/ ./internal/agent/ ./internal/provider/:
  ok.
- GUI: claude-subagent-effort-row, tier-effort, i18n-keys,
  subagent-effort, claude-subagents-pick, desktop-subagent-model,
  agent-prefs, agent-tweak-fold, claude-signin and agent-field-names:
  147/147.
- gofmt clean. go vet darwin, linux -tags nogui and windows: ok.

Run at the same change before its last rebase:
- Full Go suite (`go test -count=1 -timeout 45m ./...`, sandbox HOME,
  through the suite lock) at 11b9d671c (on 45593cc1d): every package ok
  but internal/gui's TestCoreScriptBudget. app.js was 1,337,535 bytes,
  over its 1,337,000 budget, after the rebase brought in upstream growth.
  This commit trims its own additions to 1,336,991 (one shared effortBtn,
  shorter comments, a shorter new string) and raises no budget.
  TestCoreScriptBudget now passes.
- Full Go suite at 41737d214 (on 802fb6e61): all 69 packages ok.
- Whole GUI suite at 41737d214 (11,741 tests, 5.8h at load ~250): 48 red,
  none from this change.
  - Red the same on origin/main 45593cc1d, all filed: quota-count-wrap
    and quota-name-translated (#2228), quota-reads (#2150), s3-sync
    (#2448), usage-agents-hide (#2276), usage-checkin (#2549, #3091),
    usage-fold (#2163), usage-ledger (#3104, #3001).
  - Red under load only and green alone: agent-notice-lang (#2960),
    agent-stale-copies (#1949), modal-morph (#3179), plugin-card-update
    (#3079), plugin-github (#3218), and agent-field-names and
    api-key-theme (filed as #3307).
  - Also seen red once under load: codex-subagent-model's fixed 300ms
    wait (#3277).

Not tried:
- a real Claude Code run reading the written settings;
- the Windows box;
- the whole GUI suite again after the string was shortened (only the
  files above were run);
- the tray panel (#3308).
2026-10-23 03:44:55 +08:00

155 lines
6.1 KiB
Go

package agent
import (
"path/filepath"
"slices"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
)
// Claude Code's tiers and its subagents can each run at an effort of their
// own (#536): the model the tier is given is "<model>:<level>" (before its
// [1m] mark), which magpie's gateway sends that model at that level. The
// effort stays with the tier through a model of its own, back to the main
// model, and a new main model; the main model's effort is untouched.
func TestClaudeTierEffort(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{"glm", "flash", "big"}}); err != nil {
t.Fatal(err)
}
if err := catalog.SaveLive("v", "https://example.test/v1", []catalog.Model{
{ID: "glm", Context: 200000, Efforts: []string{"low", "medium", "high", "max"}},
{ID: "flash", Context: 128000, Efforts: []string{"low", "high"}},
{ID: "big", Context: 1000000},
}); err != nil {
t.Fatal(err)
}
path := filepath.Join(home, ".claude", "settings.json")
writeFile(t, path, `{}`)
a := claude(home)
env := func(k string) string { v, _ := edit.GetJSON(path, "env."+k); return v }
set := func(key, v string) {
t.Helper()
if err := a.Field(key).Set(v); err != nil {
t.Fatalf("%s=%s: %v", key, v, err)
}
}
for _, k := range []string{"opus_effort", "sonnet_effort", "haiku_effort", "fable_effort", "subagent_effort"} {
if a.Field(k) == nil {
t.Fatalf("no %s field", k)
}
}
if err := a.Field("haiku_effort").Set("low"); err == nil {
t.Fatal("a tier's effort before Claude Code is routed should fail")
}
set("model", "v/glm")
set("effort", "high")
// a tier on a model of its own, at an effort of its own
set("haiku", "v/flash")
if got := a.Field("haiku_effort").Options(a.Values()); !slices.Equal(optionValues(got), []string{"low", "high"}) {
t.Fatalf("haiku's levels: %v", got)
}
set("haiku_effort", "low")
if env("ANTHROPIC_DEFAULT_HAIKU_MODEL") != "v/flash:low" || env("ANTHROPIC_SMALL_FAST_MODEL") != "v/flash:low" {
t.Fatalf("haiku at low: %v", a.Values())
}
if a.Field("haiku").Get() != "v/flash" || a.Field("haiku_effort").Get() != "low" || a.Field("sonnet_effort").Get() != "" {
t.Fatalf("read back: %v", a.Values())
}
if v, _ := edit.GetJSON(path, "model"); v != "v/glm" || env("ANTHROPIC_MODEL") != "" || a.Field("effort").Get() != "high" || env("ANTHROPIC_DEFAULT_OPUS_MODEL") != "v/glm" {
t.Fatalf("the main model's effort changed: %v", a.Values())
}
// a tier following the main model, at another effort
set("sonnet_effort", "medium")
if env("ANTHROPIC_DEFAULT_SONNET_MODEL") != "v/glm:medium" || a.Field("sonnet").Get() != "" || a.Field("sonnet_effort").Get() != "medium" {
t.Fatalf("sonnet at medium: %v", a.Values())
}
// a 1M model keeps its mark after the effort, where Claude Code takes it off
set("opus", "v/big")
set("opus_effort", "high")
if env("ANTHROPIC_DEFAULT_OPUS_MODEL") != "v/big:high[1m]" || a.Field("opus").Get() != "v/big[1m]" || a.Field("opus_effort").Get() != "high" {
t.Fatalf("opus at high: %v", a.Values())
}
if env(claudeContextEnv) != "200000" {
t.Fatalf("window: %s", env(claudeContextEnv))
}
// the effort stays with the tier through a new model, and back to
// following the main model ("Same as main model"; the main model
// picked by name is the tier's own, #2654)
set("haiku", "")
if env("ANTHROPIC_DEFAULT_HAIKU_MODEL") != "v/glm:low" || a.Field("haiku").Get() != "" {
t.Fatalf("haiku on the main model: %v", a.Values())
}
// a new main model: tiers that followed it follow it at their effort,
// one of its own keeps its model
set("model", "v/flash")
if env("ANTHROPIC_DEFAULT_SONNET_MODEL") != "v/flash:medium" || env("ANTHROPIC_DEFAULT_HAIKU_MODEL") != "v/flash:low" ||
env("ANTHROPIC_DEFAULT_OPUS_MODEL") != "v/big:high[1m]" || env("ANTHROPIC_DEFAULT_FABLE_MODEL") != "" {
t.Fatalf("new main model: %v", a.Values())
}
// the tier's effort taken away: its model as before
set("sonnet_effort", "")
set("haiku_effort", "")
if env("ANTHROPIC_DEFAULT_SONNET_MODEL") != "v/flash" || env("ANTHROPIC_DEFAULT_HAIKU_MODEL") != "v/flash" {
t.Fatalf("efforts off: %v", a.Values())
}
if err := a.Field("haiku_effort").Set("ultra"); err == nil {
t.Fatal("a level that isn't one should fail")
}
// subagents: an effort with no model of their own is kept unwritten
// (the main model at it pinned every subagent there), and applied once
// they have one; see TestClaudeSubagentEffortNeedsAModel
set("opus", "")
set("opus_effort", "")
if env("CLAUDE_CODE_SUBAGENT_MODEL") != "" {
t.Fatalf("subagents follow: %v", a.Values())
}
set("subagent_effort", "low")
if env("CLAUDE_CODE_SUBAGENT_MODEL") != "" || a.Field("subagent").Get() != "" || a.Field("subagent_effort").Get() != "" {
t.Fatalf("subagents at low with no model: %v", a.Values())
}
set("model", "v/glm")
if env("CLAUDE_CODE_SUBAGENT_MODEL") != "" || a.Field("subagent").Get() != "" {
t.Fatalf("subagents follow the new model: %v", a.Values())
}
set("subagent", "v/flash")
if env("CLAUDE_CODE_SUBAGENT_MODEL") != "v/flash:low" || a.Field("subagent").Get() != "v/flash" || a.Field("subagent_effort").Get() != "low" {
t.Fatalf("subagents on flash at low: %v", a.Values())
}
set("subagent", "")
if env("CLAUDE_CODE_SUBAGENT_MODEL") != "" || a.Field("subagent_effort").Get() != "" {
t.Fatalf("subagents back: %v", a.Values())
}
set("subagent_effort", "")
set("subagent", "v/flash")
if env("CLAUDE_CODE_SUBAGENT_MODEL") != "v/flash" || a.Field("subagent_effort").Get() != "" {
t.Fatalf("subagents on flash, effort cleared: %v", a.Values())
}
set("subagent", "")
// back to Claude Code as installed: nothing of magpie's left
set("model", "")
for _, k := range claudeEnv {
if env(k) != "" {
t.Fatalf("%s left: %s", k, env(k))
}
}
}
func optionValues(os []Option) []string {
var out []string
for _, o := range os {
out = append(out, o.Value)
}
return out
}