asx-rs 0.14.0

AS2 and AS4 B2B messaging library for Rust — signing, encryption, MDN, and ebMS3/AS4 profile support
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
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
//! Startup validation for regulated deployments.
//!
//! A deployment that is *configured* strictly but never *validated* strictly is
//! indistinguishable from a correct one until the first message that depends on
//! the difference. This module turns that into a startup error.
//!
//! ```no_run
//! use asx_rs::presets::{DeploymentTopology, StrictRuntimeBootstrap};
//!
//! # fn main() -> asx_rs::Result<()> {
//! # let (event_bus, dedup, reconciliation, session): (
//! #     asx_rs::observability::EventBus,
//! #     std::sync::Arc<dyn asx_rs::storage::DedupStorage>,
//! #     std::sync::Arc<dyn asx_rs::storage::ReconciliationStorage>,
//! #     asx_rs::core::SessionContext,
//! # ) = unimplemented!();
//! let token = StrictRuntimeBootstrap::new("startup")
//!     .event_bus(&event_bus)
//!     .dedup(dedup.as_ref())
//!     .reconciliation(reconciliation.as_ref())
//!     .topology(DeploymentTopology::SingleNode)
//!     .validate()?;
//!
//! // Only a token-bound session may enter a strict protocol entry point.
//! let session = token.bind(&session);
//! # let _ = session;
//! # Ok(())
//! # }
//! ```

use std::any::type_name_of_val;
use std::time::SystemTime;

#[cfg(feature = "as4")]
use crate::as4::As4TopologyCoordination;
use crate::core::SessionContext;
use crate::core::{AsxError, ErrorCode, ErrorContext, InteropMode, Result};
use crate::observability::{EventBus, EventEmissionMode};
use crate::storage::{DedupStorage, ReconciliationStorage};

/// Runtime deployment topology used by strict-production startup validation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum DeploymentTopology {
    /// Single process / single replica.
    SingleNode,
    /// Multiple replicas, where cross-node coordination is required for the
    /// AS4 pull store and the conversation ordering gate.
    Clustered,
}

/// Unforgeable proof that strict-production startup validation succeeded.
///
/// The only way to obtain one is [`StrictRuntimeBootstrap::validate`], and the
/// only thing it is good for is [`bind`](Self::bind)ing a session so that
/// strict protocol entry points will accept it. There is no public
/// constructor, no `Default`, and no way to set the session marker without it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StrictRuntimeBootstrapToken {
    issued_at: SystemTime,
    stage: &'static str,
    as4_topology: Option<DeploymentTopology>,
}

impl StrictRuntimeBootstrapToken {
    /// The stage label validation ran under.
    #[must_use]
    pub fn stage(&self) -> &'static str {
        self.stage
    }

    /// When validation succeeded.
    #[must_use]
    pub fn issued_at(&self) -> SystemTime {
        self.issued_at
    }

    /// The AS4 topology this token covers, if AS4 coordination was validated.
    #[must_use]
    pub fn as4_topology(&self) -> Option<DeploymentTopology> {
        self.as4_topology
    }

    /// Return a clone of `session` marked as startup-validated.
    ///
    /// Strict protocol entry points refuse a session that carries no marker.
    #[must_use]
    pub fn bind(&self, session: &SessionContext) -> SessionContext {
        session
            .clone()
            .with_strict_runtime_bootstrap_validated(true)
    }
}

