Skip to content
mentoor

Topic 5 of 5 · 5 min read

Rewrites that lose legacy behaviour

Goal: Spot behaviour lost in an agent's rewrite of legacy code by comparing the old and new code paths.

A rewrite looks finished when it handles the main case; the work is in the behaviour it quietly dropped. Compare old and new path by path, and treat everything that is missing as a bug until you prove otherwise.

Check yourself · 1 of 3

An agent deletes a function because a search finds no callers in the repo. What should you check before accepting?

Choose one answer

Why it matters

An agent can produce a clean rewrite of a 400-line module in a few minutes. It will be shorter, nicer and better named. Part of that shortness is real cleanup. Part of it is lost behaviour: the odd branch, the retry, the log line a monitoring alert depends on. Joel Spolsky's well-known essay argues that old code is ugly because it carries years of bug fixes, and that throwing it away throws those fixes away too.

The four usual losses

  1. Dropped edge cases. Empty input, a legacy date format, a null from a partner API. The new code handles the cases the agent thought of.
  2. "Dead" code that was alive. The agent finds a function with no callers and deletes it. But it was called by reflection, a scheduled job, a config file or another repository.
  3. Changed error handling. The old code retried three times and returned a safe default. The new code throws, or swallows the error and returns success.
  4. Changed side effects. Order of writes, a lock, an event sent, an audit entry, a cache clear.

How to check

  1. Make a list of old behaviours. Use the characterization tests, the code's branches and the tickets from the history.
  2. Walk the old paths one by one and find where each one lives in the new code. Mark any that have no home.
  3. Search before accepting a deletion. grep for the name, check config and job schedules, and look in other repos or the logs.
  4. Run old and new side by side on recorded real inputs and compare the outputs.
  5. Prefer small steps. The strangler fig pattern replaces one slice at a time, so a loss shows up in a small slice.

Before and after

-  for (let i = 0; i < 3; i++) {
-    try { return await gateway.charge(order); }
-    catch (e) { if (!isTransient(e)) throw e; await sleep(200 * (i + 1)); }
-  }
-  throw new PaymentError("gateway unavailable");
+  return gateway.charge(order);

The new line is cleaner, and it has dropped the retries and the error type. A reviewer asks where the retry now lives, and the answer is nowhere.

What goes wrong

  • Trusting "functionally equivalent". The agent states it, but has not run both.
  • Deleting by grep alone. Reflection and config do not show up.
  • Judging by line count. Fewer lines can mean fewer behaviours.

Examples

The vanished retry

The old charge retried transient gateway errors up to three times and raised PaymentError. The rewrite calls gateway.charge once. Nothing fails in tests, but in production a brief gateway timeout now cancels checkouts. The fix is to put the retry back and add a test with a fake gateway that fails twice.

The unused function

An agent deletes exportLegacyCsv because grep finds no callers. It is run every night by a cron entry in a deployment file outside the repo. The finance team's report stops arriving. The reviewer's rule: no deletion of unused code without checking schedules, config and logs.

Key points

  • A shorter rewrite often means dropped behaviour, not only cleaner code.
  • The four usual losses are edge cases, code that looked dead, error handling and side effects.
  • List the old behaviours first, then find the new home of each one.
  • Before accepting a deletion of unused code, check config, reflection, scheduled jobs and other repos.
  • Run old and new on recorded real inputs and compare, rather than trusting a claim of equivalence.

Common mistakes

  • Accepting "behaviour is unchanged" without running both versions.
  • Deleting code that has no callers in the repo but is used from outside.
  • Missing that an error is now swallowed or thrown differently.
  • Rewriting the whole module at once instead of one slice at a time.

Check yourself · 2 of 3

A rewrite replaces a loop that retried a payment call three times with a single call. Which kind of loss is this?

Choose one answer

Check yourself · 3 of 3

What is the strongest way to check an agent's claim that a rewrite is "functionally equivalent"?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic