Files
magpie/internal/sessions/zcode_titles.go
T
yetone b0b995e085 sessions: a ZCode session is named as the ZCode app names it, from its task index (Flandern1211 on X)
ZCode's desktop app keeps the names it shows (and the user's renames) in
~/.zcode/v2/tasks-index.sqlite, tasks.title by task_id = the CLI database's
session id. The CLI's own title is the model's guess or "default" (blank
here), so the Context page showed a first prompt where ZCode shows a name.
zcDB.info now takes the task's title when it has a non-default one; the
task's updated_at counts toward the file's time so a rename, which touches
no CLI row, is read again. The index is read once per change of the file or
its -wal, and a failed read keeps the last good one.

Run (mote): internal/sessions whole package ok under a sandbox HOME;
gofmt clean; go vet darwin, linux nogui, windows ok.
Broken by hand: the title overlay off -> new test red (title empty);
updated_at bump off -> the rename step red (stale title).
Not tried: a real ZCode with the CLI db (this machine has only the app's
index, no cli/db), Windows with a moved data folder (ZCodeDataIn is the
existing resolver).
2026-11-11 17:52:02 +08:00

68 lines
2.0 KiB
Go

package sessions
// 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
// join over a large database took ~20s.
//
// mu is held by Titles, as openDB's handles expect.
func zcodeTitleFiles() []file {
if !Reads("zcode") {
return nil
}
path := zcodeDB()
if !fileExists(path) {
return nil
}
db := openDB(path)
if db == nil {
return nil
}
rows, err := db.Query(`SELECT id, COALESCE(parent_id, ''), time_updated FROM session`)
if err != nil {
return nil
}
defer rows.Close()
parent := map[string]string{}
var out []file
for rows.Next() {
var id, par string
var updated int64
if rows.Scan(&id, &par, &updated) != nil {
continue
}
parent[id] = par
if t, ok := zcodeTaskOf(id); ok { // a rename in the app is no change to the CLI's rows
updated = max(updated, t.updated)
}
out = append(out, file{agent: "zcode", path: path + "#" + id, mod: modOf(ms(updated)),
more: &fileMore{sid: id, oc: zcDB{ocDB{db}}}})
}
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, modOf(ms(max(f.mod.UnixMilli(), last)))
}
}