syncbat 0.10.0

Sync-first runtime layer for batpak-family operation kits.
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
//! PROVES: S12-SUBSCRIPTION-RUNTIME-OPERATION-STATUS syncbat runtime engine.
//! CATCHES: catch-up/live/watermark/ACK/backpressure/cursor/checkout regressions.

use std::collections::{BTreeMap, BTreeSet};
use std::sync::Arc;
use std::time::Duration;

use batpak::prelude::*;
use batpak::store::Freshness;
use batpak::store::{Store, StoreConfig};
use flume::bounded;
use syncbat::operation_status::{
    OperationStatusFactV1, OperationStatusLifecycle, OperationStatusView,
};
use syncbat::operation_status_sink::{operation_status_entity, StoreOperationStatusSink};
use syncbat::subscription_runtime::error::stream_code;
use syncbat::{
    CompositeSubscriptionRuntime, Core, Ctx, EffectClass, Handler, HandlerResult, OperationName,
    OperationStatusSink, OperationStatusSinkError, OperationStatusStreamCursorV1,
    OperationStatusStreamEnvelopeV1, ReceiptHashPolicy, RuntimeCursor, SessionControl,
    SessionDelivery, SessionPoll, SubscriptionId, SubscriptionRegistry, SubscriptionRoute,
    SubscriptionRuntimeConfig, SubscriptionRuntimeError, SubscriptionSession,
    SubscriptionSessionFactory, SubscriptionStore, SYNCBAT_RECEIPT_EVENT_KIND,
};

const SUBSCRIPTION_ID: &str = "echo.status.v1";
const OPERATION: &str = "mod.a.echo";
const WIRE_SCHEMA: &str = "batpak.operation-status-stream-envelope.v1";

struct EchoHandler;

impl Handler for EchoHandler {
    fn handle(&mut self, input: &[u8], _cx: &mut Ctx<'_>) -> HandlerResult {
        Ok(input.to_vec())
    }
}

struct FailHandler;

impl Handler for FailHandler {
    fn handle(&mut self, _input: &[u8], _cx: &mut Ctx<'_>) -> HandlerResult {
        Err(syncbat::HandlerError::failed("handler failed"))
    }
}

struct FailingStatusSink;

impl OperationStatusSink for FailingStatusSink {
    fn record_fact(&self, _fact: &OperationStatusFactV1) -> Result<(), OperationStatusSinkError> {
        Err(OperationStatusSinkError::new("status sink offline"))
    }
}

fn test_store() -> Result<(Arc<Store>, tempfile::TempDir), Box<dyn std::error::Error>> {
    let dir = tempfile::TempDir::new()?;
    let store = Store::open(
        StoreConfig::new(dir.path())
            .with_enable_checkpoint(false)
            .with_enable_mmap_index(false)
            .with_sync_every_n_events(1),
    )?;
    Ok((Arc::new(store), dir))
}

fn status_entity() -> Result<String, Box<dyn std::error::Error>> {
    Ok(operation_status_entity(OPERATION)?)
}

fn test_registry() -> Result<SubscriptionRegistry, Box<dyn std::error::Error>> {
    let operation = OperationName::new(OPERATION).map_err(|error| {
        std::io::Error::other(format!("PROPERTY: operation name invalid: {error}"))
    })?;
    let entity = status_entity()?;
    let mut registry = SubscriptionRegistry::new();
    registry.insert(
        SubscriptionId::new(SUBSCRIPTION_ID)?,
        SubscriptionRoute::OperationStatus {
            operation,
            entity,
            wire_payload_schema_ref: WIRE_SCHEMA.to_owned(),
            inner_status_schema_ref: None,
            freshness: Freshness::Consistent,
            backpressure_capacity: None,
        },
    )?;
    Ok(registry)
}

fn append_status_fact(
    store: &Store,
    fact: &OperationStatusFactV1,
) -> Result<(), Box<dyn std::error::Error>> {
    let entity = status_entity()?;
    let coord = Coordinate::new(&entity, "scope:operation-status")?;
    let _receipt = store
        .append_typed(&coord, fact)
        .map_err(|error| -> Box<dyn std::error::Error> { Box::new(error) })?;
    Ok(())
}

