eidetic-engine 0.15.2

Durable, local-first, explainable memory for coding agents.
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
//! Trust class taxonomy (EE-260, ADR-0009, ADR-0086).
//!
//! Defines the six-class trust taxonomy for memories:
//! - `human_explicit`: Human invoked `ee remember` directly (0.85)
//! - `peer_human_attested`: Active member's signed origin declared `human_explicit` (0.75)
//! - `agent_validated`: Agent assertion + validated outcome (0.65)
//! - `agent_assertion`: Agent assertion, no outcome yet (0.50)
//! - `cass_evidence`: Imported session span from `cass` (0.45)
//! - `legacy_import`: Imported from pre-v1 Eidetic Engine (0.30)
//!
//! Trust class is exposed as `trust_class` on every memory in
//! `ee.memory.v1`. An optional `trust_subclass` qualifier provides
//! project-tunable metadata without affecting scoring.

use std::fmt;
use std::str::FromStr;

use crate::models::memory::MemoryLevel;
use crate::models::rule::RuleMaturity;

fn normalized_trust_token(input: &str) -> String {
    let trimmed = input.trim();
    let mut normalized = String::with_capacity(trimmed.len());
    let mut previous_was_lowercase = false;
    let mut previous_was_separator = false;

    for character in trimmed.chars() {
        match character {
            '-' | '_' => {
                if !normalized.is_empty() && !previous_was_separator {
                    normalized.push('_');
                }
                previous_was_lowercase = false;
                previous_was_separator = true;
            }
            character if character.is_ascii_uppercase() => {
                if previous_was_lowercase && !previous_was_separator {
                    normalized.push('_');
                }
                normalized.push(character.to_ascii_lowercase());
                previous_was_lowercase = false;
                previous_was_separator = false;
            }
            character => {
                normalized.push(character.to_ascii_lowercase());
                previous_was_lowercase = character.is_ascii_lowercase();
                previous_was_separator = false;
            }
        }
    }

    normalized
}

/// Stable schema marker for local signing-key policy decisions.
pub const LOCAL_SIGNING_KEY_POLICY_SCHEMA_V1: &str = "ee.local_signing_key_policy.v1";

/// Trust class for a memory, determining initial confidence and
/// scoring weight.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum TrustClass {
    /// Human invoked `ee remember` directly.
    HumanExplicit,
    /// Signed peer origin from an active member declared `human_explicit`.
    PeerHumanAttested,
    /// Agent assertion with at least one validated outcome.
    AgentValidated,
    /// Agent assertion, no outcome events yet.
    AgentAssertion,
    /// Imported session span from `cass`.
    CassEvidence,
    /// Imported from a pre-v1 Eidetic Engine artifact.
    LegacyImport,
}

impl TrustClass {
    /// Stable lowercase wire form.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::HumanExplicit => "human_explicit",
            Self::PeerHumanAttested => "peer_human_attested",
            Self::AgentValidated => "agent_validated",
            Self::AgentAssertion => "agent_assertion",
            Self::CassEvidence => "cass_evidence",
            Self::LegacyImport => "legacy_import",
        }
    }

    /// Initial confidence for this trust class per ADR-0009 and ADR-0086 TC-D7.
    #[must_use]
    pub const fn initial_confidence(self) -> f32 {
        match self {
            Self::HumanExplicit => 0.85,
            Self::PeerHumanAttested => 0.75,
            Self::AgentValidated => 0.65,
            Self::AgentAssertion => 0.50,
            Self::CassEvidence => 0.45,
            Self::LegacyImport => 0.30,
        }
    }

    /// All variants in a stable order.
    #[must_use]
    pub const fn all() -> [Self; 6] {
        [
            Self::HumanExplicit,
            Self::PeerHumanAttested,
            Self::AgentValidated,
            Self::AgentAssertion,
            Self::CassEvidence,
            Self::LegacyImport,
        ]
    }

    /// Whether validated procedural memories in this class need a
    /// local signature before authoritative use.
    #[must_use]
    pub const fn requires_local_signature_for_validated_procedural(self) -> bool {
        matches!(
            self,
            Self::HumanExplicit | Self::PeerHumanAttested | Self::AgentValidated
        )
    }
}

impl fmt::Display for TrustClass {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}

/// Error when parsing an invalid trust class string.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ParseTrustClassError {
    input: String,
}

impl ParseTrustClassError {
    /// The invalid input that was attempted.
    pub fn input(&self) -> &str {
        &self.input
    }
}

