taskvisor 0.8.0

In-process Tokio task supervisor with retries, graceful shutdown, reliable final outcomes, and per-key admission control
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
//! Assembles the configuration and resources for a stopped [`Supervisor`].
//!
//! [`SupervisorBuilder`] combines runtime limits, task defaults, subscribers, and optional controller settings.
//! Building validates bounded capacities, creates the runtime state and channels, and reserves subscriber ownership.
//! It does not spawn Tokio tasks.
//!
//! ```text
//! application
//!      │ SupervisorConfig + TaskDefaults + subscribers
//!//! SupervisorBuilder
//!//! stopped Supervisor
//!      │ run* or serve
//!//! running runtime
//! ```
//!
//! With subscribers, construction starts background cleanup workers before reading subscriber metadata.
//! These workers destroy retained user values outside Tokio runtime paths.
//! Without subscribers, they stay dormant until the first task or controller ownership admission.

use std::num::NonZeroUsize;
use std::sync::Arc;
use std::time::Duration;

use tokio::{sync, sync::mpsc};

use super::{
    deferred_drop,
    registry::Registry,
    runtime::{CoreSettings, SupervisorCore},
    supervisor::Supervisor,
};
use crate::{
    BuildError,
    core::{ConfigError, SupervisorConfig, TaskDefaults},
    events::Bus,
    subscribers::{Subscribe, SubscriberSet},
};

/// Collects all immutable settings used by one [`Supervisor`].
///
/// Use [`Supervisor::new`] when runtime configuration and subscribers are enough. Use this builder to change
/// inherited task settings, enable controller admission, or handle construction failure with [`try_build`](Self::try_build).
///
/// Setter calls consume and return the builder. Calling [`with_runtime_config`](Self::with_runtime_config) replaces changes
/// made by earlier runtime-setting shortcuts. Call [`build`](Self::build) for a convenience panic on failure
/// or [`try_build`](Self::try_build) for a typed [`BuildError`].
#[must_use]
pub struct SupervisorBuilder {
    runtime: SupervisorConfig,
    task_defaults: TaskDefaults,
    subscribers: Vec<Arc<dyn Subscribe>>,

    #[cfg(feature = "controller")]
    controller_config: Option<crate::controller::ControllerConfig>,
}

impl SupervisorBuilder {
    /// Creates a builder with runtime settings and [`TaskDefaults::default`].
    ///
    /// No subscribers or controller are configured initially.
    pub fn new(runtime: SupervisorConfig) -> Self {
        Self {
            runtime,
            task_defaults: TaskDefaults::default(),
            subscribers: Vec::new(),

            #[cfg(feature = "controller")]
            controller_config: None,
        }
    }

    /// Replaces all runtime settings stored by this builder.
    ///
    /// This also replaces values set by earlier runtime-setting shortcuts such as [`with_grace`](Self::with_grace).
    pub fn with_runtime_config(mut self, runtime: SupervisorConfig) -> Self {
        self.runtime = runtime;
        self
    }

    /// Replaces defaults used by inherited [`TaskSpec`](crate::TaskSpec) settings.
    pub fn with_task_defaults(mut self, task_defaults: TaskDefaults) -> Self {
        self.task_defaults = task_defaults;
        self
    }

    /// Sets the cooperative task-stop window before logical force-abort.
    ///
    /// See [`SupervisorConfig::with_grace`] for normalization and zero behavior.
    pub fn with_grace(mut self, grace: Duration) -> Self {
        self.runtime = self.runtime.with_grace(grace);
        self
    }

    /// Sets the shared deadline for draining subscriber queues during shutdown.
    ///
    /// See [`SupervisorConfig::with_subscriber_shutdown_timeout`] for the callback boundary.
    pub fn with_subscriber_shutdown_timeout(mut self, timeout: Duration) -> Self {
        self.runtime = self.runtime.with_subscriber_shutdown_timeout(timeout);
        self
    }

    /// Sets or clears the limit for task attempts running at the same time.
    ///
    /// [`try_build`](Self::try_build) validates the stored concurrency limit.
    pub fn with_max_concurrent(mut self, max_concurrent: impl Into<Option<NonZeroUsize>>) -> Self {
        self.runtime = self.runtime.with_max_concurrent(max_concurrent.into());
        self
    }

