limen-core 0.1.0-alpha.1

Limen core contracts and primitives.
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
//! (Work)bench [test] Runtime implementation.

use crate::edge::EdgeOccupancy;
use crate::errors::{NodeErrorKind, RuntimeError, RuntimeInvariantError};
use crate::event_message;
use crate::graph::GraphApi;
use crate::node::StepResult;
use crate::policy::{BatchingPolicy, BudgetPolicy, DeadlinePolicy, NodePolicy, WatermarkState};
use crate::prelude::{PlatformClock, Telemetry};

use super::LimenRuntime;

/// A tiny, no_std test runtime:
/// - round-robin over nodes
/// - uses a single occupancy array
/// - no heap, no threads, no timers
pub struct TestNoStdRuntime<C, T, const NODE_COUNT: usize, const EDGE_COUNT: usize>
where
    C: PlatformClock + Sized,
    T: Telemetry + Sized,
{
    stop: bool,
    next: usize,
    occ: [EdgeOccupancy; EDGE_COUNT],
    node_policies: [NodePolicy; NODE_COUNT],
    clock: Option<C>,
    telemetry: Option<T>,
}

impl<C, T, const NODE_COUNT: usize, const EDGE_COUNT: usize>
    TestNoStdRuntime<C, T, NODE_COUNT, EDGE_COUNT>
where
    C: PlatformClock + Sized,
    T: Telemetry + Sized,
{
    /// Construct with a pessimistic initial occupancy; `init()` will overwrite it.
    pub const fn new() -> Self {
        const INIT_OCC: EdgeOccupancy = EdgeOccupancy::new(0, 0, WatermarkState::AtOrAboveHard);
        const INIT_POLICY: NodePolicy = NodePolicy::new(
            BatchingPolicy::none(),
            BudgetPolicy::new(None, None),
            DeadlinePolicy::new(false, None, None),
        );

        Self {
            stop: false,
            next: 0,
            occ: [INIT_OCC; EDGE_COUNT],
            node_policies: [INIT_POLICY; NODE_COUNT],
            clock: None,
            telemetry: None,
        }
    }

    /// Decide whether a node's `StepResult` constitutes "progress".
    /// Currently conservative: treat any `Ok(_)` as progress to keep the runtime simple.
    /// If/when `StepResult` exposes a richer API (e.g., `is_progress()`), update this.
    #[inline]
    fn made_progress(sr: &StepResult) -> bool {
        match sr {
            StepResult::MadeProgress => true,
            StepResult::Terminal => true,
            // TODO: Handle this.
            StepResult::YieldUntil(_) => true,
            StepResult::NoInput | StepResult::Backpressured | StepResult::WaitingOnExternal => {
                false
            }
        }
    }

    /// Internal helper for a monotonic nanosecond timestamp.
    #[inline]
    fn now_nanos(clock: &C) -> u64 {
        let ticks = clock.now_ticks();
        clock.ticks_to_nanos(ticks)
    }

    /// Hot-path inner step: requires `&mut self`, `&C`, and `&mut T`.
    #[inline]
    fn step_inner<Graph>(
        &mut self,
        graph: &mut Graph,
        clock: &C,
        telemetry: &mut T,
    ) -> Result<bool, RuntimeError>
    where
        Graph: GraphApi<NODE_COUNT, EDGE_COUNT>,
    {
        // Try each node once, starting from `self.next` (round-robin).
        let start = self.next;
        let mut tried = 0usize;

        while tried < NODE_COUNT {
            let node_index = (start + tried) % NODE_COUNT;

            // Execute the node step. All node-level telemetry (latency, processed,
            // deadline, NodeStep events) is now handled in NodeLink::step via
            // the graph implementation, not here.
            let result = graph.step_node_by_index(node_index, clock, telemetry);

            // ---- Scheduler logic (unchanged) ----
            match result {
                Ok(step_result) => {
                    if Self::made_progress(&step_result) {
                        // Keep this as the canonical place where the runtime refreshes the
                        // occupancy buffer for scheduling. This remains separate from
                        // telemetry, which is handled in StepContext/NodeLink.
                        graph
                            .write_all_edge_occupancies(&mut self.occ)
                            .map_err(RuntimeError::from)?;

                        self.next = (node_index + 1) % NODE_COUNT;
                        return Ok(true);
                    } else {
                        tried += 1;
                        continue;
                    }
                }
                Err(error) => match error.kind() {
                    NodeErrorKind::NoInput | NodeErrorKind::Backpressured => {
                        tried += 1;
                        continue;
                    }
                    _ => return Err(RuntimeError::from(error)),
                },
            }
        }

        // We tried all nodes and none made progress.
        Ok(false)
    }

    /// Safely access telemetry by mutable reference, if it is present.
    ///
    /// Returns:
    /// - `Ok(Some(r))` if telemetry is present and `f` ran.
    /// - `Ok(None)` if telemetry is currently absent (e.g. not yet initialized).
    ///
    /// This never panics and never moves telemetry out of the runtime.
    #[inline]
    pub fn with_telemetry<F, R>(&mut self, f: F) -> Result<Option<R>, RuntimeError>
    where
        F: FnOnce(&mut T) -> R,
    {
        if let Some(t) = self.telemetry.as_mut() {
            let r = f(t);
            Ok(Some(r))
        } else {
            Ok(None)
        }
    }
}

