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

use super::types::{
    AdvertisedWriterSeqByEventType, ReaderSelectionPolicy, SelectedDataSeqByEventType,
};
use super::{
    CompositeEntrySpec, ContractTracker, ContractsWiring, DeliveredCount, DeliveryFilter,
    FeedContractChain, FeedIdentity, ReaderSlot, ReaderTiebreakKey, SelectedFeedMetadata, StageKey,
    SubscriptionState, UpstreamSubscription,
};
use crate::contracts::ContractChain;
use crate::control_plane::NoControlPlane;
use crate::messaging::upstream_subscription_policy::build_policy_stack_for_upstream;
use async_trait::async_trait;
use obzenflow_core::event::provenance::FlowContext;
use obzenflow_core::event::types::SeqNo;
use obzenflow_core::event::JournalEvent;
use obzenflow_core::journal::journal_error::JournalError;
use obzenflow_core::journal::reader::JournalReader;
use obzenflow_core::journal::Journal;
use obzenflow_core::{
    DeliveryContract, EventType, JournalRecord, Result, StageId, TransportContract,
};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;

/// Fallback reader used when a real journal reader cannot be created.
///
/// This reader behaves as an always-empty journal (EOF), allowing the
/// subscription machinery to continue operating without failing the FSM.
struct EmptyJournalReader<T: JournalEvent> {
    _phantom: std::marker::PhantomData<T>,
}

impl<T: JournalEvent> EmptyJournalReader<T> {
    fn new() -> Self {
        Self {
            _phantom: std::marker::PhantomData,
        }
    }
}

#[async_trait]
impl<T> JournalReader<T> for EmptyJournalReader<T>
where
    T: JournalEvent + Send + Sync + 'static,
{
    async fn next(
        &mut self,
    ) -> std::result::Result<Option<JournalRecord<T::Payload>>, JournalError> {
        Ok(None)
    }

    fn position(&self) -> u64 {
        0
    }

    fn is_at_end(&self) -> bool {
        true
    }
    fn initial_prefix_complete(&self) -> std::result::Result<bool, JournalError> {
        Ok(true)
    }
}

