arkhe-forge-platform 0.15.0

L2 services for ArkheForge Runtime: projection observer, manifest loader, policy, rate limiter, audit receipts, crypto-erasure coordinator, process-protection shim. Builds on L0 arkhe-kernel + L1 arkhe-forge-core.
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
//! L2 Projection observer pipeline.
//!
//! L0 emits deterministic events; L2 turns those events into denormalized
//! read-model rows that PG (or another store) serves to higher layers. This
//! module exposes: the [`Projection`] trait, a
//! [`ProjectionRouter`] that dispatches [`EventRecord`]s by `TypeCode`,
//! a [`ProjectionStore`] abstraction, an in-memory store, and active /
//! passive / draining lifecycle transitions.
//!
//! The PG-backed store, the L0 observer bridge, and the
//! `kernel_projection_state` chain-anchored view route through the trait surface.

use core::marker::PhantomData;
use std::collections::HashMap;

use arkhe_forge_core::activity::{ActivityId, ActivityRecord, EntityShellId};
use arkhe_forge_core::actor::{ActorId, ActorProfile, UserBinding};
use arkhe_forge_core::brand::ShellId;
use arkhe_forge_core::context::EventRecord;
use arkhe_forge_core::entry::{EntryBody, EntryCore, EntryId, EntryParentDepth};
use arkhe_forge_core::event::{ArkheEvent, CrossShellActivity};
use arkhe_forge_core::space::{ParentChainDepth, SpaceConfig, SpaceId, SpaceMembership};
use arkhe_kernel::abi::{InstanceId, Tick, TypeCode};
use bytes::Bytes;
use serde::{Deserialize, Serialize};

use crate::manifest::ManifestSnapshot;

// ===================== Lifecycle + Context + Errors =====================

/// Observer worker lifecycle (active-passive HA).
///
/// * `Passive` — read-only secondary. Consumes events for warm standby but
///   does not commit writes upstream.
/// * `Active` — primary writer. The only projection state permitted to call
///   `ProjectionStore` mutators.
/// * `Draining` — graceful shutdown. Rejects new work, flushes in-flight.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ObserverState {
    /// Not primary; events are observed but writes are blocked.
    Passive,
    /// Primary writer.
    Active,
    /// Winding down — rejects new work.
    Draining,
}

/// Outcome of an auto-promote policy evaluation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum PromotionDecision {
    /// Policy + elapsed time justify a `Passive → Active` transition.
    Promote,
    /// Policy requires additional wait / manual operator approval.
    Wait,
}

/// Minimum number of KMS health channels that must report `Healthy` for the
/// `after_60min` auto-promote policy to clear its guardrail. Matches the
/// default 2-of-3 quorum — operators that provision more channels
/// can re-tune via a future manifest field.
pub const HF2_HEALTH_QUORUM_MIN: usize = 2;

/// Per-dispatch context carried alongside an [`EventRecord`]. The `'i`
/// lifetime reserves the slot that binds to the L0
/// `Effect<'i, Authorized>` borrow, and also scopes the optional manifest
/// snapshot reference.
pub struct ProjectionContext<'i> {
    /// Tick at which the event is being applied.
    pub tick: Tick,
    /// Runtime instance identifier.
    pub instance_id: InstanceId,
    /// Active manifest snapshot, if one has been loaded. `None` is legal
    /// for Tier-0 dev bootstrap paths that run before the first manifest
    /// has been emitted via `RuntimeBootstrap`.
    pub manifest: Option<&'i ManifestSnapshot>,
    _phantom: PhantomData<&'i ()>,
}

impl<'i> ProjectionContext<'i> {
    /// Construct a projection dispatch context without a manifest.
    #[inline]
    #[must_use]
    pub fn new(tick: Tick, instance_id: InstanceId) -> Self {
        Self {
            tick,
            instance_id,
            manifest: None,
            _phantom: PhantomData,
        }
    }

    /// Construct a projection dispatch context with an attached manifest
    /// snapshot. Callers that have loaded a manifest should use this path
    /// so projection workers can key on shell policy (tier, cipher, etc.).
    #[inline]
    #[must_use]
    pub fn with_manifest(
        tick: Tick,
        instance_id: InstanceId,
        manifest: &'i ManifestSnapshot,
    ) -> Self {
        Self {
            tick,
            instance_id,
            manifest: Some(manifest),
            _phantom: PhantomData,
        }
    }
}

