A model checker found six races in the coffee booking flow
Booking a coffee at /coffee writes to three stores that can't commit together. On 2026-11-28 a TLA+ model of that flow, checked by everystate, found six interleavings that break it. Two of them replayed against the real code 10 times out of 10, and one ends with two guests on the same half hour.
One booking lived in three stores
Before the fix, a booking's state was split three ways. The KV record booking:<id> was read, edited and written back whole, so the last writer won. A KV key held:<slot> hid the slot from /slots, and its delete never checked who owned it. A claim in the Counter Durable Object refused a second booking on the slot, and that one was atomic and checked the booking id.
Each route kept the three in step by ordering its writes. KV has no compare-and-set, so a read and its write are two steps, and another request can land between them. cal's own tests already called that gap "the window KV cannot close" (#1577). The tests treated it as a duplicate mail. The model asks what else it costs.
Step through the first finding
This is everystate's counterexample for finding 1, copied state by state from its trace. Booking b1 starts pending on slot s1. An approve and a decline for b1 start at once, and a second guest, b2, wants the same half hour.
Step to 4. The decline has already written declined, and the approve writes confirmed over it from a copy it read before the decline landed. The decline never rereads the record, so it frees s1 in both stores and mails a decline anyway. At the end b1 is confirmed on s1 and b2 is booking s1.
Now tick compare-and-set and step again. At step 4 the approve's write checks the live record, finds declined, and loses. b1 gets one mail, and b2 books a slot nobody holds.
Six findings, and what a compare-and-set fixes
Each finding is a run of the model that breaks one named property. I reran every one on 2026-11-30 with the record write switched to a compare-and-set, the guard a D1 conditional update or a Durable Object would give it.
| # | What interleaves | Breaks | On the real code | Compare-and-set |
|---|---|---|---|---|
| 1 | An approve and a decline both land in the record's read-write gap | NoDoubleBooking | replayed, 10 of 10 | clears it |
| 2 | The expiry step is retried after its slot release throws, finds the booking expired, and releases nothing | NoLeakedClaim | replayed, 10 of 10 | still fails |
| 3 | A reschedule writes back a record a decline already retired, and the booking returns as pending | TellsTheTruth | model only | clears it |
| 4 | A double-submitted reschedule frees the slot it just moved into | LiveOwnsSlot | model only | still fails |
| 5 | A request cut between its steps leaves a claim with no hold, so the slot shows free and refuses everyone | NoLeakedClaim | model only | still fails |
| 6 | An approve races the expiry timer, mails an invite, and the slot is freed under it | LiveOwnsSlot | model only | clears it |
A compare-and-set clears 3 of the 6. The other three need the record, the hold and the claim to change in one step, and nothing in the old design could see all three at once. Two guard patches tried on 2026-11-28 left them failing. A seventh came from reading the design: the daily cap was counted from a KV list, so two guests on different slots each passed a cap of one.
The replays used a KV proxy that held each write until the other request had read. Without that hook neither outcome happened, which is the control: the proxy forces the interleaving, and the code does the rest. The fix in #1939 moves every booking into one Durable Object instance, A second model, One gap stays on purpose. Mail goes out at most once, as before: a request cut after its ledger call keeps the change and drops the mail. The model shows that too, as two runs expected to fail. The model is small on purpose: 3 bookings, 2 slots on one day, and time as a single flag for whether b1's pending window has lapsed. It leaves out KV's cross-region lag, which only widens the old windows, plus the calendar feed, the weekly cap and the 90-day sweep. The model of the old code is a hand translation of One owner for all three
coffee-ledger, on the Counter class that already held the claims (#268 added them). A slot counts as held because a live booking sits on it, so the hold is derived from the status and there's no second copy to drift. Each route makes one ledger call and mails only when the ledger says it moved the booking.CoffeeLedger.tla, checks that design with all 9 actors, crashes and retries: 11,674,788 states, and every safety property holds. A model that never fails proves little, so the suite also plants 4 defects in the ledger, such as splitting one call into two, and each one fails the property that guards it. The models and their runner live in cal/model/.What the model leaves out
cal/src at 19665e4c. Findings 3 to 6 rest on that translation being faithful, which is why the two that could be forced were replayed on the code. The runner takes about 6 s and isn't wired into CI yet.