evm-amm-state 0.2.0

EVM-backed AMM state loading, cache synchronization, and pool simulation models
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
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
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
//! Protocol-neutral AMM live-runtime domain vocabulary.
//!
//! These types identify asynchronous work and published state independently of
//! the actor/runtime implementation that will consume them in later stages.

use std::collections::BTreeMap;
use std::fmt;
use std::sync::Arc;
#[cfg(feature = "live-runtime")]
use std::sync::atomic::{AtomicU64, Ordering};

use alloy_primitives::{Address, B256};

use super::{PoolKey, PoolStatus, ProtocolId};

#[cfg(feature = "live-runtime")]
static NEXT_AMM_RUNTIME_ID: AtomicU64 = AtomicU64::new(1);

/// Process-unique lineage of one cache-owning AMM runtime.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct AmmRuntimeId(u64);

impl AmmRuntimeId {
    #[cfg(feature = "live-runtime")]
    pub(crate) fn allocate() -> Self {
        let value = NEXT_AMM_RUNTIME_ID
            .fetch_update(Ordering::AcqRel, Ordering::Acquire, |value| {
                value.checked_add(1)
            })
            .expect("AMM runtime identity exhausted");
        Self(value)
    }

    /// Numeric process-local runtime identity.
    pub const fn get(self) -> u64 {
        self.0
    }
}

/// Error returned when a monotonic runtime sequence would overflow.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RuntimeSequenceOverflow {
    sequence: &'static str,
}

impl RuntimeSequenceOverflow {
    /// Construct an overflow error for `sequence`.
    pub const fn new(sequence: &'static str) -> Self {
        Self { sequence }
    }

    /// Name of the exhausted sequence type.
    pub const fn sequence(self) -> &'static str {
        self.sequence
    }
}

impl fmt::Display for RuntimeSequenceOverflow {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} sequence exhausted", self.sequence)
    }
}

impl std::error::Error for RuntimeSequenceOverflow {}

/// Monotonic generation assigned to one accepted [`PoolKey`] registration.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PoolGeneration(u64);

impl PoolGeneration {
    /// Construct a generation from its persisted/runtime counter value.
    pub const fn new(value: u64) -> Self {
        Self(value)
    }

    /// Return the numeric generation.
    pub const fn get(self) -> u64 {
        self.0
    }

    /// Return the next generation, rejecting exhaustion rather than wrapping.
    pub fn checked_next(self) -> Result<Self, RuntimeSequenceOverflow> {
        self.0
            .checked_add(1)
            .map(Self)
            .ok_or_else(|| RuntimeSequenceOverflow::new("PoolGeneration"))
    }
}

/// Runtime identity of one concrete pool registration instance.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PoolInstanceId {
    key: PoolKey,
    generation: PoolGeneration,
}

impl PoolInstanceId {
    /// Construct a pool instance identity.
    pub const fn new(key: PoolKey, generation: PoolGeneration) -> Self {
        Self { key, generation }
    }

    /// Logical pool identity shared by replacement generations.
    pub const fn key(&self) -> &PoolKey {
        &self.key
    }

    /// Generation of this accepted registration.
    pub const fn generation(&self) -> PoolGeneration {
        self.generation
    }

    /// Consume the identity into its logical key and generation.
    pub fn into_parts(self) -> (PoolKey, PoolGeneration) {
        (self.key, self.generation)
    }
}

/// Stable logical key for one registered adapter family.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct AdapterKey {
    protocols: Vec<ProtocolId>,
}

impl AdapterKey {
    /// Construct a canonical non-empty adapter-family key.
    ///
    /// `primary` guarantees the family is non-empty. All protocol ids are
    /// sorted and de-duplicated, so caller ordering cannot split one adapter
    /// family into several generation domains.
    pub fn new(primary: ProtocolId, additional: impl IntoIterator<Item = ProtocolId>) -> Self {
        let mut protocols = vec![primary];
        protocols.extend(additional);
        protocols.sort_unstable();
        protocols.dedup();
        Self { protocols }
    }

    /// Canonical sorted protocol ids served by the adapter family.
    pub fn protocols(&self) -> &[ProtocolId] {
        &self.protocols
    }
}

/// Monotonic generation assigned to one accepted adapter-family registration.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct AdapterGeneration(u64);

impl AdapterGeneration {
    /// Construct an adapter generation.
    pub const fn new(value: u64) -> Self {
        Self(value)
    }

    /// Return the numeric generation.
    pub const fn get(self) -> u64 {
        self.0
    }

    /// Return the next generation, rejecting exhaustion rather than wrapping.
    pub fn checked_next(self) -> Result<Self, RuntimeSequenceOverflow> {
        self.0
            .checked_add(1)
            .map(Self)
            .ok_or_else(|| RuntimeSequenceOverflow::new("AdapterGeneration"))
    }
}

/// Runtime identity of one concrete adapter-family registration.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct AdapterInstanceId {
    key: AdapterKey,
    generation: AdapterGeneration,
}

impl AdapterInstanceId {
    /// Construct an adapter instance identity.
    pub const fn new(key: AdapterKey, generation: AdapterGeneration) -> Self {
        Self { key, generation }
    }

    /// Logical adapter-family key.
    pub const fn key(&self) -> &AdapterKey {
        &self.key
    }

