obzenflow_runtime 0.2.5

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
// SPDX-License-Identifier: MIT OR Apache-2.0
// SPDX-FileCopyrightText: 2025-2026 ObzenFlow Contributors
// https://obzenflow.dev

//! The approved state/input contract, including invalid internal inputs and
//! journal observations that must remain valid across lifecycle boundaries.

use super::support::{
    empty_topology, make_fsm_context, source_sink_topology_with_source, test_context,
    TestPipelineStageHandle,
};
use crate::feed_plan::{FeedKey, FeedRole};
use crate::pipeline::fsm::{
    build_pipeline_fsm_with_initial, PipelineAction as A, PipelineDeadline, PipelineFsmEvent as E,
    PipelineFsmState as S,
};
use crate::pipeline::resources::{ProducerTail, StageCommand};
use crate::pipeline::termination::{ExecutionOutcome, PublishedTermination};
use crate::pipeline::tests::support::new_system_journal;
use crate::pipeline::FlowStopMode;
use obzenflow_core::event::context::StageType;
use obzenflow_core::event::payloads::system_payload::SystemFeedRole;
use obzenflow_core::event::types::SeqNo;
use obzenflow_core::event::{
    PipelineLifecycleEvent, SystemEvent, SystemEventFactory, SystemPayload,
};
use obzenflow_core::journal::factory::FlowJournalFactory;
use obzenflow_core::journal::AppendOptions;
use obzenflow_core::{StageId, SystemId};
use std::time::{Duration, Instant};

fn live_phases() -> [S; 13] {
    [
        S::Created,
        S::Materializing,
        S::AwaitingStageReadiness,
        S::ReadyForRun,
        S::StartingSources,
        S::Running,
        S::SourceCompleted,
        S::Draining,
        S::SettlingStages,
        S::CatchingUpProducers,
        S::PublishingTerminal,
        S::FinalisingMetrics,
        S::PublishingFinalMarker,
    ]
}

pub async fn controlled_journal_preserves_causality_groups_and_live_readers(
    make_journals: fn() -> Box<dyn FlowJournalFactory>,
) {
    use super::support::ControlledJournal;
    use crate::testing::assert_happens_before;
    use obzenflow_core::Journal;

    let mut journals = make_journals();
    let inner = new_system_journal(&mut *journals, SystemId::new());
    let journal = ControlledJournal::new(inner.clone());
    assert_eq!(journal.id(), inner.id());
    assert_eq!(journal.owner(), inner.owner());

    let parent = inner
        .append(
            SystemEvent::stage_running(StageId::new()),
            Default::default(),
        )
        .await
        .unwrap();
    let writer = StageId::new();
    let child = journal
        .append(
            SystemEvent::stage_running(writer),
            AppendOptions::new(Some(&parent)),
        )
        .await
        .unwrap();
    assert_happens_before(&parent, &child).unwrap();
    let group = journal
        .append_group(
            "fixture.causal-group",
            vec![
                SystemEvent::stage_running(writer),
                SystemEvent::stage_running(writer),
            ],
            AppendOptions::new(Some(&child)),
        )
        .await
        .unwrap();
    assert_eq!(group.len(), 2);
    assert_happens_before(&child, &group[0]).unwrap();
    assert_happens_before(&group[0], &group[1]).unwrap();
    for (index, row) in group.iter().enumerate() {
        assert_eq!(
            row.envelope.provenance.journal.journal_group_id.as_deref(),
            Some("fixture.causal-group")
        );
        let member = row
            .envelope
            .provenance
            .journal
            .journal_group_member
            .as_ref()
            .unwrap();
        assert_eq!(member.index as usize, index);
        assert_eq!(member.size, 2);
    }
    let found = journal
        .read_event(&child.envelope.provenance.event.id)
        .await
        .unwrap()
        .unwrap();
    assert_eq!(
        found.envelope.provenance.journal.vector_clock,
        child.envelope.provenance.journal.vector_clock
    );
    let stored = inner.read_all_unordered().await.unwrap();
    let observed = journal.read_causally_ordered().await.unwrap();
    assert_eq!(
        stored
            .iter()
            .map(|row| row.envelope.provenance.event.id)
            .collect::<Vec<_>>(),
        observed
            .iter()
            .map(|row| row.envelope.provenance.event.id)
            .collect::<Vec<_>>()
    );

    let mut reader = journal.reader_from(1).await.unwrap();
    assert_eq!(reader.position(), 1);
    for expected in [&child, &group[0], &group[1]] {
        assert_eq!(
            reader
                .next()
                .await
                .unwrap()
                .unwrap()
                .envelope
                .provenance
                .event
                .id,
            expected.envelope.provenance.event.id
        );
    }
    assert!(reader.next().await.unwrap().is_none());
    assert!(reader.is_at_end());
    let later = inner
        .append(
            SystemEvent::stage_running(writer),
            AppendOptions::new(Some(&group[1])),
        )
        .await
        .unwrap();
    assert_eq!(
        reader
            .next()
            .await
            .unwrap()
            .unwrap()
            .envelope
            .provenance
            .event
            .id,
        later.envelope.provenance.event.id
    );
    assert_eq!(reader.position(), 5);
    let tail = journal.read_last_n(2).await.unwrap();
    assert_eq!(
        tail[0].envelope.provenance.event.id,
        later.envelope.provenance.event.id
    );
    assert_eq!(
        tail[1].envelope.provenance.event.id,
        group[1].envelope.provenance.event.id
    );
}

pub async fn internal_input_admission_is_phase_specific_even_with_satisfied_guards(
    make_journals: fn() -> Box<dyn FlowJournalFactory>,
) {
    for phase in live_phases() {
        for event in [
            E::Bootstrap,
            E::PhysicalSettlementSatisfied,
            E::GracefulStopExpired,
            E::StageCleanupExpired,
        ] {
            let mut ctx = make_fsm_context(make_journals);
            ctx.stop_intent.apply_request(
                FlowStopMode::Graceful {
                    timeout: Duration::ZERO,
                },
                None,
            );
            ctx.progress.cleanup_deadline = Some(Instant::now());
            ctx.resources.stages_joined = true;
            ctx.resources.producer_tail = ProducerTail::Reached;
            ctx.resources.metrics_joined = true;
            ctx.progress.metrics_cancelled = true;
            ctx.progress.final_marker_seen = true;
            let permitted = match event {
                E::Bootstrap => matches!(phase, S::Created),
                E::PhysicalSettlementSatisfied => matches!(
                    phase,
                    S::Materializing
                        | S::SettlingStages
                        | S::CatchingUpProducers
                        | S::FinalisingMetrics
                        | S::PublishingFinalMarker
                ),
                E::GracefulStopExpired => matches!(
                    phase,
                    S::Draining
                        | S::SettlingStages
                        | S::CatchingUpProducers
                        | S::PublishingTerminal
                        | S::FinalisingMetrics
                ),
                E::StageCleanupExpired => matches!(phase, S::SettlingStages),
                _ => unreachable!(),
            };
            let mut machine = build_pipeline_fsm_with_initial(phase.clone());
            let result = machine.handle(event.clone(), &mut ctx).await;
            assert_eq!(
                result.is_ok(),
                permitted,
                "{phase:?} / {event:?}: {result:?}"
            );
            if !permitted {
                assert_eq!(machine.state(), &phase);
            }
        }
    }
}

pub async fn settlement_in_an_eligible_phase_still_requires_its_resource_predicate(
    make_journals: fn() -> Box<dyn FlowJournalFactory>,
) {
    for phase in [
        S::Materializing,
        S::SettlingStages,
        S::CatchingUpProducers,
        S::FinalisingMetrics,
        S::PublishingFinalMarker,
    ] {
        let mut ctx = make_fsm_context(make_journals);
        if matches!(phase, S::Materializing) {
            ctx.resources
                .delivery
                .enqueue(
                    vec![TestPipelineStageHandle::boxed(
                        StageId::new(),
                        "pending",
                        StageType::Sink,
                    )],
                    &[StageCommand::Initialize],
                    1,
                )
                .unwrap();
        }
        let mut machine = build_pipeline_fsm_with_initial(phase.clone());
        assert!(machine
            .handle(E::PhysicalSettlementSatisfied, &mut ctx)
            .await
            .is_err());
        assert_eq!(machine.state(), &phase);
    }
}

