zinit 0.3.8

Process supervisor with dependency management
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
# zinit-server - Dependency Graph

Dependency graph structure using petgraph.

## Dependency Types

```rust
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DepType {
    /// Ordering only: start this after dependency has STARTED (not necessarily ready)
    /// Does not fail if dependency fails.
    After,

    /// Hard dependency: dependency must be RUNNING (satisfied).
    /// Cannot start if dependency not running.
    Requires,

    /// Soft dependency: try to start, but ok if fails.
    Wants,

    /// Mutual exclusion: cannot run at same time.
    Conflicts,
}
```

## Semantics

| Type | Blocks start? | Fails if dep fails? | Use case |
|------|---------------|---------------------|----------|
| `after` | Waits for dep to START | No | Ordering preference |
| `requires` | Waits for dep to be RUNNING | Can be configured | Hard dependency |
| `wants` | No | No | Nice-to-have |
| `conflicts` | Waits for dep to STOP | N/A | Mutual exclusion |

## Graph Structure

```rust
use petgraph::graph::{DiGraph, NodeIndex};
use std::collections::HashMap;

pub type ServiceId = NodeIndex;

pub struct ServiceGraph {
    /// Edge direction: dependency -> dependent
    /// "sshd requires network" = edge from network to sshd
    graph: DiGraph<Service, DepType>,
    
    /// Name -> ID lookup
    by_name: HashMap<String, ServiceId>,
}
```

## Construction

```rust
impl ServiceGraph {
    pub fn new() -> Self {
        Self {
            graph: DiGraph::new(),
            by_name: HashMap::new(),
        }
    }

    pub fn add_service(&mut self, service: Service) -> ServiceId {
        let name = service.name.clone();
        let id = self.graph.add_node(service);
        self.by_name.insert(name, id);
        id
    }

    pub fn add_dependency(
        &mut self,
        dependent: &str,    // the service that HAS the dependency
        dependency: &str,   // the service it depends ON
        dep_type: DepType,
    ) -> Result<(), GraphError> {
        let dependent_id = self.by_name.get(dependent)
            .ok_or_else(|| GraphError::UnknownService(dependent.to_string()))?;
        let dependency_id = self.by_name.get(dependency)
            .ok_or_else(|| GraphError::UnknownService(dependency.to_string()))?;

        // Edge goes FROM dependency TO dependent
        self.graph.add_edge(*dependency_id, *dependent_id, dep_type);
        Ok(())
    }
}
```

## Validation

```rust
impl ServiceGraph {
    pub fn validate(&self) -> Result<(), GraphError> {
        use petgraph::algo::is_cyclic_directed;
        
        if is_cyclic_directed(&self.graph) {
            return Err(GraphError::CyclicDependency(self.find_cycle()));
        }
        Ok(())
    }

    fn find_cycle(&self) -> Vec<String> {
        use petgraph::algo::kosaraju_scc;
        
        for scc in kosaraju_scc(&self.graph) {
            if scc.len() > 1 {
                return scc.iter()
                    .map(|id| self.graph[*id].name.clone())
                    .collect();
            }
        }
        vec![]
    }
}

#[derive(Debug, thiserror::Error)]
pub enum GraphError {
    #[error("Unknown service: {0}")]
    UnknownService(String),
    
    #[error("Cyclic dependency: {}", .0.join(" -> "))]
    CyclicDependency(Vec<String>),
    
    #[error("Duplicate service name: {0}")]
    DuplicateName(String),
}
```

## Queries

```rust
impl ServiceGraph {
    pub fn get(&self, id: ServiceId) -> Option<&Service> {
        self.graph.node_weight(id)
    }

    pub fn get_mut(&mut self, id: ServiceId) -> Option<&mut Service> {
        self.graph.node_weight_mut(id)
    }

    pub fn get_by_name(&self, name: &str) -> Option<ServiceId> {
        self.by_name.get(name).copied()
    }

    pub fn get_state(&self, id: ServiceId) -> &ServiceState {
        &self.graph[id].state
    }

    pub fn set_state(&mut self, id: ServiceId, state: ServiceState) {
        self.graph[id].state = state;
    }

    pub fn all_services(&self) -> impl Iterator<Item = ServiceId> + '_ {
        self.graph.node_indices()
    }

    /// Get topologically sorted start order
    pub fn start_order(&self) -> Vec<ServiceId> {
        use petgraph::algo::toposort;
        toposort(&self.graph, None).unwrap_or_default()
    }
}
```

## Dependency Queries

