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): restore evidence links for legacy lens events (#970)
Divyam Talwar788cd2e5797b
diff --git a/cli/beacon/internal/endpoint/dashboard/events.go b/cli/beacon/internal/endpoint/dashboard/events.goindex f121846b..1a0bb34f 100644--- a/cli/beacon/internal/endpoint/dashboard/events.go+++ b/cli/beacon/internal/endpoint/dashboard/events.go@@ -166,6 +166,13 @@ func ReadTokenEventsAppendOrder(path string, query EventQuery) ([]schema.Event,// lines are skipped; a missing active log is not an error. If fn returns an error,// streaming stops and returns it.func StreamEvents(path string, fn func(schema.Event) error) error {+ return streamEventRecords(path, func(record EventRecord) error {+ return fn(record.Event)+ })+}++// streamEventRecords retains source/line identity for readers that link raw events to projections.+func streamEventRecords(path string, fn func(EventRecord) error) error {sources := eventSources(path) // [active, archive1, archive2, ...] (archives ascending)// Chronological order is oldest first: highest archive index down to 1, then the// active log last. Within a file, lines are already in append order.@@ -176,14 +183,14 @@ func StreamEvents(path string, fn func(schema.Event) error) error {ordered = append(ordered, sources[0])for _, source := range ordered {- if err := streamSource(source, fn); err != nil {+ if err := streamRecordSource(source, fn); err != nil {return err}}return nil}-func streamSource(source eventSource, fn func(schema.Event) error) error {+func streamRecordSource(source eventSource, fn func(EventRecord) error) error {file, err := os.Open(source.path)if err != nil {if os.IsNotExist(err) {@@ -195,7 +202,9 @@ func streamSource(source eventSource, fn func(schema.Event) error) error {scanner := bufio.NewScanner(file)scanner.Buffer(make([]byte, 0, 64*1024), 4*1024*1024)+ lineNo := 0for scanner.Scan() {+ lineNo++line := bytes.TrimSpace(scanner.Bytes())if len(line) == 0 {continue@@ -205,7 +214,7 @@ func streamSource(source eventSource, fn func(schema.Event) error) error {continue // skip malformed}normalizeDashboardEvent(&event)- if err := fn(event); err != nil {+ if err := fn(EventRecord{ID: source.lineID(lineNo), Line: lineNo, Event: event}); err != nil {return err}}diff --git a/cli/beacon/internal/endpoint/dashboard/lens_data.go b/cli/beacon/internal/endpoint/dashboard/lens_data.goindex e2e9b80e..f1016079 100644--- a/cli/beacon/internal/endpoint/dashboard/lens_data.go+++ b/cli/beacon/internal/endpoint/dashboard/lens_data.go@@ -39,17 +39,17 @@ func BuildLensData(logPath, traceID string, opts LensDataOptions) (LensDataV1, bif err != nil || !ok {return LensDataV1{}, ok, err}- var raw []schema.Event- err = StreamEvents(logPath, func(event schema.Event) error {- if traceProjectionID(event) == traceID {- raw = append(raw, event)+ var records []EventRecord+ err = streamEventRecords(logPath, func(record EventRecord) error {+ if traceProjectionID(record.Event) == traceID {+ records = append(records, record)}return nil})if err != nil {return LensDataV1{}, false, err}- return lensDataFromShow(show, raw, opts), true, nil+ return lensDataFromShow(show, records, opts), true, nil}// BuildSessionLensData is BuildLensData for the dashboard's session page, which shows every event@@ -79,10 +79,8 @@ func BuildSessionLensData(logPath, session string, opts LensDataOptions) (LensDaharness = strings.TrimSpace(first.Harness.Name)}builder := asymptoteobserve.NewTraceBuilder("session:"+harness+":"+session, first)- raw := make([]schema.Event, 0, len(records))for _, record := range records {builder.Add(record.Event, traceEventFromRecord(record, 0))- raw = append(raw, record.Event)}events := builder.Events()total := len(events)@@ -92,11 +90,11 @@ func BuildSessionLensData(logPath, session string, opts LensDataOptions) (LensDaSpans: asymptoteobserve.TraceSpans(events),Range: TraceRangeV1{TotalEvents: total, ReturnedEvents: total, Offset: 1, Limit: total},}- return lensDataFromShow(show, raw, opts), true, nil+ return lensDataFromShow(show, records, opts), true, nil}// lensDataFromShow finishes lens data from a projected trace and the endpoint events behind it.-func lensDataFromShow(show TraceShowResultV1, raw []schema.Event, opts LensDataOptions) LensDataV1 {+func lensDataFromShow(show TraceShowResultV1, records []EventRecord, opts LensDataOptions) LensDataV1 {data := LensDataV1{APIVersion: asymptoteobserve.LensAPIVersion,Trace: TraceBundleFromShow(show, version.Version),@@ -105,9 +103,13 @@ func lensDataFromShow(show TraceShowResultV1, raw []schema.Event, opts LensDataO// Findings and coverage are each optional in the contract. A rule pack that fails to load// leaves Findings nil -- "not scanned" -- instead of failing the whole lens, since every other// part of the data is still right.- if findings, err := lensFindings(raw, opts); err == nil {+ if findings, err := lensFindings(records, opts); err == nil {data.Findings = findings}+ raw := make([]schema.Event, 0, len(records))+ for _, record := range records {+ raw = append(raw, record.Event)+ }data.TokenCoverage = lensTokenCoverage(raw, show.Trace.Harness.Name)data.TokenUsage = lensTokenUsage(raw)return data@@ -173,8 +175,8 @@ func fitLensEvents(bundle *asymptoteobserve.TraceBundleV1, budget int) bool {// lensFindings runs the active rules over the trace's events. Any error, including no active rules// (*detect.NoRulesError), leaves the caller's Findings nil: "not scanned".-func lensFindings(raw []schema.Event, opts LensDataOptions) (*asymptoteobserve.LensFindingsV1, error) {- if len(raw) == 0 {+func lensFindings(records []EventRecord, opts LensDataOptions) (*asymptoteobserve.LensFindingsV1, error) {+ if len(records) == 0 {// The trace is known only to the history: its lines have rotated out of the live log.// Scanning nothing would report a clean scan of a trace no rule ever saw.return nil, errors.New("no live events to scan")@@ -183,7 +185,15 @@ func lensFindings(raw []schema.Event, opts LensDataOptions) (*asymptoteobserve.Lif err != nil {return nil, err}- events := append([]asymptoteobserve.Event(nil), raw...)+ events := make([]asymptoteobserve.Event, 0, len(records))+ for _, record := range records {+ event := record.Event+ // Only scan copies receive fallback IDs; token aggregation and source logs keep raw IDs.+ if event.Event.ID == "" {+ event.Event.ID = record.ID+ }+ events = append(events, event)+ }// Same reason as RunScan: the log is in append order, not the order things happened, and// ordered correlation rules miss without this.threatrules.SortEvents(events)@@ -201,9 +211,8 @@ func lensFindings(raw []schema.Event, opts LensDataOptions) (*asymptoteobserve.Lfor _, f := range found {ids := make([]string, 0, len(f.Events))for _, event := range f.Events {- // Trace events take their ID from the endpoint event ID, so evidence resolves against- // trace.events[].id without copying the event.- if id := strings.TrimSpace(event.Event.ID); id != "" {+ // The scan copy uses the same explicit ID or record fallback as trace.events[].id.+ if id := event.Event.ID; id != "" {ids = append(ids, id)}}diff --git a/cli/beacon/internal/endpoint/dashboard/lens_evidence_test.go b/cli/beacon/internal/endpoint/dashboard/lens_evidence_test.gonew file mode 100644index 00000000..80b17c95--- /dev/null+++ b/cli/beacon/internal/endpoint/dashboard/lens_evidence_test.go@@ -0,0 +1,194 @@+package dashboard++import (+ "encoding/json"+ "os"+ "path/filepath"+ "reflect"+ "strings"+ "testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/testenv"+)++func lensEvidenceRules(t *testing.T, correlation bool) string {+ t.Helper()+ match := `match: e.event.action == "command.executed"`+ if correlation {+ match = `correlation:+ scope: session+ window: 120s+ steps:+ - id: read+ match: e.event.action == "file.read"+ - id: command+ match: e.event.action == "command.executed"`+ }+ rule := `id: lens-evidence+version: 1+title: Lens evidence+severity: high+status: stable+posture: detect+` + match + `+emit:+ reason: "Evidence fixture"+tests:+ - name: command+ verdict: match+ events:+ - event: {action: file.read}+ session: {id: legacy}+ - event: {action: command.executed}+ session: {id: legacy}+`+ dir := t.TempDir()+ if err := os.WriteFile(filepath.Join(dir, "evidence.rule.yaml"), []byte(rule), 0o600); err != nil {+ t.Fatal(err)+ }+ return dir+}++func lensEvidenceBuilders() map[string]func(string, LensDataOptions) (LensDataV1, bool, error) {+ return map[string]func(string, LensDataOptions) (LensDataV1, bool, error){+ "trace": func(path string, opts LensDataOptions) (LensDataV1, bool, error) {+ return BuildLensData(path, "session:claude_code:legacy", opts)+ },+ "session": func(path string, opts LensDataOptions) (LensDataV1, bool, error) {+ return BuildSessionLensData(path, "legacy", opts)+ },+ }+}++func TestLensEvidencePreservesRecordIdentity(t *testing.T) {+ for name, build := range lensEvidenceBuilders() {+ for _, history := range []bool{false, true} {+ suffix := "/jsonl"+ if history {+ suffix = "/history"+ }+ t.Run(name+suffix, func(t *testing.T) {+ testenv.SetHome(t, t.TempDir())+ withoutHistory(t)+ event := testSchemaEvent("2026-06-11T10:00:00Z", "claude_code", "command.executed", "command", "repo-a")+ event.Session = &schema.SessionInfo{ID: "legacy"}+ event.Command = &schema.CommandInfo{Command: "fixture"}+ event.GenAI = &schema.GenAIInfo{Usage: &schema.GenAIUsageInfo{InputTokens: int64Ptr(100), OutputTokens: int64Ptr(10)}}+ explicit := event+ explicit.Event.ID = " explicit-id "+ other := event+ other.Session = &schema.SessionInfo{ID: "legacy-other"}+ lines := marshalEvents(t, event, event, explicit, other)+ path := newTestLog(t, append([][]byte{nil, []byte("malformed")}, lines...))+ archive := path + ".3" // A sparse rotation must keep its actual archive number.+ writeTestLog(t, archive, nil, []byte("malformed"), lines[0])+ original := map[string][]byte{}+ for _, file := range []string{path, archive} {+ var err error+ if original[file], err = os.ReadFile(file); err != nil {+ t.Fatal(err)+ }+ }+ if history {+ withHistory(t)+ optIn(t, path)+ }++ data, ok, err := build(path, LensDataOptions{UserMode: true, RulesDir: lensEvidenceRules(t, false)})+ if err != nil || !ok {+ t.Fatalf("build = ok %v, err %v", ok, err)+ }+ want := []string{"archive-3-line-3", "line-3", "line-4", " explicit-id "}+ var projected, evidence []string+ for _, event := range data.Trace.Events {+ projected = append(projected, event.ID)+ }+ if data.Findings == nil {+ t.Fatal("findings were not scanned")+ }+ for _, finding := range data.Findings.Items {+ evidence = append(evidence, finding.EventIDs...)+ }+ if !reflect.DeepEqual(projected, want) {+ t.Fatalf("projected IDs = %q, want %q", projected, want)+ }+ if len(data.Findings.Items) != len(want) || !reflect.DeepEqual(evidence, want) {+ t.Fatalf("%d findings have evidence IDs %q, want %q", len(data.Findings.Items), evidence, want)+ }+ if data.Truncated {+ t.Fatal("small fixture was truncated")+ }+ wantUsage := lensTokenUsage([]schema.Event{event, event, event, explicit})+ if !reflect.DeepEqual(data.TokenUsage, wantUsage) {+ t.Fatalf("token usage = %#v, want unchanged raw-event usage %#v", data.TokenUsage, wantUsage)+ }+ for file, before := range original {+ after, err := os.ReadFile(file)+ if err != nil || !reflect.DeepEqual(after, before) {+ t.Fatalf("source log %s changed: err %v", file, err)+ }+ }+ })+ }+ }+}++func TestLensEvidenceFollowsChronologicalCorrelation(t *testing.T) {+ for name, build := range lensEvidenceBuilders() {+ t.Run(name, func(t *testing.T) {+ testenv.SetHome(t, t.TempDir())+ withoutHistory(t)+ read := testSchemaEvent("2026-06-11T10:00:00Z", "claude_code", "file.read", "file", "repo-a")+ read.Session = &schema.SessionInfo{ID: "legacy"}+ command := testSchemaEvent("2026-06-11T10:00:30Z", "claude_code", "command.executed", "command", "repo-a")+ command.Session = read.Session+ // The later event arrived first; correlation must sort events without losing IDs.+ path := newTestLog(t, marshalEvents(t, command, read))+ data, ok, err := build(path, LensDataOptions{UserMode: true, RulesDir: lensEvidenceRules(t, true)})+ if err != nil || !ok {+ t.Fatalf("build = ok %v, err %v", ok, err)+ }+ if data.Findings == nil || len(data.Findings.Items) != 1 {+ t.Fatalf("findings = %#v, want one ordered correlation", data.Findings)+ }+ if got := data.Findings.Items[0].EventIDs; !reflect.DeepEqual(got, []string{"line-2", "line-1"}) {+ t.Fatalf("correlation evidence = %v, want [line-2 line-1]", got)+ }+ })+ }+}++func TestLensFindingsDoesNotMutateRawInput(t *testing.T) {+ event := testSchemaEvent("2026-06-11T10:00:00Z", "claude_code", "command.executed", "command", "repo-a")+ raw := []EventRecord{{ID: "line-1", Event: event}}+ before, err := json.Marshal(raw)+ if err != nil {+ t.Fatal(err)+ }+ if _, err := lensFindings(raw, LensDataOptions{RulesDir: lensEvidenceRules(t, false)}); err != nil {+ t.Fatal(err)+ }+ after, err := json.Marshal(raw)+ if err != nil {+ t.Fatal(err)+ }+ if !reflect.DeepEqual(after, before) {+ t.Fatal("scan mutated its caller's raw events")+ }+}++func TestLensEvidenceRetainsReferencesWhenBundleIsTruncated(t *testing.T) {+ event := testSchemaEvent("2026-06-11T10:00:00Z", "claude_code", "command.executed", "command", "repo-a")+ show := TraceShowResultV1{+ Events: []TraceEventV1{{ID: "line-1", Summary: strings.Repeat("x", lensDataEventBudget)}},+ Range: TraceRangeV1{TotalEvents: 1, ReturnedEvents: 1, Offset: 1, Limit: 1},+ }+ data := lensDataFromShow(show, []EventRecord{{ID: "line-1", Event: event}}, LensDataOptions{RulesDir: lensEvidenceRules(t, false)})+ if !data.Truncated || len(data.Trace.Events) != 0 || data.Trace.Range.TotalEvents != 1 || data.Trace.Range.ReturnedEvents != 0 {+ t.Fatalf("truncation = %v, events = %d, range = %#v", data.Truncated, len(data.Trace.Events), data.Trace.Range)+ }+ if data.Findings == nil || len(data.Findings.Items) != 1 || !reflect.DeepEqual(data.Findings.Items[0].EventIDs, []string{"line-1"}) {+ t.Fatalf("findings = %#v, want evidence for the event omitted by truncation", data.Findings)+ }+}diff --git a/docs/changelog/cli.mdx b/docs/changelog/cli.mdxindex 5dc631b0..8c09954c 100644--- a/docs/changelog/cli.mdx+++ b/docs/changelog/cli.mdx@@ -13,6 +13,7 @@ Stay up to date with the latest changes to the Agent Beacon CLI.- **Devin CLI session history sync** · `beacon endpoint devin sync` reads Devin CLI's committed SQLite session store locally and read-only into the runtime log, adding what the live hooks cannot see: assistant replies, reasoning, per-message token usage, the per-turn model with model-change events, compaction summaries, and `approval.denied` for rejected tool calls. Sessions the hooks already cover get only those additions, so nothing is counted twice, and the install backfill includes Devin CLI. Verified on macOS; the Linux and Windows paths are best effort. See [Devin CLI](/runtimes/devin#session-database-sync).- **Claude.ai and ChatGPT answers show in traces** · Answers the browser extension records as `agent.response.completed` were classified as `other` in trace views, so `beacon endpoint traces show`, the dashboard and shared traces showed the prompt without the answer. They are now assistant messages carrying the answer, as are plans recorded as `agent.plan`. OpenCode's separate completion event, which carries only usage, stays out of the conversation. Local history is rebuilt on its next read, so earlier sessions show their answers too.+- **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.- **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.---