pub async fn execution_deadlines_require_admission_and_expiry_without_mutating_on_rejection(
    make_journals: fn() -> Box<dyn FlowJournalFactory>,
) {
    for future in [false, true] {
        let mut ctx = make_fsm_context(make_journals);
        if future {
            ctx.stop_intent.apply_request(
                FlowStopMode::Graceful {
                    timeout: Duration::from_secs(60),
                },
                None,
            );
            ctx.progress.cleanup_deadline = Some(Instant::now() + Duration::from_secs(60));
        }
        let stop_deadline = ctx.stop_intent.deadline;
        let cleanup_deadline = ctx.progress.cleanup_deadline;
        let mut machine = build_pipeline_fsm_with_initial(S::SettlingStages);
        for event in [E::GracefulStopExpired, E::StageCleanupExpired] {
            assert!(machine.handle(event, &mut ctx).await.is_err());
            assert_eq!(ctx.stop_intent.deadline, stop_deadline);
            assert_eq!(ctx.progress.cleanup_deadline, cleanup_deadline);
            assert!(!ctx.progress.stages_cancelled);
        }
    }
}

pub async fn metrics_deadline_admission_and_guards_use_the_actual_acknowledgement(
    make_journals: fn() -> Box<dyn FlowJournalFactory>,
) {
    use crate::metrics::{MetricsAggregatorEvent, MetricsAggregatorState};
    use crate::supervised_base::{ChannelBuilder, HandleBuilder};
    let mut ctx = make_fsm_context(make_journals);
    for phase in [S::PublishingTerminal, S::FinalisingMetrics] {
        let mut machine = build_pipeline_fsm_with_initial(phase);
        assert!(machine.handle(E::MetricsExpired, &mut ctx).await.is_err());
    }
    let (sender, _receiver, watcher) =
        ChannelBuilder::<MetricsAggregatorEvent, MetricsAggregatorState>::new()
            .build(MetricsAggregatorState::Running);
    let task = crate::supervised_base::SupervisorTaskBuilder::<()>::new("test_metrics")
        .spawn_for_test(
            std::future::pending::<Result<(), Box<dyn std::error::Error + Send + Sync>>>,
        );
    let handle = HandleBuilder::new()
        .with_event_sender(sender)
        .with_state_watcher(watcher)
        .with_supervisor_task(task)
        .build_standard()
        .unwrap();
    ctx.resources.metrics.install_for_test(handle);
    let ack = Instant::now();
    ctx.resources.terminal_ack.set(ack).unwrap();
    ctx.metrics_drain_timeout_ms = 60_000;
    for phase in [S::PublishingTerminal, S::FinalisingMetrics] {
        let mut machine = build_pipeline_fsm_with_initial(phase);
        assert!(machine.handle(E::MetricsExpired, &mut ctx).await.is_err());
    }
    ctx.metrics_drain_timeout_ms = 0;
    for phase in live_phases() {
        let permitted = matches!(phase, S::PublishingTerminal | S::FinalisingMetrics);
        assert_eq!(
            phase.deadline_at(&ctx, PipelineDeadline::Metrics).is_some(),
            permitted
        );
        let mut machine = build_pipeline_fsm_with_initial(phase.clone());
        let result = machine.handle(E::MetricsExpired, &mut ctx).await;
        assert_eq!(result.is_ok(), permitted, "{phase:?}");
        if permitted {
            assert!(matches!(
                result.unwrap().as_slice(),
                [A::CancelMetrics, A::ObserveMetrics]
            ));
        }
        assert_eq!(ctx.resources.terminal_ack.get(), Some(&ack));
    }
    ctx.resources.metrics.abort_and_join().await.unwrap();
}

