obzenflow_runtime 0.2.4

Runtime services for ObzenFlow - execution and coordination business logic
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
// SPDX-License-Identifier: MIT OR Apache-2.0
// SPDX-FileCopyrightText: 2025-2026 ObzenFlow Contributors
// https://obzenflow.dev

//! [`JournalProbe`] (FLOWIP-114h, extended by FLOWIP-114n): assert on per-stage
//! data-event progress through a stage's data journal.
//!
//! FLOWIP-114h specified the probe for the non-cyclic single-writer-per-stage
//! case. FLOWIP-114n extends the surface with explicit semantics for:
//! - fan-in (vector-clock component observation vs author attribution),
//! - direct-parent lineage matching,
//! - cycle-depth filtering (`cycle_scc_id` + `cycle_depth`),
//! - paused-time no-event assertions that advance virtual time explicitly.
//!
//! The probe counts *data envelopes* only (by `ChainPayload::Data`), and it
//! counts them regardless of `processing_info.status`. Error-marked data events
//! are therefore counted by default.

use crate::testing::stage_journal::StageJournalLookupError;
use crate::testing::test_clock::{SettleSchedulerError, TestClockError};
use crate::testing::{FlowTestHarness, TestClock};
use obzenflow_core::event::chain_event::ChainEvent;
use obzenflow_core::event::journal_record::JournalRecord;
use obzenflow_core::event::{ChainPayload, EventId, WriterId};
use obzenflow_core::journal::Journal;
use obzenflow_core::{CycleDepth, SccId, StageId};
use std::sync::Arc;
use std::time::Duration;
use thiserror::Error;