impl fmt::Display for ParseTrustClassError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "unknown trust class `{}`; expected one of human_explicit, peer_human_attested, agent_validated, agent_assertion, cass_evidence, legacy_import",
            self.input
        )
    }
}

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

impl FromStr for TrustClass {
    type Err = ParseTrustClassError;

    fn from_str(input: &str) -> Result<Self, Self::Err> {
        match normalized_trust_token(input).as_str() {
            "human_explicit" => Ok(Self::HumanExplicit),
            "peer_human_attested" => Ok(Self::PeerHumanAttested),
            "agent_validated" => Ok(Self::AgentValidated),
            "agent_assertion" => Ok(Self::AgentAssertion),
            "cass_evidence" => Ok(Self::CassEvidence),
            "legacy_import" => Ok(Self::LegacyImport),
            _ => Err(ParseTrustClassError {
                input: input.to_owned(),
            }),
        }
    }
}

/// Local signing-key posture for a procedural memory.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum LocalSigningKeyPosture {
    /// The memory is outside the high-trust procedural policy boundary.
    NotRequired,
    /// A signature should be attached before promotion to validated authority.
    Recommended,
    /// A signature is required before authoritative procedural use.
    Required,
    /// The policy applies and the local signature is present.
    Satisfied,
}

impl LocalSigningKeyPosture {
    /// Stable lowercase wire form.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::NotRequired => "not_required",
            Self::Recommended => "recommended",
            Self::Required => "required",
            Self::Satisfied => "satisfied",
        }
    }

    /// All variants in stable wire order.
    #[must_use]
    pub const fn all() -> [Self; 4] {
        [
            Self::NotRequired,
            Self::Recommended,
            Self::Required,
            Self::Satisfied,
        ]
    }
}

impl fmt::Display for LocalSigningKeyPosture {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}

/// Deterministic local signing-key policy result for one memory posture.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct LocalSigningKeyDecision {
    /// Stable schema marker for machine consumers.
    pub schema: &'static str,
    /// Policy posture.
    pub posture: LocalSigningKeyPosture,
    /// Stable machine-readable reason code.
    pub code: &'static str,
    /// Human-facing summary that can be rendered on stderr or in reports.
    pub message: &'static str,
    /// Suggested next action when the posture is not already satisfied.
    pub repair: Option<&'static str>,
}

impl LocalSigningKeyDecision {
    const fn new(
        posture: LocalSigningKeyPosture,
        code: &'static str,
        message: &'static str,
        repair: Option<&'static str>,
    ) -> Self {
        Self {
            schema: LOCAL_SIGNING_KEY_POLICY_SCHEMA_V1,
            posture,
            code,
            message,
            repair,
        }
    }

    /// Returns true when authoritative use must be blocked until signed.
    #[must_use]
    pub const fn is_blocking(self) -> bool {
        matches!(self.posture, LocalSigningKeyPosture::Required)
    }
}

/// Evaluate the local signing-key policy for a memory.
///
/// Only validated high-trust procedural memories are blocking when unsigned.
/// Draft or candidate high-trust procedural memories get a recommendation so
/// curation can attach a local signature before promotion. Lower-trust,
/// non-procedural, and terminal memories are not required to carry one.
#[must_use]
pub const fn evaluate_local_signing_key_policy(
    level: MemoryLevel,
    trust_class: TrustClass,
    maturity: RuleMaturity,
    has_local_signature: bool,
) -> LocalSigningKeyDecision {
    if !matches!(level, MemoryLevel::Procedural)
        || maturity.is_terminal()
        || !trust_class.requires_local_signature_for_validated_procedural()
    {
        LocalSigningKeyDecision::new(
            LocalSigningKeyPosture::NotRequired,
            "local_signing_key_not_required",
            "Local signing key is not required for this memory posture.",
            None,
        )
    } else if has_local_signature {
        LocalSigningKeyDecision::new(
            LocalSigningKeyPosture::Satisfied,
            "local_signing_key_satisfied",
            "High-trust procedural memory has a local signature.",
            None,
        )
    } else if matches!(maturity, RuleMaturity::Validated) {
        LocalSigningKeyDecision::new(
            LocalSigningKeyPosture::Required,
            "local_signing_key_required",
            "Validated high-trust procedural memories require a local signature before authoritative use.",
            Some(
                "Keep the memory out of authoritative procedural sections until a local signature is attached.",
            ),
        )
    } else {
        LocalSigningKeyDecision::new(
            LocalSigningKeyPosture::Recommended,
            "local_signing_key_recommended",
            "Attach a local signature before promoting this high-trust procedural memory to validated authority.",
            Some("Keep the memory advisory until a local signature is attached."),
        )
    }
}

