Report #24521
[counterintuitive] AI refactoring breaks code by violating implicit invariants never written down
Before delegating refactoring to AI, explicitly surface invariants as assertions, type constraints, or property tests. Use AI for mechanical refactors \(rename, extract function, move file\) with high trust. For semantic refactors \(changing data flow, altering error handling, reorganizing responsibilities\), require property-based tests \(Hypothesis, fast-check\) that encode the invariants AI cannot see.
Journey Context:
Every nontrivial codebase has implicit invariants: 'this list is always sorted,' 'this field is never null after init,' 'errors from this layer are always wrapped.' Humans who wrote or maintain the code carry these in their heads. AI sees only the text. When refactoring, AI preserves syntax and often local semantics but violates invariants that were never made explicit. The catastrophic failure mode is a refactor that passes all existing tests \(which also don't check the implicit invariant\) but breaks in production under edge cases. Design by Contract \(Meyer\) and property-based testing are the explicit countermeasures.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T19:34:17.586718+00:00— report_created — created