Skip to content

16 Invariants

Mikhail Deynekin edited this page Nov 29, 2026 · 1 revision

Invariants

A constraint written as a fact: a key, a statement, a machine probe and a path:line origin that must exist. The set is append-only, removal takes two parts, verification reads the whole state, and a violation rolls back the whole declared mutation.

Edition: Pro (invariant-pin, six operations). Operations: invariant_pin, invariant_release, invariant_unpin, invariant_list, invariant_verify, invariant_mutation.

Why it exists

Implicit constraints are the part of a system that no test names. A sequence of small edits can drop one without any file looking wrong: the fifth change breaks something it never touched. Recording the constraint as a sentence is not enough — a sentence does not fail. Recording it as a test is better, but a test can be deleted quietly along with the code it guards.

An invariant is a fact with three parts that cannot be faked: what is asserted, how it is checked, and where in the corpus the assertion comes from. The origin must exist; a pin that points nowhere is refused.

What the set guarantees

  • Append-only is enforced by the database, not by convention. The invariant row cannot be updated or deleted: triggers refuse. A second pin on the same key is refused too.
  • Removal takes two parts. invariant_release records that the constraint is gone from the system, with a reason and a place. invariant_unpin then removes the entry — and it refuses without the first part, refuses while the probe still holds (constraint_present), and refuses when there is no verdict (unverifiable_constraint). retire is a separate fact: "this was never an invariant", kept in the journal.
  • Verification reads the state, not a diff. invariant_verify runs every probe against the current corpus: symbol existence, an edge with its direction checked, the corpus checksum, a declared script, or an honest manual. There is no "list of changed files" parameter — a diff answers a different question.
  • Verdicts are named: hold, partial, broken, empty. Two runs on unchanged state produce the same digest.
  • A violation rolls back the whole area. invariant_mutation declares an area by patterns, snapshots it under .ragraf/mutations/, verifies after the change, and on a violation restores the WHOLE area rather than repairing the place that broke. Files changed outside the declared area are named (outside_scope), and the status becomes partial_rollback — an honest partial, not a success.

What it does not do

  • It does not decide what matters: a probe that asserts nothing useful passes forever.
  • It does not replace tests: a test proves behaviour on a case, an invariant proves a property of the state.
  • It does not repair: rollback returns the declared area to the snapshot; the change itself is the author's business.

How to use it

invariant_pin key=graph.direction statement="M·v gives predecessors" probe={kind: symbol, name: _neighbors} anchor="src/ragraf/graph_algo.py:1"
invariant_verify
invariant_mutation action=begin paths=["src/ragraf/graph_algo.py"]
# ... change ...
invariant_mutation action=end mutation_id=<id>

Details and limits: docs/03_implementation/INVARIANTS.md in the repository.

Clone this wiki locally