# User Story 009: Web Worker Architecture (Main-thread Safe Web Library) ## Story **As a** user building robotics UIs that also render live camera previews and interactive controls **I want** `@lerobot/web` to run heavy control/recording work off the main thread **So that** my UI stays smooth (no flicker/jank) even when teleoperation and recording are active ## Background The current browser implementation runs teleoperation control loops, dataset assembly, and export logic on the main thread. When activating keyboard teleoperation while previewing a camera stream, the preview can flicker due to main-thread contention. This is a UX blocker for real-world apps that combine live video, UI interactions, and hardware control. A worker-based architecture lets us move CPU-intensive, frequent, or bursty work off the main thread. The main thread remains responsible for DOM, video rendering and user interactions. The library must preserve the existing API (`calibrate()`, `teleoperate()`, `record()`) while transparently using workers when available, and cleanly falling back to the current approach otherwise. ## Goals - Identical public API to today’s `@lerobot/web` (no breaking changes) - Main-thread safe by default: heavy or frequent work executes in a Web Worker - Graceful fallback when workers or specific APIs aren’t available - Type-safe, minimal-copy message protocol using Transferables when possible - Strict library/demo separation: UI and storage remain in demos - Maintain Python lerobot UX parity and behavior ## Non-Goals (for this story) - Changing dataset formats or camera acquisition approach - Rewriting Web Serial API usage into worker (browser support is limited in workers) - Introducing new external dependencies ## Acceptance Criteria - Smooth UI under load: - With at least one active camera preview and keyboard teleoperation at 60–120 Hz, the preview does not flicker and UI remains responsive at ~60 FPS - API compatibility: - `calibrate()`, `teleoperate()`, `record()` signatures and return shapes are unchanged - Feature-detect workers; automatically use worker-backed runtime when available, otherwise use current main-thread runtime - Clear separation of responsibilities: - Worker executes control loops, interpolation, dataset assembly, export packaging, and CPU-heavy transforms - Main thread owns DOM/UI and browser-only APIs that are unavailable in workers (e.g., Web Serial write calls) - Type-safe protocol: - Strongly typed request/response messages with versioned `type` fields; Transferable payloads used for large data - Reliability & fallback: - If the worker crashes or becomes unavailable, operations fail gracefully with descriptive errors and suggest retry - Fallback path (main-thread) is automatically used when worker creation fails - Tests & docs: - Unit tests cover protocol routing and basic round-trips - Planning docs updated; README notes main-thread-safe architecture ## Architecture Overview ### Worker Boundaries - Execute in Worker: - Control loop scheduling and target computation for teleoperation (keyboard/direct and future teleoperators) - Episode/frame buffering and interpolation (regularization) for recording - Dataset assembly (tables/metadata), packaging (ZIP writer), and background export streaming - Lightweight telemetry aggregation for UI - Execute on Main Thread: - DOM, UI, and camera previews (`