REPORT: examples/programs/ledger/ regenerated from its spec
Every function body in the fourteen modules of program 6 was written again from
mo-wiki/spec/programs/06-ledger.md, the modules’ own intents, signatures, contracts, nevers,
invariants and tests, and the .expected transcripts. The tests were not touched, and no other
program was touched.
Done-when
mo checkis green on all fourteen modules (store.mo: recipe Recipes.Store.Store: 11 signatures match).mo testis green on all fourteen: 78 tests, 0 failed, 0 skipped, plus one property at 200 seeds.mo test --sim 100is green on all fourteen;journal.moreports invariants (kept 4, tripped 3) andserver.moholds under--faults 20 --until 0.5.- All five
# run:lines ofmain.moprint their.expectedfile byte for byte and exit with the code on their# exit:line (check→ 0,compact→ 0,serve→ 2,serve data/nowhere→ 1,client 127.0.0.1 1 …→ 1). Thecheckrun was repeated to confirm it is deterministic. mo test --write --sim 100has rewritten everyverified:line and theprograms/.mo.idssidecar; the sidecar diff touches only the fourteenledger/*.morows.
Wall-clock
46 minutes 32 seconds (2026-09-16T01:43:53Z → 02:30:25Z), one session, no parallel workers.
Loops to green, by cause
Six failing mo invocations in all. Every one of the six was fixed by the next edit.
| # | Module | Cause | Fix | First fix worked |
|---|---|---|---|---|
| 1 | index.mo | MO0403 — Time.fixture() in the non-test helper t0() | Time.from_parts(2_026, 1, 1, 0, 0, 0) | yes |
| 2 | book.mo | property: requires key?(key) tripped — the walk’s key "w#{n}" spelled a 20-digit run, which card_run? refuses | "w#{n % 100_000}" | yes |
| 3 | desk.mo | MO0102 — return on a case arm’s own line | flushed restructured as a block-form if value plus a took helper | yes |
| 4 | desk.mo | MO0307 — anonymous-function parameter s bound but never read | renamed to _ | yes |
| 5 | journal.mo | MO0303 — readied took 8 parameters, the limit is 6 | the four carried values grouped into a private Started struct | yes |
| 6 | journal.mo | test failure: a retry with the same key … one with a different body is 409 — the test helper call used the key as the whole request fingerprint, so a different body compared equal | the request became "#{key} #{Json.encode(command)}" | yes |
By kind: four checker diagnostics (MO0403, MO0102, MO0307, MO0303), one contract tripped by a
property, one test expectation. Ten of the fourteen modules — money, entry, settle, teller,
store, records, api, check, main, server — were green on their first mo test, and all
five program runs matched their .expected file on the first mo run.
One correction was made before any mo invocation and is not counted above: money.mo’s day?
first used a try? form that Mo does not have, replaced by a case on Time.parse while writing.
Decisions the spec did not cover
- Refusal rule names. The spec fixes the statuses but not the
rulestrings. Chosen, and made to match the wording already inledger.expected:no_account,same_account,currency,insufficient,over_hold,closed,no_hold,over_capture,no_capture,settled,future_day.refusal_bodywrites{"error": …, "rule": …, "by": N}; a refusal that never reaches a rule (a missing account onGET /accounts/{id}, a listing of an account that is not there) is written as{"error": …}alone. - The shape of an entry’s JSON per kind. A transfer carries
amountand noaccount; a settlement carries neither; a hold, capture, release and refund carry both. The order isid, kind, [account,] [amount,] postings, key, at, thenexpires_at,hold,capture,reason,dayas each applies. - The clearing account.
clearing:<CUR>, one per currency, with noAccountrecord of its own — so it is never counted by the overdraftnever, never written as an account record, and appears in a statement only for a day it moved on. - Statement order. The book’s accounts first, by their number zero-padded to twenty digits
(so
a_10followsa_9), then the clearing accounts that moved, by id. Statement.settlementholds the entry number the statement was cut at, shown ase_N; a settlement shows every entry numbered below its own, which is what makes the same statement reproducible later.- The store’s name for a key is
i_and the key’s bytes in lowercase hex, and a key row is{"made", "status", "request", "body"}withmadefirst — both read offdata/demo/ledger.log, which the spec does not describe. - The request a key is compared by is
"<METHOD> <path>\n<body>"(Api.fingerprint). - What a key row keeps. A body only for a refusal (status ≥ 400) and for a settlement, whose statement would otherwise change once a later account opens; every other repeat is derived again from what the key made (the entry as it is, the account as it stands).
- Which accounts a call is a “look” on. The account(s) the command names; the hold’s account for a capture or a release; the capture’s account for a refund; every account for health; none for opening an account or for settling a day.
- Paging. Entries, closed holds and captures in pages of 256 by number; key rows in 1,024
buckets by
hash * 31 + byte, the same mixing the store uses for its 256 buckets. - Listings. Kept as lists under
"<account>/<kind>", an empty part standing for “all”, and capped at a hundred when the number is pushed rather than when the list is read. balanced?is no entry’s postings are unbalanced and the balances sum to the journal’s postings. The planted bug (a transfer written as two records) satisfies the second conjunct exactly — the balances read as a transfer’s would — and is caught by the first.covered?counts only the accounts the book opened, each as balance plus overdraft; clearing accounts have no overdraft and are left out.- The batch on the log is a
BEGIN <n>line and its nSETlines; a replay takes all of them or, when the log ends inside the batch, none, and the table is thencut.BEGIN 0is a bad line. - Expire timing. An
Expireis sent when a hold is staged, atdelay_to(expires_at, now), which is a second for a hold already past, so a release the log refused is tried again. A refused flush rolls the book back to what the log holds and re-arms anExpirefor every live hold in that book, which is what keeps the third invariant true across a rollback. - The journal’s message shapes.
Servestages, flushes and collects in one update;Collectflushes only while its ticket is still waiting, so calls staged together share one append; the acceptor’sIdleasks for aFlushbounded at 10 seconds. - The worker’s two deadlines, since an exchange carries none of the client’s: 10 seconds to stage, 30 to collect, and a timed-out collect is 503.
- A journal that has not opened retries opening at every message, not only on
Open(the privatereadiedhelper), so a fault window during start does not leave it closed for the rest of the run. wait Nin a check script is a request to a listener of the check’s own that nothing ever accepts, timing out after N ms, printed aswaited N ms.steadymasksat,expires_at,created_atanduptime_ms, then replaces the clock’s day with<today>; nothing else is touched.- Private helpers the stripped signatures did not name were added where a nested closure, the
parameter limit or the no-
return-on-an-arm rule forced one:value_fields(entry);summed_in(book, settle);item_of,shown_item(settle);whole_text,pending_taken,emptied,applied_if(store);taken_pending,emptied,bad_at,entry_pairs,key_pairs(records);took(desk);made_under(teller);Started,readied,armed,made(journal);balance_in(server). - Four signatures the stripper had cut mid-return-type were restored:
Index.pushed → Map(String, List(UInt64)),Book.captured,Book.released,Book.refunded → Result(Moved, Refusal), andRecords.flushed_to → Result(Table, StoreError). The contracts on the threeBookfunctions went with them and were written again from thetest rejectseach must trip:requires key?(key)oncaptured,requires reason?(reason)onreleased,requires amount?(amount)onrefunded, plus theensuresthe spec asks for on a capture. - Test fixtures the spec left open. Ada’s overdraft is 10,000 and grace’s 0 throughout;
Teller.t0()andRecords.t0()are 2026-09-14T10:00:00Z so that settling that day is not a future day;Journal.startedmakes the folderdbefore it opens, since the journal tests do not.