koi_compose/lib.rs
1//! Koi composition layer — the single place that constructs domain cores, installs the
2//! cross-domain integration bridges, runs the container orchestrator, assembles
3//! capability status, and tears everything down in order.
4//!
5//! Three consumers share it: the `koi` daemon (`daemon_mode`), the Windows service
6//! (`run_service`), and `koi-embedded`. Building the composition once makes Windows and
7//! embedded parity true *by construction* — the verified `koi install` defect (a weaker
8//! Windows daemon missing the orchestrator + certmesh background loops) cannot recur,
9//! because all three call the same code.
10//!
11//! This is a **composition crate**, not a domain crate: it depends on every domain it
12//! wires. Nothing depends on it except the top-level consumers, so the `koi-common`
13//! kernel and the domain crates keep clean dependency closures.
14
15/// The cross-domain integration-trait bridges (moved from the binary's `integrations.rs`).
16pub mod bridges;
17
18/// Certmesh role-driven background loops + the enrollment-approval pump (moved from the
19/// binary's `main.rs`). Shared so Windows-service and embedded daemons reach parity.
20pub mod certmesh;
21
22/// The container-runtime orchestrator: translates runtime lifecycle events into
23/// mDNS/DNS/health/proxy operations (moved from the binary's `orchestrator.rs`). Shared so
24/// Windows-service and embedded daemons can spawn it too.
25pub mod orchestrator;
26
27/// Daemon core composition: `build_cores` (the one core+bridge construction graph the
28/// daemon and the Windows service share), `init_certmesh_core`, and `ordered_shutdown`.
29pub mod cores;
30
31/// Unified capability-status assembly (`assemble_capabilities`) — the single capability
32/// ladder shared by `/v1/status`, the dashboard snapshot, and the embedded snapshot.
33pub mod status;
34
35#[cfg(test)]
36mod parity_tests {
37 //! Acceptance proof for the `koi install` parity fix (P07).
38 //!
39 //! The Windows service (`run_service`) and the foreground daemon (`daemon_mode`) now
40 //! spawn certmesh background tasks + the orchestrator through these exact composition
41 //! functions. Asserting the spawned-task inventory here — with no SCM and no network —
42 //! proves Windows gets the same task set the daemon does (the verified defect was a
43 //! structurally weaker Windows daemon missing precisely these tasks).
44
45 use std::sync::Arc;
46
47 use tokio::task::JoinHandle;
48 use tokio_util::sync::CancellationToken;
49
50 fn test_certmesh() -> Arc<koi_certmesh::CertmeshCore> {
51 let dir = std::env::temp_dir().join(format!("koi-compose-parity-{}", std::process::id()));
52 let paths = koi_certmesh::CertmeshPaths::with_data_dir(dir);
53 Arc::new(koi_certmesh::CertmeshCore::uninitialized_with_paths(paths))
54 }
55
56 fn test_runtime() -> Arc<koi_runtime::RuntimeCore> {
57 // Constructed but never `start_watching`'d — no backend connection, no network.
58 Arc::new(koi_runtime::RuntimeCore::new(koi_runtime::RuntimeConfig {
59 backend_kind: koi_runtime::RuntimeBackendKind::Auto,
60 socket_path: None,
61 }))
62 }
63
64 #[tokio::test]
65 async fn certmesh_role_loops_spawn_one_task() {
66 // ADR-017 F6: a single member-pull renewal loop. CA failover is manual
67 // (`koi certmesh promote`) and the old broken health-heartbeat loop was
68 // removed, so there is exactly one background loop, needing no mDNS.
69 let certmesh = test_certmesh();
70 let cancel = CancellationToken::new();
71 let mut tasks: Vec<JoinHandle<()>> = Vec::new();
72
73 crate::certmesh::spawn_certmesh_background_tasks(&certmesh, &cancel, &mut tasks);
74 assert_eq!(
75 tasks.len(),
76 1,
77 "expected the single member-pull renewal loop"
78 );
79
80 cancel.cancel();
81 for task in tasks {
82 let _ = task.await;
83 }
84 }
85
86 #[tokio::test]
87 async fn windows_parity_full_task_inventory() {
88 // Mirror the exact spawn sequence windows.rs run_service now uses with certmesh +
89 // runtime enabled: 1 approval pump + 1 certmesh renewal loop + 1 orchestrator = 3.
90 let certmesh = test_certmesh();
91 let runtime = test_runtime();
92 let cancel = CancellationToken::new();
93 let mut tasks: Vec<JoinHandle<()>> = Vec::new();
94
95 crate::certmesh::spawn_enrollment_approval(
96 &certmesh,
97 crate::certmesh::deny_and_log_decider(),
98 &cancel,
99 &mut tasks,
100 )
101 .await;
102 crate::certmesh::spawn_certmesh_background_tasks(&certmesh, &cancel, &mut tasks);
103 tasks.push(crate::orchestrator::spawn_orchestrator(
104 &runtime,
105 crate::orchestrator::OrchestrationTargets {
106 mdns: None,
107 dns: None,
108 health: None,
109 proxy: None,
110 },
111 cancel.clone(),
112 ));
113
114 assert_eq!(
115 tasks.len(),
116 3,
117 "Windows parity: 1 approval + 1 certmesh renewal loop + 1 orchestrator"
118 );
119
120 cancel.cancel();
121 for task in tasks {
122 let _ = task.await;
123 }
124 }
125}