cordis-core 0.0.2

A typed, scope-based plugin runtime inspired by Cordis
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
use std::sync::{
    Arc,
    atomic::{AtomicBool, Ordering},
};

use futures::{FutureExt, future::BoxFuture};
use tokio::sync::{mpsc, oneshot, watch};

use crate::{
    Context, Error, Plugin, PluginContext, PluginHandle, PluginStatus, Result,
    plugin::{ActivationId, FailurePhase, PluginCommand, PluginId},
    runtime::{DependencySnapshot, Runtime},
    scope::ScopeInner,
};

type ApplyFn = Arc<dyn Fn(PluginContext) -> BoxFuture<'static, Result<()>> + Send + Sync>;

struct Activation {
    scope: Arc<ScopeInner>,
    snapshot: DependencySnapshot,
}

#[derive(Default)]
pub(crate) struct ControlPlane {
    registrations: std::sync::Mutex<Vec<RegistrationRecord>>,
    transition: tokio::sync::Mutex<()>,
}

#[derive(Clone)]
struct RegistrationRecord {
    id: PluginId,
    name: &'static str,
    dependencies: Vec<crate::Dependency>,
    provides: Vec<crate::ServiceDeclaration>,
    commands: mpsc::Sender<PluginCommand>,
    status: watch::Receiver<PluginStatus>,
}

/// Root runtime and owner of every persistent plugin registration.
pub struct App {
    runtime: Arc<Runtime>,
    root: Arc<ScopeInner>,
    control: Arc<ControlPlane>,
    started: AtomicBool,
    shutdown: AtomicBool,
}

impl App {
    pub fn new() -> Self {
        let runtime = Runtime::new();
        let root = ScopeInner::new(runtime.next_id(), "application");
        Self {
            runtime,
            root,
            control: Arc::new(ControlPlane::default()),
            started: AtomicBool::new(false),
            shutdown: AtomicBool::new(false),
        }
    }

    pub fn context(&self) -> Context {
        Context::root(self.runtime.clone())
    }

    pub fn is_started(&self) -> bool {
        self.started.load(Ordering::Acquire)
    }

    /// Registers a plugin and performs its first reconciliation.
    ///
    /// Missing required services produce a suspended handle, not an error.
    /// The same registration is automatically reactivated when declared
    /// service generations appear, disappear, or change.
    pub async fn install<P: Plugin>(&self, plugin: P, config: P::Config) -> Result<PluginHandle> {
        let name = plugin.name();
        let dependencies = plugin.dependencies();
        let provides = plugin.provides();
        let plugin = Arc::new(plugin);
        let config = Arc::new(config);
        let apply: ApplyFn = Arc::new(move |ctx| {
            let plugin = plugin.clone();
            let config = config.clone();
            Box::pin(async move { plugin.apply(ctx, config).await })
        });
        self.install_apply(name, dependencies, provides, apply)
            .await
    }

    pub async fn install_erased(
        &self,
        plugin: Arc<dyn crate::ErasedPlugin>,
        config: crate::ErasedConfig,
    ) -> Result<PluginHandle> {
        let name = plugin.name();
        let dependencies = plugin.dependencies();
        let provides = plugin.provides();
        let apply: ApplyFn = Arc::new(move |ctx| plugin.apply(ctx, config.clone()));
        self.install_apply(name, dependencies, provides, apply)
            .await
    }

