Files
magpie/internal/agent/kept_effort_test.go
T
yetone 9be72c9480 zcode: a level's thinking carries its budget, and the user's own level options stay (#3852)
Review of d7e1e284f, two things it changed beyond its case:

- Every level that thinks was written as {"thinking":{"type":"enabled"}}
  with no budget_tokens. ZCode puts a level's options in the request, so
  a Claude before 4.6 (Sonnet 4.5, Haiku 4.5, a relay's) asked through
  magpie at any level would be sent "enabled" without the budget
  Anthropic's Messages API requires with it, a 400 it didn't get before.
  Each level now carries the budget the gateway reads that effort as
  (gateway.ThinkingBudget, budgetOf: low 4096 ... max 32000). The
  gateway already makes it adaptive for Claude 4.6 and later
  (adaptiveThinking; ZCode's output_config.effort wins over the budget)
  and fits it under max_tokens (maxoutput.go).
- providerOptionsByLevel was written over on every sync. A user who put
  their own there (the workaround for this very bug) lost it. A level's
  options already in config.json now stay; no magpie before #3852
  wrote any, so whatever is there is the user's.

Tests: TestZCodeOutputAndReasoning expects the budgets;
TestZCodeKeepsTheUsersLevelOptions keeps a hand-written high level and
fills the others.

Run on mote: go test -run TestZCode ./internal/agent, ok. Broken by
hand: the kept options ignored and the budget left out, and those two
tests fail.
Not tried: a real ZCode 3.x (this Mac has Z Code 0.9.1, a different
app; mote has none), so ZCode putting the options in the body is the
reporter's and the author's reading of zcode.cjs, not seen here.

Batch record (gua's #2833 #3052 #3205 #3867 #4107 #4247 #4245 #4113
#3852 and the three review follow-ups; #4106 left out, its test isn't
red without its fix, see the issue):
- Full run on mote before the rebase (base fa4704f06): gofmt -l clean;
  go vet on darwin, GOOS=linux -tags nogui and GOOS=windows clean;
  go test -count=1 -timeout 45m ./...: 85 packages ok, internal/gui red
  in TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
  TestMainWindowCorners/macOS_26_SDK (#3949), both already filed, and
  neither in a file this batch touches.
- Rebased on origin/main 9162277b9, at this tree, on mote: gofmt clean;
  go test -count=1 -timeout 45m ./internal/gateway/... ./internal/agent
  ./internal/library ./internal/handoff ./internal/routes, all ok
  (and ./internal/redact ok at the base before, b2145eac1). Then
  rebased again on ccb75030a (#4371, fastjson.go only) and
  ./internal/gateway/... run again at that tree: ok (636s). Pushed on 8ac1c7a56 (internal/catalog
  only), where catalog, handoff and library ran whole and the batch's
  gateway (Bili, TestRuleCompact, Kimi, TestClaudeMem, the two stall
  tests) and agent (TestZCode) tests ran: all ok. The whole gateway
  package was not run again on top of 8ac1c7a56.
- Windows (win-run): -run TestClaudeMem ./internal/gateway ok; with
  claude_room.go put back, TestClaudeMemRunsFromTheCgroup hangs in
  readLimit to the 5m timeout.
- Linux (wsl-run): ./internal/handoff ./internal/library ok; with the
  fixes put back, TestGiveKeepsTheListenerNonblocking,
  TestSaveDropsEmptyApplied and TestSyncAloneLeavesNoEmptyApplied fail.
Not tried: real Kimi, ZCode 3.x, Alma or bili accounts; #4245 is a
test-only change and was not reproduced red.
2026-11-01 10:26:42 +08:00

377 lines
12 KiB
Go

package agent
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"github.com/pelletier/go-toml/v2"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// keptEffortHome is a sandbox HOME with a provider of thinking models: deep
// (none, low, high: magpie's default high) and wide (low, medium, high:
// magpie's default medium).
func keptEffortHome(t *testing.T, models ...string) string {
t.Helper()
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"))
t.Setenv("WORKBUDDY_CONFIG_DIR", "")
t.Setenv("CODEBUDDY_CONFIG_DIR", "")
t.Setenv("KIMI_SHARE_DIR", "")
t.Setenv("KIMI_CODE_HOME", "")
t.Setenv("MINIMAX_DATA_DIR", "")
keptEffortModels(t, models...)
return home
}
func keptEffortModels(t *testing.T, models ...string) {
t.Helper()
if len(models) == 0 {
models = []string{"deep", "wide"}
}
if err := provider.Save(provider.Provider{ID: "think", Name: "Think", Chat: "https://example.test/v1", Key: "k", Models: models}); err != nil {
t.Fatal(err)
}
if err := catalog.SaveLive("think", "https://example.test/v1", []catalog.Model{
{ID: "deep", Efforts: []string{"none", "low", "high"}},
{ID: "wide", Efforts: []string{"low", "medium", "high"}},
{ID: "fresh", Efforts: []string{"low", "medium", "high"}},
}); err != nil {
t.Fatal(err)
}
}
// 悠悠哥 on Discord: the default thinking effort set on one of magpie's
// models in WorkBuddy was put back to magpie's on every sync. WorkBuddy's
// Settings > Models saves an edited model whole into models.json, with
// reasoning.defaultEffort (empty for its Auto) and the vendor of the
// provider it matches, "Custom" for magpie's (WorkBuddy 5.x's
// inferProviderIdFromLocalCustomModel and the form's save); one set by hand
// keeps vendor magpie. Either way a sync keeps the user's effort while the
// model offers it, a new model gets magpie's default, and a model magpie no
// longer has is taken out, with no second entry of any model.
func TestWorkBuddyKeepsTheUsersDefaultEffort(t *testing.T) {
home := keptEffortHome(t)
path := filepath.Join(home, ".workbuddy", "models.json")
os.MkdirAll(filepath.Dir(path), 0o755)
a := workbuddy(home)
if err := a.Field("provider").Set(magpieID); err != nil {
t.Fatal(err)
}
read := func() map[string]map[string]any {
t.Helper()
var ms []map[string]any
b, _ := os.ReadFile(path)
if err := json.Unmarshal(b, &ms); err != nil {
t.Fatalf("%v\n%s", err, b)
}
out := map[string]map[string]any{}
for _, m := range ms {
id := m["id"].(string)
if out[id] != nil {
t.Fatalf("%s twice: %s", id, b)
}
out[id] = m
}
return out
}
write := func(ms map[string]map[string]any) {
t.Helper()
var list []map[string]any
for _, id := range []string{"think/deep", "think/wide", "think/fresh"} {
if ms[id] != nil {
list = append(list, ms[id])
}
}
b, _ := json.Marshal(list)
os.WriteFile(path, b, 0o600)
}
effort := func(m map[string]any) any {
r, _ := m["reasoning"].(map[string]any)
if r == nil {
return "no reasoning"
}
return r["defaultEffort"]
}
ms := read()
if effort(ms["think/deep"]) != "high" || effort(ms["think/wide"]) != "medium" {
t.Fatalf("magpie's defaults: %v %v", effort(ms["think/deep"]), effort(ms["think/wide"]))
}
// deep set by hand; wide saved from WorkBuddy's model settings
ms["think/deep"]["reasoning"].(map[string]any)["defaultEffort"] = "low"
wide := ms["think/wide"]
wide["vendor"] = "Custom"
wide["useCustomProtocol"] = false
wide["reasoning"] = map[string]any{"defaultEffort": "high", "supportedEfforts": []string{"low", "medium", "high"}, "canDisableThinking": false}
write(ms)
if err := a.Sync(); err != nil {
t.Fatal(err)
}
ms = read()
if len(ms) != 2 || effort(ms["think/deep"]) != "low" || effort(ms["think/wide"]) != "high" || ms["think/wide"]["vendor"] != magpieID {
t.Fatalf("after sync: %v", ms)
}
// WorkBuddy's Auto: no defaultEffort, which stays so
delete(ms["think/wide"]["reasoning"].(map[string]any), "defaultEffort")
ms["think/wide"]["vendor"] = "Custom"
write(ms)
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if ms = read(); effort(ms["think/wide"]) != nil {
t.Fatalf("Auto: %v", ms["think/wide"])
}
// an effort the model no longer offers gives way to magpie's default
ms["think/deep"]["reasoning"].(map[string]any)["defaultEffort"] = "xhigh"
write(ms)
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if ms = read(); effort(ms["think/deep"]) != "high" {
t.Fatalf("unoffered effort: %v", effort(ms["think/deep"]))
}
// a new model gets magpie's default; a model gone is taken out, the
// one saved from WorkBuddy's settings too
ms["think/deep"]["reasoning"].(map[string]any)["defaultEffort"] = "low"
ms["think/wide"]["vendor"] = "Custom"
write(ms)
keptEffortModels(t, "deep", "fresh")
if err := a.Sync(); err != nil {
t.Fatal(err)
}
ms = read()
if len(ms) != 2 || ms["think/wide"] != nil || effort(ms["think/fresh"]) != "medium" || effort(ms["think/deep"]) != "low" {
t.Fatalf("after the catalog changed: %v", ms)
}
// off takes a model saved from WorkBuddy's settings out too
ms["think/deep"]["vendor"] = "Custom"
write(ms)
if err := a.Field("provider").Set(""); err != nil {
t.Fatal(err)
}
if ms = read(); len(ms) != 0 {
t.Fatalf("after off: %v", ms)
}
}
// CodeBuddy Code reads the same models.json shape (buddyWrite), under its
// own key.
func TestCodeBuddyKeepsTheUsersDefaultEffort(t *testing.T) {
home := keptEffortHome(t)
path := filepath.Join(home, ".codebuddy", "models.json")
os.MkdirAll(filepath.Dir(path), 0o755)
a := codebuddy(home)
if err := a.Field("model").Set("magpie/think/deep"); err != nil {
t.Fatal(err)
}
b, _ := os.ReadFile(path)
os.WriteFile(path, []byte(strings.Replace(string(b), `"defaultEffort": "high"`, `"defaultEffort": "low"`, 1)), 0o600)
if err := a.Sync(); err != nil {
t.Fatal(err)
}
var doc struct {
Models []struct {
ID string
Reasoning struct{ DefaultEffort string }
}
}
b, _ = os.ReadFile(path)
json.Unmarshal(b, &doc)
if len(doc.Models) != 2 || doc.Models[0].ID != "think/deep" || doc.Models[0].Reasoning.DefaultEffort != "low" {
t.Fatalf("after sync: %s", b)
}
}
// ZCode's config.json: a defaultLevel set by hand stays through a sync.
func TestZCodeKeepsTheUsersDefaultLevel(t *testing.T) {
home := keptEffortHome(t)
path := filepath.Join(home, ".zcode", "v2", "config.json")
os.MkdirAll(filepath.Dir(path), 0o755)
a := zcode(home)
if err := a.Field("provider").Set(magpieID); err != nil {
t.Fatal(err)
}
var doc map[string]any
b, _ := os.ReadFile(path)
json.Unmarshal(b, &doc)
r := doc["provider"].(map[string]any)[magpieID].(map[string]any)["models"].(map[string]any)["think/wide"].(map[string]any)["reasoning"].(map[string]any)
if r["defaultLevel"] != "medium" {
t.Fatalf("magpie's default: %s", b)
}
r["defaultLevel"] = "high"
b, _ = json.Marshal(doc)
os.WriteFile(path, b, 0o644)
if err := a.Sync(); err != nil {
t.Fatal(err)
}
var c struct {
Provider map[string]struct {
Models map[string]struct {
Reasoning struct{ DefaultLevel string }
}
}
}
b, _ = os.ReadFile(path)
json.Unmarshal(b, &c)
ms := c.Provider[magpieID].Models
if ms["think/deep"].Reasoning.DefaultLevel != "high" || ms["think/wide"].Reasoning.DefaultLevel != "high" {
t.Fatalf("after sync: %s", b)
}
}
// A default a magpie before #3852 wrote as defaultVariant — a key ZCode
// never read — is carried over: the user's choice survives the first sync
// on the keys ZCode reads.
func TestZCodeCarriesTheOldDefaultVariantOver(t *testing.T) {
home := keptEffortHome(t)
path := filepath.Join(home, ".zcode", "v2", "config.json")
os.MkdirAll(filepath.Dir(path), 0o755)
a := zcode(home)
if err := a.Field("provider").Set(magpieID); err != nil {
t.Fatal(err)
}
var doc map[string]any
b, _ := os.ReadFile(path)
json.Unmarshal(b, &doc)
r := doc["provider"].(map[string]any)[magpieID].(map[string]any)["models"].(map[string]any)["think/wide"].(map[string]any)["reasoning"].(map[string]any)
delete(r, "defaultLevel")
r["defaultVariant"] = "high"
b, _ = json.Marshal(doc)
os.WriteFile(path, b, 0o644)
if err := a.Sync(); err != nil {
t.Fatal(err)
}
var c struct {
Provider map[string]struct {
Models map[string]struct {
Reasoning struct{ DefaultLevel string }
}
}
}
b, _ = os.ReadFile(path)
json.Unmarshal(b, &c)
ms := c.Provider[magpieID].Models
if ms["think/wide"].Reasoning.DefaultLevel != "high" {
t.Fatalf("after sync: %s", b)
}
}
// Kimi Code's default_effort set by hand on one of magpie's models stays
// through a sync.
func TestKimiKeepsTheUsersDefaultEffort(t *testing.T) {
home := keptEffortHome(t)
path := filepath.Join(home, ".kimi-code", "config.toml")
writeFile(t, path, "")
a := kimi(home)
if err := a.Field("model").Set("magpie/think/deep"); err != nil {
t.Fatal(err)
}
b, _ := os.ReadFile(path)
at := strings.Index(string(b), `[models."magpie/think/wide"]`)
if at < 0 || !strings.Contains(string(b[at:]), `default_effort = "high"`) {
t.Fatalf("magpie's default: %s", b)
}
s := string(b[:at]) + strings.Replace(string(b[at:]), `default_effort = "high"`, `default_effort = "low"`, 1)
os.WriteFile(path, []byte(s), 0o600)
if err := a.Sync(); err != nil {
t.Fatal(err)
}
var c struct {
Models map[string]struct {
DefaultEffort string `toml:"default_effort"`
}
}
b, _ = os.ReadFile(path)
if err := toml.Unmarshal(b, &c); err != nil {
t.Fatalf("%v\n%s", err, b)
}
if c.Models["magpie/think/wide"].DefaultEffort != "low" || c.Models["magpie/think/deep"].DefaultEffort != "high" {
t.Fatalf("after sync: %s", b)
}
}
// MiniMax Code's thinking.defaultEffort set by hand stays through a sync.
func TestMiniMaxKeepsTheUsersDefaultEffort(t *testing.T) {
home, path := miniMaxHome(t)
keptEffortModels(t)
a := miniMax(home)
if err := a.Field("model").Set("magpie/think/deep"); err != nil {
t.Fatal(err)
}
c, raw := readMiniMax(t, path)
if c.CustomProvider[magpieID].Models["think~wide"].Thinking.DefaultEffort != "high" {
t.Fatalf("magpie's default:\n%s", raw)
}
at := strings.Index(raw, "think~wide:")
raw = raw[:at] + strings.Replace(raw[at:], "defaultEffort: high", "defaultEffort: low", 1)
os.WriteFile(path, []byte(raw), 0o644)
if err := a.Sync(); err != nil {
t.Fatal(err)
}
c, raw = readMiniMax(t, path)
ms := c.CustomProvider[magpieID].Models
if ms["think~wide"].Thinking.DefaultEffort != "low" || ms["think~deep"].Thinking.DefaultEffort != "high" {
t.Fatalf("after sync:\n%s", raw)
}
}
// ZCode's config.json: providerOptionsByLevel a user wrote for a level
// (the workaround #3852's reporter could have had in place) stays through
// a sync; the levels they left alone get magpie's.
func TestZCodeKeepsTheUsersLevelOptions(t *testing.T) {
home := keptEffortHome(t)
path := filepath.Join(home, ".zcode", "v2", "config.json")
os.MkdirAll(filepath.Dir(path), 0o755)
a := zcode(home)
if err := a.Field("provider").Set(magpieID); err != nil {
t.Fatal(err)
}
var doc map[string]any
b, _ := os.ReadFile(path)
json.Unmarshal(b, &doc)
r := doc["provider"].(map[string]any)[magpieID].(map[string]any)["models"].(map[string]any)["think/deep"].(map[string]any)["reasoning"].(map[string]any)
r["providerOptionsByLevel"] = map[string]any{"high": map[string]any{"thinking": map[string]any{"type": "adaptive"}}}
b, _ = json.Marshal(doc)
os.WriteFile(path, b, 0o644)
if err := a.Sync(); err != nil {
t.Fatal(err)
}
var c struct {
Provider map[string]struct {
Models map[string]struct {
Reasoning struct {
Options map[string]struct {
Thinking struct {
Type string `json:"type"`
Budget int `json:"budget_tokens"`
} `json:"thinking"`
} `json:"providerOptionsByLevel"`
}
}
}
}
b, _ = os.ReadFile(path)
json.Unmarshal(b, &c)
o := c.Provider[magpieID].Models["think/deep"].Reasoning.Options
if o["high"].Thinking.Type != "adaptive" || o["high"].Thinking.Budget != 0 {
t.Fatalf("the user's high level was written over: %s", b)
}
if o["low"].Thinking.Type != "enabled" || o["low"].Thinking.Budget != 4096 || o["disabled"].Thinking.Type != "disabled" {
t.Fatalf("the levels the user left alone: %s", b)
}
}