    /// Generation of this accepted adapter registration.
    pub const fn generation(&self) -> AdapterGeneration {
        self.generation
    }
}

/// Stable logical key for one discovery source or factory watcher.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct DiscoveryOwnerKey(String);

impl DiscoveryOwnerKey {
    /// Construct a discovery-owner key.
    pub fn new(key: impl Into<String>) -> Self {
        Self(key.into())
    }

    /// Borrow the stable key.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

/// Monotonic generation assigned to one accepted discovery-owner registration.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct DiscoveryGeneration(u64);

impl DiscoveryGeneration {
    /// Construct a discovery generation.
    pub const fn new(value: u64) -> Self {
        Self(value)
    }

    /// Return the numeric generation.
    pub const fn get(self) -> u64 {
        self.0
    }

    /// Return the next generation, rejecting exhaustion rather than wrapping.
    pub fn checked_next(self) -> Result<Self, RuntimeSequenceOverflow> {
        self.0
            .checked_add(1)
            .map(Self)
            .ok_or_else(|| RuntimeSequenceOverflow::new("DiscoveryGeneration"))
    }
}

/// Runtime identity of one concrete discovery source or watcher.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct DiscoveryOwnerId {
    key: DiscoveryOwnerKey,
    generation: DiscoveryGeneration,
}

impl DiscoveryOwnerId {
    /// Construct a discovery-owner identity.
    pub const fn new(key: DiscoveryOwnerKey, generation: DiscoveryGeneration) -> Self {
        Self { key, generation }
    }

    /// Logical discovery-owner key.
    pub const fn key(&self) -> &DiscoveryOwnerKey {
        &self.key
    }

    /// Generation of this accepted discovery-owner registration.
    pub const fn generation(&self) -> DiscoveryGeneration {
        self.generation
    }
}

/// Generation-scoped owner of asynchronous runtime work.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum RuntimeOwnerId {
    /// Work owned by one concrete pool registration.
    Pool(PoolInstanceId),
    /// Work owned by one concrete adapter-family registration.
    Adapter(AdapterInstanceId),
    /// Work owned by one concrete discovery source or watcher.
    Discovery(DiscoveryOwnerId),
}

/// Monotonic identifier for one scheduled attempt under a runtime owner.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct WorkId(u64);

impl WorkId {
    /// Construct a work identifier.
    pub const fn new(value: u64) -> Self {
        Self(value)
    }

    /// Return the numeric identifier.
    pub const fn get(self) -> u64 {
        self.0
    }

    /// Return the next work identifier, rejecting exhaustion rather than wrapping.
    pub fn checked_next(self) -> Result<Self, RuntimeSequenceOverflow> {
        self.0
            .checked_add(1)
            .map(Self)
            .ok_or_else(|| RuntimeSequenceOverflow::new("WorkId"))
    }
}

/// Position represented by a canonical AMM state point.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum StatePosition {
    /// State after every transaction and log in the identified block.
    PostBlock,
}

/// Chain provenance for one coherent AMM state.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct AmmStatePoint {
    chain_id: u64,
    block_number: u64,
    block_hash: B256,
    position: StatePosition,
}

impl AmmStatePoint {
    /// Construct a post-block state point.
    pub const fn post_block(chain_id: u64, block_number: u64, block_hash: B256) -> Self {
        Self {
            chain_id,
            block_number,
            block_hash,
            position: StatePosition::PostBlock,
        }
    }

    /// Chain identifier.
    pub const fn chain_id(self) -> u64 {
        self.chain_id
    }

    /// Block number.
    pub const fn block_number(self) -> u64 {
        self.block_number
    }

    /// Canonical block hash.
    pub const fn block_hash(self) -> B256 {
        self.block_hash
    }

    /// State position within the block.
    pub const fn position(self) -> StatePosition {
        self.position
    }

    /// First block whose events are absent from this state point.
    pub fn first_unapplied_block(self) -> Result<u64, RuntimeSequenceOverflow> {
        match self.position {
            StatePosition::PostBlock => self
                .block_number
                .checked_add(1)
                .ok_or_else(|| RuntimeSequenceOverflow::new("AmmStatePoint.block_number")),
        }
    }
}

/// Monotonic version of a coherent published AMM state snapshot.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct AmmStateVersion(u64);

impl AmmStateVersion {
    /// Initial snapshot version.
    pub const fn initial() -> Self {
        Self(0)
    }

    /// Construct a state version.
    pub const fn new(value: u64) -> Self {
        Self(value)
    }

    /// Return the numeric version.
    pub const fn get(self) -> u64 {
        self.0
    }

    /// Return the next version, rejecting exhaustion rather than wrapping.
    pub fn checked_next(self) -> Result<Self, RuntimeSequenceOverflow> {
        self.0
            .checked_add(1)
            .map(Self)
            .ok_or_else(|| RuntimeSequenceOverflow::new("AmmStateVersion"))
    }
}

/// Monotonic revision of one pool's quote-relevant state and eligibility.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PoolStateRevision(u64);

impl PoolStateRevision {
    /// Construct a pool-state revision.
    pub const fn new(value: u64) -> Self {
        Self(value)
    }

    /// Return the numeric revision.
    pub const fn get(self) -> u64 {
        self.0
    }

