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
package threatrules
import (
"testing"
"github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
)
// actions renders a sorted stream compactly so a failure shows the order it produced.
func actions(events []asymptoteobserve.Event) []string {
out := make([]string, 0, len(events))
for _, e := range events {
out = append(out, e.Event.Action)
}
return out
}
func equalStrings(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func TestSortEventsMakesOrderedCorrelationFireOnAppendOrder(t *testing.T) {
// The failure this workstream is about: read-then-egress happened in order, but the
// egress event reached the log first, so the ordered-window rule saw the reverse and
// silently missed.
c := readThenEgressRule(t)
appended := []asymptoteobserve.Event{
corrEvent("2026-08-21T10:00:30.000000000Z", "command.executed", "s1", withCurl),
corrEvent("2026-08-21T10:00:00.000000000Z", "file.read", "s1", withEnv),
}
verdict, err := c.Evaluate(appended)
if err != nil {
t.Fatalf("eval: %v", err)
}
if verdict != VerdictNoMatch {
t.Fatalf("append order should miss (that is the bug), got %s", verdict)
}
SortEvents(appended)
verdict, err = c.Evaluate(appended)
if err != nil {
t.Fatalf("eval: %v", err)
}
if verdict != VerdictMatch {
t.Fatalf("sorted stream want match, got %s", verdict)
}
}
func TestSortEventsBreaksTimestampTiesOnSequence(t *testing.T) {
// One metric export flushes datapoints that all carry that export's collection
// instant, so they tie however precise the timestamp is. Sequence is the only thing
// left that knows which came first.
tie := "2026-08-21T10:00:00.000000000Z"
events := []asymptoteobserve.Event{
corrEvent(tie, "third", "s1", func(e *asymptoteobserve.Event) { e.Sequence = 30 }),
corrEvent(tie, "first", "s1", func(e *asymptoteobserve.Event) { e.Sequence = 10 }),
corrEvent(tie, "second", "s1", func(e *asymptoteobserve.Event) { e.Sequence = 20 }),
}
SortEvents(events)
want := []string{"first", "second", "third"}
if got := actions(events); !equalStrings(got, want) {
t.Fatalf("order = %v, want %v", got, want)
}
}
func TestSortEventsKeepsTimestampAheadOfSequence(t *testing.T) {
// Sequence orders one writer's stream, not the log: the hook process that emitted
// sequence 1 may have started long after the exporter reached sequence 900. The
// timestamp is the only cross-writer key, so it has to win.
events := []asymptoteobserve.Event{
corrEvent("2026-08-21T10:00:05.000000000Z", "hook", "s1", func(e *asymptoteobserve.Event) { e.Sequence = 1 }),
corrEvent("2026-08-21T10:00:01.000000000Z", "exporter", "s1", func(e *asymptoteobserve.Event) { e.Sequence = 900 }),
}
SortEvents(events)
if got := actions(events); !equalStrings(got, []string{"exporter", "hook"}) {
t.Fatalf("order = %v, want exporter before hook", got)
}
}
func TestSortEventsIgnoresUnsequencedTie(t *testing.T) {
// A zero sequence means "unsequenced", not "first". Events written before sequence
// stamping must keep the order they arrived in rather than being dragged ahead of
// everything that does carry a counter.
tie := "2026-08-21T10:00:00.000000000Z"
events := []asymptoteobserve.Event{
corrEvent(tie, "legacy", "s1", nil),
corrEvent(tie, "sequenced", "s1", func(e *asymptoteobserve.Event) { e.Sequence = 7 }),
}
SortEvents(events)
if got := actions(events); !equalStrings(got, []string{"legacy", "sequenced"}) {
t.Fatalf("order = %v, want input order preserved", got)
}
}
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.
events := []asymptoteobserve.Event{
corrEvent("2026-08-21T10:00:05.000000000Z", "late", "s1", nil),
corrEvent("2026-08-21T10:00:01.000000000Z", "early", "s1", nil),
corrEvent("", "early-companion", "s1", nil),
corrEvent("not-a-timestamp", "early-companion-2", "s1", nil),
}
SortEvents(events)
want := []string{"early", "early-companion", "early-companion-2", "late"}
if got := actions(events); !equalStrings(got, want) {
t.Fatalf("order = %v, want %v", got, want)
}
}
func TestSortEventsLeavesAnUnstampedStreamAlone(t *testing.T) {
// Conformance fixtures carry no timestamps at all; their given order is the sequence
// under test and must survive.
events := []asymptoteobserve.Event{
corrEvent("", "one", "s1", nil),
corrEvent("", "two", "s1", nil),
corrEvent("", "three", "s1", nil),
}
SortEvents(events)
if got := actions(events); !equalStrings(got, []string{"one", "two", "three"}) {
t.Fatalf("order = %v, want input order preserved", got)
}
}
func TestSortEventsIsATotalOrder(t *testing.T) {
// Same log in, same order out -- including for events that tie on every key, which
// fall back to input position rather than to whatever the sort implementation does.
tie := "2026-08-21T10:00:00.000000000Z"
build := func() []asymptoteobserve.Event {
return []asymptoteobserve.Event{
corrEvent(tie, "a", "s1", nil),
corrEvent(tie, "b", "s2", nil),
corrEvent(tie, "c", "s1", nil),
corrEvent("2026-08-21T09:59:59.999999999Z", "d", "s1", nil),
}
}
first := build()
SortEvents(first)
for i := 0; i < 5; i++ {
again := build()
SortEvents(again)
if !equalStrings(actions(first), actions(again)) {
t.Fatalf("run %d = %v, want %v", i, actions(again), actions(first))
}
}
if got := actions(first); !equalStrings(got, []string{"d", "a", "b", "c"}) {
t.Fatalf("order = %v, want [d a b c]", got)
}
}