eredu-runtime 0.1.0

Backend-neutral model execution runtime for Eredu
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
//! Validated execution-group dependency graphs and ready-set scheduling.

use std::collections::{BTreeMap, BTreeSet};
use std::ops::Range;

/// Stable non-empty identity for one architecture execution group.
#[derive(Debug, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ExecutionGroupId(String);

impl ExecutionGroupId {
    /// Creates a validated execution-group identifier.
    pub fn new(id: impl Into<String>) -> Result<Self, ExecutionGraphError> {
        let id = id.into();
        if id.trim().is_empty() {
            return Err(ExecutionGraphError::EmptyGroupId);
        }
        Ok(Self(id))
    }

    /// Returns the stable identifier.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl std::fmt::Display for ExecutionGroupId {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(&self.0)
    }
}

/// One named execution group and the groups whose outputs it consumes.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ExecutionGroupSpec {
    id: String,
    dependencies: Vec<String>,
}

impl ExecutionGroupSpec {
    /// Declares a root execution group.
    pub fn root(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            dependencies: Vec::new(),
        }
    }

    /// Declares a group with named input dependencies.
    pub fn with_dependencies(
        id: impl Into<String>,
        dependencies: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        Self {
            id: id.into(),
            dependencies: dependencies.into_iter().map(Into::into).collect(),
        }
    }

    /// Returns the stable group identifier.
    pub fn id(&self) -> &str {
        &self.id
    }

    /// Returns dependency identifiers in declaration order.
    pub fn dependencies(&self) -> &[String] {
        &self.dependencies
    }
}

/// Validated execution-group dependency graph with one authoritative output.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ExecutionGraph {
    groups: Vec<ExecutionGroupSpec>,
    dependencies: Vec<Vec<usize>>,
    dependents: Vec<Vec<usize>>,
    execution_order: Vec<usize>,
    output: usize,
}

/// Stable architecture-group and group-local address of one flattened execution unit.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct ExecutionUnitAddress {
    group: usize,
    index: usize,
}

impl ExecutionUnitAddress {
    /// Returns the architecture execution-group slot.
    pub const fn group(self) -> usize {
        self.group
    }

    /// Returns the unit's group-local index.
    pub const fn index(self) -> usize {
        self.index
    }

    /// Returns the same execution-group address with a semantic state index.
    pub const fn with_index(self, index: usize) -> Self {
        Self {
            group: self.group,
            index,
        }
    }
}

/// Validated mapping between architecture groups and the flat residency-unit order.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ExecutionUnitLayout {
    group_ids: Vec<ExecutionGroupId>,
    group_ranges: Vec<Range<usize>>,
    addresses: Vec<ExecutionUnitAddress>,
}

impl ExecutionUnitLayout {
    /// Builds a stable group-major unit order for one validated execution graph.
    pub fn new(
        graph: &ExecutionGraph,
        group_unit_counts: impl IntoIterator<Item = usize>,
    ) -> Result<Self, ExecutionUnitLayoutError> {
        let counts = group_unit_counts.into_iter().collect::<Vec<_>>();
        if counts.len() != graph.groups().len() {
            return Err(ExecutionUnitLayoutError::GroupCountMismatch {
                graph_groups: graph.groups().len(),
                declared_groups: counts.len(),
            });
        }
        let group_ids = graph
            .groups()
            .iter()
            .map(|group| {
                ExecutionGroupId::new(group.id().to_owned())
                    .expect("validated execution graph has non-empty group identifiers")
            })
            .collect();
        let mut group_ranges = Vec::with_capacity(counts.len());
        let mut addresses = Vec::new();
        for (group, count) in counts.into_iter().enumerate() {
            let start = addresses.len();
            let end = start
                .checked_add(count)
                .ok_or(ExecutionUnitLayoutError::UnitCountOverflow)?;
            addresses.reserve(count);
            addresses.extend((0..count).map(|index| ExecutionUnitAddress { group, index }));
            group_ranges.push(start..end);
        }
        Ok(Self {
            group_ids,
            group_ranges,
            addresses,
        })
    }

