yantrikdb-server 0.14.0

YantrikDB database server — multi-tenant cognitive memory with wire protocol, HTTP gateway, replication, auto-failover, and at-rest encryption
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
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
//! YRP replica safety core (RFC 028 v2 §2) — election + log replication,
//! pure logic, no I/O.
//!
//! Phase A1 delivered the election layer; Phase A1b (this revision) adds the
//! canonical prefix log, append continuity, conflict truncation, per-write
//! quorum confirmation, and the current-term commit rule. One state machine
//! owns all of it — split cores sharing term/role state is where
//! synchronization bugs live.
//!
//! ## The invariants this module owns
//!
//! **Gate A #1 — vote safety (scenario R2).** A node must never vote for two
//! candidates in one term, including across crash/restart. Enforced
//! structurally: any decision that changes durable state returns
//! [`Effect::Persist`] and *withholds* dependent messages; they are only
//! released by [`ReplicaCore::state_persisted`]. There is no API to obtain
//! the withheld messages without asserting durability first.
//!
//! **Gate A #2 — authority safety (scenario R1).** No two nodes may both
//! report durable success for conflicting writes. Mechanism: per-write quorum
//! confirmation. An entry is committed only when a quorum of voters has
//! *durably* accepted it (each acceptor's success `AppendResponse` is gated
//! behind its own persist — the same withhold mechanism), and only if the
//! entry is from the leader's **current term** (Raft §5.4.2: prior-term
//! entries commit solely by implication, via the no-op a new leader appends
//! on election). Term fencing rejects stale leaders at every acceptor; no
//! wall-clock assumption is load-bearing.
//!
//! **Gate A #3 — possibly-committed-suffix protection (scenario R3).**
//! Election freshness is Raft's last-`(term, index)` comparison over the
//! candidate's log — never a scalar watermark, never a committed frontier.
//!
//! ## Durability model
//!
//! [`Effect::Persist`] carries a full snapshot `(hard state, log)`. The pure
//! core favors an unarguable contract over an efficient encoding; the
//! production driver will persist deltas (append/truncate) with identical
//! semantics — the sim proves the semantics, the driver owns the encoding.
//! The driver persists the snapshot, then calls
//! [`ReplicaCore::state_persisted`] to obtain the withheld messages.
//! A crash before persist loses unsent messages (safe); a crash after
//! persist loses only responses (liveness, never safety).
//!
//! ## What is deliberately NOT here (yet)
//!
//! Quarantine + incarnation fencing (Phase A2); membership changes. The
//! voter set is fixed at construction. Timers are the driver's job.
//! (`payload` carries real engine-mutation bytes as of the
//! runtime-integration slice — see [`Payload`].)

use std::collections::{BTreeMap, BTreeSet};

use serde::{Deserialize, Serialize};

use super::types::{quorum, HardState, LogPosition, NodeId, Term};

/// The replicated command a log entry carries (runtime-integration slice —
/// this is where the opaque Phase A/B `u64` became real bytes).
///
/// Equality is load-bearing: the append-path duplicate check and the sim's
/// committed-entry ledgers compare entries structurally, so two ops are the
/// same entry iff their bytes are the same.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Payload {
    /// The no-op a new leader appends on election (§5.4.2 — lets prior-term
    /// entries commit by implication) and the carrier for pure
    /// capability-activation entries.
    Noop,
    /// Opaque numbered payload for the simulator and unit tests. Never
    /// produced on a production path.
    Test(u64),
    /// A serialized engine mutation (`crate::commit::MemoryMutation` via
    /// bincode) with the client-visible rid INSIDE the bytes (codex F3):
    /// a retry answered from the claims table recovers its outcome from
    /// the entry itself.
    Op(Vec<u8>),
    /// A serialized control-plane mutation (`super::control_op::ControlOp`,
    /// RFC 029) applied to `control.db` on every node — the replicated
    /// identity/authorization plane (tokens, databases). A *distinct*
    /// variant rather than a re-wrapping of [`Op`], so a mixed-version
    /// cluster keeps replicating **data** ops unchanged; only nodes that
    /// understand control ops are ever sent them (the operator upgrades
    /// every node before the first replicated control op is minted —
    /// RFC 029 bootstrap).
    Control(Vec<u8>),
}

/// Test-only ergonomic comparison against the sim's numeric payloads:
/// `entry.payload == 42` ⇔ the payload is `Payload::Test(42)`.
#[cfg(test)]
impl PartialEq<u64> for Payload {
    fn eq(&self, other: &u64) -> bool {
        matches!(self, Payload::Test(n) if n == other)
    }
}

/// One canonical-log entry. `payload` is the replicated command — engine
/// mutation bytes on the production path (embedding bytes, provenance, HLC
/// all inside), [`Payload::Noop`] for protocol-internal entries.
///
/// `key` (Phase B, RFC 028 §7): the idempotency claim, carried IN the entry
/// so claim and op are one atomic replicated unit — committed together,
/// truncated together, snapshot together. This is what makes the two
/// crash-scenario halves (pre-registered by the trading workspace, who
/// converged on the identical invariant independently) hold by
/// construction: a committed-but-unacked keyed entry survives failover and
/// dedupes the retry (claim never lost after commit); a tentative keyed
/// entry truncates WITH its claim, so the retry re-executes cleanly (no
/// settled-claim-without-effect ghost).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LogEntry {
    pub term: Term,
    pub payload: Payload,
    pub key: Option<u64>,
    /// Phase B (RFC 028 §3): capability-activation entry. Bits activate on
    /// COMMIT (never on append), monotonically — there is no deactivation.
    pub activate: Option<u32>,
}

impl LogEntry {
    /// Unkeyed entry (the common case; also every pre-Phase-B test entry).
    pub fn unkeyed(term: Term, payload: Payload) -> Self {
        Self {
            term,
            payload,
            key: None,
            activate: None,
        }
    }
}

/// Outcome of a keyed proposal at the leader's origin ingress (RFC 028 §7:
/// claims are checked at ORIGIN, carried in the log, and never re-gated at
/// apply).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum KeyedProposal {
    /// Fresh claim: the entry was appended at `index`; effects carry the
    /// persist + fan-out. The caller acks its client only when the commit
    /// index reaches `index` (quorum-durable tier — never before).
    Appended { index: u64, effects: Vec<Effect> },
    /// The key already claims `index`, and that entry is COMMITTED: a
    /// dedupe hit. The caller answers the retry with the ORIGINAL entry's
    /// outcome immediately — this is the "committed but client never
    /// learned" failover half.
    DuplicateCommitted { index: u64 },
    /// The key claims `index` but that entry is NOT yet committed (in
    /// flight, possibly from this same client's earlier attempt). The
    /// caller waits for commit (or its loss by truncation) — it must NOT
    /// append a second entry, and must NOT report success yet.
    DuplicatePending { index: u64 },
}