    async fn install_apply(
        &self,
        name: &'static str,
        dependencies: Vec<crate::Dependency>,
        provides: Vec<crate::ServiceDeclaration>,
        apply: ApplyFn,
    ) -> Result<PluginHandle> {
        if self.shutdown.load(Ordering::Acquire) {
            return Err(Error::ApplicationShutdown);
        }
        self.ensure_acyclic(name, &dependencies, &provides)?;
        let id = PluginId(self.runtime.next_id());
        let initial = PluginStatus::Suspended {
            missing: dependencies
                .iter()
                .filter(|dependency| dependency.required)
                .map(|dependency| dependency.name)
                .collect::<Vec<_>>()
                .into(),
        };
        let (status_tx, status_rx) = watch::channel(initial.clone());
        let diagnostics = Arc::new(std::sync::Mutex::new(vec![crate::PluginDiagnostic {
            at: std::time::SystemTime::now(),
            status: initial,
        }]));
        let diagnostic_log = diagnostics.clone();
        let mut diagnostic_status = status_rx.clone();
        tokio::spawn(async move {
            while diagnostic_status.changed().await.is_ok() {
                diagnostic_log
                    .lock()
                    .expect("diagnostics lock poisoned")
                    .push(crate::PluginDiagnostic {
                        at: std::time::SystemTime::now(),
                        status: diagnostic_status.borrow().clone(),
                    });
            }
        });
        let (command_tx, command_rx) = mpsc::channel(16);
        let (initialized_tx, initialized_rx) = oneshot::channel();
        let services = self.runtime.subscribe_services();
        let runtime = self.runtime.clone();
        let control = self.control.clone();

        tokio::spawn(run_plugin(
            runtime,
            control,
            id,
            name,
            dependencies.clone(),
            provides.clone(),
            apply,
            command_rx,
            services,
            status_tx,
            initialized_tx,
        ));

        let shutdown_tx = command_tx.clone();
        if let Err(error) = self.root.push(Box::new(move || {
            Box::pin(async move {
                let (reply_tx, reply_rx) = oneshot::channel();
                if shutdown_tx
                    .send(PluginCommand::Dispose(reply_tx))
                    .await
                    .is_err()
                {
                    return Ok(());
                }
                reply_rx.await.unwrap_or(Ok(()))
            })
        })) {
            let (reply_tx, _) = oneshot::channel();
            let _ = command_tx.send(PluginCommand::Dispose(reply_tx)).await;
            return Err(error);
        }

        initialized_rx.await.map_err(|_| Error::PluginDisposed)??;
        self.control
            .registrations
            .lock()
            .expect("registration lock poisoned")
            .push(RegistrationRecord {
                id,
                name,
                dependencies,
                provides,
                commands: command_tx.clone(),
                status: status_rx.clone(),
            });
        Ok(PluginHandle::new(
            id,
            name,
            command_tx,
            status_rx,
            diagnostics,
            self.control.clone(),
        ))
    }

    fn ensure_acyclic(
        &self,
        name: &'static str,
        dependencies: &[crate::Dependency],
        provides: &[crate::ServiceDeclaration],
    ) -> Result<()> {
        let records = self
            .control
            .registrations
            .lock()
            .expect("registration lock poisoned");
        let mut declarations: Vec<_> = records
            .iter()
            .filter(|record| !matches!(*record.status.borrow(), PluginStatus::Disposed))
            .map(|record| {
                (
                    record.name,
                    record.dependencies.clone(),
                    record.provides.clone(),
                )
            })
            .collect();
        declarations.push((name, dependencies.to_vec(), provides.to_vec()));
        if topological_order(&declarations).len() != declarations.len() {
            let names = declarations
                .iter()
                .map(|(name, _, _)| *name)
                .collect::<Vec<_>>()
                .join(" -> ");
            return Err(Error::DependencyCycle(names));
        }
        Ok(())
    }

    pub async fn start(&self) -> Result<()> {
        if self.shutdown.load(Ordering::Acquire) {
            return Err(Error::ApplicationShutdown);
        }
        if !self.started.swap(true, Ordering::AcqRel) {
            self.context().emit(crate::Ready).await?;
        }
        Ok(())
    }

    /// Disposes consumers before their declared providers, independent of
    /// installation order, then clears application-owned resources.
    pub async fn shutdown(&self) -> Result<()> {
        if self.shutdown.swap(true, Ordering::AcqRel) {
            return Ok(());
        }

        let _transition = self.control.transition.lock().await;
        let records = self
            .control
            .registrations
            .lock()
            .expect("registration lock poisoned")
            .clone();
        let declarations = records
            .iter()
            .map(|record| {
                (
                    record.name,
                    record.dependencies.clone(),
                    record.provides.clone(),
                )
            })
            .collect::<Vec<_>>();
        let mut first_error = None;
        for index in topological_order(&declarations).into_iter().rev() {
            let record = &records[index];
            if matches!(*record.status.borrow(), PluginStatus::Disposed) {
                continue;
            }
            let (reply_tx, reply_rx) = oneshot::channel();
            if record
                .commands
                .send(PluginCommand::Dispose(reply_tx))
                .await
                .is_ok()
            {
                if let Ok(Err(error)) = reply_rx.await {
                    first_error.get_or_insert(error);
                }
            }
        }
        if let Err(error) = self.root.dispose().await {
            first_error.get_or_insert(error);
        }
        first_error.map_or(Ok(()), Err)
    }
}

impl Default for App {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Clone, Copy)]
enum ControlAction {
    Reload,
    Quiesce,
    Resume,
    Dispose,
}

