Skip to content

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

3.9 KB
Raw Blame
package threatrules

import (
	"fmt"
	"time"

	"github.com/google/cel-go/cel"

	"github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
)

// Evaluator produces a Verdict for an ordered event sequence. A CompiledRule is the
// reference implementation; a separate (closed) engine satisfies the conformance
// contract by implementing this interface and returning the verdict each fixture
// declares.
type Evaluator interface {
	Evaluate(events []asymptoteobserve.Event) (Verdict, error)
}

// CompiledRule is a validated, CEL-compiled rule ready to evaluate event sequences. It
// is built once (programs are compiled up front) and is safe to reuse across many
// Evaluate calls.
type CompiledRule struct {
	rule     *Rule
	match    matcher       // set for single-event rules
	steps    []matcher     // 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
}

// evalObserver, when set, is called once per expression evaluation. It is nil outside tests,
// which set it to count how much work a rule does without timing it.
var evalObserver func()

// eval evaluates one compiled expression against one event, deriving fields first only for
// rules that reference them. An expression that cannot be true for the event's action is not
// evaluated (see eventActions).
func (c *CompiledRule) eval(m matcher, event asymptoteobserve.Event) (bool, error) {
	if m.excludes(event.Event.Action) {
		return false, nil
	}
	if c.derived {
		event = withDerivedFields(event)
	}
	act := newEventActivation(event)
	return evalObserved(m.program(act), act)
}

// evalObserved evaluates one compiled expression against an activation whose event the caller
// has already derived, if the rule needs it.
func evalObserved(prog cel.Program, act *eventActivation) (bool, error) {
	if evalObserver != nil {
		evalObserver()
	}
	return evalMatch(prog, act)
}

// Compile validates a rule and compiles its CEL expressions into a reusable evaluator.
// Validation (including CEL type-checking) runs first, so Compile fails on any rule that
// would not load.
func Compile(rule *Rule) (*CompiledRule, error) {
	if err := rule.Validate(); err != nil {
		return nil, err
	}
	c := &CompiledRule{rule: rule, derived: referencesDerivedField(rule.Match)}
	if rule.Correlation != nil {
		c.steps = make([]matcher, len(rule.Correlation.Steps))
		for i, step := range rule.Correlation.Steps {
			c.derived = c.derived || referencesDerivedField(step.Match)
			m, err := compileMatcher(step.Match)
			if err != nil {
				return nil, fmt.Errorf("correlation.steps[%d] (%s): %w", i, step.ID, err)
			}
			c.steps[i] = m
		}
		window, err := rule.Correlation.ParseWindow()
		if err != nil {
			return nil, fmt.Errorf("correlation.window: %w", err)
		}
		c.window = window
		c.anyOrder = rule.Correlation.AnyOrder()
		return c, nil
	}
	m, err := compileMatcher(rule.Match)
	if err != nil {
		return nil, fmt.Errorf("match: %w", err)
	}
	c.match = m
	return c, nil
}

// Rule returns the underlying rule.
func (c *CompiledRule) Rule() *Rule { return c.rule }

// Evaluate reports whether the rule fires over the given ordered event sequence.
//
// A single-event rule matches if any event in the sequence satisfies its expression. A
// correlation rule matches if any session (events grouped by session.id) satisfies the
// windowed steps, in listed order unless the rule declares order: any.
func (c *CompiledRule) Evaluate(events []asymptoteobserve.Event) (Verdict, error) {
	if c.steps != nil {
		return c.evaluateCorrelation(events)
	}
	for i := range events {
		matched, err := c.eval(c.match, events[i])
		if err != nil {
			return "", err
		}
		if matched {
			return VerdictMatch, nil
		}
	}
	return VerdictNoMatch, nil
}