policy

package
v1.21.1 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: GPL-3.0 Imports: 9 Imported by: 0 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 policy implements the declarative merge-policy gate behind the `commitbrief guard` command (ADR-0029). A policy is a small YAML document (`.commitbrief/policy.yml`) that caps how many findings of each severity a change may carry before it is blocked — a richer, opt-in alternative to the single `--fail-on=<severity>` threshold, aimed at gating high-volume (AI-authored) pull requests.

The gate evaluates the *actionable* finding set — the findings that survive baseline + inline suppression (signal control, ADR-0027) — because that is exactly what `--json` emits and what a human reviewer sees. Rule-id-scoped allow/deny lists (`rules:`, ADR-0045) key on the closed rule taxonomy id each finding carries: `deny` blocks on any matching finding, `allow` waives the matching findings (they are excluded from the thresholds and `total` and reported as `waived`). Entries are validated against the taxonomy at load time, so a typo is a hard error and never silently opens the gate.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("policy file not found")

ErrNotFound is returned by Load when the policy file does not exist.

Functions

This section is empty.

Types

type Policy

type Policy struct {
	// Version is the policy schema version. Only 1 is understood today.
	Version int `yaml:"version"`
	// Thresholds maps a severity ("critical".."info") to the maximum number
	// of findings of that severity allowed before the gate blocks. A severity
	// absent from the map — or present with a null/`~` value — is unlimited.
	Thresholds map[render.Severity]*int `yaml:"thresholds"`
	// Total is an optional cap on the total number of actionable findings.
	// nil means no overall cap.
	Total *int `yaml:"total"`
	// Rules is the optional rule-id scoped allow/deny block (ADR-0045).
	Rules RuleLists `yaml:"rules"`
}

Policy is the parsed `.commitbrief/policy.yml` document.

func Load

func Load(path string) (*Policy, error)

Load reads and validates a policy file. A missing file is reported as a distinct error (errors.Is(err, ErrNotFound)) so the caller can tell "no policy configured" from "policy is malformed".

func Parse

func Parse(data []byte) (*Policy, error)

Parse decodes + validates a policy document. Unknown keys are rejected so a typo (e.g. "threshold:") is a hard error, never a silent no-op.

func (*Policy) Evaluate

func (p *Policy) Evaluate(findings []render.Finding) Verdict

Evaluate counts the findings by severity and checks them against the policy. findings must be the actionable set (post baseline + suppression). Findings waived by rules.allow are excluded from Counts, Total and every limit and reported in Waived. The returned Verdict is deterministic: Counts always has all five severities, and Violations are ordered critical→info, then "total", then one "rule:<id>" per denied finding, alphabetically by id.

func (*Policy) Offending added in v1.21.0

func (p *Policy) Offending(findings []render.Finding) []render.Finding

Offending returns the findings that cause a violation, in input order: those at a severity whose cap is exceeded, those matching rules.deny, and -- when the total cap is exceeded -- every non-waived finding (no single finding is to blame for an overflow, so all of them are). Findings waived by rules.allow are never returned. A passing policy yields nil. It shares evaluate with Evaluate, so the two can never disagree about what failed.

type RuleLists added in v1.19.0

type RuleLists struct {
	// Deny: any actionable finding matching an entry is a violation,
	// whatever the thresholds say.
	Deny []string `yaml:"deny"`
	// Allow: findings matching an entry are waived — excluded from the
	// thresholds and total, counted in Verdict.Waived. Deny wins over allow
	// when both match a finding.
	Allow []string `yaml:"allow"`
}

RuleLists is the `rules:` block. Each entry is a canonical taxonomy id (`sec.injection`) or a category wildcard (`sec.*`); nothing else.

type Verdict

type Verdict struct {
	Passed     bool           `json:"passed"`
	Counts     map[string]int `json:"counts"`
	Total      int            `json:"total"`
	Violations []Violation    `json:"violations"`
	// Waived is the number of findings an `allow` entry exempted from the
	// thresholds and Total. Counts and Total describe only the remainder.
	Waived int `json:"waived"`
}

Verdict is the result of evaluating findings against a Policy.

type Violation

type Violation struct {
	// Severity is the breached severity, "total" for the overall cap, or
	// "rule:<id>" for a denied rule, where <id> is the canonical rule id of
	// the offending finding (never the policy entry, so a "sec.*" entry
	// yields "rule:sec.injection"). One finding is one such violation, with
	// Allowed 0 and Actual 1 (ADR-0045 §6, ADR-0029 §Update).
	Severity string `json:"severity"`
	// Allowed is the configured maximum; Actual is the observed count.
	Allowed int `json:"allowed"`
	Actual  int `json:"actual"`
}

Violation is a single breached limit.

Jump to

Keyboard shortcuts

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