fn open_session(
    store: Arc<Store>,
    registry: &SubscriptionRegistry,
    resume_cursor: Option<&[u8]>,
    client_window: u32,
) -> Result<Box<dyn SubscriptionSession>, Box<dyn std::error::Error>> {
    let (_control_tx, control_rx) = bounded(4);
    let runtime = CompositeSubscriptionRuntime::new(
        SubscriptionStore::new(store),
        registry.clone(),
        SubscriptionRuntimeConfig::default(),
    );
    runtime
        .open_session(SUBSCRIPTION_ID, resume_cursor, client_window, control_rx)
        .map_err(|error| -> Box<dyn std::error::Error> { Box::new(error) })
}

fn collect_deliveries(
    session: &mut dyn SubscriptionSession,
    max_steps: usize,
) -> Result<Vec<SessionDelivery>, Box<dyn std::error::Error>> {
    let mut out = Vec::new();
    for _ in 0..max_steps {
        match session.poll(Duration::from_millis(250))? {
            SessionPoll::Delivery(delivery) => {
                let done = matches!(
                    delivery,
                    SessionDelivery::Error(_) | SessionDelivery::End(_)
                );
                out.push(delivery);
                if done {
                    break;
                }
            }
            SessionPoll::Blocked => break,
            SessionPoll::Ended => break,
        }
    }
    Ok(out)
}

fn status_generations(
    deliveries: &[SessionDelivery],
) -> Result<Vec<u64>, Box<dyn std::error::Error>> {
    let mut generations = Vec::new();
    for delivery in deliveries {
        if let SessionDelivery::Event(event) = delivery {
            let envelope: OperationStatusStreamEnvelopeV1 =
                batpak::canonical::from_bytes(&event.envelope_bytes)?;
            generations.push(envelope.entity_generation);
        }
    }
    Ok(generations)
}

fn echo_descriptor() -> syncbat::OperationDescriptor {
    syncbat::OperationDescriptor::new(
        OPERATION,
        EffectClass::Inspect,
        "schema.echo.input.v1",
        "schema.echo.output.v1",
        "receipt.echo.v1",
    )
}

#[test]
fn subscription_runtime_operation_status_cursor_v1_roundtrip_and_resume_rules(
) -> Result<(), Box<dyn std::error::Error>> {
    let entity = status_entity()?;
    let beginning = OperationStatusStreamCursorV1::beginning(SUBSCRIPTION_ID, OPERATION, &entity);
    let decoded = OperationStatusStreamCursorV1::decode(&beginning.encode())?;
    assert_eq!(decoded, beginning);
    assert_eq!(decoded.resume_after_entity_generation(), None);

    let after_one = OperationStatusStreamCursorV1::after_entity_generation(
        SUBSCRIPTION_ID,
        OPERATION,
        &entity,
        1,
    );
    assert_eq!(after_one.resume_after_entity_generation(), Some(1));

    let mismatched = OperationStatusStreamCursorV1::after_entity_generation(
        SUBSCRIPTION_ID,
        "other.operation",
        &entity,
        1,
    );
    let err = mismatched.validate_route(SUBSCRIPTION_ID, OPERATION, &entity);
    assert!(matches!(
        err,
        Err(SubscriptionRuntimeError::CursorMismatch { .. })
    ));
    Ok(())
}

#[test]
fn subscription_runtime_operation_status_registry_rejects_duplicate_subscription_id(
) -> Result<(), Box<dyn std::error::Error>> {
    let registry = test_registry()?;
    let mut registry = registry;
    let error = match registry.insert(
        SubscriptionId::new(SUBSCRIPTION_ID)?,
        SubscriptionRoute::OperationStatus {
            operation: OperationName::new(OPERATION)?,
            entity: status_entity()?,
            wire_payload_schema_ref: WIRE_SCHEMA.to_owned(),
            inner_status_schema_ref: None,
            freshness: Freshness::Consistent,
            backpressure_capacity: None,
        },
    ) {
        Ok(()) => {
            return Err(std::io::Error::other(
                "PROPERTY: duplicate subscription id must be rejected",
            )
            .into())
        }
        Err(error) => error,
    };
    assert!(matches!(
        error,
        SubscriptionRuntimeError::DuplicateSubscription { .. }
    ));
    Ok(())
}

