The summary's Claude revision window (state.Claude) was json:"-", and a Claude file's parse with no window starts again from byte 0. So after every restart, which is every update, the Sessions list read each grown Claude session from its first line: the live 2 GB session on the owner's machine took seconds of the first list. state.Claude now goes into sessions.json as its bounded window (claudeUsageState MarshalJSON/UnmarshalJSON, the entries in eviction order; window() and claudeWindowOf are shared with the calls .rev sidecar). It is bounded by claudeRevisionEntries and trimSummaryRevisions as in memory. A sessions.json from an older magpie has no "claude" and reads from the start as before; an entry without a contribution is skipped, so a bad window never loses the whole index. #697 kept message maps out of the index because they were unbounded; the window is bounded now, so TestClaudeNativeReplayAcrossReadersAndRestart checks the kept window's weight instead of its absence, and TestQoderUsageAfterWindowLoss's restart case strips "claude" from sessions.json to stand for an older index. Real data (a sandbox HOME with the owner's real ~/.claude and ~/.codex, read only): cold Sessions list after a restart with the live sessions grown, 6.3s with the 2 GB file reparsed -> 0.77-0.96s over three runs, only small subagent files reparsed. sessions.json: 7 windows, ~1 MB of 21.7 MB. Run at this tree: go test -count=1 -timeout 45m ./internal/sessions ./internal/usage ./internal/usageapi ./internal/gui ok (sessions after the two test updates); go test -race ./internal/sessions ok; GOOS=linux and windows go vet -tags nogui ./internal/sessions ok; gofmt clean. TestClaudeSummaryReadOnAfterRestart is red with the tag back at "-" (the summary is read again from its start, its marker lost).
75 lines
2.2 KiB
Go
75 lines
2.2 KiB
Go
package sessions
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/gob"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// A Claude Code session's revision window (claudeUsageState) lived in memory
|
|
// alone, so once magpie started again, or let a ninth live session's window
|
|
// go, every Claude file that had grown was read again from its first line: a
|
|
// live 2 GB session took seconds of the first Requests page after every
|
|
// update (#3493). The window is bounded (claudeRevisionEntries), so it is
|
|
// kept beside the file's shard, with where the read that made it stopped.
|
|
|
|
// callRevisions is a shard's Claude window, for the read that ended at Off.
|
|
type callRevisions struct {
|
|
Off int64
|
|
ContentHash string
|
|
CompactMs int64
|
|
Window []claudeWindowEntry
|
|
}
|
|
|
|
func callRevisionsPath(path string) string {
|
|
return strings.TrimSuffix(callCachePath(path), ".gob") + ".rev"
|
|
}
|
|
|
|
// writeCallRevisions keeps st's window beside its shard, or takes away a
|
|
// window kept before when st has none.
|
|
func writeCallRevisions(st *callFile) {
|
|
path := callRevisionsPath(st.Path)
|
|
if st.Claude == nil {
|
|
_ = os.Remove(path)
|
|
return
|
|
}
|
|
r := callRevisions{Off: st.Off, ContentHash: st.ContentHash, CompactMs: st.CompactMs, Window: st.Claude.window()}
|
|
var data bytes.Buffer
|
|
if gob.NewEncoder(&data).Encode(r) != nil {
|
|
return
|
|
}
|
|
out, err := os.CreateTemp(filepath.Dir(path), "calls-*.tmp")
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer os.Remove(out.Name())
|
|
_, err = out.Write(data.Bytes())
|
|
if closeErr := out.Close(); err == nil {
|
|
err = closeErr
|
|
}
|
|
if err == nil {
|
|
_ = os.Rename(out.Name(), path)
|
|
}
|
|
}
|
|
|
|
// withCallRevisions is old with the window kept for it, when old is a
|
|
// Claude file's read without one and the window kept is of that same read.
|
|
func withCallRevisions(old *callFile) *callFile {
|
|
if old == nil || old.Claude != nil || (old.Agent != "claude" && old.Agent != "claude-desktop") || old.ContentHash == "" {
|
|
return old
|
|
}
|
|
data, err := os.ReadFile(callRevisionsPath(old.Path))
|
|
if err != nil {
|
|
return old
|
|
}
|
|
var r callRevisions
|
|
if gob.NewDecoder(bytes.NewReader(data)).Decode(&r) != nil || r.Off != old.Off || r.ContentHash != old.ContentHash {
|
|
return old
|
|
}
|
|
c := *old
|
|
c.Claude, c.CompactMs = claudeWindowOf(r.Window), r.CompactMs
|
|
return &c
|
|
}
|