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.
157 lines
6.0 KiB
Go
157 lines
6.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")
|
|
}
|
|
}
|
|
|
|
// A fork that went on working: its calls after the copies are its own,
|
|
// even one whose numbers are those of a call its parent made after the
|
|
// fork (both given the same next prompt over the same history). Only the
|
|
// copied lines at the head of the fork are the parent's (Sky,
|
|
// skyhuang1994: forks that went on working lost their own usage on
|
|
// ZCode; this is Codex's side of it).
|
|
func TestCodexForkThatWentOnWorkingKeepsItsOwnCalls(t *testing.T) {
|
|
d := setupCalls(t)
|
|
const parent, fork = "019d186b-a100-7aaa-8bbb-cccdddeeefff", "019d1f2d-f9c0-7111-8222-333444555666"
|
|
read := func(name string) []string {
|
|
b, err := os.ReadFile(filepath.Join("testdata", name))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return strings.Split(strings.TrimSpace(string(b)), "\n")
|
|
}
|
|
p, c := read("codex-fork-parent.jsonl"), read("codex-fork-child.jsonl")
|
|
// the fork's own first call: the parent's call after the fork, number
|
|
// for number, at the fork's time; then a second call of its own
|
|
same := strings.Replace(p[12], `"timestamp":"2026-03-24T10:00:20.000Z"`, `"timestamp":"2026-03-24T09:30:12.000Z"`, 1)
|
|
if same == p[12] || !strings.Contains(c[14], `"type":"token_count"`) {
|
|
t.Fatal("the fixtures changed")
|
|
}
|
|
c[14] = same
|
|
next := strings.NewReplacer(
|
|
`"timestamp":"2026-03-24T10:00:20.000Z"`, `"timestamp":"2026-03-24T09:40:00.000Z"`,
|
|
`{"input_tokens":178000,"cached_input_tokens":134912,"output_tokens":3000,"reasoning_output_tokens":600,"total_tokens":181000}`,
|
|
`{"input_tokens":228000,"cached_input_tokens":174912,"output_tokens":3400,"reasoning_output_tokens":650,"total_tokens":231400}`,
|
|
`{"input_tokens":46928,"cached_input_tokens":46080,"output_tokens":600,"reasoning_output_tokens":88,"total_tokens":47528}`,
|
|
`{"input_tokens":50000,"cached_input_tokens":40000,"output_tokens":400,"reasoning_output_tokens":50,"total_tokens":50400}`,
|
|
).Replace(p[12])
|
|
c = append(c, next)
|
|
writeLines(t, filepath.Join(d.codex, "sessions", "2026", "03", "23", "rollout-2026-03-23T10-00-00-"+parent+".jsonl"), strings.Join(p, "\n"))
|
|
writeLines(t, filepath.Join(d.codex, "sessions", "2026", "03", "24", "rollout-2026-03-24T17-30-00-"+fork+".jsonl"), strings.Join(c, "\n"))
|
|
|
|
call := func(in, cached, out int) Tokens { return Tokens{Input: in - cached, CacheRead: cached, Output: out} }
|
|
wantFork := call(46928, 46080, 600)
|
|
wantFork.add(call(50000, 40000, 400))
|
|
var onFork, all, want Tokens
|
|
n := 0
|
|
for _, x := range Calls(time.Time{}) {
|
|
all.add(x.Tokens)
|
|
if strings.Contains(x.File, fork) {
|
|
onFork.add(x.Tokens)
|
|
n++
|
|
}
|
|
}
|
|
if n != 2 || onFork != wantFork {
|
|
t.Fatalf("the fork's calls: %d %+v, want its 2 own %+v", n, onFork, wantFork)
|
|
}
|
|
for _, v := range []Tokens{call(41200, 3072, 812), call(43680, 41088, 688), call(46192, 44672, 900), call(46928, 46080, 600), wantFork} {
|
|
want.add(v)
|
|
}
|
|
if all != want {
|
|
t.Fatalf("calls booked %+v, want %+v", all, want)
|
|
}
|
|
var listed Tokens
|
|
for _, s := range List(0) {
|
|
listed.add(s.Tokens)
|
|
}
|
|
if listed != want {
|
|
t.Fatalf("sessions booked %+v, want %+v", listed, want)
|
|
}
|
|
}
|