Raft (slock's raft-computer) runs its agents on Pi and keeps each one's Pi SDK session files in that agent's folder: <slock>/agents/<id>/ .pi-sessions/<ts>_<id>.jsonl, or .builtin-sessions/ for its built-in runtime. <slock> 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 one61e10535): - 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 on5d24bc6cc: 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/mainc1660481con 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/mainc1660481ctoo (#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.
205 lines
6.1 KiB
Go
205 lines
6.1 KiB
Go
package sessions
|
|
|
|
import (
|
|
"cmp"
|
|
"math"
|
|
"slices"
|
|
"sort"
|
|
"time"
|
|
)
|
|
|
|
// ExternalModel and ExternalSession are read-only session projections from a
|
|
// request ledger such as the gateway. They intentionally carry no file or
|
|
// resume capability.
|
|
type ExternalModel struct {
|
|
Model string
|
|
Tokens
|
|
Cost float64
|
|
Priced bool
|
|
}
|
|
type ExternalSession struct {
|
|
Agent, ID string
|
|
Title string // what it was first asked (GatewayTitle); its id when ""
|
|
Start, Last time.Time
|
|
Models []ExternalModel
|
|
Daily []ExternalDayUsage
|
|
Tokens
|
|
Cost float64
|
|
Unpriced int
|
|
}
|
|
|
|
// MergeExternal adds external sessions to a file-backed listing. Native
|
|
// sessions win on the exact Agent+ID identity and external entries are kept
|
|
// read-only (no file of the agent's to resume or open) with a gateway transcript
|
|
// reader; magpie's own record of one is deleted by DeleteGateway.
|
|
func MergeExternal(native []Session, external []ExternalSession) []Session {
|
|
names := desktopExternalTitles(external)
|
|
seen := map[string]bool{}
|
|
for _, s := range native {
|
|
for _, k := range GatewayKeys(s) {
|
|
seen[k] = true
|
|
}
|
|
for _, id := range s.Copies {
|
|
seen[s.Agent+"|"+id] = true
|
|
}
|
|
}
|
|
out := append([]Session(nil), native...)
|
|
for _, e := range external {
|
|
if e.Agent == "" || e.ID == "" || seen[e.Agent+"|"+e.ID] {
|
|
continue
|
|
}
|
|
s := Session{ReadOnly: true, Gateway: true, Transcript: true, Agent: e.Agent, ID: e.ID, Title: cmp.Or(names[e.Agent+"|"+e.ID], e.Title, e.ID), Start: e.Start, Last: e.Last, Models: []Model{}, Unpriced: e.Unpriced}
|
|
for _, m := range e.Models {
|
|
s.Models = append(s.Models, Model{Model: m.Model, Tokens: m.Tokens, Cost: m.Cost, Priced: m.Priced})
|
|
s.Tokens.add(m.Tokens)
|
|
}
|
|
s.Cost = e.Cost
|
|
out = append(out, s)
|
|
}
|
|
sort.SliceStable(out, func(i, j int) bool { return out[i].Last.After(out[j].Last) })
|
|
return out
|
|
}
|
|
|
|
// desktopExternalTitles are the names an app saved for its sessions among
|
|
// external, by Agent|ID: Claude Desktop's (DesktopTitles), PI-Desktop's
|
|
// (PIDesktopTitles, #1967). They come before the gateway's recorded first
|
|
// prompt.
|
|
func desktopExternalTitles(external []ExternalSession) map[string]string {
|
|
var ids, pi []string
|
|
for _, e := range external {
|
|
if e.Agent == "claude-desktop" {
|
|
ids = append(ids, e.ID)
|
|
} else if isPIDesktop(e.Agent) {
|
|
pi = append(pi, e.ID)
|
|
}
|
|
}
|
|
out := map[string]string{}
|
|
for id, name := range DesktopTitles(ids) {
|
|
out["claude-desktop|"+id] = name
|
|
}
|
|
if len(pi) > 0 {
|
|
names := PIDesktopTitles(pi)
|
|
for _, e := range external {
|
|
if name := names[e.ID]; name != "" && isPIDesktop(e.Agent) {
|
|
out[e.Agent+"|"+e.ID] = name
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// MergeExternalStats appends external daily usage and session summaries to a
|
|
// Stats snapshot. It is deliberately data-only so callers can keep their own
|
|
// source and pricing policy.
|
|
type ExternalDayUsage struct {
|
|
Date, Agent, Cwd, Model string
|
|
Tokens
|
|
Cost float64
|
|
Priced bool
|
|
}
|
|
|
|
// st itself is not written: its Days and Sessions may be kept for the next
|
|
// read (keptStats), so what is changed is copied first.
|
|
func MergeExternalStats(st Stats, days []ExternalDayUsage, external []ExternalSession) Stats {
|
|
oldFirst, _ := time.ParseInLocation(time.DateOnly, st.From, time.Local)
|
|
sessionsOwn := false
|
|
for _, d := range days {
|
|
if d.Date <= st.To && (st.From == "" || d.Date < st.From) {
|
|
st.From = d.Date
|
|
}
|
|
}
|
|
first, _ := time.ParseInLocation(time.DateOnly, st.From, time.Local)
|
|
if !oldFirst.IsZero() && !first.Equal(oldFirst) {
|
|
shift := int(math.Round(oldFirst.Sub(first).Hours() / 24))
|
|
st.Sessions, sessionsOwn = slices.Clone(st.Sessions), true
|
|
for i := range st.Sessions {
|
|
s := &st.Sessions[i]
|
|
s.Days = slices.Clone(s.Days)
|
|
for j := range s.Days {
|
|
s.Days[j] += shift
|
|
}
|
|
s.perDay = slices.Clone(s.perDay)
|
|
for j := range s.perDay {
|
|
s.perDay[j].day += shift
|
|
}
|
|
}
|
|
}
|
|
st.Days = slices.Clone(st.Days)
|
|
byDate, own := map[string]int{}, map[int]bool{}
|
|
for i := range st.Days {
|
|
byDate[st.Days[i].Date] = i
|
|
}
|
|
for _, u := range days {
|
|
if u.Date < st.From || u.Date > st.To {
|
|
continue
|
|
}
|
|
i, ok := byDate[u.Date]
|
|
if !ok {
|
|
st.Days = append(st.Days, Day{Date: u.Date, Usage: []Usage{}, Active: []Active{}})
|
|
i = len(st.Days) - 1
|
|
byDate[u.Date], own[i] = i, true
|
|
}
|
|
d := &st.Days[i]
|
|
if !own[i] {
|
|
// a day of st's: its rows are copied before one is added to
|
|
d.Usage, own[i] = slices.Clone(d.Usage), true
|
|
}
|
|
found := false
|
|
for i := range d.Usage {
|
|
if d.Usage[i].Agent == u.Agent && d.Usage[i].Cwd == u.Cwd && d.Usage[i].Model == u.Model {
|
|
d.Usage[i].Tokens.add(u.Tokens)
|
|
d.Usage[i].Cost += u.Cost
|
|
d.Usage[i].Priced = d.Usage[i].Priced && u.Priced
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
d.Usage = append(d.Usage, Usage{Agent: u.Agent, Cwd: u.Cwd, Model: u.Model, Tokens: u.Tokens, Cost: u.Cost, Priced: u.Priced})
|
|
}
|
|
}
|
|
names := desktopExternalTitles(external)
|
|
for _, e := range external {
|
|
if e.ID == "" || e.Agent == "" {
|
|
continue
|
|
}
|
|
s := Summary{Key: e.Agent + ":" + e.ID, Agent: e.Agent, ID: e.ID, Title: cmp.Or(names[e.Agent+"|"+e.ID], e.Title), Gateway: true, Last: e.Last, Tokens: e.Tokens, Cost: e.Cost, Priced: true, Models: []string{}, Days: []int{}}
|
|
perDay := map[int]*summaryDay{}
|
|
for _, u := range e.Daily {
|
|
day, err := time.ParseInLocation(time.DateOnly, u.Date, time.Local)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
d := int(math.Round(day.Sub(first).Hours() / 24))
|
|
if first.IsZero() || u.Date < st.From || u.Date > st.To {
|
|
continue
|
|
}
|
|
pd := perDay[d]
|
|
if pd == nil {
|
|
pd = &summaryDay{day: d}
|
|
perDay[d] = pd
|
|
s.Days = append(s.Days, d)
|
|
}
|
|
pd.output += u.Output
|
|
}
|
|
sort.Ints(s.Days)
|
|
for _, d := range s.Days {
|
|
s.perDay = append(s.perDay, *perDay[d])
|
|
}
|
|
for _, m := range e.Models {
|
|
s.Models = append(s.Models, m.Model)
|
|
s.Priced = s.Priced && m.Priced
|
|
}
|
|
if !sessionsOwn {
|
|
st.Sessions, sessionsOwn = slices.Clip(st.Sessions), true
|
|
}
|
|
st.Sessions = append(st.Sessions, s)
|
|
}
|
|
sort.Slice(st.Days, func(i, j int) bool { return st.Days[i].Date < st.Days[j].Date })
|
|
if sessionsOwn {
|
|
// st's own come sorted so
|
|
sort.SliceStable(st.Sessions, func(i, j int) bool { return st.Sessions[i].Last.After(st.Sessions[j].Last) })
|
|
}
|
|
return st
|
|
}
|