Skip to content

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

Merge origin/main into claude/issue-700-tool-result-cel

Claude4bb7fe18f49a
diff --cc pkg/asymptoteobserve/threatrules/correlation.go
index 68655d01,e459cb1c..4a4bda08
--- a/pkg/asymptoteobserve/threatrules/correlation.go
+++ b/pkg/asymptoteobserve/threatrules/correlation.go
@@@ -90,6 -102,97 +102,97 @@@ func (c *CompiledRule) completeFrom(eve
return nil, nil
}
+ // matchSessionAnyOrder returns the matched events, in the order they occurred, for the
+ // first set of distinct events in one session that satisfies every step within the
+ // window regardless of which step's event came first, or nil if none.
+ //
+ // The window is measured from the earliest matched event, as it is for a sequence: every
+ // matched event must fall within window of that anchor. Each event is evaluated against
+ // each step once, up front. Then every event that satisfies some step is tried as the
+ // anchor, and the steps are assigned distinct events at or after it by bipartite
+ // matching, so one event never satisfies two steps and a step whose only candidate is
+ // already taken can displace the earlier choice onto an alternative.
+ func (c *CompiledRule) matchSessionAnyOrder(events []asymptoteobserve.Event) ([]asymptoteobserve.Event, error) {
+ // hits[s] lists, ascending, the indices of the events that satisfy step s.
+ hits := make([][]int, len(c.steps))
+ anchorable := make([]bool, len(events))
+ for s, prog := range c.steps {
+ for i := range events {
- matched, err := EvalMatch(prog, events[i])
++ matched, err := c.eval(prog, events[i])
+ if err != nil {
+ return nil, err
+ }
+ if matched {
+ hits[s] = append(hits[s], i)
+ anchorable[i] = true
+ }
+ }
+ if len(hits[s]) == 0 {
+ return nil, nil // some step never matches in this session
+ }
+ }
+ for start := range events {
+ if !anchorable[start] {
+ continue
+ }
+ if picked := c.assignFrom(events, hits, start); picked != nil {
+ sort.Ints(picked)
+ seq := make([]asymptoteobserve.Event, len(picked))
+ for k, i := range picked {
+ seq[k] = events[i]
+ }
+ return seq, nil
+ }
+ }
+ return nil, nil
+ }
+
+ // assignFrom assigns each step a distinct event at or after start and within the window
+ // of events[start], returning the chosen event indices (one per step, in step order), or
+ // nil when no complete assignment exists. It is Kuhn's augmenting-path matching, which is
+ // exact and cheap at the handful of steps a rule carries. The anchor is always part of the
+ // result: it is the earliest eligible hit of every step it satisfies, and augmenting only
+ // moves a step off an event when another step takes that event. So the window is measured
+ // from the anchor, and, as for a sequence, is not enforced when the anchor has no timestamp.
+ func (c *CompiledRule) assignFrom(events []asymptoteobserve.Event, hits [][]int, start int) []int {
+ startTime, startKnown := eventTime(events[start])
+ eligible := func(i int) bool {
+ if !startKnown {
+ return true
+ }
+ t, known := eventTime(events[i])
+ return !known || t.Sub(startTime) <= c.window
+ }
+ owner := make(map[int]int, len(c.steps)) // event index -> step holding it
+ picked := make([]int, len(c.steps))
+ var visited map[int]bool
+ var augment func(step int) bool
+ augment = func(step int) bool {
+ for _, i := range hits[step][sort.SearchInts(hits[step], start):] {
+ if !eligible(i) {
+ break // input is chronological: every later hit is further out
+ }
+ if visited[i] {
+ continue
+ }
+ visited[i] = true
+ if prev, taken := owner[i]; !taken || augment(prev) {
+ owner[i] = step
+ picked[step] = i
+ return true
+ }
+ }
+ return false
+ }
+ for step := range c.steps {
+ visited = make(map[int]bool)
+ if !augment(step) {
+ return nil
+ }
+ }
+ return picked
+ }
+
// groupBySession partitions events by session.id, preserving per-group input order and
// recording the order in which sessions first appear for deterministic evaluation.
func groupBySession(events []asymptoteobserve.Event) (map[string][]asymptoteobserve.Event, []string) {
diff --cc pkg/asymptoteobserve/threatrules/evaluator.go
index cb18c06a,e376273c..0de34a40
--- a/pkg/asymptoteobserve/threatrules/evaluator.go
+++ b/pkg/asymptoteobserve/threatrules/evaluator.go
@@@ -22,31 -21,11 +22,32 @@@ type Evaluator interface
// is built once (programs are compiled up front) and is safe to reuse across many
// Evaluate calls.
type CompiledRule struct {
- rule *Rule
- match cel.Program // set for single-event rules
- steps []cel.Program // set for correlation rules
- window time.Duration // parsed correlation window
+ rule *Rule
+ match cel.Program // set for single-event rules
+ steps []cel.Program // set for correlation rules
+ window time.Duration // parsed correlation window
+ anyOrder bool // correlation steps may match in any order
+ // derived is set when any of the rule's expressions references an engine-derived field
+ // (see withDerivedFields). Only those rules pay to derive it, so the rest of a pack runs
+ // exactly as it did before the field existed.
+ derived bool
+}
+
+// referencesDerivedField reports whether a match expression names an engine-derived field.
+// A textual check is enough: a false positive (the name inside a string literal) only costs
+// the derivation, and a false negative cannot happen because CEL has no other way to reach
+// the field.
+func referencesDerivedField(expr string) bool {
+ return strings.Contains(expr, "result_text")
+}
+
+// eval evaluates one compiled expression against one event, deriving fields first only for
+// rules that reference them.
+func (c *CompiledRule) eval(prog cel.Program, event asymptoteobserve.Event) (bool, error) {
+ if c.derived {
+ event = withDerivedFields(event)
+ }
+ return evalMatch(prog, event)
}
// Compile validates a rule and compiles its CEL expressions into a reusable evaluator.