continuo 0.10.1

Runtime service composition for multi-service Rust applications: typed component resolution, dependency-ordered lifecycle, reload, runnable tasks, graceful drain, and finalization.
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};

use tokio::sync::{mpsc, oneshot};
use tokio::task::{Id, JoinError, JoinSet};
use tokio_util::sync::CancellationToken;
use tracing::{Instrument, debug, error};

#[cfg(feature = "registry")]
use crate::registry::FinalizeOutcome;
use crate::registry::{AnyComponent, AnyRunnable, Error, Registry, Result};
use crate::service::{
    RunContext, ServiceCommand, ServiceManager, ServiceManagerError,
    ServiceSnapshot, ServiceStartPolicy, ServiceStatus,
};
use crate::state::SharedState;
#[cfg(feature = "registry")]
use crate::{HasRegistry, Register};

/// Apply one reload with the catalog bound during runtime configuration.
/// Native file watching is only an invalidation source, not a prerequisite.
#[cfg(feature = "config")]
async fn apply_configured_reload<S: 'static>(
    reader: Option<&congif::ConfigReader>,
    component: &dyn AnyComponent<S>,
    reloadable: &dyn crate::Reloadable<S>,
    state: &S,
    config_prepared: bool,
) -> Result<()> {
    let candidate =
        if let Some(owner) = component.as_configurable() {
            let reader = reader.ok_or_else(|| Error::msg(
            "configure the runtime before reloading a configurable service",
        ))?;
            if !config_prepared {
                reader.prepare_owners(&[owner]).await?;
            }
            reader.prepared_config(owner)
        } else {
            None
        };
    if config_prepared
        && let (Some(reader), Some(owner)) =
            (reader, component.as_configurable())
    {
        reader.notify_changed(owner).await?;
        return Ok(());
    }
    reloadable.reload(state).await?;
    if let (Some(reader), Some(candidate)) = (reader, candidate) {
        reader.confirm_applied(candidate).await?;
    }
    Ok(())
}

#[cfg(all(test, feature = "config"))]
#[path = "../tests/runtime/config_reload.rs"]
mod config_reload_tests;

#[cfg(all(test, feature = "registry", feature = "config-watch"))]
#[path = "../tests/runtime/preparation.rs"]
mod preparation_tests;

struct ServiceExit {
    name: &'static str,
    generation: u64,
    result: Result<()>,
}

#[derive(Clone, Copy)]
struct TaskIdentity {
    name: &'static str,
    generation: u64,
}

struct ServiceEntry<S> {
    component: Arc<dyn AnyComponent<S>>,
    runnable: Arc<dyn AnyRunnable<S>>,
    status: ServiceStatus,
    active_executions: Arc<AtomicU64>,
    #[cfg(feature = "fleet")]
    dispatch: crate::fleet::Dispatch,
    generation: u64,
    cancellation: Option<CancellationToken>,
    applied_start_policy: ServiceStartPolicy,
    start_after_drain: bool,
    last_error: Option<String>,
    reload_revision: u64,
    last_reload_error: Option<String>,
    stop_waiters:
        Vec<oneshot::Sender<Result<ServiceSnapshot, ServiceManagerError>>>,
}

impl<S: 'static> ServiceEntry<S> {
    fn snapshot(
        &self,
        name: &'static str,
        state: &S,
    ) -> ServiceSnapshot {
        ServiceSnapshot {
            name,
            status: self.status,
            active_executions: self.active_executions.load(Ordering::SeqCst),
            #[cfg(feature = "fleet")]
            dispatch: self.dispatch,
            start_policy: self.runnable.start_policy(state),
            reloadable: self.component.as_reloadable().is_some(),
            generation: self.generation,
            last_error: self.last_error.clone(),
            reload_revision: self.reload_revision,
            last_reload_error: self.last_reload_error.clone(),
        }
    }
}

/// Owns the live generations of every registered runnable service.
///
/// Tokio schedules the futures; `Runtime<S>` decides which service generation
/// exists. Its [`ServiceManager`] handle is a registered application capability
/// that sends commands back to this sole owner.
pub struct Runtime<S> {
    join_set: JoinSet<ServiceExit>,
    task_identities: HashMap<Id, TaskIdentity>,
    services: HashMap<&'static str, ServiceEntry<S>>,
    state: Option<S>,
    registry: Option<Registry<S>>,
    manager: Arc<ServiceManager>,
    commands: mpsc::Receiver<ServiceCommand>,
    shutting_down: bool,
    // Registrations recorded by `with_services`, applied by `register_services`.
    registrations: Vec<fn(&S) -> Result<()>>,
    // Configuration is a runtime capability, independent of native watching.
    #[cfg(feature = "config")]
    config_reader: Option<fn(&S) -> &congif::ConfigReader>,
    #[cfg(feature = "fleet")]
    dispatcher: Option<Arc<dyn crate::fleet::Dispatcher<S>>>,
    #[cfg(feature = "config-watch")]
    configuration: Option<crate::watcher::ConfigurationWatch<S>>,
}