    /// Sets the task-attempt concurrency limit from a raw integer.
    ///
    /// # Errors
    ///
    /// Returns [`ConfigError::Zero`] for zero or [`ConfigError::TooLarge`] above Tokio's structural limit.
    pub fn try_with_max_concurrent(mut self, max_concurrent: usize) -> Result<Self, ConfigError> {
        self.runtime = self.runtime.try_with_max_concurrent(max_concurrent)?;
        Ok(self)
    }

    /// Sets or clears the registry membership limit.
    ///
    /// See [`SupervisorConfig::with_max_registered_tasks`] for which lifecycle phases consume the limit.
    pub fn with_max_registered_tasks(
        mut self,
        max_registered_tasks: impl Into<Option<NonZeroUsize>>,
    ) -> Self {
        self.runtime = self
            .runtime
            .with_max_registered_tasks(max_registered_tasks.into());
        self
    }

    /// Sets the registry membership limit from a raw integer.
    ///
    /// # Errors
    ///
    /// Returns [`ConfigError::Zero`] when `max_registered_tasks` is zero.
    pub fn try_with_max_registered_tasks(
        mut self,
        max_registered_tasks: usize,
    ) -> Result<Self, ConfigError> {
        self.runtime = self
            .runtime
            .try_with_max_registered_tasks(max_registered_tasks)?;
        Ok(self)
    }

    /// Sets or clears the limit for user lifetimes owned by this supervisor.
    ///
    /// See [`SupervisorConfig::with_ownership_capacity`] for the shared task and
    /// subscriber contract.
    pub fn with_ownership_capacity(
        mut self,
        ownership_capacity: impl Into<Option<NonZeroUsize>>,
    ) -> Self {
        self.runtime = self
            .runtime
            .with_ownership_capacity(ownership_capacity.into());
        self
    }

    /// Sets the user-lifetime ownership limit from a raw integer.
    ///
    /// # Errors
    ///
    /// Returns [`ConfigError::Zero`] when `ownership_capacity` is zero.
    pub fn try_with_ownership_capacity(
        mut self,
        ownership_capacity: usize,
    ) -> Result<Self, ConfigError> {
        self.runtime = self
            .runtime
            .try_with_ownership_capacity(ownership_capacity)?;
        Ok(self)
    }

    /// Sets the best-effort event-bus capacity from a raw integer.
    ///
    /// # Errors
    ///
    /// Returns [`ConfigError::Zero`] for zero or [`ConfigError::TooLarge`] above Tokio's structural limit.
    pub fn try_with_bus_capacity(mut self, bus_capacity: usize) -> Result<Self, ConfigError> {
        self.runtime = self.runtime.try_with_bus_capacity(bus_capacity)?;
        Ok(self)
    }

    /// Sets the bounded registry management-queue capacity.
    ///
    /// [`try_build`](Self::try_build) validates the stored queue capacity.
    pub fn with_registry_queue_capacity(mut self, registry_queue_capacity: NonZeroUsize) -> Self {
        self.runtime = self
            .runtime
            .with_registry_queue_capacity(registry_queue_capacity);
        self
    }

    /// Sets the registry management-queue capacity from a raw integer.
    ///
    /// # Errors
    ///
    /// Returns [`ConfigError::Zero`] for zero or [`ConfigError::TooLarge`] above Tokio's structural limit.
    pub fn try_with_registry_queue_capacity(
        mut self,
        registry_queue_capacity: usize,
    ) -> Result<Self, ConfigError> {
        self.runtime = self
            .runtime
            .try_with_registry_queue_capacity(registry_queue_capacity)?;
        Ok(self)
    }

    /// Sets how many newest events the best-effort event bus retains.
    ///
    /// [`try_build`](Self::try_build) validates the stored event capacity.
    pub fn with_bus_capacity(mut self, bus_capacity: NonZeroUsize) -> Self {
        self.runtime = self.runtime.with_bus_capacity(bus_capacity);
        self
    }

    /// Replaces all subscribers that receive best-effort lifecycle events.
    ///
    /// An empty vector disables the event bus and subscriber workers.
    pub fn with_subscribers(mut self, subscribers: Vec<Arc<dyn Subscribe>>) -> Self {
        self.subscribers = subscribers;
        self
    }

    /// Enables slot admission for `SupervisorHandle::submit*` methods.
    ///
    /// Without this setting, `submit*` methods return [`ControllerError::NotConfigured`](crate::ControllerError::NotConfigured).
    /// Direct `add*` methods always bypass the controller and register with the runtime.
    #[cfg(feature = "controller")]
    #[cfg_attr(docsrs, doc(cfg(feature = "controller")))]
    pub fn with_controller(mut self, config: crate::controller::ControllerConfig) -> Self {
        self.controller_config = Some(config);
        self
    }

