Skip to main content

pitchfork_cli/ipc/
mod.rs

1use crate::Result;
2use crate::daemon::{Daemon, RunOptions};
3use crate::daemon_id::DaemonId;
4use crate::env;
5use interprocess::local_socket::Name;
6#[cfg(unix)]
7use interprocess::local_socket::{GenericFilePath, ToFsName};
8#[cfg(windows)]
9use interprocess::local_socket::{GenericNamespaced, ToNsName};
10use miette::{Context, IntoDiagnostic};
11use std::path::PathBuf;
12
13pub(crate) mod batch;
14pub(crate) mod client;
15pub(crate) mod server;
16
17// #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, strum::Display, strum::EnumIs)]
18// pub enum IpcMessage {
19//     Connect(String),
20//     ConnectOK,
21//     Run(String, Vec<String>),
22//     Stop(String),
23//     DaemonAlreadyRunning(String),
24//     DaemonAlreadyStopped(String),
25//     DaemonStart(Daemon),
26//     DaemonStop { name: String },
27//     DaemonFailed { name: String, error: String },
28//     Response(String),
29// }
30
31#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, strum::Display, strum::EnumIs)]
32#[allow(clippy::large_enum_variant)]
33pub enum IpcRequest {
34    Connect,
35    /// Versioned connect handshake (v2): client sends its version so the supervisor can
36    /// detect mismatches. Kept as a separate variant so the wire format of `Connect`
37    /// (unit variant) stays unchanged for backward compatibility with older supervisors.
38    ConnectV2 {
39        version: String,
40    },
41    Clean,
42    Stop {
43        id: DaemonId,
44    },
45    GetActiveDaemons,
46    GetDisabledDaemons,
47    Run(RunOptions),
48    Enable {
49        id: DaemonId,
50    },
51    Disable {
52        id: DaemonId,
53    },
54    UpdateShellDir {
55        shell_pid: u32,
56        dir: PathBuf,
57    },
58    GetNotifications,
59    /// Notify the supervisor that the slug registry has changed (e.g. `proxy add/remove`).
60    /// The supervisor should re-read slugs and update mDNS records accordingly.
61    SyncMdns,
62    /// Notify the supervisor that settings have changed.
63    /// The supervisor should reload settings from config files.
64    ReloadConfig,
65    /// Enter or replace a project session for a host PID in a directory.
66    ProjectEnter {
67        pid: u32,
68        dir: PathBuf,
69    },
70    /// Leave a project session for a host PID in a directory.
71    ProjectLeave {
72        pid: u32,
73        dir: PathBuf,
74    },
75    /// List all tracked project sessions with live liveness status filled in
76    /// by the supervisor.
77    GetProjectSessions,
78    /// A daemon's log sink reporting a line the supervisor needs to act on:
79    /// one matching the readiness pattern, one that should fire the
80    /// `on_output` hook, or both.
81    ///
82    /// The sink owns the daemon's output stream, so it does the matching and
83    /// tells the supervisor rather than the other way around. Sent by
84    /// `pitchfork log-sink`, not by any user-facing command.
85    SinkOutputLine {
86        id: DaemonId,
87        /// Identifies the start attempt this sink belongs to, so a report from
88        /// a sink still draining a previous attempt cannot satisfy a retry.
89        token: u64,
90        /// Whether this line passed the `on_output` hook's filter and debounce.
91        /// A line reported only because it matched the readiness pattern must
92        /// not fire a hook that filters for something else.
93        fires_hook: bool,
94        line: String,
95    },
96    /// Ask the supervisor for the URL of the web UI, if it is running.
97    /// Reflects the actual bound address, not static config.
98    GetWebUrl,
99    /// Remove stopped daemon registrations matching the supplied filters.
100    /// Appended to preserve the wire indexes of existing variants.
101    CleanFiltered {
102        namespaces: Vec<String>,
103        daemons: Vec<DaemonId>,
104        prune: bool,
105    },
106    /// Invalid request (failed to deserialize)
107    #[serde(skip)]
108    Invalid {
109        error: String,
110    },
111}
112
113/// A snapshot of a single project session, returned by `GetProjectSessions`.
114///
115/// `liveness_title` is the title recorded at enter time. `alive` and
116/// `current_title` are filled in by the supervisor from its `PROCS` singleton
117/// so the client does not need process introspection.
118#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
119pub struct ProjectSessionInfo {
120    pub pid: u32,
121    pub directory: PathBuf,
122    #[serde(skip_serializing_if = "Option::is_none", default)]
123    pub liveness_title: Option<String>,
124    pub alive: bool,
125    #[serde(skip_serializing_if = "Option::is_none", default)]
126    pub current_title: Option<String>,
127}
128
129#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, strum::Display, strum::EnumIs)]
130pub enum IpcResponse {
131    Ok,
132    /// Successful connect handshake, includes supervisor version for mismatch detection
133    ConnectOk {
134        version: String,
135    },
136    Yes,
137    No,
138    Error(String),
139    Notifications(Vec<(log::LevelFilter, String)>),
140    ActiveDaemons(Vec<Daemon>),
141    DisabledDaemons(Vec<DaemonId>),
142    DaemonAlreadyRunning,
143    DaemonStart {
144        daemon: Daemon,
145    },
146    DaemonFailed {
147        error: String,
148    },
149    /// Port conflict detected with detailed process information
150    PortConflict {
151        port: u16,
152        process: String,
153        pid: u32,
154    },
155    /// No available ports found after exhausting auto-bump attempts
156    NoAvailablePort {
157        start_port: u16,
158        attempts: u32,
159    },
160    DaemonReady {
161        daemon: Daemon,
162    },
163    DaemonFailedWithCode {
164        exit_code: Option<i32>,
165    },
166    /// Process was not running but had a PID record (unexpected exit)
167    DaemonWasNotRunning,
168    /// mDNS sync completed (or was a no-op if LAN mode is disabled)
169    MdnsSynced,
170    /// Settings reloaded from config files
171    ConfigReloaded,
172    /// URL of the running web UI, or `None` if the web UI is not running
173    WebUrl {
174        url: Option<String>,
175    },
176    /// Failed to kill the process (still running)
177    DaemonStopFailed {
178        error: String,
179    },
180    /// Daemon exists but is not running (no PID)
181    DaemonNotRunning,
182    DaemonNotFound,
183    /// Snapshot of all project sessions (response to `GetProjectSessions`).
184    ProjectSessions(Vec<ProjectSessionInfo>),
185    /// Number of daemon registrations removed by `CleanFiltered`.
186    Cleaned {
187        count: u64,
188    },
189}
190fn fs_name(name: &str) -> Result<Name<'_>> {
191    // Unix: use a filesystem path for the AF_UNIX socket.
192    #[cfg(unix)]
193    {
194        let path = env::IPC_SOCK_DIR.join(name).with_extension("sock");
195        let fs_name = path.to_fs_name::<GenericFilePath>().into_diagnostic()?;
196        Ok(fs_name)
197    }
198    // Windows: named pipes use a flat namespace (\\.\pipe\<name>) that
199    // cannot contain path separators. Derive a unique pipe name from the
200    // state directory to preserve test isolation when multiple supervisors
201    // run concurrently with different PITCHFORK_STATE_DIR values.
202    //
203    // Use a hash of the state directory path rather than character replacement
204    // to guarantee injectivity: `C:\a.b` and `C:\a\b` would both flatten to
205    // `C--a-b` with the old approach, causing pipe name collisions.
206    #[cfg(windows)]
207    {
208        let state_dir = env::PITCHFORK_STATE_DIR.to_string_lossy();
209        // FNV-1a hash: deterministic, stable across Rust versions.
210        // DefaultHasher's algorithm is not guaranteed stable, which would
211        // break IPC if the CLI and supervisor were ever compiled with
212        // different toolchains.
213        let mut hash: u64 = 0xcbf29ce484222325;
214        for byte in state_dir.bytes() {
215            hash ^= byte as u64;
216            hash = hash.wrapping_mul(0x100000001b3);
217        }
218        let pipe_name = format!("pitchfork-{hash:016x}-{name}");
219        Ok(pipe_name
220            .to_ns_name::<GenericNamespaced>()
221            .into_diagnostic()?)
222    }
223}
224
225fn serialize<T: serde::Serialize>(msg: &T) -> Result<Vec<u8>> {
226    if *env::IPC_JSON {
227        serde_json::to_vec(msg)
228            .into_diagnostic()
229            .wrap_err("failed to serialize IPC message as JSON")
230    } else {
231        rmp_serde::to_vec(msg)
232            .into_diagnostic()
233            .wrap_err("failed to serialize IPC message as MessagePack")
234    }
235}
236
237fn deserialize<T: serde::de::DeserializeOwned>(bytes: &[u8]) -> Result<T> {
238    let mut bytes = bytes.to_vec();
239    bytes.pop();
240    let preview = std::str::from_utf8(&bytes).unwrap_or("<binary>");
241    trace!("msg: {preview:?}");
242    if *env::IPC_JSON {
243        serde_json::from_slice(&bytes)
244            .into_diagnostic()
245            .wrap_err("failed to deserialize IPC JSON response")
246    } else {
247        rmp_serde::from_slice(&bytes)
248            .into_diagnostic()
249            .wrap_err("failed to deserialize IPC MessagePack response")
250    }
251}
252
253#[cfg(test)]
254mod tests {
255    use super::*;
256
257    #[test]
258    fn filtered_clean_ipc_round_trips() {
259        let request = IpcRequest::CleanFiltered {
260            namespaces: vec!["worktree".to_string()],
261            daemons: vec![DaemonId::new("worktree", "api")],
262            prune: true,
263        };
264        let mut bytes = serialize(&request).unwrap();
265        bytes.push(b'\n');
266        let decoded: IpcRequest = deserialize(&bytes).unwrap();
267        match decoded {
268            IpcRequest::CleanFiltered {
269                namespaces,
270                daemons,
271                prune,
272            } => {
273                assert_eq!(namespaces, ["worktree"]);
274                assert_eq!(daemons, [DaemonId::new("worktree", "api")]);
275                assert!(prune);
276            }
277            other => panic!("unexpected request: {other:?}"),
278        }
279
280        let mut bytes = serialize(&IpcResponse::Cleaned { count: 3 }).unwrap();
281        bytes.push(b'\n');
282        let decoded: IpcResponse = deserialize(&bytes).unwrap();
283        assert!(matches!(decoded, IpcResponse::Cleaned { count: 3 }));
284    }
285}