impl<Graph, C, T, const NODE_COUNT: usize, const EDGE_COUNT: usize>
    LimenRuntime<Graph, NODE_COUNT, EDGE_COUNT> for TestNoStdRuntime<C, T, NODE_COUNT, EDGE_COUNT>
where
    Graph: GraphApi<NODE_COUNT, EDGE_COUNT>,
    C: PlatformClock + Sized,
    T: Telemetry + Sized,
{
    type Clock = C;
    type Telemetry = T;
    type Error = RuntimeError;

    #[cfg(feature = "std")]
    type StopHandle = crate::runtime::RuntimeStopHandle;

    #[inline]
    fn init(
        &mut self,
        graph: &mut Graph,
        clock: Self::Clock,
        mut telemetry: Self::Telemetry,
    ) -> Result<(), Self::Error> {
        const GRAPH_ID: crate::telemetry::GraphInstanceId = 0;

        // Validate (pure, read-only).
        graph.validate_graph().map_err(RuntimeError::from)?;

        // Snapshot occupancies into our persistent buffer.
        graph
            .write_all_edge_occupancies(&mut self.occ)
            .map_err(RuntimeError::from)?;

        // Cache NodePolicy for every node using the GraphApi hook.
        self.node_policies = graph.get_node_policies();

        if T::EVENTS_STATICALLY_ENABLED && telemetry.events_enabled() {
            let timestamp_ns = Self::now_nanos(&clock);
            let event = crate::telemetry::TelemetryEvent::runtime(
                crate::telemetry::RuntimeTelemetryEvent::new(
                    GRAPH_ID,
                    timestamp_ns,
                    crate::telemetry::RuntimeTelemetryEventKind::GraphStarted,
                    None,
                ),
            );
            telemetry.push_event(event);
        }

        self.clock = Some(clock);
        self.telemetry = Some(telemetry);

        self.stop = false;
        self.next = 0;

        Ok(())
    }

    #[inline]
    fn reset(&mut self, graph: &Graph) -> Result<(), Self::Error> {
        const GRAPH_ID: crate::telemetry::GraphInstanceId = 0;

        self.stop = false;
        self.next = 0;
        graph
            .write_all_edge_occupancies(&mut self.occ)
            .map_err(RuntimeError::from)?;

        if let (Some(ref clock), Some(telemetry)) = (&self.clock, self.telemetry.as_mut()) {
            if T::EVENTS_STATICALLY_ENABLED && telemetry.events_enabled() {
                let timestamp_ns = Self::now_nanos(clock);
                let event = crate::telemetry::TelemetryEvent::runtime(
                    crate::telemetry::RuntimeTelemetryEvent::new(
                        GRAPH_ID,
                        timestamp_ns,
                        crate::telemetry::RuntimeTelemetryEventKind::GraphStarted,
                        Some(event_message!("graph reset")),
                    ),
                );
                telemetry.push_event(event);
            }
        }

        Ok(())
    }

    #[inline]
    fn request_stop(&mut self) {
        self.stop = true;

        const GRAPH_ID: crate::telemetry::GraphInstanceId = 0;

        if let (Some(ref clock), Some(telemetry)) = (&self.clock, self.telemetry.as_mut()) {
            if T::EVENTS_STATICALLY_ENABLED && telemetry.events_enabled() {
                let timestamp_ns = Self::now_nanos(clock);
                let event = crate::telemetry::TelemetryEvent::runtime(
                    crate::telemetry::RuntimeTelemetryEvent::new(
                        GRAPH_ID,
                        timestamp_ns,
                        crate::telemetry::RuntimeTelemetryEventKind::GraphStopped,
                        None,
                    ),
                );
                telemetry.push_event(event);
            }
        }
    }

    #[inline]
    fn is_stopping(&self) -> bool {
        self.stop
    }

    #[inline]
    fn occupancies(&self) -> &[EdgeOccupancy; EDGE_COUNT] {
        &self.occ
    }

    #[inline]
    fn step(&mut self, graph: &mut Graph) -> Result<bool, Self::Error> {
        if self.stop {
            return Ok(false);
        }

        // Safely take the clock; error if missing.
        let clock = match self.clock.take() {
            Some(c) => c,
            None => {
                return Err(RuntimeError::RuntimeInvariant(
                    RuntimeInvariantError::UninitializedClock,
                ))
            }
        };

        // Safely take telemetry; if missing, put clock back before returning.
        let mut telemetry = match self.telemetry.take() {
            Some(t) => t,
            None => {
                self.clock = Some(clock);
                return Err(RuntimeError::RuntimeInvariant(
                    RuntimeInvariantError::UninitializedTelemetry,
                ));
            }
        };

        let result = self.step_inner(graph, &clock, &mut telemetry);

        // Put telemetry and clock back.
        self.telemetry = Some(telemetry);
        self.clock = Some(clock);

        result
    }
}