/// A log-compaction snapshot (RFC 028 §6, pure-model form). The production
/// manifest binds an engine checkpoint (content hash, membership epoch,
/// schema/capability/generation state); the pure core carries what the
/// PROTOCOL needs:
/// - `last`: the exact frontier the snapshot covers (entries ≤ last.index
///   are gone from the log);
/// - `claims`: every idempotency claim at or below the frontier — sol
///   P1-9's rule made structural: compaction may never create a window
///   where a GC'd claim can be replayed, so the claims RIDE the snapshot
///   exactly as they rode the entries.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Snapshot {
    pub last: LogPosition,
    pub claims: BTreeMap<u64, u64>,
    /// Active capability bits at the frontier — part of the snapshot's
    /// identity (codex F8): two states with identical bytes but different
    /// active semantics are NOT interchangeable.
    pub active: u32,
}

/// Wire messages for the replica layer.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Message {
    /// Liveness probe before a real election (never changes voter state).
    PreVoteRequest {
        term: Term,
        candidate: NodeId,
        last_log: LogPosition,
    },
    PreVoteResponse {
        term: Term,
        granted: bool,
    },
    VoteRequest {
        term: Term,
        candidate: NodeId,
        last_log: LogPosition,
        /// Candidate's capability set: a voter refuses candidates that
        /// cannot cover the voter's ACTIVE view (semantically incomplete
        /// leaders, sol r2 P1-14).
        supported: u32,
    },
    VoteResponse {
        term: Term,
        granted: bool,
    },
    /// Log replication + heartbeat (empty `entries`). `prev` is the position
    /// immediately before `entries`; `commit` is the leader's commit index.
    AppendEntries {
        term: Term,
        leader: NodeId,
        prev: LogPosition,
        entries: Vec<LogEntry>,
        commit: u64,
    },
    /// Snapshot install (leader → straggler whose next index fell below
    /// the leader's compaction base). Acked with a normal AppendResponse
    /// at `snapshot.last.index` once durable.
    InstallSnapshot {
        term: Term,
        leader: NodeId,
        snapshot: Snapshot,
    },
    /// `last_index` is the acceptor's last DURABLE matching index on
    /// success — the per-write quorum-confirmation signal. A success
    /// response for newly appended entries is sent only after they are
    /// persisted (gated), which is what makes counting it safe.
    AppendResponse {
        term: Term,
        success: bool,
        last_index: u64,
        /// Refusal reason: the batch (or snapshot) contained capability
        /// bits outside the acceptor's `supported` set. Distinguishable so
        /// the leader can stall + alarm instead of retransmit-storming
        /// (codex F1/F7) — a capability NACK is a config problem, not a
        /// log-divergence problem.
        unsupported: bool,
    },
}