    /// Returns the total number of execution units in group-major order.
    pub fn len(&self) -> usize {
        self.addresses.len()
    }

    /// Returns whether the architecture declares no executable units.
    pub fn is_empty(&self) -> bool {
        self.addresses.is_empty()
    }

    /// Returns the number of architecture execution groups.
    pub fn group_count(&self) -> usize {
        self.group_ranges.len()
    }

    /// Returns one architecture execution group's stable identifier.
    pub fn group_id(&self, group: usize) -> Option<&ExecutionGroupId> {
        self.group_ids.get(group)
    }

    /// Returns the group-major flat range for one architecture group.
    pub fn group_range(&self, group: usize) -> Option<Range<usize>> {
        self.group_ranges.get(group).cloned()
    }

    /// Resolves one flat residency-unit slot to its architecture address.
    pub fn address(&self, ordinal: usize) -> Option<ExecutionUnitAddress> {
        self.addresses.get(ordinal).copied()
    }

    /// Resolves one architecture address to its flat residency-unit slot.
    pub fn ordinal(&self, group: usize, index: usize) -> Option<usize> {
        let range = self.group_ranges.get(group)?;
        (index < range.len()).then_some(range.start + index)
    }
}

/// Invalid architecture execution-unit grouping.
#[derive(Debug, Clone, Eq, PartialEq, thiserror::Error)]
pub enum ExecutionUnitLayoutError {
    /// The architecture did not provide exactly one unit count per graph group.
    #[error(
        "execution graph contains {graph_groups} groups but the architecture declared {declared_groups} group counts"
    )]
    GroupCountMismatch {
        /// Validated graph group count.
        graph_groups: usize,
        /// Architecture-declared group count.
        declared_groups: usize,
    },
    /// The total number of units exceeded the addressable range.
    #[error("execution-unit count overflowed usize")]
    UnitCountOverflow,
}

impl ExecutionGraph {
    /// Validates names, dependency references, acyclicity, and output reachability.
    pub fn new(
        groups: Vec<ExecutionGroupSpec>,
        output: impl AsRef<str>,
    ) -> Result<Self, ExecutionGraphError> {
        if groups.is_empty() {
            return Err(ExecutionGraphError::EmptyGraph);
        }
        let mut by_id = BTreeMap::new();
        for (index, group) in groups.iter().enumerate() {
            if group.id.trim().is_empty() {
                return Err(ExecutionGraphError::EmptyGroupId);
            }
            if by_id.insert(group.id.clone(), index).is_some() {
                return Err(ExecutionGraphError::DuplicateGroup(group.id.clone()));
            }
        }
        let output_name = output.as_ref();
        let output = by_id
            .get(output_name)
            .copied()
            .ok_or_else(|| ExecutionGraphError::UnknownOutput(output_name.to_owned()))?;
        let mut dependencies = Vec::with_capacity(groups.len());
        let mut dependents = vec![Vec::new(); groups.len()];
        let mut indegree = vec![0usize; groups.len()];
        for (index, group) in groups.iter().enumerate() {
            let mut seen = BTreeSet::new();
            let mut resolved = Vec::with_capacity(group.dependencies.len());
            for dependency in &group.dependencies {
                let dependency_index = by_id.get(dependency).copied().ok_or_else(|| {
                    ExecutionGraphError::UnknownDependency {
                        group: group.id.clone(),
                        dependency: dependency.clone(),
                    }
                })?;
                if dependency_index == index {
                    return Err(ExecutionGraphError::SelfDependency(group.id.clone()));
                }
                if !seen.insert(dependency_index) {
                    return Err(ExecutionGraphError::DuplicateDependency {
                        group: group.id.clone(),
                        dependency: dependency.clone(),
                    });
                }
                resolved.push(dependency_index);
                dependents[dependency_index].push(index);
            }
            indegree[index] = resolved.len();
            dependencies.push(resolved);
        }
        let mut ready = indegree
            .iter()
            .enumerate()
            .filter_map(|(index, &degree)| (degree == 0).then_some(index))
            .collect::<BTreeSet<_>>();
        let mut execution_order = Vec::with_capacity(groups.len());
        while let Some(index) = ready.pop_first() {
            execution_order.push(index);
            for &dependent in &dependents[index] {
                indegree[dependent] -= 1;
                if indegree[dependent] == 0 {
                    ready.insert(dependent);
                }
            }
        }
        if execution_order.len() != groups.len() {
            return Err(ExecutionGraphError::Cycle);
        }
        let mut contributes = BTreeSet::new();
        let mut pending = vec![output];
        while let Some(index) = pending.pop() {
            if contributes.insert(index) {
                pending.extend(dependencies[index].iter().copied());
            }
        }
        if contributes.len() != groups.len() {
            let disconnected = groups
                .iter()
                .enumerate()
                .filter_map(|(index, group)| {
                    (!contributes.contains(&index)).then_some(group.id.clone())
                })
                .collect();
            return Err(ExecutionGraphError::Disconnected { disconnected });
        }
        Ok(Self {
            groups,
            dependencies,
            dependents,
            execution_order,
            output,
        })
    }

