Files
magpie/internal/usage/subagent_row_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

38 lines
1.8 KiB
Go

package usage
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/sessions"
)
// A call read from a Claude Code subagent's own file names that subagent
// and, from its meta file, the subagent that started it,
// and the subagent and the one that started it are kept when the rows are
// packed away and read back.
func TestRowKeepsSubagent(t *testing.T) {
f := filepath.FromSlash("/h/.claude/projects/-p/2bd9379e-9a4a-4cb6-ba42-e6b2fb6dc5a5/subagents/agent-a76fc11ece25eafc9.jsonl")
if r := logRecord(sessions.Call{File: f, Agent: "claude", Model: "m"}); r.Subagent != "a76fc11ece25eafc9" || r.ParentAgent != "" {
t.Fatalf("from the subagent's file: %+v", r)
}
if r := logRecord(sessions.Call{File: filepath.FromSlash("/h/.claude/projects/-p/2bd9379e.jsonl"), Agent: "claude", Model: "m"}); r.Subagent != "" {
t.Fatalf("from the session's own file: %q", r.Subagent)
}
sub := filepath.Join(t.TempDir(), "3ac5fc87", "subagents")
os.MkdirAll(sub, 0o755)
os.WriteFile(filepath.Join(sub, "agent-a143287ac46b82a96.meta.json"), []byte(`{"agentType":"general-purpose","parentAgentId":"a99ca20c03d1813d8","spawnDepth":2}`), 0o644)
if r := logRecord(sessions.Call{File: filepath.Join(sub, "agent-a143287ac46b82a96.jsonl"), Agent: "claude", Model: "m"}); r.Subagent != "a143287ac46b82a96" || r.ParentAgent != "a99ca20c03d1813d8" {
t.Fatalf("a subagent a subagent started: %+v", r)
}
c := &rowChunk{}
c.add(Row{Record: Record{Time: time.Now(), Agent: "claude", Session: "conv-1", Subagent: "a1", ParentAgent: "a0"}}, "msg", 1, false)
for _, chunk := range []*rowChunk{c, c.pack().unpack()} {
if r := chunk.row(0); r.Subagent != "a1" || r.ParentAgent != "a0" || r.Session != "conv-1" || chunk.Strings[chunk.Rows[0].Text[rowMsg]] != "msg" {
t.Fatalf("read back: %+v", r)
}
}
}