synaps 0.1.0

Terminal-native AI agent runtime — parallel orchestration, reactive subagents, MCP, autonomous supervision
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
//! watcher — Autonomous agent supervisor daemon
//!
//! Spawns, monitors, and restarts agent worker processes.
//! Manages agent lifecycles with heartbeat monitoring and crash recovery.
//!
//! Usage:
//!   watcher run                    — start supervisor daemon (foreground)
//!   watcher deploy <name>          — start supervising an agent
//!   watcher stop <name>            — stop an agent
//!   watcher status                 — show all agent statuses
//!   watcher list                   — list configured agents
//!   watcher init <name>            — create agent from template
//!   watcher once <name>            — run agent once, no supervision
//!   watcher logs <name>            — show agent logs

mod ipc;
mod supervisor;
mod display;

use std::collections::HashMap;
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::sync::Arc;
use std::time::{Duration, Instant};
use std::os::unix::fs::PermissionsExt;
use synaps_cli::{AgentConfig, WatcherCommand, WatcherResponse, AgentStatusInfo};
use tokio::sync::{Mutex, Semaphore};
use tokio::net::{UnixListener, UnixStream};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use notify::Watcher;

use ipc::*;
use supervisor::*;
use display::*;

pub(crate) fn watcher_dir() -> PathBuf {
    synaps_cli::config::base_dir().join("watcher")
}

pub(crate) fn agent_binary() -> PathBuf {
    // Same binary, different subcommand
    std::env::current_exe().unwrap_or_default()
}

pub(crate) fn log(msg: &str) {
    let ts = chrono::Local::now().format("%Y-%m-%dT%H:%M:%S");
    eprintln!("[{}] [watcher] {}", ts, msg);
}

pub(crate) fn validate_agent_name(name: &str) -> Result<(), String> {
    if name.is_empty() {
        return Err("Agent name cannot be empty".to_string());
    }
    if name.len() > 64 {
        return Err("Agent name too long (max 64 characters)".to_string());
    }
    if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_') {
        return Err(format!("Agent name '{}' contains invalid characters (use a-z, 0-9, -, _)", name));
    }
    if name.starts_with('-') || name.starts_with('_') {
        return Err("Agent name cannot start with - or _".to_string());
    }
    Ok(())
}

pub(crate) fn load_agent_stats(agent_dir: &std::path::Path) -> synaps_cli::watcher_types::AgentStats {
    let path = agent_dir.join("stats.json");
    std::fs::read_to_string(&path)
        .ok()
        .and_then(|s| serde_json::from_str(&s).ok())
        .unwrap_or_default()
}

/// State for a managed agent
pub(crate) struct ManagedAgent {
    pub(crate) name: String,
    pub(crate) config_path: PathBuf,
    pub(crate) config: AgentConfig,
    pub(crate) child: Option<tokio::process::Child>,
    pub(crate) pid: Option<u32>,
    pub(crate) session_count: u64,
    pub(crate) consecutive_crashes: u32,
    pub(crate) last_start: Option<Instant>,
    pub(crate) total_uptime_secs: f64,
    pub(crate) stopped: bool, // manually stopped, don't restart
}

impl ManagedAgent {
    pub(crate) fn new(name: String, config_path: PathBuf, config: AgentConfig) -> Self {
        Self {
            name,
            config_path,
            config,
            child: None,
            pid: None,
            session_count: 0,
            consecutive_crashes: 0,
            last_start: None,
            total_uptime_secs: 0.0,
            stopped: false,
        }
    }

    pub(crate) fn is_running(&self) -> bool {
        self.child.is_some()
    }

    pub(crate) fn status_str(&self) -> &str {
        if self.stopped {
            "stopped"
        } else if self.is_running() {
            "running"
        } else {
            "sleeping"
        }
    }


    pub(crate) fn current_uptime_secs(&self) -> Option<f64> {
        if self.is_running() {
            self.last_start.map(|s| s.elapsed().as_secs_f64())
        } else {
            None
        }
    }

    pub(crate) fn to_status_info(&self) -> AgentStatusInfo {
        let agent_dir = AgentConfig::agent_dir(&self.config_path);
        let stats = load_agent_stats(&agent_dir);
        
        AgentStatusInfo {
            name: self.name.clone(),
            trigger: self.config.agent.trigger.clone(),
            status: self.status_str().to_string(),
            session_count: self.session_count,
            uptime_secs: self.current_uptime_secs(),
            pid: self.pid,
            consecutive_crashes: self.consecutive_crashes,
            cost_today: stats.today.cost_usd,
            cost_limit: self.config.limits.max_daily_cost_usd,
            tokens_today: stats.today.tokens,
            total_sessions: stats.total_sessions,
            model: self.config.agent.model.clone(),
        }
    }
}

