diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go index 1f44a7aa5..6a87c0fa1 100644 --- a/internal/sessions/sessions.go +++ b/internal/sessions/sessions.go @@ -2765,6 +2765,7 @@ func Titles(keys []string) map[string]string { subAsked[k] = f.path } } + zcodeTitleStamps(files, want) groups := byKey(files, func(f *file) bool { return want[f.key] }) var fs []file for _, f := range files { diff --git a/internal/sessions/zcode_titles.go b/internal/sessions/zcode_titles.go index de68c73c5..bb5ab3cdd 100644 --- a/internal/sessions/zcode_titles.go +++ b/internal/sessions/zcode_titles.go @@ -37,3 +37,28 @@ func zcodeTitleFiles() []file { } return ocRoots(out, parent) } + +// zcodeTitleStamps gives the ZCode sessions a name poll asks for the size +// and last activity zcodeFiles would give them, read from each one's own +// messages: ZCode writes a message without touching its session's row, so +// 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) { + for i := range files { + f := &files[i] + if f.agent != "zcode" || !want[f.key] { + continue + } + zc, ok := f.m().oc.(zcDB) + if !ok { + continue + } + var n, last int64 + 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, ms(max(f.mod.UnixMilli(), last)) + } +} diff --git a/internal/sessions/zcode_titles_test.go b/internal/sessions/zcode_titles_test.go index ba8de46b2..c4169f343 100644 --- a/internal/sessions/zcode_titles_test.go +++ b/internal/sessions/zcode_titles_test.go @@ -114,3 +114,62 @@ func TestZCodeDefaultTitleFallsBackToFirstPrompt(t *testing.T) { t.Fatalf("title %q, want %q (all %v)", got["zcode:"+id], prompt, got) } } + +// A name poll sees a ZCode session's new messages, as the full listing +// does: ZCode writes a message without touching its session's row, so a +// session still under its default title is named by its first prompt once +// that prompt is in, not when the row next changes. +func TestZCodeTitlesSeeNewMessages(t *testing.T) { + home := setupAgents(t) + const id = "9a1d2c3b-4e5f-4a6b-8c7d-0e1f2a3b4c5d" + const prompt = "The prompt typed after the first poll" + 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', 'New session - default', 'default', '0.16.9', 1790496400000, 1790496400000)`, "sess_"+id); err != nil { + t.Fatal(err) + } + if got := Titles([]string{"zcode:" + id}); got["zcode:"+id] != "" { + t.Fatalf("before any prompt: %q, want none", got["zcode:"+id]) + } + ocInsert(t, db, "sess_"+id, "msg_late", 1790496401000, 1790496401000, `{"id":"msg_late","role":"user","time":{"created":1790496401000},"semantics":{"origin":"real_user"}}`) + if _, err := db.Exec("INSERT INTO part VALUES (?, ?, ?, ?, ?, ?)", "prt_late", "msg_late", "sess_"+id, 1790496401000, 1790496401000, `{"type":"text","text":"`+prompt+`"}`); err != nil { + t.Fatal(err) + } + if got := Titles([]string{"zcode:" + id}); got["zcode:"+id] != prompt { + t.Fatalf("after the prompt: %q, want %q (all %v)", got["zcode:"+id], prompt, got) + } +} + +// What a name poll keeps of a ZCode session is what the Sessions page's +// listing would keep: the same size and last activity, so neither reads +// the session again only because the other read it last. +func TestZCodeTitlesKeepTheListingsStamp(t *testing.T) { + setupAgents(t) + if got := Titles([]string{"zcode:ses_zcA"}); got["zcode:ses_zcA"] == "" { + t.Fatalf("no title for ses_zcA") + } + mu.Lock() + defer mu.Unlock() + release := useDBs() + defer release() + var checked bool + for _, f := range zcodeFiles() { + if f.m().sid != "ses_zcA" { + continue + } + checked = true + st := cache[f.path] + if st == nil { + t.Fatalf("%s not kept", f.path) + } + if st.Size != f.size || st.Mod != f.mod.UnixNano() { + t.Fatalf("kept size %d mod %d, listing has size %d mod %d", st.Size, st.Mod, f.size, f.mod.UnixNano()) + } + } + if !checked { + t.Fatal("ses_zcA not listed") + } +}