    /// Creates a dependency chain whose final group is the output.
    pub fn chain(
        ids: impl IntoIterator<Item = impl Into<String>>,
    ) -> Result<Self, ExecutionGraphError> {
        let ids = ids.into_iter().map(Into::into).collect::<Vec<String>>();
        let output = ids.last().cloned().ok_or(ExecutionGraphError::EmptyGraph)?;
        let groups = ids
            .iter()
            .enumerate()
            .map(|(index, id)| match index.checked_sub(1) {
                Some(previous) => Self::group_with_dependency(id.clone(), ids[previous].clone()),
                None => ExecutionGroupSpec::root(id.clone()),
            })
            .collect();
        Self::new(groups, output)
    }

    fn group_with_dependency(id: String, dependency: String) -> ExecutionGroupSpec {
        ExecutionGroupSpec::with_dependencies(id, [dependency])
    }

    /// Returns group specifications in stable architecture slot order.
    pub fn groups(&self) -> &[ExecutionGroupSpec] {
        &self.groups
    }

    /// Resolves a stable execution-group identity to its architecture slot.
    pub fn group_index(&self, id: &str) -> Option<usize> {
        self.groups.iter().position(|group| group.id() == id)
    }

    /// Returns stable topological execution slots.
    pub fn execution_order(&self) -> &[usize] {
        &self.execution_order
    }

    /// Returns dependency slots for an architecture group slot.
    pub fn dependencies(&self, group: usize) -> Option<&[usize]> {
        self.dependencies.get(group).map(Vec::as_slice)
    }

    /// Returns dependent slots in stable declaration order.
    pub fn dependents(&self, group: usize) -> Option<&[usize]> {
        self.dependents.get(group).map(Vec::as_slice)
    }

    /// Returns the authoritative output group slot.
    pub const fn output(&self) -> usize {
        self.output
    }

    /// Returns one consumer count per group slot.
    pub fn consumer_counts(&self) -> Vec<usize> {
        let mut counts = vec![0; self.groups.len()];
        for dependencies in &self.dependencies {
            for &dependency in dependencies {
                counts[dependency] += 1;
            }
        }
        counts
    }
}

/// State of one execution group in a ready-set scheduler.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum ReadyGroupState {
    /// Dependencies have not all been ordered yet.
    Pending,
    /// Work was submitted and its consumers may insert completion waits.
    Ordered,
    /// Submission failed.
    Failed,
    /// An upstream failure made this group unreachable.
    Blocked,
}

#[derive(Debug)]
struct ExecutionGroupReadySet<'a> {
    graph: &'a ExecutionGraph,
    remaining_dependencies: Vec<usize>,
    states: Vec<ReadyGroupState>,
    ready: BTreeSet<usize>,
}

