Files
magpie/internal/sessions/zcode_gateway_keys_test.go
T
yetone 7cabf0da98 sessions: one place says which ids a ZCode request names, and drafts are one group (#5754)
On top of Flandern1211's cherry-picked fix (the commit before this one).
GatewayKeys gave the bare id of a ZCode session (sess_<id> in ZCode's
database, <id> in the gateway's record), but NativeIDs kept its own copy
of the keys: the management page still counted every ZCode session sent
through the gateway as a gateway-only session, its delete listed them,
and its search read them a second time. requestIDs is now the one place
that says which ids a request names a session by; GatewayKeys (Sessions
list, its spend, the stats' native set, viaOf, the manage search) and
NativeIDs (the manage page's count and delete) both read it.

ZCode runs a session started without a project in a folder it makes for
that session alone: <workspaceDirectory>/draft-<id> or <ms>-<uuid>, with
workspaceDirectory = ~/.zcode/workspace (read from ZCode 3.17.2's
out/host/index.js, downloaded from cdn-zcode.z.ai; never the live
~/.zcode). The reporter's group is renamed from "Scratch sessions" to
"Draft sessions" (草稿会话 / 草稿會話 / 下書きセッション /
Entwurfssitzungen), after ZCode's own word for them, with a GUI test of
its own. Comments no longer quote "Seen through the gateway", which
i18n-keys' plugin-words guard read as the core saying the word.

Run (mote, at this tree, rebased on 0eac4e6aa):
- gofmt -l: clean. go vet of sessions/gui/usage for darwin, linux
  (-tags nogui), windows; GOOS=linux -tags nogui and GOOS=windows
  go build: ok.
- go test -run 'ZCode|Gateway|Native|Manage|Merge|External'
  ./internal/sessions ./internal/usage ./internal/gui: ok.
- GUI, Chromium and WebKit: sessions-zcode-drafts (new, 5 languages x 2
  widths), sessions-folder-list, i18n-en-no-cjk: ok. i18n-keys' "a
  plugin's words are its own" is red at origin/main for three words not
  touched here, filed: #5332 (View routing), #5426 (No session ID),
  #5482 (Use the library's). The reporter's comments quoted "Seen
  through the gateway" and made a fourth; reworded.

Broken by hand:
- TestZCodeGatewayRowIsTheSessionItself on origin/main's raft.go,
  manage.go and external.go: red (keys lack zcode|main, fork_of_main,
  abc; NativeIDs lack them; 29 rows for 26 sessions).
- sessions-zcode-drafts on origin/main's Sessions client.js/i18n.js:
  red in every language and width.

Not tried:
- The full Go suite and the whole Sessions GUI set: queued behind other
  sessions on mote's suite lock; run after the push, reds filed.
- A real ZCode database: none on this machine; the reporter's own run
  over 1,105 sessions is in the issue.
- Windows/WSL GUI, and a ZCode data folder the user moved elsewhere
  (only ~/.zcode/workspace is matched).

Reported-by: flandern1211_58540 (Discord)
Refs #5754 (left open for the reporter)
2026-11-09 18:55:01 +08:00

76 lines
3.0 KiB
Go

package sessions
import (
"database/sql"
"path/filepath"
"slices"
"testing"
)
// A ZCode request names its session by the id its database gives it with a
// prefix (sess_…, a subagent's sess_subagent_agent_…), so a session whose
// files are on this computer was listed twice: once under its folder, and
// again as a gateway-only row: the same session under its bare
// id. Its own id, its fork's and its subagent's bare ids are keys of it
// (eachGatewayKey), and those of neither of the two are rows of their own.
func TestZCodeGatewayRowIsTheSessionItself(t *testing.T) {
inZone(t, 0)
home := setupAgents(t)
zcForkDB(t, home)
db, err := sql.Open("sqlite", filepath.Join(home, ".zcode", "cli", "db", "db.sqlite"))
if err != nil {
t.Fatal(err)
}
defer db.Close()
// a fork of sess_main and a subagent's own session, filed under it
// (ocRoots) and no rows of their own
for _, s := range []struct{ id, title string }{
{"sess_fork_of_main", "Fork of Port the parser"},
{"sess_subagent_agent_abc", "Subagent"},
} {
if _, err := db.Exec("INSERT INTO session (id, project_id, parent_id, slug, directory, title, title_source, version, time_created, time_updated) VALUES (?, 'p', 'sess_main', 's', '/work/zcf', ?, 'generated', '3.17.2', 1791000000000, 1791000000000)",
s.id, s.title); err != nil {
t.Fatal(err)
}
ocInsert(t, db, s.id, s.id+"_m", 1791000000000, 1791000010000,
`{"id":"`+s.id+`_m","role":"assistant","modelId":"glm-5.3","tokens":{"input":10,"output":2,"cache":{"read":0,"write":0}},"time":{"created":1791000000000,"completed":1791000010000}}`)
}
list, _, _ := ListUnless(All, -1)
var main Session
for _, s := range list {
if s.Agent == "zcode" && s.ID == "sess_main" {
main = s
}
}
if main.ID == "" {
t.Fatal("sess_main not listed")
}
keys := GatewayKeys(main)
for _, want := range []string{"zcode|sess_main", "zcode|main", "zcode|fork_of_main", "zcode|abc"} {
if !slices.Contains(keys, want) {
t.Errorf("keys %v lack %q", keys, want)
}
}
// the management page's ids (NativeIDs) are the same: its count of
// gateway-only sessions and its delete leave these records alone
native := NativeIDs(map[string]bool{"zcode": true})["zcode"]
for _, want := range []string{"sess_main", "main", "fork_of_main", "abc"} {
if !native[want] {
t.Errorf("NativeIDs lack %q: %v", want, native)
}
}
// what the gateway saw of them is that session's record: no row added
ext := []ExternalSession{
{Agent: "zcode", ID: "main"},
{Agent: "zcode", ID: "fork_of_main"},
{Agent: "zcode", ID: "abc"},
}
if added := MergeExternal(list, ext); len(added) != len(list) {
t.Fatalf("%d rows for %d sessions: one here was listed again as a gateway row", len(added), len(list))
}
// one whose files are not here is a row of its own, as before
if added := MergeExternal(list, []ExternalSession{{Agent: "zcode", ID: "gone"}}); len(added) != len(list)+1 {
t.Fatalf("%d rows for %d sessions: a session with no files here was left out", len(added), len(list))
}
}