/// Projection-side failure taxonomy.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ProjectionError {
    /// The event stream moved backward (corruption, mis-ordered redelivery,
    /// or two computes sharing one tick — see
    /// [`ProjectionRouter::dispatch`]).
    #[error("projection stream backward: last {last:?}, incoming {incoming:?}")]
    SequenceBackward {
        /// Stream head the incoming event was ordered against — the last
        /// accepted event or, when a failed dispatch is awaiting retry,
        /// the pinned unapplied position.
        last: ProjectionCursor,
        /// Position of the rejected incoming event.
        incoming: ProjectionCursor,
    },

    /// The stream skipped an event — a same-tick sequence jump, or a new
    /// tick whose batch does not start at sequence 0. The replay harness
    /// needs to fetch the missing range before dispatch can advance.
    #[error("projection stream gap: last {last:?}, incoming {incoming:?}")]
    SequenceGap {
        /// Stream head the incoming event was ordered against — the last
        /// accepted event or, when a failed dispatch is awaiting retry,
        /// the pinned unapplied position.
        last: ProjectionCursor,
        /// Position of the event that exposed the gap.
        incoming: ProjectionCursor,
    },

    /// A DIFFERENT event arrived at a position the router has already
    /// pinned — the accepted cursor position, or a failed dispatch's
    /// pending-retry pin. Two same-tick computes collided on the
    /// `(tick, sequence)` identity (driver contract violation),
    /// distinguished from a true redelivery/retry by event identity
    /// (type code + payload).
    #[error("projection stream position conflict at {at:?}")]
    PositionConflict {
        /// The contested stream position.
        at: ProjectionCursor,
    },

    /// Caller attempted a mutation in a non-`Active` state (observer is
    /// Passive or Draining).
    #[error("observer not active: current state {state:?}")]
    NotActive {
        /// Observer state at the time of the attempted mutation.
        state: ObserverState,
    },

    /// Storage-layer error (in-memory corruption, PG driver, …).
    #[error("projection storage error: {0}")]
    Storage(&'static str),

    /// Event payload failed to decode.
    #[error("event decode failed: {0}")]
    DecodeFailed(&'static str),

    /// An event targeted an Actor / Space / Entry / Activity that the
    /// projection has no row for.
    #[error("projection row missing")]
    MissingRow,
}

// ===================== Projection trait =====================

/// L2 projection worker. Each implementor owns a read-model view that is
/// kept in sync with the L1 event stream for a specific set of `TypeCode`s.
///
/// Implementors must be `Send + Sync` so the `ProjectionRouter` can run
/// across worker threads. Stream dedup / gap detection is centralised in
/// the router's own cursor; the router additionally consults
/// [`Projection::last_applied`] to skip events a projection has already
/// applied (catch-up after a router rebuild or a partially failed
/// fan-out).
pub trait Projection: Send + Sync {
    /// TypeCodes this projection observes — the router filters incoming
    /// events against this slice.
    fn observes(&self) -> &[TypeCode];

    /// Apply an event. Called only after router-side dedup + gap checks
    /// have succeeded. Implementations must:
    ///
    /// 1. Update their internal view state.
    /// 2. Bump `last_applied` to the event's `(sequence, tick)`.
    fn on_event(
        &mut self,
        event: &EventRecord,
        ctx: &ProjectionContext<'_>,
    ) -> Result<(), ProjectionError>;

    /// React to a worker-state transition (Passive ↔ Active ↔ Draining).
    /// Default is no-op.
    fn on_state_change(&mut self, _new_state: ObserverState) -> Result<(), ProjectionError> {
        Ok(())
    }

    /// Last `(sequence, tick)` applied — `None` if the projection is fresh.
    /// The router skips any event at or before this position (tick-major
    /// order), so the `on_event` bump is what makes application
    /// at-most-once across redeliveries.
    fn last_applied(&self) -> Option<(u64, Tick)>;
}

// ===================== Projection view structs =====================

/// Composite event-stream position. `EventRecord.sequence` is
/// per-compute — it restarts at 0 for every action's `ActionContext` — so
/// an event is identified in the stream by the `(tick, sequence)` pair,
/// ordered tick-major.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
pub struct ProjectionCursor {
    /// Sequence number within the emitting compute's batch.
    pub sequence: u64,
    /// Tick at which the event was emitted.
    pub tick: Tick,
}

/// Actor-facing read-model row — `ActorProfile` + optional `UserBinding`.
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct ActorProjection {
    /// Wire schema version.
    pub schema_version: u16,
    /// Actor identity.
    pub actor_id: ActorId,
    /// Authoritative `ActorProfile` Component.
    pub profile: ActorProfile,
    /// `UserBinding` is present iff the actor is `Authenticated` (E-actor-2).
    pub user_binding: Option<UserBinding>,
    /// Event cursor — dedup / gap anchor.
    pub cursor: Option<ProjectionCursor>,
}

/// Space read-model row — `SpaceConfig` + parent-chain cache + membership.
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct SpaceProjection {
    /// Wire schema version.
    pub schema_version: u16,
    /// Space identity.
    pub space_id: SpaceId,
    /// Authoritative `SpaceConfig` Component.
    pub config: SpaceConfig,
    /// Cached parent-chain depth (E-space-4 O(1)).
    pub parent_chain_depth: Option<ParentChainDepth>,
    /// Membership list for PrivateInvite Spaces.
    pub membership: Option<SpaceMembership>,
    /// Event cursor.
    pub cursor: Option<ProjectionCursor>,
}

/// Entry read-model row — `EntryCore` + body metadata + depth cache.
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct EntryProjection {
    /// Wire schema version.
    pub schema_version: u16,
    /// Entry identity.
    pub entry_id: EntryId,
    /// Authoritative `EntryCore` Component.
    pub core: EntryCore,
    /// `EntryBody` — absent when soft-deleted (E-entry-5).
    pub body: Option<EntryBody>,
    /// Cached parent-chain depth (E-entry-3).
    pub parent_depth: Option<EntryParentDepth>,
    /// Event cursor.
    pub cursor: Option<ProjectionCursor>,
}

/// Activity read-model row — `ActivityRecord` + optional Extension-target
/// shell marker.
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct ActivityProjection {
    /// Wire schema version.
    pub schema_version: u16,
    /// Activity identity.
    pub activity_id: ActivityId,
    /// Authoritative `ActivityRecord` Component.
    pub record: ActivityRecord,
    /// Shell marker for Extension-target Activities (E-act-7).
    pub entity_shell_id: Option<EntityShellId>,
    /// Event cursor.
    pub cursor: Option<ProjectionCursor>,
}

// ===================== ProjectionStore =====================

/// Storage abstraction for projection rows. An in-memory
/// implementation ships with this crate; PG-backed storage plugs in
/// at the L2 service layer.
pub trait ProjectionStore: Send + Sync {
    /// Upsert an Actor row. `Active` observer only.
    fn upsert_actor(&mut self, row: &ActorProjection) -> Result<(), ProjectionError>;

    /// Upsert a Space row. `Active` observer only.
    fn upsert_space(&mut self, row: &SpaceProjection) -> Result<(), ProjectionError>;

    /// Upsert an Entry row. `Active` observer only.
    fn upsert_entry(&mut self, row: &EntryProjection) -> Result<(), ProjectionError>;

    /// Upsert an Activity row. `Active` observer only.
    fn upsert_activity(&mut self, row: &ActivityProjection) -> Result<(), ProjectionError>;

    /// Read an Actor row.
    fn get_actor(&self, actor_id: ActorId) -> Option<ActorProjection>;
    /// Read a Space row.
    fn get_space(&self, space_id: SpaceId) -> Option<SpaceProjection>;
    /// Read an Entry row.
    fn get_entry(&self, entry_id: EntryId) -> Option<EntryProjection>;
    /// Read an Activity row.
    fn get_activity(&self, activity_id: ActivityId) -> Option<ActivityProjection>;
}

/// In-memory [`ProjectionStore`] — intended for tests and Tier-0 dev runs.
#[derive(Debug, Default)]
pub struct InMemoryProjectionStore {
    actors: HashMap<ActorId, ActorProjection>,
    spaces: HashMap<SpaceId, SpaceProjection>,
    entries: HashMap<EntryId, EntryProjection>,
    activities: HashMap<ActivityId, ActivityProjection>,
}

impl InMemoryProjectionStore {
    /// Construct an empty store.
    #[inline]
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }
}

impl ProjectionStore for InMemoryProjectionStore {
    fn upsert_actor(&mut self, row: &ActorProjection) -> Result<(), ProjectionError> {
        self.actors.insert(row.actor_id, row.clone());
        Ok(())
    }
    fn upsert_space(&mut self, row: &SpaceProjection) -> Result<(), ProjectionError> {
        self.spaces.insert(row.space_id, row.clone());
        Ok(())
    }
    fn upsert_entry(&mut self, row: &EntryProjection) -> Result<(), ProjectionError> {
        self.entries.insert(row.entry_id, row.clone());
        Ok(())
    }
    fn upsert_activity(&mut self, row: &ActivityProjection) -> Result<(), ProjectionError> {
        self.activities.insert(row.activity_id, row.clone());
        Ok(())
    }
    fn get_actor(&self, actor_id: ActorId) -> Option<ActorProjection> {
        self.actors.get(&actor_id).cloned()
    }
    fn get_space(&self, space_id: SpaceId) -> Option<SpaceProjection> {
        self.spaces.get(&space_id).cloned()
    }
    fn get_entry(&self, entry_id: EntryId) -> Option<EntryProjection> {
        self.entries.get(&entry_id).cloned()
    }
    fn get_activity(&self, activity_id: ActivityId) -> Option<ActivityProjection> {
        self.activities.get(&activity_id).cloned()
    }
}