```rust
impl ServiceGraph {
    /// What does this service depend on?
    pub fn dependencies(&self, id: ServiceId) -> Vec<(ServiceId, DepType)> {
        use petgraph::Direction;
        
        self.graph
            .edges_directed(id, Direction::Incoming)
            .map(|e| (e.source(), *e.weight()))
            .collect()
    }

    /// What depends on this service?
    pub fn dependents(&self, id: ServiceId) -> Vec<ServiceId> {
        use petgraph::Direction;
        
        self.graph
            .edges_directed(id, Direction::Outgoing)
            .map(|e| e.target())
            .collect()
    }

    /// Can this service start right now?
    pub fn can_start(&self, id: ServiceId) -> Result<(), BlockedReason> {
        let mut waiting_on = Vec::new();
        let mut conflicts_with = Vec::new();

        for (dep_id, dep_type) in self.dependencies(id) {
            let dep_state = &self.graph[dep_id].state;
            let dep_name = self.graph[dep_id].name.clone();

            match dep_type {
                DepType::Requires => {
                    if !dep_state.is_satisfied() {
                        waiting_on.push(dep_name);
                    }
                }
                DepType::After => {
                    // Just needs to have started (not still Inactive/Blocked)
                    if matches!(dep_state, ServiceState::Inactive | ServiceState::Blocked { .. }) {
                        waiting_on.push(dep_name);
                    }
                }
                DepType::Wants => {
                    // Soft dep - don't block
                }
                DepType::Conflicts => {
                    if dep_state.is_active() {
                        conflicts_with.push(dep_name);
                    }
                }
            }
        }

        if !conflicts_with.is_empty() {
            return Err(BlockedReason::ConflictsWith(conflicts_with));
        }
        if !waiting_on.is_empty() {
            return Err(BlockedReason::WaitingOn(waiting_on));
        }
        Ok(())
    }

    /// Check if all "requires" deps are satisfied
    pub fn all_requires_satisfied(&self, id: ServiceId) -> bool {
        self.dependencies(id)
            .iter()
            .filter(|(_, dt)| *dt == DepType::Requires)
            .all(|(dep_id, _)| self.graph[*dep_id].state.is_satisfied())
    }
}

#[derive(Debug, Clone)]
pub enum BlockedReason {
    WaitingOn(Vec<String>),
    ConflictsWith(Vec<String>),
}
```

## Hot Reload

```rust
impl ServiceGraph {
    /// Reload configuration from disk.
    /// Validates before applying. Running states preserved.
    pub fn reload_from_directory(&mut self, path: &Path) -> Result<ReloadDiff, ReloadError> {
        // 1. Load candidate graph from disk
        let candidate = ServiceGraph::load_from_directory(path)?;
        
        // 2. Validate candidate
        candidate.validate()?;
        
        // 3. Compute diff
        let diff = self.compute_diff(&candidate);
        
        // 4. Check if removals are safe
        for name in &diff.removed {
            let id = self.get_by_name(name).unwrap();
            let running_deps = self.dependents(id)
                .into_iter()
                .filter(|d| self.graph[*d].state.is_active())
                .map(|d| self.graph[d].name.clone())
                .collect::<Vec<_>>();
            
            if !running_deps.is_empty() {
                return Err(ReloadError::UnsafeRemoval {
                    service: name.clone(),
                    running_dependents: running_deps,
                });
            }
        }
        
        // 5. Preserve states for services that still exist
        let mut preserved_states = HashMap::new();
        for name in diff.unchanged.iter().chain(&diff.changed) {
            if let Some(id) = self.get_by_name(name) {
                preserved_states.insert(name.clone(), self.graph[id].state.clone());
            }
        }
        
        // 6. Swap graphs
        *self = candidate;
        
        // 7. Restore preserved states
        for (name, state) in preserved_states {
            if let Some(id) = self.get_by_name(&name) {
                self.graph[id].state = state;
            }
        }
        
        Ok(diff)
    }
    
    fn compute_diff(&self, candidate: &ServiceGraph) -> ReloadDiff {
        let old_names: HashSet<_> = self.by_name.keys().cloned().collect();
        let new_names: HashSet<_> = candidate.by_name.keys().cloned().collect();
        
        ReloadDiff {
            added: new_names.difference(&old_names).cloned().collect(),
            removed: old_names.difference(&new_names).cloned().collect(),
            changed: vec![],  // TODO: compare configs
            unchanged: old_names.intersection(&new_names).cloned().collect(),
        }
    }
}

#[derive(Debug)]
pub struct ReloadDiff {
    pub added: Vec<String>,
    pub removed: Vec<String>,
    pub changed: Vec<String>,
    pub unchanged: Vec<String>,
}

#[derive(Debug, thiserror::Error)]
pub enum ReloadError {
    #[error("Config error: {0}")]
    Config(#[from] ConfigError),
    
    #[error("Graph error: {0}")]
    Graph(#[from] GraphError),
    
    #[error("Cannot remove {service}: running dependents: {}", running_dependents.join(", "))]
    UnsafeRemoval {
        service: String,
        running_dependents: Vec<String>,
    },
}
```

