Skip to main content

pitchfork_cli/supervisor/
state.rs

1//! State access layer for the supervisor
2//!
3//! All state getter/setter operations for daemons, shell directories, and notifications.
4
5use super::Supervisor;
6use crate::Result;
7use crate::daemon::Daemon;
8use crate::daemon::RunOptions;
9use crate::daemon_id::DaemonId;
10use crate::daemon_status::DaemonStatus;
11use crate::pitchfork_toml::CpuLimit;
12use crate::pitchfork_toml::CronRetrigger;
13use crate::pitchfork_toml::HealthCmd;
14use crate::pitchfork_toml::HealthHttp;
15use crate::pitchfork_toml::HealthPort;
16use crate::pitchfork_toml::MemoryLimit;
17use crate::pitchfork_toml::PitchforkToml;
18use crate::pitchfork_toml::PortConfig;
19use crate::pitchfork_toml::ReadyCmd;
20use crate::pitchfork_toml::ReadyHttp;
21use crate::pitchfork_toml::ReadyOutput;
22use crate::pitchfork_toml::ReadyPort;
23use crate::pitchfork_toml::Retry;
24use crate::pitchfork_toml::StopConfig;
25use crate::pitchfork_toml::WatchMode;
26use crate::procs::PROCS;
27use crate::state_file::DiskRecord;
28use indexmap::IndexMap;
29use std::collections::HashMap;
30use std::path::PathBuf;
31
32fn should_clean_daemon(
33    id: &DaemonId,
34    daemon: &Daemon,
35    namespaces: &[String],
36    daemons: &[DaemonId],
37) -> bool {
38    let namespace_matches =
39        namespaces.is_empty() || namespaces.iter().any(|ns| ns == id.namespace());
40    let daemon_matches = daemons.is_empty() || daemons.contains(id);
41    daemon.pid.is_none() && namespace_matches && daemon_matches
42}
43
44fn prune_candidate(
45    id: &DaemonId,
46    daemon: &Daemon,
47    namespaces: &[String],
48    daemons: &[DaemonId],
49) -> Option<(DaemonId, PathBuf)> {
50    should_clean_daemon(id, daemon, namespaces, daemons)
51        .then(|| daemon.dir.clone())
52        .flatten()
53        .map(|dir| (id.clone(), dir))
54}
55
56/// Options for upserting a daemon's state.
57///
58/// Use `UpsertDaemonOpts::builder(id)` to create, then set fields directly and call `.build()`.
59#[derive(Debug, Default)]
60pub(crate) struct UpsertDaemonOpts {
61    pub id: DaemonId,
62    pub pid: Option<u32>,
63    pub status: DaemonStatus,
64    pub shell_pid: Option<u32>,
65    pub dir: Option<PathBuf>,
66    pub cmd: Option<Vec<String>>,
67    pub run: Option<String>,
68    /// Start `cmd` directly, without a shell. `None` inherits the existing
69    /// record's value, like `oneshot`.
70    pub no_shell: Option<bool>,
71    /// `None` inherits the existing record's value; see
72    /// `Daemon::scheduled_from_config`.
73    pub scheduled_from_config: Option<bool>,
74    /// Stop the daemon when its directory is left. A start sets it from its
75    /// `RunOptions`, so removing `auto = ["stop"]` takes effect; `None`
76    /// inherits the existing record's value, like `oneshot`.
77    pub autostop: Option<bool>,
78    /// Run-to-completion task rather than a long-running service.
79    /// `None` inherits the existing record's value, so a status-only upsert
80    /// (stop, exit finalization) does not reclassify the daemon.
81    pub oneshot: Option<bool>,
82    /// A start sets it, `None` included, as it does `watch_base_dir`: an
83    /// ad-hoc `pitchfork run` of a former config cron daemon's id must not keep
84    /// that daemon's schedule, which also marks the record as made from
85    /// config. Every other upsert inherits it.
86    pub cron_schedule: Option<Option<String>>,
87    pub cron_retrigger: Option<CronRetrigger>,
88    pub cron_immediate: Option<bool>,
89    pub last_exit_success: Option<bool>,
90    pub retry: Option<Retry>,
91    pub retry_count: Option<u32>,
92    pub ready_delay: Option<u64>,
93    pub ready_output: Option<ReadyOutput>,
94    pub ready_http: Option<ReadyHttp>,
95    pub ready_port: Option<ReadyPort>,
96    /// See `Daemon::configured_ready_port`.
97    pub configured_ready_port: Option<ReadyPort>,
98    pub ready_cmd: Option<ReadyCmd>,
99    /// The ready checks above replace the record's: one left `None` is
100    /// cleared rather than inherited. See `RunOptions::replaces_ready_checks`.
101    pub replaces_ready_checks: bool,
102    pub health_cmd: Option<HealthCmd>,
103    pub health_http: Option<HealthHttp>,
104    pub health_port: Option<HealthPort>,
105    /// Port configuration
106    pub port: Option<PortConfig>,
107    /// Resolved ports actually used after auto-bump (may differ from expected).
108    /// `None` inherits the existing record's value; `Some` sets it explicitly,
109    /// including an empty vec, which clears ports left by a previous run.
110    pub resolved_port: Option<Vec<u16>>,
111    /// The first port the process is actually listening on (detected at runtime).
112    pub active_port: Option<u16>,
113    /// Optional stable slug alias for this daemon.
114    pub slug: Option<String>,
115    /// Whether to proxy this daemon (None = use global proxy.enable setting).
116    pub proxy: Option<bool>,
117    pub depends: Option<Vec<DaemonId>>,
118    pub env: Option<IndexMap<String, String>>,
119    pub watch: Option<Vec<String>>,
120    pub watch_mode: Option<WatchMode>,
121    /// The directory of the project whose config defined the daemon. A
122    /// start sets it, `None` included: an ad-hoc `pitchfork run` of a former
123    /// config daemon's id must not keep that daemon's directory, which marks
124    /// the record as made from config. Every other upsert inherits it.
125    pub watch_base_dir: Option<Option<PathBuf>>,
126    pub mise: Option<bool>,
127    /// `Some` replaces the saved template context for a deferred `run`, including
128    /// with `None` when a start no longer defers; `None` keeps what is saved.
129    pub deferred_template_context: Option<Option<String>>,
130    /// The project's mise binary. `None` keeps the record's, so a status-only
131    /// upsert (stop, exit finalization) does not lose it; a start sets it from
132    /// its `RunOptions`, so one removed from the project's settings is dropped.
133    pub mise_bin: Option<Option<PathBuf>>,
134    /// Unix user to run this daemon as
135    pub user: Option<String>,
136    /// Memory limit for the daemon process
137    pub memory_limit: Option<MemoryLimit>,
138    /// CPU usage limit as a percentage
139    pub cpu_limit: Option<CpuLimit>,
140    /// Unix signal to send for graceful shutdown
141    pub stop_signal: Option<StopConfig>,
142    /// Archive hook command invoked before retention prunes this daemon's logs.
143    pub archive_hook: Option<String>,
144    /// Log format for this daemon.
145    pub log_format: Option<String>,
146    /// Allocate a pseudo-terminal for the daemon process.
147    pub pty: Option<bool>,
148    /// True for config-only cron daemons auto-registered into state.
149    pub config_registered: bool,
150    /// A start that describes the daemon in full, so an unset configuration
151    /// field clears the existing record's value instead of inheriting it. An
152    /// ad-hoc run under a config daemon's id must not keep that daemon's
153    /// command, schedule or environment. See `RunOptions::replaces_saved_record`.
154    pub replaces_config: bool,
155    /// Idle-shutdown ownership. `None` inherits the existing record's, so a
156    /// status-only upsert (stop, exit finalization) keeps it; a start sets it
157    /// from its `RunOptions`, which is what makes an explicit start explicit.
158    pub proxy_idle_timeout_ms: Option<Option<u64>>,
159}
160
161/// Builder for UpsertDaemonOpts - ensures daemon ID is always provided.
162///
163/// # Example
164/// ```ignore
165/// let opts = UpsertDaemonOpts::builder(daemon_id)
166///     .set(|o| {
167///         o.pid = Some(pid);
168///         o.status = DaemonStatus::Running;
169///     })
170///     .build();
171/// ```
172#[derive(Debug)]
173pub(crate) struct UpsertDaemonOptsBuilder {
174    pub opts: UpsertDaemonOpts,
175}
176
177impl UpsertDaemonOpts {
178    /// Create a builder with the required daemon ID.
179    pub fn builder(id: DaemonId) -> UpsertDaemonOptsBuilder {
180        UpsertDaemonOptsBuilder {
181            opts: UpsertDaemonOpts {
182                id,
183                ..Default::default()
184            },
185        }
186    }
187
188    /// Build an `UpsertDaemonOptsBuilder` from `RunOptions`, mapping all
189    /// config-carried fields. Callers chain `.set()` to add runtime-specific
190    /// fields (pid, resolved_port, etc.) and then call `.build()`.
191    pub(crate) fn from_run_options(
192        opts: &RunOptions,
193        status: DaemonStatus,
194    ) -> UpsertDaemonOptsBuilder {
195        UpsertDaemonOpts::builder(opts.id.clone()).set(|o| {
196            o.replaces_config = opts.replaces_saved_record;
197            o.status = status;
198            o.shell_pid = opts.shell_pid;
199            o.dir = Some(opts.dir.0.clone());
200            o.cmd = Some(opts.cmd.clone());
201            o.run = opts.run.clone();
202            o.no_shell = Some(opts.no_shell);
203            // Only a client clears it; the supervisor's own starts (the
204            // schedule, retries, file watching) leave it as it was.
205            o.scheduled_from_config = opts.requested_by_client.then_some(false);
206            o.autostop = Some(opts.autostop);
207            o.oneshot = Some(opts.oneshot);
208            o.cron_schedule = Some(opts.cron_schedule.clone());
209            o.cron_retrigger = opts.cron_retrigger;
210            o.cron_immediate = opts.cron_immediate;
211            o.retry = Some(opts.retry);
212            o.retry_count = Some(opts.retry_count);
213            o.ready_delay = opts.ready_delay;
214            o.ready_output = opts.ready_output.clone();
215            o.ready_http = opts.ready_http.clone();
216            o.ready_port = opts.ready_port.clone();
217            o.configured_ready_port = opts.ready_port.clone();
218            o.ready_cmd = opts.ready_cmd.clone();
219            o.replaces_ready_checks = opts.replaces_ready_checks;
220            o.health_cmd = opts.health_cmd.clone();
221            o.health_http = opts.health_http.clone();
222            o.health_port = opts.health_port.clone();
223            o.port = opts.port.clone();
224            o.depends = Some(opts.depends.clone());
225            o.env = opts.env.clone();
226            o.watch = Some(opts.watch.clone());
227            o.watch_mode = Some(opts.watch_mode);
228            o.watch_base_dir = Some(opts.watch_base_dir.clone());
229            o.mise = opts.mise;
230            o.deferred_template_context = Some(opts.deferred_template_context.clone());
231            o.mise_bin = Some(opts.mise_bin.clone());
232            o.user = opts.user.clone();
233            o.memory_limit = opts.memory_limit;
234            o.cpu_limit = opts.cpu_limit;
235            o.stop_signal = opts.stop_signal;
236            o.pty = opts.pty;
237            o.archive_hook = opts.archive_hook.clone();
238            o.log_format = opts.log_format.clone();
239            o.proxy_idle_timeout_ms = Some(opts.proxy_idle_timeout_ms);
240        })
241    }
242}
243
244impl UpsertDaemonOptsBuilder {
245    /// Modify opts fields with a closure.
246    pub fn set<F: FnOnce(&mut UpsertDaemonOpts)>(mut self, f: F) -> Self {
247        f(&mut self.opts);
248        self
249    }
250
251    /// Build the UpsertDaemonOpts.
252    pub fn build(self) -> UpsertDaemonOpts {
253        self.opts
254    }
255}
256
257impl Supervisor {
258    /// Put this supervisor's record back into the state file if the file no
259    /// longer has it.
260    ///
261    /// The CLI decides whether a supervisor is running, and which process
262    /// `supervisor stop` signals, from this record. The supervisor only
263    /// writes the file when its own state changes, so if the file is replaced
264    /// or rewritten by something else the record could stay missing
265    /// indefinitely, leaving this supervisor running but unaccounted for.
266    /// Only the record is restored; the rest of the file is left as it is.
267    pub(crate) async fn restore_own_record(&self) {
268        if self
269            .shutting_down
270            .load(std::sync::atomic::Ordering::Acquire)
271        {
272            return;
273        }
274        let pitchfork_id = DaemonId::pitchfork();
275        let state = self.state_file.lock().await;
276        // Checked again under the lock: `close()` may have begun while this
277        // waited for it. (Its removal of the record happens under this lock
278        // too, so a restore can never outlive it; this just skips the work.)
279        if self
280            .shutting_down
281            .load(std::sync::atomic::Ordering::Acquire)
282        {
283            return;
284        }
285        let Some(own) = state
286            .daemons
287            .get(&pitchfork_id)
288            .filter(|d| d.pid.is_some())
289            .cloned()
290        else {
291            return;
292        };
293        let own_pid = own.pid.unwrap_or_default();
294        let path = state.path.clone();
295        // The state lock stays held across the file work, as it is for a
296        // flush, so the two cannot interleave.
297        let result = tokio::task::spawn_blocking(move || {
298            crate::state_file::StateFile::restore_daemon_in_file(&path, &own)
299        })
300        .await;
301        match result {
302            Ok(Ok(DiskRecord::Present)) => {}
303            Ok(Ok(DiskRecord::Restored)) => {
304                // Not flushed yet (e.g. a client connecting right after
305                // startup): the record was simply written early.
306                if state.is_dirty() {
307                    debug!("recorded this supervisor in the state file ahead of the next flush");
308                } else {
309                    warn!(
310                        "state file {} no longer recorded this supervisor (pid {own_pid}); restored it",
311                        state.path.display()
312                    );
313                }
314                // The file now differs from what this supervisor last wrote,
315                // so its next flush must not be skipped as unchanged.
316                state.forget_written_snapshot();
317            }
318            Ok(Ok(DiskRecord::Unparseable)) => {
319                warn!(
320                    "state file {} cannot be parsed; rewriting it from the supervisor's state",
321                    state.path.display()
322                );
323                state.force_next_write();
324            }
325            Ok(Err(e)) => warn!("failed to restore the supervisor record in the state file: {e}"),
326            Err(e) => warn!("failed to restore the supervisor record in the state file: {e}"),
327        }
328    }
329
330    /// Run options for starting `id` from its config, as `pitchfork start`
331    /// would build them: templates rendered and the top-level `[env]` merged.
332    ///
333    /// Other daemons' ports come from the state held here rather than the file
334    /// on disk, which can trail it — a daemon started a moment ago may not be
335    /// written out yet, and a template referring to its port would fail.
336    pub(crate) async fn run_options_from_config(
337        &self,
338        id: &DaemonId,
339        config: &crate::pitchfork_toml::PitchforkTomlDaemon,
340        pt: &PitchforkToml,
341    ) -> Result<RunOptions> {
342        let resolved_daemons: std::collections::HashMap<DaemonId, Vec<u16>> = {
343            let state = self.state_file.lock().await;
344            state
345                .daemons
346                .iter()
347                .filter(|(_, d)| !d.resolved_port.is_empty())
348                .map(|(id, d)| (id.clone(), d.resolved_port.clone()))
349                .collect()
350        };
351        // On a blocking worker, as for a client's start: building the context
352        // reads configuration and derives hostnames by walking the project's
353        // checkouts, which must not hold up the supervisor's other tasks.
354        let id = id.clone();
355        let mut config = config.clone();
356        let pt = pt.clone();
357        tokio::task::spawn_blocking(move || {
358            crate::ipc::batch::render_daemon_config_with(&id, &mut config, &pt, &resolved_daemons)?;
359            let cmd = config
360                .run
361                .argv()
362                .map_err(|e| miette::miette!("failed to parse command for daemon {id}: {e}"))?;
363            let mut opts = config.to_run_options(&id, cmd);
364            // A client's start resolves these from the daemon's project before
365            // it reaches the supervisor, which would otherwise use its own
366            // settings. Rendering assumed the project's, so launch has to as well.
367            let (mise, mise_bin) = crate::ipc::batch::project_mise_options(&config);
368            opts.mise = Some(mise);
369            opts.mise_bin = mise_bin;
370            // The project's default delay, which a client's start applies too,
371            // decides when a service without other ready checks is ready.
372            if crate::ipc::batch::should_inject_default_ready_delay(&opts) {
373                let project_dir =
374                    crate::ipc::batch::resolve_config_base_dir(config.path.as_deref());
375                let (settings, _) = crate::settings::Settings::resolve_from_dir(&project_dir);
376                opts.ready_delay = Some(
377                    settings
378                        .general_ready_delay_secs()
379                        .map_err(|e| miette::miette!("{e}"))?,
380                );
381            }
382            Ok(opts)
383        })
384        .await
385        .map_err(|e| miette::miette!("rendering daemon config panicked: {e}"))?
386    }
387
388    /// Upsert a daemon's state, merging with existing values
389    pub(crate) async fn upsert_daemon(&self, opts: UpsertDaemonOpts) -> Result<Daemon> {
390        info!(
391            "upserting daemon: {} pid: {} status: {}",
392            opts.id,
393            opts.pid.unwrap_or(0),
394            opts.status
395        );
396        let mut state_file = self.state_file.lock().await;
397        let existing = state_file.daemons.get(&opts.id);
398        // What configuration fields fall back to: nothing for a start, which
399        // sets them all; see `UpsertDaemonOpts::replaces_config`.
400        let configured = existing.filter(|_| !opts.replaces_config);
401        // What unset ready checks fall back to: also nothing when this start's
402        // ready checks replace the daemon's; see `replaces_ready_checks`.
403        let ready_base = configured.filter(|_| !opts.replaces_ready_checks);
404        let process_may_live = matches!(
405            opts.status,
406            DaemonStatus::Running | DaemonStatus::Waiting | DaemonStatus::Stopping
407        );
408        let daemon = Daemon {
409            id: opts.id.clone(),
410            // title/start_time identify the process for orphan cleanup after a
411            // supervisor crash. They are looked up from the process cache; if
412            // the cache has no entry (e.g. an upsert between refreshes) fall
413            // back to the existing values as long as the PID is unchanged, so
414            // the recorded identity is not wiped mid-lifetime.
415            title: opts.pid.and_then(|pid| {
416                PROCS.title(pid).or_else(|| {
417                    existing
418                        .filter(|d| d.pid == Some(pid))
419                        .and_then(|d| d.title.clone())
420                })
421            }),
422            start_time: opts.pid.and_then(|pid| {
423                PROCS.start_time(pid).or_else(|| {
424                    existing
425                        .filter(|d| d.pid == Some(pid))
426                        .and_then(|d| d.start_time)
427                })
428            }),
429            // Boot time is a system-wide constant, so it needs no cache
430            // fallback — it is recorded for any daemon with a live PID and
431            // dropped when the PID is cleared.
432            boot_time: opts.pid.map(|_| PROCS.boot_time()),
433            pid: opts.pid,
434            status: opts.status,
435            shell_pid: opts.shell_pid,
436            autostop: opts
437                .autostop
438                .unwrap_or_else(|| existing.is_some_and(|d| d.autostop)),
439            // A start carries the current config value (including a removed
440            // `oneshot = true`, which must reclassify the daemon); every other
441            // upsert leaves it None and inherits, so finalizing a completed
442            // oneshot's exit does not forget what it is.
443            oneshot: opts
444                .oneshot
445                .unwrap_or_else(|| existing.is_some_and(|d| d.oneshot)),
446            dir: opts.dir.or(configured.and_then(|d| d.dir.clone())),
447            cmd: opts.cmd.or(configured.and_then(|d| d.cmd.clone())),
448            // A start in the argv form has no command line, and must not
449            // inherit the one a shell-form run left behind.
450            run: if opts.no_shell == Some(true) {
451                None
452            } else {
453                opts.run.or(configured.and_then(|d| d.run.clone()))
454            },
455            no_shell: opts
456                .no_shell
457                .unwrap_or_else(|| existing.is_some_and(|d| d.no_shell)),
458            scheduled_from_config: opts
459                .scheduled_from_config
460                .unwrap_or_else(|| existing.is_some_and(|d| d.scheduled_from_config)),
461            cron_schedule: opts
462                .cron_schedule
463                .unwrap_or_else(|| configured.and_then(|d| d.cron_schedule.clone())),
464            cron_retrigger: opts
465                .cron_retrigger
466                .or(configured.and_then(|d| d.cron_retrigger)),
467            cron_immediate: opts
468                .cron_immediate
469                .or(configured.and_then(|d| d.cron_immediate)),
470            last_cron_triggered: existing.and_then(|d| d.last_cron_triggered),
471            last_cron_run: existing.and_then(|d| d.last_cron_run),
472            last_exit_success: opts
473                .last_exit_success
474                .or(existing.and_then(|d| d.last_exit_success)),
475            retry: opts
476                .retry
477                .unwrap_or_else(|| existing.map(|d| d.retry).unwrap_or_default()),
478            retry_count: opts
479                .retry_count
480                .unwrap_or(existing.map(|d| d.retry_count).unwrap_or(0)),
481            ready_delay: opts.ready_delay.or(ready_base.and_then(|d| d.ready_delay)),
482            ready_output: opts
483                .ready_output
484                .or(ready_base.and_then(|d| d.ready_output.clone())),
485            ready_http: opts
486                .ready_http
487                .or(ready_base.and_then(|d| d.ready_http.clone())),
488            ready_port: opts
489                .ready_port
490                .or(ready_base.and_then(|d| d.ready_port.clone())),
491            configured_ready_port: opts
492                .configured_ready_port
493                .or(ready_base.and_then(|d| d.configured_ready_port.clone())),
494            ready_cmd: opts
495                .ready_cmd
496                .or(ready_base.and_then(|d| d.ready_cmd.clone())),
497            health_cmd: opts
498                .health_cmd
499                .or(configured.and_then(|d| d.health_cmd.clone())),
500            health_http: opts
501                .health_http
502                .or(configured.and_then(|d| d.health_http.clone())),
503            health_port: opts
504                .health_port
505                .or(configured.and_then(|d| d.health_port.clone())),
506            port: opts
507                .port
508                .or_else(|| configured.and_then(|d| d.port.clone())),
509            resolved_port: match opts.resolved_port {
510                Some(ports) => ports,
511                None => existing
512                    .map(|d| d.resolved_port.clone())
513                    .unwrap_or_default(),
514            },
515            depends: opts
516                .depends
517                .unwrap_or_else(|| existing.map(|d| d.depends.clone()).unwrap_or_default()),
518            env: opts.env.or(configured.and_then(|d| d.env.clone())),
519            watch: opts
520                .watch
521                .unwrap_or_else(|| existing.map(|d| d.watch.clone()).unwrap_or_default()),
522            watch_mode: opts
523                .watch_mode
524                .unwrap_or_else(|| existing.map(|d| d.watch_mode).unwrap_or_default()),
525            watch_base_dir: opts
526                .watch_base_dir
527                .unwrap_or_else(|| configured.and_then(|d| d.watch_base_dir.clone())),
528            mise: opts.mise.or(configured.and_then(|d| d.mise)),
529            deferred_template_context: opts
530                .deferred_template_context
531                .unwrap_or_else(|| existing.and_then(|d| d.deferred_template_context.clone())),
532            mise_bin: opts
533                .mise_bin
534                .unwrap_or_else(|| configured.and_then(|d| d.mise_bin.clone())),
535            user: opts.user.or(configured.and_then(|d| d.user.clone())),
536            proxy: opts.proxy.or(existing.and_then(|d| d.proxy)),
537            // active_port is intentionally NOT inherited from the existing daemon.
538            // When a daemon restarts, the new process has not yet bound a port, so
539            // carrying over the old process's active_port would cause the proxy to
540            // route to a port that is no longer listening.  The port will be
541            // re-detected by detect_and_store_active_port once the new process is ready.
542            active_port: opts.active_port,
543            slug: opts.slug.or(existing.and_then(|d| d.slug.clone())),
544            memory_limit: opts
545                .memory_limit
546                .or(configured.and_then(|d| d.memory_limit)),
547            cpu_limit: opts.cpu_limit.or(configured.and_then(|d| d.cpu_limit)),
548            stop_signal: opts.stop_signal.or(configured.and_then(|d| d.stop_signal)),
549            archive_hook: opts
550                .archive_hook
551                .or(configured.and_then(|d| d.archive_hook.clone())),
552            log_format: opts
553                .log_format
554                .or(configured.and_then(|d| d.log_format.clone())),
555            pty: opts.pty.or(configured.and_then(|d| d.pty)),
556            config_registered: opts.config_registered,
557            // A new run starts without a status; the process reports its own.
558            // Status-only upserts (stop, error, retry bookkeeping) keep it, so
559            // a `done` or `error` report survives them.
560            // A different pid is a new run, wherever the record came from.
561            program_status: existing
562                .filter(|d| !opts.replaces_config && (opts.pid.is_none() || d.pid == opts.pid))
563                .and_then(|d| d.program_status.clone())
564                // Whatever path ends a run (monitored, adopted or unmonitored
565                // stop), the status it leaves is one that outlives the process.
566                .filter(|s| !s.state.ends_with_process() || process_may_live),
567            proxy_idle_timeout_ms: opts
568                .proxy_idle_timeout_ms
569                .unwrap_or_else(|| existing.and_then(|d| d.proxy_idle_timeout_ms)),
570        };
571        state_file.insert_daemon(&opts.id, daemon.clone());
572        Ok(daemon)
573    }
574
575    /// Enable a daemon (remove from disabled set)
576    pub async fn enable(&self, id: &DaemonId) -> Result<bool> {
577        info!("enabling daemon: {id}");
578        let config = PitchforkToml::all_merged_all_namespaces().await?;
579        let mut state_file = self.state_file.lock().await;
580        let exists = state_file.daemons.contains_key(id) || config.daemons.contains_key(id);
581        if !exists {
582            return Err(miette::miette!("daemon '{}' not found", id));
583        }
584        let result = state_file.enable_daemon(id);
585        Ok(result)
586    }
587
588    /// Disable a daemon (add to disabled set)
589    pub async fn disable(&self, id: &DaemonId) -> Result<bool> {
590        info!("disabling daemon: {id}");
591        let config = PitchforkToml::all_merged_all_namespaces().await?;
592        let not_found = || miette::miette!("daemon '{}' not found", id);
593        // Checked before `stop_lock`, whose entries are never removed, so an
594        // unknown ID does not leave one behind.
595        if !config.daemons.contains_key(id)
596            && !self.state_file.lock().await.daemons.contains_key(id)
597        {
598            return Err(not_found());
599        }
600        // Taken before the state file, in `run_inner`'s order, so a retry
601        // that has already checked the disabled set finishes its start first.
602        let lock = self.stop_lock(id).await;
603        let _guard = lock.lock().await;
604        let mut state_file = self.state_file.lock().await;
605        let exists = state_file.daemons.contains_key(id) || config.daemons.contains_key(id);
606        if !exists {
607            return Err(not_found());
608        }
609        let result = state_file.disable_daemon(id);
610        // A start sleeping out a retry backoff makes no further attempt, so
611        // it is woken now rather than when the backoff ends.
612        self.cancel_retrying(id);
613        Ok(result)
614    }
615
616    /// Get a daemon by ID
617    pub(crate) async fn get_daemon(&self, id: &DaemonId) -> Option<Daemon> {
618        self.state_file.lock().await.daemons.get(id).cloned()
619    }
620
621    /// Get all active daemons (those with PIDs, excluding pitchfork itself)
622    pub(crate) async fn active_daemons(&self) -> Vec<Daemon> {
623        let pitchfork_id = DaemonId::pitchfork();
624        self.state_file
625            .lock()
626            .await
627            .daemons
628            .values()
629            .filter(|d| d.pid.is_some() && d.id != pitchfork_id)
630            .cloned()
631            .collect()
632    }
633
634    /// Remove a daemon from state
635    pub(crate) async fn remove_daemon(&self, id: &DaemonId) -> Result<()> {
636        let mut state_file = self.state_file.lock().await;
637        state_file.remove_daemon(id);
638        Ok(())
639    }
640
641    /// Set the shell's working directory
642    pub(crate) async fn set_shell_dir(&self, shell_pid: u32, dir: PathBuf) -> Result<()> {
643        let mut state_file = self.state_file.lock().await;
644        state_file.set_shell_dir(shell_pid, dir);
645        Ok(())
646    }
647
648    /// Get the shell's working directory
649    pub(crate) async fn get_shell_dir(&self, shell_pid: u32) -> Option<PathBuf> {
650        self.state_file
651            .lock()
652            .await
653            .shell_dirs
654            .get(&shell_pid.to_string())
655            .cloned()
656    }
657
658    /// Remove a shell PID from tracking
659    #[cfg(unix)]
660    pub(crate) async fn remove_shell_pid(&self, shell_pid: u32) -> Result<()> {
661        let mut state_file = self.state_file.lock().await;
662        state_file.remove_shell_dir(shell_pid);
663        Ok(())
664    }
665
666    /// Get all directories with their associated shell PIDs
667    pub(crate) async fn get_dirs_with_shell_pids(&self) -> HashMap<PathBuf, Vec<u32>> {
668        self.state_file.lock().await.shell_dirs.iter().fold(
669            HashMap::new(),
670            |mut acc, (pid, dir)| {
671                if let Ok(pid) = pid.parse() {
672                    acc.entry(dir.clone()).or_default().push(pid);
673                }
674                acc
675            },
676        )
677    }
678
679    /// Get pending notifications and clear the queue
680    pub(crate) async fn get_notifications(&self) -> Vec<(log::LevelFilter, String)> {
681        self.pending_notifications.lock().await.drain(..).collect()
682    }
683
684    /// Clean up daemons that have no PID
685    pub(crate) async fn clean(&self) -> Result<()> {
686        self.clean_filtered(&[], &[], false).await?;
687        Ok(())
688    }
689
690    /// Clean up PID-less daemon registrations matching all supplied filters.
691    pub(crate) async fn clean_filtered(
692        &self,
693        namespaces: &[String],
694        daemons: &[DaemonId],
695        prune: bool,
696    ) -> Result<u64> {
697        if prune {
698            let candidates: Vec<(DaemonId, PathBuf)> = {
699                let state_file = self.state_file.lock().await;
700                state_file
701                    .daemons
702                    .iter()
703                    .filter_map(|(id, daemon)| prune_candidate(id, daemon, namespaces, daemons))
704                    .collect()
705            };
706
707            let mut missing = HashMap::new();
708            for (id, dir) in candidates {
709                match tokio::fs::try_exists(&dir).await {
710                    Ok(false) => {
711                        missing.insert(id, dir);
712                    }
713                    Ok(true) => {}
714                    // Unknown is not gone: keep the record, prune the rest.
715                    Err(e) => warn!("cannot check {} to prune {id}: {e}", dir.display()),
716                }
717            }
718
719            let mut removed = 0;
720            let mut state_file = self.state_file.lock().await;
721            state_file.retain_daemons(|id, daemon| {
722                let remove = should_clean_daemon(id, daemon, namespaces, daemons)
723                    && missing
724                        .get(id)
725                        .is_some_and(|missing_dir| daemon.dir.as_ref() == Some(missing_dir));
726                removed += u64::from(remove);
727                !remove
728            });
729            return Ok(removed);
730        }
731
732        let mut removed = 0;
733        let mut state_file = self.state_file.lock().await;
734        state_file.retain_daemons(|id, daemon| {
735            let remove = should_clean_daemon(id, daemon, namespaces, daemons);
736            removed += u64::from(remove);
737            !remove
738        });
739        Ok(removed)
740    }
741
742    /// Return the union of active directories from shell tracking and project
743    /// sessions. These are the directories that should keep auto-stop daemons
744    /// alive.
745    pub(crate) async fn get_active_directories(&self) -> Vec<PathBuf> {
746        self.state_file.lock().await.active_directories()
747    }
748
749    /// Collect all project sessions as `(pid, dir, liveness_title)`. Every
750    /// project session carries a host PID in its key, so every session is a
751    /// liveness session. Used by the refresh loop to clean up stale sessions.
752    pub(crate) async fn get_liveness_sessions(&self) -> Vec<(u32, PathBuf, Option<String>)> {
753        self.state_file
754            .lock()
755            .await
756            .iter_project_sessions()
757            .into_iter()
758            .filter_map(|(pid_str, dir, session)| {
759                pid_str
760                    .parse()
761                    .ok()
762                    .map(|pid| (pid, dir.clone(), session.liveness_title.clone()))
763            })
764            .collect()
765    }
766
767    /// Build a snapshot of all project sessions with live liveness status
768    /// filled in from `PROCS`. Used to answer `GetProjectSessions` IPC
769    /// requests.
770    pub(crate) async fn get_project_sessions_info(&self) -> Vec<crate::ipc::ProjectSessionInfo> {
771        let sessions: Vec<(u32, PathBuf, Option<String>)> = self
772            .state_file
773            .lock()
774            .await
775            .iter_project_sessions()
776            .into_iter()
777            .filter_map(|(pid_str, dir, session)| {
778                pid_str
779                    .parse()
780                    .ok()
781                    .map(|pid| (pid, dir.clone(), session.liveness_title.clone()))
782            })
783            .collect();
784        let pids: Vec<u32> = sessions.iter().map(|(pid, _, _)| *pid).collect();
785        if !pids.is_empty() {
786            PROCS.refresh_pids(&pids);
787        }
788        sessions
789            .into_iter()
790            .map(
791                |(pid, directory, liveness_title)| crate::ipc::ProjectSessionInfo {
792                    pid,
793                    directory,
794                    liveness_title,
795                    alive: PROCS.is_running(pid),
796                    current_title: PROCS.title(pid),
797                },
798            )
799            .collect()
800    }
801
802    /// Atomically enter (or replace) a project session for `(pid, dir)`.
803    /// Returns the previous session, if any, so the caller can evaluate the
804    /// previous entry for autostop.
805    pub(crate) async fn enter_project_session(
806        &self,
807        pid: u32,
808        dir: PathBuf,
809    ) -> Result<Option<crate::state_file::ProjectSession>> {
810        if pid == 0 {
811            return Err(miette::miette!("invalid host PID 0"));
812        }
813        if pid > i32::MAX as u32 {
814            return Err(miette::miette!("host PID {pid} exceeds i32::MAX"));
815        }
816        PROCS.refresh_pids(&[pid]);
817        let liveness_title = PROCS.title(pid);
818        // On Unix, reject dead host PIDs up front so we don't register a
819        // session the refresh loop would immediately evict. On Windows, Git
820        // Bash `$$` is a Cygwin-internal PID invisible to sysinfo, so the
821        // liveness check is skipped and sessions rely on explicit
822        // `project leave`.
823        #[cfg(unix)]
824        if !PROCS.is_running(pid) {
825            return Err(miette::miette!("host PID {pid} is not running"));
826        }
827        let mut state_file = self.state_file.lock().await;
828        let previous = state_file.set_project_session(
829            pid,
830            dir,
831            crate::state_file::ProjectSession { liveness_title },
832        );
833        Ok(previous)
834    }
835
836    /// Atomically remove a project session for `(pid, dir)`. Returns the
837    /// directory of the removed session so the caller can evaluate it for
838    /// autostop.
839    pub(crate) async fn leave_project_session(
840        &self,
841        pid: u32,
842        dir: &std::path::Path,
843    ) -> Result<Option<PathBuf>> {
844        let mut state_file = self.state_file.lock().await;
845        if state_file.remove_project_session(pid, dir).is_some() {
846            Ok(Some(dir.to_path_buf()))
847        } else {
848            Ok(None)
849        }
850    }
851}
852
853#[cfg(test)]
854mod tests {
855    use super::*;
856
857    fn daemon(id: &DaemonId, pid: Option<u32>, dir: Option<PathBuf>) -> Daemon {
858        Daemon {
859            id: id.clone(),
860            pid,
861            dir,
862            ..Daemon::default()
863        }
864    }
865
866    #[test]
867    fn clean_filters_intersect_and_preserve_running_daemons() {
868        let api = DaemonId::new("project-a", "api");
869        let worker = DaemonId::new("project-a", "worker");
870        let namespaces = vec!["project-a".to_string()];
871        let daemons = vec![api.clone()];
872
873        assert!(should_clean_daemon(
874            &api,
875            &daemon(&api, None, None),
876            &namespaces,
877            &daemons,
878        ));
879        assert!(!should_clean_daemon(
880            &worker,
881            &daemon(&worker, None, None),
882            &namespaces,
883            &daemons,
884        ));
885        assert!(!should_clean_daemon(
886            &api,
887            &daemon(&api, Some(42), None),
888            &namespaces,
889            &daemons,
890        ));
891    }
892
893    #[test]
894    fn prune_candidates_require_a_recorded_directory() {
895        let id = DaemonId::new("project-a", "api");
896        assert!(prune_candidate(&id, &daemon(&id, None, None), &[], &[]).is_none());
897        assert_eq!(
898            prune_candidate(
899                &id,
900                &daemon(&id, None, Some(PathBuf::from("missing"))),
901                &[],
902                &[]
903            ),
904            Some((id, PathBuf::from("missing")))
905        );
906    }
907}