Skip to content

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

sessions: a Claude conversation Claude Code copied into another file is one row

yetonef4762e9217cc
diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go
index d066fa743..81ef991ee 100644
--- a/internal/gui/sessions.go
+++ b/internal/gui/sessions.go
@@ -106,6 +106,10 @@ func sessionRoutes(mux *http.ServeMux, w Windows) {
nativeKeys := map[string]bool{}
for _, s := range list {
nativeKeys[s.Agent+"|"+s.ID] = true
+ // a copy of its conversation is the agent's own too
+ for _, id := range s.Copies {
+ nativeKeys[s.Agent+"|"+id] = true
+ }
}
gateway := listedGateway(usage.GatewaySessions(time.Time{}, nativeKeys))
ext := make([]sessions.ExternalSession, 0, len(gateway))
diff --git a/internal/sessions/cc_copies.go b/internal/sessions/cc_copies.go
new file mode 100644
index 000000000..7b96e9088
--- /dev/null
+++ b/internal/sessions/cc_copies.go
@@ -0,0 +1,154 @@
+package sessions
+
+import (
+ "bytes"
+ "cmp"
+ "slices"
+
+ "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 stays its own row, as Claude
+// Code's /resume lists it.
+
+// 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 {
+ type one struct {
+ f file
+ st *state
+ }
+ groups := map[string][]one{}
+ for _, f := range files {
+ if !f.main || !parserFor(f.agent).claude {
+ continue
+ }
+ st := cache[f.path]
+ if st == nil || st.Root == "" || st.Leaf == "" {
+ continue
+ }
+ k := f.agent + "\x00" + f.m().wsl + "\x00" + st.Root
+ groups[k] = append(groups[k], one{f, st})
+ }
+ var out map[string]string
+ used := map[string]bool{}
+ for _, g := range groups {
+ if len(g) < 2 {
+ continue
+ }
+ holds := func(b one, uuid string) bool {
+ used[b.f.path] = true
+ return b.st.Leaf == uuid || ccUUIDsOf(b.f)[uuid]
+ }
+ // before reports whether b is kept over a: the newer file, then
+ // the later key, so of two files that hold each other one stays
+ before := func(b, a one) bool {
+ 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 {
+ if i == j || a.f.key == b.f.key || !holds(b, a.st.Leaf) {
+ continue
+ }
+ if !holds(a, b.st.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.Leaf) {
+ if out == nil {
+ out = map[string]string{}
+ }
+ out[a.f.key] = b.f.key
+ break
+ }
+ }
+ }
+ }
+ // a file judged here that no longer starts like another lets its
+ // uuids go; another caller's files are left as they are
+ for _, g := range groups {
+ for _, o := range g {
+ if !used[o.f.path] {
+ delete(ccUUIDMemo, o.f.path)
+ }
+ }
+ }
+ return out
+}
+
+// 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 map[string]bool
+}
+
+// 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) map[string]bool {
+ m := ccUUIDMemo[f.path]
+ if m == nil || f.size < m.size || f.size == m.size && f.mod.UnixNano() != m.mod {
+ m = &ccUUIDs{set: map[string]bool{}}
+ 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[r[1].String()] = true
+ }
+ return true
+ })
+ if err == nil {
+ m.off = off
+ }
+ m.size, m.mod = f.size, f.mod.UnixNano()
+ return m.set
+}
+
+// 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
+}
diff --git a/internal/sessions/cc_copies_test.go b/internal/sessions/cc_copies_test.go
new file mode 100644
index 000000000..50de33f54
--- /dev/null
+++ b/internal/sessions/cc_copies_test.go
@@ -0,0 +1,238 @@
+package sessions
+
+import (
+ "os"
+ "path/filepath"
+ "slices"
+ "strings"
+ "testing"
+ "time"
+)
+
+// testdata/claude-forks is Claude Code 2.1.312's own files, from a sandbox
+// run with a fake API, trimmed of its request records and attachments'
+// text: original.jsonl is a session of two prompts, fork-on.jsonl is
+// `claude --resume <it> --fork-session` with a third prompt, and
+// fork-other.jsonl another such fork with a different third prompt. Both
+// forks start with every message of the original again, under the same
+// uuids and their own sessionId.
+const (
+ ccOriginal = "fb0bf5a9-e23f-4c29-b71c-a184c38bb005"
+ ccForkOn = "7e99b4bb-7d93-4b3c-9453-901a1a9298d1"
+ ccForkOther = "c2141c85-7cf5-46c0-a389-d584029233fb"
+)
+
+// ccForkFixture writes the fixtures named (by file, to the id it is saved
+// as) into dir/projects/-w-proj, oldest first, each a minute after the last
+// and all of them over a minute old, so none is still being written.
+// A copy saved under another id is the same file with its sessionId
+// rewritten, as /branch or a background move leaves one before a turn.
+func ccForkFixture(t *testing.T, dir string, files ...[2]string) {
+ t.Helper()
+ proj := filepath.Join(dir, "projects", "-w-proj")
+ if err := os.MkdirAll(proj, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ at := time.Now().Add(-time.Hour)
+ for _, f := range files {
+ b, err := os.ReadFile(filepath.Join("testdata", "claude-forks", f[0]))
+ if err != nil {
+ t.Fatal(err)
+ }
+ var from string
+ switch f[0] {
+ case "original.jsonl":
+ from = ccOriginal
+ case "fork-on.jsonl":
+ from = ccForkOn
+ default:
+ from = ccForkOther
+ }
+ b = []byte(strings.ReplaceAll(string(b), from, f[1]))
+ p := filepath.Join(proj, f[1]+".jsonl")
+ if err := os.WriteFile(p, b, 0o644); err != nil {
+ t.Fatal(err)
+ }
+ at = at.Add(time.Minute)
+ if err := os.Chtimes(p, at, at); err != nil {
+ t.Fatal(err)
+ }
+ }
+ Reset()
+}
+
+// ccRows is the ids of agent's sessions in the fixture's project, sorted.
+func ccRows(agent string) []string {
+ var ids []string
+ for _, m := range ListAgent(agent) {
+ if m.Cwd == "/w/proj" {
+ ids = append(ids, m.ID)
+ }
+ }
+ slices.Sort(ids)
+ return ids
+}
+
+func ccPrompts(t *testing.T, agent, id string) []string {
+ t.Helper()
+ s, ok := Find(agent, id)
+ if !ok {
+ t.Fatalf("Find(%s, %s) found nothing", agent, id)
+ }
+ tr, err := TranscriptOf(s)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var out []string
+ for _, p := range tr.Parts {
+ if p.Role == "user" && p.Kind == "text" {
+ out = append(out, p.Text)
+ }
+ }
+ return out
+}
+
+var ccCopyIDs = []string{
+ "11111111-1111-4111-8111-111111111111",
+ "22222222-2222-4222-8222-222222222222",
+ "33333333-3333-4333-8333-333333333333",
+}
+
+// Neo (#feedback): one Claude conversation listed as ten rows of the same
+// title, size and message count, one per copy Claude Code had made of it.
+// The copies and the session they were forked from are one row, the
+// session that holds all of them, and it reads whole.
+func TestClaudeForkCopiesAreOneRow(t *testing.T) {
+ claude, _ := setup(t)
+ before := agentCount("claude")
+ files := [][2]string{{"original.jsonl", ccOriginal}}
+ for _, id := range ccCopyIDs {
+ files = append(files, [2]string{"original.jsonl", id})
+ }
+ files = append(files, [2]string{"fork-on.jsonl", ccForkOn})
+ ccForkFixture(t, claude, files...)
+
+ if got := ccRows("claude"); !slices.Equal(got, []string{ccForkOn}) {
+ t.Fatalf("rows = %v, want only the fork that holds them all, %s", got, ccForkOn)
+ }
+ var listed []string
+ for _, s := range List(0) {
+ if s.Agent == "claude" && s.Cwd == "/w/proj" {
+ listed = append(listed, s.ID)
+ }
+ }
+ if !slices.Equal(listed, []string{ccForkOn}) {
+ t.Errorf("List rows = %v, want [%s]", listed, ccForkOn)
+ }
+ if got := agentCount("claude"); got != before+1 {
+ t.Errorf("Claude Code's count = %d, want %d: one conversation", got, before+1)
+ }
+ want := []string{"first prompt", "second prompt resumed", "third prompt forked"}
+ if got := ccPrompts(t, "claude", ccForkOn); !slices.Equal(got, want) {
+ t.Errorf("prompts = %q, want %q", got, want)
+ }
+
+ // what the conversation spent is counted once: three replies of 10 in,
+ // 5 out, not again for each copy of the first two
+ var tk Tokens
+ for _, s := range StatsFor(0).Sessions {
+ if s.Agent == "claude" && s.Cwd == "/w/proj" {
+ tk.Input += s.Input
+ tk.Output += s.Output
+ }
+ }
+ if tk.Input != 30 || tk.Output != 15 {
+ t.Errorf("tokens = %d in, %d out; want 30 in, 15 out", tk.Input, tk.Output)
+ }
+}
+
+// A fork that went on with a turn of its own is a session of its own, as
+// Claude Code's /resume lists it; the session both were forked from is in
+// each of them.
+func TestClaudeForkThatWentOnIsItsOwnRow(t *testing.T) {
+ claude, _ := setup(t)
+ ccForkFixture(t, claude,
+ [2]string{"original.jsonl", ccOriginal},
+ [2]string{"fork-on.jsonl", ccForkOn},
+ [2]string{"fork-other.jsonl", ccForkOther})
+ want := []string{ccForkOther, ccForkOn}
+ slices.Sort(want)
+ if got := ccRows("claude"); !slices.Equal(got, want) {
+ t.Fatalf("rows = %v, want the two forks %v", got, want)
+ }
+ if got := ccPrompts(t, "claude", ccForkOther); !slices.Equal(got, []string{"first prompt", "second prompt resumed", "a different turn"}) {
+ t.Errorf("the other fork's prompts = %q", got)
+ }
+}
+
+// Deleting the conversation takes its copies along, or the next list
+// would show the newest of them in its place; restoring brings them back.
+func TestClaudeDeleteTakesCopiesAlong(t *testing.T) {
+ claude, _ := setup(t)
+ ccForkFixture(t, claude,
+ [2]string{"original.jsonl", ccOriginal},
+ [2]string{"original.jsonl", ccCopyIDs[0]},
+ [2]string{"fork-on.jsonl", ccForkOn})
+ if got := ccRows("claude"); !slices.Equal(got, []string{ccForkOn}) {
+ t.Fatalf("rows = %v", got)
+ }
+ tr, err := Delete("claude", ccForkOn)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got := ccRows("claude"); len(got) != 0 {
+ t.Errorf("after delete, rows = %v: a copy came back", got)
+ }
+ proj := filepath.Join(claude, "projects", "-w-proj")
+ for _, id := range []string{ccOriginal, ccCopyIDs[0], ccForkOn} {
+ if _, err := os.Stat(filepath.Join(proj, id+".jsonl")); !os.IsNotExist(err) {
+ t.Errorf("%s left behind: %v", id, err)
+ }
+ }
+ if _, err := Restore(tr.Key); err != nil {
+ t.Fatal(err)
+ }
+ for _, id := range []string{ccOriginal, ccCopyIDs[0], ccForkOn} {
+ if _, err := os.Stat(filepath.Join(proj, id+".jsonl")); err != nil {
+ t.Errorf("%s not restored: %v", id, err)
+ }
+ }
+ if got := ccRows("claude"); !slices.Equal(got, []string{ccForkOn}) {
+ t.Errorf("after restore, rows = %v", got)
+ }
+}
+
+// Qoder writes Claude Code's files, and its copies are one row too.
+func TestQoderForkCopiesAreOneRow(t *testing.T) {
+ for _, agent := range []string{"qoder", "qoder-cn"} {
+ t.Run(agent, func(t *testing.T) {
+ setup(t)
+ ccForkFixture(t, QoderDir(agent),
+ [2]string{"original.jsonl", ccOriginal},
+ [2]string{"original.jsonl", ccCopyIDs[0]},
+ [2]string{"fork-on.jsonl", ccForkOn})
+ if got := ccRows(agent); !slices.Equal(got, []string{ccForkOn}) {
+ t.Fatalf("rows = %v, want [%s]", got, ccForkOn)
+ }
+ })
+ }
+}
+
+func agentCount(agent string) int {
+ for _, a := range Agents() {
+ if a.Agent == agent {
+ return a.Count
+ }
+ }
+ return 0
+}
+
+// The gateway may know a copy's id from its requests: that is the listed
+// session's, not a gateway-only session of its own.
+func TestMergeExternalKnowsCopies(t *testing.T) {
+ native := []Session{{Agent: "claude", ID: ccForkOn, Copies: []string{ccOriginal}}}
+ out := MergeExternal(native, []ExternalSession{{Agent: "claude", ID: ccOriginal, Title: "x"}})
+ if len(out) != 1 {
+ t.Errorf("rows = %d, want 1: the copy came back from the gateway", len(out))
+ }
+}
diff --git a/internal/sessions/claude.go b/internal/sessions/claude.go
index 7072f1752..0a818f250 100644
--- a/internal/sessions/claude.go
+++ b/internal/sessions/claude.go
@@ -20,6 +20,7 @@ type ccLine struct {
IsSidechain bool `json:"isSidechain"`
Cwd string `json:"cwd"`
SessionID string `json:"sessionId"`
+ UUID string `json:"uuid"`
RequestID ccStr `json:"requestId"`
AITitle string `json:"aiTitle"`
CustomTitle string `json:"customTitle"`
@@ -94,6 +95,7 @@ func claudeLine(s *state, b []byte, main bool) {
at := tsAt(b, true)
s.saw(at, main)
ccWhere(s, b)
+ ccLink(s, h, b, main)
ccReply(s, at, msg, b, main)
case bytes.HasPrefix(msg, ccRole) && typeAfter(msg, ccRole) == "user":
if main && !ccToolResult(msg) {
@@ -103,11 +105,64 @@ func claudeLine(s *state, b []byte, main bool) {
}
s.saw(tsAt(b, true), main)
ccWhere(s, b)
+ ccLink(s, h, b, main)
default:
claudeFull(s, b, main)
}
}
+var ccSidechain = []byte(`"isSidechain":true`)
+
+// ccLink notes a message of the file's own conversation by its uuid, the
+// first as Root and the last as Leaf. Claude Code copies a conversation
+// into a new file under the same uuids when it forks one (--fork-session,
+// /branch) or moves it to a background session, so a file whose Leaf
+// another file holds is a copy of that one's conversation (ccCopies). The
+// uuid is the line's own: a tool's input or result can hold one too.
+func ccLink(s *state, h, b []byte, main bool) {
+ if !main || bytes.Contains(h, ccSidechain) {
+ return
+ }
+ if u := gjson.GetBytes(b, "uuid"); u.Type == gjson.String {
+ s.link(u.String())
+ }
+}
+
+// link notes uuid as the file's latest message. Only a uuid as Claude
+// Code makes them, random, tells a copy: a file whose writer numbers its
+// messages, or names them "u" as tests do, shares them with every other.
+func (s *state) link(uuid string) {
+ if !ccRandomUUID(uuid) {
+ return
+ }
+ if s.Root == "" {
+ s.Root = uuid
+ }
+ s.Leaf = uuid
+}
+
+// ccRandomUUID reports whether u is a version 4 uuid, as Claude Code's
+// message uuids are.
+func ccRandomUUID(u string) bool {
+ if len(u) != 36 || u[14] != '4' {
+ return false
+ }
+ for i := 0; i < 36; i++ {
+ c := u[i]
+ switch i {
+ case 8, 13, 18, 23:
+ if c != '-' {
+ return false
+ }
+ default:
+ if !('0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F') {
+ return false
+ }
+ }
+ }
+ return true
+}
+
// ccWhere takes the session's folder and id from the end of a line, while
// they are not known.
func ccWhere(s *state, b []byte) {
@@ -273,6 +328,9 @@ func claudeFull(s *state, b []byte, main bool) {
if s.ID == "" && l.SessionID != "" {
s.ID = l.SessionID
}
+ if main && !l.IsSidechain && (l.Type == "user" || l.Type == "assistant") {
+ s.link(l.UUID)
+ }
switch l.Type {
case "ai-title":
if l.AITitle != "" {
diff --git a/internal/sessions/external.go b/internal/sessions/external.go
index 3b79137ec..80e5c5001 100644
--- a/internal/sessions/external.go
+++ b/internal/sessions/external.go
@@ -37,6 +37,9 @@ func MergeExternal(native []Session, external []ExternalSession) []Session {
seen := map[string]bool{}
for _, s := range native {
seen[s.Agent+"|"+s.ID] = true
+ for _, id := range s.Copies {
+ seen[s.Agent+"|"+id] = true
+ }
}
out := append([]Session(nil), native...)
for _, e := range external {
diff --git a/internal/sessions/manage.go b/internal/sessions/manage.go
index 5bdb8677b..529ee3f97 100644
--- a/internal/sessions/manage.go
+++ b/internal/sessions/manage.go
@@ -91,7 +91,13 @@ func Agents() []AgentCount {
func agentCounts(all []file) []AgentCount {
n := map[string]map[string]bool{}
+ // as listAgent lists them, from the parses kept: a copy of a
+ // conversation is not a session of its own (ccCopies)
+ copies := ccCopies(all)
for _, f := range all {
+ if copies[f.key] != "" {
+ continue
+ }
if n[f.agent] == nil {
n[f.agent] = map[string]bool{}
}
@@ -216,10 +222,16 @@ func listAgent(agent string, all []file) []Managed {
identities = codexIdentitySnapshot(mine)
taskTitles = codexSubagentTaskTitles(mine)
}
+ copies := ccCopies(mine)
+ copyIDs := copiesByKeeper(copies, groups)
price, titles := pricer(), codexTitleOf()
out := []Managed{}
- for _, fs := range groups {
+ for k, fs := range groups {
+ if copies[k] != "" {
+ continue
+ }
s, _ := assemble(fs, price, titles)
+ s.Copies = copyIDs[k]
setSessionCodexIdentity(&s, identities)
setSessionCodexTaskTitle(&s, taskTitles)
listed(&s)
@@ -322,10 +334,11 @@ func Delete(agent, id string) (Trashed, error) {
// the session as the page lists it: by the id assemble gives it
var fs []file
var s Session
+ key := ""
price, titles := pricer(), codexTitleOf()
- for _, g := range groups {
+ for k, g := range groups {
if one, _ := assembleListed(g, price, titles); one.ID == id {
- fs, s = g, one
+ fs, s, key = g, one, k
break
}
}
@@ -336,6 +349,16 @@ func Delete(agent, id string) (Trashed, error) {
return Trashed{}, errors.New("this session is read only")
}
paths := sessionPaths(agent, id, fs)
+ // the copies of its conversation go with it: they were listed as
+ // this session, and one would come back as its row after (ccCopies)
+ if len(paths) > 0 {
+ for k, keeper := range ccCopies(mine) {
+ if c := groups[k]; keeper == key && len(c) > 0 && !c[0].readOnly {
+ paths = append(paths, sessionPaths(agent, sessionID(c), c)...)
+ fs = append(fs[:len(fs):len(fs)], c...)
+ }
+ }
+ }
if len(paths) == 0 {
// a stopped distro's listing, of files since gone
wslForget(fs)
@@ -375,6 +398,7 @@ func Delete(agent, id string) (Trashed, error) {
}
for _, f := range fs {
delete(cache, f.path)
+ delete(ccUUIDMemo, f.path)
}
saveCache()
wslForget(fs)
diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go
index ca9c1fb3c..5dbd9b26e 100644
--- a/internal/sessions/sessions.go
+++ b/internal/sessions/sessions.go
@@ -151,6 +151,9 @@ type Session struct {
// Convs are the ids magpie's gateway keys the session's requests by
// when they carry no session id of their own (dshConvs)
Convs []string `json:"-"`
+ // Copies are the ids of the sessions that are copies of its
+ // conversation, not listed apart from it (ccCopies)
+ Copies []string `json:"-"`
}
// PriceOf is the effective price of a model as a session names it, at the
@@ -208,6 +211,10 @@ type state struct {
MsgModel string `json:"msg_model,omitempty"`
MsgUse Tokens `json:"msg_use"`
MsgDay string `json:"msg_day,omitempty"`
+ // Claude Code: the uuids of the first and the last message of the
+ // file's own conversation (ccLink)
+ Root string `json:"root,omitempty"`
+ Leaf string `json:"leaf,omitempty"`
// Codex: the model in use, and its running total (input with cache) last seen
Model string `json:"model,omitempty"`
Total *Tokens `json:"total,omitempty"`
@@ -922,7 +929,8 @@ func CachePath() string { return filepath.Join(filepath.Dir(catalog.CachePath())
// 20: Codex's source and direct parent thread identity.
// 21: Codex subagent nicknames and parent spawn task names.
// 22: dsh's gateway conversation ids (Convs).
-const cacheVersion = 22
+// 23: Claude Code's first and last message uuids (Root, Leaf).
+const cacheVersion = 23
type cacheFile struct {
Version int `json:"version"`
@@ -1427,10 +1435,17 @@ func listSessions(limit int) []Session {
identities := codexIdentitySnapshot(files)
taskTitles := codexSubagentTaskTitles(files)
+ copies := ccCopies(files)
+ copyIDs := copiesByKeeper(copies, groups)
+
price, titles := pricer(), codexTitleOf()
out := []Session{}
for _, k := range keys {
+ if copies[k] != "" {
+ continue
+ }
if s, ok := assemble(groups[k], price, titles); ok {
+ s.Copies = copyIDs[k]
setSessionCodexIdentity(&s, identities)
setSessionCodexTaskTitle(&s, taskTitles)
out = append(out, s)
diff --git a/internal/sessions/stats.go b/internal/sessions/stats.go
index 07203308e..a2024380a 100644
--- a/internal/sessions/stats.go
+++ b/internal/sessions/stats.go
@@ -199,9 +199,12 @@ func statsRead(days int, now time.Time, native map[string]bool) Stats {
}
}
}
+ // a copy of a conversation is counted in the session it copies, which
+ // holds every message of it (ccCopies)
+ copies := ccCopies(files)
var want []file
for _, f := range files {
- if !f.mod.Before(since) {
+ if !f.mod.Before(since) && copies[f.key] == "" {
want = append(want, f)
}
}
diff --git a/internal/sessions/testdata/claude-forks/fork-on.jsonl b/internal/sessions/testdata/claude-forks/fork-on.jsonl
new file mode 100644
index 000000000..3e45a1dd2
--- /dev/null
+++ b/internal/sessions/testdata/claude-forks/fork-on.jsonl
@@ -0,0 +1,27 @@
+{"type":"mode","mode":"normal","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1"}
+{"type":"atis-latch","atis":"","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1"}
+{"type":"queue-operation","operation":"enqueue","timestamp":"2026-10-28T02:00:46.283Z","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1","content":"third prompt forked"}
+{"type":"queue-operation","operation":"dequeue","timestamp":"2026-10-28T02:00:46.284Z","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1"}
+{"parentUuid":null,"isSidechain":false,"promptId":"8d23f3d6-034f-4cb4-a84f-c68995420e53","type":"user","message":{"role":"user","content":"first prompt"},"uuid":"d5d9a092-79c3-4b10-9cfc-05d40d2c1c84","timestamp":"2026-10-28T02:00:38.961Z","permissionMode":"default","promptSource":"sdk","turnOrigin":"sdk","turnPosition":{"promptIndex":0,"turnIndex":1},"userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1","version":"2.1.312","gitBranch":""}
+{"parentUuid":"d5d9a092-79c3-4b10-9cfc-05d40d2c1c84","isSidechain":false,"attachment":{"type":"environment"},"type":"attachment","uuid":"3ee1d058-98b3-4d58-bad4-6fa7498cdb37","timestamp":"2026-10-28T02:00:38.960Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1","version":"2.1.312","gitBranch":""}
+{"parentUuid":"3ee1d058-98b3-4d58-bad4-6fa7498cdb37","isSidechain":false,"attachment":{"type":"model"},"type":"attachment","uuid":"2ce4f96c-e3aa-4e9e-922f-bdccb7f533f4","timestamp":"2026-10-28T02:00:38.960Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1","version":"2.1.312","gitBranch":""}
+{"parentUuid":"2ce4f96c-e3aa-4e9e-922f-bdccb7f533f4","isSidechain":false,"attachment":{"type":"deferred_tools_delta"},"type":"attachment","uuid":"62d68463-b45c-49a6-be57-ff654856de8e","timestamp":"2026-10-28T02:00:38.960Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1","version":"2.1.312","gitBranch":""}
+{"parentUuid":"62d68463-b45c-49a6-be57-ff654856de8e","isSidechain":false,"attachment":{"type":"agent_listing_delta"},"type":"attachment","uuid":"452a9782-0872-4e15-8f75-d72668e1736b","timestamp":"2026-10-28T02:00:38.960Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1","version":"2.1.312","gitBranch":""}
+{"parentUuid":"452a9782-0872-4e15-8f75-d72668e1736b","isSidechain":false,"attachment":{"type":"skill_listing"},"type":"attachment","uuid":"6d8f7dae-bbcb-4ce3-84c4-aa7b81214825","timestamp":"2026-10-28T02:00:38.960Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1","version":"2.1.312","gitBranch":""}
+{"parentUuid":"6d8f7dae-bbcb-4ce3-84c4-aa7b81214825","isSidechain":false,"attachment":{"type":"total_tokens_reminder"},"type":"attachment","uuid":"c5d68e02-2b87-4709-bb42-8598267ce210","timestamp":"2026-10-28T02:00:38.960Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1","version":"2.1.312","gitBranch":""}
+{"parentUuid":"c5d68e02-2b87-4709-bb42-8598267ce210","isSidechain":false,"attachment":{"type":"instructions"},"type":"attachment","uuid":"9c8ebbfb-46d6-4f20-ae7f-5b542a2dfcdc","timestamp":"2026-10-28T02:00:39.553Z","renderedRole":"user","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1","version":"2.1.312","gitBranch":""}
+{"parentUuid":"9c8ebbfb-46d6-4f20-ae7f-5b542a2dfcdc","isSidechain":false,"attachment":{"type":"session_context"},"type":"attachment","uuid":"9a6f1ab3-9703-404a-86d7-860a335918cf","timestamp":"2026-10-28T02:00:39.553Z","renderedRole":"user","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1","version":"2.1.312","gitBranch":""}
+{"parentUuid":"9a6f1ab3-9703-404a-86d7-860a335918cf","isSidechain":false,"attachment":{"type":"date"},"type":"attachment","uuid":"04e2708b-d20a-4e1e-a4cd-ebc92dbce802","timestamp":"2026-10-28T02:00:39.553Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1","version":"2.1.312","gitBranch":""}
+{"parentUuid":"04e2708b-d20a-4e1e-a4cd-ebc92dbce802","isSidechain":false,"attachment":{"type":"remote_session_change"},"type":"attachment","uuid":"83a6a2e3-ac91-4bad-91cc-f4363b8d7d96","timestamp":"2026-10-28T02:00:39.554Z","renderedRole":"user","renderedBesideToolResult":true,"userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1","version":"2.1.312","gitBranch":""}
+{"parentUuid":"83a6a2e3-ac91-4bad-91cc-f4363b8d7d96","isSidechain":false,"attachment":{"type":"prompt_snapshot"},"type":"attachment","uuid":"ed13bc7d-2e41-4b6c-9232-20483b8ccf14","timestamp":"2026-10-28T02:00:39.555Z","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1","version":"2.1.312","gitBranch":""}
+{"parentUuid":"ed13bc7d-2e41-4b6c-9232-20483b8ccf14","isSidechain":false,"message":{"id":"msg_42fbd77ef9324c1a90f4","type":"message","role":"assistant","model":"claude-opus-5-5","content":[{"type":"text","text":"{\"title\": \"Fake title here\"}"}],"stop_reason":"end_turn","stop_sequence":null,"usage":{"input_tokens":10,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":5,"output_tokens_details":{"thinking_tokens":0},"server_tool_use":{"web_search_requests":0,"web_fetch_requests":0},"service_tier":"standard","cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"inference_geo":"","iterations":[],"speed":"standard","fallback_credit":null},"stop_details":null},"apiBlockIndex":0,"requestRef":"d70f8cd4-b7f4-4787-94a5-1cd42d4a381f","requestedModel":"claude-opus-5-5","type":"assistant","uuid":"49d96869-bc36-471a-9df5-fd8fcf2483d7","timestamp":"2026-10-28T02:00:39.566Z","effort":"medium","perTurnEffort":"medium","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1","version":"2.1.312","gitBranch":""}
+{"parentUuid":"49d96869-bc36-471a-9df5-fd8fcf2483d7","isSidechain":false,"attachment":{"type":"prompt_snapshot"},"type":"attachment","uuid":"689ab8ec-d966-4c19-bc90-35c2fa438b1c","timestamp":"2026-10-28T02:00:39.568Z","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1","version":"2.1.312","gitBranch":""}
+{"parentUuid":"689ab8ec-d966-4c19-bc90-35c2fa438b1c","isSidechain":false,"promptId":"8d23f3d6-034f-4cb4-a84f-c68995420e53","type":"user","message":{"role":"user","content":"second prompt resumed"},"uuid":"c4a8a9b0-19b0-4c61-95aa-29ee6e8be191","timestamp":"2026-10-28T02:00:46.121Z","permissionMode":"default","promptSource":"sdk","turnOrigin":"sdk","turnPosition":{"promptIndex":0,"turnIndex":2},"userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1","version":"2.1.312","gitBranch":""}
+{"parentUuid":"c4a8a9b0-19b0-4c61-95aa-29ee6e8be191","isSidechain":false,"attachment":{"type":"total_tokens_reminder"},"type":"attachment","uuid":"78f4e138-954f-4675-a82a-d1a7d281ad32","timestamp":"2026-10-28T02:00:46.120Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1","version":"2.1.312","gitBranch":""}
+{"parentUuid":"78f4e138-954f-4675-a82a-d1a7d281ad32","isSidechain":false,"message":{"id":"msg_1ee6a49d0a8549358ad5","type":"message","role":"assistant","model":"claude-opus-5-5","content":[{"type":"text","text":"{\"title\": \"Fake title here\"}"}],"stop_reason":"end_turn","stop_sequence":null,"usage":{"input_tokens":10,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":5,"output_tokens_details":{"thinking_tokens":0},"server_tool_use":{"web_search_requests":0,"web_fetch_requests":0},"service_tier":"standard","cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"inference_geo":"","iterations":[],"speed":"standard","fallback_credit":null},"stop_details":null},"apiBlockIndex":0,"requestRef":"4daaf26b-7512-4de0-bae2-7de5fde0a210","requestedModel":"claude-opus-5-5","type":"assistant","uuid":"74b57f13-e31f-4f67-900f-bd17e06ebe5f","timestamp":"2026-10-28T02:00:46.140Z","effort":"medium","perTurnEffort":"medium","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1","version":"2.1.312","gitBranch":""}
+{"parentUuid":"74b57f13-e31f-4f67-900f-bd17e06ebe5f","isSidechain":false,"promptId":"8d23f3d6-034f-4cb4-a84f-c68995420e53","type":"user","message":{"role":"user","content":"third prompt forked"},"uuid":"6012637e-baf8-447f-960f-a4900abc5058","timestamp":"2026-10-28T02:00:46.298Z","permissionMode":"default","promptSource":"sdk","turnOrigin":"sdk","turnPosition":{"promptIndex":0,"turnIndex":3},"userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1","version":"2.1.312","gitBranch":""}
+{"parentUuid":"6012637e-baf8-447f-960f-a4900abc5058","isSidechain":false,"attachment":{"type":"total_tokens_reminder"},"type":"attachment","uuid":"9a5725fd-10e2-40b5-a256-9f0b7a8ee551","timestamp":"2026-10-28T02:00:46.297Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1","version":"2.1.312","gitBranch":""}
+{"parentUuid":"9a5725fd-10e2-40b5-a256-9f0b7a8ee551","isSidechain":false,"message":{"id":"msg_52dce875550441d1a8ed","type":"message","role":"assistant","model":"claude-opus-5-5","content":[{"type":"text","text":"{\"title\": \"Fake title here\"}"}],"stop_reason":"end_turn","stop_sequence":null,"usage":{"input_tokens":10,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":5,"output_tokens_details":{"thinking_tokens":0},"server_tool_use":{"web_search_requests":0,"web_fetch_requests":0},"service_tier":"standard","cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"inference_geo":"","iterations":[],"speed":"standard","fallback_credit":null},"stop_details":null},"apiBlockIndex":0,"requestRef":"69d37b8a-669c-4c8f-8885-e4e52a348dff","requestedModel":"claude-opus-5-5","type":"assistant","uuid":"8e9f0700-d1f0-41b2-a196-8467ead424a1","timestamp":"2026-10-28T02:00:46.316Z","effort":"medium","perTurnEffort":"medium","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1","version":"2.1.312","gitBranch":""}
+{"type":"last-prompt","lastPrompt":"first prompt","leafUuid":"8e9f0700-d1f0-41b2-a196-8467ead424a1","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1"}
+{"type":"mode","mode":"normal","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1"}
+{"type":"atis-latch","atis":"","sessionId":"7e99b4bb-7d93-4b3c-9453-901a1a9298d1"}
diff --git a/internal/sessions/testdata/claude-forks/fork-other.jsonl b/internal/sessions/testdata/claude-forks/fork-other.jsonl
new file mode 100644
index 000000000..be3ca9b9e
--- /dev/null
+++ b/internal/sessions/testdata/claude-forks/fork-other.jsonl
@@ -0,0 +1,27 @@
+{"type":"mode","mode":"normal","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb"}
+{"type":"atis-latch","atis":"","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb"}
+{"type":"queue-operation","operation":"enqueue","timestamp":"2026-10-28T02:09:32.305Z","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb","content":"a different turn"}
+{"type":"queue-operation","operation":"dequeue","timestamp":"2026-10-28T02:09:32.305Z","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb"}
+{"parentUuid":null,"isSidechain":false,"promptId":"75e4ccf6-333b-492a-b1ae-ba914d02ebae","type":"user","message":{"role":"user","content":"first prompt"},"uuid":"d5d9a092-79c3-4b10-9cfc-05d40d2c1c84","timestamp":"2026-10-28T02:00:38.961Z","permissionMode":"default","promptSource":"sdk","turnOrigin":"sdk","turnPosition":{"promptIndex":0,"turnIndex":1},"userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb","version":"2.1.312","gitBranch":""}
+{"parentUuid":"d5d9a092-79c3-4b10-9cfc-05d40d2c1c84","isSidechain":false,"attachment":{"type":"environment"},"type":"attachment","uuid":"3ee1d058-98b3-4d58-bad4-6fa7498cdb37","timestamp":"2026-10-28T02:00:38.960Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb","version":"2.1.312","gitBranch":""}
+{"parentUuid":"3ee1d058-98b3-4d58-bad4-6fa7498cdb37","isSidechain":false,"attachment":{"type":"model"},"type":"attachment","uuid":"2ce4f96c-e3aa-4e9e-922f-bdccb7f533f4","timestamp":"2026-10-28T02:00:38.960Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb","version":"2.1.312","gitBranch":""}
+{"parentUuid":"2ce4f96c-e3aa-4e9e-922f-bdccb7f533f4","isSidechain":false,"attachment":{"type":"deferred_tools_delta"},"type":"attachment","uuid":"62d68463-b45c-49a6-be57-ff654856de8e","timestamp":"2026-10-28T02:00:38.960Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb","version":"2.1.312","gitBranch":""}
+{"parentUuid":"62d68463-b45c-49a6-be57-ff654856de8e","isSidechain":false,"attachment":{"type":"agent_listing_delta"},"type":"attachment","uuid":"452a9782-0872-4e15-8f75-d72668e1736b","timestamp":"2026-10-28T02:00:38.960Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb","version":"2.1.312","gitBranch":""}
+{"parentUuid":"452a9782-0872-4e15-8f75-d72668e1736b","isSidechain":false,"attachment":{"type":"skill_listing"},"type":"attachment","uuid":"6d8f7dae-bbcb-4ce3-84c4-aa7b81214825","timestamp":"2026-10-28T02:00:38.960Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb","version":"2.1.312","gitBranch":""}
+{"parentUuid":"6d8f7dae-bbcb-4ce3-84c4-aa7b81214825","isSidechain":false,"attachment":{"type":"total_tokens_reminder"},"type":"attachment","uuid":"c5d68e02-2b87-4709-bb42-8598267ce210","timestamp":"2026-10-28T02:00:38.960Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb","version":"2.1.312","gitBranch":""}
+{"parentUuid":"c5d68e02-2b87-4709-bb42-8598267ce210","isSidechain":false,"attachment":{"type":"instructions"},"type":"attachment","uuid":"9c8ebbfb-46d6-4f20-ae7f-5b542a2dfcdc","timestamp":"2026-10-28T02:00:39.553Z","renderedRole":"user","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb","version":"2.1.312","gitBranch":""}
+{"parentUuid":"9c8ebbfb-46d6-4f20-ae7f-5b542a2dfcdc","isSidechain":false,"attachment":{"type":"session_context"},"type":"attachment","uuid":"9a6f1ab3-9703-404a-86d7-860a335918cf","timestamp":"2026-10-28T02:00:39.553Z","renderedRole":"user","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb","version":"2.1.312","gitBranch":""}
+{"parentUuid":"9a6f1ab3-9703-404a-86d7-860a335918cf","isSidechain":false,"attachment":{"type":"date"},"type":"attachment","uuid":"04e2708b-d20a-4e1e-a4cd-ebc92dbce802","timestamp":"2026-10-28T02:00:39.553Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb","version":"2.1.312","gitBranch":""}
+{"parentUuid":"04e2708b-d20a-4e1e-a4cd-ebc92dbce802","isSidechain":false,"attachment":{"type":"remote_session_change"},"type":"attachment","uuid":"83a6a2e3-ac91-4bad-91cc-f4363b8d7d96","timestamp":"2026-10-28T02:00:39.554Z","renderedRole":"user","renderedBesideToolResult":true,"userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb","version":"2.1.312","gitBranch":""}
+{"parentUuid":"83a6a2e3-ac91-4bad-91cc-f4363b8d7d96","isSidechain":false,"attachment":{"type":"prompt_snapshot"},"type":"attachment","uuid":"ed13bc7d-2e41-4b6c-9232-20483b8ccf14","timestamp":"2026-10-28T02:00:39.555Z","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb","version":"2.1.312","gitBranch":""}
+{"parentUuid":"ed13bc7d-2e41-4b6c-9232-20483b8ccf14","isSidechain":false,"message":{"id":"msg_42fbd77ef9324c1a90f4","type":"message","role":"assistant","model":"claude-opus-5-5","content":[{"type":"text","text":"{\"title\": \"Fake title here\"}"}],"stop_reason":"end_turn","stop_sequence":null,"usage":{"input_tokens":10,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":5,"output_tokens_details":{"thinking_tokens":0},"server_tool_use":{"web_search_requests":0,"web_fetch_requests":0},"service_tier":"standard","cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"inference_geo":"","iterations":[],"speed":"standard","fallback_credit":null},"stop_details":null},"apiBlockIndex":0,"requestRef":"d70f8cd4-b7f4-4787-94a5-1cd42d4a381f","requestedModel":"claude-opus-5-5","type":"assistant","uuid":"49d96869-bc36-471a-9df5-fd8fcf2483d7","timestamp":"2026-10-28T02:00:39.566Z","effort":"medium","perTurnEffort":"medium","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb","version":"2.1.312","gitBranch":""}
+{"parentUuid":"49d96869-bc36-471a-9df5-fd8fcf2483d7","isSidechain":false,"attachment":{"type":"prompt_snapshot"},"type":"attachment","uuid":"689ab8ec-d966-4c19-bc90-35c2fa438b1c","timestamp":"2026-10-28T02:00:39.568Z","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb","version":"2.1.312","gitBranch":""}
+{"parentUuid":"689ab8ec-d966-4c19-bc90-35c2fa438b1c","isSidechain":false,"promptId":"75e4ccf6-333b-492a-b1ae-ba914d02ebae","type":"user","message":{"role":"user","content":"second prompt resumed"},"uuid":"c4a8a9b0-19b0-4c61-95aa-29ee6e8be191","timestamp":"2026-10-28T02:00:46.121Z","permissionMode":"default","promptSource":"sdk","turnOrigin":"sdk","turnPosition":{"promptIndex":0,"turnIndex":2},"userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb","version":"2.1.312","gitBranch":""}
+{"parentUuid":"c4a8a9b0-19b0-4c61-95aa-29ee6e8be191","isSidechain":false,"attachment":{"type":"total_tokens_reminder"},"type":"attachment","uuid":"78f4e138-954f-4675-a82a-d1a7d281ad32","timestamp":"2026-10-28T02:00:46.120Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb","version":"2.1.312","gitBranch":""}
+{"parentUuid":"78f4e138-954f-4675-a82a-d1a7d281ad32","isSidechain":false,"message":{"id":"msg_1ee6a49d0a8549358ad5","type":"message","role":"assistant","model":"claude-opus-5-5","content":[{"type":"text","text":"{\"title\": \"Fake title here\"}"}],"stop_reason":"end_turn","stop_sequence":null,"usage":{"input_tokens":10,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":5,"output_tokens_details":{"thinking_tokens":0},"server_tool_use":{"web_search_requests":0,"web_fetch_requests":0},"service_tier":"standard","cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"inference_geo":"","iterations":[],"speed":"standard","fallback_credit":null},"stop_details":null},"apiBlockIndex":0,"requestRef":"4daaf26b-7512-4de0-bae2-7de5fde0a210","requestedModel":"claude-opus-5-5","type":"assistant","uuid":"74b57f13-e31f-4f67-900f-bd17e06ebe5f","timestamp":"2026-10-28T02:00:46.140Z","effort":"medium","perTurnEffort":"medium","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb","version":"2.1.312","gitBranch":""}
+{"parentUuid":"74b57f13-e31f-4f67-900f-bd17e06ebe5f","isSidechain":false,"promptId":"75e4ccf6-333b-492a-b1ae-ba914d02ebae","type":"user","message":{"role":"user","content":"a different turn"},"uuid":"af050f06-4cf5-48b6-b701-776cb0035c9e","timestamp":"2026-10-28T02:09:32.319Z","permissionMode":"default","promptSource":"sdk","turnOrigin":"sdk","turnPosition":{"promptIndex":0,"turnIndex":3},"userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb","version":"2.1.312","gitBranch":""}
+{"parentUuid":"af050f06-4cf5-48b6-b701-776cb0035c9e","isSidechain":false,"attachment":{"type":"total_tokens_reminder"},"type":"attachment","uuid":"67564524-1f96-41d5-a0c6-e7a6208a4cb4","timestamp":"2026-10-28T02:09:32.318Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb","version":"2.1.312","gitBranch":""}
+{"parentUuid":"67564524-1f96-41d5-a0c6-e7a6208a4cb4","isSidechain":false,"message":{"id":"msg_7fb0f28094f147c5833c","type":"message","role":"assistant","model":"claude-opus-5-5","content":[{"type":"text","text":"{\"title\": \"Fake title here\"}"}],"stop_reason":"end_turn","stop_sequence":null,"usage":{"input_tokens":10,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":5,"output_tokens_details":{"thinking_tokens":0},"server_tool_use":{"web_search_requests":0,"web_fetch_requests":0},"service_tier":"standard","cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"inference_geo":"","iterations":[],"speed":"standard","fallback_credit":null},"stop_details":null},"apiBlockIndex":0,"requestRef":"36176111-5e7c-4415-99ea-82af8e6ba517","requestedModel":"claude-opus-5-5","type":"assistant","uuid":"e034a55e-78ac-486a-9210-48ae0fbe3667","timestamp":"2026-10-28T02:09:32.337Z","effort":"medium","perTurnEffort":"medium","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb","version":"2.1.312","gitBranch":""}
+{"type":"last-prompt","lastPrompt":"first prompt","leafUuid":"e034a55e-78ac-486a-9210-48ae0fbe3667","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb"}
+{"type":"mode","mode":"normal","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb"}
+{"type":"atis-latch","atis":"","sessionId":"c2141c85-7cf5-46c0-a389-d584029233fb"}
diff --git a/internal/sessions/testdata/claude-forks/original.jsonl b/internal/sessions/testdata/claude-forks/original.jsonl
new file mode 100644
index 000000000..088d6b410
--- /dev/null
+++ b/internal/sessions/testdata/claude-forks/original.jsonl
@@ -0,0 +1,26 @@
+{"type":"queue-operation","operation":"enqueue","timestamp":"2026-10-28T02:00:38.943Z","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005","content":"first prompt"}
+{"type":"queue-operation","operation":"dequeue","timestamp":"2026-10-28T02:00:38.943Z","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005"}
+{"parentUuid":null,"isSidechain":false,"promptId":"39298a4c-8d2c-4884-a1f0-ce96adeff203","type":"user","message":{"role":"user","content":"first prompt"},"uuid":"d5d9a092-79c3-4b10-9cfc-05d40d2c1c84","timestamp":"2026-10-28T02:00:38.961Z","permissionMode":"default","promptSource":"sdk","turnOrigin":"sdk","turnPosition":{"promptIndex":0,"turnIndex":1},"userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005","version":"2.1.312","gitBranch":""}
+{"parentUuid":"d5d9a092-79c3-4b10-9cfc-05d40d2c1c84","isSidechain":false,"attachment":{"type":"environment"},"type":"attachment","uuid":"3ee1d058-98b3-4d58-bad4-6fa7498cdb37","timestamp":"2026-10-28T02:00:38.960Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005","version":"2.1.312","gitBranch":""}
+{"parentUuid":"3ee1d058-98b3-4d58-bad4-6fa7498cdb37","isSidechain":false,"attachment":{"type":"model"},"type":"attachment","uuid":"2ce4f96c-e3aa-4e9e-922f-bdccb7f533f4","timestamp":"2026-10-28T02:00:38.960Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005","version":"2.1.312","gitBranch":""}
+{"parentUuid":"2ce4f96c-e3aa-4e9e-922f-bdccb7f533f4","isSidechain":false,"attachment":{"type":"deferred_tools_delta"},"type":"attachment","uuid":"62d68463-b45c-49a6-be57-ff654856de8e","timestamp":"2026-10-28T02:00:38.960Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005","version":"2.1.312","gitBranch":""}
+{"parentUuid":"62d68463-b45c-49a6-be57-ff654856de8e","isSidechain":false,"attachment":{"type":"agent_listing_delta"},"type":"attachment","uuid":"452a9782-0872-4e15-8f75-d72668e1736b","timestamp":"2026-10-28T02:00:38.960Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005","version":"2.1.312","gitBranch":""}
+{"parentUuid":"452a9782-0872-4e15-8f75-d72668e1736b","isSidechain":false,"attachment":{"type":"skill_listing"},"type":"attachment","uuid":"6d8f7dae-bbcb-4ce3-84c4-aa7b81214825","timestamp":"2026-10-28T02:00:38.960Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005","version":"2.1.312","gitBranch":""}
+{"parentUuid":"6d8f7dae-bbcb-4ce3-84c4-aa7b81214825","isSidechain":false,"attachment":{"type":"total_tokens_reminder"},"type":"attachment","uuid":"c5d68e02-2b87-4709-bb42-8598267ce210","timestamp":"2026-10-28T02:00:38.960Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005","version":"2.1.312","gitBranch":""}
+{"type":"atis-latch","atis":"","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005"}
+{"parentUuid":"c5d68e02-2b87-4709-bb42-8598267ce210","isSidechain":false,"attachment":{"type":"instructions"},"type":"attachment","uuid":"9c8ebbfb-46d6-4f20-ae7f-5b542a2dfcdc","timestamp":"2026-10-28T02:00:39.553Z","renderedRole":"user","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005","version":"2.1.312","gitBranch":""}
+{"parentUuid":"9c8ebbfb-46d6-4f20-ae7f-5b542a2dfcdc","isSidechain":false,"attachment":{"type":"session_context"},"type":"attachment","uuid":"9a6f1ab3-9703-404a-86d7-860a335918cf","timestamp":"2026-10-28T02:00:39.553Z","renderedRole":"user","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005","version":"2.1.312","gitBranch":""}
+{"parentUuid":"9a6f1ab3-9703-404a-86d7-860a335918cf","isSidechain":false,"attachment":{"type":"date"},"type":"attachment","uuid":"04e2708b-d20a-4e1e-a4cd-ebc92dbce802","timestamp":"2026-10-28T02:00:39.553Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005","version":"2.1.312","gitBranch":""}
+{"parentUuid":"04e2708b-d20a-4e1e-a4cd-ebc92dbce802","isSidechain":false,"attachment":{"type":"remote_session_change"},"type":"attachment","uuid":"83a6a2e3-ac91-4bad-91cc-f4363b8d7d96","timestamp":"2026-10-28T02:00:39.554Z","renderedRole":"user","renderedBesideToolResult":true,"userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005","version":"2.1.312","gitBranch":""}
+{"parentUuid":"83a6a2e3-ac91-4bad-91cc-f4363b8d7d96","isSidechain":false,"attachment":{"type":"prompt_snapshot"},"type":"attachment","uuid":"ed13bc7d-2e41-4b6c-9232-20483b8ccf14","timestamp":"2026-10-28T02:00:39.555Z","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005","version":"2.1.312","gitBranch":""}
+{"parentUuid":"ed13bc7d-2e41-4b6c-9232-20483b8ccf14","isSidechain":false,"message":{"id":"msg_42fbd77ef9324c1a90f4","type":"message","role":"assistant","model":"claude-opus-5-5","content":[{"type":"text","text":"{\"title\": \"Fake title here\"}"}],"stop_reason":"end_turn","stop_sequence":null,"usage":{"input_tokens":10,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":5,"output_tokens_details":{"thinking_tokens":0},"server_tool_use":{"web_search_requests":0,"web_fetch_requests":0},"service_tier":"standard","cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"inference_geo":"","iterations":[],"speed":"standard","fallback_credit":null},"stop_details":null},"apiBlockIndex":0,"requestRef":"d70f8cd4-b7f4-4787-94a5-1cd42d4a381f","requestedModel":"claude-opus-5-5","type":"assistant","uuid":"49d96869-bc36-471a-9df5-fd8fcf2483d7","timestamp":"2026-10-28T02:00:39.566Z","effort":"medium","perTurnEffort":"medium","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005","version":"2.1.312","gitBranch":""}
+{"parentUuid":"49d96869-bc36-471a-9df5-fd8fcf2483d7","isSidechain":false,"attachment":{"type":"prompt_snapshot"},"type":"attachment","uuid":"689ab8ec-d966-4c19-bc90-35c2fa438b1c","timestamp":"2026-10-28T02:00:39.568Z","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005","version":"2.1.312","gitBranch":""}
+{"type":"last-prompt","lastPrompt":"first prompt","leafUuid":"689ab8ec-d966-4c19-bc90-35c2fa438b1c","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005"}
+{"type":"atis-latch","atis":"","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005"}
+{"type":"queue-operation","operation":"enqueue","timestamp":"2026-10-28T02:00:46.107Z","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005","content":"second prompt resumed"}
+{"type":"queue-operation","operation":"dequeue","timestamp":"2026-10-28T02:00:46.107Z","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005"}
+{"parentUuid":"689ab8ec-d966-4c19-bc90-35c2fa438b1c","isSidechain":false,"promptId":"f6b763b6-5b1e-4520-b4ca-7747c80a608c","type":"user","message":{"role":"user","content":"second prompt resumed"},"uuid":"c4a8a9b0-19b0-4c61-95aa-29ee6e8be191","timestamp":"2026-10-28T02:00:46.121Z","permissionMode":"default","promptSource":"sdk","turnOrigin":"sdk","turnPosition":{"promptIndex":0,"turnIndex":2},"userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005","version":"2.1.312","gitBranch":""}
+{"parentUuid":"c4a8a9b0-19b0-4c61-95aa-29ee6e8be191","isSidechain":false,"attachment":{"type":"total_tokens_reminder"},"type":"attachment","uuid":"78f4e138-954f-4675-a82a-d1a7d281ad32","timestamp":"2026-10-28T02:00:46.120Z","renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005","version":"2.1.312","gitBranch":""}
+{"parentUuid":"78f4e138-954f-4675-a82a-d1a7d281ad32","isSidechain":false,"message":{"id":"msg_1ee6a49d0a8549358ad5","type":"message","role":"assistant","model":"claude-opus-5-5","content":[{"type":"text","text":"{\"title\": \"Fake title here\"}"}],"stop_reason":"end_turn","stop_sequence":null,"usage":{"input_tokens":10,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":5,"output_tokens_details":{"thinking_tokens":0},"server_tool_use":{"web_search_requests":0,"web_fetch_requests":0},"service_tier":"standard","cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"inference_geo":"","iterations":[],"speed":"standard","fallback_credit":null},"stop_details":null},"apiBlockIndex":0,"requestRef":"4daaf26b-7512-4de0-bae2-7de5fde0a210","requestedModel":"claude-opus-5-5","type":"assistant","uuid":"74b57f13-e31f-4f67-900f-bd17e06ebe5f","timestamp":"2026-10-28T02:00:46.140Z","effort":"medium","perTurnEffort":"medium","userType":"external","entrypoint":"sdk-cli","cwd":"/w/proj","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005","version":"2.1.312","gitBranch":""}
+{"type":"last-prompt","lastPrompt":"second prompt resumed","leafUuid":"74b57f13-e31f-4f67-900f-bd17e06ebe5f","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005"}
+{"type":"mode","mode":"normal","sessionId":"fb0bf5a9-e23f-4c29-b71c-a184c38bb005"}