Follow-up to Koohoko's 3d6f8b8e, after review. - Cost: assemble asks DesktopTitles once per native Claude Desktop session, and each call globbed and stat'ed every local_*.json, so a list of N Cowork sessions looked at the metadata N times. The names are now one index, rebuilt at most once a second (desktopTitlesHeld); a rename in Desktop shows within that second. Files are read through filememo, the shared reader the usage resolver already uses for these same files, which also catches a same-size rewrite inside one mtime tick (the patch's own size+mtime cache didn't). - A record that is there but doesn't parse (Desktop midway through a write) keeps its last parse instead of dropping to the bare id; a record gone still takes its names with it. - Handles: Claude.app 2.7032's LocalSessionManager (app.asar, read-only) counts unarchivedCliSessionId as a live handle beside cliSessionId and preClearCliSessionId as lineage beside priorCliSessionIds, and runs a record with no cliSessionId under its sessionId without "local_". All are named now; the raw "local_..." id no longer is (no usage record carries it). - Siblings: MergeExternalStats (the Sessions page's stats summaries) and the Context page named gateway-only Desktop sessions by the recorded prompt or nothing; both use the Desktop name now. - docs/subsystems/gateway-sessions.md says all of the above. Real layout: no Desktop session records exist on this Mac or the Windows box (only claude_desktop_config.json), so field names and types were checked against Claude.app 2.7032's own reader/writer code: a string title, string cliSessionId/unarchivedCliSessionId/ preClearCliSessionId, an array priorCliSessionIds, files <account>/<org>/local_*.json under claude-code-sessions and local-agent-mode-sessions. Dirs come from desktopdir.Everywhere, the resolver the other Desktop session readers use (Windows %APPDATA% and MSIX LocalCache, Linux ~/.config); Windows/Linux not run. Run: new tests TestDesktopSessionTitleHandles, TestDesktopSessionTitleInStats, TestDesktopTitlesReadOncePerList, and context checks in TestDesktopSessionTitlesOnPages. Each broken by hand went red: rescan per call -> "metadata looked at 12 times, want once"; no last-good -> "a half-written file lost its names"; no unarchived/stem -> missing ids; stats without the name -> "stats titles map[cli-s:Recorded prompt ...]"; Context without it -> "context titles map[...: ...]". go test -race -count=20 -run TestDesktop ./internal/sessions; go test -tags nogui ./... (sandbox HOME, pinned caches): all ok but internal/plugin's TestPiPackage (pi-codemode export missing; passes alone, doesn't import sessions or gui), filed as #1879; gofmt -l; go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...; go build, GOOS=linux go build -tags nogui, GOOS=windows go build. GUI: routing-sessions, session-identity, sessions-overview, sessions-bar-names, sessions-gateway-delete, sessions-manage, context-window in Chromium and WebKit (84/84 each). Real binary: magpie web under a sandbox HOME with Claude-3p Code and Claude Cowork fixture records and five claude-desktop usage records; the Sessions page (Chromium, WebKit) showed both titles on current, prior and unarchived ids and the bare id on the unmatched one; /api/gateway/session-titles named them; a rename showed after 1.3s; a half-written file kept the name; deleting the record went back to the id. Co-authored-by: Koohoko <koohoko@gmail.com>
125 lines
4.0 KiB
Go
125 lines
4.0 KiB
Go
package sessions
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/filememo"
|
|
)
|
|
|
|
// desktopTitle is what a Desktop session record (local_<id>.json) says of
|
|
// its name and the CLI ids it ran under, in Desktop's own field names
|
|
// (Claude.app 2.7032's LocalSessionManager): its live handles are
|
|
// cliSessionId and unarchivedCliSessionId, its lineage preClearCliSessionId
|
|
// and priorCliSessionIds, and a record with no cliSessionId runs under its
|
|
// sessionId without "local_".
|
|
type desktopTitle struct {
|
|
Session string `json:"sessionId"`
|
|
CLI string `json:"cliSessionId"`
|
|
Unarchived string `json:"unarchivedCliSessionId"`
|
|
PreClear string `json:"preClearCliSessionId"`
|
|
Prior []string `json:"priorCliSessionIds"`
|
|
Title string `json:"title"`
|
|
}
|
|
|
|
var desktopTitles struct {
|
|
sync.Mutex
|
|
at time.Time
|
|
names map[string]string // CLI id → name; "" when its owners disagree
|
|
last map[string]desktopTitle // metadata file → its last parse that read
|
|
scans int // how many times the files were looked at, for tests
|
|
}
|
|
|
|
// desktopTitlesHeld is how long the names read stand before the files are
|
|
// looked at again: assemble asks once per Desktop session, and a list of a
|
|
// few hundred of them would otherwise stat every metadata file each time.
|
|
// A rename in Desktop shows within it. Tests set it to 0.
|
|
var desktopTitlesHeld = time.Second
|
|
|
|
// DesktopTitles reads Code and Cowork names from Desktop's local metadata,
|
|
// even when the CLI transcript is absent. Only IDs explicitly recorded there
|
|
// are named; prior CLI runs keep their own usage identity and share the name.
|
|
func DesktopTitles(ids []string) map[string]string {
|
|
out := map[string]string{}
|
|
if len(ids) == 0 {
|
|
return out
|
|
}
|
|
desktopTitles.Lock()
|
|
defer desktopTitles.Unlock()
|
|
if desktopTitles.names == nil || time.Since(desktopTitles.at) >= desktopTitlesHeld {
|
|
scanDesktopTitles()
|
|
}
|
|
for _, id := range ids {
|
|
if name := desktopTitles.names[id]; name != "" {
|
|
out[id] = name
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// scanDesktopTitles rebuilds the names; the caller holds desktopTitles.
|
|
// A file that is there but can't be read or parsed (Desktop midway through
|
|
// writing it) is unknown, not unnamed: its last parse stands. A file gone
|
|
// takes its names with it.
|
|
func scanDesktopTitles() {
|
|
desktopTitles.scans++
|
|
names, ranks := map[string]string{}, map[string]int{}
|
|
// A current ID takes precedence over a prior ID. Conflicting owners of
|
|
// the same rank leave the name unknown instead of picking a profile.
|
|
add := func(id, name string, rank int) {
|
|
if id == "" || rank < ranks[id] {
|
|
return
|
|
}
|
|
if rank > ranks[id] {
|
|
ranks[id], names[id] = rank, name
|
|
} else if names[id] != name {
|
|
names[id] = ""
|
|
}
|
|
}
|
|
last := map[string]desktopTitle{}
|
|
for _, dir := range DesktopDataDirs() {
|
|
for _, kind := range []string{"claude-code-sessions", "local-agent-mode-sessions"} {
|
|
paths, _ := SessionGlob(filepath.Join(dir, kind, "*", "*", "local_*.json"))
|
|
for _, path := range paths {
|
|
info, err := os.Stat(path)
|
|
if err != nil || !info.Mode().IsRegular() || info.Size() > 8<<20 {
|
|
continue
|
|
}
|
|
d, err := filememo.Read("session desktop title", path, func(b []byte) (desktopTitle, error) {
|
|
var d desktopTitle
|
|
err := json.Unmarshal(b, &d)
|
|
d.Title = title(d.Title)
|
|
return d, err
|
|
})
|
|
if err != nil {
|
|
old, ok := desktopTitles.last[path]
|
|
if !ok || errors.Is(err, fs.ErrNotExist) {
|
|
continue
|
|
}
|
|
d = old
|
|
}
|
|
last[path] = d
|
|
stem := strings.TrimPrefix(d.Session, "local_")
|
|
if d.CLI == "" && stem != d.Session {
|
|
add(stem, d.Title, 2)
|
|
} else if stem != d.Session {
|
|
add(stem, d.Title, 1)
|
|
}
|
|
add(d.CLI, d.Title, 2)
|
|
add(d.Unarchived, d.Title, 2)
|
|
add(d.PreClear, d.Title, 1)
|
|
for _, id := range d.Prior {
|
|
add(id, d.Title, 1)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
desktopTitles.names, desktopTitles.last, desktopTitles.at = names, last, time.Now()
|
|
}
|