omni_dev/daemon.rs
1//! The extensible omni-dev daemon: a long-lived supervisor that hosts pluggable
2//! [`DaemonService`](service::DaemonService)s over a local Unix-domain control
3//! socket.
4//!
5//! The daemon owns **lifecycle, single-instance supervision, status
6//! aggregation, and (on macOS) the menu-bar shell**. Each service wraps its own
7//! work and exposes status/control; the browser bridge is the first such
8//! service (#987). The control socket is a private operator/tray channel — it
9//! does **not** carry any service's own data plane (e.g. the bridge keeps its
10//! loopback-TCP planes per ADR-0036). See ADR-0039.
11//!
12//! Process model:
13//! - `daemon run` *becomes* the daemon ([`server::run`]), blocking until a
14//! signal or a built-in `shutdown` op.
15//! - `daemon start` launches it in the background (a launchd LaunchAgent on
16//! macOS, a systemd user unit on Linux); `stop` / `restart` / `status` are thin
17//! [`client::DaemonClient`]s.
18
19// The control plane is a Unix-domain socket (`UnixListener`/`UnixStream`), so the
20// daemon runtime is Unix-only and gated `#[cfg(unix)]`; running the daemon on
21// Windows is future work (#1041). `paths` stays cross-platform because the
22// request log and the browser thin-client token discovery depend on it.
23pub mod paths;
24
25#[cfg(unix)]
26pub mod client;
27#[cfg(unix)]
28pub mod lifecycle;
29#[cfg(unix)]
30pub mod protocol;
31#[cfg(unix)]
32pub mod registry;
33#[cfg(unix)]
34pub mod server;
35#[cfg(unix)]
36pub mod service;
37#[cfg(unix)]
38pub mod services;
39#[cfg(unix)]
40pub mod single_instance;
41
42#[cfg(target_os = "macos")]
43pub mod launchd;
44
45#[cfg(target_os = "linux")]
46pub mod systemd;
47
48#[cfg(all(target_os = "macos", feature = "menu-bar"))]
49pub mod tray;
50
51#[cfg(unix)]
52use std::path::Path;
53#[cfg(unix)]
54use std::path::PathBuf;
55#[cfg(unix)]
56use std::sync::Arc;
57
58#[cfg(unix)]
59use anyhow::Result;
60
61#[cfg(unix)]
62use crate::browser::BridgeConfig;
63#[cfg(unix)]
64use crate::snowflake::SnowflakeEngineConfig;
65#[cfg(unix)]
66use registry::ServiceRegistry;
67#[cfg(unix)]
68use server::DaemonOptions;
69#[cfg(unix)]
70use services::bridge::BridgeService;
71#[cfg(unix)]
72use services::snowflake::SnowflakeService;
73#[cfg(unix)]
74use services::worktrees::WorktreesService;
75
76/// Everything `daemon run` needs to start the daemon, resolved from the CLI.
77///
78/// Shared by the headless path ([`run_headless`]) and the macOS menu-bar path
79/// (`tray::run`) so both start an identical daemon. The latter is a plain code
80/// span, not an intra-doc link, because the `tray` module is feature- and
81/// target-gated and absent from the docs build.
82#[cfg(unix)]
83#[derive(Debug, Clone)]
84pub struct DaemonRunConfig {
85 /// Control-socket path (also the single-instance lock).
86 pub socket_path: PathBuf,
87 /// Browser-bridge configuration (ports, allow-origin, limits).
88 pub bridge_config: BridgeConfig,
89 /// Optional file the bridge session token is read from instead of generated.
90 pub bridge_token_file: Option<PathBuf>,
91 /// Where the resolved bridge token is persisted (`0600`) for thin clients.
92 pub bridge_token_path: PathBuf,
93}
94
95/// Builds the daemon's default service registry: starts the browser bridge on
96/// its loopback-TCP planes and registers it alongside the Snowflake query
97/// service and the cross-window worktrees registry.
98///
99/// `bridge_token_file` overrides token generation; `bridge_token_path` is where
100/// the resolved token is persisted (`0600`) for thin-client discovery. The
101/// Snowflake service is registered cheaply (no eager auth or I/O); its sessions
102/// are authenticated lazily on first query. The worktrees service is likewise
103/// cheap (in-memory only); it fills as VS Code windows register.
104#[cfg(unix)]
105pub async fn build_default_registry(
106 bridge_config: BridgeConfig,
107 bridge_token_file: Option<&Path>,
108 bridge_token_path: PathBuf,
109) -> Result<ServiceRegistry> {
110 let mut registry = ServiceRegistry::new();
111 let bridge = BridgeService::start(bridge_config, bridge_token_file, bridge_token_path).await?;
112 registry.register(Arc::new(bridge));
113 let snowflake = SnowflakeService::new(SnowflakeEngineConfig::from_env_and_settings()?);
114 registry.register(Arc::new(snowflake));
115 registry.register(Arc::new(WorktreesService::new()));
116 Ok(registry)
117}
118
119/// Runs the daemon headlessly (no tray).
120///
121/// Builds the registry and serves until a signal or `daemon stop`. The default
122/// `daemon run` path on every platform, and the only path when the `menu-bar`
123/// feature is off.
124#[cfg(unix)]
125pub async fn run_headless(cfg: DaemonRunConfig) -> Result<()> {
126 let registry = build_default_registry(
127 cfg.bridge_config,
128 cfg.bridge_token_file.as_deref(),
129 cfg.bridge_token_path,
130 )
131 .await?;
132 server::run(
133 registry,
134 DaemonOptions {
135 socket_path: cfg.socket_path,
136 },
137 )
138 .await
139}