Skip to content

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

fix(dashboard): finish the trace title and free-text fixes, cover them with tests

Claude46184c835d8b
diff --git a/cli/beacon/internal/endpoint/dashboard/traces.go b/cli/beacon/internal/endpoint/dashboard/traces.go
index 9a5fc1d6..8639b24a 100644
--- a/cli/beacon/internal/endpoint/dashboard/traces.go
+++ b/cli/beacon/internal/endpoint/dashboard/traces.go
@@ -74,9 +74,9 @@ func ReadTraceList(path string, query TraceQuery) (TraceListResultV1, error) {
filtered := filterTraceSummaries(traces, query)
if needle := strings.TrimSpace(query.Q); needle != "" {
matched := make([]TraceSummaryV1, 0, len(filtered))
- for _, s := range filtered {
- if traceSummaryMatches(s, needle) {
- matched = append(matched, s)
+ for _, summary := range filtered {
+ if traceAggregateMatches(traces[summary.ID], needle) {
+ matched = append(matched, summary)
}
}
filtered = matched
@@ -227,10 +227,11 @@ func readTraceAggregates(path string, query EventQuery) (map[string]*traceAggreg
agg := traces[id]
if agg == nil {
agg = &traceAggregate{
- summary: newTraceSummary(id, event),
- spans: map[string]*TraceSpanV1{},
- models: map[string]bool{},
- methods: map[string]bool{},
+ summary: newTraceSummary(id, event),
+ spans: map[string]*TraceSpanV1{},
+ models: map[string]bool{},
+ methods: map[string]bool{},
+ titlePriority: -1,
}
traces[id] = agg
}
@@ -276,7 +277,9 @@ func (a *traceAggregate) update(event schema.Event, traceEvent TraceEventV1) {
}
if p := traceTitlePriority(traceEvent); p > a.titlePriority {
a.summary.Title = traceTitle(event, traceEvent)
- a.summary.Preview = tracePreview(traceEvent)
+ if preview := tracePreview(traceEvent); preview != "" {
+ a.summary.Preview = preview
+ }
a.titlePriority = p
}
if event.Session != nil && a.summary.Session == nil {
@@ -551,6 +554,13 @@ func traceActor(event schema.Event) string {
}
}
+// traceTitlePriority ranks an event as a source for the trace title and
+// preview. A trace usually opens on lifecycle noise (session.started,
+// session.context), so a later user prompt outranks it and takes the label;
+// ties keep the earlier event, so the first prompt names the trace. The
+// aggregate seeds this at -1 rather than 0, so a trace that only ever carries
+// lifecycle events is still named by its first one rather than falling back to
+// its raw trace ID in finish().
func traceTitlePriority(event TraceEventV1) int {
switch event.Type {
case "user_message":
@@ -738,6 +748,25 @@ func filterTraceSummaries(traces map[string]*traceAggregate, query TraceQuery) [
return out
}
+// traceAggregateMatches keeps a trace whose summary or any of its events match
+// the free-text query. Matching the summary alone would hide a trace whose only
+// mention of the term is in a command, a path, or a message -- the events the
+// list is built from but does not show.
+func traceAggregateMatches(agg *traceAggregate, needle string) bool {
+ if agg == nil {
+ return false
+ }
+ if traceSummaryMatches(agg.summary, needle) {
+ return true
+ }
+ for _, event := range agg.events {
+ if traceEventMatches(event, needle) {
+ return true
+ }
+ }
+ return false
+}
+
func sortTraceSummaries(traces []TraceSummaryV1) {
sort.SliceStable(traces, func(i, j int) bool {
it, _ := schema.ParseTimestamp(traces[i].UpdatedAt)
diff --git a/cli/beacon/internal/endpoint/dashboard/traces_test.go b/cli/beacon/internal/endpoint/dashboard/traces_test.go
index d3f41e0d..cc69d284 100644
--- a/cli/beacon/internal/endpoint/dashboard/traces_test.go
+++ b/cli/beacon/internal/endpoint/dashboard/traces_test.go
@@ -196,4 +196,184 @@ func TestTraceAPIEndpoints(t *testing.T) {
}
}
+func TestTraceTitlePrefersUserPromptOverEarlierLifecycleEvents(t *testing.T) {
+ path := filepath.Join(t.TempDir(), "runtime.jsonl")
+ started := testSchemaEvent("2026-06-11T10:00:00Z", "cursor", "session.started", "session", "repo-a")
+ started.Event.ID = "evt-started"
+ started.Session = &schema.SessionInfo{ID: "s1", WorkingDirectory: "/repo/a"}
+ context := testSchemaEvent("2026-06-11T10:00:01Z", "cursor", "session.context", "session", "repo-a")
+ context.Event.ID = "evt-context"
+ context.Session = started.Session
+ prompt := testSchemaEvent("2026-06-11T10:00:02Z", "cursor", "prompt.submitted", "prompt", "repo-a")
+ prompt.Event.ID = "evt-prompt"
+ prompt.Session = started.Session
+ prompt.Prompt = &schema.PromptInfo{Text: "Investigate the flaky dashboard test"}
+ later := testSchemaEvent("2026-06-11T10:00:03Z", "cursor", "prompt.submitted", "prompt", "repo-a")
+ later.Event.ID = "evt-prompt-2"
+ later.Session = started.Session
+ later.Prompt = &schema.PromptInfo{Text: "Now ship it"}
+ writeTestLog(t, path, marshalEvents(t, started, context, prompt, later)...)
+
+ result, err := ReadTraceList(path, TraceQuery{Limit: 10})
+ if err != nil {
+ t.Fatalf("ReadTraceList returned error: %v", err)
+ }
+ if len(result.Traces) != 1 {
+ t.Fatalf("traces = %#v, want 1", result.Traces)
+ }
+ trace := result.Traces[0]
+ if trace.Title != "Investigate the flaky dashboard test" {
+ t.Fatalf("trace title = %q, want the first user prompt", trace.Title)
+ }
+ if trace.Preview != "Investigate the flaky dashboard test" {
+ t.Fatalf("trace preview = %q, want the first user prompt", trace.Preview)
+ }
+ if trace.EventCount != 4 {
+ t.Fatalf("trace event count = %d, want 4", trace.EventCount)
+ }
+}
+
+func TestTraceTitleFallsBackToFirstEventWithoutPrompt(t *testing.T) {
+ path := filepath.Join(t.TempDir(), "runtime.jsonl")
+ command := testSchemaEvent("2026-06-11T10:00:00Z", "cursor", "command.executed", "command", "repo-a")
+ command.Event.ID = "evt-command"
+ command.Session = &schema.SessionInfo{ID: "s1"}
+ command.Command = &schema.CommandInfo{Command: "go test ./..."}
+
+ lifecycle := testSchemaEvent("2026-06-11T11:00:00Z", "cursor", "session.started", "session", "repo-a")
+ lifecycle.Event.ID = "evt-lifecycle"
+ lifecycle.Session = &schema.SessionInfo{ID: "s2"}
+ writeTestLog(t, path, marshalEvents(t, command, lifecycle)...)
+
+ result, err := ReadTraceList(path, TraceQuery{Limit: 10})
+ if err != nil {
+ t.Fatalf("ReadTraceList returned error: %v", err)
+ }
+ titles := map[string]string{}
+ previews := map[string]string{}
+ for _, trace := range result.Traces {
+ titles[trace.ID] = trace.Title
+ previews[trace.ID] = trace.Preview
+ }
+ if titles["session:cursor:s1"] != "go test ./..." {
+ t.Fatalf("command trace title = %q, want its only event", titles["session:cursor:s1"])
+ }
+ // A trace that never gets past lifecycle events is still named by that event
+ // rather than falling back to its raw trace ID.
+ if titles["session:cursor:s2"] != "session.started" {
+ t.Fatalf("lifecycle-only trace title = %q, want session.started", titles["session:cursor:s2"])
+ }
+ if previews["session:cursor:s2"] == "" {
+ t.Fatalf("lifecycle-only trace has no preview: %#v", result.Traces)
+ }
+}
+
+func TestSearchTracesEventResultsAreOrderedAndTypeFiltered(t *testing.T) {
+ path := filepath.Join(t.TempDir(), "runtime.jsonl")
+ events := []schema.Event{}
+ // Three sessions, each a prompt plus a command, all mentioning "migrations".
+ // Sessions are written oldest first so the newest sorts to the top.
+ for _, session := range []struct{ id, hour string }{{"s1", "10"}, {"s2", "11"}, {"s3", "12"}} {
+ prompt := testSchemaEvent("2026-06-11T"+session.hour+":00:00Z", "cursor", "prompt.submitted", "prompt", "repo-a")
+ prompt.Event.ID = "evt-prompt-" + session.id
+ prompt.Session = &schema.SessionInfo{ID: session.id}
+ prompt.Prompt = &schema.PromptInfo{Text: "Investigate migrations in " + session.id}
+ command := testSchemaEvent("2026-06-11T"+session.hour+":00:01Z", "cursor", "command.executed", "command", "repo-a")
+ command.Event.ID = "evt-command-" + session.id
+ command.Session = prompt.Session
+ command.Command = &schema.CommandInfo{Command: "go test ./migrations # " + session.id}
+ events = append(events, prompt, command)
+ }
+ writeTestLog(t, path, marshalEvents(t, events...)...)
+
+ // Six events match but only two are returned, so the order decides which
+ // ones. Repeat the search: the aggregates live in a map, and an unsorted
+ // walk would hand back a different pair on some runs.
+ for i := 0; i < 8; i++ {
+ results, err := SearchTraces(path, TraceQuery{EventQuery: EventQuery{Q: "migrations"}, Limit: 2, ResultLevel: "event"})
+ if err != nil {
+ t.Fatalf("SearchTraces returned error: %v", err)
+ }
+ if results.TotalMatched != 6 || len(results.Events) != 2 {
+ t.Fatalf("event results = matched %d returned %d, want 6/2", results.TotalMatched, len(results.Events))
+ }
+ if got := results.Events[0].Event.ID; got != "evt-prompt-s3" {
+ t.Fatalf("first event = %q, want evt-prompt-s3 (newest trace first)", got)
+ }
+ if got := results.Events[1].Event.ID; got != "evt-command-s3" {
+ t.Fatalf("second event = %q, want evt-command-s3", got)
+ }
+ }
+
+ filtered, err := SearchTraces(path, TraceQuery{
+ EventQuery: EventQuery{Q: "migrations"},
+ Limit: 10,
+ ResultLevel: "event",
+ EventTypes: []string{"command"},
+ })
+ if err != nil {
+ t.Fatalf("SearchTraces filtered returned error: %v", err)
+ }
+ if filtered.TotalMatched != 3 || len(filtered.Events) != 3 {
+ t.Fatalf("filtered results = matched %d returned %d, want 3/3", filtered.TotalMatched, len(filtered.Events))
+ }
+ for _, match := range filtered.Events {
+ if match.Event.Type != "command" {
+ t.Fatalf("event_type filter returned %q", match.Event.Type)
+ }
+ }
+ if filtered.Filters["event_type"] != "command" {
+ t.Fatalf("filters = %#v, want event_type command", filtered.Filters)
+ }
+}
+
+func TestFreeTextSelectsTracesWithoutDroppingTheirEvents(t *testing.T) {
+ path := filepath.Join(t.TempDir(), "runtime.jsonl")
+ prompt := testSchemaEvent("2026-06-11T10:00:00Z", "cursor", "prompt.submitted", "prompt", "repo-a")
+ prompt.Event.ID = "evt-prompt"
+ prompt.Session = &schema.SessionInfo{ID: "s1"}
+ prompt.Prompt = &schema.PromptInfo{Text: "Investigate database migrations"}
+ command := testSchemaEvent("2026-06-11T10:01:00Z", "cursor", "command.executed", "command", "repo-a")
+ command.Event.ID = "evt-command"
+ command.Session = prompt.Session
+ command.Command = &schema.CommandInfo{Command: "go test ./migrations"}
+ note := testSchemaEvent("2026-06-11T10:02:00Z", "cursor", "file.modified", "file", "repo-a")
+ note.Event.ID = "evt-file"
+ note.Session = prompt.Session
+ note.File = &schema.FileInfo{Path: "notes.txt", Operation: "modify"}
+ other := testSchemaEvent("2026-06-11T09:00:00Z", "cursor", "prompt.submitted", "prompt", "repo-b")
+ other.Event.ID = "evt-other"
+ other.Session = &schema.SessionInfo{ID: "s2"}
+ other.Prompt = &schema.PromptInfo{Text: "Rewrite the changelog"}
+ writeTestLog(t, path, marshalEvents(t, other, prompt, command, note)...)
+
+ list, err := ReadTraceList(path, TraceQuery{EventQuery: EventQuery{Q: "database"}, Limit: 10})
+ if err != nil {
+ t.Fatalf("ReadTraceList returned error: %v", err)
+ }
+ if list.TotalMatched != 1 || len(list.Traces) != 1 || list.Traces[0].ID != "session:cursor:s1" {
+ t.Fatalf("list = %#v, want only the matching trace", list)
+ }
+ if list.Traces[0].EventCount != 3 {
+ t.Fatalf("matched trace event count = %d, want all 3 of its events", list.Traces[0].EventCount)
+ }
+
+ // A term that only one event carries still selects the whole trace.
+ byEvent, err := ReadTraceList(path, TraceQuery{EventQuery: EventQuery{Q: "notes.txt"}, Limit: 10})
+ if err != nil {
+ t.Fatalf("ReadTraceList by event returned error: %v", err)
+ }
+ if byEvent.TotalMatched != 1 || byEvent.Traces[0].EventCount != 3 {
+ t.Fatalf("event-matched list = %#v, want the whole trace", byEvent.Traces)
+ }
+
+ show, ok, err := ShowTrace(path, "session:cursor:s1", TraceQuery{EventQuery: EventQuery{Q: "database"}, Limit: 10})
+ if err != nil || !ok {
+ t.Fatalf("ShowTrace ok=%t err=%v", ok, err)
+ }
+ if show.Range.TotalEvents != 3 || len(show.Events) != 3 {
+ t.Fatalf("show = range %#v events %d, want the full 3-event timeline", show.Range, len(show.Events))
+ }
+}
+
func int64Ptr(v int64) *int64 { return &v }