#[test]
fn subscription_runtime_operation_status_registry_rejects_invalid_entity_route(
) -> Result<(), Box<dyn std::error::Error>> {
    let mut registry = SubscriptionRegistry::new();
    let error = match registry.insert(
        SubscriptionId::new(SUBSCRIPTION_ID)?,
        SubscriptionRoute::OperationStatus {
            operation: OperationName::new(OPERATION)?,
            entity: "wrong:entity".to_owned(),
            wire_payload_schema_ref: WIRE_SCHEMA.to_owned(),
            inner_status_schema_ref: None,
            freshness: Freshness::Consistent,
            backpressure_capacity: None,
        },
    ) {
        Ok(()) => {
            return Err(std::io::Error::other(
                "PROPERTY: invalid operation-status entity route must be rejected",
            )
            .into())
        }
        Err(error) => error,
    };
    assert!(matches!(
        error,
        SubscriptionRuntimeError::InvalidRoute { .. }
    ));
    Ok(())
}

#[test]
fn subscription_runtime_operation_status_open_rejects_invalid_cursor(
) -> Result<(), Box<dyn std::error::Error>> {
    let (store, _dir) = test_store()?;
    let error = match open_session(Arc::clone(&store), &test_registry()?, Some(&[0xFF; 8]), 128) {
        Ok(_) => {
            return Err(std::io::Error::other("PROPERTY: invalid cursor must be rejected").into())
        }
        Err(error) => error,
    };
    assert!(
        format!("{error}").contains("cursor"),
        "PROPERTY: invalid cursor must surface cursor failure"
    );
    Ok(())
}

#[test]
fn subscription_runtime_operation_status_open_rejects_cursor_mismatch(
) -> Result<(), Box<dyn std::error::Error>> {
    let (store, _dir) = test_store()?;
    let _entity = status_entity()?;
    let mismatched = OperationStatusStreamCursorV1::after_entity_generation(
        SUBSCRIPTION_ID,
        OPERATION,
        "other:entity",
        1,
    );
    let error = match open_session(
        Arc::clone(&store),
        &test_registry()?,
        Some(&mismatched.encode()),
        128,
    ) {
        Ok(_) => {
            return Err(std::io::Error::other("PROPERTY: cursor mismatch must be rejected").into())
        }
        Err(error) => error,
    };
    assert!(
        format!("{error}").contains("mismatch"),
        "PROPERTY: cursor mismatch must surface mismatch failure"
    );
    Ok(())
}

#[test]
fn subscription_runtime_operation_status_checkout_writes_started_and_completed_facts(
) -> Result<(), Box<dyn std::error::Error>> {
    let (store, _dir) = test_store()?;
    let status_sink = StoreOperationStatusSink::new(Arc::clone(&store));
    let mut builder = Core::builder();
    builder.register(echo_descriptor(), EchoHandler)?;
    builder.status_sink(status_sink);
    builder.without_receipts();
    let mut core = builder.build()?;
    core.invoke(OPERATION, b"hello".to_vec())
        .map_err(|error| -> Box<dyn std::error::Error> { Box::new(error) })?;

    let registry = test_registry()?;
    let mut session = open_session(Arc::clone(&store), &registry, None, 128)?;
    let deliveries = collect_deliveries(session.as_mut(), 8)?;
    let envelope_bytes = deliveries
        .iter()
        .find_map(|delivery| match delivery {
            SessionDelivery::Event(event) => Some(event.envelope_bytes.clone()),
            SessionDelivery::Watermark(_) | SessionDelivery::Error(_) | SessionDelivery::End(_) => {
                None
            }
        })
        .ok_or_else(|| std::io::Error::other("PROPERTY: expected catch-up SUB_EVENT"))?;
    let envelope: OperationStatusStreamEnvelopeV1 = batpak::canonical::from_bytes(&envelope_bytes)?;
    let view: OperationStatusView = batpak::canonical::from_bytes(&envelope.status)?;
    assert_eq!(
        view.started_count, 1,
        "PROPERTY: checkout must write started fact"
    );
    assert_eq!(
        view.completed_count, 1,
        "PROPERTY: checkout must write completed fact"
    );
    assert_eq!(
        view.lifecycle,
        OperationStatusLifecycle::Completed,
        "PROPERTY: latest lifecycle must be completed"
    );
    Ok(())
}

