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): retain newest events across timestamp ties (#844)
Rudy Mizrahi Celeklidb7fbcfce548
diff --git a/cli/beacon/internal/endpoint/dashboard/events.go b/cli/beacon/internal/endpoint/dashboard/events.goindex 33624eef..f121846b 100644--- a/cli/beacon/internal/endpoint/dashboard/events.go+++ b/cli/beacon/internal/endpoint/dashboard/events.go@@ -86,10 +86,7 @@ func ReadEvents(path string, query EventQuery) (EventResult, error) {}}sort.SliceStable(result.Events, func(i, j int) bool {- if result.Events[i].Parsed.Equal(result.Events[j].Parsed) {- return result.Events[i].ID > result.Events[j].ID- }- return result.Events[i].Parsed.After(result.Events[j].Parsed)+ return recordBefore(result.Events[j], result.Events[i])})result.Returned = len(result.Events)result.Truncated = result.TotalMatched > len(result.Events)@@ -101,8 +98,8 @@ func ReadEvents(path string, query EventQuery) (EventResult, error) {// order because runtime timestamps are second-resolution, so a batch of// cumulative datapoints from one export commonly shares a timestamp; the// cumulative dedup then relies on append order to recover the emission sequence.-// The newest-first ReadEvents sort breaks such ties on lexicographic line IDs,-// which mis-orders line-9 vs line-10 within a second.+// Restore oldest-first emission order after the newest-first ReadEvents sort+// before aggregating cumulative counters.func ReadEventsAppendOrder(path string, query EventQuery) ([]schema.Event, error) {query.NoLimit = trueresult, err := ReadEvents(path, query)@@ -550,32 +547,36 @@ func rawString(raw map[string]interface{}, key string) string {// tie on the timestamp however precise it is, and the cumulative dedup needs// their emission order back. Sequence recovers it directly; the archive-and-line// tiebreak below is the fallback for events written before sequence stamping,-// and is still needed because the newest-first ReadEvents sort breaks ties on-// lexicographic line IDs, which mis-orders line-9 against line-10.+// and is also used in reverse by the newest-first ReadEvents sort so limiting+// a result cannot discard newer events on lexicographic line-ID ties.func SortRecordsAppendOrder(records []EventRecord) {sort.SliceStable(records, func(i, j int) bool {- if !records[i].Parsed.Equal(records[j].Parsed) {- return records[i].Parsed.Before(records[j].Parsed)- }- si, sj := records[i].Event.Sequence, records[j].Event.Sequence- iHasSeq := si != 0- jHasSeq := sj != 0- if iHasSeq != jHasSeq {- return !iHasSeq // unsequenced before sequenced- }- if iHasSeq && si != sj {- return si < sj- }- ai, li, _ := parseRecordID(records[i].ID)- aj, lj, _ := parseRecordID(records[j].ID)- if ai != aj {- // Higher archive index is an older rotation, so it comes first.- return ai > aj- }- return li < lj+ return recordBefore(records[i], records[j])})}+func recordBefore(a, b EventRecord) bool {+ if !a.Parsed.Equal(b.Parsed) {+ return a.Parsed.Before(b.Parsed)+ }+ si, sj := a.Event.Sequence, b.Event.Sequence+ iHasSeq := si != 0+ jHasSeq := sj != 0+ if iHasSeq != jHasSeq {+ return !iHasSeq // unsequenced before sequenced+ }+ if iHasSeq && si != sj {+ return si < sj+ }+ ai, li, _ := parseRecordID(a.ID)+ aj, lj, _ := parseRecordID(b.ID)+ if ai != aj {+ // Higher archive index is an older rotation, so it comes first.+ return ai > aj+ }+ return li < lj+}+func readEventsFromSource(source eventSource, query EventQuery, result *EventResult, limit int) error {file, err := os.Open(source.path)if err != nil {@@ -618,10 +619,7 @@ func readEventsFromSource(source eventSource, query EventQuery, result *EventResresult.Events = append(result.Events, record)if !query.NoLimit {sort.SliceStable(result.Events, func(i, j int) bool {- if result.Events[i].Parsed.Equal(result.Events[j].Parsed) {- return result.Events[i].ID > result.Events[j].ID- }- return result.Events[i].Parsed.After(result.Events[j].Parsed)+ return recordBefore(result.Events[j], result.Events[i])})if len(result.Events) > limit {result.Events = result.Events[:limit]diff --git a/cli/beacon/internal/endpoint/dashboard/newest_ties_test.go b/cli/beacon/internal/endpoint/dashboard/newest_ties_test.gonew file mode 100644index 00000000..a103949e--- /dev/null+++ b/cli/beacon/internal/endpoint/dashboard/newest_ties_test.go@@ -0,0 +1,74 @@+package dashboard++import (+ "encoding/json"+ "net/http"+ "net/http/httptest"+ "path/filepath"+ "testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/testenv"+)++func TestReadEventsNewestTiesUseNumericLineBeforeLimit(t *testing.T) {+ testenv.SetHome(t, t.TempDir())+ path := filepath.Join(t.TempDir(), "runtime.jsonl")+ lines := make([][]byte, 11)+ for i := range lines {+ lines[i] = testEvent("2026-05-13T01:00:00Z", "cursor", "tool.completed", "tool", "repo")+ }+ writeTestLog(t, path, lines...)+ handler, err := Handler(Options{UserMode: true, LogPath: path})+ if err != nil {+ t.Fatal(err)+ }+ response := httptest.NewRecorder()+ handler.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/api/events?limit=3", nil))+ if response.Code != http.StatusOK {+ t.Fatalf("HTTP %d: %s", response.Code, response.Body.String())+ }+ var result EventResult+ if err := json.Unmarshal(response.Body.Bytes(), &result); err != nil {+ t.Fatal(err)+ }+ if result.TotalMatched != 11 || result.Returned != 3 || !result.Truncated {+ t.Fatalf("wrong result metadata: %+v", result)+ }+ for i, want := range []string{"line-11", "line-10", "line-9"} {+ if result.Events[i].ID != want {+ t.Fatalf("event %d=%s, want %s", i, result.Events[i].ID, want)+ }+ }+}++func TestReadEventsNewestTiesUseEmissionSequence(t *testing.T) {+ path := filepath.Join(t.TempDir(), "runtime.jsonl")+ newest := testSchemaEvent("2026-05-13T01:00:00Z", "cursor", "tool.completed", "tool", "repo")+ newest.Sequence = 12+ older := newest+ older.Sequence = 3+ writeTestLog(t, path, marshalEvents(t, newest, older)...)+ for _, noLimit := range []bool{false, true} {+ result, err := ReadEvents(path, EventQuery{Limit: 1, NoLimit: noLimit})+ if err != nil {+ t.Fatal(err)+ }+ if result.Events[0].Event.Sequence != 12 {+ t.Fatalf("NoLimit=%t first sequence=%d, want 12", noLimit, result.Events[0].Event.Sequence)+ }+ }+}++func TestReadEventsNewestTiesKeepNewerRotation(t *testing.T) {+ path := filepath.Join(t.TempDir(), "runtime.jsonl")+ for _, suffix := range []string{".1", ".2"} {+ writeTestLog(t, path+suffix, testEvent("2026-05-13T01:00:00Z", "cursor", "tool.completed", "tool", "repo"))+ }+ result, err := ReadEvents(path, EventQuery{Limit: 1})+ if err != nil {+ t.Fatal(err)+ }+ if result.Events[0].ID != "archive-1-line-1" {+ t.Fatalf("first event=%s, want newest archive", result.Events[0].ID)+ }+}