shangyuan0818 on X: on Windows, Usage > Sessions took over ten seconds to open with one very long session (tens to 100 MB), though there were only a few sessions. Cause: a Codex rollout's summary window (state.Codex) and its calls' window (cxRun) lived in memory only. After a restart, or once the window had been dropped, the first line a live rollout gained made parseRead, prepareCalls and readsWhole read the rollout again from its first line: once for the summary and once for the calls. Claude Code sessions were already read on through their kept windows (#3493). Fix: the window of a rollout being written (changed within the hour, the same files memory keeps windows for) is now kept on disk with the parser version, a window version, and the size, offset and content hash of the read that made it. The summary's goes in codex-windows/<hash>.gob next to sessions.json. It is written with sessions.json, and it is removed once the cache drops that window. The calls' window goes in the shard's .rev file next to Claude's. Each is used only when all of those match, so a window from another read, or from an older layout, is never used; the file is read whole instead, as before. Measured (100 MB rollout built from testdata/codex-native-paginated.jsonl, plus a 100 MB Claude Code session, GET /api/sessions in a sandbox HOME): mote (M5), before: after one line appended following a restart, 2.51 s (Codex alone 2.25 s). After: 9.7 ms (Codex alone 6.3 ms). Windows (xiaomi-gpu), before: 2.61 s. After: 22.8 ms. The first read with nothing kept is unchanged: 2.6 s on mote, 2.8 s on Windows. Warm and plain-restart opens were already 10 to 30 ms. stats and overview stayed at a few ms throughout. A real 32 MB rollout: 24 ms after an append, with 2 KB and 4 KB windows. A 100 MB rollout with a unique response id per turn: 11 ms, with 623 KB and 834 KB windows. Tests: TestCodexSessionReadOnAfterRestart reads a rollout, restarts, then appends a replayed token_count and a new turn. It checks that neither reader starts at offset 0, and that calls and the session's tokens, models and cost match a read from scratch. TestCodexWindowLeavesWithItsSession covers writing, reading back, refusing another read's window and removal. TestPlainRolloutReadOnceForSessionsAndCalls (#4369) expected both readers to start from 0 after a restart; it now expects them to read on. Broken by hand, each red: the summary window not read (starts [319830 0]); the calls window not read ([0 319830]); a summary window emptied of what it had seen (it panicked in codexUsageState.count, which is why the version guard was added); a calls window emptied (62 calls vs 61). Run: gofmt -l clean. GOOS=linux and GOOS=windows go vet -tags nogui ./... on mote. Full go test -tags nogui -timeout 45m ./... on Linux (wsl-run, magpie-desktop) at this change on70bd270ae: every package ok but internal/usage, whose TestRequestStateKeptAcrossACatalogFetchedAgain is red the same way alone on origin/main7b50e1f2c(11 of 20; a same-size catalog write in one mtime tick, filed as #6572). mote's suite.lock had five runs queued ahead, so its run of this change is still waiting. Rebased onto7b50e1f2c(7 files in internal/provider and internal/usage, none under internal/sessions); provider, usage and sessions run again there on Linux: all ok (#6572 passed this time). win-run ./internal/sessions/: only TestAgySessions, TestAgyRowOnly, TestAntigravityAppFolder and TestCCListingKeepsItsNames fail, and they fail the same way on origin/mainea9f2639b(#6071). win-run -run 'Session|Usage|Calls' ./internal/gui/: only TestGatewayModeStatsNameTheGatewaySessions fails, the same onea9f2639b(#6068). Old data: right after updating, every user's live rollout is read whole once more on its next change, since no window has been kept yet. From then on it is read on. Nothing else changes for files not written within the hour. Not changed: WorkBuddy, WorkBuddy AI and CodeBuddy calls are still read whole on every line (556 ms per 100 MB on mote), filed as #6565. Not tried: the reporter's own machine and files.
278 lines
7.7 KiB
Go
278 lines
7.7 KiB
Go
package sessions
|
|
|
|
import (
|
|
"bytes"
|
|
"maps"
|
|
"runtime"
|
|
"sync"
|
|
)
|
|
|
|
// The Sessions page answers before the files it has never read are read
|
|
// whole (hey_boyce on X: the page showed its placeholder rows for a long
|
|
// time on its first open). A first read of a long history (13 GB of Claude
|
|
// Code's and 52 GB of Codex's files on one computer) takes minutes, and
|
|
// Manage used to read all of it before it answered: the agent shown, and
|
|
// every agent with a gateway session for its ids, Codex's too while Claude
|
|
// Code was shown. It still reads at once what is cheap: a file it has a
|
|
// parse of that has only grown, read on from where the parse left off. A
|
|
// file it would read whole (none kept, an older magpie's index, a file
|
|
// written over, or a Claude Code or Codex file whose kept parse can't be
|
|
// read on after a restart) is read behind the answer, as Stats and List
|
|
// read it, and until then its row stands on what is known now: the parse
|
|
// kept of it, or a glance at its first lines (its id, folder, first prompt
|
|
// and start), with its last activity the file's time. Such a row says it
|
|
// is Pending, and the page asks again until none is.
|
|
|
|
// glanceBytes is how far into a file a glance reads at most.
|
|
const glanceBytes = 256 << 10
|
|
|
|
// quickBytes is how much reading whole the page still waits for: a store
|
|
// that small is read in well under a second, even on a slow disk, and its
|
|
// rows come complete the first time.
|
|
var quickBytes int64 = 32 << 20
|
|
|
|
var (
|
|
// glances are the files glanced at, by path, while their whole read
|
|
// is under way; each stands for the size and time it was read at
|
|
glances = map[string]*state{}
|
|
// overlaid are the glances put into the cache for one pass of
|
|
// Manage, never saved with it
|
|
overlaid = map[string]*state{}
|
|
// behind are the paths a read behind an answer has taken, and
|
|
// behindRuns the reads, waited for by Reset
|
|
behind = map[string]bool{}
|
|
behindRuns sync.WaitGroup
|
|
)
|
|
|
|
// readsWhole says whether bringing f's parse up to date reads the file
|
|
// from its start: no parse of it is kept, or the one kept can't be read on.
|
|
// Only the agents read line by line can be glanced at; the others are read
|
|
// as before.
|
|
func readsWhole(f file, old *state) bool {
|
|
p := parserFor(f.agent)
|
|
if p.line == nil {
|
|
return false
|
|
}
|
|
if old == nil || f.size < old.Size || old.Off > f.size {
|
|
return true
|
|
}
|
|
if f.agent == "codex" && old.Codex == nil {
|
|
return !codexWindowKept(f, old)
|
|
}
|
|
return p.claude && old.Claude == nil
|
|
}
|
|
|
|
func current(f file, s *state) bool {
|
|
return s != nil && s.Size == f.size && s.Mod == f.mod.UnixNano()
|
|
}
|
|
|
|
// quickRead is refresh for the Sessions page, under mu: what is cheap is
|
|
// read now, and what would be read whole is read behind. pending are the
|
|
// paths not read yet, and done takes the glances back out of the cache;
|
|
// nothing may let mu go between the two.
|
|
func quickRead(want, all []file) (pending map[string]bool, done func()) {
|
|
var now, later []file
|
|
for _, f := range want {
|
|
if f.cold {
|
|
continue
|
|
}
|
|
if old := cache[f.path]; !current(f, old) && readsWhole(f, old) {
|
|
later = append(later, f)
|
|
continue
|
|
}
|
|
now = append(now, f)
|
|
}
|
|
var whole int64
|
|
for _, f := range later {
|
|
whole += f.size
|
|
}
|
|
if whole <= quickBytes {
|
|
now, later = append(now, later...), nil
|
|
}
|
|
refresh(now, all)
|
|
pending = map[string]bool{}
|
|
var start, look []file
|
|
for _, f := range later {
|
|
old := cache[f.path]
|
|
if current(f, old) {
|
|
continue // read meanwhile
|
|
}
|
|
pending[f.path] = true
|
|
if !behind[f.path] {
|
|
start = append(start, f)
|
|
}
|
|
if old == nil && f.main && !current(f, glances[f.path]) {
|
|
look = append(look, f)
|
|
}
|
|
}
|
|
if len(start) > 0 {
|
|
readBehind(start)
|
|
}
|
|
if len(look) > 0 {
|
|
mu.Unlock()
|
|
seen := glanceAll(look)
|
|
mu.Lock()
|
|
for i, f := range look {
|
|
if seen[i] != nil {
|
|
glances[f.path] = seen[i]
|
|
}
|
|
}
|
|
}
|
|
put := map[string]*state{}
|
|
for p := range pending {
|
|
if cache[p] == nil && glances[p] != nil {
|
|
put[p] = glances[p]
|
|
}
|
|
}
|
|
for p, g := range put {
|
|
cache[p], overlaid[p] = g, g
|
|
}
|
|
return pending, func() {
|
|
for p, g := range put {
|
|
if cache[p] == g {
|
|
delete(cache, p)
|
|
}
|
|
delete(overlaid, p)
|
|
}
|
|
}
|
|
}
|
|
|
|
// readBehind reads fs whole after the answer, as refresh does, and lets
|
|
// their glances go once read.
|
|
func readBehind(fs []file) {
|
|
for _, f := range fs {
|
|
behind[f.path] = true
|
|
}
|
|
behindRuns.Add(1)
|
|
go func() {
|
|
defer behindRuns.Done()
|
|
storeMu.RLock()
|
|
defer storeMu.RUnlock()
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
loadCache()
|
|
refresh(fs, nil)
|
|
for _, f := range fs {
|
|
delete(behind, f.path)
|
|
delete(glances, f.path)
|
|
}
|
|
}()
|
|
}
|
|
|
|
// glanceAll glances at each of fs, a few at a time.
|
|
func glanceAll(fs []file) []*state {
|
|
out := make([]*state, len(fs))
|
|
ch := make(chan int)
|
|
var wg sync.WaitGroup
|
|
for range min(max(runtime.NumCPU()/2, 1), len(fs)) {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
for i := range ch {
|
|
out[i] = glance(fs[i])
|
|
}
|
|
}()
|
|
}
|
|
for i := range fs {
|
|
ch <- i
|
|
}
|
|
close(ch)
|
|
wg.Wait()
|
|
return out
|
|
}
|
|
|
|
// glance is what f's first lines say of its session: its id, folder,
|
|
// title and start, read with its agent's own parser, and none of what it
|
|
// spent, which only a whole read can count. Its last activity is the
|
|
// file's time.
|
|
func glance(f file) *state {
|
|
p := parserFor(f.agent)
|
|
s := &state{}
|
|
var head func([]byte) bool
|
|
var thin func([]byte) (thinner, bool)
|
|
switch {
|
|
case p.claude:
|
|
thin = thinClaude
|
|
case f.agent == "codex":
|
|
head = func(b []byte) bool { return codexHead(s, b, f.main) }
|
|
thin = thinCodex
|
|
}
|
|
idIn := idInFile(f.agent)
|
|
_, err := scanThin(f.path, 0, head, thin, func(b []byte, _, end int64) bool {
|
|
p.line(s, b, f.main)
|
|
if s.Cwd != "" && s.Title != "" && (!idIn || s.ID != "") {
|
|
return false
|
|
}
|
|
return end < glanceBytes
|
|
})
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
if p.claude && f.main {
|
|
claudeTail(s, f)
|
|
}
|
|
if isBuddy(f.agent) && s.Cwd == "" {
|
|
workbuddyMeta(s, f.path)
|
|
}
|
|
g := &state{Size: f.size, Mod: f.mod.UnixNano(), ID: s.ID, Cwd: s.Cwd, Title: s.Title,
|
|
Start: s.Start, Last: f.mod.Time(), CodexIdentity: s.CodexIdentity, Provider: s.Provider,
|
|
StateMore: moreOf(StateMore{Named: s.more().Named, Custom: s.more().Custom, First: s.more().First,
|
|
CodexNickname: s.more().CodexNickname})}
|
|
if g.Start.IsZero() || g.Start.After(g.Last) {
|
|
g.Start = g.Last
|
|
}
|
|
return g
|
|
}
|
|
|
|
// parsed is the parse kept of path: none for a glance put in the cache,
|
|
// which a refresh reads whole as though nothing were kept.
|
|
func parsed(path string) *state {
|
|
s := cache[path]
|
|
if s != nil && overlaid[path] == s {
|
|
return nil
|
|
}
|
|
return s
|
|
}
|
|
|
|
// tailBytes is how much of a Claude Code file's end a glance reads for its
|
|
// titles.
|
|
const tailBytes = 64 << 10
|
|
|
|
// claudeTail reads the titles at the end of a Claude Code file into s: the
|
|
// one Claude Code gives a session, and one the user gave it, are lines it
|
|
// adds as the session goes on, the last of each the one that stands. So is
|
|
// /cd's relocated line, naming the folder the session was moved to.
|
|
func claudeTail(s *state, f file) {
|
|
from := max(f.size-tailBytes, 0)
|
|
p := parserFor(f.agent)
|
|
scanThin(f.path, from, nil, nil, func(b []byte, start, _ int64) bool {
|
|
if start == from && from > 0 {
|
|
return true // the end of a line begun before
|
|
}
|
|
if bytes.Contains(b, ccTitle) || bytes.Contains(b, ccCustom) || bytes.Contains(b, ccRelocatedCwd) {
|
|
p.line(s, b, true)
|
|
}
|
|
return true
|
|
})
|
|
}
|
|
|
|
// savable is the cache as it is saved: without the glances put in it.
|
|
func savable() map[string]*state {
|
|
c := maps.Clone(cache)
|
|
for p, g := range overlaid {
|
|
if c[p] == g {
|
|
delete(c, p)
|
|
}
|
|
}
|
|
return c
|
|
}
|
|
|
|
// forgetBehind waits for the reads behind answers, and forgets the glances
|
|
// (Reset).
|
|
func forgetBehind() {
|
|
behindRuns.Wait()
|
|
mu.Lock()
|
|
glances, overlaid, behind = map[string]*state{}, map[string]*state{}, map[string]bool{}
|
|
mu.Unlock()
|
|
}
|