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

//! FSM admission rules, state projection, stop intent and contract matching.

use crate::feed_plan::{FeedKey, FeedRole};
#[cfg(test)]
use crate::pipeline::fsm::context::{record_stage_completion, StopIntent, StopRequestOutcome};
use crate::pipeline::fsm::{
    build_pipeline_fsm_with_initial, PipelineAction, PipelineFsmEvent, PipelineFsmState,
};
use crate::pipeline::tests::support::new_system_journal;
use crate::pipeline::tests::support::{
    make_fsm_context, source_sink_topology_with_source, test_context,
};
use crate::pipeline::{FlowStopMode, PipelineControl, PipelineState};
#[cfg(test)]
use crate::stages::common::stage_handle::{STOP_REASON_TIMEOUT, STOP_REASON_USER_STOP};
use obzenflow_core::event::SystemEventFactory;
use obzenflow_core::journal::factory::FlowJournalFactory;
#[cfg(test)]
use obzenflow_core::StageId;
use obzenflow_core::SystemId;
#[cfg(test)]
use std::time::Duration;

#[test]
fn terminal_states_are_exactly_the_locked_set() {
    assert!(PipelineState::Drained.is_terminal());
    assert!(PipelineState::Failed {
        reason: "x".to_string(),
        failure_cause: None
    }
    .is_terminal());
    assert!(!PipelineState::Created.is_terminal());
    assert!(!PipelineState::Running.is_terminal());
    assert!(!PipelineState::Draining.is_terminal());
    assert!(!PipelineState::SourceCompleted.is_terminal());
}

#[test]
fn stop_intent_cancel_sets_defaults() {
    let mut intent = StopIntent::default();
    let outcome = intent.apply_request(FlowStopMode::Cancel, None);

    assert!(intent.requested);
    assert!(matches!(intent.mode, Some(FlowStopMode::Cancel)));
    assert_eq!(intent.reason.as_deref(), Some(STOP_REASON_USER_STOP));
    assert!(intent.deadline.is_none());

    match outcome {
        StopRequestOutcome::Applied { reason_label, .. } => {
            assert_eq!(reason_label, STOP_REASON_USER_STOP);
        }
        StopRequestOutcome::Ignored => {
            panic!("cancel request should never be ignored");
        }
    }
}

#[test]
fn stop_intent_graceful_sets_deadline() {
    let mut intent = StopIntent::default();
    let timeout = Duration::from_secs(3);
    let before = std::time::Instant::now();

    let _ = intent.apply_request(FlowStopMode::Graceful { timeout }, None);
    let after = std::time::Instant::now();

    assert!(intent.requested);
    assert!(matches!(
        intent.mode,
        Some(FlowStopMode::Graceful { timeout: t }) if t == timeout
    ));
    assert_eq!(intent.reason.as_deref(), Some(STOP_REASON_USER_STOP));

    let deadline = intent
        .deadline
        .expect("graceful stop should set a deadline");
    assert!(deadline >= before + timeout);
    assert!(deadline <= after + timeout);
}

#[test]
fn stop_intent_cancel_overrides_graceful() {
    let mut intent = StopIntent::default();
    let _ = intent.apply_request(
        FlowStopMode::Graceful {
            timeout: Duration::from_secs(5),
        },
        None,
    );
    assert!(intent.deadline.is_some());

    let _ = intent.apply_request(FlowStopMode::Cancel, None);
    assert!(matches!(intent.mode, Some(FlowStopMode::Cancel)));
    assert!(intent.deadline.is_none());
}

#[test]
fn stop_intent_graceful_is_ignored_after_cancel() {
    let mut intent = StopIntent::default();
    let _ = intent.apply_request(FlowStopMode::Cancel, None);
    let outcome = intent.apply_request(
        FlowStopMode::Graceful {
            timeout: Duration::from_secs(1),
        },
        None,
    );

    assert!(matches!(outcome, StopRequestOutcome::Ignored));
    assert!(matches!(intent.mode, Some(FlowStopMode::Cancel)));
    assert!(intent.deadline.is_none());
}

