Skip to content

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

sessions: Droid's and Grok's listings open only the files that changed

yetone1f411802b821
diff --git a/internal/sessions/droid.go b/internal/sessions/droid.go
index 885688508..3d9eb5602 100644
--- a/internal/sessions/droid.go
+++ b/internal/sessions/droid.go
@@ -64,10 +64,18 @@ func readDroidStart(path string) (droidStart, bool) {
return s, ok
}
+// droidCallers is whose subagent each transcript is, as its session_start
+// line said when the listing last read it: a listing runs on every poll, and
+// opening every session's transcript for its first line each time cost more
+// than the rest of the listing together.
+var droidCallers listMemo[string]
+
func droidFiles() []file {
root := filepath.Join(FactoryDir(), "sessions")
a, _ := filepath.Glob(filepath.Join(root, "*.jsonl"))
b, _ := filepath.Glob(filepath.Join(root, "*", "*.jsonl"))
+ callers := droidCallers.round()
+ defer callers.done()
var out []file
for _, p := range append(a, b...) {
id := strings.TrimSuffix(filepath.Base(p), ".jsonl")
@@ -78,6 +86,10 @@ func droidFiles() []file {
if !stat(&f) {
continue
}
+ calling, _ := callers.get(p, f.size, f.mod, func() (string, bool) {
+ s, ok := readDroidStart(p)
+ return s.Calling, ok
+ })
// what it spent and on which model are in its settings
m := file{path: strings.TrimSuffix(p, ".jsonl") + ".settings.json"}
if stat(&m) {
@@ -87,8 +99,8 @@ func droidFiles() []file {
f.mod = m.mod
}
}
- if s, ok := readDroidStart(p); ok && s.Calling != "" && s.Calling != id && safeID.MatchString(s.Calling) {
- f.key, f.main = "droid:"+s.Calling, false
+ if calling != "" && calling != id && safeID.MatchString(calling) {
+ f.key, f.main = "droid:"+calling, false
}
out = append(out, f)
}
diff --git a/internal/sessions/grok.go b/internal/sessions/grok.go
index a679067d7..ea68115a5 100644
--- a/internal/sessions/grok.go
+++ b/internal/sessions/grok.go
@@ -51,13 +51,31 @@ type grokSummary struct {
Kind string `json:"session_kind"`
}
+// grokSummaryHook, when a test sets it, is told each time a summary.json
+// is read.
+var grokSummaryHook func(path string)
+
func readGrokSummary(path string) (grokSummary, bool) {
+ if grokSummaryHook != nil {
+ grokSummaryHook(path)
+ }
var s grokSummary
b, err := os.ReadFile(path)
return s, err == nil && json.Unmarshal(b, &s) == nil
}
+// grokKind is what a listing needs of a session's summary.json: whether it
+// is a subagent's, and whose.
+type grokKind struct{ Kind, Parent string }
+
+// grokKinds keeps each summary's kind as the listing last read it: a
+// listing runs on every poll, and reading and decoding every session's
+// summary each time cost more than the rest of the listing together.
+var grokKinds listMemo[grokKind]
+
func grokFiles() []file {
+ kinds := grokKinds.round()
+ defer kinds.done()
root := filepath.Join(GrokDir(), "sessions")
paths, _ := filepath.Glob(filepath.Join(root, "*", "*", "updates.jsonl"))
// a session's subagents are named in its subagents/ folder too
@@ -83,7 +101,11 @@ func grokFiles() []file {
if m.mod.After(f.mod) {
f.mod = m.mod
}
- if s, ok := readGrokSummary(m.path); ok && strings.HasPrefix(s.Kind, "subagent") {
+ s, ok := kinds.get(m.path, m.size, m.mod, func() (grokKind, bool) {
+ s, ok := readGrokSummary(m.path)
+ return grokKind{s.Kind, s.Parent}, ok
+ })
+ if ok && strings.HasPrefix(s.Kind, "subagent") {
parent := s.Parent
if parent == "" {
parent = parentOf[id]
diff --git a/internal/sessions/listmemo.go b/internal/sessions/listmemo.go
new file mode 100644
index 000000000..912d7935d
--- /dev/null
+++ b/internal/sessions/listmemo.go
@@ -0,0 +1,62 @@
+package sessions
+
+import (
+ "sync"
+ "time"
+)
+
+// listMemo keeps what a listing read from each of many small files, by the
+// file's own size and mtime, so a listing that runs on every poll opens only
+// the files that changed since the last one rather than every session's.
+// Each listing is a round: a file it did not ask about (a session deleted)
+// is dropped when the round ends. A read that failed is not kept, so the
+// next listing tries again: a failed read means unknown, not empty.
+type listMemo[T any] struct {
+ sync.Mutex
+ m map[string]memoEntry[T]
+}
+
+type memoEntry[T any] struct {
+ size, mod int64
+ v T
+}
+
+type memoRound[T any] struct {
+ l *listMemo[T]
+ old, next map[string]memoEntry[T]
+}
+
+// round starts a listing.
+func (l *listMemo[T]) round() *memoRound[T] {
+ l.Lock()
+ old := l.m
+ l.Unlock()
+ return &memoRound[T]{l: l, old: old, next: make(map[string]memoEntry[T], len(old))}
+}
+
+// get is what read gives for the file at path, read again only when its
+// size or mtime is not what it was when last read.
+func (r *memoRound[T]) get(path string, size int64, mod time.Time, read func() (T, bool)) (T, bool) {
+ if e, ok := r.old[path]; ok && e.size == size && e.mod == mod.UnixNano() {
+ r.next[path] = e
+ return e.v, true
+ }
+ v, ok := read()
+ if ok {
+ r.next[path] = memoEntry[T]{size, mod.UnixNano(), v}
+ }
+ return v, ok
+}
+
+// done keeps what this round read, and only that, for the next.
+func (r *memoRound[T]) done() {
+ r.l.Lock()
+ r.l.m = r.next
+ r.l.Unlock()
+}
+
+func (l *listMemo[T]) reset() {
+ l.Lock()
+ l.m = nil
+ l.Unlock()
+}
diff --git a/internal/sessions/listmemo_test.go b/internal/sessions/listmemo_test.go
new file mode 100644
index 000000000..5648ac468
--- /dev/null
+++ b/internal/sessions/listmemo_test.go
@@ -0,0 +1,157 @@
+package sessions
+
+import (
+ "os"
+ "path/filepath"
+ "sync"
+ "testing"
+)
+
+// keyOf is the key a listing gives the file at path.
+func keyOf(t *testing.T, fs []file, path string) string {
+ t.Helper()
+ for _, f := range fs {
+ if f.path == path {
+ return f.key
+ }
+ }
+ t.Fatalf("%s not listed", path)
+ return ""
+}
+
+// readCounter counts reads of each file a hook is told of.
+type readCounter struct {
+ mu sync.Mutex
+ n map[string]int
+}
+
+func (c *readCounter) saw(p string) {
+ c.mu.Lock()
+ c.n[p]++
+ c.mu.Unlock()
+}
+
+func (c *readCounter) take() map[string]int {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ n := c.n
+ c.n = map[string]int{}
+ return n
+}
+
+// Droid's listing, run on every poll, opens a transcript for whose
+// subagent it is only when the transcript changed; a transcript it could
+// not read is tried again on the next listing, not taken as nobody's.
+func TestDroidListingReadsOnlyChangedTranscripts(t *testing.T) {
+ inZone(t, 0)
+ home := setupAgents(t)
+ putDroid(t, home)
+ dir := filepath.Join(home, ".factory", "sessions", "-work-droid")
+ sub := filepath.Join(dir, "d0000000-0000-4000-8000-0000000000aa.jsonl")
+ start := `{"type":"session_start","id":"d0000000-0000-4000-8000-0000000000aa","title":"Sub","cwd":"/work","callingSessionId":"` + droidMain + `"}` + "\n"
+ if err := os.WriteFile(sub, []byte(start), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ half := filepath.Join(dir, "d0000000-0000-4000-8000-0000000000bb.jsonl")
+ if err := os.WriteFile(half, []byte(`{"type":"session_start","id":"d0000000-0000-4000-8000-0000000000bb","callingSessionId":"`+droidMain), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ c := &readCounter{n: map[string]int{}}
+ scanHook = func(p string, _ int64) { c.saw(p) }
+ t.Cleanup(func() { scanHook = nil })
+
+ fs := droidFiles()
+ if n := c.take(); n[sub] != 1 || n[half] != 1 || len(n) < 3 {
+ t.Fatalf("first listing read %v", n)
+ }
+ if k := keyOf(t, fs, sub); k != "droid:"+droidMain {
+ t.Fatalf("subagent keyed %q", k)
+ }
+ fs = droidFiles()
+ if n := c.take(); len(n) != 1 || n[half] != 1 {
+ t.Fatalf("nothing changed: read %v, want only the half-written one again", n)
+ }
+ if k := keyOf(t, fs, sub); k != "droid:"+droidMain {
+ t.Fatalf("subagent keyed %q from what was kept", k)
+ }
+
+ // appended to: read again, still the same caller
+ appendText(t, sub, `{"type":"message","id":"m1","timestamp":"2026-09-27T08:00:00.000Z","message":{"role":"user","content":"go"}}`+"\n")
+ fs = droidFiles()
+ if n := c.take(); n[sub] != 1 || n[half] != 1 || len(n) != 2 {
+ t.Fatalf("one appended to: read %v", n)
+ }
+ // the half-written line finished: read, and kept from then on
+ appendText(t, half, `"}`+"\n")
+ fs = droidFiles()
+ if k := keyOf(t, fs, half); k != "droid:"+droidMain {
+ t.Fatalf("finished line keyed %q", k)
+ }
+ c.take()
+ droidFiles()
+ if n := c.take(); len(n) != 0 {
+ t.Fatalf("nothing changed: read %v", n)
+ }
+ // written over without a caller: its own session now
+ if err := os.WriteFile(sub, []byte(`{"type":"session_start","id":"d0000000-0000-4000-8000-0000000000aa","title":"Own"}`+"\n"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if k := keyOf(t, droidFiles(), sub); k != "droid:d0000000-0000-4000-8000-0000000000aa" {
+ t.Fatalf("written over: keyed %q", k)
+ }
+}
+
+// Grok Build's listing reads a session's summary.json, for whether it is
+// a subagent's, only when the summary changed.
+func TestGrokListingReadsOnlyChangedSummaries(t *testing.T) {
+ inZone(t, 0)
+ home := setupAgents(t)
+ id := "0190aaaa-0000-7000-8000-0000000000dd"
+ dir := filepath.Join(home, ".grok", "sessions", "%2Fwork%2Fgrok", id)
+ if err := os.MkdirAll(dir, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ p := filepath.Join(dir, "updates.jsonl")
+ if err := os.WriteFile(p, []byte(`{"timestamp":1790496001}`+"\n"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ summary := filepath.Join(dir, "summary.json")
+ if err := os.WriteFile(summary, []byte(`{"info":{"id":"`+id+`"},"session_kind":"subagent","parent_session_id":"`+grokMain+`"}`), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ c := &readCounter{n: map[string]int{}}
+ grokSummaryHook = c.saw
+ t.Cleanup(func() { grokSummaryHook = nil })
+
+ fs := grokFiles()
+ if n := c.take(); n[summary] != 1 || len(n) < 2 {
+ t.Fatalf("first listing read %v", n)
+ }
+ if k := keyOf(t, fs, p); k != "grok:"+grokMain {
+ t.Fatalf("subagent keyed %q", k)
+ }
+ fs = grokFiles()
+ if n := c.take(); len(n) != 0 {
+ t.Fatalf("nothing changed: read %v", n)
+ }
+ if k := keyOf(t, fs, p); k != "grok:"+grokMain {
+ t.Fatalf("subagent keyed %q from what was kept", k)
+ }
+ // the updates grew: the summary is not read for that
+ appendText(t, p, `{"timestamp":1790496002}`+"\n")
+ grokFiles()
+ if n := c.take(); len(n) != 0 {
+ t.Fatalf("updates grew: read %v", n)
+ }
+ // the summary written over, no longer a subagent's: read, its own now
+ if err := os.WriteFile(summary, []byte(`{"info":{"id":"`+id+`"},"session_kind":"main","generated_title":"Own"}`), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ fs = grokFiles()
+ if n := c.take(); n[summary] != 1 || len(n) != 1 {
+ t.Fatalf("summary changed: read %v", n)
+ }
+ if k := keyOf(t, fs, p); k != "grok:"+id {
+ t.Fatalf("written over: keyed %q", k)
+ }
+}
diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go
index f317bbc30..a906d0033 100644
--- a/internal/sessions/sessions.go
+++ b/internal/sessions/sessions.go
@@ -994,6 +994,8 @@ func Reset() {
directoryCache.entries = map[string]directoryEntry{}
directoryCache.Unlock()
resetCalls()
+ droidCallers.reset()
+ grokKinds.reset()
wslReset()
forgetHermesPrints()
mu.Lock()