Daemon
The daemon is Mia’s central process. It runs in the background, manages all subsystems, and routes messages between interfaces (CLI, mobile) and AI plugins.
Lifecycle
Section titled “Lifecycle”Startup Sequence
Section titled “Startup Sequence”The daemon initializes subsystems in a specific order:
- Process safety — Registers handlers for unhandled rejections and exceptions. After 10 crashes in 5 minutes, the daemon exits.
- Environment — Captures the project working directory, then
chdirto~/.mia. Loads.envfrom~/.mia/.env. - PID file — Writes
~/.mia/.mia.pidto prevent duplicate daemons. - Codebase context — Scans the project directory for languages, frameworks, file counts. Cached for the daemon’s lifetime.
- Memory store — Opens SQLite database, runs migrations, schedules periodic TTL pruning.
- Plugin registry — Registers all four plugins (Claude Code, Codex, OpenCode, Gemini), initializes each with config, warms availability cache.
- Middleware chain — Sets up ContextPreparer → TraceLogger → PostDispatchVerifier → MemoryExtractor.
- P2P networking — Derives topic key from seed, spawns P2P sub-agent as child process, wires message handlers.
- Scheduler — Loads
scheduled-tasks.json, starts all enabled cron jobs. - Event loop — Begins accepting messages from CLI (IPC) and mobile (P2P).
Graceful Shutdown
Section titled “Graceful Shutdown”When the daemon receives a shutdown signal:
- Arms a 15-second grace period timer
- Aborts all running plugin tasks
- Flushes memory cache
- Closes P2P connections
- Closes message store
- Writes final status
- Exits cleanly
Watchdog
Section titled “Watchdog”A watchdog timer detects event loop stalls. If the main loop blocks for too long, the daemon logs a warning and can take corrective action.
Message Routing
Section titled “Message Routing”CLI → Daemon
Section titled “CLI → Daemon”CLI commands connect to the daemon via IPC (Unix domain socket). The daemon parses the command and routes to the appropriate handler:
- Routed commands:
ask,chat,commit,standup,changelog,memory,plugin,scheduler,p2p,config,doctor,log,usage,update - Daemon commands:
start,stop,restart,status,logs(handled by the CLI directly)
Mobile → Daemon
Section titled “Mobile → Daemon”Mobile messages arrive via the Hyperswarm P2P connection. The swarm-message-handler (the largest module, ~1000 lines) parses typed JSON messages and routes them.
Message types include:
| Category | Examples |
|---|---|
| Conversations | new_conversation, load_conversation, rename, delete |
| Dispatch | dispatch (main prompt), abort |
| State queries | conversations_request, plugins_request, history_request |
| Plugin mgmt | plugin_switch |
| Scheduler | scheduler_list_request, scheduler_create, scheduler_delete |
| Search | search_request |
| System | ping, suggestions_request |
Process Management
Section titled “Process Management”PID Files
Section titled “PID Files”The daemon writes its PID to ~/.mia/.mia.pid on startup. The CLI checks this file to determine if the daemon is running before attempting to start a new instance.
PM2 Integration
Section titled “PM2 Integration”For production use, the daemon runs under PM2 for automatic restarts and log management:
mia start # Starts daemon via PM2mia stop # Stops PM2 processmia restart # Restarts PM2 processmia logs # Tails PM2 logsError Boundaries
Section titled “Error Boundaries”- Unhandled rejections: Caught and logged. After 10 in a 5-minute window, the daemon exits to prevent runaway failures.
- Plugin errors: Standardized
PluginErrortype with codes (TIMEOUT,SPAWN_FAILURE,PROCESS_EXIT, etc.). Errors trigger fallback chain if configured. - Stall detection: Child processes killed if no output for 120 seconds.