Heuristic:1ay1 Agentty Compile Time Proofs
| Knowledge Sources | |
|---|---|
| Domains | Type_Safety, C_Plus_Plus |
| Last Updated | 2026-10-28 14:00 GMT |
Overview
Use C++ templates, concepts, and constexpr to prove correctness at compile time, so that category errors and invariant violations are caught by the compiler, not by crashes.
Description
The Compile_Time_Proofs heuristic pushes correctness checks to compile time using C++20 concepts, `constexpr`/`consteval`, template constraints, and type-level encoding. Instead of a runtime assertion that fires in production, the code simply does not compile if the invariant is violated. This includes: concepts that constrain template parameters to the exact interface they need, `constexpr` functions that evaluate configuration at build time, and type-level encodings (like lock ordering levels) that make violations a type error.
In agentty this appears in: refinement types (`refined.hpp`) with `constexpr` validators; the typestate IO lifecycle (`fsm.hpp`) with phantom-type-encoded states; settings registry (`settings_registry.hpp`) with `consteval` key validation; lock ordering types (`lock_levels.hpp`) with type-level lock levels; and the PowerShell command encoder (`win_shell_encode.hpp`) with `constexpr` encoding.
Usage
Apply this heuristic when an invariant can be expressed as a type constraint or a `constexpr` computation. If a runtime check is guarding something the compiler could check, move it to compile time. The cost is longer compile times and more complex template code; the benefit is that the bug class is eliminated, not just detected.
The Insight (Rule of Thumb)
- Action: Replace runtime assertions with `constexpr`/concepts/type-level constraints.
- Value: The code does not compile if the invariant is violated.
- Trade-off: Longer compile times, more complex template metaprogramming; but zero runtime cost and zero missed checks.
- Compatibility: Requires C++20 or later for concepts; C++17 for `constexpr`/`consteval`.
Reasoning
A runtime assertion only fires if the code path is executed — and testing never covers all paths. A compile-time constraint fires for every build, every configuration, every call site. The compiler is a tireless, exhaustive checker that never skips a branch. Moving checks to compile time trades human vigilance (testing) for mechanical guarantee (compilation), which is strictly more reliable.