Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
src/turns.ts can keep the queue order and the current driver's turn end in a file and restore them - module and test only #266
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
Turns now takes an optional file. With it, the queue order and the driver are written on every change and restored when a new Turns is built on the same file. Without file, nothing changes, and no caller passes one yet.
shipped merged
1 earlier comment · 1 event
Turns now takes an optional file. With it, the queue order and the driver are written on every change and restored when a new Turns is built on the same file. Without file, nothing changes, and no caller passes one yet. Only src/turns.ts and src/turns.test.ts changed, and forge.json is untouched.
- Saving: the write goes to a tmp file and is then renamed, as
chat.tsdoes. It happens insideemit(), so it covers every join, leave, handover, pause, configure, extend and ready. The driver is stored as id, name, owner,startedAt,endsAtandbonusMs. - Restoring: a driver whose
endsAthas passed is dropped, and the queue is kept. A restored driver gets a fresh idle clock and no ready deadline. - Dropped driver, queue kept: the head of the line does not take over until someone calls
join. I left that alone to keep this piece small. - Bad files: a missing file starts empty without a log line. An unreadable or malformed one starts empty with one logged line.
- Tests:
turns.test.tshas three new tests with the injected clock: same queue and driver after reopening, an expired turn is dropped while the queue stays, and a corrupt file starts empty. The 18 tests in that file pass. The type check printed nothing but "bun is unable to write files to tempdir: ReadOnlyFileSystem", so I can't say it was clean. - Full suite: one failure in
server.test.ts("serves the model, its meshes and the vendored libraries…") returns 404 instead of 200. It is in the model routes, not in turns, and I didn't look into it.
🤖 Generated with Claude Code
Persistence works for the happy path, but restoring an ended turn leaves the line stuck. Fix first: after load(), if there is no driver and the queue is not empty (and not paused), call handOver(null) so the head is called, as check() does when a turn ends. Right now the test "a turn that has ended is not restored, the line is" asserts driver null with b waiting, which is a stuck state: nobody is promoted until a new join. Change that test to expect b to be driving (with the ready deadline) so it fails without the fix. Also: the full-suite server.test.ts failure was left uninvestigated; please say whether it fails on main too. The type check was not confirmed clean (tempdir read-only); use any sparingly in load (person/d are any). Otherwise scope is right: module and test only, atomic tmp+rename, corrupt file handled.
Turns now takes an optional file. It saves the queue order and the driver's turn end atomically on every change (tmp file, then rename) and restores them on construction. With no file nothing changes, and no caller passes one yet. forge.json is untouched.
- Stuck line (review fix): after restoring, if there is no driver, the line is not empty and
pausedis false,load()callshandOver(null). An ended turn no longer leaves the head of the line waiting. A turn whose time has passed is still not restored. - Test: "a turn that has ended is not restored; the head of the line is called" now expects
bto be driving withreadyByset to now plusreadyMs, and an empty queue. It fails without the fix. - Types:
load()no longer usesany. It usesunknownwith small type guards instead. Thebun x tsc --noEmitrun still only printed "bun is unable to write files to tempdir: ReadOnlyFileSystem", so I can't confirm it clean here. The 18 turns tests pass. server.test.tsfailure: "serves the model, its meshes and the vendored libraries, and nothing outside them" fails the same way on a checkout oforigin/main(53 pass, 1 fail). It is not caused by this pull.
🤖 Generated with Claude Code
Approved. It does what the issue asked: an optional file on Turns, saved atomically (tmp then rename) from emit(), restored on construction, and untouched when no file is given. Only turns.ts and its test changed, and no caller uses it yet, so there is nothing for the preview to show. I did not run the preview. A bad or missing file starts empty, an ended turn is dropped, and the head of the line is called. The three new tests cover those cases, and the ended-turn test would fail without the handOver(null) fix. The server.test.ts model-route failure also fails on main, so it is not from this pull. No new dependency and no security surface.
Two things to do next:
- Wire
file: join(FORGE_APP_DATA, "turns.json")insrc/server.tsand add a test that a restart keeps the line. A restored driver has no connection until they reconnect, so confirm that the idle clock (lastActive: now) gives them a fair grace period. - In the corrupt-file test,
open(f).join(alice)is never disposed, so its timer is left running. Keep the instance and calldispose().
Turnsnow takes an optionalfile. With it, the queue order and the driver are written on every change and restored when a newTurnsis built on the same file. Withoutfile, nothing changes, and no caller passes one yet. Onlysrc/turns.tsandsrc/turns.test.tschanged, andforge.jsonis untouched.chat.tsdoes. It happens insideemit(), so it covers every join, leave, handover, pause, configure, extend and ready. The driver is stored as id, name, owner,startedAt,endsAtandbonusMs.endsAthas passed is dropped, and the queue is kept. A restored driver gets a fresh idle clock and no ready deadline.join. I left that alone to keep this piece small.turns.test.tshas three new tests with the injected clock: same queue and driver after reopening, an expired turn is dropped while the queue stays, and a corrupt file starts empty. The 18 tests in that file pass. The type check printed nothing but "bun is unable to write files to tempdir: ReadOnlyFileSystem", so I can't say it was clean.server.test.ts("serves the model, its meshes and the vendored libraries…") returns 404 instead of 200. It is in the model routes, not in turns, and I didn't look into it.🤖 Generated with Claude Code
Closes #251