Files
magpie/internal/sessions/listmemo.go
T
yetone 1f411802b8 sessions: Droid's and Grok's listings open only the files that changed
Droid's listing opened every transcript to read its first line, for
whether it is a subagent's (callingSessionId), and Grok Build's read and
decoded every session's summary.json for its session_kind and parent.
The listing runs on every 15s poll of Sessions and Usage, so with
thousands of sessions each poll opened thousands of files that had not
changed.

listMemo (listmemo.go) keeps what a listing read from each file by that
file's own size and mtime, not the folded size the listing gives the
session. A listing is a round: a file not seen in it (a session deleted)
is dropped when it ends, so the memo never outgrows the sessions there
are. A read that failed, such as a transcript whose first line is still
being written, is not kept, and the next listing reads it again: a
failed read means unknown, not "nobody's". Reset clears both memos.

Measured (3000 sessions each, one listing after another, CPU by
getrusage, the machine at load ~500):
  Droid before: 1103 ms CPU / 8.3 s wall per listing
  Droid after:  27-47 ms CPU / 0.05-1.0 s wall (first listing 1166 ms)
  Grok before:  649 ms CPU / 4.2 s wall per listing
  Grok after:   252-295 ms CPU / 1.8-2.2 s wall (first listing 707 ms)
What Grok's listing still spends is its globs and two stats a session
(updates.jsonl, summary.json), not reads.

Tests: TestDroidListingReadsOnlyChangedTranscripts counts transcript
opens (scanHook): a second listing opens only the half-written one,
appending reopens only that file, a finished first line is then kept,
and a transcript written over without a caller is its own session.
TestGrokListingReadsOnlyChangedSummaries counts summary reads
(grokSummaryHook): none when nothing changed or only the updates grew,
one when the summary is written over, and the key follows it.
Broken by hand: the memo never hit -> both red ("nothing changed: read
map[...all...]"); failed reads kept -> Droid red ("nothing changed: read
map[], want only the half-written one again").

Run: go test ./internal/sessions -run 'Listing|Droid|Grok' -count=1 ok;
gofmt -l and go vet ./internal/sessions clean. The full-suite record is
in the last commit of this series.
2026-10-19 11:30:47 +08:00

63 lines
1.5 KiB
Go

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()
}