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.