routers_realtime 0.5.0

A Demonstration for Real-Time Map Matching
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
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
//! Ordered raw reader: envelope validation and duplicate suppression.
//!
//! Classifies one raw partition delivery as a new observation to queue, poison
//! to ack-and-forget, or a duplicate to suppress. It performs no I/O and acks
//! nothing. Identity must agree across the delivery subject, the vehicle id's
//! partition, and the reader's own; a mismatch is poison.

use async_nats::HeaderMap;
use routers_network::Entry;
use tokio::time::Instant;
use web_time::SystemTime;

use crate::bus::Wire;
use crate::bus::adapter::{AckHandle, Delivery};
use crate::event::Payload;
use crate::ingress;
use crate::orchestrator::frontier::{FrontierTracker, Observed};
use crate::orchestrator::scheduler::{
    Enqueue, EnqueueReadiness, PendingObservation, PublishedAtMicros, Scheduler,
};
use crate::partition;
use crate::protocol::ids::{ObservationId, SCHEMA_VERSION, SchemaVersion, headers};
use crate::topology::partition_of_subject;

/// Why a raw message can never become an observation and is acked-and-forgotten.
///
/// Every variant is a permanent fault of this message, so the worker acks and
/// completes its sequence rather than looping. Labels are bounded for metrics.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PoisonReason {
    /// The payload bytes did not decode as a protobuf [`Payload`].
    Decode,
    /// The delivery subject's partition disagreed with the reader's (or was
    /// absent), so the message was misrouted.
    SubjectPartition {
        /// The partition the reader owns.
        expected: u16,
        /// The partition parsed from the subject, if any.
        got: Option<u16>,
    },
    /// The vehicle id hashes to a different partition than the one that
    /// delivered it — a producer that published to the wrong subject.
    VehiclePartition {
        /// The partition the message was delivered under (the reader's).
        subject: u16,
        /// The partition the vehicle id actually hashes to.
        vehicle: u16,
    },
    /// A stamped wire-schema header did not match this build's
    /// [`SCHEMA_VERSION`].
    Schema {
        /// The version a peer stamped, if one was present and well-formed.
        got: Option<u32>,
    },
    /// The payload failed a basic sanity check (zero vehicle id, non-finite or
    /// out-of-range coordinate).
    Invalid {
        /// The bounded [`ingress::IngressError::kind`] of the failing check.
        kind: &'static str,
    },
    /// The broker publication instant was absent or outside the representable
    /// non-negative Unix-microsecond range required for stable job identity.
    PublishTime,
}

impl PoisonReason {
    /// A bounded, stable metric label for this reason.
    #[must_use]
    pub fn label(&self) -> &'static str {
        match self {
            PoisonReason::Decode => "decode",
            PoisonReason::SubjectPartition { .. } => "subject_partition",
            PoisonReason::VehiclePartition { .. } => "vehicle_partition",
            PoisonReason::Schema { .. } => "schema",
            // Already a bounded `IngressError::kind`, so it doubles as the label.
            PoisonReason::Invalid { kind } => kind,
            PoisonReason::PublishTime => "publish_time",
        }
    }
}

/// Why a valid raw message was suppressed rather than queued.
///
/// Every suppression is terminal for this sequence once acknowledged.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SuppressReason {
    /// The sequence is at or below the completion frontier — an already-terminal
    /// redelivery.
    BehindFrontier,
    /// The loaded checkpoint already covers this observation's input.
    Committed,
}

impl SuppressReason {
    /// A bounded, stable metric label for this reason.
    #[must_use]
    pub fn label(&self) -> &'static str {
        match self {
            SuppressReason::BehindFrontier => "behind_frontier",
            SuppressReason::Committed => "committed",
        }
    }
}

/// Why a valid raw delivery must remain broker-owned for later redelivery.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DeferReason {
    /// The vehicle's bounded pending FIFO is full.
    PendingFull,
    /// The original delivery is pending or in flight and owns durability.
    DuplicateOwned,
}

impl DeferReason {
    /// A bounded, stable metric/log label.
    #[must_use]
    pub fn label(self) -> &'static str {
        match self {
            DeferReason::PendingFull => "pending_full",
            DeferReason::DuplicateOwned => "duplicate_owned",
        }
    }
}