#[test]
fn stop_intent_expired_timeout_preserves_deadline() {
    let mut intent = StopIntent::default();
    let _ = intent.apply_request(
        FlowStopMode::Graceful {
            timeout: Duration::ZERO,
        },
        Some("first_reason".to_string()),
    );
    assert_eq!(intent.reason.as_deref(), Some("first_reason"));
    let original_deadline = intent.deadline;

    let _ = intent.apply_request(FlowStopMode::Cancel, Some(STOP_REASON_TIMEOUT.to_string()));
    assert_eq!(intent.reason.as_deref(), Some(STOP_REASON_TIMEOUT));
    assert_eq!(
        intent.deadline, original_deadline,
        "timeout escalation must preserve the graceful-stop deadline"
    );
}

#[test]
fn first_graceful_deadline_wins_in_both_duration_orders() {
    for (first, second) in [(1, 60), (60, 1)] {
        let mut intent = StopIntent::default();
        intent.apply_request(
            FlowStopMode::Graceful {
                timeout: Duration::from_secs(first),
            },
            Some("first".into()),
        );
        let first_deadline = intent.deadline;
        assert!(matches!(
            intent.apply_request(
                FlowStopMode::Graceful {
                    timeout: Duration::from_secs(second)
                },
                Some("second".into()),
            ),
            StopRequestOutcome::Ignored
        ));
        assert_eq!(intent.deadline, first_deadline);
        assert_eq!(intent.reason.as_deref(), Some("first"));
    }
}

#[test]
fn cancel_is_absorbing_including_reason_and_admission_time() {
    let mut intent = StopIntent::default();
    intent.apply_request(FlowStopMode::Cancel, Some("explicit_cancel".into()));
    for (mode, reason) in [
        (FlowStopMode::Cancel, "duplicate"),
        (FlowStopMode::Cancel, STOP_REASON_TIMEOUT),
        (
            FlowStopMode::Graceful {
                timeout: Duration::ZERO,
            },
            "late_grace",
        ),
    ] {
        assert!(matches!(
            intent.apply_request(mode, Some(reason.into())),
            StopRequestOutcome::Ignored
        ));
        assert_eq!(intent.reason.as_deref(), Some("explicit_cancel"));
    }
}

#[test]
fn timeout_cancel_requires_an_expired_graceful_stop() {
    let mut intent = StopIntent::default();
    assert!(matches!(
        intent.apply_request(FlowStopMode::Cancel, Some(STOP_REASON_TIMEOUT.into())),
        StopRequestOutcome::Ignored
    ));
    assert!(!intent.requested);
    intent.apply_request(
        FlowStopMode::Graceful {
            timeout: Duration::from_secs(60),
        },
        None,
    );
    let deadline = intent.deadline;
    assert!(matches!(
        intent.apply_request(FlowStopMode::Cancel, Some(STOP_REASON_TIMEOUT.into())),
        StopRequestOutcome::Ignored
    ));
    assert_eq!(intent.deadline, deadline);
    assert!(matches!(intent.mode, Some(FlowStopMode::Graceful { .. })));
}

#[test]
fn record_stage_completion_is_idempotent_for_duplicate_terminal_events() {
    let stage_a = StageId::new();
    let stage_b = StageId::new();
    let mut completed = vec![stage_a];

    let (is_new, all_completed_now) = record_stage_completion(&mut completed, stage_b, 2);
    assert!(is_new);
    assert!(all_completed_now);
    assert_eq!(completed, vec![stage_a, stage_b]);

    let (is_new, all_completed_now) = record_stage_completion(&mut completed, stage_b, 2);
    assert!(!is_new);
    assert!(!all_completed_now);
    assert_eq!(completed, vec![stage_a, stage_b]);
}

pub fn contract_keys_for_stage_pair_returns_all_matching_logical_feeds(
    make_journals: fn() -> Box<dyn FlowJournalFactory>,
) {
    let system_id = SystemId::new();
    let mut journals = make_journals();
    let system_journal = new_system_journal(&mut *journals, system_id);
    let (topology, upstream, downstream) = source_sink_topology_with_source();
    let first_key = FeedKey::new(upstream, downstream, "test.first", FeedRole::Reference);
    let second_key = FeedKey::new(upstream, downstream, "test.second", FeedRole::Stream);
    let mut context = test_context(topology, system_id, system_journal, None);
    context.expected_contract_pairs.insert(first_key.clone());
    context.expected_contract_pairs.insert(second_key.clone());

    let keys = context.contract_keys_for_stage_pair(upstream, downstream);

    assert_eq!(keys.len(), 2);
    assert!(keys.contains(&first_key));
    assert!(keys.contains(&second_key));
}

pub fn contract_keys_for_contract_event_returns_matching_logical_feed(
    make_journals: fn() -> Box<dyn FlowJournalFactory>,
) {
    let system_id = SystemId::new();
    let mut journals = make_journals();
    let system_journal = new_system_journal(&mut *journals, system_id);
    let (topology, upstream, downstream) = source_sink_topology_with_source();
    let first_key = FeedKey::new(upstream, downstream, "test.first", FeedRole::Reference);
    let second_key = FeedKey::new(upstream, downstream, "test.second", FeedRole::Stream);
    let mut context = test_context(topology, system_id, system_journal, None);
    context.expected_contract_pairs.insert(first_key.clone());
    context.expected_contract_pairs.insert(second_key.clone());

    let keys = context.contract_keys_for_contract_event(
        upstream,
        downstream,
        Some("test.first"),
        Some("reference"),
    );

    assert_eq!(keys, vec![first_key]);
}

pub fn contract_keys_for_stage_pair_falls_back_for_legacy_stage_pair_status(
    make_journals: fn() -> Box<dyn FlowJournalFactory>,
) {
    let system_id = SystemId::new();
    let mut journals = make_journals();
    let system_journal = new_system_journal(&mut *journals, system_id);
    let (topology, upstream, downstream) = source_sink_topology_with_source();
    let context = test_context(topology, system_id, system_journal, None);

    assert_eq!(
        context.contract_keys_for_stage_pair(upstream, downstream),
        vec![FeedKey::legacy_stage_pair(upstream, downstream)]
    );
}

pub async fn repeated_graceful_controls_have_no_actions_and_cancel_folds_once(
    make_journals: fn() -> Box<dyn FlowJournalFactory>,
) {
    use std::time::Duration;
    for (first, second) in [(1, 60), (60, 1)] {
        let mut context = make_fsm_context(make_journals);
        context.flow_start_time = Some(std::time::Instant::now());
        let mut fsm = build_pipeline_fsm_with_initial(PipelineFsmState::Running);
        let event = |seconds| {
            PipelineFsmEvent::from(PipelineControl::Stop {
                mode: FlowStopMode::Graceful {
                    timeout: Duration::from_secs(seconds),
                },
            })
        };
        let initial = fsm.handle(event(first), &mut context).await.unwrap();
        assert!(matches!(
            initial.as_slice(),
            [PipelineAction::Publish { control: true, .. }]
        ));
        let deadline = context.stop_intent.deadline;
        for _ in 0..20 {
            assert!(fsm
                .handle(event(second), &mut context)
                .await
                .unwrap()
                .is_empty());
            assert_eq!(context.stop_intent.deadline, deadline);
        }
        let cancel = PipelineFsmEvent::from(PipelineControl::Stop {
            mode: FlowStopMode::Cancel,
        });
        let admitted = fsm.handle(cancel.clone(), &mut context).await.unwrap();
        assert!(matches!(
            admitted.first(),
            Some(PipelineAction::CancelStages { .. })
        ));
        assert_eq!(
            admitted
                .iter()
                .filter(|a| matches!(a, PipelineAction::Publish { control: true, .. }))
                .count(),
            1
        );
        assert!(fsm.handle(cancel, &mut context).await.unwrap().is_empty());
    }
}

