diff --git a/internal/sessions/cline.go b/internal/sessions/cline.go index 7374e2ce1..4b3d2d150 100644 --- a/internal/sessions/cline.go +++ b/internal/sessions/cline.go @@ -95,38 +95,87 @@ type clineMessage struct { } `json:"metrics"` } -// parseCline reads a session's messages whole, as they are written. -func parseCline(f file) *state { - s := &state{Size: f.size, Mod: f.mod.UnixNano()} - var since time.Time +// clineCarry is what a read of a session's messages.json keeps, to know +// whether it can stand for the next: the file as it was read, and what of +// the manifest its counts were made under. Prompt is the first prompt in +// it that names the session, for when the manifest's doesn't. +type clineCarry struct { + Size int64 `json:"size"` + Mod int64 `json:"mod"` + Since time.Time `json:"since,omitzero"` + Start time.Time `json:"start,omitzero"` + Prompt string `json:"prompt,omitempty"` +} + +// parseCline reads a session's messages whole, as they are written, with +// its manifest. Cline rewrites the manifest on its own (a title given, the +// session touched), so when only that changed and it still says the same +// start and fork, what the messages were counted as stands and only the +// manifest is read again. +func parseCline(f file, old *state) *state { + var since, start time.Time + var m clineManifest + hasM := false if f.manifest != "" { - var m clineManifest if b, err := os.ReadFile(f.manifest); err == nil && json.Unmarshal(b, &m) == nil { - s.ID, s.Cwd = m.SessionID, m.Cwd - s.Named = title(m.Metadata.Title) + hasM = true if m.Metadata.Fork != nil { since, _ = time.Parse(time.RFC3339Nano, m.Metadata.Fork.ForkedAt) } - start, _ := time.Parse(time.RFC3339Nano, m.StartedAt) - if since.IsZero() || !start.Before(since) { - s.saw(start, f.main) + at, _ := time.Parse(time.RFC3339Nano, m.StartedAt) + if since.IsZero() || !at.Before(since) { + start = at } + } + } + name := func(s *state, prompt string) *state { + s.Size, s.Mod = f.size, f.mod.UnixNano() + s.ID, s.Cwd, s.Named, s.Title, s.First = "", "", "", "", "" + if hasM { + s.ID, s.Cwd = m.SessionID, m.Cwd + s.Named = title(m.Metadata.Title) clineTitle(s, m.Prompt) } + if f.main { + clineTitle(s, prompt) + } + return s + } + fi, err := os.Stat(f.path) + if err != nil { + return name(&state{}, "") + } + if o := old; o != nil && o.Cline != nil && o.Cline.Size == fi.Size() && o.Cline.Mod == fi.ModTime().UnixNano() && o.Cline.Since.Equal(since) && o.Cline.Start.Equal(start) { + s := o.clone() + return name(s, o.Cline.Prompt) // the title its messages gave, when the manifest gives none + + } + s := &state{} + s.saw(start, f.main) + c := &clineCarry{Size: fi.Size(), Mod: fi.ModTime().UnixNano(), Since: since, Start: start} + if clineHook != nil { + clineHook(f.path) } b, err := os.ReadFile(f.path) if err != nil { - return s + return name(s, "") } var doc struct { Messages []clineMessage `json:"messages"` } if json.Unmarshal(b, &doc) != nil { - return s + // half written, or not Cline's: read again when it changes + return name(s, "") } + named := false for _, m := range doc.Messages { - if f.main && m.Role == "user" && s.Title == "" && s.First == "" { - clineTitle(s, ccText(m.Content)) + if !named && m.Role == "user" { + // the first prompt that names the session by itself + var t state + text := ccText(m.Content) + if clineTitle(&t, text); t.Title != "" || t.First != "" { + c.Prompt, named = text, true + } } at := ms(m.TS) if !since.IsZero() && at.Before(since) { @@ -137,9 +186,14 @@ func parseCline(f file) *state { s.use(dateOf(at), m.ModelInfo.ID, Tokens{Input: max(0, u.Input-u.CacheRead-u.CacheWrite), Output: u.Output, CacheRead: u.CacheRead, CacheWrite: u.CacheWrite}) } } - return s + s.Cline = c + return name(s, c.Prompt) } +// clineHook, when a test sets it, is told each time a messages file is +// read. +var clineHook func(path string) + var ( clineNotice = regexp.MustCompile(`(?s)]*>.*?`) clineInput = regexp.MustCompile(`]*>`) diff --git a/internal/sessions/grow_test.go b/internal/sessions/grow_test.go index 3c7aecccb..a67233d66 100644 --- a/internal/sessions/grow_test.go +++ b/internal/sessions/grow_test.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" "testing" + "time" "github.com/klauspost/compress/zstd" ) @@ -400,3 +401,103 @@ func TestDshEndSeedAcrossReads(t *testing.T) { t.Fatalf("the fork's own: %+v, title %q", g.old.Models, g.old.Title) } } + +// Cline rewrites a session's manifest on its own (a title given, the +// session touched), and its size and time are the session file's: when only +// it changed, the messages are not read again. When they changed, or the +// manifest moved the start or the fork their counts were made under, they +// are. +func TestClineReadsMessagesOnlyWhenTheyChange(t *testing.T) { + inZone(t, 0) + home := setupAgents(t) + dir := filepath.Join(home, ".cline", "data", "sessions", clineMain) + path := filepath.Join(dir, clineMain+".messages.json") + manifest := filepath.Join(dir, clineMain+".json") + reads := 0 + clineHook = func(p string) { + if p == path { + reads++ + } + } + t.Cleanup(func() { clineHook = nil }) + at := time.Now() + touch := func(p string) { + at = at.Add(time.Minute) + if err := os.Chtimes(p, at, at); err != nil { + t.Fatal(err) + } + } + edit := func(p, from, to string) { + t.Helper() + b, err := os.ReadFile(p) + if err != nil { + t.Fatal(err) + } + if !bytes.Contains(b, []byte(from)) { + t.Fatalf("no %q in %s", from, p) + } + os.WriteFile(p, bytes.Replace(b, []byte(from), []byte(to), 1), 0o644) + touch(p) + } + var old *state + // read reads the session on from old and says whether it read its + // messages, checking it against a whole read + read := func() bool { + t.Helper() + var f file + for _, c := range clineFiles() { + if c.path == path { + f = c + } + } + was := reads + s := parse(f, old) + read := reads > was + whole := parse(f, nil) + got, _ := json.Marshal(s) + want, _ := json.Marshal(whole) + if !bytes.Equal(got, want) { + t.Fatalf("read on:\n%s\nread whole:\n%s", got, want) + } + old = s + return read + } + // no title of its own, so the first prompt names it + edit(manifest, `"title": "Importer speed-up"`, `"status": "x"`) + edit(manifest, `"prompt": "Speed up the\nimporter",`, ``) + if !read() { + t.Fatal("first read: messages not read") + } + if old.Title != "Speed up the importer" { + t.Fatalf("title %q", old.Title) + } + // the manifest touched and given a title: the messages are not read + edit(manifest, `"status": "completed"`, `"status": "running"`) + if read() { + t.Fatal("manifest touched: messages read again") + } + edit(manifest, `"status": "x"`, `"title": "Faster importer"`) + if read() || old.Named != "Faster importer" || old.Title != "Speed up the importer" { + t.Fatalf("manifest titled: named %q, title %q", old.Named, old.Title) + } + // a message more: read again + edit(path, `"content": "done",`, `"content": "done, all of it",`) + if !read() { + t.Fatal("messages changed: not read") + } + // the same size, a later time: read again + touch(path) + if !read() { + t.Fatal("messages touched: not read") + } + // made a fork after its first reply: what came before is its source's + edit(manifest, `"metadata": {`, `"metadata": {"fork": {"forkedAt": "2026-09-27T10:01:00.000Z"},`) + if !read() || old.Models["claude-opus-5-5"] != (Tokens{}) { + t.Fatalf("forked: %+v", old.Models) + } + // started at another time: read again + edit(manifest, `"started_at": "2026-09-27T10:00:00.000Z"`, `"started_at": "2026-09-27T10:05:00.000Z"`) + if !read() { + t.Fatal("start moved: not read") + } +} diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go index 44e88442d..f317bbc30 100644 --- a/internal/sessions/sessions.go +++ b/internal/sessions/sessions.go @@ -221,6 +221,8 @@ type state struct { Kimi *kimiCarry `json:"kimi,omitempty"` // dsh: where its file was read to, and its header Dsh *dshCarry `json:"dsh,omitempty"` + // Cline: the messages file its counts were read from + Cline *clineCarry `json:"cline,omitempty"` } // day is one local date's share of a file: tokens by model, and the time @@ -1200,7 +1202,7 @@ func parserFor(agent string) sessionParser { case "dsh": return sessionParser{grow: parseDsh} case "cline": - return sessionParser{whole: parseCline} + return sessionParser{grow: parseCline} case "grok": return sessionParser{grow: parseGrok} case "kimi":