/// Multiplicity evidence for a memory that was one recorded attempt out of a
/// declared family of sibling attempts (bd-multiplicity-aware-trust-p0u7g).
///
/// The canonical discount and completion math lives here so the trust report,
/// pack ranking, and promotion gate cannot drift apart. An incomplete declared
/// family discounts every selected member by exactly `1 / declared_size`,
/// regardless of how many siblings have subsequently been recorded.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AttemptFamilyPromotionPosture {
    /// The family has exactly the canonical selected/rejected composition.
    Eligible,
    /// A declared denominator is required before a family may promote.
    BlockedUndeclared,
    /// The declared denominator is zero and therefore cannot describe a family.
    BlockedInvalidDeclaredSize,
    /// A slot was recorded more than once.
    BlockedDuplicateSlots,
    /// One logical memory was recorded into more than one slot.
    BlockedDuplicateMembers,
    /// One logical memory belongs to more than one attempt family.
    BlockedMultipleFamilies,
    /// The family contains more members than its declared attempt count.
    BlockedOverfull,
    /// An explicit slot is outside `1..=declared_size`.
    BlockedOutOfRangeSlots,
    /// A family member lacks an explicit attempt slot.
    BlockedUnslottedMembers,
    /// One or more declared slots remain unrecorded.
    BlockedIncomplete,
    /// The slots are complete but do not contain one selected and N-1 rejected.
    BlockedInvalidComposition,
}

impl AttemptFamilyPromotionPosture {
    /// Stable machine-facing posture token.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Eligible => "eligible",
            Self::BlockedUndeclared => "blocked_undeclared",
            Self::BlockedInvalidDeclaredSize => "blocked_invalid_declared_size",
            Self::BlockedDuplicateSlots => "blocked_duplicate_slots",
            Self::BlockedDuplicateMembers => "blocked_duplicate_members",
            Self::BlockedMultipleFamilies => "blocked_multiple_families",
            Self::BlockedOverfull => "blocked_overfull",
            Self::BlockedOutOfRangeSlots => "blocked_out_of_range_slots",
            Self::BlockedUnslottedMembers => "blocked_unslotted_members",
            Self::BlockedIncomplete => "blocked_incomplete",
            Self::BlockedInvalidComposition => "blocked_invalid_composition",
        }
    }

    /// Stable explanation of the promotion posture.
    #[must_use]
    pub const fn reason(self) -> &'static str {
        match self {
            Self::Eligible => "family has the canonical selected/rejected composition",
            Self::BlockedUndeclared => "family has no declared attempt count",
            Self::BlockedInvalidDeclaredSize => "declared attempt count must be greater than zero",
            Self::BlockedDuplicateSlots => "one or more attempt slots were recorded more than once",
            Self::BlockedDuplicateMembers => {
                "one or more logical memories were recorded into multiple attempt slots"
            }
            Self::BlockedMultipleFamilies => {
                "the logical memory belongs to more than one attempt family"
            }
            Self::BlockedOverfull => "family has more members than its declared attempt count",
            Self::BlockedOutOfRangeSlots => {
                "one or more attempt slots are outside the declared attempt count"
            }
            Self::BlockedUnslottedMembers => "one or more family members have no attempt slot",
            Self::BlockedIncomplete => "not every declared attempt slot is recorded",
            Self::BlockedInvalidComposition => {
                "canonical completion requires exactly one selected member and N-1 rejected members"
            }
        }
    }
}

impl fmt::Display for AttemptFamilyPromotionPosture {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AttemptFamilyMultiplicity {
    /// Stable pre-registered family identity.
    pub family_id: String,
    /// Largest declared sibling count among recorded members, when any
    /// member declared one.
    pub declared_size: Option<u32>,
    /// Distinct in-range attempt slots covered by live members. Completion
    /// is measured in slots, never raw rows: re-recording the winner N times
    /// occupies one slot and cannot launder an incomplete family.
    pub recorded_slots: u32,
    /// Live slotted members recorded as `selected` (winners).
    pub selected_count: u32,
    /// Live slotted members recorded as `rejected` (negative siblings).
    pub rejected_count: u32,
    /// Live members without a slot; they are visible evidence but never
    /// count toward completion.
    pub unslotted_count: u32,
    /// Number of live members, including duplicates and unslotted members.
    pub member_count: u32,
    /// Members that repeated a slot already occupied by another member.
    pub duplicate_slot_count: u32,
    /// Members whose revision-stable logical identity was already recorded in
    /// another slot. Revisions share one logical identity and therefore cannot
    /// be counted as distinct sibling attempts.
    pub duplicate_member_count: u32,
    /// Members whose explicit slot is outside `1..=declared_size`.
    pub out_of_range_slot_count: u32,
}

impl AttemptFamilyMultiplicity {
    /// Aggregate member rows (slot, disposition) into the canonical
    /// multiplicity posture. Duplicate and out-of-range members remain
    /// visible because canonical promotion must reject them rather than
    /// silently deduplicating them away.
    #[must_use]
    pub fn from_members<'a>(
        family_id: String,
        declared_size: Option<u32>,
        members: impl IntoIterator<Item = (Option<u32>, Option<&'a str>)>,
    ) -> Self {
        Self::from_member_records(
            family_id,
            declared_size,
            members
                .into_iter()
                .map(|(slot, disposition)| (None, slot, disposition)),
        )
    }

    /// Aggregate member rows while preserving their revision-stable logical
    /// identities. Storage-backed consumers use this constructor so one
    /// logical memory repeated across otherwise distinct slots fails closed.
    #[must_use]
    pub fn from_identified_members<'a>(
        family_id: String,
        declared_size: Option<u32>,
        members: impl IntoIterator<Item = (&'a str, Option<u32>, Option<&'a str>)>,
    ) -> Self {
        Self::from_member_records(
            family_id,
            declared_size,
            members
                .into_iter()
                .map(|(logical_id, slot, disposition)| (Some(logical_id), slot, disposition)),
        )
    }

    fn from_member_records<'a>(
        family_id: String,
        declared_size: Option<u32>,
        members: impl IntoIterator<Item = (Option<&'a str>, Option<u32>, Option<&'a str>)>,
    ) -> Self {
        let mut all_slots = std::collections::BTreeSet::new();
        let mut seen_valid_slots = std::collections::BTreeSet::new();
        let mut seen_logical_ids = std::collections::BTreeSet::new();
        let mut selected_count = 0_u32;
        let mut rejected_count = 0_u32;
        let mut unslotted_count = 0_u32;
        let mut member_count = 0_u32;
        let mut duplicate_slot_count = 0_u32;
        let mut duplicate_member_count = 0_u32;
        let mut out_of_range_slot_count = 0_u32;
        for (logical_id, slot, disposition) in members {
            member_count = member_count.saturating_add(1);
            if logical_id.is_some_and(|logical_id| !seen_logical_ids.insert(logical_id)) {
                duplicate_member_count = duplicate_member_count.saturating_add(1);
            }
            match slot {
                Some(slot) => {
                    if !all_slots.insert(slot) {
                        duplicate_slot_count = duplicate_slot_count.saturating_add(1);
                    }
                    if declared_size.is_some_and(|declared| slot == 0 || slot > declared) {
                        out_of_range_slot_count = out_of_range_slot_count.saturating_add(1);
                        unslotted_count = unslotted_count.saturating_add(1);
                    } else if seen_valid_slots.insert(slot) {
                        match disposition {
                            Some("selected") => selected_count = selected_count.saturating_add(1),
                            Some("rejected") => rejected_count = rejected_count.saturating_add(1),
                            _ => {}
                        }
                    }
                }
                None => unslotted_count = unslotted_count.saturating_add(1),
            }
        }
        let recorded_slots = u32::try_from(seen_valid_slots.len()).unwrap_or(u32::MAX);
        Self {
            family_id,
            declared_size,
            recorded_slots,
            selected_count,
            rejected_count,
            unslotted_count,
            member_count,
            duplicate_slot_count,
            duplicate_member_count,
            out_of_range_slot_count,
        }
    }

    /// Declared sibling slots that no live member occupies.
    #[must_use]
    pub fn unrecorded_count(&self) -> u32 {
        self.declared_size
            .map_or(0, |declared| declared.saturating_sub(self.recorded_slots))
    }

    /// True when every declared sibling slot is occupied by exactly one live
    /// member. Composition remains a separate canonical-promotion check.
    #[must_use]
    pub fn is_complete(&self) -> bool {
        self.declared_size.is_some_and(|declared| {
            declared > 0
                && self.recorded_slots == declared
                && self.member_count == declared
                && self.duplicate_slot_count == 0
                && self.duplicate_member_count == 0
                && self.out_of_range_slot_count == 0
                && self.unslotted_count == 0
        })
    }

    /// True for the selection-bias shape the discount exists for: a declared
    /// family of more than one whose recorded slot coverage is a lone
    /// surviving winner.
    #[must_use]
    pub fn is_survivor_only(&self) -> bool {
        self.declared_size.is_some_and(|declared| declared > 1)
            && self.recorded_slots <= 1
            && self.selected_count >= 1
    }

    /// Deterministic multiplicity discount in (0.0, 1.0]. A declared family
    /// larger than one remains exactly `1 / declared_size` until it reaches
    /// the canonical promotion posture; neither partial coverage nor an
    /// invalid complete-looking composition can launder selection bias.
    /// Rejected evidence remains undiscounted through
    /// [`Self::member_discount_factor`].
    #[must_use]
    pub fn discount_factor(&self) -> f32 {
        let Some(declared) = self.declared_size else {
            return 1.0;
        };
        if declared <= 1
            || matches!(
                self.promotion_posture(),
                AttemptFamilyPromotionPosture::Eligible
            )
        {
            return 1.0;
        }
        #[allow(clippy::cast_possible_truncation)]
        let factor = (1.0_f64 / f64::from(declared)) as f32;
        factor
    }

    /// Stable promotion posture for this family.
    #[must_use]
    pub fn promotion_posture(&self) -> AttemptFamilyPromotionPosture {
        let Some(declared) = self.declared_size else {
            return AttemptFamilyPromotionPosture::BlockedUndeclared;
        };
        if declared == 0 {
            return AttemptFamilyPromotionPosture::BlockedInvalidDeclaredSize;
        }
        if self.duplicate_slot_count > 0 {
            return AttemptFamilyPromotionPosture::BlockedDuplicateSlots;
        }
        if self.duplicate_member_count > 0 {
            return AttemptFamilyPromotionPosture::BlockedDuplicateMembers;
        }
        if self.member_count > declared {
            return AttemptFamilyPromotionPosture::BlockedOverfull;
        }
        if self.out_of_range_slot_count > 0 {
            return AttemptFamilyPromotionPosture::BlockedOutOfRangeSlots;
        }
        if self.unslotted_count > 0 {
            return AttemptFamilyPromotionPosture::BlockedUnslottedMembers;
        }
        if !self.is_complete() {
            return AttemptFamilyPromotionPosture::BlockedIncomplete;
        }
        if self.selected_count == 1 && self.rejected_count == declared - 1 {
            AttemptFamilyPromotionPosture::Eligible
        } else {
            AttemptFamilyPromotionPosture::BlockedInvalidComposition
        }
    }

    /// Stable human-readable reason for the current promotion posture.
    #[must_use]
    pub fn promotion_reason(&self) -> &'static str {
        self.promotion_posture().reason()
    }

    /// True when the family has the declared, exact, canonical fan-out shape:
    /// one selected member and `N - 1` rejected members.
    #[must_use]
    pub fn is_promotion_eligible(&self) -> bool {
        matches!(
            self.promotion_posture(),
            AttemptFamilyPromotionPosture::Eligible
        )
    }

    /// Per-member ranking discount. The multiplicity discount exists to
    /// deflate survivor-selection bias, so it applies to `selected` winners
    /// only; `rejected` siblings are the negative/safety evidence the family
    /// exists to preserve and are never discounted (deflating them would
    /// hide the very failures the denominator is meant to surface).
    #[must_use]
    pub fn member_discount_factor(&self, disposition: Option<&str>) -> f32 {
        match disposition {
            Some("selected") => self.discount_factor(),
            _ => 1.0,
        }
    }

    /// Human-facing summary of the recorded/declared posture, e.g.
    /// `"1 of 18 attempt slots recorded; 17 unrecorded"`.
    #[must_use]
    pub fn summary(&self) -> String {
        let mut summary = match self.declared_size {
            Some(declared) => format!(
                "{} of {declared} attempt slots recorded; {} unrecorded",
                self.recorded_slots,
                self.unrecorded_count()
            ),
            None => format!(
                "{} attempt slots recorded; no declared sibling count",
                self.recorded_slots
            ),
        };
        if self.unslotted_count > 0 {
            summary.push_str(&format!(
                " ({} unslotted member(s) excluded from completion)",
                self.unslotted_count
            ));
        }
        summary
    }
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use crate::models::memory::MemoryLevel;
    use crate::models::rule::RuleMaturity;

    use super::{
        AttemptFamilyMultiplicity, AttemptFamilyPromotionPosture, LocalSigningKeyPosture,
        ParseTrustClassError, TrustClass, evaluate_local_signing_key_policy,
    };

    #[test]
    fn attempt_family_promotion_posture_is_exported_by_models_facade() {
        let posture: crate::models::AttemptFamilyPromotionPosture =
            AttemptFamilyPromotionPosture::BlockedUndeclared;
        assert_eq!(posture.as_str(), "blocked_undeclared");
    }

    #[test]
    fn incomplete_selected_n18_stays_at_one_over_n_at_every_coverage_level() {
        for recorded_slots in [1_u32, 2, 17] {
            let members = (1..=recorded_slots).map(|slot| {
                (
                    Some(slot),
                    Some(if slot == 1 { "selected" } else { "rejected" }),
                )
            });
            let family = AttemptFamilyMultiplicity::from_members(
                format!("fam-n18-{recorded_slots}"),
                Some(18),
                members,
            );

            assert!(!family.is_complete());
            assert_eq!(
                family.promotion_posture(),
                AttemptFamilyPromotionPosture::BlockedIncomplete
            );
            assert_eq!(
                family.promotion_reason(),
                "not every declared attempt slot is recorded"
            );
            assert!((family.member_discount_factor(Some("selected")) - 1.0 / 18.0).abs() < 1.0e-7);
            assert!((family.member_discount_factor(Some("rejected")) - 1.0).abs() < f32::EPSILON);
        }
    }

    #[test]
    fn canonical_completion_requires_declared_exact_slots_and_composition() {
        let canonical_n18 = AttemptFamilyMultiplicity::from_members(
            "fam-canonical-n18".to_owned(),
            Some(18),
            (1..=18).map(|slot| {
                (
                    Some(slot),
                    Some(if slot == 1 { "selected" } else { "rejected" }),
                )
            }),
        );
        assert!(canonical_n18.is_complete());
        assert!(canonical_n18.is_promotion_eligible());
        assert!((canonical_n18.discount_factor() - 1.0).abs() < f32::EPSILON);

        let canonical = AttemptFamilyMultiplicity::from_members(
            "fam-canonical".to_owned(),
            Some(3),
            [
                (Some(1), Some("selected")),
                (Some(2), Some("rejected")),
                (Some(3), Some("rejected")),
            ],
        );
        assert!(canonical.is_complete());
        assert!(canonical.is_promotion_eligible());
        assert_eq!(
            canonical.promotion_posture(),
            AttemptFamilyPromotionPosture::Eligible
        );
        assert_eq!(
            canonical.promotion_reason(),
            "family has the canonical selected/rejected composition"
        );
        assert!((canonical.member_discount_factor(Some("selected")) - 1.0).abs() < f32::EPSILON);

        // All-winners laundering fills every slot but has no negative evidence.
        let all_selected = AttemptFamilyMultiplicity::from_members(
            "fam-all-selected".to_owned(),
            Some(3),
            [
                (Some(1), Some("selected")),
                (Some(2), Some("selected")),
                (Some(3), Some("selected")),
            ],
        );
        assert!(all_selected.is_complete());
        assert!(!all_selected.is_promotion_eligible());
        assert_eq!(
            all_selected.promotion_posture(),
            AttemptFamilyPromotionPosture::BlockedInvalidComposition
        );
        assert_eq!(
            all_selected.promotion_reason(),
            "canonical completion requires exactly one selected member and N-1 rejected members"
        );
        assert!((all_selected.member_discount_factor(Some("selected")) - 1.0 / 3.0).abs() < 1.0e-7);

        let undeclared = AttemptFamilyMultiplicity::from_members(
            "fam-undeclared".to_owned(),
            None,
            [(Some(1), Some("selected")), (Some(2), Some("rejected"))],
        );
        assert!(!undeclared.is_complete());
        assert!(!undeclared.is_promotion_eligible());
        assert_eq!(
            undeclared.promotion_posture(),
            AttemptFamilyPromotionPosture::BlockedUndeclared
        );
    }

    #[test]
    fn malformed_or_composition_invalid_families_have_stable_blocking_postures() {
        let duplicate = AttemptFamilyMultiplicity::from_members(
            "fam-duplicate".to_owned(),
            Some(3),
            [
                (Some(1), Some("selected")),
                (Some(1), Some("rejected")),
                (Some(2), Some("rejected")),
            ],
        );
        assert_eq!(duplicate.duplicate_slot_count, 1);
        assert_eq!(
            duplicate.promotion_posture(),
            AttemptFamilyPromotionPosture::BlockedDuplicateSlots
        );

        let duplicate_member = AttemptFamilyMultiplicity::from_identified_members(
            "fam-duplicate-member".to_owned(),
            Some(2),
            [
                ("logical-winner", Some(1), Some("selected")),
                ("logical-winner", Some(2), Some("rejected")),
            ],
        );
        assert_eq!(duplicate_member.duplicate_member_count, 1);
        assert!(!duplicate_member.is_complete());
        assert_eq!(
            duplicate_member.promotion_posture(),
            AttemptFamilyPromotionPosture::BlockedDuplicateMembers
        );
        assert_eq!(
            duplicate_member.promotion_reason(),
            "one or more logical memories were recorded into multiple attempt slots"
        );
        assert!(
            (duplicate_member.member_discount_factor(Some("selected")) - 0.5).abs() < f32::EPSILON
        );

        let overfull = AttemptFamilyMultiplicity::from_members(
            "fam-overfull".to_owned(),
            Some(3),
            [
                (Some(1), Some("selected")),
                (Some(2), Some("rejected")),
                (Some(3), Some("rejected")),
                (Some(4), Some("rejected")),
            ],
        );
        assert_eq!(overfull.out_of_range_slot_count, 1);
        assert_eq!(
            overfull.promotion_posture(),
            AttemptFamilyPromotionPosture::BlockedOverfull
        );

        let out_of_range = AttemptFamilyMultiplicity::from_members(
            "fam-out-of-range".to_owned(),
            Some(3),
            [
                (Some(1), Some("selected")),
                (Some(2), Some("rejected")),
                (Some(4), Some("rejected")),
            ],
        );
        assert_eq!(
            out_of_range.promotion_posture(),
            AttemptFamilyPromotionPosture::BlockedOutOfRangeSlots
        );
        assert_eq!(
            out_of_range.promotion_reason(),
            "one or more attempt slots are outside the declared attempt count"
        );

        let unslotted = AttemptFamilyMultiplicity::from_members(
            "fam-unslotted".to_owned(),
            Some(3),
            [
                (Some(1), Some("selected")),
                (Some(2), Some("rejected")),
                (None, Some("rejected")),
            ],
        );
        assert_eq!(
            unslotted.promotion_posture(),
            AttemptFamilyPromotionPosture::BlockedUnslottedMembers
        );
        assert_eq!(
            unslotted.promotion_reason(),
            "one or more family members have no attempt slot"
        );

        let missing_disposition = AttemptFamilyMultiplicity::from_members(
            "fam-composition".to_owned(),
            Some(2),
            [(Some(1), Some("selected")), (Some(2), None)],
        );
        assert!(missing_disposition.is_complete());
        assert_eq!(
            missing_disposition.promotion_posture(),
            AttemptFamilyPromotionPosture::BlockedInvalidComposition
        );
    }

    #[test]
    fn trust_class_round_trip_for_every_variant() {
        for class in TrustClass::all() {
            let rendered = class.to_string();
            let parsed = TrustClass::from_str(&rendered);
            assert_eq!(parsed, Ok(class));
        }
        assert_eq!(
            TrustClass::from_str(" Agent-Validated "),
            Ok(TrustClass::AgentValidated)
        );
        assert_eq!(
            TrustClass::from_str("humanExplicit"),
            Ok(TrustClass::HumanExplicit)
        );
        assert_eq!(
            TrustClass::from_str("PeerHumanAttested"),
            Ok(TrustClass::PeerHumanAttested)
        );
        assert_eq!(
            TrustClass::from_str("CassEvidence"),
            Ok(TrustClass::CassEvidence)
        );
        assert_eq!(
            TrustClass::from_str("legacyImport"),
            Ok(TrustClass::LegacyImport)
        );
    }

    #[test]
    fn trust_class_initial_confidences_match_adr() {
        assert!((TrustClass::HumanExplicit.initial_confidence() - 0.85).abs() < 0.001);
        assert!((TrustClass::PeerHumanAttested.initial_confidence() - 0.75).abs() < 0.001);
        assert!((TrustClass::AgentValidated.initial_confidence() - 0.65).abs() < 0.001);
        assert!((TrustClass::AgentAssertion.initial_confidence() - 0.50).abs() < 0.001);
        assert!((TrustClass::CassEvidence.initial_confidence() - 0.45).abs() < 0.001);
        assert!((TrustClass::LegacyImport.initial_confidence() - 0.30).abs() < 0.001);
    }

    #[test]
    fn trust_class_rejects_unknown_input() {
        assert_eq!(
            TrustClass::from_str("unknown_class"),
            Err(ParseTrustClassError {
                input: "unknown_class".to_owned(),
            })
        );
    }

    #[test]
    fn trust_class_as_str_is_stable() {
        assert_eq!(
            TrustClass::all().map(TrustClass::as_str),
            [
                "human_explicit",
                "peer_human_attested",
                "agent_validated",
                "agent_assertion",
                "cass_evidence",
                "legacy_import",
            ]
        );
        assert_eq!(TrustClass::HumanExplicit.as_str(), "human_explicit");
        assert_eq!(
            TrustClass::PeerHumanAttested.as_str(),
            "peer_human_attested"
        );
        assert_eq!(TrustClass::AgentValidated.as_str(), "agent_validated");
        assert_eq!(TrustClass::AgentAssertion.as_str(), "agent_assertion");
        assert_eq!(TrustClass::CassEvidence.as_str(), "cass_evidence");
        assert_eq!(TrustClass::LegacyImport.as_str(), "legacy_import");
    }

    #[test]
    fn local_signing_policy_requires_validated_high_trust_procedural_signatures() {
        for trust_class in [
            TrustClass::HumanExplicit,
            TrustClass::PeerHumanAttested,
            TrustClass::AgentValidated,
        ] {
            let decision = evaluate_local_signing_key_policy(
                MemoryLevel::Procedural,
                trust_class,
                RuleMaturity::Validated,
                false,
            );
            assert_eq!(decision.posture, LocalSigningKeyPosture::Required);
            assert_eq!(decision.code, "local_signing_key_required");
            assert!(decision.is_blocking());
            assert!(decision.repair.is_some());
        }
    }

    #[test]
    fn local_signing_policy_is_satisfied_by_present_signature() {
        let decision = evaluate_local_signing_key_policy(
            MemoryLevel::Procedural,
            TrustClass::HumanExplicit,
            RuleMaturity::Validated,
            true,
        );

        assert_eq!(decision.posture, LocalSigningKeyPosture::Satisfied);
        assert_eq!(decision.code, "local_signing_key_satisfied");
        assert!(!decision.is_blocking());
    }

    #[test]
    fn local_signing_policy_recommends_signature_before_promotion() {
        let decision = evaluate_local_signing_key_policy(
            MemoryLevel::Procedural,
            TrustClass::AgentValidated,
            RuleMaturity::Candidate,
            false,
        );

        assert_eq!(decision.posture, LocalSigningKeyPosture::Recommended);
        assert_eq!(decision.code, "local_signing_key_recommended");
        assert!(!decision.is_blocking());
    }

    #[test]
    fn local_signing_policy_ignores_non_authoritative_postures() {
        for (level, trust_class, maturity) in [
            (
                MemoryLevel::Semantic,
                TrustClass::HumanExplicit,
                RuleMaturity::Validated,
            ),
            (
                MemoryLevel::Procedural,
                TrustClass::AgentAssertion,
                RuleMaturity::Validated,
            ),
            (
                MemoryLevel::Procedural,
                TrustClass::CassEvidence,
                RuleMaturity::Validated,
            ),
            (
                MemoryLevel::Procedural,
                TrustClass::LegacyImport,
                RuleMaturity::Validated,
            ),
            (
                MemoryLevel::Procedural,
                TrustClass::HumanExplicit,
                RuleMaturity::Deprecated,
            ),
            (
                MemoryLevel::Procedural,
                TrustClass::AgentValidated,
                RuleMaturity::Superseded,
            ),
        ] {
            let decision = evaluate_local_signing_key_policy(level, trust_class, maturity, false);
            assert_eq!(decision.posture, LocalSigningKeyPosture::NotRequired);
            assert_eq!(decision.code, "local_signing_key_not_required");
            assert!(!decision.is_blocking());
        }
    }

    #[test]
    fn local_signing_key_posture_wire_order_is_stable() {
        let rendered: Vec<&str> = LocalSigningKeyPosture::all()
            .iter()
            .map(|posture| posture.as_str())
            .collect();

        assert_eq!(
            rendered.as_slice(),
            &["not_required", "recommended", "required", "satisfied"],
        );
    }
}