Skip to main content

Module daemon

Module daemon 

Source

Functions§

cleanup_daemon_files
Cleanup PID file and IPC endpoint on shutdown.
daemon_addr
daemon_pid_path
daemon_status
init_foreground_daemon
Initialize the foreground-daemon process. Commits the XDG layout pin (and drains a residual ~/.lean-ctx) before the daemon writes anything, so this long-running, possibly launchd/systemd-autostarted writer can never re-collapse config/data/state/cache onto a stray legacy dir (GL #623). The MCP server pins on its own start; the daemon is the other independent entry point (e.g. serve --_foreground-daemon), so it must heal too. heal() is idempotent and cheap — a no-op once pinned and when no residual dir exists.
is_daemon_running
Fast check on the hot path (every MCP request): the recorded PID is alive and, when the daemon recorded its identity, still that process. A PID file of an earlier release has no identity; verified_daemon also checks the command line.
is_foreground_daemon
Whether this process is the long-lived foreground daemon.
mark_foreground_daemon
Mark this process as the foreground daemon. Called once at daemon init.
owned_service_pids
PIDs of the background services this instance (data dir) recorded: the daemon PID file, the proxy that answers /health on the configured port, and the Cockpit launch record. Nothing else is ever a stop candidate — MCP stdio servers, lean-ctx -c runners, hooks and other instances’ services have no record here (#2176).
read_daemon_pid
read_daemon_record
The recorded daemon: the PID from daemon.pid and, when the identity file names the same PID, the identity recorded at start. A PID file of an earlier release has no identity; it is gated by command line only.
restore_stopped_proxy
Start a proxy that restart/config apply stopped again when no autostart service brought it back (#2176 review #4): detached, with the arguments it ran with, by the installed binary. None when there was nothing to restore; otherwise the PID or the spawn error.
start_daemon
start_daemon_with_binary
Source installs launch the verified installed copy, preserving the caller’s CWD.
stop_daemon
stop_owned_services
Stop this instance’s recorded background services that sweep includes (see owned_service_pids); every PID is gated again by command line, user and process identity in ipc::process::stop_services.
unrecorded_services
lean-ctx background services of this user that this instance has no record of. Listed by stop, uninstall and doctor, never stopped (owner decision 02.11.).
unrecorded_services_warning
The two lines stop/uninstall/doctor print for unrecorded_services — what still runs, and how to stop it — or None.
verified_daemon
The PID of the recorded daemon when it really is this user’s lean-ctx daemon: command line serve --_foreground-daemon, lean-ctx executable, same user, and the recorded identity where there is one. Slower than is_daemon_running (reads the command line); for doctor and stop.