FormingSkillclaude-codepublished fresh until

How do I refactor legacy code with an agent without breaking prod?

Summary When refactoring a legacy Django codebase with an agent, write characterization tests first, record hard rules in CLAUDE.md, refactor in small steps, and rerun tests after each step; the post also promotes a review tool.

Not for a refactor PR too big to review; see agent-rewrite-pr-too-big-to-review-split-into-stacked-branches

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

The option

Option
ASkill · v1.0.0refactor skill (GitHub awesome-copilot)
How you use itYou ask for it
What it can dowrites files
What's inside
ASkill · v1.0.0refactor skill (GitHub awesome-copilot)
How you use it
You ask for it
What it can do
writes files
What's inside

What this thread does

The problem

You ask an agent to "refactor this legacy module to be cleaner". It produces tidy, modular code that does not behave like the old code, and you spend days finding what changed. Legacy code has no spec, sparse tests and load-bearing oddities.

What the post says

A r/ClaudeCode post by u/thewritingwallah (27 January 2026) describes refactoring about 50k lines of a Django monolith untouched for about four years. The author's claim, not independently checked: about 3 weeks instead of an estimated 2 to 3 months. The workflow:

1. Characterization tests first. Before any refactor, ask for tests that capture what the code does now. The author's prompt: generate minimal pytest characterization tests for a module, capture current outputs for realistic inputs, no behaviour changes. They spent the first four days only on this, covering the parts they were most afraid to touch. 2. A CLAUDE.md with boundaries. Sections for build and test commands, an architecture overview (which modules are coupled), refactoring guidelines (always run relevant tests, prefer incremental change, keep original signatures as wrappers when extracting) and hard rules: modules, migration files and base classes the agent must not modify without approval. The author notes CLAUDE.md files in subdirectories load when Claude touches that code, so module-specific edge cases go next to the module. Run /init to draft the project instructions, then refine the boundaries it discovers. Check /context all under Memory files to confirm the instructions loaded. 3. Small prompted steps. One task per prompt ("extract the discount calculation from Invoice.process() into a method"), run the matching tests, commit if green, debug before moving on if red. 4. Extra review. The author runs an AI review tool on every PR; one commenter called the post a long sales pitch for that tool, so weigh that part accordingly.

Where it breaks down, per the author: context gets long (they restart with handoff documents), the agent still invents APIs, and architecture decisions remain human work.

What commenters added

  • Several said the whole point is tests before refactoring; one said you would probably find a bug while writing them.
  • One suggested snapshot (approval) testing, for example the Verify libraries, to prove byte-for-byte identical output before and after.
  • One asked whether hooks that run tests and block edits to protected files beat CLAUDE.md rules; the author's answer is not in the part of the thread I read.
  • Another stressed planning and a progress-tracking file, and small commits.

How to ask your agent

Do not change any code yet. For billing/invoice.py, write pytest characterization tests that record current outputs for realistic inputs, including the awkward ones. Run them and show me they pass on the unchanged code. Then I will give you refactor steps one at a time; after each, run these tests and stop if anything fails.

Mistakes to check

  • Tests generated from the new code instead of the old: always run them on the untouched code first.
  • Characterization tests that only assert "no exception". Ask for exact outputs.
  • Letting the agent edit the tests to get green during a refactor.
  • Rules that only live in CLAUDE.md: for must-not-touch files, a permission deny rule or hook is enforcement. Register the blocking script as a PreToolUse hook in .claude/settings.json for Edit and Write calls.

Limits

Not run by me. A single self-reported result on one codebase.

Primary source

Primary source: Claude Code memory docs (Anthropic). Checked: CLAUDE.md and CLAUDE.local.md files in subdirectories load on demand once Claude reads, writes or edits a file in that subdirectory, which matches the post's point about module-specific notes; Claude treats CLAUDE.md as context rather than enforced configuration and the docs point to a PreToolUse hook to block an action, which supports the thread's advice to enforce must-not-touch files with a hook or deny rule. Not verified: everything specific to the r/ClaudeCode post (the 50k-line Django monolith, the 3 weeks versus 2 to 3 months, four days of tests, vote counts), the commenters' remarks, and the refactor skill package; no vendor or maintainer source exists for those.

Started from a post by @thewritingwallah, packaged by the editor. Original post

  1. 01 · YouPaste the line from Try it into your agent.
  2. 02 · Your agentGets the package and a trial ticket, installs it on a new branch, runs its check.
  3. 03 · YouApprove a one-line report. Nothing is posted without you.

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# How do I refactor legacy code with an agent without breaking prod?
Summary: When refactoring a legacy Django codebase with an agent, write characterization tests first, record hard rules in CLAUDE.md, refactor in small steps, and rerun tests after each step; the post also promotes a review tool.

## Options
- A · refactor skill (GitHub awesome-copilot) · early, no verdict yet · writes files
Copy for agent
get the whatworks thread legacy-code-refactor-characterization-tests-before-agent and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread legacy-code-refactor-characterization-tests-before-agent

How the verdict is calculated

We count trial reports at Tested and above, weighted by evidence: Verified counts ×1.5 and Platform ×2. A report that worked partly counts as half.

Under 10 trials no verdict is shown. The option asks for founding testers instead.

Ask your agent
how does whatworks decide a verdict

A · refactor skill (GitHub awesome-copilot)

Surgical, behaviour-preserving refactoring skill: small steps with tests after each, a code-smell catalogue, an extract-method walkthrough and type-safety improvements. Less drastic than a rewrite.

Format
Skill
Source
github:github/awesome-copilot#skills/refactor
Pinned
v1.0.0 · cb64ad12e327
Content hash
cb64ad12e32737ae
Audit
scan passed
Can do
writes files
Works with
claude-code
Check
Skill installed in the project; SKILL.md front matter names the skill; The behaviour-preserved golden rule is present
Author
@github
Kudos
0
Are you @github? Sign in with GitHub to claim it.
Ask your agent
show me the whatworks package awesome-copilot-refactor-skill before installing

A · Files

Read from the package source at the pinned version. Shown as is; nothing here runs.

Loading files…

Ask your agent
show me the files of the whatworks package awesome-copilot-refactor-skill
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp