From c9cf7c49dc79855f60cc31a34199befc8edf840f Mon Sep 17 00:00:00 2001 From: MacBook Date: Fri, 1 May 2026 06:06:46 +0200 Subject: [PATCH] Fix logic flaws and update documentation --- AI_INIT.md | 5 +++-- ARCHITECTURE.md | 18 +++++++++++++----- README.md | 4 ++-- SYNC_GUIDE.md | 27 ++++++++++----------------- extension/background.js | 19 +++++++++++++++---- server/index.js | 1 + 6 files changed, 44 insertions(+), 30 deletions(-) diff --git a/AI_INIT.md b/AI_INIT.md index 88e63b2..8527644 100644 --- a/AI_INIT.md +++ b/AI_INIT.md @@ -23,7 +23,7 @@ KoalaSync is a specialized tool for **synchronized video playback** across multi - `docker-compose.yml`: Root-level orchestration for the relay server. > [!IMPORTANT] -> **Single Source of Truth**: `shared/constants.js` and `shared/blacklist.js` are the master files. They must be synchronized to the `extension/shared/` directory using `.\scripts\sync-constants.bat` or `./scripts/sync-constants.sh`. +> **Single Source of Truth**: `shared/constants.js` and `shared/blacklist.js` are the master files. They must be synchronized to the `extension/shared/` directory using `node scripts/build-extension.js`. > - **Extension Modules** (`background.js`, `popup.js`) import directly from `./shared/constants.js`. > - **Content Scripts** (`content.js`) use a **manual synchronous mirror** to prevent race conditions during page load. Always verify parity after sync. @@ -56,6 +56,7 @@ The popup UI follows a strict design system. Do not modify these variables or th ## 5. Non-Negotiables (Core Logic) The following features are critical and must not be removed or fundamentally altered: - **Two-Phase Force Sync**: The `Prepare` → `ACK` → `Execute` flow ensures all peers are buffered before playback resumes. +- **Episode Auto-Sync**: Ensures series binges stay perfectly synced. A lobby initiates during title transitions, freezing peers until everyone is ready. - **Dual Heartbeat**: - **Background Heartbeat (30s)**: Ensures session persistence even without a video element. - **Content Heartbeat (15s)**: Transmits current video metadata (time, title). @@ -84,7 +85,7 @@ The following features are critical and must not be removed or fundamentally alt ### Adding a Protocol Event 1. Add the event name to `shared/constants.js`. -2. Run the sync script (`.\scripts\sync-constants.bat` or `./scripts/sync-constants.sh`). +2. Run the build script (`node scripts/build-extension.js`). 3. Implement the handler in `server/index.js` and `background.js`. ### Testing Locally diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index a97174b..022ba5a 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -25,25 +25,33 @@ When a user interacts with a video: Ensures all peers are frame-perfect and buffered before resuming: 1. **Prepare**: Initiator sends `FORCE_SYNC_PREPARE` with the target timestamp. 2. **Buffer**: Peers seek and pause. Once buffered (`readyState >= 3`), they send a `FORCE_SYNC_ACK`. -3. **Execute**: Once the Initiator collects ACKs (or after a 5s timeout), they send `FORCE_SYNC_EXECUTE`. +3. **Execute**: Once the Initiator collects ACKs (or after an 8.5s timeout), they send `FORCE_SYNC_EXECUTE`. 4. **Resume**: All peers call `play()` simultaneously. -## 4. Peer Lifecycle & Dual Heartbeat +## 4. Episode Auto-Sync +Maintains continuous synchronized viewing when watching series: +1. **Detection**: `content.js` monitors the Media Session API for title changes. +2. **Lobby Creation**: When a new title is detected, the peer initiates an `EPISODE_LOBBY` and broadcasts the new title. +3. **Wait State**: All peers freeze their video until they have also loaded the exact same title. +4. **Mid-Lobby Joins**: If a new user joins the room during an active lobby, the lobby initiator broadcasts the active lobby state so the newcomer can sync up. +5. **Resume**: Once all peers report `EPISODE_READY`, the lobby is resolved and playback resumes perfectly. + +## 5. Peer Lifecycle & Dual Heartbeat To maintain a clean room state and eliminate "Ghost Peers": - **Session Heartbeat (Background)**: Every 30 seconds, `background.js` sends an "I'm alive" signal to the server. This keeps you in the room even if no video is playing. - **Video Heartbeat (Content)**: Every 15 seconds, `content.js` sends current playback metadata (time, title, state) if a video is found. - **Server Pruning**: The server runs a "Reaper" every 2 minutes. If a peer has sent **zero** activity (no events and no heartbeats) for 5 minutes, they are forcefully disconnected. - **Immediate Cleanup**: Rooms are deleted instantly when the last peer leaves or disconnects. -## 5. Security & Stability +## 6. Security & Stability - **Service Worker Lifecycle**: Uses `chrome.alarms` to prevent the Manifest V3 service worker from suspending while in an active room. - **Rate Limiting**: Server-side per-socket and per-IP rate limits to prevent sync-spamming or DoS. - **Noise Filtering**: Uses a curated blacklist of domains (Search Engines, Social Media) to declutter the "Target Tab" selector in the popup. - **Diagnostics**: A "Dev" tab provides real-time access to the underlying `