Sky (skyhuang1994) on Discord, on their real ZCode database after v0.1.2847 (1d0fc2bb0): September's message table holds 57,787 rows, 13.552B tokens; 5,204 rows (1.537B) carry a forkOrigin and are copies, and ZCode's cloud usage says 12.057B. The new version took 5.094B out, 3.6B of it real. The biggest was the "msc" session: forked at the end of August, then 11,409 calls of its own in September (3.489B), none with a forkOrigin, a custom-UUID provider. The Usage page had 0 of them. Cause (ZCode): readZCodeCalls marked copies in a map keyed by m.ID. A row's id is in the message table's id column, not in its data (OpenCode writes data without id; checked on this machine's real opencode.db: 2 of 2 rows have no $.id), so every message decodes with ID "". One copy in a session set copies[""], and every assistant message of that session was left out: a fork that went on working lost all of its own calls. The test fixtures had "id" inside data, so they never saw it. The Sessions page (parseOpenCode) asks zcDB.copied per message and was right. Fix: copies are marked by row index. Only a message whose forkOrigin names a messageId still in the database is skipped. Codex: a fork's checkpoint was a copy whenever its {total, last} was one the parent's rollout holds, anywhere in the fork. A fork goes on working, and its first own call can carry exactly the numbers of the parent's next call after the fork (same history, same next prompt). The copies are now the head of the fork only: the fork's first turn_id the parent never had (turn_context or task_started) ends them (ownTurn, CopiedTurns, read with the parent's checkpoints). A rollout with no turn ids is matched as before; its running total keeps later calls apart. callParseVersions codex 2, zcode 2 and parseVersions codex 2, so the parses kept by v0.1.2847 are read again once. ZCode's Sessions parse didn't change and stays at 1. Tests (fixtures follow Sky's case: a parent of 08-30, a fork with forkOrigin copies of its replies, ZCode's fork notice, then 40 replies of its own in September with no forkOrigin, no id in data, provider a UUID): - TestZCodeForkThatWentOnWorkingKeepsItsOwnCalls: red on origin/main7d022bdb6("the fork's calls: 0 {...}, want its 40 own"), as Sky saw. - TestCodexForkThatWentOnWorkingKeepsItsOwnCalls: red on main and with ownTurn turned off by hand (1 call on the fork, want its 2 own). docs/subsystems/gateway-sessions.md says this. Run on mote (mote-run) at this change on origin/main 7d022bdb6; rebased on08d7bfaaa(gui, redact, gateway-key only), go build ./... and internal/sessions run again: ok. - gofmt -l internal: empty - GOOS=linux go vet -tags nogui ./... ; GOOS=windows go vet ./... : ok - go test -count=1 -timeout 45m ./internal/sessions ./internal/usage ./internal/tune ./internal/otlp ./internal/davsync ./internal/tui : ok - go test -run 'Session|Usage|Calls|Ledger|Stats|ZCode|Codex|Fork' ./internal/gui ./internal/cli : ok Broken by hand: - the copies keyed by message id again (origin/main): the ZCode test red, 0 calls on the fork. - ownTurn's call in context() off: the Codex test red, 1 call on the fork. Not tried: - Sky's real ZCode database; there is none on this machine and no ZCode source to read its writer. That ZCode's data has no id, as OpenCode's, rests on the symptom (every row of the fork gone) and OpenCode's real rows here. - a real forked Codex rollout that went on working; the turn ids of a real 0.117 fork were read from the fixtures' shape, not a real file. - the whole gui and gateway suites, and a full go test ./...: other sessions' suites were running on mote.
127 lines
4.1 KiB
Go
127 lines
4.1 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 {
|
|
if !Reads("zcode") {
|
|
return nil
|
|
}
|
|
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
|
|
var copies []bool
|
|
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)
|
|
// a copy of a message still in the database (zcDB.copied): the
|
|
// original's session counts its call. Marked by row, not by id: a
|
|
// row's id is in its own column and not in its data, so every
|
|
// message here reads with an empty ID, and one copy marked by id
|
|
// left out the whole session, a fork's own replies too (Sky,
|
|
// skyhuang1994: a fork's 11,409 calls of its own, 3.49B tokens).
|
|
copies = make([]bool, len(msgs))
|
|
if z, ok := store.(zcDB); ok {
|
|
for i, b := range msgs {
|
|
var m ocMessage
|
|
if json.Unmarshal(b, &m) == nil && m.Role == "assistant" && z.copied(m) {
|
|
copies[i] = true
|
|
}
|
|
}
|
|
}
|
|
}) {
|
|
return st, false
|
|
}
|
|
if st.Session == "" {
|
|
st.Session = info.ID
|
|
}
|
|
for i, b := range msgs {
|
|
var m ocMessage
|
|
if json.Unmarshal(b, &m) != nil || m.Role != "assistant" || ocGatewayProvider(m.ProviderID) || copies[i] {
|
|
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
|
|
}
|