## ASCII Visualization

```rust
impl ServiceGraph {
    /// Explain why a service is blocked
    pub fn format_why_blocked(&self, name: &str) -> String {
        let Some(id) = self.get_by_name(name) else {
            return format!("Unknown service: {name}");
        };

        let service = &self.graph[id];
        let mut out = String::new();

        match &service.state {
            ServiceState::Blocked { waiting_on, conflicts_with } => {
                out.push_str(&format!("{} {} (blocked)\n", 
                    service.state.symbol(), name));
                
                if !waiting_on.is_empty() {
                    for (i, dep_id) in waiting_on.iter().enumerate() {
                        let dep = &self.graph[*dep_id];
                        let is_last = i == waiting_on.len() - 1 && conflicts_with.is_empty();
                        let prefix = if is_last { "└──" } else { "├──" };
                        out.push_str(&format!(
                            "{} requires: {} ({}) {}\n",
                            prefix,
                            dep.name,
                            dep.state.name(),
                            if dep.state.is_satisfied() { "" } else { "<- waiting" }
                        ));
                    }
                }
                
                for (i, dep_id) in conflicts_with.iter().enumerate() {
                    let dep = &self.graph[*dep_id];
                    let is_last = i == conflicts_with.len() - 1;
                    let prefix = if is_last { "└──" } else { "├──" };
                    out.push_str(&format!(
                        "{} conflicts: {} ({}) <- must stop\n",
                        prefix,
                        dep.name,
                        dep.state.name(),
                    ));
                }
            }
            _ => {
                out.push_str(&format!("{} {} ({})", 
                    service.state.symbol(), name, service.state.name()));
            }
        }

        out
    }

    /// Full dependency tree
    pub fn format_tree(&self) -> String {
        let mut out = String::new();
        let order = self.start_order();
        
        // Find root services (nothing depends on them)
        let roots: Vec<_> = order.iter()
            .filter(|id| self.dependents(**id).is_empty())
            .copied()
            .collect();

        for (i, root) in roots.iter().enumerate() {
            self.format_tree_node(*root, &mut out, "", i == roots.len() - 1);
        }

        out.push_str("\n[-]=inactive [?]=blocked [>]=starting [+]=running [!]=stopping [.]=exited [X]=failed\n");
        out
    }

    fn format_tree_node(&self, id: ServiceId, out: &mut String, prefix: &str, is_last: bool) {
        let service = &self.graph[id];
        let connector = if is_last { "└── " } else { "├── " };
        let child_prefix = format!("{}{}", prefix, if is_last { "    " } else { "│   " });
        
        let kind = if service.is_target { " [target]" } else { "" };
        out.push_str(&format!(
            "{}{}{} {}{} ({})\n",
            prefix, connector,
            service.state.symbol(),
            service.name,
            kind,
            service.state.name()
        ));

        let deps = self.dependencies(id);
        for (i, (dep_id, _)) in deps.iter().enumerate() {
            self.format_tree_node(*dep_id, out, &child_prefix, i == deps.len() - 1);
        }
    }

    /// Simple list
    pub fn format_list(&self) -> String {
        let mut out = String::new();
        for id in self.all_services() {
            let service = &self.graph[id];
            let pid_str = service.state.pid()
                .map(|p| format!(" (pid: {})", p))
                .unwrap_or_default();
            out.push_str(&format!(
                "{} {:20} {}{}\n",
                service.state.symbol(),
                service.name,
                service.state.name(),
                pid_str
            ));
        }
        out
    }
}
```

## Example Output

```
$ zinit list
[+] network-ready      running
[+] sshd               running
[>] my-app             starting
[?] worker             blocked

$ zinit why worker
[?] worker (blocked)
├── requires: database (starting) <- waiting
└── requires: redis (inactive) <- waiting

$ zinit tree
[+] system [target] (running)
├── [+] network-ready [target] (running)
│   ├── [+] dhcp (running)
│   └── [+] dns (running)
└── [>] my-app (starting)
    └── [+] network-ready [target] (running)
```