pub async fn finished_has_no_outgoing_inputs_including_controls_and_journal_rows(
    make_journals: fn() -> Box<dyn FlowJournalFactory>,
) {
    let mut ctx = make_fsm_context(make_journals);
    let mut machine = build_pipeline_fsm_with_initial(S::PublishingFinalMarker);
    ctx.progress.final_marker_seen = true;
    machine
        .handle(E::PhysicalSettlementSatisfied, &mut ctx)
        .await
        .unwrap();
    let finished = machine.state().clone();
    assert!(matches!(finished, S::Finished { .. }));
    let row = ctx
        .system_journal
        .append(
            SystemEvent::stage_running(StageId::new()),
            Default::default(),
        )
        .await
        .unwrap();
    for event in [
        E::Bootstrap,
        E::Start,
        E::GracefulStop {
            timeout: Duration::ZERO,
        },
        E::Cancel,
        E::Abort {
            reason: "late".into(),
        },
        E::GracefulStopExpired,
        E::StageCleanupExpired,
        E::MetricsExpired,
        E::PhysicalSettlementSatisfied,
        E::OperationalFailure {
            message: "late".into(),
        },
        E::Journal(Box::new(row)),
    ] {
        assert!(
            machine.handle(event.clone(), &mut ctx).await.is_err(),
            "{event:?}"
        );
        assert_eq!(machine.state(), &finished);
    }
}

pub async fn controls_and_operational_failure_follow_the_approved_successor_matrix(
    make_journals: fn() -> Box<dyn FlowJournalFactory>,
) {
    for phase in live_phases() {
        for event in [
            E::Start,
            E::GracefulStop {
                timeout: Duration::from_secs(60),
            },
            E::Cancel,
            E::Abort {
                reason: "abort".into(),
            },
            E::OperationalFailure {
                message: "failure".into(),
            },
        ] {
            let mut ctx = make_fsm_context(make_journals);
            if !matches!(
                phase,
                S::Created | S::Materializing | S::AwaitingStageReadiness | S::ReadyForRun
            ) {
                ctx.flow_start_time = Some(Instant::now());
            }
            let expected = match event {
                E::Start if phase == S::ReadyForRun => S::StartingSources,
                E::Start => phase.clone(),
                E::OperationalFailure { .. } | E::Abort { .. } => match phase {
                    S::PublishingFinalMarker if matches!(event, E::Abort { .. }) => phase.clone(),
                    S::CatchingUpProducers => S::CatchingUpProducers,
                    S::PublishingTerminal | S::FinalisingMetrics | S::PublishingFinalMarker => {
                        S::FinalisingMetrics
                    }
                    _ => S::SettlingStages,
                },
                _ if matches!(
                    phase,
                    S::SettlingStages
                        | S::CatchingUpProducers
                        | S::PublishingTerminal
                        | S::FinalisingMetrics
                        | S::PublishingFinalMarker
                ) =>
                {
                    phase.clone()
                }
                E::GracefulStop { .. } if ctx.flow_start_time.is_some() => S::Draining,
                _ => S::SettlingStages,
            };
            let mut machine = build_pipeline_fsm_with_initial(phase.clone());
            machine.handle(event.clone(), &mut ctx).await.unwrap();
            assert_eq!(machine.state(), &expected, "{phase:?} / {event:?}");
        }
    }
}

fn contract_row(
    upstream: StageId,
    reader: StageId,
    selected: Option<&str>,
    role: Option<SystemFeedRole>,
    pass: bool,
) -> SystemEvent {
    SystemEvent::new(
        reader.into(),
        SystemPayload::ContractStatus {
            upstream,
            reader,
            selected_event_type: selected.map(Into::into),
            feed_role: role,
            pass,
            reader_seq: Some(SeqNo(1)),
            advertised_writer_seq: Some(SeqNo(1)),
            reason: None,
        },
    )
}