// ===================== Router =====================

/// Event-stream dispatcher. Matches incoming events to registered
/// projections by `TypeCode`, enforces stream-order dedup + gap detection
/// via a router-held `(tick, sequence)` cursor, and propagates observer
/// state transitions.
pub struct ProjectionRouter {
    projections: Vec<Box<dyn Projection>>,
    /// `TypeCode -> projection indices that observe it`, built at
    /// `register()` time. Indices are stored in registration order, so
    /// dispatch visits matching projections in the same order a linear scan
    /// would — dispatch-order semantics are preserved (see `dispatch`).
    observers_by_code: HashMap<TypeCode, Vec<usize>>,
    /// Position of the last event accepted by `dispatch`, including events
    /// no projection observes — gap detection needs the full stream, not
    /// the per-projection filtered view. Advanced only after a fully
    /// successful fan-out so a failed dispatch stays retryable.
    cursor: Option<ProjectionCursor>,
    /// Identity (type code + payload) of the last accepted event — lets
    /// the cursor-equality path distinguish a true redelivery (`Ok(0)`)
    /// from a DIFFERENT event colliding on the same position
    /// ([`ProjectionError::PositionConflict`]).
    last_event: Option<(u32, Bytes)>,
    /// Position + identity (type code, payload) of the event a failed
    /// fan-out left unapplied — set on every failed fan-out (a
    /// projection apply error), first or mid-stream; ordering
    /// rejections never move it (re-pinning to a stray position would
    /// deadlock the legitimate retry). Only a retry of this exact
    /// EVENT may proceed: a skip-ahead would silently advance past the
    /// lost event, and a DIFFERENT event at the same position would
    /// conflate same-tick compute streams.
    pending_retry: Option<(ProjectionCursor, u32, Bytes)>,
    state: ObserverState,
}

impl ProjectionRouter {
    /// Build a router in the `Passive` state — promote to `Active` before
    /// committing writes.
    #[inline]
    #[must_use]
    pub fn new() -> Self {
        Self {
            projections: Vec::new(),
            observers_by_code: HashMap::new(),
            cursor: None,
            last_event: None,
            pending_retry: None,
            state: ObserverState::Passive,
        }
    }

    /// Register a projection. Its observed `TypeCode`s are folded into the
    /// dispatch index so `dispatch` only visits matching projections.
    pub fn register(&mut self, projection: Box<dyn Projection>) {
        let idx = self.projections.len();
        for &tc in projection.observes() {
            self.observers_by_code.entry(tc).or_default().push(idx);
        }
        self.projections.push(projection);
    }

    /// Current observer state.
    #[inline]
    #[must_use]
    pub fn state(&self) -> ObserverState {
        self.state
    }

    /// Transition to `Active` — fails if currently `Draining`.
    pub fn promote_to_active(&mut self) -> Result<(), ProjectionError> {
        self.transition(ObserverState::Active)
    }

    /// Evaluate the shell's `kms_auto_promote` policy against three inputs:
    /// the elapsed outage, the multi-channel KMS health quorum, and the
    /// threshold-HSM share readiness.
    ///
    /// Policy values:
    ///
    /// | `kms_auto_promote`  | Decision matrix |
    /// |---------------------|-----------------|
    /// | `"manual"`          | Always `Some(Wait)` — operator drives the promotion manually. |
    /// | `"after_60min"`     | `Some(Promote)` iff `primary_down_duration >= 1h` **and** the KMS health quorum has at least [`HF2_HEALTH_QUORUM_MIN`] channels healthy; otherwise `Some(Wait)`. |
    /// | `"threshold_hsm"`   | `Some(Promote)` iff `threshold_ready` (t-of-n shares collected); otherwise `Some(Wait)`. |
    /// | other               | `None` — unrecognised policy string, operator intervention required. |
    ///
    /// Returning `None` is conservative by design: callers treat unknown
    /// policies as "do not auto-promote" and fall back to manual operator
    /// action.
    #[must_use]
    pub fn evaluate_auto_promote(
        &self,
        manifest: &crate::manifest::ManifestSnapshot,
        primary_down_duration: core::time::Duration,
        health: &crate::hf2_kms::health::MultiChannelHealth,
        threshold_ready: bool,
    ) -> Option<PromotionDecision> {
        match manifest.audit.kms_auto_promote.as_str() {
            "manual" => Some(PromotionDecision::Wait),
            "after_60min" => {
                let elapsed_ok = primary_down_duration >= core::time::Duration::from_secs(60 * 60);
                let health_ok = health.healthy_count() >= HF2_HEALTH_QUORUM_MIN;
                if elapsed_ok && health_ok {
                    Some(PromotionDecision::Promote)
                } else {
                    Some(PromotionDecision::Wait)
                }
            }
            "threshold_hsm" => {
                if threshold_ready {
                    Some(PromotionDecision::Promote)
                } else {
                    Some(PromotionDecision::Wait)
                }
            }
            _ => None,
        }
    }

    /// Transition to `Passive`. Used when ceding primary to a peer.
    pub fn demote_to_passive(&mut self) -> Result<(), ProjectionError> {
        self.transition(ObserverState::Passive)
    }

    /// Transition to `Draining`. Terminal — no further state changes.
    pub fn begin_draining(&mut self) -> Result<(), ProjectionError> {
        self.transition(ObserverState::Draining)
    }

    fn transition(&mut self, next: ObserverState) -> Result<(), ProjectionError> {
        // Draining is terminal: once a graceful shutdown begins, no
        // transition path (including Draining → Passive → Active) may
        // resurrect the router. Gating here covers every public
        // transition method with a single guard.
        if self.state == ObserverState::Draining {
            return Err(ProjectionError::NotActive { state: self.state });
        }
        for p in &mut self.projections {
            p.on_state_change(next)?;
        }
        self.state = next;
        Ok(())
    }