#[test]
fn subscription_runtime_operation_status_catch_up_snapshot_and_live_update(
) -> Result<(), Box<dyn std::error::Error>> {
    let (store, _dir) = test_store()?;
    append_status_fact(
        &store,
        &OperationStatusFactV1::started(OPERATION, "receipt.echo.v1"),
    )?;
    append_status_fact(
        &store,
        &OperationStatusFactV1::terminal(
            OPERATION,
            OperationStatusLifecycle::Completed,
            "receipt.echo.v1",
            None,
            None,
            None,
            None,
        ),
    )?;

    let registry = test_registry()?;
    let mut session = open_session(Arc::clone(&store), &registry, None, 128)?;
    let catch_up = collect_deliveries(session.as_mut(), 4)?;
    assert!(
        catch_up
            .iter()
            .any(|delivery| matches!(delivery, SessionDelivery::Event(_))),
        "PROPERTY: catch-up must deliver a materialized status snapshot"
    );

    append_status_fact(
        &store,
        &OperationStatusFactV1::started(OPERATION, "receipt.echo.v1"),
    )?;
    let live = collect_deliveries(session.as_mut(), 4)?;
    assert!(
        live.iter()
            .any(|delivery| matches!(delivery, SessionDelivery::Event(_))),
        "PROPERTY: live wake must deliver newly materialized status updates"
    );
    Ok(())
}

#[test]
fn subscription_runtime_operation_status_denied_and_failed_outcome_updates_view(
) -> Result<(), Box<dyn std::error::Error>> {
    let (store, _dir) = test_store()?;
    append_status_fact(
        &store,
        &OperationStatusFactV1::terminal(
            OPERATION,
            OperationStatusLifecycle::Denied,
            "receipt.echo.v1",
            Some("admission.denied".to_owned()),
            Some("denied by guard".to_owned()),
            None,
            None,
        ),
    )?;
    append_status_fact(
        &store,
        &OperationStatusFactV1::started(OPERATION, "receipt.echo.v1"),
    )?;
    append_status_fact(
        &store,
        &OperationStatusFactV1::terminal(
            OPERATION,
            OperationStatusLifecycle::Failed,
            "receipt.echo.v1",
            Some("fail.test".to_owned()),
            Some("handler failed".to_owned()),
            None,
            None,
        ),
    )?;

    let registry = test_registry()?;
    let mut session = open_session(Arc::clone(&store), &registry, None, 128)?;
    let deliveries = collect_deliveries(session.as_mut(), 8)?;
    let last_event = deliveries
        .iter()
        .filter_map(|delivery| match delivery {
            SessionDelivery::Event(event) => Some(event.envelope_bytes.clone()),
            SessionDelivery::Watermark(_) | SessionDelivery::Error(_) | SessionDelivery::End(_) => {
                None
            }
        })
        .next_back()
        .ok_or_else(|| std::io::Error::other("PROPERTY: expected status SUB_EVENT"))?;
    let envelope: OperationStatusStreamEnvelopeV1 = batpak::canonical::from_bytes(&last_event)?;
    let view: OperationStatusView = batpak::canonical::from_bytes(&envelope.status)?;
    assert_eq!(view.denied_count, 1, "PROPERTY: denied fact must fold");
    assert_eq!(view.failed_count, 1, "PROPERTY: failed fact must fold");
    assert_eq!(
        view.lifecycle,
        OperationStatusLifecycle::Failed,
        "PROPERTY: latest lifecycle must reflect failed terminal fact"
    );
    Ok(())
}

