跪拜 Guibai
← All articles
Frontend

Refactoring with Codex: A Batch-by-Batch Workflow That Preserves Business Contracts

By qiaofugui ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

The difference between a successful AI refactoring and a broken one is whether the agent is forced to prove it understands the business contracts before it writes code. This workflow turns that principle into a repeatable prompt sequence that any team can adopt immediately.

Summary

The core risk in AI-assisted refactoring is not bad code but premature coding — an agent that guesses business logic from file names and silently breaks contracts. A reliable workflow starts by making Codex read the target project, source project, interface docs, and reference material to build a chain of evidence before touching any file. High-stakes decisions like authentication, permissions, and pagination protocols get explicit confirmation gates with 2–3 options and impact analysis. Only after a design document fixes boundaries does implementation begin, starting with the global adaptation layer — request handling, auth, navigation, pagination — before migrating business modules one batch at a time. Each module migration aligns three axes simultaneously: visuals to the target project, business rules to the source project, and data contracts to the interface spec. User feedback becomes explicit constraints for the next round, and a structured difference review against the source project replaces vague claims of completion. The article provides a full set of reusable prompts covering context reconnaissance, solution confirmation, batch implementation, module migration, difference review, targeted fixes, and layered verification.

Takeaways
— Start by making Codex read the target project, source project, interface docs, and reference material and output a context report before modifying any files.
— Set explicit confirmation gates for global contracts: auth, permissions, navigation, pagination, error handling, and whether backend fields can change.
— Write a design document that fixes boundaries — goals, non-goals, information priorities, global adaptation layers, and phased scope — before writing implementation code.
— Build the foundation first: request encapsulation, auth state, permission/navigation adaptation, pagination protocols, and common component patterns.
— Migrate modules one at a time, aligning visuals to the target project, business rules to the source project, and data contracts to the interface spec.
— Turn user feedback into explicit constraints for the next round rather than vague corrections.
— Run a structured difference review against the source project covering list fields, operation flows, detail views, permissions, data refresh, and error handling.
— Verify in layers: static checks, build, route reachability, data contract alignment, business rule alignment, and workspace cleanliness.
— Deposit refactoring experience into project rules — component usage, conventions, review checklists, and common pitfalls — so the next round starts from a higher baseline.
Conclusions

AI refactoring failures are rarely about code quality; they happen when the model makes silent business decisions based on file-name guesses or template assumptions.

Requiring Codex to output a context report before coding shifts it from executor to investigator and surfaces gaps in its understanding early.

The three-axis alignment rule — visuals to target, business to source, data to contract — catches mismatches that single-axis checks miss.

Difference review is more valuable than a build pass because build success says nothing about whether the old business rules survived the migration.

Depositing refactoring experience into project rules turns a one-off collaboration into a compounding asset for future maintenance.

Concepts & terms
Confirmation gate
A deliberate pause in an AI workflow where the model must present options and wait for human approval before proceeding on high-risk decisions like auth models or pagination protocols.
Global adaptation layer
The foundational infrastructure — request handling, auth state, permission/navigation logic, pagination — that must be established before migrating individual business modules to prevent cascading inconsistencies.
Three-axis alignment
A module migration rule requiring simultaneous alignment of visuals/interaction to the target project, business rules to the source project, and data contracts to the interface specification.
Difference review
A structured audit comparing refactored output against the source project across list fields, operation flows, detail views, permissions, data refresh, and error handling, with inferences explicitly labeled.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