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, ""