Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

Fix token_count first-wins gate to use last-wins per turn

Cursor Agentbb335af99107
diff --git a/cli/beacon/internal/codexsession/codexsession_test.go b/cli/beacon/internal/codexsession/codexsession_test.go
index 3df9d708..cbd3d467 100644
--- a/cli/beacon/internal/codexsession/codexsession_test.go
+++ b/cli/beacon/internal/codexsession/codexsession_test.go
@@ -84,6 +84,35 @@ func TestMapCodexTranscriptProducesEndpointEvents(t *testing.T) {
}
}
+func TestTokenCountLastWinsForMultipleSnapshotsPerTurn(t *testing.T) {
+ ref := SessionRef{ID: "sess-1", Path: "/tmp/codex.jsonl", Workspace: "/tmp/repo"}
+ records := decodeFixture(t, []string{
+ sessionMetaLine("sess-1", "/tmp/repo"),
+ turnContextLine("turn-1", "gpt-6-astra"),
+ messageLine("user", "msg-user", "build it"),
+ tokenCountLine("turn-1", 0, 0, 0, 0),
+ tokenCountLine("turn-1", 50, 20, 50, 20),
+ tokenCountLine("turn-1", 80, 30, 130, 50),
+ })
+ mapped := MapSession(ref, records, MapOptions{})
+ var usageEvents []schema.Event
+ for _, item := range mapped {
+ if item.Event.Event.Action == "token.usage" {
+ usageEvents = append(usageEvents, item.Event)
+ }
+ }
+ if len(usageEvents) != 1 {
+ t.Fatalf("expected 1 token.usage event, got %d", len(usageEvents))
+ }
+ ev := usageEvents[0]
+ if ev.GenAI == nil || ev.GenAI.Usage == nil || ev.GenAI.Usage.OutputTokens == nil {
+ t.Fatal("missing usage on token.usage event")
+ }
+ if *ev.GenAI.Usage.OutputTokens != 30 {
+ t.Fatalf("output tokens = %d, want 30 (last snapshot)", *ev.GenAI.Usage.OutputTokens)
+ }
+}
+
func TestCollectOnceUsesCursorAndPrintDoesNotAdvance(t *testing.T) {
dir := t.TempDir()
codexDir := filepath.Join(dir, ".codex")
@@ -231,6 +260,10 @@ func quote(s string) string {
return string(data)
}
+func tokenCountLine(turnID string, lastInput, lastOutput, totalInput, totalOutput int64) string {
+ return `{"timestamp":"2026-09-19T22:00:04.000Z","type":"event_msg","payload":{"type":"token_count","turn_id":` + quote(turnID) + `,"info":{"last_token_usage":{"input_tokens":` + i64(lastInput) + `,"output_tokens":` + i64(lastOutput) + `},"total_token_usage":{"input_tokens":` + i64(totalInput) + `,"output_tokens":` + i64(totalOutput) + `}}}}`
+}
+
func i64(v int64) string {
return strconv.FormatInt(v, 10)
}
diff --git a/cli/beacon/internal/codexsession/mapper.go b/cli/beacon/internal/codexsession/mapper.go
index 3fd02465..6dbc8f86 100644
--- a/cli/beacon/internal/codexsession/mapper.go
+++ b/cli/beacon/internal/codexsession/mapper.go
@@ -42,8 +42,8 @@ type mapper struct {
turnID string
started bool
- tokenRecordTurns map[string]bool
- emittedTokenCountTurns map[string]bool
+ tokenRecordTurns map[string]bool
+ emittedTokenCountIdx map[string]int
}
func MapSession(ref SessionRef, records []Record, opts MapOptions) []MappedEvent {
@@ -59,8 +59,8 @@ func MapSession(ref SessionRef, records []Record, opts MapOptions) []MappedEvent
tools: map[string]codexToolCall{},
sessionID: ref.ID,
workspace: ref.Workspace,
- tokenRecordTurns: tokenRecordTurns,
- emittedTokenCountTurns: map[string]bool{},
+ tokenRecordTurns: tokenRecordTurns,
+ emittedTokenCountIdx: map[string]int{},
}
for i := range records {
m.consume(records[i])
@@ -199,16 +199,10 @@ func (m *mapper) consumeTokenCount(record Record, msg *EventMessage) {
if turnID != "" && m.tokenRecordTurns[turnID] {
return
}
- if turnID != "" && m.emittedTokenCountTurns[turnID] {
- return
- }
usage := usageFromCodex(firstUsage(msg.Info.LastTokenUsage, msg.Info.TotalTokenUsage))
if usage == nil {
return
}
- if turnID != "" {
- m.emittedTokenCountTurns[turnID] = true
- }
ev := m.base(record, "token.usage", "metric", schema.SeverityInfo, schema.FidelityObserved, "Codex token usage observed")
ev.GenAI = mergeGenAI(ev.GenAI, &schema.GenAIInfo{Usage: usage})
if ev.GenAI == nil {
@@ -223,7 +217,17 @@ func (m *mapper) consumeTokenCount(record Record, msg *EventMessage) {
"source": "codex_session_token_count",
},
}
- m.append(record, "usage.token_count."+firstNonEmpty(msg.TurnID, itoa(record.Line)), ev)
+ suffix := "usage.token_count." + firstNonEmpty(msg.TurnID, itoa(record.Line))
+ if turnID != "" {
+ if idx, ok := m.emittedTokenCountIdx[turnID]; ok {
+ coordinate := firstNonEmpty(m.sessionID, m.ref.ID, m.ref.Path) + ":" + itoa(record.Line) + ":" + suffix
+ ev.Event.ID = codexEventID(coordinate)
+ m.out[idx] = MappedEvent{DedupID: ev.Event.ID, SourceLine: record.Line, Event: ev}
+ return
+ }
+ m.emittedTokenCountIdx[turnID] = len(m.out)
+ }
+ m.append(record, suffix, ev)
}
func (m *mapper) consumeTokenUsageRecord(record Record, emit bool) {