policy

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package policy evaluates immutable, side-effect-free automation rules for semantic agent events. It deliberately does not encode or deliver a decision: callers must fall back to a human whenever an adapter cannot represent the proposed action or a delivery fails.

Package policy evaluates immutable, side-effect-free automation rules for semantic agent events. It deliberately does not encode or deliver a decision: callers must fall back to a human whenever an adapter cannot represent the proposed action or a delivery fails.

Index

Constants

View Source
const (
	ReasonDefault          = "default_action"
	ReasonRule             = "rule_match"
	ReasonInvalidEvent     = "invalid_event"
	ReasonNonActionable    = "non_actionable"
	ReasonSensitive        = "sensitive_event"
	ReasonRisk             = "risk_not_low"
	ReasonDryRun           = "dry_run"
	ReasonNoEngine         = "engine_unavailable"
	ReasonConsecutiveLimit = "consecutive_auto_limit"
	ReasonRateLimit        = "rate_limit_exceeded"
	ReasonDestructive      = "destructive_command_blocked"
	ReasonExfiltration     = "exfiltration_attempt_blocked"
	ReasonGuardrailBlocked = "guardrail_pattern_blocked"
)

Static evaluation reasons are safe to expose in logs. They never contain event text, regex matches, metadata, or other terminal output.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action string

Action is the outcome selected by the policy engine.

const (
	ActionAllow Action = "allow"
	ActionAsk   Action = "ask"
	ActionDeny  Action = "deny"
)

type Config

type Config struct {
	DefaultAction               Action
	DryRun                      bool
	MaxConsecutiveAutoDecisions int
	RateLimitPerMinute          int
	Guardrails                  GuardrailsConfig
	Rules                       []Rule
}

Config describes the ordered rules evaluated by an Engine.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig preserves Relayer's human-in-the-loop behavior.

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine is immutable after construction and safe for concurrent evaluation.

func New

func New(config Config) (*Engine, error)

New validates and defensively copies a policy configuration. Regexes are compiled once so Evaluate remains deterministic and allocation-light.

func (*Engine) Config

func (e *Engine) Config() Config

Config returns a deep copy that cannot mutate the engine.

func (*Engine) Evaluate

func (e *Engine) Evaluate(event adapters.Event) Evaluation

Evaluate applies the first matching rule without mutating either the engine or event. Invalid and non-actionable events can never become automatic.

type Evaluation

type Evaluation struct {
	Action         Action
	ProposedAction Action
	RuleName       string
	Reason         string
	EventID        string
	Automatic      bool
	DryRun         bool
}

Evaluation separates the configured proposal from the effective action. Sensitive events and dry-run configurations retain ProposedAction for a safe audit record while forcing ActionAsk and disabling automation.

type GuardrailsConfig

type GuardrailsConfig struct {
	BlockDestructive  bool
	BlockExfiltration bool
	BlockedPatterns   []string
}

GuardrailsConfig defines safety boundaries that prevent autonomous execution of high-risk actions even when a policy rule would otherwise allow them.

type Match

type Match struct {
	EventTypes []adapters.EventType
	TextRegex  string
	AgentIDs   []string
	RiskLevels []adapters.RiskLevel
	Sensitive  *bool
}

Match combines fields with AND semantics. Values inside each list use OR semantics. TextRegex is evaluated against Summary + "\n" + Match but that text is never retained by Engine or copied into Evaluation.

type Rule

type Rule struct {
	Name   string
	Match  Match
	Action Action
}

Rule applies Action when every populated Match field accepts an event. Rules are evaluated in order and the first match wins.

type Tracker

type Tracker struct {
	// contains filtered or unexported fields
}

Tracker manages session-level execution history for policy guardrails, including consecutive automatic decisions and sliding-window rate limits. It is thread-safe and safe for concurrent calls across different sessions.

func NewTracker

func NewTracker() *Tracker

NewTracker creates a Tracker using the system clock.

func NewTrackerWithClock

func NewTrackerWithClock(clock func() time.Time) *Tracker

NewTrackerWithClock creates a Tracker with a custom clock function, primarily for deterministic unit testing.

func (*Tracker) CheckLimits

func (t *Tracker) CheckLimits(sessionID string, config Config) (Action, string, bool)

CheckLimits checks whether an automatic decision is allowed for the given sessionID against the configured MaxConsecutiveAutoDecisions and RateLimitPerMinute. If a limit is exceeded, it returns (ActionAsk, reason, false). If limits are respected, it returns (ActionAllow, "", true).

func (*Tracker) Consecutive

func (t *Tracker) Consecutive(sessionID string) int

Consecutive returns the current consecutive automatic decision count for the session.

func (*Tracker) RecordAutoDecision

func (t *Tracker) RecordAutoDecision(sessionID string)

RecordAutoDecision registers that an automatic decision was delivered for the session. It increments the consecutive counter and appends the current timestamp to the history window.

func (*Tracker) RecordHumanDecision

func (t *Tracker) RecordHumanDecision(sessionID string)

RecordHumanDecision registers that an operator intervened for the session. It resets the consecutive counter to 0. Rate limiting timestamps remain intact.

func (*Tracker) Reset

func (t *Tracker) Reset(sessionID string)

Reset clears all consecutive counters and history for the specified session.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL