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

use super::*;
use crate::supervised_base::with_external_events::record_terminal_commands;
use obzenflow_core::event::{
    CommandDiscardDisposition, JournalRecord, JournalWriterId, SystemEvent, SystemPayload,
};
use obzenflow_core::journal::AppendOptions;
use obzenflow_core::journal::{journal_owner::JournalOwner, Journal, JournalError, JournalReader};
use obzenflow_core::{EventId, JournalId};
use std::sync::Mutex;
use tokio::sync::Notify;

#[derive(Default)]
pub(super) struct TestJournal {
    id: JournalId,
    records: Mutex<Vec<JournalRecord<SystemPayload>>>,
    attempts: AtomicUsize,
    first_append_gate: Option<(Arc<Notify>, Arc<Notify>)>,
    fail: bool,
}

#[async_trait::async_trait]
impl Journal<SystemEvent> for TestJournal {
    fn id(&self) -> &JournalId {
        &self.id
    }

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

    async fn append(
        &self,
        event: SystemEvent,
        mut options: AppendOptions<'_, SystemEvent>,
    ) -> Result<JournalRecord<SystemPayload>, JournalError> {
        let event = options.capture.prepare(0, event);
        let attempt = self.attempts.fetch_add(1, Ordering::SeqCst);
        if attempt == 0 {
            if let Some((entered, release)) = &self.first_append_gate {
                entered.notify_one();
                release.notified().await;
            }
        }
        if self.fail {
            return Err(JournalError::Full);
        }
        let record = JournalRecord::new(JournalWriterId::from(self.id), event);
        self.records.lock().unwrap().push(record.clone());
        Ok(record)
    }

    async fn read_all_unordered(&self) -> Result<Vec<JournalRecord<SystemPayload>>, JournalError> {
        Ok(self.records.lock().unwrap().clone())
    }

    async fn read_event(
        &self,
        id: &EventId,
    ) -> Result<Option<JournalRecord<SystemPayload>>, JournalError> {
        Ok(self
            .records
            .lock()
            .unwrap()
            .iter()
            .find(|record| record.id() == id)
            .cloned())
    }

    async fn reader_from(
        &self,
        _position: u64,
    ) -> Result<Box<dyn JournalReader<SystemEvent>>, JournalError> {
        unreachable!("terminal mailbox tests read committed records directly")
    }

    async fn read_last_n(
        &self,
        count: usize,
    ) -> Result<Vec<JournalRecord<SystemPayload>>, JournalError> {
        Ok(self
            .records
            .lock()
            .unwrap()
            .iter()
            .rev()
            .take(count)
            .cloned()
            .collect())
    }
}

#[tokio::test]
async fn terminal_mailbox_records_each_command_once_and_rejects_later_sends() {
    for state in [
        ExternalEventTestState::Drained,
        ExternalEventTestState::Failed("first failure".into()),
    ] {
        let journal = Arc::new(TestJournal::default());
        let (sender, receiver, watcher) = ChannelBuilder::new()
            .with_event_buffer(3)
            .build(state.clone());
        sender
            .send(ExternalEventTestEvent::Initialize)
            .await
            .unwrap();
        sender
            .send(ExternalEventTestEvent::Error("late failure".into()))
            .await
            .unwrap();
        sender
            .send(ExternalEventTestEvent::Error(
                crate::stages::common::stage_handle::STOP_REASON_USER_STOP.into(),
            ))
            .await
            .unwrap();

        let mut pending_send =
            tokio_test::task::spawn(sender.send(ExternalEventTestEvent::Initialize));
        tokio_test::assert_pending!(pending_send.poll());
        let stage_id = StageId::new();
        let inner = ExternalEventTestHandlerSupervisor {
            name: "terminal-worker".into(),
            dispatch_calls: Arc::new(AtomicUsize::new(0)),
            stage_id,
        };
        let mut supervisor =
            HandlerSupervisedWithExternalEvents::new(inner, receiver, watcher, journal.clone());
        let mut context = ExternalEventTestContext;
        supervisor
            .dispatch_state(&state, &mut context)
            .await
            .unwrap();
        assert!(tokio_test::assert_ready!(pending_send.poll()).is_err());
        assert!(sender
            .send(ExternalEventTestEvent::Initialize)
            .await
            .is_err());
        supervisor
            .dispatch_state(&state, &mut context)
            .await
            .unwrap();

        let records = journal.read_all_unordered().await.unwrap();
        assert_eq!(records.len(), 3);
        let expected = [
            (
                "Initialize",
                CommandDiscardDisposition::ObsoleteControl,
                None,
            ),
            (
                "Error",
                CommandDiscardDisposition::UnexpectedError,
                Some("late failure"),
            ),
            (
                "Error",
                CommandDiscardDisposition::ObsoleteControl,
                Some("user_stop"),
            ),
        ];
        for (record, (name, expected_disposition, expected_error)) in records.iter().zip(expected) {
            assert_eq!(*record.writer_id(), WriterId::from(stage_id));
            assert_eq!(
                record.envelope.provenance.event.event_type,
                "system.supervisor.command_discarded"
            );
            let SystemPayload::SupervisorCommandDiscarded {
                supervisor,
                terminal_state,
                command,
                disposition,
                error,
            } = &record.payload
            else {
                panic!("expected a command disposition fact");
            };
            assert_eq!(supervisor, "terminal-worker");
            assert_eq!(terminal_state, state.variant_name());
            assert_eq!(command, name);
            assert_eq!(*disposition, expected_disposition);
            assert_eq!(error.as_deref(), expected_error);
        }
    }
}