    /// Return the next revision, rejecting exhaustion rather than wrapping.
    pub fn checked_next(self) -> Result<Self, RuntimeSequenceOverflow> {
        self.0
            .checked_add(1)
            .map(Self)
            .ok_or_else(|| RuntimeSequenceOverflow::new("PoolStateRevision"))
    }
}

/// Complete provenance key for one pool's quote-relevant state.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct PoolStateRef {
    pool: PoolInstanceId,
    revision: PoolStateRevision,
    point: AmmStatePoint,
}

impl PoolStateRef {
    /// Construct a pool-state reference.
    pub const fn new(
        pool: PoolInstanceId,
        revision: PoolStateRevision,
        point: AmmStatePoint,
    ) -> Self {
        Self {
            pool,
            revision,
            point,
        }
    }

    /// Pool registration instance.
    pub const fn pool(&self) -> &PoolInstanceId {
        &self.pool
    }

    /// Quote-relevant revision.
    pub const fn revision(&self) -> PoolStateRevision {
        self.revision
    }

    /// Chain point at which this state is valid.
    pub const fn point(&self) -> AmmStatePoint {
        self.point
    }
}

/// Operational scheduler/transport state of one pool registration.
///
/// This is a sidecar to [`PoolStatus`], which continues to express adapter and
/// quote health.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum PoolRuntimeState {
    /// Metadata was discovered but no work has been accepted.
    Discovered,
    /// A generation-scoped hydration job is queued.
    Queued,
    /// Cold-start provider work is in flight or ready to commit.
    Hydrating,
    /// A verified staging state is catching up to a canonical commit fence.
    CatchingUp,
    /// A coherent published snapshot can quote the pool.
    Searchable,
    /// Catch-up and subsequent live delivery are continuous.
    Live,
    /// Current state quality is insufficient for normal search policy.
    Degraded,
    /// Teardown is rejecting new work and removing ownership.
    Removing,
    /// No runtime ownership remains for this generation.
    Removed,
    /// The current pre-searchable attempt failed; retry is explicit.
    Failed,
}

impl PoolRuntimeState {
    /// Required/normal adapter-facing status for this operational state.
    ///
    /// `None` means teardown/failure preserves the last adapter classification.
    pub const fn required_pool_status(self) -> Option<PoolStatus> {
        match self {
            Self::Discovered | Self::Queued => Some(PoolStatus::Pending),
            Self::Hydrating | Self::CatchingUp => Some(PoolStatus::Cold),
            Self::Searchable | Self::Live => Some(PoolStatus::Ready),
            Self::Degraded => Some(PoolStatus::Degraded),
            Self::Removing | Self::Removed | Self::Failed => None,
        }
    }

    /// Whether `next` is a legal lifecycle transition.
    pub const fn can_transition_to(self, next: Self) -> bool {
        if matches!(next, Self::Removing) {
            return !matches!(self, Self::Removing | Self::Removed);
        }

        matches!(
            (self, next),
            (Self::Discovered, Self::Queued)
                | (Self::Queued, Self::Hydrating | Self::Failed)
                | (Self::Hydrating, Self::CatchingUp | Self::Failed)
                | (Self::CatchingUp, Self::Searchable | Self::Failed)
                | (
                    Self::Searchable,
                    Self::Live | Self::CatchingUp | Self::Degraded
                )
                | (Self::Live, Self::CatchingUp | Self::Degraded)
                | (Self::Degraded, Self::CatchingUp | Self::Live)
                | (Self::Failed, Self::Queued)
                | (Self::Removing, Self::Removed)
        )
    }
}

/// Rejected transition between operational pool lifecycle states.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct InvalidPoolRuntimeTransition {
    from: PoolRuntimeState,
    to: PoolRuntimeState,
}

impl InvalidPoolRuntimeTransition {
    /// Construct a transition error.
    pub const fn new(from: PoolRuntimeState, to: PoolRuntimeState) -> Self {
        Self { from, to }
    }

    /// Current state.
    pub const fn from(self) -> PoolRuntimeState {
        self.from
    }

    /// Rejected target state.
    pub const fn to(self) -> PoolRuntimeState {
        self.to
    }
}

impl fmt::Display for InvalidPoolRuntimeTransition {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "invalid pool runtime transition: {:?} -> {:?}",
            self.from, self.to
        )
    }
}

impl std::error::Error for InvalidPoolRuntimeTransition {}

/// Checked operational lifecycle for one concrete pool registration.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PoolLifecycle {
    pool: PoolInstanceId,
    state: PoolRuntimeState,
}

impl PoolLifecycle {
    /// Construct a lifecycle at `state`.
    pub const fn new(pool: PoolInstanceId, state: PoolRuntimeState) -> Self {
        Self { pool, state }
    }

    /// Pool registration instance governed by this lifecycle.
    pub const fn pool(&self) -> &PoolInstanceId {
        &self.pool
    }

    /// Current operational state.
    pub const fn state(&self) -> PoolRuntimeState {
        self.state
    }

    /// Apply one checked lifecycle transition.
    pub fn transition_to(
        &mut self,
        next: PoolRuntimeState,
    ) -> Result<(), InvalidPoolRuntimeTransition> {
        if !self.state.can_transition_to(next) {
            return Err(InvalidPoolRuntimeTransition::new(self.state, next));
        }
        self.state = next;
        Ok(())
    }
}

/// Stable identifier for manual/configuration registration evidence.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct RegistrationSourceKey(String);

