Skip to content

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

Fold the session list's sharing state in append order

Claude4f63a00da2ad
diff --git a/cli/beacon/internal/endpoint/dashboard/events.go b/cli/beacon/internal/endpoint/dashboard/events.go
index a1c60bfb..ff9cd9f4 100644
--- a/cli/beacon/internal/endpoint/dashboard/events.go
+++ b/cli/beacon/internal/endpoint/dashboard/events.go
@@ -263,14 +263,26 @@ func classifySessions(path string, query EventQuery) (map[string]sessionClassifi
Until: query.Until,
}
stats := map[string]*sessionStateStats{}
- sharingAcc := map[string]*sessionAccumulator{}
+ sessionRecords := map[string][]EventRecord{}
+ shared := map[string]bool{}
for _, source := range eventSources(path) {
- if err := collectSessionStateStats(source, stateQuery, stats, sharingAcc); err != nil {
+ if err := collectSessionStateStats(source, stateQuery, stats, sessionRecords, shared); err != nil {
return nil, nil, err
}
}
+ // Sharing state depends on the order of a session's events, so fold them in append order
+ // (across rotated archives), as ReadSessionDetail and the trace history do.
sharing := map[string]*SessionSharing{}
- for id, acc := range sharingAcc {
+ for id := range shared {
+ records := sessionRecords[id]
+ SortRecordsAppendOrder(records)
+ acc := &sessionAccumulator{record: SessionRecord{ID: id}}
+ for _, record := range records {
+ if acc.record.Harness == "" {
+ acc.record.Harness = record.Event.Harness.Name
+ }
+ acc.noteSharing(record)
+ }
if acc.record.Sharing == nil {
continue
}
@@ -301,7 +313,7 @@ func classifySessions(path string, query EventQuery) (map[string]sessionClassifi
return classifications, sharing, nil
}
-func collectSessionStateStats(source eventSource, query EventQuery, stats map[string]*sessionStateStats, sharing map[string]*sessionAccumulator) error {
+func collectSessionStateStats(source eventSource, query EventQuery, stats map[string]*sessionStateStats, sessionRecords map[string][]EventRecord, shared map[string]bool) error {
file, err := os.Open(source.path)
if err != nil {
if os.IsNotExist(err) {
@@ -337,12 +349,11 @@ func collectSessionStateStats(source eventSource, query EventQuery, stats map[st
if event.Session == nil || event.Session.ID == "" {
continue
}
- if acc := sharing[event.Session.ID]; acc == nil {
- sharing[event.Session.ID] = &sessionAccumulator{record: SessionRecord{ID: event.Session.ID, Harness: event.Harness.Name}}
- } else if acc.record.Harness == "" {
- acc.record.Harness = event.Harness.Name
+ // Every session's records, slimmed to what the sharing state reads, whatever the filters.
+ sessionRecords[event.Session.ID] = append(sessionRecords[event.Session.ID], shareRelevantRecord(record))
+ if event.Event.Action == "session.shared" {
+ shared[event.Session.ID] = true
}
- sharing[event.Session.ID].noteSharing(record)
if !matchesQuery(record, query) {
continue
}
diff --git a/cli/beacon/internal/endpoint/dashboard/share_activity.go b/cli/beacon/internal/endpoint/dashboard/share_activity.go
index 32e528f0..c2a469d1 100644
--- a/cli/beacon/internal/endpoint/dashboard/share_activity.go
+++ b/cli/beacon/internal/endpoint/dashboard/share_activity.go
@@ -109,3 +109,43 @@ func isAssistantOutput(event schema.Event) bool {
}
return event.GenAI != nil && event.GenAI.Usage != nil
}
+
+// shareRelevantRecord is the part of record that the sharing state reads (noteSharing and
+// shareActivity), so the unfiltered sharing pass can hold a session's records until it can fold
+// them in append order without holding their content.
+func shareRelevantRecord(record EventRecord) EventRecord {
+ e := record.Event
+ slim := schema.Event{
+ Timestamp: e.Timestamp,
+ Event: e.Event,
+ Harness: e.Harness,
+ Session: e.Session,
+ MCP: e.MCP,
+ }
+ if e.Event.Action == "session.shared" {
+ slim.Raw = e.Raw
+ }
+ if e.Prompt != nil {
+ slim.Prompt = &schema.PromptInfo{ID: e.Prompt.ID}
+ if strings.TrimSpace(e.Prompt.Text) != "" {
+ slim.Prompt.Text = "-"
+ }
+ }
+ if e.Command != nil {
+ slim.Command = &schema.CommandInfo{Command: e.Command.Command}
+ }
+ if e.Tool != nil {
+ tool := *e.Tool
+ slim.Tool = &tool
+ }
+ if e.File != nil {
+ slim.File = &schema.FileInfo{Path: e.File.Path}
+ }
+ if e.GenAI != nil {
+ slim.GenAI = &schema.GenAIInfo{Usage: e.GenAI.Usage}
+ if id := asymptoteobserve.ToolCallID(&e); id != "" {
+ slim.GenAI.Tool = &asymptoteobserve.GenAIToolInfo{Call: &asymptoteobserve.GenAIToolCallInfo{ID: id}}
+ }
+ }
+ return EventRecord{ID: record.ID, Line: record.Line, Event: slim, Parsed: record.Parsed}
+}
diff --git a/cli/beacon/internal/endpoint/dashboard/sharing_view_test.go b/cli/beacon/internal/endpoint/dashboard/sharing_view_test.go
index a313ba96..74565aae 100644
--- a/cli/beacon/internal/endpoint/dashboard/sharing_view_test.go
+++ b/cli/beacon/internal/endpoint/dashboard/sharing_view_test.go
@@ -1,6 +1,8 @@
package dashboard
import (
+ "os"
+ "path/filepath"
"strings"
"testing"
"time"
@@ -236,3 +238,49 @@ func TestShareFromOutsideTheSessionGoesStaleOnAReply(t *testing.T) {
}
}
}
+
+// Sharing state folds a session's events in append order across rotated archives, so the session
+// list, the session page and the trace list agree after a rotation.
+func TestSharingStateAcrossARotatedLog(t *testing.T) {
+ dir := t.TempDir()
+ path := filepath.Join(dir, "runtime.jsonl")
+ archived := []string{
+ `{"timestamp":"2026-06-11T10:00:00Z","event":{"id":"r1","action":"prompt.submitted","category":"prompt"},"harness":{"name":"claude_code"},"session":{"id":"rot1"},"prompt":{"text":"share this"}}`,
+ `{"timestamp":"2026-06-11T10:00:05Z","event":{"id":"r2","action":"tool.invoked","category":"tool"},"harness":{"name":"claude_code"},"session":{"id":"rot1"},"tool":{"name":"Bash","command":"beacon share"},"gen_ai":{"tool":{"call":{"id":"call-r"}}}}`,
+ `{"timestamp":"2026-06-11T10:00:09Z","event":{"id":"r3","action":"session.shared","category":"session","fidelity":"inferred"},"harness":{"name":"claude_code"},"session":{"id":"rot1"},"raw":{"shared_url":"https://beacon.sh/s/rot1","visibility":"direct","shared_at":"2026-06-11T10:00:09Z","last_refreshed_at":"2026-06-11T10:00:09Z","shared_through":"2026-06-11T10:00:06Z"}}`,
+ }
+ active := []string{
+ `{"timestamp":"2026-06-11T10:00:10Z","event":{"id":"r4","action":"tool.completed","category":"tool"},"harness":{"name":"claude_code"},"session":{"id":"rot1"},"tool":{"name":"Bash"},"gen_ai":{"tool":{"call":{"id":"call-r"}}}}`,
+ `{"timestamp":"2026-06-11T10:00:15Z","event":{"id":"r5","action":"agent.message","category":"agent"},"harness":{"name":"claude_code"},"session":{"id":"rot1"},"message":"Shared."}`,
+ }
+ if err := os.WriteFile(path+".1", []byte(strings.Join(archived, "\n")+"\n"), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(path, []byte(strings.Join(active, "\n")+"\n"), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ withoutHistory(t)
+ if s := sessionSharing(t, path, EventQuery{})["rot1"]; s == nil || s.State != "shared" {
+ t.Fatalf("ReadSessions sharing = %+v, want shared", s)
+ }
+ detail, ok, err := ReadSessionDetail(path, "rot1")
+ if err != nil || !ok || detail.Session.Sharing == nil || detail.Session.Sharing.State != "shared" {
+ t.Fatalf("ReadSessionDetail sharing = %+v (%v %v), want shared", detail.Session.Sharing, ok, err)
+ }
+ list, err := ReadTraceList(path, TraceQuery{Limit: 50})
+ if err != nil {
+ t.Fatal(err)
+ }
+ found := false
+ for _, trace := range list.Traces {
+ if trace.ID == "session:claude_code:rot1" {
+ found = true
+ if trace.Sharing.State != "shared" {
+ t.Fatalf("trace sharing = %+v, want shared", trace.Sharing)
+ }
+ }
+ }
+ if !found {
+ t.Fatal("trace list missed rot1")
+ }
+}