In Claude Desktop's gateway mode, Claude Code's WebSearch call always went to the session's model (Opus here, 11-19s a search). Claude Code 2.1.321 sends it to its small (haiku-tier) model only when the remote flag tengu_plum_vx3 is on, and Desktop sets DISABLE_GROWTHBOOK=1 in gateway mode, so the flag falls back to off. WebFetch's page summaries already run on the haiku pick (#5534); WebSearch now does too. - desktopSearchCall knows the call by its shape only: tools is exactly one web_search_* server tool named web_search, and tool_choice is {"type":"tool","name":"web_search"}. The prompt is never read. - desktopMoveTo, for a request Claude Code sent in Desktop, puts it on DesktopTiers()["haiku"] when that pick is set, served and not already the model asked, recorded as desktop-search. It doesn't change the session's recorded model (desktopPicked). - A subagent's search taken by the haiku pick is Claude Code's helper, not the subagent's turn: Desktop's subagent model, effort and turn tag leave it (desktopSearchHelper; subagentTurnOf takes the agent, so terminal Claude Code is unchanged). - No haiku pick, or one magpie doesn't serve: nothing changes. - The Routing page says why (desktop-search, en/zh/zh-TW/ja/de), and gateway-routing.md has the rule. Ported from mdwsk88's f68197aa6 (mdwsk88/magpie desktop-websearch-haiku-tier), reviewed line by line. Changed in the port: the gateway-routing.md conflict was merged onto main's text (main's codex-suggestions kept), and the cheap pre-check is bodyHas on `"tool_choice"` and `web_search_` instead of bytes.Contains, since desktop.go no longer imports bytes on main (the patch didn't compile there) and every Desktop request from Claude Code takes this check. web_search_* is kept as a prefix, so a newer search tool version (web_search_20260209) is matched as the same call. Run on mote (go1.26.8). Whole packages last at base659e8850a(alsofefe3f658,05d46f42e,4a1ac8442before; all ok): - go test -count=1 -timeout 45m ./internal/gateway/ ./internal/gui/: ok (gateway 949s, gui 218s at659e8850a). - After rebasing on50c5a52db(backup, touches internal/gui): go test ./internal/gui/ ok, and the focused gateway run below ok. - Focused -run '^(TestDesktop|TestClaudeDesktop|TestSubagentTurn)': ok. - Broken by hand, each red: the search rule removed (old routing) -> TestDesktopWebSearchHaiku, ...KeepsSessionPick, ...Route fail; the Claude Code guard removed -> TestDesktopWebSearchScope fails; the subagent helper exemption removed -> TestDesktopSubagentWebSearchIsHelper and TestDesktopWebSearchRoute fail. - node --test routing-moved-why, i18n-keys, i18n-en-no-cjk (chromium and webkit): 37/38; the one red is i18n-keys "a plugin's words are its own" on ", with an image", red on origin/main and filed as #6877. - gofmt -l: clean. GOOS=linux go vet (-tags nogui and without) and GOOS=windows go vet on ./internal/gateway ./internal/gui: ok. Not tried: a real Claude Desktop session's WebSearch; the reporter's routing log will show it landing on the haiku pick. Co-authored-by: mdwsk88 <11055210+mdwsk88@users.noreply.github.com>
134 lines
5.0 KiB
Go
134 lines
5.0 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// Every Claude Code subagent's turn, Desktop's or a terminal's, moved, kept
|
|
// or neither, keeps the id it was sent with, as the trace's subagentId the
|
|
// Routing page tags. Kind stays empty. The lead's turns, a subagent's
|
|
// tool-less helper calls and a blank id carry none.
|
|
func TestSubagentTurnTagged(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name, key, sub, model, effort string
|
|
tools bool
|
|
want string
|
|
}{
|
|
{"desktop", "claude-desktop", "worker", "", "", true, "worker"},
|
|
{"desktop moved", "claude-desktop", "worker", "fake/m2", "", true, "worker"},
|
|
{"desktop kept", "claude-desktop", "worker", "fake/missing", "", true, "worker"},
|
|
{"desktop effort", "claude-desktop", "worker", "", "high", true, "worker"},
|
|
{"terminal", "claude", "worker", "", "", true, "worker"},
|
|
{"trimmed", "claude", " worker ", "", "", true, "worker"},
|
|
{"lead", "claude-desktop", "", "fake/m2", "high", true, ""},
|
|
{"helper", "claude-desktop", "worker", "fake/m2", "high", false, ""},
|
|
{"terminal helper", "claude", "worker", "", "", false, ""},
|
|
{"blank", "claude-desktop", " ", "fake/m2", "high", true, ""},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
desktopSubagentSetup(t)
|
|
desktopSubagentSet(t, tc.model)
|
|
if err := provider.SetDesktopSubagentEffort(tc.effort); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := New()
|
|
desktopSubagentAsk(t, s, Token+"-"+tc.key, "fake/m1", tc.sub, "/v1/messages", tc.tools)
|
|
r := lastRoute(s)
|
|
if r.SubagentID != tc.want || r.Kind != "" {
|
|
t.Fatalf("subagentId %q kind %q, want %q and no kind", r.SubagentID, r.Kind, tc.want)
|
|
}
|
|
b, err := json.Marshal(r)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := strings.Contains(string(b), `"subagentId":"worker"`); got != (tc.want != "") {
|
|
t.Fatalf("route JSON %s", b)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A call that names a kind keeps its own tag: the subagent id on it adds
|
|
// none.
|
|
func TestSubagentTurnKindFirst(t *testing.T) {
|
|
desktopSubagentSetup(t)
|
|
desktopSubagentSet(t, "fake/m2")
|
|
s := New()
|
|
req := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(
|
|
`{"model":"fake/m1","max_tokens":200,"messages":[{"role":"user","content":"hi"}],"tools":[{"name":"Bash","input_schema":{"type":"object"}}]}`))
|
|
req.Header.Set("x-api-key", Token+"-claude-desktop")
|
|
req.Header.Set("User-Agent", desktopCodeUA)
|
|
req.Header.Set("x-claude-code-agent-id", "worker")
|
|
req.Header.Set("x-openai-subagent", "review")
|
|
rec := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(rec, req)
|
|
if rec.Code != 200 {
|
|
t.Fatalf("%d %s", rec.Code, rec.Body)
|
|
}
|
|
if r := lastRoute(s); r.Kind != "review" || r.SubagentID != "" {
|
|
t.Fatalf("kind %q subagentId %q", r.Kind, r.SubagentID)
|
|
}
|
|
}
|
|
|
|
// A day's history keeps the id, gzipped too, and a route kept before the id
|
|
// was has none.
|
|
func TestHistoryKeepsSubagentID(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
now := historyNoon(t)
|
|
saveRoute(Route{ID: 1, Time: now, Agent: "claude-desktop", SubagentID: "worker", Done: true})
|
|
saveRoute(Route{ID: 2, Time: now, Agent: "claude-desktop", Done: true})
|
|
pruneHistory(HistoryDir(), now.AddDate(0, 0, 1))
|
|
_, routes, _ := History(now.Format(dayForm))
|
|
if len(routes) != 2 || routes[0].SubagentID != "worker" || routes[1].SubagentID != "" {
|
|
t.Fatalf("history: %+v", routes)
|
|
}
|
|
}
|
|
|
|
// A turn a Desktop pick or effort found is trusted, and a request with no
|
|
// agent id is left alone, so neither decodes the body: past reading the
|
|
// headers, the tag allocates nothing.
|
|
func TestSubagentTurnDoesNotDecode(t *testing.T) {
|
|
tools := []byte(`{"tools":[{"name":"Bash"}]}`)
|
|
lead := httptest.NewRequest("POST", "/v1/messages", nil)
|
|
sub := httptest.NewRequest("POST", "/v1/messages", nil)
|
|
sub.Header.Set("x-claude-code-agent-id", "worker")
|
|
for _, tc := range []struct {
|
|
name string
|
|
r *http.Request
|
|
body []byte
|
|
known bool
|
|
want string
|
|
}{
|
|
{"lead", lead, tools, false, ""},
|
|
{"known", sub, tools, true, "worker"},
|
|
// trusted without a look at the body
|
|
{"known without tools", sub, []byte(`{}`), true, "worker"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
headers := testing.AllocsPerRun(10, func() { subagentOf(tc.r.Header) })
|
|
allocs := testing.AllocsPerRun(10, func() {
|
|
if got := subagentTurnOf(tc.r, "claude-desktop", "", tc.body, tc.known); got != tc.want {
|
|
t.Fatalf("got %q, want %q", got, tc.want)
|
|
}
|
|
})
|
|
if allocs > headers {
|
|
t.Fatalf("allocated %g times, reading the headers %g", allocs, headers)
|
|
}
|
|
})
|
|
}
|
|
// the decode the cases above leave out shows in the count
|
|
if got := subagentTurnOf(sub, "claude-desktop", "", tools, false); got != "worker" {
|
|
t.Fatalf("unknown turn with tools: %q", got)
|
|
}
|
|
headers := testing.AllocsPerRun(10, func() { subagentOf(sub.Header) })
|
|
if n := testing.AllocsPerRun(10, func() { subagentTurnOf(sub, "claude-desktop", "", tools, false) }); n <= headers {
|
|
t.Fatalf("reading an unknown turn's tools allocated %g times, its headers %g", n, headers)
|
|
}
|
|
}
|