netsky 0.1.7

netsky CLI: the viable system launcher and subcommand dispatcher
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! `netsky down` — tear down agent0 + clones. Leaves agentinfinity + ticker alone.

use std::fs;
use std::path::PathBuf;
use std::thread;
use std::time::{Duration, Instant};

use netsky_core::consts::{AGENT0_NAME, CLONE_PREFIX, MCP_CHANNEL_DIR_PREFIX};
use netsky_core::envelope::{Envelope, write_envelope};
use netsky_core::paths::{assert_no_symlink_under, home};
use netsky_db::SessionEvent;
use netsky_sh::tmux;

use crate::observability;

const GRACEFUL_TIMEOUT: Duration = Duration::from_secs(60);
const GRACEFUL_POLL: Duration = Duration::from_secs(1);
const SHUTDOWN_KIND: &str = "shutdown";
const SHUTDOWN_ACK_KIND: &str = "shutdown_ack";

pub fn run(force: bool) -> netsky_core::Result<()> {
    let sessions = RealSessions;
    let channel = FsShutdownChannel::new(home());
    run_with(&sessions, &channel, force, GRACEFUL_TIMEOUT, GRACEFUL_POLL)
}

fn run_with<S, C>(
    sessions: &S,
    channel: &C,
    force: bool,
    timeout: Duration,
    poll: Duration,
) -> netsky_core::Result<()>
where
    S: SessionOps,
    C: ShutdownChannel,
{
    let targets = filter_agent_sessions(sessions.list_sessions());
    if targets.is_empty() {
        observability::record_session(AGENT0_NAME, 0, SessionEvent::Down);
        println!("[netsky down] no agent sessions running");
        return Ok(());
    }

    if force {
        force_kill(sessions, &targets)?;
        observability::record_session(AGENT0_NAME, 0, SessionEvent::Down);
        return Ok(());
    }

    let requests = send_shutdown_requests(channel, &targets)?;
    let outcomes = wait_for_graceful_closes(sessions, channel, &requests, timeout, poll);
    for outcome in outcomes {
        let name = outcome.agent;
        match outcome.outcome {
            ShutdownOutcome::Closed => {
                println!("[netsky down] '{name}' closed gracefully");
            }
            ShutdownOutcome::Acked => {
                println!("[netsky down] '{name}' acked shutdown; killing remaining tmux session");
                sessions.kill_session(&name)?;
                println!("[netsky down] killed '{name}' after shutdown ack");
            }
            ShutdownOutcome::TimedOut => {
                println!(
                    "[netsky down] '{name}' did not close within {}s; forcing kill",
                    timeout.as_secs()
                );
                sessions.kill_session(&name)?;
                println!("[netsky down] killed '{name}' after graceful timeout");
            }
        }
    }

    observability::record_session(AGENT0_NAME, 0, SessionEvent::Down);
    Ok(())
}

fn send_shutdown_requests<C: ShutdownChannel>(
    channel: &C,
    targets: &[String],
) -> netsky_core::Result<Vec<ShutdownRequest>> {
    targets
        .iter()
        .map(|name| {
            let request_id = format!(
                "shutdown-{name}-{}",
                chrono::Utc::now().timestamp_nanos_opt().unwrap_or(0)
            );
            channel.send_shutdown(name, &request_id)?;
            println!("[netsky down] requested graceful shutdown for '{name}'");
            Ok(ShutdownRequest {
                agent: name.clone(),
                request_id,
            })
        })
        .collect()
}

fn force_kill<S: SessionOps>(sessions: &S, targets: &[String]) -> netsky_core::Result<()> {
    for name in targets {
        sessions.kill_session(name)?;
        println!("[netsky down] killed '{name}'");
    }
    Ok(())
}

