Files
magpie/internal/sessions/subagent_task_test.go
T
yetone a23fbaeadf sessions: a subagent's file is read for its task only when its meta names none, once per change
Why: a 48-minute heap profile of the user's magpie allocated 6.3 GB
in sessions.scanAt, 3.5 GB of it from ccTask. The user has 3,174
Claude Code subagent files, 1,798 of them over 1 MB. Two things
caused it:

- subagentsOf titled each one `cmp.Or(m.Description,
  ccTask(f.path))`. Go evaluates both arguments, so every subagent's
  file was read for a first user line that was then thrown away
  whenever the meta file had a description, which is nearly always.
- ccTask kept only a found task. A file whose first 16 lines name
  none was read again on every ask.

What changed:

- Claude Code and Kimi subagent titles read the file only when the
  meta (or the spawn record) gives no description.
- ccTask keeps what it read, a task or none, with the file's size and
  mtime from before the read. An unchanged file isn't read again. A
  grown, rewritten (even at the same size), or touched file is read
  again from its first lines.
- A read that fails keeps the last good result, and a deleted file
  gives "".
- A line appended while the file is being read changes the size, so
  the next ask reads it.
- The docs/subsystems/gateway-sessions.md subagent section says so.

Tests:

- TestSubagentTaskReadOnlyWhenTheFileChanges counts scans through
  scanHook for these cases:
  - first ask: 1 scan;
  - unchanged: 0;
  - grown with a task: 1, then 0 once found;
  - grown after the task: 1;
  - rewritten shorter: 1;
  - rewritten with no task: 1;
  - same size with a new mtime: 1;
  - a task appended past line 16: 1 scan, still "";
  - appended during the read: found on the next ask;
  - deleted: "", 0 scans.
- TestSubagentWithMetaTaskIsNotReadForIt lists a session after a
  simulated restart (Saved, Reset, memos cleared) and expects 0 scans
  of a subagent whose meta names its task.
- Broken by hand:
  - ccTask back to keeping only a found task (`t.task != ""`) makes
    the first test go red: "unchanged, no task yet: 1 scans, want 0".
  - The title back to cmp.Or makes the second go red: "the list read
    a subagent's file 1 times for a task its meta names".
  - Both green again once restored.

Measured on a read-only APFS clone of the user's ~/.claude/projects
and ~/.codex/sessions/2026/10, in a sandbox HOME, at load ~500 from
other sessions. Times are noisy; allocations are not. Restart, then
List(All):

| Code | Allocated | Allocations |
| --- | --- | --- |
| ccTask keep, title still cmp.Or | 153.9 MB | 270,838 |
| this commit too | 25.8 MB | 109,854 |
| origin/main dee5c9d75 | 301.1 MB | 80,004 |

The origin/main row also lacks the buffer pool (a later commit).

Run at the final tree of this series:

- gofmt -l internal: nothing.
- GOOS=linux and GOOS=windows go vet -tags nogui ./...: clean.
- go vet ./internal/sessions/ ./internal/gui/: clean.
- go test -count=1 -timeout 45m ./internal/sessions/ ./internal/gui/:
  - sessions: ok, 366.7s.
  - gui: only TestMainWindowCorners failed, "measuring the windows:
    signal: killed". It also fails alone on an untouched origin/main
    dee5c9d75 export, at load ~500. Filed as #1936 and #2645
    (real-window AppKit tests killed at their deadline under load).
    Every other gui test passed.
- go test -race -count=3 on the new and neighbouring subagent and
  scan tests: ok.
2026-10-20 20:32:04 +08:00

156 lines
5.0 KiB
Go

