From cc272572f60409f9a32972ad71687ce11a9fc2a1 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 16 Nov 2026 06:12:57 -0800 Subject: [PATCH 1/5] chore(quest): claim quest/m2/sockets/carla.md Quest-Claim: 2d0aeb2eee5c3623 From 2d48dc79e21285c6e1bdb159acee65a5fec8514a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 16 Nov 2026 06:30:52 -0800 Subject: [PATCH 2/5] feat(carla)!: drivers publish their controls again The CARLA demo drives trucks without socket endpoints. Each driver publishes viewer/carla/{truck}/{driver} with a control track, one JSON command per group and a one-heartbeat max age; the worker follows each truck's driver prefix and acknowledges the holder on the truck's status track. Co-Authored-By: Claude Opus 5.5 --- demo/carla/README.md | 98 ++-- demo/carla/sim/moq_carla_sim/drive.py | 80 ++-- demo/carla/sim/moq_carla_sim/main.py | 25 +- demo/carla/sim/moq_carla_sim/publish.py | 231 +++++----- demo/carla/sim/moq_carla_sim/runtime.py | 44 +- .../carla/sim/systemd/install-user-service.sh | 4 +- .../sim/systemd/install-user-service.test.sh | 2 +- demo/carla/sim/tests/conftest.py | 27 +- demo/carla/sim/tests/test_drive.py | 109 ++--- demo/carla/sim/tests/test_main.py | 15 +- demo/carla/sim/tests/test_publish.py | 434 ++++++++++-------- demo/carla/sim/tests/test_runtime.py | 131 +++--- demo/carla/web/src/element.ts | 14 +- demo/carla/web/src/hint.test.ts | 2 +- demo/carla/web/src/hint.ts | 7 +- demo/carla/web/src/paths.test.ts | 17 +- demo/carla/web/src/paths.ts | 38 +- demo/carla/web/src/player.ts | 33 +- demo/carla/web/src/preview.ts | 1 + demo/carla/web/src/schemas.test.ts | 1 + demo/carla/web/src/schemas.ts | 10 +- demo/carla/web/src/truck.test.ts | 282 ++++++------ demo/carla/web/src/truck.ts | 242 +++++----- demo/relay/prod.toml | 16 +- doc/bin/demo.md | 2 +- packaging/moq-relay/relay.toml | 2 +- quest/m2/sockets/carla.md | 89 ---- quest/m2/sockets/remove.md | 1 - quest/m3/release/demos-deploy.md | 3 +- 29 files changed, 901 insertions(+), 1059 deletions(-) delete mode 100644 quest/m2/sockets/carla.md diff --git a/demo/carla/README.md b/demo/carla/README.md index f4abe1daeb8..c9d4b60b295 100644 --- a/demo/carla/README.md +++ b/demo/carla/README.md @@ -3,10 +3,9 @@ Drive simulated trucks from a browser over MoQ. Trucks share one [CARLA](https://carla.org) world and drive themselves until someone takes control. Each truck publishes its cameras and a status track; a web player -watches them on a map, and a driver steers one by dialing the truck's -[socket](../../doc/concept/socket.md) endpoint: its controls and the truck's -acknowledgements travel on a session only the two of them read, while what -every viewer shows stays on `status`. +watches them on a map, and a driver steers one by publishing a **control** +track back. The truck follows every driver's broadcast and echoes what it +accepted on `status`, which every viewer reads. ```text ├── web/ # the player: the element, with map, video, keyboard and steering-wheel input (@moq/demo-carla) @@ -45,7 +44,7 @@ The player alone runs against any relay with `just carla web `. Append `?preview` to the local URL for a fixture-driven layout review that opens no connection; it is removed from production builds. -## Broadcasts and sessions +## Broadcasts ```text carla/{truck}/{camera}.hang <- one media broadcast per camera (hang catalog) @@ -53,21 +52,16 @@ carla/{truck}/{camera}.hang <- one media broadcast per camera (hang catalo hd 720p H.264, for the focused view status JSON Snapshot telemetry -carla/.server/{truck} <- the truck's server socket endpoint, which drivers dial -carla/.client/{id} <- one client endpoint per driver lease +viewer/carla/{truck}/{driverId} <- one broadcast per driver lease + control one JSON command per group ``` -Each truck accepts `track` sessions on its server endpoint. A driver dials from -a client endpoint of its own, one session per lease, and sends one control per message; -the truck answers each control it accepted from the lease holder on the same -session. Cameras are sibling broadcasts, so switching views never creates a -second claim on the same wheel. +The worker follows the announcements under `viewer/carla/{truck}/` for each +truck and fans every driver's `control` track into that truck's gate. Cameras +are sibling broadcasts, so switching views never creates a second claim on the +same wheel, and the driver scope is disjoint from every media path. -Sockets need moq-lite-07 or moq-transport on every hop, so the worker and the -player offer only `moq-lite-07-wip` and `moq-transport-22`; a relay must -accept one of them. - -### Control (player to truck, on the driver's session) +### Control (player to truck) ```json { "type": "drive", "sequence": 42, "steering": -1.0..1.0, @@ -78,27 +72,18 @@ accept one of them. { "type": "respawn", "sequence": 45, "timestamps": [...] } ``` -The truck reads them oldest first, skipping one that falls a heartbeat (250 ms) -behind the newest, so a late control never holds up a newer one, while a burst, -such as a reset right before a drive, still arrives whole. - -### Acknowledgement (truck to player, on the driver's session) - -```json -{ "sequence": 44, "media_pipeline": [ { "label": "received→decoded", "ms": 12 } ] } -``` - -Sent for each `drive`, `estop`, and `reset_estop` the truck accepted from the -lease holder, so the player measures its round trip and its own media pipeline. -A bystander's e-stop, a command the gate refused, or an accepted `respawn` -(confirmed from `status` instead) gets none. +Each command is a group of its own, and a superseded one stays fresh for a +heartbeat (250 ms, the track's max age). The truck reads them oldest first with +the same delay budget, so a late control never holds up a newer one, while a +burst, such as a reset right before a drive, still arrives whole. ### Status (truck to every viewer) ```json { "accepted": { "steering": .., "throttle": .., "brake": .., "gear": ".." }, "autonomy": "autonomous|manual", "estopped": true, - "driver": "driver-3eb1bd43", "estop_by": "driver-9c0a7f12", + "driver": "3eb1bd43c0a7", "estop_by": "safety-9c0a7f12e4d1", "sequence": 44, + "media_pipeline": { "3eb1bd43c0a7": [ { "label": "received→decoded", "ms": 12 } ] }, "map": "Town10HD_Opt", "pose": { "x": 12.35, "y": -78.9, "yaw": 180.1, "speed": 9.88 }, "measured": { "steering": .., "throttle": .., "brake": .., "gear": ".." }, @@ -111,6 +96,11 @@ autonomous truck changes no accepted state as it moves. `pose` is in CARLA world metres (x east, y south, yaw clockwise from +x), which is what the player's baked road maps use. +`sequence` and `media_pipeline` acknowledge the lease holder: the newest +`drive`, `estop`, or `reset_estop` the truck accepted from it, which the player +times its round trip by, and the media pipeline stages it reported. A +bystander's e-stop or a refused command changes neither. + `accepted` is the gate repeating what it let through. `measured`, `motion`, and `collision` come from the vehicle itself, so the player can draw requested against measured steering and show the gap. `collision.at` is the unix @@ -124,15 +114,17 @@ collision. The worker's gate (`sim/moq_carla_sim/drive.py`) owns every truck's controls: - **One driver at a time.** The first driver to send a command holds the - lease until its session ends or its 1 s watchdog expires; the player - heartbeats every 250 ms. The lease is bound to the driver's session, so a - driver that redials, even from the same endpoint id, is a new claimant - rather than a continuation. + lease until its broadcast ends or its 1 s watchdog expires; the player + heartbeats every 250 ms, and lets go once `status` stops naming it. The + lease is bound to the driver's path and the publisher instance serving it, + so a driver that republishes at the same path is a new claimant rather than + a continuation. - **E-stop latches** until an explicit reset, from any viewer. The lease - holder sends it on its session; any other viewer dials a short-lived session - of its own, so a bystander's stop neither takes the lease nor waits behind - the holder's controls. A latched truck is also cleared when it respawns as - stopped, below. + holder sends it on its control track; any other viewer publishes a + short-lived `safety-{id}` broadcast of its own until `status` echoes it, so + a bystander's stop neither takes the lease nor waits behind the holder's + controls. A latched truck is also cleared when it respawns as stopped, + below. - **Gear changes are interlocked**: the gate keeps the current gear unless the command releases throttle and applies some brake. - **Takeover starts braking**: the player keeps measured steering and gear, @@ -148,20 +140,15 @@ The worker's gate (`sim/moq_carla_sim/drive.py`) owns every truck's controls: - **A stopped truck respawns**: any truck that has not moved 2 m in 2 minutes of a running world, whether autonomous, e-stopped, or parked under a driver, returns to a free spawn point under autonomy, its e-stop cleared. The worker - ends the holder's driving session, so the player lets go instead of + ignores the rest of the holder's run, so the player lets go instead of reclaiming the fresh spawn. -The lease is cooperative. Anonymous drivers share one grant to publish -`carla/.client/*`, so an endpoint id is all it takes to announce in a driver's -place and win the relay's route to its next session. Each driver's id is -therefore 128 random bits, and `status` names the holder and the e-stop's -sender by a handle instead: `driver-` and 8 hex digits of the SHA-256 of the -session's secret id, which the driver derives the same way to recognize its -own lease. The grant covers only client endpoints, so no driver can announce a -truck's server endpoint, whatever its name. Anyone may still claim a free truck -or press its e-stop, so the lease stops two honest drivers fighting over a -truck, not a deliberate disruption. Anything beyond a simulator needs -per-driver tokens scoped to the driver's own endpoint. +The lease is cooperative. Anonymous drivers share one grant to publish under +`viewer/**`, and `status` names the holder, so anyone can announce at the +holder's path under another instance, which ends the holder's run and frees +the lease. It stops two honest drivers fighting over a truck, not a deliberate +takeover. Anything beyond a simulator needs per-driver tokens scoped to the +driver's own path. ## The simulator @@ -223,10 +210,9 @@ checkout. just carla venv just carla service-install -# The units read this. MOQ_URL carries a token allowed to publish `carla/**`, -# which covers the trucks' server endpoints, and subscribe `carla/.client/**`, -# the endpoints drivers dial from; keeping it here keeps it off the command -# line. CARLA_IMAGE is the tag the moq-dev/carla build printed, since the worker +# The units read this. MOQ_URL carries a token allowed to publish `carla/**` +# and subscribe `viewer/carla/**`, where drivers publish; keeping it here keeps +# it off the command line. CARLA_IMAGE is the tag the moq-dev/carla build printed, since the worker # refuses a stock simulator. cat >~/.config/moq/carla.env <<'EOF' MOQ_URL=https://cdn.moq.dev/demo?jwt=... diff --git a/demo/carla/sim/moq_carla_sim/drive.py b/demo/carla/sim/moq_carla_sim/drive.py index a08215216a1..9933f106467 100644 --- a/demo/carla/sim/moq_carla_sim/drive.py +++ b/demo/carla/sim/moq_carla_sim/drive.py @@ -8,7 +8,6 @@ from __future__ import annotations -import hashlib from dataclasses import dataclass, field, replace from enum import Enum from typing import Literal @@ -24,16 +23,6 @@ class Gear(str, Enum): REVERSE = "reverse" -def handle(session: str) -> str: - """The name `status` gives the lease or e-stop a session carried. - - A hash of the session id, which only that session's two ends know, so the - driver recognizes its own lease while no viewer learns its endpoint id, - which anyone could announce to take the route to that driver's next dial. - """ - return "driver-" + hashlib.sha256(session.encode()).hexdigest()[:8] - - def _clamp(value: float, low: float, high: float) -> float: return max(low, min(high, value)) @@ -105,17 +94,15 @@ class Command: `drive`, `estop`, `reset_estop`, and `respawn` arrive on the wire; `driver_left` and `watchdog_expired` are synthesized locally. - `driver_id` is the driver's socket endpoint id, which stays private. - `session` is the socket session that carried it, one per lease the player - takes: the same id on another session is a different driver for the lease. - The session id is the secret that keeps a session to its two ends, so only - its `handle` leaves this process. Only a locally synthesized command has - none. + `driver_id` is the driver's path segment, which `status` echoes. `instance` + names the run that sent it, one per publisher instance serving the path: the + same id under another instance is another run of the driver, so it is a + different driver for the lease. """ kind: CommandKind driver_id: str = "" - session: str | None = None + instance: str | None = None sequence: int = 0 state: DriveState | None = None timestamps: tuple[TimestampEntry, ...] = () @@ -127,16 +114,19 @@ class Drive: The first driver to send a command owns the truck until it disconnects or its command lease expires. Other drivers cannot silently steal control. - The lease binds to the driver's socket session, so a driver whose session - ends loses it, and another session claiming the same driver id is another - driver. `driver` and `estop_by` are session handles, never endpoint ids, so - the public `status` track gives nobody an id to announce in the holder's - place. + The lease binds to the driver's broadcast path and the run serving it, so a + driver whose broadcast ends or restarts loses it. + It is still cooperative, not enforced: the holder's id is echoed on the + public `status` track, so anyone allowed to publish there can announce at + that path under another instance, which ends the holder's broadcast and frees + the lease for the newcomer. This stops two honest drivers colliding; it + does not stop a deliberate takeover. """ def __init__(self) -> None: self._driver: str | None = None - self._session: str | None = None + self._instance: str | None = None + self._sequence: int | None = None self._state = DriveState.stopped() self._takeover_pedals: tuple[float, float] | None = None self._estopped = False @@ -144,13 +134,17 @@ def __init__(self) -> None: @property def driver(self) -> str | None: - """The lease holder's handle, or None while nobody holds it.""" - return None if self._session is None else handle(self._session) + return self._driver + + @property + def holder(self) -> tuple[str, str | None] | None: + """The lease holder's driver id and run, or None while nobody holds it.""" + return None if self._driver is None else (self._driver, self._instance) @property - def holder(self) -> tuple[str, str] | None: - """The lease holder's driver id and session, or None while nobody holds it.""" - return None if self._driver is None or self._session is None else (self._driver, self._session) + def sequence(self) -> int | None: + """The newest sequence the holder's run had accepted, which `status` echoes.""" + return self._sequence @property def state(self) -> DriveState: @@ -162,12 +156,11 @@ def estopped(self) -> bool: @property def estop_by(self) -> str | None: - """The handle of the session that latched the e-stop.""" return self._estop_by def holds(self, command: Command) -> bool: - """Whether `command` comes from the session holding the lease.""" - return self._driver is not None and (self._driver, self._session) == (command.driver_id, command.session) + """Whether `command` comes from the run of the driver holding the lease.""" + return self._driver is not None and (self._driver, self._instance) == (command.driver_id, command.instance) def accepted(self) -> Accepted: return Accepted(state=self._state, estopped=self._estopped) @@ -175,12 +168,12 @@ def accepted(self) -> Accepted: def apply(self, command: Command) -> bool: """Apply a command, returning whether it changed or refreshed state.""" if command.kind == "drive": - # A lease binds to a session, so a command without one cannot hold it. - if command.session is None or self._estopped or not (self._driver is None or self.holds(command)): + if self._estopped or not (self._driver is None or self.holds(command)): return False claiming = self._driver is None self._driver = command.driver_id - self._session = command.session + self._instance = command.instance + self._sequence = command.sequence state = (command.state or DriveState.stopped()).sanitized() pedals = (state.throttle, state.brake) if claiming: @@ -201,17 +194,23 @@ def apply(self, command: Command) -> bool: # latches and brings the truck to a stop, but it must never transfer # the driving lease to its sender. Otherwise a bystander could # e-stop, then reset_estop, and drive off with a lease they never - # legitimately claimed. + # legitimately claimed. Only refresh the acknowledgement sequence + # when the sender already holds the lease, so a bystander's e-stop + # cannot masquerade as the authoritative command either. + if self.holds(command): + self._sequence = command.sequence self._state = DriveState.stopped() self._estopped = True - if not was_estopped and command.session is not None: - self._estop_by = handle(command.session) + if not was_estopped: + self._estop_by = command.driver_id elif command.kind == "reset_estop": if not self._estopped: return False # Reset clears the global latch without claiming or transferring the - # lease. + # lease. Only the holder's sequence is authoritative for that lease. + if self.holds(command): + self._sequence = command.sequence self._state = DriveState.stopped() self._estopped = False self._estop_by = None @@ -235,6 +234,7 @@ def _can_shift(state: DriveState) -> bool: def _clear_driver(self) -> None: self._driver = None - self._session = None + self._instance = None + self._sequence = None self._state = DriveState.stopped() self._takeover_pedals = None diff --git a/demo/carla/sim/moq_carla_sim/main.py b/demo/carla/sim/moq_carla_sim/main.py index 6d77d0c3ae6..88585fb09d0 100644 --- a/demo/carla/sim/moq_carla_sim/main.py +++ b/demo/carla/sim/moq_carla_sim/main.py @@ -37,11 +37,6 @@ # A local development relay, which needs no token. DEFAULT_URL = "http://localhost:4443" -# Drivers dial each truck's socket endpoint, which only these versions carry: -# an endpoint is a hidden broadcast, and moq-lite before 07 hides it from every -# peer. The web player offers the same list. -VERSIONS = ("moq-lite-07-wip", "moq-transport-22") - def default_trucks() -> list[str]: trucks = json.loads(TRUCKS_FILE.read_text()) @@ -115,6 +110,11 @@ def broadcast_path(truck: str, camera: str) -> str: return f"carla/{truck}/{camera}.hang" +def driver_prefix(truck: str) -> str: + """Where a truck's drivers publish, each at `{prefix}/{driverId}`.""" + return f"viewer/carla/{truck}" + + # The one task allowed to end the run without failing it. SHUTDOWN = "shutdown" @@ -238,7 +238,7 @@ async def run(args: argparse.Namespace) -> None: # shares the connection. events = asyncio.create_task(bridge.run(), name="bridge") try: - async with moq.connect(args.url, versions=VERSIONS) as client: + async with moq.connect(args.url) as client: try: for truck in world.trucks: route = client.route(broadcast_path(truck.name, args.camera), hidden=True) @@ -248,17 +248,16 @@ async def run(args: argparse.Namespace) -> None: session = TruckSession(truck, route, video, args.location, map_name, world.clock) sessions.append(session) log.info( - "publishing %s, accepting drivers at %s", + "publishing %s, consuming %s/*", broadcast_path(truck.name, args.camera), - session.drivers.path, + driver_prefix(truck.name), ) - relay = client.session - if relay is None: - raise RuntimeError("relay session is not connected") - origin = relay.publish() tasks = [ - asyncio.create_task(session.run(origin), name=f"truck:{truck.name}") + asyncio.create_task( + session.run(client, driver_prefix(truck.name)), + name=f"truck:{truck.name}", + ) for session, truck in zip(sessions, world.trucks) ] diff --git a/demo/carla/sim/moq_carla_sim/publish.py b/demo/carla/sim/moq_carla_sim/publish.py index a471431d696..85a504cb52f 100644 --- a/demo/carla/sim/moq_carla_sim/publish.py +++ b/demo/carla/sim/moq_carla_sim/publish.py @@ -1,17 +1,16 @@ """MoQ wiring: video and status out, driver control in. -The layout is the one `demo/carla/README.md` documents and the web player -speaks: +The broadcast layout is the one `demo/carla/README.md` documents and the web +player speaks: carla/{truck}/{camera}.hang video (sd, hd) + status - carla/.server/{truck} the truck's server socket endpoint - carla/.client/{id} each driver's client socket endpoint + viewer/carla/{truck}/{driverId} control """ from __future__ import annotations import asyncio -import contextlib +import itertools import json import logging import math @@ -101,6 +100,8 @@ class Status: estop_by: str | None autonomous: bool driver: str | None + sequence: int | None + media_pipeline: dict[str, list[dict[str, Any]]] location: str | None map_name: str | None telemetry: Telemetry | None @@ -129,6 +130,7 @@ def status_payload(status: Status) -> dict[str, Any]: "capabilities": ["respawn"], "autonomy": "autonomous" if status.autonomous else "manual", "estopped": status.estopped, + "media_pipeline": status.media_pipeline, } # Omitted rather than null when absent; the player reads a missing field # as unset. @@ -136,6 +138,8 @@ def status_payload(status: Status) -> dict[str, Any]: payload["driver"] = status.driver if status.estop_by is not None: payload["estop_by"] = status.estop_by + if status.sequence is not None: + payload["sequence"] = status.sequence if status.location is not None: payload["location"] = status.location if status.map_name is not None: @@ -213,7 +217,7 @@ async def unused(self) -> None: # The player's pipeline stages. Anything else a driver sends is dropped, so an -# anonymous lease holder cannot grow the reply the truck echoes. +# anonymous lease holder cannot grow the status the truck echoes. TIMESTAMP_LABELS = ("received", "decoded", "rendered") @@ -233,8 +237,8 @@ def _parse_timestamps(raw: Any) -> tuple[TimestampEntry, ...]: return tuple(entries.values()) -def parse_command(driver_id: str, message: Any, session: str) -> Command | None: - """Turn one control message from a driver's session into a Command. +def parse_command(driver_id: str, message: Any, instance: str | None = None) -> Command | None: + """Turn one `control` track message from a driver run into a Command. Returns None for anything unrecognized. A driver is an untrusted publisher, so a malformed message is dropped here rather than reaching the gate: the @@ -260,7 +264,7 @@ def parse_command(driver_id: str, message: Any, session: str) -> Command | None: return Command( kind="drive", driver_id=driver_id, - session=session, + instance=instance, sequence=sequence, state=DriveState( steering=_number(message.get("steering")), @@ -272,7 +276,7 @@ def parse_command(driver_id: str, message: Any, session: str) -> Command | None: ) if kind in ("estop", "reset_estop", "respawn"): - return Command(kind=kind, driver_id=driver_id, session=session, sequence=sequence, timestamps=timestamps) + return Command(kind=kind, driver_id=driver_id, instance=instance, sequence=sequence, timestamps=timestamps) return None @@ -293,144 +297,150 @@ def _number_or_none(value: Any) -> float | None: return number if math.isfinite(number) else None -# Every endpoint the trucks and their drivers talk through sits under this -# prefix: a truck's as `{prefix}/.server/{truck}`, a driver's as `{prefix}/.client/{id}`. -SOCKET_PREFIX = "carla" - # How far a control may fall behind the newest before it is skipped: one player # heartbeat, by which time a newer control has superseded it. Within it, a burst # arrives whole and in order, so a reset never loses to the drive right after it. CONTROL_DELAY = timedelta(milliseconds=250) +# Names each driver run for the lease. The bindings hand out a run's broadcast +# without its route instance, and a process-wide count keeps a run's late +# `driver_left` from releasing a later run of the same driver. +_runs = itertools.count(1) + class Drivers: - """A truck's server socket endpoint: accepts each driver's session, fans its - controls into the command queue, and answers the driver on the same session. + """A truck's drivers: follows each driver broadcast under the truck's prefix + and fans its controls into the command queue. - Each driver dials from a client endpoint of its own, one `track` session per lease, - whose messages are one control per group. Replies go back the same way, so a - driver's acknowledgements never reach anyone else. + A driver publishes a broadcast at `{prefix}/{driverId}` whose `control` track + holds one JSON control per group. Each publisher instance serving that path + is a run of the driver, which the lease tells apart from the driver's other + runs. The truck answers on its shared `status` track. """ - def __init__(self, truck: str, commands: asyncio.Queue[Command]) -> None: - self._truck = truck + def __init__(self, commands: asyncio.Queue[Command]) -> None: self._commands = commands - # Keyed like the lease, by driver and session, since a dialer picks its own session id. - self._replies: dict[tuple[str, str], moq.TrackProducer] = {} - # Sessions the truck ended, until the gate reads their `driver_left`. - self._revoked: set[tuple[str, str]] = set() - - @property - def path(self) -> str: - """Where the truck's server endpoint is announced.""" - return f"{SOCKET_PREFIX}/.server/{self._truck}" - - def reply(self, command: Command, message: dict[str, Any]) -> None: - """Send `message` on the session that carried `command`, if it is still open. - - A session that has just ended is not an error: its pump is already on - its way to releasing the lease. - """ - if command.session is None: - return - producer = self._replies.get((command.driver_id, command.session)) - if producer is None: - return - # A session track is scale-less: each reply is a group keyed one past the last. - largest = producer.largest() - key = 0 if largest is None else largest.group + 1 - try: - producer.write_frame(json.dumps(message, separators=(",", ":")).encode(), moq.Timestamp.unscaled(key)) - # uniffi's stubs declare the error a plain class; at runtime it is an Exception. - except moq.Error as err: # pyright: ignore[reportGeneralTypeIssues] - log.info("%s: reply failed: %s", self._truck, err) + # Runs the truck revoked, until the gate reads their `driver_left`. + self._revoked: set[tuple[str, str | None]] = set() + # Set once the truck shuts down, whose gate no longer reads the queue. + self._closing = False - def revoke(self, driver: str, session: str) -> None: - """End a driver's session from the truck's side and drop what it still sends. + def revoke(self, driver: str, instance: str | None) -> None: + """Drop what a driver run still sends, until it ends. - The player gives up control once its replies end, but heartbeats already + The player lets go once `status` stops naming it, but heartbeats already queued or in flight would otherwise claim the lease straight back. """ - self._revoked.add((driver, session)) - producer = self._replies.get((driver, session)) - if producer is not None: - with contextlib.suppress(moq.Error): # pyright: ignore[reportGeneralTypeIssues] - producer.finish() + self._revoked.add((driver, instance)) def revoked(self, command: Command) -> bool: - """Whether `command` came from a revoked session, which the gate must ignore. + """Whether `command` came from a revoked run, which the gate must ignore. - The session's `driver_left` is the last command it queues, so reading it - forgets the session. + The run's `driver_left` is the last command it queues, so reading it + forgets the run. """ - if command.session is None: - return False - key = (command.driver_id, command.session) + key = (command.driver_id, command.instance) if key not in self._revoked: return False if command.kind == "driver_left": self._revoked.discard(key) return True - async def run(self, origin: moq.OriginProducer) -> None: - """Accept drivers until cancelled, each session in a task of its own, - so one slow or stuck driver cannot hold up the others.""" - sessions: set[asyncio.Task[None]] = set() + async def run(self, client: moq.Client, prefix: str) -> None: + """Discover driver broadcasts at `{prefix}/{driverId}` until cancelled. + + Each driver path the listing reports present gets its own task, so one + slow or stuck driver cannot hold up the others. That task follows the + path across the driver's runs; a run that another takes over, or whose + path goes away, ends, and with it its lease. + """ + drivers: dict[str, asyncio.Task[None]] = {} + try: + async with client.announced(prefix, filter="*") as announced: + async for event in announced: + # Every variant carries `announce`; uniffi's stubs type only the enum. + driver = event.announce.captures[0] # pyright: ignore[reportAttributeAccessIssue] + if isinstance(event, (moq.AnnounceEventAbsent, moq.AnnounceEventOffline)): + task = drivers.pop(driver, None) + if task is not None: + task.cancel() + # Availability changes reach the follow, which waits them out. + elif isinstance(event, moq.AnnounceEventPresent) and driver not in drivers: + drivers[driver] = asyncio.create_task( + self._follow(client, driver, f"{prefix}/{driver}"), name=f"driver:{driver}" + ) + finally: + self._closing = True + for task in drivers.values(): + task.cancel() + await asyncio.gather(*drivers.values(), return_exceptions=True) + + async def _follow(self, client: moq.Client, driver: str, path: str) -> None: + """Pump each run of one driver in turn, ending a run once another takes + over, it goes offline, or the path goes away.""" + run: tuple[asyncio.Task[None], str] | None = None try: - async with moq.socket.Endpoint(origin, SOCKET_PREFIX, self._truck) as endpoint: + async with client.follow(path) as following: while True: - incoming = await endpoint.accept_track() - task = asyncio.create_task(self._serve(incoming), name=f"driver:{incoming.peer}") - sessions.add(task) - task.add_done_callback(sessions.discard) + try: + broadcast = await following.available() + # A refusal leaves the follow waiting for another run; only shutdown ends it. + except moq.Error as err: # pyright: ignore[reportGeneralTypeIssues] + if moq.is_shutdown(err): + raise + log.info("driver %s refused: %s", driver, err) + continue + if run is not None: + await self._end(driver, *run) + run = None + if broadcast is not None: + instance = str(next(_runs)) + task = asyncio.create_task( + self._pump(driver, instance, broadcast), name=f"driver:{driver}:{instance}" + ) + run = (task, instance) + # uniffi's stubs declare the error a plain class; at runtime it is an Exception. + except moq.Error as err: # pyright: ignore[reportGeneralTypeIssues] + log.info("driver %s follow ended: %s", driver, err) finally: - for task in sessions: - task.cancel() - await asyncio.gather(*sessions, return_exceptions=True) + if run is not None: + await self._end(driver, *run) - async def _serve(self, incoming: moq.socket.IncomingTrack) -> None: - """Feed one driver session's controls into the command queue. + async def _end(self, driver: str, task: asyncio.Task[None], instance: str) -> None: + """Stop a run, releasing its lease after the last command it queued. - Emits `driver_left` once the session was open, however it ends, so the - gate releases a lease even when the session fails. Cancellation is the - exception: that is the truck shutting down, whose gate no longer reads - the queue, so waiting to enqueue there would hang the shutdown. + A run that ended on its own already queued its `driver_left`, and a + shutting-down truck must not wait to enqueue one. """ - driver = incoming.peer - try: - stream = await incoming.accept() - # A dial forged in another endpoint's name fails here, as does one whose - # dialer gave up first; neither ever reached the gate. - except moq.Error as err: # pyright: ignore[reportGeneralTypeIssues] - log.info("driver %s not accepted: %s", driver, err) - return - - session = stream.session - key = (driver, session) - if key in self._replies: - # It would share the open session's lease and replies; refuse it. - log.info("driver %s dialed a session id it already has open", driver) - stream.producer.finish() - return - stream.consumer.update(moq.Subscription(delay=moq.Delay(max=CONTROL_DELAY), order=moq.Order.ASCENDING)) - self._replies[key] = stream.producer - log.info("driver %s connected", driver) + task.cancel() + await asyncio.gather(task, return_exceptions=True) + if task.cancelled() and not self._closing: + await self._commands.put(Command(kind="driver_left", driver_id=driver, instance=instance)) + + async def _pump(self, driver: str, instance: str, broadcast: moq.BroadcastConsumer) -> None: + """Feed one driver run's `control` track into the command queue. + + Emits `driver_left` once the run ends on its own, however it ends, so the + gate releases a lease even when the track fails. A stopped run leaves that + to `_end`. + """ + key = (driver, instance) + log.info("driver %s connected (run %s)", driver, instance) try: - while (frame := await stream.consumer.read_frame()) is not None: + control = await broadcast.subscribe_track( + "control", moq.Subscription(delay=moq.Delay(max=CONTROL_DELAY), order=moq.Order.ASCENDING) + ) + while (frame := await control.read_frame()) is not None: if key in self._revoked: break - command = parse_command(driver, _decode(frame.payload), session) + command = parse_command(driver, _decode(frame.payload), instance) if command is not None: await self._commands.put(command) except moq.Error as err: # pyright: ignore[reportGeneralTypeIssues] - log.info("driver %s session ended: %s", driver, err) + log.info("driver %s track ended: %s", driver, err) finally: - del self._replies[key] - # Closes this side too; a session the peer reset has nothing left to close. - with contextlib.suppress(moq.Error): # pyright: ignore[reportGeneralTypeIssues] - stream.producer.finish() - log.info("driver %s disconnected", driver) - await self._commands.put(Command(kind="driver_left", driver_id=driver, session=session)) + log.info("driver %s disconnected (run %s)", driver, instance) + await self._commands.put(Command(kind="driver_left", driver_id=driver, instance=instance)) def _decode(payload: bytes) -> Any: @@ -443,7 +453,6 @@ def _decode(payload: bytes) -> Any: __all__ = [ "CONTROL_DELAY", - "SOCKET_PREFIX", "Drivers", "StatusPublisher", "Collision", diff --git a/demo/carla/sim/moq_carla_sim/runtime.py b/demo/carla/sim/moq_carla_sim/runtime.py index a300cf73341..ea22b90b92b 100644 --- a/demo/carla/sim/moq_carla_sim/runtime.py +++ b/demo/carla/sim/moq_carla_sim/runtime.py @@ -1,8 +1,8 @@ """The per-truck control loop. One `TruckSession` per truck: it owns the safety gate, the MoQ broadcast, the -socket endpoint its drivers dial, and the truck's bridge stream, whose -compressed video the bridge publishes into that broadcast. +drivers it follows, and the truck's bridge stream, whose compressed video the +bridge publishes into that broadcast. """ from __future__ import annotations @@ -113,7 +113,8 @@ def __init__( # memory-growth path under one that floods; a full queue instead blocks # that driver's own task. self._commands: asyncio.Queue[Command] = asyncio.Queue(maxsize=COMMAND_QUEUE_DEPTH) - self._drivers = Drivers(truck.name, self._commands) + self._drivers = Drivers(self._commands) + self._media_pipeline: dict[str, list[dict[str, Any]]] = {} self._deadline: float | None = None self._stuck = StuckDetector() # The route starts hidden; the player discovers trucks by announcement, @@ -135,17 +136,17 @@ def commands(self) -> asyncio.Queue[Command]: def drivers(self) -> Drivers: return self._drivers - async def run(self, origin: moq.OriginProducer) -> None: + async def run(self, client: moq.Client, drivers: str) -> None: """Consume driver commands and actuate until cancelled. - The driver endpoint runs in a task group rather than a bare task, so a + Driver discovery under `drivers` runs in a task group rather than a bare task, so a failure there fails the session. A detached task's exception is never retrieved: the truck would keep publishing video and status while silently accepting no driver ever again, which reads as healthy. """ self._publish() async with asyncio.TaskGroup() as group: - watcher = group.create_task(self._drivers.run(origin), name=f"drivers:{self._truck.name}") + watcher = group.create_task(self._drivers.run(client, drivers), name=f"drivers:{self._truck.name}") # Video demand is what renders the world for this truck, and the # bridge follows the same demand to pick its camera. demand = group.create_task( @@ -185,26 +186,23 @@ async def _loop(self) -> None: continue # Only the lease holder's accepted traffic refreshes the watchdog - # and is acknowledged. A bystander's e-stop still latches, but must - # not extend the holder's lease, and an e-stopped holder's refused - # drive frames must not keep the lease alive forever. - acknowledged = command.kind in ("drive", "estop", "reset_estop") and self._drive.holds(command) - if acknowledged: + # and its pipeline timing, which `status` echoes. A bystander's + # e-stop still latches, but must not extend the holder's lease, and + # an e-stopped holder's refused drive frames must not keep the lease + # alive forever. + if command.kind in ("drive", "estop", "reset_estop") and self._drive.holds(command): + self._media_pipeline = {command.driver_id: latency_breakdown(command.timestamps)} self._deadline = asyncio.get_running_loop().time() + COMMAND_TIMEOUT - elif command.kind == "driver_left": + elif command.kind in ("driver_left", "watchdog_expired"): + self._media_pipeline = {} self._deadline = None - elif command.kind == "watchdog_expired": - self._deadline = None - log.warning("%s: control watchdog expired, returning to autonomy", self._truck.name) + if command.kind == "watchdog_expired": + log.warning("%s: control watchdog expired, returning to autonomy", self._truck.name) # Actuate before publishing, so the world and the status echo can # never disagree about what was accepted. self._actuate() self._publish() - if acknowledged: - self._drivers.reply( - command, {"sequence": command.sequence, "media_pipeline": latency_breakdown(command.timestamps)} - ) async def _report_video(self) -> None: """Log the published frame rate per rendition. @@ -248,8 +246,9 @@ async def _publish_telemetry(self) -> None: handing back, which engages autonomy within a tick once it slows to walking pace. A driver who parked and left the tab open, or e-stopped and never reset, holds the lease for as long as it heartbeats, so the - respawn revokes that driver's session: the player lets go rather than - reclaiming the fresh spawn, and the fresh gate clears any latch. + respawn revokes that driver's run: the truck ignores the rest of it, the + player lets go once `status` stops naming it, and the fresh gate clears + any latch. """ while True: await asyncio.sleep(TELEMETRY_INTERVAL) @@ -286,6 +285,7 @@ def _respawn(self) -> None: self._truck.respawn() self._drive = Drive() self._deadline = None + self._media_pipeline = {} self._stuck.reset() self._hold_clock() @@ -346,6 +346,8 @@ def _publish(self, telemetry: Telemetry | None = None) -> None: estop_by=self._drive.estop_by, autonomous=self._truck.autonomous, driver=self._drive.driver, + sequence=self._drive.sequence, + media_pipeline=self._media_pipeline, location=self._location, map_name=self._map_name, telemetry=self._truck.telemetry() if telemetry is None else telemetry, diff --git a/demo/carla/sim/systemd/install-user-service.sh b/demo/carla/sim/systemd/install-user-service.sh index d5c2e7e3911..129ea0f4f3c 100755 --- a/demo/carla/sim/systemd/install-user-service.sh +++ b/demo/carla/sim/systemd/install-user-service.sh @@ -44,8 +44,8 @@ Before starting, write the relay settings to: CARLA_IMAGE=localhost/moq-carla: The token is a demo-project JWT scoped to publish carla/** and subscribe -carla/.client/**, the endpoints drivers dial from. For local -development, MOQ_URL=http://localhost:4443 needs no token. +viewer/carla/**. For local development, MOQ_URL=http://localhost:4443 needs +no token. Then run: just carla service-enable diff --git a/demo/carla/sim/systemd/install-user-service.test.sh b/demo/carla/sim/systemd/install-user-service.test.sh index 1282474b963..fcd3de472dd 100755 --- a/demo/carla/sim/systemd/install-user-service.test.sh +++ b/demo/carla/sim/systemd/install-user-service.test.sh @@ -45,7 +45,7 @@ CARLA_SYSTEMD_USER_DIR="$tmp/units" \ grep -Fxq ' MOQ_URL=https://cdn.moq.dev/demo?jwt=' "$tmp/install.out" grep -Fxq ' CARLA_IMAGE=localhost/moq-carla:' "$tmp/install.out" grep -Fq 'scoped to publish carla/** and subscribe' "$tmp/install.out" -grep -Fq 'carla/.client/**, the endpoints drivers dial from. For local' "$tmp/install.out" +grep -Fq 'viewer/carla/**. For local development, MOQ_URL=http://localhost:4443 needs' "$tmp/install.out" test "$(stat -c %a "$tmp/units")" = 700 test "$(stat -c %a "$tmp/config")" = 700 test "$(stat -c %a "$tmp/units/carla-sim.service")" = 600 diff --git a/demo/carla/sim/tests/conftest.py b/demo/carla/sim/tests/conftest.py index b0845af8c01..39154c02d5a 100644 --- a/demo/carla/sim/tests/conftest.py +++ b/demo/carla/sim/tests/conftest.py @@ -10,6 +10,25 @@ class MoqError(Exception): pass +class AnnounceEvent: + """One announcement event; the subclass names which kind, as in moq's.""" + + def __init__(self, announce: SimpleNamespace) -> None: + self.announce = announce + + +class AnnounceEventPresent(AnnounceEvent): + pass + + +class AnnounceEventAbsent(AnnounceEvent): + pass + + +class AnnounceEventOffline(AnnounceEvent): + pass + + class SnapshotProducer: """Records every published value; demand comes from the wrapped track.""" @@ -30,10 +49,12 @@ def update(self, value: object, timestamp: object) -> None: moq.Delay = lambda **kwargs: SimpleNamespace(**kwargs) moq.Order = SimpleNamespace(ASCENDING="ascending") moq.Compression = SimpleNamespace(DEFLATE="deflate") -# Tests that run a truck's endpoint script it in place of this. -moq.socket = ModuleType("moq.socket") +# The stub has no shutdown errors: every `MoqError` it raises is a failure. +moq.is_shutdown = lambda err: False +moq.AnnounceEventPresent = AnnounceEventPresent +moq.AnnounceEventAbsent = AnnounceEventAbsent +moq.AnnounceEventOffline = AnnounceEventOffline moq.Timestamp = lambda value, scale: SimpleNamespace(value=value, scale=scale) -moq.Timestamp.unscaled = lambda key: SimpleNamespace(value=key, scale=None) moq.TrackInfo = lambda **kwargs: SimpleNamespace(**kwargs) moq_json = ModuleType("moq.json") diff --git a/demo/carla/sim/tests/test_drive.py b/demo/carla/sim/tests/test_drive.py index 499d77d2da7..d639f61417d 100644 --- a/demo/carla/sim/tests/test_drive.py +++ b/demo/carla/sim/tests/test_drive.py @@ -4,75 +4,60 @@ import pytest -from moq_carla_sim.drive import Command, Drive, DriveState, Gear, handle +from moq_carla_sim.drive import Command, Drive, DriveState, Gear def drive(driver_id: str, sequence: int, throttle: float) -> Command: return Command( kind="drive", driver_id=driver_id, - session=driver_id, sequence=sequence, state=DriveState(throttle=throttle, brake=0.0), ) def estop(driver_id: str, sequence: int) -> Command: - return Command(kind="estop", driver_id=driver_id, session=driver_id, sequence=sequence) + return Command(kind="estop", driver_id=driver_id, sequence=sequence) def reset(driver_id: str, sequence: int) -> Command: - return Command(kind="reset_estop", driver_id=driver_id, session=driver_id, sequence=sequence) + return Command(kind="reset_estop", driver_id=driver_id, sequence=sequence) def test_first_driver_keeps_control() -> None: state = Drive() assert state.apply(drive("first", 1, 0.5)) assert not state.apply(drive("second", 2, 1.0)) - assert state.driver == handle("first") + assert state.driver == "first" assert state.state.throttle == 0.0 assert state.state.brake == 1.0 assert state.apply(drive("first", 2, 0.6)) assert state.state.throttle == 0.6 -def test_the_lease_belongs_to_one_session_of_a_driver() -> None: - """A driver id on another session is another driver: it cannot steer while - the first session holds the lease, nor release it.""" +def test_the_lease_belongs_to_one_run_of_a_driver() -> None: + """A driver id announced under another instance is another driver: it cannot + steer while the first run holds the lease, nor release it.""" state = Drive() - first = Command(kind="drive", driver_id="abc", session="s1", sequence=1, state=DriveState(throttle=0.5, brake=0.0)) - second = Command(kind="drive", driver_id="abc", session="s2", sequence=1, state=DriveState(throttle=1.0, brake=0.0)) - assert state.apply(Command(kind="drive", driver_id="abc", session="s1")) + first = Command( + kind="drive", driver_id="abc", instance="run-1", sequence=1, state=DriveState(throttle=0.5, brake=0.0) + ) + second = Command( + kind="drive", driver_id="abc", instance="run-2", sequence=1, state=DriveState(throttle=1.0, brake=0.0) + ) + assert state.apply(Command(kind="drive", driver_id="abc", instance="run-1")) assert state.apply(first) assert not state.apply(second) assert state.state.throttle == 0.5 - assert not state.apply(Command(kind="driver_left", driver_id="abc", session="s2")) - assert state.driver == handle("s1") + assert not state.apply(Command(kind="driver_left", driver_id="abc", instance="run-2")) + assert state.driver == "abc" - # The first session leaving frees the lease for the second. - assert state.apply(Command(kind="driver_left", driver_id="abc", session="s1")) + # The first run leaving frees the lease for the second. + assert state.apply(Command(kind="driver_left", driver_id="abc", instance="run-1")) assert state.driver is None assert state.apply(second) assert state.holds(second) - assert state.driver == handle("s2") - - -def test_status_names_sessions_by_a_handle_the_player_derives_too() -> None: - """The handle, never the endpoint id, names the holder and the e-stop's sender. - `web/src/paths.test.ts` pins the player to the same handle for this session.""" - state = Drive() - session = "0123456789abcdef0123456789abcdef" - assert state.apply(Command(kind="drive", driver_id="driver-holder", session=session)) - assert state.apply(Command(kind="estop", driver_id="driver-holder", session=session)) - assert state.driver == state.estop_by == "driver-3eb1bd43" - - -def test_a_command_without_a_session_takes_no_lease_but_still_stops() -> None: - state = Drive() - assert not state.apply(Command(kind="drive", driver_id="driver")) - assert state.driver is None - assert state.apply(Command(kind="estop", driver_id="driver")) - assert state.estopped and state.estop_by is None + assert state.driver == "abc" def test_estop_latches_until_explicit_reset() -> None: @@ -92,6 +77,7 @@ def test_watchdog_stops_and_releases_driver() -> None: assert state.apply(drive("driver", 1, 1.0)) assert state.apply(Command(kind="watchdog_expired")) assert state.driver is None + assert state.sequence is None assert state.state == DriveState.stopped() @@ -100,11 +86,11 @@ def test_disconnect_does_not_clear_estop() -> None: assert state.apply(drive("driver", 1, 1.0)) assert state.apply(estop("driver", 2)) assert state.estopped - assert state.driver == handle("driver") + assert state.driver == "driver" # The driver disconnects: the lease releases but the latch must persist, so # the truck stays stopped until an explicit reset. - assert state.apply(Command(kind="driver_left", driver_id="driver", session="driver")) + assert state.apply(Command(kind="driver_left", driver_id="driver")) assert state.estopped assert state.driver is None @@ -116,34 +102,49 @@ def test_estop_from_a_bystander_latches_without_transferring_the_lease() -> None # Anyone may hit the big red button, so the latch must engage... assert state.apply(estop("second", 2)) assert state.estopped - assert state.estop_by == handle("second") + assert state.estop_by == "second" - # ...but the lease stays with "first". - assert state.driver == handle("first") + # ...but the lease stays with "first", and the bystander's sequence is not + # recorded as the authoritative command. + assert state.driver == "first" + assert state.sequence == 1 - # The holder's own e-stop does not replace the actor that originally - # latched the stop. + # The holder heartbeat is acknowledged without replacing the actor that + # originally latched the stop. assert state.apply(estop("first", 3)) - assert state.estop_by == handle("second") - assert state.holds(estop("first", 3)) + assert state.estop_by == "second" + assert state.sequence == 3 - # Anyone may clear the global latch, but doing so does not transfer the - # lease. + # Anyone may clear the global latch, but doing so neither transfers the + # lease nor records the bystander's sequence as authoritative. assert state.apply(reset("second", 4)) assert not state.estopped assert state.estop_by is None - assert state.driver == handle("first") + assert state.driver == "first" + assert state.sequence == 3 assert not state.apply(drive("second", 5, 1.0)) +def test_estop_from_holder_refreshes_the_acknowledgement_sequence() -> None: + state = Drive() + assert state.apply(drive("holder", 5, 0.3)) + assert state.sequence == 5 + + # The holder's own e-stop is acknowledged (sequence advances) and it keeps + # the lease. + assert state.apply(estop("holder", 6)) + assert state.estopped + assert state.sequence == 6 + assert state.driver == "holder" + + def test_axes_are_clamped_into_range() -> None: state = Drive() - assert state.apply(Command(kind="drive", driver_id="driver", session="driver", sequence=1)) + assert state.apply(Command(kind="drive", driver_id="driver", sequence=1)) assert state.apply( Command( kind="drive", driver_id="driver", - session="driver", sequence=2, state=DriveState(steering=-4.0, throttle=9.0, brake=-1.0, gear=Gear.FORWARD), ) @@ -163,7 +164,6 @@ def test_every_new_lease_resets_pedals_without_changing_steering_or_gear(gear: G command = Command( kind="drive", driver_id="driver", - session="driver", sequence=1, state=DriveState(steering=0.3, throttle=1.0, brake=0.0, gear=gear), ) @@ -193,12 +193,7 @@ def test_every_new_lease_resets_pedals_without_changing_steering_or_gear(gear: G def test_takeover_brake_allows_a_forward_claim_without_autonomous_feedback() -> None: state = Drive() assert state.apply( - Command( - kind="drive", - driver_id="driver", - session="driver", - state=DriveState(throttle=0.85, brake=0.0, gear=Gear.FORWARD), - ) + Command(kind="drive", driver_id="driver", state=DriveState(throttle=0.85, brake=0.0, gear=Gear.FORWARD)) ) assert state.state == DriveState(gear=Gear.FORWARD) @@ -211,7 +206,6 @@ def test_gear_change_requires_brake_and_released_throttle() -> None: Command( kind="drive", driver_id="driver", - session="driver", sequence=1, state=DriveState.stopped(), ) @@ -221,7 +215,6 @@ def test_gear_change_requires_brake_and_released_throttle() -> None: Command( kind="drive", driver_id="driver", - session="driver", sequence=2, state=DriveState(throttle=0.0, brake=0.0, gear=Gear.FORWARD), ) @@ -232,7 +225,6 @@ def test_gear_change_requires_brake_and_released_throttle() -> None: Command( kind="drive", driver_id="driver", - session="driver", sequence=3, state=DriveState(throttle=0.01, brake=0.001, gear=Gear.FORWARD), ) @@ -243,7 +235,6 @@ def test_gear_change_requires_brake_and_released_throttle() -> None: Command( kind="drive", driver_id="driver", - session="driver", sequence=4, state=DriveState(throttle=0.02, brake=1.0, gear=Gear.REVERSE), ) diff --git a/demo/carla/sim/tests/test_main.py b/demo/carla/sim/tests/test_main.py index 09678915033..d999629431e 100644 --- a/demo/carla/sim/tests/test_main.py +++ b/demo/carla/sim/tests/test_main.py @@ -6,8 +6,7 @@ import pytest from moq_carla_sim import main as main_module -from moq_carla_sim.main import broadcast_path, default_trucks, parse_args -from moq_carla_sim.publish import Drivers +from moq_carla_sim.main import broadcast_path, default_trucks, driver_prefix, parse_args from moq_carla_sim.watchdog import Heartbeat @@ -38,7 +37,7 @@ def test_relay_url_defaults_to_the_local_relay(monkeypatch: pytest.MonkeyPatch) def test_paths_match_the_player() -> None: assert broadcast_path("truck1", "front") == "carla/truck1/front.hang" - assert Drivers("truck1", asyncio.Queue()).path == "carla/.server/truck1" + assert driver_prefix("truck1") == "viewer/carla/truck1" def test_log_level_configures_python_and_rust(monkeypatch: pytest.MonkeyPatch) -> None: @@ -189,7 +188,7 @@ def destroy(self) -> None: class FakeConnect: async def __aenter__(self): events.append("session.open") - return SimpleNamespace(session=SimpleNamespace(publish=SimpleNamespace)) + return SimpleNamespace() async def __aexit__(self, *_exc) -> None: events.append("session.close") @@ -210,13 +209,7 @@ async def fake_connect_bridge(_path: str) -> FakeBridge: monkeypatch.setattr(main_module.World, "connect", staticmethod(lambda *_args: FakeWorld())) monkeypatch.setattr(main_module, "connect_bridge", fake_connect_bridge) - - def fake_connect(_url: str, *, versions: tuple[str, ...]) -> FakeConnect: - # Drivers dial the trucks' endpoints, which only these versions carry. - assert versions == ("moq-lite-07-wip", "moq-transport-22") - return FakeConnect() - - monkeypatch.setattr(main_module.moq, "connect", fake_connect, raising=False) + monkeypatch.setattr(main_module.moq, "connect", lambda _url: FakeConnect(), raising=False) monkeypatch.setattr(main_module, "supervise", fake_supervise) asyncio.run(main_module.run(parse_args(["--url", "http://localhost:4443"]))) diff --git a/demo/carla/sim/tests/test_publish.py b/demo/carla/sim/tests/test_publish.py index 5ce4d2ffa6c..0c9174e37b1 100644 --- a/demo/carla/sim/tests/test_publish.py +++ b/demo/carla/sim/tests/test_publish.py @@ -6,7 +6,7 @@ import pytest from moq_carla_sim import publish -from moq_carla_sim.drive import Command, Drive, DriveState, Gear, TimestampEntry, handle +from moq_carla_sim.drive import Command, Drive, DriveState, Gear, TimestampEntry from moq_carla_sim.publish import ( Collision, Motion, @@ -26,6 +26,8 @@ def status(**overrides) -> Status: estop_by=None, autonomous=False, driver=None, + sequence=None, + media_pipeline={}, location=None, map_name=None, telemetry=None, @@ -56,7 +58,6 @@ def test_malformed_timestamp_does_not_drop_driver_command() -> None: {"label": "received", "ts": 12.5}, ], }, - "s1", ) assert command is not None @@ -70,7 +71,6 @@ def test_only_the_players_pipeline_stages_are_kept() -> None: command = parse_command( "driver", {"type": "drive", "sequence": 1, "timestamps": [*stages, *junk, {"label": "received", "ts": 9.0}]}, - "s1", ) assert command is not None @@ -81,7 +81,7 @@ def test_only_the_players_pipeline_stages_are_kept() -> None: def test_respawn_command_is_carla_only_control_input() -> None: - command = parse_command("driver", {"type": "respawn", "sequence": 8}, "s1") + command = parse_command("driver", {"type": "respawn", "sequence": 8}) assert command is not None assert command.kind == "respawn" @@ -90,15 +90,12 @@ def test_respawn_command_is_carla_only_control_input() -> None: def test_status_names_the_driver_that_latched_estop() -> None: - payload = status_payload(status(estopped=True, estop_by="bystander", driver="holder")) + payload = status_payload(status(estopped=True, estop_by="bystander", driver="holder", sequence=4)) assert payload["driver"] == "holder" assert payload["estop_by"] == "bystander" assert payload["mode"] == "simulated" assert payload["capabilities"] == ["respawn"] - # Acknowledgements are the holder's alone, on its own session. - assert "sequence" not in payload - assert "media_pipeline" not in payload def test_status_carries_the_map_and_a_rounded_pose() -> None: @@ -180,16 +177,57 @@ def closed(_payload: object, _timestamp: object) -> None: publisher.publish({}) -class Session: - """One accepted driver session: controls the driver sends, replies the truck writes.""" +class Scripted: + """A scripted async source. `send` returns once the reader has handled the + item and come back for the next one.""" - def __init__(self, id: str) -> None: - self.session = id - self.consumer = self - self.producer = self + def __init__(self) -> None: + self._items: asyncio.Queue = asyncio.Queue() + self._idle = asyncio.Event() + + async def next(self): + if self._items.empty(): + self._idle.set() + item = await self._items.get() + self._idle.clear() + return item + + async def send(self, item) -> None: + self._idle.clear() + self._items.put_nowait(item) + await self._idle.wait() + + +class Announcements(Scripted): + """The scripted listing of driver paths, by the id the filter captured.""" + + async def __aenter__(self): + return self + + async def __aexit__(self, *_exc) -> None: + pass + + def __aiter__(self): + return self + + async def __anext__(self): + return await self.next() + + async def present(self, driver: str) -> None: + await self.send(moq.AnnounceEventPresent(SimpleNamespace(captures=[driver]))) + + async def absent(self, driver: str) -> None: + await self.send(moq.AnnounceEventAbsent(SimpleNamespace(captures=[driver]))) + + async def offline(self, driver: str) -> None: + await self.send(moq.AnnounceEventOffline(SimpleNamespace(captures=[driver]))) + + +class Run: + """One driver run's broadcast: its `control` track hands out what `send` queues.""" + + def __init__(self) -> None: self.subscription = None - self.replies: list[dict] = [] - self.finished = False self._frames: asyncio.Queue = asyncio.Queue() def send(self, message) -> None: @@ -199,8 +237,10 @@ def send(self, message) -> None: def end(self, error: BaseException | None = None) -> None: self._frames.put_nowait(error) - def update(self, subscription) -> None: + async def subscribe_track(self, name: str, subscription): + assert name == "control" self.subscription = subscription + return self async def read_frame(self): frame = await self._frames.get() @@ -208,74 +248,77 @@ async def read_frame(self): raise frame return frame - def largest(self): - return SimpleNamespace(group=len(self.replies) - 1) if self.replies else None - def write_frame(self, payload: bytes, timestamp) -> None: - if self.finished: - raise moq.Error("session closed") - assert timestamp == moq.Timestamp.unscaled(len(self.replies)), "a reply keys the next group" - self.replies.append(json.loads(payload)) +class Following(Scripted): + """A scripted follow of one path: each `run` hands out another instance, `offline` a `None`, + and `refuse` an error.""" - def finish(self) -> None: - self.finished = True + async def __aenter__(self): + return self + async def __aexit__(self, *_exc) -> None: + pass + + async def available(self): + item = await self.next() + if isinstance(item, BaseException): + raise item + return item -class Incoming: - """A dial claiming `peer`; accepting it fails the return-routability check when forged.""" + async def refuse(self) -> None: + """Fail the next `available()`, as a handler refusing the path does.""" + await self.send(moq.Error("refused")) - def __init__(self, peer: str, session: Session | None) -> None: - self.peer = peer - self._session = session + async def run(self, *messages) -> Run: + """Hand out an instance whose control track holds `messages`, then stays open.""" + run = Run() + for message in messages: + run.send(message) + await self.send(run) + return run - async def accept(self) -> Session: - if self._session is None: - raise moq.Error("not dialed") - return self._session + async def offline(self) -> None: + await self.send(None) -class Endpoint: - """A truck's endpoint, handing out scripted dials.""" +class Relay: + """A relay holding driver broadcasts.""" def __init__(self) -> None: - self.dials: asyncio.Queue = asyncio.Queue() - self.opened: list[tuple[str, str]] = [] - self.closed = False + self.announcements = Announcements() + self.followed: list[str] = [] + self._followings: dict[str, Following] = {} - def __call__(self, origin, prefix: str, id: str) -> "Endpoint": - self.opened.append((prefix, id)) - return self + def announced(self, prefix: str, *, filter: str): + assert (prefix, filter) == (PREFIX, "*") + return self.announcements - async def __aenter__(self) -> "Endpoint": - return self + def following(self, driver: str) -> Following: + return self._followings.setdefault(f"{PREFIX}/{driver}", Following()) - async def __aexit__(self, *_exc) -> None: - self.closed = True + def follow(self, path: str) -> Following: + self.followed.append(path) + return self._followings.setdefault(path, Following()) - async def accept_track(self) -> Incoming: - return await self.dials.get() - def dial(self, peer: str, id: str | None) -> Session | None: - session = None if id is None else Session(id) - self.dials.put_nowait(Incoming(peer, session)) - return session +PREFIX = "viewer/carla/truck1" +DRIVE = {"type": "drive", "sequence": 1, "throttle": 0.5} -async def serving(scenario, monkeypatch: pytest.MonkeyPatch) -> None: - """Run `scenario(endpoint, drivers, commands, gate)` against a live `Drivers`.""" - endpoint = Endpoint() - monkeypatch.setattr(moq.socket, "Endpoint", endpoint, raising=False) +async def watching(scenario) -> None: + """Run `scenario(relay, drivers, commands, gate)` against a live `Drivers`.""" + relay = Relay() commands: asyncio.Queue = asyncio.Queue() - drivers = publish.Drivers("truck1", commands) - task = asyncio.create_task(drivers.run(SimpleNamespace())) - # Raced against the endpoint, so a failed pump fails the test rather than leaving it waiting. - play = asyncio.create_task(scenario(endpoint, drivers, commands, Drive())) + drivers = publish.Drivers(commands) + task = asyncio.create_task(drivers.run(relay, PREFIX)) + # Raced against the watcher, so a failed one fails the test rather than leaving it waiting. + play = asyncio.create_task(scenario(relay, drivers, commands, Drive())) try: await asyncio.wait({task, play}, return_when=asyncio.FIRST_COMPLETED) - # The pump never returns on its own, so one that ended failed, whichever finished first. + # The watcher never returns on its own, so one that ended failed, whichever finished first. if task.done(): task.result() - raise AssertionError("the driver endpoint stopped before the scenario finished") + raise AssertionError("the driver watcher stopped before the scenario finished") play.result() finally: play.cancel() @@ -283,8 +326,6 @@ async def serving(scenario, monkeypatch: pytest.MonkeyPatch) -> None: task.cancel() with pytest.raises(asyncio.CancelledError): await task - assert endpoint.opened == [("carla", "truck1")] - assert endpoint.closed async def next_command(commands: asyncio.Queue, gate: Drive): @@ -293,190 +334,191 @@ async def next_command(commands: asyncio.Queue, gate: Drive): return command -DRIVE = {"type": "drive", "sequence": 1, "throttle": 0.5} - - -def test_a_driver_whose_session_ends_loses_the_lease(monkeypatch: pytest.MonkeyPatch) -> None: - async def scenario(endpoint: Endpoint, drivers, commands: asyncio.Queue, gate: Drive) -> None: - session = endpoint.dial("driver-abc", "s1") - session.send(DRIVE) +def test_a_driver_whose_broadcast_goes_absent_loses_the_lease() -> None: + async def scenario(relay: Relay, drivers, commands: asyncio.Queue, gate: Drive) -> None: + await relay.announcements.present("abc") + await relay.following("abc").run(DRIVE) command = await next_command(commands, gate) - assert (command.kind, command.driver_id, command.session) == ("drive", "driver-abc", "s1") - assert gate.driver == handle("s1") + assert (command.kind, command.driver_id) == ("drive", "abc") + assert gate.driver == "abc" - session.end() + await relay.announcements.absent("abc") left = await next_command(commands, gate) - assert (left.kind, left.session) == ("driver_left", "s1") + assert (left.kind, left.instance) == ("driver_left", command.instance) assert gate.driver is None - assert session.finished - asyncio.run(serving(scenario, monkeypatch)) + asyncio.run(watching(scenario)) -def test_a_failed_session_still_releases_its_lease(monkeypatch: pytest.MonkeyPatch) -> None: - async def scenario(endpoint: Endpoint, drivers, commands: asyncio.Queue, gate: Drive) -> None: - session = endpoint.dial("driver-abc", "s1") - session.send(DRIVE) +def test_a_driver_this_side_lets_go_of_loses_the_lease() -> None: + async def scenario(relay: Relay, drivers, commands: asyncio.Queue, gate: Drive) -> None: + await relay.announcements.present("abc") + await relay.following("abc").run(DRIVE) await next_command(commands, gate) - session.end(moq.Error("reset")) - left = await next_command(commands, gate) - assert (left.kind, left.session) == ("driver_left", "s1") + await relay.announcements.offline("abc") + assert (await next_command(commands, gate)).kind == "driver_left" assert gate.driver is None - asyncio.run(serving(scenario, monkeypatch)) - + # Back again, it is followed afresh. + await relay.announcements.present("abc") + await relay.following("abc").run(DRIVE) + assert (await next_command(commands, gate)).kind == "drive" + assert relay.followed == [f"{PREFIX}/abc"] * 2 -def test_shutdown_never_waits_on_a_full_command_queue(monkeypatch: pytest.MonkeyPatch) -> None: - """Shutdown stops the gate first, so a pump cancelled then must not wait to report its departure.""" + asyncio.run(watching(scenario)) - async def scenario() -> None: - endpoint = Endpoint() - monkeypatch.setattr(moq.socket, "Endpoint", endpoint, raising=False) - commands: asyncio.Queue = asyncio.Queue(maxsize=1) - drivers = publish.Drivers("truck1", commands) - task = asyncio.create_task(drivers.run(SimpleNamespace())) - session = endpoint.dial("driver-abc", "s1") - session.send(DRIVE) - session.send(DRIVE) - # The queue is full and the second control is waiting for room. - for _ in range(10): - await asyncio.sleep(0) - assert commands.full() - task.cancel() - await asyncio.wait_for(asyncio.gather(task, return_exceptions=True), timeout=1) - assert session.finished +def test_a_driver_whose_run_goes_offline_loses_the_lease_until_it_returns() -> None: + async def scenario(relay: Relay, drivers, commands: asyncio.Queue, gate: Drive) -> None: + await relay.announcements.present("abc") + await relay.following("abc").run(DRIVE) + first = await next_command(commands, gate) - asyncio.run(scenario()) + await relay.following("abc").offline() + left = await next_command(commands, gate) + assert (left.kind, left.instance) == ("driver_left", first.instance) + assert gate.driver is None + await relay.following("abc").run(DRIVE) + second = await next_command(commands, gate) + assert second.kind == "drive" and second.instance != first.instance + assert gate.driver == "abc" -def test_a_new_session_of_the_same_driver_is_a_new_driver(monkeypatch: pytest.MonkeyPatch) -> None: - """A lease is one session: an endpoint posing under the holder's id never continues it.""" + asyncio.run(watching(scenario)) - async def scenario(endpoint: Endpoint, drivers, commands: asyncio.Queue, gate: Drive) -> None: - first = endpoint.dial("driver-abc", "s1") - first.send(DRIVE) - await next_command(commands, gate) - second = endpoint.dial("driver-abc", "s2") - second.send({**DRIVE, "throttle": 1.0}) +def test_a_failed_control_track_still_releases_its_lease() -> None: + async def scenario(relay: Relay, drivers, commands: asyncio.Queue, gate: Drive) -> None: + await relay.announcements.present("abc") + run = await relay.following("abc").run(DRIVE) command = await next_command(commands, gate) - assert command.session == "s2" - assert not gate.holds(command) - second.end() - assert (await next_command(commands, gate)).kind == "driver_left" - assert gate.driver == handle("s1") - assert gate.holds(Command(kind="drive", driver_id="driver-abc", session="s1")) + run.end(moq.Error("reset")) + left = await next_command(commands, gate) + assert (left.kind, left.instance) == ("driver_left", command.instance) + assert gate.driver is None - asyncio.run(serving(scenario, monkeypatch)) + # The run's own departure is its only one, however the follow ends it later. + await relay.following("abc").offline() + assert commands.empty() + asyncio.run(watching(scenario)) -def test_a_second_session_under_an_open_driver_and_session_id_is_refused(monkeypatch: pytest.MonkeyPatch) -> None: - async def scenario(endpoint: Endpoint, drivers, commands: asyncio.Queue, gate: Drive) -> None: - first = endpoint.dial("driver-abc", "s1") - first.send(DRIVE) - await next_command(commands, gate) - copy = endpoint.dial("driver-abc", "s1") - copy.send({**DRIVE, "throttle": 1.0}) - first.send({**DRIVE, "sequence": 2}) - command = await next_command(commands, gate) - assert command.sequence == 2 - assert copy.finished - assert commands.empty() - drivers.reply(command, {"sequence": 2}) - assert first.replies == [{"sequence": 2}] +def test_availability_changes_never_follow_a_driver_twice() -> None: + async def scenario(relay: Relay, drivers, commands: asyncio.Queue, gate: Drive) -> None: + await relay.announcements.present("abc") + await relay.announcements.send(moq.AnnounceEventPresent(SimpleNamespace(captures=["abc"]))) + assert relay.followed == [f"{PREFIX}/abc"] - asyncio.run(serving(scenario, monkeypatch)) + asyncio.run(watching(scenario)) -def test_a_forged_dial_never_reaches_the_gate(monkeypatch: pytest.MonkeyPatch) -> None: - async def scenario(endpoint: Endpoint, drivers, commands: asyncio.Queue, gate: Drive) -> None: - endpoint.dial("driver-forged", None) - session = endpoint.dial("driver-abc", "s1") - session.send(DRIVE) +def test_a_refused_driver_is_still_followed() -> None: + """A refusal leaves the follow waiting, so the driver's next run still takes the lease.""" + + async def scenario(relay: Relay, drivers, commands: asyncio.Queue, gate: Drive) -> None: + await relay.announcements.present("abc") + await relay.following("abc").refuse() + await relay.following("abc").run(DRIVE) command = await next_command(commands, gate) - assert command.driver_id == "driver-abc" - assert commands.empty() + assert (command.kind, command.driver_id) == ("drive", "abc") + assert relay.followed == [f"{PREFIX}/abc"] - asyncio.run(serving(scenario, monkeypatch)) + asyncio.run(watching(scenario)) -def test_a_malformed_control_is_dropped_and_the_session_keeps_going(monkeypatch: pytest.MonkeyPatch) -> None: - async def scenario(endpoint: Endpoint, drivers, commands: asyncio.Queue, gate: Drive) -> None: - session = endpoint.dial("driver-abc", "s1") - session.send(b"\xff not json") - session.send(["not", "a", "command"]) - session.send(DRIVE) - command = await next_command(commands, gate) - assert command.kind == "drive" +def test_a_driver_that_restarts_under_a_new_instance_is_a_new_driver() -> None: + """The old run's lease ends with it, even though the path and id are the same.""" + + async def scenario(relay: Relay, drivers, commands: asyncio.Queue, gate: Drive) -> None: + await relay.announcements.present("abc") + await relay.following("abc").run(DRIVE) + first = await next_command(commands, gate) - asyncio.run(serving(scenario, monkeypatch)) + await relay.following("abc").run({**DRIVE, "sequence": 2}) + # The old run's departure comes first, after everything it queued. + left = await next_command(commands, gate) + assert (left.kind, left.instance) == ("driver_left", first.instance) + second = await next_command(commands, gate) + assert second.kind == "drive" and second.instance != first.instance + # The path stays present across the restart, so one follow sees both runs. + assert relay.followed == [f"{PREFIX}/abc"] + assert gate.holds(second) + assert not gate.apply(Command(kind="driver_left", driver_id="abc", instance=first.instance)) + assert gate.driver == "abc" -def test_a_late_control_never_blocks_a_newer_one(monkeypatch: pytest.MonkeyPatch) -> None: - """The truck reads oldest first, skipping a control one heartbeat behind the newest.""" + asyncio.run(watching(scenario)) - async def scenario(endpoint: Endpoint, drivers, commands: asyncio.Queue, gate: Drive) -> None: - session = endpoint.dial("driver-abc", "s1") - session.send(DRIVE) - await next_command(commands, gate) - assert session.subscription == moq.Subscription( - delay=moq.Delay(max=publish.CONTROL_DELAY), order=moq.Order.ASCENDING - ) - asyncio.run(serving(scenario, monkeypatch)) +def test_a_malformed_control_is_dropped_and_the_run_keeps_going() -> None: + async def scenario(relay: Relay, drivers, commands: asyncio.Queue, gate: Drive) -> None: + await relay.announcements.present("abc") + await relay.following("abc").run(b"\xff not json", ["not", "a", "command"], DRIVE) + command = await next_command(commands, gate) + assert command.kind == "drive" + assert commands.empty() + asyncio.run(watching(scenario)) -def test_replies_reach_only_their_own_open_session(monkeypatch: pytest.MonkeyPatch) -> None: - async def scenario(endpoint: Endpoint, drivers, commands: asyncio.Queue, gate: Drive) -> None: - first = endpoint.dial("driver-abc", "s1") - second = endpoint.dial("driver-def", "s2") - first.send(DRIVE) - second.send(DRIVE) - await next_command(commands, gate) - await next_command(commands, gate) - drivers.reply(Command(kind="drive", driver_id="driver-abc", session="s1"), {"sequence": 1}) - assert first.replies == [{"sequence": 1}] - assert second.replies == [] - # A session id is the dialer's pick, so only the pair names a session. - drivers.reply(Command(kind="drive", driver_id="driver-def", session="s1"), {"sequence": 9}) - assert first.replies == [{"sequence": 1}] - assert second.replies == [] +def test_a_late_control_never_blocks_a_newer_one() -> None: + """The truck reads oldest first, skipping a control one heartbeat behind the newest, + so a reset sent right before a drive still arrives whole.""" - first.end() - await next_command(commands, gate) - # The pump releasing the lease may race a reply to the gone session. - drivers.reply(Command(kind="drive", driver_id="driver-abc", session="s1"), {"sequence": 2}) - assert first.replies == [{"sequence": 1}] + async def scenario(relay: Relay, drivers, commands: asyncio.Queue, gate: Drive) -> None: + await relay.announcements.present("abc") + run = await relay.following("abc").run({"type": "reset_estop", "sequence": 1}, {**DRIVE, "sequence": 2}) + assert [(await commands.get()).kind for _ in range(2)] == ["reset_estop", "drive"] + assert run.subscription == moq.Subscription( + delay=moq.Delay(max=publish.CONTROL_DELAY), order=moq.Order.ASCENDING + ) - asyncio.run(serving(scenario, monkeypatch)) + asyncio.run(watching(scenario)) -def test_a_revoked_session_ends_and_nothing_it_sends_reaches_the_gate(monkeypatch: pytest.MonkeyPatch) -> None: - async def scenario(endpoint: Endpoint, drivers, commands: asyncio.Queue, gate: Drive) -> None: - session = endpoint.dial("driver-abc", "s1") - session.send(DRIVE) +def test_a_revoked_run_ends_and_nothing_it_sends_reaches_the_gate() -> None: + async def scenario(relay: Relay, drivers, commands: asyncio.Queue, gate: Drive) -> None: + await relay.announcements.present("abc") + run = await relay.following("abc").run(DRIVE) claim = await next_command(commands, gate) assert not drivers.revoked(claim) # A heartbeat already queued ahead of the revoke. - session.send(DRIVE) + run.send(DRIVE) queued = await commands.get() - drivers.revoke("driver-abc", "s1") - # The player reads the end of its replies and lets go. - assert session.finished + drivers.revoke(*gate.holder) assert drivers.revoked(queued) # One still in flight stops the pump, which reports the departure last. - session.send(DRIVE) + run.send(DRIVE) left = await commands.get() - assert (left.kind, left.session) == ("driver_left", "s1") + assert (left.kind, left.instance) == ("driver_left", claim.instance) assert drivers.revoked(left) - # Reading the departure forgets the session. + # Reading the departure forgets the run. assert not drivers.revoked(left) - asyncio.run(serving(scenario, monkeypatch)) + asyncio.run(watching(scenario)) + + +def test_shutdown_never_waits_on_a_full_command_queue() -> None: + """Shutdown stops the gate first, so a run cancelled then must not wait to report its departure.""" + + async def scenario() -> None: + relay = Relay() + commands: asyncio.Queue = asyncio.Queue(maxsize=1) + task = asyncio.create_task(publish.Drivers(commands).run(relay, PREFIX)) + await relay.announcements.present("abc") + # The queue fills and the second control waits for room. + await relay.following("abc").run(DRIVE, DRIVE) + for _ in range(10): + await asyncio.sleep(0) + assert commands.full() + + task.cancel() + await asyncio.wait_for(asyncio.gather(task, return_exceptions=True), timeout=1) + assert task.cancelled() + + asyncio.run(scenario()) diff --git a/demo/carla/sim/tests/test_runtime.py b/demo/carla/sim/tests/test_runtime.py index 283d55f99f5..2294ad8dd0e 100644 --- a/demo/carla/sim/tests/test_runtime.py +++ b/demo/carla/sim/tests/test_runtime.py @@ -5,7 +5,7 @@ import pytest from moq_carla_sim import runtime -from moq_carla_sim.drive import Command, Drive, DriveState, Gear, TimestampEntry, handle +from moq_carla_sim.drive import Command, Drive, DriveState, Gear, TimestampEntry from moq_carla_sim.publish import Pose from moq_carla_sim.runtime import MAX_STAGE_MS, StuckDetector, latency_breakdown from moq_carla_sim.world import Truck, drive_hold @@ -25,26 +25,21 @@ def release(self, reason: str) -> None: self.held.discard(reason) -class Replies: - """Records what the truck answers on each driver session.""" +class Revocations: + """Records which driver runs the truck revoked, until their departure.""" def __init__(self) -> None: - self.sent: list[tuple[str, dict]] = [] + self.revoked_runs: list[tuple[str, str | None]] = [] - self.revoked_sessions: list[tuple[str, str]] = [] - - def reply(self, command: Command, message: dict) -> None: - self.sent.append((command.session, message)) - - def revoke(self, driver: str, session: str) -> None: - self.revoked_sessions.append((driver, session)) + def revoke(self, driver: str, instance: str | None) -> None: + self.revoked_runs.append((driver, instance)) def revoked(self, command: Command) -> bool: - key = (command.driver_id, command.session) - if key not in self.revoked_sessions: + key = (command.driver_id, command.instance) + if key not in self.revoked_runs: return False if command.kind == "driver_left": - self.revoked_sessions.remove(key) + self.revoked_runs.remove(key) return True @@ -81,7 +76,7 @@ def test_takeover_preserves_steering_and_gear_but_resets_pedals(gear: Gear) -> N async def scenario() -> None: measured = DriveState(steering=-0.3, throttle=0.85, brake=0.0, gear=gear) commands = asyncio.Queue() - commands.put_nowait(Command(kind="drive", driver_id="driver", session="driver", sequence=1, state=measured)) + commands.put_nowait(Command(kind="drive", driver_id="driver", instance="driver", sequence=1, state=measured)) session = runtime.TruckSession.__new__(runtime.TruckSession) session._clock = FakeClock() @@ -92,7 +87,7 @@ async def scenario() -> None: session._drive = Drive() session._commands = commands session._deadline = None - session._drivers = Replies() + session._drivers = Revocations() actuated = [] published = asyncio.Event() session._truck.actuate = lambda accepted, *, has_driver: actuated.append((accepted, has_driver)) @@ -104,7 +99,7 @@ async def scenario() -> None: await asyncio.wait({task, published_waiter}, return_when=asyncio.FIRST_COMPLETED) if task.done(): await task - assert session._drive.driver == handle("driver") + assert session._drive.driver == "driver" assert session._drive.state == DriveState(steering=-0.3, throttle=0.0, brake=1.0, gear=gear) assert actuated == [(session._drive.accepted(), True)] finally: @@ -115,19 +110,19 @@ async def scenario() -> None: asyncio.run(scenario()) -def test_only_the_holders_accepted_commands_are_acknowledged_on_its_session() -> None: - """The ack and the pipeline echo are the holder's alone, so they ride its session.""" +def test_only_the_holders_accepted_commands_are_acknowledged_in_status() -> None: + """The ack and the pipeline echo are the holder's alone.""" async def scenario() -> None: stages = (TimestampEntry(label="received", ts=10.0), TimestampEntry(label="decoded", ts=14.0)) commands = asyncio.Queue() for command in ( - Command(kind="drive", driver_id="holder", session="s1", sequence=1, state=DriveState(), timestamps=stages), - # Another session of the same id is another driver, refused while the lease is held. - Command(kind="drive", driver_id="holder", session="s2", sequence=2, state=DriveState()), + Command(kind="drive", driver_id="holder", instance="s1", sequence=1, state=DriveState(), timestamps=stages), + # Another run of the same id is another driver, refused while the lease is held. + Command(kind="drive", driver_id="holder", instance="s2", sequence=2, state=DriveState()), # A bystander's e-stop latches, but is not the lease's to acknowledge. - Command(kind="estop", driver_id="bystander", session="s3", sequence=3), - Command(kind="estop", driver_id="holder", session="s1", sequence=4), + Command(kind="estop", driver_id="bystander", instance="s3", sequence=3), + Command(kind="estop", driver_id="holder", instance="s1", sequence=4), ): commands.put_nowait(command) @@ -137,9 +132,11 @@ async def scenario() -> None: session._drive = Drive() session._commands = commands session._deadline = None - session._drivers = Replies() + session._media_pipeline = {} + session._drivers = Revocations() session._actuate = lambda: None - session._publish = lambda: None + published: list[tuple[int | None, dict]] = [] + session._publish = lambda: published.append((session._drive.sequence, session._media_pipeline)) task = asyncio.create_task(session._loop()) try: @@ -148,9 +145,11 @@ async def scenario() -> None: break await asyncio.sleep(0) await asyncio.sleep(0) - assert session._drivers.sent == [ - ("s1", {"sequence": 1, "media_pipeline": [{"label": "received→decoded", "ms": 4}]}), - ("s1", {"sequence": 4, "media_pipeline": []}), + assert published == [ + (1, {"holder": [{"label": "received→decoded", "ms": 4}]}), + # The bystander's latch publishes, still echoing the holder's sequence. + (1, {"holder": [{"label": "received→decoded", "ms": 4}]}), + (4, {"holder": []}), ] finally: task.cancel() @@ -163,10 +162,10 @@ async def scenario() -> None: def test_only_unestopped_lease_holder_can_respawn_truck() -> None: async def scenario() -> None: commands = asyncio.Queue() - commands.put_nowait(Command(kind="respawn", driver_id="bystander", session="bystander", sequence=2)) - commands.put_nowait(Command(kind="respawn", driver_id="holder", session="holder", sequence=3)) - commands.put_nowait(Command(kind="reset_estop", driver_id="holder", session="holder", sequence=4)) - commands.put_nowait(Command(kind="respawn", driver_id="holder", session="holder", sequence=5)) + commands.put_nowait(Command(kind="respawn", driver_id="bystander", instance="bystander", sequence=2)) + commands.put_nowait(Command(kind="respawn", driver_id="holder", instance="holder", sequence=3)) + commands.put_nowait(Command(kind="reset_estop", driver_id="holder", instance="holder", sequence=4)) + commands.put_nowait(Command(kind="respawn", driver_id="holder", instance="holder", sequence=5)) respawns: list[str] = [] published: list[bool] = [] @@ -176,12 +175,12 @@ async def scenario() -> None: session._drive = Drive() session._stuck = StuckDetector() assert session._drive.apply( - Command(kind="drive", driver_id="holder", session="holder", sequence=1, state=DriveState()) + Command(kind="drive", driver_id="holder", instance="holder", sequence=1, state=DriveState()) ) - assert session._drive.apply(Command(kind="estop", driver_id="bystander", session="bystander", sequence=2)) + assert session._drive.apply(Command(kind="estop", driver_id="bystander", instance="bystander", sequence=2)) session._commands = commands session._deadline = None - session._drivers = Replies() + session._drivers = Revocations() session._actuate = lambda: None session._publish = lambda: published.append(True) @@ -228,7 +227,7 @@ async def watch_demand(on_demand) -> None: session._loop = wait_forever session._drivers = SimpleNamespace(run=wait_forever) - run = asyncio.create_task(session.run(SimpleNamespace())) + run = asyncio.create_task(session.run(SimpleNamespace(), "viewer/carla/truck1")) await watching.wait() run.cancel() await asyncio.gather(run, return_exceptions=True) @@ -258,7 +257,7 @@ async def scenario() -> None: session._drivers = SimpleNamespace(run=wait_forever) with pytest.raises(ExceptionGroup) as failure: - await session.run(SimpleNamespace()) + await session.run(SimpleNamespace(), "viewer/carla/truck1") assert len(failure.value.exceptions) == 1 assert str(failure.value.exceptions[0]) == "CARLA rejected settings" @@ -306,7 +305,7 @@ def telemetry_session(pose, *, autonomous: bool = True) -> runtime.TruckSession: ) session._drive = Drive() session._deadline = None - session._drivers = Replies() + session._drivers = Revocations() session._stuck = StuckDetector() return session @@ -461,23 +460,25 @@ async def next_command() -> Command: # Claim with the brake applied, then supply a pedal input. This also # exercises a genuine manual takeover with the takeover pedal hold. await command( - Command(kind="drive", driver_id="holder", session="holder", sequence=1, state=DriveState(gear=Gear.FORWARD)) + Command( + kind="drive", driver_id="holder", instance="holder", sequence=1, state=DriveState(gear=Gear.FORWARD) + ) ) await command( Command( kind="drive", driver_id="holder", - session="holder", + instance="holder", sequence=2, state=DriveState(throttle=0.5, brake=0.0, gear=Gear.FORWARD), ) ) assert not truck.autonomous assert controls[-1].throttle == 0.5 - await command(Command(kind="estop", driver_id="holder", session="holder", sequence=3)) + await command(Command(kind="estop", driver_id="holder", instance="holder", sequence=3)) assert controls[-1].brake == 1.0 and controls[-1].hand_brake if reset_before_handback: - await command(Command(kind="reset_estop", driver_id="holder", session="holder", sequence=4)) + await command(Command(kind="reset_estop", driver_id="holder", instance="holder", sequence=4)) if handback == "watchdog_expired": assert session._deadline is not None clock = session._deadline @@ -485,7 +486,7 @@ async def next_command() -> Command: assert expired.kind == "watchdog_expired" await command(expired) else: - await command(Command(kind="driver_left", driver_id="holder", session="holder")) + await command(Command(kind="driver_left", driver_id="holder", instance="holder")) assert session._drive.driver is None assert session._drive.estopped is not reset_before_handback assert truck.autonomous is reset_before_handback @@ -512,7 +513,7 @@ async def sleep(delay: float) -> None: assert published[-1] == (False, None, True) # Until then the latch stood, attributed to the driver who set it. if not reset_before_handback: - assert (True, handle("holder"), False) in published + assert (True, "holder", False) in published asyncio.run(scenario()) @@ -520,20 +521,20 @@ async def sleep(delay: float) -> None: def holding(drive: Drive) -> None: """A driver parked with the brake on.""" assert drive.apply( - Command(kind="drive", driver_id="holder", session="holder", sequence=1, state=DriveState(brake=1.0)) + Command(kind="drive", driver_id="holder", instance="holder", sequence=1, state=DriveState(brake=1.0)) ) def estopped_holding(drive: Drive) -> None: """A driver who e-stopped and still holds the lease.""" holding(drive) - assert drive.apply(Command(kind="estop", driver_id="holder", session="holder", sequence=2)) - assert drive.driver == handle("holder") and drive.estopped + assert drive.apply(Command(kind="estop", driver_id="holder", instance="holder", sequence=2)) + assert drive.driver == "holder" and drive.estopped def estopped_by_bystander(drive: Drive) -> None: """A bystander's e-stop outlives the lease it never held.""" - assert drive.apply(Command(kind="estop", driver_id="bystander", session="bystander", sequence=1)) + assert drive.apply(Command(kind="estop", driver_id="bystander", instance="bystander", sequence=1)) assert drive.driver is None and drive.estopped @@ -574,7 +575,7 @@ def respawn() -> None: assert len(respawns) == 1 assert runtime.STUCK_TIMEOUT <= respawns[0] < runtime.STUCK_TIMEOUT + 1.0 - assert session._drivers.revoked_sessions == [("holder", "holder")] + assert session._drivers.revoked_runs == [("holder", "holder")] assert session._drive.driver is None and not session._drive.estopped assert session._clock.held == set() @@ -588,15 +589,16 @@ async def scenario() -> None: session._truck = SimpleNamespace(name="truck1", actuate=lambda *_args, **_kwargs: None) session._drive = Drive() session._deadline = None - session._drivers = Replies() + session._media_pipeline = {} + session._drivers = Revocations() session._drivers.revoke("holder", "holder") session._publish = lambda: None session._commands = asyncio.Queue() session._commands.put_nowait( - Command(kind="drive", driver_id="holder", session="holder", sequence=9, state=DriveState(throttle=0.5)) + Command(kind="drive", driver_id="holder", instance="holder", sequence=9, state=DriveState(throttle=0.5)) ) - session._commands.put_nowait(Command(kind="estop", driver_id="holder", session="holder", sequence=10)) - session._commands.put_nowait(Command(kind="driver_left", driver_id="holder", session="holder")) + session._commands.put_nowait(Command(kind="estop", driver_id="holder", instance="holder", sequence=10)) + session._commands.put_nowait(Command(kind="driver_left", driver_id="holder", instance="holder")) task = asyncio.create_task(session._loop()) try: while not session._commands.empty(): @@ -607,9 +609,8 @@ async def scenario() -> None: with contextlib.suppress(asyncio.CancelledError): await task assert session._drive.driver is None and not session._drive.estopped - assert session._drivers.sent == [] - # Its departure was read last, which forgot the session. - assert session._drivers.revoked_sessions == [] + # Its departure was read last, which forgot the run. + assert session._drivers.revoked_runs == [] asyncio.run(scenario()) @@ -688,7 +689,7 @@ async def scenario() -> None: monkeypatch.setattr(asyncio.get_running_loop(), "time", lambda: 0.0) commands = asyncio.Queue() commands.put_nowait( - Command(kind="drive", driver_id="holder", session="holder", sequence=1, state=DriveState(throttle=0.5)) + Command(kind="drive", driver_id="holder", instance="holder", sequence=1, state=DriveState(throttle=0.5)) ) session = runtime.TruckSession.__new__(runtime.TruckSession) @@ -697,7 +698,7 @@ async def scenario() -> None: session._drive = Drive() session._commands = commands session._deadline = None - session._drivers = Replies() + session._drivers = Revocations() session._stuck = StuckDetector() session._actuate = lambda: None session._publish = lambda: None @@ -795,7 +796,7 @@ async def scenario() -> None: session._drive = Drive() session._commands = asyncio.Queue() session._deadline = None - session._drivers = Replies() + session._drivers = Revocations() session._actuate = lambda: None session._publish = lambda: published.append(True) @@ -809,14 +810,14 @@ async def settle(count: int) -> None: task = asyncio.create_task(session._loop()) try: session._commands.put_nowait( - Command(kind="drive", driver_id="holder", session="holder", sequence=1, state=DriveState(brake=1.0)) + Command(kind="drive", driver_id="holder", instance="holder", sequence=1, state=DriveState(brake=1.0)) ) await settle(1) assert session._clock.held == {"drive:truck1"} - session._commands.put_nowait(Command(kind="estop", driver_id="holder", session="holder", sequence=2)) + session._commands.put_nowait(Command(kind="estop", driver_id="holder", instance="holder", sequence=2)) await settle(2) - assert session._drive.driver == handle("holder") + assert session._drive.driver == "holder" assert session._clock.held == set() assert session._deadline == runtime.COMMAND_TIMEOUT @@ -825,9 +826,9 @@ async def settle(count: int) -> None: # marks the refused frame as processed. now = runtime.COMMAND_TIMEOUT / 2 session._commands.put_nowait( - Command(kind="drive", driver_id="holder", session="holder", sequence=3, state=DriveState(throttle=0.5)) + Command(kind="drive", driver_id="holder", instance="holder", sequence=3, state=DriveState(throttle=0.5)) ) - session._commands.put_nowait(Command(kind="estop", driver_id="bystander", session="bystander", sequence=1)) + session._commands.put_nowait(Command(kind="estop", driver_id="bystander", instance="bystander", sequence=1)) await settle(3) assert session._deadline == runtime.COMMAND_TIMEOUT assert session._clock.held == set() diff --git a/demo/carla/web/src/element.ts b/demo/carla/web/src/element.ts index 52f1b3348a0..79288b029e3 100644 --- a/demo/carla/web/src/element.ts +++ b/demo/carla/web/src/element.ts @@ -8,7 +8,6 @@ */ import * as Moq from "@moq/net"; -import { VERSIONS } from "./paths.ts"; import { mount, PREVIEW } from "./player.ts"; /** The attributes the element observes. */ @@ -31,22 +30,13 @@ export default class MoqCarla extends HTMLElement { /** The relay connection, open while the element is in a document. */ readonly connection: Moq.Connection; - // Owned rather than borrowed from the connection, since a driver's socket - // endpoint publishes into it. - readonly #origin = new Moq.Origin.Producer(); - // Set when the element is connected to the DOM. readonly #connected = new Moq.Signals.Signal(false); #dispose?: () => void; constructor() { super(); - this.connection = new Moq.Connection({ - enabled: PREVIEW ? false : this.#connected, - versions: VERSIONS, - publish: this.#origin.consume(), - consume: this.#origin, - }); + this.connection = new Moq.Connection({ enabled: PREVIEW ? false : this.#connected }); cleanup.register(this, this.connection); } @@ -56,7 +46,7 @@ export default class MoqCarla extends HTMLElement { // Render into a Shadow DOM, reusing the existing one on reconnect. const shadow = this.shadowRoot ?? this.attachShadow({ mode: "open" }); - this.#dispose = mount(shadow, this.connection, this.#origin); + this.#dispose = mount(shadow, this.connection); } /** Called by the browser when the element leaves its document. */ diff --git a/demo/carla/web/src/hint.test.ts b/demo/carla/web/src/hint.test.ts index 399e1fdd20a..454e8c5e391 100644 --- a/demo/carla/web/src/hint.test.ts +++ b/demo/carla/web/src/hint.test.ts @@ -6,7 +6,7 @@ const active: ControlHintState = { focused: true, estopped: false, status: "pending", - handle: "mine", + driverId: "mine", wheel: false, }; diff --git a/demo/carla/web/src/hint.ts b/demo/carla/web/src/hint.ts index 57e5f54bddb..5ef977b9430 100644 --- a/demo/carla/web/src/hint.ts +++ b/demo/carla/web/src/hint.ts @@ -4,8 +4,7 @@ export interface ControlHintState { estopped: boolean; estopBy?: string; status: "pending" | "available" | "unavailable"; - /** How `status` names this player's lease. */ - handle?: string; + driverId?: string; statusDriver?: string; wheel: boolean; } @@ -25,8 +24,8 @@ export function controlHint(state: ControlHintState): string { if (!state.active && state.status === "pending") return "Waiting for vehicle status"; if (!state.active) return "Take control to drive"; if (state.status === "unavailable") return "Vehicle status lost · Control unconfirmed"; - if (!state.handle || !state.statusDriver) return "Requesting control…"; - if (state.statusDriver !== state.handle) return `Driven by ${state.statusDriver}`; + if (!state.driverId || !state.statusDriver) return "Requesting control…"; + if (state.statusDriver !== state.driverId) return `Driven by ${state.statusDriver}`; if (state.wheel) { return "Wheel · Brake before shifting · Esc to release"; } diff --git a/demo/carla/web/src/paths.test.ts b/demo/carla/web/src/paths.test.ts index a65fe94a953..7d8c87c8b36 100644 --- a/demo/carla/web/src/paths.test.ts +++ b/demo/carla/web/src/paths.test.ts @@ -1,5 +1,5 @@ import { describe, expect, test } from "bun:test"; -import { driverId, handle, parseCamera } from "./paths.ts"; +import { parseCamera } from "./paths.ts"; describe("parseCamera", () => { test("accepts exactly carla//.hang", () => { @@ -13,22 +13,9 @@ describe("parseCamera", () => { "carla/truck1/front", "carla/truck1/rear/extra.hang", "other/truck1/front.hang", - "carla/.server/truck1", - "carla/.client/driver", + "viewer/carla/truck1/abc", ]) { expect(parseCamera(path)).toBeUndefined(); } }); }); - -describe("driver names", () => { - test("an endpoint id carries 128 random bits", () => { - expect(driverId()).toMatch(/^driver-[0-9a-f]{32}$/); - expect(driverId()).not.toBe(driverId()); - }); - - test("a session's handle is the one the worker publishes", async () => { - // Pinned by the worker's test_status_names_sessions_by_a_handle_the_player_derives_too. - expect(await handle("0123456789abcdef0123456789abcdef")).toBe("driver-3eb1bd43"); - }); -}); diff --git a/demo/carla/web/src/paths.ts b/demo/carla/web/src/paths.ts index 343788fc434..3e035a04c32 100644 --- a/demo/carla/web/src/paths.ts +++ b/demo/carla/web/src/paths.ts @@ -1,11 +1,9 @@ /** - * The paths and connection settings shared with the simulator. + * The broadcast paths shared with the simulator. * - * Trucks publish one media broadcast per camera at `carla/{truck}/{camera}.hang`, - * and accept drivers on a server socket endpoint named for the truck. A driver - * dials it from a client endpoint of its own, the only role the demo relay lets - * anonymous viewers publish. The truck's public `status` names a driver by its - * session's {@link handle} instead of its id. + * Trucks publish one media broadcast per camera at `carla/{truck}/{camera}.hang`. + * Drivers publish their `control` track at `viewer/carla/{truck}/{driver}`, a + * prefix the demo relay lets anonymous viewers publish under. * * @module */ @@ -29,30 +27,12 @@ export function parseCamera(path: string): Camera | undefined { return { truck, id: `${truck}/${file.slice(0, -".hang".length)}` }; } -/** The prefix every truck and driver socket endpoint sits under: `carla/.server/{truck}` and `carla/.client/{driver}`. */ -export const SOCKET = Moq.Path.from("carla"); - -/** - * The versions offered to the relay, most preferred first: the ones that carry socket - * endpoints, which are hidden broadcasts that moq-lite before 07 never carries. - */ -export const VERSIONS = ["moq-lite-07-wip", "moq-transport-22"] as const; - -function hex(bytes: Uint8Array): string { - return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join(""); +/** Where one driver of a truck publishes its control broadcast. */ +export function driverPath(truck: string, driver: string): Moq.Path.Valid { + return Moq.Path.from(`viewer/carla/${truck}/${driver}`); } -/** A fresh driver endpoint id: random, so no other viewer can claim it. */ +/** A fresh driver id, public since the truck's status names its driver. */ export function driverId(): string { - return `driver-${hex(crypto.getRandomValues(new Uint8Array(16)))}`; -} - -/** - * The name the truck's `status` gives the lease or e-stop a session carried, as the worker's `drive.handle` derives it. - * - * A hash of the session id, which only the session's two ends know. - */ -export async function handle(session: string): Promise { - const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(session)); - return `driver-${hex(new Uint8Array(digest, 0, 4))}`; + return crypto.randomUUID().replaceAll("-", "").slice(0, 12); } diff --git a/demo/carla/web/src/player.ts b/demo/carla/web/src/player.ts index 988da952457..0853f3b861b 100644 --- a/demo/carla/web/src/player.ts +++ b/demo/carla/web/src/player.ts @@ -4,8 +4,8 @@ * Connects to the relay, discovers camera broadcasts by announcement (like * moq-boy), renders each on a map with a video preview, and lets you take * control of one vehicle with the keyboard or a steering wheel. The - * {@link Truck} backend dials the vehicle's socket endpoint to drive it and - * subscribes to its `status` track. + * {@link Truck} backend publishes a `control` track and subscribes to the + * vehicle's `status` track for round-trip confirmation. * * @module */ @@ -53,13 +53,12 @@ import { export const PREVIEW = import.meta.env.DEV && new URLSearchParams(location.search).has("preview"); /** - * Render the player into `root` and run it on `connection`, which serves and - * fills `origin`. + * Render the player into `root` and run it on `connection`. * * Everything it starts, window listeners and the input loop included, stops * when the returned function is called. */ -export function mount(root: ShadowRoot, connection: Moq.Connection, origin: Moq.Origin.Producer): () => void { +export function mount(root: ShadowRoot, connection: Moq.Connection): () => void { root.innerHTML = `${template}`; preloadAerials(); @@ -127,14 +126,7 @@ export function mount(root: ShadowRoot, connection: Moq.Connection, origin: Moq. // A restarted simulator keeps its path present, and the watch broadcast // follows it, so only an absence removes the tile. if (entry.kind !== "absent" && !tiles.has(entry.prefix)) { - const truck = new Truck({ - broadcastId: entry.prefix, - connection, - origin, - driving, - focused, - hovered, - }); + const truck = new Truck({ broadcastId: entry.prefix, connection, driving, focused, hovered }); tiles.set(entry.prefix, new TruckTile(truck)); vehicles.set(tiles.size); refreshFocusLayout(); @@ -1011,11 +1003,15 @@ export function mount(root: ShadowRoot, connection: Moq.Connection, origin: Moq. // Status carries the measured takeover state. Never guess while it is unavailable. controlButton.disabled = !active && !status; const respawnButton = q(".respawn-btn") as HTMLButtonElement; - const handle = effect.get(this.truck.handle); + const driverId = effect.get(this.truck.driverId); const estopped = effect.get(this.truck.estopped) || Boolean(status?.estopped); respawnButton.hidden = !isFocused; respawnButton.disabled = - effect.get(this.truck.respawning) || !active || !handle || status?.driver !== handle || estopped; + effect.get(this.truck.respawning) || + !active || + !driverId || + status?.driver !== driverId || + estopped; // Latched if either side thinks so: the local signal reacts // instantly, the status echo is authoritative (see resetEstop). const stopButton = q(".estop-btn") as HTMLButtonElement; @@ -1045,7 +1041,7 @@ export function mount(root: ShadowRoot, connection: Moq.Connection, origin: Moq. estopped, estopBy: status?.estop_by, status: statusState, - handle, + driverId, statusDriver: status?.driver, wheel: effect.get(source) === "wheel", }); @@ -1096,7 +1092,8 @@ export function mount(root: ShadowRoot, connection: Moq.Connection, origin: Moq. ? "Manual" : "Unknown"; q(".mode").className = `mode${s?.estopped ? " estopped" : ""}`; - const mine = effect.get(this.truck.pipeline); + const me = effect.get(this.truck.driverId); + const mine = me ? s?.media_pipeline[me] : undefined; const roundTrip = effect.get(this.truck.roundTripMs); q(".rtt").textContent = roundTrip == null ? "Not measured" : `${Math.round(roundTrip)} ms`; q(".lat").textContent = mine?.length @@ -1235,7 +1232,7 @@ export function mount(root: ShadowRoot, connection: Moq.Connection, origin: Moq. const { populatePreview } = await import("./preview.ts"); if (abort.signal.aborted) return; const cleanup = populatePreview((path) => { - const truck = new Truck({ broadcastId: path, connection, origin, driving, focused, hovered }); + const truck = new Truck({ broadcastId: path, connection, driving, focused, hovered }); const tile = new TruckTile(truck); tiles.set(path, tile); vehicles.set(tiles.size); diff --git a/demo/carla/web/src/preview.ts b/demo/carla/web/src/preview.ts index c4ef756bd5b..d0e7b04ea95 100644 --- a/demo/carla/web/src/preview.ts +++ b/demo/carla/web/src/preview.ts @@ -30,6 +30,7 @@ export function populatePreview(add: (path: Moq.Path.Valid) => Truck) { estopped: false, map: "Town10HD_Opt", pose, + media_pipeline: {}, }, // The status track is scale-less, so the write names the group it opens. at: Moq.Time.Timestamp.unscaled(0n), diff --git a/demo/carla/web/src/schemas.test.ts b/demo/carla/web/src/schemas.test.ts index 8b7f0181037..8e446a590d0 100644 --- a/demo/carla/web/src/schemas.test.ts +++ b/demo/carla/web/src/schemas.test.ts @@ -4,6 +4,7 @@ import { TruckStatusSchema } from "./schemas.ts"; const status = { accepted: { steering: 0, throttle: 0, brake: 1, gear: "neutral" }, estopped: false, + media_pipeline: {}, }; describe("TruckStatusSchema", () => { diff --git a/demo/carla/web/src/schemas.ts b/demo/carla/web/src/schemas.ts index f65eabf0c4c..408f481f8c6 100644 --- a/demo/carla/web/src/schemas.ts +++ b/demo/carla/web/src/schemas.ts @@ -66,14 +66,6 @@ const LatencyEntrySchema = z.object({ label: z.string(), ms: z.number(), }); -export type LatencyEntry = z.infer; - -/** The truck's answer on a driver's session to each command it accepted from the lease holder. */ -export const ReplySchema = z.object({ - sequence: z.number(), - media_pipeline: z.array(LatencyEntrySchema), -}); -export type Reply = z.infer; /** Per-update telemetry the truck publishes on the `status` track. */ export const TruckStatusSchema = z.object({ @@ -84,6 +76,8 @@ export const TruckStatusSchema = z.object({ estopped: z.boolean(), estop_by: z.optional(z.string()), driver: z.optional(z.string()), + sequence: z.optional(z.number()), + media_pipeline: z.record(z.string(), z.array(LatencyEntrySchema)), location: z.optional(z.string()), // Absent until the simulator's first physics tick reports where the vehicle is. map: z.optional(z.string()), diff --git a/demo/carla/web/src/truck.test.ts b/demo/carla/web/src/truck.test.ts index c4efc83daf1..d686e6b1df0 100644 --- a/demo/carla/web/src/truck.test.ts +++ b/demo/carla/web/src/truck.test.ts @@ -1,9 +1,10 @@ import { describe, expect, test } from "bun:test"; -import { yieldNow } from "@moq/clock"; +import { Duration, yieldNow } from "@moq/clock"; +import { Manual } from "@moq/clock/test"; import * as Json from "@moq/json"; import * as Moq from "@moq/net"; import { applyKeys, REST } from "./input.ts"; -import { handle, SOCKET } from "./paths.ts"; +import { driverPath } from "./paths.ts"; import type { TruckStatus } from "./schemas.ts"; import { shiftGear, Truck } from "./truck.ts"; @@ -14,7 +15,6 @@ const key = () => Moq.Time.Timestamp.unscaled(keys++); describe("Truck video demand", () => { test("only hovered or focused cameras subscribe, and focus suppresses other previews", async () => { const connection = new Moq.Connection({ enabled: false }); - const origin = new Moq.Origin.Producer(); const driving = new Moq.Signals.Signal(undefined); const focused = new Moq.Signals.Signal(undefined); const hovered = new Moq.Signals.Signal(undefined); @@ -23,7 +23,6 @@ describe("Truck video demand", () => { new Truck({ broadcastId: Moq.Path.from(id), connection, - origin, driving, focused, hovered, @@ -53,7 +52,6 @@ describe("Truck video demand", () => { } finally { for (const truck of trucks) truck.close(); connection.close(); - origin.close(); } }); }); @@ -64,6 +62,7 @@ describe("Truck status", () => { accepted: { steering: 0.25, throttle, brake: 0, gear: "forward" }, autonomy: "manual", estopped: false, + media_pipeline: {}, }; } @@ -76,13 +75,11 @@ describe("Truck status", () => { test("clears valid telemetry when the status track ends", async () => { const connection = new Moq.Connection({ enabled: false }); - const origin = new Moq.Origin.Producer(); const driving = new Moq.Signals.Signal(undefined); const focused = new Moq.Signals.Signal(undefined); const truck = new Truck({ broadcastId: Moq.Path.from("carla/truck1/front.hang"), connection, - origin, driving, focused, }); @@ -106,19 +103,16 @@ describe("Truck status", () => { broadcast.close(); truck.close(); connection.close(); - origin.close(); } }); test("a status from before the truck saw an e-stop does not unlatch it", async () => { const connection = new Moq.Connection({ enabled: false }); - const origin = new Moq.Origin.Producer(); const driving = new Moq.Signals.Signal(undefined); const focused = new Moq.Signals.Signal(undefined); const truck = new Truck({ broadcastId: Moq.Path.from("carla/truck1/front.hang"), connection, - origin, driving, focused, }); @@ -147,19 +141,16 @@ describe("Truck status", () => { broadcast.close(); truck.close(); connection.close(); - origin.close(); } }); test("marks status unavailable when an active broadcast disappears", async () => { const connection = new Moq.Connection({ enabled: false }); - const origin = new Moq.Origin.Producer(); const driving = new Moq.Signals.Signal(undefined); const focused = new Moq.Signals.Signal(undefined); const truck = new Truck({ broadcastId: Moq.Path.from("carla/truck1/front.hang"), connection, - origin, driving, focused, }); @@ -180,19 +171,16 @@ describe("Truck status", () => { broadcast.close(); truck.close(); connection.close(); - origin.close(); } }); test("a canceled status consumer cannot overwrite its replacement", async () => { const connection = new Moq.Connection({ enabled: false }); - const origin = new Moq.Origin.Producer(); const driving = new Moq.Signals.Signal(undefined); const focused = new Moq.Signals.Signal(undefined); const truck = new Truck({ broadcastId: Moq.Path.from("carla/truck1/front.hang"), connection, - origin, driving, focused, }); @@ -226,7 +214,6 @@ describe("Truck status", () => { second.broadcast.close(); truck.close(); connection.close(); - origin.close(); } }); }); @@ -234,17 +221,15 @@ describe("Truck status", () => { describe("Truck.setDrive", () => { function truck() { const connection = new Moq.Connection({ enabled: false }); - const origin = new Moq.Origin.Producer(); const driving = new Moq.Signals.Signal(undefined); const focused = new Moq.Signals.Signal(undefined); const t = new Truck({ broadcastId: Moq.Path.from("carla/truck1/front.hang"), connection, - origin, driving, focused, }); - return { t, connection, origin }; + return { t, connection }; } // Guards setDrive's contract: local state advances however small the step, @@ -252,7 +237,7 @@ describe("Truck.setDrive", () => { // in input.test.ts's stepDrive suite; this one calls setDrive directly and so // cannot catch a gate added at the call site. test("accumulates sub-epsilon frames instead of discarding them", () => { - const { t, connection, origin } = truck(); + const { t, connection } = truck(); try { const dt = 1 / 120; for (let i = 0; i < 120; i++) { @@ -264,31 +249,27 @@ describe("Truck.setDrive", () => { } finally { t.close(); connection.close(); - origin.close(); } }); test("a single sub-epsilon frame still moves the local state", () => { - const { t, connection, origin } = truck(); + const { t, connection } = truck(); try { t.setDrive(applyKeys(t.drive.peek(), new Set(["d"]), 1 / 60)); expect(t.drive.peek().steering).toBeGreaterThan(0); } finally { t.close(); connection.close(); - origin.close(); } }); test("claims with measured steering and gear but zero throttle and full brake", async () => { const connection = new Moq.Connection({ enabled: false }); - const origin = new Moq.Origin.Producer(); const driving = new Moq.Signals.Signal(undefined); const focused = new Moq.Signals.Signal("truck1/front"); const t = new Truck({ broadcastId: Moq.Path.from("carla/truck1/front.hang"), connection, - origin, driving, focused, }); @@ -298,6 +279,7 @@ describe("Truck.setDrive", () => { accepted: { ...REST }, autonomy: "autonomous", estopped: false, + media_pipeline: {}, measured, }); expect(t.beginTakeover()).toBe(true); @@ -308,19 +290,17 @@ describe("Truck.setDrive", () => { } finally { t.close(); connection.close(); - origin.close(); } }); test("refuses a takeover before the truck reports status", () => { - const { t, connection, origin } = truck(); + const { t, connection } = truck(); try { expect(t.beginTakeover()).toBe(false); expect(t.drive.peek()).toEqual(REST); } finally { t.close(); connection.close(); - origin.close(); } }); }); @@ -328,13 +308,11 @@ describe("Truck.setDrive", () => { describe("Truck safety controls", () => { test("a viewer e-stop does not claim the driving session", () => { const connection = new Moq.Connection({ enabled: false }); - const origin = new Moq.Origin.Producer(); const driving = new Moq.Signals.Signal(undefined); const focused = new Moq.Signals.Signal(undefined); const truck = new Truck({ broadcastId: Moq.Path.from("carla/truck1/front.hang"), connection, - origin, driving, focused, }); @@ -347,19 +325,16 @@ describe("Truck safety controls", () => { } finally { truck.close(); connection.close(); - origin.close(); } }); test("a focused viewer resets only after the truck confirms the latch", () => { const connection = new Moq.Connection({ enabled: false }); - const origin = new Moq.Origin.Producer(); const driving = new Moq.Signals.Signal(undefined); const focused = new Moq.Signals.Signal(undefined); const truck = new Truck({ broadcastId: Moq.Path.from("carla/truck1/front.hang"), connection, - origin, driving, focused, }); @@ -373,6 +348,7 @@ describe("Truck safety controls", () => { accepted: { ...REST }, autonomy: "unknown", estopped: true, + media_pipeline: {}, estop_by: "bystander", }); truck.resetEstop(); @@ -381,19 +357,16 @@ describe("Truck safety controls", () => { } finally { truck.close(); connection.close(); - origin.close(); } }); test("the tile safety action exits a confirmed e-stop without claiming control", () => { const connection = new Moq.Connection({ enabled: false }); - const origin = new Moq.Origin.Producer(); const driving = new Moq.Signals.Signal(undefined); const focused = new Moq.Signals.Signal(undefined); const truck = new Truck({ broadcastId: Moq.Path.from("carla/truck1/front.hang"), connection, - origin, driving, focused, }); @@ -405,6 +378,7 @@ describe("Truck safety controls", () => { accepted: { ...REST }, autonomy: "manual", estopped: true, + media_pipeline: {}, }); truck.toggleEmergencyStop(); @@ -414,182 +388,184 @@ describe("Truck safety controls", () => { } finally { truck.close(); connection.close(); - origin.close(); } }); }); -describe("Truck sessions", () => { +describe("Truck control", () => { const decoder = new TextDecoder(); - const encoder = new TextEncoder(); + const HEARTBEAT = Duration.millis(250); - /** A truck whose socket endpoint accepts on the same origin as the player. */ + /** A truck on a connection lending `origin`, where the test plays the simulator and the clock. */ function setup() { - const connection = new Moq.Connection({ enabled: false }); + const clock = new Manual(); + const installed = clock.install(); const origin = new Moq.Origin.Producer(); + // The truck reads only the origin, which a real connection lends while it has a relay. + const connection = { + origin: new Moq.Signals.Signal(origin), + } as unknown as Moq.Connection; const driving = new Moq.Signals.Signal(undefined); const focused = new Moq.Signals.Signal(undefined); + const camera = origin.route(Moq.Path.from("carla/truck1/front.hang")); + const producer = new Json.Snapshot.Producer({ + track: camera.broadcast.createTrack("status", { compression: "deflate" }), + }); const truck = new Truck({ broadcastId: Moq.Path.from("carla/truck1/front.hang"), connection, - origin, driving, focused, }); - const endpoint = new Moq.Socket.Endpoint(origin, SOCKET, "truck1"); + + /** Publish a status, and wait until the truck has read it. */ + const echo = async (value: Partial) => { + const status: TruckStatus = { + accepted: { ...REST }, + autonomy: "manual", + estopped: false, + media_pipeline: {}, + ...value, + }; + producer.update({ value: status, at: key() }); + while (!Bun.deepEquals(truck.status.peek(), status)) await truck.status.changed(); + }; const close = () => { truck.close(); - endpoint.close(); - connection.close(); + producer.finish(); + camera.close(); origin.close(); + installed.close(); }; - return { truck, driving, endpoint, close }; + return { truck, driving, origin, clock, echo, close }; } - async function read(stream: Moq.Socket.TrackStream, reader = stream.consumer.ordered()) { - const frame = await reader.readFrame(); - return frame && JSON.parse(decoder.decode(frame.payload)); + /** Read the `control` track at `path` oldest first, a heartbeat late like the truck. */ + async function control(origin: Moq.Origin.Producer, path: Moq.Path.Valid) { + const request = origin.request(path); + let broadcast = request.active.peek(); + while (!broadcast) broadcast = await request.active.changed(); + const track = broadcast.track("control"); + const reader = track.subscribe({ order: "ascending", delay: { max: HEARTBEAT } }).ordered(); + const read = async () => { + const frame = await reader.readFrame(); + return frame && JSON.parse(decoder.decode(frame.payload)); + }; + return { request, track, read }; } - test("a driver's commands and the truck's acknowledgements travel on its own session", async () => { - const { truck, driving, endpoint, close } = setup(); + /** Wait for the driver id the truck publishes its lease under. */ + async function drive(truck: Truck, driving: Moq.Signals.Signal) { + driving.set(truck.id); + let id = truck.driverId.peek(); + while (!id) id = await truck.driverId.changed(); + return id; + } + + test("a driver publishes each command as a group of its own, which the status acknowledges", async () => { + const { truck, driving, origin, echo, close } = setup(); try { - const incoming = endpoint.acceptTrack(); - driving.set(truck.id); - const dial = await incoming; - expect(dial.peer).toMatch(/^driver-[0-9a-f]{32}$/); - const stream = await dial.accept(); - // Status names the lease by the session, never by the endpoint id. - while (truck.handle.peek() === undefined) await truck.handle.changed(); - expect(truck.handle.peek()).toBe(await handle(stream.session)); + const id = await drive(truck, driving); + const { track, read } = await control(origin, driverPath("truck1", id)); + // A superseded command stays fresh for the truck's delay budget. + expect((await track.info()).maxAge?.asMillis()).toBe(250); // Taking control heartbeats at once, so the truck hears the claim. - const command = await read(stream); + const command = await read(); expect(command.type).toBe("drive"); const measured = truck.roundTripMs.changed(); - const pipeline = [{ label: "received→decoded", ms: 4 }]; - const reply = { sequence: command.sequence, media_pipeline: pipeline }; - const group = stream.producer.appendGroup(); - group.writeFrame({ - payload: encoder.encode(JSON.stringify(reply)), - timestamp: Moq.Time.Timestamp.unscaled(group.sequence), - }); - group.finish(); + await echo({ driver: id, sequence: command.sequence }); expect(await measured).toBeGreaterThanOrEqual(0); - expect(truck.pipeline.peek()).toEqual(pipeline); } finally { close(); } }); - test("the truck ending the session ends the lease", async () => { - const { truck, driving, endpoint, close } = setup(); + test("a reset right before a drive reaches a heartbeat-late reader whole", async () => { + const { truck, driving, origin, echo, close } = setup(); try { - const incoming = endpoint.acceptTrack(); - driving.set(truck.id); - const stream = await (await incoming).accept(); - await read(stream); + const id = await drive(truck, driving); + const { read } = await control(origin, driverPath("truck1", id)); + expect((await read()).type).toBe("drive"); + await echo({ driver: id, estopped: true }); - const released = driving.changed(); - stream.producer.finish(); - expect(await released).toBeUndefined(); - // The lease's endpoint goes with it. - while (truck.handle.peek() !== undefined) await truck.handle.changed(); + // The clock is still, so no heartbeat lands between the two. + truck.resetEstop(); + truck.setDrive({ throttle: 0.5 }); + expect((await read()).type).toBe("reset_estop"); + expect((await read()).type).toBe("drive"); } finally { close(); } }); - test("releasing control ends the session cleanly", async () => { - const { truck, driving, endpoint, close } = setup(); + test("a claim the status has not confirmed yet is kept", async () => { + const { truck, driving, echo, close } = setup(); try { - const incoming = endpoint.acceptTrack(); - driving.set(truck.id); - const stream = await (await incoming).accept(); - const reader = stream.consumer.ordered(); - await read(stream, reader); + await drive(truck, driving); + await echo({ driver: "someone-else" }); + await echo({}); + expect(driving.peek()).toBe(truck.id); + } finally { + close(); + } + }); + + test("the truck no longer naming its driver ends the lease and its broadcast", async () => { + const { truck, driving, origin, echo, close } = setup(); + try { + const id = await drive(truck, driving); + const { request } = await control(origin, driverPath("truck1", id)); + await echo({ driver: id }); - driving.set(undefined); - // Anything the heartbeat sent first, then the clean end. - while ((await read(stream, reader)) !== undefined); + // A stuck truck's respawn or the watchdog let go of it. + const released = driving.changed(); + await echo({}); + expect(await released).toBeUndefined(); + while (truck.driverId.peek() !== undefined) await truck.driverId.changed(); + // The request settles once nothing serves the path any more. + await request.closed; } finally { close(); } }); - test("a bystander's e-stop dials a session of its own without taking the lease", async () => { - const { truck, driving, endpoint, close } = setup(); + test("a bystander's e-stop publishes a broadcast of its own until the status echoes it", async () => { + const { truck, driving, origin, echo, close } = setup(); + const announced = origin.announced(Moq.Path.Pattern.parse("viewer/carla/truck1/*")); try { - const incoming = endpoint.acceptTrack(); truck.emergencyStop(); - const dial = await incoming; - expect(dial.peer).toMatch(/^driver-/); - const stream = await dial.accept(); - const reader = stream.consumer.ordered(); - expect((await read(stream, reader)).type).toBe("estop"); + const entry = await announced.next(); + if (!entry) throw new Error("no safety broadcast"); + expect(entry.prefix).toMatch(/^viewer\/carla\/truck1\/safety-[0-9a-f]{12}$/); + const { request, read } = await control(origin, entry.prefix); + expect((await read()).type).toBe("estop"); expect(driving.peek()).toBeUndefined(); - expect(truck.handle.peek()).toBeUndefined(); + expect(truck.driverId.peek()).toBeUndefined(); - // The truck's status echoing the latch is what lets the session go. - const broadcast = new Moq.Broadcast.Producer(); - const producer = new Json.Snapshot.Producer({ - track: broadcast.createTrack("status", { compression: "deflate" }), - }); - try { - (truck.broadcast.out.active as Moq.Signals.Signal).set( - broadcast.consume(), - ); - producer.update({ - value: { - accepted: { ...REST }, - autonomy: "manual", - estopped: true, - estop_by: await handle(dial.session), - }, - at: key(), - }); - expect(await read(stream, reader)).toBeUndefined(); - } finally { - producer.finish(); - broadcast.close(); - } + // The truck's status echoing the latch is what lets the broadcast go. + await echo({ estopped: true, estop_by: entry.prefix.split("/").at(-1) }); + // The request settles once nothing serves the path any more. + await request.closed; + expect(truck.estopped.peek()).toBe(true); } finally { + announced.close(); close(); } }); -}); -describe("Truck safety sessions", () => { - test("an e-stop whose session ends unconfirmed stops showing as latched", async () => { - const connection = new Moq.Connection({ enabled: false }); - const origin = new Moq.Origin.Producer(); - const driving = new Moq.Signals.Signal(undefined); - const focused = new Moq.Signals.Signal(undefined); - const truck = new Truck({ - broadcastId: Moq.Path.from("carla/truck1/front.hang"), - connection, - origin, - driving, - focused, - }); - const endpoint = new Moq.Socket.Endpoint(origin, SOCKET, "truck1"); + test("an e-stop the status never echoes stops showing as latched", async () => { + const { truck, clock, close } = setup(); try { - const incoming = endpoint.acceptTrack(); truck.emergencyStop(); expect(truck.estopped.peek()).toBe(true); - const stream = await (await incoming).accept(); - await stream.consumer.ordered().readFrame(); - - // The truck goes away before its status ever echoed the latch. - const cleared = truck.estopped.changed(); - stream.producer.finish(); - expect(await cleared).toBe(false); + await clock.advance(Duration.millis(1_999)); + expect(truck.estopped.peek()).toBe(true); + await clock.advance(Duration.millis(1)); + expect(truck.estopped.peek()).toBe(false); } finally { - truck.close(); - endpoint.close(); - connection.close(); - origin.close(); + close(); } }); }); @@ -597,13 +573,11 @@ describe("Truck safety sessions", () => { describe("Truck respawn", () => { test("requires the truck's current lease and clears old input", async () => { const connection = new Moq.Connection({ enabled: false }); - const origin = new Moq.Origin.Producer(); const driving = new Moq.Signals.Signal(undefined); const focused = new Moq.Signals.Signal("truck1/front"); const truck = new Truck({ broadcastId: Moq.Path.from("carla/truck1/front.hang"), connection, - origin, driving, focused, }); @@ -613,11 +587,12 @@ describe("Truck respawn", () => { driving.set(truck.id); expect(await active).toBe(true); - truck.handle.set("mine"); + truck.driverId.set("mine"); const status = { accepted: { ...REST }, autonomy: "manual", estopped: false, + media_pipeline: {}, driver: "someone-else", } satisfies TruckStatus; truck.status.set(status); @@ -641,7 +616,6 @@ describe("Truck respawn", () => { } finally { truck.close(); connection.close(); - origin.close(); } }); }); diff --git a/demo/carla/web/src/truck.ts b/demo/carla/web/src/truck.ts index e485241166b..2b125e82336 100644 --- a/demo/carla/web/src/truck.ts +++ b/demo/carla/web/src/truck.ts @@ -1,11 +1,10 @@ /** * The non-UI truck steering session backend. * - * A video pipeline pulled from `@moq/watch`, the truck's `status` track that - * every viewer reads, and, while driving, a socket session to the truck that - * carries this driver's commands one way and the truck's acknowledgements the - * other. The UI layer reads signals from this class to render video, the - * control panel, and telemetry. + * A video pipeline pulled from `@moq/watch`, a `control` track this driver + * publishes, and the truck's `status` track that every viewer reads, which + * echoes the lease and acknowledges the holder's commands. The UI layer reads + * signals from this class to render video, the control panel, and telemetry. * * @module */ @@ -14,9 +13,9 @@ import * as Json from "@moq/json"; import * as Moq from "@moq/net"; import * as Watch from "@moq/watch"; import { changed, REST, takeoverState } from "./input.ts"; -import { driverId, handle, parseCamera, SOCKET } from "./paths.ts"; -import type { DriveState, Gear, LatencyEntry, Reply, TruckStatus } from "./schemas.ts"; -import { ReplySchema, TruckStatusSchema } from "./schemas.ts"; +import { driverId, driverPath, parseCamera } from "./paths.ts"; +import type { DriveState, Gear, TruckStatus } from "./schemas.ts"; +import { TruckStatusSchema } from "./schemas.ts"; /** Configuration for creating a Truck session. */ export interface TruckConfig { @@ -24,8 +23,6 @@ export interface TruckConfig { broadcastId: Moq.Path.Valid; /** MoQ connection to the relay. */ connection: Moq.Connection; - /** The origin the connection serves and fills, which driver endpoints publish into. */ - origin: Moq.Origin.Producer; /** Shared signal tracking which truck is currently being driven. */ driving: Moq.Signals.Signal; /** Shared signal tracking which truck is currently focused. */ @@ -41,21 +38,41 @@ interface Command { timestamps: { label: string; ts: number }[]; } -/** A driver session: pending until the truck accepts, `null` once the dial or the session failed. */ -type Session = Moq.Signals.Signal; - const CONTROL_HEARTBEAT = Duration.millis(250); const MAX_PENDING_ACKS = 64; +// How long a bystander's e-stop or reset waits for the status to echo it. The +// truck finds the safety broadcast by announcement, then subscribes, then +// answers on `status`: a few round trips, well inside this on any real path. +const SAFETY_DEADLINE = Duration.millis(2_000); const encoder = new TextEncoder(); -const decoder = new TextDecoder(); -/** Send one command as its own message, so a late one never holds up the next. */ -function write(stream: Moq.Socket.TrackStream, command: Command) { - // A session that already ended has nothing to send to; its reply reader reports the end. - if (stream.producer.closed.peek() !== undefined) return; +/** + * Publish a `control` broadcast at `path` in `origin`, closed with `effect`. + * + * Each command is a group of its own, so a late one never holds up the next. + * A superseded command stays fresh for one heartbeat, which the truck's delay + * budget matches: a burst, such as a reset right before a drive, arrives whole. + */ +function publishControl( + effect: Moq.Signals.Effect, + origin: Moq.Origin.Table, + path: Moq.Path.Valid, +): Moq.Track.Producer { + const route = origin.route(path, { hidden: true }); + effect.cleanup(() => route.close()); + const track = route.broadcast.createTrack("control", { maxAge: CONTROL_HEARTBEAT }); + // Announced once the track exists, under a fresh instance: the truck binds its + // single driver to this path and instance. + route.announce(); + return track; +} + +/** Send one command as a group of its own. */ +function write(track: Moq.Track.Producer, command: Command) { + if (track.closed.peek() !== undefined) return; const message = { ...command.cmd, sequence: command.sequence, timestamps: command.timestamps }; - // The socket track is scale-less, so the frame carries its group's key. - const group = stream.producer.appendGroup(); + // The track is scale-less, so the frame carries its group's key. + const group = track.appendGroup(); group.writeFrame({ payload: encoder.encode(JSON.stringify(message)), timestamp: Moq.Time.Timestamp.unscaled(group.sequence), @@ -79,13 +96,11 @@ export class Truck { readonly active = new Moq.Signals.Signal(false); readonly status = new Moq.Signals.Signal(undefined); readonly statusState = new Moq.Signals.Signal<"pending" | "available" | "unavailable">("pending"); - /** How `status` names this player's lease, once the truck accepted the driving session. */ - readonly handle = new Moq.Signals.Signal(undefined); + /** The id this player drives under, which `status` names while it holds the lease. */ + readonly driverId = new Moq.Signals.Signal(undefined); readonly estopped = new Moq.Signals.Signal(false); readonly respawning = new Moq.Signals.Signal(false); readonly roundTripMs = new Moq.Signals.Signal(undefined); - /** This driver's media pipeline stages, as the truck last measured them. */ - readonly pipeline = new Moq.Signals.Signal(undefined); // The driver's locally-requested drive state (what we are sending). readonly drive = new Moq.Signals.Signal({ ...REST }); @@ -98,34 +113,34 @@ export class Truck { readonly videoRenderer: Watch.Video.Renderer; // Internal control publishing state. - #command = new Moq.Signals.Signal(undefined); + /** The control track while this player drives, which each command is written to at once. */ + #control?: Moq.Track.Producer; #safetyCommand = new Moq.Signals.Signal(undefined); #sequence = 0; #sentAt = new Map(); #sawStatusSource = false; /** This player's e-stop, not yet echoed in a status. */ #estopUnconfirmed = false; + /** Whether `status` has named this lease's driver as the holder. */ + #held = false; // The canvas is created by the UI layer after construction, so the renderer // takes it as a signal input rather than a property set afterwards. readonly #canvas = new Moq.Signals.Signal(undefined); - readonly #origin: Moq.Origin.Producer; - constructor(config: TruckConfig) { - const { broadcastId, connection, origin, driving, focused, hovered } = config; + const { broadcastId, connection, driving, focused, hovered } = config; const camera = parseCamera(broadcastId); if (!camera) throw new Error(`not a camera broadcast: ${broadcastId}`); this.broadcastId = broadcastId; this.id = camera.id; this.name = camera.truck; this.#driving = driving; - this.#origin = origin; // Active when this truck is the one being driven. this.#signals.run(this.#runActive.bind(this)); this.#signals.run(this.#runControlLease.bind(this)); - this.#signals.run(this.#runRespawn.bind(this)); + this.#signals.run(this.#runLease.bind(this)); this.#signals.interval(() => this.#heartbeat(), CONTROL_HEARTBEAT); // Video pipeline. @@ -175,9 +190,9 @@ export class Truck { // Subscribe to the truck's status (telemetry) track. this.#signals.run(this.#runStatus.bind(this)); - // Dial the truck while driving it, or to deliver a bystander's e-stop. - this.#signals.run(this.#runControl.bind(this)); - this.#signals.run(this.#runSafetyControl.bind(this)); + // Publish control while driving this truck, or to deliver a bystander's e-stop. + this.#signals.run(this.#runControl.bind(this, connection)); + this.#signals.run(this.#runSafetyControl.bind(this, connection)); } /** Attach the canvas the UI created so the renderer can draw into it. */ @@ -216,8 +231,8 @@ export class Truck { /** * Latch e-stop on this truck even without holding the driving lease. The - * holder sends it on its driving session; a viewer dials a short safety - * session of its own, so it neither takes the lease nor waits for one. + * holder sends it on its control track; a viewer publishes a short-lived + * safety broadcast of its own, so it neither takes the lease nor waits for one. */ emergencyStop() { this.respawning.set(false); @@ -266,8 +281,8 @@ export class Truck { this.estopped.peek() || !status || status.estopped || - !this.handle.peek() || - status.driver !== this.handle.peek() + !this.driverId.peek() || + status.driver !== this.driverId.peek() ) { return false; } @@ -292,7 +307,9 @@ export class Truck { if (oldest == null) break; this.#sentAt.delete(oldest); } - this.#command.set(command, true); + // Written now rather than through a signal, which would coalesce a burst + // such as a reset right before a drive into its last command. + if (this.#control) write(this.#control, command); } #sendSafety(cmd: Record) { @@ -339,30 +356,39 @@ export class Truck { #runControlLease(effect: Moq.Signals.Effect) { if (!effect.get(this.active)) { - // Releasing control closes the driving session, which may not have - // delivered this player's e-stop yet: carry it on a safety session, + // Releasing control closes the driving broadcast, which may not have + // delivered this player's e-stop yet: carry it on a safety broadcast, // which stays up until a status echoes the latch. if (this.#estopUnconfirmed) this.#sendSafety({ type: "estop" }); this.respawning.set(false); this.drive.set({ ...REST }); - this.#command.set(undefined); - return; } - // Inactive already reset the state. Resetting again on activation races // the input that claimed control: held keys recover on the next frame, but - // an edge-triggered gear press would be silently lost. - this.#heartbeat(); + // an edge-triggered gear press would be silently lost. The claim goes out + // once the control track exists. } - #runRespawn(effect: Moq.Signals.Effect) { - if (!effect.get(this.respawning)) return; + /** + * Let go once the truck stops naming this player as its driver. + * + * Before `status` first names it, the claim is still on its way. After, the + * lease ended on the truck's side: a respawn this player asked for, the + * watchdog, or a stuck truck's respawn revoking it. Holding on would + * heartbeat a claim on whatever the truck does next. + */ + #runLease(effect: Moq.Signals.Effect) { + const id = effect.get(this.driverId); const status = effect.get(this.status); - // No handle means the driving session already ended, and the lease with it. - const handle = effect.get(this.handle); - if (!status || (handle !== undefined && status.driver === handle)) return; + if (!id || !status) return; + if (status.driver === id) { + this.#held = true; + return; + } + // A respawn only the holder may ask for, so its own request counts as held. + if (!this.#held && !effect.get(this.respawning)) return; this.respawning.set(false); - if (this.#driving.peek() === this.id) this.#driving.set(undefined); + this.#release(); } #runStatus(effect: Moq.Signals.Effect) { @@ -408,63 +434,50 @@ export class Truck { if (status.estopped) this.#estopUnconfirmed = false; if (!this.#estopUnconfirmed) this.estopped.set(status.estopped); this.#acknowledgeSafety(status); + this.#acknowledge(status); this.status.set(status); this.statusState.set("available"); } }); } - #runControl(effect: Moq.Signals.Effect) { - if (!effect.get(this.active)) return; + #runControl(connection: Moq.Connection, effect: Moq.Signals.Effect) { + const origin = effect.get(connection.origin); + if (!origin || !effect.get(this.active)) return; - // A fresh endpoint and session per lease: the truck binds its single - // driver to the session, so a new lease is a new claimant. + // A fresh id and broadcast per lease: the truck binds its single driver to + // the run, so a new lease is a new claimant. + const id = driverId(); + this.#held = false; + this.driverId.set(id); effect.cleanup(() => { - this.handle.set(undefined); + this.driverId.set(undefined); this.roundTripMs.set(undefined); - this.pipeline.set(undefined); this.#sentAt.clear(); }); - const session = this.#dial(effect, driverId()); - effect.run((nested) => { - const stream = nested.get(session); - // The truck refused the dial or ended the session, which ended the lease. - if (stream === null) return this.#release(); - if (!stream) return; - nested.spawn(async () => { - const name = await handle(stream.session); - if (!nested.abort.aborted) this.handle.set(name); - }); - nested.spawn(async () => { - if (await this.#readReplies(nested, stream)) this.#release(); - }); - nested.run((inner) => { - const command = inner.get(this.#command); - if (command) write(stream, command); - }); + this.#control = publishControl(effect, origin, driverPath(this.name, id)); + effect.cleanup(() => { + this.#control = undefined; }); + // The claim, which the activation's own heartbeat sent before the track existed. + this.#heartbeat(); } - #runSafetyControl(effect: Moq.Signals.Effect) { + #runSafetyControl(connection: Moq.Connection, effect: Moq.Signals.Effect) { const command = effect.get(this.#safetyCommand); if (!command) return; + // A publish never fails where the player can see it, so the truck's silence is the failure. + effect.timer(() => this.#undelivered(command), SAFETY_DEADLINE); + const origin = effect.get(connection.origin); + if (!origin) return; - const session = this.#dial(effect, driverId()); - effect.run((nested) => { - const stream = nested.get(session); - if (stream === null) return this.#undelivered(command); - if (!stream) return; - write(stream, command); - // The truck answers a bystander with nothing but the session's end. - nested.spawn(async () => { - if (await this.#readReplies(nested, stream)) this.#undelivered(command); - }); - }); + const track = publishControl(effect, origin, driverPath(this.name, `safety-${driverId()}`)); + write(track, command); } /** - * Drop a safety command whose session failed before the status confirmed it. + * Drop a safety command the status has not confirmed by its deadline. * * The local latch then follows the truck's status again, rather than showing a * stop that never arrived: the viewer sees it and can press again. @@ -477,65 +490,18 @@ export class Truck { this.estopped.set(this.status.peek()?.estopped ?? false); } - /** Dial the truck from a new endpoint `id`, both closed with `effect`. */ - #dial(effect: Moq.Signals.Effect, id: string): Session { - const session: Session = new Moq.Signals.Signal(undefined); - const endpoint = new Moq.Socket.Endpoint(this.#origin, SOCKET, id); - effect.cleanup(() => { - // Finishing first ends the session cleanly rather than resetting it. - session.peek()?.producer.finish(); - endpoint.close(); - }); - effect.spawn(async () => { - try { - session.set(await endpoint.dialTrack(this.name, undefined, { signal: effect.abort })); - } catch (err) { - if (effect.abort.aborted) return; - console.warn(`dialing ${this.name} failed:`, err); - session.set(null); - } - }); - return session; - } - - /** Read the truck's acknowledgements, returning true once the session ends unless `effect` closed it. */ - async #readReplies(effect: Moq.Signals.Effect, stream: Moq.Socket.TrackStream): Promise { - // An acknowledgement a heartbeat late is superseded by the next one. - stream.consumer.update({ order: "ascending", delay: { max: CONTROL_HEARTBEAT } }); - const replies = stream.consumer.ordered(); - try { - for (;;) { - const frame = await effect.race(replies.readFrame()); - if (effect.abort.aborted) return false; - if (!frame) break; - let reply: Reply; - try { - reply = ReplySchema.parse(JSON.parse(decoder.decode(frame.payload))); - } catch (err) { - console.warn("Invalid reply JSON:", err); - continue; - } - this.#acknowledge(reply); - } - } catch (err) { - if (effect.abort.aborted) return false; - console.warn(`session with ${this.name} failed:`, err); - } - return true; - } - /** Give up driving this truck, if this player still is. */ #release() { if (this.#driving.peek() === this.id) this.#driving.set(undefined); } - #acknowledge(reply: Reply) { - this.pipeline.set(reply.media_pipeline); - const sentAt = this.#sentAt.get(reply.sequence); + #acknowledge(status: TruckStatus) { + if (status.driver !== this.driverId.peek() || status.sequence == null) return; + const sentAt = this.#sentAt.get(status.sequence); if (sentAt == null) return; this.roundTripMs.set(performance.now() - sentAt); for (const sequence of this.#sentAt.keys()) { - if (sequence <= reply.sequence) this.#sentAt.delete(sequence); + if (sequence <= status.sequence) this.#sentAt.delete(sequence); } } diff --git a/demo/relay/prod.toml b/demo/relay/prod.toml index f3292a5c961..f0acc82ab5f 100644 --- a/demo/relay/prod.toml +++ b/demo/relay/prod.toml @@ -45,17 +45,17 @@ key = "key.pem" # # moq auth serve --listen 127.0.0.1:4440 --key root.jwk \ # --public-subscribe 'anon/**' --public-subscribe 'demo/**' \ -# --public-publish 'anon/**' \ +# --public-publish 'anon/**' --public-publish 'demo/viewer/**' \ # --public-publish 'demo/boy/.client/*' \ -# --public-publish 'demo/carla/.client/*' \ # --mtls-publish '**' --mtls-subscribe '**' --mtls-link peer # -# Anonymous MoQ Boy viewers and CARLA drivers get one shared grant each, covering only -# client socket endpoints: a game's or truck's server endpoint, `.server/`, stays -# its publisher's, which signs in with a token. `demo/**` lets viewers dial those -# servers, and also read another viewer's client endpoint, which yields nothing without -# a session's secret id. Each viewer's id is 128 random bits, so no viewer can guess -# another's. +# Anonymous CARLA drivers share `demo/viewer/**`, under which each one publishes its +# controls: a truck's broadcasts stay under `demo/carla/`, its worker's, which signs in +# with a token. Anonymous MoQ Boy viewers get one shared grant covering only client +# socket endpoints: a game's server endpoint, `.server/`, stays the emulator's. +# `demo/**` lets viewers dial those servers, and also read another viewer's client +# endpoint, which yields nothing without a session's secret id. Each viewer's id is 128 +# random bits, so no viewer can guess another's. # # For multiple keys with rotation, give it `--key-dir /path/to/keys/` instead: # keys are named {kid}.jwk and resolved by the kid in the JWT header. diff --git a/doc/bin/demo.md b/doc/bin/demo.md index 0a0bd24ceab..13bf1ae3662 100644 --- a/doc/bin/demo.md +++ b/doc/bin/demo.md @@ -64,7 +64,7 @@ just carla # relay + trucks + player What it demonstrates: - **GPU to wire.** Camera images never leave the GPU: `moq-video` encodes them on the device that rendered them, and a camera renders only while someone subscribes. -- **Control with a lease.** A driver dials the truck's [socket](/concept/socket) endpoint and sends JSON controls on its own session, which carries the truck's acknowledgements back; the truck accepts one driver at a time, bound to that session, and returns to autonomy when the lease lapses. +- **Control with a lease.** A driver publishes a JSON `control` track under `viewer/carla/{truck}/`; the truck accepts one driver at a time, bound to the instance it announced with, and returns to autonomy when the lease lapses. - **Measured feedback.** Each truck's `status` track carries what the gate accepted and what the vehicle did, so the player draws requested against measured steering. Code: [`demo/carla`](https://github.com/moq-dev/moq/tree/main/demo/carla). diff --git a/packaging/moq-relay/relay.toml b/packaging/moq-relay/relay.toml index 1a26eb48d79..801d3b409e5 100644 --- a/packaging/moq-relay/relay.toml +++ b/packaging/moq-relay/relay.toml @@ -33,7 +33,7 @@ key = "/var/lib/moq-relay/key.pem" # # moq auth serve --listen 127.0.0.1:4440 --key /var/lib/moq-relay/root.jwk \ # --public-subscribe 'anon/**' --public-subscribe 'demo/**' \ -# --public-publish 'anon/**' +# --public-publish 'anon/**' --public-publish 'demo/viewer/**' # # Generate the key with `moq auth generate`. See https://moq.dev/docs/auth. [auth] diff --git a/quest/m2/sockets/carla.md b/quest/m2/sockets/carla.md deleted file mode 100644 index d63709a1525..00000000000 --- a/quest/m2/sockets/carla.md +++ /dev/null @@ -1,89 +0,0 @@ -# [M] CARLA drivers publish their controls again - -## Goal - -The CARLA demo drives trucks without socket endpoints, in the format it had -before [moq-dev/moq#8011](https://github.com/moq-dev/moq/pull/8011). Each -driver publishes a broadcast at `viewer/carla/{truck}/{driver}` with a -`control` track; the worker follows the announcements under -`viewer/carla/{truck}/` for each truck and feeds every driver's controls -into the safety gate; acknowledgements ride the truck's shared `status` -track. The worker and the player need no version that carries hidden -endpoint broadcasts. - -## Plan - -Decided 2026-11-16 (maintainer, /quest-plan of the pre-cut audit): ✅ go back -to the old wire format (rejected: keeping the demo on sockets). The demo -itself stays in this repository because moq.dev hosts it (same date). - -The old format, as `4d51f9c051fd^` (the parent of #8011) has it: - -- **Paths** (`demo/carla/web/src/paths.ts`, `demo/carla/README.md`): cameras - at `carla/{truck}/{camera}.hang`; one broadcast per driver at - `viewer/carla/{truck}/{driverId}` with a `control` track of `moq-json` - snapshots. -- **Controls**: `{"type":"drive","sequence":42,"steering":..}`, `estop`, - `reset_estop`, and `respawn`, each with a `sequence` and `timestamps`. -- **Worker** (`demo/carla/sim/moq_carla_sim/publish.py`): an announce, - follow, and request loop per truck. It lists `viewer/carla/{truck}/`, - follows each driver path across that driver's runs, and pumps one run's - `control` track at a time into the command queue, ending a run when - another takes over or it goes offline. -- **Lease** (`drive.py`): bound to `(driver id, run counter)`. The counter - is process-wide and local to the worker, from - [moq-dev/moq#7678](https://github.com/moq-dev/moq/pull/7678): the bindings - hand out a run's broadcast without its route instance, and the count keeps - a run's late `driver_left` from releasing a later run of the same driver. -- **Status**: `driver`, `estop_by`, `sequence`, and `media_pipeline` on the - truck's shared `status` track; the status is the control echo. -- **E-stop**: the holder's rides its control track; a bystander latches and - resets through a short-lived `safety-` broadcast without taking the - lease. -- **Grants**: anonymous viewers publish under `demo/viewer/**`; the worker's - token publishes `carla/**` and subscribes `viewer/carla/**`. - -What #8011 changed and this undoes: `publish.Drivers` and the endpoint at -`carla/.server/{truck}`, the lease bound to a session id, replies on the -driver's session, the player's endpoint and its own origin, the -`VERSIONS` pin (`demo/carla/sim/moq_carla_sim/main.py:43`, -`demo/carla/web/src/paths.ts:39`), and the `demo/carla/.client/*` grant. - -Restore the format, not the commit. For the PR: - -- Keep what #8011's reviews fixed that is not about sockets (a bystander's - e-stop failure surfaces; shutdown does not hang on a full queue), and the - one-heartbeat (250 ms) delay budget on controls, so a reset right before a - drive still arrives whole. -- Re-decide rather than restore, each with a recommendation: - - The lease's run identity. Recommended: the local run counter returns as - it was. The alternative, an `instance()` accessor on the bindings' - broadcast consumer, was deferred on 2026-11-03 "if the move to sockets - doesn't make that counter moot"; sockets are gone, so it is open again, - and the bindings rule now adds an accessor only when a binding user - needs it. The demo works without it. - - The anonymous grant. Recommended: `demo/viewer/**` returns to - `demo/relay/prod.toml` and the `packaging/moq-relay/relay.toml` sample, - shared with [MoQ Boy](/quest/m2/sockets/boy.md). Anyone who can publish - under `viewer/carla/` can claim a driver id; the lease stays - cooperative, and the README says so as it did. - - The version list. Recommended: the worker and the player offer their - defaults again. - - Whether the player's element keeps the origin it took for the endpoint. - Recommended: whichever is less code against today's `@moq/net`; the - driver's control broadcast needs an origin to publish into either way. -- Update `demo/carla/README.md`, `doc/bin/demo.md` (the control bullet), the - installer text under `demo/carla/sim/systemd/`, and the token example. - -Tests: `just carla check` (the worker's lease, watchdog, and e-stop tests -against the restored loop) and `bun test` in `demo/carla/web`. The live -simulator needs the GPU image: drive a truck from a browser after deploying, -including a bystander e-stop, as #8011 asked. - -Public API: none. Wire (app level): controls and acknowledgements move back -to broadcasts; the worker and the player deploy together. - -## Related - -- [moq.dev's hosted demos run the cut](/quest/m3/release/demos-deploy.md) - deploys this -- [moq-json writes take a value and a time](/quest/m2/json-write-at.md) - changes the write the `control` track uses diff --git a/quest/m2/sockets/remove.md b/quest/m2/sockets/remove.md index a078b6f91c6..1b944500f54 100644 --- a/quest/m2/sockets/remove.md +++ b/quest/m2/sockets/remove.md @@ -89,7 +89,6 @@ Public API: breaking for `main`-only API in every language; nothing on ## Required - [MoQ Boy viewers publish their commands again](/quest/m2/sockets/boy.md) - the emulator and its element stop using `moq_net::socket` and `@moq/net`'s `Socket` -- [CARLA drivers publish their controls again](/quest/m2/sockets/carla.md) - the worker stops using the Python wrapper and the player `@moq/net`'s `Socket` ## Related diff --git a/quest/m3/release/demos-deploy.md b/quest/m3/release/demos-deploy.md index 68690fb71f7..7f9a67b6ea6 100644 --- a/quest/m3/release/demos-deploy.md +++ b/quest/m3/release/demos-deploy.md @@ -24,7 +24,7 @@ What that changes here: MoQ Boy on `release` already speaks the restored format ([its quest](/quest/m2/sockets/boy.md)), so the hosted relays need no new version for it and the auth server keeps `demo/viewer/**`. CARLA was added on `main` after `release` branched; its restored format -([its quest](/quest/m2/sockets/carla.md)) uses the same grant. Read both +([moq-dev/moq#8937](https://github.com/moq-dev/moq/pull/8937)) uses the same grant. Read both quests' merged PRs for the grants they settled before deploying, since each re-decides its grant rather than restoring it blindly. @@ -59,4 +59,3 @@ Public API: none. Wire: none. ## Required - [MoQ Boy viewers publish their commands again](/quest/m2/sockets/boy.md) - settles the emulator's and viewers' grants this deploys -- [CARLA drivers publish their controls again](/quest/m2/sockets/carla.md) - settles the worker's and drivers' grants this deploys From 0a8f7f9e81bd30a47e03d11dfb88ada14ee5c5ae Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 16 Nov 2026 06:49:33 -0800 Subject: [PATCH 3/5] chore(carla): drop the unused drivers accessor and reflow the deploy quest Co-Authored-By: Claude Opus 5.5 --- demo/carla/sim/moq_carla_sim/runtime.py | 4 ---- quest/m3/release/demos-deploy.md | 6 +++--- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/demo/carla/sim/moq_carla_sim/runtime.py b/demo/carla/sim/moq_carla_sim/runtime.py index ea22b90b92b..4f82abb7302 100644 --- a/demo/carla/sim/moq_carla_sim/runtime.py +++ b/demo/carla/sim/moq_carla_sim/runtime.py @@ -132,10 +132,6 @@ def stop_video(self) -> None: def commands(self) -> asyncio.Queue[Command]: return self._commands - @property - def drivers(self) -> Drivers: - return self._drivers - async def run(self, client: moq.Client, drivers: str) -> None: """Consume driver commands and actuate until cancelled. diff --git a/quest/m3/release/demos-deploy.md b/quest/m3/release/demos-deploy.md index 7f9a67b6ea6..56cc5d3e3a8 100644 --- a/quest/m3/release/demos-deploy.md +++ b/quest/m3/release/demos-deploy.md @@ -24,9 +24,9 @@ What that changes here: MoQ Boy on `release` already speaks the restored format ([its quest](/quest/m2/sockets/boy.md)), so the hosted relays need no new version for it and the auth server keeps `demo/viewer/**`. CARLA was added on `main` after `release` branched; its restored format -([moq-dev/moq#8937](https://github.com/moq-dev/moq/pull/8937)) uses the same grant. Read both -quests' merged PRs for the grants they settled before deploying, since each -re-decides its grant rather than restoring it blindly. +([moq-dev/moq#8937](https://github.com/moq-dev/moq/pull/8937)) uses the same +grant. Read both quests' merged PRs for the grants they settled before +deploying, since each re-decides its grant rather than restoring it blindly. The cut publishes no MoQ Boy package ([moq-dev/moq#8446](https://github.com/moq-dev/moq/pull/8446), 2026-11-10): From 8b041fc6e87f46bc0ff88b797cd686bad4a787d6 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 02:34:01 -0800 Subject: [PATCH 4/5] fix(carla): an oversized number no longer ends a run, and a closed control publication lets go --- demo/carla/sim/moq_carla_sim/publish.py | 6 +++++- demo/carla/sim/tests/test_publish.py | 9 +++++++++ demo/carla/web/src/truck.ts | 8 +++++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/demo/carla/sim/moq_carla_sim/publish.py b/demo/carla/sim/moq_carla_sim/publish.py index aabd9cd0c0c..f3eec270075 100644 --- a/demo/carla/sim/moq_carla_sim/publish.py +++ b/demo/carla/sim/moq_carla_sim/publish.py @@ -293,7 +293,11 @@ def _number(value: Any) -> float: def _number_or_none(value: Any) -> float | None: if not isinstance(value, (int, float)) or isinstance(value, bool): return None - number = float(value) + try: + number = float(value) + # An integer past the float range, which `float` refuses rather than rounding to infinity. + except OverflowError: + return None return number if math.isfinite(number) else None diff --git a/demo/carla/sim/tests/test_publish.py b/demo/carla/sim/tests/test_publish.py index 0c9174e37b1..b441663ad4b 100644 --- a/demo/carla/sim/tests/test_publish.py +++ b/demo/carla/sim/tests/test_publish.py @@ -65,6 +65,15 @@ def test_malformed_timestamp_does_not_drop_driver_command() -> None: assert command.timestamps == (TimestampEntry(label="received", ts=12.5),) +def test_an_integer_past_the_float_range_is_not_a_number() -> None: + command = parse_command("driver", {"type": "drive", "sequence": 10**400, "throttle": 10**400}) + + assert command is not None + assert command.sequence == 0 + assert command.state is not None + assert command.state.throttle == 0.0 + + def test_only_the_players_pipeline_stages_are_kept() -> None: stages = [{"label": "received", "ts": 1.0}, {"label": "decoded", "ts": 2.0}] junk = [{"label": "x" * 10_000, "ts": 3.0}] * 1_000 diff --git a/demo/carla/web/src/truck.ts b/demo/carla/web/src/truck.ts index 2b125e82336..cc925ed8eff 100644 --- a/demo/carla/web/src/truck.ts +++ b/demo/carla/web/src/truck.ts @@ -456,10 +456,16 @@ export class Truck { this.#sentAt.clear(); }); - this.#control = publishControl(effect, origin, driverPath(this.name, id)); + const control = publishControl(effect, origin, driverPath(this.name, id)); + this.#control = control; effect.cleanup(() => { this.#control = undefined; }); + // A closed publication drops every command, so letting go beats staying active on it. + if (effect.get(control.closed) !== undefined) { + this.#release(); + return; + } // The claim, which the activation's own heartbeat sent before the track existed. this.#heartbeat(); } From 83f2017e331cd60c385aa202bdceeec3aa002290 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 02:35:26 -0800 Subject: [PATCH 5/5] fix(carla): a closed control publication releases the lease from a nested effect --- demo/carla/web/src/truck.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/demo/carla/web/src/truck.ts b/demo/carla/web/src/truck.ts index cc925ed8eff..0ff22a0e1ef 100644 --- a/demo/carla/web/src/truck.ts +++ b/demo/carla/web/src/truck.ts @@ -462,10 +462,10 @@ export class Truck { this.#control = undefined; }); // A closed publication drops every command, so letting go beats staying active on it. - if (effect.get(control.closed) !== undefined) { - this.#release(); - return; - } + // Nested, so the closure releases the lease instead of rerunning this effect with a fresh id. + effect.run((inner) => { + if (inner.get(control.closed) !== undefined) this.#release(); + }); // The claim, which the activation's own heartbeat sent before the track existed. this.#heartbeat(); }