zeph-orchestration 0.20.1

Task orchestration: DAG execution, failure propagation, and persistence for Zeph
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
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Cascade-aware routing for DAG execution (arXiv:2603.17112).
//!
//! Tracks failure propagation across DAG regions. When a subtree's failure rate
//! exceeds the configured threshold, tasks in that subtree are deprioritised in
//! [`DagScheduler::tick`] so that healthy independent branches run first.
//!
//! A "region" is defined by the "heaviest root" of each task — the root ancestor
//! that reaches the most downstream tasks. This prevents over-aggressive deprioritisation
//! on diamond-shaped DAGs.
//!
//! [`DagScheduler::tick`]: crate::scheduler::DagScheduler::tick

use std::collections::{HashSet, VecDeque};

use super::graph::{TaskGraph, TaskId};

/// Decision returned by [`CascadeDetector::evaluate_abort`].
///
/// Callers match on this to decide whether to abort the DAG immediately.
///
/// # Examples
///
/// ```rust
/// use zeph_orchestration::cascade::AbortDecision;
/// use zeph_orchestration::graph::TaskId;
///
/// let decision = AbortDecision::None;
/// assert!(matches!(decision, AbortDecision::None));
/// ```
#[derive(Debug, Clone)]
pub enum AbortDecision {
    /// No abort warranted; continue normal execution.
    None,
    /// A DAG region's failure rate exceeded the configured threshold.
    FanOutCascade {
        /// The root task ID of the failing region.
        region_root: TaskId,
        /// Failure rate at the time of abort (0.0–1.0).
        failure_rate: f32,
        /// Total tasks observed in the region (completed + failed).
        region_size: usize,
    },
}

/// Per-region failure health snapshot.
///
/// Accumulated by [`CascadeDetector::record_outcome`] and read by
/// [`CascadeDetector::is_cascading`] and [`CascadeDetector::deprioritized_tasks`].
#[derive(Debug, Clone)]
pub struct RegionHealth {
    /// Total number of task outcomes recorded for this region.
    pub total_tasks: usize,
    /// Number of failed task outcomes.
    pub failed_tasks: usize,
    /// `failed_tasks / total_tasks`. `0.0` when `total_tasks = 0`.
    pub failure_rate: f32,
}

impl RegionHealth {
    fn new() -> Self {
        Self {
            total_tasks: 0,
            failed_tasks: 0,
            failure_rate: 0.0,
        }
    }

    fn record(&mut self, failed: bool) {
        self.total_tasks += 1;
        if failed {
            self.failed_tasks += 1;
        }
        #[allow(clippy::cast_precision_loss)]
        {
            self.failure_rate = self.failed_tasks as f32 / self.total_tasks as f32;
        }
    }
}

/// Configuration for cascade detection.
///
/// Extracted from `OrchestrationConfig` at [`DagScheduler`] construction time.
///
/// [`DagScheduler`]: crate::scheduler::DagScheduler
#[derive(Debug, Clone)]
pub struct CascadeConfig {
    /// Failure rate (0.0–1.0) above which a region is considered "cascading".
    ///
    /// For example, `0.5` means more than half of the completed tasks in a
    /// region must have failed before it is deprioritised.
    pub failure_threshold: f32,
}

/// Tracks failure propagation across DAG regions for cascade-aware routing.
///
/// A "region" is the set of tasks reachable from the "heaviest root" of each task — the
/// root ancestor that is the source of the most downstream tasks. For tasks with multiple
/// roots (diamond patterns), we pick the single root that covers the most descendants,
/// preventing over-aggressive deprioritisation on diamond-shaped DAGs.
///
/// ## Caching
///
/// The forward adjacency map (task → direct dependents) is computed lazily on first use
/// and reused until `reset()` is called. Cache invariants:
///
/// - `forward_adjacency.is_none()` OR
///   `forward_adjacency.as_ref().unwrap().len() == graph.tasks.len()` at use time.
/// - Indexed by `TaskId::index()`; relies on the dense-id invariant in [`TaskGraph`].
/// - Any post-construction mutation of `graph.tasks` (push, truncate, reorder) OR of any
///   `TaskNode::depends_on` on a task already in `graph.tasks` MUST be followed by
///   [`CascadeDetector::reset`]. `inject_tasks` and its callers currently guarantee this;
///   no other mutation path exists in-tree.
#[derive(Debug)]
pub struct CascadeDetector {
    config: CascadeConfig,
    /// Per-root failure health. Key = root `TaskId`.
    region_health: std::collections::HashMap<TaskId, RegionHealth>,
    /// Cached forward adjacency: `forward_adjacency[i]` is the list of task IDs that
    /// depend on `TaskId(i)`. Populated lazily on first use; invalidated by `reset()`.
    ///
    /// `None` means not yet built or just invalidated. See struct-level "Caching" section.
    forward_adjacency: Option<Vec<Vec<TaskId>>>,
}

