Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Lock Keeper rules: a tested `lock-keeper/lock.js` that works boats through a flight of canal locks from plain data #154
Check · Review · Land · Live done — Landed and live in production.
I added the Lock Keeper rules in lock-keeper/lock.js with tests in lock-keeper/lock.test.mjs. The new test file passes (11 tests, about 7 s, almost all of it the par search). scripts/gate.ts and scripts/fast.ts don't exist in this clone, so I didn't run them.
shipped production is serving it deploy ab54f53
The rules read correctly and the tests cover each refusal, the flood and the win. One thing has to change before this lands.
Fix first: newRound(seed) calls par(seed). For any seed outside the shipped 1, 2 and 3, that runs a synchronous breadth-first search of up to 80 levels on the caller's thread. The test file takes about 7 s mostly because of this, and newRound(7) in the determinism test pays for it. Starting a round on an unshipped seed will freeze a page or a request far past the 50 ms bar. Make newRound O(1). Either leave par out of the state (use the table value or null) and expose par(seed) as an explicit call, or restrict newRound to SEEDS. Add a test that newRound(7) returns quickly and that its par is a number or null, never a hang. shortest can also return null after 80 levels, so line must not print par null.
Also fix:
newRoundBareduplicates the lock literal innewRound. Extract onefreshLocks()and use it in both.- The
PARScomment has stray alignment spaces. throughis exported and used only inside the module. Keep it only if the page will call it.
I addressed all of @juniper's points and pushed to the same branch. The new test file passes (12 tests). I didn't run tsc or the other checks again after this change. The test file still takes about 5.5 s because of the par-reachability test, which runs the search on purpose.
newRoundis O(1):par(seed)now returns the table value for seeds 1 to 3 andnullfor any other seed, with no search.shortest(seed)is the explicit, slow way to find a par. I dropped the memo cache because nothing needs it now.linewith no par: it leaves out the "(par …)" part whenparisnull, so it never printspar null.- New test:
newRound(7)returns in under 50 ms, itsparisnull, and itslinehas no mention of par. Seed 1 still reports par 36. freshLocks(): one shared function now builds the locks, andnewRoundBareis gone.- Cleanups: I fixed the stray spaces in the
PARScomment and removedthroughfrom the exports, since only the module uses it.
🤖 Generated with Claude Code
Reviewed every line of lock-keeper/lock.js and its tests. It is a pure rules module with no I/O, no network, no storage, no eval and no secrets, so there is no injection, authorization or escaping surface. apply() validates lock and act at the boundary, returns the same state object on refusal and never throws. newRound is O(1) now that par is a table lookup. I checked the gate and paddle rules and the flood rule by hand, and the tests cover the refusals, the flood, the win and the determinism of the queue. I did not run the tests: the checkout I had did not contain lock-keeper/. Like the sibling rules pulls (ferryman, hoopla, tower-duel), it ships the rules only, as the issue asks. Nit, not blocking: the PARS comment is still spaced differently from its neighbours.
I added the Lock Keeper rules in
lock-keeper/lock.jswith tests inlock-keeper/lock.test.mjs. The new test file passes (11 tests, about 7 s, almost all of it the par search).scripts/gate.tsandscripts/fast.tsdon't exist in this clone, so I didn't run them. I did not runscripts/design-lint.tsbecause nothing insrc/web/changed.What it does
loworhighlevel, a top and bottom gate, and a top and bottom paddle.apply(state, { lock, act })takespaddle-top,paddle-bottom,gate-top,gate-bottomormove. It returns{ state, ok, why, events }.uneven-water,gate-open,gate-closed,no-room,no-boat,round-over,no-such-lockorno-such-act. It never throws.reason: 'flood'.move, only through an open gate. A lock holds one boat and a middle pound holds two.opsagainstpar.newRound(seed)gives one boat waiting at each end and, for some seeds, a third, so 2 or 3 boats. The same seed always gives the same queue. Shipped seeds are 1, 2 and 3.shortest(seed), over every legal operation sequence that avoids a flood. The test checks that the search gives the same figures, so par is reachable and no shorter solution exists.Choices to know about
I did not touch
shell.js,index.htmlorrounds.mjs, and I added no page and no catalogue entry, as the issue asked.To try it, run
bun test lock-keeper/lock.test.mjs.🤖 Generated with Claude Code
Closes #140