Autonomous Semantic Codebase Refactoring and Migration Chain
Construct a multi-step agent workflow chain for large-scale codebase migrations with AST verification and regression prevention.
Deploy this framework to orchestrate automated refactoring across legacy code repositories. It ensures syntactic accuracy, validates semantic invariants via AST tooling, and manages automated branch staging for engineering reviews.
Role: Staff Core Platform Architect specializing in static analysis, compiler tooling, and automated codebase modernization.
Context
- Legacy Codebase Specification: {{legacy_codebase_spec}}
- Target Language / Framework Version: {{target_language_version}}
- AST & Static Analysis Tooling: {{ast_validation_tooling}}
- Test Coverage Baseline: {{test_coverage_threshold}}
- Migration State Persistence: {{state_persistence_layer}}
- Review & Approval Quorum: {{review_quorum_rules}}
Task
Design a reliable, iterative agent workflow chain that systematically ingests {{legacy_codebase_spec}}, generates syntactically correct migrations to {{target_language_version}}, verifies AST equivalence via {{ast_validation_tooling}}, maintains {{test_coverage_threshold}}, and persists migration progress in {{state_persistence_layer}} under {{review_quorum_rules}}.
Method
- Configure the dependency mapping agent to decompose {{legacy_codebase_spec}} into topologically sorted module batches.
- Design the code translation agent state using grammar rules and syntax targets for {{target_language_version}}.
- Implement the AST verification agent step to compare control flow graphs using {{ast_validation_tooling}}.
- Build the test synthesizer agent to generate new unit tests for uncovered branches to meet {{test_coverage_threshold}}.
- Formulate the state management protocol to record batch status and rollback checkpoints in {{state_persistence_layer}}.
- Specify the mutation testing and linting agent to detect regressions and code style discrepancies.
- Construct the PR generation node that packages migration diffs and verification reports per {{review_quorum_rules}}.
Constraints
- Code transformations MUST preserve runtime behavioral semantics without altering external API contracts.
- MUST NOT commit migrated modules if test coverage drops below {{test_coverage_threshold}}.
- Transformations that fail {{ast_validation_tooling}} checks must automatically trigger an iterative correction cycle.
- Every migration batch MUST maintain isolated git branches with deterministic commit histories.
- The workflow must store state checkpoints after each module to support resumption after failure.
Output format
- Module Decomposition & Migration Pipeline Graph (ordered stages, agent nodes, and retry logic)
- Agent Node Specifications (node name, system instructions, AST tool schemas, validation criteria)
- State & Checkpoint Persistence Model (schema definitions for {{state_persistence_layer}})
- Verification & Review Gate Protocol (automated checklist required before submitting to {{review_quorum_rules}})
Self-review
- Ensure semantic invariance is formally verified through {{ast_validation_tooling}} at each step.
- Validate that batching logic prevents context window exhaustion during large file processing.
- Check that rollback logic in {{state_persistence_layer}} leaves the repository in a clean state upon failure.
Explicit role, a named task, and discrete steps the model can follow.
Background, inputs and variables the model needs before it starts.
Hard boundaries — what the model must and must not do.
A named, field-level shape for the response.
Ordered work items that force analysis before an answer.
Length and structure that travel across frontier models.
Signal density — instruction weight without padding.
Documented variables so the scaffold adapts to new inputs.
Quality bar, assumptions and behaviour when inputs are thin.
How much real usage the template has behind it.