Skip to content
agentusehub
CodingAdvancedSoftware

Ship a 40-file refactor with a coding agent

Plan, execute and review a cross-cutting refactor in small batches, with Claude Code making the edits and GitHub Copilot reviewing each PR.

Time saved
15h / week
Setup time
3 hours
Running cost
$110/mo
Cost range
$100+/mo

The problem it solves

Cross-cutting changes like swapping a logging library, renaming a core type or moving from callbacks to async/await sit in the backlog for months because they touch dozens of files. Doing them by hand eats a week of senior time and produces one giant PR nobody wants to review. Half-finished migrations then leave two patterns in the codebase for years.

How it flows
  1. Write migration plan
  2. Agent edits in batches
  3. Tests per batch
  4. Copilot reviews PRs

Step-by-step setup

  1. Lock down the safety net

    Before touching anything, make sure the test suite and type checker pass on main and finish in under ten minutes. Add a few characterization tests around the riskiest modules if coverage is thin; the agent relies on them to know when it broke something.

  2. Write a CLAUDE.md for the migration

    Document the old pattern, the new pattern with one before/after example, files that are out of scope, and the exact verification commands, for example npm run typecheck and npm test. Claude Code reads this file at the start of every session.

  3. Have the agent plan before it edits

    Start Claude Code in plan mode with the planning prompt below. It should search for every call site and return batches of 5–8 files grouped by module, with risky files flagged. Review and edit the plan; this is where most of your judgement goes.

  4. Execute one batch per branch

    Ask the agent to implement a single batch, run the type checker and related tests, fix failures and commit. Keep each batch on its own branch and PR so a reviewer can read it in under 15 minutes.

  5. Add Copilot as a reviewer

    Request a review from Copilot on each PR alongside one human reviewer. Paste any valid findings back into Claude Code and fix them on the same branch before merging.

  6. Finish with a sweep and a guardrail

    After the last batch, have the agent search for any remaining uses of the old pattern. Then add a lint rule such as no-restricted-imports so the old pattern cannot creep back in.

Example prompts

Migration planning prompt
We are migrating {{old_pattern}} to {{new_pattern}} across this repository. Read CLAUDE.md for the rules and an example.

Do not edit any files yet. First:
1. Find every file that uses {{old_pattern}}, including dynamic usages and test helpers. Show the search commands you ran.
2. Group the files into batches of 5-8 that can be merged independently, ordered from lowest to highest risk.
3. For each batch, list the files, what changes in each, and which tests cover them.
4. Flag files where behaviour could change (error handling, ordering, side effects) and explain why.
5. List anything you are unsure about as questions for me.

Write the plan to docs/migration-plan.md.
Batch execution prompt
Implement batch {{batch_number}} from docs/migration-plan.md and nothing else.

For each file, apply the new pattern exactly as shown in CLAUDE.md, keep behaviour identical, and do not reformat unrelated code.

After editing, run {{typecheck_command}} and {{test_command}}. If anything fails, fix it and run both again. If a fix would require changing a file outside this batch, stop and tell me instead.

When everything passes, commit with the message "refactor: {{short_description}} (batch {{batch_number}})" and summarise the files changed, anything you were unsure about, and tests you think are missing.
Coding · Advanced
  1. Run coverage
  2. Pick 3 riskiest files
  3. Write + run tests

Generate missing unit tests for untested modules

Claude Code reads your coverage report, picks the riskiest untested modules and writes unit tests that pass, one small PR at a time.

Claude Code · GitHub Copilot · Slack
Coding · Intermediate
  1. PR opened
  2. Copilot inline review
  3. Claude risk summary

Automated first-pass review on every pull request

Copilot and Claude review each PR for bugs, missing tests and risky changes before a human looks, so reviewers can focus on design.

GitHub Copilot · Claude · Slack