Skip to content

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

Keep context-only events out of everything additive

Claude6eca27040560
diff --git a/cli/beacon/internal/tokens/tokens.go b/cli/beacon/internal/tokens/tokens.go
index ffdfb3a5..a7af0fd1 100644
--- a/cli/beacon/internal/tokens/tokens.go
+++ b/cli/beacon/internal/tokens/tokens.go
@@ -130,6 +130,10 @@ type usageEvent struct {
spanID string
parentSpanID string
usage Usage
+ // contextOnly marks an event that reached the collector only because it reported context
+ // occupancy. It feeds the utilization report and nothing else: it is not spend, so it must
+ // not appear in a total, a group, a time bucket, or the count of usage-bearing events.
+ contextOnly bool
contextUsed int64
contextLimit int64
cumulative bool
@@ -173,6 +177,12 @@ func aggregate(events []schema.Event, opts Options, sessionUsers sessionUserInde
byRun := map[string]*Usage{}
buckets := map[time.Time]*Usage{}
for _, ue := range usageEvents {
+ if ue.contextOnly {
+ // Context occupancy is not spend. Counting these would put zero-token rows under
+ // every grouping and report a runtime that spends nothing as usage-bearing -- which
+ // the coverage report reads as "this runtime is reporting its spend".
+ continue
+ }
report.Totals.add(ue.usage)
addGroup(byModel, ue.model, ue.usage)
addGroup(bySession, ue.session, ue.usage)
@@ -188,7 +198,11 @@ func aggregate(events []schema.Event, opts Options, sessionUsers sessionUserInde
buckets[start].add(ue.usage)
}
}
- report.EventsWithUsage = len(usageEvents)
+ for _, ue := range usageEvents {
+ if !ue.contextOnly {
+ report.EventsWithUsage++
+ }
+ }
report.ByModel = sortedGroups(byModel, opts.TopLimit)
report.BySession = sortedGroups(bySession, opts.TopLimit)
report.ByUser = sortedGroups(byUser, opts.TopLimit)
@@ -427,8 +441,13 @@ func collectUsageEvents(events []schema.Event, sessionUsers sessionUserIndex) []
ue.contextLimit = *context.LimitTokens
}
}
- if ue.usage.TotalTokens() == 0 && ue.usage.ReasoningOutputTokens == 0 && ue.usage.CostUSD == 0 && ue.contextUsed == 0 {
- continue
+ if ue.usage.TotalTokens() == 0 && ue.usage.ReasoningOutputTokens == 0 && ue.usage.CostUSD == 0 {
+ if ue.contextUsed == 0 {
+ continue
+ }
+ // Kept for utilization, excluded from everything additive. A runtime that reports
+ // both context and usage is not context-only and counts normally.
+ ue.contextOnly = true
}
if event.Raw != nil {
if temporality, _ := event.Raw["metric_temporality"].(string); strings.EqualFold(temporality, "cumulative") {
@@ -787,7 +806,7 @@ func buildSessionDetail(events []*usageEvent, sessionID string) *SessionDetail {
for _, ue := range events {
// Match case-insensitively to stay consistent with the case-insensitive
// session query the token callers use to select events.
- if !strings.EqualFold(ue.session, sessionID) {
+ if !strings.EqualFold(ue.session, sessionID) || ue.contextOnly {
continue
}
detail.Usage.add(ue.usage)
diff --git a/cli/beacon/internal/tokens/tokens_test.go b/cli/beacon/internal/tokens/tokens_test.go
index f451a5e8..4b3003e0 100644
--- a/cli/beacon/internal/tokens/tokens_test.go
+++ b/cli/beacon/internal/tokens/tokens_test.go
@@ -477,8 +477,14 @@ func TestUtilizationPrefersReportedContextOverInference(t *testing.T) {
}
}
-// Context is a level, not spend. A context-only event must not add anything to the totals a report
-// sums, or a Qwen session's cost would grow with the square of its length.
+// Context is a level, not spend. A context-only event must not add anything a report sums, or a
+// Qwen session's cost would grow with the square of its length.
+//
+// "Anything" is the whole shape, not just the token and cost fields. An earlier version of this
+// test checked only those two and passed while the event was still incrementing the event counts
+// and creating zero-token rows under every grouping -- which reads as a runtime that spent nothing
+// but was nonetheless active in a spend report, and which the coverage report in beacon
+// token-usage --coverage would have read as "this runtime is reporting its spend".
func TestContextOnlyEventsAddNothingToTotals(t *testing.T) {
events := []schema.Event{
usageEventFixture("2026-06-11T10:00:00Z", "qwen_code", "s1", "qwen3-coder-plus", func(e *schema.Event) {
@@ -489,12 +495,62 @@ func TestContextOnlyEventsAddNothingToTotals(t *testing.T) {
}
}),
}
- report := Aggregate(events, Options{})
+ report := Aggregate(events, Options{BucketSize: time.Hour, SessionID: "s1"})
if got := report.Totals.TotalTokens(); got != 0 {
- t.Fatalf("totals = %d tokens, want 0 -- context occupancy is not spend", got)
+ t.Errorf("totals = %d tokens, want 0 -- context occupancy is not spend", got)
}
if report.Totals.CostUSD != 0 {
- t.Fatalf("totals cost = %v, want 0", report.Totals.CostUSD)
+ t.Errorf("totals cost = %v, want 0", report.Totals.CostUSD)
+ }
+ if report.Totals.Events != 0 {
+ t.Errorf("totals events = %d, want 0 -- the event carries no usage to have counted", report.Totals.Events)
+ }
+ if report.EventsWithUsage != 0 {
+ t.Errorf("events_with_usage = %d, want 0", report.EventsWithUsage)
+ }
+ for name, group := range map[string][]Group{
+ "by_model": report.ByModel, "by_session": report.BySession,
+ "by_harness": report.ByHarness, "by_user": report.ByUser,
+ "by_repository": report.ByRepository, "by_run": report.ByRun,
+ } {
+ if len(group) != 0 {
+ t.Errorf("%s = %+v, want no rows -- a zero-token row reads as a runtime that spent nothing but was active", name, group)
+ }
+ }
+ if len(report.Series) != 0 {
+ t.Errorf("series = %+v, want no buckets", report.Series)
+ }
+ if report.SessionDetail != nil && len(report.SessionDetail.Steps) != 0 {
+ t.Errorf("session steps = %+v, want none", report.SessionDetail.Steps)
+ }
+ // The one thing it must still do.
+ if len(report.Utilization) != 1 {
+ t.Fatalf("utilization = %+v, want the context-only event to still be measured", report.Utilization)
+ }
+}
+
+// An event reporting both spend and context is not context-only: its usage counts normally and its
+// context still feeds utilization.
+func TestEventsWithBothUsageAndContextCountAsSpend(t *testing.T) {
+ events := []schema.Event{
+ usageEventFixture("2026-06-11T10:00:00Z", "some_runtime", "s1", "some-model", func(e *schema.Event) {
+ e.GenAI.Usage.InputTokens = int64Ptr(900)
+ e.GenAI.Usage.OutputTokens = int64Ptr(100)
+ e.GenAI.Context = &schema.GenAIContextInfo{
+ UsedTokens: int64Ptr(50000),
+ LimitTokens: int64Ptr(200000),
+ }
+ }),
+ }
+ report := Aggregate(events, Options{})
+ if got := report.Totals.TotalTokens(); got != 1000 {
+ t.Errorf("totals = %d, want the reported 1000 spend tokens", got)
+ }
+ if report.EventsWithUsage != 1 {
+ t.Errorf("events_with_usage = %d, want 1", report.EventsWithUsage)
+ }
+ if len(report.Utilization) != 1 || report.Utilization[0].MaxInputTokens != 50000 {
+ t.Errorf("utilization = %+v, want the reported 50000 occupancy, not the 900 input", report.Utilization)
}
}