async fn send_control(sender: &mpsc::Sender<PluginCommand>, action: ControlAction) -> Result<()> {
    let (reply_tx, reply_rx) = oneshot::channel();
    let command = match action {
        ControlAction::Reload => PluginCommand::Reload(reply_tx),
        ControlAction::Quiesce => PluginCommand::Quiesce(reply_tx),
        ControlAction::Resume => PluginCommand::Resume(reply_tx),
        ControlAction::Dispose => PluginCommand::Dispose(reply_tx),
    };
    sender
        .send(command)
        .await
        .map_err(|_| Error::PluginDisposed)?;
    reply_rx.await.map_err(|_| Error::PluginDisposed)?
}

impl ControlPlane {
    pub(crate) async fn reload(&self, id: PluginId) -> Result<()> {
        self.transition(id, false).await
    }

    pub(crate) async fn dispose(&self, id: PluginId) -> Result<()> {
        self.transition(id, true).await
    }

    async fn transition(&self, id: PluginId, disposing: bool) -> Result<()> {
        let _guard = self.transition.lock().await;
        let records = self
            .registrations
            .lock()
            .expect("registration lock poisoned")
            .clone();
        let target = records
            .iter()
            .position(|record| record.id == id)
            .ok_or(Error::PluginDisposed)?;
        let declarations = records
            .iter()
            .map(|record| {
                (
                    record.name,
                    record.dependencies.clone(),
                    record.provides.clone(),
                )
            })
            .collect::<Vec<_>>();
        let order = topological_order(&declarations);
        let dependents = dependent_indices(&declarations, target);

        for index in order.iter().rev().copied() {
            if dependents.contains(&index)
                && !matches!(*records[index].status.borrow(), PluginStatus::Disposed)
            {
                send_control(&records[index].commands, ControlAction::Quiesce).await?;
            }
        }

        let target_action = if disposing {
            ControlAction::Dispose
        } else {
            ControlAction::Reload
        };
        let target_result = send_control(&records[target].commands, target_action).await;

        for index in order {
            if dependents.contains(&index)
                && !matches!(*records[index].status.borrow(), PluginStatus::Disposed)
            {
                let _ = send_control(&records[index].commands, ControlAction::Resume).await;
            }
        }
        target_result
    }
}

fn graph_plan(
    control: &ControlPlane,
    id: PluginId,
) -> Result<(
    Vec<RegistrationRecord>,
    Vec<usize>,
    std::collections::HashSet<usize>,
)> {
    let records = control
        .registrations
        .lock()
        .expect("registration lock poisoned")
        .clone();
    let target = records
        .iter()
        .position(|record| record.id == id)
        .ok_or(Error::PluginDisposed)?;
    let declarations = records
        .iter()
        .map(|record| {
            (
                record.name,
                record.dependencies.clone(),
                record.provides.clone(),
            )
        })
        .collect::<Vec<_>>();
    let order = topological_order(&declarations);
    let dependents = dependent_indices(&declarations, target);
    Ok((records, order, dependents))
}

async fn quiesce_plan(
    records: &[RegistrationRecord],
    order: &[usize],
    dependents: &std::collections::HashSet<usize>,
) -> Result<()> {
    for index in order.iter().rev().copied() {
        if dependents.contains(&index)
            && !matches!(*records[index].status.borrow(), PluginStatus::Disposed)
        {
            send_control(&records[index].commands, ControlAction::Quiesce).await?;
        }
    }
    Ok(())
}

async fn resume_plan(
    records: &[RegistrationRecord],
    order: &[usize],
    dependents: &std::collections::HashSet<usize>,
) {
    for index in order.iter().copied() {
        if dependents.contains(&index)
            && !matches!(*records[index].status.borrow(), PluginStatus::Disposed)
        {
            let _ = send_control(&records[index].commands, ControlAction::Resume).await;
        }
    }
}

