Files
magpie/internal/sessions/calls_zcode.go
T
yetone 11dcc743f4 sessions: a listed file is 96 bytes, what only some agents' files have kept behind one pointer
A Sessions poll copies every agent's list of ~20k files several times
(listedRollouts, heldFiles' clone, callFilesWith, allFiles, byKey,
statsRead). Each file was 232 bytes, most of it fields only OpenCode,
ZCode, Hermes, Alma, agy, Cursor's editor or WSL files fill. Those move
into fileMore behind file.more (nil when a file has none); reads go
through m(), writes through w(), which copies first, so copies of a file
never see each other's writes, as before.

A/B with the real ~/.codex and ~/.claude data (ab5.sh, 8 polls 15s
apart, alloc_space between heap-0 and heap-1), base f51ab771f vs this:
- sessionRoutes 848 MB -> 731 MB
- allFiles 426 MB -> 281 MB, heldFiles 322 MB -> 216 MB
- callFilesWith 199 MB -> 109 MB, byKey 86 MB -> 56 MB
- statsRead 467 MB -> 375 MB, listSessions 456 MB -> 352 MB
- all HTTP handlers 1422 MB -> 1379 MB; the profile's total rose
  (2392 -> 2570 MB) in usage's ledger reads, a package this doesn't
  touch, on live data that grew between the runs.
Footprint was not compared: a GUI suite and Go tests ran beside it.

Run before the rebase, at f51ab771f plus this change:
go test -timeout 45m ./internal/sessions/... ./internal/gui/
./internal/usage/... ./internal/cli/ ./internal/tui/ ./internal/otlp/
./internal/tune/ ./internal/agent/ ./internal/gateway/: all ok.
Run at the pushed head (rebased on 6a6c06868, which touches no
sessions file): go test ./internal/sessions/... ok; gofmt clean;
GOOS=linux, windows, darwin go vet -tags nogui ./internal/sessions/
./internal/gui/ ok; the whole tree vetted on all three before the rebase.
Not run: the GUI JS suite (no GUI file changes).
2026-10-27 12:40:00 +08:00

108 lines
3.4 KiB
Go

package sessions
import (
"database/sql"
"encoding/json"
"sort"
"strings"
"sync"
)
// ZCode's calls: the same OpenCode tables its reader uses (zcode.go), one
// call per assistant message, with the AI SDK's tokens — input with the
// cache in it, output with the reasoning. A message's model is read the way
// the tables hold it: modelID, or model.id under a message written by a newer
// ZCode, and the provider the reply came from beside it. The rows are read
// here rather than taken from the session summary, because the summary counts
// a reply once however many of its lines carry it, while each assistant
// message is a call of its own.
//
// As with dsh, the Sessions page read these all along and the usage ledger
// did not, so ZCode's tokens never reached the Usage page.
//
// A reply through magpie's gateway is left out here as OpenCode's is
// (ocGatewayProvider): ZCode records the provider it was told to use, and
// the route magpie writes says "magpie", so unlike dsh's this one is a
// provider id magpie itself chose and can be trusted to mean the gateway.
// zcCallList is the last listing of ZCode's database, kept while the
// database and its WAL are as they were. Its lock is the one ocCallList
// uses: callSources runs on more than one read at a time, and two of them
// finding the stamp changed would write this at once.
var zcCallList struct {
sync.Mutex
stamp string
files []file
}
// zcodeCallFiles are ZCode's sessions as call sources.
func zcodeCallFiles() []file {
path := zcodeDB()
if !fileExists(path) {
return nil
}
stamp := ocStamp(path)
zcCallList.Lock()
defer zcCallList.Unlock()
if stamp != "" && stamp == zcCallList.stamp {
return append([]file(nil), zcCallList.files...)
}
var out []file
withOCCallDB(path, func(db *sql.DB) {
out = openCodeDBFilesIn("zcode", path, db, "COUNT(m.id) + COALESCE(SUM(m.time_updated), 0)")
})
for i := range out {
out[i].w().oc = nil // the handle is not kept; a read opens it again
}
zcCallList.stamp, zcCallList.files = stamp, out
return append([]file(nil), out...)
}
// readZCodeCalls reads a session's calls whole, as readOpenCodeCalls does.
// ZCode is OpenCode's tables with a title source of its own, so its store is
// zcDB: the model id is modelId or the older modelID, and its tokens the AI
// SDK's.
func readZCodeCalls(f file) (*callFile, bool) {
st := &callFile{Agent: "zcode", Path: f.path, Session: strings.TrimPrefix(f.key, "zcode:")}
i := strings.LastIndex(f.path, "#")
if i < 0 {
return st, false
}
path, sid := f.path[:i], f.path[i+1:]
var info ocInfo
var msgs [][]byte
if !withOCCallDB(path, func(db *sql.DB) {
var store ocStore = zcDB{ocDB{db}}
if ocHasTable(db, "session_v2") {
var n int
if db.QueryRow(`SELECT COUNT(*) FROM session_v2 WHERE id = ?`, sid).Scan(&n) == nil && n > 0 {
store = ocV2DB{db}
}
}
info, _ = store.info(sid)
msgs = store.messages(sid)
}) {
return st, false
}
if st.Session == "" {
st.Session = info.ID
}
for _, b := range msgs {
var m ocMessage
if json.Unmarshal(b, &m) != nil || m.Role != "assistant" || ocGatewayProvider(m.ProviderID) {
continue
}
if c, ok := ocCall(st, m, info.Directory); ok {
c.Agent = "zcode"
st.Calls = append(st.Calls, c)
}
}
sort.SliceStable(st.Calls, func(i, j int) bool {
if !st.Calls[i].Time.Equal(st.Calls[j].Time) {
return st.Calls[i].Time.Before(st.Calls[j].Time)
}
return st.Calls[i].Msg < st.Calls[j].Msg
})
return st, true
}