zacor 0.1.0

Package manager and dispatcher for zr — install, manage, and run modular CLI packages
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
use crate::error::*;
use crate::package_definition::{self, PackageDefinition};
use crate::paths;
use crate::receipt;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::io::{BufRead, BufReader, Write};
use std::net::{TcpListener, TcpStream};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

const DAEMON_PORT: u16 = 19100;
const HEALTH_CHECK_INTERVAL: Duration = Duration::from_secs(10);
const HEALTH_CHECK_TIMEOUT: Duration = Duration::from_secs(10);
const MAX_RESTART_FAILURES: u32 = 5;
const MAX_BACKOFF: Duration = Duration::from_secs(60);

#[derive(Debug, Clone, PartialEq, Eq)]
enum ServiceState {
    Starting,
    Running,
    Failed,
}

impl std::fmt::Display for ServiceState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ServiceState::Starting => write!(f, "starting"),
            ServiceState::Running => write!(f, "running"),
            ServiceState::Failed => write!(f, "failed"),
        }
    }
}

struct ManagedService {
    name: String,
    port: u16,
    health_path: String,
    state: ServiceState,
    process: Option<Child>,
    consecutive_failures: u32,
    bin_path: PathBuf,
}

pub struct DaemonServer {
    home: PathBuf,
    services: Arc<Mutex<HashMap<String, ManagedService>>>,
    shutdown: Arc<Mutex<bool>>,
}

#[derive(Debug, Deserialize)]
struct DaemonRequest {
    request: String,
    #[serde(default)]
    name: Option<String>,
}

#[derive(Debug, Serialize)]
struct DaemonResponse {
    ok: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    error: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    port: Option<u16>,
    #[serde(skip_serializing_if = "Option::is_none")]
    services: Option<Vec<ServiceStatusEntry>>,
}

#[derive(Debug, Serialize)]
struct ServiceStatusEntry {
    name: String,
    port: u16,
    status: String,
}

impl DaemonServer {
    pub fn new(home: PathBuf) -> Self {
        DaemonServer {
            home,
            services: Arc::new(Mutex::new(HashMap::new())),
            shutdown: Arc::new(Mutex::new(false)),
        }
    }

    /// Run the daemon: IPC listener + health monitor loop.
    pub fn run(&self) -> Result<()> {
        let addr = format!("127.0.0.1:{}", DAEMON_PORT);
        let listener = TcpListener::bind(&addr)
            .with_context(|| format!("failed to bind daemon on {} — is another daemon running?", addr))?;

        eprintln!("daemon: listening on {}", addr);

        // Set a timeout on the listener so we can check for shutdown
        listener.set_nonblocking(false).ok();

        // Start health monitor thread
        let services = self.services.clone();
        let shutdown = self.shutdown.clone();
        let home = self.home.clone();
        std::thread::Builder::new()
            .name("zr-health-monitor".into())
            .spawn(move || health_monitor_loop(services, shutdown, home))
            .context("failed to spawn health monitor")?;

        // Accept IPC connections
        for stream in listener.incoming() {
            if *self.shutdown.lock().unwrap() {
                break;
            }
            match stream {
                Ok(stream) => {
                    let services = self.services.clone();
                    let shutdown = self.shutdown.clone();
                    let home = self.home.clone();
                    std::thread::Builder::new()
                        .name("zr-daemon-conn".into())
                        .spawn(move || {
                            if let Err(e) = handle_connection(stream, &services, &shutdown, &home) {
                                eprintln!("daemon: connection error: {:#}", e);
                            }
                        })
                        .ok();
                }
                Err(e) => {
                    if *self.shutdown.lock().unwrap() {
                        break;
                    }
                    eprintln!("daemon: accept error: {}", e);
                }
            }
        }

        // Shutdown: stop all services
        self.stop_all_services();
        eprintln!("daemon: stopped");
        Ok(())
    }

    fn stop_all_services(&self) {
        let mut services = self.services.lock().unwrap();
        for (name, svc) in services.iter_mut() {
            if let Some(ref mut child) = svc.process {
                eprintln!("daemon: stopping service '{}'", name);
                let _ = child.kill();
                let _ = child.wait();
            }
        }
        services.clear();
    }
}

