sessions: find a ZCode title by its gateway id (#5195)

ZCode sends its session id to the gateway without the sess_ prefix stored
in its database. Routing asks Titles with that bare id, gets no title, and
falls back to the folder name even though the Sessions page can show the
real title.

Titles treats zcode:<id> as an alias for zcode:sess_<id> when it reads
local session files, and returns the title under the key Routing asked
for (the sess_ key too, which callers ignore). A regression test uses the
database id shape ZCode writes and the bare request id it sends.

Ported from Flandern's b1a952b73 (fix/zcode-routing-session-titles on
flandern12111/magpie), code and test unchanged, reviewed line by line;
the body is rewritten (the original had literal \n's), and
gateway-sessions.md says it.

Run on mote via mote-run, at origin/main c7bbd3faf plus the change before
this one: go test ./internal/sessions -run
'TestZCodeTitlesByRequestID|TestSessionTitles|TestTitles|TestZCode'
-count=1: ok. Again at this commit on origin/main 14f6d10de: go test
./internal/sessions -count=1 ok.
  Pushed on c4245c4c2, which changes only internal/gateway's
  object_root and its doc after 14f6d10de; not run again on it.

Broken by hand: the alias lookup removed, TestZCodeTitlesByRequestID red
("zcode:578bd3d1-…: title \"\", want \"A ZCode session's title\"").

Not tried: a real ZCode session through a running gateway.

Co-authored-by: Flandern <3180066912wzw@gmail.com>
This commit is contained in:
yetone
2026-11-04 18:33:07 +08:00
co-authored by Flandern
parent 70a98dd85c
commit e7b0c398ee
3 changed files with 49 additions and 0 deletions
+5
View File
@@ -203,6 +203,11 @@ capabilities to a gateway row. Verification: `TestDesktopSessionTitles`,
`TestDesktopSessionTitleInStats`, `TestDesktopTitlesReadOncePerList`,
`TestDesktopSessionTitlesOnPages`.
A ZCode request names its session without the `sess_` prefix ZCode's
database gives it, so `Titles` looks a `zcode:` key up under both and
answers under the id asked for (#5195). Verification:
`TestZCodeTitlesByRequestID`.
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
+13
View File
@@ -2679,15 +2679,25 @@ func findListed(agent, id string) (Session, bool) {
// agent's own title, else its first prompt. A key with no files, or a
// session with no title, is left out. Desktop's local metadata can name a
// session without its CLI transcript. The Routing page uses this too (#1293).
// A ZCode request names its session without the sess_ prefix its database
// gives it, so that id names the session too.
func Titles(keys []string) map[string]string {
out := map[string]string{}
if len(keys) == 0 {
return out
}
want := make(map[string]bool, len(keys))
// alias maps the key a file is grouped under back to the key asked for:
// ZCode's request id, without the prefix its database gives it.
alias := map[string]string{}
var desktopIDs, piDesktopIDs []string
for _, k := range keys {
want[k] = true
if agent, id, ok := strings.Cut(k, ":"); ok && agent == "zcode" && !strings.HasPrefix(id, "sess_") {
full := agent + ":sess_" + id
want[full] = true
alias[full] = k
}
if id, ok := strings.CutPrefix(k, "claude-desktop:"); ok {
desktopIDs = append(desktopIDs, id)
continue
@@ -2744,6 +2754,9 @@ func Titles(keys []string) map[string]string {
continue
}
out[k] = s.Title
if asked := alias[k]; asked != "" {
out[asked] = s.Title
}
}
}
return out
+31
View File
@@ -0,0 +1,31 @@
package sessions
import (
"database/sql"
"path/filepath"
"testing"
)
// A ZCode request carries its session's id without the prefix its database
// gives it (sess_…), so the Routing page asks for the title by that id. The
// title is found under the database's key and handed back under the id asked
// for, as it is for the prefixed id.
func TestZCodeTitlesByRequestID(t *testing.T) {
home := setupAgents(t)
const id = "578bd3d1-834b-43fb-b40c-ed9d17ad7731"
const name = "A ZCode session's title"
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', ?, 'generated', '0.16.9', 1790496000000, 1790496000000)`, "sess_"+id, name); err != nil {
t.Fatal(err)
}
got := Titles([]string{"zcode:" + id, "zcode:sess_" + id})
for _, k := range []string{"zcode:" + id, "zcode:sess_" + id} {
if got[k] != name {
t.Fatalf("%s: title %q, want %q (all %v)", k, got[k], name, got)
}
}
}