The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
Refine Codex usage deduplication
shukieshahe78bad0e92af
diff --git a/cli/beacon/cmd/token_usage_test.go b/cli/beacon/cmd/token_usage_test.goindex 2115c3ab..e6d23883 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 (OTLP suppressed)") {+ if !strings.Contains(output, "Codex source: session files with OTLP fallback (overlaps 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.goindex 2c11d57c..08b98533 100644--- a/cli/beacon/internal/tokens/tokens.go+++ b/cli/beacon/internal/tokens/tokens.go@@ -5,6 +5,7 @@package tokensimport (+ "fmt""sort""strings""time"@@ -344,10 +345,24 @@ func selectCodexUsageSource(events []*usageEvent, source string) ([]*usageEvent,if summary.CodexSessionEvents == 0 && summary.CodexOTLPEvents == 0 {return events, nil}- if source == CodexSourceAuto && hasSession {- source = CodexSourceRuntime- }if source == CodexSourceAuto {+ if hasSession {+ out := events[:0]+ sessionKeys := codexSessionOverlapKeys(events)+ for _, event := range events {+ if isCodexOTLPUsage(event) && sessionKeys[codexOverlapKey(event)] {+ summary.CodexOTLPSuppressed+++ continue+ }+ out = append(out, event)+ }+ if summary.CodexOTLPSuppressed > 0 {+ summary.Codex = "session files with OTLP fallback (overlaps suppressed)"+ } else {+ summary.Codex = "session files with OTLP fallback"+ }+ return out, summary+ }summary.Codex = "otlp metrics"return events, summary}@@ -383,6 +398,28 @@ func selectCodexUsageSource(events []*usageEvent, source string) ([]*usageEvent,return out, summary}+func codexSessionOverlapKeys(events []*usageEvent) map[string]bool {+ keys := map[string]bool{}+ for _, event := range events {+ if isCodexSessionUsage(event) {+ keys[codexOverlapKey(event)] = true+ }+ }+ return keys+}++func codexOverlapKey(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 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.goindex dc8fa00e..7f6fc7cc 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 (OTLP suppressed)" || report.Source.CodexOTLPSuppressed != 1 {+ if report.Source == nil || report.Source.Codex != "session files with OTLP fallback (overlaps suppressed)" || report.Source.CodexOTLPSuppressed != 1 {t.Fatalf("source summary = %#v", report.Source)}if len(report.BySession) != 1 || report.BySession[0].Key != "codex:s1" {@@ -168,6 +168,27 @@ func TestAggregatePrefersCodexSessionUsageOverOTLP(t *testing.T) {}}+func TestAggregateAutoKeepsNonOverlappingCodexOTLPUsage(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.Raw = map[string]interface{}{"source": "codex_session_jsonl", "dedup_key": "session-turn-1"}+ })+ otlpOnlyEvent := usageEventFixture("2026-06-11T10:01:00Z", "codex_cli", "", "gpt-5", func(e *schema.Event) {+ e.GenAI.Usage.InputTokens = int64Ptr(9)+ e.GenAI.Usage.OutputTokens = int64Ptr(3)+ e.Raw = map[string]interface{}{"metric_name": "codex.turn.token_usage", "metric_temporality": "Delta"}+ })++ 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 TestAggregateDedupesCodexSessionUsageByRawDedupKey(t *testing.T) {event := usageEventFixture("2026-06-11T10:00:00Z", "codex_cli", "codex:s1", "gpt-5", func(e *schema.Event) {e.GenAI.Usage.InputTokens = int64Ptr(40)diff --git a/pkg/asymptoteobserve/codexusage/codexusage.go b/pkg/asymptoteobserve/codexusage/codexusage.goindex b0531c40..bd0de58d 100644--- a/pkg/asymptoteobserve/codexusage/codexusage.go+++ b/pkg/asymptoteobserve/codexusage/codexusage.go@@ -317,13 +317,15 @@ func ReconcileAndWrite(opts ReconcileOptions, write WriteFunc) (ReconcileResult,if event.DedupKey == "" || seen[event.DedupKey] {continue}- if err := write(event); err != nil {- return result, err- }seen[event.DedupKey] = trueif err := saveStateFile(statePath, seen); err != nil {return result, err}+ if err := write(event); err != nil {+ delete(seen, event.DedupKey)+ _ = saveStateFile(statePath, seen)+ return result, err+ }result.Events = append(result.Events, event)}}