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(dashboard): apply session-state filters to token totals (#967)
Divyam Talward547a3484b4a
diff --git a/cli/beacon/internal/endpoint/dashboard/events.go b/cli/beacon/internal/endpoint/dashboard/events.goindex 1a0bb34f..21621f6d 100644--- a/cli/beacon/internal/endpoint/dashboard/events.go+++ b/cli/beacon/internal/endpoint/dashboard/events.go@@ -70,14 +70,9 @@ type EventResult struct {func ReadEvents(path string, query EventQuery) (EventResult, error) {limit := normalizeLimit(query.Limit)- state := normalizeSessionState(query.SessionState)- query.SessionState = state- if state != "" && query.sessionStateIDs == nil {- ids, err := sessionIDsForState(path, query, state)- if err != nil {- return EventResult{}, err- }- query.sessionStateIDs = ids+ query, err := resolveSessionState(path, query)+ if err != nil {+ return EventResult{}, err}result := EventResult{Limit: limit, Query: strings.TrimSpace(query.Q), Filters: activeFilters(query)}for _, source := range eventSources(path) {@@ -93,6 +88,20 @@ func ReadEvents(path string, query EventQuery) (EventResult, error) {return result, nil}+// resolveSessionState normalizes query's session-state filter and, when one is set, resolves the+// sessions in that state, which every reader must do before it matches a single event.+func resolveSessionState(path string, query EventQuery) (EventQuery, error) {+ query.SessionState = normalizeSessionState(query.SessionState)+ if query.SessionState != "" && query.sessionStateIDs == nil {+ ids, err := sessionIDsForState(path, query, query.SessionState)+ if err != nil {+ return query, err+ }+ query.sessionStateIDs = ids+ }+ return query, nil+}+// ReadEventsAppendOrder reads the full matching log (NoLimit) and returns the// events in chronological (log append) order. Token aggregation needs append// order because runtime timestamps are second-resolution, so a batch of@@ -125,6 +134,10 @@ func ReadEventsAppendOrder(path string, query EventQuery) ([]schema.Event, error// live channel started capturing the session rather than where the window starts// or where its first usage record landed.func ReadTokenEventsAppendOrder(path string, query EventQuery) ([]schema.Event, []schema.Event, error) {+ query, err := resolveSessionState(path, query)+ if err != nil {+ return nil, nil, err+ }result, err := ReadEvents(path, EventQuery{NoLimit: true})if err != nil {return nil, nil, errdiff --git a/cli/beacon/internal/endpoint/dashboard/token_session_state_test.go b/cli/beacon/internal/endpoint/dashboard/token_session_state_test.gonew file mode 100644index 00000000..9ffa91a5--- /dev/null+++ b/cli/beacon/internal/endpoint/dashboard/token_session_state_test.go@@ -0,0 +1,183 @@+package dashboard++import (+ "encoding/json"+ "fmt"+ "net/http"+ "net/http/httptest"+ "path/filepath"+ "reflect"+ "testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/testenv"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/tokens"+)++func TestReadTokenEventsSessionStateMatchesEventReader(t *testing.T) {+ var events []schema.Event+ for i, session := range []struct {+ id, harness, model, prompt string+ input int64+ }{+ {"recurring-a", "cursor", "model-a", "repeat this task", 10},+ {"recurring-b", "cursor", "model-a", "repeat this task", 20},+ {"captured-cursor", "cursor", "model-a", "unique task", 30},+ {"captured-codex", "codex_cli", "model-b", "another task", 40},+ {"empty", "cursor", "model-a", "", 50},+ } {+ start := recurringTestEvent(fmt.Sprintf("2026-06-11T10:00:%02dZ", i), session.id, session.prompt, session.harness, "repo-a", "alice")+ if session.prompt == "" {+ start.Event.Action, start.Event.Category = "session.started", "session"+ start.Prompt = nil+ }+ usage := testSchemaEvent(fmt.Sprintf("2026-06-11T10:00:%02dZ", 10+i), session.harness, "token.usage", "metric", "repo-a")+ usage.Session = start.Session+ usage.Model = session.model+ usage.GenAI = &schema.GenAIInfo{Usage: &schema.GenAIUsageInfo{InputTokens: int64Ptr(session.input)}}+ events = append(events, start, usage)+ }+ path := newTestLog(t, marshalEvents(t, events...))+ for _, tc := range []struct {+ name string+ query EventQuery+ input int64+ }{+ {"captured", EventQuery{SessionState: "captured"}, 70},+ {"recurring", EventQuery{SessionState: "recurring"}, 30},+ {"empty", EventQuery{SessionState: "empty"}, 50},+ {"normalized captured", EventQuery{SessionState: " CaPtUrEd "}, 70},+ {"normalized recurring", EventQuery{SessionState: " ReCuRrInG "}, 30},+ {"normalized empty", EventQuery{SessionState: " EmPtY "}, 50},+ {"all", EventQuery{SessionState: "all"}, 150},+ {"unknown", EventQuery{SessionState: "unknown"}, 150},+ {"unfiltered", EventQuery{}, 150},+ {"time window", EventQuery{SessionState: "captured", Since: mustParseTime(t, "2026-06-11T10:00:02Z"), Until: mustParseTime(t, "2026-06-11T10:00:12Z")}, 30},+ {"harness", EventQuery{SessionState: "captured", Harness: "cursor"}, 30},+ {"model", EventQuery{SessionState: "captured", Model: "model-b"}, 40},+ {"session", EventQuery{SessionState: "recurring", Session: "recurring-a"}, 10},+ {"limit", EventQuery{SessionState: "captured", Limit: 1}, 70},+ } {+ t.Run(tc.name, func(t *testing.T) {+ want, err := ReadEventsAppendOrder(path, tc.query)+ if err != nil {+ t.Fatal(err)+ }+ got, contexts, err := ReadTokenEventsAppendOrder(path, tc.query)+ if err != nil {+ t.Fatal(err)+ }+ if !reflect.DeepEqual(got, want) {+ t.Fatalf("token reader selected %d events, ordinary reader selected %d; events differ", len(got), len(want))+ }+ report := tokens.AggregateScopedWithContexts(got, contexts, "", tokens.Options{})+ if report.Totals.InputTokens != tc.input || report.TotalEvents != len(want) {+ t.Fatalf("input=%d events=%d, want input=%d events=%d", report.Totals.InputTokens, report.TotalEvents, tc.input, len(want))+ }+ })+ }+}++func TestTokensEndpointSessionStateKeepsUnfilteredContexts(t *testing.T) {+ testenv.SetHome(t, t.TempDir())+ context := testSchemaEvent("2026-06-11T09:59:00Z", "claude_code", "session.context", "session", "repo-a")+ context.Event.ID = "context"+ context.Session = &schema.SessionInfo{ID: "s1"}+ context.User = schema.UserInfo{Name: "alice", UID: "501"}+ live := testSchemaEvent("2026-06-11T10:00:00Z", "claude_code", "prompt.submitted", "prompt", "repo-a")+ live.Event.ID = "live-start"+ live.Session = context.Session+ live.Harness.CollectionMethod = "otlp"+ live.Prompt = &schema.PromptInfo{Text: "first task"}+ command := testSchemaEvent("2026-06-11T10:00:01Z", "claude_code", "command.executed", "command", "repo-a")+ command.Session = context.Session+ command.Command = &schema.CommandInfo{Command: "go test ./..."}+ usage := func(timestamp, method string) schema.Event {+ event := testSchemaEvent(timestamp, "claude_code", "token.usage", "metric", "repo-a")+ event.Session = context.Session+ event.Harness.CollectionMethod = method+ event.Model = "claude-opus-4-6"+ event.GenAI = &schema.GenAIInfo{Usage: &schema.GenAIUsageInfo{InputTokens: int64Ptr(6), OutputTokens: int64Ptr(120)}}+ return event+ }+ path := newTestLog(t, marshalEvents(t, context, live, command,+ usage("2026-06-11T10:00:02Z", "poll"), usage("2026-06-11T10:00:06Z", "otlp")))+ query := EventQuery{SessionState: "captured", Since: mustParseTime(t, "2026-06-11T10:00:01Z")}+ events, contexts, err := ReadTokenEventsAppendOrder(path, query)+ if err != nil {+ t.Fatal(err)+ }+ if len(events) != 3 {+ t.Fatalf("selected events=%d, want command and two usage records", len(events))+ }+ if len(contexts) != 2 || contexts[0].Event.ID != "context" || contexts[1].Event.ID != "live-start" {+ t.Fatalf("contexts=%+v, want the session context and first live event outside the window", contexts)+ }+ assertReport := func(t *testing.T, report tokens.Report) {+ t.Helper()+ if report.TotalEvents != 3 || report.EventsWithUsage != 1 || report.SuppressedPollEvents != 1 ||+ report.Totals.InputTokens != 6 || report.Totals.OutputTokens != 120 {+ t.Fatalf("report=%+v, want three selected events and one usage contribution", report)+ }+ if len(report.ByUser) != 1 || report.ByUser[0].Key != "alice [501]" {+ t.Fatalf("users=%+v, want attribution from the context before the window", report.ByUser)+ }+ }+ assertReport(t, tokens.AggregateScopedWithContexts(events, contexts, "", tokens.Options{}))+ handler, err := Handler(Options{UserMode: true, LogPath: path})+ if err != nil {+ t.Fatal(err)+ }+ rec := httptest.NewRecorder()+ handler.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/api/tokens?session_state=captured&since=2026-06-11T10:00:01Z", nil))+ if rec.Code != http.StatusOK {+ t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String())+ }+ var report tokens.Report+ if err := json.Unmarshal(rec.Body.Bytes(), &report); err != nil {+ t.Fatal(err)+ }+ assertReport(t, report)+}++func TestReadTokenEventsSessionStateKeepsCumulativeAppendOrder(t *testing.T) {+ for _, sequenced := range []bool{false, true} {+ t.Run(fmt.Sprintf("sequenced=%t", sequenced), func(t *testing.T) {+ usage := func(input int64) schema.Event {+ event := testSchemaEvent("2026-06-11T10:00:00Z", "claude_code", "token.usage", "metric", "repo-a")+ event.Session = &schema.SessionInfo{ID: "s1"}+ event.Model = "model-a"+ event.GenAI = &schema.GenAIInfo{Usage: &schema.GenAIUsageInfo{InputTokens: int64Ptr(input)}}+ event.Raw = map[string]interface{}{"metric_name": "claude_code.token.usage", "metric_temporality": "Cumulative"}+ if sequenced {+ event.Sequence = uint64(input)+ }+ return event+ }+ path := filepath.Join(t.TempDir(), "runtime.jsonl")+ writeTestLog(t, path+".2", marshalEvents(t, usage(100))...)+ writeTestLog(t, path+".1", marshalEvents(t, usage(200))...)+ active := []schema.Event{usage(300), usage(400)}+ if sequenced {+ active[0], active[1] = active[1], active[0]+ }+ writeTestLog(t, path, marshalEvents(t, active...)...)+ events, contexts, err := ReadTokenEventsAppendOrder(path, EventQuery{SessionState: "empty"})+ if err != nil {+ t.Fatal(err)+ }+ if len(events) != 4 {+ t.Fatalf("selected events=%d, want all four metric-only session records", len(events))+ }+ for i, event := range events {+ if got, want := *event.GenAI.Usage.InputTokens, int64(i+1)*100; got != want {+ t.Fatalf("event %d input=%d want %d in append order", i, got, want)+ }+ }+ report := tokens.AggregateScopedWithContexts(events, contexts, "", tokens.Options{})+ if report.Totals.InputTokens != 400 {+ t.Fatalf("cumulative total=%d, want 400", report.Totals.InputTokens)+ }+ })+ }+}diff --git a/docs/changelog/cli.mdx b/docs/changelog/cli.mdxindex 835f09da..4316ddea 100644--- a/docs/changelog/cli.mdx+++ b/docs/changelog/cli.mdx@@ -16,6 +16,7 @@ Stay up to date with the latest changes to the Agent Beacon CLI.- **Lens findings link to events without an ID** · When a session's events carried no `event.id`, as events written through the SDK's `JSONLSink` or by releases before late August do, a lens finding on one of them came back with empty `event_ids`, so its evidence link pointed nowhere. Findings now name the same `line-N` identifier the trace shows for that event, and an explicit ID containing whitespace is no longer trimmed out of matching it.- **Session lenses show when a share is out of date** · A lens opened on a dashboard session page reported a shared session as `shared` after new work, while the trace lens and the session details showed it as stale, and it gave no refresh command. Session lenses now apply the same sharing state as the rest of the dashboard, including the `beacon share refresh` command.- **Shared Hermes sessions keep their token usage and cost** · `beacon share` of a Hermes Agent session read from its `state.db` left out the session's token usage and cost, so the shared trace showed none. They are now included, with Hermes's actual cost preferred over its estimate, as `beacon endpoint hermes sync` records them.+- **Dashboard token totals honor the session filter** · With the dashboard's session-state filter set to captured, recurring or empty, the token view showed zero tokens and no events, and `all` or an unrecognized value hid everything. Token totals now cover the same sessions the event list shows for that filter.- **Dashboard output tokens count reasoning once** · The local dashboard's token view added reasoning tokens to output, but `gen_ai.usage.output_tokens` already includes them, so models that report reasoning had their output and total tokens overstated. Output and the total now count reasoning once, the split bar no longer shows reasoning as a separate segment, and a note below it gives the reasoning count included in output. `beacon token-usage` already counted it this way.---