/// Backend-neutral execution-group orchestration and dependency-output lifetime tracking.
#[derive(Debug)]
pub struct ExecutionGroupSchedule<'a> {
    graph: &'a ExecutionGraph,
    ready: ExecutionGroupReadySet<'a>,
    started: Vec<bool>,
    remaining_consumers: Vec<usize>,
}

impl<'a> ExecutionGroupSchedule<'a> {
    /// Creates a schedule for one validated execution graph.
    pub fn new(graph: &'a ExecutionGraph) -> Self {
        Self {
            graph,
            ready: ExecutionGroupReadySet::new(graph),
            started: vec![false; graph.groups.len()],
            remaining_consumers: graph.consumer_counts(),
        }
    }

    /// Returns ready groups which have not begun architecture setup.
    pub fn startable_groups(&self) -> impl Iterator<Item = usize> + '_ {
        self.ready
            .ready_groups()
            .filter(|&group| !self.started[group])
    }

    /// Returns dependency slots in architecture declaration order.
    pub fn dependencies(&self, group: usize) -> Result<&[usize], ExecutionScheduleError> {
        self.graph
            .dependencies(group)
            .ok_or(ExecutionScheduleError::UnknownGroup {
                group,
                count: self.started.len(),
            })
    }

    /// Commits successful architecture setup and returns producer outputs whose final
    /// consumer has now captured them.
    pub fn started(&mut self, group: usize) -> Result<Vec<usize>, ExecutionScheduleError> {
        let count = self.started.len();
        let started = self
            .started
            .get_mut(group)
            .ok_or(ExecutionScheduleError::UnknownGroup { group, count })?;
        if *started {
            return Err(ExecutionScheduleError::AlreadyStarted { group });
        }
        if !self.ready.ready.contains(&group) {
            return Err(ExecutionScheduleError::DependenciesPending { group });
        }
        *started = true;
        let mut releasable = Vec::new();
        for &dependency in &self.graph.dependencies[group] {
            self.remaining_consumers[dependency] -= 1;
            if self.remaining_consumers[dependency] == 0 {
                releasable.push(dependency);
            }
        }
        Ok(releasable)
    }

    /// Commits a successfully ordered group and unlocks its dependents.
    pub fn ordered(&mut self, group: usize) -> Result<(), ExecutionScheduleError> {
        match self.started.get(group).copied() {
            None => Err(ExecutionScheduleError::UnknownGroup {
                group,
                count: self.started.len(),
            }),
            Some(false) => Err(ExecutionScheduleError::NotStarted { group }),
            Some(true) if self.ready.state(group) == Some(ReadyGroupState::Pending) => {
                self.ready.ordered(group);
                Ok(())
            }
            Some(true) => Err(ExecutionScheduleError::AlreadyOrdered { group }),
        }
    }

    /// Closes a failed group and its dependent subgraph.
    pub fn fail(&mut self, group: usize) -> Result<(), ExecutionScheduleError> {
        if group >= self.started.len() {
            return Err(ExecutionScheduleError::UnknownGroup {
                group,
                count: self.started.len(),
            });
        }
        self.ready.fail(group);
        Ok(())
    }

    /// Returns one group's ordering state.
    pub fn state(&self, group: usize) -> Option<ReadyGroupState> {
        self.ready.state(group)
    }
}

/// Invalid transition in backend-neutral execution-group orchestration.
#[derive(Debug, Clone, Eq, PartialEq, thiserror::Error)]
pub enum ExecutionScheduleError {
    /// The group slot is outside the validated graph.
    #[error("execution group {group} is outside the {count}-group schedule")]
    UnknownGroup {
        /// Requested group slot.
        group: usize,
        /// Number of groups in the schedule.
        count: usize,
    },
    /// Architecture setup was committed more than once.
    #[error("execution group {group} was already started")]
    AlreadyStarted {
        /// Conflicting group slot.
        group: usize,
    },
    /// Architecture setup was attempted before every dependency was ordered.
    #[error("execution group {group} still has unordered dependencies")]
    DependenciesPending {
        /// Premature group slot.
        group: usize,
    },
    /// Ordering was committed without successful architecture setup.
    #[error("execution group {group} was ordered before it started")]
    NotStarted {
        /// Invalid group slot.
        group: usize,
    },
    /// Ordering was committed more than once or after closure.
    #[error("execution group {group} was already ordered or closed")]
    AlreadyOrdered {
        /// Conflicting group slot.
        group: usize,
    },
}

