Files
magpie/internal/gateway/advisor_test.go
T
yetone d54b7acf43 gateway: Claude Code's dashed advisor id goes upstream as the provider names it
Refeccd (Discord, v0.1.4366): Claude Code with Opus 5.5 as the model and
Fable 5.2 as the advisor, both on one provider; the advisor's turns came
from Opus and Routing showed no Fable request.

Claude Code 2.1.316 (real binary, sandbox HOME, a fake upstream) never
sends a request of its own for the advisor: it is the server tool
{"type":"advisor_20260301","model":"relay/claude-fable-5-2"} inside the
main request, with settings.json's advisorModel as written. magpie writes
a dotted Claude id dashed (ClaudeSpelled), and the gateway took the main
model back to the served id (validateModel) but not the advisor's. On a
provider listing claude-opus-5.5 and claude-fable-5.2 the request went
upstream as model claude-opus-5.5 with advisor claude-fable-5-2, a name
the relay doesn't know.

advisorID now takes the advisor's model to the served id as a request's
own model is (ClaudeUndashed, then unflat and CanonicalRef), and drops
[2m] beside [1m]. Every reader of it goes through it: advisorModel
(passthrough name), advisorKeyModel (key limits), advisorAsked
(advisedHere, advisedInBridge) and readAnthropic's Request.Advisor
(magpie-run advice).

Run (on mica):
- go test -count=1 -timeout 45m ./internal/gateway/: ok at 38babcb13
  (this change on efcf8d9c0), and before at d67ccbd3c and 8e1534f5f
- rebased since onto a922e3864, which adds only internal/library,
  internal/agent/dsh.go and internal/provider/agy_move.go: at this head,
  go test -run Advis ./internal/gateway/ ok, and GOOS=linux and
  GOOS=windows go vet ./internal/gateway/ ./internal/provider/ ok
- gofmt -l internal/gateway: clean
- GOOS=linux and GOOS=windows go vet ./internal/gateway/: ok

Broken by hand: advisorID returning the trimmed id as before makes
TestAdvisorModelAsTheUpstreamNamesIt's new "a dotted id as Claude Code
names it" case red: advisor sent as ["claude-fable-5-2"], want
"claude-fable-5.2", the reporter's shape.