impl RegistrationSourceKey {
    /// Construct a stable source key.
    pub fn new(key: impl Into<String>) -> Self {
        Self(key.into())
    }

    /// Borrow the stable source key.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

/// Reorg policy for pool evidence obtained from a state query.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum QueryEvidencePolicy {
    /// The query was accepted at a finalized point and remains stable evidence.
    Finalized,
    /// An orphaned query point must be checked again on the canonical chain.
    RevalidateOnReorg,
}

/// One independent piece of evidence supporting a pool registration.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum RegistrationProvenance {
    /// Stable manual or configuration evidence.
    Stable {
        /// Stable configuration/source identity.
        source: RegistrationSourceKey,
    },
    /// Evidence obtained from a state query at one block.
    StateQuery {
        /// Generation-scoped discovery source.
        owner: DiscoveryOwnerId,
        /// Chain identifier.
        chain_id: u64,
        /// Queried block number.
        block_number: u64,
        /// Queried block hash.
        block_hash: B256,
        /// Finality/revalidation policy.
        policy: QueryEvidencePolicy,
    },
    /// Evidence obtained from one factory creation log.
    FactoryLog {
        /// Generation-scoped factory watcher.
        owner: DiscoveryOwnerId,
        /// Factory that emitted the log.
        factory: Address,
        /// Chain identifier.
        chain_id: u64,
        /// Block number containing the log.
        block_number: u64,
        /// Block hash containing the log.
        block_hash: B256,
        /// Transaction hash that emitted the log.
        transaction_hash: B256,
        /// Log index within the block.
        log_index: u64,
    },
}

impl RegistrationProvenance {
    /// Construct stable manual/configuration evidence.
    pub const fn stable(source: RegistrationSourceKey) -> Self {
        Self::Stable { source }
    }

    /// Construct state-query evidence.
    pub const fn state_query(
        owner: DiscoveryOwnerId,
        chain_id: u64,
        block_number: u64,
        block_hash: B256,
        policy: QueryEvidencePolicy,
    ) -> Self {
        Self::StateQuery {
            owner,
            chain_id,
            block_number,
            block_hash,
            policy,
        }
    }

    /// Construct factory-log evidence.
    #[allow(clippy::too_many_arguments)]
    pub const fn factory_log(
        owner: DiscoveryOwnerId,
        factory: Address,
        chain_id: u64,
        block_number: u64,
        block_hash: B256,
        transaction_hash: B256,
        log_index: u64,
    ) -> Self {
        Self::FactoryLog {
            owner,
            factory,
            chain_id,
            block_number,
            block_hash,
            transaction_hash,
            log_index,
        }
    }
}

/// Required registration response after canonical hashes are dropped.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum RegistrationReorgAction {
    /// At least one unaffected stable/canonical evidence item remains.
    Keep,
    /// No stable evidence remains, but a state query can be re-run.
    Revalidate,
    /// Every supporting evidence item was orphaned.
    Remove,
}

/// All known independent evidence supporting one logical pool registration.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RegistrationEvidenceSet {
    evidence: Vec<RegistrationProvenance>,
}

impl RegistrationEvidenceSet {
    /// Construct a non-empty evidence set in canonical order.
    ///
    /// Requiring a primary item makes the invariant that every accepted
    /// registration has provenance explicit in the type construction path.
    pub fn new(
        primary: RegistrationProvenance,
        additional: impl IntoIterator<Item = RegistrationProvenance>,
    ) -> Self {
        let mut evidence = vec![primary];
        evidence.extend(additional);
        evidence.sort_unstable();
        evidence.dedup();
        Self { evidence }
    }

    /// Borrow every supporting evidence item.
    pub fn evidence(&self) -> &[RegistrationProvenance] {
        &self.evidence
    }

    /// Determine the conservative response to the supplied dropped hashes.
    pub fn reorg_action(&self, dropped_hashes: &[B256]) -> RegistrationReorgAction {
        let mut requires_revalidation = false;

        for evidence in &self.evidence {
            match evidence {
                RegistrationProvenance::Stable { .. }
                | RegistrationProvenance::StateQuery {
                    policy: QueryEvidencePolicy::Finalized,
                    ..
                } => return RegistrationReorgAction::Keep,
                RegistrationProvenance::StateQuery {
                    block_hash,
                    policy: QueryEvidencePolicy::RevalidateOnReorg,
                    ..
                } => {
                    if dropped_hashes.contains(block_hash) {
                        requires_revalidation = true;
                    } else {
                        return RegistrationReorgAction::Keep;
                    }
                }
                RegistrationProvenance::FactoryLog { block_hash, .. } => {
                    if !dropped_hashes.contains(block_hash) {
                        return RegistrationReorgAction::Keep;
                    }
                }
            }
        }

        if requires_revalidation {
            RegistrationReorgAction::Revalidate
        } else {
            RegistrationReorgAction::Remove
        }
    }
}

/// Quality of the complete AMM state represented by a committed change set.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum AmmStateQuality {
    /// State is coherent at the advertised point.
    Coherent,
    /// State is coherent, but one or more pools are unavailable/degraded.
    Degraded,
    /// Canonical trust was lost and consumers must await recovery.
    Untrusted,
}

