Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
A restart mid-turn is proven: the driver reconnects with control, the queue is intact, and no motion is sent in the gap #333
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/235-a-restart-mid-turn-is-proven-the-driver → main 3 commits fork
2 files
changed +111
−2
Software/so101/src/stop.test.ts +2 −2
| @@ -269,7 +269,7 @@ describe("STOP through the relay, to a Pi that holds nothing itself", () => { | ||
| 269 | 269 | } |
| 270 | 270 | await sleep(random() < 0.1 ? 250 : Math.floor(random() * 25)); |
| 271 | 271 | } |
| 272 | await sleep(80); | |
| 272 | await sleep(400); | |
| 273 | 273 | // Every frame that reached the Pi was made while the arm was not held, and reached it before any later STOP took effect. |
| 274 | 274 | const heldAt = (t: number) => { |
| 275 | 275 | const stop = stops.filter((s) => s <= t).at(-1); |
| @@ -287,7 +287,7 @@ describe("STOP through the relay, to a Pi that holds nothing itself", () => { | ||
| 287 | 287 | expect(await page.open()).toBe(true); |
| 288 | 288 | const last = page.make(++n); |
| 289 | 289 | page.send(last, "drag"); |
| 290 | await sleep(50); | |
| 290 | for (let waited = 0; pi.got.at(-1)?.n !== last.n && waited < 3000; waited += 25) await sleep(25); | |
| 291 | 291 | if (pi.got.at(-1)?.n !== last.n) print(`seed ${seed} FAILED`, timeline); |
| 292 | 292 | expect({ seed, last: pi.got.at(-1)?.n }).toEqual({ seed, last: last.n }); |
| 293 | 293 | if (seed === 3) print(`(a) property run, seed ${seed}: ${pi.got.length} of ${n} frames reached the Pi; ${stops.length} stops`, timeline); |
Software/so101/src/turn-restart.test.ts added +109
| @@ -0,0 +1,109 @@ | ||
| 1 | import { afterEach, expect, test } from "bun:test"; | |
| 2 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 3 | import { createServer, type Server } from "node:http"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import WebSocket from "ws"; | |
| 7 | import { sign } from "./identity.ts"; | |
| 8 | import { Lobby } from "./lobby.ts"; | |
| 9 | import { startRelay, type RunningRelay } from "./relay.ts"; | |
| 10 | ||
| 11 | const KEY = "k".repeat(64); | |
| 12 | const as = (name: string) => { | |
| 13 | const identity = `id-${name}|${name}|user|${Date.now() + 600_000}`; | |
| 14 | return { "x-forge-identity": identity, "x-forge-signature": sign(identity, KEY) }; | |
| 15 | }; | |
| 16 | const [ALICE, BOB, CAROL] = [as("alice"), as("bob"), as("carol")]; | |
| 17 | const MOVE = { method: "POST", body: '{"targets":{"1":2000}}' }; | |
| 18 | ||
| 19 | let dir = ""; | |
| 20 | let arm: Server | undefined; | |
| 21 | let relay: RunningRelay | undefined; | |
| 22 | let lobby: Lobby | undefined; | |
| 23 | const sockets: WebSocket[] = []; | |
| 24 | afterEach(async () => { | |
| 25 | for (const ws of sockets.splice(0)) ws.terminate(); | |
| 26 | await relay?.close(); | |
| 27 | lobby?.close(); | |
| 28 | arm?.close(); | |
| 29 | if (dir) rmSync(dir, { recursive: true, force: true }); | |
| 30 | dir = ""; | |
| 31 | relay = lobby = arm = undefined; | |
| 32 | }); | |
| 33 | ||
| 34 | /** A fake arm that writes down every command it is sent, and when. */ | |
| 35 | async function fakeArm() { | |
| 36 | const seen: Array<{ route: string; at: number }> = []; | |
| 37 | arm = createServer((req, res) => { | |
| 38 | if (req.method !== "GET") seen.push({ route: `${req.method} ${req.url}`, at: Date.now() }); | |
| 39 | res.writeHead(200, { "content-type": "application/json" }).end("{}"); | |
| 40 | }); | |
| 41 | await new Promise<void>((r) => arm!.listen(0, "127.0.0.1", r)); | |
| 42 | return { seen, url: `http://127.0.0.1:${(arm.address() as { port: number }).port}` }; | |
| 43 | } | |
| 44 | ||
| 45 | async function boot(simulation: string) { | |
| 46 | lobby = new Lobby({ identityKey: KEY, adminFile: join(dir, "admin.json"), turnsFile: join(dir, "turns.json") }); | |
| 47 | relay = await startRelay({ port: 0, simulation, lobby }); | |
| 48 | return relay.port; | |
| 49 | } | |
| 50 | ||
| 51 | /** Open /live as someone, optionally asking to drive, and wait for the first state that has a driver. */ | |
| 52 | async function sit(port: number, headers: Record<string, string>, join: boolean) { | |
| 53 | const ws = new WebSocket(`ws://127.0.0.1:${port}/live`, { headers }); | |
| 54 | sockets.push(ws); | |
| 55 | const states: Array<{ driver: { name: string; endsAt: number } | null; queue: Array<{ name: string }> }> = []; | |
| 56 | ws.on("message", (data) => { | |
| 57 | const m = JSON.parse(String(data)); | |
| 58 | if (m.type === "state") states.push(m); | |
| 59 | }); | |
| 60 | await new Promise((r, j) => (ws.once("open", r), ws.once("error", j))); | |
| 61 | if (join) ws.send(JSON.stringify({ type: "join" })); | |
| 62 | return { ws, states }; | |
| 63 | } | |
| 64 | const until = async (ok: () => boolean) => { | |
| 65 | for (let i = 0; i < 100 && !ok(); i++) await new Promise((r) => setTimeout(r, 20)); | |
| 66 | expect(ok()).toBe(true); | |
| 67 | }; | |
| 68 | ||
| 69 | test("a restart mid-turn: the driver reconnects with control, the line is as it was, and the arm is sent no move in the gap", async () => { | |
| 70 | dir = mkdtempSync(join(tmpdir(), "so101-turn-restart-")); | |
| 71 | const fake = await fakeArm(); | |
| 72 | let port = await boot(fake.url); | |
| 73 | ||
| 74 | const alice = await sit(port, ALICE, true); | |
| 75 | await until(() => alice.states.at(-1)?.driver?.name === "alice"); | |
| 76 | for (const [who, headers] of [["bob", BOB], ["carol", CAROL]] as const) { | |
| 77 | const guest = await sit(port, headers, true); | |
| 78 | await until(() => guest.states.at(-1)?.queue.some((p) => p.name === who) ?? false); | |
| 79 | } | |
| 80 | await until(() => alice.states.at(-1)?.queue.map((p) => p.name).join() === "bob,carol"); | |
| 81 | const before = lobby!.turns.snapshot(); | |
| 82 | expect((await fetch(`http://127.0.0.1:${port}/move`, { ...MOVE, headers: ALICE })).status).toBe(200); | |
| 83 | expect(fake.seen).toHaveLength(1); | |
| 84 | ||
| 85 | // Stop: everyone is dropped, nothing is asked of the arm until the driver is back. | |
| 86 | for (const ws of sockets.splice(0)) ws.terminate(); | |
| 87 | await relay!.close(); | |
| 88 | lobby!.close(); | |
| 89 | const stoppedAt = Date.now(); | |
| 90 | port = await boot(fake.url); | |
| 91 | ||
| 92 | const restored = lobby!.turns.snapshot(); | |
| 93 | expect(restored.driver?.name).toBe("alice"); | |
| 94 | expect(restored.driver?.endsAt).toBe(before.driver!.endsAt); | |
| 95 | expect(restored.queue.map((p) => p.name)).toEqual(["bob", "carol"]); | |
| 96 | // Others still wait: the restored turn is alice's, not whoever asks first. | |
| 97 | expect((await fetch(`http://127.0.0.1:${port}/move`, { ...MOVE, headers: BOB })).status).toBe(403); | |
| 98 | ||
| 99 | const again = await sit(port, ALICE, false); | |
| 100 | await until(() => again.states.at(-1)?.driver?.name === "alice"); | |
| 101 | const reconnectedAt = Date.now(); | |
| 102 | expect(again.states.at(-1)!.driver!.endsAt).toBe(before.driver!.endsAt); | |
| 103 | expect(again.states.at(-1)!.queue.map((p) => p.name)).toEqual(["bob", "carol"]); | |
| 104 | expect(fake.seen.filter((m) => m.at > stoppedAt && m.at < reconnectedAt)).toEqual([]); | |
| 105 | ||
| 106 | // She still drives: her next move reaches the arm. | |
| 107 | expect((await fetch(`http://127.0.0.1:${port}/move`, { ...MOVE, headers: ALICE })).status).toBe(200); | |
| 108 | expect(fake.seen).toHaveLength(2); | |
| 109 | }); |
new above
offline