#[tokio::test]
async fn terminal_mailbox_retains_the_whole_queue_when_recording_waiter_is_cancelled() {
    let entered = Arc::new(Notify::new());
    let release = Arc::new(Notify::new());
    let journal = Arc::new(TestJournal {
        first_append_gate: Some((entered.clone(), release.clone())),
        ..Default::default()
    });
    let (sender, mut receiver, _) = ChannelBuilder::new().build(ExternalEventTestState::Drained);
    for event in [
        ExternalEventTestEvent::Initialize,
        ExternalEventTestEvent::Error("late failure".into()),
    ] {
        sender.send(event).await.unwrap();
    }
    let scope = PublicationScope::new();
    let owner = scope.clone();
    let target = journal.clone();
    let waiter = tokio::spawn(async move {
        owner
            .enter(record_terminal_commands(
                &mut receiver,
                target,
                WriterId::from(StageId::new()),
                "worker",
                "Drained",
            ))
            .await
    });
    entered.notified().await;
    waiter.abort();
    assert!(waiter.await.unwrap_err().is_cancelled());
    assert!(sender
        .send(ExternalEventTestEvent::Initialize)
        .await
        .is_err());
    assert!(journal.read_all_unordered().await.unwrap().is_empty());
    release.notify_one();
    scope.join().await.unwrap();
    assert_eq!(journal.read_all_unordered().await.unwrap().len(), 2);
}

struct CompletionContext {
    entered: Arc<Notify>,
    release: Arc<Notify>,
    fail_action: bool,
}
impl FsmContext for CompletionContext {}

#[derive(Clone, Debug)]
struct CompletionAction;

#[async_trait::async_trait]
impl FsmAction for CompletionAction {
    type Context = CompletionContext;

    async fn execute(
        &self,
        context: &mut CompletionContext,
    ) -> Result<(), obzenflow_fsm::FsmError> {
        context.entered.notify_one();
        context.release.notified().await;
        if context.fail_action {
            return Err(obzenflow_fsm::FsmError::HandlerError(
                "terminal action failed".into(),
            ));
        }
        Ok(())
    }
}

struct CompletionSupervisor;

impl Supervisor for CompletionSupervisor {
    type State = ExternalEventTestState;
    type Event = ExternalEventTestEvent;
    type Context = CompletionContext;
    type Action = CompletionAction;

    fn build_state_machine(
        &self,
        initial_state: Self::State,
    ) -> StateMachine<Self::State, Self::Event, Self::Context, Self::Action> {
        fsm! {
            state: ExternalEventTestState;
            event: ExternalEventTestEvent;
            context: CompletionContext;
            action: CompletionAction;
            initial: initial_state;

            state ExternalEventTestState::Running {
                on ExternalEventTestEvent::Initialize => |_state: &ExternalEventTestState, _event: &ExternalEventTestEvent, _ctx: &mut CompletionContext| {
                    Box::pin(async { Ok(Transition { next_state: ExternalEventTestState::Drained, actions: vec![CompletionAction] }) })
                };
            }
            state ExternalEventTestState::Drained {
                on ExternalEventTestEvent::Error => |_state: &ExternalEventTestState, event: &ExternalEventTestEvent, _ctx: &mut CompletionContext| {
                    let ExternalEventTestEvent::Error(error) = event else { unreachable!() };
                    let error = error.clone();
                    Box::pin(async { Ok(Transition { next_state: ExternalEventTestState::Failed(error), actions: vec![] }) })
                };
            }
            state ExternalEventTestState::Failed {
                on ExternalEventTestEvent::Error => |state: &ExternalEventTestState, _event: &ExternalEventTestEvent, _ctx: &mut CompletionContext| {
                    let state = state.clone();
                    Box::pin(async { Ok(Transition { next_state: state, actions: vec![] }) })
                };
            }
        }
    }

    fn name(&self) -> &str {
        "completion-worker"
    }
}

