HQ Mode Documentation
HQ (Headquarters) mode allows multiple VibeTunnel servers to work together in a distributed setup, where one server acts as the central HQ and others register as remote servers.Overview
In HQ mode:- HQ Server: Acts as a central aggregator and router
- Remote Servers: Individual VibeTunnel servers that register with the HQ
- Clients: Connect to the HQ server and can create/manage sessions on any remote
How It Works
1. Registration Flow
When a remote server starts with HQ configuration:- It generates a unique bearer token
- Registers itself with the HQ using Basic Auth (HQ credentials)
- Provides its ID, name, URL, and bearer token
- HQ stores this information in its
RemoteRegistry
2. Session Management
Creating Sessions:- Clients can specify a
remoteId
when creating sessions - HQ forwards the request to the specified remote using the bearer token
- The remote creates the session locally
- HQ tracks which sessions belong to which remote
- All session operations (get info, send input, kill, etc.) are proxied through HQ
- HQ checks its registry to find which remote owns the session
- Requests are forwarded with bearer token authentication
3. Health Monitoring
- HQ performs health checks every 15 seconds on all registered remotes
- Health check:
GET /api/health
with 5-second timeout - Failed remotes are automatically unregistered
4. Session Discovery
- Remote servers watch their control directory for new sessions
- When sessions are created/deleted, remotes notify HQ via
/api/remotes/{name}/refresh-sessions
- HQ fetches the latest session list from the remote and updates its registry
Setup
Running an HQ Server
Running Remote Servers
Command-Line Options
HQ Server Options:--hq
- Enable HQ mode--username
- Admin username for HQ access--password
- Admin password for HQ access
--hq-url
- URL of the HQ server--hq-username
- Username to authenticate with HQ--hq-password
- Password to authenticate with HQ--name
- Unique name for this remote server--allow-insecure-hq
- Allow HTTP connections to HQ (dev only)--no-hq-auth
- Disable HQ authentication (testing only)
API Endpoints
HQ-Specific Endpoints
List Remotes:Session Management Through HQ
Create Session on Remote:GET /api/sessions
- Aggregates from all remotesGET /api/sessions/:id
- Proxied to owning remotePOST /api/sessions/:id/input
- Proxied to owning remoteDELETE /api/sessions/:id
- Proxied to owning remoteGET /api/sessions/:id/stream
- SSE stream proxied from remote
Authentication Flow
- Client → HQ: Standard authentication (Basic Auth or JWT)
- HQ → Remote: Bearer token (provided by remote during registration)
- Remote → HQ: Basic Auth (HQ credentials)
WebSocket Support
- Buffer updates (
/buffers
) are aggregated from all remotes - Input WebSocket (
/ws/input
) connections are proxied to the owning remote - HQ maintains WebSocket connections and forwards messages transparently
Implementation Details
Key Components
RemoteRegistry (src/server/services/remote-registry.ts
):
- Maintains map of registered remotes
- Tracks session ownership (which sessions belong to which remote)
- Performs periodic health checks
- Handles registration/unregistration
src/server/services/hq-client.ts
):
- Used by remote servers to register with HQ
- Handles registration and cleanup
- Manages bearer token generation
src/server/routes/sessions.ts
):
- Checks if running in HQ mode
- For remote sessions, forwards requests using bearer token
- For local sessions, handles normally
src/server/services/control-dir-watcher.ts
):
- Watches for new/deleted sessions
- Notifies HQ about session changes
- Triggers session list refresh on HQ
Session Tracking
- Each remote maintains its own session IDs
- HQ tracks which sessions belong to which remote
- Session IDs are not namespaced - they remain unchanged
- The
source
field in session objects indicates the remote name
Testing
The e2e tests insrc/test/e2e/hq-mode.e2e.test.ts
demonstrate:
- Starting HQ server and multiple remotes
- Remote registration
- Creating sessions on specific remotes
- Proxying session operations
- WebSocket buffer aggregation
- Cleanup and unregistration
Limitations
- Remotes must be network-accessible from the HQ server
- Health checks use a fixed 15-second interval
- No built-in load balancing (clients must specify remoteId)
- Bearer tokens are generated per server startup (not persistent)
- No automatic reconnection if remote temporarily fails
Security Considerations
- Always use HTTPS in production (use
--allow-insecure-hq
only for local dev) - Bearer tokens are sensitive - they allow HQ to execute commands on remotes
- HQ credentials should be strong and kept secure
- Consider network isolation between HQ and remotes
- Remotes should not be directly accessible from the internet