/// Startup validator for regulated deployments.
///
/// Checks, in one place, that the runtime a deployment is about to accept
/// traffic on is the one its profile assumes:
///
/// | Component | Requirement |
/// |---|---|
/// | [`EventBus`] | Strict emission (not `BestEffort`) and a production-durable audit sink |
/// | [`DedupStorage`] | `is_durable()` **and** `cluster_safe()` |
/// | [`ReconciliationStorage`] | `is_durable()` **and** `cluster_safe()` |
/// | AS4 pull store / conversation gate | `cluster_safe()`, when the topology is [`DeploymentTopology::Clustered`] |
///
/// Those last two are self-declarations: this validates that the operator made
/// a claim, not that the claim is true. The restart tests a backend must
/// actually pass are the embedder's to run.
pub struct StrictRuntimeBootstrap<'a> {
    stage: &'static str,
    event_bus: Option<&'a EventBus>,
    dedup: Option<&'a dyn DedupStorage>,
    reconciliation: Option<&'a dyn ReconciliationStorage>,
    topology: DeploymentTopology,
    #[cfg(feature = "as4")]
    as4_pull_store: Option<&'a dyn As4TopologyCoordination>,
    #[cfg(feature = "as4")]
    as4_conversation_gate: Option<&'a dyn As4TopologyCoordination>,
    #[cfg(not(feature = "as4"))]
    _marker: std::marker::PhantomData<&'a ()>,
}

impl std::fmt::Debug for StrictRuntimeBootstrap<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("StrictRuntimeBootstrap")
            .field("stage", &self.stage)
            .field("event_bus", &self.event_bus.is_some())
            .field("dedup", &self.dedup.is_some())
            .field("reconciliation", &self.reconciliation.is_some())
            .field("topology", &self.topology)
            .finish_non_exhaustive()
    }
}

impl<'a> StrictRuntimeBootstrap<'a> {
    /// Begin validation. `stage` labels the errors this produces.
    #[must_use]
    pub fn new(stage: &'static str) -> Self {
        Self {
            stage,
            event_bus: None,
            dedup: None,
            reconciliation: None,
            topology: DeploymentTopology::SingleNode,
            #[cfg(feature = "as4")]
            as4_pull_store: None,
            #[cfg(feature = "as4")]
            as4_conversation_gate: None,
            #[cfg(not(feature = "as4"))]
            _marker: std::marker::PhantomData,
        }
    }

    /// The bus protocol operations will emit through. Required.
    #[must_use]
    pub fn event_bus(mut self, event_bus: &'a EventBus) -> Self {
        self.event_bus = Some(event_bus);
        self
    }

    /// The duplicate-detection backend. Required.
    #[must_use]
    pub fn dedup(mut self, dedup: &'a dyn DedupStorage) -> Self {
        self.dedup = Some(dedup);
        self
    }

    /// The reconciliation backend. Required.
    #[must_use]
    pub fn reconciliation(mut self, reconciliation: &'a dyn ReconciliationStorage) -> Self {
        self.reconciliation = Some(reconciliation);
        self
    }

    /// Declare the deployment topology. Defaults to
    /// [`DeploymentTopology::SingleNode`]; declaring `Clustered` turns on the
    /// AS4 coordination checks below.
    #[must_use]
    pub fn topology(mut self, topology: DeploymentTopology) -> Self {
        self.topology = topology;
        self
    }

    /// The AS4 pull store. Required when the topology is `Clustered`.
    #[cfg(feature = "as4")]
    #[must_use]
    pub fn as4_pull_store(mut self, pull_store: &'a dyn As4TopologyCoordination) -> Self {
        self.as4_pull_store = Some(pull_store);
        self
    }

    /// The AS4 conversation ordering gate. Required when the topology is
    /// `Clustered`.
    #[cfg(feature = "as4")]
    #[must_use]
    pub fn as4_conversation_gate(mut self, gate: &'a dyn As4TopologyCoordination) -> Self {
        self.as4_conversation_gate = Some(gate);
        self
    }