    /// Builds a stopped supervisor and panics if construction fails.
    ///
    /// This method is safe to call outside Tokio and does not spawn Tokio tasks.
    /// Use [`try_build`](Self::try_build) when the application must report or recover from resource and capacity failures.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use std::time::Duration;
    /// use taskvisor::{SupervisorBuilder, SupervisorConfig};
    ///
    /// let supervisor = SupervisorBuilder::new(SupervisorConfig::default())
    ///     .with_grace(Duration::from_secs(15))
    ///     .build();
    ///
    /// assert_eq!(
    ///     supervisor.runtime_config().grace(),
    ///     Duration::from_secs(15)
    /// );
    /// ```
    ///
    /// # Panics
    ///
    /// Panics when [`try_build`](Self::try_build) would return an error.
    /// A panic from [`Subscribe::name`] or [`Subscribe::queue_capacity`] also reaches the caller.
    #[must_use]
    pub fn build(self) -> Arc<Supervisor> {
        self.try_build().unwrap_or_else(|error| {
            panic!(
                "SupervisorBuilder::build rejected its configuration: {error}; use SupervisorBuilder::try_build for a typed error"
            )
        })
    }

    /// Builds a stopped supervisor and returns typed construction failures.
    ///
    /// This method is safe to call outside Tokio and does not spawn Tokio tasks.
    /// It reserves all subscriber ownership slots as one batch before calling [`Subscribe::name`] or [`Subscribe::queue_capacity`].
    /// A rejected batch calls neither method and keeps no ownership slots.
    ///
    /// # Errors
    ///
    /// - [`BuildError::ResourceLimitReached`] when the supervisor cannot own every configured subscriber.
    /// - [`BuildError::ThreadStartFailed`] when background cleanup workers cannot start.
    /// - [`BuildError::CapacityTooLarge`] when a runtime, controller, or subscriber capacity exceeds Tokio's structural limit.
    ///
    /// # Panics
    ///
    /// A panic from [`Subscribe::name`] or [`Subscribe::queue_capacity`] reaches the caller after Taskvisor reserves
    /// subscriber ownership for deferred cleanup.
    pub fn try_build(self) -> Result<Arc<Supervisor>, BuildError> {
        self.validate_configuration()?;
        let drop_domain = deferred_drop::DropDomain::unstarted(self.runtime.ownership_capacity());
        let reservations = drop_domain
            .try_reserve_many(self.subscribers.len())
            .map_err(Self::ownership_admission_build_error)?;
        self.build_with_reservations(drop_domain, reservations)
    }

    #[cfg(test)]
    fn try_build_with_reservation_source(
        self,
        source: &deferred_drop::TestReservationSource,
    ) -> Result<Arc<Supervisor>, BuildError> {
        self.validate_configuration()?;
        let reservations = source
            .try_reserve_many(self.subscribers.len())
            .map_err(Self::ownership_build_error)?;
        self.build_with_reservations(source.domain(), reservations)
    }

    #[cfg(test)]
    fn try_build_with_drop_domain(
        self,
        drop_domain: deferred_drop::DropDomain,
    ) -> Result<Arc<Supervisor>, BuildError> {
        self.validate_configuration()?;
        let reservations = drop_domain
            .try_reserve_many(self.subscribers.len())
            .map_err(Self::ownership_admission_build_error)?;
        self.build_with_reservations(drop_domain, reservations)
    }

    fn validate_configuration(&self) -> Result<(), BuildError> {
        self.runtime
            .validate()
            .map_err(Self::configuration_build_error)?;
        #[cfg(feature = "controller")]
        if let Some(config) = &self.controller_config {
            config.validate().map_err(Self::configuration_build_error)?;
        }
        Ok(())
    }

    fn configuration_build_error(error: ConfigError) -> BuildError {
        match error {
            ConfigError::TooLarge { field, value, max } => {
                BuildError::CapacityTooLarge { field, value, max }
            }
            ConfigError::Zero { .. } => {
                unreachable!("stored capacities use NonZeroUsize")
            }
        }
    }

