Skip to content

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

Match token session filter case-insensitively

Claude7fcbbd897c8d
diff --git a/cli/beacon/cmd/endpoint_tokens.go b/cli/beacon/cmd/endpoint_tokens.go
index eb8ea161..3b68a34d 100644
--- a/cli/beacon/cmd/endpoint_tokens.go
+++ b/cli/beacon/cmd/endpoint_tokens.go
@@ -75,9 +75,10 @@ func runEndpointTokens(cmd *cobra.Command, args []string) error {
runID := strings.TrimSpace(endpointTokensOpts.runID)
events := make([]schema.Event, 0, len(result.Events))
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) {
+ // Match the session case-insensitively (consistent with the
+ // case-insensitive event query) so totals/grouping stay aligned with
+ // the per-step drilldown.
+ if session != "" && (record.Event.Session == nil || !strings.EqualFold(record.Event.Session.ID, session)) {
continue
}
if runID != "" {
diff --git a/cli/beacon/internal/endpoint/dashboard/server.go b/cli/beacon/internal/endpoint/dashboard/server.go
index 1acd0f30..7e17375a 100644
--- a/cli/beacon/internal/endpoint/dashboard/server.go
+++ b/cli/beacon/internal/endpoint/dashboard/server.go
@@ -110,9 +110,10 @@ func Handler(opts Options) (http.Handler, error) {
session := strings.TrimSpace(query.Session)
events := make([]schema.Event, 0, len(result.Events))
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) {
+ // Match the session case-insensitively (consistent with the
+ // case-insensitive event query) so totals/grouping stay aligned
+ // with the per-step drilldown.
+ if session != "" && (record.Event.Session == nil || !strings.EqualFold(record.Event.Session.ID, session)) {
continue
}
events = append(events, record.Event)
diff --git a/cli/beacon/internal/endpoint/dashboard/server_test.go b/cli/beacon/internal/endpoint/dashboard/server_test.go
index 06dfd1a1..7ba69f29 100644
--- a/cli/beacon/internal/endpoint/dashboard/server_test.go
+++ b/cli/beacon/internal/endpoint/dashboard/server_test.go
@@ -212,6 +212,38 @@ func TestTokensEndpointSessionFilterIsExact(t *testing.T) {
}
}
+// TestTokensEndpointSessionFilterIsCaseInsensitive guards the token session
+// filter against case mismatches: the event query matches sessions
+// case-insensitively, so the post-filter and drilldown must too, or a
+// differently-cased filter value returns zero usage for matching events.
+func TestTokensEndpointSessionFilterIsCaseInsensitive(t *testing.T) {
+ logPath := filepath.Join(t.TempDir(), "runtime.jsonl")
+ line := `{"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-ABC"},"model":"claude-sonnet-4-5","gen_ai":{"usage":{"input_tokens":123}},"message":"claude_code.token.usage","raw":{"metric_name":"claude_code.token.usage","metric_temporality":"Delta"}}`
+ if err := os.WriteFile(logPath, []byte(line+"\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-abc", 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 != 123 {
+ t.Fatalf("totals = %d, want 123 for case-insensitive session match", report.Totals.InputTokens)
+ }
+ if report.SessionDetail == nil || report.SessionDetail.Usage.InputTokens != 123 {
+ t.Fatalf("session detail = %#v, want case-insensitive match", 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 3ec54000..36bbd553 100644
--- a/cli/beacon/internal/endpoint/tokens/tokens.go
+++ b/cli/beacon/internal/endpoint/tokens/tokens.go
@@ -456,7 +456,9 @@ func buildSessionDetail(events []*usageEvent, sessionID string) *SessionDetail {
detail := &SessionDetail{SessionID: sessionID}
var ordered []*usageEvent
for _, ue := range events {
- if ue.session != sessionID {
+ // 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) {
continue
}
detail.Usage.add(ue.usage)