Public: anyone can read this; the crew can change it.

Migrated from github.com/Trevato/SO-ARM100

STOP can lose to a command already in flight: the relay must drop what it holds, and the Pi must refuse commands older than its last STOP #435

Merged opened this in answer to

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

Merged

A STOP now beats any command the relay had already accepted, and the Pi refuses commands stamped before its last STOP. Tests. With four CPU burners running, the STOP property test (the "STOP through the relay" group, cases a–c) passed 24 of 24, which is 8 reruns of 3 tests.

Notes

shipped merged

opened this

A STOP now beats any command the relay had already accepted, and the Pi refuses commands stamped before its last STOP.

Tests.

  • With four CPU burners running, the STOP property test (the "STOP through the relay" group, cases a–c) passed 24 of 24, which is 8 reruns of 3 tests. That is short of the 50 of 50 the issue asks for, because each rerun takes about 11 s here.
  • The affected suites (relay, gate, stop, frame-limit, teleop, slider-follow, leader-arm, machine-feed, fake-serve) pass.
  • I did not run the rest of bun test or the unix-socket suites; the forge's check runs those.
  • tsc reports one error, in src/visitor-counts.ts, which I didn't touch.

Relay (src/relay.ts)

  • On POST /stop the relay now shuts the gate, raises a STOP epoch, and terminates every arm-side /follow socket. That discards anything accepted but not yet written. Only then does it close the browser streams and forward the STOP.
  • The epoch is a clock-based counter, so it never repeats across a relay restart. It goes out on the STOP body and on every frame forwarded to the Pi.

Pi (src/command-gate.ts, src/server.ts)

  • POST /stop records epoch, and the gate drops any frame with a lower epoch (before-stop), however the network orders them.
  • Frames with no epoch, such as the LAN leader's, are unaffected.

Tests

  • command-gate.test.ts covers the epoch rule.
  • stop.test.ts: the fake Pi now enforces the epoch the way the real one does. New case (c) runs the interleaving property with each write landing 50 ms late.
  • frame-limit.test.ts and relay.test.ts compared forwarded frames exactly. They now strip the epoch field first; the tests were right, the wire format changed.

Real-Pi address in tests: src/fake-serve.test.ts spawned bench/fake-serve.ts with all of process.env. In the check cell that includes the live ARM_SERVER, so the child dialled the real Pi at 100.89.95.68. It now drops every ARM_* variable. That is my inference from reading the code. I did not reproduce it, since ARM_SERVER isn't set here. The other tests that mention that address only parse strings.

An unclean arm-socket close, as the relay now does on STOP, may be treated by the real Pi as a lost driver rather than a deliberate close. The STOP holds the Pi anyway, so I left it.

🤖 Generated with Claude Code

Closes #434

2 earlier comments · 1 event
commented

A STOP now beats any command the relay had already accepted, and the Pi refuses commands stamped before its last STOP.

Tests.

  • With four CPU burners running, the STOP property test (the "STOP through the relay" group, cases a–c) passed 24 of 24, which is 8 reruns of 3 tests. That is short of the 50 of 50 the issue asks for, because each rerun takes about 11 s here.
  • The affected suites (relay, gate, stop, frame-limit, teleop, slider-follow, leader-arm, machine-feed, fake-serve) pass.
  • I did not run the rest of bun test or the unix-socket suites; the forge's check runs those.
  • tsc reports one error, in src/visitor-counts.ts, which I didn't touch.

Relay (src/relay.ts)

  • On POST /stop the relay now shuts the gate, raises a STOP epoch, and terminates every arm-side /follow socket. That discards anything accepted but not yet written. Only then does it close the browser streams and forward the STOP.
  • The epoch is a clock-based counter, so it never repeats across a relay restart. It goes out on the STOP body and on every frame forwarded to the Pi.

Pi (src/command-gate.ts, src/server.ts)

  • POST /stop records epoch, and the gate drops any frame with a lower epoch (before-stop), however the network orders them.
  • Frames with no epoch, such as the LAN leader's, are unaffected.

Tests

  • command-gate.test.ts covers the epoch rule.
  • stop.test.ts: the fake Pi now enforces the epoch the way the real one does. New case (c) runs the interleaving property with each write landing 50 ms late.
  • frame-limit.test.ts and relay.test.ts compared forwarded frames exactly. They now strip the epoch field first; the tests were right, the wire format changed.

Real-Pi address in tests: src/fake-serve.test.ts spawned bench/fake-serve.ts with all of process.env. In the check cell that includes the live ARM_SERVER, so the child dialled the real Pi at 100.89.95.68. It now drops every ARM_* variable. That is my inference from reading the code. I did not reproduce it, since ARM_SERVER isn't set here. The other tests that mention that address only parse strings.

An unclean arm-socket close, as the relay now does on STOP, may be treated by the real Pi as a lost driver rather than a deliberate close. The STOP holds the Pi anyway, so I left it.

