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

36 lines
896 B
Go

package sessions
import (
"encoding/json"
"strings"
"time"
)
// CodexThreadCwd is the folder Codex thread id runs in, as its newest
// rollout's session_meta says; "" when no rollout of it is read. magpie's
// MCP hub asks it for Codex, which says no roots (roots/list answers
// none) but names its thread in each tool call's _meta, so magpie-image
// behind the hub still saves into the thread's project.
func CodexThreadCwd(id string) string {
id = strings.TrimSpace(id)
if id == "" {
return ""
}
var path string
var mod time.Time
for _, f := range codexFiles() {
if f.cold || !strings.EqualFold(f.key, "codex:"+id) || (path != "" && !f.mod.After(modOf(mod))) {
continue
}
path, mod = f.path, f.mod.Time()
}
if path == "" {
return ""
}
var l cxLine
if json.Unmarshal(firstLine(path), &l) != nil || l.Type != "session_meta" {
return ""
}
return l.Payload.Cwd
}