    /// Run every configured check and mint a token.
    ///
    /// # Errors
    ///
    /// [`ErrorCode::InvalidInput`] when a required component was not supplied,
    /// and [`ErrorCode::ReliabilityFailure`] when one of them does not meet the
    /// strict-production requirement. Every message names the component, its
    /// concrete type and the properties it declared.
    pub fn validate(self) -> Result<StrictRuntimeBootstrapToken> {
        let stage = self.stage;
        let event_bus = self.event_bus.ok_or_else(|| {
            missing_component(stage, "event_bus", "StrictRuntimeBootstrap::event_bus")
        })?;
        let dedup = self
            .dedup
            .ok_or_else(|| missing_component(stage, "dedup", "StrictRuntimeBootstrap::dedup"))?;
        let reconciliation = self.reconciliation.ok_or_else(|| {
            missing_component(
                stage,
                "reconciliation",
                "StrictRuntimeBootstrap::reconciliation",
            )
        })?;

        validate_event_bus(stage, event_bus)?;
        require_durable_backend(stage, "reconciliation", reconciliation.as_durability())?;
        require_durable_backend(stage, "dedup", dedup.as_durability())?;

        #[cfg(feature = "as4")]
        let as4_topology = {
            validate_as4_topology(
                stage,
                self.topology,
                self.as4_pull_store,
                self.as4_conversation_gate,
            )?;
            Some(self.topology)
        };
        #[cfg(not(feature = "as4"))]
        let as4_topology = None;

        Ok(StrictRuntimeBootstrapToken {
            issued_at: SystemTime::now(),
            stage,
            as4_topology,
        })
    }
}

fn missing_component(stage: &'static str, component: &str, setter: &str) -> AsxError {
    AsxError::new(
        ErrorCode::InvalidInput,
        format!(
            "strict runtime bootstrap requires a {component}; call {setter}(..) before validate()"
        ),
        ErrorContext::new(stage),
    )
}

/// The durability properties a storage backend declares about itself.
#[derive(Debug, Clone, Copy)]
pub struct BackendDurability {
    /// Survives a process restart.
    pub durable: bool,
    /// Coordinates correctly across replicas.
    pub cluster_safe: bool,
    /// The concrete backend type, for error messages.
    pub backend_type: &'static str,
}

trait DeclaredDurability {
    fn as_durability(&self) -> BackendDurability;
}

impl DeclaredDurability for &dyn DedupStorage {
    fn as_durability(&self) -> BackendDurability {
        BackendDurability {
            durable: self.is_durable(),
            cluster_safe: self.cluster_safe(),
            backend_type: type_name_of_val(*self),
        }
    }
}

impl DeclaredDurability for &dyn ReconciliationStorage {
    fn as_durability(&self) -> BackendDurability {
        BackendDurability {
            durable: self.is_durable(),
            cluster_safe: self.cluster_safe(),
            backend_type: type_name_of_val(*self),
        }
    }
}

fn require_durable_backend(
    stage: &'static str,
    component: &str,
    d: BackendDurability,
) -> Result<()> {
    let missing = if !d.durable {
        "durable"
    } else if !d.cluster_safe {
        "cluster-safe"
    } else {
        return Ok(());
    };
    Err(AsxError::new(
        ErrorCode::ReliabilityFailure,
        format!(
            "strict production requires a {missing} {component} backend; backend_type={}; durable={}; cluster_safe={}",
            d.backend_type, d.durable, d.cluster_safe
        ),
        ErrorContext::new(stage),
    ))
}

fn validate_event_bus(stage: &'static str, event_bus: &EventBus) -> Result<()> {
    let mode = event_bus.emission_mode();
    let has_durable_audit_sink = event_bus.has_production_durable_audit_sink();

    // `StrictWithAuditFallback` is a *strict* mode: it cannot be constructed
    // without a production-durable sink, and it persists every event there when
    // no subscriber is live. Rejecting it here would have forced regulated
    // deployments that follow its own documentation to fail this gate.
    if matches!(mode, EventEmissionMode::BestEffort) {
        return Err(AsxError::new(
            ErrorCode::ReliabilityFailure,
            format!(
                "strict production requires a strict event emission mode \
                 (StrictTransactional or StrictWithAuditFallback); emission_mode={mode:?}; \
                 durable_audit_sink={has_durable_audit_sink}"
            ),
            ErrorContext::new(stage),
        ));
    }

    if !has_durable_audit_sink {
        return Err(AsxError::new(
            ErrorCode::ReliabilityFailure,
            format!(
                "strict production requires a durable audit sink; emission_mode={mode:?}; \
                 durable_audit_sink={has_durable_audit_sink}"
            ),
            ErrorContext::new(stage),
        ));
    }

    Ok(())
}