package sessions
import (
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
)
// Real Claude Code subagent lines (agent-<id>.jsonl), their words and ids
// replaced: a hook's progress line, which comes before the task, and the
// user line that gives the task.
func ccHookLine(n int) string {
return `{"parentUuid":null,"isSidechain":true,"agentId":"b2","type":"progress","data":{"type":"hook_progress","hookEvent":"SessionStart","hookName":"SessionStart:startup","command":"hook"},"timestamp":"2026-09-20T10:05:0` + string(rune('0'+n%10)) + `.000Z","uuid":"h` + string(rune('a'+n%26)) + `","userType":"external"}` + "\n"
}
func ccTaskLine(task string) string {
return `{"parentUuid":null,"isSidechain":true,"agentId":"b2","type":"user","message":{"role":"user","content":"` + task + `"},"uuid":"u1","timestamp":"2026-09-20T10:06:00.000Z","cwd":"/work/app","sessionId":"11111111-2222-3333-4444-555555555555"}` + "\n"
}
// scansOf counts the scans of the file at path from here on.
func scansOf(t *testing.T, path string) func() int {
t.Helper()
var mu sync.Mutex
n := 0
scanHook = func(p string, _ int64) {
if p == path {
mu.Lock()
n++
mu.Unlock()
}
}
t.Cleanup(func() { scanHook = nil })
return func() int {
mu.Lock()
defer mu.Unlock()
c := n
n = 0
return c
}
}
// A subagent's task is read from its file and kept, found or not, until
// the file's size or time changes: grown, written again from its start,
// written again at the same size, or written to while it was read.
func TestSubagentTaskReadOnlyWhenTheFileChanges(t *testing.T) {
dir := t.TempDir()
p := filepath.Join(dir, "agent-b2.jsonl")
writeFile(t, p, ccHookLine(0)+ccHookLine(1))
scans := scansOf(t, p)
ask := func(want string, wantScans int, why string) {
t.Helper()
if got := ccTask(p); got != want {
t.Fatalf("%s: task %q, want %q", why, got, want)
}
if n := scans(); n != wantScans {
t.Fatalf("%s: %d scans, want %d", why, n, wantScans)
}
}
ask("", 1, "first ask")
ask("", 0, "unchanged, no task yet")
ask("", 0, "unchanged again")
// appended: read again, and the task found
appendTo(t, p, ccTaskLine("Map the parser callers"))
ask("Map the parser callers", 1, "grown with its task")
ask("Map the parser callers", 0, "found")
appendTo(t, p, ccHookLine(2))
ask("Map the parser callers", 1, "grown after its task")
// written again from the start, shorter, with another task
writeFile(t, p, ccTaskLine("Read the docs"))
ask("Read the docs", 1, "rewritten shorter")
// written again with the same size and a new time
writeFile(t, p, ccHookLine(3))
ask("", 1, "rewritten without a task")
writeFile(t, p, ccHookLine(4))
later := time.Now().Add(2 * time.Second)
if err := os.Chtimes(p, later, later); err != nil {
t.Fatal(err)
}
ask("", 1, "same size, new time")
// 16 lines without a task: none, and none as the file grows
var b strings.Builder
for i := range 17 {
b.WriteString(ccHookLine(i))
}
writeFile(t, p, b.String())
ask("", 1, "17 lines, none a task")
appendTo(t, p, ccTaskLine("too late"))
ask("", 1, "a task after the first 16 lines")
// written to while it was read: what was read stands for now, and the
// next ask reads the file again
writeFile(t, p, ccHookLine(5))
scansOf(t, p) // count afresh
once := sync.Once{}
scanHook = func(q string, _ int64) {
if q == p {
once.Do(func() { appendTo(t, p, ccTaskLine("written meanwhile")) })
}
}
ccTask(p)
scans = scansOf(t, p)
ask("written meanwhile", 1, "appended while read")
// gone
if err := os.Remove(p); err != nil {
t.Fatal(err)
}
ask("", 0, "deleted")
}
func appendTo(t *testing.T, p, s string) {
t.Helper()
f, err := os.OpenFile(p, os.O_APPEND|os.O_WRONLY, 0)
if err != nil {
t.Fatal(err)
}
if _, err := f.WriteString(s); err != nil {
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
}
}
// A Claude Code subagent whose meta file names its task is titled from it,
// and its file is not read for one: cmp.Or read every subagent's file for
// a first user line it then threw away, for each one at every start.
func TestSubagentWithMetaTaskIsNotReadForIt(t *testing.T) {
inZone(t, 0)
claude, _ := setup(t)
subs := filepath.Join(claude, "projects", "-work-app", "11111111-2222-3333-4444-555555555555", "subagents")
writeFile(t, filepath.Join(subs, "agent-a1.meta.json"), `{"agentType":"Explore","description":"Inventory the parser's callers","toolUseId":"toolu_01BZaaaaaaaaaaaaaaaaaaaa","spawnDepth":1}`)
sub := filepath.Join(subs, "agent-a1.jsonl")
List(0) // every file read once
Saved()
// a restart: the parses come back from the disk, nothing else kept
Reset()
ccTasks.Clear()
ccMetas.Clear()
scans := scansOf(t, sub)
s := find(t, List(0), "claude", "11111111-2222-3333-4444-555555555555")
if n := scans(); n != 0 {
t.Fatalf("the list read a subagent's file %d times for a task its meta names", n)
}
if len(s.Subagents) == 0 || s.Subagents[0].ID != "a1" || s.Subagents[0].Title != "Inventory the parser's callers" {
t.Fatalf("subagents %+v", s.Subagents)
}
}