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

//! Explicit, best-effort inspection of protected accounting in journal tails.
//!
//! These helpers use the same live current-key lookup as background refresh.
//! They never scan or reconstruct journal history.

use obzenflow_core::event::context::StageType;
use obzenflow_core::event::provenance::RuntimeProvenance;
use obzenflow_core::event::ChainEvent;
use obzenflow_core::id::StageId;
use obzenflow_core::metrics::{FlowLifecycleMetricsSnapshot, StageMetadata, StageMetricsSnapshot};
use obzenflow_core::{Journal, WriterId};
use std::collections::HashMap;
use std::sync::Arc;

type StageJournalEntry = (
    StageId,
    Arc<dyn Journal<ChainEvent>>,
    Option<Arc<dyn Journal<ChainEvent>>>,
);

/// Read the most recent `RuntimeProvenance` from a journal's tail.
///
/// Reads selected current carriers, newest first. Missing live locators yield
/// no value; archive history is not rebuilt by a metrics helper.
pub async fn read_latest_runtime_context(
    journal: &Arc<dyn Journal<ChainEvent>>,
) -> Option<RuntimeProvenance> {
    journal
        .read_metrics_tail()
        .await
        .ok()?
        .into_iter()
        .find_map(|record| record.envelope.provenance.event.runtime)
}

/// Read the most recent `RuntimeProvenance` from a journal's tail for a specific
/// stage, filtering by `flow_context.stage_id`.
///
/// This stricter variant is used by metrics code to ensure that only runtime
/// snapshots authored by the stage associated with a journal are considered
/// when deriving per-stage metrics. Forwarded control events that still carry
/// an upstream `flow_context` are ignored.
pub async fn read_latest_runtime_context_for_stage(
    journal: &Arc<dyn Journal<ChainEvent>>,
    stage_id: StageId,
) -> Option<RuntimeProvenance> {
    journal
        .read_metrics_tail()
        .await
        .ok()?
        .into_iter()
        .find_map(|record| {
            let event = record.envelope.provenance.event;
            (event.flow_context.stage_id == stage_id && event.writer_id == WriterId::from(stage_id))
                .then_some(event.runtime)
                .flatten()
        })
}

/// Read stage metrics from journal tails.
///
/// Checks both data and error journals (if provided):
/// Protected counters retain their physical populations and max semantics.
/// Measurement families are selected independently by capture scope and sequence
/// across both journals; a later error-rail read has no special precedence.
pub async fn read_stage_metrics_from_tail(
    data_journal: &Arc<dyn Journal<ChainEvent>>,
    error_journal: Option<&Arc<dyn Journal<ChainEvent>>>,
    stage_id: StageId,
) -> Option<StageMetricsSnapshot> {
    use obzenflow_core::event::observability::ObservationSource;
    let mut metrics = super::fsm::StageMetrics::default();
    let observations = super::observations::LatestObservationMap::default();
    for journal in std::iter::once(data_journal).chain(error_journal) {
        if let Ok(records) = journal.read_metrics_tail().await {
            for record in records {
                if let Some(packet) = &record.envelope.observability {
                    observations.offer_recorded(packet);
                }
                let event = record.envelope.provenance.event;
                if event.flow_context.stage_id == stage_id
                    && event.writer_id == WriterId::from(stage_id)
                {
                    if let Some(provenance) = event.runtime {
                        metrics.merge_runtime_context(&provenance);
                    }
                }
            }
        }
    }
    for packet in observations.snapshot() {
        if let Some(runtime) = packet.runtime {
            metrics.merge_runtime_measurements(&runtime);
        }
    }
    metrics
        .latest_events_processed_total
        .map(|events| StageMetricsSnapshot {
            events_processed_total: events,
            events_accumulated_total: metrics.latest_events_accumulated_total.unwrap_or(0),
            events_emitted_total: metrics.latest_events_emitted_total.unwrap_or(0),
            errors_total: metrics.latest_errors_total.unwrap_or(0),
            errors_by_kind: metrics.errors_by_kind,
            in_flight: metrics.last_in_flight,
            recent_p50_ms: metrics.snapshot_p50_ms,
            recent_p90_ms: metrics.snapshot_p90_ms,
            recent_p95_ms: metrics.snapshot_p95_ms,
            recent_p99_ms: metrics.snapshot_p99_ms,
            recent_p999_ms: metrics.snapshot_p999_ms,
            processing_time_count: metrics.processing_time_count,
            processing_time_sum_nanos: metrics.processing_time_sum_nanos,
            timing_window: metrics.timing_window,
            event_loops_total: metrics.event_loops_total,
            event_loops_with_work_total: metrics.event_loops_with_work_total,
        })
}

