Files
magpie/internal/sessions/cc_copies.go
T
yetone 14b6b95556 sessions: a session resumed into copies isn't read whole again after a start (shangyuan0818 on X)
shangyuan0818 on X, on the latest Windows build: after using an agent,
Sessions took 18 s to load. They had ~100 Claude Code sessions of 30 to
100 MB and read Claude Code only, and guessed it was dedup, since
ccusage is slow on the same files.

Cause: older `claude --resume` wrote each resume as a whole copy of the
session under a new sessionId. ccFamily judges such a group by every
file's message uuids (ccUUIDsOf), and ccRest reads each copy for what
its keeper doesn't hold. Both were kept in memory only. cc-rest.json
kept the rests, but without the uuids they were read from (ids nil).
After a start, the first turn of the session the copies are kept in
changed the group's signature and its size, so every file of the chain
was read whole twice: gigabytes before Sessions showed. Codex is not
read with Claude-only reads (the cold load is the same with its folder
empty).

Fix: the uuids of each file of a group are kept on disk, one file each
in cc-uuids/ (named by a hash of the path), with the size, mtime, the
offset read to and a prefixHash of the bytes up to it. A start reads
them back and goes on from the offset only while the hash still
matches the file's start; otherwise the file is read from its start.
The rest ids are kept in cc-rest-ids.bin under the rest's key, so a
keeper that only grew leaves its copies unread after a start too.
Entries whose session file is gone are pruned once a start. Trash
drops an entry. Reset forgets the in-memory uuids, like a real start.
Not changed: a first start (no cache) still reads each copy three
times (parse, ccUUIDsOf, ccRest), filed as #7143.

Windows (xiaomi-gpu, Windows 11, CGO_ENABLED=0 builds, Claude-only
reads, sandbox HOME). The fixture is 5 resume chains of 20 files, 30 to
107 MB each, 6.9 GB. Time is for the page's three requests together.
                                     origin/main   this change
  cold, no cache                         20.2 s        17.6 s
  warm restart                            0.30 s        0.30 s
  restart after 1 keeper's turn       3.29/3.14 s   0.38/0.25 s
  1 keeper's turn while running           1.60 s        0.11 s
  restart after 5 keepers' turns          7.25 s        0.92 s
  5 keepers' turns while running          3.32 s        0.78 s
The profile of a restart after a turn on origin/main was ccFamily →
ccUUIDsOf (1.23 s) and ccCopyStates → ccRest (1.14 s) in scanThin.
The kept uuids take 2.4 MB for the fixture's 100 files.

Tests (internal/sessions/cc_copies_test.go):
- TestClaudeKeeperTurnAfterAStartReadsNoCopy: a turn, then Reset, reads
  no file from its start, and the turn is counted.
- TestClaudeKeeperTurnTheBranchHeldAfterAStart: a keeper that grows,
  after a start, with a message the branch held on its own counts it
  once.
- TestClaudeKeptUUIDsOfAFileWrittenOver: an entry whose file was
  written over doesn't stand.
- TestClaudeKeptUUIDsRoundTrip.

