Files
magpie/internal/agent/claude_advisor_test.go
T
yetone db9a5ec7c4 gateway: Claude Code's advisor runs in magpie for a model whose upstream can't (Awad_Yoo on X)
Claude Code 2.1.316 sends its advisor (/advisor, settings.json's
advisorModel) for any model when CLAUDE_CODE_ENABLE_EXPERIMENTAL_ADVISOR_TOOL
is set: an advisor_20260301 server tool, which only Anthropic's server
runs. Until now magpie left it out for every other upstream (b6fae8efd).

Now, when the advisor model is one magpie serves and the upstream can't
run it (not a Claude model on Anthropic's API with a same-provider Claude
advisor, which keeps the native passthrough), magpie runs it:
- the model is offered an "advisor" function tool with no parameters;
- a call is answered by asking the advisor model, through magpie's own
  routing, with the conversation so far and a short ask after it (up to 3
  calls a reply, then max_uses_exceeded, as Anthropic answers);
- the client gets the server_tool_use and advisor_tool_result blocks
  Anthropic writes (advisor_tool_result_error with its error_code on a
  failure), and the history Claude Code echoes back is read as a call and
  its result.
Claude Code's system prompt, identity text and its own advisor
instructions go upstream as it wrote them; nothing about the advisor is
added to the prompt beside the tool's neutral description.

Claude Code's agent row gets an Advisor line under Tiers, after Auto
mode, default Off: picking a model writes advisorModel and the env switch
(only when the user hasn't set it themselves); Off removes both, and so
does disconnecting. CLI: magpie claude advisor <model|default>.

Run (on the Linux box, xiaomi-gpu magpie-desktop, at this head; mote was
over its load the whole time and mica's disk is full):
- go vet ./internal/gateway ./internal/agent, and with GOOS
  linux/windows/darwin (CGO_ENABLED=0); gofmt -l clean
- go test ./internal/gateway -run 'Advisor|Advised|Search|Anthropic': ok
- go test ./internal/agent -run Claude: ok
- node --test claude-advisor, claude-auto-mode, i18n-keys,
  i18n-en-no-cjk (Chromium + WebKit, en and zh, 1100 and 560px): 24/24
- TestCoreScriptBudget measured by its own rule: app.js 1,084,341 of
  1,086,267, core 1,267,726 of 1,267,762 (no raise)
- Real Claude Code 2.1.316 in a sandbox HOME/CLAUDE_CONFIG_DIR against a
  sandbox `magpie serve` (darwin nogui build of this tree) with only the
  DeepSeek provider: `magpie claude advisor deepseek/deepseek-v4-pro`
  wrote advisorModel + the env; `claude -p --model deepseek/deepseek-flash`
  asked to consult the advisor got text, server_tool_use (srvtoolu_…),
  advisor_tool_result with deepseek-v4-pro's advice (usage row: v4-pro
  11,205 in / 221 out), then its answer quoting it. A --resume turn sent
  that history back and completed.

Broken by hand:
- advisedHere returning false: TestAdvisorServedForAVendorModel,
  TestAdvisorHistoryGoesBackAsACall, TestAdvisorFailureIsAnErrorCode,
  TestAdvisedHere and TestAdvisorModelAsTheUpstreamNamesIt red.
- the env write left out of the advisor field's Set:
  TestClaudeAdvisorModel red.

Not tried: the interactive TUI's /advisor picker (only -p and the
settings.json route); an advisor on a Chat-only or Responses upstream
with a real account (the fake upstream in the tests is Chat); an advisor
call in the same round as a client tool call, which is dropped as a
search call is. Seen on the way, not changed: in the fresh sandbox,
Claude Code's own "sonnet[1m]" default resolved to sonnet-4-5-20250929
rather than ANTHROPIC_DEFAULT_SONNET_MODEL without --model.
2026-11-13 15:31:06 +08:00

99 lines
3.1 KiB
Go