/// The verdict on one raw delivery.
///
/// [`Queued`](RawDisposition::Queued) hands no ack handle back — the scheduler
/// owns it; the other variants return the handle for the worker to ack.
#[derive(Debug)]
#[must_use = "a disposition may carry an ack handle that must be acknowledged"]
pub enum RawDisposition<H: AckHandle> {
    /// The observation was appended to its vehicle's FIFO; the scheduler holds
    /// the handle.
    Queued {
        /// The vehicle's pending depth after the append.
        depth: usize,
    },
    /// The message can never be processed; ack it and complete its sequence.
    Poison {
        /// The handle to acknowledge the poison delivery.
        handle: H,
        /// Why it is poison.
        reason: PoisonReason,
    },
    /// The message is valid but already durably decided; acknowledge it and
    /// complete its sequence.
    Suppressed {
        /// The handle to acknowledge the suppressed delivery.
        handle: H,
        /// Why it was suppressed.
        reason: SuppressReason,
    },
    /// The message is valid but local capacity is exhausted or its original
    /// delivery still owns durability; negatively acknowledge it without
    /// completing the frontier hole.
    Deferred {
        /// The handle to negatively acknowledge for broker redelivery.
        handle: H,
        /// Why local processing cannot currently own the message.
        reason: DeferReason,
    },
}

impl<H: AckHandle> RawDisposition<H> {
    /// Whether the worker should mark this message's sequence complete on the
    /// frontier after acking it.
    ///
    /// Queued and deferred messages are not terminal; everything else is.
    #[must_use]
    pub fn is_terminal(&self) -> bool {
        match self {
            RawDisposition::Poison { .. } => true,
            RawDisposition::Suppressed { .. } => true,
            RawDisposition::Queued { .. } => false,
            RawDisposition::Deferred { .. } => false,
        }
    }
}

/// A raw message still in its wire form, for the path where decoding may fail.
pub struct RawEnvelope<'a, H: AckHandle> {
    /// The concrete subject the message was delivered on.
    pub subject: &'a str,
    /// The message headers, if any were carried (the schema header lives here).
    pub headers: Option<&'a HeaderMap>,
    /// The undecoded payload bytes.
    pub bytes: &'a [u8],
    /// Stable producer publish time decoded by the bus adapter, if stamped.
    pub sent_at: Option<SystemTime>,
    /// The acknowledgement handle for this delivery.
    pub handle: H,
}

/// A decoded raw delivery passed through the reader's shared validation tail.
struct DecodedRaw<H: AckHandle> {
    schema: Option<SchemaVersion>,
    payload: Payload,
    published_at: Option<SystemTime>,
    handle: H,
}

/// The per-partition raw reader.
///
/// It holds only its partition; the scheduler and frontier tracker are passed
/// in per call, so it is trivially shareable and every decision is pure.
#[derive(Clone, Copy, Debug)]
pub struct RawReader {
    partition: u16,
}

impl RawReader {
    /// A reader for `partition`.
    #[must_use]
    pub fn new(partition: u16) -> Self {
        Self { partition }
    }

    /// The partition this reader owns.
    #[must_use]
    pub fn partition(&self) -> u16 {
        self.partition
    }

    /// Classify an already-decoded delivery.
    ///
    /// A [`Delivery`] carries no headers, so the schema check is skipped as an
    /// absent (optional) header. Use [`RawReader::admit_bytes`] on the raw path.
    pub fn admit<E, H>(
        &self,
        scheduler: &mut Scheduler<E, H>,
        tracker: &mut FrontierTracker,
        delivery: Delivery<Payload, H>,
        now: Instant,
    ) -> RawDisposition<H>
    where
        E: Entry,
        H: AckHandle,
    {
        let observed = tracker.observe(delivery.handle.sequence());
        let got = partition_of_subject(&delivery.subject);
        if got != Some(self.partition) {
            return RawDisposition::Poison {
                handle: delivery.handle,
                reason: PoisonReason::SubjectPartition {
                    expected: self.partition,
                    got,
                },
            };
        }
        self.admit_payload(
            scheduler,
            tracker,
            DecodedRaw {
                schema: None,
                payload: delivery.item,
                published_at: delivery.sent_at,
                handle: delivery.handle,
            },
            now,
            observed,
        )
    }

