Files
magpie/internal/gateway/desktop_subagent.go
T
Haogao Gu d966e19154 gateway, gui: the Routing page tags a Claude Code subagent's turn as it does Codex's
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, on b9c589eb, 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. On b9c589eb
  untouched (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.
2026-10-15 03:34:59 +08:00

87 lines
3.4 KiB
Go

package gateway
import (
"net/http"
"github.com/yetone/magpie/internal/provider"
)
// desktopSubagentPick is what is done with the model of a Claude Desktop
// request: nil when it isn't a subagent's tool turn or no model is set
// for Desktop's subagents (provider.DesktopSubagentModel), so it goes on
// as asked. Desktop's Claude Code marks a subagent's requests with
// x-claude-code-agent-id (subagentOf), and its lead's never. A subagent
// also sends tool-less helper calls of Claude Code's own on the haiku
// tier's model, which stay where they are: only a request that offers
// tools is the subagent's turn. named is the catalog id the request asked
// for, which the trace and the ledger keep; resolved is where it would go
// now, after desktopTurn, the tiers and the groups, so a turn already on
// the model set isn't marked as moved. The model set must still be
// enabled, served by its exact id and allowed to the gateway key; when it
// isn't, the request goes on as asked and the pick says why. The cheap
// checks come first, so an unset model costs a held settings read.
func desktopSubagentPick(r *http.Request, agent, named, resolved string, body []byte) *SubagentPick {
to := provider.DesktopSubagentModel()
if to == "" || agent != "claude-desktop" || to == named || to == resolved {
return nil
}
if sub, _ := subagentOf(r.Header); sub == "" {
return nil
}
if !hasTools(body) {
return nil
}
pick := &SubagentPick{Asked: named, To: to}
if _, off := idOff(to); off {
pick.Kept = "it is disabled in magpie"
return pick
}
if why := provider.DesktopSubagentUnserved(to); why != "" {
pick.Kept = why
return pick
}
// a key held to some models (#882) reads as Desktop only with
// Desktop's own client; its Code tab with a named key is Claude Code
if who, held := keyHolds(r); held {
g, ms, isGroup := provider.FindGroup(to)
p, model, ok := provider.Resolve(to)
if isGroup && !groupAllowed(who, g, ms) || !isGroup && (!ok || !modelAllowed(who, p, model)) {
pick.Kept = "the gateway key may not use it"
}
}
return pick
}
// desktopSubagentEffort is independent of a model replacement: effort-only
// picks, an unchanged model and a kept model all take it. A non-nil Desktop
// model pick already checked the agent id and tools, so the body isn't decoded
// twice. With no effort set, even that check is left out.
func desktopSubagentEffort(r *http.Request, agent string, body []byte, pick *SubagentPick) string {
level := provider.DesktopSubagentEffort()
if level == "" || agent != "claude-desktop" {
return ""
}
if pick == nil {
if sub, _ := subagentOf(r.Header); sub == "" || !hasTools(body) {
return ""
}
}
return level
}
// subagentTurnOf is the Claude Code subagent whose turn r is, for the
// Routing page's tag (Route.SubagentID): its x-claude-code-agent-id on a
// request that offers tools and names no kind, the turn
// desktopSubagentPick and desktopSubagentEffort take. Claude Code sends the
// id from a terminal as from Desktop, so both are tagged; a subagent's
// tool-less helper calls aren't its turns. known is a Desktop pick or effort
// that found the turn already, so the body isn't decoded again, and a
// request without the id isn't decoded at all.
func subagentTurnOf(r *http.Request, kind string, body []byte, known bool) string {
sub, _ := subagentOf(r.Header)
if sub == "" || kind != "" || !known && !hasTools(body) {
return ""
}
return sub
}