Overview
The HAPI runner is a background service (daemon) that manages AI agent sessions. It allows you to:- Keep sessions running even after closing your terminal
- Spawn new sessions remotely via web/mobile app
- Manage multiple concurrent sessions
- Automatically restart sessions on system reboot
How the Runner Works
The runner operates as a detached background process that:- Listens for session spawn requests from the hub
- Manages the lifecycle of active sessions
- Sends periodic heartbeats to the hub
- Cleans up terminated sessions
- Maintains state in
~/.hapi/runner.state.json
The runner starts automatically when you run
hapi for the first time. Manual management is optional.Commands
hapi runner start
Start the runner as a detached background process.Output Example
Behavior
- Spawns runner as detached process with
stdio: 'ignore' - Waits up to 5 seconds to confirm successful start
- Exits with error if runner fails to start
- Safe to run if runner is already running (no-op)
hapi runner stop
Stop the runner gracefully. Active sessions remain alive.Stopping the runner does not terminate active sessions. Sessions continue running as independent processes.
What Happens
- Sends shutdown signal to runner process
- Runner saves its state
- Runner closes HTTP server
- Runner exits gracefully
- Active sessions keep running
To Kill All Processes
If you need to stop both the runner and all sessions:hapi runner status
Show detailed runner diagnostics and health information.hapi doctor but focuses on runner-specific information.
Output Example
hapi runner list
List all active sessions currently managed by the runner.Output Example
No Active Sessions
Runner Not Running
hapi runner stop-session
Terminate a specific session by its ID.The unique session ID to terminate. Get session IDs from
hapi runner list.Examples
Errors
hapi runner logs
Print the path to the latest runner log file.Output Example
View Logs
Combine with other commands to view or tail logs:No Logs Found
Runner State File
The runner maintains state in~/.hapi/runner.state.json (or $HAPI_HOME/runner.state.json).
State File Structure
Process ID of the runner daemon
HTTP port used for runner control API
ISO timestamp when runner started
CLI version that started this runner instance
ISO timestamp of last heartbeat (updated every 60 seconds by default)
Path to current log file
Automatic Runner Management
The runner starts automatically in these scenarios:1. First Session Start
When you runhapi (or any session command) for the first time:
2. Version Mismatch
If the running runner version doesn’t match the installed CLI version, it’s automatically restarted:3. Stale or Dead Runner
If the runner state is stale or the process is dead, it’s cleaned up and restarted automatically.Runner Lifecycle
Startup Sequence
- Runner process spawned as detached
- Loads configuration from environment and settings
- Acquires exclusive lock file (
runner.state.json.lock) - Writes state to
runner.state.json - Starts HTTP control server on random port
- Connects to HAPI hub via Socket.IO
- Registers runner with hub
- Begins heartbeat loop (every 60 seconds)
- Listens for session spawn requests
Heartbeat
Every 60 seconds (configurable viaHAPI_RUNNER_HEARTBEAT_INTERVAL):
- Updates
lastHeartbeatin state file - Sends heartbeat to hub
- Cleans up dead session processes
Shutdown Sequence
- Receives shutdown signal (from
hapi runner stop) - Stops accepting new session requests
- Closes HTTP control server
- Disconnects from hub
- Releases lock file
- Exits process
- Active sessions continue running independently
Troubleshooting
Runner Won’t Start
Check if already running:Stale Runner State
If runner state becomes corrupted:Version Mismatch
If runner and CLI versions don’t match:Port Already in Use
The runner uses a random available port, so port conflicts are rare. If you encounter issues:Sessions Not Appearing
Check runner is running:Advanced Usage
Custom Heartbeat Interval
Custom HTTP Timeout
Multiple Runners (Multi-Namespace)
Run separate runners for different HAPI namespaces:Related Commands
Diagnostics
View full system diagnostics including runner status
Session Commands
Start sessions managed by the runner
Next Steps
- Diagnostics - Full system health check
- Environment Variables - Configure runner behavior
- Settings File - Understand local storage