Skip to main content

agentmux_srv_core/backend/
teardown_hooks.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! What the controller code asks of the agent teardown.
5//!
6//! The watchdog, the controller replace and the tab close below `sagas/` each end an agent's processes
7//! through the one teardown path (`sagas/agent_teardown.rs`, which a structural test pins as the only place
8//! that stops them). This module is the seam: `backend/` names the [`TeardownHooks`] it needs, and the sagas
9//! register them once at startup ([`install`]), so `backend/` stops naming `sagas` (Area54 #1350).
10
11use std::sync::{Arc, OnceLock};
12
13use crate::backend::blockcontroller::Controller;
14
15/// The teardown's entries for callers that have no `AppState`.
16pub trait TeardownHooks: Send + Sync {
17    /// `controller.stop()` and nothing else, as the watchdog and the pane's `AgentStop` ask. Errors with
18    /// `NOT_RUNNING` when the block has no controller.
19    fn stop_now(&self, block_id: &str, graceful: bool) -> Result<(), String>;
20
21    /// Stop the old controller's own CLI process only, for a controller about to be replaced.
22    fn replace_now(&self, ctrl: &dyn Controller) -> Result<(), String>;
23
24    /// Drop a block's controller and process tracker at once, with no graceful phase.
25    fn discard(&self, block_id: &str);
26}
27
28static HOOKS: OnceLock<Arc<dyn TeardownHooks>> = OnceLock::new();
29
30/// Register the teardown's entries. Later calls change nothing.
31pub fn install(hooks: Arc<dyn TeardownHooks>) {
32    let _ = HOOKS.set(hooks);
33}
34
35/// The registered entries, or ones that stop and drop nothing. The sagas register them at the start of
36/// startup, before any controller runs, so [`NotReady`] is only what a caller reads if that registration is
37/// somehow lost. Under test, [`PlainEntries`] stand in until something registers.
38pub(crate) fn hooks() -> Arc<dyn TeardownHooks> {
39    if let Some(h) = HOOKS.get() {
40        return h.clone();
41    }
42    #[cfg(any(test, feature = "test-support"))]
43    {
44        Arc::new(PlainEntries)
45    }
46    #[cfg(not(any(test, feature = "test-support")))]
47    {
48        Arc::new(NotReady)
49    }
50}
51
52struct NotReady;
53
54impl TeardownHooks for NotReady {
55    fn stop_now(&self, block_id: &str, _: bool) -> Result<(), String> {
56        Err(format!("NOT_RUNNING: AgentMux isn't ready to stop block {block_id} yet"))
57    }
58    fn replace_now(&self, _: &dyn Controller) -> Result<(), String> {
59        Err("AgentMux isn't ready to replace a controller yet".to_string())
60    }
61    fn discard(&self, _: &str) {}
62}
63
64/// What a controller test gets when nothing registered: a replace stops the old controller's own CLI process
65/// the way the agent teardown's replace does, and nothing else is stopped or dropped.
66#[cfg(any(test, feature = "test-support"))]
67struct PlainEntries;
68
69#[cfg(any(test, feature = "test-support"))]
70impl TeardownHooks for PlainEntries {
71    fn stop_now(&self, block_id: &str, _: bool) -> Result<(), String> {
72        Err(format!("NOT_RUNNING: no controller for block {block_id}"))
73    }
74    fn replace_now(&self, ctrl: &dyn Controller) -> Result<(), String> {
75        ctrl.stop_for_replace(crate::backend::blockcontroller::STATUS_DONE)
76    }
77    fn discard(&self, _: &str) {}
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn a_lost_registration_stops_and_drops_nothing_and_says_so() {
86        let not_ready: &dyn TeardownHooks = &NotReady;
87        let err = not_ready.stop_now("blk-hook", true).unwrap_err();
88        assert!(err.starts_with("NOT_RUNNING"), "{err}");
89        not_ready.discard("blk-hook");
90    }
91
92    #[test]
93    fn under_test_plain_entries_stand_in_until_something_registers() {
94        let err = hooks().stop_now("no-such-block-hook", false).unwrap_err();
95        assert!(err.starts_with("NOT_RUNNING: no controller for block no-such-block-hook"), "{err}");
96        hooks().discard("no-such-block-hook");
97    }
98}