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).
This commit is contained in:
yetone
2026-11-11 17:52:02 +08:00
parent f10c546dbe
commit b0b995e085
6 changed files with 166 additions and 0 deletions
+8
View File
@@ -260,6 +260,14 @@ database gives it, so `Titles` looks a `zcode:` key up under both and
answers under the id asked for (#5195). Verification:
`TestZCodeTitlesByRequestID`.
A ZCode session's name is the app's: its task index
(`v2/tasks-index.sqlite` in the data folder, `tasks.title` by `task_id`,
the session's id) holds what the app shows and what the user renamed it to,
and the CLI database's title is a guess or the default. `zcodeTaskOf`
(`zcode_tasks.go`) overlays it on `zcDB.info`, and the task's `updated_at`
counts toward the file's time so a rename is read again. Verification:
`TestZCodeTitleIsTheAppsTaskName`.
Routing's stage names the session an account is serving (connor_yang on
Discord): a row answering, or that answered, the request shown adds that
request's session title after its status, on one line cut short with the
+5
View File
@@ -350,6 +350,11 @@ func ocDBRows(db *sql.DB, query, agent, path string, store ocStore, parent map[s
continue
}
parent[id] = par
if agent == "zcode" { // a rename in the app is no change to the CLI's rows
if t, ok := zcodeTaskOf(id); ok {
updated = max(updated, t.updated)
}
}
out = append(out, file{agent: agent, path: path + "#" + id, size: n, mod: modOf(ms(max(updated, last))), more: &fileMore{sid: id, oc: store}})
}
return out
+4
View File
@@ -49,6 +49,10 @@ func (s zcDB) info(sid string) (ocInfo, bool) {
if source == "default" {
i.Title = ""
}
// the name the app shows, when it has one
if t, ok := zcodeTaskOf(sid); ok && title(t.title) != "" && !ocDefaultTitle(title(t.title)) {
i.Title = t.title
}
return i, err == nil
}
+93
View File
@@ -0,0 +1,93 @@
package sessions
import (
"os"
"path/filepath"
"strconv"
"sync"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/provider"
)
// ZCode's desktop app names its sessions in an index of its own,
// ~/.zcode/v2/tasks-index.sqlite (in the data folder the user chose in its
// settings): a task per session, its task_id the CLI database's session id
// and its title the name the app shows, the user's own when they renamed it
// (title_overridden). The CLI database's title is the model's guess, or
// nothing at all (title_source "default"), so a session ZCode shows by name
// read as its first prompt here (Flandern1211 on X).
type zcodeTask struct {
title string
updated int64
}
var zcodeTasks struct {
sync.Mutex
path string
stamp string
byID map[string]zcodeTask
}
func zcodeTasksPath() string {
home, err := os.UserHomeDir()
if err != nil {
return ""
}
return filepath.Join(appdir.ZCodeDataIn(appdir.ZCodeHome(home)), ".zcode", "v2", "tasks-index.sqlite")
}
// zcodeTaskOf is the app's task for a session id, read again only when the
// index or its write-ahead log changed. An index that can't be read names
// nothing: the last good read stays.
func zcodeTaskOf(sid string) (zcodeTask, bool) {
path := zcodeTasksPath()
if path == "" {
return zcodeTask{}, false
}
stamp := ""
for _, p := range []string{path, path + "-wal"} {
if fi, err := os.Stat(p); err == nil {
stamp += fi.ModTime().String() + "/" + strconv.FormatInt(fi.Size(), 10) + ";"
}
}
zcodeTasks.Lock()
defer zcodeTasks.Unlock()
if zcodeTasks.path != path || zcodeTasks.stamp != stamp {
if m, ok := readZCodeTasks(path); ok {
zcodeTasks.byID = m
} else if zcodeTasks.path != path {
zcodeTasks.byID = nil
}
zcodeTasks.path, zcodeTasks.stamp = path, stamp
}
t, ok := zcodeTasks.byID[sid]
return t, ok
}
func readZCodeTasks(path string) (map[string]zcodeTask, bool) {
if !fileExists(path) {
return nil, false
}
db, err := provider.OpenReadOnly(path)
if err != nil {
return nil, false
}
defer db.Close()
rows, err := db.Query(`SELECT task_id, title, updated_at FROM tasks WHERE deleted = 0`)
if err != nil {
return nil, false
}
defer rows.Close()
out := map[string]zcodeTask{}
for rows.Next() {
var id, title string
var updated int64
if rows.Scan(&id, &title, &updated) != nil {
return nil, false
}
out[id] = zcodeTask{title, updated}
}
return out, rows.Err() == nil
}
+3
View File
@@ -32,6 +32,9 @@ func zcodeTitleFiles() []file {
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}}}})
}
+53
View File
@@ -2,6 +2,7 @@ package sessions
import (
"database/sql"
"os"
"path/filepath"
"testing"
)
@@ -213,3 +214,55 @@ func TestZCodeTitlesKeepTheListingsStamp(t *testing.T) {
t.Fatal("ses_zcA not listed")
}
}
// ZCode's app names a session in its task index (v2/tasks-index.sqlite), not
// in the CLI's database, where a session it never titled is "default" and the
// model's guess is not what the app shows. The app's name is the session's,
// and a rename in the app (the CLI's rows unchanged) is read at the next ask
// (Flandern1211 on X).
func TestZCodeTitleIsTheAppsTaskName(t *testing.T) {
home := setupAgents(t)
cli, err := sql.Open("sqlite", filepath.Join(home, ".zcode", "cli", "db", "db.sqlite"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { cli.Close() })
for _, s := range []struct{ id, title, source string }{
{"sess_aaaa", "New session", "default"},
{"sess_bbbb", "A model's guess", "ai"},
{"sess_cccc", "Only the CLI knows", "ai"},
} {
if _, err := cli.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', ?, ?, '0.16.9', 1790496000000, 1790496000000)`, s.id, s.title, s.source); err != nil {
t.Fatal(err)
}
}
idx, err := sql.Open("sqlite", filepath.Join(home, ".zcode", "v2", "tasks-index.sqlite"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { idx.Close() })
if err := os.MkdirAll(filepath.Join(home, ".zcode", "v2"), 0o755); err != nil {
t.Fatal(err)
}
if _, err := idx.Exec(`CREATE TABLE tasks (workspace_key TEXT NOT NULL, task_id TEXT NOT NULL, title TEXT NOT NULL DEFAULT '', updated_at INTEGER NOT NULL, deleted INTEGER NOT NULL DEFAULT 0, PRIMARY KEY (workspace_key, task_id))`); err != nil {
t.Fatal(err)
}
put := func(id, title string, updated int64) {
if _, err := idx.Exec(`INSERT OR REPLACE INTO tasks (workspace_key, task_id, title, updated_at) VALUES ('w', ?, ?, ?)`, id, title, updated); err != nil {
t.Fatal(err)
}
}
put("sess_aaaa", "整理发票", 1790496000000)
put("sess_bbbb", "My own name", 1790496000000)
keys := []string{"zcode:sess_aaaa", "zcode:sess_bbbb", "zcode:sess_cccc"}
got := Titles(keys)
for k, want := range map[string]string{"zcode:sess_aaaa": "整理发票", "zcode:sess_bbbb": "My own name", "zcode:sess_cccc": "Only the CLI knows"} {
if got[k] != want {
t.Fatalf("%s: %q, want %q (all %v)", k, got[k], want, got)
}
}
put("sess_aaaa", "Renamed", 1790496100000)
if got := Titles(keys); got["zcode:sess_aaaa"] != "Renamed" {
t.Fatalf("after a rename: %q, want Renamed", got["zcode:sess_aaaa"])
}
}