#[test]
fn subscription_runtime_operation_status_denied_after_started_counts_one_attempt(
) -> Result<(), Box<dyn std::error::Error>> {
    let (store, _dir) = test_store()?;
    append_status_fact(
        &store,
        &OperationStatusFactV1::started(OPERATION, "receipt.echo.v1"),
    )?;
    append_status_fact(
        &store,
        &OperationStatusFactV1::terminal(
            OPERATION,
            OperationStatusLifecycle::Denied,
            "receipt.echo.v1",
            Some("effect.violation".to_owned()),
            Some("observed append was not declared".to_owned()),
            None,
            None,
        ),
    )?;

    let mut session = open_session(Arc::clone(&store), &test_registry()?, None, 128)?;
    let deliveries = collect_deliveries(session.as_mut(), 8)?;
    let last_event = deliveries
        .iter()
        .filter_map(|delivery| match delivery {
            SessionDelivery::Event(event) => Some(event.envelope_bytes.clone()),
            SessionDelivery::Watermark(_) | SessionDelivery::Error(_) | SessionDelivery::End(_) => {
                None
            }
        })
        .next_back()
        .ok_or_else(|| std::io::Error::other("PROPERTY: expected status SUB_EVENT"))?;
    let envelope: OperationStatusStreamEnvelopeV1 = batpak::canonical::from_bytes(&last_event)?;
    let view: OperationStatusView = batpak::canonical::from_bytes(&envelope.status)?;
    assert_eq!(
        view.attempts_seen, 1,
        "PROPERTY: started + denied terminal facts describe one checkout attempt"
    );
    assert_eq!(view.started_count, 1, "PROPERTY: started fact must fold");
    assert_eq!(view.denied_count, 1, "PROPERTY: denied fact must fold");
    Ok(())
}

#[test]
fn subscription_runtime_operation_status_resume_honors_cursor_after_entity_generation(
) -> Result<(), Box<dyn std::error::Error>> {
    let (store, _dir) = test_store()?;
    append_status_fact(
        &store,
        &OperationStatusFactV1::started(OPERATION, "receipt.echo.v1"),
    )?;
    append_status_fact(
        &store,
        &OperationStatusFactV1::terminal(
            OPERATION,
            OperationStatusLifecycle::Completed,
            "receipt.echo.v1",
            None,
            None,
            None,
            None,
        ),
    )?;
    append_status_fact(
        &store,
        &OperationStatusFactV1::started(OPERATION, "receipt.echo.v1"),
    )?;

    let entity = status_entity()?;
    let resume = OperationStatusStreamCursorV1::after_entity_generation(
        SUBSCRIPTION_ID,
        OPERATION,
        &entity,
        1,
    );
    let mut session = open_session(
        Arc::clone(&store),
        &test_registry()?,
        Some(&resume.encode()),
        128,
    )?;
    let deliveries = collect_deliveries(session.as_mut(), 8)?;
    let generations = status_generations(&deliveries)?;
    assert!(
        !generations.is_empty(),
        "PROPERTY: resume test requires at least one delivered event"
    );
    assert!(
        generations.iter().all(|gen| *gen > 1),
        "PROPERTY: resume must skip entity_generation <= 1, got {generations:?}"
    );
    Ok(())
}

#[test]
fn subscription_runtime_operation_status_watermark_for_empty_fold(
) -> Result<(), Box<dyn std::error::Error>> {
    let (store, _dir) = test_store()?;
    let entity = status_entity()?;
    let coord = Coordinate::new(&entity, "scope:operation-status")?;
    let payload = serde_json::json!({"ignored": true});
    let _receipt = store.append(&coord, SYNCBAT_RECEIPT_EVENT_KIND, &payload)?;

    let mut session = open_session(Arc::clone(&store), &test_registry()?, None, 128)?;
    let deliveries = collect_deliveries(session.as_mut(), 4)?;
    assert!(
        deliveries
            .iter()
            .any(|delivery| matches!(delivery, SessionDelivery::Watermark(_))),
        "PROPERTY: generation advance with empty fold must emit SUB_WATERMARK"
    );
    Ok(())
}