/// Read flow-level metrics by aggregating all stage journal tails.
///
/// Returns `FlowLifecycleMetricsSnapshot` (the lifecycle event payload
/// type), not `FlowMetricsSnapshot` (the Prometheus/AppMetricsSnapshot
/// type). These are intentionally different:
/// - FlowLifecycleMetricsSnapshot: minimal view for lifecycle events.
/// - FlowMetricsSnapshot: full view for Prometheus export.
pub async fn read_flow_metrics_from_tails(
    stage_journals: &[StageJournalEntry],
    stage_metadata: &HashMap<StageId, StageMetadata>,
) -> FlowLifecycleMetricsSnapshot {
    let mut events_in_total: u64 = 0;
    let mut events_out_total: u64 = 0;
    let mut errors_total: u64 = 0;

    for (stage_id, data_journal, error_journal) in stage_journals {
        if let Some(snapshot) =
            read_stage_metrics_from_tail(data_journal, error_journal.as_ref(), *stage_id).await
        {
            if let Some(metadata) = stage_metadata.get(stage_id) {
                match metadata.stage_type {
                    StageType::FiniteSource | StageType::InfiniteSource => {
                        events_in_total += snapshot.events_processed_total;
                    }
                    StageType::Sink => {
                        events_out_total += snapshot.events_processed_total;
                    }
                    _ => {}
                }
            }
            errors_total += snapshot.errors_total;
        }
    }

    FlowLifecycleMetricsSnapshot {
        events_in_total,
        events_out_total,
        errors_total,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use async_trait::async_trait;
    use obzenflow_core::event::identity::journal_writer_id::JournalWriterId;
    use obzenflow_core::event::journal_record::JournalRecord;
    use obzenflow_core::event::provenance::{ExecutionAccounting, RuntimeProvenance};
    use obzenflow_core::event::status::processing_status::ErrorKind;
    use obzenflow_core::event::ChainPayload;
    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::{ChainEvent, WriterId};
    use std::sync::{Arc, Mutex};

    /// Minimal in-memory journal for ChainEvent used in tail-read tests.
    ///
    /// Stores envelopes in a Vec and implements `read_last_n` with the contract
    /// expected by `read_latest_runtime_context` (most recent first).
    struct InMemoryChainJournal {
        id: JournalId,
        owner: Option<JournalOwner>,
        events: Arc<Mutex<Vec<JournalRecord<ChainPayload>>>>,
    }

    impl InMemoryChainJournal {
        fn new(owner: JournalOwner) -> Self {
            Self {
                id: JournalId::new(),
                owner: Some(owner),
                events: Arc::new(Mutex::new(Vec::new())),
            }
        }

        fn append_raw(&self, event: ChainEvent) {
            let envelope = JournalRecord::new(JournalWriterId::from(self.id), event);
            let mut guard = self.events.lock().unwrap();
            guard.push(envelope);
        }
    }

    struct InMemoryReader {
        events: Vec<JournalRecord<ChainPayload>>,
        pos: usize,
    }

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

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

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

        async fn read_metrics_tail(
            &self,
        ) -> Result<Vec<JournalRecord<ChainPayload>>, JournalError> {
            Ok(self.events.lock().unwrap().iter().rev().cloned().collect())
        }

        async fn read_all_unordered(
            &self,
        ) -> Result<Vec<JournalRecord<ChainPayload>>, JournalError> {
            let guard = self.events.lock().unwrap();
            Ok(guard.clone())
        }

        async fn read_event(
            &self,
            _event_id: &obzenflow_core::EventId,
        ) -> Result<Option<JournalRecord<ChainPayload>>, JournalError> {
            Ok(None)
        }

        async fn reader_from(
            &self,
            position: u64,
        ) -> Result<Box<dyn JournalReader<ChainEvent>>, JournalError> {
            let guard = self.events.lock().unwrap();
            Ok(Box::new(InMemoryReader {
                events: guard.clone(),
                pos: position as usize,
            }))
        }

        async fn read_last_n(
            &self,
            count: usize,
        ) -> Result<Vec<JournalRecord<ChainPayload>>, JournalError> {
            let guard = self.events.lock().unwrap();
            let len = guard.len();
            let start = len.saturating_sub(count);
            // Return most recent first, matching Journal::read_last_n contract.
            Ok(guard[start..].iter().rev().cloned().collect())
        }
    }

    #[async_trait]
    impl JournalReader<ChainEvent> for InMemoryReader {
        async fn next(&mut self) -> Result<Option<JournalRecord<ChainPayload>>, JournalError> {
            if self.pos >= self.events.len() {
                Ok(None)
            } else {
                let envelope = self.events.get(self.pos).cloned();
                self.pos += 1;
                Ok(envelope)
            }
        }

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

        fn is_at_end(&self) -> bool {
            self.pos >= self.events.len()
        }
    }

    fn runtime_context_with_errors(
        events_processed_total: u64,
        errors_total: u64,
        by_kind: &[(ErrorKind, u64)],
    ) -> RuntimeProvenance {
        RuntimeProvenance {
            accounting: ExecutionAccounting {
                events_processed_total,
                events_accumulated_total: 0,
                events_emitted_total: 0,
                data_outputs_by_event_type: Vec::new(),
                data_inputs_by_upstream_event_type: Vec::new(),
                errors_total,
                failures_total: 0,
                errors_by_kind: by_kind.iter().cloned().collect(),
            },
        }
    }

    #[tokio::test]
    async fn read_stage_metrics_from_tail_uses_max_semantics_for_errors_by_kind() {
        use obzenflow_core::event::ChainEventFactory;
        use obzenflow_core::StageId;

        let stage_id = StageId::new();
        let owner = JournalOwner::stage(stage_id);

        let data_journal_raw = Arc::new(InMemoryChainJournal::new(owner.clone()));
        let error_journal_raw = Arc::new(InMemoryChainJournal::new(owner));
        let data_journal: Arc<dyn Journal<ChainEvent>> = data_journal_raw.clone();
        let error_journal: Arc<dyn Journal<ChainEvent>> = error_journal_raw.clone();

        // Data journal snapshot: 10 events, 2 domain errors.
        let data_ctx = runtime_context_with_errors(10, 2, &[(ErrorKind::Domain, 2)]);
        let mut data_event = ChainEventFactory::data_event(
            WriterId::from(stage_id),
            "test.data",
            serde_json::json!({"k": "v"}),
        );
        data_event.flow_context.stage_id = stage_id;
        data_event = data_event.with_runtime_provenance(data_ctx);
        data_journal_raw.append_raw(data_event);

        // Error journal snapshot: later snapshot with 5 total errors, including the same
        // domain errors plus additional remote errors.
        let error_ctx =
            runtime_context_with_errors(10, 5, &[(ErrorKind::Domain, 2), (ErrorKind::Remote, 3)]);
        let mut error_event = ChainEventFactory::data_event(
            WriterId::from(stage_id),
            "test.error",
            serde_json::json!({"k": "v2"}),
        );
        error_event.flow_context.stage_id = stage_id;
        error_event = error_event.with_runtime_provenance(error_ctx);
        error_journal_raw.append_raw(error_event);

        let snapshot = read_stage_metrics_from_tail(&data_journal, Some(&error_journal), stage_id)
            .await
            .expect("snapshot should be present");

        // Counters use monotonic max semantics across journals.
        assert_eq!(snapshot.events_processed_total, 10);
        assert_eq!(snapshot.errors_total, 5);

        // errors_by_kind uses monotonic max semantics (avoid double-counting across journals).
        assert_eq!(snapshot.errors_by_kind.get(&ErrorKind::Domain), Some(&2));
        assert_eq!(snapshot.errors_by_kind.get(&ErrorKind::Remote), Some(&3));
    }

    #[tokio::test]
    async fn read_latest_runtime_context_for_stage_ignores_mismatched_stage_ids() {
        use obzenflow_core::event::ChainEventFactory;
        use obzenflow_core::StageId;

        let local_stage_id = StageId::new();
        let upstream_stage_id = StageId::new();
        let owner = JournalOwner::stage(local_stage_id);

        let journal_raw = Arc::new(InMemoryChainJournal::new(owner));
        let journal: Arc<dyn Journal<ChainEvent>> = journal_raw.clone();

        // Upstream event with non-zero errors_total but wrong stage_id.
        let upstream_ctx = runtime_context_with_errors(100, 10, &[]);
        let mut upstream_event = ChainEventFactory::data_event(
            WriterId::from(upstream_stage_id),
            "upstream.data",
            serde_json::json!({"k": "v_upstream"}),
        );
        upstream_event.flow_context.stage_id = upstream_stage_id;
        upstream_event = upstream_event.with_runtime_provenance(upstream_ctx);
        journal_raw.append_raw(upstream_event);

        // Local event with zero errors_total and matching stage_id.
        let local_ctx = runtime_context_with_errors(50, 0, &[]);
        let mut local_event = ChainEventFactory::data_event(
            WriterId::from(local_stage_id),
            "local.data",
            serde_json::json!({"k": "v_local"}),
        );
        local_event.flow_context.stage_id = local_stage_id;
        local_event = local_event.with_runtime_provenance(local_ctx);
        journal_raw.append_raw(local_event);

        // Generic helper may see either; the stage-aware helper must only see the local snapshot.
        let generic_ctx = read_latest_runtime_context(&journal).await.expect("ctx");
        assert_eq!(generic_ctx.accounting.events_processed_total, 50);

        let filtered_ctx = read_latest_runtime_context_for_stage(&journal, local_stage_id)
            .await
            .expect("ctx");
        assert_eq!(filtered_ctx.accounting.events_processed_total, 50);
        assert_eq!(filtered_ctx.accounting.errors_total, 0);
    }

    #[tokio::test]
    async fn read_latest_runtime_context_for_stage_searches_past_forwarded_events() {
        use obzenflow_core::event::ChainEventFactory;
        use obzenflow_core::StageId;
        use serde_json::json;

        let stage_id = StageId::new();
        let owner = JournalOwner::stage(stage_id);

        let journal_raw = Arc::new(InMemoryChainJournal::new(owner));
        let journal: Arc<dyn Journal<ChainEvent>> = journal_raw.clone();

        // Seed a stage-authored event with runtime_context.
        let seeded_ctx = runtime_context_with_errors(42, 0, &[]);
        let mut seeded_event = ChainEventFactory::data_event(
            WriterId::from(stage_id),
            "seeded.data",
            json!({"k": "v"}),
        );
        seeded_event.flow_context.stage_id = stage_id;
        seeded_event = seeded_event.with_runtime_provenance(seeded_ctx);
        journal_raw.append_raw(seeded_event);

        // Append many forwarded/control-like events without runtime_context.
        // This simulates stateful/join stage journals where tail events may not carry a snapshot.
        for i in 0..50 {
            let mut forwarded = ChainEventFactory::data_event(
                WriterId::from(stage_id),
                "forwarded.control",
                json!({"i": i}),
            );
            forwarded.flow_context.stage_id = stage_id;
            journal_raw.append_raw(forwarded);
        }

        let ctx = read_latest_runtime_context_for_stage(&journal, stage_id)
            .await
            .expect("ctx");
        assert_eq!(ctx.accounting.events_processed_total, 42);
    }
}