pub async fn repeated_abort_controls_preserve_the_first_failure_without_new_work(
    make_journals: fn() -> Box<dyn FlowJournalFactory>,
) {
    let mut ctx = make_fsm_context(make_journals);
    let mut machine = build_pipeline_fsm_with_initial(PipelineFsmState::Running);
    let first = machine
        .handle(
            PipelineFsmEvent::from(PipelineControl::Abort {
                reason: "first failure".into(),
            }),
            &mut ctx,
        )
        .await
        .unwrap();
    assert!(!first.is_empty());
    for _ in 0..128 {
        assert!(machine
            .handle(
                PipelineFsmEvent::from(PipelineControl::Abort {
                    reason: "later request".into(),
                }),
                &mut ctx
            )
            .await
            .unwrap()
            .is_empty());
    }
    assert_eq!(
        ctx.termination.failure.as_ref().unwrap().reason,
        "Force abort: first failure"
    );
    assert!(matches!(machine.state(), PipelineFsmState::SettlingStages));
}

pub async fn readiness_and_start_consume_committed_pipeline_facts(
    make_journals: fn() -> Box<dyn FlowJournalFactory>,
) {
    let mut ctx = make_fsm_context(make_journals);
    let mut fsm = build_pipeline_fsm_with_initial(PipelineFsmState::AwaitingStageReadiness);
    assert!(fsm
        .handle(PipelineFsmEvent::PhysicalSettlementSatisfied, &mut ctx)
        .await
        .is_err());
    assert!(matches!(
        fsm.state(),
        PipelineFsmState::AwaitingStageReadiness
    ));
    ctx.progress.ready_announced = true;
    let event = SystemEventFactory::new(ctx.system_id).pipeline_ready_for_run(None);
    let envelope = ctx
        .system_journal
        .append(event, Default::default())
        .await
        .unwrap();
    fsm.handle(PipelineFsmEvent::Journal(Box::new(envelope)), &mut ctx)
        .await
        .unwrap();
    assert!(matches!(fsm.state(), PipelineFsmState::ReadyForRun));
    let actions = fsm.handle(PipelineFsmEvent::Start, &mut ctx).await.unwrap();
    assert!(matches!(fsm.state(), PipelineFsmState::StartingSources));
    assert!(actions
        .iter()
        .all(|action| matches!(action, PipelineAction::Publish { .. })));
    assert!(!ctx.progress.sources_authorised);
    let running = SystemEventFactory::new(ctx.system_id).pipeline_running();
    let envelope = ctx
        .system_journal
        .append(running, Default::default())
        .await
        .unwrap();
    let actions = fsm
        .handle(PipelineFsmEvent::Journal(Box::new(envelope)), &mut ctx)
        .await
        .unwrap();
    assert!(matches!(actions.as_slice(), [PipelineAction::StartSources]));
}

pub async fn pre_ready_and_duplicate_start_controls_do_not_authorise_sources(
    make_journals: fn() -> Box<dyn FlowJournalFactory>,
) {
    for initial in [
        PipelineFsmState::Created,
        PipelineFsmState::Materializing,
        PipelineFsmState::AwaitingStageReadiness,
        PipelineFsmState::StartingSources,
        PipelineFsmState::Running,
        PipelineFsmState::SourceCompleted,
        PipelineFsmState::Draining,
        PipelineFsmState::SettlingStages,
        PipelineFsmState::CatchingUpProducers,
        PipelineFsmState::PublishingTerminal,
        PipelineFsmState::FinalisingMetrics,
        PipelineFsmState::PublishingFinalMarker,
    ] {
        let mut ctx = make_fsm_context(make_journals);
        let mut fsm = build_pipeline_fsm_with_initial(initial.clone());
        assert!(fsm
            .handle(PipelineFsmEvent::Start, &mut ctx)
            .await
            .unwrap()
            .is_empty());
        assert_eq!(fsm.state(), &initial);
    }
}

