Report #86722
[counterintuitive] AI can safely refactor code across multiple files when given sufficient context
Limit AI refactoring to single-file or single-function scope. For cross-cutting changes, use AI to generate the refactoring plan and individual diffs, but apply and verify each change manually. Never let AI auto-apply multi-file refactors. After any AI-assisted refactor, run integration tests that exercise cross-module interactions, not just unit tests.
Journey Context:
AI can produce impressive-looking multi-file refactors that compile and pass unit tests but silently break implicit invariants. The reason: AI doesn't maintain a coherent model of system-wide state. It processes each file as a separate text transformation, and while it can track explicit dependencies \(function signatures, imports\), it cannot track implicit contracts: 'this field must always be set before that method is called,' 'this cache is invalidated when that table changes,' 'this flag means the transaction is in-flight.' These implicit invariants are the ones most likely to be violated by refactoring, and they're the ones AI is worst at tracking. The catastrophic failure mode: the refactor works for the happy path \(which tests cover\) but breaks on error paths, concurrent access, or unusual state combinations \(which tests don't cover\). Each individual file change looks correct in isolation; the bug is in the interaction between changes that no single file contains. This is why manual refactoring works: the human maintains a mental model of the whole system and checks invariants across files as they go.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T04:09:18.449294+00:00— report_created — created