Regenerating examples/programs/notes/
Every function body in the seven modules of program 4 was written from the modules’ own
intents, comments, signatures, contracts and tests, plus the spec at
mo-wiki/spec/programs/04-web-backend.md. The tests were not touched: every test,
test rejects and property block is byte-identical to the stripped tree.
Done-when
mo checkclean onlimits.mo,note.mo,store.mo,api.mo,service.mo,server.mo,main.mo.mo checkalso matched both recipes:Recipes.RateLimiter.RateLimiter(3 signatures) onlimits.moandRecipes.Store.Store(11 signatures) onstore.mo, and ran each recipe’s own tests green on the implementation.mo testgreen on all seven: 42 passed, 0 failed, 0 skipped.mo test --sim 100green on all seven: 8 process tests under 100 seeds with 5% faults, 8 held under faults, 0 passed only without faults; bothServiceinvariants kept, 0 tripped.- All five
# run:lines print their.expectedfile byte for byte and exit as declared:check data/demo data/session.txt(0),compact data/compact(0),serve(2),serve data/nowhere(1),client 127.0.0.1 1 ada GET /notes(1). mo fmt --checkclean on all seven.mo test --write --sim 100rewrote everyverified:line and theprograms/.mo.idssidecar.data/demoanddata/compactare unchanged by the runs (compaction is idempotent on an already-compact log; a check writes onlynotes.check.log, removed before and after).
Wall clock: 17 minutes (00:43:41 UTC to 01:00:33 UTC, 16 September 2026).
Loop count by cause
Three failures in all, every one a mo check grammar diagnostic in server.mo, and every one
fixed by the next edit.
| # | Command | Code | What | Next edit fixed it |
|---|---|---|---|---|
| 1 | mo check server.mo | MO0101 | fn(kept: Note) ... — an anonymous function’s parameter may not carry a type annotation | yes |
| 2 | mo check server.mo | MO0101 | Next(now): held = now — a case arm holds one expression, and an assignment is a statement | yes |
| 3 | mo check server.mo | MO0102 | Stopped: break — same law, for break | yes |
mo checkfailures: 3 (MO0101 ×2, MO0102 ×1). First fix worked every time.mo testfailures: 0.- Run failures: 0 — all five
# run:lines matched their.expectedon the first run.
Not counted as loops, since they are not check, test or a run: mo fmt --check reported
layout differences in four files (limits.mo, service.mo, server.mo, main.mo) — a
blank line after a contract block, case arms and list literals that fit on one line, and a
multi-line one-line if that wants block form. mo fmt fixed all four, and the verified:
lines were rewritten afterwards.
Decisions the spec did not cover
- How an id is never reused. The spec states the rule but not the mechanism. Ids are
reserved in blocks of 100 under the store key
ids: before the first create whose id is at or past the reserved floor, the service appendsSET ids <next_id + 100>; on replaynext_idis the larger of that floor and one past the highest id in the log. This is whatdata/demo/notes.log’s first line (SET ids 101) encodes, and why the check’s first create isn_101and notn_6. idsis a store key but not a note./health’snotesis the sum of the per-client live-note counts andclientsis the number of clients holding at least one live note; a client whose last note is deleted leaves the table. The reservation key is never counted, never listed, and never reachable through a route.- The store key of a note is
<token>/<id>. A token cannot hold a slash (the rate limiter’s token rule), so no client’s keys fall under another’s — that is what makes “a client never reads, changes, or deletes another client’s note” checkable as a string property in the service’snever. - The wording of the 400s.
notes.expectedfixed four of them (the body is not JSON,title is missing,title must be a string,title must be 1 to 200 bytes with no control characters). The two the corpus does not pin were written to match:the body must be a JSON objectandbody must be at most 60 KiB with no control characters but newlines. - A malformed body still takes a token. A body the Api cannot read becomes a
Refusecall on the service rather than an immediate 400, so 401 still wins over 400 and a 400 counts against the client’s minute. The service answers it without touching the store. - Route precedence. The method is decided before the token, and an unknown route before
both:
GET /nowherewith no token is 404,PATCH /notes/n_1with no token is 405, and only a route-and-method the service has reaches the 401 check. - “No control characters” read as C0, DEL and C1.
note.mo’s own test refuses U+0085, so the rule is: no U+0000–U+001F (a newline allowed in a body), no U+007F, and no U+0080–U+009F. Sizes are counted in bytes: a title is 1 to 200, a body at most 61,440. - The two 503s. A change the log did not take is
503 {"error": "the log did not take the change"}; an ask the service does not answer in 30 seconds is503 {"error": "the service did not answer in time"}. Thirty seconds is longer than the slowest change the store can make (a 20 s rewrite plus a 5 s rename plus a 5 s append), so in practice the store decides every 503 and the store is unchanged by it — which is whatserver.mo’s wire test asserts. - A full service. The
state.notes.size <= 1_000_000invariant is guarded, not tripped: a create at 1,000,000 keys is refused with 400the service holds a million notes already. serve’s banner.notes serve <dir>writesnotes: serving <n> notes on 127.0.0.1:<port>on stdout and then hands the loop to the runtime; a log whose last line was cut short is compacted first, so the next change starts on a line of its own. No# run:line exercises the success path.- What a check writes.
notes checkreplays the folder’s log, moves the store tonotes.check.logbeside it, and appends every change — the id reservation included — to that file only; it is removed before and after, sodata/demo/notes.logis untouched. A file left over from an earlier check that cannot be removed is exit 1. - What “steadied” masks. Exactly four keys:
created_atandupdated_atbecome"<key>",uptime_msandretry_after_msbecome<key>. Masking is per response line and repeats, so every note in a listing is steadied. - A change the log refuses still spends its id.
issuedbumps the counter before the log is asked, so a create answered 503 burnsn_krather than handing it out twice. tornis sticky. A log that may end in part of a line is rewritten whole with the store’scompactbefore the next change; if that rewrite itself fails, the flag stays set and the next change tries again. This is what lets work resume once faults stop, and it is whatservice.mo’s torn test and the 100-seed fault runs exercise.- One private helper added.
server.mogrewmissed, which reads a 404 as the right answer for a move on a note the client does not hold — needed because under injected faults a create can be answered 503 and the moves after it then carry no id. Nothing else was added beyond the declared signatures.