GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Why Go
  • Case Studies
  • Use Cases
  • Security
  • Learn
  • Effective Go
  • Go User Manual
  • Standard library
  • Release Notes
  • API
  • Packages
  • Recorded Talks
  • Meetups
  • Conferences

This view

  • Original page
  • New address

GeezerVison is showing pkg.go.dev — engine package - github.com/majorcontext/keep/internal/engine - Go Packages

Details

  • checked Valid go.mod file

    The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.

  • checked Redistributable license

    Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.

  • checked Tagged version

    Modules with tagged versions give importers more predictable builds.

  • unchecked Stable version

    When a project reaches major version v1 it is considered stable.

  • Learn more about best practices

Repository

github.com/majorcontext/keep

Links

  • Open Source Insights Logo Open Source Insights

Documentation ¶

Overview ¶

Package engine implements Keep's core policy evaluation.

Index ¶

  • Constants
  • func GlobMatch(pattern, name string) bool
  • type AuditEntry
  • type Call
  • type CallContext
  • type Decision
  • type EvalResult
  • type Evaluator
    • func NewEvaluator(celEnv *keepcel.Env, scope string, mode config.Mode, onError config.ErrorMode, ...) (*Evaluator, error)
    • func (ev *Evaluator) Evaluate(ctx context.Context, call Call) EvalResult
    • func (ev *Evaluator) RequiresBody() bool
    • func (ev *Evaluator) RequiresBodyFor(operation string) bool
    • func (ev *Evaluator) SetJudgeFunc(fn JudgeHandler)
  • type JudgeAudit
  • type JudgeHandler
  • type JudgeResult
  • type JudgeUsage
  • type RedactedField
  • type RuleResult

Constants ¶

View Source
const ParamBody = "body"

ParamBody is the params key under which the request body is exposed to rules (params.body). It is the single source of truth shared by the call helpers that populate the body and the analysis that detects references to it.

Variables ¶

This section is empty.

Functions ¶

func GlobMatch ¶

func GlobMatch(pattern, name string) bool

GlobMatch returns true if the operation name matches the glob pattern. Supports * (any sequence) and ? (any single character). An empty pattern matches everything.

Types ¶

type AuditEntry ¶

type AuditEntry struct {
	Timestamp      time.Time
	Scope          string
	Operation      string
	AgentID        string
	UserID         string
	Direction      string
	Decision       Decision
	Rule           string
	Message        string
	RulesEvaluated []RuleResult
	ParamsSummary  string
	// Enforced is true when the Decision was actually applied to the call.
	// It is false in audit_only mode, where Decision records what would have
	// happened but the call is allowed regardless.
	Enforced      bool
	RedactSummary []RedactedField `json:",omitempty"`
}

AuditEntry is the structured log record for a single evaluation.

type Call ¶

type Call struct {
	Operation string
	Params    map[string]any
	Context   CallContext
}

Call is the normalized input to the policy engine.

type CallContext ¶

type CallContext struct {
	AgentID   string
	UserID    string
	Timestamp time.Time
	Scope     string
	Direction string
	Labels    map[string]string
}

CallContext is metadata about who is making the call and when.

type Decision ¶

type Decision string

Decision is the outcome of a policy evaluation.

const (
	Allow  Decision = "allow"
	Deny   Decision = "deny"
	Redact Decision = "redact"
)

type EvalResult ¶

type EvalResult struct {
	Decision  Decision
	Rule      string
	Message   string
	Mutations []redact.Mutation
	Audit     AuditEntry
}

EvalResult is the output of a policy evaluation.

type Evaluator ¶

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

Evaluator runs the rule evaluation loop for a single scope.

func NewEvaluator ¶

func NewEvaluator(
	celEnv *keepcel.Env,
	scope string,
	mode config.Mode,
	onError config.ErrorMode,
	rules []config.Rule,
	aliases map[string]string,
	defs map[string]string,
	detector *secrets.Detector,
	caseSensitive bool,
) (*Evaluator, error)

NewEvaluator creates an evaluator for a scope. Compiles all CEL expressions and redact patterns at creation time. Returns an error if any expression fails to compile.

func (*Evaluator) Evaluate ¶

func (ev *Evaluator) Evaluate(ctx context.Context, call Call) EvalResult

Evaluate runs all rules against the given call and returns the result.

func (*Evaluator) RequiresBody ¶ added in v0.5.0

func (ev *Evaluator) RequiresBody() bool

RequiresBody reports whether any compiled rule in this scope references the request body (params.body). Callers can use this to decide whether the (potentially expensive) request body needs to be buffered and supplied before evaluation. The answer is fixed at compile time and precomputed in NewEvaluator, so this is an O(1) lookup safe to call per request.

func (*Evaluator) RequiresBodyFor ¶ added in v0.7.0

func (ev *Evaluator) RequiresBodyFor(operation string) bool

RequiresBodyFor reports whether evaluating a call with the given operation needs the request body. It narrows RequiresBody by each body rule's operation glob: a rule that references params.body only counts when its operation matches (a body rule with no operation counts for every call). The operation is normalized exactly as Evaluate normalizes it, so a pinned rule like `operation: "post api.example.com/graphql"` matches regardless of the case the caller used for method or host.

Gatekeepers use this to buffer bodies only for the requests a body rule can actually inspect, instead of every request in the scope.

func (*Evaluator) SetJudgeFunc ¶ added in v0.4.0

func (ev *Evaluator) SetJudgeFunc(fn JudgeHandler)

SetJudgeFunc sets the judge handler for this evaluator.

type JudgeAudit ¶ added in v0.4.0

type JudgeAudit struct {
	Model     string     `json:"model"`
	Verdict   string     `json:"verdict"`
	Reason    string     `json:"reason"`
	Cached    bool       `json:"cached,omitempty"`
	LatencyMS int64      `json:"latency_ms"`
	Usage     JudgeUsage `json:"usage"`
	Error     string     `json:"error,omitempty"`
}

JudgeAudit records the result of a judge call.

type JudgeHandler ¶ added in v0.4.0

type JudgeHandler func(ctx context.Context, model, prompt, content string) (JudgeResult, error)

JudgeHandler is the function signature for judge evaluation.

type JudgeResult ¶ added in v0.4.0

type JudgeResult struct {
	Decision     string
	Reason       string
	InputTokens  int
	OutputTokens int
	Cached       bool
}

JudgeResult holds the raw result from a judge call at the engine level.

type JudgeUsage ¶ added in v0.4.0

type JudgeUsage struct {
	InputTokens  int `json:"input_tokens"`
	OutputTokens int `json:"output_tokens"`
}

JudgeUsage tracks token consumption for a judge call.

type RedactedField ¶

type RedactedField struct {
	Path     string // e.g. "params.text"
	Replaced string // the post-redaction value (contains [REDACTED:...] placeholders)
}

RedactedField records what was redacted without exposing the original value.

type RuleResult ¶

type RuleResult struct {
	Name         string
	Matched      bool
	Action       string
	Skipped      bool
	Error        bool        // true if a CEL eval error occurred for this rule
	ErrorMessage string      // the CEL eval error message, populated when Error is true
	Judge        *JudgeAudit `json:"judge,omitempty"`
}

RuleResult records what happened when a single rule was checked.

Source Files ¶

View all Source files
  • eval.go
  • glob.go
  • normalize.go
Click to show internal directories.
Click to hide internal directories.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.