impl<T> UpstreamSubscription<T>
where
    T: JournalEvent + 'static,
{
    /// Create a new subscription from upstream journals
    pub async fn new_with_names(
        owner_label: &str,
        upstream_journals: &[(StageId, String, Arc<dyn Journal<T>>)],
    ) -> Result<Self> {
        // Delegate to the position-aware constructor with all starting
        // positions at 0 (from-beginning semantics).
        let start_positions = vec![0u64; upstream_journals.len()];
        Self::new_with_names_from_positions(owner_label, upstream_journals, &start_positions).await
    }

    /// Create a new subscription from upstream journals, starting each reader
    /// from an explicit position.
    ///
    /// This is used by the metrics aggregator (FLOWIP-059 Phase 6) to
    /// fast-forward readers to the tail while still seeding snapshot metrics
    /// from wide events. Other callers should generally prefer `new_with_names`.
    pub async fn new_with_names_from_positions(
        owner_label: &str,
        upstream_journals: &[(StageId, String, Arc<dyn Journal<T>>)],
        start_positions: &[u64],
    ) -> Result<Self> {
        if upstream_journals.len() != start_positions.len() {
            return Err(format!(
                "start_positions length {} does not match upstream_journals length {}",
                start_positions.len(),
                upstream_journals.len()
            )
            .into());
        }

        let mut readers = Vec::new();

        tracing::debug!(
            "Creating subscription for {} upstream journals",
            upstream_journals.len()
        );

        tracing::debug!(
            target: "flowip-080o",
            owner = owner_label,
            readers = ?upstream_journals
                .iter()
                .map(|(_id, name, journal)| format!("{} ({})", name, journal.id()))
                .collect::<Vec<_>>(),
            "UpstreamSubscription::new_with_names binding readers"
        );

        for ((stage_id, stage_name, journal), position) in
            upstream_journals.iter().zip(start_positions.iter())
        {
            // Get journal ID for debugging
            let journal_id = journal.id();
            tracing::debug!(
                target: "flowip-080o",
                stage_id = ?stage_id,
                stage_name = stage_name,
                journal_id = ?journal_id,
                "Creating reader for upstream journal"
            );
            let reader_result = if *position == 0 {
                journal.reader().await
            } else {
                journal.reader_from(*position).await
            };

            let reader: Box<dyn JournalReader<T>> = match reader_result {
                Ok(reader) => reader,
                Err(e)
                    if crate::runtime_resource_limits::journal_error_is_too_many_open_files(&e) =>
                {
                    return Err(format!(
                        "Too many open files while creating reader for upstream journal (owner={owner_label}, stage_id={stage_id:?}, stage_name={stage_name}, journal_id={journal_id:?}). Increase RLIMIT_NOFILE / `ulimit -n` or reduce pipeline size (for development, disable metrics in obzenflow.toml). Underlying error: {e}"
                    )
                    .into());
                }
                Err(JournalError::Implementation { message, source }) => {
                    // Best-effort: log the failure and use an empty reader so the
                    // FSM can continue operating (upstream treated as having no events).
                    tracing::error!(
                        target: "flowip-080o",
                        stage_id = ?stage_id,
                        stage_name = stage_name,
                        journal_id = ?journal_id,
                        journal_error_message = %message,
                        journal_error_source = %source,
                        "Failed to create reader for upstream journal; using EmptyJournalReader (no events)"
                    );
                    Box::new(EmptyJournalReader::<T>::new()) as Box<dyn JournalReader<T>>
                }
                Err(e) => {
                    return Err(
                        format!("Failed to create reader for stage {stage_id:?}: {e}").into(),
                    );
                }
            };
            readers.push(ReaderSlot {
                stage_id: *stage_id,
                stage_key: StageKey::new(stage_name.clone()),
                reader,
            });
        }

        let state = SubscriptionState::new(readers.len());
        let reader_tiebreak_keys = readers
            .iter()
            .map(|slot| ReaderTiebreakKey {
                stage_key: slot.stage_key.clone(),
                feed_identity: FeedIdentity::unfiltered(),
            })
            .collect();

        Ok(Self {
            delivery_filter: DeliveryFilter::All,
            owner_label: owner_label.to_string(),
            selected_data_seq_by_reader: vec![SeqNo(0); readers.len()],
            selected_data_seq_by_reader_event_type: vec![
                SelectedDataSeqByEventType::default();
                readers.len()
            ],
            advertised_writer_seq_by_reader_event_type: vec![
                AdvertisedWriterSeqByEventType::default(
                );
                readers.len()
            ],
            held_heads: readers.iter().map(|_| None).collect(),
            delivered_count_by_reader: vec![DeliveredCount::default(); readers.len()],
            generation_by_reader: vec![obzenflow_core::ReaderGeneration::default(); readers.len()],
            last_positional_seq: vec![obzenflow_core::AdmissionSeq(0); readers.len()],
            last_delivered_generation: None,
            reader_tiebreak_keys,
            readers,
            archived_stage_ids_by_current: HashMap::new(),
            selected_event_types_by_stage: HashMap::new(),
            selected_feeds_by_stage: HashMap::new(),
            composite_entries_by_stage: HashMap::new(),
            state,
            contract_tracker: None,
            contract_chains: Vec::new(),
            contract_feed_chains: Vec::new(),
            contract_policies: Vec::new(),
            control_plane: Arc::new(NoControlPlane),
            last_eof_outcome: None,
            last_delivered_upstream_stage: None,
            next_stage_input_position: 1,
            last_delivered_stage_input_position: None,
            reader_selection: ReaderSelectionPolicy::default(),
            seq_ordered: false,
            entered_generation: obzenflow_core::ReaderGeneration::default(),
            last_merge_wait: None,
            merge_candidate_index: None,
        })
    }

    /// Choose the reader-selection policy (FLOWIP-095d).
    ///
    /// `CanonicalMerge` requires every reader to start at position zero with
    /// no tail-start baseline: a non-zero starting point would shift
    /// per-reader delivered ordinals between runs and break the
    /// deterministic-order function. The invariant is enforced here, where
    /// the policy is chosen, because construction order (`new_at_tail` then
    /// this) would otherwise admit a silently broken subscription.
    pub fn with_reader_selection(mut self, policy: ReaderSelectionPolicy) -> Self {
        if policy == ReaderSelectionPolicy::CanonicalMerge {
            for (index, slot) in self.readers.iter().enumerate() {
                let position = slot.reader.position();
                assert!(
                    position == 0 && !self.state.baseline_at_tail[index],
                    "FLOWIP-095d: CanonicalMerge requires reader '{}' at position \
                     zero with no tail-start baseline (position {position}); a non-zero \
                     starting point shifts per-reader delivered ordinals between runs and \
                     breaks the deterministic merge",
                    slot.stage_key
                );
            }
        }
        self.reader_selection = policy;
        self
    }

    /// FLOWIP-120n F18: run the seq-ordered merge. Only meaningful under
    /// `CanonicalMerge`; the flow build sets both together on marked
    /// source-fed ordered fan-ins.
    pub fn with_seq_ordered(mut self, seq_ordered: bool) -> Self {
        self.seq_ordered = seq_ordered;
        self
    }

    /// The generation this run entered at (FLOWIP-120n F18): 0 live, archive
    /// max recorded generation + 1 on replay/resume. Gates the seq-mode
    /// quiet-input wait exemption per reader.
    pub fn with_entered_generation(mut self, entered: obzenflow_core::ReaderGeneration) -> Self {
        self.entered_generation = entered;
        self
    }

    /// Install stable replay aliases from current topology stages to the
    /// corresponding archived stage authors.
    pub fn with_archived_stage_ids(
        mut self,
        archived_stage_ids_by_current: HashMap<StageId, StageId>,
    ) -> Self {
        self.archived_stage_ids_by_current = archived_stage_ids_by_current;
        self
    }

    /// Configure this subscription to deliver only transport-relevant events to the caller.
    ///
    /// This is intended for *stage runtime* subscriptions where downstream stages should
    /// not be forced to process upstream observability events (e.g. middleware metrics)
    /// as part of normal draining / shutdown.
    pub fn transport_only(mut self) -> Self {
        self.delivery_filter = DeliveryFilter::TransportOnly;
        self
    }

    /// Configure selected Data event types per upstream reader.
    pub fn with_selected_event_types(
        mut self,
        selected_event_types_by_stage: HashMap<StageId, HashSet<EventType>>,
    ) -> Self {
        self.selected_feeds_by_stage = selected_event_types_by_stage
            .iter()
            .map(|(stage_id, event_types)| {
                let feeds = event_types
                    .iter()
                    .cloned()
                    .map(SelectedFeedMetadata::unscoped)
                    .collect();
                (*stage_id, feeds)
            })
            .collect();
        self.selected_event_types_by_stage = selected_event_types_by_stage;
        self.recompute_tiebreak_keys();
        self
    }

    /// Configure selected logical feeds per upstream reader.
    pub fn with_selected_feeds(
        mut self,
        selected_feeds_by_stage: HashMap<StageId, Vec<SelectedFeedMetadata>>,
    ) -> Self {
        self.selected_event_types_by_stage = selected_feeds_by_stage
            .iter()
            .map(|(stage_id, feeds)| {
                (
                    *stage_id,
                    feeds
                        .iter()
                        .map(|feed| feed.event_type().clone())
                        .collect::<HashSet<_>>(),
                )
            })
            .collect();
        self.selected_feeds_by_stage = selected_feeds_by_stage;
        self.recompute_tiebreak_keys();
        self
    }

    /// Configure exact composite input-boundary stamps per upstream edge.
    pub fn with_composite_entries(
        mut self,
        entries_by_stage: HashMap<StageId, Vec<CompositeEntrySpec>>,
    ) -> Self {
        self.composite_entries_by_stage = entries_by_stage;
        self
    }

    /// Recompute per-reader tiebreak keys (FLOWIP-095d).
    ///
    /// The key is (stage key, feed identity), both stable across runs;
    /// per-run `StageId` ULIDs never participate. Two readers consuming
    /// different selected feeds of the same upstream stage share a stage key,
    /// so the feed identity keeps the key total, and its role qualifier keeps
    /// it total even when two readers share a stage key and event type but
    /// differ by feed role.
    fn recompute_tiebreak_keys(&mut self) {
        self.reader_tiebreak_keys = self
            .readers
            .iter()
            .map(|slot| ReaderTiebreakKey {
                stage_key: slot.stage_key.clone(),
                feed_identity: self
                    .selected_feeds_by_stage
                    .get(&slot.stage_id)
                    .map(|feeds| FeedIdentity::from_feeds(feeds))
                    .unwrap_or_default(),
            })
            .collect();
    }

    /// Create a subscription starting from explicit tail positions.
    ///
    /// Readers are treated as logically at EOF for historical data
    /// (baseline_at_tail = true) but will still observe any new
    /// events appended after subscription creation. This is used by
    /// tail-first observers like the metrics aggregator which seed
    /// from tail snapshots and do not need to re-observe historical
    /// EOF control events.
    pub async fn new_at_tail(
        owner_label: &str,
        upstream_journals: &[(StageId, String, Arc<dyn Journal<T>>)],
        tail_positions: &[u64],
    ) -> Result<Self> {
        let mut sub =
            Self::new_with_names_from_positions(owner_label, upstream_journals, tail_positions)
                .await?;

        // FLOWIP-095d: tail-start subscriptions are incompatible with the
        // canonical merge; `with_reader_selection` enforces the invariant.

        // Only mark a reader as baseline-at-tail if we're actually skipping historical
        // events (i.e., the computed tail position is non-zero). For fresh/empty
        // journals, setting this baseline would incorrectly allow "logical EOF"
        // termination before new events are observed.
        let mut baseline_count = 0usize;
        for (idx, tail_position) in tail_positions.iter().take(sub.readers.len()).enumerate() {
            if *tail_position > 0 {
                sub.state.mark_reader_baseline_at_tail(idx);
                baseline_count += 1;
            }
        }

        tracing::info!(
            target: "flowip-059d",
            owner = owner_label,
            reader_count = sub.readers.len(),
            baseline_count = baseline_count,
            "Created tail-start upstream subscription"
        );

        Ok(sub)
    }

    /// Backwards-compatible constructor using stage IDs as names
    pub async fn new(upstream_journals: &[(StageId, Arc<dyn Journal<T>>)]) -> Result<Self> {
        let with_names: Vec<(StageId, String, Arc<dyn Journal<T>>)> = upstream_journals
            .iter()
            .map(|(id, journal)| (*id, format!("{id:?}"), journal.clone()))
            .collect();
        Self::new_with_names("unknown_owner", &with_names).await
    }

    /// Attach the consuming stage's context before a framework subscription is polled.
    /// Contracts must already be enabled; public construction keeps its existing default.
    pub(crate) fn with_contract_flow_context(mut self, owner: FlowContext) -> Self {
        self.contract_tracker
            .as_mut()
            .expect("contract flow context requires contract wiring")
            .flow_context = Some(owner);
        self
    }

    /// Enable contract emission for at-least-once delivery guarantees
    pub fn with_contracts(mut self, wiring: ContractsWiring) -> Self {
        let ContractsWiring {
            writer_id,
            contract_journal,
            config,
            system_journal,
            reader_stage,
            control_plane,
            include_delivery_contract,
            cycle_guard_config,
        } = wiring;

        self.control_plane = control_plane.clone();

        self.contract_tracker = Some(ContractTracker {
            config,
            writer_id,
            journal: contract_journal,
            system_journal,
            reader_stage,
            receipt_aware_progress: include_delivery_contract,
            flow_context: None,
            output_events_written: SeqNo(0),
        });

        // Initialize per-reader contract chains using the new Contract framework.
        // For 090c v1, we attach a TransportContract to each upstream edge.
        if !self.readers.is_empty() {
            self.contract_chains = self
                .readers
                .iter()
                .map(|slot| {
                    let mut chain = ContractChain::new()
                        .with_contract(TransportContract::new())
                        .with_contract(obzenflow_core::SourceContract::new());
                    if include_delivery_contract {
                        chain = chain.with_contract(DeliveryContract::default());
                    }

                    // Divergence detection is attached only for SCC-internal upstreams
                    // when cycle metadata is available (FLOWIP-080r).
                    if let Some(cycle_cfg) = &cycle_guard_config {
                        if cycle_cfg.internal_upstreams.contains(&slot.stage_id) {
                            let thresholds = obzenflow_core::DivergenceThresholds {
                                max_cycle_depth: cycle_cfg.max_iterations.as_u16(),
                                ..Default::default()
                            };
                            chain = chain.with_contract(
                                obzenflow_core::DivergenceContract::with_thresholds(
                                    cycle_cfg.scc_id,
                                    thresholds,
                                ),
                            );
                        }
                    }
                    Some(chain)
                })
                .collect();

            // Selected-feed chains intentionally start with the transport
            // contract only. Delivery receipts and divergence predicates still
            // use the physical edge chain until those contracts gain explicit
            // selected-feed semantics.
            self.contract_feed_chains = self
                .readers
                .iter()
                .map(|slot| {
                    let Some(feeds) = self.selected_feeds_by_stage.get(&slot.stage_id) else {
                        return Vec::new();
                    };
                    if feeds.len() <= 1 {
                        return Vec::new();
                    }

                    feeds
                        .iter()
                        .cloned()
                        .map(|feed| {
                            FeedContractChain::new(
                                feed,
                                ContractChain::new().with_contract(TransportContract::new()),
                            )
                        })
                        .collect()
                })
                .collect();

            // Initialize per-reader policy stacks using the upstream stage IDs.
            self.contract_policies = self
                .readers
                .iter()
                .map(|slot| {
                    let stack = build_policy_stack_for_upstream(slot.stage_id);
                    Some(stack)
                })
                .collect();
        }

        self
    }
}