fn wait_for_graceful_closes<S, C>(
    sessions: &S,
    channel: &C,
    requests: &[ShutdownRequest],
    timeout: Duration,
    poll: Duration,
) -> Vec<AgentShutdownOutcome>
where
    S: SessionOps,
    C: ShutdownChannel,
{
    let deadline = Instant::now() + timeout;
    let mut outcomes: Vec<Option<ShutdownOutcome>> = vec![None; requests.len()];
    loop {
        for (idx, request) in requests.iter().enumerate() {
            if outcomes[idx].is_some() {
                continue;
            }
            if !sessions.has_session(&request.agent) {
                outcomes[idx] = Some(ShutdownOutcome::Closed);
                continue;
            }
            if channel.has_shutdown_ack(&request.agent, &request.request_id) {
                outcomes[idx] = Some(ShutdownOutcome::Acked);
            }
        }
        if outcomes.iter().all(Option::is_some) {
            break;
        }
        if Instant::now() >= deadline {
            for outcome in &mut outcomes {
                if outcome.is_none() {
                    *outcome = Some(ShutdownOutcome::TimedOut);
                }
            }
            break;
        }
        thread::sleep(poll);
    }

    requests
        .iter()
        .zip(outcomes)
        .map(|(request, outcome)| AgentShutdownOutcome {
            agent: request.agent.clone(),
            outcome: outcome.expect("shutdown outcome must be set"),
        })
        .collect()
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct ShutdownRequest {
    agent: String,
    request_id: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct AgentShutdownOutcome {
    agent: String,
    outcome: ShutdownOutcome,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ShutdownOutcome {
    Closed,
    Acked,
    TimedOut,
}

trait SessionOps {
    fn list_sessions(&self) -> Vec<String>;
    fn has_session(&self, name: &str) -> bool;
    fn kill_session(&self, name: &str) -> netsky_core::Result<()>;
}

struct RealSessions;

impl SessionOps for RealSessions {
    fn list_sessions(&self) -> Vec<String> {
        tmux::list_sessions()
    }

    fn has_session(&self, name: &str) -> bool {
        tmux::has_session(name)
    }

    fn kill_session(&self, name: &str) -> netsky_core::Result<()> {
        tmux::kill_session(name).map_err(Into::into)
    }
}

trait ShutdownChannel {
    fn send_shutdown(&self, target: &str, request_id: &str) -> netsky_core::Result<()>;
    fn has_shutdown_ack(&self, target: &str, request_id: &str) -> bool;
}

struct FsShutdownChannel {
    root: PathBuf,
}

impl FsShutdownChannel {
    fn new(home_dir: PathBuf) -> Self {
        Self {
            root: home_dir.join(MCP_CHANNEL_DIR_PREFIX),
        }
    }

    fn inbox_dir(&self, agent: &str) -> PathBuf {
        self.root.join(agent).join("inbox")
    }
}

impl ShutdownChannel for FsShutdownChannel {
    fn send_shutdown(&self, target: &str, request_id: &str) -> netsky_core::Result<()> {
        let inbox = self.inbox_dir(target);
        assert_no_symlink_under(&self.root, &inbox)?;
        let mut envelope = Envelope::new(
            AGENT0_NAME,
            "shutdown: please /down",
            chrono::Utc::now().to_rfc3339(),
        );
        envelope.id = Some(request_id.to_string());
        envelope.to = Some(target.to_string());
        envelope.kind = Some(SHUTDOWN_KIND.to_string());
        envelope.requires_ack = Some(true);
        write_envelope(&inbox, &envelope)?;
        Ok(())
    }

    fn has_shutdown_ack(&self, target: &str, request_id: &str) -> bool {
        let inbox = self.inbox_dir(AGENT0_NAME);
        let Ok(entries) = fs::read_dir(inbox) else {
            return false;
        };
        entries
            .flatten()
            .filter_map(|entry| fs::read_to_string(entry.path()).ok())
            .filter_map(|raw| serde_json::from_str::<Envelope>(&raw).ok())
            .any(|env| is_shutdown_ack(&env, target, request_id))
    }
}

fn is_shutdown_ack(env: &Envelope, target: &str, request_id: &str) -> bool {
    if env.from != target {
        return false;
    }
    if env.kind.as_deref() == Some(SHUTDOWN_ACK_KIND)
        && env.in_reply_to.as_deref() == Some(request_id)
    {
        return true;
    }
    env.text.to_ascii_lowercase().contains("shutdown ack")
        && (env.in_reply_to.as_deref() == Some(request_id) || env.id.as_deref() == Some(request_id))
}

fn filter_agent_sessions(sessions: Vec<String>) -> Vec<String> {
    sessions
        .into_iter()
        .filter(|n| is_agent_session(n))
        .collect()
}

fn is_agent_session(name: &str) -> bool {
    let rest = match name.strip_prefix(CLONE_PREFIX) {
        Some(r) => r,
        None => return false,
    };
    !rest.is_empty() && rest.chars().all(|c| c.is_ascii_digit())
}

#[cfg(test)]
mod tests {
    use std::cell::{Cell, RefCell};
    use std::collections::HashSet;
    use std::rc::Rc;

    use super::*;

    #[derive(Default)]
    struct FakeSessions {
        listed: Vec<String>,
        alive: RefCell<HashSet<String>>,
        has_checks: Cell<usize>,
        killed: RefCell<Vec<String>>,
        events: Rc<RefCell<Vec<String>>>,
    }

    impl FakeSessions {
        fn with(listed: &[&str]) -> Self {
            Self::with_events(listed, Rc::new(RefCell::new(Vec::new())))
        }

        fn with_events(listed: &[&str], events: Rc<RefCell<Vec<String>>>) -> Self {
            Self {
                listed: listed.iter().map(|s| s.to_string()).collect(),
                alive: RefCell::new(listed.iter().map(|s| s.to_string()).collect()),
                has_checks: Cell::new(0),
                killed: RefCell::new(Vec::new()),
                events,
            }
        }
    }

    impl SessionOps for FakeSessions {
        fn list_sessions(&self) -> Vec<String> {
            self.listed.clone()
        }

        fn has_session(&self, name: &str) -> bool {
            self.has_checks.set(self.has_checks.get() + 1);
            self.alive.borrow().contains(name)
        }

        fn kill_session(&self, name: &str) -> netsky_core::Result<()> {
            self.alive.borrow_mut().remove(name);
            self.killed.borrow_mut().push(name.to_string());
            self.events.borrow_mut().push(format!("kill {name}"));
            Ok(())
        }
    }

    #[derive(Default)]
    struct FakeChannel {
        sent: RefCell<Vec<(String, String)>>,
        acked: RefCell<HashSet<String>>,
        events: Rc<RefCell<Vec<String>>>,
    }

    impl FakeChannel {
        fn with_events(events: Rc<RefCell<Vec<String>>>) -> Self {
            Self {
                events,
                ..Default::default()
            }
        }
    }

    impl ShutdownChannel for FakeChannel {
        fn send_shutdown(&self, target: &str, request_id: &str) -> netsky_core::Result<()> {
            self.sent
                .borrow_mut()
                .push((target.to_string(), request_id.to_string()));
            self.events.borrow_mut().push(format!("send {target}"));
            Ok(())
        }

        fn has_shutdown_ack(&self, target: &str, _request_id: &str) -> bool {
            self.acked.borrow().contains(target)
        }
    }

    #[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"));
    }

    #[test]
    fn default_targets_leave_agentinfinity_alive() {
        let targets = filter_agent_sessions(vec![
            "agent0".to_string(),
            "agent1".to_string(),
            "agentinfinity".to_string(),
            "netsky-ticker".to_string(),
        ]);
        assert_eq!(targets, vec!["agent0", "agent1"]);
    }

    #[test]
    fn force_kills_immediately_without_shutdown_envelopes() {
        let sessions = FakeSessions::with(&["agent0", "agent1"]);
        let channel = FakeChannel::default();

        run_with(
            &sessions,
            &channel,
            true,
            Duration::from_millis(0),
            Duration::from_millis(0),
        )
        .unwrap();

        assert_eq!(channel.sent.borrow().len(), 0);
        assert_eq!(*sessions.killed.borrow(), vec!["agent0", "agent1"]);
        assert_eq!(sessions.has_checks.get(), 0);
    }

    #[test]
    fn default_sends_shutdown_envelopes_and_waits_before_force_kill() {
        let events = Rc::new(RefCell::new(Vec::new()));
        let sessions = FakeSessions::with_events(&["agent1", "agent2"], events.clone());
        let channel = FakeChannel::with_events(events.clone());

        run_with(
            &sessions,
            &channel,
            false,
            Duration::from_millis(0),
            Duration::from_millis(0),
        )
        .unwrap();

        assert_eq!(channel.sent.borrow().len(), 2);
        assert_eq!(channel.sent.borrow()[0].0, "agent1");
        assert_eq!(channel.sent.borrow()[1].0, "agent2");
        assert_eq!(sessions.has_checks.get(), 2);
        assert_eq!(*sessions.killed.borrow(), vec!["agent1", "agent2"]);
        assert_eq!(
            *events.borrow(),
            vec!["send agent1", "send agent2", "kill agent1", "kill agent2"]
        );
    }

    #[test]
    fn default_kills_after_shutdown_ack() {
        let sessions = FakeSessions::with(&["agent1"]);
        let channel = FakeChannel::default();
        channel.acked.borrow_mut().insert("agent1".to_string());

        run_with(
            &sessions,
            &channel,
            false,
            Duration::from_secs(60),
            Duration::from_millis(0),
        )
        .unwrap();

        assert_eq!(channel.sent.borrow().len(), 1);
        assert_eq!(*sessions.killed.borrow(), vec!["agent1"]);
    }

    #[test]
    fn default_does_not_kill_session_that_closed_itself() {
        let sessions = FakeSessions::with(&["agent1"]);
        sessions.alive.borrow_mut().remove("agent1");
        let channel = FakeChannel::default();

        run_with(
            &sessions,
            &channel,
            false,
            Duration::from_secs(60),
            Duration::from_millis(0),
        )
        .unwrap();

        assert_eq!(channel.sent.borrow().len(), 1);
        assert!(sessions.killed.borrow().is_empty());
    }

    #[test]
    fn recognizes_shutdown_ack_envelope() {
        let mut env = Envelope::new("agent3", "shutdown ack", "2026-04-17T00:00:00Z");
        env.kind = Some(SHUTDOWN_ACK_KIND.to_string());
        env.in_reply_to = Some("shutdown-agent3-1".to_string());

        assert!(is_shutdown_ack(&env, "agent3", "shutdown-agent3-1"));
        assert!(!is_shutdown_ack(&env, "agent4", "shutdown-agent3-1"));
        assert!(!is_shutdown_ack(&env, "agent3", "other"));
    }
}