simulation

package
v0.0.0-...-230ac96 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package simulation provides testing and simulation modes for agentmon.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SaveTestFile

func SaveTestFile(path string, tests []PolicyTestCase) error

SaveTestFile saves test cases to a YAML file.

Types

type Decision

type Decision string

Decision represents a policy decision.

const (
	DecisionAllow    Decision = "allow"
	DecisionDeny     Decision = "deny"
	DecisionApprove  Decision = "approve"
	DecisionRedirect Decision = "redirect"
	DecisionAudit    Decision = "audit"
)

type DecisionLogger

type DecisionLogger interface {
	LogDecision(op *Operation, decision Decision, wouldEnforce bool)
}

DecisionLogger logs decisions in various modes.

type Difference

type Difference struct {
	Event       RecordedEvent `json:"event"`
	OldDecision string        `json:"old_decision"`
	NewDecision string        `json:"new_decision"`
	OldRule     string        `json:"old_rule,omitempty"`
	NewRule     string        `json:"new_rule,omitempty"`
}

Difference describes a decision difference during replay.

type DryRunConfig

type DryRunConfig struct {
	Enabled      bool `json:"enabled" yaml:"enabled"`
	LogDecisions bool `json:"log_decisions" yaml:"log_decisions"`
}

DryRunConfig configures dry-run mode.

type ExpectedResult

type ExpectedResult struct {
	Decision   string `yaml:"decision" json:"decision"`
	PolicyRule string `yaml:"policy_rule,omitempty" json:"policy_rule,omitempty"`
	RedirectTo string `yaml:"redirect_to,omitempty" json:"redirect_to,omitempty"`
	Message    string `yaml:"message,omitempty" json:"message,omitempty"`
}

ExpectedResult defines the expected outcome of a test.

type Mode

type Mode string

Mode represents the current operational mode.

const (
	ModeNormal     Mode = "normal"
	ModeDryRun     Mode = "dry_run"
	ModeSimulation Mode = "simulation"
	ModePermissive Mode = "permissive"
	ModeStrict     Mode = "strict"
)

type ModeConfig

type ModeConfig struct {
	// DryRun logs what would happen without enforcing
	DryRun DryRunConfig `json:"dry_run" yaml:"dry_run"`

	// Simulation runs against recorded sessions
	Simulation SimulationConfig `json:"simulation" yaml:"simulation"`

	// Permissive allows all but logs everything
	Permissive PermissiveConfig `json:"permissive" yaml:"permissive"`

	// Strict denies by default
	Strict StrictConfig `json:"strict" yaml:"strict"`
}

ModeConfig defines simulation mode configuration.

func DefaultModeConfig

func DefaultModeConfig() ModeConfig

DefaultModeConfig returns sensible defaults.

type ModeManager

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

ModeManager manages operational modes.

func NewModeManager

func NewModeManager(config ModeConfig, logger DecisionLogger) *ModeManager

NewModeManager creates a new mode manager.

func (*ModeManager) Config

func (m *ModeManager) Config() ModeConfig

Config returns the current mode configuration.

func (*ModeManager) CurrentMode

func (m *ModeManager) CurrentMode() Mode

CurrentMode returns the current operational mode.

func (*ModeManager) DefaultDecision

func (m *ModeManager) DefaultDecision() Decision

DefaultDecision returns the default decision for the current mode.

func (*ModeManager) IsDryRun

func (m *ModeManager) IsDryRun() bool

IsDryRun returns whether dry-run mode is active.

func (*ModeManager) IsPermissive

func (m *ModeManager) IsPermissive() bool

IsPermissive returns whether permissive mode is active.

func (*ModeManager) IsSimulation

func (m *ModeManager) IsSimulation() bool

IsSimulation returns whether simulation mode is active.

func (*ModeManager) IsStrict

func (m *ModeManager) IsStrict() bool

IsStrict returns whether strict mode is active.

func (*ModeManager) ProcessDecision

func (m *ModeManager) ProcessDecision(op *Operation, decision Decision) Decision

ProcessDecision processes a decision based on the current mode.

func (*ModeManager) SetMode

func (m *ModeManager) SetMode(mode Mode)

SetMode changes the operational mode.

func (*ModeManager) ShouldEnforce