pub async fn unrelated_rows_only_advance_observation_in_every_live_phase(
    make_journals: fn() -> Box<dyn FlowJournalFactory>,
) {
    for phase in live_phases() {
        let mut ctx = make_fsm_context(make_journals);
        let upstream = StageId::new();
        let reader = StageId::new();
        let mut machine = build_pipeline_fsm_with_initial(phase.clone());
        for event in [
            SystemEvent::stage_running(upstream),
            SystemEvent::stage_completed(upstream),
            SystemEvent::stage_failed(upstream, "unrelated".into(), false),
            contract_row(upstream, reader, None, None, false),
            SystemEventFactory::new(SystemId::new()).pipeline_all_stages_completed(),
        ] {
            let id = event.id;
            ctx.resources.producer_tail = ProducerTail::Through(id);
            let row = ctx
                .system_journal
                .append(event, Default::default())
                .await
                .unwrap();
            assert!(machine
                .handle(E::Journal(Box::new(row)), &mut ctx)
                .await
                .unwrap()
                .is_empty());
            assert_eq!(machine.state(), &phase);
            assert_eq!(ctx.last_system_event_id_seen, Some(id));
            assert!(matches!(ctx.resources.producer_tail, ProducerTail::Reached));
            assert!(ctx.running_stages.is_empty() && ctx.completed_stages.is_empty());
            assert!(ctx.contract_pairs.is_empty() && ctx.termination.failure.is_none());
        }
    }
}

pub async fn declared_contract_feeds_do_not_fall_back_on_unknown_payload_or_role(
    make_journals: fn() -> Box<dyn FlowJournalFactory>,
) {
    let system_id = SystemId::new();
    let mut journals = make_journals();
    let journal = new_system_journal(&mut *journals, system_id);
    let (topology, upstream, reader) = source_sink_topology_with_source();
    let mut ctx = test_context(topology, system_id, journal, None);
    let feed = FeedKey::new(upstream, reader, "payment", FeedRole::Input);
    ctx.expected_contract_pairs.insert(feed.clone());
    let mut machine = build_pipeline_fsm_with_initial(S::Running);
    for row in [
        contract_row(
            upstream,
            reader,
            Some("unrelated"),
            Some(SystemFeedRole::Input),
            false,
        ),
        contract_row(
            upstream,
            reader,
            Some("payment"),
            Some(SystemFeedRole::Reference),
            false,
        ),
        contract_row(upstream, reader, None, Some(SystemFeedRole::Stream), false),
        contract_row(reader, upstream, None, None, false),
    ] {
        let row = ctx
            .system_journal
            .append(row, Default::default())
            .await
            .unwrap();
        assert!(machine
            .handle(E::Journal(Box::new(row)), &mut ctx)
            .await
            .unwrap()
            .is_empty());
        assert!(ctx.contract_pairs.is_empty());
        assert!(ctx.termination.failure.is_none());
        assert_eq!(machine.state(), &S::Running);
    }
    let row = ctx
        .system_journal
        .append(
            contract_row(
                upstream,
                reader,
                Some("payment"),
                Some(SystemFeedRole::Input),
                false,
            ),
            Default::default(),
        )
        .await
        .unwrap();
    let actions = machine
        .handle(E::Journal(Box::new(row)), &mut ctx)
        .await
        .unwrap();
    assert_eq!(machine.state(), &S::SettlingStages);
    assert!(ctx.contract_pairs.contains_key(&feed));
    assert!(ctx.termination.failure.is_some());
    assert!(matches!(
        actions.as_slice(),
        [
            A::CancelStages {
                contract_abort: true
            },
            A::ObserveStages
        ]
    ));
}

pub async fn empty_topology_cannot_announce_or_consume_all_stage_completion(
    make_journals: fn() -> Box<dyn FlowJournalFactory>,
) {
    for phase in [
        S::Created,
        S::Materializing,
        S::AwaitingStageReadiness,
        S::Running,
    ] {
        let id = SystemId::new();
        let mut journals = make_journals();
        let journal = new_system_journal(&mut *journals, id);
        let mut ctx = test_context(empty_topology(), id, journal, None);
        let mut machine = build_pipeline_fsm_with_initial(phase.clone());
        for event in [
            SystemEvent::stage_running(StageId::new()),
            SystemEventFactory::new(id).pipeline_all_stages_completed(),
        ] {
            let row = ctx
                .system_journal
                .append(event, Default::default())
                .await
                .unwrap();
            assert!(machine
                .handle(E::Journal(Box::new(row)), &mut ctx)
                .await
                .unwrap()
                .is_empty());
            assert_eq!(machine.state(), &phase);
            assert!(!ctx.progress.all_stages_announced);
        }
    }
}