    /// Classify a raw, still-encoded delivery, decoding it here so a decode
    /// failure poisons cleanly with the ack handle intact.
    pub fn admit_bytes<E, H>(
        &self,
        scheduler: &mut Scheduler<E, H>,
        tracker: &mut FrontierTracker,
        envelope: RawEnvelope<'_, H>,
        now: Instant,
    ) -> RawDisposition<H>
    where
        E: Entry,
        H: AckHandle,
    {
        let RawEnvelope {
            subject,
            headers,
            bytes,
            sent_at,
            handle,
        } = envelope;
        let observed = tracker.observe(handle.sequence());

        let got = partition_of_subject(subject);
        if got != Some(self.partition) {
            return RawDisposition::Poison {
                handle,
                reason: PoisonReason::SubjectPartition {
                    expected: self.partition,
                    got,
                },
            };
        }

        let payload = match Payload::decode(bytes) {
            Ok(payload) => payload,
            Err(_) => {
                return RawDisposition::Poison {
                    handle,
                    reason: PoisonReason::Decode,
                };
            }
        };

        self.admit_payload(
            scheduler,
            tracker,
            DecodedRaw {
                schema: headers.and_then(headers::schema_of),
                payload,
                published_at: sent_at,
                handle,
            },
            now,
            observed,
        )
    }