impl CascadeDetector {
    /// Create a new detector.
    #[must_use]
    pub fn new(config: CascadeConfig) -> Self {
        Self {
            config,
            region_health: std::collections::HashMap::new(),
            forward_adjacency: None,
        }
    }

    /// Record a task outcome and update the health of its primary region.
    pub fn record_outcome(&mut self, task_id: TaskId, succeeded: bool, graph: &TaskGraph) {
        let root = self.primary_root(task_id, graph);
        self.region_health
            .entry(root)
            .or_insert_with(RegionHealth::new)
            .record(!succeeded);
    }

    /// Returns `true` when the primary region of `task_id` is in cascade failure.
    #[must_use]
    pub fn is_cascading(&mut self, task_id: TaskId, graph: &TaskGraph) -> bool {
        let root = self.primary_root(task_id, graph);
        self.region_health
            .get(&root)
            .is_some_and(|h| h.failure_rate > self.config.failure_threshold)
    }

    /// Returns the set of task IDs that should be deprioritized due to cascade failure.
    ///
    /// Returns an empty set when no region is cascading, avoiding unnecessary reordering.
    #[must_use]
    pub fn deprioritized_tasks(&mut self, graph: &TaskGraph) -> HashSet<TaskId> {
        // Collect cascading roots first to avoid calling is_cascading per-task.
        let cascading_roots: HashSet<TaskId> = self
            .region_health
            .iter()
            .filter(|(_, h)| h.failure_rate > self.config.failure_threshold)
            .map(|(&root, _)| root)
            .collect();

        if cascading_roots.is_empty() {
            return HashSet::new();
        }

        // Log degenerate case: all known regions are cascading.
        let total_regions = self.region_health.len();
        if cascading_roots.len() == total_regions && total_regions > 0 {
            tracing::warn!(
                cascading_regions = total_regions,
                "all DAG regions are in cascade failure state; \
                 deprioritisation has no effect — falling back to default ordering"
            );
            return HashSet::new();
        }

        // Collect task ids first to avoid borrow conflict with &mut self in primary_root.
        let task_ids: Vec<TaskId> = graph.tasks.iter().map(|t| t.id).collect();
        task_ids
            .into_iter()
            .filter(|&id| cascading_roots.contains(&self.primary_root(id, graph)))
            .collect()
    }

    /// Evaluate whether a cascade abort should be triggered by fan-out failure rate.
    ///
    /// Returns [`AbortDecision::FanOutCascade`] when the primary region of `failed_task_id`
    /// has a failure rate ≥ `rate_threshold` AND the region has at least 3 tasks (floor
    /// prevents a single-failure 100%-rate region from triggering an abort prematurely).
    ///
    /// Returns [`AbortDecision::None`] when `rate_threshold ≤ 0.0` (disabled by default).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use zeph_orchestration::cascade::{AbortDecision, CascadeConfig, CascadeDetector};
    /// use zeph_orchestration::graph::{TaskGraph, TaskId, TaskNode};
    ///
    /// fn make_node(id: u32, deps: &[u32]) -> TaskNode {
    ///     let mut n = TaskNode::new(id, format!("t{id}"), "desc");
    ///     n.depends_on = deps.iter().map(|&d| TaskId(d)).collect();
    ///     n
    /// }
    ///
    /// let mut g = TaskGraph::new("test");
    /// g.tasks = vec![
    ///     make_node(0, &[]),
    ///     make_node(1, &[0]),
    ///     make_node(2, &[0]),
    ///     make_node(3, &[0]),
    /// ];
    ///
    /// let mut det = CascadeDetector::new(CascadeConfig { failure_threshold: 0.5 });
    /// det.record_outcome(TaskId(1), false, &g);
    /// det.record_outcome(TaskId(2), false, &g);
    /// det.record_outcome(TaskId(3), true, &g);
    ///
    /// // 2/3 failures = 0.67 >= threshold 0.7? No, so None here.
    /// match det.evaluate_abort(&g, TaskId(1), 0.9) {
    ///     AbortDecision::None => {}
    ///     other => panic!("unexpected: {:?}", other),
    /// }
    /// ```
    #[must_use]
    pub fn evaluate_abort(
        &mut self,
        graph: &TaskGraph,
        failed_task_id: TaskId,
        rate_threshold: f32,
    ) -> AbortDecision {
        if rate_threshold <= f32::EPSILON {
            return AbortDecision::None;
        }
        let root = self.primary_root(failed_task_id, graph);
        if let Some(health) = self.region_health.get(&root)
            && health.failure_rate >= rate_threshold
            && health.total_tasks >= 3
        {
            return AbortDecision::FanOutCascade {
                region_root: root,
                failure_rate: health.failure_rate,
                region_size: health.total_tasks,
            };
        }
        AbortDecision::None
    }