    /// Dispatch an event to every matching projection. Returns `Ok(n)`
    /// where `n` is the number of projections that applied the event.
    ///
    /// Matching projections are visited in registration order (the dispatch
    /// index stores their indices in that order), so a projection registered
    /// earlier always sees an event before one registered later — callers may
    /// rely on that ordering. A `TypeCode` with no registered observer is a
    /// cheap miss (no index entry) returning `Ok(0)` — the event still
    /// advances the stream cursor.
    ///
    /// ## Stream-order contract
    ///
    /// `EventRecord.sequence` is per-compute: every compute's batch starts
    /// at 0 and is contiguous, so an event's stream identity is the
    /// composite `(tick, sequence)` pair, ordered tick-major. Callers must
    /// feed every drained event of a compute in emission order and must
    /// advance the tick between computes. The kernel scheduler permits
    /// same-tick scheduled actions, but two same-tick computes emit
    /// colliding positions — this router rejects the collision loudly
    /// rather than conflating the streams: a colliding head behind the
    /// cursor is [`ProjectionError::SequenceBackward`], and a DIFFERENT
    /// event at exactly the cursor position (the single-event-batch
    /// shape) is [`ProjectionError::PositionConflict`], distinguished
    /// from a true redelivery by event identity (type code + payload).
    ///
    /// Checks against the stream cursor (last accepted event):
    ///
    /// * the same `(tick, sequence)` redelivered with the same identity —
    ///   duplicate, `Ok(0)`;
    /// * a different event at the cursor position —
    ///   [`ProjectionError::PositionConflict`];
    /// * a position behind the cursor —
    ///   [`ProjectionError::SequenceBackward`];
    /// * a same-tick sequence jump, or a new tick whose batch does not
    ///   start at sequence 0 — [`ProjectionError::SequenceGap`];
    /// * a fresh router (no cursor yet) accepts any position as its
    ///   resume anchor.
    ///
    /// The cursor advances only after a fully successful fan-out. A
    /// FAILED fan-out (first or mid-stream) pins the unapplied event's
    /// position AND identity: only a retry of that exact event may
    /// proceed — a skip-ahead is a gap (it would silently advance past
    /// the lost event), a different event at the pinned position is a
    /// [`ProjectionError::PositionConflict`] — and the projections that
    /// already applied it before the failure are skipped on the retry
    /// via [`Projection::last_applied`].
    ///
    /// Only the `Active` state may dispatch — `Passive` and `Draining`
    /// reject with [`ProjectionError::NotActive`]. The `Passive` rejection
    /// is the production guardrail for active-passive HA: a secondary
    /// observer that mistakenly accepts writes would create split-brain
    /// rows in the PG-backed store. The dylint `arkhe-trait-default-check`
    /// CI gate ensures the contract is honoured by every L2 deployment.
    pub fn dispatch(
        &mut self,
        event: &EventRecord,
        ctx: &ProjectionContext<'_>,
    ) -> Result<usize, ProjectionError> {
        if self.state != ObserverState::Active {
            return Err(ProjectionError::NotActive { state: self.state });
        }
        let incoming = ProjectionCursor {
            sequence: event.sequence,
            tick: event.tick,
        };
        if let Some(last) = self.cursor {
            if incoming == last {
                // Same position: a true redelivery is a no-op, but a
                // DIFFERENT event here means two same-tick computes
                // collided on the cursor — never conflate silently.
                let is_redelivery = self.last_event.as_ref().is_some_and(|(tc, payload)| {
                    *tc == event.type_code && *payload == event.payload
                });
                if is_redelivery {
                    return Ok(0);
                }
                return Err(ProjectionError::PositionConflict { at: incoming });
            }
        }
        if let Some((pending, pending_tc, pending_payload)) = &self.pending_retry {
            // A prior dispatch failed at `pending` (first or mid-stream)
            // and was never applied — accept only the retry of that exact
            // EVENT: a skip-ahead would lose it silently, and a different
            // event at the same position would conflate same-tick compute
            // streams. The pending position already passed the ordering
            // checks against the cursor when it first arrived.
            let pending = *pending;
            if (incoming.tick, incoming.sequence) < (pending.tick, pending.sequence) {
                return Err(ProjectionError::SequenceBackward {
                    last: pending,
                    incoming,
                });
            }
            if incoming != pending {
                return Err(ProjectionError::SequenceGap {
                    last: pending,
                    incoming,
                });
            }
            if *pending_tc != event.type_code || *pending_payload != event.payload {
                return Err(ProjectionError::PositionConflict { at: incoming });
            }
        } else if let Some(last) = self.cursor {
            if (incoming.tick, incoming.sequence) < (last.tick, last.sequence) {
                return Err(ProjectionError::SequenceBackward { last, incoming });
            }
            let contiguous = if incoming.tick == last.tick {
                incoming.sequence == last.sequence.saturating_add(1)
            } else {
                // A new tick means a new compute whose sequence restarts
                // at 0 — anything else is a missing batch head.
                incoming.sequence == 0
            };
            if !contiguous {
                return Err(ProjectionError::SequenceGap { last, incoming });
            }
        }
        let tc = TypeCode(event.type_code);
        // Split-borrow the disjoint fields: the index slice (immutable) and
        // the projection vec (mutable) are borrowed independently so the loop
        // can mutate `projections[i]` while reading the matching index list.
        let Self {
            projections,
            observers_by_code,
            cursor,
            last_event,
            pending_retry,
            ..
        } = self;
        // Indices are already in registration order.
        let mut applied = 0usize;
        if let Some(matching) = observers_by_code.get(&tc) {
            for &i in matching {
                let p = &mut projections[i];
                if let Some((last_seq, last_tick)) = p.last_applied() {
                    // Already applied — catch-up redelivery into a rebuilt
                    // router or a retry after a partial fan-out failure.
                    if (incoming.tick, incoming.sequence) <= (last_tick, last_seq) {
                        continue;
                    }
                }
                if let Err(e) = p.on_event(event, ctx) {
                    *pending_retry = Some((incoming, event.type_code, event.payload.clone()));
                    return Err(e);
                }
                applied += 1;
            }
        }
        *cursor = Some(incoming);
        *last_event = Some((event.type_code, event.payload.clone()));
        *pending_retry = None;
        Ok(applied)
    }
}

impl Default for ProjectionRouter {
    fn default() -> Self {
        Self::new()
    }
}

// ===================== CrossShellActivity fanout =====================

/// Read-only fanout projection for `CrossShellActivity`. Stores cross-shell
/// notifications keyed by the target shell — never touches the source
/// shell's rows, preserving shell isolation (E-act-2 RA tier).
#[derive(Debug)]
pub struct CrossShellActivityFanout {
    observes: [TypeCode; 1],
    by_target_shell: HashMap<ShellId, Vec<CrossShellActivity>>,
    cursor: Option<ProjectionCursor>,
}

impl Default for CrossShellActivityFanout {
    fn default() -> Self {
        Self::new()
    }
}

impl CrossShellActivityFanout {
    /// Construct a fresh fanout, pre-wired to observe `CrossShellActivity`.
    #[inline]
    #[must_use]
    pub fn new() -> Self {
        Self {
            observes: [TypeCode(CrossShellActivity::TYPE_CODE)],
            by_target_shell: HashMap::new(),
            cursor: None,
        }
    }

    /// Borrow the notification queue for a shell (read-only).
    #[inline]
    #[must_use]
    pub fn notifications_for(&self, shell: &ShellId) -> &[CrossShellActivity] {
        self.by_target_shell
            .get(shell)
            .map(Vec::as_slice)
            .unwrap_or(&[])
    }
}

impl Projection for CrossShellActivityFanout {
    fn observes(&self) -> &[TypeCode] {
        &self.observes
    }

    fn on_event(
        &mut self,
        event: &EventRecord,
        _ctx: &ProjectionContext<'_>,
    ) -> Result<(), ProjectionError> {
        let notice: CrossShellActivity = postcard::from_bytes(&event.payload)
            .map_err(|_| ProjectionError::DecodeFailed("CrossShellActivity payload"))?;
        self.by_target_shell
            .entry(notice.target_shell_id)
            .or_default()
            .push(notice);
        self.cursor = Some(ProjectionCursor {
            sequence: event.sequence,
            tick: event.tick,
        });
        Ok(())
    }