    fn ownership_build_error(error: deferred_drop::DropCapacityError) -> BuildError {
        let limit = error
            .limit()
            .expect("a fresh unlimited ownership domain cannot reject capacity");
        BuildError::ResourceLimitReached {
            resource: deferred_drop::OWNERSHIP_RESOURCE,
            limit: limit.get(),
        }
    }

    fn ownership_admission_build_error(error: deferred_drop::DropAdmissionError) -> BuildError {
        match error {
            deferred_drop::DropAdmissionError::Start(error) => Self::drop_domain_build_error(error),
            deferred_drop::DropAdmissionError::Capacity(error) => {
                Self::ownership_build_error(error)
            }
        }
    }

    fn drop_domain_build_error(error: deferred_drop::DropStartError) -> BuildError {
        BuildError::ThreadStartFailed {
            component: "destructor_isolation",
            worker: error.worker(),
            kind: error.source_kind(),
            raw_os_error: error.raw_os_error(),
        }
    }

    fn build_with_reservations(
        self,
        drop_domain: deferred_drop::DropDomain,
        reservations: Vec<deferred_drop::DropReservation>,
    ) -> Result<Arc<Supervisor>, BuildError> {
        let bus = Bus::new(self.runtime.bus_capacity().get());
        let subs = Arc::new(SubscriberSet::from_reserved(
            self.subscribers,
            reservations,
            bus.clone(),
            self.runtime.subscriber_shutdown_timeout(),
        )?);
        let runtime_token = tokio_util::sync::CancellationToken::new();

        let semaphore = self
            .runtime
            .max_concurrent()
            .map(|limit| Arc::new(sync::Semaphore::new(limit.get())));

        let (cmd_tx, cmd_rx) = mpsc::channel(self.runtime.registry_queue_capacity().get());
        let registry = Registry::new(
            bus.clone(),
            runtime_token.clone(),
            semaphore,
            self.runtime.grace(),
            self.task_defaults.clone(),
            self.runtime.max_registered_tasks(),
            cmd_rx,
        );
        let core = SupervisorCore::new_internal(
            CoreSettings::new(self.runtime, self.task_defaults),
            bus.clone(),
            subs,
            registry,
            drop_domain,
            runtime_token,
            cmd_tx,
        );

        #[cfg(feature = "controller")]
        let controller = self
            .controller_config
            .map(|config| crate::controller::Controller::new(config, &core, bus.clone()));

        #[cfg(feature = "controller")]
        if let Some(controller) = &controller {
            core.attach_controller(controller);
        }

        Ok(Supervisor::from_parts(
            core,
            #[cfg(feature = "controller")]
            controller,
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{BackoffPolicy, RestartPolicy};
    use std::num::NonZeroU32;
    use std::sync::atomic::{AtomicUsize, Ordering};

    struct MetadataProbe {
        name_calls: Arc<AtomicUsize>,
        capacity_calls: Arc<AtomicUsize>,
    }

    impl Subscribe for MetadataProbe {
        fn on_event(&self, _event: &crate::Event) {}

        fn name(&self) -> &str {
            self.name_calls.fetch_add(1, Ordering::AcqRel);
            "metadata-probe"
        }

        fn queue_capacity(&self) -> NonZeroUsize {
            self.capacity_calls.fetch_add(1, Ordering::AcqRel);
            NonZeroUsize::new(8).expect("test capacity is non-zero")
        }
    }

    struct OversizedQueueSubscriber;

    impl Subscribe for OversizedQueueSubscriber {
        fn on_event(&self, _event: &crate::Event) {}

        fn queue_capacity(&self) -> NonZeroUsize {
            NonZeroUsize::new(crate::core::MAX_ASYNC_CAPACITY + 1)
                .expect("the excessive test value is non-zero")
        }
    }

    struct StartupOrderingProbe {
        drop_domain: deferred_drop::DropDomain,
        name_calls: Arc<AtomicUsize>,
        capacity_calls: Arc<AtomicUsize>,
    }

    impl StartupOrderingProbe {
        fn observe_started_domain(&self) {
            assert!(
                self.drop_domain.is_started(),
                "subscriber metadata must run only after the destructor-isolation core is published"
            );
        }
    }

    impl Subscribe for StartupOrderingProbe {
        fn on_event(&self, _event: &crate::Event) {}

        fn name(&self) -> &str {
            self.observe_started_domain();
            self.name_calls.fetch_add(1, Ordering::AcqRel);
            "startup-ordering-probe"
        }

        fn queue_capacity(&self) -> NonZeroUsize {
            self.observe_started_domain();
            self.capacity_calls.fetch_add(1, Ordering::AcqRel);
            NonZeroUsize::new(8).expect("test capacity is non-zero")
        }
    }

    #[test]
    fn builder_keeps_runtime_and_task_defaults_separate() {
        let runtime = SupervisorConfig::default()
            .with_grace(Duration::from_secs(30))
            .with_subscriber_shutdown_timeout(Duration::from_secs(2))
            .with_max_concurrent(NonZeroUsize::new(4))
            .with_ownership_capacity(NonZeroUsize::new(64))
            .with_bus_capacity(NonZeroUsize::new(2048).unwrap())
            .with_registry_queue_capacity(NonZeroUsize::new(256).unwrap());
        let task_defaults = TaskDefaults::default()
            .with_restart(RestartPolicy::Never)
            .with_backoff(BackoffPolicy::constant(Duration::from_millis(50)))
            .with_timeout(Duration::from_secs(5))
            .with_max_retries(NonZeroU32::new(10));

        let builder =
            SupervisorBuilder::new(runtime.clone()).with_task_defaults(task_defaults.clone());

        assert_eq!(builder.runtime.grace(), runtime.grace());
        assert_eq!(
            builder.runtime.subscriber_shutdown_timeout(),
            runtime.subscriber_shutdown_timeout()
        );
        assert_eq!(builder.runtime.max_concurrent(), runtime.max_concurrent());
        assert_eq!(
            builder.runtime.ownership_capacity(),
            runtime.ownership_capacity()
        );
        assert_eq!(builder.runtime.bus_capacity(), runtime.bus_capacity());
        assert_eq!(
            builder.runtime.registry_queue_capacity(),
            runtime.registry_queue_capacity()
        );
        assert!(matches!(
            builder.task_defaults.restart(),
            RestartPolicy::Never
        ));
        assert_eq!(
            builder.task_defaults.backoff().first(),
            Duration::from_millis(50)
        );
        assert_eq!(
            builder.task_defaults.timeout(),
            Some(Duration::from_secs(5))
        );
        assert_eq!(
            builder.task_defaults.max_retries().map(NonZeroU32::get),
            Some(10)
        );
    }

    #[test]
    fn raw_zero_values_return_errors_instead_of_panicking() {
        type RawSetter = fn(SupervisorBuilder, usize) -> Result<SupervisorBuilder, ConfigError>;
        let cases: [(&str, RawSetter); 4] = [
            ("max_concurrent", SupervisorBuilder::try_with_max_concurrent),
            (
                "ownership_capacity",
                SupervisorBuilder::try_with_ownership_capacity,
            ),
            ("bus_capacity", SupervisorBuilder::try_with_bus_capacity),
            (
                "registry_queue_capacity",
                SupervisorBuilder::try_with_registry_queue_capacity,
            ),
        ];

        for (field, set) in cases {
            assert!(matches!(
                set(SupervisorBuilder::new(SupervisorConfig::default()), 0),
                Err(ConfigError::Zero { field: actual }) if actual == field
            ));
        }
    }

    #[test]
    fn max_concurrent_accepts_a_limit_or_an_option() {
        let limit = NonZeroUsize::new(4).unwrap();
        let direct = SupervisorBuilder::new(SupervisorConfig::default()).with_max_concurrent(limit);
        let optional =
            SupervisorBuilder::new(SupervisorConfig::default()).with_max_concurrent(Some(limit));
        let cleared = SupervisorBuilder::new(SupervisorConfig::default()).with_max_concurrent(None);

        assert_eq!(direct.runtime.max_concurrent(), Some(limit));
        assert_eq!(optional.runtime.max_concurrent(), Some(limit));
        assert_eq!(cleared.runtime.max_concurrent(), None);
    }

    #[test]
    fn ownership_capacity_accepts_a_limit_or_an_option() {
        let limit = NonZeroUsize::new(4).unwrap();
        let direct =
            SupervisorBuilder::new(SupervisorConfig::default()).with_ownership_capacity(limit);
        let optional = SupervisorBuilder::new(SupervisorConfig::default())
            .with_ownership_capacity(Some(limit));
        let cleared =
            SupervisorBuilder::new(SupervisorConfig::default()).with_ownership_capacity(None);

        assert_eq!(direct.runtime.ownership_capacity(), Some(limit));
        assert_eq!(optional.runtime.ownership_capacity(), Some(limit));
        assert_eq!(cleared.runtime.ownership_capacity(), None);
    }

    #[test]
    fn subscriber_free_build_leaves_destructor_isolation_dormant() {
        let supervisor = SupervisorBuilder::new(SupervisorConfig::default())
            .try_build()
            .expect("a subscriber-free supervisor must build");

        assert!(!supervisor.core().drop_domain().is_started());
    }

    #[test]
    fn subscriber_build_starts_destructor_isolation_before_metadata() {
        let drop_domain =
            deferred_drop::DropDomain::unstarted(SupervisorConfig::default().ownership_capacity());
        let name_calls = Arc::new(AtomicUsize::new(0));
        let capacity_calls = Arc::new(AtomicUsize::new(0));
        let subscriber: Arc<dyn Subscribe> = Arc::new(StartupOrderingProbe {
            drop_domain: drop_domain.clone(),
            name_calls: Arc::clone(&name_calls),
            capacity_calls: Arc::clone(&capacity_calls),
        });

        let supervisor = SupervisorBuilder::new(SupervisorConfig::default())
            .with_subscribers(vec![subscriber])
            .try_build_with_drop_domain(drop_domain.clone())
            .expect("subscriber ownership must start the destructor-isolation core");

        assert!(drop_domain.is_started());
        assert!(supervisor.core().drop_domain().is_started());
        assert_eq!(name_calls.load(Ordering::Acquire), 1);
        assert_eq!(capacity_calls.load(Ordering::Acquire), 1);
    }

    #[test]
    fn subscriber_core_start_failure_is_typed_and_skips_metadata() {
        let injected = deferred_drop::TestLazyDomain::fail_first_start_at_worker(2, 1);
        let drop_domain = injected.domain();
        let name_calls = Arc::new(AtomicUsize::new(0));
        let capacity_calls = Arc::new(AtomicUsize::new(0));
        let subscriber: Arc<dyn Subscribe> = Arc::new(MetadataProbe {
            name_calls: Arc::clone(&name_calls),
            capacity_calls: Arc::clone(&capacity_calls),
        });

        let result = SupervisorBuilder::new(SupervisorConfig::default())
            .with_subscribers(vec![subscriber])
            .try_build_with_drop_domain(drop_domain.clone());

        match result {
            Err(BuildError::ThreadStartFailed {
                component,
                worker,
                kind,
                raw_os_error,
                ..
            }) => {
                assert_eq!(component, "destructor_isolation");
                assert_eq!(worker, 1);
                assert_eq!(kind, std::io::ErrorKind::Other);
                assert_eq!(raw_os_error, None);
            }
            Err(error) => panic!("expected a typed core-start failure, got {error}"),
            Ok(_) => panic!("the injected first core startup must fail"),
        }
        assert_eq!(name_calls.load(Ordering::Acquire), 0);
        assert_eq!(capacity_calls.load(Ordering::Acquire), 0);
        assert!(!drop_domain.is_started());
        assert_eq!(injected.spawn_calls(), 2);
    }

    #[test]
    fn try_build_reserves_the_complete_subscriber_batch_before_subscriber_metadata() {
        let source = deferred_drop::TestReservationSource::new(1);
        let name_calls = Arc::new(AtomicUsize::new(0));
        let capacity_calls = Arc::new(AtomicUsize::new(0));
        let subscribers: Vec<Arc<dyn Subscribe>> = (0..2)
            .map(|_| {
                Arc::new(MetadataProbe {
                    name_calls: Arc::clone(&name_calls),
                    capacity_calls: Arc::clone(&capacity_calls),
                }) as Arc<dyn Subscribe>
            })
            .collect();

        let result = SupervisorBuilder::new(SupervisorConfig::default())
            .with_subscribers(subscribers)
            .try_build_with_reservation_source(&source);

        assert!(matches!(
            result,
            Err(BuildError::ResourceLimitReached {
                resource: deferred_drop::OWNERSHIP_RESOURCE,
                limit: 1,
                ..
            })
        ));
        assert_eq!(name_calls.load(Ordering::Acquire), 0);
        assert_eq!(capacity_calls.load(Ordering::Acquire), 0);
        let untouched = source
            .try_reserve()
            .expect("an atomic rejection cannot retain partial capacity");
        drop(untouched);
    }

    #[test]
    fn configured_ownership_limit_rejects_subscribers_before_metadata() {
        let name_calls = Arc::new(AtomicUsize::new(0));
        let capacity_calls = Arc::new(AtomicUsize::new(0));
        let subscribers: Vec<Arc<dyn Subscribe>> = (0..2)
            .map(|_| {
                Arc::new(MetadataProbe {
                    name_calls: Arc::clone(&name_calls),
                    capacity_calls: Arc::clone(&capacity_calls),
                }) as Arc<dyn Subscribe>
            })
            .collect();

        let result = SupervisorBuilder::new(
            SupervisorConfig::default().with_ownership_capacity(NonZeroUsize::new(1)),
        )
        .with_subscribers(subscribers)
        .try_build();

        assert!(matches!(
            result,
            Err(BuildError::ResourceLimitReached {
                resource: deferred_drop::OWNERSHIP_RESOURCE,
                limit: 1,
                ..
            })
        ));
        assert_eq!(name_calls.load(Ordering::Acquire), 0);
        assert_eq!(capacity_calls.load(Ordering::Acquire), 0);
    }

    #[test]
    fn disabled_ownership_limit_accepts_the_same_subscriber_batch() {
        let name_calls = Arc::new(AtomicUsize::new(0));
        let capacity_calls = Arc::new(AtomicUsize::new(0));
        let subscribers: Vec<Arc<dyn Subscribe>> = (0..2)
            .map(|_| {
                Arc::new(MetadataProbe {
                    name_calls: Arc::clone(&name_calls),
                    capacity_calls: Arc::clone(&capacity_calls),
                }) as Arc<dyn Subscribe>
            })
            .collect();

        let supervisor =
            SupervisorBuilder::new(SupervisorConfig::default().with_ownership_capacity(None))
                .with_subscribers(subscribers)
                .try_build()
                .expect("an unlimited ownership domain must accept the subscriber batch");

        assert_eq!(supervisor.runtime_config().ownership_capacity(), None);
        assert_eq!(name_calls.load(Ordering::Acquire), 2);
        assert_eq!(capacity_calls.load(Ordering::Acquire), 2);
    }

    #[test]
    fn try_build_rejects_structurally_invalid_runtime_capacities() {
        let excessive = NonZeroUsize::new(crate::core::MAX_ASYNC_CAPACITY + 1)
            .expect("the excessive test value is non-zero");
        let cases = [
            (
                SupervisorConfig::default().with_max_concurrent(Some(excessive)),
                "max_concurrent",
            ),
            (
                SupervisorConfig::default().with_bus_capacity(excessive),
                "bus_capacity",
            ),
            (
                SupervisorConfig::default().with_registry_queue_capacity(excessive),
                "registry_queue_capacity",
            ),
        ];

        for (runtime, field) in cases {
            assert!(matches!(
                SupervisorBuilder::new(runtime).try_build(),
                Err(BuildError::CapacityTooLarge {
                    field: actual,
                    value,
                    max: crate::core::MAX_ASYNC_CAPACITY,
                    ..
                }) if actual == field && value == excessive.get()
            ));
        }
    }

    #[cfg(feature = "controller")]
    #[test]
    fn try_build_rejects_structurally_invalid_controller_capacity() {
        let excessive = NonZeroUsize::new(crate::core::MAX_ASYNC_CAPACITY + 1)
            .expect("the excessive test value is non-zero");
        let config = crate::ControllerConfig::default().with_queue_capacity(excessive);

        assert!(matches!(
            SupervisorBuilder::new(SupervisorConfig::default())
                .with_controller(config)
                .try_build(),
            Err(BuildError::CapacityTooLarge {
                field: "controller_queue_capacity",
                value,
                max: crate::core::MAX_ASYNC_CAPACITY,
                ..
            }) if value == excessive.get()
        ));
    }

    #[test]
    fn try_build_rejects_oversized_subscriber_queue_before_runtime_start() {
        let source = deferred_drop::TestReservationSource::new(1);
        let result = SupervisorBuilder::new(SupervisorConfig::default())
            .with_subscribers(vec![Arc::new(OversizedQueueSubscriber)])
            .try_build_with_reservation_source(&source);

        assert!(matches!(
            result,
            Err(BuildError::CapacityTooLarge {
                field: "subscriber_queue_capacity",
                value,
                max: crate::core::MAX_ASYNC_CAPACITY,
                ..
            }) if value == crate::core::MAX_ASYNC_CAPACITY + 1
        ));
    }
}