Skip to content

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

Address Bugbot findings on token reports

Claude797cbf4803c4
diff --git a/cli/beacon/cmd/endpoint_tokens.go b/cli/beacon/cmd/endpoint_tokens.go
index 53f57121..7017f334 100644
--- a/cli/beacon/cmd/endpoint_tokens.go
+++ b/cli/beacon/cmd/endpoint_tokens.go
@@ -67,13 +67,20 @@ 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.
+ // Feed Aggregate in chronological (append) order so cumulative metric
+ // series resolve correctly when a batch of datapoints shares the same
+ // second-resolution timestamp.
+ dashboard.SortRecordsAppendOrder(result.Events)
+ session := strings.TrimSpace(endpointTokensOpts.session)
+ runID := strings.TrimSpace(endpointTokensOpts.runID)
events := make([]schema.Event, 0, len(result.Events))
- for i := len(result.Events) - 1; i >= 0; i-- {
- record := result.Events[i]
- if runID := strings.TrimSpace(endpointTokensOpts.runID); runID != "" {
+ for _, record := range result.Events {
+ // Exact session match keeps totals/grouping consistent with the
+ // per-step drilldown, which matches the session id exactly.
+ if session != "" && (record.Event.Session == nil || record.Event.Session.ID != session) {
+ continue
+ }
+ if runID != "" {
if record.Event.Run == nil || record.Event.Run.RunID != runID {
continue
}
diff --git a/cli/beacon/internal/endpoint/dashboard/events.go b/cli/beacon/internal/endpoint/dashboard/events.go
index 51c8a3a4..93501352 100644
--- a/cli/beacon/internal/endpoint/dashboard/events.go
+++ b/cli/beacon/internal/endpoint/dashboard/events.go
@@ -83,6 +83,27 @@ func ReadEvents(path string, query EventQuery) (EventResult, error) {
return result, nil
}
+// SortRecordsAppendOrder orders records oldest-first in log append order: by
+// timestamp, then older archives before the live log, then by numeric line.
+// Token aggregation needs this because runtime timestamps are second-resolution
+// (a batch of cumulative datapoints shares a timestamp) and the cumulative
+// dedup relies on append order; the newest-first ReadEvents sort breaks ties on
+// lexicographic line IDs, which mis-orders line-9 vs line-10 within a second.
+func SortRecordsAppendOrder(records []EventRecord) {
+ sort.SliceStable(records, func(i, j int) bool {
+ if !records[i].Parsed.Equal(records[j].Parsed) {
+ return records[i].Parsed.Before(records[j].Parsed)
+ }
+ ai, li, _ := parseRecordID(records[i].ID)
+ aj, lj, _ := parseRecordID(records[j].ID)
+ if ai != aj {
+ // Higher archive index is an older rotation, so it comes first.
+ return ai > aj
+ }
+ return li < lj
+ })
+}
+
func readEventsFromSource(source eventSource, query EventQuery, result *EventResult, limit int) error {
file, err := os.Open(source.path)
if err != nil {
diff --git a/cli/beacon/internal/endpoint/dashboard/server.go b/cli/beacon/internal/endpoint/dashboard/server.go
index 9463624b..1acd0f30 100644
--- a/cli/beacon/internal/endpoint/dashboard/server.go
+++ b/cli/beacon/internal/endpoint/dashboard/server.go
@@ -103,12 +103,19 @@ 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.
+ // Feed Aggregate in chronological (append) order so cumulative metric
+ // series resolve correctly when a batch of datapoints shares the same
+ // second-resolution timestamp.
+ SortRecordsAppendOrder(result.Events)
+ session := strings.TrimSpace(query.Session)
events := make([]schema.Event, 0, len(result.Events))
- for i := len(result.Events) - 1; i >= 0; i-- {
- events = append(events, result.Events[i].Event)
+ for _, record := range result.Events {
+ // Exact session match keeps totals/grouping consistent with the
+ // per-step drilldown, which matches the session id exactly.
+ if session != "" && (record.Event.Session == nil || record.Event.Session.ID != session) {
+ continue
+ }
+ events = append(events, record.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.go
index e028adbd..06dfd1a1 100644
--- a/cli/beacon/internal/endpoint/dashboard/server_test.go
+++ b/cli/beacon/internal/endpoint/dashboard/server_test.go
@@ -145,6 +145,73 @@ func TestTokensEndpointDedupesCumulativeSameSecondDatapoints(t *testing.T) {
func itoa(v int64) string { return strconv.FormatInt(v, 10) }
+// TestTokensEndpointDedupesManySameSecondDatapoints uses more than nine
+// cumulative datapoints in the same second so line numbers reach two digits.
+// ReadEvents breaks same-timestamp ties on lexicographic line IDs (line-9 >
+// line-10), so the token path must reorder by numeric append order or the
+// cumulative dedup scrambles and inflates totals.
+func TestTokensEndpointDedupesManySameSecondDatapoints(t *testing.T) {
+ logPath := filepath.Join(t.TempDir(), "runtime.jsonl")
+ var lines []string
+ for k := int64(1); k <= 12; k++ {
+ // Cumulative input k*100 -> per-interval delta of 100 each -> 1200 total.
+ lines = append(lines, `{"timestamp":"2026-06-11T10:00:00Z","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(k*100)+`}},"message":"claude_code.token.usage","raw":{"metric_name":"claude_code.token.usage","metric_temporality":"Cumulative"}}`)
+ }
+ 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)
+ }
+ if report.Totals.InputTokens != 1200 {
+ t.Fatalf("cumulative input tokens = %d, want 1200", report.Totals.InputTokens)
+ }
+}
+
+// TestTokensEndpointSessionFilterIsExact guards against substring session
+// matching: filtering session-1 must not fold in session-10's usage.
+func TestTokensEndpointSessionFilterIsExact(t *testing.T) {
+ logPath := filepath.Join(t.TempDir(), "runtime.jsonl")
+ mk := func(session string, input int64) string {
+ return `{"timestamp":"2026-06-11T10:00:00Z","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":"` + session + `"},"model":"claude-sonnet-4-5","gen_ai":{"usage":{"input_tokens":` + itoa(input) + `}},"message":"claude_code.token.usage","raw":{"metric_name":"claude_code.token.usage","metric_temporality":"Delta"}}`
+ }
+ lines := []string{mk("session-1", 100), mk("session-10", 999)}
+ 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?session=session-1", 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)
+ }
+ if report.Totals.InputTokens != 100 {
+ t.Fatalf("session-1 totals = %d, want 100 (session-10 must not fold in)", report.Totals.InputTokens)
+ }
+ if report.SessionDetail == nil || report.SessionDetail.Usage.InputTokens != 100 {
+ t.Fatalf("session detail = %#v, want exact session-1 usage", report.SessionDetail)
+ }
+}
+
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.go
index 608ec72f..2a67c161 100644
--- a/cli/beacon/internal/endpoint/tokens/tokens.go
+++ b/cli/beacon/internal/endpoint/tokens/tokens.go
@@ -209,8 +209,15 @@ func collectUsageEvents(events []schema.Event) []*usageEvent {
if event.Session != nil {
ue.session = event.Session.ID
}
- if event.Run != nil && (event.Run.Provider != "" || event.Run.RunID != "") {
- ue.run = strings.TrimSuffix(event.Run.Provider+"/"+event.Run.RunID, "/")
+ if event.Run != nil {
+ switch {
+ case event.Run.Provider != "" && event.Run.RunID != "":
+ ue.run = event.Run.Provider + "/" + event.Run.RunID
+ case event.Run.Provider != "":
+ ue.run = event.Run.Provider
+ default:
+ ue.run = event.Run.RunID
+ }
}
if event.Trace != nil {
ue.traceID = event.Trace.ID
diff --git a/cli/beacon/internal/endpoint/tokens/tokens_test.go b/cli/beacon/internal/endpoint/tokens/tokens_test.go
index 20b3c760..1db2c973 100644
--- a/cli/beacon/internal/endpoint/tokens/tokens_test.go
+++ b/cli/beacon/internal/endpoint/tokens/tokens_test.go
@@ -117,6 +117,19 @@ func TestAggregateAttributesCIRuns(t *testing.T) {
}
}
+func TestAggregateRunKeyWithoutProvider(t *testing.T) {
+ events := []schema.Event{
+ usageEventFixture("2026-06-11T10:00:00Z", "claude_code", "s1", "claude-sonnet-4-5", func(e *schema.Event) {
+ e.GenAI.Usage.InputTokens = int64Ptr(7)
+ e.Run = &schema.RunInfo{RunID: "run-abc"} // provider empty
+ }),
+ }
+ report := Aggregate(events, Options{})
+ if len(report.ByRun) != 1 || report.ByRun[0].Key != "run-abc" {
+ t.Fatalf("by_run = %#v, want bare run id without leading slash", report.ByRun)
+ }
+}
+
func TestAggregateBuildsSessionStepTree(t *testing.T) {
spanEvent := func(ts, span, parent string, input int64) schema.Event {
return usageEventFixture(ts, "asymptote_observe", "s1", "gpt-4o-mini", func(e *schema.Event) {