#[allow(clippy::too_many_arguments)]
async fn run_plugin(
    runtime: Arc<Runtime>,
    control: Arc<ControlPlane>,
    id: PluginId,
    name: &'static str,
    dependencies: Vec<crate::Dependency>,
    provides: Vec<crate::ServiceDeclaration>,
    apply: ApplyFn,
    mut commands: mpsc::Receiver<PluginCommand>,
    mut services: watch::Receiver<u64>,
    status: watch::Sender<PluginStatus>,
    initialized: oneshot::Sender<Result<()>>,
) {
    let mut active = None;
    let mut failed_generations = None;
    let mut quiesced = false;
    let initial = reconcile(
        &runtime,
        id,
        name,
        &dependencies,
        &apply,
        &status,
        &mut active,
        &mut failed_generations,
        false,
    )
    .await;
    let _ = initialized.send(Ok(()));
    if initial.is_err() {
        // The status channel contains the actionable failure.
    }

    loop {
        tokio::select! {
            biased;
            command = commands.recv() => {
                let Some(command) = command else { break };
                match command {
                    PluginCommand::Reload(reply) | PluginCommand::Retry(reply) => {
                        let result = reconcile(
                            &runtime, id, name, &dependencies, &apply, &status,
                            &mut active, &mut failed_generations, true,
                        ).await;
                        let _ = reply.send(result);
                    }
                    PluginCommand::Quiesce(reply) => {
                        quiesced = true;
                        let result = dispose_activation(&runtime, id, &status, &mut active).await;
                        if result.is_ok() {
                            status.send_replace(PluginStatus::Suspended { missing: Arc::new([]) });
                        }
                        let _ = reply.send(result);
                    }
                    PluginCommand::Resume(reply) => {
                        quiesced = false;
                        let result = reconcile(
                            &runtime, id, name, &dependencies, &apply, &status,
                            &mut active, &mut failed_generations, false,
                        ).await;
                        let _ = reply.send(result);
                    }
                    PluginCommand::Dispose(reply) => {
                        let result = dispose_activation(&runtime, id, &status, &mut active).await;
                        status.send_replace(PluginStatus::Disposed);
                        let _ = reply.send(result);
                        break;
                    }
                }
            }
            changed = services.changed() => {
                if changed.is_err() { break; }
                tokio::time::sleep(std::time::Duration::from_millis(1)).await;
                while services.has_changed().unwrap_or(false) {
                    services.borrow_and_update();
                }
                if quiesced { continue; }
                let snapshot = runtime.dependency_snapshot(&dependencies);
                let changes_active_provider = active.as_ref().is_some_and(|activation| {
                    activation.snapshot.generations != snapshot.generations
                        || !snapshot.missing.is_empty()
                });
                if changes_active_provider && !provides.is_empty() {
                    let _transition = control.transition.lock().await;
                    if let Ok((records, order, dependents)) = graph_plan(&control, id) {
                        let _ = quiesce_plan(&records, &order, &dependents).await;
                        let _ = reconcile(
                            &runtime, id, name, &dependencies, &apply, &status,
                            &mut active, &mut failed_generations, false,
                        ).await;
                        resume_plan(&records, &order, &dependents).await;
                    }
                } else {
                    let _ = reconcile(
                        &runtime, id, name, &dependencies, &apply, &status,
                        &mut active, &mut failed_generations, false,
                    ).await;
                }
            }
        }
    }
}

#[allow(clippy::too_many_arguments)]
async fn reconcile(
    runtime: &Arc<Runtime>,
    plugin_id: PluginId,
    name: &'static str,
    dependencies: &[crate::Dependency],
    apply: &ApplyFn,
    status: &watch::Sender<PluginStatus>,
    active: &mut Option<Activation>,
    failed_generations: &mut Option<Vec<(std::any::TypeId, Option<u64>)>>,
    force: bool,
) -> Result<()> {
    let mut snapshot = runtime.dependency_snapshot(dependencies);

    if !snapshot.missing.is_empty() {
        dispose_activation(runtime, plugin_id, status, active).await?;
        *failed_generations = None;
        status.send_replace(PluginStatus::Suspended {
            missing: snapshot.missing,
        });
        return Ok(());
    }

    let unchanged = active
        .as_ref()
        .is_some_and(|activation| activation.snapshot.generations == snapshot.generations);
    if unchanged && !force {
        return Ok(());
    }
    if !force
        && failed_generations
            .as_ref()
            .is_some_and(|failed| *failed == snapshot.generations)
    {
        return Ok(());
    }

    dispose_activation(runtime, plugin_id, status, active).await?;

    // A dependency may change while apply is running. Discard stale staging
    // activations and retry against the latest snapshot, with a small bound to
    // avoid pathological self-triggered loops.
    for _ in 0..8 {
        status.send_replace(PluginStatus::Starting {
            revision: snapshot.revision,
        });
        let activation_id = ActivationId(runtime.next_id());
        let scope = ScopeInner::new(activation_id.0, name);
        let context = Context::for_scope(runtime.clone(), activation_id.0);
        let plugin_context = PluginContext::new(context, scope.clone());

        let applied = std::panic::AssertUnwindSafe(apply(plugin_context))
            .catch_unwind()
            .await;
        let applied = match applied {
            Ok(result) => result,
            Err(payload) => Err(Error::panic(payload)),
        };
        if let Err(error) = applied {
            let _ = scope.dispose().await;
            *failed_generations = Some(snapshot.generations.clone());
            status.send_replace(PluginStatus::Failed {
                phase: FailurePhase::Apply,
                message: error.to_string().into(),
                revision: snapshot.revision,
            });
            return Err(error);
        }

        let after_apply = runtime.dependency_snapshot(dependencies);
        if after_apply.generations != snapshot.generations || !after_apply.missing.is_empty() {
            scope.dispose().await?;
            if !after_apply.missing.is_empty() {
                status.send_replace(PluginStatus::Suspended {
                    missing: after_apply.missing,
                });
                return Ok(());
            }
            snapshot = after_apply;
            continue;
        }

        if let Err(error) = scope.commit().await {
            let _ = scope.dispose().await;
            *failed_generations = Some(snapshot.generations.clone());
            status.send_replace(PluginStatus::Failed {
                phase: FailurePhase::Apply,
                message: error.to_string().into(),
                revision: snapshot.revision,
            });
            return Err(error);
        }
        runtime.commit_owner(activation_id.0);
        *failed_generations = None;
        status.send_replace(PluginStatus::Active {
            activation: activation_id,
            revision: snapshot.revision,
        });
        *active = Some(Activation { scope, snapshot });
        if let Err(error) = runtime
            .emit_serial(crate::Fork {
                plugin: plugin_id,
                activation: activation_id,
            })
            .await
        {
            let _ = dispose_activation(runtime, plugin_id, status, active).await;
            status.send_replace(PluginStatus::Failed {
                phase: FailurePhase::Apply,
                message: error.to_string().into(),
                revision: runtime.dependency_snapshot(dependencies).revision,
            });
            return Err(error);
        }
        return Ok(());
    }

    let error = Error::cleanup("dependency snapshot did not stabilize");
    status.send_replace(PluginStatus::Failed {
        phase: FailurePhase::Apply,
        message: error.to_string().into(),
        revision: snapshot.revision,
    });
    Err(error)
}