/// Final kind of one pool change within a committed AMM state version.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum AmmPoolChangeKind {
    /// A pool instance became visible.
    Added,
    /// Quote-relevant state or metadata changed.
    Updated,
    /// The pool became unavailable for normal search policy.
    Degraded,
    /// The pool recovered normal search eligibility.
    Recovered,
    /// The pool instance was removed.
    Removed,
}

/// Search-facing consequences of one pool change.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct AmmChangeImpact {
    state: bool,
    quoteability: bool,
    topology: bool,
}

impl AmmChangeImpact {
    /// Construct explicit impact flags.
    pub const fn new(state: bool, quoteability: bool, topology: bool) -> Self {
        Self {
            state,
            quoteability,
            topology,
        }
    }

    /// A quote-state update with unchanged eligibility/topology.
    pub const fn state_only() -> Self {
        Self::new(true, false, false)
    }

    /// A search-eligibility change with unchanged topology metadata.
    pub const fn quoteability() -> Self {
        Self::new(false, true, false)
    }

    /// A graph-topology metadata change with unchanged state/eligibility.
    pub const fn topology() -> Self {
        Self::new(false, false, true)
    }

    /// State, search eligibility, and topology all changed.
    pub const fn all() -> Self {
        Self::new(true, true, true)
    }

    /// Whether quote-relevant state changed.
    pub const fn state_changed(self) -> bool {
        self.state
    }

    /// Whether search eligibility changed.
    pub const fn quoteability_changed(self) -> bool {
        self.quoteability
    }

    /// Whether graph topology metadata changed.
    pub const fn topology_changed(self) -> bool {
        self.topology
    }

    /// Union two independently observed impact sets.
    pub const fn union(self, other: Self) -> Self {
        Self::new(
            self.state || other.state,
            self.quoteability || other.quoteability,
            self.topology || other.topology,
        )
    }
}

/// One pool's final change within a committed AMM state version.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AmmPoolChange {
    pool: PoolInstanceId,
    revision: PoolStateRevision,
    kind: AmmPoolChangeKind,
    impact: AmmChangeImpact,
}

impl AmmPoolChange {
    /// Construct a pool change.
    pub const fn new(
        pool: PoolInstanceId,
        revision: PoolStateRevision,
        kind: AmmPoolChangeKind,
        impact: AmmChangeImpact,
    ) -> Self {
        Self {
            pool,
            revision,
            kind,
            impact,
        }
    }

    /// Affected pool registration instance.
    pub const fn pool(&self) -> &PoolInstanceId {
        &self.pool
    }

    /// Resulting quote-relevant revision.
    pub const fn revision(&self) -> PoolStateRevision {
        self.revision
    }

    /// Final change kind.
    pub const fn kind(&self) -> AmmPoolChangeKind {
        self.kind
    }

    /// Search-facing consequences.
    pub const fn impact(&self) -> AmmChangeImpact {
        self.impact
    }
}

/// Canonical incident represented by a committed AMM state version.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum AmmStateIncident {
    /// Previously canonical block points were dropped.
    Reorg {
        /// Dropped post-block points in ascending chain order.
        dropped: Vec<AmmStatePoint>,
    },
    /// A forward canonical block range was not observed.
    Gap {
        /// First missed block, inclusive.
        from: u64,
        /// Last missed block, inclusive.
        to: u64,
    },
    /// A tracked account root changed without a covering decoder.
    CoverageGap {
        /// Account with unknown changed storage.
        address: Address,
        /// Block at which the gap was detected.
        block: u64,
    },
}

/// Invalid construction of a committed AMM change set.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum AmmChangeSetError {
    /// More than one final change was supplied for a pool instance.
    DuplicatePool {
        /// Duplicated pool registration instance.
        pool: PoolInstanceId,
    },
}

impl AmmChangeSetError {
    /// Construct a duplicate-pool error.
    pub const fn duplicate_pool(pool: PoolInstanceId) -> Self {
        Self::DuplicatePool { pool }
    }
}

impl fmt::Display for AmmChangeSetError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::DuplicatePool { pool } => {
                write!(f, "duplicate AMM change for pool instance {pool:?}")
            }
        }
    }
}

impl std::error::Error for AmmChangeSetError {}

/// Deterministically ordered changes for one coherent published AMM state.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AmmChangeSet {
    version: AmmStateVersion,
    point: AmmStatePoint,
    quality: AmmStateQuality,
    pool_changes: Vec<AmmPoolChange>,
    incidents: Vec<AmmStateIncident>,
    requires_full_refresh: bool,
}

impl AmmChangeSet {
    /// Construct a committed change set and canonicalize pool-change ordering.
    pub fn new(
        version: AmmStateVersion,
        point: AmmStatePoint,
        quality: AmmStateQuality,
        pool_changes: impl IntoIterator<Item = AmmPoolChange>,
        incidents: impl IntoIterator<Item = AmmStateIncident>,
        requires_full_refresh: bool,
    ) -> Result<Self, AmmChangeSetError> {
        let mut pool_changes: Vec<_> = pool_changes.into_iter().collect();
        pool_changes.sort_by(|left, right| left.pool.cmp(&right.pool));
        for duplicate in pool_changes.windows(2) {
            if duplicate[0].pool == duplicate[1].pool {
                return Err(AmmChangeSetError::duplicate_pool(duplicate[0].pool.clone()));
            }
        }

        let mut incidents: Vec<_> = incidents
            .into_iter()
            .map(|incident| match incident {
                AmmStateIncident::Reorg { mut dropped } => {
                    dropped.sort_unstable();
                    dropped.dedup();
                    AmmStateIncident::Reorg { dropped }
                }
                incident => incident,
            })
            .collect();
        incidents.sort_unstable();
        incidents.dedup();

        Ok(Self {
            version,
            point,
            quality,
            pool_changes,
            incidents,
            requires_full_refresh,
        })
    }

