# Zinit - Implementation Order
Phased implementation plan.
## Phase 0: Workspace Setup
1. Create workspace `Cargo.toml`
2. Create `zinit-common` crate skeleton
3. Verify workspace compiles
```bash
mkdir zinit && cd zinit
# Create Cargo.toml with workspace definition
cargo new --lib zinit-common
cargo new --bin zinit-server
cargo new --bin zinit-client
cargo new --bin zinit-pid1
cargo check
```
## Phase 1: zinit-common (Shared Crate)
**Goal**: All shared types compilable and tested.
4. `state.rs` - ServiceState, FailureReason
5. `config.rs` - ServiceConfig, DependencyDef, LifecycleDef
6. `protocol.rs` - RpcRequest, RpcResponse, error_codes
7. `responses.rs` - ServiceInfo, LogLine, etc.
8. `socket.rs` - Path helpers
9. `signal.rs` - Parse/name functions
10. `validate.rs` - Config validation
11. `client.rs` - Blocking ZinitClient
12. Unit tests for all modules
**Test**:
```bash
cd zinit-common
cargo test
```
## Phase 2: zinit-server Graph
**Goal**: Graph operations work correctly.
13. `graph/mod.rs` - ServiceGraph using petgraph
14. `graph/query.rs` - can_start, dependents, dependencies
15. `graph/display.rs` - ASCII tree, format_why_blocked
16. Config loading from directory
17. Graph validation (cycles, missing deps)
18. Unit tests for graph
**Test**:
```bash
# Create test config files
mkdir -p /tmp/zinit-test/services
echo '[service]
name = "test"
exec = "/bin/true"' > /tmp/zinit-test/services/test.toml
# Run graph tests
cargo test -p zinit-server graph
```
## Phase 3: zinit-server Process Management
**Goal**: Can spawn and monitor processes.
19. `process/spawn.rs` - Spawn with pipes, process groups
20. `process/signal.rs` - Signal handling (wraps nix)
21. `log/buffer.rs` - Ring buffer
22. `log/reader.rs` - Log reader task
**Test**:
```bash
# Manual test: spawn a process, capture output
cargo test -p zinit-server process
```
## Phase 4: zinit-server Supervisor
**Goal**: Event loop works, states transition correctly.
23. `supervisor/mod.rs` - Supervisor struct, Arc<RwLock<>>
24. `supervisor/event_loop.rs` - Event handling
25. `supervisor/startup.rs` - Start all services in order
26. `supervisor/timers.rs` - Timeout management
**Test**:
```bash
# Integration test with mock services
cargo test -p zinit-server supervisor
```
## Phase 5: zinit-server IPC
**Goal**: Can communicate with server via socket.
27. `ipc/server.rs` - Unix socket listener
28. `ipc/handlers.rs` - Method dispatch
29. Integration tests with real socket
**Test**:
```bash
# Start server in background
cargo run -p zinit-server -- --config /tmp/zinit-test &
# Test with netcat
## Phase 6: zinit-server Polish
**Goal**: Full functionality.
30. `graph/reload.rs` - Hot reload
31. `process/health.rs` - TCP/HTTP/exec health checks
32. `log/shipper.rs` - Optional forwarding to vector.io
33. `main.rs` - CLI args, signal handling
34. End-to-end tests
**Test**:
```bash
# Full integration test
./test/integration.sh
```
## Phase 7: zinit-client
**Goal**: Usable CLI.
35. CLI argument parsing with clap
36. Command implementations using ZinitClient
37. TUI (optional, can defer)
**Test**:
```bash
cargo run -p zinit-client -- list
cargo run -p zinit-client -- status sshd
cargo run -p zinit-client -- tree
```
## Phase 8: zinit-pid1
**Goal**: Works as PID 1.
38. Basic structure, spawn zinit-server
39. Signal forwarding
40. Zombie reaping
41. Health check via ZinitClient.ping()
**Test**:
```bash
# Test in container
docker run --init=false -v ./target/release:/zinit alpine /zinit/zinit-pid1
```
---
## Milestone Checklist
### M1: Types Compile
- [ ] zinit-common compiles
- [ ] All types serialize/deserialize
- [ ] Unit tests pass
### M2: Graph Works
- [ ] Can load config from directory
- [ ] Cycle detection works
- [ ] can_start() returns correct results
- [ ] ASCII tree renders
### M3: Processes Run
- [ ] Can spawn process
- [ ] stdout/stderr captured
- [ ] Exit status reported
- [ ] Process groups work
### M4: Supervisor Loops
- [ ] Event loop runs
- [ ] State transitions work
- [ ] Timeouts fire
- [ ] Restart policy honored
### M5: IPC Works
- [ ] Socket accepts connections
- [ ] All methods implemented
- [ ] Errors returned correctly
### M6: Server Complete
- [ ] Hot reload works
- [ ] Health checks work
- [ ] Logs can be shipped
- [ ] Graceful shutdown
### M7: Client Complete
- [ ] All commands work
- [ ] Output is readable
- [ ] Errors are clear
### M8: PID 1 Complete
- [ ] Spawns server
- [ ] Reaps zombies
- [ ] Forwards signals
- [ ] Shutdown/reboot works
- [ ] Container mode works
---
## Quick Reference
### Build
```bash
cargo build --release
```
### Test
```bash
cargo test --workspace
```
### Run (dev)
```bash
# Server
RUST_LOG=debug cargo run -p zinit-server -- --config ./test/config
# Client
cargo run -p zinit-client -- list
```
### Install
```bash
cargo install --path zinit-server
cargo install --path zinit-client
cargo install --path zinit-pid1
```