/// Instructions to the driver.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Effect {
    /// Durably persist this `(hard, log)` snapshot, then call
    /// [`ReplicaCore::state_persisted`]. Multiple `Persist` effects in one
    /// batch coalesce: persisting the last one seen satisfies all of them.
    Persist {
        hard: HardState,
        base: LogPosition,
        log: Vec<LogEntry>,
        claims: BTreeMap<u64, u64>,
        /// Committed-active capability bits. Sound to persist eagerly:
        /// `active` only ever contains committed bits, and committed bits
        /// never revert — restoring them early merely shrinks the
        /// stale-low window (codex F5).
        active: u32,
    },
    Send {
        to: NodeId,
        msg: Message,
    },
    /// Send to every voter except self (driver expands the fan-out).
    Broadcast {
        msg: Message,
    },
    BecameLeader {
        term: Term,
    },
    SteppedDown {
        term: Term,
    },
    /// The commit index advanced to `to` (inclusive). The driver applies
    /// entries `(previous commit, to]` to the state machine, in order.
    CommitAdvanced {
        to: u64,
    },
    /// A snapshot was adopted: the driver replaces its applied state with
    /// the checkpoint at `last_index` (no per-entry replay — the entries
    /// are gone; the state arrives wholesale, like A2's rejoin adoption).
    InstallState {
        last_index: u64,
    },
    /// A peer NACKed replication for capability reasons: it cannot store
    /// bits the log now carries. The leader has STOPPED sending to it
    /// (no retransmit storm); the driver must alarm the operator — the
    /// peer needs a binary upgrade (then a new capability exchange clears
    /// the stall via set_peer_caps). Codex F1/F7.
    PeerIncompatible {
        peer: NodeId,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Role {
    Follower,
    PreCandidate,
    Candidate,
    Leader,
}

/// State staged for persistence, plus messages gated on that durability.
#[derive(Debug, Clone)]
struct Pending {
    hard: HardState,
    held: Vec<Effect>,
}

/// The pure replica state machine. See module docs for the contract.
pub struct ReplicaCore {
    id: NodeId,
    voters: BTreeSet<NodeId>,
    /// Last DURABLY PERSISTED hard state — survives a crash.
    persisted_hard: HardState,
    /// Durable log length (entries `1..=durable_len` survive a crash).
    durable_len: u64,
    /// Compaction base (RFC 028 §6): the last position covered by the
    /// adopted/created snapshot. Entries ≤ base.index are compacted away;
    /// `log[0]` holds absolute index `base.index + 1`. ZERO = uncompacted.
    base: LogPosition,
    /// The in-memory canonical log SUFFIX above `base`.
    log: Vec<LogEntry>,
    /// In-flight persist + withheld messages. Lost on crash.
    pending: Option<Pending>,
    role: Role,
    /// Volatile commit index (re-learned from the leader after restart).
    commit: u64,
    /// Leader bookkeeping: highest DURABLY-acked index per voter.
    match_index: BTreeMap<NodeId, u64>,
    /// Leader bookkeeping: next index to send per voter.
    next_index: BTreeMap<NodeId, u64>,
    /// Phase B (RFC 028 §7): key → log index of the entry claiming it.
    /// Maintained incrementally on append/truncate and rebuilt from the
    /// restored log at construction — the claims table IS the log's keyed
    /// entries, never a separate source of truth.
    claims: BTreeMap<u64, u64>,
    votes: BTreeSet<NodeId>,
    pre_votes: BTreeSet<NodeId>,
    use_pre_vote: bool,
    /// Phase B (RFC 028 §3/§4): voters that are WITNESSES — they vote in
    /// elections (control quorum) but never count toward commit durability
    /// (data quorum) and never campaign. With commits requiring a quorum of
    /// DATA voters, a witness-assisted election can only ever elect a data
    /// node that holds every committed entry: in the 2-data+witness
    /// topology, every commit is on BOTH data nodes, so "stale data node +
    /// witness elect a leader" (the design review's P1-7 scenario) can lose
    /// only tentative entries — never acknowledged-durable ones.
    witnesses: BTreeSet<NodeId>,
    /// This node's capability set (binary version). Static per process.
    supported: u32,
    /// Cluster-active bits: derived from COMMITTED activation entries +
    /// the snapshot base. Monotone. Stale-low after restart until commit
    /// re-advances — safe for election gating (freshness closes the gap;
    /// codex Q1/F5), but the DRIVER must wait for a current-term commit
    /// barrier before serving capability-dependent surfaces.
    active: u32,
    /// Peer capability advertisements (driver-fed from the session-start
    /// capability exchange, #53). May be stale — the append-time NACK is
    /// the backstop.
    peer_caps: BTreeMap<NodeId, u32>,
    /// Peers that NACKed on capability grounds: sends suspended until a
    /// fresh exchange updates peer_caps (no retransmit storm).
    stalled: BTreeSet<NodeId>,
}

impl ReplicaCore {
    /// `restored_hard` / `restored_log` are what the boot path recovered —
    /// exactly what the last completed [`Effect::Persist`] wrote. Phase A2's
    /// quarantine decides whether construction is allowed at all; torn state
    /// must never reach this constructor (fail closed upstream).
    pub fn new(
        id: NodeId,
        voters: BTreeSet<NodeId>,
        restored_hard: HardState,
        restored_log: Vec<LogEntry>,
        use_pre_vote: bool,
    ) -> Self {
        debug_assert!(voters.contains(&id), "a core's own id must be a voter");
        let durable_len = restored_log.len() as u64;
        // Rebuild the claims table from the restored log: claims live IN
        // keyed entries, so a crash can never separate a claim from its op.
        let claims = restored_log
            .iter()
            .enumerate()
            .filter_map(|(i, e)| e.key.map(|k| (k, i as u64 + 1)))
            .collect();
        Self {
            id,
            voters,
            persisted_hard: restored_hard,
            durable_len,
            base: LogPosition::ZERO,
            log: restored_log,
            pending: None,
            role: Role::Follower,
            commit: 0,
            match_index: BTreeMap::new(),
            next_index: BTreeMap::new(),
            claims,
            votes: BTreeSet::new(),
            pre_votes: BTreeSet::new(),
            use_pre_vote,
            witnesses: BTreeSet::new(),
            supported: u32::MAX,
            active: 0,
            peer_caps: BTreeMap::new(),
            stalled: BTreeSet::new(),
        }
    }

    /// Declare this node's capability set (binary version).
    pub fn set_supported(&mut self, supported: u32) {
        self.supported = supported;
    }

    /// Feed peer capability advertisements (the #53 session-start
    /// exchange). Clears capability stalls — a new exchange means a new
    /// binary may have arrived.
    pub fn set_peer_caps(&mut self, caps: BTreeMap<NodeId, u32>) {
        self.peer_caps = caps;
        self.stalled.clear();
    }

    /// Cluster-active capability bits (committed view).
    pub fn active_caps(&self) -> u32 {
        self.active
    }

    /// Propose activating capability `bits` (RFC 028 §3). Leader-only;
    /// refused unless EVERY voter — witnesses included, they store the
    /// log — advertises support (codex Q4). Advertisements can be stale;
    /// the append-time NACK + stall is the backstop, and activation
    /// remains monotone regardless. When membership changes land, this
    /// check must bind to the exact config epoch (codex F3 — documented
    /// constraint for that slice).
    pub fn propose_activation(&mut self, bits: u32) -> Option<Vec<Effect>> {
        if self.role != Role::Leader || bits == 0 {
            return None;
        }
        if self.supported & bits != bits {
            return None;
        }
        for v in self.voters.iter() {
            if *v == self.id {
                continue;
            }
            let caps = self.peer_caps.get(v).copied().unwrap_or(0);
            if caps & bits != bits {
                return None;
            }
        }
        let term = self.current_term();
        self.log.push(LogEntry {
            term,
            payload: Payload::Noop,
            key: None,
            activate: Some(bits),
        });
        let mut out = vec![self.stage_log()];
        self.append_effects_for_followers(&mut out);
        Some(out)
    }

    /// Fold newly committed activation entries into `active` — called
    /// wherever the commit index advances. Compacted ranges are covered by
    /// snapshot.active.
    fn apply_committed_caps(&mut self, from: u64, to: u64) {
        for i in from.max(self.base.index + 1)..=to {
            if let Some(bits) = self.entry(i).and_then(|e| e.activate) {
                self.active |= bits;
            }
        }
    }

    /// Declare the witness subset of the voter set (driver/config-time).
    /// Witnesses vote, never campaign, never count toward commits, and a
    /// witness id must be in `voters` (it IS a voter for elections).
    pub fn set_witnesses(&mut self, witnesses: BTreeSet<NodeId>) {
        debug_assert!(witnesses.iter().all(|w| self.voters.contains(w)));
        // Codex F4: with ONE witness, every election quorum still contains
        // enough data nodes to intersect every data-commit quorum (proven
        // by enumeration for D≤4, pattern holds generally). TWO OR MORE
        // witnesses break that intersection — an incompatible/stale data
        // node + witnesses could elect without touching any commit-quorum
        // member. Multi-witness needs certified committed-watermark
        // machinery we deliberately do not have; refuse the topology.
        debug_assert!(
            witnesses.len() <= 1,
            "multi-witness topologies break election/data quorum intersection"
        );
        debug_assert!(
            !witnesses.contains(&self.id) || self.role == Role::Follower,
            "a witness cannot already hold a data role"
        );
        self.witnesses = witnesses;
    }

    /// Restore a COMPACTED node: `base` + `snapshot_claims` come from the
    /// durable snapshot; `restored_log` is the suffix above the base. The
    /// claims table = snapshot claims + suffix claims (a suffix entry may
    /// re-claim a key whose original was compacted only if the original
    /// was superseded — in practice suffix wins, matching log order).
    #[allow(clippy::too_many_arguments)]
    pub fn new_from_durable(
        id: NodeId,
        voters: BTreeSet<NodeId>,
        restored_hard: HardState,
        base: LogPosition,
        restored_log: Vec<LogEntry>,
        snapshot_claims: BTreeMap<u64, u64>,
        snapshot_active: u32,
        use_pre_vote: bool,
    ) -> Self {
        let mut core = Self::new(id, voters, restored_hard, Vec::new(), use_pre_vote);
        core.base = base;
        core.claims = snapshot_claims;
        core.active = snapshot_active;
        // The state below base is adopted wholesale (checkpoint semantics).
        core.commit = base.index;
        for e in restored_log {
            let key = e.key;
            core.log.push(e);
            if let Some(k) = key {
                core.claims.insert(k, core.last_index());
            }
        }
        core.durable_len = core.log.len() as u64;
        core
    }

    /// Absolute index of the last entry (base + suffix).
    pub fn last_index(&self) -> u64 {
        self.base.index + self.log.len() as u64
    }

    /// The compaction base (snapshot frontier).
    pub fn base(&self) -> LogPosition {
        self.base
    }

    /// Compact the log through `up_to` (absolute index). Only COMMITTED
    /// entries may compact (RFC 028 §6: every recovery path keeps log
    /// coverage or a verified snapshot — an uncommitted entry has neither).
    /// Returns the snapshot the driver persists alongside the suffix; the
    /// claims table travels in it (sol P1-9).
    pub fn compact(&mut self, up_to: u64) -> Option<(Snapshot, Vec<Effect>)> {
        if up_to <= self.base.index || up_to > self.commit {
            return None;
        }
        let last_term = self.entry(up_to)?.term;
        let drop = (up_to - self.base.index) as usize;
        self.log.drain(..drop);
        self.base = LogPosition {
            term: last_term.0,
            index: up_to,
        };
        self.durable_len = self.durable_len.saturating_sub(drop as u64);
        // The shape change (base + dropped prefix + active-at-base) must be
        // durable atomically — stage a persist like every other durable
        // decision. (Caught by the activation-survives-restart test: an
        // unpersisted compaction left disk in the old shape, which is SAFE
        // but loses the eager active/claims durability the snapshot form
        // provides.)
        let persist = self.stage_log();
        Some((
            Snapshot {
                last: self.base,
                claims: self.claims.clone(),
                active: self.active,
            },
            vec![persist],
        ))
    }

    // ── accessors ──────────────────────────────────────────────────

    pub fn role(&self) -> Role {
        self.role
    }

    pub fn current_term(&self) -> Term {
        self.effective().current_term
    }

    pub fn persisted_hard_state(&self) -> HardState {
        self.persisted_hard
    }

    pub fn commit_index(&self) -> u64 {
        self.commit
    }

    /// Leader's next send index for `peer` (observability + tests).
    pub fn next_index_of(&self, peer: NodeId) -> Option<u64> {
        self.next_index.get(&peer).copied()
    }

    pub fn log_len(&self) -> u64 {
        self.log.len() as u64
    }

    /// Entry at 1-based `index`, if present.
    pub fn entry(&self, index: u64) -> Option<&LogEntry> {
        if index <= self.base.index {
            return None; // compacted (or the 0 sentinel)
        }
        self.log.get((index - self.base.index) as usize - 1)
    }

    /// Position of the last in-memory log entry (sentinel ZERO when empty).
    /// Used for election freshness; in-memory-ahead-of-durable only makes a
    /// voter STRICTER (it may refuse an equally-fresh candidate — liveness,
    /// never safety), and grants persist before leaving anyway.
    pub fn last_log(&self) -> LogPosition {
        match self.log.last() {
            Some(e) => LogPosition {
                term: e.term.0,
                index: self.last_index(),
            },
            None => self.base,
        }
    }

    fn effective(&self) -> HardState {
        self.pending
            .as_ref()
            .map(|p| p.hard)
            .unwrap_or(self.persisted_hard)
    }

    // ── the durability gate (R2 + per-write quorum confirmation) ──

    /// Stage durable state. Emits a `Persist` snapshot of the CURRENT
    /// `(hard, log)`; messages that must not leave before durability go
    /// through [`Self::hold`]. Coalescing within one pending window is safe:
    /// the persisted snapshot is always ≥ every held message's implied state.
    fn stage(&mut self, hard: HardState) -> Effect {
        match &mut self.pending {
            Some(p) => p.hard = hard,
            None => {
                self.pending = Some(Pending {
                    hard,
                    held: Vec::new(),
                })
            }
        }
        Effect::Persist {
            hard,
            base: self.base,
            log: self.log.clone(),
            claims: self.claims.clone(),
            active: self.active,
        }
    }

    /// Stage the current log for persistence without a hard-state change.
    fn stage_log(&mut self) -> Effect {
        let hard = self.effective();
        self.stage(hard)
    }

    fn hold(&mut self, eff: Effect) {
        debug_assert!(self.pending.is_some(), "hold() requires a staged persist");
        if let Some(p) = &mut self.pending {
            p.held.push(eff);
        }
    }

    /// If a persist is pending, hold `eff` behind it; otherwise emit it now.
    fn hold_or(&mut self, eff: Effect, out: &mut Vec<Effect>) {
        if self.pending.is_some() {
            self.hold(eff);
        } else {
            out.push(eff);
        }
    }

    /// The driver confirms the staged snapshot is durable. Returns withheld
    /// messages — the ONLY way to obtain them. On a leader, self-acceptance
    /// becomes countable here (its own log is now durable), so the commit
    /// index may advance as part of the flush.
    pub fn state_persisted(&mut self) -> Vec<Effect> {
        let mut out = match self.pending.take() {
            Some(p) => {
                self.persisted_hard = p.hard;
                self.durable_len = self.log.len() as u64;
                p.held
            }
            None => return Vec::new(),
        };
        if self.role == Role::Leader {
            self.match_index
                .insert(self.id, self.base.index + self.durable_len);
            self.try_advance_commit(&mut out);
        }
        out
    }

    // ── client-facing (leader) ─────────────────────────────────────

    /// Propose a new entry. Leader-only; returns `None` otherwise (the
    /// caller redirects to the leader — the API layer's job in Phase B).
    pub fn propose(&mut self, payload: Payload) -> Option<Vec<Effect>> {
        if self.role != Role::Leader {
            return None;
        }
        let term = self.current_term();
        self.log.push(LogEntry::unkeyed(term, payload));
        let mut out = vec![self.stage_log()];
        // Fan out eagerly; success responses confirm durable acceptance.
        self.append_effects_for_followers(&mut out);
        Some(out)
    }

    /// Keyed proposal (Phase B, RFC 028 §7). The claim check happens HERE —
    /// origin ingress — against the claims table rebuilt from the log:
    /// a key claimed by a committed entry dedupes; a key claimed by an
    /// in-flight entry parks the retry (no second append, no premature
    /// success); a fresh key appends an entry that CARRIES the claim.
    /// Leader-only; `None` = redirect to the leader.
    ///
    /// The caller's ack contract (the pre-registered twin properties):
    /// report success for `index` only once `commit_index() >= index`
    /// (never-success-without-durable-effect), and never append the same
    /// key twice while it is claimed (never-double-write). Both halves are
    /// proven in the simulator across failover/quarantine interleavings.
    pub fn propose_keyed(&mut self, key: u64, payload: Payload) -> Option<KeyedProposal> {
        if self.role != Role::Leader {
            return None;
        }
        if let Some(&index) = self.claims.get(&key) {
            return Some(if index <= self.commit {
                KeyedProposal::DuplicateCommitted { index }
            } else {
                KeyedProposal::DuplicatePending { index }
            });
        }
        let term = self.current_term();
        self.log.push(LogEntry {
            term,
            payload,
            key: Some(key),
            activate: None,
        });
        let index = self.last_index();
        self.claims.insert(key, index);
        let mut effects = vec![self.stage_log()];
        self.append_effects_for_followers(&mut effects);
        Some(KeyedProposal::Appended { index, effects })
    }

    /// Leader heartbeat tick (driver timer): send each follower its next
    /// entries (empty AppendEntries when caught up). Also how stragglers
    /// catch up after drops.
    pub fn tick_heartbeat(&mut self) -> Vec<Effect> {
        if self.role != Role::Leader {
            return Vec::new();
        }
        let mut out = Vec::new();
        self.append_effects_for_followers(&mut out);
        out
    }

    fn append_effects_for_followers(&mut self, out: &mut Vec<Effect>) {
        let peers: Vec<NodeId> = self
            .voters
            .iter()
            .copied()
            .filter(|p| *p != self.id)
            .collect();
        for peer in peers {
            self.send_append_to(peer, out);
        }
    }

    fn send_append_to(&mut self, peer: NodeId, out: &mut Vec<Effect>) {
        if self.stalled.contains(&peer) {
            return; // capability-stalled: waiting for a new exchange
        }
        let term = self.current_term();
        let next = *self.next_index.get(&peer).unwrap_or(&1);
        // Straggler below our compaction base: entries are gone — ship the
        // snapshot instead (the stale-rejoin-beyond-GC path, §6). Claims
        // ride along, so the GC'd claim can never be replay-lost (P1-9).
        if next <= self.base.index {
            let msg = Message::InstallSnapshot {
                term,
                leader: self.id,
                snapshot: Snapshot {
                    last: self.base,
                    claims: self.claims.clone(),
                    active: self.active,
                },
            };
            self.hold_or(Effect::Send { to: peer, msg }, out);
            return;
        }
        let prev_index = next.saturating_sub(1);
        let prev = if prev_index == self.base.index {
            self.base // covers the ZERO sentinel when uncompacted
        } else {
            match self.entry(prev_index) {
                Some(e) => LogPosition {
                    term: e.term.0,
                    index: prev_index,
                },
                None => self.base,
            }
        };
        let entries: Vec<LogEntry> = self
            .log
            .iter()
            .skip((prev.index - self.base.index) as usize)
            .cloned()
            .collect();
        let msg = Message::AppendEntries {
            term,
            leader: self.id,
            prev,
            entries,
            commit: self.commit,
        };
        // If our own persist of these entries is still pending, hold: a
        // follower must never hold entries our own disk lacks (its durable
        // ack could otherwise out-run the leader's durability).
        self.hold_or(Effect::Send { to: peer, msg }, out);
    }

    // ── events ─────────────────────────────────────────────────────

    pub fn on_election_timeout(&mut self) -> Vec<Effect> {
        // A witness never campaigns: it has no data to lead with. It still
        // VOTES (its on_vote_request path is untouched), which is its whole
        // job — the tiebreak in even-data-node topologies.
        if self.witnesses.contains(&self.id) {
            return Vec::new();
        }
        match self.role {
            Role::Leader => Vec::new(),
            _ if self.use_pre_vote => self.start_pre_vote(),
            _ => self.start_campaign(),
        }
    }

    pub fn on_message(
        &mut self,
        from: NodeId,
        msg: Message,
        leader_recently_seen: bool,
    ) -> Vec<Effect> {
        match msg {
            Message::PreVoteRequest {
                term,
                candidate,
                last_log,
            } => self.on_pre_vote_request(from, term, candidate, last_log, leader_recently_seen),
            Message::PreVoteResponse { term, granted } => {
                self.on_pre_vote_response(from, term, granted)
            }
            Message::VoteRequest {
                term,
                candidate,
                last_log,
                supported,
            } => self.on_vote_request(from, term, candidate, last_log, supported),
            Message::VoteResponse { term, granted } => self.on_vote_response(from, term, granted),
            Message::AppendEntries {
                term,
                leader,
                prev,
                entries,
                commit,
            } => self.on_append_entries(from, term, leader, prev, entries, commit),
            Message::AppendResponse {
                term,
                success,
                last_index,
                unsupported,
            } => self.on_append_response(from, term, success, last_index, unsupported),
            Message::InstallSnapshot {
                term,
                leader: _,
                snapshot,
            } => self.on_install_snapshot(from, term, snapshot),
        }
    }

    // ── election (semantics unchanged from Phase A1) ───────────────

    fn start_pre_vote(&mut self) -> Vec<Effect> {
        self.role = Role::PreCandidate;
        self.pre_votes.clear();
        self.pre_votes.insert(self.id);
        if self.pre_quorum_reached() {
            return self.start_campaign();
        }
        vec![Effect::Broadcast {
            msg: Message::PreVoteRequest {
                term: self.current_term().next(),
                candidate: self.id,
                last_log: self.last_log(),
            },
        }]
    }

    fn start_campaign(&mut self) -> Vec<Effect> {
        self.role = Role::Candidate;
        self.votes.clear();
        self.votes.insert(self.id);
        let term = self.current_term().next();
        let persist = self.stage(HardState {
            current_term: term,
            voted_for: Some(self.id),
        });
        if self.vote_quorum_reached() {
            // Single-voter cluster: leadership (and its no-op) is held
            // behind the self-vote persist.
            self.become_leader_held(term);
            return vec![persist];
        }
        self.hold(Effect::Broadcast {
            msg: Message::VoteRequest {
                term,
                candidate: self.id,
                last_log: self.last_log(),
                supported: self.supported,
            },
        });
        vec![persist]
    }

    fn on_pre_vote_request(
        &mut self,
        from: NodeId,
        term: Term,
        _candidate: NodeId,
        last_log: LogPosition,
        leader_recently_seen: bool,
    ) -> Vec<Effect> {
        let grant = !leader_recently_seen
            && term > self.current_term()
            && last_log.is_at_least_as_up_to_date_as(&self.last_log());
        vec![Effect::Send {
            to: from,
            msg: Message::PreVoteResponse {
                term,
                granted: grant,
            },
        }]
    }

    fn on_pre_vote_response(&mut self, from: NodeId, term: Term, granted: bool) -> Vec<Effect> {
        if self.role != Role::PreCandidate || term != self.current_term().next() || !granted {
            return Vec::new();
        }
        if self.voters.contains(&from) {
            self.pre_votes.insert(from);
        }
        if self.pre_quorum_reached() {
            return self.start_campaign();
        }
        Vec::new()
    }

    fn on_vote_request(
        &mut self,
        from: NodeId,
        term: Term,
        candidate: NodeId,
        last_log: LogPosition,
        candidate_supported: u32,
    ) -> Vec<Effect> {
        let mut effects = Vec::new();
        let cur = self.current_term();
        if term < cur {
            return vec![Effect::Send {
                to: from,
                msg: Message::VoteResponse {
                    term: cur,
                    granted: false,
                },
            }];
        }
        // ONE decision → ONE persist. Deciding the vote and adopting the
        // term must land in a single staged hard state: a crash between a
        // term-adoption persist and a vote persist would durably adopt the
        // term while forgetting the vote — the split-persist bug the sim
        // caught in Phase A1 (and again in A1b review). Compute the final
        // hard state first, stage exactly once.
        let newer = term > cur;
        if newer {
            if matches!(self.role, Role::Leader | Role::Candidate) {
                effects.push(Effect::SteppedDown { term: cur });
            }
            self.role = Role::Follower;
        }
        // A vote from an older term never constrains a newer term.
        let prior_vote = if newer {
            None
        } else {
            self.effective().voted_for
        };
        let may_vote = prior_vote.is_none() || prior_vote == Some(candidate);
        // Gate A #3 (R3): Raft freshness over the candidate's log.
        let fresh = last_log.is_at_least_as_up_to_date_as(&self.last_log());
        // Capability gate (sol P1-14 / codex Q1): a candidate that cannot
        // cover OUR active view is semantically incomplete — refuse. Note
        // freshness usually catches this first (an incompatible node's log
        // cannot contain the committed activation entry), but active-view
        // gating closes the belt-and-braces gap when our own active is
        // ahead of the candidate's advertisement.
        let capable = candidate_supported & self.active == self.active;
        if may_vote && fresh && capable {
            // Gate A #1 (R2): the grant changes voted_for → stage + HOLD.
            effects.push(self.stage(HardState {
                current_term: term,
                voted_for: Some(candidate),
            }));
            self.hold(Effect::Send {
                to: from,
                msg: Message::VoteResponse {
                    term,
                    granted: true,
                },
            });
        } else {
            let refusal = Effect::Send {
                to: from,
                msg: Message::VoteResponse {
                    term,
                    granted: false,
                },
            };
            if newer {
                // Refusal, but the term adoption still needs durability;
                // sequence the refusal behind that single persist.
                effects.push(self.stage(HardState {
                    current_term: term,
                    voted_for: None,
                }));
                self.hold(refusal);
            } else {
                self.hold_or(refusal, &mut effects);
            }
        }
        effects
    }

    fn on_vote_response(&mut self, from: NodeId, term: Term, granted: bool) -> Vec<Effect> {
        let cur = self.current_term();
        if term > cur {
            return self.step_down_to(term, cur);
        }
        if self.role != Role::Candidate || term != cur || !granted {
            return Vec::new();
        }
        if self.voters.contains(&from) {
            self.votes.insert(from);
        }
        if self.vote_quorum_reached() {
            let mut out = Vec::new();
            self.become_leader(cur, &mut out);
            return out;
        }
        Vec::new()
    }

    // ── log replication (Phase A1b) ────────────────────────────────

    fn on_append_entries(
        &mut self,
        from: NodeId,
        term: Term,
        _leader: NodeId,
        prev: LogPosition,
        entries: Vec<LogEntry>,
        leader_commit: u64,
    ) -> Vec<Effect> {
        let mut effects = Vec::new();
        let cur = self.current_term();

        // Term fencing: refuse a stale leader with our newer term — the
        // response is what fences it (it steps down on receipt).
        if term < cur {
            return vec![Effect::Send {
                to: from,
                msg: Message::AppendResponse {
                    term: cur,
                    success: false,
                    last_index: self.base.index + self.durable_len,
                    unsupported: false,
                },
            }];
        }

        // Valid leadership for `term`: recognize it. Term adoption is NOT
        // staged here — one decision → one persist: the final staged hard
        // state (below, per path) carries the adopted term, so a crash can
        // never durably adopt the term while losing the accompanying
        // log/vote decision (the A1 split-persist lesson).
        if matches!(self.role, Role::Leader | Role::Candidate) {
            effects.push(Effect::SteppedDown { term: cur });
        }
        self.role = Role::Follower;
        let newer = term > cur;
        let adopted = if newer {
            HardState {
                current_term: term,
                voted_for: None,
            }
        } else {
            self.effective()
        };

        // Everything at or below our base is COMMITTED here, and committed
        // entries are globally unique (Gate A #2) — a leader probing below
        // our base can simply fast-forward to it.
        if prev.index < self.base.index {
            let ack = Effect::Send {
                to: from,
                msg: Message::AppendResponse {
                    term,
                    success: true,
                    last_index: self.base.index,
                    unsupported: false,
                },
            };
            if newer {
                effects.push(self.stage(adopted));
                self.hold(ack);
            } else {
                self.hold_or(ack, &mut effects);
            }
            return effects;
        }

        // Continuity check at `prev` (the append-side of Gate A #2: a hole
        // or a term mismatch means these entries do not extend our history —
        // refuse; the leader backs up).
        let prev_ok = (prev.index == self.base.index && prev.term == self.base.term)
            || self
                .entry(prev.index)
                .is_some_and(|e| e.term.0 == prev.term);
        if !prev_ok {
            let refusal = Effect::Send {
                to: from,
                msg: Message::AppendResponse {
                    term,
                    success: false,
                    last_index: (self.base.index + self.durable_len)
                        .min(prev.index.saturating_sub(1)),
                    unsupported: false,
                },
            };
            if newer {
                effects.push(self.stage(adopted));
                self.hold(refusal);
            } else {
                self.hold_or(refusal, &mut effects);
            }
            return effects;
        }

        // Capability gate (codex F1 backstop): if any entry in the batch
        // activates bits we cannot support, refuse the WHOLE batch with a
        // distinguishable NACK — our log must never contain an activation
        // we cannot honor (that is what makes the freshness argument for
        // incompatible candidates sound).
        if entries
            .iter()
            .any(|e| e.activate.is_some_and(|b| self.supported & b != b))
        {
            let refusal = Effect::Send {
                to: from,
                msg: Message::AppendResponse {
                    term,
                    success: false,
                    last_index: self.base.index + self.durable_len,
                    unsupported: true,
                },
            };
            if newer {
                effects.push(self.stage(adopted));
                self.hold(refusal);
            } else {
                self.hold_or(refusal, &mut effects);
            }
            return effects;
        }

        // Append, truncating any conflicting (necessarily uncommitted)
        // suffix. The committed prefix is never truncated: a conflict at or
        // below `commit` is protocol corruption — fail closed rather than
        // destroy applied history (Phase A2 turns this into quarantine).
        let mut idx = prev.index;
        let mut changed = false;
        for e in &entries {
            idx += 1;
            match self.entry(idx) {
                Some(existing) if *existing == *e => {}
                Some(_) => {
                    if idx <= self.commit {
                        debug_assert!(false, "conflict at/below commit index — corruption");
                        let refusal = Effect::Send {
                            to: from,
                            msg: Message::AppendResponse {
                                term,
                                success: false,
                                last_index: self.base.index + self.durable_len,
                                unsupported: false,
                            },
                        };
                        if newer {
                            effects.push(self.stage(adopted));
                            self.hold(refusal);
                        } else {
                            self.hold_or(refusal, &mut effects);
                        }
                        return effects;
                    }
                    // The claim dies WITH its truncated entry (the atomic
                    // unit, RFC 028 §7): remove keys owned by the removed
                    // suffix so a re-proposed key can claim afresh.
                    for removed in self.log.iter().skip((idx - self.base.index) as usize - 1) {
                        if let Some(k) = removed.key {
                            if self.claims.get(&k) >= Some(&idx) {
                                self.claims.remove(&k);
                            }
                        }
                    }
                    self.log.truncate((idx - self.base.index) as usize - 1);
                    self.log.push(e.clone());
                    if let Some(k) = e.key {
                        self.claims.insert(k, idx);
                    }
                    changed = true;
                }
                None => {
                    self.log.push(e.clone());
                    if let Some(k) = e.key {
                        self.claims.insert(k, idx);
                    }
                    changed = true;
                }
            }
        }
        let covered = idx.max(prev.index);

        if changed || newer {
            // Durable acceptance: the success response — our quorum-
            // confirmation vote for these entries — is gated on OUR persist
            // of BOTH the entries and the (possibly adopted) term, staged
            // as one snapshot.
            effects.push(self.stage(adopted));
            self.hold(Effect::Send {
                to: from,
                msg: Message::AppendResponse {
                    term,
                    success: true,
                    last_index: if changed {
                        covered
                    } else {
                        (self.base.index + self.durable_len).min(covered)
                    },
                    unsupported: false,
                },
            });
        } else {
            // Heartbeat / duplicate at the current term: durable state
            // already covers it.
            let ack = Effect::Send {
                to: from,
                msg: Message::AppendResponse {
                    term,
                    success: true,
                    last_index: (self.base.index + self.durable_len).min(covered),
                    unsupported: false,
                },
            };
            self.hold_or(ack, &mut effects);
        }

        // Adopt the leader's commit proof, bounded by what we hold.
        let new_commit = leader_commit.min(covered);
        if new_commit > self.commit {
            let from_idx = self.commit + 1;
            self.commit = new_commit;
            self.apply_committed_caps(from_idx, new_commit);
            // Sequence apply behind the persist: apply never outruns
            // durability.
            let advanced = Effect::CommitAdvanced { to: new_commit };
            self.hold_or(advanced, &mut effects);
        }
        effects
    }

    fn on_append_response(
        &mut self,
        from: NodeId,
        term: Term,
        success: bool,
        last_index: u64,
        unsupported: bool,
    ) -> Vec<Effect> {
        let cur = self.current_term();
        if term > cur {
            return self.step_down_to(term, cur);
        }
        if self.role != Role::Leader || term != cur {
            return Vec::new();
        }
        let mut out = Vec::new();
        if !success && unsupported {
            // Capability NACK: retrying is useless until the peer upgrades
            // (codex F7). Suspend sends, surface the incompatibility.
            if self.stalled.insert(from) {
                out.push(Effect::PeerIncompatible { peer: from });
            }
            return out;
        }
        // Codex finding 2: delayed/duplicate responses must not regress
        // next_index below match_index + 1. match_index is monotonic (max);
        // matched entries are confirmed-durable-matching, so probing below
        // match+1 is never needed — a stale success or an out-of-date
        // failure clamped to the floor costs nothing, while an unclamped
        // one triggers spurious full-suffix retransmissions.
        if success {
            let m = self.match_index.entry(from).or_insert(0);
            *m = (*m).max(last_index);
            let floor = *m + 1;
            self.next_index.insert(from, floor);
            self.try_advance_commit(&mut out);
        } else {
            let floor = self.match_index.get(&from).copied().unwrap_or(0) + 1;
            let next = self
                .next_index
                .get(&from)
                .copied()
                .unwrap_or(self.last_index() + 1);
            let backed = next.saturating_sub(1).clamp(1, last_index + 1).max(floor);
            self.next_index.insert(from, backed);
            self.send_append_to(from, &mut out);
        }
        out
    }

    /// Adopt a leader's snapshot (we are a straggler below its compaction
    /// base). Same discipline as every other durable decision: ONE persist,
    /// ack + InstallState held behind it. A snapshot at or below our own
    /// commit is stale — ack our durable frontier instead (never regress).
    fn on_install_snapshot(&mut self, from: NodeId, term: Term, snapshot: Snapshot) -> Vec<Effect> {
        let mut effects = Vec::new();
        let cur = self.current_term();
        if term < cur {
            return vec![Effect::Send {
                to: from,
                msg: Message::AppendResponse {
                    term: cur,
                    success: false,
                    last_index: self.base.index + self.durable_len,
                    unsupported: false,
                },
            }];
        }
        if matches!(self.role, Role::Leader | Role::Candidate) {
            effects.push(Effect::SteppedDown { term: cur });
        }
        self.role = Role::Follower;
        let newer = term > cur;
        let adopted = if newer {
            HardState {
                current_term: term,
                voted_for: None,
            }
        } else {
            self.effective()
        };
        // Preflight (codex F6): refuse an install whose active set we
        // cannot support (quarantine-bypass) or that would REGRESS our
        // active view (monotonicity — a legitimately higher snapshot's
        // active is always a superset of ours).
        if self.supported & snapshot.active != snapshot.active {
            let refusal = Effect::Send {
                to: from,
                msg: Message::AppendResponse {
                    term,
                    success: false,
                    last_index: self.base.index + self.durable_len,
                    unsupported: true,
                },
            };
            if newer {
                effects.push(self.stage(adopted));
                self.hold(refusal);
            } else {
                self.hold_or(refusal, &mut effects);
            }
            return effects;
        }
        if snapshot.active & self.active != self.active {
            // Active-regressing snapshot: forged or badly stale — refuse.
            let refusal = Effect::Send {
                to: from,
                msg: Message::AppendResponse {
                    term,
                    success: false,
                    last_index: self.base.index + self.durable_len,
                    unsupported: false,
                },
            };
            if newer {
                effects.push(self.stage(adopted));
                self.hold(refusal);
            } else {
                self.hold_or(refusal, &mut effects);
            }
            return effects;
        }
        if snapshot.last.index <= self.commit {
            // Stale snapshot: our committed state is already ahead.
            let ack = Effect::Send {
                to: from,
                msg: Message::AppendResponse {
                    term,
                    success: true,
                    last_index: self.base.index + self.durable_len,
                    unsupported: false,
                },
            };
            if newer {
                effects.push(self.stage(adopted));
                self.hold(ack);
            } else {
                self.hold_or(ack, &mut effects);
            }
            return effects;
        }
        // Adopt: checkpoint replaces log prefix AND claims wholesale.
        let last_index = snapshot.last.index;
        self.base = snapshot.last;
        self.log.clear();
        self.claims = snapshot.claims;
        self.active |= snapshot.active;
        self.commit = last_index;
        effects.push(self.stage(adopted));
        self.hold(Effect::InstallState { last_index });
        self.hold(Effect::Send {
            to: from,
            msg: Message::AppendResponse {
                term,
                success: true,
                last_index,
                unsupported: false,
            },
        });
        effects
    }

    /// The commit rule (Gate A #2): the largest `n` such that a quorum of
    /// voters has DURABLY accepted through `n` and `log[n].term` is the
    /// CURRENT term. Prior-term entries never commit by counting — only by
    /// implication under a committed current-term entry (the no-op
    /// guarantees one exists). Phase B narrows the counted set to
    /// data-bearing voters (witness exclusion).
    fn try_advance_commit(&mut self, out: &mut Vec<Effect>) {
        let cur = self.current_term();
        let mut n = self.last_index();
        while n > self.commit {
            if self.entry(n).map(|e| e.term) == Some(cur) {
                // Data quorum ONLY: witnesses never count toward commit
                // durability (RFC 028 §4 — a witness ack is a position
                // ack, not a data copy).
                let data_voters = || self.voters.iter().filter(|v| !self.witnesses.contains(v));
                let acks = data_voters()
                    .filter(|v| self.match_index.get(v).copied().unwrap_or(0) >= n)
                    .count();
                if acks >= quorum(data_voters().count()) {
                    let from_idx = self.commit + 1;
                    self.commit = n;
                    self.apply_committed_caps(from_idx, n);
                    let advanced = Effect::CommitAdvanced { to: n };
                    self.hold_or(advanced, out);
                    // Share the new commit index promptly.
                    self.append_effects_for_followers(out);
                    return;
                }
            }
            n -= 1;
        }
    }

    // ── shared transitions ─────────────────────────────────────────

    fn become_leader(&mut self, term: Term, out: &mut Vec<Effect>) {
        self.init_leader_state(term);
        out.push(Effect::BecameLeader { term });
        // §5.4.2 no-op: gives the new term an entry so the prior-term
        // suffix can commit by implication.
        self.log.push(LogEntry::unkeyed(term, Payload::Noop));
        out.push(self.stage_log());
        self.append_effects_for_followers(out);
    }

    /// Single-voter-cluster variant: everything (leadership announcement,
    /// no-op, fan-out) rides behind the already-staged self-vote persist.
    fn become_leader_held(&mut self, term: Term) {
        debug_assert!(self.pending.is_some());
        self.init_leader_state(term);
        self.hold(Effect::BecameLeader { term });
        self.log.push(LogEntry::unkeyed(term, Payload::Noop));
        let _ = self.stage_log(); // coalesces into the pending persist
    }

    fn init_leader_state(&mut self, _term: Term) {
        self.role = Role::Leader;
        let last = self.last_index();
        self.next_index.clear();
        self.match_index.clear();
        for v in self.voters.iter().copied() {
            self.next_index.insert(v, last + 1);
            self.match_index.insert(v, 0);
        }
        self.match_index
            .insert(self.id, self.base.index + self.durable_len);
    }

    fn step_down_to(&mut self, newer: Term, cur: Term) -> Vec<Effect> {
        let mut effects = Vec::new();
        if matches!(self.role, Role::Leader | Role::Candidate) {
            effects.push(Effect::SteppedDown { term: cur });
        }
        self.role = Role::Follower;
        effects.push(self.stage(HardState {
            current_term: newer,
            voted_for: None,
        }));
        effects
    }

    /// Rejoin authorization (RFC 028 v2 §5): produce a snapshot grant for a
    /// quarantined node — but only when this node holds a **quorum-backed
    /// leadership certificate**: it is leader AND has committed an entry in
    /// its CURRENT term (the election no-op guarantees one exists once a
    /// quorum has confirmed the reign). A stale partitioned "leader" cannot
    /// satisfy that (its current-term entries never commit — Gate A #2), so
    /// it can never authorize a resync — review scenario R5's fix.
    ///
    /// Returns `(term, log, commit)` for the driver to wrap into a
    /// `bootstrap::RejoinMessage::Grant`; `None` = not authorized (the
    /// quarantined node stays quarantined and retries — fail closed).
    /// Returns `(term, base, durable_suffix, claims, commit)` — the full
    /// durable picture, snapshot-aware: `base` + `claims` carry compacted
    /// history, `durable_suffix` the live entries above it.
    #[allow(clippy::type_complexity)]
    pub fn rejoin_grant(
        &self,
    ) -> Option<(
        Term,
        LogPosition,
        Vec<LogEntry>,
        BTreeMap<u64, u64>,
        u32,
        u64,
    )> {
        if self.role != Role::Leader {
            return None;
        }
        let cur = self.current_term();
        let committed_in_current_term =
            self.commit > 0 && self.entry(self.commit).map(|e| e.term) == Some(cur);
        if !committed_in_current_term {
            return None;
        }
        // Grant only the DURABLE suffix: a snapshot must never carry
        // entries our own disk could forget in a crash. Base + claims are
        // durable by construction (they only advance via persisted state).
        let durable: Vec<LogEntry> = self
            .log
            .iter()
            .take(self.durable_len as usize)
            .cloned()
            .collect();
        let commit = self.commit.min(self.base.index + self.durable_len);
        Some((
            cur,
            self.base,
            durable,
            self.claims.clone(),
            self.active,
            commit,
        ))
    }

    fn pre_quorum_reached(&self) -> bool {
        self.pre_votes.len() >= quorum(self.voters.len())
    }

    fn vote_quorum_reached(&self) -> bool {
        self.votes.len() >= quorum(self.voters.len())
    }
}