func (m *ModeManager) ShouldEnforce() bool

ShouldEnforce returns whether decisions should be enforced.

func (*ModeManager) ShouldLog

func (m *ModeManager) ShouldLog() bool

ShouldLog returns whether decisions should be logged.

type Operation

type Operation struct {
	Type        string            `json:"type"`
	Path        string            `json:"path,omitempty"`
	Domain      string            `json:"domain,omitempty"`
	Variable    string            `json:"variable,omitempty"`
	ProcessName string            `json:"process_name,omitempty"`
	Metadata    map[string]string `json:"metadata,omitempty"`
}

Operation represents an operation being evaluated.

type PermissiveConfig

type PermissiveConfig struct {
	Enabled bool `json:"enabled" yaml:"enabled"`
}

PermissiveConfig configures permissive mode.

type PolicyEvaluator

type PolicyEvaluator interface {
	Evaluate(op *Operation) TestResult
}

PolicyEvaluator evaluates operations against policies.

type PolicyTestCase

type PolicyTestCase struct {
	Name        string         `yaml:"name" json:"name"`
	Description string         `yaml:"description,omitempty" json:"description,omitempty"`
	Operation   TestOperation  `yaml:"operation" json:"operation"`
	Expected    ExpectedResult `yaml:"expected" json:"expected"`
}

PolicyTestCase represents a single policy test case.

func LoadTestFile

func LoadTestFile(path string) ([]PolicyTestCase, error)

LoadTestFile loads test cases from a YAML file.

type PolicyTestFile

type PolicyTestFile struct {
	Tests []PolicyTestCase `yaml:"tests" json:"tests"`
}

PolicyTestFile represents a file containing test cases.

type PolicyTester

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

PolicyTester runs policy tests.

func NewPolicyTester

func NewPolicyTester(evaluator PolicyEvaluator) *PolicyTester

NewPolicyTester creates a new policy tester.

func (*PolicyTester) RunTestDir

func (p *PolicyTester) RunTestDir(dir string) (*TestResults, error)

RunTestDir runs all test files in a directory.

func (*PolicyTester) RunTestFile

func (p *PolicyTester) RunTestFile(path string) (*TestResults, error)

RunTestFile runs tests from a file.

func (*PolicyTester) RunTests

func (p *PolicyTester) RunTests(tests []PolicyTestCase) *TestResults

RunTests runs a set of test cases.

type RecordedEvent

type RecordedEvent struct {
	Timestamp  time.Time       `json:"ts"`
	Type       string          `json:"type"`
	SessionID  string          `json:"session_id,omitempty"`
	AgentID    string          `json:"agent_id,omitempty"`
	Request    json.RawMessage `json:"request"`
	Decision   string          `json:"decision"`
	PolicyRule string          `json:"policy_rule,omitempty"`
	Latency    time.Duration   `json:"latency_ns"`
}

RecordedEvent represents a recorded operation event.

func LoadRecording

func LoadRecording(path string) ([]RecordedEvent, error)

LoadRecording loads all events from a recording file.

type RecorderConfig

type RecorderConfig struct {
	SessionID  string
	OutputPath string
	Append     bool
}

RecorderConfig configures the session recorder.

type RecordingStats

type RecordingStats struct {
	EventCount    int            `json:"event_count"`
	Duration      time.Duration  `json:"duration"`
	FirstEvent    time.Time      `json:"first_event"`
	LastEvent     time.Time      `json:"last_event"`
	EventsByType  map[string]int `json:"events_by_type"`
	DecisionCount map[string]int `json:"decisions"`
	SessionID     string         `json:"session_id,omitempty"`
}

RecordingStats returns statistics about a recording.

func GetRecordingStats

func GetRecordingStats(path string) (*RecordingStats, error)

GetRecordingStats returns statistics about a recording file.

type ReplayResults

type ReplayResults struct {
	TotalEvents int          `json:"total_events"`
	Matched     int          `json:"matched"`
	Differences []Difference `json:"differences,omitempty"`
	Errors      []string     `json:"errors,omitempty"`
}

ReplayResults contains the results of replaying a session.

func (*ReplayResults) HasDifferences

func (r *ReplayResults) HasDifferences() bool

HasDifferences returns true if there are any differences.

func (*ReplayResults) Summary

