Files
magpie/internal/usage/purpose.go
T
yetone 81c666e1c2 gateway: an advisor call Anthropic's server ran is a usage row of its own (#6977)
A Claude Code turn relayed on Messages with its advisor on has the advisor
run at the vendor. The reply's top-level usage is the executor's alone;
each advisor call is an advisor_message entry of usage.iterations, billed
at the advisor model's rates (Anthropic's advisor tool page, "Usage and
billing"). magpie read only the top level, so an Opus advisor beside a
Sonnet executor was in no row and priced nowhere.

aUsage reads iterations, whole and from a stream's message_delta (whose
list is cumulative, so the latest stands for the reply). Each advisor call
becomes its own row (advisorRows, written by appendAdvised at every row of
a try that reached a provider, and on the native route): under the
advisor's model by magpie's id (advisorKeyModel of the request's advisor
tool), Served as the vendor names it, Kind "advisor" (named Advisor in
Usage's filters, already translated), the request's route, agent, account
and session, status 200, counted as its own request as magpie's own
advisor calls already are. The executor's row is unchanged. Routing prices
the calls too, before the request's own usage so the prompt is still read
from the last entry. Iterations with no advisor entry (Copilot) leave
Usage as it was.

The fixture is Anthropic's documented example word for word; no real
reply with an advisor call was captured.

The core script budget is raised by 52, the Advisor entry in the
generated purposes.js (the base had no room; 1 byte less is red). The
kinds are read wherever Usage and Routing draw a row, so there is no page
to move it to.

Run at this commit's tree on f1b0663f1, then on b1f044fc1 (which changed
core scripts only, i18n.js and syncfiles.js, so internal/gui and the GUI
files were run again there):
- mica: gofmt -l (nothing), go vet (darwin), GOOS=linux go vet -tags nogui,
  GOOS=windows go vet for internal/gateway, internal/usage, internal/gui: ok.
- mica: go test -timeout 45m -count=1 ./internal/gateway ./internal/usage
  ./internal/gui: ok (916s, 64s, 161s).
- mote: node --test purpose-filter, routing-purpose-state, usage-big-write,
  usage-ledger-mcp, usage-ledger-search, i18n-keys (Chromium and WebKit):
  74 of 74 pass.
- On b1f044fc1, the pushed base: mica go test -timeout 45m -count=1
  ./internal/gui ok (147s); mote the same six GUI files, 74 of 74 pass.
- Before the rebase, at ae94571d7, the same packages on mote were ok
  except TestCoreScriptBudget, which this commit's budget line answers.

Broken by hand:
- aUsage.usage never setting advised: TestAdvisorIterationsRead red
  ("stream=false: advisor calls <nil>, want one"), TestAdvisorCallHasItsOwnRow
  red in both subtests ("advisor rows: []").
- passthrough without advisedAs: TestAdvisorCallHasItsOwnRow red in both
  subtests, the row named claude-opus-5-5 and not magpie's claude-opus-5.5.

Not tried: a real Anthropic reply with an advisor call (no account with the
advisor beta at hand); Bedrock and Vertex passthrough with an advisor
(they share the sniffer, not run); the Usage page with an advisor row in a
real browser session beyond the GUI files above.

Reported by Refeccd on Discord.
2026-11-18 00:38:36 +08:00

82 lines
3.4 KiB
Go

package usage
import "maps"
//go:generate go run ../gui/genpurposes -out ../gui/assets/purposes.js
// PurposeKind is the shared filtering key and untranslated display name of a kind.
type PurposeKind struct {
Purpose string `json:"purpose"`
Name string `json:"name"`
}
var purposeKinds = func() map[string]PurposeKind {
out := map[string]PurposeKind{}
add := func(name string, kinds ...string) {
for _, kind := range kinds {
out[kind] = PurposeKind{"kind:" + kinds[0], name}
}
}
add("Approval check", "guardian", "auto_review", "guardian_review")
add("Review", "review")
add("Compaction", "compact")
add("Memory", "memory_consolidation", "memgen", "memory")
add("Title", "thread_title", "thread_title_reconsideration", "title_generation", "title")
add("Description", "thread_description")
add("Prediction", "composer_predictions")
add("Subagent", "collab_spawn", "thread_spawn", "agent_job")
add("Luna Reserve", "luna_reserve")
add("Suggestions", "ambient_suggestions", "ambient_suggestion_safety")
add("Web search", "web_search")
add("Image description", "vision")
add("Routing classifier", "routing")
add("Connection check", "connection_check") // Claude Desktop's (gateway.KindDesktopCheck)
add("Test", "provider_test") // magpie's own (probelog.KindTest)
add("Detect APIs", "api_detect") // probelog.KindDetect
add("Model list", "model_list") // probelog.KindList
add("MCP tool", KindMCP) // an MCP tool call (SourceMCP)
add("Cache keep-alive", KindKeepalive) // a plugin's own ping (plugin_calls.go)
add("Advisor", KindAdvisor) // an advisor call its vendor ran (gateway's advisorRows)
// Claude Code names neither of these requests, so the gateway tells them
// by their content: the *_inferred kinds. They share the purpose of the
// plain kind (a client's own marker, were one sent), and say in their
// name that magpie read it from the body, not that the client said so.
pair := func(name, kind string) {
out[kind] = PurposeKind{"kind:" + kind, name}
out[kind+InferredSuffix] = PurposeKind{"kind:" + kind, name + " (inferred)"}
}
pair("Progress summary", KindAgentSummary) // Claude Code's AgentSummary
pair("Auto mode check", KindAutoMode) // Claude Code's auto mode classifier
return out
}()
// KindAgentSummary and KindAutoMode are the kinds of Claude Code's
// sub-agent progress summary and its auto mode classifier; InferredSuffix
// follows one that was told by the request's content.
const (
// KindAdvisor is the kind of an advisor call's row: one Anthropic's
// server ran for a request's advisor tool, its usage listed in the
// reply's iterations (#6977)
KindAdvisor = "advisor"
KindAgentSummary = "agent_summary"
KindAutoMode = "auto_mode_check"
InferredSuffix = "_inferred"
)
// PurposeKinds returns a copy of the catalog used to generate the UI's catalog.
// Add aliases here, then run go generate ./internal/usage from the repo root.
func PurposeKinds() map[string]PurposeKind { return maps.Clone(purposeKinds) }
// PurposeOf groups the agent's equivalent call-kind spellings for filtering.
// The recorded Kind stays untouched. Prefixing nonempty kinds keeps future
// names distinct from the unmarked bucket (which is not necessarily a chat).
func PurposeOf(kind string) string {
if kind == "" {
return "unmarked"
}
if p, ok := purposeKinds[kind]; ok {
return p.Purpose
}
return "kind:" + kind
}