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.
233 lines
9.1 KiB
Go
233 lines
9.1 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// pluginMarksBody is a non-stream Messages ask of the shape Claude Code's
|
|
// auto mode classifier sends a plugin account (#4571): a system block, a
|
|
// transcript of history blocks and the current operation, each marked with
|
|
// cache_control of the client's own, an hour long. The mark on the last
|
|
// history block is the boundary that moves on as the history grows.
|
|
const pluginMarksBody = `{"model":"m1","stream":false,"max_tokens":64,
|
|
"system":[{"type":"text","text":"fixed system","cache_control":{"type":"ephemeral","ttl":"1h"}}],
|
|
"messages":[{"role":"user","content":[
|
|
{"type":"text","text":"<transcript>"},
|
|
{"type":"text","text":"history one"},
|
|
{"type":"text","text":"history two","cache_control":{"type":"ephemeral","ttl":"1h"}},
|
|
{"type":"text","text":"current operation","cache_control":{"type":"ephemeral","ttl":"1h"}}]}]}`
|
|
|
|
// pluginMessagesReply is an Anthropic upstream's streamed reply.
|
|
const pluginMessagesReply = `event: message_start
|
|
data: {"type":"message_start","message":{"id":"msg_1","type":"message","role":"assistant","model":"m1","content":[],"usage":{"input_tokens":13,"output_tokens":0}}}
|
|
|
|
event: content_block_start
|
|
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}
|
|
|
|
event: content_block_delta
|
|
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"ok"}}
|
|
|
|
event: content_block_stop
|
|
data: {"type":"content_block_stop","index":0}
|
|
|
|
event: message_delta
|
|
data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":1}}
|
|
|
|
event: message_stop
|
|
data: {"type":"message_stop"}
|
|
`
|
|
|
|
// pluginFake points a plugin account that only streams at a fake upstream,
|
|
// the shape #4571 reports: its whole-body Messages asks are translated,
|
|
// asked streamed and assembled into one Message JSON for the client.
|
|
func pluginFake(t *testing.T) (*fake, *httptest.Server) {
|
|
t.Helper()
|
|
f := &fake{t: t, reply: pluginMessagesReply}
|
|
up := httptest.NewServer(f)
|
|
t.Cleanup(up.Close)
|
|
return f, up
|
|
}
|
|
|
|
func pluginTranslate(t *testing.T, f *fake, up *httptest.Server, body string) (int, string, []byte, []byte) {
|
|
t.Helper()
|
|
s := New()
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest(http.MethodPost, "/v1/messages", nil)
|
|
p := provider.Provider{ID: "fake", Name: "Factory", Key: "k", Models: []string{"m1"},
|
|
Anthropic: up.URL, Account: &provider.Account{Agent: "plugin", User: "u", Stream: true}}
|
|
var u Usage
|
|
status, msg := s.translate(w, r, p, provider.Anthropic, provider.Anthropic, "m1", []byte(body), &u)
|
|
return status, msg, w.Body.Bytes(), f.got
|
|
}
|
|
|
|
// A translation keeps the client's cache marks where it put them, ttl and
|
|
// all (#4571): the boundary it marked last turn stands at the same block
|
|
// again, so a turn whose history only grew reads that write instead of
|
|
// writing the whole history again, for the hours it asked for.
|
|
func TestPluginWholeAskedKeepsTheClientCacheMarks(t *testing.T) {
|
|
f, up := pluginFake(t)
|
|
code, msg, answered, asked := pluginTranslate(t, f, up, pluginMarksBody)
|
|
if code != 200 {
|
|
t.Fatalf("%d: %s", code, msg)
|
|
}
|
|
// the client asked for one JSON body and is told one
|
|
var out struct {
|
|
Type string `json:"type"`
|
|
Content []struct {
|
|
Type string `json:"type"`
|
|
Text string `json:"text"`
|
|
} `json:"content"`
|
|
}
|
|
if err := json.Unmarshal(answered, &out); err != nil || out.Type != "message" ||
|
|
len(out.Content) != 1 || out.Content[0].Text != "ok" {
|
|
t.Fatalf("answer %s (%v)", answered, err)
|
|
}
|
|
// the upstream is asked streamed, whatever the client asked for
|
|
if !bytes.Contains(asked, []byte(`"stream":true`)) {
|
|
t.Fatalf("asked %s", asked)
|
|
}
|
|
var wire struct {
|
|
System []map[string]any `json:"system"`
|
|
Messages []struct {
|
|
Content []map[string]any `json:"content"`
|
|
} `json:"messages"`
|
|
}
|
|
if err := json.Unmarshal(asked, &wire); err != nil {
|
|
t.Fatalf("asked %s (%v)", asked, err)
|
|
}
|
|
mark := map[string]any{"type": "ephemeral", "ttl": "1h"}
|
|
if len(wire.System) != 1 || wire.System[0]["text"] != "fixed system" {
|
|
t.Fatalf("system %v, want the client's block", wire.System)
|
|
}
|
|
// the client's blocks are the asked ones, in the order it sent them
|
|
if len(wire.Messages) != 1 || len(wire.Messages[0].Content) != 4 {
|
|
t.Fatalf("messages %v, want the client's four blocks", wire.Messages)
|
|
}
|
|
wantTexts := []string{"<transcript>", "history one", "history two", "current operation"}
|
|
for i, want := range wantTexts {
|
|
if wire.Messages[0].Content[i]["text"] != want {
|
|
t.Errorf("block %d is %v, want %q", i, wire.Messages[0].Content[i], want)
|
|
}
|
|
}
|
|
for _, i := range []int{0, 1} {
|
|
if cc := wire.Messages[0].Content[i]["cache_control"]; cc != nil {
|
|
t.Errorf("block %d the client left unmarked is marked %v", i, cc)
|
|
}
|
|
}
|
|
if cc := wire.Messages[0].Content[2]["cache_control"]; !sameMark(cc, mark) {
|
|
t.Errorf("the history boundary is marked %v, want %v: %s", cc, mark, asked)
|
|
}
|
|
if cc := wire.Messages[0].Content[3]["cache_control"]; !sameMark(cc, mark) {
|
|
t.Errorf("the tail is marked %v, want %v", cc, mark)
|
|
}
|
|
if cc := wire.System[0]["cache_control"]; !sameMark(cc, mark) {
|
|
t.Errorf("the system is marked %v, want %v", cc, mark)
|
|
}
|
|
}
|
|
|
|
func sameMark(got any, want map[string]any) bool {
|
|
m, ok := got.(map[string]any)
|
|
if !ok {
|
|
return false
|
|
}
|
|
return m["type"] == want["type"] && m["ttl"] == want["ttl"]
|
|
}
|
|
|
|
// A request that only mentions cache_control in its text marks nothing of
|
|
// its own: the rebuild marks it the way it marked requests before
|
|
// (#4571), rather than reading a mark set out of words.
|
|
func TestPluginWholeAskedMentioningMarksIsMarkedAsBefore(t *testing.T) {
|
|
body := `{"model":"m1","stream":false,"max_tokens":64,
|
|
"system":[{"type":"text","text":"fixed system"}],
|
|
"messages":[{"role":"user","content":[
|
|
{"type":"text","text":"what does cache_control do?"}]}]}`
|
|
f, up := pluginFake(t)
|
|
code, msg, _, asked := pluginTranslate(t, f, up, body)
|
|
if code != 200 {
|
|
t.Fatalf("%d: %s", code, msg)
|
|
}
|
|
var wire struct {
|
|
System []map[string]any `json:"system"`
|
|
Messages []struct {
|
|
Content []map[string]any `json:"content"`
|
|
} `json:"messages"`
|
|
}
|
|
if err := json.Unmarshal(asked, &wire); err != nil {
|
|
t.Fatalf("asked %s (%v)", asked, err)
|
|
}
|
|
last := wire.Messages[len(wire.Messages)-1].Content
|
|
if cc, ok := last[len(last)-1]["cache_control"].(map[string]any); !ok || cc["ttl"] != nil {
|
|
t.Errorf("the tail is %v, want magpie's five-minute mark", last[len(last)-1]["cache_control"])
|
|
}
|
|
if len(wire.System) != 1 {
|
|
t.Fatalf("system %v", wire.System)
|
|
}
|
|
if cc, ok := wire.System[0]["cache_control"].(map[string]any); !ok || cc["ttl"] != nil {
|
|
t.Errorf("the system is %v, want magpie's five-minute mark", wire.System[0]["cache_control"])
|
|
}
|
|
}
|
|
|
|
// A set of marks no Messages request may carry (an hour after five
|
|
// minutes) goes the default way, its marks dropped as before, not refused
|
|
// with a 400 the old translation never gave (#3835).
|
|
func TestPluginWholeAskedServesBadMarksAsBefore(t *testing.T) {
|
|
bad := bytes.Replace([]byte(pluginMarksBody),
|
|
[]byte(`"system":[{"type":"text","text":"fixed system","cache_control":{"type":"ephemeral","ttl":"1h"}}]`),
|
|
[]byte(`"system":[{"type":"text","text":"fixed system","cache_control":{"type":"ephemeral","ttl":"5m"}}]`), 1)
|
|
if !bytes.Contains(bad, []byte(`"ttl":"5m"`)) {
|
|
t.Fatal("fixture: no system mark turned to 5m")
|
|
}
|
|
f, up := pluginFake(t)
|
|
code, msg, _, asked := pluginTranslate(t, f, up, string(bad))
|
|
if code != 200 {
|
|
t.Fatalf("%d: %s", code, msg)
|
|
}
|
|
var wire struct {
|
|
Messages []struct {
|
|
Content []map[string]any `json:"content"`
|
|
} `json:"messages"`
|
|
}
|
|
if err := json.Unmarshal(asked, &wire); err != nil {
|
|
t.Fatalf("asked %s (%v)", asked, err)
|
|
}
|
|
last := wire.Messages[len(wire.Messages)-1].Content
|
|
for i, b := range last {
|
|
if m, ok := b["cache_control"].(map[string]any); ok && m["ttl"] == "1h" {
|
|
t.Errorf("block %d keeps a mark of a set the checks refuse: %v", i, m)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A Messages ask read with its client's marks (parseTranslated) is
|
|
// finished like any other parse: a call id that carries a Gemini
|
|
// thought signature, from a turn a Gemini model answered, goes to the
|
|
// Anthropic upstream as the call's own id, its result answering it.
|
|
func TestTranslatedWithMarksTakesSignaturesOffCallIDs(t *testing.T) {
|
|
id := signedID("toolu_1", "c2lnbmF0dXJl")
|
|
if id == "toolu_1" {
|
|
t.Fatal("signedID left the id unsigned")
|
|
}
|
|
q, _ := json.Marshal(id)
|
|
body := []byte(`{"model":"m1","max_tokens":64,"messages":[
|
|
{"role":"user","content":[{"type":"text","text":"go"}]},
|
|
{"role":"assistant","content":[{"type":"tool_use","id":` + string(q) + `,"name":"Bash","input":{}}]},
|
|
{"role":"user","content":[{"type":"tool_result","tool_use_id":` + string(q) + `,"content":"ok","cache_control":{"type":"ephemeral","ttl":"1h"}}]}]}`)
|
|
req, err := parseTranslated(provider.Anthropic, provider.Anthropic, body, false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if req.clientCache == nil || req.clientCache.tail != "1h" {
|
|
t.Fatalf("not read with its marks: %+v", req.clientCache)
|
|
}
|
|
call, result := req.Messages[1].Parts[0], req.Messages[2].Parts[0]
|
|
if call.ID != "toolu_1" || result.CallID != "toolu_1" {
|
|
t.Fatalf("call %q, result %q: want toolu_1 for both", call.ID, result.CallID)
|
|
}
|
|
}
|