Files
magpie/internal/sessions/zcode_titles.go
T
yetone 7c0a103eb1 sessions: a file's modification time is unix nanoseconds, 80 bytes a file (#2278)
A Sessions poll copies every session file's `file` into several slices
(distinctOf, listedRollouts, listCCFiles, distinctCodexFiles), and
rolloutsSeen keeps one of every Codex rollout. Its time.Time was 24 of
its 96 bytes. mod is now a modTime (int64 nanoseconds, 0 for none) with
the few methods the package used; a time.Time going in is modOf(t), one
coming out is .Time(). UnixNano keeps the zero time's own value, so a
parse kept on disk for a file with no time still matches.

Run on mote (M5 Pro, macOS 27), with a copy of the real accounts:
- go vet ./...; GOOS=linux and GOOS=windows go vet ./internal/sessions: ok.
- go test ./internal/sessions ./internal/gui ./internal/usage: ok.
- TestFileFitsEightyBytes (new) holds the size and the round trip of a
  time and of none.
- /api/sessions and /api/sessions/stats?days=30 from a fresh start, real
  history (~23k files): byte-identical to v0.1.3499's code (8,068,574 and
  623,801 bytes).
- 40 /api/sessions polls with X-Magpie-Had: 208 MB allocated before,
  185.5 MB with this.
2026-11-10 10:48:04 +08:00

65 lines
1.9 KiB
Go

package sessions
// zcodeTitleFiles are ZCode's sessions as a title lookup needs them: the
// session rows alone, without the message join zcodeFiles makes for a
// session's size and last activity. Titles asks again every 15s, and that
// join over a large database took ~20s.
//
// mu is held by Titles, as openDB's handles expect.
func zcodeTitleFiles() []file {
if !Reads("zcode") {
return nil
}
path := zcodeDB()
if !fileExists(path) {
return nil
}
db := openDB(path)
if db == nil {
return nil
}
rows, err := db.Query(`SELECT id, COALESCE(parent_id, ''), time_updated FROM session`)
if err != nil {
return nil
}
defer rows.Close()
parent := map[string]string{}
var out []file
for rows.Next() {
var id, par string
var updated int64
if rows.Scan(&id, &par, &updated) != nil {
continue
}
parent[id] = par
out = append(out, file{agent: "zcode", path: path + "#" + id, mod: modOf(ms(updated)),
more: &fileMore{sid: id, oc: zcDB{ocDB{db}}}})
}
return ocRoots(out, parent)
}
// zcodeTitleStamps gives the ZCode sessions a name poll asks for the size
// and last activity zcodeFiles would give them, read from each one's own
// messages: ZCode writes a message without touching its session's row, so
// the row's time alone missed a first prompt typed since the last poll, and
// a stamp unlike the full listing's had the Sessions page and the poll each
// read the session again after the other. A session whose messages can't
// be read keeps its row's stamp.
func zcodeTitleStamps(files []file, want map[string]bool) {
for i := range files {
f := &files[i]
if f.agent != "zcode" || !want[f.key] {
continue
}
zc, ok := f.m().oc.(zcDB)
if !ok {
continue
}
var n, last int64
if zc.db.QueryRow(`SELECT COUNT(id), COALESCE(MAX(time_updated), 0) FROM message WHERE session_id = ?`, f.m().sid).Scan(&n, &last) != nil {
continue
}
f.size, f.mod = n, modOf(ms(max(f.mod.UnixMilli(), last)))
}
}