func (r *ReplayResults) Summary() string

Summary returns a human-readable summary.

type SessionRecorder

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

SessionRecorder records session events to a file.

func NewSessionRecorder

func NewSessionRecorder(config RecorderConfig) (*SessionRecorder, error)

NewSessionRecorder creates a new session recorder.

func NewSessionRecorderWriter

func NewSessionRecorderWriter(sessionID string, w io.WriteCloser) *SessionRecorder

NewSessionRecorderWriter creates a recorder with a custom writer.

func (*SessionRecorder) Close

func (r *SessionRecorder) Close() error

Close closes the recorder.

func (*SessionRecorder) Duration

func (r *SessionRecorder) Duration() time.Duration

Duration returns the recording duration.

func (*SessionRecorder) EventCount

func (r *SessionRecorder) EventCount() int

EventCount returns the number of recorded events.

func (*SessionRecorder) Record

func (r *SessionRecorder) Record(op *Operation, decision Decision, policyRule string, latency time.Duration) error

Record records an event.

func (*SessionRecorder) RecordRaw

func (r *SessionRecorder) RecordRaw(event RecordedEvent) error

RecordRaw records a raw event.

type SessionReplayer

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

SessionReplayer replays recorded sessions against a policy.

func NewSessionReplayer

func NewSessionReplayer(recordingPath string, evaluator PolicyEvaluator) *SessionReplayer

NewSessionReplayer creates a new session replayer.

func (*SessionReplayer) Replay

func (r *SessionReplayer) Replay() (*ReplayResults, error)

Replay replays the recorded session and compares decisions.

func (*SessionReplayer) ReplayReader

func (r *SessionReplayer) ReplayReader(reader io.Reader) (*ReplayResults, error)

ReplayReader replays from a reader.

type SimulationConfig

type SimulationConfig struct {
	Enabled    bool   `json:"enabled" yaml:"enabled"`
	ReplayFile string `json:"replay_file" yaml:"replay_file"`
}

SimulationConfig configures simulation/replay mode.

type StrictConfig

type StrictConfig struct {
	Enabled bool `json:"enabled" yaml:"enabled"`
}

StrictConfig configures strict mode.

type TestFailure

type TestFailure struct {
	TestName    string         `json:"test_name"`
	Description string         `json:"description,omitempty"`
	Operation   TestOperation  `json:"operation"`
	Expected    ExpectedResult `json:"expected"`
	Got         TestResult     `json:"got"`
	Error       string         `json:"error,omitempty"`
}

TestFailure describes a failed test.

type TestOperation

type TestOperation struct {
	Type        string            `yaml:"type" json:"type"`
	Path        string            `yaml:"path,omitempty" json:"path,omitempty"`
	Domain      string            `yaml:"domain,omitempty" json:"domain,omitempty"`
	Port        int               `yaml:"port,omitempty" json:"port,omitempty"`
	Variable    string            `yaml:"variable,omitempty" json:"variable,omitempty"`
	Command     string            `yaml:"command,omitempty" json:"command,omitempty"`
	ProcessName string            `yaml:"process_name,omitempty" json:"process_name,omitempty"`
	Metadata    map[string]string `yaml:"metadata,omitempty" json:"metadata,omitempty"`
}

TestOperation defines the operation to test.

func (*TestOperation) ToOperation

func (t *TestOperation) ToOperation() *Operation

ToOperation converts TestOperation to Operation.

type TestResult

type TestResult struct {
	Decision   string `json:"decision"`
	PolicyRule string `json:"policy_rule,omitempty"`
	RedirectTo string `json:"redirect_to,omitempty"`
	Message    string `json:"message,omitempty"`
}

TestResult is the actual result from evaluation.

type TestResults

type TestResults struct {
	Passed  int           `json:"passed"`
	Failed  int           `json:"failed"`
	Skipped int           `json:"skipped"`
	Total   int           `json:"total"`
	Errors  []TestFailure `json:"errors,omitempty"`
}

TestResults contains the results of running policy tests.

func (*TestResults) Success

func (r *TestResults) Success() bool

Success returns true if all tests passed.

func (*TestResults) Summary

func (r *TestResults) Summary() string

Summary returns a human-readable summary of test results.

Jump to

Keyboard shortcuts

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