engine

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package engine implements the rule checking engine for Guardian. It provides a registry of rule checkers, a unified orchestrator, and diff parsing utilities.

Index

Constants

This section is empty.

Variables

View Source
var Registry = map[string]RuleChecker{}

Registry maps rule type names to their corresponding checkers.

Functions

func RegisterChecker

func RegisterChecker(c RuleChecker)

RegisterChecker registers a RuleChecker in the global registry by its type name.

Types

type CheckContext

type CheckContext struct {
	ChangedFiles []string
	DiffContent  string
	RuleConfig   map[string]interface{}
	Severity     string
	RuleID       string
	RuleDesc     string
}

CheckContext provides all the information a RuleChecker needs to evaluate a rule.

type DiffPatternForbiddenChecker

type DiffPatternForbiddenChecker struct{}

DiffPatternForbiddenChecker checks that added lines in the diff do not match any of the configured forbidden regular expressions. Optionally scoped to specific file path patterns via only_in_paths.

func (*DiffPatternForbiddenChecker) Check

Check evaluates the diff_pattern_forbidden rule against the given context.

func (*DiffPatternForbiddenChecker) Type

Type returns the checker type identifier.

type DiffPatternRequiresChecker

type DiffPatternRequiresChecker struct{}

DiffPatternRequiresChecker checks that when files matching only_in_paths are changed, at least one of the required_regexes patterns appears somewhere in the added lines of the diff. If none match, a violation is returned.

func (*DiffPatternRequiresChecker) Check

Check evaluates the diff_pattern_requires rule against the given context.

func (*DiffPatternRequiresChecker) Type

Type returns the checker type identifier.

type Engine

type Engine struct {
	Rules      []config.Rule
	Exceptions []config.Exception
}

Engine orchestrates rule checking by running all configured rules against the given diff and filtering out exceptions.

func NewEngine

func NewEngine(rules []config.Rule, exceptions []config.Exception) *Engine

NewEngine creates a new Engine with the given rules and exceptions.

func (*Engine) Run

func (e *Engine) Run(changedFiles []string, diffContent string) (*EngineResult, error)

Run executes all registered rule checkers against the changed files and diff content, filters out exceptions, and returns the aggregated result.

type EngineResult

type EngineResult struct {
	Violations []Violation
	Errors     int
	Warnings   int
}

EngineResult holds the aggregated results of running all rules.

type FileDiff

type FileDiff struct {
	Path       string
	AddedLines []string // lines starting with "+" (without the leading "+")
}

FileDiff represents the diff for a single file.

func ParseDiff

func ParseDiff(diffContent string) []FileDiff

ParseDiff parses unified diff content into per-file diffs. Each entry in the returned slice corresponds to one file in the diff and contains the file path and all added lines (lines prefixed with "+", excluding the "+++ b/" header).

type ImportsForbiddenChecker

type ImportsForbiddenChecker struct{}

ImportsForbiddenChecker checks that files matching from_globs do not contain imports from paths matching forbid_globs. It works by inspecting added lines in the diff for path segments derived from the forbidden glob patterns.

func (*ImportsForbiddenChecker) Check

Check evaluates the imports_forbidden rule against the given context.

func (*ImportsForbiddenChecker) Type

func (c *ImportsForbiddenChecker) Type() string

Type returns the checker type identifier.

type MetaChecker

type MetaChecker struct{}

MetaChecker detects unauthorized changes to governance files (.agreements/constitution.yml and .agreements/rules.yml). Changes to these files without a corresponding accepted proposal produce a violation.

func (*MetaChecker) Check

func (m *MetaChecker) Check(changedFiles []string, proposalsDir string) ([]Violation, error)

Check inspects the changed files for protected governance files and verifies that a corresponding accepted proposal exists in the proposals directory.

type RuleChecker

type RuleChecker interface {
	// Check evaluates the rule against the given context and returns any violations found.
	Check(ctx *CheckContext) ([]Violation, error)
	// Type returns the unique identifier for this checker type (e.g., "imports_forbidden").
	Type() string
}

RuleChecker is the interface that all rule type checkers must implement.

type Violation

type Violation struct {
	RuleID         string `json:"rule_id"`
	Severity       string `json:"severity"`
	Description    string `json:"description"`
	FilePath       string `json:"file_path"`
	DiffSnippet    string `json:"diff_snippet"`
	LLMExplanation string `json:"llm_explanation"`
}

Violation represents a single rule violation found during checking.

Jump to

Keyboard shortcuts

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