Daemon Management
The Happy daemon is a persistent background process that enables remote control from the mobile app, manages AI sessions, and handles automatic version updates.What is the Daemon?
The daemon provides:- Remote session control - Start and stop AI sessions from your mobile device
- Session tracking - Monitor active sessions and their status
- Auto-updates - Automatically restarts when Happy CLI is upgraded
- Background persistence - Keeps running even when you close your terminal
The daemon automatically starts when you run
happy for the first time. You typically don’t need to manage it manually.Daemon Commands
Start Daemon
Start the daemon as a background process:- Check if another daemon is already running
- Verify version compatibility with current CLI
- Stop old daemon if version mismatch detected
- Start new daemon process in background
- Write PID and state to
daemon.state.json
Stop Daemon
Gracefully shutdown the daemon:- Updates backend status to “shutting-down”
- Closes WebSocket connection
- Stops HTTP control server
- Deletes state file
- Releases lock file
Check Status
View detailed daemon status and diagnostics:- Daemon running status
- Process ID (PID)
- CLI version
- HTTP port
- Start time
- All Happy-related processes
List Active Sessions
Show all sessions tracked by the daemon:Stop a Session
Terminate a specific session by ID:View Daemon Logs
Get the path to the latest daemon log file:Daemon Lifecycle
Startup Process
Version check
Daemon reads
daemon.state.json and compares startedWithCliVersion with the current CLI version from package.json.Stop old daemon
If version mismatch detected, stops the old daemon gracefully (or forcefully with SIGKILL).
Start servers
- HTTP control server on random port (127.0.0.1 only)
- WebSocket connection to Happy backend
Register RPC handlers
Exposes
spawn-happy-session, stop-session, requestShutdown for remote control.Auto-Update Mechanism
The daemon automatically detects when you upgrade Happy CLI:- Heartbeat (every 60s) reads
package.jsonfrom disk - Compares disk version with compiled
configuration.currentCliVersion - If mismatch detected:
- Spawns new daemon with updated version
- Waits to be killed by new daemon
- New daemon starts, detects old version in
daemon.state.json - New daemon stops old daemon via
/stopendpoint or SIGKILL - New daemon takes over
Heartbeat interval can be customized with
HAPPY_DAEMON_HEARTBEAT_INTERVAL environment variable (default: 60000ms).Shutdown Process
Graceful shutdown sequence:- Receive shutdown signal (SIGINT, SIGTERM, HTTP
/stop, or RPCrequestShutdown) - Update backend state to “shutting-down” via WebSocket
- Disconnect WebSocket from Happy server
- Stop HTTP server (control endpoints become unavailable)
- Delete state file (
daemon.state.json) - Release lock file (allows new daemon to start)
- Exit process with code 0
State Management
daemon.state.json
Location:~/.happy/daemon.state.json (or $HAPPY_HOME_DIR/)
Lock File
Location:~/.happy/daemon.lock
- Created with
O_EXCLflag for atomic acquisition - Contains PID for debugging
- Prevents multiple daemon instances
- Automatically cleaned up on graceful shutdown
HTTP Control Server
The daemon runs a local HTTP server (127.0.0.1 only) for CLI control:Endpoints
POST /session-started
POST /session-started
Purpose: Session webhook - notifies daemon when a new session startsRequest Body:Response:
200 OKGET /list
GET /list
Purpose: List all tracked sessionsResponse:
POST /stop-session
POST /stop-session
Purpose: Stop a specific sessionRequest Body:Response:
200 OK or 404 Not FoundPOST /stop
POST /stop
Purpose: Gracefully shutdown the daemonResponse:
200 OKRemote Control (RPC)
The daemon exposes RPC methods via WebSocket for mobile app control:spawn-happy-session
Start a new AI session remotely:stop-session
Stop a running session:requestShutdown
Request daemon shutdown from mobile app:Process Management
Tracked Sessions
The daemon tracks sessions in memory:- Daemon-spawned: Via mobile app RPC (tracked immediately)
- Terminal-spawned: User runs
happydirectly (tracked via webhook)
Session Health Monitoring
Daemon monitors session health:- Heartbeat interval checks process status
- Exit handlers remove dead sessions from tracking
- Webhook timeout (10s) for daemon-spawned sessions
Troubleshooting
Daemon won’t start
Check if another daemon is running:Version mismatch
If daemon shows wrong version:Orphaned processes
Clean up runaway Happy processes:- Stale daemon processes
- Hung version checks
- Orphaned sessions
Port conflicts
Daemon uses random port for HTTP server. If conflicts occur:- Stop daemon:
happy daemon stop - Check for port usage:
lsof -i tcp:<port> - Restart daemon:
happy daemon start
Lock file stuck
If daemon fails to acquire lock:Environment Variables
Related Commands
happy doctor- Diagnose daemon issueshappy auth- Manage authenticationhappy sandbox- Configure sandboxing