fn handle_connection(
    stream: TcpStream,
    services: &Arc<Mutex<HashMap<String, ManagedService>>>,
    shutdown: &Arc<Mutex<bool>>,
    home: &Path,
) -> Result<()> {
    let mut reader = BufReader::new(stream.try_clone()?);
    let mut line = String::new();
    reader.read_line(&mut line)?;

    let req: DaemonRequest = serde_json::from_str(line.trim())
        .context("invalid daemon request")?;

    let response = match req.request.as_str() {
        "ping" => DaemonResponse {
            ok: true,
            error: None,
            port: None,
            services: None,
        },
        "status" => handle_status(services),
        "start-service" => {
            let name = req.name.as_deref().unwrap_or("");
            if name.is_empty() {
                DaemonResponse {
                    ok: false,
                    error: Some("missing service name".into()),
                    port: None,
                    services: None,
                }
            } else {
                handle_start_service(services, home, name)
            }
        }
        "stop-service" => {
            let name = req.name.as_deref().unwrap_or("");
            handle_stop_service(services, name)
        }
        "shutdown" => {
            *shutdown.lock().unwrap() = true;
            // Connect to ourselves to unblock the accept loop
            let _ = TcpStream::connect(format!("127.0.0.1:{}", DAEMON_PORT));
            DaemonResponse {
                ok: true,
                error: None,
                port: None,
                services: None,
            }
        }
        _ => DaemonResponse {
            ok: false,
            error: Some(format!("unknown request: {}", req.request)),
            port: None,
            services: None,
        },
    };

    let mut writer = stream;
    let json = serde_json::to_string(&response)?;
    writeln!(writer, "{}", json)?;
    writer.flush()?;
    Ok(())
}

fn handle_status(services: &Arc<Mutex<HashMap<String, ManagedService>>>) -> DaemonResponse {
    let services = services.lock().unwrap();
    let entries: Vec<ServiceStatusEntry> = services
        .values()
        .map(|svc| ServiceStatusEntry {
            name: svc.name.clone(),
            port: svc.port,
            status: svc.state.to_string(),
        })
        .collect();
    DaemonResponse {
        ok: true,
        error: None,
        port: None,
        services: Some(entries),
    }
}

fn handle_start_service(
    services: &Arc<Mutex<HashMap<String, ManagedService>>>,
    home: &Path,
    name: &str,
) -> DaemonResponse {
    // Check if already running
    {
        let svcs = services.lock().unwrap();
        if let Some(svc) = svcs.get(name) {
            if svc.state == ServiceState::Running {
                return DaemonResponse {
                    ok: true,
                    error: None,
                    port: Some(svc.port),
                    services: None,
                };
            }
        }
    }

    // Load package definition to get service config
    let (def, version) = match load_package_def(home, name) {
        Ok(v) => v,
        Err(e) => {
            return DaemonResponse {
                ok: false,
                error: Some(format!("{:#}", e)),
                port: None,
                services: None,
            };
        }
    };

    let service = match def.service.as_ref() {
        Some(s) => s,
        None => {
            return DaemonResponse {
                ok: false,
                error: Some(format!("package '{}' has no service section", name)),
                port: None,
                services: None,
            };
        }
    };

    let port = match service.port {
        Some(p) => p,
        None => {
            return DaemonResponse {
                ok: false,
                error: Some(format!("service '{}' has no port configured", name)),
                port: None,
                services: None,
            };
        }
    };

    let health_path = service.health.clone().unwrap_or_else(|| "/health".into());
    let binary_name = match def.binary.as_ref() {
        Some(b) => b,
        None => {
            return DaemonResponse {
                ok: false,
                error: Some(format!("package '{}' has no binary", name)),
                port: None,
                services: None,
            };
        }
    };

    let bin_path = paths::store_binary_path(home, name, &version, binary_name);
    if !bin_path.exists() {
        return DaemonResponse {
            ok: false,
            error: Some(format!("binary not found: {}", bin_path.display())),
            port: None,
            services: None,
        };
    }

    // Spawn the service
    let child = match Command::new(&bin_path)
        .arg(format!("--listen=:{}", port))
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::inherit())
        .spawn()
    {
        Ok(c) => c,
        Err(e) => {
            return DaemonResponse {
                ok: false,
                error: Some(format!("failed to spawn service: {}", e)),
                port: None,
                services: None,
            };
        }
    };

    eprintln!("daemon: started service '{}' on port {}", name, port);

    // Wait for health check to pass
    if !wait_for_health(port, &health_path, HEALTH_CHECK_TIMEOUT) {
        return DaemonResponse {
            ok: false,
            error: Some("health check timeout".into()),
            port: None,
            services: None,
        };
    }

    let mut svcs = services.lock().unwrap();
    // Reset failure count on manual start
    svcs.insert(
        name.to_string(),
        ManagedService {
            name: name.to_string(),
            port,
            health_path,
            state: ServiceState::Running,
            process: Some(child),
            consecutive_failures: 0,
            bin_path,
        },
    );

    DaemonResponse {
        ok: true,
        error: None,
        port: Some(port),
        services: None,
    }
}

