Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

sessions: a subagent's file is read for its task only when its meta names none, once per change

yetonea23fbaeadf2f
diff --git a/docs/subsystems/gateway-sessions.md b/docs/subsystems/gateway-sessions.md
index e26c7672b..6c415e434 100644
--- a/docs/subsystems/gateway-sessions.md
+++ b/docs/subsystems/gateway-sessions.md
@@ -449,9 +449,13 @@ for the session) and `start`/`last`; its file is never sent.
- Claude Code, Qoder and Qoder CN: each `subagents/agent-<id>.jsonl` (and
under `workflows/<run>/`), with `agentType`, `description` and
`parentAgentId` from the meta file beside it (`ccMeta`). With no meta file
- (Qoder), the title is the file's first user line (`ccTask`, at most 16
- lines read). `agent-acompact-<id>.jsonl` is a compaction's own run and is
- left out.
+ (Qoder), or one naming no `description`, the title is the file's first
+ user line (`ccTask`, at most 16 lines read). The file is read for it only
+ then, and what was read, a task or none, is kept until the file's size or
+ time changes; a read that fails keeps the last one. The size and time are
+ taken before the read, so a line written during it is read on the next
+ ask. `agent-acompact-<id>.jsonl` is a compaction's own run and is left
+ out.
- OpenCode: a child session (`parent_id`, from the JSON store or
`opencode.db`) is a subagent of its root, not a session of its own;
`ocRoots` records its parent. Its title splits 1.x's
@@ -476,7 +480,8 @@ subagent's conversation; the id is looked up among the session's own
`subagents` (`sessions.SubagentTranscript`), never used as a path, and one
not listed is an error. See `TestClaudeSubagents`, `TestQoderSubagents`,
`TestOpenCodeSubagents`, `TestOpenCodeV2SubagentKind`, `TestKimiSubagents`,
-`TestKimiSubagentTask` and
+`TestKimiSubagentTask`, `TestSubagentTaskReadOnlyWhenTheFileChanges`,
+`TestSubagentWithMetaTaskIsNotReadForIt` and
`TestSessionSubagentTranscript`.
The Sessions page draws only main sessions on top. A session's subagents are
diff --git a/internal/sessions/subagent_task_test.go b/internal/sessions/subagent_task_test.go
new file mode 100644
index 000000000..6b57e6a7d
--- /dev/null
+++ b/internal/sessions/subagent_task_test.go
@@ -0,0 +1,155 @@
+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)
+ }
+}
diff --git a/internal/sessions/subagents.go b/internal/sessions/subagents.go
index 73a810b3b..b520c5b97 100644
--- a/internal/sessions/subagents.go
+++ b/internal/sessions/subagents.go
@@ -56,7 +56,11 @@ func subagentsOf(s Session, fs []file) []Subagent {
}
m := ccMeta(f.path)
sa.Kind, sa.Parent = m.AgentType, m.ParentAgentID
- sa.Title = cmp.Or(m.Description, ccTask(f.path))
+ // the file only when the meta names no task: cmp.Or read
+ // every Claude Code subagent's file for a title it threw away
+ if sa.Title = m.Description; sa.Title == "" {
+ sa.Title = ccTask(f.path)
+ }
case "opencode":
sa.ID = f.sid
if f.parent != s.ID {
@@ -85,7 +89,9 @@ func subagentsOf(s Session, fs []file) []Subagent {
sa.Parent = sp.ParentAgentID
}
sa.Kind = sp.SubagentName
- sa.Title = cmp.Or(title(sp.Description), kimiWireOf(f.path).Prompt)
+ if sa.Title = title(sp.Description); sa.Title == "" {
+ sa.Title = kimiWireOf(f.path).Prompt
+ }
default:
continue
}
@@ -134,13 +140,22 @@ func ccMeta(p string) ccSubMeta {
var ccMetas sync.Map
// ccTask is what a Claude Code subagent was asked: its file's first user
-// line, which Claude Code writes as it starts it. Kept once found.
+// line, which Claude Code writes as it starts it, among its first 16. What
+// was read, a task or none, is kept until the file's size or time changes,
+// and only its first lines are read again then: every Sessions list asks
+// for each subagent's task, and a file naming none was read on every ask.
func ccTask(p string) string {
+ fi, err := os.Stat(p)
+ if err != nil {
+ return ""
+ }
if v, ok := ccTasks.Load(p); ok {
- return v.(string)
+ if t := v.(ccTaskRead); fi.Size() == t.size && fi.ModTime().Equal(t.mod) {
+ return t.task
+ }
}
task, n := "", 0
- scanAt(p, 0, nil, func(b []byte, _, _ int64) bool {
+ _, err = scanAt(p, 0, nil, func(b []byte, _, _ int64) bool {
if n++; n > 16 {
return false
}
@@ -159,12 +174,26 @@ func ccTask(p string) string {
}
return task == ""
})
- if task != "" {
- ccTasks.Store(p, task)
+ if err != nil {
+ // not readable now: what was read before, if anything
+ if v, ok := ccTasks.Load(p); ok {
+ return v.(ccTaskRead).task
+ }
+ return ""
}
+ // the size and time from before the read: a line written while it was
+ // read changes them, and the file is read again on the next ask
+ ccTasks.Store(p, ccTaskRead{task: task, size: fi.Size(), mod: fi.ModTime()})
return task
}
+// ccTaskRead is what ccTask read of a file, as of its size and time.
+type ccTaskRead struct {
+ task string
+ size int64
+ mod time.Time
+}
+
var ccTasks sync.Map
// ocSubagentTitle splits the title OpenCode gives a session its task tool