From 6d3caf6fa45173454c7b776e44548644530dcfca Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 19 Oct 2026 06:52:03 +0800 Subject: [PATCH] sessions: a Cline session whose manifest alone changed is not read again Cline keeps a session's messages in one JSON document, rewritten whole, and its title, start and fork in a manifest beside it, whose size and mtime are folded into the session file's. Cline rewrites that manifest on its own (a title given, the session's status touched), and every such rewrite made the Sessions and Usage pages decode the whole messages file again, though nothing in it had changed. parseCline now keeps what its messages were counted from: the messages file's own size and mtime, and the start and fork time the manifest gave then, with the first prompt that names the session. When the messages file is as it was and the manifest still gives the same start and fork, the counts stand and only the manifest is read again for the id, cwd, name and title. Messages rewritten (any size or mtime change), or a start or fork that moved, read them whole again as before. A messages document can't be read on from where it was: it is rewritten, not appended to. Measured (CPU, getrusage; 50 MB messages file, the manifest rewritten between reads; machine loaded): before: ~205-290 ms per read after a manifest change after: 0.1 ms per read after a manifest change; first read ~290 ms Test: TestClineReadsMessagesOnlyWhenTheyChange counts reads of the messages file (clineHook) and checks each read against a whole one: a manifest touched or titled reads none of it, and the title from the first prompt stays; a message changed, the same size at a later time, a fork added and the start moved each read it again. Broken by hand: no resume -> red ("manifest touched: messages read again"); the start and fork not compared -> red (read on differs from whole, after the fork); the carried prompt dropped -> red (read on differs from whole). Run: go test ./internal/sessions -run Cline -count=1 ok, and the whole package green at the previous commit; gofmt -l and go vet ./internal/sessions clean. The full-suite record is in the last commit of this series. --- internal/sessions/cline.go | 84 ++++++++++++++++++++++----- internal/sessions/grow_test.go | 101 +++++++++++++++++++++++++++++++++ internal/sessions/sessions.go | 4 +- 3 files changed, 173 insertions(+), 16 deletions(-) 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":