impl<'a> ExecutionGroupReadySet<'a> {
    fn new(graph: &'a ExecutionGraph) -> Self {
        let remaining_dependencies = graph.dependencies.iter().map(Vec::len).collect::<Vec<_>>();
        let ready = remaining_dependencies
            .iter()
            .enumerate()
            .filter_map(|(group, &remaining)| (remaining == 0).then_some(group))
            .collect();
        Self {
            graph,
            remaining_dependencies,
            states: vec![ReadyGroupState::Pending; graph.groups.len()],
            ready,
        }
    }

    fn ready_groups(&self) -> impl Iterator<Item = usize> + '_ {
        self.ready.iter().copied()
    }

    fn ordered(&mut self, group: usize) {
        debug_assert_eq!(self.states[group], ReadyGroupState::Pending);
        self.ready.remove(&group);
        self.states[group] = ReadyGroupState::Ordered;
        for &dependent in &self.graph.dependents[group] {
            if self.states[dependent] != ReadyGroupState::Pending {
                continue;
            }
            self.remaining_dependencies[dependent] -= 1;
            if self.remaining_dependencies[dependent] == 0 {
                self.ready.insert(dependent);
            }
        }
    }

    fn fail(&mut self, group: usize) {
        self.close_subgraph(group, ReadyGroupState::Failed);
    }

    fn close_subgraph(&mut self, group: usize, state: ReadyGroupState) {
        let mut pending = vec![(group, state)];
        while let Some((group, state)) = pending.pop() {
            if self.states[group] != ReadyGroupState::Pending {
                continue;
            }
            self.ready.remove(&group);
            self.states[group] = state;
            pending.extend(
                self.graph.dependents[group]
                    .iter()
                    .copied()
                    .map(|dependent| (dependent, ReadyGroupState::Blocked)),
            );
        }
    }

    fn state(&self, group: usize) -> Option<ReadyGroupState> {
        self.states.get(group).copied()
    }
}

/// Invalid execution graph declaration.
#[derive(Debug, Clone, Eq, PartialEq, thiserror::Error)]
pub enum ExecutionGraphError {
    /// No groups were declared.
    #[error("execution-group graph must contain at least one group")]
    EmptyGraph,
    /// A group identity is empty.
    #[error("execution-group identifiers must not be empty")]
    EmptyGroupId,
    /// Two groups share an identity.
    #[error("duplicate execution-group identifier {0:?}")]
    DuplicateGroup(String),
    /// The declared output is unknown.
    #[error("execution-group graph output {0:?} does not exist")]
    UnknownOutput(String),
    /// A dependency is unknown.
    #[error("execution group {group:?} depends on unknown group {dependency:?}")]
    UnknownDependency {
        /// Dependent group.
        group: String,
        /// Missing dependency.
        dependency: String,
    },
    /// A group depends on itself.
    #[error("execution group {0:?} cannot depend on itself")]
    SelfDependency(String),
    /// A dependency is repeated.
    #[error("execution group {group:?} repeats dependency {dependency:?}")]
    DuplicateDependency {
        /// Dependent group.
        group: String,
        /// Repeated dependency.
        dependency: String,
    },
    /// The graph contains a dependency cycle.
    #[error("execution-group graph contains a dependency cycle")]
    Cycle,
    /// Some groups do not contribute to the output.
    #[error("execution groups do not contribute to the graph output: {disconnected:?}")]
    Disconnected {
        /// Disconnected group identities.
        disconnected: Vec<String>,
    },
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn graph_order_is_stable_and_dependency_driven() {
        let graph = ExecutionGraph::new(
            vec![
                ExecutionGroupSpec::root("image"),
                ExecutionGroupSpec::root("audio"),
                ExecutionGroupSpec::with_dependencies("text", ["image", "audio"]),
            ],
            "text",
        )
        .unwrap();
        assert_eq!(graph.execution_order(), &[0, 1, 2]);
        assert_eq!(graph.dependencies(2), Some([0, 1].as_slice()));

        let mut ready = ExecutionGroupReadySet::new(&graph);
        assert_eq!(ready.ready_groups().collect::<Vec<_>>(), vec![0, 1]);
        ready.ordered(1);
        assert_eq!(ready.ready_groups().collect::<Vec<_>>(), vec![0]);
        ready.ordered(0);
        assert_eq!(ready.ready_groups().collect::<Vec<_>>(), vec![2]);
    }

