Systematic Codebase Architecture Refactoring
Guide an AI coding agent through safe, incremental architecture improvements that reduce complexity, clarify module boundaries, and preserve existing behavior and external contracts
Example media
These files may include inputs, references, or generated results
Prompt template
- Format
- Markdown
- Inputs
- Codebase (code) · Required
- Outputs
- Refactored_codebase (code) Engineering_records (document)
/goal Systematically reduce architectural entropy across the codebase, normalize and standardize its structure, decouple modules, and manage complexity so that it evolves into a well-organized, easy-to-understand and maintain engineering system suited to extensive parallel development by agents.
## Core principles
Assume today is the first day of this product, but that we already know all of its actual requirements, business constraints, and product shape. Consider what the codebase should ideally look like under those conditions, then move toward that state through safe, incremental, verifiable refactoring.
Pursue these engineering properties:
1. **Single Source of Truth**
Every business rule, data definition, configuration, state, and design specification should have one clear authoritative source.
2. **Canonical Implementation**
Keep one standard implementation for each semantic concept and responsibility. Eliminate parallel legacy implementations, duplicated logic, temporary patches, meaningless compatibility layers, and multiple coexisting technical approaches. Retain compatibility only when required by actual callers or explicit contracts.
3. **Deep Modules**
Refer to John Ousterhout's *A Philosophy of Software Design*. Encapsulate complexity internally and expose capabilities through interfaces that are as simple and stable as possible. Reduce shallow modules, meaningless forwarding layers, and abstractions that expose internal implementation details.
4. **High Cohesion, Low Coupling**
Give each module a clear responsibility, ownership, and dependency direction. Keep modules highly cohesive internally and have them interact through stable contracts. Eliminate circular dependencies, implicit coupling, cross-layer access, and shared mutable state.
5. **Locality of Change**
When changing a business concept, make the change in as few logical places as possible. Eliminate situations where one requirement forces changes across many unrelated files.
6. **Deletion over Accumulation**
Prefer removing unnecessary complexity. Whenever a new canonical implementation is introduced, identify and migrate its old callers, then remove the superseded implementation once safety is confirmed. Do not let old and new systems coexist indefinitely.
7. **Enforceable Architecture**
Turn established engineering rules into machine-enforceable constraints. Use the type system, linting, AST checks, contract tests, and CI to prevent future work from reintroducing resolved problems.
## Execution process
First, understand the codebase's actual business domain, architecture, historical decisions, module relationships, technology stack, existing tests, and production constraints.
Build a complete architecture map, dependency graph, state-ownership map, inventory of duplicate implementations, inventory of technical approaches, and list of deviations from standards.
Inspect and improve each of the following areas:
- UI components and design systems
- Business logic and domain models
- APIs and data contracts
- State management and data flow
- Data access and caching
- Configuration and environment variables
- Error handling and retry mechanisms
- Logging and observability
- Permissions and authentication
- Naming, directory structure, and module boundaries
- Utility functions and shared infrastructure
- Tests, builds, and the development toolchain
Repeat this cycle:
Scan the entire system
→ Find architectural entropy and deviations from standards
→ Analyze actual callers and dependencies
→ Propose multiple refactoring options
→ Choose the option with the lowest overall complexity
→ Establish behavioral baselines and tests
→ Complete a coherent batch of migrations
→ Remove superseded implementations
→ Conduct an independent review
→ Run validation
→ Add regression-prevention constraints
→ Record the results and continue to the next cycle
Close the loop on every refactoring cycle. Do not merely create new abstractions, directories, or interfaces without migrating existing callers.
Apply the Deletion Test to every abstraction: if this layer were removed, would complexity disappear or spread to callers? Keep only abstractions that genuinely encapsulate complexity.
## Acceptance criteria
- Every core business concept has a clearly authoritative implementation.
- Every core module has a clear responsibility and public interface.
- No semantic concept has multiple unexplained implementations.
- Core modules have no unapproved circular dependencies.
- Business logic is appropriately decoupled from UI, storage, frameworks, and external services.
- New features can be implemented within clearly defined module boundaries.
- Established, important architectural rules have automated regression checks.
- All existing functionality, data compatibility, and external contracts are preserved.
Do not over-abstract, move directories without value, sacrifice readability to reduce line count, or merge concepts with different meanings just to make them uniform.
Work in an isolated Git worktree and create a separate commit for each effective batch of improvements. Continuously maintain a progress file, architecture decision records, and an optimization task backlog.
If an item requires a product decision, a major architectural change, or a breaking migration, record it as pending confirmation and continue with other independent tasks.
Do not ask for confirmation after every cycle. Continue until all high-value governance tasks have converged, the operating budget is reached, or no further safe improvements can be made.
What you'll create
This prompt asks an AI coding agent to inspect and improve a software codebase through verified, incremental refactoring. It targets clearer module responsibilities, less duplication and complexity, and automated safeguards while preserving existing functionality and contracts
Usage instructions
Frequently asked questions
How should legacy implementations and compatibility layers be handled?
Migrate callers and remove replaced implementations when it is safe; retain compatibility only when real callers or explicit contracts require it
What should happen when work requires a product decision or a breaking migration?
Record it as pending confirmation and continue with other independent tasks
Comments
Tried this prompt? Share what you made, compare results, or ask a question