impl<C, T, const NODE_COUNT: usize, const EDGE_COUNT: usize> Default
    for TestNoStdRuntime<C, T, NODE_COUNT, EDGE_COUNT>
where
    C: PlatformClock + Sized,
    T: Telemetry + Sized,
{
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

/// ===== std test runtime: scheduler-driven concurrent execution =====
#[cfg(feature = "std")]
pub mod concurrent_runtime {
    use crate::edge::EdgeOccupancy;
    use crate::errors::{RuntimeError, RuntimeInvariantError};
    use crate::event_message;
    use crate::graph::{GraphApi, ScopedGraphApi};
    use crate::node::StepResult;
    use crate::policy::WatermarkState;
    use crate::prelude::{PlatformClock, Readiness, Telemetry};
    use crate::runtime::LimenRuntime;
    use crate::scheduling::{WorkerDecision, WorkerScheduler, WorkerState};
    use crate::types::NodeIndex;

    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::Arc;

    // ------------------------------------------------------------------
    // SimpleBackoffScheduler
    // ------------------------------------------------------------------

    /// Simple backoff scheduler for test/bench use.
    ///
    /// - Steps immediately when ready or when there's no prior result.
    /// - Backs off on backpressure (longer wait).
    /// - Idles on no-input/waiting (shorter wait).
    /// - Stops when the shared `AtomicBool` is set.
    pub struct SimpleBackoffScheduler {
        stop: Arc<AtomicBool>,
        idle_micros: u64,
        backpressure_micros: u64,
    }

    impl SimpleBackoffScheduler {
        /// Create a new scheduler with the given stop flag and backoff durations.
        pub fn new(stop: Arc<AtomicBool>, idle_micros: u64, backpressure_micros: u64) -> Self {
            Self {
                stop,
                idle_micros,
                backpressure_micros,
            }
        }
    }

    impl WorkerScheduler for SimpleBackoffScheduler {
        fn decide(&self, state: &WorkerState) -> WorkerDecision {
            // Honor immediate stop.
            if self.stop.load(Ordering::Relaxed) {
                return WorkerDecision::Stop;
            }

            // If we have a last step result, handle the authoritative cases first.
            if let Some(last) = state.last_step {
                match last {
                    StepResult::Terminal => return WorkerDecision::Stop,
                    StepResult::Backpressured => {
                        return WorkerDecision::WaitMicros(self.backpressure_micros)
                    }
                    StepResult::MadeProgress => {}
                    // For NoInput / WaitingOnExternal / YieldUntil(_) we'll consult readiness below.
                    StepResult::NoInput
                    | StepResult::WaitingOnExternal
                    | StepResult::YieldUntil(_) => {}
                }
            }

            // Either last_step was None, or the last step did not make progress.
            // Use the node's computed readiness (set by the worker loop) to decide.
            match state.readiness {
                Readiness::Ready | Readiness::ReadyUnderPressure => WorkerDecision::Step,
                Readiness::NotReady => WorkerDecision::WaitMicros(self.idle_micros),
            }
        }
    }

    // ------------------------------------------------------------------
    // TestScopedRuntime
    // ------------------------------------------------------------------

    /// Concurrent runtime that delegates to `ScopedGraphApi::run_scoped`.
    ///
    /// Implements `LimenRuntime`. The runtime is the orchestrator:
    /// - `step()`: sequential round-robin via `GraphApi::step_node_by_index`.
    ///   For debug/test single-tick use.
    /// - `run()`: overrides default. Creates a `SimpleBackoffScheduler` and calls
    ///   `graph.run_scoped(clock, telemetry, scheduler)` — one scoped thread per
    ///   node, true concurrent execution, scheduler-controlled stepping.
    pub struct TestScopedRuntime<C, T, const NODE_COUNT: usize, const EDGE_COUNT: usize>
    where
        C: PlatformClock + Clone + Send + Sync + 'static,
        T: Telemetry + Clone + Send + 'static,
    {
        stop: Arc<AtomicBool>,
        occ: [EdgeOccupancy; EDGE_COUNT],
        /// Per-node last step result, used by `step()` to make scheduler decisions.
        node_last_step: [Option<StepResult>; NODE_COUNT],
        clock: Option<C>,
        telemetry: Option<T>,
    }

    impl<C, T, const NODE_COUNT: usize, const EDGE_COUNT: usize>
        TestScopedRuntime<C, T, NODE_COUNT, EDGE_COUNT>
    where
        C: PlatformClock + Clone + Send + Sync + 'static,
        T: Telemetry + Clone + Send + 'static,
    {
        /// Construct with pessimistic initial occupancy; `init()` will overwrite.
        pub fn new() -> Self {
            const INIT_OCC: EdgeOccupancy = EdgeOccupancy::new(0, 0, WatermarkState::AtOrAboveHard);
            Self {
                stop: Arc::new(AtomicBool::new(false)),
                occ: [INIT_OCC; EDGE_COUNT],
                node_last_step: [None; NODE_COUNT],
                clock: None,
                telemetry: None,
            }
        }

        /// Internal helper for a monotonic nanosecond timestamp.
        #[inline]
        fn now_nanos(clock: &C) -> u64 {
            let ticks = clock.now_ticks();
            clock.ticks_to_nanos(ticks)
        }

        /// Safely access telemetry by mutable reference, if present.
        #[inline]
        pub fn with_telemetry<F, R>(&mut self, f: F) -> Result<Option<R>, RuntimeError>
        where
            F: FnOnce(&mut T) -> R,
        {
            if let Some(t) = self.telemetry.as_mut() {
                let r = f(t);
                Ok(Some(r))
            } else {
                Ok(None)
            }
        }
    }

    impl<Graph, C, T, const NODE_COUNT: usize, const EDGE_COUNT: usize>
        LimenRuntime<Graph, NODE_COUNT, EDGE_COUNT>
        for TestScopedRuntime<C, T, NODE_COUNT, EDGE_COUNT>
    where
        Graph: GraphApi<NODE_COUNT, EDGE_COUNT> + ScopedGraphApi<NODE_COUNT, EDGE_COUNT>,
        C: PlatformClock + Clone + Send + Sync + 'static,
        T: Telemetry + Clone + Send + 'static,
    {
        type Clock = C;
        type Telemetry = T;
        type Error = RuntimeError;

        #[cfg(feature = "std")]
        type StopHandle = crate::runtime::RuntimeStopHandle;

        fn init(
            &mut self,
            graph: &mut Graph,
            clock: Self::Clock,
            mut telemetry: Self::Telemetry,
        ) -> Result<(), Self::Error> {
            const GRAPH_ID: crate::telemetry::GraphInstanceId = 0;

            graph.validate_graph().map_err(RuntimeError::from)?;

            graph
                .write_all_edge_occupancies(&mut self.occ)
                .map_err(RuntimeError::from)?;

            if T::EVENTS_STATICALLY_ENABLED && telemetry.events_enabled() {
                let timestamp_ns = Self::now_nanos(&clock);
                let event = crate::telemetry::TelemetryEvent::runtime(
                    crate::telemetry::RuntimeTelemetryEvent::new(
                        GRAPH_ID,
                        timestamp_ns,
                        crate::telemetry::RuntimeTelemetryEventKind::GraphStarted,
                        None,
                    ),
                );
                telemetry.push_event(event);
            }

            self.clock = Some(clock);
            self.telemetry = Some(telemetry);
            self.stop.store(false, Ordering::Relaxed);
            self.node_last_step = [None; NODE_COUNT];

            Ok(())
        }

        fn reset(&mut self, graph: &Graph) -> Result<(), Self::Error> {
            const GRAPH_ID: crate::telemetry::GraphInstanceId = 0;

            self.stop.store(false, Ordering::Relaxed);
            self.node_last_step = [None; NODE_COUNT];
            graph
                .write_all_edge_occupancies(&mut self.occ)
                .map_err(RuntimeError::from)?;

            if let (Some(ref clock), Some(telemetry)) = (&self.clock, self.telemetry.as_mut()) {
                if T::EVENTS_STATICALLY_ENABLED && telemetry.events_enabled() {
                    let timestamp_ns = Self::now_nanos(clock);
                    let event = crate::telemetry::TelemetryEvent::runtime(
                        crate::telemetry::RuntimeTelemetryEvent::new(
                            GRAPH_ID,
                            timestamp_ns,
                            crate::telemetry::RuntimeTelemetryEventKind::GraphStarted,
                            Some(event_message!("graph reset")),
                        ),
                    );
                    telemetry.push_event(event);
                }
            }

            Ok(())
        }

        fn request_stop(&mut self) {
            const GRAPH_ID: crate::telemetry::GraphInstanceId = 0;

            self.stop.store(true, Ordering::Relaxed);

            if let (Some(ref clock), Some(telemetry)) = (&self.clock, self.telemetry.as_mut()) {
                if T::EVENTS_STATICALLY_ENABLED && telemetry.events_enabled() {
                    let timestamp_ns = Self::now_nanos(clock);
                    let event = crate::telemetry::TelemetryEvent::runtime(
                        crate::telemetry::RuntimeTelemetryEvent::new(
                            GRAPH_ID,
                            timestamp_ns,
                            crate::telemetry::RuntimeTelemetryEventKind::GraphStopped,
                            None,
                        ),
                    );
                    telemetry.push_event(event);
                }
            }
        }

        #[cfg(feature = "std")]
        fn stop_handle(&self) -> Option<Self::StopHandle> {
            Some(crate::runtime::RuntimeStopHandle::new(self.stop.clone()))
        }

        #[inline]
        fn is_stopping(&self) -> bool {
            self.stop.load(Ordering::Relaxed)
        }

        #[inline]
        fn occupancies(&self) -> &[EdgeOccupancy; EDGE_COUNT] {
            &self.occ
        }

        /// Sequential scheduler-driven step for debug/test single-tick use.
        ///
        /// For each node, consults `SimpleBackoffScheduler` using the stored
        /// per-node `last_step` to decide whether to step. This ensures the
        /// same scheduling policy governs both `step()` and `run()`.
        ///
        /// `WaitMicros` decisions are treated as "skip this tick" — the
        /// `last_step` is then cleared so the scheduler returns `Step` on
        /// the next `step()` call. This avoids permanent starvation in
        /// sequential mode where there is no real time passage between ticks.
        fn step(&mut self, graph: &mut Graph) -> Result<bool, Self::Error> {
            if <TestScopedRuntime<C, T, NODE_COUNT, EDGE_COUNT> as LimenRuntime<
                Graph,
                NODE_COUNT,
                EDGE_COUNT,
            >>::is_stopping(self)
            {
                return Ok(false);
            }

            let clock = match self.clock.take() {
                Some(c) => c,
                None => {
                    return Err(RuntimeError::RuntimeInvariant(
                        RuntimeInvariantError::UninitializedClock,
                    ))
                }
            };
            let mut telemetry = match self.telemetry.take() {
                Some(t) => t,
                None => {
                    self.clock = Some(clock);
                    return Err(RuntimeError::RuntimeInvariant(
                        RuntimeInvariantError::UninitializedTelemetry,
                    ));
                }
            };

            let scheduler = SimpleBackoffScheduler::new(self.stop.clone(), 50, 200);
            let mut any_progress = false;

            // Refresh a cheap occupancy snapshot for this tick so we can compute
            // inexpensive readiness for each node (same idea as run_scoped workers).
            graph
                .write_all_edge_occupancies(&mut self.occ)
                .map_err(RuntimeError::from)?;

            for i in 0..NODE_COUNT {
                let mut state = WorkerState::new(i, NODE_COUNT, clock.now_ticks());
                state.last_step = self.node_last_step[i];

                // Compute max output backpressure and whether any input has items
                // by scanning the graph's edge descriptors and the occupancy
                // snapshot `self.occ`.
                let mut _max_wm = WatermarkState::BelowSoft;
                let mut any_input_has_items = false;
                let node_idx = NodeIndex::from(i);

                for ed in graph.get_edge_descriptors().iter() {
                    let eid = *ed.id().as_usize();
                    // Upstream edges contribute to output backpressure.
                    if ed.upstream().node() == &node_idx {
                        let occ = &self.occ[eid];
                        if *occ.watermark() > _max_wm {
                            _max_wm = *occ.watermark();
                        }
                    }
                    // Downstream edges determine whether this node has any input items.
                    if ed.downstream().node() == &node_idx {
                        let occ = &self.occ[eid];
                        if *occ.items() > 0 {
                            any_input_has_items = true;
                        }
                    }
                }
                state.backpressure = _max_wm;

                // Compatibility: if we have no prior step (first probe) let the
                // scheduler see Ready so the node gets probed at least once.
                if state.last_step.is_none() {
                    state.readiness = Readiness::Ready;
                } else {
                    // Otherwise use the cheap pre-check: if there are items on any
                    // input edge (ingress included) we are Ready; otherwise NotReady.
                    state.readiness = if any_input_has_items {
                        if _max_wm >= WatermarkState::BetweenSoftAndHard {
                            Readiness::ReadyUnderPressure
                        } else {
                            Readiness::Ready
                        }
                    } else {
                        Readiness::NotReady
                    };
                }

                // Ask the scheduler and log the decision for debugging.
                let decision = scheduler.decide(&state);
                // ::std::eprintln!(
                //     "sched-debug: node={} last_step={:?} readiness={:?} => decision={:?}",
                //     i,
                //     state.last_step,
                //     state.readiness,
                //     decision
                // );

                match decision {
                    WorkerDecision::Step => {
                        match graph.step_node_by_index(i, &clock, &mut telemetry) {
                            Ok(sr) => {
                                // Record last step result and note progress.
                                self.node_last_step[i] = Some(sr);
                                if matches!(sr, StepResult::MadeProgress | StepResult::Terminal) {
                                    any_progress = true;
                                }

                                // **Crucial:** update occupancy snapshot immediately so
                                // downstream nodes in this same step() iteration see the
                                // newly-produced items and compute readiness correctly.
                                graph
                                    .write_all_edge_occupancies(&mut self.occ)
                                    .map_err(RuntimeError::from)?;
                            }
                            Err(e) => {
                                ::std::eprintln!("sched-debug: node={} step error: {:?}", i, e);
                                // NodeError is not fatal; clear state so we retry.
                                self.node_last_step[i] = None;
                            }
                        }
                    }
                    WorkerDecision::WaitMicros(_) => {
                        // In sequential mode, skip this node for this tick.
                        // Clear last_step so the scheduler returns Step next tick,
                        // preventing permanent starvation.
                        self.node_last_step[i] = None;
                    }
                    WorkerDecision::Stop => {
                        // Node terminated or runtime stopping; do not step.
                    }
                }
            }

            // No final write_all_edge_occupancies here: we update occupancies
            // right after each successful step above, so self.occ is up-to-date.

            self.telemetry = Some(telemetry);
            self.clock = Some(clock);

            Ok(any_progress)
        }

        /// Concurrent execution via `ScopedGraphApi::run_scoped`.
        ///
        /// Creates a `SimpleBackoffScheduler` and delegates to the graph's
        /// scoped thread infrastructure. Each node gets its own thread;
        /// the scheduler controls per-worker stepping.
        ///
        /// After all threads join, refreshes the occupancy snapshot.
        fn run(&mut self, graph: &mut Graph) -> Result<(), Self::Error> {
            const GRAPH_ID: crate::telemetry::GraphInstanceId = 0;

            let clock = self.clock.clone().ok_or(RuntimeError::RuntimeInvariant(
                RuntimeInvariantError::UninitializedClock,
            ))?;
            let telemetry = self
                .telemetry
                .clone()
                .ok_or(RuntimeError::RuntimeInvariant(
                    RuntimeInvariantError::UninitializedTelemetry,
                ))?;

            let scheduler = SimpleBackoffScheduler::new(
                self.stop.clone(),
                50,  // idle_micros
                200, // backpressure_micros
            );

            graph.run_scoped(clock, telemetry, scheduler);

            // After scope exits (all threads joined): refresh occupancies.
            graph
                .write_all_edge_occupancies(&mut self.occ)
                .map_err(RuntimeError::from)?;

            if let (Some(ref clock), Some(telemetry)) = (&self.clock, self.telemetry.as_mut()) {
                if T::EVENTS_STATICALLY_ENABLED && telemetry.events_enabled() {
                    let timestamp_ns = Self::now_nanos(clock);
                    let event = crate::telemetry::TelemetryEvent::runtime(
                        crate::telemetry::RuntimeTelemetryEvent::new(
                            GRAPH_ID,
                            timestamp_ns,
                            crate::telemetry::RuntimeTelemetryEventKind::GraphStopped,
                            None,
                        ),
                    );
                    telemetry.push_event(event);
                }
            }

            Ok(())
        }
    }

    impl<C, T, const NODE_COUNT: usize, const EDGE_COUNT: usize> Default
        for TestScopedRuntime<C, T, NODE_COUNT, EDGE_COUNT>
    where
        C: PlatformClock + Clone + Send + Sync + 'static,
        T: Telemetry + Clone + Send + 'static,
    {
        #[inline]
        fn default() -> Self {
            Self::new()
        }
    }
}