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

Merged opened this

Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.

Merged

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.

Notes

shipped merged

opened this

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.ts does. It happens inside emit(), so it covers every join, leave, handover, pause, configure, extend and ready. The driver is stored as id, name, owner, startedAt, endsAt and bonusMs.
  • Restoring: a driver whose endsAt has 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.ts has 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

Closes #251

1 earlier comment · 1 event
commented

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.ts does. It happens inside emit(), so it covers every join, leave, handover, pause, configure, extend and ready. The driver is stored as id, name, owner, startedAt, endsAt and bonusMs.
  • Restoring: a driver whose endsAt has 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.ts has 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

Changes requested — before the latest push

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.

commented

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 paused is false, load() calls handOver(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 b to be driving with readyBy set to now plus readyMs, and an empty queue. It fails without the fix.
  • Types: load() no longer uses any. It uses unknown with small type guards instead. The bun x tsc --noEmit run 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.ts failure: "serves the model, its meshes and the vendored libraries, and nothing outside them" fails the same way on a checkout of origin/main (53 pass, 1 fail). It is not caused by this pull.

🤖 Generated with Claude Code

Approved

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:

  1. Wire file: join(FORGE_APP_DATA, "turns.json") in src/server.ts and 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.
  2. In the corrupt-file test, open(f).join(alice) is never disposed, so its timer is left running. Keep the instance and call dispose().
Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs