use netsky_db::SessionEvent;
use netsky_sh::tmux;
use crate::observability;
pub fn run() -> netsky_core::Result<()> {
let targets = agent_sessions();
if targets.is_empty() {
observability::record_session(netsky_core::consts::AGENT0_NAME, 0, SessionEvent::Down);
println!("[netsky down] no agent sessions running");
return Ok(());
}
for name in &targets {
tmux::kill_session(name)?;
println!("[netsky down] killed '{name}'");
}
observability::record_session(netsky_core::consts::AGENT0_NAME, 0, SessionEvent::Down);
Ok(())
}
fn agent_sessions() -> Vec<String> {
tmux::list_sessions()
.into_iter()
.filter(|n| is_agent_session(n))
.collect()
}
fn is_agent_session(name: &str) -> bool {
let rest = match name.strip_prefix(netsky_core::consts::CLONE_PREFIX) {
Some(r) => r,
None => return false,
};
!rest.is_empty() && rest.chars().all(|c| c.is_ascii_digit())
}
#[cfg(test)]
mod tests {
use super::is_agent_session;
#[test]
fn matches_agent_sessions_only() {
assert!(is_agent_session("agent0"));
assert!(is_agent_session("agent5"));
assert!(is_agent_session("agent42"));
assert!(!is_agent_session("agentinfinity"));
assert!(!is_agent_session("agent"));
assert!(!is_agent_session("agentfoo"));
assert!(!is_agent_session("foo"));
assert!(!is_agent_session("netsky-ticker"));
}
}