fn handle_stop_service(
    services: &Arc<Mutex<HashMap<String, ManagedService>>>,
    name: &str,
) -> DaemonResponse {
    let mut svcs = services.lock().unwrap();
    if let Some(mut svc) = svcs.remove(name) {
        if let Some(ref mut child) = svc.process {
            eprintln!("daemon: stopping service '{}'", name);
            let _ = child.kill();
            let _ = child.wait();
        }
    }
    // Idempotent: ok even if not running
    DaemonResponse {
        ok: true,
        error: None,
        port: None,
        services: None,
    }
}

fn load_package_def(home: &Path, name: &str) -> Result<(PackageDefinition, String)> {
    let receipt = receipt::read(home, name)?
        .ok_or_else(|| anyhow!("package '{}' not found", name))?;
    let version = receipt.current.clone();
    let def_path = paths::definition_path(home, name, &version);
    let def = package_definition::parse_file(&def_path)?;
    Ok((def, version))
}

/// Wait for a service health endpoint to respond with HTTP 200.
fn wait_for_health(port: u16, health_path: &str, timeout: Duration) -> bool {
    let start = Instant::now();
    let interval = Duration::from_millis(100);

    loop {
        if check_health(port, health_path) {
            return true;
        }
        if start.elapsed() > timeout {
            return false;
        }
        std::thread::sleep(interval);
    }
}

/// Check health of a service via a simple HTTP GET.
fn check_health(port: u16, health_path: &str) -> bool {
    let addr = format!("127.0.0.1:{}", port);
    let Ok(mut stream) = TcpStream::connect(&addr) else {
        return false;
    };
    stream
        .set_read_timeout(Some(Duration::from_secs(2)))
        .ok();
    let request = format!(
        "GET {} HTTP/1.0\r\nHost: 127.0.0.1:{}\r\nConnection: close\r\n\r\n",
        health_path, port
    );
    if stream.write_all(request.as_bytes()).is_err() {
        return false;
    }
    if stream.flush().is_err() {
        return false;
    }
    let mut reader = BufReader::new(stream);
    let mut status_line = String::new();
    if reader.read_line(&mut status_line).is_err() {
        return false;
    }
    // Check for HTTP 200
    status_line.contains("200")
}

/// Calculate exponential backoff duration for restart attempts.
/// Sequence: 1s, 2s, 4s, 8s, then capped at 60s.
fn backoff_duration(failures: u32) -> Duration {
    let shift = failures.saturating_sub(1).min(63);
    let secs = 1u64.checked_shl(shift).unwrap_or(u64::MAX);
    Duration::from_secs(secs.min(MAX_BACKOFF.as_secs()))
}

