Files
magpie/internal/agent/adaptive_rule_test.go
T
yetone 5ba53b2392 agent: dsh and Pi ask a Gemini 3+ on Messages adaptively, so Xhigh reaches it as xhigh (#6778)
larryhaonlp: DSH's Xhigh on agy-cli/gemini-4-hydrogen through
magpie-anthropic reached agy-cli as high, and a compat
forceAdaptiveThinking: true added by hand in dsh went on restart.

Cause, from the installed sources (dsh 0.2.1-rc.2's and Pi 1.4.0's
pi-ai, anthropic-messages.js and simple-options.js): without
compat.forceAdaptiveThinking pi-ai asks a budget through
adjustMaxTokensForThinking, whose clampReasoning turns xhigh and max into
high (16384), and agy-cli's requestedEffort reads a budget of 16384 as
high. With the switch it sends thinking adaptive plus output_config.effort
from thinkingLevelMap, which agy-cli reads first. magpie wrote the switch
only for Claude that thinks only adaptively and GLM-5.2/5.3.

- gateway.AsksAdaptively is the one rule: adaptiveOnly, AdaptiveWithEffort
  or a Gemini 3 or later (geminiGeneration, the rule geminiLevelBody
  already turns a budget into adaptive + effort by). dsh's dshKindConfig
  and Pi's piModelOn (both cases) use it. Pi now also gets the switch for
  GLM-5.2/5.3, which it lacked (same clamp).
- omp is left on AdaptiveThinking, said in omp.go: its budget isn't
  clamped (ANTHROPIC_THINKING xhigh and max are 32768, read as xhigh), and
  its anthropic-adaptive mode turns thinking off by leaving thinking out at
  the lowest effort, which a model that can stop thinking takes as on.
- The user's own values: dshKeepCompat kept a hand-set switch magpie
  doesn't write, but magpie's own switch won over the user's. magpie
  writes its switches only as true (omitempty), so a false in the file is
  the user's, and now stays.
- The restart: dsh-settings' importLegacyDocument imports
  ~/.dsh/settings.yaml into the profile dsh starts on and renames it
  .imported; magpie writes settings.yaml again with routes that had no
  hand-set compat, so each start of dsh took it out of the profile.
  dshProfilesCompat carries the profiles' hand-set compat into
  settings.yaml's routes, by the same dshKeepCompat.

Existing users: KeepDshWired's sync (30s) and Pi's sync rewrite magpie's
routes, so forceAdaptiveThinking: true lands on a Gemini 3+ there with no
action; a false the user set stays. Pi's models.json provider block is
magpie's own and is written whole, as before.

Run (mote; sandbox HOME):
- go test -tags nogui ./internal/agent -run the new tests and the dsh,
  Pi and omp compat tests (TestDshMaxReachesOutputEffortModels,
  TestDshRefreshKeepsHandSetCompat, TestDshKeepCompatNotAcrossProtocols,
  Pi.*API, Omp): ok
- gofmt -l: clean
- Shipped before the whole ./internal/agent and ./internal/gateway run and
  vet (darwin, linux, windows) finished: it was still going on mote, over
  its cores, and mica's disk was full. A red there gets its own fix or
  issue.

Broken by hand:
- the new tests at origin/main 8608ce024: TestPiAgentsAskGeminiAdaptively
  red (dsh and Pi gemini-4-hydrogen, gemini-3.8-flash; Pi GLM-5.3) and
  TestDshHandSetCompatOutlivesGeminiRuleAndRestart red
- the explicit-false keep taken out: red (hydrogen's false put back on)
- dshProfilesCompat calls taken out: red (settings.yaml lacks the false)

Not tried: a real dsh or Pi run against agy-cli's gemini-4-hydrogen
(the effort agy-cli reads is taken from its requestedEffort source);
omp with a Gemini on Messages.
2026-11-16 21:40:30 +08:00

106 lines
4.8 KiB
Go

package agent
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// #6778 (larryhaonlp): DSH's Xhigh reached agy-cli/gemini-4-hydrogen, on
// magpie's Anthropic route, as high. pi-ai asks a model on Messages for a
// budget unless its compat says forceAdaptiveThinking, and the budget path
// clamps xhigh and max to high's 16384, which agy-cli reads as high. A
// Gemini 3 or later is now told to think adaptively there, by the one rule
// dsh and Pi (both pi-ai) are written by; a Gemini 2.x, another route and
// a model that takes a budget are not.
func TestPiAgentsAskGeminiAdaptively(t *testing.T) {
for _, tc := range []struct {
id string
want bool
}{
{"agy-cli/gemini-4-hydrogen", true},
{"relay/gemini-3.8-flash", true},
{"anth/claude-sonnet-5", true},
{"zcode/GLM-5.3", true},
{"relay/gemini-2.5-pro", false},
{"kimi/kimi-k3", false},
{"minimax/MiniMax-M3.1-Flash", false},
} {
m := catalog.Model{ID: tc.id, Context: 1048576, Output: 65536, APIs: []string{string(provider.Anthropic)}, Efforts: []string{"low", "medium", "high", "xhigh"}}
for _, k := range dshKinds {
got := dshKindConfig(k, []catalog.Model{m}, k.api, "http://127.0.0.1:3425").Models[0]
adaptive := got.Compat != nil && got.Compat.ForceAdaptiveThinking
if want := tc.want && k.api == "anthropic-messages"; adaptive != want {
t.Errorf("dsh %s on %s: forceAdaptiveThinking %v, want %v", tc.id, k.api, adaptive, want)
}
}
for _, on := range []string{"", "anthropic-messages", "openai-completions"} {
e := piModelOn(m, "http://127.0.0.1:3425", true, on)
c, _ := e["compat"].(map[string]any)
if want := tc.want && on != "openai-completions"; (c["forceAdaptiveThinking"] == true) != want {
t.Errorf("Pi %s on %q: compat %v, want forceAdaptiveThinking %v", tc.id, on, c, want)
}
}
}
}
// A compat switch magpie writes is put as magpie has it, but a false one
// stands in the file only by the user's hand (magpie writes its switches
// as true alone), and stays. And what dsh's Models page set by hand on one
// of magpie's models goes into settings.yaml too: dsh imports that file
// into the profile it starts on, so one written without them took them
// out of the profile at each start (the reporter's compat went on
// restart).
func TestDshHandSetCompatOutlivesGeminiRuleAndRestart(t *testing.T) {
home, dir, _ := dshRouteHome(t)
dshKeyFixture(dir)
if err := provider.Save(provider.Provider{ID: "anth", Name: "Anth", Key: "k", Anthropic: "http://127.0.0.1:1", Models: []string{"gemini-4-hydrogen", "gemini-4-carbon"}}); err != nil {
t.Fatal(err)
}
if err := catalog.SaveLive("anth", "http://127.0.0.1:1", []catalog.Model{
{ID: "gemini-4-hydrogen", Efforts: []string{"low", "medium", "high", "xhigh"}},
{ID: "gemini-4-carbon", Efforts: []string{"low", "medium", "high", "xhigh"}},
}); err != nil {
t.Fatal(err)
}
desktop := filepath.Join(dir, "profiles", "desktop", "cordis.patch.yml")
os.MkdirAll(filepath.Dir(desktop), 0o755)
os.WriteFile(desktop, []byte("# Your patch layer for this dsh profile.\n"+
"- id: llm-pi-ai\n name: \"@deepseek-ai/dsh-llm-pi-ai\"\n config:\n providers:\n"+
" magpie-anthropic:\n displayName: Magpie · Anthropic\n apiKeyEnv: "+dshKeyRef+"\n api: anthropic-messages\n baseURL: http://127.0.0.1:3425\n models:\n"+
" - id: anth/gemini-4-hydrogen\n compat:\n forceAdaptiveThinking: false\n"+
" - id: anth/gemini-4-carbon\n compat:\n maxTokensField: max_tokens\n"+
"- id: agent-default-model\n config:\n provider: magpie-anthropic\n model: anth/gemini-4-hydrogen\n"), 0o644)
a := dsh(home)
for i := 1; i <= 2; i++ {
if err := a.Sync(); err != nil {
t.Fatal(err)
}
_, compat := dshRouteCompat(t, desktop, "magpie-anthropic")
if c := compat["anth/gemini-4-hydrogen"]; c["forceAdaptiveThinking"] != false {
b, _ := os.ReadFile(desktop)
t.Fatalf("sync %d: a forceAdaptiveThinking turned off by hand was put back on: %v\n%s", i, compat, b)
}
if c := compat["anth/gemini-4-carbon"]; c["forceAdaptiveThinking"] != true || c["maxTokensField"] != "max_tokens" {
b, _ := os.ReadFile(desktop)
t.Fatalf("sync %d: carbon's compat: %v\n%s", i, compat, b)
}
// dsh eats settings.yaml at each start; magpie writes it again
raw, err := os.ReadFile(dshSettings(dir))
if err != nil {
t.Fatal(err)
}
s := string(raw)
for _, want := range []string{"forceAdaptiveThinking: false", "maxTokensField: max_tokens", "forceAdaptiveThinking: true"} {
if !strings.Contains(s, want) {
t.Fatalf("sync %d: settings.yaml, which dsh imports into the profile at its start, lacks %q:\n%s", i, want, s)
}
}
os.Rename(dshSettings(dir), dshSettings(dir)+".imported")
}
}