sessions: a ZCode name poll stamps a copy, not the kept file list

v0.1.3800 (729be88e1) has allFilesForTitles give back the list it keeps
from poll to poll, and other polls read it meanwhile. Titles then
wrote each asked ZCode session's message count and time into that list
in place (zcodeTitleStamps): a data race with any poll reading it, and
the kept list no longer matched the rows, so it was made again on the
next pass. The stamps now go in a copy made at the first one that
changes a file; a poll with nothing to stamp copies nothing.

Audited the other users of allFiles/allFilesForTitles and the
in-place writes in internal/sessions (&files[i], .w(), sort.Slice):
the rest write into lists of their own (byKey copies, traceLineFiles
concatenates, callSources goes through distinctOf).

Run (mica): go test -count=1 -timeout 45m ./internal/sessions/ ok;
go test -race -count=3 -run 'TestZCode|TestFilesKept|Titles' ok; vet
ok; gofmt clean. Broken by hand: stamping in place again makes
TestZCodeTitleStampsLeaveKeptListAlone red ("the kept list was
stamped in place: size 1, mod 1790496500000").
Not run: the full ./... and GUI suites (one package touched).
This commit is contained in:
yetone
2026-11-12 02:44:19 +08:00
parent 4b002184ed
commit facc092d00
3 changed files with 49 additions and 4 deletions
+1 -1
View File
@@ -3067,7 +3067,7 @@ func Titles(keys []string) map[string]string {
subAsked[k] = f.path
}
}
zcodeTitleStamps(files, want)
files = zcodeTitleStamps(files, want)
groups := byKey(files, func(f *file) bool { return want[f.key] })
var fs []file
for _, f := range files {
+16 -3
View File
@@ -1,5 +1,7 @@
package sessions
import "slices"
// zcodeTitleFiles are ZCode's sessions as a title lookup needs them: the
// session rows alone, without the message join zcodeFiles makes for a
// session's size and last activity. Titles asks again every 15s, and that
@@ -47,8 +49,11 @@ func zcodeTitleFiles() []file {
// the row's time alone missed a first prompt typed since the last poll, and
// a stamp unlike the full listing's had the Sessions page and the poll each
// read the session again after the other. A session whose messages can't
// be read keeps its row's stamp.
func zcodeTitleStamps(files []file, want map[string]bool) {
// be read keeps its row's stamp. files is shared (allFilesForTitles keeps
// it from poll to poll, and other polls read it meanwhile), so the stamps
// go in a copy, made at the first one: the answer is files or that copy.
func zcodeTitleStamps(files []file, want map[string]bool) []file {
own := false
for i := range files {
f := &files[i]
if f.agent != "zcode" || !want[f.key] {
@@ -62,6 +67,14 @@ func zcodeTitleStamps(files []file, want map[string]bool) {
if zc.db.QueryRow(`SELECT COUNT(id), COALESCE(MAX(time_updated), 0) FROM message WHERE session_id = ?`, f.m().sid).Scan(&n, &last) != nil {
continue
}
f.size, f.mod = n, modOf(ms(max(f.mod.UnixMilli(), last)))
size, mod := n, modOf(ms(max(f.mod.UnixMilli(), last)))
if size == f.size && mod == f.mod {
continue
}
if !own {
files, own = slices.Clone(files), true
}
files[i].size, files[i].mod = size, mod
}
return files
}
+32
View File
@@ -4,6 +4,7 @@ import (
"database/sql"
"os"
"path/filepath"
"slices"
"testing"
)
@@ -266,3 +267,34 @@ func TestZCodeTitleIsTheAppsTaskName(t *testing.T) {
t.Fatalf("after a rename: %q, want Renamed", got["zcode:sess_aaaa"])
}
}
// A name poll stamps a ZCode session with its messages' count and time,
// but the list it reads is allFilesForTitles' kept one, shared with every
// poll meanwhile: the stamps go in a copy, and the kept list stays as the
// rows gave it.
func TestZCodeTitleStampsLeaveKeptListAlone(t *testing.T) {
home := setupAgents(t)
const id = "sess_9d0b1c8e-2f3a-4b5c-8d7e-6f5a4b3c2d1e"
db, err := sql.Open("sqlite", filepath.Join(home, ".zcode", "cli", "db", "db.sqlite"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { db.Close() })
if _, err := db.Exec(`INSERT INTO session (id, project_id, parent_id, slug, directory, title, title_source, version, time_created, time_updated) VALUES (?, 'p', NULL, 's', '/work/zc', 'Stamped', 'generated', '0.16.9', 1790496000000, 1790496000000)`, id); err != nil {
t.Fatal(err)
}
if _, err := db.Exec(`INSERT INTO message (id, session_id, time_created, time_updated, data) VALUES ('m1', ?, 1790496500000, 1790496500000, '{}')`, id); err != nil {
t.Fatal(err)
}
if got := Titles([]string{"zcode:" + id}); got["zcode:"+id] != "Stamped" {
t.Fatalf("title %v", got)
}
kept := allKept[1].last()
i := slices.IndexFunc(kept, func(f file) bool { return f.agent == "zcode" && f.m().sid == id })
if i < 0 {
t.Fatalf("no kept ZCode file for %s in %d files", id, len(kept))
}
if f := kept[i]; f.size != 0 || f.mod.UnixMilli() != 1790496000000 {
t.Fatalf("the kept list was stamped in place: size %d, mod %d", f.size, f.mod.UnixMilli())
}
}