Skip to content

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

Fix non-transitive sort in SortEvents and SortRecordsAppendOrder

Cursor Agenta2c4add6c5db
diff --git a/cli/beacon/internal/endpoint/dashboard/events.go b/cli/beacon/internal/endpoint/dashboard/events.go
index f56b83b0..72df6d96 100644
--- a/cli/beacon/internal/endpoint/dashboard/events.go
+++ b/cli/beacon/internal/endpoint/dashboard/events.go
@@ -175,7 +175,12 @@ func SortRecordsAppendOrder(records []EventRecord) {
return records[i].Parsed.Before(records[j].Parsed)
}
si, sj := records[i].Event.Sequence, records[j].Event.Sequence
- if si != 0 && sj != 0 && si != sj {
+ 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)
diff --git a/cli/beacon/internal/endpoint/dashboard/ordering_test.go b/cli/beacon/internal/endpoint/dashboard/ordering_test.go
index 72700fcf..c201897d 100644
--- a/cli/beacon/internal/endpoint/dashboard/ordering_test.go
+++ b/cli/beacon/internal/endpoint/dashboard/ordering_test.go
@@ -58,6 +58,36 @@ func TestReadEventsAppendOrderFallsBackToLineOrderWhenUnsequenced(t *testing.T)
}
}
+func TestReadEventsAppendOrderTransitiveWithMixedSequences(t *testing.T) {
+ // Three events at the same timestamp mixing sequenced and unsequenced must sort
+ // transitively: unsequenced events come before sequenced ones (in line order among
+ // themselves), and sequenced events sort by sequence among themselves.
+ tie := "2026-05-13T01:00:00.000000000Z"
+ highSeq := testSchemaEvent(tie, "claude_code", "metric.observed", "metric", "")
+ highSeq.Sequence = 10
+ highSeq.Message = "high-seq"
+ legacy := testSchemaEvent(tie, "claude_code", "metric.observed", "metric", "")
+ legacy.Message = "legacy"
+ lowSeq := testSchemaEvent(tie, "claude_code", "metric.observed", "metric", "")
+ lowSeq.Sequence = 5
+ lowSeq.Message = "low-seq"
+
+ path := filepath.Join(t.TempDir(), "runtime.jsonl")
+ writeTestLog(t, path, marshalEvents(t, highSeq, legacy, lowSeq)...)
+
+ events, err := ReadEventsAppendOrder(path, EventQuery{})
+ if err != nil {
+ t.Fatalf("ReadEventsAppendOrder returned error: %v", err)
+ }
+ if len(events) != 3 {
+ t.Fatalf("read %d events, want 3", len(events))
+ }
+ if events[0].Message != "legacy" || events[1].Message != "low-seq" || events[2].Message != "high-seq" {
+ t.Fatalf("order = %q, %q, %q; want legacy, low-seq, high-seq",
+ events[0].Message, events[1].Message, events[2].Message)
+ }
+}
+
func TestBuildSummaryPicksLastEventByInstantNotByString(t *testing.T) {
// A log spanning the move to nanosecond stamping: ":05Z" is the older event but sorts
// after ":05.500000000Z" byte-wise, because 'Z' outranks '.'.
diff --git a/pkg/asymptoteobserve/threatrules/ordering.go b/pkg/asymptoteobserve/threatrules/ordering.go
index c05c1975..7a1bcd46 100644
--- a/pkg/asymptoteobserve/threatrules/ordering.go
+++ b/pkg/asymptoteobserve/threatrules/ordering.go
@@ -54,7 +54,12 @@ func SortEvents(events []asymptoteobserve.Event) {
if !a.at.Equal(b.at) {
return a.at.Before(b.at)
}
- if a.seq != 0 && b.seq != 0 && a.seq != b.seq {
+ aHasSeq := a.seq != 0
+ bHasSeq := b.seq != 0
+ if aHasSeq != bHasSeq {
+ return !aHasSeq // unsequenced before sequenced
+ }
+ if aHasSeq && a.seq != b.seq {
return a.seq < b.seq
}
return a.idx < b.idx
diff --git a/pkg/asymptoteobserve/threatrules/ordering_test.go b/pkg/asymptoteobserve/threatrules/ordering_test.go
index fa3dc996..5876943f 100644
--- a/pkg/asymptoteobserve/threatrules/ordering_test.go
+++ b/pkg/asymptoteobserve/threatrules/ordering_test.go
@@ -117,6 +117,23 @@ func TestSortEventsIgnoresUnsequencedTie(t *testing.T) {
}
}
+func TestSortEventsTransitiveWithMixedSequences(t *testing.T) {
+ // Three events at the same timestamp mixing sequenced and unsequenced must yield a
+ // transitive order. Before the fix, seq=10 < unsequenced < seq=5 by pairwise
+ // comparison but seq=5 < seq=10 directly — a cycle that violated sort.Slice's contract.
+ tie := "2026-08-21T10:00:00.000000000Z"
+ events := []asymptoteobserve.Event{
+ corrEvent(tie, "high-seq", "s1", func(e *asymptoteobserve.Event) { e.Sequence = 10 }),
+ corrEvent(tie, "legacy", "s1", nil),
+ corrEvent(tie, "low-seq", "s1", func(e *asymptoteobserve.Event) { e.Sequence = 5 }),
+ }
+ SortEvents(events)
+ want := []string{"legacy", "low-seq", "high-seq"}
+ if got := actions(events); !equalStrings(got, want) {
+ t.Fatalf("order = %v, want %v", got, want)
+ }
+}
+
func TestSortEventsKeepsUnstampedEventsBesideTheirNeighbours(t *testing.T) {
// An event with no parseable timestamp inherits the last one seen, so it stays where
// it arrived instead of collapsing to the epoch and jumping to the front.