pub(crate) fn discover_agents() -> Vec<(String, PathBuf)> {
    let dir = watcher_dir();
    let mut agents = Vec::new();
    if let Ok(entries) = std::fs::read_dir(&dir) {
        for entry in entries.flatten() {
            if entry.path().is_dir() {
                let config_path = entry.path().join("config.toml");
                if config_path.exists() {
                    let name = entry.file_name().to_string_lossy().to_string();
                    // Filter out invalid names
                    if validate_agent_name(&name).is_ok() {
                        agents.push((name, config_path));
                    }
                }
            }
        }
    }
    agents.sort_by(|a, b| a.0.cmp(&b.0));
    agents
}

pub(crate) fn print_status(agents: &HashMap<String, ManagedAgent>) {
    if agents.is_empty() {
        println!("No agents configured. Run: watcher init <name>");
        return;
    }
    let infos: Vec<AgentStatusInfo> = agents.values().map(|a| a.to_status_info()).collect();
    print_status_table(infos);
}

pub async fn run(command: String, args: Vec<String>) {
    let command = command.as_str();
    // Shift args so `args[2]` semantics from original code still work:
    // original code accessed std::env::args() where args[0]=bin, args[1]=command.
    // New scheme: args here are positional ones AFTER the subcommand. We'll rebuild
    // an argv-like vec to minimize code changes below.
    let argv: Vec<String> = {
        let mut v = vec!["synaps".to_string(), command.to_string()];
        v.extend(args.iter().cloned());
        v
    };
    let args = &argv;

    match command {
        "init" => {
            let name = args.get(2).unwrap_or_else(|| {
                eprintln!("Usage: watcher init <name>");
                std::process::exit(1);
            });
            if let Err(e) = init_agent(name) {
                eprintln!("Error: {}", e);
                std::process::exit(1);
            }
        }

        "list" => {
            let agents = discover_agents();
            if agents.is_empty() {
                println!("No agents configured. Run: watcher init <name>");
            } else {
                println!("{:<15} {:<50}", "AGENT", "CONFIG");
                println!("{}", "".repeat(65));
                for (name, path) in &agents {
                    println!("{:<15} {}", name, path.display());
                }
            }
        }

        "once" => {
            let name = args.get(2).unwrap_or_else(|| {
                eprintln!("Usage: watcher once <name>");
                std::process::exit(1);
            });
            let config_path = watcher_dir().join(name).join("config.toml");
            let config = AgentConfig::load(&config_path).unwrap_or_else(|e| {
                eprintln!("Failed to load agent '{}': {}", name, e);
                std::process::exit(1);
            });
            let mut agent = ManagedAgent::new(name.clone(), config_path, config);
            if let Err(e) = spawn_agent(&mut agent, "one-shot run").await {
                eprintln!("Error: {}", e);
                std::process::exit(1);
            }
            // Wait for completion
            if let Some(ref mut child) = agent.child {
                // Wait for agent with a timeout — agent shutdown can hang
                // if spawned tasks (shell reaper, etc.) don't terminate
                let wait_result = tokio::time::timeout(
                    std::time::Duration::from_secs(30),
                    child.wait()
                ).await;

                let code = match wait_result {
                    Ok(Ok(status)) => status.code().unwrap_or(1),
                    Ok(Err(e)) => {
                        eprintln!("Error waiting for agent: {}", e);
                        1
                    }
                    Err(_) => {
                        log(&format!("[{}] agent didn't exit within 30s, killing", name));
                        let _ = child.kill().await;
                        // Reap the killed process to avoid zombie
                        let reap_status = tokio::time::timeout(
                            std::time::Duration::from_secs(5),
                            child.wait()
                        ).await;
                        match reap_status {
                            Ok(Ok(s)) => s.code().unwrap_or(137), // killed
                            _ => 137 // SIGKILL exit code
                        }
                    }
                };

                let elapsed = agent.last_start.map(|s| s.elapsed().as_secs_f64()).unwrap_or(0.0);
                log(&format!("[{}] exited with code {}", name, code));
                if agent.config.hooks.notify_inbox {
                    log(&format!("[{}] notify_inbox hook firing", name));
                    supervisor::notify_inbox_completion(name, agent.session_count, elapsed, code);
                }
                std::process::exit(code);
            }
        }

        "run" => {
            run_supervisor().await;
        }

        "status" => {
            if let Some(agent_name) = args.get(2) {
                // Validate agent name
                if let Err(e) = validate_agent_name(agent_name) {
                    eprintln!("Error: {}", e);
                    std::process::exit(1);
                }
                
                // Detailed status for specific agent
                match send_ipc_command(WatcherCommand::AgentStatus { name: agent_name.clone() }).await {
                    Ok(WatcherResponse::AgentDetail { info }) => {
                        print_agent_detail(info);
                    }
                    Ok(WatcherResponse::Error { message }) => {
                        eprintln!("Error: {}", message);
                        std::process::exit(1);
                    }
                    Err(_e) => {
                        // Fallback to static detailed status
                        let config_path = watcher_dir().join(agent_name).join("config.toml");
                        if let Ok(config) = AgentConfig::load(&config_path) {
                            let agent = ManagedAgent::new(agent_name.clone(), config_path, config);
                            print_agent_detail(agent.to_status_info());
                        } else {
                            eprintln!("Agent '{}' not found", agent_name);
                            std::process::exit(1);
                        }
                    }
                    _ => {
                        eprintln!("Unexpected response from supervisor");
                        std::process::exit(1);
                    }
                }
            } else {
                // Overall status
                match send_ipc_command(WatcherCommand::Status).await {
                    Ok(WatcherResponse::Status { agents }) => {
                        print_status_table(agents);
                    }
                    Ok(WatcherResponse::Error { message }) => {
                        eprintln!("Error: {}", message);
                        std::process::exit(1);
                    }
                    Err(e) => {
                        // Fallback to static status if supervisor not running
                        let discovered = discover_agents();
                        let mut agents: HashMap<String, ManagedAgent> = HashMap::new();
                        for (name, config_path) in discovered {
                            if let Ok(config) = AgentConfig::load(&config_path) {
                                agents.insert(name.clone(), ManagedAgent::new(name, config_path, config));
                            }
                        }
                        print_status(&agents);
                        if !e.contains("Supervisor not running") {
                            eprintln!("Warning: {}", e);
                        }
                    }
                    _ => {
                        eprintln!("Unexpected response from supervisor");
                        std::process::exit(1);
                    }
                }
            }
        }

        "deploy" => {
            let name = args.get(2).unwrap_or_else(|| {
                eprintln!("Usage: watcher deploy <name>");
                std::process::exit(1);
            });
            
            // Validate agent name
            if let Err(e) = validate_agent_name(name) {
                eprintln!("Error: {}", e);
                std::process::exit(1);
            }
            
            match send_ipc_command(WatcherCommand::Deploy { name: name.clone() }).await {
                Ok(WatcherResponse::Ok { message }) => {
                    println!("{}", message);
                }
                Ok(WatcherResponse::Error { message }) => {
                    eprintln!("Error: {}", message);
                    std::process::exit(1);
                }
                Err(e) => {
                    eprintln!("Error: {}", e);
                    std::process::exit(1);
                }
                _ => {
                    eprintln!("Unexpected response from supervisor");
                    std::process::exit(1);
                }
            }
        }

        "stop" => {
            let name = args.get(2).unwrap_or_else(|| {
                eprintln!("Usage: watcher stop <name>");
                std::process::exit(1);
            });
            
            // Validate agent name
            if let Err(e) = validate_agent_name(name) {
                eprintln!("Error: {}", e);
                std::process::exit(1);
            }
            
            match send_ipc_command(WatcherCommand::Stop { name: name.clone() }).await {
                Ok(WatcherResponse::Ok { message }) => {
                    println!("{}", message);
                }
                Ok(WatcherResponse::Error { message }) => {
                    eprintln!("Error: {}", message);
                    std::process::exit(1);
                }
                Err(e) => {
                    eprintln!("Error: {}", e);
                    std::process::exit(1);
                }
                _ => {
                    eprintln!("Unexpected response from supervisor");
                    std::process::exit(1);
                }
            }
        }

        "logs" => {
            let name = args.get(2).unwrap_or_else(|| {
                eprintln!("Usage: watcher logs <name> [--follow | --session N | --last N]");
                std::process::exit(1);
            });

            // Validate agent name
            if let Err(e) = validate_agent_name(name) {
                eprintln!("Error: {}", e);
                std::process::exit(1);
            }

            // Parse flags
            let follow = args.iter().any(|a| a == "--follow" || a == "-f");
            let session_num = args.iter().position(|a| a == "--session").and_then(|i| args.get(i + 1)).and_then(|s| s.parse::<u64>().ok());
            let last_n = args.iter().position(|a| a == "--last").and_then(|i| args.get(i + 1)).and_then(|s| s.parse::<usize>().ok());

            if let Err(e) = show_logs(name, follow, session_num, last_n).await {
                eprintln!("Error: {}", e);
                std::process::exit(1);
            }
        }

        "help" | "--help" | "-h" => {
            println!("watcher — Autonomous agent supervisor");
            println!();
            println!("USAGE:");
            println!("  watcher run                 Start supervisor daemon (foreground)");
            println!("  watcher deploy <name>       Deploy/start an agent");
            println!("  watcher stop <name>         Stop an agent");  
            println!("  watcher once <name>         Run agent once without supervision");
            println!("  watcher init <name>         Create new agent from template");
            println!("  watcher list                List configured agents");
            println!("  watcher status              Show all agent statuses");
            println!("  watcher status <name>       Show detailed status for agent");
            println!("  watcher logs <name>         Show latest session log");
            println!("  watcher logs <name> --follow  Tail current session log");
            println!("  watcher logs <name> --session N  Show specific session");
            println!("  watcher help                Show this help");
            println!();
            println!("AGENTS DIR: {}", watcher_dir().display());
        }

        _ => {
            eprintln!("Unknown command: {}", command);
            eprintln!("Run 'watcher help' for usage information");
            std::process::exit(1);
        }
    }
}