Your AI Fixed One Bug and Broke Two: How to Stop the Loop

If your app has tests, one fix cannot quietly break something else, because a test goes red the moment it does. If it has no tests, the AI has nothing to check its fix against, so “the error went away” is the only signal it gets.
This post shows it on a small shop app, with real output, then how to set up the tests that stop it. Everything below was run on Node 22; copy it into a folder and run it yourself.
Why does a fix in one place break another?
The AI sees the files it opened. In a real app, the file that depends on yours is often not one of them. It was also asked to make an error go away, not to keep everything else working.
Here is the shop. Three screens share one pricing helper. Prices are in cents.
// src/pricing.js (shared by cart, checkout and invoice)function lineTotal(item) { return item.price * item.qty;}function orderTotal(items) { return items.reduce((sum, item) => sum + lineTotal(item), 0);}module.exports = { lineTotal, orderTotal };const { orderTotal } = require('./pricing');function cartView(items) { const total = orderTotal(items); if (Number.isNaN(total)) return { error: 'Pick a quantity for every item' }; return { total };}module.exports = { cartView };const { cartView } = require('./cart');function charge(items) { const view = cartView(items); if (view.error) throw new Error(view.error); return { chargedCents: view.total };}module.exports = { charge };const { orderTotal } = require('./pricing');function invoiceText(order) { const dollars = (orderTotal(order.items) / 100).toFixed(2); return `Invoice ${order.id}: total $${dollars}`;}module.exports = { invoiceText };The cart quietly relies on one odd thing: an item with no quantity makes the total NaN (“not a number”), and that is how it knows to ask the customer. Code like this exists in plenty of apps, and nobody wrote it down. A later section comes back to it.
What do the tests look like?
Each test describes something a person using the shop would see. Save this as test/shop.test.js and add "scripts": { "test": "node --test" } to package.json. Node’s built-in test runner needs no install.
const test = require('node:test');const assert = require('node:assert');const { cartView } = require('../src/cart');const { charge } = require('../src/checkout');const { invoiceText } = require('../src/invoice');
const items = [{ price: 1999, qty: 3 }, { price: 500, qty: 1 }];
test('cart shows the right total', () => { assert.deepStrictEqual(cartView(items), { total: 6497 });});test('cart asks for a quantity instead of guessing one', () => { const view = cartView([{ price: 1999 }]); assert.match(view.error, /quantity/i);});test('checkout charges exactly what the cart showed', () => { assert.strictEqual(charge(items).chargedCents, cartView(items).total);});test('checkout refuses an item with no quantity', () => { assert.throws(() => charge([{ price: 1999 }]), /quantity/i);});test('invoice prints the total in dollars', () => { assert.strictEqual(invoiceText({ id: 7, items }), 'Invoice 7: total $64.97');});Run npm test. Five pass. Commit it (git add -A && git commit -m "Add behavior tests"), because that commit is the undo point later advice depends on. Now there is a safety net.
The bug: write the failing test before the fix
A customer reports that invoices for old orders show $NaN. Those old orders were imported before quantities existed, so their items have a price and no quantity (they meant one).
Do not ask the AI to fix it yet. First, reproduce it as a test and watch it fail:
test('invoice for an old order with no quantity counts one', () => { const oldOrder = { id: 3, items: [{ price: 1999 }] }; assert.strictEqual(invoiceText(oldOrder), 'Invoice 3: total $19.99');});✔ cart shows the right total✔ cart asks for a quantity instead of guessing one✔ checkout charges exactly what the cart showed✔ checkout refuses an item with no quantity✔ invoice prints the total in dollars✖ invoice for an old order with no quantity counts one + actual - expected + 'Invoice 3: total $NaN' - 'Invoice 3: total $19.99'Now the bug is reproduced on demand, and that is the definition of “fixed” the AI needs. If the report started as a stack trace, this step makes it runnable.
What happens when the AI fixes the nearest thing?
The invoice reads its total from the shared helper, so the shortest fix is in the helper. Here is a plausible one-line patch, the kind that comes from reading only the invoice and the helper:
function lineTotal(item) { return item.price * (item.qty ?? 1); // was: item.qty}Run the tests:
✔ cart shows the right total✖ cart asks for a quantity instead of guessing one✔ checkout charges exactly what the cart showed✖ checkout refuses an item with no quantity✔ invoice prints the total in dollars✔ invoice for an old order with no quantity counts oneℹ pass 4ℹ fail 2The invoice is fixed. And the shop now charges customers for items they never picked a quantity for, because the cart’s NaN alarm no longer fires. Without the two cart tests this patch looks perfect. With them, the break shows up in under a second.
Hand the AI the red output and tell it to fix the invoice without changing the cart or checkout. The fix that keeps the tests green lives in the invoice alone:
function invoiceText(order) { // Orders imported before quantities existed have no qty (missing or null): they meant one. const items = order.items.map((item) => ({ ...item, qty: item.qty ?? 1 })); const dollars = (orderTotal(items) / 100).toFixed(2); return `Invoice ${order.id}: total $${dollars}`;}ℹ tests 6ℹ pass 6ℹ fail 0One more check, on the data you actually have. Old rows from a database or an import often hold qty: null, not a missing key. A first version of this fix used { qty: 1, ...item }, which only fills the key when it is absent. Add this test:
test('invoice for an old order whose quantity is null counts one', () => { const oldOrder = { id: 9, items: [{ price: 1999, qty: null }] }; assert.strictEqual(invoiceText(oldOrder), 'Invoice 9: total $19.99');});With the spread version it fails (actual: 'Invoice 9: total $0.00', expected $19.99): no error and no NaN, just a wrong invoice. With the ?? 1 version above all seven tests pass. Tests define “fixed” only for the cases you wrote down.
Why test behavior, not internals?
The tests above call cartView, charge and invoiceText, which are what the screens do. Suppose you had tested lineTotal instead:
test('lineTotal multiplies price by qty', () => { assert.strictEqual(lineTotal({ price: 100, qty: 2 }), 200);});Run against the bad patch above, it still passes (checked). It tests how the helper works inside, and the patch kept that working. The bug was in what the cart does, and only a test about the cart can see it.
For the other half of the problem, see test behaviors, not code. Write each test as “when a customer does X, they see or get Y”.
What do tests not catch?
Only what they cover. Delete the two cart tests and the bad patch above passes cleanly. A test suite is a list of behaviors you decided to protect, and the AI will cheerfully break the ones you did not write down.
The same goes for the odd contract. The cart treats NaN as its only validation, so a quantity of 0 is charged as 0 cents, -2 as -3998 cents, and the string "3" as 5997 (run on the final code). The tests exposed a hidden rule, and the durable fix is to make it explicit: the cart accepts only a whole quantity of 1 or more. Two new behavior tests (“cart rejects a quantity of 0”, “cart rejects a negative quantity”) fail on the old cart (7 pass, 2 fail). After changing cartView to require Number.isInteger(i.qty) && i.qty >= 1 for every item, all nine pass.
Two habits close most of the gap:
- Add a test with every bug you fix. The failing test from the section above stays in the suite forever, so the same break cannot come back unnoticed.
- Watch for edited tests. An AI can turn a red test green by changing the test. After a fix, run
git diff --stat HEAD -- test/. Any output means a test changed; if you did not ask for that, ask why. To make this automatic, put “do not edit existing tests” in your tool’s rules file (see Cursor rules best practices).
How do I get tests for an app that has none?
Ask the AI to write tests that describe what the app does today, before changing anything. Paste this:
Do not change any application code. Write tests for the current behavior of the cart, checkout and invoice, from the point of view of someone using the app: inputs a customer would give, and what they should see or be charged. Use the built-in test runner. Do not test private helper functions. After you write them, list every behavior you decided not to cover.
Read the tests, not just the green result. A test that describes wrong behavior now locks in a bug. The list of uncovered behaviors shows where your safety net has holes.
From then on, every fix prompt gets one extra sentence: “Run npm test before and after. Do not edit existing tests. Show me the output.”
Does the research agree?
Mostly. A 2026 study of LLMs in an iterative bug-fixing loop found that models kept claiming to find bugs in correct programs, and that the rate of repairing broken programs was lower than the rate of damaging correct ones (single-file competitive programming, smaller models, so not a full app). A March 2026 paper on regressions measured about 6.5 previously passing tests broken per patch by a plain agent in its setup (two small local models). One part of that paper is worth a second read: just telling a model to “do test-driven development” made regressions worse, while a tool that told the agent which tests its change could affect cut test-level regressions from 6.08% to 1.82% (about 70% fewer, measured on 100 instances with one model). Running the whole suite after every fix gives the agent the same view.
If a fix still fails twice, stop correcting. Anthropic’s Claude Code docs say a third correction mostly adds to a conversation already full of two failed attempts, and a fresh start with one better prompt works better almost every time. In Claude Code that command is /clear. Revert to your last commit, open a new chat, and paste in the failing test. The test is the briefing. For the longer version of that loop, see Stop pasting the error back, and the launch checklist for what else to check before real users arrive.
The Get AI Native ebook goes deeper on this. It is free, and the expected launch is around Christmas. Join the waitlist on the homepage.