Files
magpie/internal/sessions/codex_fork_test.go
T
yetone 1d0fc2bb08 sessions: a fork's copies of its parent's calls are counted once (Codex, ZCode)
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.
2026-11-03 19:13:00 +08:00

92 lines
3.0 KiB
Go

package sessions
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// A fork's rollout starts with every line of its parent's, token_count
// events included, stamped when the fork wrote them (codex-rs 0.117's
// InitialHistory::Forked). The fixtures are that shape: a gpt-5.4 parent of
// 2026-03-23 with three calls (and one more after the fork), and a
// spawn_agent fork of it on 03-24 that made one call of its own. The copies
// are the parent's calls, which the parent's rollout counts; read as the
// fork's, they put the parent's whole history on 03-24 again (Sky).
func TestCodexForkCountsOnlyItsOwnCalls(t *testing.T) {
d := setupCalls(t)
const parent, fork = "019d186b-a100-7aaa-8bbb-cccdddeeefff", "019d1f2d-f9c0-7111-8222-333444555666"
place := func(fixture, day, stamp, id string) {
b, err := os.ReadFile(filepath.Join("testdata", fixture))
if err != nil {
t.Fatal(err)
}
writeLines(t, filepath.Join(d.codex, "sessions", "2026", "03", day, "rollout-2026-03-"+day+"T"+stamp+"-"+id+".jsonl"), strings.TrimSpace(string(b)))
}
place("codex-fork-parent.jsonl", "23", "10-00-00", parent)
place("codex-fork-child.jsonl", "24", "17-30-00", fork)
call := func(in, cached, out, reasoning int) Tokens {
return Tokens{Input: in - cached, CacheRead: cached, Output: out}
}
own := map[string][]Tokens{
parent: {call(41200, 3072, 812, 256), call(43680, 41088, 688, 144), call(46192, 44672, 900, 112), call(46928, 46080, 600, 88)},
fork: {call(47328, 44672, 550, 48)},
}
var want Tokens
for _, ts := range own {
for _, v := range ts {
want.add(v)
}
}
got := map[string]int{}
var total Tokens
var onFork Tokens
for _, c := range Calls(time.Time{}) {
got[c.File]++
total.add(c.Tokens)
if strings.Contains(c.File, fork) {
onFork.add(c.Tokens)
}
}
if total != want {
t.Fatalf("calls booked %+v, want %+v (calls per file %v)", total, want, got)
}
if onFork != own[fork][0] {
t.Fatalf("the fork's calls are %+v, want its own one call %+v", onFork, own[fork][0])
}
var listed Tokens
for _, s := range List(0) {
listed.add(s.Tokens)
}
if listed != want {
t.Fatalf("sessions booked %+v, want %+v", listed, want)
}
}
// With the parent's rollout gone, its calls are counted once, in the fork:
// they were made, and nothing else counts them now.
func TestCodexForkWithoutItsParentCountsTheCopies(t *testing.T) {
d := setupCalls(t)
b, err := os.ReadFile("testdata/codex-fork-child.jsonl")
if err != nil {
t.Fatal(err)
}
writeLines(t, filepath.Join(d.codex, "sessions", "2026", "03", "24", "rollout-2026-03-24T17-30-00-019d1f2d-f9c0-7111-8222-333444555666.jsonl"), strings.TrimSpace(string(b)))
if n := len(Calls(time.Time{})); n != 4 {
t.Fatalf("%d calls, want the 3 copied and the fork's own", n)
}
}
func TestUUIDV7Time(t *testing.T) {
at, ok := uuidV7Time("019d186b-a100-7aaa-8bbb-cccdddeeefff")
if !ok || !at.Equal(time.Date(2026, 3, 23, 2, 0, 0, 0, time.UTC)) {
t.Fatalf("%v %v", at, ok)
}
if _, ok := uuidV7Time("4f2c1a7e-0000-4000-8000-000000000000"); ok {
t.Fatal("a v4 id has no time")
}
}