/// Health monitor loop: checks each service periodically, restarts on failure.
fn health_monitor_loop(
    services: Arc<Mutex<HashMap<String, ManagedService>>>,
    shutdown: Arc<Mutex<bool>>,
    home: PathBuf,
) {
    loop {
        std::thread::sleep(HEALTH_CHECK_INTERVAL);

        if *shutdown.lock().unwrap() {
            break;
        }

        let names: Vec<String> = {
            let svcs = services.lock().unwrap();
            svcs.keys().cloned().collect()
        };

        for name in names {
            if *shutdown.lock().unwrap() {
                return;
            }

            let mut svcs = services.lock().unwrap();
            let Some(svc) = svcs.get_mut(&name) else {
                continue;
            };

            if svc.state == ServiceState::Failed {
                continue;
            }

            // Check if process is still alive
            let process_dead = svc
                .process
                .as_mut()
                .is_some_and(|c| c.try_wait().ok().flatten().is_some());

            let healthy = if process_dead {
                false
            } else {
                check_health(svc.port, &svc.health_path)
            };

            if healthy {
                svc.consecutive_failures = 0;
                svc.state = ServiceState::Running;
                continue;
            }

            // Unhealthy or dead — attempt restart
            svc.consecutive_failures += 1;
            eprintln!(
                "daemon: service '{}' unhealthy (failure {}/{})",
                name, svc.consecutive_failures, MAX_RESTART_FAILURES
            );

            if svc.consecutive_failures >= MAX_RESTART_FAILURES {
                eprintln!("daemon: service '{}' marked as failed after {} consecutive failures", name, MAX_RESTART_FAILURES);
                svc.state = ServiceState::Failed;
                if let Some(ref mut child) = svc.process {
                    let _ = child.kill();
                    let _ = child.wait();
                }
                continue;
            }

            // Kill old process
            if let Some(ref mut child) = svc.process {
                let _ = child.kill();
                let _ = child.wait();
            }

            let wait = backoff_duration(svc.consecutive_failures);
            let bin_path = svc.bin_path.clone();
            let port = svc.port;
            let health_path = svc.health_path.clone();
            let svc_name = name.clone();

            // Drop the lock before sleeping
            drop(svcs);

            eprintln!("daemon: restarting '{}' in {:?}", svc_name, wait);
            std::thread::sleep(wait);

            if *shutdown.lock().unwrap() {
                return;
            }

            // Respawn
            match Command::new(&bin_path)
                .arg(format!("--listen=:{}", port))
                .stdin(Stdio::null())
                .stdout(Stdio::null())
                .stderr(Stdio::inherit())
                .spawn()
            {
                Ok(child) => {
                    if wait_for_health(port, &health_path, HEALTH_CHECK_TIMEOUT) {
                        let mut svcs = services.lock().unwrap();
                        if let Some(svc) = svcs.get_mut(&svc_name) {
                            svc.process = Some(child);
                            svc.state = ServiceState::Running;
                            svc.consecutive_failures = 0;
                            eprintln!("daemon: service '{}' restarted successfully", svc_name);
                        }
                    } else {
                        eprintln!("daemon: service '{}' failed health check after restart", svc_name);
                    }
                }
                Err(e) => {
                    eprintln!("daemon: failed to restart '{}': {}", svc_name, e);
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_backoff_duration() {
        assert_eq!(backoff_duration(1), Duration::from_secs(1));
        assert_eq!(backoff_duration(2), Duration::from_secs(2));
        assert_eq!(backoff_duration(3), Duration::from_secs(4));
        assert_eq!(backoff_duration(4), Duration::from_secs(8));
        assert_eq!(backoff_duration(5), Duration::from_secs(16));
        assert_eq!(backoff_duration(6), Duration::from_secs(32));
        assert_eq!(backoff_duration(7), Duration::from_secs(60)); // 64 → capped at 60
        assert_eq!(backoff_duration(100), Duration::from_secs(60));
    }

    #[test]
    fn test_service_state_display() {
        assert_eq!(ServiceState::Starting.to_string(), "starting");
        assert_eq!(ServiceState::Running.to_string(), "running");
        assert_eq!(ServiceState::Failed.to_string(), "failed");
    }
}