async fn dispose_activation(
    runtime: &Arc<Runtime>,
    plugin_id: PluginId,
    status: &watch::Sender<PluginStatus>,
    active: &mut Option<Activation>,
) -> Result<()> {
    let Some(activation) = active.take() else {
        return Ok(());
    };
    let activation_id = ActivationId(activation.scope.id);
    status.send_replace(PluginStatus::Stopping {
        activation: activation_id,
    });
    let event_error = runtime
        .emit_serial(crate::Dispose {
            plugin: plugin_id,
            activation: activation_id,
        })
        .await
        .err();
    let cleanup_error = activation.scope.dispose().await.err();
    if let Some(error) = event_error.or(cleanup_error) {
        status.send_replace(PluginStatus::Failed {
            phase: FailurePhase::Dispose,
            message: error.to_string().into(),
            revision: activation.snapshot.revision,
        });
        return Err(error);
    }
    Ok(())
}

fn topological_order(
    declarations: &[(
        &'static str,
        Vec<crate::Dependency>,
        Vec<crate::ServiceDeclaration>,
    )],
) -> Vec<usize> {
    let count = declarations.len();
    let mut outgoing = vec![Vec::new(); count];
    let mut indegree = vec![0usize; count];

    for (provider_index, (_, _, provided)) in declarations.iter().enumerate() {
        for (consumer_index, (_, dependencies, _)) in declarations.iter().enumerate() {
            let linked = provided.iter().any(|service| {
                dependencies
                    .iter()
                    .any(|dependency| dependency.key == service.key)
            });
            if linked {
                outgoing[provider_index].push(consumer_index);
                indegree[consumer_index] += 1;
            }
        }
    }

    let mut ready = std::collections::VecDeque::new();
    for (index, degree) in indegree.iter().enumerate() {
        if *degree == 0 {
            ready.push_back(index);
        }
    }
    let mut order = Vec::with_capacity(count);
    while let Some(index) = ready.pop_front() {
        order.push(index);
        for consumer in &outgoing[index] {
            indegree[*consumer] -= 1;
            if indegree[*consumer] == 0 {
                ready.push_back(*consumer);
            }
        }
    }
    order
}

fn dependent_indices(
    declarations: &[(
        &'static str,
        Vec<crate::Dependency>,
        Vec<crate::ServiceDeclaration>,
    )],
    provider: usize,
) -> std::collections::HashSet<usize> {
    let mut result = std::collections::HashSet::new();
    let mut pending = vec![provider];
    while let Some(current) = pending.pop() {
        let provided = &declarations[current].2;
        for (index, (_, dependencies, _)) in declarations.iter().enumerate() {
            if index == provider || result.contains(&index) {
                continue;
            }
            if provided.iter().any(|service| {
                dependencies
                    .iter()
                    .any(|dependency| dependency.key == service.key)
            }) {
                result.insert(index);
                pending.push(index);
            }
        }
    }
    result
}