Run at this tree:
- go test ./... -count=1 -timeout 45m on mote: 102 packages ok, 2
  red, both filed: internal/cli's TestHealthcheckFindsTheRunningGateway
  WithoutMagpieAddr, TestUpdateRestartsTheGatewayItFindsWithoutMagpieAddr
  and TestUpdateFindsAMagpieThatWroteNoRecord (#6471, #6223: they ask
  whatever serves 127.0.0.1:3425 on mote), and internal/gateway's
  TestOpenCodeGoOwnFreeModelGetsNothingOfOpenCodes (#7117, red on main).
- go test ./internal/sessions -race -count=1 -run TestClaude -timeout
  25m on mica: every new test passes; 2 reds, both data races filed and
  outside this change: TestClaudeCdKeptParseReadAgain (#6213, red the
  same at 0e3d09361 on mica) and the claude-desktop-3p subtest of
  TestClaudeTraceSubagentsShareSessionWithoutIDCollisions, failed by
  testenv.Zone's cleanup against provider's sweepStale timer (#4360).
  A first try at -count=3 hit go test's default 10m timeout mid-run
  (3 x ~240s under -race), not a hang.
- go vet ./internal/sessions on darwin, GOOS=linux and GOOS=windows
  (mote): clean. gofmt -l: clean.
- win-run go test ./internal/sessions: 4 reds, TestAgySessions,
  TestAgyRowOnly, TestAntigravityAppFolder and
  TestCCListingKeepsItsNames. The same 4 are red at origin/main
  0e3d09361 on the same box, filed as #6071.
- No GUI test reaches this: the GUI tests fake the API, and no asset
  changed.
Broken by hand:
- kept uuids not read back (ccUUIDKept ignored):
  TestClaudeKeeperTurnAfterAStartReadsNoCopy red, all three files read
  whole on each turn.
- rest ids not read back: that test red, the two copies read whole.
- rest ids read back as empty: TestClaudeKeeperTurnTheBranchHeldAfterAStart
  red, 90 tokens in where 80 are wanted.
Not tried: the reporter's own files, or a real Claude Code --resume
chain (the fixture is generated in that shape). Not tried on a slow
disk or with Defender scanning, which the reporter's 18 s may include.
2026-11-19 09:10:33 +08:00

459 lines
14 KiB
Go

package sessions
import (
"bytes"
"cmp"
"maps"
"slices"
"strings"
"github.com/tidwall/gjson"
)
// Claude Code copies a conversation into a new session file when it forks
// one (`--resume --fork-session`, /branch) or moves it to a background
// session: every message of it again, under the same uuids, with the new
// session's id. A client that forks a session for each turn it runs leaves
// one file per fork, and each was listed as a session of its own: the same
// title, the same messages and size, ten rows of one conversation (Neo,
// #feedback). A file whose last message another file of the same agent
// holds is such a copy: everything it says is in that file too, so it is
// not a row of its own. A fork that went on with a turn of its own has a
// last message no other file holds, and is a branch (ccFamily). A copy
// made after a compaction (/compact, or
// /rewind's summarize) holds only the compaction's summary and what
// follows it, so it starts with the summary, not with the conversation's
// first message; it is judged with the files that hold that summary.
// ccCopies is, for each session (by key) that is a copy, the key of the
// session it is a copy of, which no other holds. Only the files cache has
// read are judged: what has not been read is no one's copy.
func ccCopies(files []file) map[string]string {
copies, _ := ccFamily(files)
return copies
}
// A fork that went on is still the conversation it was forked from: a
// client that forks a compacted session for each task it runs (`claude -p
// --resume <id> --fork-session`, the Agent SDK's forkSession) leaves a
// file per task that starts with the compaction's summary and ends with
// that task's turn, and each was a row of the same title that opened on
// the summary and one turn (Neo, #feedback, after v0.1.1809). Such a file
// is a branch of the session that holds the conversation from its start:
// it is listed under that session (ParentSession, Role "branch"), not
// beside it, and reads from the conversation's first line (TranscriptOf).
// ccFamily is ccCopies, and for each session (by key) that is a branch,
// the key of the session that holds its conversation from the start, the
// one of its files no copy and no branch: the one that holds the others'
// first message as they don't hold its (it was compacted after they
// started; they start from its summary), else the one a branch names in
// forkedFrom (/branch), else the earliest, then the lowest key.
func ccFamily(files []file) (copies, branches map[string]string) {
// files start alike when they start with the same message, or one
// starts with a compaction's summary the other holds: a copy made
// after /compact holds only what follows the summary
// a file is held by its place in files, not copied: ~200 bytes for
// each Claude Code file on each poll, and a group made for each of
// them though only one of two or more is judged, were ~140 MB in 80s
// of polls (#2278)
var all []ccOne
up := []int{} // each file's group, by its index in all (union-find)
find := func(i int) int {
for up[i] != i {
up[i] = up[up[i]]
i = up[i]
}
return i
}
at := map[string]int{} // a message a file starts with or holds a summary as
for k := range files {
f := &files[k]
if !f.main || !parserFor(f.agent).claude {
continue
}
st := cache[f.path]
if st == nil || st.more().Root == "" || st.more().Leaf == "" {
continue
}
i := len(all)
all = append(all, ccOne{f, st})
up = append(up, i)
in := f.agent + "\x00" + f.m().wsl + "\x00"
for _, u := range append([]string{st.more().Root}, st.more().Compacts...) {
if j, ok := at[in+u]; ok {
up[find(i)] = find(j)
} else {
at[in+u] = i
}
}
}
size := make([]int, len(all))
for i := range all {
size[find(i)]++
}
groups := map[int][]ccOne{}
for i, o := range all {
if r := find(i); size[r] > 1 {
if groups[r] == nil {
groups[r] = make([]ccOne, 0, size[r])
}
groups[r] = append(groups[r], o)
}
}
var out map[string]string
used := map[string]bool{}
ccFamiliesLoad()
for _, g := range groups {
if len(g) < 2 {
continue
}
sig := ccGroupSig(g)
if kept := ccFamilyKept(sig); kept != nil {
// its files still start alike: the uuids read of them stay,
// so when one grows the group is judged on what it added,
// not on every file of it read whole again (a long session
// resumed into copies, each ~100 MB, took seconds a change)
for _, o := range g {
used[o.f.path] = true
}
if len(kept.Copies) > 0 {
if out == nil {
out = map[string]string{}
}
maps.Copy(out, kept.Copies)
}
if len(kept.Branches) > 0 {
if branches == nil {
branches = map[string]string{}
}
maps.Copy(branches, kept.Branches)
}
continue
}
var gc, gb map[string]string // what this group comes to
holds := func(b ccOne, uuid string) bool {
used[b.f.path] = true
return b.st.more().Leaf == uuid || ccUUIDsOf(*b.f).has(uuid)
}
// before reports whether b is kept over a: the one that holds
// the other's first message too (a copy made after a compaction
// starts with its summary, and the conversation holds what came
// before), then the newer file, then the later key, so of two
// files that hold each other one stays
before := func(b, a ccOne) bool {
if ab, ba := holds(a, b.st.more().Root), holds(b, a.st.more().Root); ab != ba {
return ba
}
if c := b.f.mod.Compare(a.f.mod); c != 0 {
return c > 0
}
return cmp.Compare(b.f.key, a.f.key) > 0
}
hidden := make([]bool, len(g))
for i, a := range g {
for j, b := range g {
// a file that holds what came before the other's start (a
// compaction the fork started after) is not that one's
// copy: the conversation's file a fork went on from
if i == j || a.f.key == b.f.key || !holds(b, a.st.more().Leaf) || !holds(b, a.st.more().Root) {
continue
}
if !holds(a, b.st.more().Leaf) || before(b, a) {
hidden[i] = true
break
}
}
}
for i, a := range g {
if !hidden[i] {
continue
}
// the file it is kept in: one that is not a copy itself and
// holds its last message, as the longest of them does
for j, b := range g {
if !hidden[j] && i != j && holds(b, a.st.more().Leaf) {
if gc == nil {
gc = map[string]string{}
}
gc[a.f.key] = b.f.key
break
}
}
}
// what is left went on apart: one is the conversation, and the
// others are its branches
head := -1
for i, a := range g {
if hidden[i] {
continue
}
if head < 0 || headFirst(a, g[head], holds) {
head = i
}
}
for i, a := range g {
if hidden[i] || i == head || a.f.key == g[head].f.key {
continue
}
if gb == nil {
gb = map[string]string{}
}
gb[a.f.key] = g[head].f.key
}
ccFamilyKeepOut(sig, gc, gb)
if len(gc) > 0 {
if out == nil {
out = map[string]string{}
}
maps.Copy(out, gc)
}
if len(gb) > 0 {
if branches == nil {
branches = map[string]string{}
}
maps.Copy(branches, gb)
}
}
ccFamiliesSave()
ccUUIDSave()
ccUUIDPrune()
// a file judged here that no longer starts like another lets its
// uuids go; another caller's files are left as they are
for _, o := range all {
if !used[o.f.path] {
delete(ccUUIDMemo, o.f.path)
}
}
return out, branches
}
// ccOne is a Claude-format main file and its parse, as ccFamily judges it.
type ccOne struct {
f *file // in the files ccFamily was given
st *state
}
// headFirst reports whether a, not b, is the conversation two files that
// went on apart are of (ccFamily).
func headFirst(a, b ccOne, holds func(ccOne, string) bool) bool {
if ab, ba := holds(a, b.st.more().Root), holds(b, a.st.more().Root); ab != ba {
return ab
}
ida, idb := strings.TrimPrefix(a.f.key, a.f.agent+":"), strings.TrimPrefix(b.f.key, b.f.agent+":")
if fa, fb := ccForkedFrom(*a.f), ccForkedFrom(*b.f); (fb == ida) != (fa == idb) {
return fb == ida
}
if !a.st.Start.Equal(b.st.Start) && !a.st.Start.IsZero() && !b.st.Start.IsZero() {
return a.st.Start.Before(b.st.Start)
}
return a.f.key < b.f.key
}
// ccUUIDMemo is each file's message uuids as ccUUIDsOf read them, kept
// while the file is one of a pair that start alike. Under mu.
var ccUUIDMemo = map[string]*ccUUIDs{}
type ccUUIDs struct {
size, mod, off int64
set uuidSet
hash digest // prefixHash of the file up to off
// forked is the session its messages say they were copied from
// (forkedFrom, which /branch writes on each), "" for none
forked string
}
// ccForkedFrom is the session /branch copied f's conversation from, as
// its messages name it; "" when they name none (--fork-session doesn't).
func ccForkedFrom(f file) string {
ccUUIDsOf(f)
if m := ccUUIDMemo[f.path]; m != nil {
return m.forked
}
return ""
}
var ccForked = []byte(`"forkedFrom":{`)
// ccUUIDsOf is the uuids of the messages in f, read on from where the last
// read stopped while the file only grew.
func ccUUIDsOf(f file) uuidSet {
m := ccUUIDMemo[f.path]
if m == nil {
// what an earlier start read of it, read on from there
if m = ccUUIDKept(f); m != nil {
ccUUIDMemo[f.path] = m
}
}
if m == nil || f.size < m.size || f.size == m.size && f.mod.UnixNano() != m.mod {
m = &ccUUIDs{set: uuidSet{}}
ccUUIDMemo[f.path] = m
}
if m.size == f.size && m.mod == f.mod.UnixNano() {
return m.set
}
off, err := scanThin(f.path, m.off, nil, thinClaude, func(b []byte, _, _ int64) bool {
if !bytes.Contains(b, ccUser) && !bytes.Contains(b, ccAssistant) {
return true
}
r := gjson.GetManyBytes(b, "type", "uuid")
if t := r[0].String(); (t == "user" || t == "assistant") && r[1].Type == gjson.String {
m.set.add(r[1].Str)
if m.forked == "" && bytes.Contains(b, ccForked) {
m.forked = gjson.GetBytes(b, "forkedFrom.sessionId").String()
}
}
return true
})
if err == nil {
m.off = off
m.hash = prefixHash(f.path, off)
ccUUIDDirty[f.path] = true
}
m.size, m.mod = f.size, f.mod.UnixNano()
return m.set
}
// ccJoin is copies and branches (ccFamily) in one map, each by the key of
// the session whose file holds what it shares: what ccCopyStates reads.
func ccJoin(copies, branches map[string]string) map[string]string {
if len(branches) == 0 {
return copies
}
out := make(map[string]string, len(copies)+len(branches))
maps.Copy(out, copies)
maps.Copy(out, branches)
return out
}
// copiesByKeeper is the ids of the copies kept in each session, by its key.
func copiesByKeeper(copies map[string]string, groups map[string][]file) map[string][]string {
if len(copies) == 0 {
return nil
}
out := map[string][]string{}
for k, keeper := range copies {
if g := groups[k]; len(g) > 0 {
out[keeper] = append(out[keeper], sessionID(g))
}
}
for _, ids := range out {
slices.Sort(ids)
}
return out
}
// ccRestMemo is, for each copy's own file, what it holds that the session
// it is kept in doesn't (ccRest), while neither file has changed. Under mu.
var ccRestMemo = map[string]*ccRestOf{}
type ccRestOf struct {
size, mod int64
keeper string
ksize, kmod int64
st *state
// ids is the uuids of the messages st was read from (uuidKey), nil
// when not known: while the copy stays as it is and the keeper only
// grows, st stands as long as the keeper holds none of them. Kept on
// disk with st (cc-rest-ids.bin).
ids [][16]byte
}
// ccRest is the copy's file read for its messages the keeper's file
// doesn't hold. A copy holds the conversation along its last message
// only: what came before a compaction it started after, what /rewind
// left on another branch and a subagent's messages written in the file
// are in no other file, and were spent all the same. Copies that are
// copies through and through come out empty.
func ccRest(copyFile, keeper file) *state {
m := ccRestMemo[copyFile.path]
if m != nil && m.size == copyFile.size && m.mod == copyFile.mod.UnixNano() &&
m.keeper == keeper.path && m.ksize == keeper.size && m.kmod == keeper.mod.UnixNano() {
return m.st
}
held := ccUUIDsOf(keeper)
// the keeper only grew (a live session the copies were made of): what
// it added is new messages, so the copy's rest is what it was unless
// the keeper now holds one of them, and the copy, as large as the
// session, is not read again on each turn
if m != nil && m.ids != nil && m.size == copyFile.size && m.mod == copyFile.mod.UnixNano() &&
m.keeper == keeper.path && keeper.size > m.ksize &&
!slices.ContainsFunc(m.ids, func(k [16]byte) bool { _, ok := held[k]; return ok }) {
m.ksize, m.kmod = keeper.size, keeper.mod.UnixNano()
ccRestKept.dirty = true
return m.st
}
line := parserFor(copyFile.agent).line
s := &state{}
ids := [][16]byte{}
_, err := scanThin(copyFile.path, 0, nil, thinClaude, func(b []byte, _, _ int64) bool {
if !bytes.Contains(b, ccUser) && !bytes.Contains(b, ccAssistant) {
return true
}
r := gjson.GetManyBytes(b, "type", "uuid")
if t := r[0].String(); t != "user" && t != "assistant" || r[1].Type != gjson.String || held.has(r[1].Str) {
return true
}
ids = append(ids, uuidKey(r[1].Str))
line(s, b, copyFile.main)
return true
})
if err != nil {
// unread is not empty: the copy is counted whole, as before it was
// known to be one, until it reads
return cache[copyFile.path]
}
ccRestMemo[copyFile.path] = &ccRestOf{copyFile.size, copyFile.mod.UnixNano(), keeper.path, keeper.size, keeper.mod.UnixNano(), s, ids}
ccRestKept.dirty = true
return s
}
// ccCopyStates is what each file of a copy adds to the session it is kept
// in: a subagent's file whole, as Claude Code leaves it in the folder of
// the session it ran in and copies none of it, and the copy's own file for
// what the keeper doesn't hold (ccRest). By the file's path.
func ccCopyStates(files []file, copies map[string]string) map[string]*state {
ccRestLoad()
defer ccRestSave()
if len(copies) == 0 {
if len(ccRestMemo) > 0 {
clear(ccRestMemo)
ccRestKept.dirty = true
}
return nil
}
mains := map[string]file{}
for _, f := range files {
if f.main {
mains[f.key] = f
}
}
out := map[string]*state{}
for _, f := range files {
keeper := copies[f.key]
if keeper == "" {
continue
}
if !f.main {
if st := cache[f.path]; st != nil {
out[f.path] = st
}
continue
}
k, ok := mains[keeper]
if !ok {
continue
}
if st := ccRest(f, k); st != nil {
out[f.path] = st
}
}
for p := range ccRestMemo {
if out[p] == nil {
delete(ccRestMemo, p)
ccRestKept.dirty = true
}
}
return out
}