Broken code announces itself. A stack trace, a red console, a button that does nothing. Almost-right code returns a plausible number and waits until somebody pays you to reveal that the number was wrong.
That distinction matters because the fixes are different. Broken code gets fixed by reading the error. Almost-right code gets fixed by knowing what the answer should have been before the function computed it.
Stack Overflow's 2025 survey puts almost-right at the top of developer complaints about AI output. The failure class has six favourite hiding places, and I have hit five of them on one product.
Why the tests rarely catch it
When the same model writes the implementation and the test, both are built from the same sentence. The test asserts the number the implementation produces, and the suite goes green.
A test only catches this bug when it was written from an independent understanding of the rule. That means a person, a second source, or a property that holds regardless of implementation.
Money and the cent that multiplies
Currency arithmetic on floats produces totals that differ by a cent. One cent across ten thousand rows becomes a refund queue and a support inbox that fills for a month.
The pattern shows up when a price calculation is written with plain floats, because the shortest working example in the training data uses them. Percentage discounts reproduce it. Apply ten percent to a total, and then again per line, and the two answers differ by rounding direction rather than logic.
Check: Sum a thousand rows in integer cents and compare against the float implementation. Any disagreement means the float version is wrong.
A worked example, because this one is abstract until it bites
An invoice has three lines at $19.99 and a ten percent discount. The float path rounds each discounted line to two decimals, then sums, and arrives at $53.97. The integer path sums to 5997 cents, takes ten percent once, and arrives at 5397 cents. Both print $53.97 in this example, and that is the problem: the disagreement needs a line count that pushes a half-cent the other way.
Add a tax rate with four decimals and the gap appears within a few hundred rows. You will not find it by reading the function. You will find it by running both implementations against the same data and diffing the output.
The lesson generalises past currency. Any two places computing the same number from different inputs will disagree eventually, and the disagreement will show up in a report long after the code looked fine.
Dates without a zone
A date with no zone is a claim about a place. Generated code routinely stores local time, compares it against server time, and produces a feature that works in the developer's time zone and fails everywhere else.
This one hides for months because it only breaks at an edge. Monthly reports, billing periods and streak counters all break at the same boundary, and they break at the end of a month when everybody is asleep.
Check: Create a record at 23:30 local time and query for it from a client three zones away. If it appears on the wrong day, the boundary is local.
The page boundary
Offset pagination with a limit and a page number looks correct on page one. At the boundary, the last item of one page and the first item of the next get duplicated or skipped, because the comparison uses the wrong inequality.
Ties make it worse. When the sort key is not unique, the database breaks ties differently between two queries, and the row that sat on the boundary moves between pages.
Check: Fetch every page, collect the ids, and compare the count against the table count. Duplicates and gaps both surface immediately.
Retries that create twins
Networks drop. Webhooks fire twice. The guard that is supposed to handle a duplicate is written as check-then-insert, which is not atomic, so two concurrent retries both pass the check and both insert.
The fix lives in the database rather than the handler. A unique constraint makes the second insert fail on its own, and the handler learns to treat that failure as success.
Check: Fire the same request twice at the same moment. Two rows means the guard is a branch rather than a constraint.
The catch block that returns zero
A try block that catches everything and returns a default turns a failure into a valid-looking value. The page renders, the dashboard shows a figure, and the figure is zero because the query threw.
Zero is the most dangerous default in software. It is a legitimate number, so nobody investigates it, and a broken feature looks like an empty one for as long as you are willing to believe the emptiness.
Check: Search for catch blocks that neither log nor rethrow. Every one of them hides a failure behind a default.
The route nobody protected
A permission check gets written for the main route and forgotten on the admin variant, the export endpoint, or the mobile API added a week later. The feature behaves exactly as designed on the route you tested.
Check: List every route that touches the resource and call each one with a lower-privileged session. The route that answers is the bug.
An invariant is a sentence with a query attached
Most teams write tests that call a function and compare a value. An invariant takes the other shape. It states a property of the data and then queries the whole table to prove the property holds for every row, not just the one in the fixture.
The invoice invariant reads like this. For every paid order, the stored total equals the sum of its line items in integer cents. The query is a join and a comparison. When it returns rows, those rows are the bug, and they come with ids you can open.
Three properties are worth writing for any product with money in it. Totals equal the sum of their parts. Every paid order has exactly one payment record. No order moves from refunded back to paid. Each one is a sentence a non-engineer can read and a query an engineer can run on production.
The reason this catches almost-right code is that it never consults the implementation. It asks the database what happened, and the database has no opinion about what the function intended.
One practical note about who writes the check. If you are solo, write the invariant before you generate the feature, from the business rule rather than from the code. If you have a teammate, have them write it, which is faster than arguing about whether the output looks correct.
Keep the invariant queries in the same place as the schema migration that created the table. The migration tells you when it should run; the query tells you whether the data still agrees with the rule you wrote it for.
The order I check them in
Order matters because the cheap checks eliminate the expensive ones. Start with the money path, since a wrong total is the failure that produces refunds, and refunds are the only bug your users will email about.
Next, permissions. A leak of somebody else's rows costs more than any calculation error, and the test is four lines of curl. Then boundaries, because pagination and dates fail on data volume rather than on logic, and volume arrives after launch.
Leave the swallowed errors for last, and treat them as a cleanup rather than a check. Searching for catch blocks is quick, and each hit tells you which feature was quietly returning zero. Fix them in one pass once the three checks above hold.
What catches this class of bug
Review does not. Review reads code, and this code reads fine. Three practices catch it instead, and all three compare the system against something outside itself, so they cannot inherit the model's misunderstanding.
- Invariants checked against the database, written as statements that must hold for any row rather than for one function.
- One real transaction on a real card, including a refund, before the money path is called finished.
- A second source for any number the product reports about itself, recomputed from raw rows.
Each one asks the deployed system what happened. The deployed system has no opinion about what the code intended, which is the entire point.
Start with the real transaction. Most of the six hiding places sit on that path, and one afternoon spent running an actual payment and an actual refund surfaces more than a week of reading diffs.
When to run the six checks is a judgement call about cost. Before a public launch, run all of them. Before a feature inside a product where the blast radius is small, run the money path and the permission check, and keep the rest for the release that touches payments. The point is to know which failure you are accepting, month by month, instead of accepting all six by default.