    /// The shared tail: identity, schema, and sanity, then frontier and
    /// scheduler suppression (the subject is already validated).
    fn admit_payload<E, H>(
        &self,
        scheduler: &mut Scheduler<E, H>,
        tracker: &mut FrontierTracker,
        raw: DecodedRaw<H>,
        now: Instant,
        observed: Observed,
    ) -> RawDisposition<H>
    where
        E: Entry,
        H: AckHandle,
    {
        let DecodedRaw {
            schema,
            payload,
            published_at,
            handle,
        } = raw;
        let vehicle_partition = partition::partition_of(payload.vehicle_id) as u16;
        if vehicle_partition != self.partition {
            return RawDisposition::Poison {
                handle,
                reason: PoisonReason::VehiclePartition {
                    subject: self.partition,
                    vehicle: vehicle_partition,
                },
            };
        }

        if let Some(version) = schema
            && version != SCHEMA_VERSION
        {
            return RawDisposition::Poison {
                handle,
                reason: PoisonReason::Schema {
                    got: Some(version.0),
                },
            };
        }

        if let Err(error) = ingress::validate(&payload) {
            return RawDisposition::Poison {
                handle,
                reason: PoisonReason::Invalid { kind: error.kind() },
            };
        }

        let Some(published_at) = published_at.and_then(PublishedAtMicros::from_system_time) else {
            return RawDisposition::Poison {
                handle,
                reason: PoisonReason::PublishTime,
            };
        };

        let sequence = handle.sequence();
        if sequence <= tracker.frontier() {
            return RawDisposition::Suppressed {
                handle,
                reason: SuppressReason::BehindFrontier,
            };
        }

        let observation = PendingObservation {
            id: ObservationId {
                partition: self.partition,
                sequence,
            },
            payload,
            published_at,
            handle,
            received: now,
        };
        let readiness = scheduler.enqueue_readiness(&observation);
        match observed {
            Observed::BehindFrontier => {
                return RawDisposition::Suppressed {
                    handle: observation.handle,
                    reason: SuppressReason::BehindFrontier,
                };
            }
            Observed::Duplicate if readiness == EnqueueReadiness::Coalesced => {
                return RawDisposition::Deferred {
                    handle: observation.handle,
                    reason: DeferReason::DuplicateOwned,
                };
            }
            // A deferred sequence already reserves a frontier hole but owns no
            // scheduler slot. Its redelivery must be allowed to enqueue once
            // capacity returns.
            Observed::Duplicate | Observed::New => {}
        }
        if matches!(readiness, EnqueueReadiness::Backpressured { .. }) {
            return RawDisposition::Deferred {
                handle: observation.handle,
                reason: DeferReason::PendingFull,
            };
        }
        match scheduler.enqueue(observation, now) {
            Enqueue::Queued { depth } => RawDisposition::Queued { depth },
            Enqueue::Coalesced(handle) => RawDisposition::Deferred {
                handle,
                reason: DeferReason::DuplicateOwned,
            },
            Enqueue::Committed(handle) => RawDisposition::Suppressed {
                handle,
                reason: SuppressReason::Committed,
            },
            Enqueue::Overflow { handle, .. } => RawDisposition::Deferred {
                handle,
                reason: DeferReason::PendingFull,
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use alloc::sync::Arc;
    use core::time::Duration;
    use std::sync::Mutex;

    use chrono::{TimeZone, Utc};
    use geo::Point;
    use routers_network::mock::MockEntryId;

    use super::*;
    use crate::event::VehicleId;
    use crate::orchestrator::scheduler::{CheckpointState, SchedulerConfig};
    use crate::protocol::ids::{GraphVersion, RegionId, Revision, SCHEMA_VERSION, SegmentId};
    use crate::store::checkpoint::{PartitionFrontier, VehicleCheckpoint};
    use web_time::UNIX_EPOCH;

    use routers_transition::matcher::Trip;

    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    enum AckOp {
        Ack(u64),
        Nak(u64),
    }

    #[derive(Clone, Debug)]
    struct TestAck {
        seq: u64,
        log: Arc<Mutex<Vec<AckOp>>>,
    }

    impl AckHandle for TestAck {
        async fn ack(self) -> anyhow::Result<()> {
            self.log.lock().unwrap().push(AckOp::Ack(self.seq));
            Ok(())
        }

        async fn nak(self, _delay: Option<Duration>) -> anyhow::Result<()> {
            self.log.lock().unwrap().push(AckOp::Nak(self.seq));
            Ok(())
        }

        fn sequence(&self) -> u64 {
            self.seq
        }

        fn deliveries(&self) -> u32 {
            1
        }
    }

    /// Vehicle 1 hashes to partition 485 (pinned in `partition.rs`); tests rely
    /// on that pairing.
    const PARTITION: u16 = 485;
    const VEHICLE: u64 = 1;

    struct Fixture {
        log: Arc<Mutex<Vec<AckOp>>>,
        region: RegionId,
        now: Instant,
    }

    impl Fixture {
        fn new() -> Self {
            Self {
                log: Arc::new(Mutex::new(Vec::new())),
                region: RegionId::new("r1").unwrap(),
                now: Instant::now(),
            }
        }

        fn ack(&self, seq: u64) -> TestAck {
            TestAck {
                seq,
                log: self.log.clone(),
            }
        }

        fn payload(&self, vehicle: u64, lon: f64, lat: f64) -> Payload {
            Payload {
                vehicle_id: VehicleId(vehicle),
                timestamp: Utc.timestamp_micros(1_775_000_000_000_000).unwrap(),
                point: Point::new(lon, lat),
            }
        }

        fn good(&self) -> Payload {
            self.payload(VEHICLE, 151.2093, -33.8688)
        }

        fn envelope<'a>(
            &self,
            subject: &'a str,
            headers: Option<&'a HeaderMap>,
            bytes: &'a [u8],
            seq: u64,
        ) -> RawEnvelope<'a, TestAck> {
            RawEnvelope {
                subject,
                headers,
                bytes,
                sent_at: Some(UNIX_EPOCH + Duration::from_secs(1)),
                handle: self.ack(seq),
            }
        }

        fn present_checkpoint(&self, last_seq: u64) -> CheckpointState<MockEntryId> {
            CheckpointState::Present(VehicleCheckpoint {
                trip: Trip::new(),
                last_input: ObservationId {
                    partition: PARTITION,
                    sequence: last_seq,
                },
                revision: Revision(last_seq),
                segment: SegmentId(1),
                finalized_through: None,
                graph: GraphVersion::new("g1").unwrap(),
                schema: SCHEMA_VERSION,
                region: self.region.clone(),
                routing_version: 1,
            })
        }
    }

    fn scheduler() -> Scheduler<MockEntryId, TestAck> {
        Scheduler::new(SchedulerConfig::default())
    }

    fn tracker(fx: &Fixture) -> FrontierTracker {
        FrontierTracker::new(PARTITION, None, fx.now)
    }

    fn subject() -> String {
        crate::topology::raw_subject(u64::from(PARTITION))
    }

    #[test]
    fn happy_path_queues_and_observes() {
        let fx = Fixture::new();
        let reader = RawReader::new(PARTITION);
        let mut sched = scheduler();
        let mut frontier = tracker(&fx);
        let bytes = fx.good().encode().unwrap();
        let subject = subject();

        let disposition = reader.admit_bytes(
            &mut sched,
            &mut frontier,
            fx.envelope(&subject, None, &bytes, 10),
            fx.now,
        );

        assert!(matches!(disposition, RawDisposition::Queued { depth: 1 }));
        assert!(!disposition.is_terminal());
        assert_eq!(sched.stats().pending, 1);
        assert_eq!(frontier.outstanding(), 1);
        assert_eq!(frontier.oldest_outstanding(), Some(10));
    }

    #[test]
    fn decoded_delivery_path_queues() {
        let fx = Fixture::new();
        let reader = RawReader::new(PARTITION);
        let mut sched = scheduler();
        let mut frontier = tracker(&fx);

        let delivery = Delivery {
            item: fx.good(),
            handle: fx.ack(4),
            subject: subject(),
            msg_id: None,
            headers: HeaderMap::new(),
            sent_at: Some(UNIX_EPOCH + Duration::from_secs(1)),
            redelivered: false,
        };

        let disposition = reader.admit(&mut sched, &mut frontier, delivery, fx.now);
        assert!(matches!(disposition, RawDisposition::Queued { depth: 1 }));
        assert_eq!(sched.stats().pending, 1);
    }

    #[test]
    fn wrong_subject_partition_is_poison() {
        let fx = Fixture::new();
        let reader = RawReader::new(PARTITION);
        let mut sched = scheduler();
        let mut frontier = tracker(&fx);
        let bytes = fx.good().encode().unwrap();
        let subject = crate::topology::raw_subject(486);

        let disposition = reader.admit_bytes(
            &mut sched,
            &mut frontier,
            fx.envelope(&subject, None, &bytes, 1),
            fx.now,
        );

        match disposition {
            RawDisposition::Poison {
                handle,
                reason: PoisonReason::SubjectPartition { expected, got },
            } => {
                assert_eq!(handle.sequence(), 1);
                assert_eq!(expected, PARTITION);
                assert_eq!(got, Some(486));
            }
            other => panic!("expected SubjectPartition poison, got {other:?}"),
        }
        assert_eq!(
            frontier.outstanding(),
            1,
            "even poison reserves a frontier hole until the worker acks it"
        );
        assert_eq!(sched.stats().pending, 0);
    }

    #[test]
    fn missing_publish_time_is_poison_after_reserving_the_frontier_hole() {
        let fx = Fixture::new();
        let reader = RawReader::new(PARTITION);
        let mut sched = scheduler();
        let mut frontier = tracker(&fx);
        let bytes = fx.good().encode().unwrap();
        let subject = subject();
        let mut envelope = fx.envelope(&subject, None, &bytes, 3);
        envelope.sent_at = None;

        let disposition = reader.admit_bytes(&mut sched, &mut frontier, envelope, fx.now);

        assert!(matches!(
            disposition,
            RawDisposition::Poison {
                reason: PoisonReason::PublishTime,
                ..
            }
        ));
        assert_eq!(frontier.oldest_outstanding(), Some(3));
        assert_eq!(sched.stats().pending, 0);
    }

    #[test]
    fn unparseable_subject_is_poison() {
        let fx = Fixture::new();
        let reader = RawReader::new(PARTITION);
        let mut sched = scheduler();
        let mut frontier = tracker(&fx);
        let bytes = fx.good().encode().unwrap();

        let disposition = reader.admit_bytes(
            &mut sched,
            &mut frontier,
            fx.envelope("events.raw.p.not-a-number", None, &bytes, 1),
            fx.now,
        );
        assert!(matches!(
            disposition,
            RawDisposition::Poison {
                reason: PoisonReason::SubjectPartition { got: None, .. },
                ..
            }
        ));
    }

    #[test]
    fn undecodable_payload_is_poison() {
        let fx = Fixture::new();
        let reader = RawReader::new(PARTITION);
        let mut sched = scheduler();
        let mut frontier = tracker(&fx);
        let subject = subject();
        // A truncated length-delimited field: valid tag, length 127, no data.
        let bytes = [0x0a, 0x7f];

        let disposition = reader.admit_bytes(
            &mut sched,
            &mut frontier,
            fx.envelope(&subject, None, &bytes, 2),
            fx.now,
        );

        assert!(disposition.is_terminal());
        match disposition {
            RawDisposition::Poison {
                handle,
                reason: PoisonReason::Decode,
            } => assert_eq!(handle.sequence(), 2),
            other => panic!("expected Decode poison, got {other:?}"),
        }
    }

    #[test]
    fn wrong_vehicle_partition_is_poison() {
        let fx = Fixture::new();
        // Reader owns partition 0, but vehicle 1 hashes to 485.
        let reader = RawReader::new(0);
        let mut sched = scheduler();
        let mut frontier = FrontierTracker::new(0, None, fx.now);
        let bytes = fx.good().encode().unwrap();
        // Deliver it on partition 0's subject so the subject check passes first.
        let subject = crate::topology::raw_subject(0);

        let disposition = reader.admit_bytes(
            &mut sched,
            &mut frontier,
            fx.envelope(&subject, None, &bytes, 1),
            fx.now,
        );

        match disposition {
            RawDisposition::Poison {
                reason: PoisonReason::VehiclePartition { subject, vehicle },
                ..
            } => {
                assert_eq!(subject, 0);
                assert_eq!(vehicle, PARTITION);
            }
            other => panic!("expected VehiclePartition poison, got {other:?}"),
        }
    }

    #[test]
    fn schema_mismatch_is_poison() {
        let fx = Fixture::new();
        let reader = RawReader::new(PARTITION);
        let mut sched = scheduler();
        let mut frontier = tracker(&fx);
        let bytes = fx.good().encode().unwrap();
        let subject = subject();

        let mut wrong = HeaderMap::new();
        wrong.insert(headers::SCHEMA, (SCHEMA_VERSION.0 + 1).to_string());

        let disposition = reader.admit_bytes(
            &mut sched,
            &mut frontier,
            fx.envelope(&subject, Some(&wrong), &bytes, 1),
            fx.now,
        );
        match disposition {
            RawDisposition::Poison {
                reason: PoisonReason::Schema { got },
                ..
            } => assert_eq!(got, Some(SCHEMA_VERSION.0 + 1)),
            other => panic!("expected Schema poison, got {other:?}"),
        }
    }

    #[test]
    fn matching_schema_header_passes() {
        let fx = Fixture::new();
        let reader = RawReader::new(PARTITION);
        let mut sched = scheduler();
        let mut frontier = tracker(&fx);
        let bytes = fx.good().encode().unwrap();
        let subject = subject();

        let mut ok = HeaderMap::new();
        headers::stamp_schema(&mut ok);

        let disposition = reader.admit_bytes(
            &mut sched,
            &mut frontier,
            fx.envelope(&subject, Some(&ok), &bytes, 1),
            fx.now,
        );
        assert!(matches!(disposition, RawDisposition::Queued { .. }));
    }

    #[test]
    fn zero_vehicle_is_poison_invalid() {
        let fx = Fixture::new();
        // Vehicle 0 hashes to partition 0, so identity passes and sanity trips.
        let reader = RawReader::new(0);
        let mut sched = scheduler();
        let mut frontier = FrontierTracker::new(0, None, fx.now);
        let bytes = fx.payload(0, 151.0, -33.0).encode().unwrap();
        let subject = crate::topology::raw_subject(0);

        let disposition = reader.admit_bytes(
            &mut sched,
            &mut frontier,
            fx.envelope(&subject, None, &bytes, 1),
            fx.now,
        );
        match disposition {
            RawDisposition::Poison {
                reason: PoisonReason::Invalid { kind },
                ..
            } => assert_eq!(kind, "zero_vehicle"),
            other => panic!("expected Invalid poison, got {other:?}"),
        }
    }

    #[test]
    fn non_finite_coordinate_is_poison_invalid() {
        let fx = Fixture::new();
        let reader = RawReader::new(PARTITION);
        let mut sched = scheduler();
        let mut frontier = tracker(&fx);
        let bytes = fx.payload(VEHICLE, f64::NAN, -33.0).encode().unwrap();
        let subject = subject();

        let disposition = reader.admit_bytes(
            &mut sched,
            &mut frontier,
            fx.envelope(&subject, None, &bytes, 1),
            fx.now,
        );
        match disposition {
            RawDisposition::Poison {
                reason: PoisonReason::Invalid { kind },
                ..
            } => assert_eq!(kind, "non_finite"),
            other => panic!("expected Invalid poison, got {other:?}"),
        }
    }

    #[test]
    fn behind_frontier_is_suppressed_and_terminal() {
        let fx = Fixture::new();
        let reader = RawReader::new(PARTITION);
        let mut sched = scheduler();
        // Recover with the frontier already at 100.
        let mut frontier = FrontierTracker::new(
            PARTITION,
            Some(PartitionFrontier {
                partition: PARTITION,
                sequence: 100,
            }),
            fx.now,
        );
        let bytes = fx.good().encode().unwrap();
        let subject = subject();

        let disposition = reader.admit_bytes(
            &mut sched,
            &mut frontier,
            fx.envelope(&subject, None, &bytes, 50),
            fx.now,
        );
        assert!(disposition_terminal(&disposition));
        match disposition {
            RawDisposition::Suppressed {
                handle,
                reason: SuppressReason::BehindFrontier,
            } => assert_eq!(handle.sequence(), 50),
            other => panic!("expected BehindFrontier suppression, got {other:?}"),
        }
        assert_eq!(frontier.outstanding(), 0);
        assert_eq!(sched.stats().pending, 0);
    }

    #[test]
    fn duplicate_in_flight_is_coalesced_and_not_terminal() {
        let fx = Fixture::new();
        let reader = RawReader::new(PARTITION);
        let mut sched = scheduler();
        let mut frontier = tracker(&fx);
        let bytes = fx.good().encode().unwrap();
        let subject = subject();

        let first = reader.admit_bytes(
            &mut sched,
            &mut frontier,
            fx.envelope(&subject, None, &bytes, 7),
            fx.now,
        );
        assert!(matches!(first, RawDisposition::Queued { .. }));

        // A duplicate delivery stays broker-owned: acknowledging it could
        // retire the original before its commit becomes durable.
        let again = reader.admit_bytes(
            &mut sched,
            &mut frontier,
            fx.envelope(&subject, None, &bytes, 7),
            fx.now,
        );
        match &again {
            RawDisposition::Deferred {
                handle,
                reason: DeferReason::DuplicateOwned,
            } => assert_eq!(handle.sequence(), 7),
            other => panic!("expected DuplicateOwned deferral, got {other:?}"),
        }
        assert!(!again.is_terminal());
        assert_eq!(sched.stats().pending, 1);
        assert_eq!(frontier.outstanding(), 1);
    }

    #[test]
    fn already_committed_is_suppressed_and_terminal() {
        let fx = Fixture::new();
        let reader = RawReader::new(PARTITION);
        let mut sched = scheduler();
        let mut frontier = tracker(&fx);
        let bytes = fx.good().encode().unwrap();
        let subject = subject();

        // Track at a low observation, then load a checkpoint committed to seq 100.
        let seed = reader.admit_bytes(
            &mut sched,
            &mut frontier,
            fx.envelope(&subject, None, &bytes, 10),
            fx.now,
        );
        assert!(matches!(seed, RawDisposition::Queued { .. }));
        sched.set_checkpoint(VehicleId(VEHICLE), fx.present_checkpoint(100));

        // Seq 50 is above the frontier but already covered by the checkpoint.
        let disposition = reader.admit_bytes(
            &mut sched,
            &mut frontier,
            fx.envelope(&subject, None, &bytes, 50),
            fx.now,
        );
        match &disposition {
            RawDisposition::Suppressed {
                handle,
                reason: SuppressReason::Committed,
            } => assert_eq!(handle.sequence(), 50),
            other => panic!("expected Committed suppression, got {other:?}"),
        }
        assert!(disposition.is_terminal());
    }

    #[test]
    fn full_pending_queue_defers_and_reserves_the_frontier_hole() {
        let fx = Fixture::new();
        let reader = RawReader::new(PARTITION);
        let mut sched = Scheduler::<MockEntryId, TestAck>::new(SchedulerConfig {
            pending_limit: 1,
            ..SchedulerConfig::default()
        });
        let mut frontier = tracker(&fx);
        let bytes = fx.good().encode().unwrap();
        let subject = subject();

        let first = reader.admit_bytes(
            &mut sched,
            &mut frontier,
            fx.envelope(&subject, None, &bytes, 10),
            fx.now,
        );
        assert!(matches!(first, RawDisposition::Queued { depth: 1 }));

        let disposition = reader.admit_bytes(
            &mut sched,
            &mut frontier,
            fx.envelope(&subject, None, &bytes, 11),
            fx.now,
        );
        match &disposition {
            RawDisposition::Deferred {
                handle,
                reason: DeferReason::PendingFull,
            } => assert_eq!(handle.sequence(), 11),
            other => panic!("expected PendingFull deferral, got {other:?}"),
        }
        assert!(!disposition.is_terminal());
        assert_eq!(
            frontier.outstanding(),
            2,
            "the deferred raw reserves a hole"
        );
        assert_eq!(frontier.oldest_outstanding(), Some(10));
    }

    #[test]
    fn deferred_hole_blocks_later_completion_and_redelivery_can_enqueue() {
        let fx = Fixture::new();
        let reader = RawReader::new(PARTITION);
        let mut full = Scheduler::<MockEntryId, TestAck>::new(SchedulerConfig {
            pending_limit: 1,
            ..SchedulerConfig::default()
        });
        let mut open = scheduler();
        let mut frontier = FrontierTracker::new(
            PARTITION,
            Some(PartitionFrontier {
                partition: PARTITION,
                sequence: 10,
            }),
            fx.now,
        );
        let subject = subject();
        let first_payload = fx.good();
        let first_bytes = first_payload.encode().unwrap();
        assert!(matches!(
            full.enqueue(
                PendingObservation {
                    id: ObservationId {
                        partition: PARTITION,
                        sequence: 9,
                    },
                    payload: first_payload,
                    published_at: PublishedAtMicros::from_unix_micros(1_000_000)
                        .expect("test timestamp is non-negative"),
                    handle: fx.ack(9),
                    received: fx.now,
                },
                fx.now,
            ),
            Enqueue::Queued { .. }
        ));

        let deferred = reader.admit_bytes(
            &mut full,
            &mut frontier,
            fx.envelope(&subject, None, &first_bytes, 11),
            fx.now,
        );
        assert!(matches!(
            deferred,
            RawDisposition::Deferred {
                reason: DeferReason::PendingFull,
                ..
            }
        ));

        let other_vehicle = (VEHICLE + 1..)
            .find(|&vehicle| partition::partition_of(VehicleId(vehicle)) as u16 == PARTITION)
            .expect("another vehicle in the partition");
        let later_bytes = fx.payload(other_vehicle, 151.21, -33.86).encode().unwrap();
        assert!(matches!(
            reader.admit_bytes(
                &mut open,
                &mut frontier,
                fx.envelope(&subject, None, &later_bytes, 12),
                fx.now,
            ),
            RawDisposition::Queued { .. }
        ));
        frontier.complete(12);
        assert_eq!(frontier.frontier(), 10, "sequence 11 remains a hard hole");

        assert!(matches!(
            reader.admit_bytes(
                &mut open,
                &mut frontier,
                fx.envelope(&subject, None, &first_bytes, 11),
                fx.now,
            ),
            RawDisposition::Queued { .. }
        ));
        frontier.complete(11);
        assert_eq!(frontier.frontier(), 12);
    }

    #[test]
    fn returned_handle_can_be_acknowledged() {
        let fx = Fixture::new();
        let reader = RawReader::new(PARTITION);
        let mut sched = scheduler();
        let mut frontier = tracker(&fx);
        let bytes = fx.good().encode().unwrap();
        let subject = crate::topology::raw_subject(486); // wrong subject -> poison

        let disposition = reader.admit_bytes(
            &mut sched,
            &mut frontier,
            fx.envelope(&subject, None, &bytes, 9),
            fx.now,
        );
        let RawDisposition::Poison { handle, .. } = disposition else {
            panic!("expected poison");
        };
        futures::executor::block_on(handle.ack()).unwrap();
        assert_eq!(&*fx.log.lock().unwrap(), &[AckOp::Ack(9)]);
    }

    #[test]
    fn labels_are_bounded() {
        assert_eq!(PoisonReason::Decode.label(), "decode");
        assert_eq!(
            PoisonReason::SubjectPartition {
                expected: 0,
                got: None
            }
            .label(),
            "subject_partition"
        );
        assert_eq!(
            PoisonReason::VehiclePartition {
                subject: 0,
                vehicle: 1
            }
            .label(),
            "vehicle_partition"
        );
        assert_eq!(PoisonReason::Schema { got: None }.label(), "schema");
        assert_eq!(PoisonReason::PublishTime.label(), "publish_time");
        assert_eq!(
            PoisonReason::Invalid {
                kind: "zero_vehicle"
            }
            .label(),
            "zero_vehicle"
        );
        assert_eq!(SuppressReason::BehindFrontier.label(), "behind_frontier");
        assert_eq!(SuppressReason::Committed.label(), "committed");
        assert_eq!(DeferReason::PendingFull.label(), "pending_full");
        assert_eq!(DeferReason::DuplicateOwned.label(), "duplicate_owned");
    }

    /// `is_terminal` on a borrowed disposition (the enum owns a non-`Copy`
    /// handle, so tests that keep asserting on it borrow through this).
    fn disposition_terminal(d: &RawDisposition<TestAck>) -> bool {
        d.is_terminal()
    }
}