Codex names a subagent's request (x-openai-subagent: collab_spawn), and the Routing page tags its row Subagent, with why it went where it did. Claude Code names no kind, so Claude Desktop's subagent turns, which magpie now moves to their own model and effort, read as the lead's: a Desktop session's rows on one routing group could not be told apart. Claude Code 2.1.295 sends x-claude-code-agent-id from every agent context but the main one. Its x-claude-code-request-class and x-claude-code-agent-type hints go only to a first-party base URL or with CLAUDE_CODE_GATEWAY_HINT_HEADERS, so magpie doesn't get them. - subagentTurnOf keeps the id in Route.SubagentID for a request that offers tools and names no kind: the turn desktopSubagentPick and desktopSubagentEffort take. Desktop's and terminal Claude Code's turns are kept, moved or not. The lead's turns, a subagent's tool-less helper calls (#2081: Claude Code's housekeeping) and a blank id are not. A kind wins over the id. - A Desktop pick or effort that found the turn is trusted, so the body isn't decoded again; a request without the id isn't decoded at all. Only a subagent's turn with neither setting reads its tools once. - Kind stays empty (#2081), so the ledger, the Usage page and the Purpose filter keep the turn unmarked. The ledger's Subagent already keeps the same id, on the helper calls too. - routing.js tags it through tagKind in the request list (by request and by session), the story, the replay and the tray panel, with Codex's words and reasons (kindWhy's collab_spawn: moved or kept, within a routing group, on the model asked for). No new strings. - The day's history keeps the id. A route kept before it has no tag, and its moved/kept story reads as before. Verified at this head, onb9c589eb, under a temporary HOME with Go's caches pinned: - Each new test fails with its guarantee broken by hand (a real FAIL, not a build error), then passes restored. Go, 10: the id never set; tools, kind or the trusted pick ignored; tools decoded before the header (the lead's request then allocates 10 times, 1 for its headers); the route not given the id; Desktop only (terminal fails); every request trusted (the helpers fail); the JSON key renamed; the field left out of the history. GUI, 8, each 0 pass / 4 fail (Chromium and WebKit, 1100 and 420, zh) at its own assertion: tagKind ignoring the id; the list, the story, the replay or the panel testing Kind alone; kindWhy's collab_spawn reasons on Kind alone; the id over a kind; the Purpose filter following the tag. - go test -tags nogui ./...: 53 packages ok. - go test -race -count=20 -run '^(TestSubagentTurn|TestHistoryKeepsSubagentID|TestDesktopSubagent)' ./internal/gateway: ok. - gofmt -l: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./..., go build, GOOS=linux go build -tags nogui, GOOS=windows go build: exit 0. - claude-subagent-tag.test.cjs: 20/20 (Chromium and WebKit; en, zh, zh-TW, ja, de; 1100 and 420), three runs in a row. - Desktop-bundled Claude Code 2.1.295 through the built gateway, a temporary HOME, a loopback-only sandbox and local fixture replies, its Agent tool spawning a real subagent: with nothing set, with a subagent model, and as terminal Claude Code, each run kept 4 routes; the subagent's one turn has the id the client sent upstream, the lead's 3 requests none, Kind empty; the model pick moved it to m2. No tool-less helper call came in these runs; the Go tests cover them. - The Routing GUI tests around it, 91 files (every routing-*, the tray panel's two, purpose-filter, click-scroll, the Codex and Desktop subagent files, subagent-effort, search-instead, search-mcp, vision-mcp, remote-group-served, reasoning-speed, usage-ledger, sessions-overview and the new one), six files at a time: Chromium 602/617, WebKit 572/617. Each failure fails without this commit too. Onb9c589ebuntouched (checked for the effort commit): routing-days-fit, routing-failed-open and routing-side-calls in both engines (their position checks against #2134's at-top rule), routing-wb-restricted (#2115's wheel), and at 420 in WebKit routing-agent-gone, routing-agent-hue-not-red, routing-rest-off and routing-held-member. On 6926b7ce: search-instead in WebKit, 5 of 5 alone, and routing-context-fold's picked request in WebKit, a row out of view, 2 of 5 runs alone there and 3 of 5 here. Not run: the whole GUI suite, a real Claude Desktop window or Cowork, Linux and Windows machines (GOOS vet and build only), WebKitGTK.
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, "", 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, "", 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, "", tools, false) }); n <= headers {
|
|
t.Fatalf("reading an unknown turn's tools allocated %g times, its headers %g", n, headers)
|
|
}
|
|
}
|