Expand description
khived daemon server — persistent warm runtime over a Unix socket.
The daemon binds ~/.khive/khived.sock, accepts length-prefixed request
frames, dispatches them through a DaemonDispatch implementor, and serves
results back. It is transport-agnostic: the MCP crate provides the dispatch
impl, but any future client (CLI, HTTP gateway) can reuse this server.
The client side (forwarding, auto-spawn) lives in the transport crate
(e.g. khive-mcp), not here.
Structs§
- Daemon
Request Frame - Request frame sent from a client to the daemon.
- Daemon
Response Frame - Response frame sent from the daemon back to a client.
- Metrics
Snapshot - Point-in-time snapshot of the daemon’s server-side gauges — the load/perf harness read-surface (measurement substrate, not a product feature).
- Phase
Guard - RAII guard for one occurrence of a named background phase. Increments the
phase’s count on creation (see
register_active_phase); decrements onDrop, so the count comes back down whether the guarded work returns normally, panics, or is cancelled — the same rationale asBackgroundTaskGuardabove.
Constants§
- MAX_
FRAME_ BYTES - Maximum frame size accepted in either direction.
- PROTOCOL_
VERSION - Wire protocol version for the daemon IPC framing.
Traits§
- Daemon
Dispatch - Transport-agnostic dispatch interface for the daemon server.
Functions§
- acquire_
daemon_ boot_ guard - Acquire the recovery/boot lock, treating failure as fatal.
- acquire_
recovery_ lock - Acquire an exclusive advisory flock on the recovery/startup lock file.
- active_
phase_ names - Currently active background-phase names, sorted for deterministic output. Empty when no tracked phase is in flight.
- background_
task_ count - Current count of in-flight tasks started via
track_background_task. Exposed for tests;drain()reads the shared counter directly. - env_
truthy - Returns
truefor non-empty env values that are not"0"or"false". - lock_
path - Advisory lock file used to serialize stale-daemon recovery across concurrent clients (flock on the file; released when the lock file handle is dropped).
- pid_
path - PID file path written by the daemon.
- read_
frame - Read one length-prefixed frame (4-byte BE u32 length + JSON bytes).
- recoverer_
lock_ path - Advisory lock file used to serialize RECOVERY (kill+respawn) attempts
across concurrent clients only — the daemon’s own boot sequence never
acquires this file (
lock_path/acquire_daemon_boot_guardis the boot-side lock). A recoverer holding this lock across dead-confirmation → kill → spawn (khive-mcp’skill_and_respawn) therefore can never deadlock against a peer daemon’s boot, unlike holding the shared boot lock for that whole span would. - register_
active_ phase - Register one occurrence of a named background phase as currently active.
Returns a guard: drop it (or let it fall out of scope) when the phase
ends. Best-effort process-wide gauge only, read by
comm.health— never load-bearing for correctness. - run_
daemon - Run the daemon: bind the socket, warm in the background, serve request frames until SIGTERM/SIGINT.
- run_
daemon_ with_ boot_ guard - Run the daemon using a startup lock acquired by the caller before
building
dispatcher, so a second process racing to boot (e.g. twokkernel mcp --daemonspawns before either has bound its socket) cannot run migrations/FTS DDL concurrently against the same database file.boot_guardis onlyNoneon non-unix targets, where there is no advisory boot lock to hold in the first place; every unix daemon-mode caller passesSome. - socket_
path - Unix socket path the daemon binds and clients connect to.
- track_
background_ task - Spawn a fire-and-forget background task that daemon shutdown’s
drain()waits for, instead of a baretokio::spawnthat a SIGTERM can abort mid-flight with no trace. Only the enqueue (an atomic increment) is synchronous on the caller’s path — the future itself still runs fully off-path, unawaited. The decrement happens viaBackgroundTaskGuard’sDrop, so a panic insidefutstill restores the count. - try_
acquire_ daemon_ boot_ guard_ until - Bounded, deadline-aware variant of
acquire_daemon_boot_guard: attempts the SAME boot/recovery lock (lock_path) but gives up atdeadlineinstead of blocking forever. For callers that need to detect “is a boot in progress right now” without risking an unbounded wait behind a wedged holder: e.g. khive-mcp’sconfirm_genuinely_deadre-probing rounds, whereDEAD_CONFIRM_ROUNDSmust bound elapsed time, not just probe count. - try_
acquire_ recoverer_ lock_ until - Bounded, deadline-aware acquisition of the recoverer-only lock
(
recoverer_lock_path). Seetry_acquire_daemon_boot_guard_untilfor the shared rationale — a second recoverer waiting for a peer’s dead confirmation/kill/spawn critical section must give up and report “uncertain” rather than block forever if that peer is itself wedged. - write_
frame - Write one length-prefixed frame.
Type Aliases§
- Daemon
Boot Guard - Guard returned by
acquire_daemon_boot_guard, held across cold-boot schema initialization (migrations + pack schema plans / FTS DDL) through daemon bind + pid-write.