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.
This commit is contained in:
yetone
2026-10-15 04:21:31 +08:00
parent a78ac27532
commit 84f14e555c
4 changed files with 48 additions and 4 deletions
+5 -1
View File
@@ -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
+36
View File
@@ -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)
}
}
}
+6 -1
View File
@@ -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
+1 -2
View File
@@ -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, ""