From ec15f87393b40eaa61ab93ff16d35b74d3539bcd Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 15 Oct 2026 02:34:05 +0800 Subject: [PATCH] sessions: a Codex Desktop fork's replayed parent checkpoints book no usage, so a thread_spawn session isn't counted ~1000x (#2234, zlxdengtianhei) A token_count event that can't be paired with a response record fell back to the cumulative total's delta whenever a previous total existed, even when the event's own last_token_usage was there. A Codex Desktop subagent fork (thread_spawn) writes two cumulative counters into one rollout: its parent's, replayed with last=0, and its own. Each switch from the fork's lower counter back up to the parent's booked the gap, tens of millions of tokens, as one call. The reporter's day read 187.4B tokens where ~10.3B was real. last_token_usage is the request's own usage, exact even when zero, so it is used whenever it is valid; only a missing or invalid last falls back to the delta, and with no previous total to the total itself, as before. Run: - TestCodexUsageForkReplayCountsOnlyLast (new), on the reporter's own five lines (testdata/codex-fork-replay-checkpoints.jsonl, numbers untouched), checking both Calls and the session summary. Old code: FAIL "calls booked {Input:832145 Output:281374 CacheRead:82834688 ...}, want {Input:3273 Output:1201 CacheRead:72448 ...}". New: PASS, on macOS and on the Windows box with every TestCodex* test. - TestCodexUsageUnpairedCountKeepsLegacyDelta and the other Codex usage tests still pass; go test -tags nogui ./... on macOS with this series: every package ok. - gofmt -l, go vet ./... on darwin, linux (nogui) and windows: clean. Not run: a reindex of the reporter's full dataset; the numbers above are from their excerpt. --- internal/sessions/codex_usage.go | 14 +++++++---- internal/sessions/codex_usage_test.go | 24 +++++++++++++++++++ .../codex-fork-replay-checkpoints.jsonl | 5 ++++ 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 internal/sessions/testdata/codex-fork-replay-checkpoints.jsonl diff --git a/internal/sessions/codex_usage.go b/internal/sessions/codex_usage.go index fb75a881c..5d52d6b1d 100644 --- a/internal/sessions/codex_usage.go +++ b/internal/sessions/codex_usage.go @@ -622,11 +622,17 @@ func (s *codexUsageState) count(at time.Time, model string, total, last *cxUsage return nil } s.advance(at, total) - // Preserve the legacy delta when there is no paired RECORD. A missing - // last value or an unexplained gap must not silently discard token usage. - if prev != nil { + // Without a paired RECORD, last is still the request's own usage, exact + // even when zero. Only a missing last falls back to the counter's delta, + // so no token usage is silently lost. The counter isn't always this + // request's: a Codex Desktop fork (thread_spawn) replays its parent's + // checkpoints, each with last=0, between its own, and its delta would + // book the parent's whole lead again on each switch back (#2234). + if last != nil && last.valid() { + v.Usage = *last + } else if prev != nil { v.Usage = total.less(*prev) - } else if last == nil || !last.valid() { + } else { v.Usage = *total } if v.Usage.zero() || !v.Usage.valid() { diff --git a/internal/sessions/codex_usage_test.go b/internal/sessions/codex_usage_test.go index 18ce24190..07eb3e69e 100644 --- a/internal/sessions/codex_usage_test.go +++ b/internal/sessions/codex_usage_test.go @@ -523,3 +523,27 @@ func TestCodexUsageRepeatedSnapshotAdvancesRevisionAcrossReload(t *testing.T) { t.Fatal("newer correction was lost", cs) } } + +// A Codex Desktop fork (thread_spawn, 0.146) replays its parent's checkpoints +// beside its own: two cumulative counters interleave, and each replayed one +// says last=0. Going back up to the parent's counter is not usage. These are +// the reporter's lines, numbers untouched (#2234): one request, 72,448 read +// from the cache, not the 82,834,688 the counter's jump books. +func TestCodexUsageForkReplayCountsOnlyLast(t *testing.T) { + b, err := os.ReadFile("testdata/codex-fork-replay-checkpoints.jsonl") + if err != nil { + t.Fatal(err) + } + codexUsageLog(t, strings.Split(strings.TrimSpace(string(b)), "\n")) + want := Tokens{Input: 75721 - 72448, Output: 1201, CacheRead: 72448} + var total Tokens + for _, c := range Calls(time.Time{}) { + total.add(c.Tokens) + } + if total != want { + t.Fatalf("calls booked %+v, want %+v", total, want) + } + if ss := List(0); len(ss) != 1 || ss[0].Tokens != want { + t.Fatalf("summary booked %+v, want %+v", ss, want) + } +} diff --git a/internal/sessions/testdata/codex-fork-replay-checkpoints.jsonl b/internal/sessions/testdata/codex-fork-replay-checkpoints.jsonl new file mode 100644 index 000000000..5e8cd77ff --- /dev/null +++ b/internal/sessions/testdata/codex-fork-replay-checkpoints.jsonl @@ -0,0 +1,5 @@ +{"timestamp":"2026-07-28T21:54:45.895Z","ordinal":0,"type":"session_meta","payload":{"session_id":"019fa314-0000-7000-8000-000000000000","id":"019faab9-0000-7000-8000-000000000001","forked_from_id":"019fa314-0000-7000-8000-000000000000","parent_thread_id":"019fa314-0000-7000-8000-000000000000","timestamp":"2026-07-28T21:54:40.644Z","cwd":"/tmp/repro","originator":"Codex Desktop","cli_version":"0.146.0-alpha.3.1","source":{"subagent":{"thread_spawn":{"parent_thread_id":"019fa314-0000-7000-8000-000000000000","depth":1,"agent_path":"/root/repro","agent_nickname":"repro","agent_role":null}}},"thread_source":"subagent","agent_nickname":"repro","agent_path":"/root/repro","model_provider":"openai","base_instructions":{"text":"x"},"history_mode":"paginated","subagent_history_start_ordinal":573,"multi_agent_version":"v2","git":{"commit_hash":"0000000000000000000000000000000000000000","branch":"main","repository_url":"https://github.com/example/repro.git"}}} +{"timestamp":"2026-07-28T21:54:45.895Z","ordinal":2,"type":"event_msg","payload":{"type":"token_count","info":{"total_token_usage":{"input_tokens":855011161,"cached_input_tokens":838592256,"cache_write_input_tokens":0,"output_tokens":2876802,"reasoning_output_tokens":553775,"total_tokens":857887963},"last_token_usage":{"input_tokens":0,"cached_input_tokens":0,"cache_write_input_tokens":0,"output_tokens":0,"reasoning_output_tokens":0,"total_tokens":18010},"model_context_window":258400}}} +{"timestamp":"2026-07-28T21:54:45.895Z","ordinal":7,"type":"event_msg","payload":{"type":"token_count","info":{"total_token_usage":{"input_tokens":771420049,"cached_input_tokens":755830016,"cache_write_input_tokens":0,"output_tokens":2596629,"reasoning_output_tokens":518322,"total_tokens":774016678},"last_token_usage":{"input_tokens":75721,"cached_input_tokens":72448,"cache_write_input_tokens":0,"output_tokens":1201,"reasoning_output_tokens":394,"total_tokens":76922},"model_context_window":258400}}} +{"timestamp":"2026-07-28T21:54:45.895Z","ordinal":2,"type":"event_msg","payload":{"type":"token_count","info":{"total_token_usage":{"input_tokens":855011161,"cached_input_tokens":838592256,"cache_write_input_tokens":0,"output_tokens":2876802,"reasoning_output_tokens":553775,"total_tokens":857887963},"last_token_usage":{"input_tokens":0,"cached_input_tokens":0,"cache_write_input_tokens":0,"output_tokens":0,"reasoning_output_tokens":0,"total_tokens":18010},"model_context_window":258400}}} +{"timestamp":"2026-07-28T21:54:45.895Z","ordinal":8,"type":"event_msg","payload":{"type":"item_completed","thread_id":"019faab9-0000-7000-8000-000000000001","turn_id":"rollout-2","item":{"type":"AgentMessage","id":"item-9","content":[{"type":"Text","text":"x"}],"phase":"commentary"},"completed_at_ms":1785275685895}}