pub async fn readiness_failure_and_cancel_stay_pending_until_settlement(
    make_journals: fn() -> Box<dyn FlowJournalFactory>,
) {
    for failure in [false, true] {
        let mut ctx = make_fsm_context(make_journals);
        let mut fsm = build_pipeline_fsm_with_initial(PipelineFsmState::ReadyForRun);
        let event = if failure {
            PipelineFsmEvent::OperationalFailure {
                message: "readiness fault".into(),
            }
        } else {
            PipelineFsmEvent::from(PipelineControl::Stop {
                mode: FlowStopMode::Cancel,
            })
        };
        let actions = fsm.handle(event, &mut ctx).await.unwrap();
        assert!(matches!(fsm.state(), PipelineFsmState::SettlingStages));
        assert_eq!(fsm.state().public_state(&ctx), PipelineState::Draining);
        assert!(actions
            .iter()
            .any(|action| matches!(action, PipelineAction::ObserveStages)));
        assert!(matches!(
            actions.first(),
            Some(PipelineAction::CancelStages { .. })
        ));
        assert_eq!(ctx.termination.failure.is_some(), failure);
    }
}

pub async fn every_private_phase_has_a_truthful_public_projection(
    make_journals: fn() -> Box<dyn FlowJournalFactory>,
) {
    use crate::pipeline::termination::{ExecutionFailure, ExecutionOutcome};
    let mut ctx = make_fsm_context(make_journals);
    let cases = [
        (PipelineFsmState::Created, PipelineState::Created),
        (
            PipelineFsmState::Materializing,
            PipelineState::Materializing,
        ),
        (
            PipelineFsmState::AwaitingStageReadiness,
            PipelineState::Materialized,
        ),
        (PipelineFsmState::ReadyForRun, PipelineState::ReadyForRun),
        (
            PipelineFsmState::StartingSources,
            PipelineState::ReadyForRun,
        ),
        (PipelineFsmState::Running, PipelineState::Running),
        (
            PipelineFsmState::SourceCompleted,
            PipelineState::SourceCompleted,
        ),
    ];
    for (state, projection) in cases {
        assert_eq!(state.public_state(&ctx), projection);
    }
    for state in [
        PipelineFsmState::Draining,
        PipelineFsmState::SettlingStages,
        PipelineFsmState::CatchingUpProducers,
        PipelineFsmState::PublishingTerminal,
        PipelineFsmState::FinalisingMetrics,
        PipelineFsmState::PublishingFinalMarker,
    ] {
        ctx.progress.abort_cause = None;
        assert_eq!(state.public_state(&ctx), PipelineState::Draining);
        ctx.progress.abort_cause = Some((
            obzenflow_core::event::types::ViolationCause::Other("contract".into()),
            None,
        ));
        assert!(matches!(
            state.public_state(&ctx),
            PipelineState::AbortRequested { .. }
        ));
        assert!(!state.public_state(&ctx).is_terminal());
    }
    for outcome in [
        ExecutionOutcome::Completed,
        ExecutionOutcome::Cancelled {
            reason: "user_stop".into(),
        },
        ExecutionOutcome::NotStarted,
    ] {
        assert_eq!(
            PipelineFsmState::Finished { outcome }.public_state(&ctx),
            PipelineState::Drained
        );
    }
    let failed = PipelineFsmState::Finished {
        outcome: ExecutionOutcome::Failed(ExecutionFailure {
            reason: "failed".into(),
            cause: None,
        }),
    };
    assert!(matches!(
        failed.public_state(&ctx),
        PipelineState::Failed { .. }
    ));
}