Skip to content

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

Prefer Codex session usage in auto mode

shukieshah37369a7326e6
diff --git a/cli/beacon/cmd/token_usage_test.go b/cli/beacon/cmd/token_usage_test.go
index e6d23883..2115c3ab 100644
--- a/cli/beacon/cmd/token_usage_test.go
+++ b/cli/beacon/cmd/token_usage_test.go
@@ -127,7 +127,7 @@ func TestTokenUsageCodexSourceSummary(t *testing.T) {
t.Fatal(err)
}
output := runTokenUsageCommand(t, "--log-path", logPath)
- if !strings.Contains(output, "Codex source: session files with OTLP fallback (overlaps suppressed)") {
+ if !strings.Contains(output, "Codex source: session files (OTLP suppressed)") {
t.Fatalf("text report missing Codex source summary:\n%s", output)
}
if !strings.Contains(output, "codex:s1") {
diff --git a/cli/beacon/internal/tokens/tokens.go b/cli/beacon/internal/tokens/tokens.go
index 759ccb1c..d6d1560c 100644
--- a/cli/beacon/internal/tokens/tokens.go
+++ b/cli/beacon/internal/tokens/tokens.go
@@ -5,7 +5,6 @@
package tokens
import (
- "fmt"
"sort"
"strings"
"time"
@@ -348,18 +347,17 @@ func selectCodexUsageSource(events []*usageEvent, source string) ([]*usageEvent,
if source == CodexSourceAuto {
if hasSession {
out := events[:0]
- suppressedOTLPKeys := codexOverlappingOTLPGroups(events)
for _, event := range events {
- if isCodexOTLPUsage(event) && suppressedOTLPKeys[codexOTLPGroupKey(event)] {
+ if isCodexOTLPUsage(event) {
summary.CodexOTLPSuppressed++
continue
}
out = append(out, event)
}
if summary.CodexOTLPSuppressed > 0 {
- summary.Codex = "session files with OTLP fallback (overlaps suppressed)"
+ summary.Codex = "session files (OTLP suppressed)"
} else {
- summary.Codex = "session files with OTLP fallback"
+ summary.Codex = "session files"
}
return out, summary
}
@@ -398,66 +396,6 @@ func selectCodexUsageSource(events []*usageEvent, source string) ([]*usageEvent,
return out, summary
}
-func codexOverlappingOTLPGroups(events []*usageEvent) map[string]bool {
- type otlpGroup struct {
- model string
- usage Usage
- }
- sessionUsage := map[string]Usage{}
- otlpGroups := map[string]*otlpGroup{}
- for _, event := range events {
- if isCodexSessionUsage(event) {
- sessionUsage[codexFullUsageKey(event)] = event.usage
- continue
- }
- if isCodexOTLPUsage(event) {
- key := codexOTLPGroupKey(event)
- group := otlpGroups[key]
- if group == nil {
- group = &otlpGroup{model: event.model}
- otlpGroups[key] = group
- }
- group.usage.add(event.usage)
- }
- }
- out := map[string]bool{}
- for groupKey, group := range otlpGroups {
- probe := &usageEvent{ts: parseCodexGroupTime(groupKey), model: group.model, usage: group.usage}
- if _, ok := sessionUsage[codexFullUsageKey(probe)]; ok {
- out[groupKey] = true
- }
- }
- return out
-}
-
-func codexOTLPGroupKey(event *usageEvent) string {
- return strings.Join([]string{
- event.ts.UTC().Format(time.RFC3339),
- event.model,
- }, "\x00")
-}
-
-func codexFullUsageKey(event *usageEvent) string {
- return strings.Join([]string{
- event.ts.UTC().Format(time.RFC3339),
- event.model,
- fmt.Sprint(event.usage.InputTokens),
- fmt.Sprint(event.usage.OutputTokens),
- fmt.Sprint(event.usage.CacheReadInputTokens),
- fmt.Sprint(event.usage.CacheCreationInputTokens),
- fmt.Sprint(event.usage.ReasoningOutputTokens),
- }, "\x00")
-}
-
-func parseCodexGroupTime(groupKey string) time.Time {
- parts := strings.SplitN(groupKey, "\x00", 2)
- if len(parts) == 0 {
- return time.Time{}
- }
- ts, _ := time.Parse(time.RFC3339, parts[0])
- return ts
-}
-
func isCodexSessionUsage(event *usageEvent) bool {
return isCodexHarness(event.harness) && strings.EqualFold(event.rawSource, "codex_session_jsonl")
}
diff --git a/cli/beacon/internal/tokens/tokens_test.go b/cli/beacon/internal/tokens/tokens_test.go
index fd698431..4970d339 100644
--- a/cli/beacon/internal/tokens/tokens_test.go
+++ b/cli/beacon/internal/tokens/tokens_test.go
@@ -150,7 +150,7 @@ func TestAggregatePrefersCodexSessionUsageOverOTLP(t *testing.T) {
if report.Totals.InputTokens != 40 || report.Totals.OutputTokens != 10 || report.Totals.CacheReadInputTokens != 60 {
t.Fatalf("auto totals = %#v, want session event only", report.Totals)
}
- if report.Source == nil || report.Source.Codex != "session files with OTLP fallback (overlaps suppressed)" || report.Source.CodexOTLPSuppressed != 1 {
+ if report.Source == nil || report.Source.Codex != "session files (OTLP suppressed)" || report.Source.CodexOTLPSuppressed != 1 {
t.Fatalf("source summary = %#v", report.Source)
}
if len(report.BySession) != 1 || report.BySession[0].Key != "codex:s1" {
@@ -168,7 +168,7 @@ func TestAggregatePrefersCodexSessionUsageOverOTLP(t *testing.T) {
}
}
-func TestAggregateAutoKeepsNonOverlappingCodexOTLPUsage(t *testing.T) {
+func TestAggregateAutoSuppressesCodexOTLPWhenSessionUsageExists(t *testing.T) {
sessionEvent := usageEventFixture("2026-06-11T10:00:00Z", "codex_cli", "codex:s1", "gpt-5", func(e *schema.Event) {
e.GenAI.Usage.InputTokens = int64Ptr(40)
e.GenAI.Usage.OutputTokens = int64Ptr(10)
@@ -181,40 +181,11 @@ func TestAggregateAutoKeepsNonOverlappingCodexOTLPUsage(t *testing.T) {
})
report := Aggregate([]schema.Event{sessionEvent, otlpOnlyEvent}, Options{})
- if report.Totals.InputTokens != 49 || report.Totals.OutputTokens != 13 || report.Totals.Events != 2 {
- t.Fatalf("auto totals = %#v, want session plus non-overlapping OTLP", report.Totals)
- }
- if report.Source == nil || report.Source.Codex != "session files with OTLP fallback" || report.Source.CodexOTLPSuppressed != 0 {
- t.Fatalf("source summary = %#v", report.Source)
- }
-}
-
-func TestAggregateAutoSuppressesSplitCodexOTLPDatapoints(t *testing.T) {
- sessionEvent := usageEventFixture("2026-06-11T10:00:00Z", "codex_cli", "codex:s1", "gpt-5", func(e *schema.Event) {
- e.GenAI.Usage.InputTokens = int64Ptr(40)
- e.GenAI.Usage.OutputTokens = int64Ptr(10)
- e.GenAI.Usage.CacheRead = &schema.GenAIUsageCacheReadInfo{InputTokens: int64Ptr(60)}
- e.Raw = map[string]interface{}{"source": "codex_session_jsonl", "dedup_key": "session-turn-1"}
- })
- otlpInput := usageEventFixture("2026-06-11T10:00:00Z", "codex_cli", "", "gpt-5", func(e *schema.Event) {
- e.GenAI.Usage.InputTokens = int64Ptr(40)
- e.Raw = map[string]interface{}{"metric_name": "codex.turn.token_usage", "metric_temporality": "Delta"}
- })
- otlpOutput := usageEventFixture("2026-06-11T10:00:00Z", "codex_cli", "", "gpt-5", func(e *schema.Event) {
- e.GenAI.Usage.OutputTokens = int64Ptr(10)
- e.Raw = map[string]interface{}{"metric_name": "codex.turn.token_usage", "metric_temporality": "Delta"}
- })
- otlpCache := usageEventFixture("2026-06-11T10:00:00Z", "codex_cli", "", "gpt-5", func(e *schema.Event) {
- e.GenAI.Usage.CacheRead = &schema.GenAIUsageCacheReadInfo{InputTokens: int64Ptr(60)}
- e.Raw = map[string]interface{}{"metric_name": "codex.turn.token_usage", "metric_temporality": "Delta"}
- })
-
- report := Aggregate([]schema.Event{sessionEvent, otlpInput, otlpOutput, otlpCache}, Options{})
- if report.Totals.InputTokens != 40 || report.Totals.OutputTokens != 10 || report.Totals.CacheReadInputTokens != 60 || report.Totals.Events != 1 {
+ if report.Totals.InputTokens != 40 || report.Totals.OutputTokens != 10 || report.Totals.Events != 1 {
t.Fatalf("auto totals = %#v, want session event only", report.Totals)
}
- if report.Source == nil || report.Source.CodexOTLPSuppressed != 3 {
- t.Fatalf("source summary = %#v, want three suppressed OTLP datapoints", report.Source)
+ if report.Source == nil || report.Source.Codex != "session files (OTLP suppressed)" || report.Source.CodexOTLPSuppressed != 1 {
+ t.Fatalf("source summary = %#v", report.Source)
}
}
diff --git a/pkg/asymptoteobserve/codexusage/codexusage.go b/pkg/asymptoteobserve/codexusage/codexusage.go
index bd0de58d..b0531c40 100644
--- a/pkg/asymptoteobserve/codexusage/codexusage.go
+++ b/pkg/asymptoteobserve/codexusage/codexusage.go
@@ -317,13 +317,11 @@ func ReconcileAndWrite(opts ReconcileOptions, write WriteFunc) (ReconcileResult,
if event.DedupKey == "" || seen[event.DedupKey] {
continue
}
- seen[event.DedupKey] = true
- if err := saveStateFile(statePath, seen); err != nil {
+ if err := write(event); err != nil {
return result, err
}
- if err := write(event); err != nil {
- delete(seen, event.DedupKey)
- _ = saveStateFile(statePath, seen)
+ seen[event.DedupKey] = true
+ if err := saveStateFile(statePath, seen); err != nil {
return result, err
}
result.Events = append(result.Events, event)