Split The code is public; the app answers only to the crew.
A match's numbers must not go backwards: the other half of the posts race #108
Reproduced end to end against a local server at 4161986, so whoever takes this has a failing case to write the test from. Two posts, same matchId, same session, the final one first and a stale periodic one second — which is exactly what a slow connection does:
POST /api/match {"matchId":"1111…5555","rounds":20,"scoreMine":13,"scoreTheirs":7,
"kills":40,"deaths":18,"headshots":10,"damage":6000,"seconds":2000,"finished":true} -> 200
POST /api/match {"matchId":"1111…5555","rounds":6,"scoreMine":4,"scoreTheirs":2,
"kills":9,"deaths":5,"headshots":2,"damage":1200,"seconds":600} -> 200
GET /api/leaderboard
standings: { kills: 9, deaths: 5, headshots: 2, damage: 1200, rounds: 6, wins: 1 }
recent: { score_mine: 4, score_theirs: 2, rounds: 6, seconds: 600, finished: 1, state: "finished" }
A 13–7 match with 40 kills and 6,000 damage is now a finished 4–2 with 9 kills and 1,200 damage. The flags held — state is still finished, which is #50's guard doing its job — and every number went backwards, permanently, because the row is the only record there is.
Two things this transcript settles for the fix:
secondswent backwards too (2000 → 600), and it is in the list this issue asks to make monotone — worth saying explicitly, because it is also whatdb.ts's ten-minute age rule reads when it decides a row is abandoned.- The standings follow the row, not the other way round: the aggregate is computed from
match, so one rewound row silently rewrites a player's career totals. There is nothing to repair afterwards, which is why the guard has to be insaveMatchand not in a later sweep.
The same two posts in the other order are the control: send the stale one first and the row ends correct, which is the test pair — same inputs, either order, same stored row.
saveMatch(src/db.ts:123) carries this comment, written when #50 landed:The race is real —
main.tsfiresvoid postResult(...)once a round and again at the end, never awaiting the one before — but only the two flags were protected. Every number in the same statement is stillexcluded.<column>:So the post that arrives last wins the numbers. A match that finished 5–3 with 22 kills, whose round-4 post was stuck in a slow connection, ends up on the board finished and 5–3 with 18 kills, permanently. The row is also the only record of a match, so nothing repairs it later.
Done looks like
Files I expect to change:
src/db.ts,src/db.test.ts, possiblysrc/client/main.ts.Risks:
MAX()in SQLite with aNULLargument isNULL— every one of these columns isNOT NULL, check that before relying on it; the upsert is one statement today and should stay one if it can, because two statements need a transaction to be worth anything; andupdated_atmust keep moving on every post, or the "abandoned after ten minutes" rule starts firing on a live match.Brief that fits: scout — the database and its tests.