What broke
Three of four FIFO implementations were blind to stock splits, including a go-live criterion
The stock app had four FIFO calculations of realized profit and loss. Only one handled stock splits, because the correct split math sat in a private function. The other three, one of them a go-live criterion, would have booked a 2:1 split as a 25,000 NOK phantom loss. A second review found the same class of bug in the frozen twin, and there it pointed the wrong way.
The short version
What went wrongThree of four FIFO profit calculations, including go-live criterion C, ignored stock splits. A 2:1 split would have booked a 25,000 NOK phantom loss and dropped a trade, with no error raised.
How we found itHenning asked for a full review of the money path. It checked the FIFO logic against the splits in the data and found three copies without split handling.
The rule nowWhen places must agree on math, export one shared function and make all of them use it. Normalize splits in every FIFO, and after each delivery ask whether the math already exists.
This post documents something Claude got wrong. The claim rests on a commit or a file, not on memory. All of Claude's mistakes
The stock app is a paper-trading system. Before it is allowed to trade real money it has to pass a go-live gate made up of several criteria. Criterion C is expectancy: the average realized profit per round trip, worked out by matching sells against buys first in, first out (FIFO).
On 30 July 2026 Henning asked for a full review of the money path, meaning every step from signal to realized krone. The review found that criterion C couldn’t handle stock splits. It also found that this wasn’t a one-off. Of four FIFO implementations in the codebase, three were blind to splits. Claude wrote all of them.
What went wrong
The commit message has a worked example. Buy 100 shares at 500. The stock splits 2:1. Sell 200 shares at 250. The true profit is zero, because you sold the same position for the same money.
The split-blind FIFO saw 100 shares in the lot and a sell of 200. It matched 100 against the lot and booked 100 × (250 − 500) = −25,000 as a loss. The other 100 shares found no lot, dropped out of the loop and never became a round trip. The result was a 25,000 phantom loss and a missing trade, and nothing raised an error.
The commit called this “the most expensive kind” of finding: “a number that looks plausible but is wrong, on the criterion that decides real money.” A single phantom loss of that size could block go-live on its own.
The data shows this wasn’t a theoretical problem. On the day of the review the commit counted 8 splits in the previous 90 days among the instruments the app tracks, including a 10:1, a 5:1, a 4:1 and a reverse split at a ratio of 0.025. With a 10:1 split, the phantom loss would have been 90 % of the position.
Why three implementations got it wrong
The root cause was not that nobody knew how to handle splits. The portfolio calculation already had a correct, tested function for the split factor. It was private, though. It wasn’t exported, so other modules couldn’t use it.
So three other places wrote their own FIFO loops without split handling:
- the expectancy calculation behind go-live criterion C
- the per-source attribution on the simulation page, which Henning reads to see which signal sources make or lose money
- the strategy detail view
One of them knew about the gap. The source attribution had a comment saying that sells without a known lot, such as those caused by split adjustments, were ignored in the attribution. The commit says it plainly: the code “knew the problem and chose silence”, while also attributing a phantom loss to a source that hadn’t lost anything.
How it was found
The bug turned up because Henning asked for a full walk through the money path, and the same review measured how many splits were actually in the data. The reference document written in the same commit sums up the pattern: “the symptom is almost always silence, never an error message.”
The fix
Claude exported the split-factor function together with a new normalization helper. The helper scales quantity up and price down by the same factor, so the total cost stays the same. The source attribution now imports both. The expectancy calculation got its own copy of the same arithmetic, which its code comment describes as the same calculation as the split-factor function, only in milliseconds. The expectancy calculation also skips splits whose ex-date is in the future, because announced splits that haven’t happened yet must not restate history early.
The commit added 12 tests that, in its words, “calculate through the numbers, not just check that the code runs”. They cover 2:1, 10:1, the reverse 0.025 split, a partial sale across two lots, and a check that friction on a round trip is about 0.8 %. The suite went from 465 to 477 tests.
The same review also confirmed some things that were correct and wrote them down as “verified, not assumed”. Raw and split-adjusted price spaces aren’t mixed in the paper accounts, and the cash flow matches the cost model in both directions.
The same bug, sixteen minutes later, pointing the wrong way
Henning then asked Claude to check its own work completely. The next commit, sixteen minutes after the first, is titled “four bugs in my own code, all of classes I documented myself”. The first of the four was the same split blindness, this time in the frozen twin.
The frozen twin answers the question “would we have been richer if the app had done nothing?” It freezes the holdings at a point in time and values them going forward. The function that rebuilt those holdings added up raw share counts. The prices it valued them at had been restated to today’s split terms. After a 2:1 split, that gave 100 shares at 250, a value of 25,000 instead of 50,000.
The commit says the direction made it worse than the FIFO bug. The twin undervalued doing nothing, which made the trading account look better than it was, “the exact opposite of what the tool exists for.” With a 10:1 split in the data, the undervaluation would have been 90 %. In the commit’s words, this was in “the number I myself called the most important in the project, written AFTER I fixed the same class in three other FIFOs.”
The fix was to use the shared split factor. The error-class document got a new section: “my own findings are not exempt, they are overrepresented”. It also adds a principle: the direction of a bug matters more than its size, and “a bug that flatters is more dangerous than one that scares.”
The rule
When two places must agree on math, export the function and make everyone use it. If it stays private, someone will write their own copy, and that copy will be worse.
That rule is now in the stock app’s list of recurring error classes, under “duplicated math that drifts apart”. The money-path document has an invariant that says to normalize splits in every FIFO and never to rewrite the loop without that step. The habit that goes with it is to go through the error classes after every delivery and check them against the code just written, asking one question in particular: does this math already exist?