impl<S> Default for Runtime<S> {
    fn default() -> Self {
        let (manager, commands) = ServiceManager::channel();
        Self {
            join_set: JoinSet::new(),
            task_identities: HashMap::new(),
            services: HashMap::new(),
            state: None,
            registry: None,
            manager,
            commands,
            shutting_down: false,
            registrations: Vec::new(),
            #[cfg(feature = "config")]
            config_reader: None,
            #[cfg(feature = "fleet")]
            dispatcher: None,
            #[cfg(feature = "config-watch")]
            configuration: None,
        }
    }
}

impl<S> Runtime<S> {
    /// Return the control capability backed by this runtime.
    ///
    /// `with_services` registers this capability automatically. For manual
    /// composition, register this same `Arc` before component boot.
    pub fn manager(&self) -> Arc<ServiceManager> {
        self.manager.clone()
    }
}

#[cfg(feature = "registry")]
impl<S: HasRegistry + 'static> Runtime<S> {
    /// Boot, then [`run_prepared`](Self::run_prepared).
    ///
    /// The whole lifecycle of an application without typed configuration:
    /// `boot_all`, `validate_all`, `spawn_all`, wait for shutdown, drain,
    /// `finalize_all`.
    pub async fn run(
        mut self,
        state: S,
    ) -> Result<FinalizeOutcome> {
        self.register_services(&state)?;
        state.registry_ref().boot_all(&state).await?;
        self.run_prepared(state).await
    }

    /// Run an already booted registry to completion.
    ///
    /// Validates every component and refuses to start if any fails
    /// (`Error::Invalid` carries the whole outcome). Spawns the runnables,
    /// waits until shutdown is initiated or a runnable fails fatally, drains,
    /// then finalizes. Finalization runs even after a failure, so named
    /// resources are released; the failure is then returned and finalize
    /// failures are reported through `tracing`. On success the finalize
    /// outcome is returned for the caller to inspect.
    pub async fn run_prepared(
        mut self,
        state: S,
    ) -> Result<FinalizeOutcome> {
        let admission =
            state.registry_ref().validate_all(&state).and_then(|validated| {
                if !validated.is_valid() {
                    return Err(Error::Invalid(validated));
                }
                self.spawn_all(state.registry_ref(), state.clone()).map(|_| ())
            });
        let outcome = match admission {
            Ok(()) => self.wait_until_shutdown(&state).await,
            Err(error) => {
                self.begin_shutdown();
                Err(error)
            }
        };
        let drained = self.drain().await;
        let finalized = state.registry_ref().finalize_all(&state).await;
        match outcome.and(drained) {
            Ok(()) => finalized,
            Err(failure) => {
                match finalized {
                    Ok(finalized) => {
                        for failure in finalized.failures() {
                            error!(component = failure.component(), error = %failure.error(), "finalize failed after runtime failure");
                        }
                    }
                    Err(error) => {
                        error!(%error, "finalization did not run after runtime failure");
                    }
                }
                Err(failure)
            }
        }
    }

    /// Record the application's service types. Nothing is registered yet:
    /// `register_services` applies the recorded registrations, and `run`,
    /// `serve`, `configure` and `prepare` call it first.
    pub fn with_services<R: Register<S>>(mut self) -> Self {
        self.registrations.push(R::register);
        self
    }

    /// Apply the registrations recorded by `with_services`, in order, together
    /// with this runtime's own `ServiceManager`. A manager belonging to
    /// another runtime is rejected, never replaced. As with `Register`, a
    /// failure retains the registrations already completed. Failed and pending
    /// callbacks stay queued; retrying requires the failed callback to tolerate
    /// any work it already completed, since registration is not transactional.
    pub fn register_services(
        &mut self,
        state: &S,
    ) -> Result<()> {
        let registry = state.registry_ref();
        if let Some(manager) = registry.resolve::<ServiceManager>() {
            if !Arc::ptr_eq(&manager, &self.manager) {
                return Err(Error::msg(
                    "service manager belongs to another runtime",
                ));
            }
        } else {
            registry.insert(self.manager())?;
        }
        while let Some(register) = self.registrations.first().copied() {
            register(state)?;
            self.registrations.remove(0);
        }
        Ok(())
    }
}

#[cfg(all(feature = "registry", feature = "config"))]
impl<S: HasRegistry + congif::HasConfigReader> Runtime<S> {
    /// Prepare and validate all registered config owners before resources run.
    /// Components do not need a boot hook merely to initialize configuration.
    pub async fn configure(
        &mut self,
        state: &S,
    ) -> Result<()> {
        if self.state.is_some() {
            return Err(Error::msg("configure services before spawning them"));
        }
        self.register_services(state)?;
        let components = state.registry_ref().configuration_entries()?;
        let owners: Vec<_> = components
            .iter()
            .filter_map(|(_, component)| component.as_configurable())
            .collect();
        state.config_reader().initialize_owners(&owners).await?;
        for registration in owners {
            state.config_reader().configure_registered(registration).await?;
        }
        // Programs are declared from the applied configuration, so their
        // environments are prepared once every owner has been configured.
        prepare_programs(
            state.config_reader(),
            &state.registry_ref().lifecycle_entries()?,
        )?;
        #[cfg(feature = "config-watch")]
        if let Some(watching) = self.configuration.as_mut() {
            watching.refresh_sources(state)?;
        }
        self.config_reader = Some(S::config_reader);
        Ok(())
    }

