Sky (skyhuang1994) on Discord: on ZCode, 10-31 showed ~0.70B GLM-5.3
tokens where ZCode's own model_usage had ~0.28B. A selection side chat
opened on 11-02 from a 10-31 session holds a copy of its 860 assistant
messages, with the 10-31 times and the same 421,041,276 tokens, and made
no calls. On Codex, a week of 2026-03 read 5,136,041,178 gpt-5.4 tokens,
where the machine had spent ~0.27B.
ZCode: ZCode 3.17's CLI copies a session's messages (gce, for a side
chat, a fork and a checkpoint restored) with a new id and session, the
original's time, model and tokens, and metadata.forkOrigin
{sessionId, messageId}. zcDB.copied says a message is a copy of one still
in the database, and readZCodeCalls and parseOpenCode (for ZCode) leave
it out. A copy of a copy names the copy it came from, so of each chain
one is counted. A copy whose original is gone is counted. ZCode's own
fork notices carry a forkOrigin with no messageId, and are not copies.
Codex: a fork (/fork, spawn_agent with the parent's context) writes every
line of the parent's rollout into its own first, token_count events too,
stamped at the fork's time (codex-rs 0.117 record_initial_history,
InitialHistory::Forked). Each copied checkpoint was booked as a call on
the fork's day. The fork's session_meta has forked_from_id. The parent's
rollout is found in the same Codex home: the UUIDv7 day of its id ±1
day, then every day, then archived_sessions. Its checkpoints, {total,
last}, are read once (8 parents kept), and a fork's checkpoint found among
them only advances the running total, on Usage (calls) and Sessions
(stats) both. A parent not found leaves the fork read as before.
callParseVersions and parseVersions raise codex and zcode to 1, so their
kept parses are read again once. The version tests (call_versions,
parse_versions, cache_decode) now hold the versions at none raised: they
test the mechanism, not the current numbers.
docs/subsystems/gateway-sessions.md says all of this. Filed #5072:
OpenCode's fork copies the same way and names no original, so it is
still counted twice.
Run at this head on mote (mote-run):
- 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'
./internal/gui ./internal/cli : ok
Broken by hand:
- zcDB.copied returning false: TestZCodeCopiesCountedOnce red. msg_c1 and
msg_d1 were counted as calls, and the Sessions page's 10-31 read
{1200 300 1800} for {400 100 600}.
- the copied() check in count() off: TestCodexForkCountsOnlyItsOwnCalls
red. 4 calls on the fork, {87984 5950 268416} for {45744 3550 179584}.
Not tried:
- a real ZCode database or side chat. There is none on this machine.
The copy shape was read from ZCode 3.17.2's zcode.cjs (gce).
- a real forked Codex rollout or one from 2026-03. ~/.codex/sessions here
spans 2026-07 to 11 and has no forks. The fixtures follow codex-rs
0.117's writer. Whether Sky's 5.1B week is all fork copies is not
known; a cumulative count read as per-turn in an older format was not
found in that source.
- newer rollouts' token_usage_record copies in a fork, and a parent in
another Codex home (WSL, CODEX_HOME).
- the whole gui and gateway suites. Another session's suite was running
on mote; gui and cli ran only the tests above.
80 lines
3.1 KiB
Go
80 lines
3.1 KiB
Go
package sessions
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// ZCode's CLI, OpenCode made over, keeps its sessions in OpenCode's tables
|
|
// in a database of its own, ~/.zcode/cli/db/db.sqlite: a session row with
|
|
// how its title came about (title_source), and its messages and parts as
|
|
// JSON documents. A message counts as OpenCode's does but for its tokens,
|
|
// the AI SDK's: input with the cache in it, output with the reasoning. Its
|
|
// model is modelId (the older ones say modelID too), and a user message
|
|
// says who it is from (semantics.origin), a prompt typed a real_user's. A
|
|
// subagent's work is a session whose parent is the one it ran in, as in
|
|
// OpenCode. ZCode is a desktop app: no command picks a session up again.
|
|
|
|
// ZCodeDir is ZCode's folder, ~/.zcode, under Go's home. Its CLI's
|
|
// database stays there, USERPROFILE on Windows, even where the app keeps
|
|
// the rest under a HOME the user set (appdir.ZCodeHome): ZCode 3.14.3 for
|
|
// Windows, run with both, wrote cli\db\db.sqlite under USERPROFILE and
|
|
// v2 under HOME.
|
|
func ZCodeDir() string {
|
|
home, _ := os.UserHomeDir()
|
|
return filepath.Join(home, ".zcode")
|
|
}
|
|
|
|
func zcodeDB() string { return filepath.Join(ZCodeDir(), "cli", "db", "db.sqlite") }
|
|
|
|
func zcodeFiles() []file {
|
|
if !Reads("zcode") {
|
|
return nil
|
|
}
|
|
if db := zcodeDB(); fileExists(db) {
|
|
return openCodeDBFiles("zcode", db)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// zcDB is ZCode's database: OpenCode's, with a session's title its own only
|
|
// when it was not the default one.
|
|
type zcDB struct{ ocDB }
|
|
|
|
func (s zcDB) info(sid string) (ocInfo, bool) {
|
|
var i ocInfo
|
|
var source string
|
|
err := s.db.QueryRow(`SELECT id, COALESCE(parent_id, ''), directory, title, COALESCE(title_source, ''), time_created, time_updated FROM session WHERE id = ?`, sid).
|
|
Scan(&i.ID, &i.ParentID, &i.Directory, &i.Title, &source, &i.Time.Created, &i.Time.Updated)
|
|
if source == "default" {
|
|
i.Title = ""
|
|
}
|
|
return i, err == nil
|
|
}
|
|
|
|
// copied says whether a message is a copy of one still in the database.
|
|
// ZCode copies a session's messages into another for a side chat on a
|
|
// selection, a fork and a checkpoint restored (ZCode 3.17's CLI): each
|
|
// copy gets an id and session of its own and keeps the original's time,
|
|
// model and tokens, and its metadata.forkOrigin names the message it was
|
|
// copied from, {sessionId, messageId}. The copy made no call, so its
|
|
// tokens are the original's, and counting both counted the session again
|
|
// on the day it ran (Sky, skyhuang1994: a side chat opened on 11-02 from a
|
|
// session of 10-31 put 0.42B more GLM-5.3 tokens on 10-31, where ZCode's
|
|
// own model_usage has none for it). A copy of a copy names the copy it
|
|
// was made from, so one copy is counted only when what it came from is
|
|
// gone: the session deleted, or never in this database. ZCode's own
|
|
// notices of a fork carry a forkOrigin of another shape, with no
|
|
// messageId, and are no copies.
|
|
func (s zcDB) copied(m ocMessage) bool {
|
|
if m.Metadata == nil || m.Metadata.ForkOrigin == nil {
|
|
return false
|
|
}
|
|
id := m.Metadata.ForkOrigin.MessageID
|
|
if id == "" || id == m.ID {
|
|
return false
|
|
}
|
|
var n int
|
|
return s.db.QueryRow(`SELECT COUNT(*) FROM message WHERE id = ?`, id).Scan(&n) == nil && n > 0
|
|
}
|