5.1 KiB
KoalaSync AI Onboarding (AI_INIT.md)
Welcome to the KoalaSync project. This file is the primary entry point for any developer or AI agent working on this codebase. It defines the architecture, non-negotiables, and workflows required to maintain the stability and security of the system.
1. Project Overview
KoalaSync is a specialized tool for synchronized video playback across multiple remote peers. It supports YouTube, Twitch, and native HTML5 video elements.
- Users: Friends or groups wanting to watch synchronized content together.
- Workflow: A user creates a room, shares an invite link (RoomID#Password), and all peers in that room are synchronized via a Node.js relay server.
2. Repository Structure
extension/: Chrome Extension (Manifest V3). Contains background service worker, content scripts, and popup UI.server/: Node.js Relay Server using Socket.IO (WebSocket-only).website/: Landing Page (Marketing, Tutorials, and Downloads).shared/: Single Source of Truth for protocol constants and event names.scripts/: Utility scripts (e.g.,sync-constants.sh).docker-compose.yml: Root-level orchestration for the relay server.
Important
shared/constants.jsandshared/blacklist.jsmust be synchronized to theextension/shared/directory after every modification by running./scripts/sync-constants.sh.
3. Mandatory Reading
Before touching any code, you MUST read the following documents in order:
- ARCHITECTURE.md – Detailed communication flows and two-phase sync protocol.
- shared/README.md – Protocol constants and synchronization requirements.
- extension/README.md – Extension components and loading process.
- server/README.md – Server setup, Docker configuration, and security.
4. Design Guidelines
The popup UI follows a strict design system. Do not modify these variables or the layout structure without explicit approval.
- Font: 'Outfit' (Google Fonts).
- Popup Width: Fixed at
320px. - Tab Structure: Must maintain the Room, Sync, and Dev tabs.
- CSS Variables:
Variable Value Purpose --bg#0f172aMain background --card#1e293bForm and info cards --accent#6366f1Primary actions and branding --success#22c55eSuccess states / Online dot --error#ef4444Errors / Offline dot
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→Executeflow ensures all peers are buffered before playback resumes. - Platform Specifics: Specialized click-logic for YouTube (
.ytp-play-button) and Twitch play/pause buttons incontent.js. - pollSeekReady(): The polling mechanism that checks
video.readyStateandcurrentTimeoffset before acknowledging a sync command. - SW Keep-alive: Use of
chrome.alarmsto prevent the Manifest V3 Service Worker from suspending. - Exponential Backoff: Reconnection logic in
background.js(1s → 30s max). - Rate Limiting: IP-based connection limits (10/min) and socket-based event limits (30/10s) on the server.
- Security: Token validation during the initial WebSocket handshake.
- Persistence:
peerIdmust be stored inchrome.storage.localto remain stable across sessions.
6. Technical Constraints
- No Bundler: The extension uses plain ES Modules. Do not introduce build steps or npm packages into the
extension/folder. - Manual Protocol:
background.jsimplements a subset of the Socket.IO wire protocol (e.g.,42[...]framing) to work with native WebSockets. - Server Transport: Restricted to
websocketonly. Polling is disabled. - Docker Context: The Docker build must run from the Repo Root, as it needs access to the
shared/directory. - Manifest Settings:
run_atmust remaindocument_idle, andall_framesmust remainfalse.
7. Security & Deployment
- Tokens:
OFFICIAL_SERVER_TOKENandOFFICIAL_SERVER_URLare intentionally hardcoded inconstants.jsby design. - Environment:
.envis excluded via.gitignore. Only.env.exampleshould be committed. - Revocation:
MIN_VERSIONin the server configuration is the only way to deprecate old extension versions. - Token Rotation: Requires updating
shared/constants.js, running the sync script, incrementing the extension version, and re-deploying the server.
8. Common Workflows
Adding a Protocol Event
- Add the event name to
shared/constants.js. - Run
./scripts/sync-constants.sh. - Implement the handler in
server/index.jsandbackground.js.
Testing Locally
- Load
extension/as an "Unpacked Extension" in Chrome. - Start the server from the root:
docker-compose up --build. - Select "Custom" server in the popup and enter
ws://localhost:3000.
Locking Old Versions
- Increase
APP_VERSIONinshared/constants.js. - Update
MIN_VERSIONin the server's.envfile. - Restart the server.