/// Failure modes for [`JournalProbe`].
#[derive(Debug, Error)]
pub enum JournalProbeError {
    /// Stage-journal lookup failed (unknown name, ambiguous, or missing
    /// journal).
    #[error(transparent)]
    StageJournalLookup(#[from] StageJournalLookupError),

    /// Reading the stage journal failed (I/O, serialisation, etc.).
    #[error("failed to read stage journal: {0}")]
    JournalRead(String),

    /// The flow handle was built without a system journal.
    #[error(
        "flow handle has no system journal; \
         cannot capture system events (build the handle with a system journal)"
    )]
    MissingSystemJournal,

    /// `expect_event(n)` was called but the journal contained fewer than
    /// `n` data envelopes when the wait expired.
    #[error(
        "expected {expected} data event(s) but observed {observed} \
         on stage `{stage}` after waiting"
    )]
    NotEnoughEvents {
        stage: String,
        expected: u64,
        observed: u64,
    },

    /// `expect_no_event_within(...)` observed a data envelope after the
    /// scheduler barrier completed.
    #[error(
        "expected no data event within {window:?} on stage `{stage}`, \
         but observed {observed} envelope(s)"
    )]
    UnexpectedEvent {
        stage: String,
        window: Duration,
        observed: u64,
    },

    /// The observed envelope vector clock did not include the target stage's
    /// writer component.
    #[error(
        "missing stage-writer component `{writer_key}` in vector clock for event `{event_id}` \
         (stage id `{stage_id}`)"
    )]
    MissingStageWriterSeq {
        stage_id: StageId,
        writer_key: String,
        event_id: EventId,
    },

    /// A paused-time assertion was attempted without a paused Tokio runtime.
    #[error(transparent)]
    Clock(#[from] TestClockError),

    /// Scheduler barrier failed to reach a stable observation under paused time.
    #[error(transparent)]
    SchedulerBarrier(#[from] SettleSchedulerError),
}

/// One observed data envelope and its derived stage-writer sequence number.
///
/// The producing stage writer's seq is the value the metrics aggregator's
/// exported watermark must cover before `MetricsBarrier::wait_for_stage_seq`
/// resolves. The envelope vector clock keyed by `WriterId::from(stage_id)`
/// carries that seq; this struct exposes it through
/// [`Self::stage_writer_seq`].
pub struct JournalProbeEvent {
    stage_id: StageId,
    envelope: JournalRecord<ChainPayload>,
}

impl JournalProbeEvent {
    /// The producing stage's seq, derived from the envelope vector clock
    /// keyed by `WriterId::from(stage_id).to_string()`.
    ///
    /// For the non-cyclic single-writer-per-stage case this probe is
    /// specified for, the envelope's vector clock must contain the writer
    /// component keyed by `WriterId::from(stage_id)`. Missing keys are helper
    /// or event-construction bugs, not expected test control flow.
    pub fn stage_writer_seq(&self) -> Result<u64, JournalProbeError> {
        let key = WriterId::from(self.stage_id).to_string();
        self.envelope
            .envelope
            .provenance
            .journal
            .vector_clock
            .clocks
            .get(&key)
            .copied()
            .ok_or_else(|| JournalProbeError::MissingStageWriterSeq {
                stage_id: self.stage_id,
                writer_key: key,
                event_id: self.envelope.envelope.provenance.event.id,
            })
    }

    /// The observed envelope.
    pub fn envelope(&self) -> &JournalRecord<ChainPayload> {
        &self.envelope
    }
}

/// Per-stage data-journal probe.
///
/// Construct with [`JournalProbe::try_on_stage`] before the flow is
/// started or moved into [`FlowHandle::run`]; the probe owns a cloned
/// `Arc<dyn Journal<ChainEvent>>` and remains usable after the handle has
/// been consumed.
pub struct JournalProbe {
    stage_name: String,
    stage_id: StageId,
    journal: Arc<dyn Journal<ChainEvent>>,
}

impl JournalProbe {
    /// Construct a probe directly from a stage id and stage journal.
    ///
    /// This is primarily for infra tests that assemble supervisors without a
    /// [`FlowTestHarness`]. `stage_name` is used for error messages only.
    pub fn on_journal(
        stage_name: impl Into<String>,
        stage_id: StageId,
        journal: Arc<dyn Journal<ChainEvent>>,
    ) -> Self {
        Self {
            stage_name: stage_name.into(),
            stage_id,
            journal,
        }
    }

    /// Build a probe pointing at the named stage's data journal.
    pub fn try_on_stage(
        handle: &FlowTestHarness,
        stage_name: &str,
    ) -> Result<Self, JournalProbeError> {
        let (stage_id, journal) = handle.stage_journal_for_test(stage_name)?;
        Ok(Self {
            stage_name: stage_name.to_string(),
            stage_id,
            journal,
        })
    }

    /// Wait until the stage has produced `n` data-event envelopes and
    /// return the `n`-th one.
    ///
    /// Counts data envelopes only: observability, lifecycle, and delivery
    /// envelopes routed through the same stage journal are ignored. Under
    /// paused-time runtimes, this loops on the pull-based journal cursor
    /// with `tokio::time::sleep` between EOF reads; under live runtimes the
    /// sleep simply yields wall time.
    pub async fn expect_event(&self, n: u64) -> Result<JournalProbeEvent, JournalProbeError> {
        assert!(n >= 1, "expect_event(n): n must be >= 1");
        loop {
            let envelopes = self.read_all_envelopes().await?;
            let mut data_count: u64 = 0;
            for envelope in envelopes {
                if envelope.consumes_data_credit() {
                    data_count += 1;
                    if data_count == n {
                        return Ok(JournalProbeEvent {
                            stage_id: self.stage_id,
                            envelope,
                        });
                    }
                }
            }
            // Pull-based journal: wait briefly between reads.
            // Under paused time this allows the runtime to auto-advance; under
            // live time it avoids a busy poll loop at EOF (FLOWIP-114h).
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
    }

    /// Wait until the stage has produced `n` data-event envelopes whose vector clock
    /// observes the given writer component (non-zero seq) and return the `n`-th one.
    ///
    /// This asserts only "the observed envelope's vector clock contains ancestry from
    /// `writer_id`". It does not assert author attribution, upstream-edge provenance, or
    /// direct lineage.
    pub async fn expect_event_observing_clock_component(
        &self,
        writer_id: WriterId,
        n: u64,
    ) -> Result<JournalProbeEvent, JournalProbeError> {
        assert!(
            n >= 1,
            "expect_event_observing_clock_component(n): n must be >= 1"
        );
        let writer_key = writer_id.to_string();
        loop {
            let envelopes = self.read_all_envelopes().await?;
            let mut count: u64 = 0;
            for envelope in envelopes {
                if !envelope.consumes_data_credit() {
                    continue;
                }
                if envelope
                    .envelope
                    .provenance
                    .journal
                    .vector_clock
                    .get(&writer_key)
                    == 0
                {
                    continue;
                }
                count += 1;
                if count == n {
                    return Ok(JournalProbeEvent {
                        stage_id: self.stage_id,
                        envelope,
                    });
                }
            }
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
    }

    /// Count the number of data-event envelopes observed so far whose vector clocks
    /// observe the given writer component (non-zero seq).
    pub async fn events_observing_clock_component_so_far(
        &self,
        writer_id: WriterId,
    ) -> Result<u64, JournalProbeError> {
        let writer_key = writer_id.to_string();
        let envelopes = self.read_all_envelopes().await?;
        Ok(envelopes
            .into_iter()
            .filter(|env| {
                env.consumes_data_credit()
                    && env
                        .envelope
                        .provenance
                        .journal
                        .vector_clock
                        .get(&writer_key)
                        != 0
            })
            .count() as u64)
    }

    /// Wait until the stage has produced `n` data-event envelopes whose direct parent
    /// is `parent_event_id` (matching `event.causality.parent_ids.first()`).
    pub async fn expect_event_child_of(
        &self,
        parent_event_id: EventId,
        n: u64,
    ) -> Result<JournalProbeEvent, JournalProbeError> {
        assert!(n >= 1, "expect_event_child_of(n): n must be >= 1");
        loop {
            let envelopes = self.read_all_envelopes().await?;
            let mut count: u64 = 0;
            for envelope in envelopes {
                if !envelope.consumes_data_credit() {
                    continue;
                }
                if envelope
                    .envelope
                    .provenance
                    .event
                    .causality
                    .parent_ids
                    .first()
                    != Some(&parent_event_id)
                {
                    continue;
                }
                count += 1;
                if count == n {
                    return Ok(JournalProbeEvent {
                        stage_id: self.stage_id,
                        envelope,
                    });
                }
            }
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
    }

    /// Wait until the stage has produced `n` data-event envelopes authored by `writer_id`.
    ///
    /// This matches `envelope.event.writer_id` and must not be used as a proxy for
    /// fan-in upstream provenance.
    pub async fn expect_event_authored_by(
        &self,
        writer_id: WriterId,
        n: u64,
    ) -> Result<JournalProbeEvent, JournalProbeError> {
        assert!(n >= 1, "expect_event_authored_by(n): n must be >= 1");
        loop {
            let envelopes = self.read_all_envelopes().await?;
            let mut count: u64 = 0;
            for envelope in envelopes {
                if !envelope.consumes_data_credit() {
                    continue;
                }
                if envelope.envelope.provenance.event.writer_id != writer_id {
                    continue;
                }
                count += 1;
                if count == n {
                    return Ok(JournalProbeEvent {
                        stage_id: self.stage_id,
                        envelope,
                    });
                }
            }
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
    }

    /// Wait until the stage has produced `n` data-event envelopes at the requested cycle depth.
    ///
    /// Matches `ChainEvent::cycle_scc_id == Some(scc_id)` and
    /// `ChainEvent::cycle_depth == Some(depth)`.
    ///
    /// Under fan-out inside an SCC, multiple derived children may share the same
    /// `(cycle_scc_id, cycle_depth)` pair; this method counts matching envelopes, not
    /// logical cycle iterations.
    pub async fn expect_event_at_cycle_depth(
        &self,
        scc_id: SccId,
        depth: CycleDepth,
        n: u64,
    ) -> Result<JournalProbeEvent, JournalProbeError> {
        assert!(n >= 1, "expect_event_at_cycle_depth(n): n must be >= 1");
        loop {
            let envelopes = self.read_all_envelopes().await?;
            let mut count: u64 = 0;
            for envelope in envelopes {
                if !envelope.consumes_data_credit() {
                    continue;
                }
                if envelope.envelope.provenance.event.cycle_scc_id != Some(scc_id) {
                    continue;
                }
                if envelope.envelope.provenance.event.cycle_depth != Some(depth) {
                    continue;
                }
                count += 1;
                if count == n {
                    return Ok(JournalProbeEvent {
                        stage_id: self.stage_id,
                        envelope,
                    });
                }
            }
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
    }

    /// Number of data-event envelopes observed so far, without blocking.
    pub async fn events_received_so_far(&self) -> Result<u64, JournalProbeError> {
        let envelopes = self.read_all_envelopes().await?;
        Ok(envelopes
            .into_iter()
            .filter(|env| env.consumes_data_credit())
            .count() as u64)
    }

    /// Assert no data envelope arrives within `window`.
    ///
    /// Under paused time, this sleeps for `window`, then runs a scheduler
    /// barrier that yields until the runtime reports no scheduled work,
    /// before reading the journal. This pins the boundary-instant case: a
    /// task scheduled exactly at the boundary is fully polled before the
    /// final read.
    pub async fn expect_no_event_within(&self, window: Duration) -> Result<(), JournalProbeError> {
        let baseline = self.events_received_so_far().await?;
        tokio::time::sleep(window).await;
        let observed = TestClock::settle_scheduler(|| self.events_received_so_far()).await?;
        let delta = observed.saturating_sub(baseline);
        if delta == 0 {
            Ok(())
        } else {
            Err(JournalProbeError::UnexpectedEvent {
                stage: self.stage_name.clone(),
                window,
                observed: delta,
            })
        }
    }

    /// Assert that no additional data events appear after a paused-time scheduler settle.
    ///
    /// This does not advance time; it is intended for cycle-aware tests that control
    /// virtual time advancement explicitly and need a stable post-advance boundary.
    pub async fn expect_no_event_after_settle(&self) -> Result<(), JournalProbeError> {
        let baseline = self.events_received_so_far().await?;
        let observed = TestClock::settle_scheduler(|| self.events_received_so_far()).await?;
        let delta = observed.saturating_sub(baseline);
        if delta == 0 {
            Ok(())
        } else {
            Err(JournalProbeError::UnexpectedEvent {
                stage: self.stage_name.clone(),
                window: Duration::ZERO,
                observed: delta,
            })
        }
    }

    /// Assert that no additional data events appear while advancing paused Tokio time by `window`.
    ///
    /// The caller supplies a [`TestClock`] so the API is explicit about paused-time usage.
    pub async fn expect_no_event_during(
        &self,
        clock: &TestClock,
        window: Duration,
    ) -> Result<(), JournalProbeError> {
        let baseline = self.events_received_so_far().await?;
        clock.advance(window).await?;
        let observed = TestClock::settle_scheduler(|| self.events_received_so_far()).await?;
        let delta = observed.saturating_sub(baseline);
        if delta == 0 {
            Ok(())
        } else {
            Err(JournalProbeError::UnexpectedEvent {
                stage: self.stage_name.clone(),
                window,
                observed: delta,
            })
        }
    }

    async fn read_all_envelopes(
        &self,
    ) -> Result<Vec<JournalRecord<ChainPayload>>, JournalProbeError> {
        let mut reader = self
            .journal
            .reader()
            .await
            .map_err(|e| JournalProbeError::JournalRead(e.to_string()))?;
        let mut envelopes = Vec::new();
        loop {
            match reader.next().await {
                Ok(Some(env)) => envelopes.push(env),
                Ok(None) => return Ok(envelopes),
                Err(e) => return Err(JournalProbeError::JournalRead(e.to_string())),
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::id_conversions::StageIdExt;
    use crate::metrics::observations::ObservationRegistry;
    use crate::pipeline::fsm::PipelineFsmEvent;
    use crate::pipeline::handle::FlowHandleExtras;
    use crate::pipeline::{FlowHandle, PipelineState};
    use crate::supervised_base::{ChannelBuilder, HandleBuilder, SupervisorTaskBuilder};
    use chrono::Utc;
    use obzenflow_core::event::journal_record::JournalRecord;
    use obzenflow_core::event::observability::NoObservations;
    use obzenflow_core::event::provenance::JournalProvenance;
    use obzenflow_core::event::status::processing_status::ProcessingStatus;
    use obzenflow_core::event::vector_clock::VectorClock;
    use obzenflow_core::event::{
        ChainEvent, ChainEventFactory, JournalEvent, JournalWriterId, WriterId,
    };
    use obzenflow_core::id::JournalId;
    use obzenflow_core::journal::journal_error::JournalError;
    use obzenflow_core::journal::journal_owner::JournalOwner;
    use obzenflow_core::journal::reader::JournalReader;
    use obzenflow_core::journal::Journal;
    use obzenflow_topology::TopologyBuilder;
    use std::sync::{Arc, Mutex};
    use std::time::Duration;

    fn test_scc_id(n: u128) -> SccId {
        SccId::from_ulid(obzenflow_core::Ulid::from(n))
    }

    struct MemoryJournal<T: JournalEvent> {
        id: JournalId,
        owner: Option<JournalOwner>,
        events: Arc<Mutex<Vec<JournalRecord<T::Payload>>>>,
    }

    impl<T: JournalEvent> Default for MemoryJournal<T> {
        fn default() -> Self {
            Self {
                id: JournalId::new(),
                owner: None,
                events: Arc::new(Mutex::new(Vec::new())),
            }
        }
    }

    impl<T: JournalEvent> MemoryJournal<T> {
        fn push_envelope(&self, envelope: JournalRecord<T::Payload>) {
            let mut guard = self.events.lock().expect("MemoryJournal: poisoned lock");
            guard.push(envelope);
        }
    }

    struct MemoryJournalReader<T: JournalEvent> {
        events: Arc<Mutex<Vec<JournalRecord<T::Payload>>>>,
        pos: usize,
    }

    #[async_trait::async_trait]
    impl<T> JournalReader<T> for MemoryJournalReader<T>
    where
        T: JournalEvent,
    {
        async fn next(&mut self) -> Result<Option<JournalRecord<T::Payload>>, JournalError> {
            let guard = self
                .events
                .lock()
                .expect("MemoryJournalReader: poisoned lock");
            if self.pos >= guard.len() {
                return Ok(None);
            }
            let envelope = guard[self.pos].clone();
            drop(guard);
            self.pos += 1;
            Ok(Some(envelope))
        }

        fn position(&self) -> u64 {
            self.pos as u64
        }
    }

    #[async_trait::async_trait]
    impl<T> Journal<T> for MemoryJournal<T>
    where
        T: JournalEvent + 'static,
    {
        fn id(&self) -> &JournalId {
            &self.id
        }

        fn owner(&self) -> Option<&JournalOwner> {
            self.owner.as_ref()
        }

        async fn append(
            &self,
            event: T,
            mut options: obzenflow_core::journal::AppendOptions<'_, T>,
        ) -> Result<JournalRecord<T::Payload>, JournalError> {
            let event = options.capture.prepare(0, event);
            let envelope = JournalRecord::new(JournalWriterId::from(self.id), event);
            let mut guard = self.events.lock().expect("MemoryJournal: poisoned lock");
            guard.push(envelope.clone());
            Ok(envelope)
        }

        async fn read_all_unordered(&self) -> Result<Vec<JournalRecord<T::Payload>>, JournalError> {
            let guard = self.events.lock().expect("MemoryJournal: poisoned lock");
            Ok(guard.clone())
        }

        async fn read_event(
            &self,
            event_id: &obzenflow_core::event::types::EventId,
        ) -> Result<Option<JournalRecord<T::Payload>>, JournalError> {
            let guard = self.events.lock().expect("MemoryJournal: poisoned lock");
            Ok(guard.iter().find(|e| e.id() == event_id).cloned())
        }

        async fn reader_from(
            &self,
            position: u64,
        ) -> Result<Box<dyn JournalReader<T>>, JournalError> {
            Ok(Box::new(MemoryJournalReader {
                events: Arc::clone(&self.events),
                pos: position as usize,
            }))
        }

        async fn read_last_n(
            &self,
            count: usize,
        ) -> Result<Vec<JournalRecord<T::Payload>>, JournalError> {
            let guard = self.events.lock().expect("MemoryJournal: poisoned lock");
            let len = guard.len();
            let start = len.saturating_sub(count);
            Ok(guard[start..].iter().rev().cloned().collect())
        }
    }

    fn harness_with_stage_journal(
        stage_name: &str,
        stage_id: StageId,
        stage_journal: Arc<dyn Journal<ChainEvent>>,
        topology: Arc<obzenflow_topology::Topology>,
    ) -> FlowTestHarness {
        let (event_sender, _event_receiver, state_watcher) =
            ChannelBuilder::<PipelineFsmEvent, PipelineState>::new().build(PipelineState::Created);
        let supervisor_task = SupervisorTaskBuilder::<PipelineState>::new("dummy_pipeline")
            .spawn_for_test(
                || async move { Ok::<(), Box<dyn std::error::Error + Send + Sync>>(()) },
            );
        let standard_handle = HandleBuilder::new()
            .with_event_sender(event_sender)
            .with_state_watcher(state_watcher)
            .with_supervisor_task(supervisor_task)
            .build_standard()
            .expect("dummy handle should build");

        let extras = FlowHandleExtras {
            observations: Arc::new(ObservationRegistry::default()),
            host_observations: Arc::new(NoObservations),

            stage_cleanup: Vec::new(),
            published_outcome: Default::default(),
            metrics: Default::default(),
            operational_failure: Default::default(),
            topology: Some(topology),
            flow_name: "dummy".to_string(),
            contract_attachments: None,
            system_journal: None,
            pipeline_writer_id: obzenflow_core::event::WriterId::from(
                obzenflow_core::id::SystemId::new(),
            ),
            liveness_snapshots: None,
            run_substrate: obzenflow_core::journal::factory::RunSubstrateState::Ephemeral,
            flow_effective_config: None,
        };

        let handle = FlowHandle::new(standard_handle, extras);
        FlowTestHarness::from_parts(handle, vec![(stage_id, stage_journal)])
            .unwrap_or_else(|e| panic!("failed to build FlowTestHarness for `{stage_name}`: {e}"))
    }

    #[tokio::test]
    async fn events_received_so_far_counts_data_envelopes_only() {
        let mut topology_builder = TopologyBuilder::new();
        let stage_topo_id = topology_builder.add_stage(Some("stage".to_string()));
        topology_builder.add_stage(Some("sink".to_string()));
        let topology = topology_builder.build_unchecked().expect("topology");
        let topology = Arc::new(topology);

        let stage_id = StageId::from_topology_id(stage_topo_id);
        let writer_id = WriterId::from(stage_id);

        let stage_journal_impl: Arc<MemoryJournal<ChainEvent>> = Arc::new(MemoryJournal::default());
        let stage_journal: Arc<dyn Journal<ChainEvent>> = stage_journal_impl.clone();

        // Non-data envelopes should be ignored by the probe.
        stage_journal
            .append(
                ChainEventFactory::eof_event(writer_id, true),
                Default::default(),
            )
            .await
            .expect("append eof");
        stage_journal
            .append(
                ChainEventFactory::data_event(writer_id, "data", serde_json::json!({})),
                Default::default(),
            )
            .await
            .expect("append data1");
        stage_journal
            .append(
                ChainEventFactory::drain_event(writer_id),
                Default::default(),
            )
            .await
            .expect("append drain");
        stage_journal
            .append(
                ChainEventFactory::data_event(writer_id, "data", serde_json::json!({})),
                Default::default(),
            )
            .await
            .expect("append data2");

        let harness = harness_with_stage_journal("stage", stage_id, stage_journal, topology);
        let probe = JournalProbe::try_on_stage(&harness, "stage").expect("probe");

        let observed = probe
            .events_received_so_far()
            .await
            .expect("events_received_so_far");
        assert_eq!(observed, 2, "expected to count data envelopes only");

        let second = probe.expect_event(2).await.expect("expect second data");
        assert!(second.envelope().consumes_data_credit());
    }

    #[tokio::test]
    async fn stage_writer_seq_errors_when_vector_clock_is_missing_writer_component() {
        let mut topology_builder = TopologyBuilder::new();
        let stage_topo_id = topology_builder.add_stage(Some("stage".to_string()));
        topology_builder.add_stage(Some("sink".to_string()));
        let topology = topology_builder.build_unchecked().expect("topology");
        let topology = Arc::new(topology);

        let stage_id = StageId::from_topology_id(stage_topo_id);
        let writer_id = WriterId::from(stage_id);
        let stage_journal_impl: Arc<MemoryJournal<ChainEvent>> = Arc::new(MemoryJournal::default());
        let stage_journal: Arc<dyn Journal<ChainEvent>> = stage_journal_impl.clone();

        let event = ChainEventFactory::data_event(writer_id, "data", serde_json::json!({}));
        let envelope = JournalRecord::commit_event(
            event,
            JournalProvenance {
                journal_writer_id: JournalWriterId::from(stage_journal_impl.id),
                vector_clock: VectorClock::new(),
                timestamp: Utc::now(),
                journal_group_id: None,
                journal_group_member: None,
            },
        )
        .expect("valid committed fixture");
        stage_journal_impl.push_envelope(envelope);

        let harness = harness_with_stage_journal("stage", stage_id, stage_journal, topology);
        let probe = JournalProbe::try_on_stage(&harness, "stage").expect("probe");

        let observed = probe.expect_event(1).await.expect("expect first data");
        let err = observed
            .stage_writer_seq()
            .expect_err("missing stage writer seq should error");
        assert!(
            matches!(err, JournalProbeError::MissingStageWriterSeq { .. }),
            "unexpected error: {err:?}"
        );
    }

    #[tokio::test(flavor = "current_thread", start_paused = true)]
    async fn expect_no_event_within_fails_when_event_written_at_boundary_instant() {
        let mut topology_builder = TopologyBuilder::new();
        let stage_topo_id = topology_builder.add_stage(Some("stage".to_string()));
        topology_builder.add_stage(Some("sink".to_string()));
        let topology = topology_builder.build_unchecked().expect("topology");
        let topology = Arc::new(topology);

        let stage_id = StageId::from_topology_id(stage_topo_id);
        let writer_id = WriterId::from(stage_id);

        let stage_journal_impl: Arc<MemoryJournal<ChainEvent>> = Arc::new(MemoryJournal::default());
        let stage_journal: Arc<dyn Journal<ChainEvent>> = stage_journal_impl.clone();

        let harness =
            harness_with_stage_journal("stage", stage_id, stage_journal.clone(), topology);
        let probe = JournalProbe::try_on_stage(&harness, "stage").expect("probe");

        let window = Duration::from_secs(1);
        tokio::spawn({
            let stage_journal = stage_journal.clone();
            async move {
                tokio::time::sleep(window).await;
                stage_journal
                    .append(
                        ChainEventFactory::data_event(writer_id, "data", serde_json::json!({})),
                        Default::default(),
                    )
                    .await
                    .expect("append data at boundary");
            }
        });

        let err = probe
            .expect_no_event_within(window)
            .await
            .expect_err("expected boundary instant event to be observed");
        assert!(
            matches!(err, JournalProbeError::UnexpectedEvent { .. }),
            "unexpected error: {err:?}"
        );
    }

    #[tokio::test(flavor = "current_thread", start_paused = true)]
    async fn expect_no_event_within_fails_on_chained_wakeup() {
        let mut topology_builder = TopologyBuilder::new();
        let stage_topo_id = topology_builder.add_stage(Some("stage".to_string()));
        topology_builder.add_stage(Some("sink".to_string()));
        let topology = topology_builder.build_unchecked().expect("topology");
        let topology = Arc::new(topology);

        let stage_id = StageId::from_topology_id(stage_topo_id);
        let writer_id = WriterId::from(stage_id);

        let stage_journal_impl: Arc<MemoryJournal<ChainEvent>> = Arc::new(MemoryJournal::default());
        let stage_journal: Arc<dyn Journal<ChainEvent>> = stage_journal_impl.clone();

        let harness =
            harness_with_stage_journal("stage", stage_id, stage_journal.clone(), topology);
        let probe = JournalProbe::try_on_stage(&harness, "stage").expect("probe");

        let window = Duration::from_secs(1);
        tokio::spawn({
            let stage_journal = stage_journal.clone();
            async move {
                tokio::time::sleep(window).await;
                tokio::spawn(async move {
                    stage_journal
                        .append(
                            ChainEventFactory::data_event(writer_id, "data", serde_json::json!({})),
                            Default::default(),
                        )
                        .await
                        .expect("append chained data");
                });
            }
        });

        let err = probe
            .expect_no_event_within(window)
            .await
            .expect_err("expected chained wakeup event to be observed");
        assert!(
            matches!(err, JournalProbeError::UnexpectedEvent { .. }),
            "unexpected error: {err:?}"
        );
    }

    #[tokio::test]
    async fn expect_event_observing_clock_component_filters_by_writer_component() {
        let mut topology_builder = TopologyBuilder::new();
        let stage_topo_id = topology_builder.add_stage(Some("stage".to_string()));
        topology_builder.add_stage(Some("sink".to_string()));
        let topology = Arc::new(topology_builder.build_unchecked().expect("topology"));

        let stage_id = StageId::from_topology_id(stage_topo_id);
        let stage_writer_id = WriterId::from(stage_id);

        let upstream_a = WriterId::from(StageId::new());
        let upstream_b = WriterId::from(StageId::new());

        let stage_journal_impl: Arc<MemoryJournal<ChainEvent>> = Arc::new(MemoryJournal::default());
        let stage_journal: Arc<dyn Journal<ChainEvent>> = stage_journal_impl.clone();

        // Non-data envelopes do not count.
        stage_journal
            .append(
                ChainEventFactory::eof_event(stage_writer_id, true),
                Default::default(),
            )
            .await
            .expect("append eof");

        let mut clock_a = VectorClock::new();
        clock_a.clocks.insert(upstream_a.to_string(), 1);
        let env_a = JournalRecord::commit_event(
            ChainEventFactory::data_event(stage_writer_id, "data.a", serde_json::json!({})),
            JournalProvenance {
                journal_writer_id: JournalWriterId::from(stage_journal_impl.id),
                vector_clock: clock_a,
                timestamp: Utc::now(),
                journal_group_id: None,
                journal_group_member: None,
            },
        )
        .expect("valid committed fixture");
        stage_journal_impl.push_envelope(env_a);

        let mut clock_b = VectorClock::new();
        clock_b.clocks.insert(upstream_b.to_string(), 1);
        let env_b = JournalRecord::commit_event(
            ChainEventFactory::data_event(stage_writer_id, "data.b", serde_json::json!({})),
            JournalProvenance {
                journal_writer_id: JournalWriterId::from(stage_journal_impl.id),
                vector_clock: clock_b,
                timestamp: Utc::now(),
                journal_group_id: None,
                journal_group_member: None,
            },
        )
        .expect("valid committed fixture");
        stage_journal_impl.push_envelope(env_b);

        let harness = harness_with_stage_journal("stage", stage_id, stage_journal, topology);
        let probe = JournalProbe::try_on_stage(&harness, "stage").expect("probe");

        let seen_a = probe
            .events_observing_clock_component_so_far(upstream_a)
            .await
            .expect("count upstream_a");
        assert_eq!(seen_a, 1);

        let seen_b = probe
            .events_observing_clock_component_so_far(upstream_b)
            .await
            .expect("count upstream_b");
        assert_eq!(seen_b, 1);

        let observed_b = probe
            .expect_event_observing_clock_component(upstream_b, 1)
            .await
            .expect("expect upstream_b-observing event");
        assert_ne!(
            observed_b
                .envelope()
                .envelope
                .provenance
                .journal
                .vector_clock
                .get(&upstream_b.to_string()),
            0
        );
    }

    #[tokio::test]
    async fn expect_event_at_cycle_depth_counts_matching_envelopes() {
        let mut topology_builder = TopologyBuilder::new();
        let stage_topo_id = topology_builder.add_stage(Some("stage".to_string()));
        topology_builder.add_stage(Some("sink".to_string()));
        let topology = Arc::new(topology_builder.build_unchecked().expect("topology"));

        let stage_id = StageId::from_topology_id(stage_topo_id);
        let writer_id = WriterId::from(stage_id);

        let scc = test_scc_id(1);
        let depth = CycleDepth::new(4);

        let stage_journal_impl: Arc<MemoryJournal<ChainEvent>> = Arc::new(MemoryJournal::default());
        let stage_journal: Arc<dyn Journal<ChainEvent>> = stage_journal_impl.clone();

        // One non-matching depth.
        let mut other = ChainEventFactory::data_event(writer_id, "other", serde_json::json!({}));
        other.cycle_scc_id = Some(scc);
        other.cycle_depth = Some(CycleDepth::new(3));
        stage_journal
            .append(other, Default::default())
            .await
            .expect("append other");

        // Two matching envelopes at the same (scc, depth).
        for label in ["match.1", "match.2"] {
            let mut ev = ChainEventFactory::data_event(writer_id, label, serde_json::json!({}));
            ev.cycle_scc_id = Some(scc);
            ev.cycle_depth = Some(depth);
            stage_journal
                .append(ev, Default::default())
                .await
                .expect("append match");
        }

        let harness = harness_with_stage_journal("stage", stage_id, stage_journal, topology);
        let probe = JournalProbe::try_on_stage(&harness, "stage").expect("probe");

        let second = probe
            .expect_event_at_cycle_depth(scc, depth, 2)
            .await
            .expect("expect 2nd match at cycle depth");
        assert_eq!(
            second.envelope().envelope.provenance.event.cycle_scc_id,
            Some(scc),
            "scc id"
        );
        assert_eq!(
            second.envelope().envelope.provenance.event.cycle_depth,
            Some(depth),
            "cycle depth"
        );
    }

    #[tokio::test]
    async fn expect_event_at_cycle_depth_counts_fan_out_children_at_same_depth() {
        let mut topology_builder = TopologyBuilder::new();
        let stage_topo_id = topology_builder.add_stage(Some("stage".to_string()));
        topology_builder.add_stage(Some("sink".to_string()));
        let topology = Arc::new(topology_builder.build_unchecked().expect("topology"));

        let stage_id = StageId::from_topology_id(stage_topo_id);
        let writer_id = WriterId::from(stage_id);

        let scc = test_scc_id(2);
        let depth = CycleDepth::new(2);

        let stage_journal_impl: Arc<MemoryJournal<ChainEvent>> = Arc::new(MemoryJournal::default());
        let stage_journal: Arc<dyn Journal<ChainEvent>> = stage_journal_impl.clone();

        let mut parent =
            ChainEventFactory::data_event(writer_id, "parent", serde_json::json!({"k": 1}));
        parent.cycle_scc_id = Some(scc);
        parent.cycle_depth = Some(depth);

        let child_1 = ChainEventFactory::derived_data_event(
            writer_id,
            &parent,
            "child.1",
            serde_json::json!({"k": 2}),
            obzenflow_core::config::LineagePolicy::default(),
        );
        let child_2 = ChainEventFactory::derived_data_event(
            writer_id,
            &parent,
            "child.2",
            serde_json::json!({"k": 3}),
            obzenflow_core::config::LineagePolicy::default(),
        );

        stage_journal
            .append(parent, Default::default())
            .await
            .expect("append parent");
        stage_journal
            .append(child_1.clone(), Default::default())
            .await
            .expect("append child_1");
        stage_journal
            .append(child_2.clone(), Default::default())
            .await
            .expect("append child_2");

        let harness = harness_with_stage_journal("stage", stage_id, stage_journal, topology);
        let probe = JournalProbe::try_on_stage(&harness, "stage").expect("probe");

        // Parent is match #1, then each child is its own distinct match.
        let third = probe
            .expect_event_at_cycle_depth(scc, depth, 3)
            .await
            .expect("expect 3rd match at cycle depth");
        assert_eq!(
            third
                .envelope()
                .envelope
                .provenance
                .event
                .causality
                .parent_ids
                .first(),
            Some(&child_2.causality.parent_ids[0]),
            "sanity: lineage still present"
        );
        assert_eq!(third.envelope().envelope.provenance.event.id, child_2.id);
    }

    #[tokio::test]
    async fn data_event_counting_includes_error_marked_data() {
        let mut topology_builder = TopologyBuilder::new();
        let stage_topo_id = topology_builder.add_stage(Some("stage".to_string()));
        topology_builder.add_stage(Some("sink".to_string()));
        let topology = Arc::new(topology_builder.build_unchecked().expect("topology"));

        let stage_id = StageId::from_topology_id(stage_topo_id);
        let writer_id = WriterId::from(stage_id);

        let stage_journal_impl: Arc<MemoryJournal<ChainEvent>> = Arc::new(MemoryJournal::default());
        let stage_journal: Arc<dyn Journal<ChainEvent>> = stage_journal_impl.clone();

        let mut ok = ChainEventFactory::data_event(writer_id, "ok", serde_json::json!({}));
        ok.processing.status = ProcessingStatus::Success;
        stage_journal
            .append(ok, Default::default())
            .await
            .expect("append ok");

        let mut err = ChainEventFactory::data_event(writer_id, "err", serde_json::json!({}));
        err.processing.status = ProcessingStatus::error("boom");
        stage_journal
            .append(err.clone(), Default::default())
            .await
            .expect("append err");

        let harness = harness_with_stage_journal("stage", stage_id, stage_journal, topology);
        let probe = JournalProbe::try_on_stage(&harness, "stage").expect("probe");

        let count = probe
            .events_received_so_far()
            .await
            .expect("events_received_so_far");
        assert_eq!(count, 2);

        let observed = probe.expect_event(2).await.expect("expect 2nd data");
        assert!(matches!(
            observed
                .envelope()
                .envelope
                .provenance
                .event
                .processing
                .status,
            ProcessingStatus::Error { .. }
        ));
        assert_eq!(observed.envelope().envelope.provenance.event.id, err.id);
    }

    #[tokio::test(flavor = "current_thread", start_paused = true)]
    async fn expect_no_event_during_advances_time_and_observes_boundary_instant_events() {
        let clock = TestClock::new().await.expect("paused runtime");

        let mut topology_builder = TopologyBuilder::new();
        let stage_topo_id = topology_builder.add_stage(Some("stage".to_string()));
        topology_builder.add_stage(Some("sink".to_string()));
        let topology = Arc::new(topology_builder.build_unchecked().expect("topology"));

        let stage_id = StageId::from_topology_id(stage_topo_id);
        let writer_id = WriterId::from(stage_id);

        let stage_journal_impl: Arc<MemoryJournal<ChainEvent>> = Arc::new(MemoryJournal::default());
        let stage_journal: Arc<dyn Journal<ChainEvent>> = stage_journal_impl.clone();

        let harness =
            harness_with_stage_journal("stage", stage_id, stage_journal.clone(), topology);
        let probe = JournalProbe::try_on_stage(&harness, "stage").expect("probe");

        let window = Duration::from_secs(1);
        tokio::spawn({
            let stage_journal = stage_journal.clone();
            async move {
                tokio::time::sleep(window).await;
                stage_journal
                    .append(
                        ChainEventFactory::data_event(writer_id, "data", serde_json::json!({})),
                        Default::default(),
                    )
                    .await
                    .expect("append data at boundary");
            }
        });

        let err = probe
            .expect_no_event_during(&clock, window)
            .await
            .expect_err("boundary instant event should be observed");
        assert!(
            matches!(err, JournalProbeError::UnexpectedEvent { .. }),
            "unexpected error: {err:?}"
        );
    }

    #[tokio::test(flavor = "current_thread", start_paused = true)]
    async fn expect_no_event_during_observes_chained_wakeup_events() {
        let clock = TestClock::new().await.expect("paused runtime");

        let mut topology_builder = TopologyBuilder::new();
        let stage_topo_id = topology_builder.add_stage(Some("stage".to_string()));
        topology_builder.add_stage(Some("sink".to_string()));
        let topology = Arc::new(topology_builder.build_unchecked().expect("topology"));

        let stage_id = StageId::from_topology_id(stage_topo_id);
        let writer_id = WriterId::from(stage_id);

        let stage_journal_impl: Arc<MemoryJournal<ChainEvent>> = Arc::new(MemoryJournal::default());
        let stage_journal: Arc<dyn Journal<ChainEvent>> = stage_journal_impl.clone();

        let harness =
            harness_with_stage_journal("stage", stage_id, stage_journal.clone(), topology);
        let probe = JournalProbe::try_on_stage(&harness, "stage").expect("probe");

        let window = Duration::from_secs(1);
        tokio::spawn({
            let stage_journal = stage_journal.clone();
            async move {
                tokio::time::sleep(window).await;
                tokio::spawn(async move {
                    stage_journal
                        .append(
                            ChainEventFactory::data_event(writer_id, "data", serde_json::json!({})),
                            Default::default(),
                        )
                        .await
                        .expect("append chained data");
                });
            }
        });

        let err = probe
            .expect_no_event_during(&clock, window)
            .await
            .expect_err("chained wakeup event should be observed");
        assert!(
            matches!(err, JournalProbeError::UnexpectedEvent { .. }),
            "unexpected error: {err:?}"
        );
    }

    #[tokio::test]
    async fn expect_event_returns_append_order_under_concurrent_writes() {
        let mut topology_builder = TopologyBuilder::new();
        let stage_topo_id = topology_builder.add_stage(Some("stage".to_string()));
        topology_builder.add_stage(Some("sink".to_string()));
        let topology = Arc::new(topology_builder.build_unchecked().expect("topology"));

        let stage_id = StageId::from_topology_id(stage_topo_id);
        let writer_id = WriterId::from(stage_id);

        let stage_journal_impl: Arc<MemoryJournal<ChainEvent>> = Arc::new(MemoryJournal::default());
        let stage_journal: Arc<dyn Journal<ChainEvent>> = stage_journal_impl.clone();

        let harness =
            harness_with_stage_journal("stage", stage_id, stage_journal.clone(), topology);
        let probe = JournalProbe::try_on_stage(&harness, "stage").expect("probe");

        let barrier = Arc::new(tokio::sync::Barrier::new(3));
        for payload in ["a", "b"] {
            let stage_journal = stage_journal.clone();
            let barrier = barrier.clone();
            tokio::spawn(async move {
                barrier.wait().await;
                stage_journal
                    .append(
                        ChainEventFactory::data_event(
                            writer_id,
                            "data",
                            serde_json::json!({"payload": payload}),
                        ),
                        Default::default(),
                    )
                    .await
                    .expect("append");
            });
        }

        // Release both writers.
        barrier.wait().await;

        let first = probe.expect_event(1).await.expect("first");
        let second = probe.expect_event(2).await.expect("second");
        assert_ne!(
            first.envelope().envelope.provenance.event.id,
            second.envelope().envelope.provenance.event.id
        );
    }
}