    /// Prepare configuration, then boot in dependency order before admitting
    /// runnable work. Preparation components may publish complete services.
    /// Their config owner remains the same; it is not parsed or configured a
    /// second time merely because a ready service exposes it.
    pub async fn prepare(
        &mut self,
        state: &S,
    ) -> Result<()> {
        self.register_services(state)?;
        let declared: std::collections::HashSet<_> = state
            .registry_ref()
            .configuration_entries()?
            .into_iter()
            .map(|(owner, _)| owner)
            .collect();
        self.configure(state).await?;
        state.registry_ref().boot_all(state).await?;
        for (owner, component) in
            state.registry_ref().configuration_entries()?
        {
            if !declared.contains(&owner) {
                return Err(Error::msg(format!(
                    "component '{}' published an undeclared config owner during boot; declare configuration in its preparation component",
                    component.name(),
                )));
            }
        }
        prepare_programs(
            state.config_reader(),
            &state.registry_ref().lifecycle_entries()?,
        )?;
        #[cfg(feature = "config-watch")]
        if let Some(watching) = self.configuration.as_ref() {
            self.configuration = Some(watching.rebuild(state)?);
        }
        Ok(())
    }
}

/// Register or replace the program environments of every `Environmental`
/// component among `components`, from their applied configurations.
#[cfg(all(feature = "registry", feature = "config"))]
fn prepare_programs<S: 'static>(
    reader: &congif::ConfigReader,
    components: &[(std::any::TypeId, Arc<dyn AnyComponent<S>>)],
) -> Result<()> {
    for (owner, component) in components {
        if let Some(environmental) = component.as_environmental() {
            reader.prepare_programs(
                *owner,
                environmental.program_environments(),
            )?;
        }
    }
    Ok(())
}

#[cfg(feature = "config-watch")]
impl<S: HasRegistry + crate::HasEvents + crate::HasConfigReader> Runtime<S> {
    /// The whole lifecycle of a configured, watched application:
    /// [`watch_configurations`](Self::watch_configurations),
    /// [`prepare`](Self::prepare), then [`run_prepared`](Self::run_prepared).
    pub async fn serve(
        mut self,
        state: S,
    ) -> Result<FinalizeOutcome> {
        self.watch_configurations(&state)?;
        self.prepare(&state).await?;
        self.run_prepared(state).await
    }

    /// Prepare common file watches and event subscription after registration,
    /// before boot reads config. The existing runtime control loop consumes
    /// notifications; no separate task, future or Application join is needed.
    pub fn watch_configurations(
        &mut self,
        state: &S,
    ) -> Result<()> {
        if self.state.is_some() {
            return Err(Error::msg(
                "configure watching before spawning services",
            ));
        }
        if self.configuration.is_some() {
            return Err(Error::msg("configuration watching already prepared"));
        }
        self.register_services(state)?;
        self.configuration =
            Some(crate::watcher::ConfigurationWatch::new(state)?);
        self.config_reader = Some(S::config_reader);
        Ok(())
    }
}

