Skip to main content

agentmux_srv_core/backend/
always_available.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The switch for agents that outlive their pane
5//! (`docs/specs/SPEC_ALWAYS_AVAILABLE_AGENTS_2026_10_27.md` ยง4, D9). Off, closing a pane deletes the
6//! agent's block as it always has; on, closing it parks the block (`Command::ParkBlock`) and the
7//! agent keeps running. On unless the setting says `false`; it is removed a release after it became
8//! the default.
9
10use std::collections::HashMap;
11
12/// The setting. On by default since P4c; `false` turns it off.
13pub const SETTING_ALWAYS_AVAILABLE: &str = "agents:always_available";
14
15/// Whether closing a pane parks the agent's block instead of deleting it.
16pub fn enabled(extra: &HashMap<String, serde_json::Value>) -> bool {
17    match extra.get(SETTING_ALWAYS_AVAILABLE) {
18        Some(serde_json::Value::Bool(on)) => *on,
19        // A hand-edited file: "false", "off", "no" and 0 mean off too, so a safety switch is not left on by a typo of type.
20        Some(serde_json::Value::String(s)) => !matches!(s.trim().to_ascii_lowercase().as_str(), "false" | "off" | "no" | "0"),
21        Some(serde_json::Value::Number(n)) => n.as_f64() != Some(0.0),
22        _ => true,
23    }
24}
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29
30    #[test]
31    fn it_is_on_unless_the_setting_is_false() {
32        let mut extra = HashMap::new();
33        assert!(enabled(&extra), "the default");
34        extra.insert(SETTING_ALWAYS_AVAILABLE.to_string(), serde_json::json!("maybe"));
35        assert!(enabled(&extra), "a value that says nothing about off leaves the default");
36        for off in [serde_json::json!("no"), serde_json::json!("False"), serde_json::json!(" off "), serde_json::json!("0"), serde_json::json!(0)] {
37            extra.insert(SETTING_ALWAYS_AVAILABLE.to_string(), off.clone());
38            assert!(!enabled(&extra), "{off} is off");
39        }
40        extra.insert(SETTING_ALWAYS_AVAILABLE.to_string(), serde_json::json!(false));
41        assert!(!enabled(&extra));
42        extra.insert(SETTING_ALWAYS_AVAILABLE.to_string(), serde_json::json!(true));
43        assert!(enabled(&extra));
44    }
45}