    /// Published state version.
    pub const fn version(&self) -> AmmStateVersion {
        self.version
    }

    /// Coherent post-block point.
    pub const fn point(&self) -> AmmStatePoint {
        self.point
    }

    /// Complete state quality.
    pub const fn quality(&self) -> AmmStateQuality {
        self.quality
    }

    /// Canonically ordered one-per-pool changes.
    pub fn pool_changes(&self) -> &[AmmPoolChange] {
        &self.pool_changes
    }

    /// Canonical incidents carried by this version.
    pub fn incidents(&self) -> &[AmmStateIncident] {
        &self.incidents
    }

    /// Whether consumers must conservatively refresh outside the known pool set.
    pub const fn requires_full_refresh(&self) -> bool {
        self.requires_full_refresh
    }
}

/// Composite identity of one scheduled attempt under a generation-scoped owner.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct RuntimeWorkId {
    owner: RuntimeOwnerId,
    work: WorkId,
}

impl RuntimeWorkId {
    /// Construct a runtime work identity.
    pub const fn new(owner: RuntimeOwnerId, work: WorkId) -> Self {
        Self { owner, work }
    }

    /// Generation-scoped owner.
    pub const fn owner(&self) -> &RuntimeOwnerId {
        &self.owner
    }

    /// Attempt identifier.
    pub const fn work(&self) -> WorkId {
        self.work
    }
}

/// Operational state shared by adapter and discovery owners.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum OwnerRuntimeState {
    /// Owner accepts and processes work.
    Active,
    /// Owner rejects new work while teardown runs.
    Removing,
    /// No ownership remains for this generation.
    Removed,
    /// Owner cannot currently make progress.
    Failed,
}

/// Recoverable latest lifecycle state for every runtime owner category.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct RuntimeLifecycleMap {
    pools: BTreeMap<PoolInstanceId, PoolRuntimeState>,
    adapters: BTreeMap<AdapterInstanceId, OwnerRuntimeState>,
    discovery: BTreeMap<DiscoveryOwnerId, OwnerRuntimeState>,
}

impl RuntimeLifecycleMap {
    /// Set the latest state of a pool instance.
    pub fn set_pool(&mut self, pool: PoolInstanceId, state: PoolRuntimeState) {
        self.pools.insert(pool, state);
    }

    /// Set the latest state of an adapter instance.
    pub fn set_adapter(&mut self, adapter: AdapterInstanceId, state: OwnerRuntimeState) {
        self.adapters.insert(adapter, state);
    }

    /// Set the latest state of a discovery owner.
    pub fn set_discovery(&mut self, owner: DiscoveryOwnerId, state: OwnerRuntimeState) {
        self.discovery.insert(owner, state);
    }

    /// Latest state of a pool instance.
    pub fn pool(&self, pool: &PoolInstanceId) -> Option<PoolRuntimeState> {
        self.pools.get(pool).copied()
    }

    /// Latest state of an adapter instance.
    pub fn adapter(&self, adapter: &AdapterInstanceId) -> Option<OwnerRuntimeState> {
        self.adapters.get(adapter).copied()
    }

    /// Latest state of a discovery owner.
    pub fn discovery(&self, owner: &DiscoveryOwnerId) -> Option<OwnerRuntimeState> {
        self.discovery.get(owner).copied()
    }

    /// Canonically ordered pool lifecycle entries.
    pub fn pools(&self) -> impl Iterator<Item = (&PoolInstanceId, PoolRuntimeState)> {
        self.pools.iter().map(|(owner, state)| (owner, *state))
    }

    /// Canonically ordered adapter lifecycle entries.
    pub fn adapters(&self) -> impl Iterator<Item = (&AdapterInstanceId, OwnerRuntimeState)> {
        self.adapters.iter().map(|(owner, state)| (owner, *state))
    }

    /// Canonically ordered discovery-owner lifecycle entries.
    pub fn discovery_owners(&self) -> impl Iterator<Item = (&DiscoveryOwnerId, OwnerRuntimeState)> {
        self.discovery.iter().map(|(owner, state)| (owner, *state))
    }
}

/// Scheduler priority/service class.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum AmmWorkClass {
    /// Reorg and canonical live input.
    Canonical,
    /// Registration catch-up.
    CatchUp,
    /// Required repair for tracked pools.
    Repair,
    /// Focused/user-requested work.
    Focused,
    /// Normal background bootstrap.
    Bootstrap,
    /// Optional deferred warming.
    Deferred,
}

/// Kind of one scheduled runtime job.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum AmmWorkKind {
    /// Pool/factory discovery.
    Discovery,
    /// Initial state hydration.
    ColdStart,
    /// Replay toward a canonical commit fence.
    CatchUp,
    /// Required state repair.
    Repair,
    /// Optional post-searchability warming.
    DeferredWarmup,
}

/// Latest progress of one scheduled runtime job.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AmmWorkProgress {
    kind: AmmWorkKind,
    completed: u64,
    total: Option<u64>,
}