    /// Reset all region health counters and invalidate the cached forward adjacency.
    ///
    /// Called by `DagScheduler::inject_tasks()` because graph topology has fundamentally
    /// changed — old failure counts no longer reflect the new task set, and the cached
    /// forward-adjacency map no longer matches the new `graph.tasks`.
    pub fn reset(&mut self) {
        self.region_health.clear();
        self.forward_adjacency = None;
    }

    /// Expose region health for testing.
    #[cfg(test)]
    #[must_use]
    pub fn region_health(&self) -> &std::collections::HashMap<TaskId, RegionHealth> {
        &self.region_health
    }

    /// Returns `true` when the forward adjacency cache is populated.
    #[cfg(test)]
    #[must_use]
    pub fn forward_adjacency_is_cached(&self) -> bool {
        self.forward_adjacency.is_some()
    }

    /// Build the forward adjacency cache on first use; return the slice on subsequent calls.
    ///
    /// Asserts (in debug builds) that a cached vector has the same length as `graph.tasks`,
    /// catching stale-cache bugs where `graph.tasks` was mutated without calling `reset()`.
    fn ensure_adjacency(&mut self, graph: &TaskGraph) -> &[Vec<TaskId>] {
        if self.forward_adjacency.is_none() {
            let mut forward: Vec<Vec<TaskId>> = vec![Vec::new(); graph.tasks.len()];
            for task in &graph.tasks {
                for &dep in &task.depends_on {
                    forward[dep.index()].push(task.id);
                }
            }
            self.forward_adjacency = Some(forward);
        } else if let Some(ref fwd) = self.forward_adjacency {
            debug_assert_eq!(
                fwd.len(),
                graph.tasks.len(),
                "forward_adjacency stale: graph.tasks was mutated without CascadeDetector::reset()"
            );
        }
        self.forward_adjacency.as_deref().unwrap()
    }

    /// Compute the "heaviest" root for `task_id` using the cached forward adjacency.
    fn primary_root(&mut self, task_id: TaskId, graph: &TaskGraph) -> TaskId {
        let roots = ancestor_roots(task_id, graph);
        if roots.is_empty() {
            return task_id;
        }
        if roots.len() == 1 {
            return roots[0];
        }
        roots
            .into_iter()
            .max_by_key(|&r| (self.descendant_count(r, graph), u32::MAX - r.as_u32()))
            .unwrap_or(task_id)
    }

    /// Count tasks reachable from `root` (inclusive) using the cached forward adjacency.
    fn descendant_count(&mut self, root: TaskId, graph: &TaskGraph) -> usize {
        // Ensure the cache is populated, then take ownership of the slice length
        // by pre-collecting children into a local work-list to avoid a long-lived
        // borrow of `self` that would conflict with the mutable `ensure_adjacency`.
        self.ensure_adjacency(graph);
        let fwd = self.forward_adjacency.as_ref().unwrap();

        let mut visited = HashSet::new();
        let mut queue = VecDeque::new();
        queue.push_back(root);
        visited.insert(root);
        while let Some(id) = queue.pop_front() {
            if let Some(children) = fwd.get(id.index()) {
                for &child in children {
                    if visited.insert(child) {
                        queue.push_back(child);
                    }
                }
            }
        }
        visited.len()
    }
}

