Enforcing Invariants in AI-Generated Code with ADRs and Contracts
The article discusses using Architecture Decision Records (ADRs) and contract-based programming to enforce invariants in AI-generated code, ensuring that code produced by large language models adheres to architectural constraints and behavioral guarantees.
Background
- The article discusses using **Architecture Decision Records (ADRs)** and **contract-style coding** (like Design by Contract) to enforce invariants—conditions that must always hold true—in AI-generated code.
- ADRs are short documents that capture important architectural decisions and their rationale; they originated in software architecture practice as a lightweight way to keep a project's design reasoning visible.
- "Contracts" refer to a programming approach (popularized by languages like Eiffel and later adopted in tools like C++ contracts or Python's PyContracts) where code explicitly states preconditions, postconditions, and invariants—essentially formal guarantees about behavior.
- The core problem: AI coding assistants (like GitHub Copilot, Cursor, or Claude-generated code) produce plausible-looking code that often violates subtle business rules or system invariants, leading to bugs that are hard to catch.
- This piece argues that explicitly recording design invariants in ADRs, then encoding them as runtime-checkable contracts, gives developers a way to catch AI errors automatically—rather than relying on code review alone.