    #[test]
    fn schedule_releases_dependency_outputs_after_their_final_consumer_starts() {
        let graph = ExecutionGraph::new(
            vec![
                ExecutionGroupSpec::root("root"),
                ExecutionGroupSpec::with_dependencies("left", ["root"]),
                ExecutionGroupSpec::with_dependencies("right", ["root"]),
                ExecutionGroupSpec::with_dependencies("output", ["left", "right"]),
            ],
            "output",
        )
        .unwrap();
        let mut schedule = ExecutionGroupSchedule::new(&graph);
        assert_eq!(schedule.startable_groups().collect::<Vec<_>>(), vec![0]);
        assert!(schedule.started(1).is_err());
        assert!(schedule.started(0).unwrap().is_empty());
        schedule.ordered(0).unwrap();
        assert_eq!(schedule.startable_groups().collect::<Vec<_>>(), vec![1, 2]);
        assert!(schedule.started(1).unwrap().is_empty());
        assert_eq!(schedule.started(2).unwrap(), vec![0]);
        schedule.ordered(1).unwrap();
        schedule.ordered(2).unwrap();
        assert_eq!(schedule.started(3).unwrap(), vec![1, 2]);
        assert!(schedule.ordered(3).is_ok());
        assert_eq!(schedule.state(3), Some(ReadyGroupState::Ordered));
    }

    #[test]
    fn execution_unit_layout_preserves_group_major_residency_order() {
        let graph = ExecutionGraph::new(
            vec![
                ExecutionGroupSpec::root("vision"),
                ExecutionGroupSpec::with_dependencies("text", ["vision"]),
            ],
            "text",
        )
        .unwrap();
        let layout = ExecutionUnitLayout::new(&graph, [2, 3]).unwrap();

        assert_eq!(graph.group_index("vision"), Some(0));
        assert_eq!(graph.group_index("text"), Some(1));
        assert_eq!(graph.group_index("missing"), None);
        assert_eq!(layout.len(), 5);
        assert_eq!(layout.group_count(), 2);
        assert_eq!(layout.group_id(0).unwrap().as_str(), "vision");
        assert_eq!(layout.group_id(1).unwrap().as_str(), "text");
        assert_eq!(layout.group_range(0), Some(0..2));
        assert_eq!(layout.group_range(1), Some(2..5));
        assert_eq!(layout.address(3).unwrap().group(), 1);
        assert_eq!(layout.address(3).unwrap().index(), 1);
        assert_eq!(layout.ordinal(1, 2), Some(4));
        assert_eq!(layout.ordinal(0, 2), None);
    }

    #[test]
    fn execution_unit_layout_rejects_graph_count_drift() {
        let graph = ExecutionGraph::chain(["vision", "text"]).unwrap();
        assert_eq!(
            ExecutionUnitLayout::new(&graph, [2]).unwrap_err(),
            ExecutionUnitLayoutError::GroupCountMismatch {
                graph_groups: 2,
                declared_groups: 1,
            }
        );
    }

    #[test]
    fn invalid_graphs_fail_closed() {
        let groups = vec![
            ExecutionGroupSpec::with_dependencies("left", ["right"]),
            ExecutionGroupSpec::with_dependencies("right", ["left"]),
        ];
        assert_eq!(
            ExecutionGraph::new(groups, "right"),
            Err(ExecutionGraphError::Cycle)
        );
    }
}