/// Invalid completed/total relationship for one runtime job.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct InvalidWorkProgress {
    completed: u64,
    total: u64,
}

impl InvalidWorkProgress {
    /// Construct an invalid-progress error.
    pub const fn new(completed: u64, total: u64) -> Self {
        Self { completed, total }
    }

    /// Reported completed units.
    pub const fn completed(self) -> u64 {
        self.completed
    }

    /// Reported total units.
    pub const fn total(self) -> u64 {
        self.total
    }
}

impl fmt::Display for InvalidWorkProgress {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "completed work {} exceeds known total {}",
            self.completed, self.total
        )
    }
}

impl std::error::Error for InvalidWorkProgress {}

impl AmmWorkProgress {
    /// Construct progress, retaining `None` when total work is unknown.
    pub const fn new(
        kind: AmmWorkKind,
        completed: u64,
        total: Option<u64>,
    ) -> Result<Self, InvalidWorkProgress> {
        if let Some(total) = total
            && completed > total
        {
            return Err(InvalidWorkProgress::new(completed, total));
        }
        Ok(Self {
            kind,
            completed,
            total,
        })
    }

    /// Work kind.
    pub const fn kind(&self) -> AmmWorkKind {
        self.kind
    }

    /// Completed units.
    pub const fn completed(&self) -> u64 {
        self.completed
    }

    /// Known total units, or `None` when discovery/planning is incomplete.
    pub const fn total(&self) -> Option<u64> {
        self.total
    }
}

/// Recoverable queued work counts by scheduler class.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct QueueDepths {
    depths: BTreeMap<AmmWorkClass, usize>,
}

impl QueueDepths {
    /// Set the latest queue depth for a class.
    pub fn set(&mut self, class: AmmWorkClass, depth: usize) {
        self.depths.insert(class, depth);
    }

    /// Latest queue depth for a class.
    pub fn get(&self, class: AmmWorkClass) -> usize {
        self.depths.get(&class).copied().unwrap_or_default()
    }

    /// Canonically ordered non-default queue depths.
    pub fn iter(&self) -> impl Iterator<Item = (AmmWorkClass, usize)> + '_ {
        self.depths.iter().map(|(class, depth)| (*class, *depth))
    }
}

/// High-level health of the AMM runtime.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum AmmRuntimeHealth {
    /// Canonical state and control paths are healthy.
    Healthy,
    /// Some pools/work are degraded but coherent publication continues.
    Degraded,
    /// Canonical trust was lost; state publication is paused.
    Untrusted,
    /// Shutdown is in progress.
    ShuttingDown,
}

/// Queryable latest runtime lifecycle/progress state for lag recovery.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AmmRuntimeStatusSnapshot {
    sequence: u64,
    state_version: AmmStateVersion,
    lifecycles: Arc<RuntimeLifecycleMap>,
    active_work: Arc<BTreeMap<RuntimeWorkId, AmmWorkProgress>>,
    queues: Arc<QueueDepths>,
    health: AmmRuntimeHealth,
}

impl AmmRuntimeStatusSnapshot {
    /// Construct a recoverable status snapshot.
    pub fn new(
        sequence: u64,
        state_version: AmmStateVersion,
        lifecycles: RuntimeLifecycleMap,
        active_work: impl IntoIterator<Item = (RuntimeWorkId, AmmWorkProgress)>,
        queues: QueueDepths,
        health: AmmRuntimeHealth,
    ) -> Self {
        Self {
            sequence,
            state_version,
            lifecycles: Arc::new(lifecycles),
            active_work: Arc::new(active_work.into_iter().collect()),
            queues: Arc::new(queues),
            health,
        }
    }

    /// Last observer sequence reflected by this snapshot.
    pub const fn sequence(&self) -> u64 {
        self.sequence
    }

    /// Latest coherent published state version.
    pub const fn state_version(&self) -> AmmStateVersion {
        self.state_version
    }

    /// Latest pool operational state.
    pub fn pool_state(&self, pool: &PoolInstanceId) -> Option<PoolRuntimeState> {
        self.lifecycles.pool(pool)
    }

    /// Latest adapter operational state.
    pub fn adapter_state(&self, adapter: &AdapterInstanceId) -> Option<OwnerRuntimeState> {
        self.lifecycles.adapter(adapter)
    }

    /// Latest discovery-owner operational state.
    pub fn discovery_state(&self, owner: &DiscoveryOwnerId) -> Option<OwnerRuntimeState> {
        self.lifecycles.discovery(owner)
    }

    /// Complete canonically ordered lifecycle map.
    pub fn lifecycles(&self) -> &RuntimeLifecycleMap {
        &self.lifecycles
    }

    /// Latest progress for one work attempt.
    pub fn active_work(&self, work: &RuntimeWorkId) -> Option<&AmmWorkProgress> {
        self.active_work.get(work)
    }

    /// Canonically ordered active-work entries.
    pub fn active_work_items(&self) -> impl Iterator<Item = (&RuntimeWorkId, &AmmWorkProgress)> {
        self.active_work.iter()
    }

    /// Latest queue depth for one scheduler class.
    pub fn queue_depth(&self, class: AmmWorkClass) -> usize {
        self.queues.get(class)
    }

