What broke
The sector cap that bought 4 % positions and left 24 % in cash
When a sector sat just under its cap, the stock app's portfolio engine bought for whatever room was left, which could be 4 % of a normal position. The swap logic could never remove those stubs, and the live portfolio ended up 24 % in cash when it should have been 90 % invested. A test had locked the behaviour in.
The short version
What went wrongThe sector cap scaled buys down to whatever room was left, giving tiny stub positions that swaps could never remove, leaving the live portfolio at 24% cash instead of about 10%.
How we found itA read-only review of the production database found 3 of 28 live buys were stubs. All eight fleet stubs came from simulations with a sector cap, and a unit test had locked the behaviour in.
The rule nowA cap that scales a value down needs a floor in the unit the cap is about, here half a position. Check that an odd test result is really wanted before trusting a green test.
This post documents something Claude got wrong. The claim rests on a commit or a file, not on memory. All of Claude's mistakes
On 7 November 2026 Henning asked for a review of the whole stock app, with one focus: is it making money? Claude ran the review read-only against the production database. Most of what it found was noise. Strategy settings flipped sign from one three-week window to the next, and the recent loss came from one sector.
The review also found one mechanical fault that had nothing to do with market regime. When a sector was already close to its cap, the engine bought tiny “stub” positions that took up a slot and could never be swapped out. The code was Claude’s, and so was the test that kept it in place.
What the engine did
The live portfolio has ten slots and a sector cap of 40 %. Each buy is sized up to a position cap, and two limits can cut it down: the sector cap (or an exchange cap) and the cash available.
The cut had no floor. If a sector cap left only a little room, the engine bought for exactly that little room.
In one case a sector stood just under the cap, and the engine bought a stock for the small room that was left: 4 % of the position cap. That position then held one of the ten slots for nine days with 0.4 % of the capital, until the trailing stop sold it.
It happened more than once. Two other buys came in at 21 % and 43 % of the position cap. In the live portfolio, 3 of 28 buys were stubs.
Why the stubs could not be swapped out
A slot taken by a stub is not a disaster on its own if the engine can swap the stub out. It could not.
A swap happens when a candidate’s score beats the weakest holding by more than a hurdle, here 0.15. The stubs scored 0.98 to 1.00. No candidate can beat 0.98 by 0.15, so the swap step never treated a stub as the weakest holding. The slot looked full and held almost nothing.
The cost showed up in the impact report on 7 November: the live portfolio was holding 24 % cash in a book meant to be 90 % invested.
All eight stubs came from the sector cap
Claude counted buys below half the position cap across every active simulation in the fleet. Since 14 September there were eight, and all eight came from simulations that run with a sector cap. None came from low cash. Simulations without a sector cap never made stubs. That narrowed the cause to one place: the cap scaled the buy down with nothing underneath.
The error log puts this in the same family as an earlier bug from 8 October, where a cap stopped the top candidate and left the slot empty. In both cases the limit answered “how much”, the answer came out as “almost nothing”, and the code still counted the slot as filled.
The test that locked it in
A unit test was named, roughly, “caps the budget at the remaining room instead of rejecting outright”. It expected a buy of 5,000 against a position cap of 15,000, which is a third of a position. The behaviour that caused the problem was written into the test as the intended result, and the test was green.
The error log says it directly: a green test is a claim about what is wanted, not proof that it is right. Claude did not delete the test. It changed the setup so the room is 8,000 (over half a position) and the expected buy is 8,000, with a comment pointing to the new rule. The change was deliberate and recorded as such.
The fix
Claude added two rules to the target-portfolio function. Both use the same threshold, half a position. A capital-bound path in the engine had used that threshold since 30 July.
- Buying into a free slot. If the sector or exchange cap cuts the budget below half a position, the engine skips the candidate and tries the next one in the queue, which gets a full-size position. The engine writes a note saying which candidate it skipped and why. A cut caused by low cash is deliberately left alone: with little cash and a free slot, a small position is the only way to put the money to work.
- Swapping. An existing position below half size counts as a free slot (score 0 in the comparison) if its sector or exchange is at the cap, because then it can never grow. A small position whose sector still has room is not a stub. It can grow when cash comes in, and the normal hurdle still applies to it. All other guards (minimum hold time, caps, absorption) are unchanged.
New tests cover each case: skipping the stub, buying when the room is at least half a position, swapping out an existing stub, leaving a small position alone when its sector has room, and still buying with low cash. After the change the suite had 291 engine tests and 1,419 in total. Fleet arms without a sector cap produce bit-identical results. The fix was deployed the same night.
After the deploy, the two remaining stubs count as free slots in the swap step while their sector is at its cap.
What we did not change
The review also asked whether the sector cap itself should come down from 40 % to 25 %. In replays, a lower cap mainly reduced variance rather than raising the expected return, so it is a risk choice. Henning owns that choice, and the cap stays at 40 %. A cap of 30 % is being measured going forward in a separate arm.
We also did not build a top-up for underweight positions. The stub swap frees the two slots that matter, and a top-up would mean extra bookkeeping and another fee.
Numbers and method
- Stub count: buys below half the position cap per active simulation, compared with the latest portfolio snapshot on or before each trade date. Trims were excluded. Window: 14 September to 7 November 2026.
- Cash share: the app’s impact report, 7 November 2026.
- Stub sizes: share of the position cap at the time of each buy, from the live portfolio’s trades.
The rule
A cap that scales a value down needs a floor. Measure the floor in the unit the cap is about, here a share of a position, not in kroner. The existing minimum order size only filters out dust. It does not protect position size. When a test expects a weird result, such as a buy a third the size of a position, check whether that result is actually what we want before trusting the green.