#[test]
fn subscription_runtime_operation_status_slow_consumer_closes_with_err(
) -> Result<(), Box<dyn std::error::Error>> {
    let (store, _dir) = test_store()?;
    append_status_fact(
        &store,
        &OperationStatusFactV1::started(OPERATION, "receipt.echo.v1"),
    )?;

    let operation = OperationName::new(OPERATION)?;
    let entity = status_entity()?;
    let mut registry = SubscriptionRegistry::new();
    registry.insert(
        SubscriptionId::new(SUBSCRIPTION_ID)?,
        SubscriptionRoute::OperationStatus {
            operation,
            entity,
            wire_payload_schema_ref: WIRE_SCHEMA.to_owned(),
            inner_status_schema_ref: None,
            freshness: Freshness::Consistent,
            backpressure_capacity: Some(1),
        },
    )?;
    let mut session = open_session(Arc::clone(&store), &registry, None, 128)?;
    let first = collect_deliveries(session.as_mut(), 1)?;
    assert!(
        first
            .iter()
            .any(|delivery| matches!(delivery, SessionDelivery::Event(_))),
        "PROPERTY: slow-consumer test requires one in-flight delivery"
    );
    append_status_fact(
        &store,
        &OperationStatusFactV1::terminal(
            OPERATION,
            OperationStatusLifecycle::Completed,
            "receipt.echo.v1",
            None,
            None,
            None,
            None,
        ),
    )?;
    append_status_fact(
        &store,
        &OperationStatusFactV1::started(OPERATION, "receipt.echo.v1"),
    )?;
    let deliveries = collect_deliveries(session.as_mut(), 8)?;
    assert!(
        deliveries.iter().any(|delivery| matches!(
            delivery,
            SessionDelivery::Error(error) if error.code == stream_code::SLOW_CONSUMER
        )),
        "PROPERTY: bounded queue must fail closed with slow_consumer"
    );
    Ok(())
}

#[test]
fn subscription_runtime_operation_status_cancel_emits_client_cancelled_end(
) -> Result<(), Box<dyn std::error::Error>> {
    let (store, _dir) = test_store()?;
    let (control_tx, control_rx) = bounded(4);
    let runtime = CompositeSubscriptionRuntime::new(
        SubscriptionStore::new(Arc::clone(&store)),
        test_registry()?,
        SubscriptionRuntimeConfig::default(),
    );
    let mut session = runtime.open_session(SUBSCRIPTION_ID, None, 128, control_rx)?;
    control_tx
        .send(SessionControl::Cancel)
        .map_err(|_| std::io::Error::other("PROPERTY: control channel send failed"))?;
    let deliveries = collect_deliveries(session.as_mut(), 4)?;
    assert!(deliveries.iter().any(|delivery| matches!(
        delivery,
        SessionDelivery::End(end) if end.reason_code == stream_code::CLIENT_CANCELLED
    )));
    Ok(())
}

#[test]
fn subscription_runtime_operation_status_cumulative_ack_frees_delivery_window(
) -> Result<(), Box<dyn std::error::Error>> {
    let (store, _dir) = test_store()?;
    append_status_fact(
        &store,
        &OperationStatusFactV1::started(OPERATION, "receipt.echo.v1"),
    )?;
    append_status_fact(
        &store,
        &OperationStatusFactV1::terminal(
            OPERATION,
            OperationStatusLifecycle::Completed,
            "receipt.echo.v1",
            None,
            None,
            None,
            None,
        ),
    )?;

    let operation = OperationName::new(OPERATION)?;
    let entity = status_entity()?;
    let mut registry = SubscriptionRegistry::new();
    registry.insert(
        SubscriptionId::new(SUBSCRIPTION_ID)?,
        SubscriptionRoute::OperationStatus {
            operation,
            entity,
            wire_payload_schema_ref: WIRE_SCHEMA.to_owned(),
            inner_status_schema_ref: None,
            freshness: Freshness::Consistent,
            backpressure_capacity: Some(1),
        },
    )?;
    let (control_tx, control_rx) = bounded(4);
    let runtime = CompositeSubscriptionRuntime::new(
        SubscriptionStore::new(Arc::clone(&store)),
        registry,
        SubscriptionRuntimeConfig::default(),
    );
    let mut session = runtime.open_session(SUBSCRIPTION_ID, None, 128, control_rx)?;
    let first = collect_deliveries(session.as_mut(), 1)?;
    let first_event = first
        .iter()
        .find_map(|delivery| match delivery {
            SessionDelivery::Event(event) => Some(event.clone()),
            SessionDelivery::Watermark(_) | SessionDelivery::Error(_) | SessionDelivery::End(_) => {
                None
            }
        })
        .ok_or_else(|| std::io::Error::other("PROPERTY: expected first SUB_EVENT delivery"))?;
    control_tx
        .send(SessionControl::Ack {
            delivery_index: first_event.delivery_index,
            cursor: first_event.cursor_after.clone(),
        })
        .map_err(|_| std::io::Error::other("PROPERTY: ack send failed"))?;
    append_status_fact(
        &store,
        &OperationStatusFactV1::started(OPERATION, "receipt.echo.v1"),
    )?;
    let second = collect_deliveries(session.as_mut(), 4)?;
    assert!(
        second
            .iter()
            .any(|delivery| matches!(delivery, SessionDelivery::Event(_))),
        "PROPERTY: cumulative ACK must free the bounded delivery window"
    );
    Ok(())
}