pub async fn genuine_early_stage_completion_can_settle_without_start_admission(
    make_journals: fn() -> Box<dyn FlowJournalFactory>,
) {
    for phase in [S::Materializing, S::AwaitingStageReadiness, S::ReadyForRun] {
        let id = SystemId::new();
        let mut journals = make_journals();
        let journal = new_system_journal(&mut *journals, id);
        let (topology, upstream, stage) = source_sink_topology_with_source();
        let mut ctx = test_context(topology, id, journal, None);
        ctx.stage_supervisors.insert(
            stage,
            TestPipelineStageHandle::boxed(stage, "sink", StageType::Sink),
        );
        ctx.stage_supervisors.insert(
            upstream,
            TestPipelineStageHandle::boxed(upstream, "completed upstream", StageType::Transform),
        );
        let mut machine = build_pipeline_fsm_with_initial(phase.clone());
        let upstream_row = ctx
            .system_journal
            .append(SystemEvent::stage_completed(upstream), Default::default())
            .await
            .unwrap();
        assert!(machine
            .handle(E::Journal(Box::new(upstream_row)), &mut ctx)
            .await
            .unwrap()
            .is_empty());
        let row = ctx
            .system_journal
            .append(SystemEvent::stage_completed(stage), Default::default())
            .await
            .unwrap();
        let actions = machine
            .handle(E::Journal(Box::new(row)), &mut ctx)
            .await
            .unwrap();
        let [A::Publish { event, .. }] = actions.as_slice() else {
            panic!("one completion announcement");
        };
        assert!(matches!(
            event.payload,
            SystemPayload::PipelineLifecycle(PipelineLifecycleEvent::AllStagesCompleted { .. })
        ));
        assert_eq!(machine.state(), &phase);
        assert!(ctx.flow_start_time.is_none());
        let row = ctx
            .system_journal
            .append(event.as_ref().clone(), Default::default())
            .await
            .unwrap();
        let actions = machine
            .handle(E::Journal(Box::new(row)), &mut ctx)
            .await
            .unwrap();
        assert_eq!(machine.state(), &S::SettlingStages);
        assert!(matches!(
            actions.as_slice(),
            [A::ObserveStages, A::DrainMetrics]
        ));
    }
}

pub async fn graceful_stop_during_startup_preserves_running_then_drain_authority(
    make_journals: fn() -> Box<dyn FlowJournalFactory>,
) {
    for cancel in [false, true] {
        let mut ctx = make_fsm_context(make_journals);
        let mut machine = build_pipeline_fsm_with_initial(S::ReadyForRun);
        let start = machine.handle(E::Start, &mut ctx).await.unwrap();
        let A::Publish { event: running, .. } = &start[1] else {
            panic!("running publication");
        };
        let stop = machine
            .handle(
                if cancel {
                    E::Cancel
                } else {
                    E::GracefulStop {
                        timeout: Duration::from_secs(60),
                    }
                },
                &mut ctx,
            )
            .await
            .unwrap();
        let admission = stop
            .iter()
            .find_map(|action| match action {
                A::Publish {
                    event,
                    control: true,
                } => Some(event.as_ref().clone()),
                _ => None,
            })
            .unwrap();
        assert_eq!(
            machine.state(),
            if cancel {
                &S::SettlingStages
            } else {
                &S::Draining
            }
        );
        let row = ctx
            .system_journal
            .append(running.as_ref().clone(), Default::default())
            .await
            .unwrap();
        let actions = machine
            .handle(E::Journal(Box::new(row)), &mut ctx)
            .await
            .unwrap();
        assert_eq!(
            actions.iter().any(|a| matches!(a, A::StartSources)),
            !cancel
        );
        let row = ctx
            .system_journal
            .append(admission, Default::default())
            .await
            .unwrap();
        let actions = machine
            .handle(E::Journal(Box::new(row)), &mut ctx)
            .await
            .unwrap();
        assert_eq!(actions.iter().any(|a| matches!(a, A::StopSources)), !cancel);
    }
}

