Files
magpie/internal/sessions/zcode_fork_test.go
T
yetone fb1f4856a8 sessions: a fork's own calls are counted; only its copies are the parent's (ZCode, Codex)
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/main
  7d022bdb6 ("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
on 08d7bfaaa (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.
2026-11-03 20:21:17 +08:00

189 lines
8.2 KiB
Go

package sessions
import (
"database/sql"
"fmt"
"path/filepath"
"testing"
"time"
)
// zcForkDB adds to the ZCode database setupAgents made a session on
// 2026-10-31, a selection side chat opened from it on 11-02, a side chat
// opened from that one, and a fork whose parent is gone, each message as
// ZCode 3.17's CLI copies it (gce): a new id and session, the original's
// time, model and tokens, and metadata.forkOrigin {sessionId, messageId}.
func zcForkDB(t *testing.T, home string) {
t.Helper()
db, err := sql.Open("sqlite", filepath.Join(home, ".zcode", "cli", "db", "db.sqlite"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { db.Close() })
day := func(d, h int) int64 { return time.Date(2026, 10, d, h, 0, 0, 0, time.UTC).UnixMilli() }
for _, s := range []struct {
id, title string
at int64
}{
{"sess_main", "Port the parser", day(31, 0)},
{"sess_side", "Selection side chat", day(33, 9)}, // 11-02
{"sess_side2", "Selection side chat", day(33, 10)},
{"sess_orphan", "Fork", day(30, 0)},
} {
if _, err := db.Exec("INSERT INTO session (id, project_id, parent_id, slug, directory, title, title_source, version, time_created, time_updated) VALUES (?, 'p', NULL, 's', '/work/zcf', ?, 'ai', '3.17.2', ?, ?)",
s.id, s.title, s.at, s.at); err != nil {
t.Fatal(err)
}
}
reply := func(id, parent string, created int64, in, out, read int, meta string) string {
return fmt.Sprintf(`{"id":%q,"role":"assistant","parentID":%q,"modelId":"glm-5.3","providerID":"zai","tokens":{"total":%d,"input":%d,"output":%d,"reasoning":0,"cache":{"read":%d,"write":0}},"time":{"created":%d,"completed":%d}%s}`,
id, parent, in+out, in, out, read, created, created+30000, meta)
}
origin := func(sid, mid string) string {
return fmt.Sprintf(`,"metadata":{"forkOrigin":{"sessionId":%q,"messageId":%q}}`, sid, mid)
}
m1 := day(31, 1)
for _, m := range []struct {
sid, id string
at int64
data string
}{
{"sess_main", "msg_m0", m1 - 1000, `{"id":"msg_m0","role":"user","time":{"created":` + fmt.Sprint(m1-1000) + `},"semantics":{"origin":"real_user"}}`},
{"sess_main", "msg_m1", m1, reply("msg_m1", "msg_m0", m1, 1000, 100, 600, "")},
// the side chat: the main session copied in, then its own reply
{"sess_side", "msg_c0", m1 - 1000, `{"id":"msg_c0","role":"user","time":{"created":` + fmt.Sprint(m1-1000) + `},"semantics":{"origin":"real_user"}` + origin("sess_main", "msg_m0") + `}`},
{"sess_side", "msg_c1", m1, reply("msg_c1", "msg_c0", m1, 1000, 100, 600, origin("sess_main", "msg_m1"))},
{"sess_side", "msg_cn", day(33, 9), `{"id":"msg_cn","role":"assistant","time":{"created":` + fmt.Sprint(day(33, 9)) + `},"metadata":{"forkOrigin":{"parentSessionId":"sess_main","targetMessageId":"msg_m1"}}}`},
{"sess_side", "msg_c2", day(33, 9) + 5000, reply("msg_c2", "msg_c0", day(33, 9)+5000, 20, 2, 0, "")},
// a side chat of the side chat: a copy of a copy
{"sess_side2", "msg_d1", m1, reply("msg_d1", "", m1, 1000, 100, 600, origin("sess_side", "msg_c1"))},
// a fork whose parent session was deleted: its copy is all there is
{"sess_orphan", "msg_o1", day(30, 1), reply("msg_o1", "", day(30, 1), 300, 30, 0, origin("sess_gone", "msg_gone"))},
} {
ocInsert(t, db, m.sid, m.id, m.at, m.at+30000, m.data)
}
}
// A message ZCode copied into a side chat, a fork or a copy of one made no
// call: the session it was copied from counts it, once (Sky, skyhuang1994:
// a side chat opened on 11-02 put a 10-31 session's 0.42B tokens on 10-31
// a second time).
func TestZCodeCopiesCountedOnce(t *testing.T) {
inZone(t, 0)
zcForkDB(t, setupAgents(t))
cs := zcodeCalls(t)
for _, id := range []string{"msg_c1", "msg_d1"} {
if c, ok := cs[id]; ok {
t.Errorf("the copy %s was counted as a call: %+v", id, c)
}
}
if c := cs["msg_m1"]; c.Session != "sess_main" || c.Tokens != (Tokens{400, 100, 600, 0, 0}) {
t.Errorf("the original: %+v", c)
}
if c := cs["msg_c2"]; c.Session != "sess_side" || c.Tokens != (Tokens{20, 2, 0, 0, 0}) {
t.Errorf("the side chat's own reply: %+v", c)
}
if c := cs["msg_o1"]; c.Session != "sess_orphan" || c.Tokens != (Tokens{300, 30, 0, 0, 0}) {
t.Errorf("a copy whose original is gone: %+v", c)
}
s := statsAt(0, time.Date(2026, 11, 3, 12, 0, 0, 0, time.UTC))
for _, d := range []struct {
date string
want Tokens
}{
{"2026-10-31", Tokens{400, 100, 600, 0, 0}},
{"2026-11-02", Tokens{20, 2, 0, 0, 0}},
{"2026-10-30", Tokens{300, 30, 0, 0, 0}},
} {
if got, _ := usageOn(s, d.date, "/work/zcf"); got != d.want {
t.Errorf("the Sessions page's %s: %+v, want %+v", d.date, got, d.want)
}
}
}
// A fork that went on working: ZCode copied an August session's replies
// into it, then it made calls of its own through September. ZCode keeps a
// message's id in its row, not in its data, as OpenCode does, so the rows
// here carry none in data; the provider is a custom one, named by a UUID.
// Only the copies are the parent's. Every reply of the fork's own is a
// call (Sky, skyhuang1994: the "msc" fork's 11,409 September calls, 3.49B
// tokens, none with a forkOrigin, went missing from the Usage page once
// the fork held one copy).
func TestZCodeForkThatWentOnWorkingKeepsItsOwnCalls(t *testing.T) {
inZone(t, 0)
home := setupAgents(t)
db, err := sql.Open("sqlite", filepath.Join(home, ".zcode", "cli", "db", "db.sqlite"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { db.Close() })
const provider = "6f1c2b9e-4a7d-4c1e-9b2a-3d5e8f0a1b2c"
at := func(m time.Month, d, h, min int) int64 {
return time.Date(2026, m, d, h, min, 0, 0, time.UTC).UnixMilli()
}
for _, s := range []struct {
id string
at int64
}{{"ses_aug", at(8, 30, 0, 0)}, {"ses_msc", at(9, 1, 0, 0)}} {
if _, err := db.Exec("INSERT INTO session (id, project_id, parent_id, slug, directory, title, title_source, version, time_created, time_updated) VALUES (?, 'p', NULL, 's', '/work/msc', ?, 'ai', '3.17.2', ?, ?)",
s.id, s.id, s.at, s.at); err != nil {
t.Fatal(err)
}
}
reply := func(created int64, in, out, read int, meta string) string {
return fmt.Sprintf(`{"role":"assistant","parentID":"msg_u","modelId":"glm-5.3","providerID":%q,"tokens":{"total":%d,"input":%d,"output":%d,"reasoning":0,"cache":{"read":%d,"write":0}},"time":{"created":%d,"completed":%d}%s}`,
provider, in+out, in, out, read, created, created+30000, meta)
}
// the parent's two replies on 08-30, and their copies in the fork
for i := range 2 {
c := at(8, 30, 1, i)
ocInsert(t, db, "ses_aug", fmt.Sprintf("msg_a%d", i), c, c+30000, reply(c, 1000, 100, 600, ""))
ocInsert(t, db, "ses_msc", fmt.Sprintf("msg_k%d", i), c, c+30000,
reply(c, 1000, 100, 600, fmt.Sprintf(`,"metadata":{"forkOrigin":{"sessionId":"ses_aug","messageId":"msg_a%d"}}`, i)))
}
// ZCode's notice of the fork
ocInsert(t, db, "ses_msc", "msg_kn", at(9, 1, 0, 0), at(9, 1, 0, 0),
fmt.Sprintf(`{"role":"assistant","time":{"created":%d},"metadata":{"forkOrigin":{"parentSessionId":"ses_aug","targetMessageId":"msg_a1"}}}`, at(9, 1, 0, 0)))
// the fork's own work in September: no forkOrigin
const own = 40
for i := range own {
c := at(9, 2+i%20, 10, i)
ocInsert(t, db, "ses_msc", fmt.Sprintf("msg_w%02d", i), c, c+30000, reply(c, 500, 50, 200, ""))
}
bySession := map[string]Tokens{}
n := map[string]int{}
for _, c := range Calls(time.Time{}) {
if c.Agent == "zcode" {
v := bySession[c.Session]
v.add(c.Tokens)
bySession[c.Session] = v
n[c.Session]++
}
}
var wantOwn Tokens
for range own {
wantOwn.add(Tokens{300, 50, 200, 0, 0})
}
if n["ses_msc"] != own || bySession["ses_msc"] != wantOwn {
t.Errorf("the fork's calls: %d %+v, want its %d own %+v", n["ses_msc"], bySession["ses_msc"], own, wantOwn)
}
if n["ses_aug"] != 2 || bySession["ses_aug"] != (Tokens{800, 200, 1200, 0, 0}) {
t.Errorf("the parent's calls: %d %+v", n["ses_aug"], bySession["ses_aug"])
}
s := statsAt(0, time.Date(2026, 11, 3, 12, 0, 0, 0, time.UTC))
if got, _ := usageOn(s, "2026-08-30", "/work/msc"); got != (Tokens{800, 200, 1200, 0, 0}) {
t.Errorf("the Sessions page's 08-30: %+v, want the parent's alone", got)
}
var sept Tokens
for d := 2; d <= 21; d++ {
got, _ := usageOn(s, fmt.Sprintf("2026-09-%02d", d), "/work/msc")
sept.add(got)
}
if sept != wantOwn {
t.Errorf("the Sessions page's September: %+v, want %+v", sept, wantOwn)
}
}