    fn last_applied(&self) -> Option<(u64, Tick)> {
        self.cursor.map(|c| (c.sequence, c.tick))
    }
}

// ===================== Tests =====================

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc;

    use super::*;
    use arkhe_forge_core::actor::ActorKind;
    use arkhe_forge_core::component::BoundedString;
    use arkhe_kernel::abi::EntityId;
    use bytes::Bytes;

    fn sid(byte: u8) -> ShellId {
        ShellId([byte; 16])
    }

    /// Counts `on_event` applications — observability for fan-out and
    /// at-most-once assertions once the projection is boxed into a router.
    struct CountingProjection {
        observes: [TypeCode; 1],
        cursor: Option<ProjectionCursor>,
        applied: Arc<AtomicUsize>,
    }

    impl CountingProjection {
        fn new(applied: Arc<AtomicUsize>) -> Self {
            Self {
                observes: [TypeCode(CrossShellActivity::TYPE_CODE)],
                cursor: None,
                applied,
            }
        }
    }

    impl Projection for CountingProjection {
        fn observes(&self) -> &[TypeCode] {
            &self.observes
        }

        fn on_event(
            &mut self,
            event: &EventRecord,
            _ctx: &ProjectionContext<'_>,
        ) -> Result<(), ProjectionError> {
            self.applied.fetch_add(1, Ordering::SeqCst);
            self.cursor = Some(ProjectionCursor {
                sequence: event.sequence,
                tick: event.tick,
            });
            Ok(())
        }

        fn last_applied(&self) -> Option<(u64, Tick)> {
            self.cursor.map(|c| (c.sequence, c.tick))
        }
    }

    /// Fails its first `on_event`, succeeds afterwards — drives the
    /// partial-fan-out retry path.
    struct FailOnceProjection {
        observes: [TypeCode; 1],
        cursor: Option<ProjectionCursor>,
        failed: bool,
    }

    impl FailOnceProjection {
        fn new() -> Self {
            Self {
                observes: [TypeCode(CrossShellActivity::TYPE_CODE)],
                cursor: None,
                failed: false,
            }
        }
    }

    impl Projection for FailOnceProjection {
        fn observes(&self) -> &[TypeCode] {
            &self.observes
        }

        fn on_event(
            &mut self,
            event: &EventRecord,
            _ctx: &ProjectionContext<'_>,
        ) -> Result<(), ProjectionError> {
            if !self.failed {
                self.failed = true;
                return Err(ProjectionError::Storage("injected first-apply failure"));
            }
            self.cursor = Some(ProjectionCursor {
                sequence: event.sequence,
                tick: event.tick,
            });
            Ok(())
        }

        fn last_applied(&self) -> Option<(u64, Tick)> {
            self.cursor.map(|c| (c.sequence, c.tick))
        }
    }

    fn ent(v: u64) -> EntityId {
        EntityId::new(v).unwrap()
    }

    fn make_cross_shell_event(seq: u64, tick: u64, target: ShellId) -> EventRecord {
        let notice = CrossShellActivity {
            schema_version: 1,
            actor: ActorId::new(ent(1)),
            target_shell_id: target,
            record_shell_id: sid(0xAA),
            detected_tick: Tick(tick),
        };
        EventRecord {
            type_code: CrossShellActivity::TYPE_CODE,
            sequence: seq,
            tick: Tick(tick),
            payload: Bytes::from(postcard::to_stdvec(&notice).unwrap()),
        }
    }

    fn ctx(tick: u64) -> ProjectionContext<'static> {
        ProjectionContext::new(Tick(tick), InstanceId::new(1).unwrap())
    }

    #[test]
    fn router_defaults_to_passive() {
        let r = ProjectionRouter::new();
        assert_eq!(r.state(), ObserverState::Passive);
    }

    #[test]
    fn router_promote_then_demote_then_drain() {
        let mut r = ProjectionRouter::new();
        r.promote_to_active().unwrap();
        assert_eq!(r.state(), ObserverState::Active);
        r.demote_to_passive().unwrap();
        assert_eq!(r.state(), ObserverState::Passive);
        r.begin_draining().unwrap();
        assert_eq!(r.state(), ObserverState::Draining);
        // Draining is terminal — EVERY transition out rejects through the
        // single gate: promote, the Draining → Passive resurrection edge,
        // and a repeated drain.
        assert!(r.promote_to_active().is_err());
        assert!(r.demote_to_passive().is_err());
        assert!(r.begin_draining().is_err());
        assert_eq!(r.state(), ObserverState::Draining);
    }

    #[test]
    fn cross_shell_fanout_routes_to_target_shell_only() {
        let mut r = ProjectionRouter::new();
        r.promote_to_active().unwrap();
        r.register(Box::new(CrossShellActivityFanout::new()));
        let target = sid(0x33);
        let ev = make_cross_shell_event(0, 100, target);
        let applied = r.dispatch(&ev, &ctx(100)).unwrap();
        assert_eq!(applied, 1);
    }

    /// #21 — the dispatch index must fan an event out to EVERY projection
    /// that observes its TypeCode, in registration order. Two fanouts both
    /// observing `CrossShellActivity` must each apply the same event.
    #[test]
    fn dispatcher_fans_out_to_all_matching_in_registration_order() {
        let mut r = ProjectionRouter::new();
        r.promote_to_active().unwrap();
        r.register(Box::new(CrossShellActivityFanout::new()));
        r.register(Box::new(CrossShellActivityFanout::new()));
        let target = sid(0x44);
        let ev = make_cross_shell_event(0, 100, target);
        let applied = r.dispatch(&ev, &ctx(100)).unwrap();
        assert_eq!(applied, 2, "both matching projections must apply the event");
    }

    /// #21 — a TypeCode with no registered observer is a cheap index miss
    /// (no entry) and returns Ok(0) without scanning any projection.
    #[test]
    fn dispatcher_unobserved_typecode_is_index_miss() {
        let mut r = ProjectionRouter::new();
        r.promote_to_active().unwrap();
        r.register(Box::new(CrossShellActivityFanout::new()));
        let other_event = EventRecord {
            type_code: 0x0003_0F02, // UserErasureScheduled — not observed
            sequence: 0,
            tick: Tick(1),
            payload: Bytes::new(),
        };
        assert_eq!(r.dispatch(&other_event, &ctx(1)).unwrap(), 0);
    }

    #[test]
    fn dispatcher_skips_projection_with_no_matching_observer() {
        let mut r = ProjectionRouter::new();
        r.promote_to_active().unwrap();
        r.register(Box::new(CrossShellActivityFanout::new()));
        let other_event = EventRecord {
            type_code: 0x0003_0F02, // UserErasureScheduled
            sequence: 0,
            tick: Tick(1),
            payload: Bytes::new(),
        };
        let applied = r.dispatch(&other_event, &ctx(1)).unwrap();
        assert_eq!(applied, 0, "non-observed TypeCode must not hit the fanout");
    }

    #[test]
    fn dispatcher_dedups_redelivered_event() {
        let mut r = ProjectionRouter::new();
        r.promote_to_active().unwrap();
        r.register(Box::new(CrossShellActivityFanout::new()));
        let target = sid(0x10);
        let ev = make_cross_shell_event(0, 5, target);
        r.dispatch(&ev, &ctx(5)).unwrap();
        let applied_again = r.dispatch(&ev, &ctx(5)).unwrap();
        assert_eq!(applied_again, 0, "redelivered (tick, sequence) must no-op");
    }

    #[test]
    fn dispatcher_rejects_same_tick_gap() {
        let mut r = ProjectionRouter::new();
        r.promote_to_active().unwrap();
        r.register(Box::new(CrossShellActivityFanout::new()));
        let target = sid(0x10);
        r.dispatch(&make_cross_shell_event(0, 5, target), &ctx(5))
            .unwrap();
        // Jump to sequence 2 within tick 5 — sequence 1 missing.
        let err = r
            .dispatch(&make_cross_shell_event(2, 5, target), &ctx(5))
            .unwrap_err();
        assert!(matches!(err, ProjectionError::SequenceGap { .. }));
    }

    #[test]
    fn dispatcher_rejects_new_tick_missing_batch_head() {
        let mut r = ProjectionRouter::new();
        r.promote_to_active().unwrap();
        r.register(Box::new(CrossShellActivityFanout::new()));
        let target = sid(0x10);
        r.dispatch(&make_cross_shell_event(0, 5, target), &ctx(5))
            .unwrap();
        // Tick 6 batch must start at sequence 0 — sequence 3 means the
        // head of the new compute's batch is missing.
        let err = r
            .dispatch(&make_cross_shell_event(3, 6, target), &ctx(6))
            .unwrap_err();
        assert!(matches!(err, ProjectionError::SequenceGap { .. }));
    }

    #[test]
    fn dispatcher_rejects_backward_sequence() {
        let mut r = ProjectionRouter::new();
        r.promote_to_active().unwrap();
        r.register(Box::new(CrossShellActivityFanout::new()));
        let target = sid(0x10);
        r.dispatch(&make_cross_shell_event(2, 5, target), &ctx(5))
            .unwrap();
        let err = r
            .dispatch(&make_cross_shell_event(1, 5, target), &ctx(5))
            .unwrap_err();
        assert!(matches!(err, ProjectionError::SequenceBackward { .. }));
    }

    #[test]
    fn dispatcher_rejects_backward_tick() {
        let mut r = ProjectionRouter::new();
        r.promote_to_active().unwrap();
        r.register(Box::new(CrossShellActivityFanout::new()));
        let target = sid(0x10);
        r.dispatch(&make_cross_shell_event(0, 5, target), &ctx(5))
            .unwrap();
        let err = r
            .dispatch(&make_cross_shell_event(0, 4, target), &ctx(4))
            .unwrap_err();
        assert!(matches!(err, ProjectionError::SequenceBackward { .. }));
    }

    /// Drivers must advance the tick between computes. If two computes
    /// share a tick, the second batch head `(tick, 0)` lands behind the
    /// cursor and must be rejected loudly — never silently dropped as a
    /// duplicate.
    #[test]
    fn same_tick_second_compute_head_rejected_loudly() {
        let mut r = ProjectionRouter::new();
        r.promote_to_active().unwrap();
        r.register(Box::new(CrossShellActivityFanout::new()));
        let target = sid(0x10);
        r.dispatch(&make_cross_shell_event(0, 5, target), &ctx(5))
            .unwrap();
        r.dispatch(&make_cross_shell_event(1, 5, target), &ctx(5))
            .unwrap();
        let err = r
            .dispatch(&make_cross_shell_event(0, 5, target), &ctx(5))
            .unwrap_err();
        assert!(matches!(err, ProjectionError::SequenceBackward { .. }));
    }

    /// Same-tick compute collision with SINGLE-event batches: the second
    /// compute's head lands exactly on the cursor, so the equality path
    /// must compare event identity — a DIFFERENT event there is a
    /// `PositionConflict`, never a silent `Ok(0)` "redelivery".
    #[test]
    fn same_tick_single_event_batches_conflict_loudly() {
        let mut r = ProjectionRouter::new();
        r.promote_to_active().unwrap();
        r.register(Box::new(CrossShellActivityFanout::new()));
        r.dispatch(&make_cross_shell_event(0, 5, sid(0x10)), &ctx(5))
            .unwrap();
        // Distinct payload (different target shell) at the same (5, 0).
        let err = r
            .dispatch(&make_cross_shell_event(0, 5, sid(0x20)), &ctx(5))
            .unwrap_err();
        assert!(matches!(
            err,
            ProjectionError::PositionConflict {
                at: ProjectionCursor {
                    sequence: 0,
                    tick: Tick(5)
                }
            }
        ));
    }

    /// A failed FIRST dispatch must not let a later position anchor the
    /// cursor silently past the lost event: only the exact retry may
    /// proceed.
    #[test]
    fn failed_first_dispatch_pins_the_anchor_until_retried() {
        let mut r = ProjectionRouter::new();
        r.promote_to_active().unwrap();
        r.register(Box::new(FailOnceProjection::new()));
        let ev = make_cross_shell_event(0, 5, sid(0x10));
        assert!(r.dispatch(&ev, &ctx(5)).is_err());
        // Skipping ahead past the failed event is a loud gap...
        let err = r
            .dispatch(&make_cross_shell_event(0, 6, sid(0x10)), &ctx(6))
            .unwrap_err();
        assert!(matches!(err, ProjectionError::SequenceGap { .. }));
        // ...while retrying the exact position succeeds and re-anchors.
        assert_eq!(r.dispatch(&ev, &ctx(5)).unwrap(), 1);
        assert_eq!(
            r.dispatch(&make_cross_shell_event(0, 6, sid(0x10)), &ctx(6))
                .unwrap(),
            1
        );
    }

    /// The failed-position pin holds the event IDENTITY too: a
    /// DIFFERENT event arriving at the never-accepted position is a
    /// `PositionConflict`, never silently absorbed as the "retry" —
    /// otherwise two same-tick computes would conflate through the
    /// failure window.
    #[test]
    fn failed_first_dispatch_rejects_a_different_event_at_the_pinned_position() {
        let mut r = ProjectionRouter::new();
        r.promote_to_active().unwrap();
        r.register(Box::new(FailOnceProjection::new()));
        let ev = make_cross_shell_event(0, 5, sid(0x10));
        assert!(r.dispatch(&ev, &ctx(5)).is_err());
        // Distinct payload (different target shell) at the pinned (5, 0).
        let err = r
            .dispatch(&make_cross_shell_event(0, 5, sid(0x20)), &ctx(5))
            .unwrap_err();
        assert!(matches!(
            err,
            ProjectionError::PositionConflict {
                at: ProjectionCursor {
                    sequence: 0,
                    tick: Tick(5)
                }
            }
        ));
        // The genuine retry still proceeds.
        assert_eq!(r.dispatch(&ev, &ctx(5)).unwrap(), 1);
    }

    /// The retry pin covers MID-STREAM failures, not only the first
    /// dispatch: after a successful anchor, a failed event must not be
    /// lost to a skip-ahead (the new-tick batch-head rule would
    /// otherwise accept it) nor replaced by a different same-position
    /// event.
    #[test]
    fn failed_mid_stream_dispatch_pins_the_retry() {
        struct FailSecondProjection {
            observes: [TypeCode; 1],
            cursor: Option<ProjectionCursor>,
            calls: usize,
        }
        impl Projection for FailSecondProjection {
            fn observes(&self) -> &[TypeCode] {
                &self.observes
            }
            fn on_event(
                &mut self,
                event: &EventRecord,
                _ctx: &ProjectionContext<'_>,
            ) -> Result<(), ProjectionError> {
                self.calls += 1;
                if self.calls == 2 {
                    return Err(ProjectionError::Storage("transient"));
                }
                self.cursor = Some(ProjectionCursor {
                    sequence: event.sequence,
                    tick: event.tick,
                });
                Ok(())
            }
            fn last_applied(&self) -> Option<(u64, Tick)> {
                self.cursor.map(|c| (c.sequence, c.tick))
            }
        }

        let mut r = ProjectionRouter::new();
        r.promote_to_active().unwrap();
        r.register(Box::new(FailSecondProjection {
            observes: [TypeCode(CrossShellActivity::TYPE_CODE)],
            cursor: None,
            calls: 0,
        }));
        assert_eq!(
            r.dispatch(&make_cross_shell_event(0, 5, sid(0x10)), &ctx(5))
                .unwrap(),
            1
        );
        let failed = make_cross_shell_event(0, 6, sid(0x10));
        assert!(r.dispatch(&failed, &ctx(6)).is_err());
        // Skip-ahead past the failed event is a loud gap (the new-tick
        // batch-head rule must not absorb it)...
        let err = r
            .dispatch(&make_cross_shell_event(0, 7, sid(0x10)), &ctx(7))
            .unwrap_err();
        assert!(matches!(err, ProjectionError::SequenceGap { .. }));
        // ...a different event at the pinned position is a conflict...
        let err = r
            .dispatch(&make_cross_shell_event(0, 6, sid(0x20)), &ctx(6))
            .unwrap_err();
        assert!(matches!(err, ProjectionError::PositionConflict { .. }));
        // ...and the genuine retry proceeds, after which the stream
        // continues normally.
        assert_eq!(r.dispatch(&failed, &ctx(6)).unwrap(), 1);
        assert_eq!(
            r.dispatch(&make_cross_shell_event(0, 7, sid(0x10)), &ctx(7))
                .unwrap(),
            1
        );
    }

    /// Two successive actions each get a fresh compute whose event sequence
    /// restarts at 0. The second `(tick+1, 0)` event is a distinct stream
    /// position — it must apply, not vanish as a "duplicate" of the first.
    #[test]
    fn successive_computes_restarting_sequence_zero_both_apply() {
        let applied = Arc::new(AtomicUsize::new(0));
        let mut r = ProjectionRouter::new();
        r.promote_to_active().unwrap();
        r.register(Box::new(CountingProjection::new(applied.clone())));
        assert_eq!(
            r.dispatch(&make_cross_shell_event(0, 100, sid(0x01)), &ctx(100))
                .unwrap(),
            1
        );
        assert_eq!(
            r.dispatch(&make_cross_shell_event(0, 101, sid(0x02)), &ctx(101))
                .unwrap(),
            1,
            "second compute's sequence-0 event must not be conflated"
        );
        assert_eq!(applied.load(Ordering::SeqCst), 2);
    }

    /// Gap detection is stream-level: an event nobody observes still
    /// advances the cursor, so a subsequent same-tick jump is a gap and
    /// the contiguous successor applies.
    #[test]
    fn unobserved_events_advance_stream_cursor() {
        let mut r = ProjectionRouter::new();
        r.promote_to_active().unwrap();
        r.register(Box::new(CrossShellActivityFanout::new()));
        let unobserved = EventRecord {
            type_code: 0x0003_0F02, // UserErasureScheduled — not observed
            sequence: 0,
            tick: Tick(5),
            payload: Bytes::new(),
        };
        assert_eq!(r.dispatch(&unobserved, &ctx(5)).unwrap(), 0);
        let err = r
            .dispatch(&make_cross_shell_event(2, 5, sid(0x10)), &ctx(5))
            .unwrap_err();
        assert!(matches!(err, ProjectionError::SequenceGap { .. }));
        assert_eq!(
            r.dispatch(&make_cross_shell_event(1, 5, sid(0x10)), &ctx(5))
                .unwrap(),
            1
        );
    }

    /// A projection carrying state from a previous run skips events at or
    /// before its `last_applied` when a rebuilt router replays the stream.
    #[test]
    fn rebuilt_router_skips_already_applied_events() {
        let applied = Arc::new(AtomicUsize::new(0));
        let mut p = CountingProjection::new(applied.clone());
        p.on_event(&make_cross_shell_event(0, 10, sid(0x01)), &ctx(10))
            .unwrap();
        assert_eq!(applied.load(Ordering::SeqCst), 1);

        let mut r = ProjectionRouter::new();
        r.promote_to_active().unwrap();
        r.register(Box::new(p));
        assert_eq!(
            r.dispatch(&make_cross_shell_event(0, 10, sid(0x01)), &ctx(10))
                .unwrap(),
            0,
            "catch-up redelivery must not double-apply"
        );
        assert_eq!(applied.load(Ordering::SeqCst), 1);
        assert_eq!(
            r.dispatch(&make_cross_shell_event(0, 11, sid(0x01)), &ctx(11))
                .unwrap(),
            1
        );
        assert_eq!(applied.load(Ordering::SeqCst), 2);
    }

    /// A projection failure mid-fan-out leaves the cursor unadvanced: the
    /// same event redispatches, projections that already applied it are
    /// skipped, and the failed projection gets its retry.
    #[test]
    fn failed_fanout_retry_does_not_double_apply() {
        let applied = Arc::new(AtomicUsize::new(0));
        let mut r = ProjectionRouter::new();
        r.promote_to_active().unwrap();
        r.register(Box::new(CountingProjection::new(applied.clone())));
        r.register(Box::new(FailOnceProjection::new()));
        let ev = make_cross_shell_event(0, 5, sid(0x10));
        assert!(r.dispatch(&ev, &ctx(5)).is_err());
        assert_eq!(applied.load(Ordering::SeqCst), 1);
        assert_eq!(
            r.dispatch(&ev, &ctx(5)).unwrap(),
            1,
            "retry applies only the previously failed projection"
        );
        assert_eq!(applied.load(Ordering::SeqCst), 1);
    }

    #[test]
    fn draining_rejects_dispatch() {
        let mut r = ProjectionRouter::new();
        r.begin_draining().unwrap();
        let err = r
            .dispatch(&make_cross_shell_event(0, 1, sid(0)), &ctx(1))
            .unwrap_err();
        assert!(matches!(
            err,
            ProjectionError::NotActive {
                state: ObserverState::Draining
            }
        ));
    }

    #[test]
    fn passive_rejects_dispatch() {
        // `ProjectionRouter::new()` starts in Passive — dispatch must reject
        // until the worker is promoted, otherwise an active-passive HA pair
        // could create split-brain rows in the PG-backed store.
        let mut r = ProjectionRouter::new();
        assert_eq!(r.state(), ObserverState::Passive);
        let err = r
            .dispatch(&make_cross_shell_event(0, 1, sid(0)), &ctx(1))
            .unwrap_err();
        assert!(matches!(
            err,
            ProjectionError::NotActive {
                state: ObserverState::Passive
            }
        ));
    }

    #[test]
    fn demote_to_passive_blocks_subsequent_dispatch() {
        // After a successful Active dispatch, demoting back to Passive must
        // immediately stop accepting writes — covers the failover-back path.
        let mut r = ProjectionRouter::new();
        r.promote_to_active().unwrap();
        r.register(Box::new(CrossShellActivityFanout::new()));
        r.dispatch(&make_cross_shell_event(0, 5, sid(0x10)), &ctx(5))
            .unwrap();
        r.demote_to_passive().unwrap();
        let err = r
            .dispatch(&make_cross_shell_event(1, 6, sid(0x10)), &ctx(6))
            .unwrap_err();
        assert!(matches!(
            err,
            ProjectionError::NotActive {
                state: ObserverState::Passive
            }
        ));
    }

    #[test]
    fn in_memory_store_roundtrip_actor() {
        let mut store = InMemoryProjectionStore::new();
        let row = ActorProjection {
            schema_version: 1,
            actor_id: ActorId::new(ent(42)),
            profile: ActorProfile {
                schema_version: 1,
                shell_id: sid(0x01),
                handle: BoundedString::<32>::new("alice").unwrap(),
                kind: ActorKind::Human,
                created_tick: Tick(1),
            },
            user_binding: None,
            cursor: None,
        };
        store.upsert_actor(&row).unwrap();
        let fetched = store.get_actor(ActorId::new(ent(42))).unwrap();
        assert_eq!(fetched, row);
    }

    #[test]
    fn cross_shell_fanout_preserves_shell_partition() {
        let mut fanout = CrossShellActivityFanout::new();
        let shell_a = sid(0xAA);
        let shell_b = sid(0xBB);
        fanout
            .on_event(&make_cross_shell_event(0, 10, shell_a), &ctx(10))
            .unwrap();
        fanout
            .on_event(&make_cross_shell_event(1, 11, shell_b), &ctx(11))
            .unwrap();
        assert_eq!(fanout.notifications_for(&shell_a).len(), 1);
        assert_eq!(fanout.notifications_for(&shell_b).len(), 1);
        assert_eq!(fanout.last_applied(), Some((1, Tick(11))));
    }

    #[test]
    fn projection_cursor_roundtrip() {
        let c = ProjectionCursor {
            sequence: 5,
            tick: Tick(10),
        };
        let bytes = postcard::to_stdvec(&c).unwrap();
        let back: ProjectionCursor = postcard::from_bytes(&bytes).unwrap();
        assert_eq!(c, back);
    }

    /// Manifest auto-promote decision table.
    /// Run via a small helper that builds a `ManifestSnapshot` with a
    /// configurable `kms_auto_promote` string.
    #[test]
    fn auto_promote_policy_matrix() {
        use crate::hf2_kms::health::{Channel, MultiChannelHealth, Status};
        use crate::manifest::{
            AuditSection, FrontendSection, ManifestSnapshot, RuntimeSection, ShellSection,
        };
        use core::time::Duration;

        fn manifest_with(policy: &str) -> ManifestSnapshot {
            ManifestSnapshot {
                schema_version: 1,
                shell: ShellSection {
                    shell_id: "test".to_string(),
                    display_name: "Test".to_string(),
                },
                runtime: RuntimeSection {
                    runtime_max: "0.15".to_string(),
                    runtime_current: "0.13".to_string(),
                },
                audit: AuditSection {
                    pii_cipher: "xchacha20-poly1305".to_string(),
                    dek_backend: "software-kek".to_string(),
                    kms_auto_promote: policy.to_string(),
                    signature_class: "ed25519".to_string(),
                    compliance_tier: 0,
                },
                frontend: FrontendSection::default(),
            }
        }

        fn healthy_trio() -> MultiChannelHealth {
            let mut h = MultiChannelHealth::new(&[
                Channel::Default,
                Channel::DnsOverHttps,
                Channel::StaticIp,
            ]);
            for c in [Channel::Default, Channel::DnsOverHttps, Channel::StaticIp] {
                h.set_status(c, Status::Healthy);
            }
            h
        }

        fn degraded_trio() -> MultiChannelHealth {
            // Only one channel healthy — below 2/3 quorum.
            let mut h = MultiChannelHealth::new(&[
                Channel::Default,
                Channel::DnsOverHttps,
                Channel::StaticIp,
            ]);
            h.set_status(Channel::Default, Status::Healthy);
            h.set_status(Channel::DnsOverHttps, Status::Failing);
            h.set_status(Channel::StaticIp, Status::Failing);
            h
        }

        let r = ProjectionRouter::new();
        let healthy = healthy_trio();
        let degraded = degraded_trio();

        // Manual policy → always Wait (operator drives the promotion).
        assert_eq!(
            r.evaluate_auto_promote(
                &manifest_with("manual"),
                Duration::from_secs(7200),
                &healthy,
                true,
            ),
            Some(PromotionDecision::Wait),
        );

        // after_60min, short outage → Wait even with full health.
        assert_eq!(
            r.evaluate_auto_promote(
                &manifest_with("after_60min"),
                Duration::from_secs(59 * 60),
                &healthy,
                false,
            ),
            Some(PromotionDecision::Wait),
        );

        // after_60min, outage cleared but health below quorum → Wait.
        assert_eq!(
            r.evaluate_auto_promote(
                &manifest_with("after_60min"),
                Duration::from_secs(60 * 60),
                &degraded,
                false,
            ),
            Some(PromotionDecision::Wait),
        );

        // after_60min, outage cleared AND health quorum met → Promote.
        assert_eq!(
            r.evaluate_auto_promote(
                &manifest_with("after_60min"),
                Duration::from_secs(60 * 60),
                &healthy,
                false,
            ),
            Some(PromotionDecision::Promote),
        );

        // threshold_hsm, shares not collected → Wait.
        assert_eq!(
            r.evaluate_auto_promote(
                &manifest_with("threshold_hsm"),
                Duration::from_secs(60 * 60),
                &degraded,
                false,
            ),
            Some(PromotionDecision::Wait),
        );

        // threshold_hsm, shares collected → Promote (health is not gating here).
        assert_eq!(
            r.evaluate_auto_promote(
                &manifest_with("threshold_hsm"),
                Duration::from_secs(0),
                &degraded,
                true,
            ),
            Some(PromotionDecision::Promote),
        );

        // Unknown policy string → None (conservative default — operator must act).
        assert!(r
            .evaluate_auto_promote(
                &manifest_with("unknown"),
                Duration::from_secs(86_400),
                &healthy,
                true,
            )
            .is_none());
    }
}