diff --git a/docs/subsystems/gateway-sessions.md b/docs/subsystems/gateway-sessions.md index 89daed72f..0123ccfd0 100644 --- a/docs/subsystems/gateway-sessions.md +++ b/docs/subsystems/gateway-sessions.md @@ -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 diff --git a/internal/sessions/opencode.go b/internal/sessions/opencode.go index 23ddd8d0e..463494d44 100644 --- a/internal/sessions/opencode.go +++ b/internal/sessions/opencode.go @@ -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 diff --git a/internal/sessions/zcode.go b/internal/sessions/zcode.go index 02d9c5651..64e05b4a1 100644 --- a/internal/sessions/zcode.go +++ b/internal/sessions/zcode.go @@ -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 } diff --git a/internal/sessions/zcode_tasks.go b/internal/sessions/zcode_tasks.go new file mode 100644 index 000000000..bee7459f9 --- /dev/null +++ b/internal/sessions/zcode_tasks.go @@ -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 +} diff --git a/internal/sessions/zcode_titles.go b/internal/sessions/zcode_titles.go index a7a217042..d3042c2bf 100644 --- a/internal/sessions/zcode_titles.go +++ b/internal/sessions/zcode_titles.go @@ -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}}}}) } diff --git a/internal/sessions/zcode_titles_test.go b/internal/sessions/zcode_titles_test.go index ef14afa74..395216c67 100644 --- a/internal/sessions/zcode_titles_test.go +++ b/internal/sessions/zcode_titles_test.go @@ -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"]) + } +}