Files
magpie/internal/gateway/advisor.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

277 lines
8.7 KiB
Go

package gateway
import (
"encoding/json"
"strings"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/provider"
)
// advisorTools fits Claude Code's advisor (/advisor, --advisor) to the
// upstream a Messages request goes to as it is (passthrough). Claude Code
// declares it as Anthropic's server tool, {"type": "advisor_20260301",
// "name": "advisor", "model": …}, and names the advisor's model as it
// names its own: on magpie, by magpie's id ("anthropic/claude-opus-5-5",
// or the tier an alias like opus is on), which no vendor knows, so the
// whole request was turned away (JackChou on Discord).
//
// The advisor runs at the vendor, on the same account: its model goes as
// that upstream's own name for it when it is p's, as the request's own
// model does (wires). An advisor magpie can't give that upstream (another
// provider's model, or not a Claude model) is left out, and so is every
// advisor when the request's model is not Claude's (a vendor's Messages
// API knows no such tool): the turn is answered without one, as a model
// that isn't on magpie would be. What Claude Code put in the conversation
// for an advisor left out goes with it (withoutAdvisor). allowed says
// whether the calling key may use one of p's models (keyAllows): the
// advisor's model is the client's to name, and the vendor runs it on the
// user's account, so a key held to some models is held here too.
func advisorTools(body []byte, p provider.Provider, upstream string, wires map[string]string, allowed func(model string) bool) []byte {
if !bodyHas(body, `"advisor_`) {
return body
}
tools := gjson.GetBytes(body, "tools")
if !tools.IsArray() {
return body
}
var top map[string]json.RawMessage
var list []map[string]json.RawMessage
if json.Unmarshal(body, &top) != nil || json.Unmarshal([]byte(tools.Raw), &list) != nil {
return body
}
claude := anthropicModel.MatchString(upstream)
changed := false
gone := map[string]bool{}
kept := list[:0]
for _, t := range list {
var typ, model string
json.Unmarshal(t["type"], &typ)
if !strings.HasPrefix(typ, "advisor_") {
kept = append(kept, t)
continue
}
json.Unmarshal(t["model"], &model)
name, ok := advisorModel(p, model, wires)
if ok && allowed != nil && !allowed(advisorKeyModel(model)) {
ok = false
}
if !claude || !ok {
var n string
json.Unmarshal(t["name"], &n)
gone[n] = true
changed = true
continue
}
if name != model {
t["model"], _ = json.Marshal(name)
changed = true
}
kept = append(kept, t)
}
if !changed {
return body
}
if len(kept) == 0 {
delete(top, "tools")
delete(top, "tool_choice")
} else {
top["tools"], _ = json.Marshal(kept)
}
if m, ok := withoutAdvisor(top["messages"], gone); ok {
top["messages"] = m
}
out, err := json.Marshal(top)
if err != nil {
return body
}
return out
}
// advisorID is the advisor model Claude Code names, taken to the id magpie
// serves as a request's own model is (validateModel): Claude Code is given
// a dotted Claude id with dashes (provider.ClaudeSpelled) and writes
// settings.json's advisorModel so, and it sends that id as written, its
// [1m] or [2m] mark dropped. Taken as it was, relay/claude-fable-5-2 was no
// model of a provider that lists claude-fable-5.2, so it went upstream as
// claude-fable-5-2 beside the request's claude-opus-5.5, a name the relay
// doesn't know (Refeccd on Discord).
func advisorID(model string) string {
model = strings.TrimSpace(model)
model = strings.TrimSuffix(strings.TrimSuffix(model, "[1m]"), "[2m]")
if model == "" {
return ""
}
if real, ok := provider.ClaudeUndashed(model); ok {
return real
}
return provider.CanonicalRef(unflat(model))
}
// advisorKeyModel is the model id a key's models are checked against for
// an advisor named model: magpie's model id when it names one, else the
// bare id as it was sent.
func advisorKeyModel(model string) string {
model = advisorID(model)
if _, m, ok := provider.Resolve(model); ok && !provider.IsGroupRef(model) {
return m
}
return model
}
// advisorModel is the name p's upstream knows the advisor model by, and
// whether p can run it: one of p's models, by magpie's id, or a Claude
// model's own id that names no provider.
func advisorModel(p provider.Provider, model string, wires map[string]string) (string, bool) {
model = advisorID(model)
if model == "" {
return "", false
}
if q, m, ok := provider.Resolve(model); ok && !provider.IsGroupRef(model) {
if q.ID != p.ID {
return "", false
}
name := provider.UpstreamNameIn(wires, p.ID, m)
return name, anthropicModel.MatchString(name)
}
if strings.Contains(model, "/") {
return "", false
}
return model, anthropicModel.MatchString(model)
}
// advisorNote opens the instructions Claude Code puts in the conversation
// when its advisor is on ("# Advisor Tool\n\nYou have access to an
// `advisor` tool … Call advisor BEFORE substantive work"), at the end of a
// {role: "system"} message's text (2.1.300), and advisorGone is what Claude
// Code itself says there once the advisor is turned off.
const (
advisorNote = "# Advisor Tool\n"
advisorGone = "The advisor tool is no longer available; disregard the earlier advisor instructions."
)
// advisorGoneIn is text with the advisor's instructions, up to the end of
// the reminder they are in or of the text, said to be void as Claude Code
// says it, and whether it held them.
func advisorGoneIn(text string) (string, bool) {
i := strings.Index(text, advisorNote)
if i < 0 {
return text, false
}
end := len(text)
if j := strings.Index(text[i:], "</system-reminder>"); j >= 0 {
end = i + j
}
note := text[i:end]
trail := note[len(strings.TrimRight(note, " \t\r\n")):]
return text[:i] + advisorGone + trail + text[end:], true
}
// advisorCut is text without the advisor's instructions, up to the end of
// the reminder they are in or of the text, and whether it held them: the
// advisor model is asked with the executor's conversation (adviceFor), and
// the executor's instructions to call it, or advisorGone in their place,
// had it answer as the executor that it has no advisor (#6804).
func advisorCut(text string) (string, bool) {
i := strings.Index(text, advisorNote)
if i < 0 {
return text, false
}
end := len(text)
if j := strings.Index(text[i:], "</system-reminder>"); j >= 0 {
end = i + j
}
return strings.TrimRight(text[:i], " \t\r\n") + text[end:], true
}
// withoutAdvisor is a Messages request's messages without what Claude Code
// put there for an advisor tool that is left out, named in gone: the
// tool_addition (or tool_removal, tool_reference) naming it, and a message
// that leaves empty; and its instructions, said to be void (advisorGoneIn).
// Left there, the reference named a tool the request no longer has, and
// the instructions had the model call advisor() as one of the agent's own
// tools, which Claude Code answers "No such tool available: advisor"
// (JackChou on Discord).
func withoutAdvisor(raw json.RawMessage, gone map[string]bool) (json.RawMessage, bool) {
if len(gone) == 0 || !bodyHas(raw, "dvisor") {
return raw, false
}
var msgs []map[string]json.RawMessage
if json.Unmarshal(raw, &msgs) != nil {
return raw, false
}
changed := false
out := msgs[:0]
for _, m := range msgs {
var s string
if json.Unmarshal(m["content"], &s) == nil {
if t, ok := advisorGoneIn(s); ok {
m["content"], _ = marshalPlain(t)
changed = true
}
out = append(out, m)
continue
}
var blocks []map[string]json.RawMessage
if json.Unmarshal(m["content"], &blocks) != nil {
out = append(out, m)
continue
}
moved := false
kept := blocks[:0]
for _, b := range blocks {
var typ string
json.Unmarshal(b["type"], &typ)
if deferredBlock(typ) && gone[advisorRefName(b)] {
moved = true
continue
}
var text string
if typ == "text" && json.Unmarshal(b["text"], &text) == nil {
if t, ok := advisorGoneIn(text); ok {
b["text"], _ = marshalPlain(t)
moved = true
}
}
kept = append(kept, b)
}
if !moved {
out = append(out, m)
continue
}
changed = true
if len(kept) == 0 {
continue // it said only that the advisor came or went
}
m["content"], _ = marshalPlain(kept)
out = append(out, m)
}
if !changed {
return raw, false
}
b, err := marshalPlain(out)
if err != nil {
return raw, false
}
return b, true
}
// advisorRefName is the tool a deferred-tool block names: a tool_addition's
// or tool_removal's tool, or a tool_reference's own name.
func advisorRefName(b map[string]json.RawMessage) string {
var n string
if t, ok := b["tool"]; ok {
var tool map[string]json.RawMessage
if json.Unmarshal(t, &tool) == nil {
json.Unmarshal(tool["name"], &n)
}
return n
}
if json.Unmarshal(b["name"], &n) == nil && n != "" {
return n
}
json.Unmarshal(b["tool_name"], &n)
return n
}