DEV Community

Cheikh Mbacke Cisse
Cheikh Mbacke Cisse

Posted on

Two algorithms died before I could prove a debt plan was safe

This post is written as part of the Kiroween 2026 hackathon.

Most financial planners answer the wrong question. You tell them your debts and your income, and they tell you a date: debt free in seventeen months. What they mean is debt free in seventeen months if nothing goes wrong. For someone with a steady salary and six months of savings, that is a reasonable thing to say. For someone whose income drops by a third twice a year, it is useless. They will not live the average. They will live the month it breaks.

So I set out to build something that answers a different question. Not how long will this take, but: is there a plan that gets me there whatever the world does, inside limits I declare myself? Not a projection. A proof, scoped to a stated model.

This is the story of getting that wrong twice before getting it right, with the numbers from each attempt. I built it with Kiro during the Kiroween 2026 hackathon.

The shape of the problem

Write the situation as a game. You have a state: cash on hand, a balance per debt, and the month. Each month you choose a move: pay the minimums and bank the rest, put the surplus on the highest rate, put it on the smallest balance, or build a buffer first. The world moves too: your income can drop, an expense can land, a rate can rise. You declare what the world is allowed to do, and how often.

Two absorbing states end it. All balances at zero is a win. Not being able to cover the minimums is a loss, and there is no coming back from it.

A plan is guaranteed if, from your position, there is a move such that every reply the world is allowed to make leads to a position that is itself guaranteed. That is the whole definition, and it is recursive, which is the clue I should have read more carefully.

Attempt one: search the tree

The obvious implementation is a game tree with alpha-beta pruning. It is the thing every chess engine does, the problem looks like a game, so I wrote the spec that way and asked Kiro to build a spike before anything else.

The spike is the part I would keep if I could keep only one habit. It is a throwaway script whose only job is to measure the thing the whole design rests on, before the design has anything resting on it.

The first results looked fine. Two starting positions, 14,410 nodes in 0.17 seconds for one and 2,815 for the other. Comfortable.

They were comfortable because in both positions the world could force a loss by month two. The tree was cut off almost immediately. The measurement said nothing about the case I actually cared about, which is a position where a guarantee exists and the search has to go all the way to the horizon to find it.

When I scanned for such a position, the first one took 14,219 nodes. The second never finished. The process ran until it stopped responding, and I had to kill it from Task Manager.

Here is why, and it is not subtle once you count. Four move types, one of which takes a parameter from zero to six, gives about ten moves per month. Three possible shocks gives the world roughly three replies. Thirty branches per month, over thirty-six months. Alpha-beta prunes that to something like its square root, and the square root of an astronomical number is an astronomical number. Plain minimax, for comparison, needed 1.5 million nodes at a horizon of five and 23.5 million at six. Fifteen times per step.

The spike did its job. It cost an hour and it killed an approach that would have cost a day.

Attempt two: solve the states, exactly

The fix is to stop thinking of it as a tree. The same position reached by two different routes is the same position, and in a tree you solve it twice. In a state space you solve it once.

So: enumerate the reachable states, then work backwards from the two absorbing sets. A state is winning if some move sends every allowed reply into a state already known winning. Each state is visited once. Exponential in depth becomes linear in states.

The question is how many states there are, which depends on how you discretise. Cash is continuous, so it has to be bucketed. Balances, I argued, do not: they follow exactly from the sequence of payments you chose, so keep them as exact integers and the model stays sound with no rounding error at all.

That argument is correct and it is also the reason it fails.

Month Distinct states Elapsed Memory
6 17,094 0.2 s 53 MB
8 212,186 2.3 s 138 MB
9 725,356 8.1 s 334 MB
10 2,443,107 28 s 588 MB
11 8,110,351 106 s 1000 MB

Roughly 3.4 times per month, with no sign of slowing. I killed it at month twelve, and the model needs thirty-six.

Balances do follow exactly from the payment sequence. That is precisely why there are as many of them as there are payment sequences. With interest rounded every month, two different routes almost never land on the same balance, so nothing merges, and the state space quietly turns back into the tree I had just escaped.

Attempt three: bucket everything, and round against yourself

Bucket the balances too. Round them up, round cash down. Always round against the user.

That rounding rule is the part worth stealing. If every approximation is made in the direction that makes the position look worse than it is, then a state the table marks safe is safe for every real value inside its bucket. The abstraction is sound by construction, and no proof of monotonicity is needed.

With cash in buckets of 25,000 the whole table solves in 1.8 seconds and 110 MB, over 458,428 reachable states. At 10,000 it is 790,382 states, 3.6 seconds and 169 MB. Both are fine in a web worker, and the table is computed once per position, after which every question is a lookup.

But look at what the two runs say:

Cash bucket States Proved winning Verdict
25,000 458,428 19,587 36 months
10,000 790,382 297,207 31 months

Five months apart, and fifteen times as many positions proved safe at the finer bucket. The coarse table is not wrong. It is pessimistic, which is what sound approximation buys you: it never claims safety it cannot back, and it pays for that by missing safety it could have had.

Which forced a change to the product, not just the code. The answer is not "debt free in nineteen months". It is "debt free in at most nineteen months, guaranteed". An upper bound. Once you say it that way the pressure to find the perfect bucket size disappears, because a coarser table is still true, just less flattering.

What Kiro did in this

Three things, concretely.

The spec came before the code. Requirements in EARS form, written and argued over before a line existed, with Kiro generating the design from them. The requirement that mattered most reads: "the engine shall search with the adversary choosing, at every step, the move that is worst for the user among the moves the domain file allows" and "the engine shall not use probabilities anywhere in the guarantee computation". Both algorithm failures above were caught against that text, not against an opinion.

A steering file kept the architecture honest. The hard rule of this project is that the language model never produces a number: it turns your sentences into a form you confirm, and turns a result into prose, and is not called during a solve. That rule is one paragraph in .kiro/steering/architecture.md, reloaded on every interaction, and it says in plain words that writing a number into a prompt and reading one back out is the failure the rule exists to prevent. At hour fourteen, tired, that paragraph is doing more work than I am.

Property based tests, run by a hook on every save under engine/. The one I care about: backward induction returns the same value as exhaustive forward search, on every small generated game. That is the property that says the fast algorithm is the slow algorithm, and it is the only reason I trust a table of half a million states I will never read.

And one thing that went badly, since a report that only lists successes is not a report. Kiro's terminal wedged for several hours after a runaway Node process from the first spike, returning exit code -1 for every command including echo. The fix was dull: kill the process from outside the editor, restart. The lesson was not: put a hard time and memory guard inside the spike itself, before you run it the first time, not after it has eaten your night.

The part I would tell someone starting

Measure the thing your design rests on before the design rests on it, and make sure you measure the hard case. My first spike was fast because it was solving an easy position, and a fast wrong measurement is worse than no measurement, because you believe it.

And when an approximation forces you to be pessimistic, say so in the product instead of fighting it. "At most nineteen months, guaranteed" is a better sentence than "nineteen months", and it has the advantage of being true.

Top comments (0)