diff --git a/docs/subsystems/gateway-sessions.md b/docs/subsystems/gateway-sessions.md index ab89ea690..ec07e55e3 100644 --- a/docs/subsystems/gateway-sessions.md +++ b/docs/subsystems/gateway-sessions.md @@ -91,7 +91,14 @@ the gateway's `omp|` is its own in the same places as Raft's `pi|` key, and is never a second session counted again (Jumy, Discord 10-30: `TestOmpSubagentsAreTheSessionsToTheGateway`, `TestOmpSubagentCountedOnceWithTheGateway`). A Claude Code session's -copies (`Copies`) are among its keys the same way. A Claude Code +copies (`Copies`) are among its keys the same way. A ZCode session stands +for its bare id too: a request names it by the id its database gives with +a prefix stripped (`sess_…`, a subagent's `sess_subagent_agent_…`: +`zcodeBareID`), its own as well as its forks' and subagents' (their files +are in its group, `Subs`, and `idInFile` reads their ids). Without it a +ZCode session whose files are here was a row under its folder and again a +row under Seen through the gateway (flandern1211_58540 on Discord; +`TestZCodeGatewayRowIsTheSessionItself`). A Claude Code session Claude Desktop's Code tab ran is in Claude Code's folder, and only its lines' `entrypoint` (`claude-desktop`, `claude-desktop-3p`) tells it apart: its parse keeps the entrypoint of the line its folder came from (`state.Entry`), and the diff --git a/docs/subsystems/gui-shell.md b/docs/subsystems/gui-shell.md index 0c3ccff0e..d8a2f8874 100644 --- a/docs/subsystems/gui-shell.md +++ b/docs/subsystems/gui-shell.md @@ -368,10 +368,17 @@ A row says the folder's name, the folder it is in (home as `~`, from the manage reply's `home`), its newest session's title, when, and how many. Temporary folders, the disk's root and apps' hidden folders under home (`scratch`) wait folded under Other folders at the end; a search, or a -session opened from elsewhere, unfolds them. Checkboxes and the Delete bar -come only with Select, and Done lets the pick go. Relocate project and -Delete all sessions are in a folder's ⋯ menu, shown on hover (always on a -touch screen) and on a right-click. See `sessions-folder-list.test.cjs`. +session opened from elsewhere, unfolds them. A ZCode session in a folder +ZCode made for that one session (`~/.zcode/workspace/-`, +one for each session started without a project) is no project of the +reader's, and one folder each was 40 folders of one session: they stand as +one Scratch sessions group after the projects, before Other folders, and a +row's tooltip gives the folder it ran in (flandern1211_58540 on Discord). +Checkboxes and the +Delete bar come only with Select, and Done lets the pick go. Relocate +project and Delete all sessions are in a folder's ⋯ menu, shown on hover +(always on a touch screen) and on a right-click. See +`sessions-folder-list.test.cjs`. ### Routing day tallies diff --git a/internal/gui/assets/plugins/sessions/client.js b/internal/gui/assets/plugins/sessions/client.js index bfed06bf1..f360239d8 100644 --- a/internal/gui/assets/plugins/sessions/client.js +++ b/internal/gui/assets/plugins/sessions/client.js @@ -86,15 +86,30 @@ magpie.plugin("sessions", (ctx) => { // another config folder): such sessions are grouped by that, not under // "No folder" with the agent's own (lc on Discord). const GW = "\x00gateway"; - const groupOf = (s) => s.cwd || (s.gateway ? GW : ""); - const groupName = (k) => k === GW ? t("Seen through the gateway") : k ? baseName(k) : t("No folder"); + // A session its agent ran with no project of its own, in a folder the + // agent made for that one session: ZCode keeps ~/.zcode/workspace/ + // -, one for each session started without a project. + // One "folder" each was 40 folders of one session, and they are no + // projects of the reader's (flandern1211_58540 on Discord): one group of + // their own, not "No folder", keeps them apart from sessions whose agent + // recorded no folder. + const SCRATCH = "\x00scratch"; + const zcodeScratch = (s) => { + const home = (data?.home || "").replace(/\\/g, "/").replace(/\/+$/, ""); + if (!home || s.agent !== "zcode" || !s.cwd) return false; + const c = s.cwd.replace(/\\/g, "/"); + return c === home + "/.zcode/workspace" || c.startsWith(home + "/.zcode/workspace/"); + }; + const groupOf = (s) => zcodeScratch(s) ? SCRATCH : s.cwd || (s.gateway ? GW : ""); + const groupName = (k) => k === GW ? t("Seen through the gateway") + : k === SCRATCH ? t("Scratch sessions") : k ? baseName(k) : t("No folder"); // A folder an app or the system made, not a project of the reader's: the // disk's root, a temp folder, or one under a hidden folder of a home // (~/.some-agent/…). They go under a fold of their own after the // projects (yetone: "/", "T" and folders named by UUIDs led the list). const scratch = (k) => { - if (!k || k === GW) return false; + if (!k || k === GW || k === SCRATCH) return false; const p = k.replace(/\\/g, "/").replace(/\/+$/, ""); return p === "" || /^[a-z]:$/i.test(p) || /^\/(private\/)?(tmp|var\/tmp|var\/folders)(\/|$)/.test(p) @@ -728,7 +743,7 @@ magpie.plugin("sessions", (ctx) => { openedFor = data.agent; opened.clear(); drawnIn.clear(); - const first = list.find((s) => !scratch(groupOf(s))) || list[0]; + const first = list.find((s) => !scratch(groupOf(s)) && groupOf(s) !== SCRATCH) || list[0]; if (first) opened.add(groupOf(first)); } nest(list); @@ -755,11 +770,19 @@ magpie.plugin("sessions", (ctx) => { return; } const q = query.trim(); - // the projects in one list, and the folders apps and the system made - // folded under them - const all = [...groups].filter(([k]) => !scratch(k)); + // the projects in one list, the folders apps and the system made + // folded under them, and the folders agents made one session each + // (SCRATCH) after the projects: they are no projects, but the reader + // looks for the sessions in them, so not folded away either + const all = [...groups].filter(([k]) => !scratch(k) && k !== SCRATCH); const rest = [...groups].filter(([k]) => scratch(k)); - if (all.length) tree.append(folderList(el("div", "list sm-folders"), all, FOLDERS, q)); + const temp = groups.get(SCRATCH); + if (all.length || temp) { + const box = el("div", "list sm-folders"); + if (all.length) folderList(box, all, FOLDERS, q); + if (temp) box.append(folder(SCRATCH, temp, q)); + tree.append(box); + } if (rest.length) tree.append(othersBox(rest, q)); toFocus(); } @@ -877,7 +900,7 @@ magpie.plugin("sessions", (ctx) => { const fold = el("button", "fold"); fold.type = "button"; fold.setAttribute("aria-expanded", String(open)); - const path = cwd === GW ? "" : cwd; + const path = cwd === GW || cwd === SCRATCH ? "" : cwd; const line = el("span", "sm-fline"); line.append(el("span", "name", groupName(cwd))); const where = path ? whereOf(path) : ""; @@ -896,7 +919,7 @@ magpie.plugin("sessions", (ctx) => { when.append(el("span", "note", t(members.length === 1 ? "{n} session" : "{n} sessions", { n: members.length }))); r.append(fold, when); const acts = []; - const allInFolder = (data.sessions || []).filter((s) => s.cwd === cwd); + const allInFolder = (data.sessions || []).filter((s) => groupOf(s) === cwd); if (data.agent === "claude" && path && allInFolder.every((s) => !s.read_only && !s.gateway) && new Set(allInFolder.map((s) => s.wsl || "")).size === 1) { acts.push({ name: "Relocate project", icon: MOVE, run: () => askRelocate(cwd, allInFolder[0].wsl || "") }); @@ -1013,6 +1036,9 @@ magpie.plugin("sessions", (ctx) => { sub.prepend(p, " "); } who.append(sub); + // a scratch session's folder is a UUID its agent made: the row says + // nothing of it otherwise (the group of them is one) + if (zcodeScratch(s)) r.title = s.cwd; const hit = hitOf(s); if (hit) who.append(hitLine(hit)); // what it spent, as the Usage page's list shows it @@ -1454,7 +1480,7 @@ magpie.plugin("sessions", (ctx) => { : folders ? t("Delete all {n} sessions in {k} folders?", { n: list.length, k: folders }) : t("Delete {n} sessions?", { n: list.length }))); ed.append(h); - if (folder && folder !== GW) ed.append(el("p", "sub sm-ask-path", folder)); + if (folder && folder !== GW && folder !== SCRATCH) ed.append(el("p", "sub sm-ask-path", folder)); const names = el("ul", "sm-ask-list"); for (const s of list.slice(0, 5)) names.append(el("li", "", s.title || s.id)); if (list.length > 5) names.append(el("li", "more", t("+{n} more", { n: list.length - 5 }))); diff --git a/internal/gui/assets/plugins/sessions/i18n.js b/internal/gui/assets/plugins/sessions/i18n.js index 92ff3feab..884879e84 100644 --- a/internal/gui/assets/plugins/sessions/i18n.js +++ b/internal/gui/assets/plugins/sessions/i18n.js @@ -46,6 +46,7 @@ magpie.strings({ "This {agent} session is read only, so magpie can't delete it.": "这个 {agent} 会话是只读的,magpie 无法删除。", "Select every session shown": "选中所有显示的会话", "No folder": "无文件夹", + "Scratch sessions": "临时会话", "deleted {when}": "{when}删除", "Delete this session?": "删除这个会话?", "Delete {n} sessions?": "删除这 {n} 个会话?", @@ -135,6 +136,7 @@ magpie.strings({ "This {agent} session is read only, so magpie can't delete it.": "這個 {agent} 會話是唯讀的,magpie 無法刪除。", "Select every session shown": "選中所有顯示的會話", "No folder": "無資料夾", + "Scratch sessions": "暫存會話", "deleted {when}": "{when}刪除", "Delete this session?": "刪除這個會話?", "Delete {n} sessions?": "刪除這 {n} 個會話?", @@ -224,6 +226,7 @@ magpie.strings({ "This {agent} session is read only, so magpie can't delete it.": "この {agent} セッションは読み取り専用のため、magpie では削除できません。", "Select every session shown": "表示中のセッションをすべて選択", "No folder": "フォルダなし", + "Scratch sessions": "一時セッション", "deleted {when}": "{when}に削除", "Delete this session?": "このセッションを削除しますか?", "Delete {n} sessions?": "{n} 件のセッションを削除しますか?", @@ -313,6 +316,7 @@ magpie.strings({ "This {agent} session is read only, so magpie can't delete it.": "Diese Sitzung von {agent} ist schreibgeschützt, daher kann magpie sie nicht löschen.", "Select every session shown": "Alle angezeigten Sitzungen auswählen", "No folder": "Kein Ordner", + "Scratch sessions": "Temporäre Sitzungen", "deleted {when}": "gelöscht {when}", "Delete this session?": "Diese Sitzung löschen?", "Delete {n} sessions?": "{n} Sitzungen löschen?", diff --git a/internal/sessions/external.go b/internal/sessions/external.go index 8e834b906..85d254d92 100644 --- a/internal/sessions/external.go +++ b/internal/sessions/external.go @@ -5,6 +5,7 @@ import ( "math" "slices" "sort" + "strings" "time" ) @@ -87,25 +88,15 @@ func EachMerged(native []Session, external []ExternalSession, each func(*Session // externalAdded are the external sessions MergeExternal adds to native, in // external's order. func externalAdded(native []Session, external []ExternalSession) []Session { - // keyed by the pair, not "agent|id": a string made for each of ~5000 - // sessions on each of a poll's listings, and the set and the list grown - // an append at a time, were ~200 MB in 80s of polls (#2278) + // keyed by the pair: the set grown an append at a time was ~200 MB in + // 80s of polls (#2278) type agentID struct{ agent, id string } seen := make(map[agentID]struct{}, len(native)) for _, s := range native { - // GatewayKeys, as pairs - seen[agentID{s.Agent, s.ID}] = struct{}{} - if s.Agent == "raft" { - seen[agentID{"pi", s.ID}] = struct{}{} - } - if s.App != "" { - seen[agentID{s.App, s.ID}] = struct{}{} - } - for _, id := range s.Copies { - seen[agentID{s.Agent, id}] = struct{}{} - } - for _, id := range s.Subs { - seen[agentID{s.Agent, id}] = struct{}{} + for _, k := range GatewayKeys(s) { + if agent, id, ok := strings.Cut(k, "|"); ok { + seen[agentID{agent, id}] = struct{}{} + } } } var not []ExternalSession diff --git a/internal/sessions/manage.go b/internal/sessions/manage.go index ae85df105..566f1f6a0 100644 --- a/internal/sessions/manage.go +++ b/internal/sessions/manage.go @@ -222,10 +222,13 @@ func readFor(want, all []file, quick bool) (pending map[string]bool, done func() } // idInFile says which agents' session ids are read from inside the -// session's file, not taken from its name (as assemble does). +// session's file, not taken from its name (as assemble does). ZCode's are: +// its database rows are the files, a fork's and a subagent's filed under +// the session they ran in (ocRoots), which a request names by their own ids +// (subIDs, zcodeBareID). func idInFile(agent string) bool { switch agent { - case "codex", "pi", "raft", "cindy", "omp", "dsh", "grok", "droid": + case "codex", "pi", "raft", "cindy", "omp", "dsh", "grok", "droid", "zcode": return true } return false diff --git a/internal/sessions/raft.go b/internal/sessions/raft.go index c304fcf4b..00b19e72a 100644 --- a/internal/sessions/raft.go +++ b/internal/sessions/raft.go @@ -52,6 +52,11 @@ func raftFilesIn(dir string) []file { // knows its requests by Desktop's user agent; the ids of the copies of its // conversation (ccCopies); and its subagents' own ids (subIDs), whose calls // it counts already. MergeExternal keeps the same keys as pairs. +// +// A ZCode request names its session by the id its database gives it with a +// prefix — sess_…, a subagent's sess_subagent_agent_… (zcodeBareID) — so the +// bare id is a key too: without it the same session was a row under its +// folder and again a row under "Seen through the gateway". func GatewayKeys(s Session) []string { k := []string{s.Agent + "|" + s.ID} if s.Agent == "raft" { @@ -66,5 +71,15 @@ func GatewayKeys(s Session) []string { for _, id := range s.Subs { k = append(k, s.Agent+"|"+id) } + if s.Agent == "zcode" { + if bare := zcodeBareID(s.ID); bare != s.ID { + k = append(k, "zcode|"+bare) + } + for _, id := range s.Subs { + if bare := zcodeBareID(id); bare != id { + k = append(k, "zcode|"+bare) + } + } + } return k } diff --git a/internal/sessions/zcode_gateway_keys_test.go b/internal/sessions/zcode_gateway_keys_test.go new file mode 100644 index 000000000..71bc2407a --- /dev/null +++ b/internal/sessions/zcode_gateway_keys_test.go @@ -0,0 +1,67 @@ +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 under "Seen through the gateway" as 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) + } + } + // 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)) + } +}