Not tried: the reporter's own relay. magpie still doesn't read
usage.iterations' advisor_message entries, so a passthrough advisor run
at the vendor shows no Fable row in Routing; that is by design of the
server tool, not changed here (#6977).
2026-11-17 21:48:13 +08:00

159 lines
6.6 KiB
Go

package gateway
import (
"os"
"strings"
"testing"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/provider"
)
// advisorTurn is a Claude Code turn with its advisor on, as Claude Code
// 2.1.300 sends it (read from its request to a fake ANTHROPIC_BASE_URL in
// a sandbox HOME, --advisor; the environment text cut short, the agent's
// tools but Read left out): the server tool, and a {role: "system"} message
// holding the advisor's instructions and a tool_addition naming it.
func advisorTurn(model, advisor string) string {
b, err := os.ReadFile("testdata/claude-code-advisor-turn.json")
if err != nil {
panic(err)
}
return strings.NewReplacer(`"MODEL"`, `"`+model+`"`, `"ADVISOR"`, `"`+advisor+`"`).Replace(string(b))
}
// Claude Code's advisor names its model as Claude Code names its own: on
// magpie, by magpie's id, which the vendor doesn't know, so the request was
// turned away whole and the advisor never ran (JackChou on Discord). The
// advisor's model now goes as the upstream's own name for it when it is
// the same provider's Claude model; one the upstream can't run (another
// provider's, a vendor's model asked) is run by magpie (advisor_serve.go),
// and one magpie doesn't serve either is left out and the turn still
// answered.
func TestAdvisorModelAsTheUpstreamNamesIt(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
u, url := newBillingUpstream(t)
for _, p := range []provider.Provider{
{ID: "relay", Name: "Relay", Key: "k", Anthropic: url, Models: []string{"claude-opus-5-5", "claude-sonnet-5-5"}},
{ID: "other", Name: "Other", Key: "k", Anthropic: url, Models: []string{"claude-opus-5-5"}},
{ID: "ds", Name: "DeepSeek", Key: "k", Anthropic: url, Models: []string{"deepseek-v4.1-flash"}},
{ID: "dot", Name: "Dotted", Key: "k", Anthropic: url, Models: []string{"claude-opus-5.5", "claude-fable-5.2"}},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
advisors := func(b string) []string {
var out []string
for _, t := range gjson.Get(b, "tools").Array() {
if strings.HasPrefix(t.Get("type").String(), "advisor_") {
out = append(out, t.Get("model").String())
}
}
return out
}
for _, c := range []struct {
name, model, advisor, want string // want "" = not sent upstream
served bool // run by magpie
}{
{"same provider", "relay/claude-sonnet-5-5", "relay/claude-opus-5-5", "claude-opus-5-5", false},
{"1M mark", "relay/claude-sonnet-5-5", "relay/claude-opus-5-5[1m]", "claude-opus-5-5", false},
{"Claude's own id", "relay/claude-sonnet-5-5", "claude-opus-5-5", "claude-opus-5-5", false},
{"another provider", "relay/claude-sonnet-5-5", "other/claude-opus-5-5", "", true},
{"a vendor's model", "ds/deepseek-v4.1-flash", "relay/claude-opus-5-5", "", true},
{"a model magpie doesn't serve", "ds/deepseek-v4.1-flash", "nobody/claude-opus-5-5", "", false},
{"2M mark", "relay/claude-sonnet-5-5", "relay/claude-opus-5-5[2m]", "claude-opus-5-5", false},
// Claude Code is given a dotted id with dashes, and names the
// advisor so (Refeccd on Discord)
{"a dotted id as Claude Code names it", "dot/claude-opus-5-5", "dot/claude-fable-5-2", "claude-fable-5.2", false},
} {
claudeCodeSends(t, advisorTurn(c.model, c.advisor))
b := u.last(t)
got := advisors(b)
switch {
case c.want == "" && len(got) != 0, c.want != "" && (len(got) != 1 || got[0] != c.want):
t.Errorf("%s: advisor sent as %q, want %q: %s", c.name, got, c.want, b)
}
if !strings.Contains(b, `"name":"Read"`) {
t.Errorf("%s: the agent's own tool lost: %s", c.name, b)
}
if c.served {
// offered as the model's own tool, Claude Code's instructions
// for it as written
if !strings.Contains(b, `"name":"advisor"`) || !strings.Contains(b, "You have access to an `advisor` tool") || strings.Contains(b, advisorGone) {
t.Errorf("%s: advisor not offered as magpie's: %s", c.name, b)
}
continue
}
advisorSaid(t, c.name, b, c.want != "")
}
}
// advisorSaid checks what an upstream body b tells the model of the
// advisor: with it on (on), Claude Code's instructions and the
// tool_addition naming it as Claude Code sent them; with it left out,
// neither, and Claude Code's own word that it is gone. The rest of the
// message holding them stays.
func advisorSaid(t *testing.T, name, b string, on bool) {
t.Helper()
instructions := strings.Contains(b, "You have access to an `advisor` tool")
ref := false
gjson.Get(b, "messages").ForEach(func(_, m gjson.Result) bool {
m.Get("content").ForEach(func(_, blk gjson.Result) bool {
if blk.Get("type").String() == "tool_addition" && blk.Get("tool.name").String() == "advisor" {
ref = true
}
return true
})
return true
})
switch {
case on && (!instructions || !ref):
t.Errorf("%s: advisor on, but its instructions (%v) or tool_addition (%v) lost: %s", name, instructions, ref, b)
case !on && (instructions || ref):
// the model called advisor() as the agent's tool: "No such tool
// available: advisor" (JackChou on Discord)
t.Errorf("%s: advisor left out, but its instructions (%v) or tool_addition (%v) still sent: %s", name, instructions, ref, b)
case !on && !strings.Contains(b, advisorGone):
t.Errorf("%s: advisor left out without saying so: %s", name, b)
}
if !strings.Contains(b, "Today's date is 2026-10-15.") {
t.Errorf("%s: the system message's own text lost: %s", name, b)
}
}
// A translation has no advisor to offer: the tool goes with every other
// server tool, and Claude Code's instructions to call it are said to be
// void, so the model doesn't call advisor() as one of the agent's tools
// (JackChou on Discord: "找不到对应工具").
func TestAdvisorLeftOutOnTranslation(t *testing.T) {
for _, c := range []struct {
name string
api func(p *provider.Provider, url string)
model string
}{
{"chat", func(p *provider.Provider, url string) { p.Chat = url + "/v1" }, "glm-5.1"},
{"responses", func(p *provider.Provider, url string) { p.Responses = url + "/v1" }, "gpt-5.5"},
{"gemini", func(p *provider.Provider, url string) { p.Gemini = url + "/v1beta" }, "gemini-3.5-pro"},
} {
t.Run(c.name, func(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
u, url := newBillingUpstream(t)
p := provider.Provider{ID: "v", Name: "V", Key: "k", Models: []string{c.model}}
c.api(&p, url)
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
claudeCodeSends(t, advisorTurn("v/"+c.model, "claude-opus-5-5"))
b := u.last(t)
if strings.Contains(b, "advisor_") {
t.Errorf("the advisor server tool sent: %s", b)
}
advisorSaid(t, c.name, b, false)
})
}
}