From 84f14e555cfb2054a7aca48e412db29783145c0e Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 15 Oct 2026 03:36:44 +0800 Subject: [PATCH] usage: a Codex call with no response_id leaves `compaction` out of Usage requests instead of saying false (#1591) Codex added token_usage_record and compaction_response_id together, in openai/codex#41912 (2026-08-31). A call that magpie reads from a token_count event alone, as in older rollouts, has no response_id. It can't carry the compaction marker either way. The previous commit reported such calls as `compaction: false`, which claims they were not compactions. codexCompactionOf now returns no state for them, so the ledger and the requests page leave the field out, as they do for any call with no native evidence. What was run: - New test TestCodexCallWithoutResponseIDHasNoCompactionState. On the previous commit it fails with `ledger: a call with no response_id claimed a compaction state: {..."compaction":false...}`, and with the same message for the requests page. It passes now. - go test -tags nogui ./internal/sessions ./internal/usage: ok. For all three commits on origin/main 1ad60446f: - gofmt -l on the changed files: clean. - go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: ok. - go build for darwin, linux (nogui) and windows: ok. - Full suite: go test -tags nogui -timeout 40m ./... (pipefail, sandbox HOME): 53 packages ok, exit 0. - No GUI asset changed, so no Playwright run. - Not tried: a real Codex subagent rollout (legacy or paginated). The formats are from Codex's source, and Hogeexxl checked two real paginated subagents. - Rebased onto origin/main f255a3e3b (12 commits newer, including #2233 and #2234 in internal/sessions; no cache-version clash). Rerun there: go test -tags nogui -count=1 ./internal/sessions ./internal/usage ./internal/gui, gofmt and vet on both packages: ok. --- docs/subsystems/gateway-sessions.md | 6 ++++- internal/usage/codex_compaction_test.go | 36 +++++++++++++++++++++++++ internal/usage/codex_identity.go | 7 ++++- internal/usage/ledger.go | 3 +-- 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/docs/subsystems/gateway-sessions.md b/docs/subsystems/gateway-sessions.md index 597445bf3..588c7dbf8 100644 --- a/docs/subsystems/gateway-sessions.md +++ b/docs/subsystems/gateway-sessions.md @@ -423,7 +423,11 @@ session metadata. `GET /api/usage/requests` and `GET /v1/magpie/usage/requests` expose Codex `compaction` as `true` or `false` when a native Codex call establishes the -marker state, and omit it when there is no matching native evidence. A unique +marker state, and omit it when there is no matching native evidence. Only a +call read from a `token_usage_record` (it has a `response_id`) establishes it: +Codex writes that record and a compaction's `compaction_response_id` together, +so a call told from `token_count` alone, in an older rollout, leaves it out +rather than saying `false`. A unique `response_id` match adds this metadata to the gateway row while preserving that row's model, usage, cost and kind. Compaction is already included in Codex's total consumption, so this marker diff --git a/internal/usage/codex_compaction_test.go b/internal/usage/codex_compaction_test.go index 47f4a94ee..24cd2ca63 100644 --- a/internal/usage/codex_compaction_test.go +++ b/internal/usage/codex_compaction_test.go @@ -303,3 +303,39 @@ func appendCompactionLine(t *testing.T, path, responseID, threadID, sessionID, t t.Fatal(err) } } + +// A Codex call told from token_count alone (a rollout from before Codex wrote +// token_usage_record) has no response_id and can't carry the native marker: +// its compaction is unknown, never false. +func TestCodexCallWithoutResponseIDHasNoCompactionState(t *testing.T) { + pageHome(t) + now := holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.UTC)) + old := LogCalls + LogCalls = func(time.Time) []sessions.Call { + return []sessions.Call{ + codexCompactionCall(now.Add(-time.Hour), "", 31, 6, false), + codexCompactionCall(now.Add(-time.Hour+time.Second), "resp-recorded", 37, 7, false), + } + } + defer func() { LogCalls = old }() + for name, rows := range map[string][]Row{"ledger": LedgerOf(All, Filter{}).Rows, "requests": QueryPage(All, Filter{}, 0, 50).Rows} { + var legacy, recorded *Row + for i := range rows { + switch rows[i].Input { + case 31: + legacy = &rows[i] + case 37: + recorded = &rows[i] + } + } + if legacy == nil || recorded == nil { + t.Fatalf("%s: native rows missing: %+v", name, rows) + } + if b, _ := json.Marshal(legacy); strings.Contains(string(b), `"compaction"`) { + t.Errorf("%s: a call with no response_id claimed a compaction state: %s", name, b) + } + if compacted, known := compactionValue(*recorded); !known || compacted { + t.Errorf("%s: a recorded call lost its false marker: %+v", name, recorded.Compaction) + } + } +} diff --git a/internal/usage/codex_identity.go b/internal/usage/codex_identity.go index 744335aea..31abeeb08 100644 --- a/internal/usage/codex_identity.go +++ b/internal/usage/codex_identity.go @@ -73,8 +73,13 @@ func sameCodexCall(a, b Row) bool { a.Error == b.Error && a.ErrType == b.ErrType && a.RequestID == b.RequestID } +// codexCompactionOf is what a native Codex call says about compaction: only +// a call read from a token_usage_record (it has a response_id) can say, as +// Codex writes that record and a compaction's compaction_response_id together +// (openai/codex#41912). An older rollout's call, told from token_count alone, +// never carries the marker, so it is unknown, not "no compaction". func codexCompactionOf(call sessions.Call) *bool { - if call.Agent != "codex" { + if call.Agent != "codex" || call.ResponseID == "" { return nil } compacted := call.Compaction diff --git a/internal/usage/ledger.go b/internal/usage/ledger.go index d16089e1a..14498ff8a 100644 --- a/internal/usage/ledger.go +++ b/internal/usage/ledger.go @@ -344,8 +344,7 @@ func logRecord(c sessions.Call) Record { Millis: c.Millis, TTFT: c.TTFT, Session: c.Session, RequestID: c.RequestID, Error: c.ErrorText, ErrType: c.Error, Subagent: sessions.SubagentOf(c.File), ParentAgent: sessions.SubagentParent(c.File)} if c.Agent == "codex" { r.ResponseID = c.ResponseID - compacted := c.Compaction - r.Compaction = &compacted + r.Compaction = codexCompactionOf(c) } if c.Agent == "codex" || c.Agent == "opencode" { r.Requested, r.Served = c.Model, ""