contracts

package
v1.0.545 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 12 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 contracts loads and checks the product contracts registry (contracts/registry.yaml): the list of customer-observable invariants, the code that implements each one, and the tests that must fail if it breaks.

It answers two questions, both mechanically:

  • Validate: is the registry itself sound? Every enforced contract names tests that exist and are not skipped, and code paths that exist.
  • Impact: comparing a base revision's registry to the head tree, was a contract weakened? The BASE registry is the reference, so editing the registry in the same change as the implementation cannot hide it.

The package makes no decision about whether a contract change is right. It only makes the change impossible to land unnoticed: an unrecorded weakening fails, and a recorded one is listed in the gate summary with its reason. No human approval is part of the gate. See docs/engineering/PRODUCT-CONTRACTS.md.

Index

Constants

View Source
const (
	StatusEnforced = "enforced"
	StatusOpen     = "open"
)

Status values. A contract is either enforced by tests or open: recorded because it matters, but contradictory or undecided, so nothing may claim it as an approved rule yet.

Variables

This section is empty.

Functions

func Validate

func Validate(r *Registry, tree fs.FS) []string

Validate checks the registry against the tree. It returns every problem found, sorted, so one run reports them all.

Types

type Contract

type Contract struct {
	ID            string      `yaml:"id"`
	Area          string      `yaml:"area"`
	Invariant     string      `yaml:"invariant"`
	Status        string      `yaml:"status"`
	OwnerApproved bool        `yaml:"owner_approved"`
	Authority     string      `yaml:"authority"`
	Code          []string    `yaml:"code"`
	Configs       []string    `yaml:"configs"`
	Transitions   Transitions `yaml:"transitions"`
	Tests         []TestRef   `yaml:"tests"`
	Validation    string      `yaml:"validation"`
	Compatibility string      `yaml:"compatibility"`
	Question      string      `yaml:"question"`
}

Contract is one registry entry.

type Impact

type Impact struct {
	// Touched lists contracts whose code paths include a changed file. It is
	// information for the reviewer, not a failure.
	Touched []string
	// Weakened lists contract changes that happened without an explicit
	// record. Always a failure.
	Weakened []string
	// Recorded lists intentional changes, reported in the summary but never a
	// failure: contracts changed or removed with a new amended/retired record,
	// and edits to protected paths (the gate's own files).
	Recorded []string
}

Impact is the result of comparing a base registry with the head tree.

func Assess

func Assess(base, head *Registry, tree fs.FS, changed []string) Impact

Assess compares base (the registry at the merge base; nil when the base has none) with head and the head tree. changed is the list of files the change touches, relative to the repository root.

type Record

type Record struct {
	ID     string `yaml:"id"`
	Reason string `yaml:"reason"`
}

Record is an explicit, reviewable statement that a contract was changed or removed on purpose.

type Registry

type Registry struct {
	Version   int        `yaml:"version"`
	Protected []string   `yaml:"protected"`
	Contracts []Contract `yaml:"contracts"`
	Amended   []Record   `yaml:"amended"`
	Retired   []Record   `yaml:"retired"`
}

Registry is the whole file.

func Parse

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

Parse decodes a registry. Unknown fields are an error, so a typo in a field name cannot silently drop a requirement.

type TestRef

type TestRef struct {
	File string `yaml:"file"`
	Name string `yaml:"name"`
}

TestRef names one test function that must fail if the contract breaks.

type Transitions

type Transitions struct {
	Permitted []string `yaml:"permitted"`
	Forbidden []string `yaml:"forbidden"`
}

Transitions lists the state changes a contract allows and forbids.

Jump to

Keyboard shortcuts

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