Files
magpie/internal/sessions/raft.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

96 lines
3.2 KiB
Go

package sessions
import (
"os"
"path/filepath"
"github.com/yetone/magpie/internal/appdir"
)
// Raft (the slock app, raft-computer) runs its agents each in a workspace
// of its own, <slock>/agents/<agent id>/. Those it runs on Pi keep their
// sessions there in .pi-sessions/, and those on its built-in runtime (Pi
// too, with its own agent folder) in .builtin-sessions/: Pi's SDK files,
// <time>_<session id>.jsonl, all in one folder (PiDriver's
// buildPiSessionDir, SessionManager.create). They are read as Pi's, as
// agent "raft". The handoff notes in <slock>/runtime-sessions are not
// sessions. Raft's Pi reads the user's own Pi agent folder, so one sent
// through magpie is the gateway's "pi" session of the same id too
// (GatewayKeys).
// SlockDir is Raft's folder: $SLOCK_HOME, else ~/.slock
// (%USERPROFILE%\.slock on Windows).
func SlockDir() string {
if d := appdir.Getenv("SLOCK_HOME"); d != "" {
if abs, err := filepath.Abs(expandHome(d)); err == nil {
return abs
}
return expandHome(d)
}
home, _ := os.UserHomeDir()
return filepath.Join(home, ".slock")
}
func raftFiles() []file { return raftFilesIn(SlockDir()) }
// raftFilesIn are the Pi sessions of the agents in a Raft folder.
func raftFilesIn(dir string) []file {
if !Reads("raft") {
return nil
}
var paths []string
for _, sub := range []string{".pi-sessions", ".builtin-sessions"} {
more, _ := filepath.Glob(filepath.Join(dir, "agents", "*", sub, "*.jsonl"))
paths = append(paths, more...)
}
return piNamed("raft", paths)
}
// GatewayKeys are the "agent|id" keys a session's gateway record can
// carry: its own; for Raft's, Pi's, as Raft's Pi tells the gateway it is
// Pi; for one Claude Desktop's Code tab ran, Desktop's, as the gateway
// 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 gateway-only row.
func GatewayKeys(s Session) []string {
k := []string{s.Agent + "|" + s.ID}
if s.Agent == "raft" {
k = append(k, "pi|"+s.ID)
}
if s.App != "" {
k = append(k, s.App+"|"+s.ID)
}
for _, id := range s.Copies {
k = append(k, s.Agent+"|"+id)
}
for _, id := range s.Subs {
for _, rid := range requestIDs(s.Agent, id) {
k = append(k, s.Agent+"|"+rid)
}
}
for _, rid := range requestIDs(s.Agent, s.ID)[1:] {
k = append(k, s.Agent+"|"+rid)
}
return k
}
// requestIDs are the ids a request through the gateway can name the
// session id by, id itself first: ZCode's database gives an id a prefix
// (sess_…, a subagent's sess_subagent_agent_…) its requests leave out
// (zcodeBareID). GatewayKeys and NativeIDs both read it, so the Sessions
// list, its spend and the management page agree on which gateway record
// is a session here (#5754).
func requestIDs(agent, id string) []string {
if agent == "zcode" {
if bare := zcodeBareID(id); bare != id {
return []string{id, bare}
}
}
return []string{id}
}