#[test]
fn subscription_runtime_operation_status_unknown_route_rejected_at_open(
) -> Result<(), Box<dyn std::error::Error>> {
    let (store, _dir) = test_store()?;
    let (_control_tx, control_rx) = bounded(4);
    let runtime = CompositeSubscriptionRuntime::new(
        SubscriptionStore::new(Arc::clone(&store)),
        SubscriptionRegistry::new(),
        SubscriptionRuntimeConfig::default(),
    );
    let error = match runtime.open_session("missing.status.v1", None, 128, control_rx) {
        Ok(_) => {
            return Err(std::io::Error::other("PROPERTY: unknown route must be rejected").into())
        }
        Err(error) => error,
    };
    assert!(matches!(
        error,
        SubscriptionRuntimeError::UnknownSubscription { .. }
    ));
    Ok(())
}

#[test]
fn subscription_runtime_operation_status_status_sink_failure_before_handler_fails_closed(
) -> Result<(), Box<dyn std::error::Error>> {
    let mut builder = Core::builder();
    builder.register(echo_descriptor(), EchoHandler)?;
    builder.status_sink(FailingStatusSink);
    builder.without_receipts();
    let mut core = builder.build()?;
    let error =
        match core.invoke(OPERATION, b"hello".to_vec()) {
            Ok(_) => return Err(std::io::Error::other(
                "PROPERTY: status sink failure must fail closed before handler side effects matter",
            )
            .into()),
            Err(error) => error,
        };
    assert!(matches!(error, syncbat::RuntimeError::StatusSink { .. }));
    Ok(())
}

#[test]
fn subscription_runtime_operation_status_missing_handler_records_failed_terminal_status(
) -> Result<(), Box<dyn std::error::Error>> {
    let (store, _dir) = test_store()?;
    let mut descriptors = BTreeMap::new();
    descriptors.insert(OPERATION.to_owned(), echo_descriptor());
    let mut core = crate::core::Core {
        descriptors,
        handlers: BTreeMap::new(),
        admission_guard: None,
        receipt_sink: None,
        status_sink: Some(Arc::new(StoreOperationStatusSink::new(Arc::clone(&store)))),
        receipt_hash_policy: ReceiptHashPolicy::default(),
        effect_backend: None,
        granted_capabilities: BTreeSet::new(),
    };

    let error = match core.invoke(OPERATION, b"hello".to_vec()) {
        Ok(_) => {
            return Err(std::io::Error::other(
                "PROPERTY: descriptor without handler must not complete",
            )
            .into())
        }
        Err(error) => error,
    };
    assert!(matches!(
        error,
        syncbat::RuntimeError::MissingHandler { .. }
    ));

    let mut session = open_session(Arc::clone(&store), &test_registry()?, None, 128)?;
    let deliveries = collect_deliveries(session.as_mut(), 8)?;
    let last_event = deliveries
        .iter()
        .filter_map(|delivery| match delivery {
            SessionDelivery::Event(event) => Some(event.envelope_bytes.clone()),
            SessionDelivery::Watermark(_) | SessionDelivery::Error(_) | SessionDelivery::End(_) => {
                None
            }
        })
        .next_back()
        .ok_or_else(|| std::io::Error::other("PROPERTY: expected missing-handler status"))?;
    let envelope: OperationStatusStreamEnvelopeV1 = batpak::canonical::from_bytes(&last_event)?;
    let view: OperationStatusView = batpak::canonical::from_bytes(&envelope.status)?;
    assert_eq!(
        view.started_count, 1,
        "PROPERTY: missing-handler branch starts attempt"
    );
    assert_eq!(
        view.failed_count, 1,
        "PROPERTY: missing-handler branch records terminal failure"
    );
    assert_eq!(
        view.attempts_seen, 1,
        "PROPERTY: missing-handler branch is one attempt"
    );
    assert_eq!(
        view.last_code.as_deref(),
        Some("missing_handler"),
        "PROPERTY: missing-handler status must carry stable class"
    );
    Ok(())
}

