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
Fix cumulative token dedup for same-second metric batches
Clauded4e1c0bf560a
diff --git a/cli/beacon/cmd/endpoint_tokens.go b/cli/beacon/cmd/endpoint_tokens.goindex 71a92f55..53f57121 100644--- a/cli/beacon/cmd/endpoint_tokens.go+++ b/cli/beacon/cmd/endpoint_tokens.go@@ -67,8 +67,12 @@ func runEndpointTokens(cmd *cobra.Command, args []string) error {if err != nil {return err}+ // ReadEvents returns events newest-first; feed Aggregate oldest-first so+ // cumulative metric series resolve in chronological (append) order even+ // when a batch of datapoints shares the same second-resolution timestamp.events := make([]schema.Event, 0, len(result.Events))- for _, record := range result.Events {+ for i := len(result.Events) - 1; i >= 0; i-- {+ record := result.Events[i]if runID := strings.TrimSpace(endpointTokensOpts.runID); runID != "" {if record.Event.Run == nil || record.Event.Run.RunID != runID {continuediff --git a/cli/beacon/internal/endpoint/dashboard/server.go b/cli/beacon/internal/endpoint/dashboard/server.goindex 56fab34a..9463624b 100644--- a/cli/beacon/internal/endpoint/dashboard/server.go+++ b/cli/beacon/internal/endpoint/dashboard/server.go@@ -103,9 +103,12 @@ func Handler(opts Options) (http.Handler, error) {writeError(w, http.StatusInternalServerError, err)return}+ // ReadEvents returns events newest-first; feed Aggregate oldest-first so+ // cumulative metric series resolve in chronological (append) order even+ // when a batch of datapoints shares the same second-resolution timestamp.events := make([]schema.Event, 0, len(result.Events))- for _, record := range result.Events {- events = append(events, record.Event)+ for i := len(result.Events) - 1; i >= 0; i-- {+ events = append(events, result.Events[i].Event)}writeJSON(w, tokens.Aggregate(events, tokenOptions(r)))})diff --git a/cli/beacon/internal/endpoint/dashboard/server_test.go b/cli/beacon/internal/endpoint/dashboard/server_test.goindex e3b06134..e028adbd 100644--- a/cli/beacon/internal/endpoint/dashboard/server_test.go+++ b/cli/beacon/internal/endpoint/dashboard/server_test.go@@ -6,6 +6,7 @@ import ("net/http/httptest""os""path/filepath"+ "strconv""strings""testing"@@ -101,6 +102,49 @@ func TestTokensEndpointAggregatesUsage(t *testing.T) {}}+// TestTokensEndpointDedupesCumulativeSameSecondDatapoints reproduces a real+// Claude Code export: a batch of cumulative token.usage datapoints written to+// the log within the same second. ReadEvents returns events newest-first, so+// the token endpoint must still feed Aggregate in chronological order or the+// cumulative dedup misreads each step-down as a counter reset and sums the raw+// cumulative values instead of the per-interval deltas.+func TestTokensEndpointDedupesCumulativeSameSecondDatapoints(t *testing.T) {+ logPath := filepath.Join(t.TempDir(), "runtime.jsonl")+ mk := func(ts string, cumulative int64) string {+ return `{"timestamp":"` + ts + `","vendor":"beacon","product":"endpoint-agent","schema_version":"1.0","event":{"kind":"agent_runtime","action":"token.usage","category":"metric"},"severity":"info","endpoint":{"os":"linux"},"harness":{"name":"claude_code"},"session":{"id":"s1"},"model":"claude-sonnet-4-5","gen_ai":{"usage":{"input_tokens":` ++ itoa(cumulative) + `}},"message":"claude_code.token.usage","raw":{"metric_name":"claude_code.token.usage","metric_temporality":"Cumulative"}}`+ }+ // Append order is chronological; all three share the same second.+ lines := []string{+ mk("2026-06-11T10:00:00Z", 1200),+ mk("2026-06-11T10:00:00Z", 2700),+ mk("2026-06-11T10:00:00Z", 4500),+ }+ if err := os.WriteFile(logPath, []byte(strings.Join(lines, "\n")+"\n"), 0644); err != nil {+ t.Fatalf("write fixture log: %v", err)+ }+ handler, err := Handler(Options{UserMode: true, LogPath: logPath})+ if err != nil {+ t.Fatalf("Handler returned error: %v", err)+ }+ req := httptest.NewRequest(http.MethodGet, "/api/tokens", nil)+ rec := httptest.NewRecorder()+ handler.ServeHTTP(rec, req)+ if rec.Code != http.StatusOK {+ t.Fatalf("status code = %d, body = %s", rec.Code, rec.Body.String())+ }+ var report tokens.Report+ if err := json.Unmarshal(rec.Body.Bytes(), &report); err != nil {+ t.Fatalf("unmarshal tokens report: %v", err)+ }+ // Deltas 1200, 1500, 1800 -> 4500. Raw (broken) sum would be 8400.+ if report.Totals.InputTokens != 4500 {+ t.Fatalf("cumulative input tokens = %d, want 4500 (raw over-count is 8400)", report.Totals.InputTokens)+ }+}++func itoa(v int64) string { return strconv.FormatInt(v, 10) }+func TestStaticDashboardPagesServe(t *testing.T) {handler, err := Handler(Options{UserMode: true, LogPath: filepath.Join(t.TempDir(), "runtime.jsonl")})if err != nil {diff --git a/cli/beacon/internal/endpoint/tokens/tokens.go b/cli/beacon/internal/endpoint/tokens/tokens.goindex 6cfa527a..608ec72f 100644--- a/cli/beacon/internal/endpoint/tokens/tokens.go+++ b/cli/beacon/internal/endpoint/tokens/tokens.go@@ -138,6 +138,12 @@ type usageEvent struct {// harness, session, model, metric name, and usage field; counter resets fall// back to the raw value) so totals never double-count. Delta metrics and// span-level usage sum directly.+//+// Events should be supplied in chronological (log append) order. Runtime+// timestamps are second-resolution, so a batch of cumulative datapoints from+// one export commonly shares a timestamp; cumulative deduping then relies on+// slice order to recover the emission sequence. Passing events newest-first+// makes each cumulative step-down look like a counter reset and inflates totals.func Aggregate(events []schema.Event, opts Options) Report {if opts.NearLimitRatio <= 0 {opts.NearLimitRatio = defaultNearLimitRatio