impl<S> Runtime<S>
where
    S: SharedState,
{
    /// Install fleet coordination before submitting any runnable services.
    /// Local management remains owned by this runtime, not by the adapter.
    #[cfg(feature = "fleet")]
    pub fn with_dispatcher(
        mut self,
        dispatcher: Arc<dyn crate::fleet::Dispatcher<S>>,
    ) -> Result<Self> {
        if self.state.is_some() {
            return Err(Error::msg(
                "install dispatcher before spawning services",
            ));
        }
        self.dispatcher = Some(dispatcher);
        Ok(self)
    }

    /// Register and start all runnable components.
    ///
    /// Runnable names must be unique because they are the stable management
    /// identity. The runtime may only be initialized once.
    pub fn spawn_all(
        &mut self,
        registry: &Registry<S>,
        state: S,
    ) -> Result<usize> {
        if self.state.is_some() {
            return Err(Error::msg(
                "runtime runnable set is already initialized",
            ));
        }
        if !self.registrations.is_empty() {
            return Err(Error::msg(
                "services were recorded after boot; register them before prepare or run",
            ));
        }

        let runnables = registry.runnable_entries()?;
        #[cfg(feature = "fleet")]
        let runnables: Vec<_> = runnables
            .into_iter()
            .filter(|entry| {
                let scope = entry
                    .component
                    .as_scoped()
                    .map(crate::fleet::Scoped::scope)
                    .or_else(|| {
                        entry
                            .component
                            .as_dispatched()
                            .map(|placement| placement.scope())
                    })
                    .unwrap_or_default();
                scope.includes(state.process_scope())
            })
            .collect();
        let mut names = HashSet::with_capacity(runnables.len());
        for entry in &runnables {
            if !names.insert(entry.name) {
                let name = entry.name;
                return Err(Error::msg(format!(
                    "duplicate runnable service name '{name}'"
                )));
            }
        }

        // Admit all automatic generations before starting any task. A missing
        // dependency must not leave earlier services partially running.
        let runnables = runnables
            .into_iter()
            .map(|entry| {
                let policy = entry.runnable.start_policy(&state);
                let bound = if policy == ServiceStartPolicy::Automatic {
                    #[cfg(feature = "fleet")]
                    if let Some(placement) = entry.component.as_dispatched()
                        && placement.dispatch().is_controller_driven()
                    {
                        let dispatcher =
                            self.dispatcher.as_ref().ok_or_else(|| {
                                Error::msg(format!(
                                    "service '{}' requires a fleet dispatcher",
                                    entry.name
                                ))
                            })?;
                        dispatcher.validate(
                            &crate::fleet::DispatchRequest {
                                name: entry.name,
                                generation: 1,
                                scope: placement.scope(),
                                dispatch: placement.dispatch(),
                                policy: placement.dispatch_policy(),
                            },
                            &state,
                        )?;
                    }
                    Some(entry.runnable.clone().bind(registry)?)
                } else {
                    None
                };
                Ok((entry, bound, policy))
            })
            .collect::<Result<Vec<_>>>()?;

        self.registry = Some(registry.shared());
        self.state = Some(state);
        for (entry, bound, policy) in runnables {
            let name = entry.name;
            let automatic = bound.is_some();
            #[cfg(feature = "fleet")]
            let dispatch = entry
                .component
                .as_dispatched()
                .map(|placement| placement.dispatch())
                .unwrap_or_default();
            self.services.insert(
                name,
                ServiceEntry {
                    component: entry.component,
                    runnable: entry.runnable,
                    status: ServiceStatus::Stopped,
                    active_executions: Arc::new(AtomicU64::new(0)),
                    #[cfg(feature = "fleet")]
                    dispatch,
                    generation: 0,
                    cancellation: None,
                    applied_start_policy: policy,
                    start_after_drain: false,
                    last_error: None,
                    reload_revision: 0,
                    last_reload_error: None,
                    stop_waiters: Vec::new(),
                },
            );
            if automatic {
                // Boot may have been interrupted after components registered work
                // but before runnable submission. Submit the initial generation
                // even with an already-cancelled parent so each runnable can
                // perform its own no-new-work/final-drain path.
                self.start_admitted_generation(name, true, bound)?;
            }
        }

        Ok(self.services.len())
    }

    fn reconcile_start_policy(
        &mut self,
        name: &'static str,
    ) -> Result<ServiceSnapshot> {
        let state = self.state.as_ref().expect("runtime state is initialized");
        let service =
            self.services.get_mut(name).expect("service is registered");
        let policy = service.runnable.start_policy(state);
        let enabling = service.applied_start_policy
            == ServiceStartPolicy::Unavailable
            && policy == ServiceStartPolicy::Automatic;
        if policy == ServiceStartPolicy::Unavailable {
            service.start_after_drain = false;
            if service.status == ServiceStatus::Running {
                service.status = ServiceStatus::Stopping;
                if let Some(cancellation) = &service.cancellation {
                    cancellation.cancel();
                }
            }
        } else if enabling {
            match service.status {
                ServiceStatus::Stopped | ServiceStatus::Failed => {
                    let snapshot = self.start_generation(name, false)?;
                    self.services.get_mut(name).unwrap().applied_start_policy =
                        policy;
                    return Ok(snapshot);
                }
                ServiceStatus::Stopping => service.start_after_drain = true,
                ServiceStatus::Running => {}
            }
        } else if policy == ServiceStartPolicy::Manual {
            service.start_after_drain = false;
        }
        service.applied_start_policy = policy;
        Ok(service.snapshot(name, state))
    }

    fn start_generation(
        &mut self,
        name: &'static str,
        allow_cancelled_parent: bool,
    ) -> Result<ServiceSnapshot> {
        self.start_admitted_generation(name, allow_cancelled_parent, None)
    }

    fn start_admitted_generation(
        &mut self,
        name: &'static str,
        allow_cancelled_parent: bool,
        bound: Option<Arc<dyn AnyRunnable<S>>>,
    ) -> Result<ServiceSnapshot> {
        let state =
            self.state.as_ref().cloned().ok_or_else(|| {
                Error::msg("service runtime is not initialized")
            })?;
        if self.shutting_down
            || (!allow_cancelled_parent && state.is_shutting_down())
        {
            return Err(Error::msg("service runtime is shutting down"));
        }

        let service = self.services.get_mut(name).ok_or_else(|| {
            Error::msg(format!("runnable service '{name}' is not registered"))
        })?;
        // Initial automatic activation was already decided during preflight.
        // Explicit starts and restarts consult the current policy instead.
        if bound.is_none()
            && service.runnable.start_policy(&state)
                == ServiceStartPolicy::Unavailable
        {
            return Err(Error::msg(format!(
                "runnable service '{name}' is unavailable on this runtime"
            )));
        }
        let generation =
            service.generation.checked_add(1).ok_or_else(|| {
                Error::msg(format!("service '{name}' generation overflow"))
            })?;
        #[cfg(feature = "fleet")]
        let dispatch = match service.component.as_dispatched() {
            Some(placement) if placement.dispatch().is_controller_driven() => {
                let request = crate::fleet::DispatchRequest {
                    name,
                    generation,
                    scope: placement.scope(),
                    dispatch: placement.dispatch(),
                    policy: placement.dispatch_policy(),
                };
                let dispatcher = self.dispatcher.clone().ok_or_else(|| {
                    Error::msg(format!(
                        "service '{name}' requires a fleet dispatcher"
                    ))
                })?;
                if bound.is_none() {
                    dispatcher.validate(&request, &state)?;
                }
                Some((dispatcher, request))
            }
            _ => None,
        };
        let runnable = match bound {
            Some(runnable) => runnable,
            None => service.runnable.clone().bind(
                self.registry.as_ref().ok_or_else(|| {
                    Error::msg("service registry is not initialized")
                })?,
            )?,
        };
        service.generation = generation;
        service.start_after_drain = false;
        service.status = ServiceStatus::Running;
        service.last_error = None;
        // A fresh census belongs only to this generation. Delayed cleanup of
        // an aborted predecessor must not decrement its successor's count.
        service.active_executions = Arc::new(AtomicU64::new(0));
        let active_executions = service.active_executions.clone();
        #[cfg(feature = "fleet")]
        {
            service.dispatch = dispatch
                .as_ref()
                .map(|(_, request)| request.dispatch)
                .unwrap_or_default();
        }

        let generation = service.generation;
        let cancellation = state.shutdown_token().child_token();
        service.cancellation = Some(cancellation.clone());
        let snapshot = service.snapshot(name, &state);

        let abort = self.join_set.spawn(
            async move {
                let context = RunContext::new(cancellation);
                #[cfg(feature = "fleet")]
                let result = if let Some((dispatcher, request)) = dispatch {
                    dispatcher
                        .run(
                            request,
                            crate::fleet::ServiceExecution::new(
                                runnable, state, active_executions,
                            ),
                            context,
                        )
                        .await
                } else {
                    let _execution = crate::service::ExecutionGuard::enter(&active_executions);
                    runnable.run(state, context).await
                };
                #[cfg(not(feature = "fleet"))]
                let result = {
                    let _execution = crate::service::ExecutionGuard::enter(&active_executions);
                    runnable.run(state, context).await
                };
                let result = result.map_err(|e| e.into_run(name));
                ServiceExit { name, generation, result }
            }
            .instrument(
                tracing::debug_span!("component", component = %name, generation),
            ),
        );
        self.task_identities
            .insert(abort.id(), TaskIdentity { name, generation });

        Ok(snapshot)
    }

    fn snapshots(&self) -> Vec<ServiceSnapshot> {
        let state = self
            .state
            .as_ref()
            .expect("runtime state is initialized before observation");
        let mut snapshots = self
            .services
            .iter()
            .map(|(name, service)| service.snapshot(name, state))
            .collect::<Vec<_>>();
        snapshots.sort_by_key(|snapshot| snapshot.name());
        snapshots
    }

    async fn handle_command(
        &mut self,
        command: ServiceCommand,
    ) {
        self.handle_prepared_command(command, false).await;
    }

    async fn handle_prepared_command(
        &mut self,
        command: ServiceCommand,
        config_prepared: bool,
    ) {
        if self.shutting_down {
            Self::reject_command(command, ServiceManagerError::ShuttingDown);
            return;
        }

        // Every arm below reaches for the state, which only exists after
        // `spawn_all`. A command that arrives before it used to panic the
        // runtime task on an `expect` — an operator command racing startup
        // taking the process down. `RuntimeUnavailable` is the answer the enum
        // already has for exactly this.
        if self.state.is_none() {
            Self::reject_command(
                command,
                ServiceManagerError::RuntimeUnavailable,
            );
            return;
        }

        match command {
            ServiceCommand::List { reply } => {
                let _ = reply.send(Ok(self.snapshots()));
            }
            ServiceCommand::Start { name, reply } => {
                let Some(static_name) =
                    self.services.get_key_value(name.as_str()).map(|(n, _)| *n)
                else {
                    let _ =
                        reply.send(Err(ServiceManagerError::NotFound(name)));
                    return;
                };

                let status = self.services[static_name].status;
                let state =
                    self.state.as_ref().expect("runtime state is initialized");
                if self.services[static_name].runnable.start_policy(state)
                    == ServiceStartPolicy::Unavailable
                {
                    let _ =
                        reply.send(Err(ServiceManagerError::Unavailable(name)));
                    return;
                }
                match status {
                    ServiceStatus::Running => {
                        let _ = reply.send(Ok(self.services[static_name]
                            .snapshot(static_name, state)));
                    }
                    ServiceStatus::Stopping => {
                        let _ = reply.send(Err(ServiceManagerError::Busy {
                            name,
                            status,
                        }));
                    }
                    ServiceStatus::Stopped | ServiceStatus::Failed => {
                        let result =
                            self.start_generation(static_name, false).map_err(
                                |error| ServiceManagerError::OperationFailed {
                                    name,
                                    message: error.to_string(),
                                },
                            );
                        let _ = reply.send(result);
                    }
                }
            }
            ServiceCommand::Stop { name, reply } => {
                let Some(static_name) =
                    self.services.get_key_value(name.as_str()).map(|(n, _)| *n)
                else {
                    let _ =
                        reply.send(Err(ServiceManagerError::NotFound(name)));
                    return;
                };

                let service = self
                    .services
                    .get_mut(static_name)
                    .expect("service key just resolved");
                service.start_after_drain = false;
                match service.status {
                    ServiceStatus::Running => {
                        service.status = ServiceStatus::Stopping;
                        service.stop_waiters.push(reply);
                        if let Some(cancellation) = &service.cancellation {
                            cancellation.cancel();
                        }
                    }
                    ServiceStatus::Stopping => service.stop_waiters.push(reply),
                    ServiceStatus::Stopped | ServiceStatus::Failed => {
                        let state = self
                            .state
                            .as_ref()
                            .expect("runtime state is initialized");
                        let _ = reply
                            .send(Ok(service.snapshot(static_name, state)));
                    }
                }
            }
            ServiceCommand::Reload { name, reply } => {
                let Some(static_name) =
                    self.services.get_key_value(name.as_str()).map(|(n, _)| *n)
                else {
                    let _ =
                        reply.send(Err(ServiceManagerError::NotFound(name)));
                    return;
                };

                let status = self.services[static_name].status;
                if status == ServiceStatus::Stopping {
                    let _ = reply
                        .send(Err(ServiceManagerError::Busy { name, status }));
                    return;
                }

                let Some(state) = self.state.as_ref().cloned() else {
                    let _ = reply
                        .send(Err(ServiceManagerError::RuntimeUnavailable));
                    return;
                };
                let component = self.services[static_name].component.clone();
                let Some(reloadable) = component.as_reloadable() else {
                    let _ = reply
                        .send(Err(ServiceManagerError::NotReloadable(name)));
                    return;
                };
                let next_revision = match self.services[static_name]
                    .reload_revision
                    .checked_add(1)
                {
                    Some(revision) => revision,
                    None => {
                        let _ = reply.send(Err(
                            ServiceManagerError::OperationFailed {
                                name,
                                message: "reload revision overflow".to_owned(),
                            },
                        ));
                        return;
                    }
                };
                #[cfg(feature = "config")]
                let reload = apply_configured_reload(
                    self.config_reader.map(|reader| reader(&state)),
                    component.as_ref(),
                    reloadable,
                    &state,
                    config_prepared,
                )
                .await;
                #[cfg(not(feature = "config"))]
                let reload = {
                    let _ = config_prepared;
                    reloadable.reload(&state).await
                };
                let reload = reload.map_err(|error: Error| {
                    error.into_reload(static_name).to_string()
                });
                let service = self
                    .services
                    .get_mut(static_name)
                    .expect("service key remains registered during reload");
                service.reload_revision = next_revision;
                let result = match reload {
                    Ok(()) => {
                        service.last_reload_error = None;
                        self.reconcile_start_policy(static_name).map_err(
                            |error| {
                                let message = error.to_string();
                                self.services
                                    .get_mut(static_name)
                                    .unwrap()
                                    .last_reload_error = Some(message.clone());
                                ServiceManagerError::OperationFailed {
                                    name,
                                    message,
                                }
                            },
                        )
                    }
                    Err(message) => {
                        service.last_reload_error = Some(message.clone());
                        Err(ServiceManagerError::OperationFailed {
                            name,
                            message,
                        })
                    }
                };
                let _ = reply.send(result);
            }
            ServiceCommand::Reconcile { name, reply } => {
                let result = match self.services.get_key_value(name.as_str()) {
                    Some((&static_name, _)) => {
                        self.reconcile_start_policy(static_name).map_err(
                            |error| ServiceManagerError::OperationFailed {
                                name,
                                message: error.to_string(),
                            },
                        )
                    }
                    None => Err(ServiceManagerError::NotFound(name)),
                };
                let _ = reply.send(result);
            }
        }
    }

    fn reject_command(
        command: ServiceCommand,
        error: ServiceManagerError,
    ) {
        match command {
            ServiceCommand::List { reply } => {
                let _ = reply.send(Err(error));
            }
            ServiceCommand::Start { reply, .. }
            | ServiceCommand::Stop { reply, .. }
            | ServiceCommand::Reload { reply, .. }
            | ServiceCommand::Reconcile { reply, .. } => {
                let _ = reply.send(Err(error));
            }
        }
    }

    fn begin_shutdown(&mut self) {
        self.shutting_down = true;
        #[cfg(feature = "config-watch")]
        {
            self.configuration = None;
        }
        self.commands.close();
        while let Ok(command) = self.commands.try_recv() {
            Self::reject_command(command, ServiceManagerError::ShuttingDown);
        }
        for service in self.services.values_mut() {
            if service.status == ServiceStatus::Running {
                service.status = ServiceStatus::Stopping;
            }
            if let Some(cancellation) = &service.cancellation {
                cancellation.cancel();
            }
        }
    }

    fn complete_service(
        &mut self,
        exit: ServiceExit,
    ) -> Result<()> {
        let state = self.state.as_ref().expect("runtime state is initialized");
        let Some(service) = self.services.get_mut(exit.name) else {
            return Err(Error::msg(format!(
                "completed runnable service '{}' is not registered",
                exit.name
            )));
        };
        if service.generation != exit.generation {
            return Err(Error::msg(format!(
                "service '{}' completed stale generation {} while generation {} is current",
                exit.name, exit.generation, service.generation
            )));
        }

        service.cancellation = None;
        match exit.result {
            Ok(()) => {
                service.status = ServiceStatus::Stopped;
                service.last_error = None;
                let snapshot = service.snapshot(exit.name, state);
                for waiter in service.stop_waiters.drain(..) {
                    let _ = waiter.send(Ok(snapshot.clone()));
                }
                debug!(
                    component = exit.name,
                    generation = exit.generation,
                    "runnable stopped"
                );
                let restart = std::mem::take(&mut service.start_after_drain)
                    && service.runnable.start_policy(state)
                        == ServiceStartPolicy::Automatic
                    && !self.shutting_down
                    && !state.is_shutting_down();
                if restart
                    && let Err(error) = self.start_generation(exit.name, false)
                {
                    // Deferred admission is still a reload operation. Report
                    // failure on this service, not as a fatal runnable exit.
                    let message = error.to_string();
                    let service = self.services.get_mut(exit.name).unwrap();
                    service.status = ServiceStatus::Failed;
                    service.last_error = Some(message.clone());
                    service.last_reload_error = Some(message);
                    service.applied_start_policy =
                        ServiceStartPolicy::Unavailable;
                    error!(component = exit.name, %error, "reloaded service activation failed");
                }
                Ok(())
            }
            Err(error) => {
                let message = error.to_string();
                service.status = ServiceStatus::Failed;
                service.last_error = Some(message.clone());
                for waiter in service.stop_waiters.drain(..) {
                    let _ = waiter.send(Err(
                        ServiceManagerError::OperationFailed {
                            name: exit.name.to_owned(),
                            message: message.clone(),
                        },
                    ));
                }

                match error {
                    Error::Recoverable { name, source } => {
                        error!(component = %name, "runnable failed (runtime continuing): {}", source);
                        Ok(())
                    }
                    fatal => Err(fatal),
                }
            }
        }
    }

    fn complete_join(
        &mut self,
        joined: std::result::Result<(Id, ServiceExit), JoinError>,
    ) -> Result<()> {
        match joined {
            Ok((id, exit)) => {
                self.task_identities.remove(&id);
                self.complete_service(exit)
            }
            Err(join_error) => {
                if let Some(identity) =
                    self.task_identities.remove(&join_error.id())
                    && let Some(service) = self.services.get_mut(identity.name)
                    && service.generation == identity.generation
                {
                    let message = join_error.to_string();
                    service.status = ServiceStatus::Failed;
                    service.last_error = Some(message.clone());
                    for waiter in service.stop_waiters.drain(..) {
                        let _ = waiter.send(Err(
                            ServiceManagerError::OperationFailed {
                                name: identity.name.to_owned(),
                                message: message.clone(),
                            },
                        ));
                    }
                }
                Err(join_error.into())
            }
        }
    }

    /// Run until process shutdown is initiated or a critical runnable failure
    /// occurs. An empty runnable set remains alive so stopped services can be
    /// started again through the manager.
    ///
    /// A fatal failure begins this runtime's own shutdown before the error is
    /// returned: the remaining runnables are cancelled and the manager stops
    /// accepting commands. It does *not* cancel the process shutdown token —
    /// what the process does about a failed runtime is the embedder's decision,
    /// and it is holding the error.
    pub async fn wait_until_shutdown(
        &mut self,
        state: &S,
    ) -> Result<()> {
        let outcome = self.run_until_shutdown(state).await;
        if outcome.is_err() {
            // Returning without this left the siblings running and the manager
            // open, which is the opposite of what `Error::Run` documents — "the
            // runtime tears the worker down". Claviron's bootstrap happened to
            // shut down on the error; nothing made that the contract.
            self.begin_shutdown();
        }

        outcome
    }

    async fn run_until_shutdown(
        &mut self,
        state: &S,
    ) -> Result<()> {
        let shutdown = state.shutdown_token();

        loop {
            #[cfg(feature = "config-watch")]
            let changes = crate::watcher::ConfigurationWatch::next(
                &mut self.configuration,
            );
            #[cfg(not(feature = "config-watch"))]
            let changes = std::future::pending::<Vec<std::any::TypeId>>();
            tokio::select! {
                    _ = shutdown.cancelled() => {
                        self.begin_shutdown();
                        debug!("runtime observed shutdown signal");
                        return Ok(());
                    }
                    command = self.commands.recv() => {
                        match command {
                            Some(command) => self.handle_command(command).await,
                            None => return Err(Error::msg("service manager command channel closed")),
                        }
                    }
                    joined = self.join_set.join_next_with_id(), if !self.join_set.is_empty() => {
                        let Some(joined) = joined else { continue };
                        self.complete_join(joined)?;
                    }
                    owners = changes => {
                        #[cfg(feature = "config-watch")]
                        self.reload_configurations(owners).await;
                        #[cfg(not(feature = "config-watch"))]
                        let _ = owners;
                    }
            }
        }
    }

    #[cfg(feature = "config-watch")]
    async fn reload_configurations(
        &mut self,
        changes: Vec<crate::watcher::ConfigurationChanged>,
    ) {
        let Some(state) = self.state.as_ref() else { return };
        let Some(watching) = self.configuration.as_mut() else { return };
        let components = match watching.prepare(state, changes).await {
            Ok(components) => components,
            Err(error) => {
                error!(%error, "configuration preparation rejected; previous snapshots retained");
                return;
            }
        };
        for component in components {
            let name = component.name();
            let result = if component.as_reloadable().is_some()
                && self.services.get(name).is_some_and(|service| {
                    Arc::ptr_eq(&service.component, &component)
                }) {
                let (reply, received) = oneshot::channel();
                // Already inside the runtime loop: call the command handler,
                // never enqueue a manager command and await ourselves.
                self.handle_prepared_command(
                    ServiceCommand::Reload { name: name.into(), reply },
                    true,
                )
                .await;
                received
                    .await
                    .map_err(|error| Error::msg(error.to_string()))
                    .and_then(|result| {
                        result
                            .map(|_| ())
                            .map_err(|error| Error::msg(error.to_string()))
                    })
            } else {
                let Some(state) = self.state.as_ref() else { continue };
                match component.as_configurable() {
                    Some(owner) => {
                        (self.config_reader.expect("watch binds reader"))(state)
                            .notify_changed(owner)
                            .await
                            .map_err(Error::from)
                    }
                    None => continue,
                }
            };
            if let Err(error) = result {
                error!(component = name, %error, "configuration reload rejected");
                continue;
            }
            // A reloaded configuration may declare programs differently.
            if let (Some(state), Some(environmental)) =
                (self.state.as_ref(), component.as_environmental())
            {
                let reader =
                    (self.config_reader.expect("watch binds reader"))(state);
                if let Err(error) = reader.prepare_programs(
                    (*component).type_id(),
                    environmental.program_environments(),
                ) {
                    error!(component = name, %error, "program environments rejected");
                }
            }
        }
        if let (Some(state), Some(watching)) =
            (self.state.as_ref(), self.configuration.as_mut())
        {
            watching.deliver_program_changes(state);
            if let Err(error) = watching.refresh_sources(state) {
                error!(%error, "configuration dependency watch update failed");
            }
        }
    }

    /// Abort and drain all remaining runnable tasks.
    pub async fn abort_and_drain(&mut self) {
        self.begin_shutdown();
        self.join_set.abort_all();
        while let Some(joined) = self.join_set.join_next_with_id().await {
            match joined {
                Ok((id, _)) => {
                    self.task_identities.remove(&id);
                }
                Err(error) => {
                    self.task_identities.remove(&error.id());
                }
            }
        }
        debug!("runtime aborted and drained remaining runnable tasks");
    }

    /// Wait for all remaining runnable tasks to finish on their own.
    ///
    /// The global shutdown token has already cancelled every generation.
    /// Each runnable owns its protocol-level graceful drain inside its future;
    /// this layer imposes no implicit deadline.
    pub async fn drain(&mut self) -> Result<()> {
        let mut first_error = None;
        while let Some(joined) = self.join_set.join_next_with_id().await {
            if let Err(error) = self.complete_join(joined) {
                error!(%error, "runnable failed during graceful drain");
                if first_error.is_none() {
                    first_error = Some(error);
                }
            }
        }
        debug!("runtime drained remaining runnable tasks");
        first_error.map_or(Ok(()), Err)
    }
}

#[cfg(test)]
#[path = "../tests/runtime/management.rs"]
mod tests;

#[cfg(test)]
#[path = "../tests/runtime/admission.rs"]
mod admission_tests;

#[cfg(test)]
#[path = "../tests/runtime/reload_activation.rs"]
mod reload_activation_tests;