#[test]
fn subscription_runtime_operation_status_checkout_failed_handler_updates_view(
) -> Result<(), Box<dyn std::error::Error>> {
    let (store, _dir) = test_store()?;
    let status_sink = StoreOperationStatusSink::new(Arc::clone(&store));
    let mut builder = Core::builder();
    builder.register(echo_descriptor(), FailHandler)?;
    builder.status_sink(status_sink);
    builder.without_receipts();
    let mut core = builder.build()?;
    let _ = core.invoke(OPERATION, b"bad".to_vec());

    let registry = test_registry()?;
    let mut session = open_session(Arc::clone(&store), &registry, None, 128)?;
    let deliveries = collect_deliveries(session.as_mut(), 8)?;
    let envelope_bytes = deliveries
        .iter()
        .find_map(|delivery| match delivery {
            SessionDelivery::Event(event) => Some(event.envelope_bytes.clone()),
            SessionDelivery::Watermark(_) | SessionDelivery::Error(_) | SessionDelivery::End(_) => {
                None
            }
        })
        .ok_or_else(|| std::io::Error::other("PROPERTY: expected failed checkout status"))?;
    let envelope: OperationStatusStreamEnvelopeV1 = batpak::canonical::from_bytes(&envelope_bytes)?;
    let view: OperationStatusView = batpak::canonical::from_bytes(&envelope.status)?;
    assert_eq!(view.failed_count, 1, "PROPERTY: failed checkout must fold");
    assert_eq!(
        view.lifecycle,
        OperationStatusLifecycle::Failed,
        "PROPERTY: failed checkout must surface failed lifecycle"
    );
    Ok(())
}

#[test]
fn subscription_runtime_operation_status_runtime_cursor_is_opaque_on_wire_path(
) -> Result<(), Box<dyn std::error::Error>> {
    let entity = status_entity()?;
    let cursor = OperationStatusStreamCursorV1::after_entity_generation(
        SUBSCRIPTION_ID,
        OPERATION,
        &entity,
        4,
    );
    let runtime = RuntimeCursor::from_bytes(cursor.encode().to_vec());
    let decoded = OperationStatusStreamCursorV1::decode(runtime.as_bytes())?;
    assert_eq!(decoded, cursor);
    Ok(())
}

#[test]
fn subscription_runtime_operation_status_client_disconnect_without_cancel_ends_session(
) -> Result<(), Box<dyn std::error::Error>> {
    let (store, _dir) = test_store()?;
    let (control_tx, control_rx) = bounded(4);
    let runtime = CompositeSubscriptionRuntime::new(
        SubscriptionStore::new(Arc::clone(&store)),
        test_registry()?,
        SubscriptionRuntimeConfig::default(),
    );
    let mut session = runtime.open_session(SUBSCRIPTION_ID, None, 128, control_rx)?;
    control_tx
        .send(SessionControl::Disconnected)
        .map_err(|_| std::io::Error::other("PROPERTY: disconnect send failed"))?;
    let poll = session.poll(Duration::from_millis(250))?;
    assert!(
        matches!(poll, SessionPoll::Ended),
        "PROPERTY: disconnect without cancel must end session"
    );
    Ok(())
}