Skip to main content

agentmux_srv_core/backend/reactive/
controller_wait.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! An agent whose pane is open but whose controller is not there yet.
5//!
6//! A pane registers its agent as it mounts; its controller is made by the
7//! resync that follows. When AgentMux starts with panes to resume, that gap
8//! is seconds long, for every agent at once. A message for such an agent
9//! can't be delivered (the sender answers [`NO_CONTROLLER`]), and that is not
10//! a refusal: the agent is starting. So what is held for it is not tried
11//! ([`awaits_controller`]), a failure that slips through is not counted
12//! against the message ([`is_no_controller`]), and the controller registering
13//! wakes whatever waits ([`controller_registered`]).
14
15use std::sync::{Arc, OnceLock};
16
17/// How the delivery path says a block has no controller (`bootstrap::delivery`).
18pub const NO_CONTROLLER: &str = "no controller for block";
19
20type HasController = Arc<dyn Fn(&str) -> bool + Send + Sync>;
21
22static HAS_CONTROLLER: OnceLock<HasController> = OnceLock::new();
23
24/// Say how to ask whether a block has a controller. Set once, when srv
25/// starts; until then every block counts as having one.
26pub fn set_has_controller(probe: HasController) {
27    let _ = HAS_CONTROLLER.set(probe);
28}
29
30/// Whether `block_id`, where an agent is registered, still waits for its
31/// controller. A lookup: no rate-limit token, no audit entry.
32pub fn awaits_controller(block_id: &str) -> bool {
33    HAS_CONTROLLER.get().is_some_and(|has| !has(block_id))
34}
35
36/// Whether a delivery error is the delivery path finding no controller on
37/// the block.
38pub fn is_no_controller(error: &str) -> bool {
39    error.starts_with(NO_CONTROLLER)
40}
41
42/// A controller registered: wake the replay of held messages, held input and
43/// pending cron fires, which left its agent alone until now. No I/O.
44pub fn controller_registered() {
45    super::held_jekt_wake().notify_one();
46    super::held_input_wake().notify_one();
47    crate::backend::jobs::pending_wake().notify_one();
48}
49
50/// For tests: blocks a test says have no controller yet. The probe this
51/// installs answers "has one" for every other block, so tests running beside
52/// it see no difference.
53#[cfg(any(test, feature = "test-support"))]
54pub mod testing {
55    use std::collections::HashSet;
56    use std::sync::{Arc, LazyLock, Mutex};
57
58    static WAITING: LazyLock<Mutex<HashSet<String>>> = LazyLock::new(Default::default);
59
60    fn waiting() -> std::sync::MutexGuard<'static, HashSet<String>> {
61        WAITING.lock().unwrap_or_else(|e| e.into_inner())
62    }
63
64    /// `block_id` has no controller from now on.
65    pub fn without_controller(block_id: &str) {
66        super::set_has_controller(Arc::new(|block_id: &str| !waiting().contains(block_id)));
67        waiting().insert(block_id.to_string());
68    }
69
70    /// `block_id`'s controller registered.
71    pub fn controller_arrived(block_id: &str) {
72        waiting().remove(block_id);
73        super::controller_registered();
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn a_block_waits_only_while_the_probe_says_it_has_no_controller() {
83        let block = format!("cw-{}", uuid::Uuid::new_v4());
84        testing::without_controller(&block);
85        assert!(awaits_controller(&block));
86        assert!(!awaits_controller("some-other-block"), "every other block is untouched");
87        testing::controller_arrived(&block);
88        assert!(!awaits_controller(&block));
89    }
90
91    #[test]
92    fn the_no_controller_error_is_recognised_and_waits_like_a_starting_agent() {
93        let error = format!("{NO_CONTROLLER} blk-1");
94        assert!(is_no_controller(&error));
95        assert!(crate::backend::reactive::is_transient_delivery_error(&error), "retried, never counted as a refusal");
96        for other in ["agent not found: x", "persistent process not running", "NOT_RUNNING: no controller for block x", ""] {
97            assert!(!is_no_controller(other), "{other:?}");
98        }
99    }
100}