package agent
import (
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
)
// Claude Code's advisor for magpie's models (Awad_Yoo on X): Claude Code
// sends it only for Anthropic's own models unless
// CLAUDE_CODE_ENABLE_EXPERIMENTAL_ADVISOR_TOOL is set (2.1.316). The
// advisor field writes advisorModel, as /advisor does, with that switch,
// only while magpie routes Claude Code and only for a model magpie serves;
// clearing it, or magpie stepping out, takes both away, but never a switch
// the user wrote.
func TestClaudeAdvisorModel(t *testing.T) {
a, path, env := claudeAutoModeHome(t, `{}`)
f := a.Field("advisor")
if f == nil {
t.Fatal("no advisor field")
}
get := func() string { v, _ := edit.GetJSON(path, "advisorModel"); return v }
if err := f.Set("relay/claude-opus-5-5"); err == nil {
t.Fatal("an advisor before Claude Code is routed should fail")
}
if err := a.Field("model").Set("relay/deepseek-pro"); err != nil {
t.Fatal(err)
}
// unset by default: no advisor, no switch
if f.Get() != "" || get() != "" || env(claudeAdvisorEnv) != "" {
t.Fatalf("advisor on by default: %q %q", get(), env(claudeAdvisorEnv))
}
if !containsOption(f.Options(nil), "relay/claude-opus-5-5") {
t.Fatalf("options miss magpie's models: %v", f.Options(nil))
}
if err := f.Set("claude-opus-5-5"); err == nil || get() != "" {
t.Fatalf("a model magpie doesn't serve taken: %v %q", err, get())
}
if err := f.Set("relay/claude-opus-5-5"); err != nil {
t.Fatal(err)
}
if got := get(); got != provider.ClaudeSpelled("relay/claude-opus-5-5") || env(claudeAdvisorEnv) != "1" {
t.Fatalf("advisorModel %q, switch %q", got, env(claudeAdvisorEnv))
}
if f.Get() != "relay/claude-opus-5-5" {
t.Fatalf("get %q", f.Get())
}
if err := f.Set(""); err != nil {
t.Fatal(err)
}
if get() != "" || env(claudeAdvisorEnv) != "" {
t.Fatalf("clearing left %q %q", get(), env(claudeAdvisorEnv))
}
// magpie stepping out takes its advisor away
if err := f.Set("relay/deepseek-pro"); err != nil {
t.Fatal(err)
}
if err := a.Disconnect(); err != nil {
t.Fatal(err)
}
if get() != "" || env(claudeAdvisorEnv) != "" {
b, _ := os.ReadFile(path)
t.Fatalf("disconnect left the advisor: %s", b)
}
}
// A switch the user turned on themselves stays theirs: clearing magpie's
// advisor, or magpie stepping out, leaves it.
func TestClaudeAdvisorUserSwitch(t *testing.T) {
a, path, env := claudeAutoModeHome(t, `{"env":{"CLAUDE_CODE_ENABLE_EXPERIMENTAL_ADVISOR_TOOL":"1"},"advisorModel":"opus"}`)
if err := a.Field("model").Set("relay/deepseek-pro"); err != nil {
t.Fatal(err)
}
f := a.Field("advisor")
if err := f.Set("relay/claude-opus-5-5"); err != nil {
t.Fatal(err)
}
if err := f.Set(""); err != nil {
t.Fatal(err)
}
if env(claudeAdvisorEnv) != "1" {
b, _ := os.ReadFile(filepath.Clean(path))
t.Fatalf("the user's own switch taken: %s", b)
}
if err := f.Set("relay/claude-opus-5-5"); err != nil {
t.Fatal(err)
}
if err := a.Disconnect(); err != nil {
t.Fatal(err)
}
if env(claudeAdvisorEnv) != "1" {
b, _ := os.ReadFile(path)
t.Fatalf("disconnect took the user's own switch: %s", b)
}
}