From 9474eaf9cbbd3080ace5f2afc4c522fd2c5f5517 Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 29 Oct 2026 13:50:53 +0800 Subject: [PATCH] sessions: Raft's Pi agents' sessions are listed as Raft's, with their prompts, calls and delete Raft (slock's raft-computer) runs its agents on Pi and keeps each one's Pi SDK session files in that agent's folder: /agents// .pi-sessions/_.jsonl, or .builtin-sessions/ for its built-in runtime. is $SLOCK_HOME or ~/.slock (%USERPROFILE%\.slock on Windows, the distro's ~/.slock under WSL). Read from raft-computer 1.0.14's PiDriver (buildPiSessionDir), BuiltInDriver and resolveSlockHome, and a read-only listing of ~/.slock here. runtime-sessions/ holds handoff notes, no sessions, and is not read. - The Sessions page lists them as Raft's (agent "raft", in agent.Clients' others), titled and opened with their prompts as Pi's are. - Their calls count on the Usage page as Raft's; calls through magpie stay the gateway's, as Pi's do. - Raft's Pi uses the user's own ~/.pi/agent, so a request it sends through magpie reaches the gateway as Pi's under the same session id. sessions.GatewayKeys gives a Raft session both "raft|ID" and "pi|ID", so the gateway's copy is not listed twice nor counted twice (Merge, stats, NativeIDs, the GUI's Via). - Delete and restore work as Pi's: the one file goes to the trash, and a folder beside it is left (the agent's workspace is not the session's). A session written in the last minutes is refused as active. - No resume command, no carry and no trace export: Raft starts its own agents, and nothing outside it can resume one. - SLOCK_HOME is passed through agentenv like the other agents' homes. Run (at 958ad9f10 on e61e10535): - mote (macOS): -run 'Raft|TestPi|TestDirs|TestVars|TestFolderVars' in sessions and agentenv: ok, and red for each break below. A full go test ./... was started there; at load ~300 from other sessions' suites it printed only magpie and internal/access (ok) before mote stopped answering (host down), so no full macOS record exists. - mote (macOS), rebased on 5d24bc6cc: go test -count=1 ./internal/sessions/ ./internal/agentenv/: ok (sessions 495s, agentenv 1.1s). agent and gui were not run in full on macOS. - mote: GOOS=windows go vet of sessions, agent, agentenv, gui; GOOS=linux go vet of sessions, agent, agentenv: ok. - Windows (win-run): agentenv ok; -run 'Raft|TestPi|TestDirs' in sessions ok. The full sessions package hit its 30m timeout after TestOneListing's red (#4114) and the Agy reds (#4115, #4177). agent and gui reds (Hermes, WorkBuddy, Lody, Qoder/Copilot plugin folders, Codex keychain, ClaudeDesktop language, Aside, InstallWithoutNode, DesktopExec, AionUi, ImportStatus, UpdateThatWontStart, GatewayHeldByAContainer, GatewaySessionHistoryAndCalendar): each run again at origin/main c1660481c on Windows and red there too, except TestHermesProfilesFollowMagpieModel which passed there alone; it is #1388's Hermes model rewrite, which this change does not reach. - Linux (wsl-run): sessions ok, agentenv ok. agent's TestInstalledVersion, TestGrokInstalled, TestCursorFolderFromTheEditorIsNotTheCLI and TestOmpDetectedWithPiDir are red at origin/main c1660481c too (#4033, #3548). gui does not build there (no gtk4/webkitgtk-6.0 in the distro). Broken by hand, each red, then restored: Raft's listing removed; the "pi|ID" key dropped from GatewayKeys; the folder-beside check reverted to Pi's absence; $SLOCK_HOME ignored; Raft's calls left out of piCallFiles; NativeIDs without Raft; stats' native keys without "pi|ID". Not tried: with real Raft. No agent on this machine has run on Pi, so the fixtures are Pi 1.1.0's own files (testdata/pi-calls, testdata/pi) placed in Raft's folders. The GUI was not run in a browser: no JS changed, Raft's name and icon come from agent.Clients. Raft's icon is the generic one. --- docs/subsystems/gateway-sessions.md | 19 ++- internal/agent/agents.go | 4 + internal/agentenv/agentenv.go | 2 + internal/gui/sessions.go | 19 ++- internal/gui/sessions_manage.go | 2 +- internal/sessions/call_cache.go | 1 + internal/sessions/calls_pi.go | 5 +- internal/sessions/external.go | 4 +- internal/sessions/manage.go | 20 ++- internal/sessions/pi.go | 8 +- internal/sessions/raft.go | 55 +++++++ internal/sessions/raft_test.go | 238 ++++++++++++++++++++++++++++ internal/sessions/sessions.go | 7 +- internal/sessions/stats.go | 4 +- internal/sessions/transcript.go | 4 +- internal/sessions/wsl.go | 3 + 16 files changed, 372 insertions(+), 23 deletions(-) create mode 100644 internal/sessions/raft.go create mode 100644 internal/sessions/raft_test.go diff --git a/docs/subsystems/gateway-sessions.md b/docs/subsystems/gateway-sessions.md index 56f330133..9603e53d8 100644 --- a/docs/subsystems/gateway-sessions.md +++ b/docs/subsystems/gateway-sessions.md @@ -43,7 +43,20 @@ user's own llm-deepseek row pointed at magpie relies on (#3298, `TestDshMarksOnEveryRoute`). When the same identity is present in a native session, the native session wins -and the gateway projection is omitted. On `/api/sessions/manage`, in its +and the gateway projection is omitted. A native session can stand for more +than its own key (`sessions.GatewayKeys`): Raft's (slock's raft-computer) +agents run Pi in their folders under `$SLOCK_HOME` (else `~/.slock`, +`%USERPROFILE%\.slock` on Windows), `agents//.pi-sessions/` and +`.builtin-sessions/`, Pi's SDK files read as agent `raft` +([`raft.go`](../../internal/sessions/raft.go)). Raft's Pi reads the user's +own Pi folder, so through magpie it says it is Pi, and a Raft session is +the gateway's `pi|` too: in `MergeExternal`, the stats' native keys, +the GUI's list and Via, and `NativeIDs` for `pi`. Raft's sessions are +listed, read and deleted as Pi's (one file to the trash, nothing beside +it; Raft starts afresh without it), with no resume command, no carry and +no trace export (the gateway's Pi identity has those calls). Verification: +`TestRaftSessions`, `TestRaftIsPiToTheGateway`, `TestRaftDeleteRestore`, +`TestRaftSlockHome`. On `/api/sessions/manage`, in its counts as in its list, and on `/api/sessions/delete`, a native session is any session file the agent keeps, one nothing was said in too, with no limit (`sessions.NativeIDs`); one pass over the agents' files (`sessions.Manage`) @@ -436,8 +449,8 @@ Code's (#1779; its sessions are on the Sessions page too, from the same `projects//.jsonl` files read as WorkBuddy's, resumed with `codebuddy --resume ` and deletable, #2657: `TestCodeBuddy`, `TestCodeBuddySessionsListedUnderTheirName`), MiniMax Code's (#1605), Kimi Code's (#1607, -[`calls_kimi.go`](../../internal/sessions/calls_kimi.go)) and Pi's and -omp's, here and in WSL (#1951, +[`calls_kimi.go`](../../internal/sessions/calls_kimi.go)) and Pi's, +Raft's Pi agents' and omp's, here and in WSL (#1951, [`calls_pi.go`](../../internal/sessions/calls_pi.go)). Each skips the calls the agent made through magpie's own entry, which the gateway logged with the account that answered: Kimi Code's are the ones whose diff --git a/internal/agent/agents.go b/internal/agent/agents.go index 7be56d7dc..1967e0e5b 100644 --- a/internal/agent/agents.go +++ b/internal/agent/agents.go @@ -105,6 +105,10 @@ var others = []*Agent{ // ("pi-desktop/0.1.0"), it is told apart from Pi by that name, which // Pi's "pi-" would take otherwise (#1966, dgmico) {ID: "pi-desktop", Name: "PI-Desktop", Icon: "pi", UA: []string{"pi-desktop"}}, + // Raft (slock's raft-computer) runs Pi in each of its agents' folders; + // Sessions lists those as Raft's (internal/sessions/raft.go), while + // their requests say "pi (…)" and are Pi's + {ID: "raft", Name: "Raft", Icon: "generic"}, } // Clients is everyone whose requests the gateway knows by name: every diff --git a/internal/agentenv/agentenv.go b/internal/agentenv/agentenv.go index 05d59da96..5cfcd8bb8 100644 --- a/internal/agentenv/agentenv.go +++ b/internal/agentenv/agentenv.go @@ -43,6 +43,8 @@ var Vars = []string{ // Pi and its forks (OmO, Senpi), whose profile and config are apart "PI_CODING_AGENT_DIR", "PI_CODING_AGENT_SESSION_DIR", "PI_CONFIG_DIR", "PI_PROFILE", "OMO_CODING_AGENT_DIR", "SENPI_CODING_AGENT_DIR", + // Raft (slock), whose agents keep their Pi sessions in its folder + "SLOCK_HOME", "PRIME_AGENT_CODING_AGENT_DIR", // StepCode: config root resolution and folder overrides "STEP_CODING_AGENT_DIR", "STEPCODE_CONFIG_DIR", diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go index 49325af6a..0d4476462 100644 --- a/internal/gui/sessions.go +++ b/internal/gui/sessions.go @@ -106,7 +106,9 @@ func sessionRoutes(mux *http.ServeMux, w Windows) { list := sessions.List(sessions.All) nativeKeys := map[string]bool{} for _, s := range list { - nativeKeys[s.Agent+"|"+s.ID] = true + for _, k := range sessions.GatewayKeys(s) { + nativeKeys[k] = true + } // a copy of its conversation is the agent's own too for _, id := range s.Copies { nativeKeys[s.Agent+"|"+id] = true @@ -139,7 +141,7 @@ func sessionRoutes(mux *http.ServeMux, w Windows) { } vias := usage.Vias(since.Add(-time.Minute)) for _, s := range list { - j := sessionJSON{Session: s, Name: s.Agent, Icon: "generic", Via: vias.Of(s.Agent + "|" + s.ID)} + j := sessionJSON{Session: s, Name: s.Agent, Icon: "generic", Via: viaOf(vias, s)} if a := agents[s.Agent]; a != nil { j.Name, j.Icon = a.SharedName(), a.Icon } @@ -250,7 +252,7 @@ func sessionRoutes(mux *http.ServeMux, w Windows) { if since.IsZero() { since = s.Last } - j.Via = usage.Vias(since.Add(-time.Minute)).Of(s.Agent + "|" + s.ID) + j.Via = viaOf(usage.Vias(since.Add(-time.Minute)), s) j.Path = tilde(j.Path) writeJSON(rw, j) }) @@ -744,3 +746,14 @@ func openTerminal(command, choice string) error { time.AfterFunc(time.Minute, func() { os.Remove(f.Name()) }) return nil } + +// viaOf is what the gateway sent a session's calls to, under any key its +// gateway record can carry (sessions.GatewayKeys: Raft's Pi says it is Pi). +func viaOf(x usage.ViaIndex, s sessions.Session) []usage.Via { + for _, k := range sessions.GatewayKeys(s) { + if v := x.Of(k); v != nil { + return v + } + } + return nil +} diff --git a/internal/gui/sessions_manage.go b/internal/gui/sessions_manage.go index 27de91d92..a27bd9ae6 100644 --- a/internal/gui/sessions_manage.go +++ b/internal/gui/sessions_manage.go @@ -215,7 +215,7 @@ func sessionManageRoutes(mux *http.ServeMux, w Windows) { vias := usage.Vias(since.Add(-time.Minute)) for _, s := range list { s.Path = tilde(s.Path) - out.Sessions = append(out.Sessions, managedJSON{Managed: s, Via: vias.Of(s.Agent + "|" + s.ID)}) + out.Sessions = append(out.Sessions, managedJSON{Managed: s, Via: viaOf(vias, s.Session)}) } sizes := sessions.GatewaySizes() for _, g := range gateway { diff --git a/internal/sessions/call_cache.go b/internal/sessions/call_cache.go index 6479ab004..11d52f5e2 100644 --- a/internal/sessions/call_cache.go +++ b/internal/sessions/call_cache.go @@ -247,6 +247,7 @@ type lineCallReader struct { var lineCallReaders = map[string]lineCallReader{ "kimi": {kimiCallsBegin, kimiCallLine, nil}, // #1607 "pi": {piCallsBegin, readPiCallLine, piCallsKept}, // #1951 + "raft": {piCallsBegin, readPiCallLine, piCallsKept}, "omp": {piCallsBegin, readPiCallLine, piCallsKept}, } diff --git a/internal/sessions/calls_pi.go b/internal/sessions/calls_pi.go index a9e161f90..c1f38ad55 100644 --- a/internal/sessions/calls_pi.go +++ b/internal/sessions/calls_pi.go @@ -41,8 +41,9 @@ import ( // piCallFiles are Pi's and omp's session files, here and in WSL. func piCallFiles() []file { - out := append(piFiles(), ompFiles()...) - return append(out, wslFiles("pi", "omp")...) + out := append(piFiles(), raftFiles()...) + out = append(out, ompFiles()...) + return append(out, wslFiles("pi", "raft", "omp")...) } // piGateway is whether a provider is magpie's own entry. diff --git a/internal/sessions/external.go b/internal/sessions/external.go index f632f516b..f5d0ad7ac 100644 --- a/internal/sessions/external.go +++ b/internal/sessions/external.go @@ -36,7 +36,9 @@ func MergeExternal(native []Session, external []ExternalSession) []Session { names := desktopExternalTitles(external) seen := map[string]bool{} for _, s := range native { - seen[s.Agent+"|"+s.ID] = true + for _, k := range GatewayKeys(s) { + seen[k] = true + } for _, id := range s.Copies { seen[s.Agent+"|"+id] = true } diff --git a/internal/sessions/manage.go b/internal/sessions/manage.go index 5ed451438..c0efe0f36 100644 --- a/internal/sessions/manage.go +++ b/internal/sessions/manage.go @@ -70,7 +70,7 @@ type AgentCount struct { // those kept as files of their own, in a layout magpie knows whole. func Deletable(agent string) bool { switch agent { - case "claude", "qoder", "qoder-cn", "workbuddy", "codebuddy", "codex", "pi", "omp", "cursor", "dsh", "droid": + case "claude", "qoder", "qoder-cn", "workbuddy", "codebuddy", "codex", "pi", "raft", "omp", "cursor", "dsh", "droid": return true } return false @@ -193,7 +193,7 @@ func readFor(want, all []file, quick bool) (pending map[string]bool, done func() // session's file, not taken from its name (as assemble does). func idInFile(agent string) bool { switch agent { - case "codex", "pi", "omp", "dsh", "grok", "droid": + case "codex", "pi", "raft", "omp", "dsh", "grok", "droid": return true } return false @@ -204,10 +204,12 @@ func nativeIDs(agents map[string]bool, all []file) map[string]map[string]bool { } func nativeIDsWith(agents map[string]bool, all []file, quick bool) map[string]map[string]bool { - groups := byKey(all, func(f *file) bool { return agents[f.agent] }) + // Raft's Pi sessions are Pi's to the gateway (GatewayKeys) + asked := func(agent string) bool { return agents[agent] || agent == "raft" && agents["pi"] } + groups := byKey(all, func(f *file) bool { return asked(f.agent) }) var want []file for _, f := range all { - if !agents[f.agent] { + if !asked(f.agent) { continue } if idInFile(f.agent) { @@ -221,7 +223,13 @@ func nativeIDsWith(agents map[string]bool, all []file, quick bool) map[string]ma out[a] = map[string]bool{} } for _, fs := range groups { - out[fs[0].agent][sessionID(fs)] = true + id := sessionID(fs) + if a := fs[0].agent; agents[a] { + out[a][id] = true + } + if fs[0].agent == "raft" && agents["pi"] { + out["pi"][id] = true + } } return out } @@ -494,7 +502,7 @@ func sessionPaths(agent, id string, fs []file) []string { } // the folder beside it: a Claude Code session's subagents and // tool results, an omp session's artifacts - if agent != "codex" && agent != "pi" { + if agent != "codex" && agent != "pi" && agent != "raft" { if d := strings.TrimSuffix(f.path, ".jsonl"); d != f.path { if fi, err := os.Stat(d); err == nil && fi.IsDir() { add(d) diff --git a/internal/sessions/pi.go b/internal/sessions/pi.go index 1eb4b3fd5..b708ac316 100644 --- a/internal/sessions/pi.go +++ b/internal/sessions/pi.go @@ -77,6 +77,12 @@ func piFilesIn(dir, sessionDir string) []file { more, _ := filepath.Glob(filepath.Join(d, "*.jsonl")) paths = append(paths, more...) } + return piNamed("pi", paths) +} + +// piNamed are the Pi session files among paths,