/// Collect all root (in-degree 0) ancestors of `task_id` via BFS.
fn ancestor_roots(task_id: TaskId, graph: &TaskGraph) -> Vec<TaskId> {
    let mut visited = HashSet::new();
    let mut queue = VecDeque::new();
    queue.push_back(task_id);
    visited.insert(task_id);

    let mut roots = Vec::new();

    while let Some(id) = queue.pop_front() {
        let task = &graph.tasks[id.index()];
        if task.depends_on.is_empty() {
            roots.push(id);
        } else {
            for &dep in &task.depends_on {
                if visited.insert(dep) {
                    queue.push_back(dep);
                }
            }
        }
    }

    roots
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graph::{TaskGraph, TaskId, TaskNode};

    fn make_node(id: u32, deps: &[u32]) -> TaskNode {
        let mut n = TaskNode::new(id, format!("t{id}"), "desc");
        n.depends_on = deps.iter().map(|&d| TaskId(d)).collect();
        n
    }

    fn graph_from(nodes: Vec<TaskNode>) -> TaskGraph {
        let mut g = TaskGraph::new("test");
        g.tasks = nodes;
        g
    }

    fn cfg(threshold: f32) -> CascadeConfig {
        CascadeConfig {
            failure_threshold: threshold,
        }
    }

    // --- ancestor_roots ---

    #[test]
    fn root_task_returns_self() {
        let g = graph_from(vec![make_node(0, &[])]);
        let roots = ancestor_roots(TaskId(0), &g);
        assert_eq!(roots, vec![TaskId(0)]);
    }

    #[test]
    fn linear_chain_root_is_task_zero() {
        // 0 -> 1 -> 2
        let g = graph_from(vec![
            make_node(0, &[]),
            make_node(1, &[0]),
            make_node(2, &[1]),
        ]);
        let roots = ancestor_roots(TaskId(2), &g);
        assert_eq!(roots, vec![TaskId(0)]);
    }

    #[test]
    fn diamond_has_two_roots() {
        // A(0) -> {B(1), C(2)} -> D(3)
        let g = graph_from(vec![
            make_node(0, &[]),
            make_node(1, &[0]),
            make_node(2, &[0]),
            make_node(3, &[1, 2]),
        ]);
        let mut roots = ancestor_roots(TaskId(3), &g);
        roots.sort_by_key(|r| r.as_u32());
        // Only one root (0) because 1 and 2 are not roots themselves.
        assert_eq!(roots, vec![TaskId(0)]);
    }

    #[test]
    fn fan_in_has_multiple_roots() {
        // A(0), B(1), C(2) -> D(3)
        let g = graph_from(vec![
            make_node(0, &[]),
            make_node(1, &[]),
            make_node(2, &[]),
            make_node(3, &[0, 1, 2]),
        ]);
        let mut roots = ancestor_roots(TaskId(3), &g);
        roots.sort_by_key(|r| r.as_u32());
        assert_eq!(roots, vec![TaskId(0), TaskId(1), TaskId(2)]);
    }

    // --- record_outcome + is_cascading ---

    #[test]
    fn no_failures_not_cascading() {
        let g = graph_from(vec![make_node(0, &[]), make_node(1, &[0])]);
        let mut det = CascadeDetector::new(cfg(0.5));
        det.record_outcome(TaskId(1), true, &g);
        assert!(!det.is_cascading(TaskId(1), &g));
    }

    #[test]
    fn failure_rate_exceeds_threshold() {
        // 0 -> 1, 0 -> 2, 0 -> 3 (fan-out). Two of three fail.
        let g = graph_from(vec![
            make_node(0, &[]),
            make_node(1, &[0]),
            make_node(2, &[0]),
            make_node(3, &[0]),
        ]);
        let mut det = CascadeDetector::new(cfg(0.5));
        det.record_outcome(TaskId(1), false, &g);
        det.record_outcome(TaskId(2), false, &g);
        det.record_outcome(TaskId(3), true, &g);
        // 2 failures / 3 total = 0.67 > 0.5 threshold
        assert!(det.is_cascading(TaskId(1), &g));
        assert!(det.is_cascading(TaskId(2), &g));
        assert!(det.is_cascading(TaskId(3), &g));
    }

    #[test]
    fn reset_clears_all_regions() {
        let g = graph_from(vec![make_node(0, &[]), make_node(1, &[0])]);
        let mut det = CascadeDetector::new(cfg(0.3));
        det.record_outcome(TaskId(1), false, &g);
        det.reset();
        assert!(!det.is_cascading(TaskId(1), &g));
        assert!(det.region_health().is_empty());
    }

    // --- deprioritized_tasks ---

    #[test]
    fn deprioritized_tasks_empty_when_healthy() {
        let g = graph_from(vec![make_node(0, &[]), make_node(1, &[0])]);
        let mut det = CascadeDetector::new(cfg(0.5));
        det.record_outcome(TaskId(1), true, &g);
        assert!(det.deprioritized_tasks(&g).is_empty());
    }

    #[test]
    fn deprioritized_tasks_returns_failing_subtree() {
        // Root 0 -> {1, 2}; Root 3 -> 4. Fail 1 and 2 (region of root 0).
        // Root 3 stays healthy.
        let g = graph_from(vec![
            make_node(0, &[]),
            make_node(1, &[0]),
            make_node(2, &[0]),
            make_node(3, &[]),
            make_node(4, &[3]),
        ]);
        let mut det = CascadeDetector::new(cfg(0.4));
        det.record_outcome(TaskId(1), false, &g);
        det.record_outcome(TaskId(2), false, &g);
        det.record_outcome(TaskId(4), true, &g);
        let dp = det.deprioritized_tasks(&g);
        // Tasks 0, 1, 2 belong to root 0 which is cascading.
        assert!(dp.contains(&TaskId(0)));
        assert!(dp.contains(&TaskId(1)));
        assert!(dp.contains(&TaskId(2)));
        // Tasks 3 and 4 are in healthy region.
        assert!(!dp.contains(&TaskId(3)));
        assert!(!dp.contains(&TaskId(4)));
    }

    #[test]
    fn all_regions_cascading_returns_empty_for_safe_fallback() {
        let g = graph_from(vec![make_node(0, &[]), make_node(1, &[0])]);
        let mut det = CascadeDetector::new(cfg(0.3));
        // Only one region (root 0), make it cascade.
        det.record_outcome(TaskId(1), false, &g);
        // With one region and it cascading, deprioritized_tasks returns empty
        // to prevent complete deadlock when all regions fail simultaneously.
        let dp = det.deprioritized_tasks(&g);
        assert!(
            dp.is_empty(),
            "all-regions-cascading should return empty to allow forward progress"
        );
    }

    // --- forward adjacency cache ---

    #[test]
    fn cache_populated_after_first_call_multi_root() {
        // Fan-in: A(0), B(1) -> C(2). Task C has two roots, so descendant_count is called
        // and the adjacency cache is built.
        let g = graph_from(vec![
            make_node(0, &[]),
            make_node(1, &[]),
            make_node(2, &[0, 1]),
        ]);
        let mut det = CascadeDetector::new(cfg(0.5));
        assert!(!det.forward_adjacency_is_cached());
        det.record_outcome(TaskId(2), true, &g);
        assert!(det.forward_adjacency_is_cached());
    }

    #[test]
    fn cache_still_populated_on_second_call() {
        // Fan-in: same multi-root graph to ensure cache is built on first call.
        let g = graph_from(vec![
            make_node(0, &[]),
            make_node(1, &[]),
            make_node(2, &[0, 1]),
        ]);
        let mut det = CascadeDetector::new(cfg(0.5));
        det.record_outcome(TaskId(2), true, &g);
        assert!(det.forward_adjacency_is_cached());
        // Second call must not rebuild (cache stays populated).
        det.record_outcome(TaskId(2), false, &g);
        assert!(det.forward_adjacency_is_cached());
    }

    #[test]
    fn reset_clears_forward_adjacency() {
        // Use multi-root graph so the cache is populated on record_outcome.
        let g = graph_from(vec![
            make_node(0, &[]),
            make_node(1, &[]),
            make_node(2, &[0, 1]),
        ]);
        let mut det = CascadeDetector::new(cfg(0.3));
        det.record_outcome(TaskId(2), false, &g);
        assert!(det.forward_adjacency_is_cached());
        det.reset();
        assert!(!det.forward_adjacency_is_cached());
    }

    #[test]
    fn primary_root_consistent_with_and_without_cache() {
        // Fan-in: two independent roots A(0) and B(1) both feed into C(2).
        // C has two root ancestors, so descendant_count is called and cache is built.
        let g = graph_from(vec![
            make_node(0, &[]),
            make_node(1, &[]),
            make_node(2, &[0, 1]),
        ]);
        let mut det = CascadeDetector::new(cfg(0.5));
        // First call builds cache.
        let root_cold = det.primary_root(TaskId(2), &g);
        assert!(det.forward_adjacency_is_cached());
        // Second call uses cache; result must be identical.
        let root_warm = det.primary_root(TaskId(2), &g);
        assert_eq!(root_cold, root_warm);
    }
}