#[cfg(feature = "as4")]
fn validate_as4_topology(
    stage: &'static str,
    topology: DeploymentTopology,
    pull_store: Option<&dyn As4TopologyCoordination>,
    conversation_gate: Option<&dyn As4TopologyCoordination>,
) -> Result<()> {
    if topology == DeploymentTopology::SingleNode {
        return Ok(());
    }

    for (component, coordination, setter) in [
        (
            "pull-store",
            pull_store,
            "StrictRuntimeBootstrap::as4_pull_store",
        ),
        (
            "conversation-ordering",
            conversation_gate,
            "StrictRuntimeBootstrap::as4_conversation_gate",
        ),
    ] {
        let Some(coordination) = coordination else {
            return Err(AsxError::new(
                ErrorCode::ReliabilityFailure,
                format!(
                    "clustered topology requires a cluster-safe AS4 {component} backend; \
                     supply one via {setter}(..)"
                ),
                ErrorContext::new(stage),
            ));
        };
        if !coordination.cluster_safe() {
            return Err(AsxError::new(
                ErrorCode::ReliabilityFailure,
                format!(
                    "clustered topology requires cluster-safe AS4 {} coordination",
                    coordination.topology_component()
                ),
                ErrorContext::new(stage),
            ));
        }
    }

    Ok(())
}

/// Enforce strict production runtime guards on receive and enqueue paths.
///
/// Centralised so AS2 and AS4 call sites cannot drift apart on which guards
/// they run.
pub(crate) fn enforce_strict_production_runtime_receive_guards(
    stage: &'static str,
    session: &SessionContext,
    event_bus: &EventBus,
    fail_closed_audit_events: bool,
    reconciliation: Option<&dyn ReconciliationStorage>,
    dedup: Option<&dyn DedupStorage>,
) -> Result<()> {
    #[cfg(not(feature = "testing"))]
    {
        if let Some(reconciliation) = reconciliation {
            require_durable_backend(stage, "reconciliation", reconciliation.as_durability())?;
        }
        if let Some(dedup) = dedup {
            require_durable_backend(stage, "dedup", dedup.as_durability())?;
        }
    }

    #[cfg(feature = "testing")]
    let _ = (reconciliation, dedup);

    #[cfg(any(feature = "as2", feature = "as4"))]
    {
        crate::observability::require_durable_audit_sink(
            session,
            event_bus,
            fail_closed_audit_events,
            stage,
        )
    }

    #[cfg(not(any(feature = "as2", feature = "as4")))]
    {
        let _ = (session, event_bus, fail_closed_audit_events, stage);
        Ok(())
    }
}

