Files
magpie/internal/usage/subagent_row_test.go
T
yetone 4042aa4d1d usage: a row keeps the strings its chunk shares once, in Shapes
The usage page holds every row of the period at once, and a packed row
was 328 bytes, 140 of them 35 string ids. On the owner's real history
(364,013 calls in 6,445 chunks) only five of those vary call by call:
the error, the request id, the archive, the response id and the message
id. The other 30 (agent, provider, model, session, endpoint, client...)
repeat for most rows of a chunk: 364k rows have 7,300 distinct sets.

So a row keeps those five ids and a Shape, an index into its chunk's
Shapes table, which holds every other set once. packedRow is 216 bytes.
A snapshot (process-private, nothing on disk) writes the table once
after the strings and per row the shape and the five ids, so archives
shrink too. block() renames and re-prices per shape, not per row.

Same output: f5b015742 and this build served byte-identical bodies on
the real history for 14 endpoints, signed in: requests.csv of every row
(94,292,195 bytes); the requests page for all, offset 20000, month,
week, day, agent=codex, agent=claude, failed=1, q=opus and
provider=anthropic; the heatmap; usage for all and for month.

Footprint (phys_footprint, what Activity Monitor shows), cold start on a
copy of the real history, two runs each, alternating:
  f5b015742  peak 483 / 541 MB  end RSS 530 / 600 MB  CPU 1:44.7 / 1:47.7
  this       peak 465 / 463 MB  end RSS 511 / 509 MB  CPU 1:45.7 / 1:47.1
At rest both settle at 188-197 MB.

Tests: TestRowsOfOneSessionShareAShape fails when packedRow grows past
216 bytes (red at 328 with 112 bytes of padding put in by hand: "a
packed row is 328 bytes"), and checks that 200 rows of two models keep
2 shapes and read back equal after pack and unpack.
go test ./... -timeout 45m at this head: 65 packages ok; agent, gateway
and library failed only with "no space left on device" (the disk filled
mid-run) and, run again once there was room, are ok (agent 208s,
gateway 342s, library 115s).
GOOS=linux and GOOS=windows go vet -tags nogui ./... clean; gofmt clean.
2026-10-21 23:21:41 +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.text(&chunk.Rows[0], rowMsg)] != "msg" {
t.Fatalf("read back: %+v", r)
}
}
}