    /// Complete queue-depth snapshot.
    pub fn queue_depths(&self) -> &QueueDepths {
        &self.queues
    }

    /// Runtime health.
    pub const fn health(&self) -> AmmRuntimeHealth {
        self.health
    }
}

/// Observer-facing runtime event payload.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum AmmRuntimeEventKind {
    /// A pool operational state changed.
    PoolLifecycleTransition {
        /// Pool registration instance.
        pool: PoolInstanceId,
        /// Previous state.
        from: PoolRuntimeState,
        /// New state.
        to: PoolRuntimeState,
    },
    /// An adapter-family operational state changed.
    AdapterLifecycleTransition {
        /// Adapter-family generation.
        adapter: AdapterInstanceId,
        /// Previous state.
        from: OwnerRuntimeState,
        /// New state.
        to: OwnerRuntimeState,
    },
    /// A discovery-source generation changed operational state.
    DiscoveryLifecycleTransition {
        /// Discovery-source generation.
        owner: DiscoveryOwnerId,
        /// Previous operational state.
        from: OwnerRuntimeState,
        /// New operational state.
        to: OwnerRuntimeState,
    },
    /// Work was classified and queued.
    WorkQueued {
        /// Work attempt.
        work: RuntimeWorkId,
        /// Scheduler class.
        class: AmmWorkClass,
        /// Work kind.
        kind: AmmWorkKind,
    },
    /// Work made progress.
    WorkProgress {
        /// Work attempt.
        work: RuntimeWorkId,
        /// Latest progress.
        progress: AmmWorkProgress,
    },
    /// Work completed successfully.
    WorkCompleted {
        /// Completed work attempt.
        work: RuntimeWorkId,
    },
    /// Work was cancelled or superseded.
    WorkCancelled {
        /// Cancelled work attempt.
        work: RuntimeWorkId,
    },
    /// Work failed.
    WorkFailed {
        /// Failed work attempt.
        work: RuntimeWorkId,
        /// Human-readable diagnostic message.
        message: String,
    },
    /// One cold-start round began.
    ColdStartRoundStarted {
        /// Cold-start work attempt.
        work: RuntimeWorkId,
        /// Zero-based round number.
        round: u64,
        /// Known total rounds, when planning is complete.
        total_rounds: Option<u64>,
    },
    /// One cold-start round completed.
    ColdStartRoundCompleted {
        /// Cold-start work attempt.
        work: RuntimeWorkId,
        /// Zero-based round number.
        round: u64,
    },
    /// A pool registration instance became canonically visible.
    RegistrationAccepted {
        /// Accepted pool instance.
        pool: PoolInstanceId,
    },
    /// A pool registration instance was canonically removed.
    RegistrationRemoved {
        /// Removed pool instance.
        pool: PoolInstanceId,
    },
    /// An adapter-family generation became available for pool dispatch.
    AdapterRegistrationAccepted {
        /// Accepted adapter generation.
        adapter: AdapterInstanceId,
    },
    /// An adapter-family generation was removed.
    AdapterRegistrationRemoved {
        /// Removed adapter generation.
        adapter: AdapterInstanceId,
    },
    /// A discovery-source generation became available for discovery work.
    DiscoveryRegistrationAccepted {
        /// Accepted discovery-source generation.
        owner: DiscoveryOwnerId,
    },
    /// A discovery-source generation was removed.
    DiscoveryRegistrationRemoved {
        /// Removed discovery-source generation.
        owner: DiscoveryOwnerId,
    },
    /// A coherent AMM state version was published.
    StateCommitted {
        /// Published state version.
        version: AmmStateVersion,
        /// Published post-block point.
        point: AmmStatePoint,
    },
    /// Previously canonical state points were dropped.
    Reorg {
        /// Dropped post-block points in ascending chain order.
        dropped: Vec<AmmStatePoint>,
    },
    /// A forward canonical block range was not observed.
    Gap {
        /// First missed block, inclusive.
        from: u64,
        /// Last missed block, inclusive.
        to: u64,
    },
    /// A tracked account root changed without a covering decoder.
    CoverageGap {
        /// Account with an unknown changed slot.
        address: Address,
        /// Block at which the gap was detected.
        block: u64,
    },
    /// Runtime health changed.
    HealthChanged {
        /// Previous health.
        from: AmmRuntimeHealth,
        /// New health.
        to: AmmRuntimeHealth,
    },
}

/// Monotonically sequenced observer event.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AmmRuntimeEvent {
    sequence: u64,
    kind: AmmRuntimeEventKind,
}

impl AmmRuntimeEvent {
    /// Construct an event at an explicit sequence.
    pub const fn new(sequence: u64, kind: AmmRuntimeEventKind) -> Self {
        Self { sequence, kind }
    }

    /// Construct the immediately following event, rejecting sequence overflow.
    pub fn checked_next(&self, kind: AmmRuntimeEventKind) -> Result<Self, RuntimeSequenceOverflow> {
        self.sequence
            .checked_add(1)
            .map(|sequence| Self { sequence, kind })
            .ok_or_else(|| RuntimeSequenceOverflow::new("AmmRuntimeEvent.sequence"))
    }

    /// Observer sequence.
    pub const fn sequence(&self) -> u64 {
        self.sequence
    }

    /// Typed event payload.
    pub const fn kind(&self) -> &AmmRuntimeEventKind {
        &self.kind
    }
}