Files
magpie/internal/sessions/subagent_test.go
T
yetone b6a263b9fd gateway, usage: the Usage page's numbers are read over the gateway, and a Claude Code subagent's call says which subagent and which started it (HoGee_xxl on X)
HoGee_xxl wants their own usage app to read magpie over the gateway,
which had no /api/usage counterpart, and asked for subagents' parent ids.

- GET /v1/magpie/usage (the /api/usage summary, ?period=) and
  GET /v1/magpie/usage/requests (the /api/usage/requests ledger page, same
  filters, offset/limit) on the gateway. The builders moved out of gui into
  internal/usageapi so both serve the same JSON; gui keeps aliases and sets
  usageapi.Clients for agent names (gateway can't import agent). Guard as
  /v1/magpie/quotas: this machine, or another with an enabled gateway key,
  behind the same Host check. A key held to a budget, models or accounts is
  refused the summary (403) and gets the ledger filtered to its own
  CallerKey, with callerKeys/agents/providers/accounts cut to its own rows
  (usageapi.OwnPage): those facets are drawn from every call in the period.
  `path` is left out for another machine.
- usage.Record gains subagent and parent_agent; session stays the parent
  conversation. Gateway rows take them from x-claude-code-agent-id and
  x-claude-code-parent-agent-id; session-file rows from the file name
  (<session>/subagents[/workflows/<run>]/agent-<id>.jsonl, no cache version
  bump) and the parentAgentId of the agent-<id>.meta.json beside it. Both
  packed into the request page's text columns (now 29, msg at 29).
  Codex sub-threads: only kind, as before; no spawned thread on disk to
  take a parent shape from.

Run: real Claude Code 2.1.296 under a sandbox CLAUDE_CONFIG_DIR against a
local fake Anthropic endpoint that made it spawn a subagent from a
subagent: the nested request carried X-Claude-Code-Agent-Id=a143287ac46b82a96
and X-Claude-Code-Parent-Agent-Id=a99ca20c03d1813d8, the first-level one
the agent id only, Session-Id the parent's throughout; the nested meta.json
had parentAgentId. Tests use those bytes. TestUsageOverGateway goes red
with OwnPage, the CallerKey pin, or the header read removed (each tried).
gofmt -l clean; go vet ./...; GOOS=linux go vet -tags nogui ./...;
GOOS=windows go vet ./...; set -o pipefail; go test ./... green.
2026-10-10 20:47:00 +08:00

67 lines
3.0 KiB
Go

package sessions
import (
"os"
"path/filepath"
"testing"
)
// A Claude Code subagent's conversation is a file of its own beside the
// session's, named by the agentId its lines carry; a workflow's agents
// sit a folder deeper. The shapes are those under ~/.claude/projects.
func TestSubagentOf(t *testing.T) {
proj := filepath.FromSlash("/h/.claude/projects/-Users-me-app")
for p, want := range map[string]string{
"2bd9379e-9a4a-4cb6-ba42-e6b2fb6dc5a5/subagents/agent-a76fc11ece25eafc9.jsonl": "a76fc11ece25eafc9",
"4c056ab3-f2e6-4918-b774-da5c713ce1e3/subagents/workflows/wf_e241ae3e-9ce/agent-aa06d649dca14e7f9.jsonl": "aa06d649dca14e7f9",
"2bd9379e-9a4a-4cb6-ba42-e6b2fb6dc5a5.jsonl": "",
"2bd9379e-9a4a-4cb6-ba42-e6b2fb6dc5a5/subagents/agent-a76fc11ece25eafc9.meta.json": "",
"2bd9379e-9a4a-4cb6-ba42-e6b2fb6dc5a5/subagents/notes.jsonl": "",
"2bd9379e-9a4a-4cb6-ba42-e6b2fb6dc5a5/workflows/wf_1/agent-a1.jsonl": "",
"agent-a1.jsonl": "",
} {
if got := SubagentOf(filepath.Join(proj, filepath.FromSlash(p))); got != want {
t.Errorf("%s: %q, want %q", p, got, want)
}
}
}
// A subagent another started says which one in its meta file; the bytes
// are Claude Code 2.1.296's for a subagent spawned by a subagent, and for
// the one the conversation spawned.
func TestSubagentParent(t *testing.T) {
dir := filepath.Join(t.TempDir(), "3ac5fc87-6335-4971-9609-0fbdc17742ce", "subagents")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
write := func(name, s string) string {
p := filepath.Join(dir, name)
if err := os.WriteFile(p, []byte(s), 0o644); err != nil {
t.Fatal(err)
}
return p
}
nested := write("agent-a143287ac46b82a96.jsonl", "{}\n")
write("agent-a143287ac46b82a96.meta.json", `{"agentType":"general-purpose","description":"check","toolUseId":"toolu_01","parentAgentId":"a99ca20c03d1813d8","spawnDepth":2,"requestShape":"background","requestNonInteractive":true}`)
first := write("agent-a99ca20c03d1813d8.jsonl", "{}\n")
write("agent-a99ca20c03d1813d8.meta.json", `{"agentType":"general-purpose","description":"check","toolUseId":"toolu_01","spawnDepth":1,"requestShape":"background","requestNonInteractive":true}`)
if got := SubagentParent(nested); got != "a99ca20c03d1813d8" {
t.Fatalf("nested: %q", got)
}
if got := SubagentParent(first); got != "" {
t.Fatalf("started by the conversation: %q", got)
}
// no meta file yet is not known, so it is read again once there is one
late := write("agent-a5.jsonl", "{}\n")
if got := SubagentParent(late); got != "" {
t.Fatalf("no meta: %q", got)
}
write("agent-a5.meta.json", `{"parentAgentId":"a99ca20c03d1813d8","spawnDepth":2}`)
if got := SubagentParent(late); got != "a99ca20c03d1813d8" {
t.Fatalf("meta written later: %q", got)
}
if got := SubagentParent(filepath.Join(filepath.Dir(dir), "x.jsonl")); got != "" {
t.Fatalf("a session's own file: %q", got)
}
}