pub async fn terminal_and_final_marker_require_the_authorised_writer_and_identity(
    make_journals: fn() -> Box<dyn FlowJournalFactory>,
) {
    let mut ctx = make_fsm_context(make_journals);
    let selected = SystemEventFactory::new(ctx.system_id).pipeline_not_started();
    ctx.progress.selected_terminal = Some((selected.clone(), ExecutionOutcome::NotStarted));
    let mut machine = build_pipeline_fsm_with_initial(S::PublishingTerminal);
    let mut wrong_writer = selected.clone();
    wrong_writer.writer_id = SystemId::new().into();
    for event in [
        SystemEventFactory::new(ctx.system_id).pipeline_not_started(),
        wrong_writer,
    ] {
        let row = ctx
            .system_journal
            .append(event, Default::default())
            .await
            .unwrap();
        assert!(machine
            .handle(E::Journal(Box::new(row)), &mut ctx)
            .await
            .unwrap()
            .is_empty());
        assert_eq!(machine.state(), &S::PublishingTerminal);
    }
    let row = ctx
        .system_journal
        .append(selected, Default::default())
        .await
        .unwrap();
    assert!(matches!(
        machine
            .handle(E::Journal(Box::new(row)), &mut ctx)
            .await
            .unwrap()
            .as_slice(),
        [A::ObserveMetrics]
    ));
    assert_eq!(machine.state(), &S::FinalisingMetrics);
    let marker = SystemEventFactory::new(ctx.system_id).pipeline_drained();
    ctx.progress.final_marker = Some(marker.id);
    let mut machine = build_pipeline_fsm_with_initial(S::PublishingFinalMarker);
    let row = ctx
        .system_journal
        .append(
            SystemEventFactory::new(ctx.system_id).pipeline_drained(),
            Default::default(),
        )
        .await
        .unwrap();
    machine
        .handle(E::Journal(Box::new(row)), &mut ctx)
        .await
        .unwrap();
    assert!(!ctx.progress.final_marker_seen);
    assert!(machine
        .handle(E::PhysicalSettlementSatisfied, &mut ctx)
        .await
        .is_err());
    let row = ctx
        .system_journal
        .append(marker, Default::default())
        .await
        .unwrap();
    machine
        .handle(E::Journal(Box::new(row)), &mut ctx)
        .await
        .unwrap();
    machine
        .handle(E::PhysicalSettlementSatisfied, &mut ctx)
        .await
        .unwrap();
    assert!(matches!(
        machine.state(),
        S::Finished {
            outcome: ExecutionOutcome::NotStarted
        }
    ));
}

pub async fn final_marker_failure_finishes_with_retained_error_without_another_marker(
    make_journals: fn() -> Box<dyn FlowJournalFactory>,
) {
    let mut ctx = make_fsm_context(make_journals);
    let selected = SystemEventFactory::new(ctx.system_id).pipeline_not_started();
    ctx.progress.selected_terminal = Some((selected.clone(), ExecutionOutcome::NotStarted));
    ctx.termination
        .published
        .set(PublishedTermination {
            outcome: ExecutionOutcome::NotStarted,
            event_id: Some(selected.id),
        })
        .unwrap();
    ctx.resources.metrics_joined = true;
    ctx.resources.stages_joined = true;
    ctx.resources
        .retain_failure(Box::new(std::io::Error::other("marker append failed")));
    let mut machine = build_pipeline_fsm_with_initial(S::PublishingFinalMarker);
    machine
        .handle(
            E::OperationalFailure {
                message: "marker append failed".into(),
            },
            &mut ctx,
        )
        .await
        .unwrap();
    assert_eq!(machine.state(), &S::FinalisingMetrics);
    let actions = machine
        .handle(E::PhysicalSettlementSatisfied, &mut ctx)
        .await
        .unwrap();
    assert!(actions.is_empty());
    assert!(matches!(
        machine.state(),
        S::Finished {
            outcome: ExecutionOutcome::NotStarted
        }
    ));
    assert_eq!(
        ctx.progress.selected_terminal.as_ref().unwrap().0.id,
        selected.id
    );
    assert!(ctx.resources.failure.get().is_some());
}