/// Fail closed at a strict-interop entry point unless startup validation was
/// bound to the session by [`StrictRuntimeBootstrapToken::bind`].
pub(crate) fn enforce_strict_runtime_bootstrap_for_strict_interop(
    stage: &'static str,
    session: &SessionContext,
    interop: InteropMode,
) -> Result<()> {
    #[cfg(feature = "testing")]
    {
        let _ = (stage, session, interop);
        Ok(())
    }

    #[cfg(not(feature = "testing"))]
    {
        if interop == InteropMode::Strict && !session.strict_runtime_bootstrap_validated() {
            return Err(AsxError::new(
                ErrorCode::PolicyViolation,
                "strict interop entry point requires a strict-runtime bootstrap token; \
                 validate startup with presets::StrictRuntimeBootstrap and bind the session \
                 with StrictRuntimeBootstrapToken::bind(..)",
                ErrorContext::for_session(stage, session),
            ));
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::observability::audit_sink::{
        AuditEvent, AuditSinkDurability, DurableAuditSink, ReplayCursor,
    };
    use crate::storage::BoxFuture;

    #[derive(Debug)]

    struct DurableTestAuditSink;

    impl DurableAuditSink for DurableTestAuditSink {
        fn durability(&self) -> AuditSinkDurability {
            AuditSinkDurability::Durable
        }

        fn has_replay_cursor_integrity_protection(&self) -> bool {
            true
        }

        fn store_event(&self, _event: &AuditEvent) -> Result<()> {
            Ok(())
        }

        fn retrieve_events_from(
            &self,
            _cursor: &ReplayCursor,
            _limit: usize,
        ) -> Result<Vec<AuditEvent>> {
            Ok(Vec::new())
        }

        fn current_cursor(&self) -> Result<ReplayCursor> {
            Ok(ReplayCursor {
                last_event_id: "0".to_string(),
                position: 0,
                last_timestamp: 0,
                integrity_tag_b64: String::new(),
            })
        }

        fn acknowledge_cursor(&self, _cursor: &ReplayCursor) -> Result<()> {
            Ok(())
        }

        fn clear(&self) -> Result<()> {
            Ok(())
        }
    }

    #[derive(Debug)]

    struct NonDurableReconciliation;

    #[derive(Debug)]

    struct NonDurableDedup;

    #[derive(Debug)]

    struct DurableClusterSafeReconciliation;

    #[derive(Debug)]

    struct DurableClusterSafeDedup;

    impl DedupStorage for NonDurableDedup {
        fn is_durable(&self) -> bool {
            false
        }

        fn first_seen<'a>(
            &'a self,
            _idempotency_key: &'a str,
        ) -> BoxFuture<'a, crate::core::Result<bool>> {
            Box::pin(async move { Ok(true) })
        }
    }

    impl DedupStorage for DurableClusterSafeDedup {
        fn is_durable(&self) -> bool {
            true
        }

        fn cluster_safe(&self) -> bool {
            true
        }

        fn first_seen<'a>(
            &'a self,
            _idempotency_key: &'a str,
        ) -> BoxFuture<'a, crate::core::Result<bool>> {
            Box::pin(async move { Ok(true) })
        }
    }

    impl ReconciliationStorage for NonDurableReconciliation {
        fn is_durable(&self) -> bool {
            false
        }

        fn enqueue<'a>(
            &'a self,
            _request: crate::reliability::ReconciliationRequest,
        ) -> BoxFuture<'a, Result<bool>> {
            Box::pin(async move { Ok(false) })
        }

        fn queued_requests(
            &self,
        ) -> BoxFuture<'_, Result<Vec<crate::reliability::ReconciliationRequest>>> {
            Box::pin(async move { Ok(Vec::new()) })
        }

        fn resolve<'a>(&'a self, _idempotency_key: &'a str) -> BoxFuture<'a, Result<bool>> {
            Box::pin(async move { Ok(false) })
        }
    }

    impl ReconciliationStorage for DurableClusterSafeReconciliation {
        fn is_durable(&self) -> bool {
            true
        }

        fn cluster_safe(&self) -> bool {
            true
        }

        fn enqueue<'a>(
            &'a self,
            _request: crate::reliability::ReconciliationRequest,
        ) -> BoxFuture<'a, Result<bool>> {
            Box::pin(async move { Ok(false) })
        }

        fn queued_requests(
            &self,
        ) -> BoxFuture<'_, Result<Vec<crate::reliability::ReconciliationRequest>>> {
            Box::pin(async move { Ok(Vec::new()) })
        }

        fn resolve<'a>(&'a self, _idempotency_key: &'a str) -> BoxFuture<'a, Result<bool>> {
            Box::pin(async move { Ok(false) })
        }
    }

    #[derive(Debug)]

    struct DurableButNonClusterReconciliation;

    impl ReconciliationStorage for DurableButNonClusterReconciliation {
        fn is_durable(&self) -> bool {
            true
        }

        fn enqueue<'a>(
            &'a self,
            _request: crate::reliability::ReconciliationRequest,
        ) -> BoxFuture<'a, Result<bool>> {
            Box::pin(async move { Ok(false) })
        }

        fn queued_requests(
            &self,
        ) -> BoxFuture<'_, Result<Vec<crate::reliability::ReconciliationRequest>>> {
            Box::pin(async move { Ok(Vec::new()) })
        }

        fn resolve<'a>(&'a self, _idempotency_key: &'a str) -> BoxFuture<'a, Result<bool>> {
            Box::pin(async move { Ok(false) })
        }
    }

    #[derive(Debug)]

    struct DurableButNonClusterDedup;

    impl DedupStorage for DurableButNonClusterDedup {
        fn is_durable(&self) -> bool {
            true
        }

        fn first_seen<'a>(
            &'a self,
            _idempotency_key: &'a str,
        ) -> BoxFuture<'a, crate::core::Result<bool>> {
            Box::pin(async move { Ok(true) })
        }
    }

    fn regulated_bus() -> EventBus {
        EventBus::new_regulated(16, std::sync::Arc::new(DurableTestAuditSink)).expect("bus")
    }

    fn ready() -> StrictRuntimeBootstrap<'static> {
        StrictRuntimeBootstrap::new("strict_production_test")
    }

    #[test]
    fn regulated_bus_is_strict_and_durable() {
        let bus = regulated_bus();
        assert_eq!(bus.emission_mode(), EventEmissionMode::StrictTransactional);
        assert!(bus.has_production_durable_audit_sink());
    }

    #[test]
    fn validate_requires_every_component() {
        let bus = regulated_bus();

        let err = ready().validate().expect_err("event bus is required");
        assert_eq!(err.code, ErrorCode::InvalidInput);
        assert!(err.message.contains("event_bus"));

        let err = ready()
            .event_bus(&bus)
            .validate()
            .expect_err("dedup is required");
        assert_eq!(err.code, ErrorCode::InvalidInput);
        assert!(err.message.contains("dedup"));

        let err = ready()
            .event_bus(&bus)
            .dedup(&DurableClusterSafeDedup)
            .validate()
            .expect_err("reconciliation is required");
        assert_eq!(err.code, ErrorCode::InvalidInput);
        assert!(err.message.contains("reconciliation"));
    }

    #[test]
    fn validate_rejects_non_durable_backends() {
        let bus = regulated_bus();

        let err = ready()
            .event_bus(&bus)
            .dedup(&DurableClusterSafeDedup)
            .reconciliation(&NonDurableReconciliation)
            .validate()
            .expect_err("non-durable reconciliation must be rejected");
        assert_eq!(err.code, ErrorCode::ReliabilityFailure);
        assert!(err.message.contains("durable reconciliation backend"));
        assert!(err.message.contains("backend_type="));

        let err = ready()
            .event_bus(&bus)
            .dedup(&NonDurableDedup)
            .reconciliation(&DurableClusterSafeReconciliation)
            .validate()
            .expect_err("non-durable dedup must be rejected");
        assert_eq!(err.code, ErrorCode::ReliabilityFailure);
        assert!(err.message.contains("durable dedup backend"));
    }

    #[test]
    fn validate_rejects_non_cluster_safe_backends() {
        let bus = regulated_bus();

        let err = ready()
            .event_bus(&bus)
            .dedup(&DurableClusterSafeDedup)
            .reconciliation(&DurableButNonClusterReconciliation)
            .validate()
            .expect_err("non-cluster-safe reconciliation must be rejected");
        assert_eq!(err.code, ErrorCode::ReliabilityFailure);
        assert!(err.message.contains("cluster-safe reconciliation backend"));
        assert!(err.message.contains("durable=true"));
        assert!(err.message.contains("cluster_safe=false"));

        let err = ready()
            .event_bus(&bus)
            .dedup(&DurableButNonClusterDedup)
            .reconciliation(&DurableClusterSafeReconciliation)
            .validate()
            .expect_err("non-cluster-safe dedup must be rejected");
        assert_eq!(err.code, ErrorCode::ReliabilityFailure);
        assert!(err.message.contains("cluster-safe dedup backend"));
    }

    #[test]
    fn validate_rejects_best_effort_emission() {
        let bus = EventBus::builder()
            .capacity(16)
            .emission_mode(EventEmissionMode::BestEffort)
            .build()
            .expect("bus");

        let err = ready()
            .event_bus(&bus)
            .dedup(&DurableClusterSafeDedup)
            .reconciliation(&DurableClusterSafeReconciliation)
            .validate()
            .expect_err("best-effort emission must be rejected");

        assert_eq!(err.code, ErrorCode::ReliabilityFailure);
        assert!(err.message.contains("strict event emission mode"));
    }

    /// `StrictWithAuditFallback` is a strict mode that cannot be built without a
    /// production-durable sink, so the startup gate must accept it. Rejecting it
    /// made the mode its own documentation recommends unusable in a regulated
    /// deployment.
    #[test]
    fn validate_accepts_strict_with_audit_fallback() {
        let bus = EventBus::builder()
            .capacity(16)
            .audit_sink(std::sync::Arc::new(DurableTestAuditSink))
            .emission_mode(EventEmissionMode::StrictWithAuditFallback)
            .build()
            .expect("bus");

        ready()
            .event_bus(&bus)
            .dedup(&DurableClusterSafeDedup)
            .reconciliation(&DurableClusterSafeReconciliation)
            .validate()
            .expect("audit-fallback strict mode is a strict mode");
    }

    #[test]
    fn validate_rejects_missing_durable_audit_sink() {
        let bus = EventBus::new(16).expect("bus");

        let err = ready()
            .event_bus(&bus)
            .dedup(&DurableClusterSafeDedup)
            .reconciliation(&DurableClusterSafeReconciliation)
            .validate()
            .expect_err("missing durable audit sink must be rejected");

        assert_eq!(err.code, ErrorCode::ReliabilityFailure);
        assert!(err.message.contains("durable audit sink"));
    }

    #[test]
    fn validate_mints_a_token_on_success() {
        let bus = regulated_bus();
        let token = ready()
            .event_bus(&bus)
            .dedup(&DurableClusterSafeDedup)
            .reconciliation(&DurableClusterSafeReconciliation)
            .validate()
            .expect("startup validation must succeed");

        assert_eq!(token.stage(), "strict_production_test");
        assert!(token.issued_at() <= SystemTime::now());
    }

    #[test]
    fn receive_guards_pass_for_durable_cluster_safe_backends() {
        let session = SessionContext::new("s", "p", "strict").expect("session");
        let bus = regulated_bus();

        enforce_strict_production_runtime_receive_guards(
            "strict_production_test",
            &session,
            &bus,
            true,
            Some(&DurableClusterSafeReconciliation),
            Some(&DurableClusterSafeDedup),
        )
        .expect("durable cluster-safe backends must pass");
    }

    #[cfg(not(feature = "testing"))]
    #[test]
    fn receive_guards_fail_closed_for_non_durable_dedup() {
        let session = SessionContext::new("s", "p", "strict").expect("session");
        let bus = regulated_bus();

        let err = enforce_strict_production_runtime_receive_guards(
            "strict_production_test",
            &session,
            &bus,
            true,
            Some(&DurableClusterSafeReconciliation),
            Some(&NonDurableDedup),
        )
        .expect_err("non-durable dedup must fail closed");

        assert_eq!(err.code, ErrorCode::ReliabilityFailure);
    }

    #[cfg(feature = "testing")]
    #[test]
    fn receive_guards_allow_non_durable_backends_under_testing_feature() {
        let session = SessionContext::new("s", "p", "strict").expect("session");
        let bus = regulated_bus();

        enforce_strict_production_runtime_receive_guards(
            "strict_production_test",
            &session,
            &bus,
            false,
            Some(&NonDurableReconciliation),
            Some(&NonDurableDedup),
        )
        .expect("the testing feature relaxes backend durability");
    }

    #[cfg(all(feature = "as4", not(feature = "testing")))]
    #[test]
    fn clustered_topology_requires_cluster_safe_as4_coordination() {
        struct NotClusterSafe(&'static str);
        impl As4TopologyCoordination for NotClusterSafe {
            fn cluster_safe(&self) -> bool {
                false
            }
            fn topology_component(&self) -> &'static str {
                self.0
            }
        }
        struct ClusterSafe(&'static str);
        impl As4TopologyCoordination for ClusterSafe {
            fn cluster_safe(&self) -> bool {
                true
            }
            fn topology_component(&self) -> &'static str {
                self.0
            }
        }

        let bus = regulated_bus();
        let base = || {
            StrictRuntimeBootstrap::new("strict_production_test")
                .dedup(&DurableClusterSafeDedup)
                .reconciliation(&DurableClusterSafeReconciliation)
                .topology(DeploymentTopology::Clustered)
        };

        // No coordination supplied at all.
        let err = base()
            .event_bus(&bus)
            .validate()
            .expect_err("clustered topology needs a pull store");
        assert_eq!(err.code, ErrorCode::ReliabilityFailure);
        assert!(err.message.contains("pull-store"));

        // Pull store present but process-local.
        let err = base()
            .event_bus(&bus)
            .as4_pull_store(&NotClusterSafe("pull-store"))
            .as4_conversation_gate(&ClusterSafe("conversation-ordering"))
            .validate()
            .expect_err("process-local pull store must be rejected");
        assert!(err.message.contains("pull-store"));

        // Conversation gate present but process-local.
        let err = base()
            .event_bus(&bus)
            .as4_pull_store(&ClusterSafe("pull-store"))
            .as4_conversation_gate(&NotClusterSafe("conversation-ordering"))
            .validate()
            .expect_err("process-local conversation gate must be rejected");
        assert!(err.message.contains("conversation-ordering"));

        // Both cluster-safe.
        let token = base()
            .event_bus(&bus)
            .as4_pull_store(&ClusterSafe("pull-store"))
            .as4_conversation_gate(&ClusterSafe("conversation-ordering"))
            .validate()
            .expect("cluster-safe coordination must pass");
        assert_eq!(token.as4_topology(), Some(DeploymentTopology::Clustered));
    }

    #[cfg(feature = "as4")]
    #[test]
    fn single_node_topology_needs_no_as4_coordination() {
        let bus = regulated_bus();
        let token = ready()
            .event_bus(&bus)
            .dedup(&DurableClusterSafeDedup)
            .reconciliation(&DurableClusterSafeReconciliation)
            .topology(DeploymentTopology::SingleNode)
            .validate()
            .expect("single node needs no distributed coordination");
        assert_eq!(token.as4_topology(), Some(DeploymentTopology::SingleNode));
    }

    #[cfg(not(feature = "testing"))]
    #[test]
    fn strict_interop_rejects_an_unbound_session() {
        let session = SessionContext::new("s", "p", "strict").expect("session");
        let err = enforce_strict_runtime_bootstrap_for_strict_interop(
            "as4_receive_push_sync",
            &session,
            InteropMode::Strict,
        )
        .expect_err("an unbound session must be refused");

        assert_eq!(err.code, ErrorCode::PolicyViolation);
        assert!(err.message.contains("StrictRuntimeBootstrap"));
    }

    #[cfg(not(feature = "testing"))]
    #[test]
    fn strict_interop_accepts_a_token_bound_session() {
        let bus = regulated_bus();
        let token = ready()
            .event_bus(&bus)
            .dedup(&DurableClusterSafeDedup)
            .reconciliation(&DurableClusterSafeReconciliation)
            .validate()
            .expect("startup validation");

        let session = token.bind(&SessionContext::new("s", "p", "strict").expect("session"));
        assert!(session.strict_runtime_bootstrap_validated());

        enforce_strict_runtime_bootstrap_for_strict_interop(
            "as4_receive_push_sync",
            &session,
            InteropMode::Strict,
        )
        .expect("a token-bound session must be accepted");
    }

    #[cfg(feature = "interop-relaxed")]
    #[test]
    fn relaxed_interop_needs_no_token() {
        let session = SessionContext::new("s", "p", "relaxed").expect("session");
        enforce_strict_runtime_bootstrap_for_strict_interop(
            "as4_receive_push_sync",
            &session,
            InteropMode::Relaxed,
        )
        .expect("relaxed interop is not gated on startup validation");
    }
}