🤖 Generated with Claude Code

commented

The failure you pasted was from the earlier push, a7faf20. The pull's current head, 76663f9, shows the deploy as ok: "running at https://arm--435--forge-trevato-apps.open-platform.sh · its tests passed". I found nothing to change, so I pushed nothing.

The first push still had two tests that compared forwarded /follow frames exactly (frame-limit.test.ts, relay.test.ts). The relay now adds an epoch field to those frames, which broke the comparison, and the second commit made the tests ignore it. The log excerpt you pasted ends mid-run and doesn't show the failing test.

On 76663f9 the full local bun test gave 694 pass, 0 fail. It also passed with six CPU burners running beside it. tsc reports only the existing src/visitor-counts.ts error, which is the same on main.

The pull has no new preview failure to fix, so I'm leaving the code as it is.

Changes requested — before the latest push

The design is right: gate shut, a rising epoch, arm-side sockets cut, then the STOP forwarded, and the Pi refuses frames stamped before its last STOP. One thing blocks it, and it is a test gap.

Fix first: the Pi-side wiring in server.ts (gate.stopped(body.epoch) on POST /stop) has no test. Delete that line and every test still passes. command-gate.test.ts calls gate.stopped directly. The stop.test.ts cases (a–c) use a FakePi that re-implements the epoch check, so they never run the real server. Add a test in the server suite that starts startServer with a fake bus, opens /follow with a stamped frame, POSTs /stop with {epoch: N}, then sends a frame stamped epoch: N-1, and expects it to be refused. Also send a frame stamped N, and check that it is still refused while the gate is held. That is the behaviour the issue asks for, and nothing exercises it end to end.

Smaller points:

  • fake-serve.test.ts dropping ARM_* from the child env is unrelated to the issue. The reason is plausible, but it is a separate change. Keep it only if the pull body says so, or split it out.
  • arm.terminate() on STOP is an unclean close. The pull itself says the real Pi may read that as a lost driver. Say in the README or a code comment what the Pi does with it, or prefer a clean close with code 1000, which the Pi treats as a deliberate close.
  • The preview shows no visible change, as expected for this change. I checked the code and tests only, not the pictures.
commented

I added the server-level test and documented the unclean close. I kept terminate() on the arm socket rather than switching to a clean close, and I did not split out the fake-serve.test.ts change. stop.test.ts and relay.test.ts pass (28 tests); I did not run the full suite for this round.

Server-level test. A new case in the "STOP on the arm server itself" group runs the real startServer. It sends a stamped frame, POSTs /stop with {epoch: N}, then resumes and opens a new stream. A frame stamped N-1 (fresh seq and t) moves nothing. A frame stamped N moves the arm to its target. I checked that the test fails when gate.stopped(body.epoch) is commented out in server.ts, and passes with it restored.

I did not test "a frame stamped N is still refused while the gate is held" as you described it. The STOP closes the stream, so there is no open socket to send on, and a new stream is refused while held. The existing "while held, a stream is refused and nothing it sends moves the arm" test already covers the held case.

terminate() on STOP. I kept it, because a clean close(1000) would flush the queue it is meant to drop. The comment at relay.ts:629 now says what the Pi does with the cut:

  • It reads the close as a lost driver and would park the arm.
  • park returns at once while the gate is held, and the STOP the relay sends holds the Pi and aborts a park in progress.
  • The old-epoch frames that got out ahead of the STOP are refused once the Pi has the STOP.

Residual risk. The cut socket can reach the Pi before the STOP HTTP request does, so a park could start for a moment before the STOP aborts it. I left that because I judged it small and bounded. If you would rather avoid it, the alternative is to send the STOP to the Pi first and cut the socket after, which gives up the ahead-of-the-STOP ordering.

fake-serve.test.ts. The change drops ARM_* from the child's environment, and the issue explicitly asks for that ("make it hermetic"). That is why I left it in this pull. It stays in the pull body as its own bullet.

🤖 Generated with Claude Code

Approved

Does what #435 asks and stays inside the safety pillar. On STOP the relay shuts the gate, raises a clock-based epoch, cuts the arm-side sockets and forwards the epoch. The Pi refuses frames stamped below its last STOP. The relay overwrites any client-supplied epoch (stamped() spreads the frame, then sets epoch), so a browser cannot forge one. Tests cover the gate rule, the real server (fails without gate.stopped) and the slow-link interleaving. The fake-serve hermetic change was asked for in the issue. I did not exercise the preview beyond reading the diff. Two things to do next: (1) the terminate() on STOP makes the Pi see a lost driver, so a park can start briefly before the STOP lands. Either send the STOP first and cut the socket right after, or add a test that the arm does not move in that window. (2) Add a line to the README or the gate doc on the epoch field in the wire format, so LAN senders know that omitting it is allowed.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs