AI-Assisted Rewriting of 300,000 Lines of Code in Six Months: An Engineering Retrospective
This article reviews a six-month effort to rewrite a 300,000-line internal web application with AI assistance. It explains why the codebase grew from 100,000 to 300,000 lines, how context engineering and task decomposition kept AI output manageable, what quality gates and cost controls were used, and how to keep the pace sustainable. Readers will find reusable practices for turning AI coding from an individual skill into an engineering system.
1. Background: Deciding to Refactor a Technical Debt Sample
An engineer with seven years of production web development experience took over the改造 of an internal web application. The application was originally written by an intern and derived from another internship project seven years earlier. Its code style was mixed, boundaries were unclear, and tests were missing—a typical technical debt sample.
He did not treat AI as a replacement, but as a capability amplifier: first using it to build demo prototypes and validate approaches, then integrating large model capabilities into the product. While AI usage inside the team generally stayed at the “code completion” stage, he had already brought AI into design, review, testing, and documentation. That was also the basis for his confidence in committing to the schedule.
It is worth emphasizing that a “large-scale rewrite in a short time” plan is inherently aggressive. The self-deprecating remark in the original article about “possibly losing five years of life” is essentially a reminder: tools can compress coding time, but they cannot compress the cost of communication, review, validation, and risk handling.
2. Why the Codebase Grew from 100,000 to 300,000 Lines
A post-refactor size of 300,000 to 400,000 lines does not mean lower quality. There are four common reasons:
- Explicitness: writing implicit conventions as types, validation, and configuration makes the code longer but the boundaries clearer.
- Testability: adding unit, integration, and regression cases; test code itself accounts for a large share.
- Observability: wiring up logging, metrics, and tracing brings a lot of boilerplate.
- Splitting and adapter layers: after breaking a monolith into multiple services, interface definitions, data mapping, and fault tolerance increase significantly.
The criteria for judging whether a refactor succeeded should not be line count, but defect rate, change lead time, rollback duration, and the rate of manual operations replaced.
3. Keeping AI Coding Tools Stable: Context Engineering
The key to large-scale AI-assisted development is not prompt tricks, but context engineering—providing the model with correct, minimal, and verifiable information.
- Establish a single source of truth: maintain interface contracts, data models, and error code conventions in one place, so the model does not generate code based on stale information.
- Supply context in layers: first architecture constraints, then module responsibilities, then local code, reducing interference from irrelevant information.
- Commit in small steps: change only one verifiable target at a time, making problems easier to locate.
- Let tools read “executable facts”: type definitions, test cases, and log samples are more reliable than natural language descriptions.
An illustrative way to organize context:
Constraint layer: tech stack versions, coding standards, compatibility requirementsContract layer: interface definitions, data structures, error codesEvidence layer: failing cases, log snippets, performance metricsTask layer: scope of this change and acceptance criteria
4. Task Decomposition: Have AI Deliver “Acceptable Small Blocks”
Breaking “rewrite the entire application” into the smallest units that can be automatically verified is the key to controlling rework. Recommended decomposition dimensions include: by domain boundary, by data table, by interface, and by user flow.
An executable task template:
Goal: Migrate the order query interface while keeping fields compatibleInput: old implementation path, interface contract, sample requestsConstraints: do not change the external protocol; keep error codes consistentAcceptance: new cases pass; regression passes; no new alertsRollback: keep a switch for the old implementation, support one-click switch back
This format makes AI output easier to review and keeps risk controllable: every step can be merged, rolled back, and measured.
5. Quality Gates: AI Output Must Pass “Three Gates”
- Static check gate: type checks, lint checks, dependency and license scanning.
- Test gate: unit tests cover core branches, integration tests cover key paths, and contract tests are introduced when necessary.
- Runtime gate: run smoke and load tests in an isolated environment close to production, watching error rate and latency changes.
An illustrative pipeline structure:
commit -> static checks -> unit tests -> integration tests -> isolated environment smoke test -> canary release -> monitoring and alerting
AI-generated code without gates turns “writing fast” into “fixing slowly.”
6. Cost Control: A High Monthly Subscription Does Not Equal High Value
The roughly 200 USD monthly cost in this case is essentially “trading cash flow for time.” A more prudent approach is tiered usage management:
- Prioritize high-value tasks: use high-capability modes for architecture design, difficult defects, and migration plans.
- Downgrade low-value tasks: handle formatting, comments, and simple boilerplate with low-cost methods.
- Cache and reuse: turn stable contracts and standards into templates to reduce repeated input.
- Measure input and output: compare “hours saved × labor cost” against tool and cloud resource spending.
If using cloud capabilities, it is recommended to build a usage dashboard through monitoring, alerting, and logging services, and meter common components such as container platforms, message queues, and object storage separately to avoid total cost getting out of control.
7. The Human Factor: Sustainability Behind Efficiency Gains
Pushing the pace to the limit can deliver in the short term, but over the long term it leads to lower review quality, knowledge concentration, and team dependence. More sustainable strategies include:
- Set “no-AI periods” to keep independent judgment on key designs.
- Let AI handle documentation and test case writing, leaving human time for trade-offs and decisions.
- Establish knowledge sharing mechanisms so the system is not truly understood by only one person.
- Use a rollback-friendly architecture to reduce psychological burden and the pressure that “it must succeed on the first try.”
8. A Reusable Implementation Checklist
- Establish contracts and standards first, then let AI write the implementation.
- Every task must have acceptance criteria and a rollback plan.
- Automating gates takes priority over optimizing prompts.
- Tier usage and allocate high-capability resources by value.
- Measure refactor benefits with business metrics, not lines of code.
- In cloud environments such as Baidu AI Cloud, general monitoring, logging, and container capabilities can be used to build an observability and elastic scaling foundation, but capacity planning should be based on your own workload.
Conclusion: the real value of this case is not the legend of “300,000 lines in six months,” but a method for upgrading AI coding from an individual skill to an engineering system: manageable context, acceptable tasks, gated quality, measurable cost, and sustainable pace.