impl ExternalEventPolicy for CompletionSupervisor {
    fn external_event_mode(state: &Self::State) -> ExternalEventMode {
        ExternalEventTestHandlerSupervisor::external_event_mode(state)
    }

    fn on_external_event_channel_closed(state: &Self::State) -> Option<Self::Event> {
        ExternalEventTestHandlerSupervisor::on_external_event_channel_closed(state)
    }
}

#[async_trait::async_trait]
impl HandlerSupervised for CompletionSupervisor {
    type Handler = ();

    async fn dispatch_state(
        &mut self,
        state: &Self::State,
        _context: &mut Self::Context,
    ) -> Result<EventLoopDirective<Self::Event>, Box<dyn std::error::Error + Send + Sync>> {
        assert!(matches!(
            state,
            ExternalEventTestState::Drained | ExternalEventTestState::Failed(_)
        ));
        Ok(EventLoopDirective::Terminate)
    }

    fn writer_id(&self) -> WriterId {
        WriterId::from(self.stage_id())
    }
    fn stage_id(&self) -> StageId {
        StageId::new_const(1)
    }
    fn event_for_action_error(&self, error: String) -> Self::Event {
        ExternalEventTestEvent::Error(error)
    }
}

#[tokio::test]
async fn terminal_mailbox_records_errors_arriving_during_completion_actions() {
    for fail_action in [false, true] {
        let journal = Arc::new(TestJournal::default());
        let entered = Arc::new(Notify::new());
        let release = Arc::new(Notify::new());
        let (sender, receiver, watcher) =
            ChannelBuilder::new().build(ExternalEventTestState::Running);
        sender
            .send(ExternalEventTestEvent::Initialize)
            .await
            .unwrap();
        let supervisor = HandlerSupervisedWithExternalEvents::new(
            CompletionSupervisor,
            receiver,
            watcher.clone(),
            journal.clone(),
        );
        let task = SupervisorTaskBuilder::new("completion-worker").spawn_handler_supervised(
            supervisor,
            ExternalEventTestState::Running,
            CompletionContext {
                entered: entered.clone(),
                release: release.clone(),
                fail_action,
            },
        );
        let handle = HandleBuilder::new()
            .with_event_sender(sender.clone())
            .with_state_watcher(watcher.clone())
            .with_supervisor_task(task)
            .build_standard()
            .unwrap();
        entered.notified().await;
        sender
            .send(ExternalEventTestEvent::Initialize)
            .await
            .unwrap();
        sender
            .send(ExternalEventTestEvent::Error(
                "late external failure".into(),
            ))
            .await
            .unwrap();
        release.notify_one();
        handle.wait_for_completion().await.unwrap();
        let state = watcher.current();
        if fail_action {
            assert!(
                matches!(&state, ExternalEventTestState::Failed(error) if error.contains("terminal action failed"))
            );
        } else {
            assert_eq!(state, ExternalEventTestState::Drained);
        }
        let records = journal.read_all_unordered().await.unwrap();
        assert_eq!(records.len(), 2);
        assert!(
            matches!(&records[1].payload, SystemPayload::SupervisorCommandDiscarded { terminal_state, disposition: CommandDiscardDisposition::UnexpectedError, error: Some(error), .. } if terminal_state == state.variant_name() && error == "late external failure")
        );
    }
}

#[tokio::test]
async fn terminal_mailbox_journal_failure_is_retained_by_supervisor_join() {
    let journal = Arc::new(TestJournal {
        fail: true,
        ..Default::default()
    });
    let (sender, receiver, watcher) = ChannelBuilder::new().build(ExternalEventTestState::Drained);
    sender
        .send(ExternalEventTestEvent::Initialize)
        .await
        .unwrap();
    let supervisor = HandlerSupervisedWithExternalEvents::new(
        CompletionSupervisor,
        receiver,
        watcher.clone(),
        journal.clone(),
    );
    let context = CompletionContext {
        entered: Arc::new(Notify::new()),
        release: Arc::new(Notify::new()),
        fail_action: false,
    };
    let task = SupervisorTaskBuilder::new("completion-worker").spawn_handler_supervised(
        supervisor,
        ExternalEventTestState::Drained,
        context,
    );
    let handle = HandleBuilder::new()
        .with_event_sender(sender)
        .with_state_watcher(watcher)
        .with_supervisor_task(task)
        .build_standard()
        .unwrap();
    let error = handle.wait_for_completion().await.unwrap_err();
    assert!(error.to_string().contains("Journal is full"), "{error}");
    assert_eq!(
        journal.attempts.load(Ordering::SeqCst),
        1,
        "failed appends must not be retried"
    );
    assert!(journal.read_all_unordered().await.unwrap().is_empty());
}