An invariant is a property that must remain true across specified inputs, program states, or operations. For AI-generated code, invariants turn vague confidence into checkable obligations: the parser returns a string, tags remain balanced, and links are neither lost nor duplicated. section 8.4.2 section 8.5.1
Use invariants as constraints, while separating them from related properties:
- Test universal behavior regardless of input, such as never crashing or always producing valid HTML. section 8.4.2
- Pair invariants with metamorphic relations, which specify how output should change when input changes. section 8.4.2
- Use conservation laws when a quantity should be preserved, such as input link count equaling output href count. section 8.4.2
- Express boundary assumptions as preconditions, postconditions, and invariants so violations fail immediately and visibly. section 8.5.1
Parser output must stay structurally balanced
A property-based test can generate arbitrary text and assert that every opening strong, emphasis, heading, or anchor tag has a matching closing tag. A failure identifies a structural guarantee the implementation violated, rather than merely exposing a missed example. section 8.4.2
Passing invariant tests is evidence that specified properties hold, not proof that the implementation is correct. Broader scrutiny may require adversarial property tests, performance and security checks, or formal verification; formal methods add specification and annotation costs, and their guarantees depend on the specifications being right. section 8.3.7 section 8.6.4

















