|
| 1 | +//! Savant role-key catalogue — 25 disjoint [`RoleKey`] slices, one |
| 2 | +//! identity per Odoo savant in [`crate::savants::SAVANTS`]. |
| 3 | +//! |
| 4 | +//! Lands in the SMB headroom `[14096..16384)` that |
| 5 | +//! [`crate::grammar::role_keys`] reserves for future SMB keys (per the |
| 6 | +//! LF-2 16K resize). Each savant gets 90 dims of pseudo-random bipolar |
| 7 | +//! identity, FNV-64-seeded from the savant's name; disjoint by |
| 8 | +//! construction. |
| 9 | +//! |
| 10 | +//! ## Layout |
| 11 | +//! |
| 12 | +//! ```text |
| 13 | +//! [14096 .. 14186) savant 1 (FiscalPositionResolver) |
| 14 | +//! [14186 .. 14276) savant 2 (PartnerTrustAdvisor) |
| 15 | +//! [14276 .. 14366) savant 3 (PricelistAssignmentAgent) |
| 16 | +//! ... |
| 17 | +//! [16256 .. 16346) savant 25 (BackorderJudge, roster id 26 — id 16 absent) |
| 18 | +//! [16346 .. 16384) 38 dims headroom (reserved for future callcenter keys) |
| 19 | +//! ``` |
| 20 | +//! |
| 21 | +//! Total footprint: 25 × 90 = 2250 dims, within the 2288-dim SMB |
| 22 | +//! headroom (`grammar::role_keys::VSA_DIMS = 16_384` minus STEUER_KEY's |
| 23 | +//! end at `14_096`). |
| 24 | +//! |
| 25 | +//! `D-ODOO-SAV-5b` of `odoo-savant-reasoners-v2`. |
| 26 | +
|
| 27 | +use std::sync::LazyLock; |
| 28 | + |
| 29 | +use crate::grammar::role_keys::RoleKey; |
| 30 | +use crate::savants::{savant, savant_by_name, Savant, SAVANTS}; |
| 31 | + |
| 32 | +/// Start of the savant identity zone — directly after STEUER_KEY's |
| 33 | +/// `[13584..14096)` slice in [`crate::grammar::role_keys`]. |
| 34 | +pub const SAVANT_SLICE_START: usize = 14_096; |
| 35 | + |
| 36 | +/// One identity slice per savant — 90 dims of FNV-seeded pseudo-random |
| 37 | +/// bits. 25 × 90 = 2250 dims < the 2288-dim SMB headroom. |
| 38 | +pub const SAVANT_SLICE_WIDTH: usize = 90; |
| 39 | + |
| 40 | +/// End of the savant identity zone: `SAVANT_SLICE_START + 25 * |
| 41 | +/// SAVANT_SLICE_WIDTH = 16_346`. 38 dims of headroom remain. |
| 42 | +pub const SAVANT_SLICE_END: usize = SAVANT_SLICE_START + 25 * SAVANT_SLICE_WIDTH; |
| 43 | + |
| 44 | +/// The 25 savant role keys in roster order (same order as |
| 45 | +/// [`crate::savants::SAVANTS`]). |
| 46 | +/// |
| 47 | +/// Indexing matches roster index `i` (NOT savant `id`; roster id 16 is |
| 48 | +/// intentionally absent per `SAVANTS.md`, so use the lookup helpers |
| 49 | +/// [`savant_role_key`] / [`savant_role_key_by_name`] rather than direct |
| 50 | +/// indexing). |
| 51 | +pub static SAVANT_ROLE_KEYS: LazyLock<[RoleKey; 25]> = LazyLock::new(|| { |
| 52 | + core::array::from_fn(|i| { |
| 53 | + let s: &Savant = &SAVANTS[i]; |
| 54 | + let start = SAVANT_SLICE_START + i * SAVANT_SLICE_WIDTH; |
| 55 | + let end = start + SAVANT_SLICE_WIDTH; |
| 56 | + RoleKey::generate(s.name, start, end) |
| 57 | + }) |
| 58 | +}); |
| 59 | + |
| 60 | +/// Look up a savant's role key by roster id (1..=15, 17..=26; id 16 |
| 61 | +/// intentionally absent). |
| 62 | +/// |
| 63 | +/// Returns `None` if the id does not appear in [`SAVANTS`]. |
| 64 | +pub fn savant_role_key(id: u8) -> Option<&'static RoleKey> { |
| 65 | + let _ = savant(id)?; |
| 66 | + SAVANTS |
| 67 | + .iter() |
| 68 | + .position(|s| s.id == id) |
| 69 | + .map(|i| &SAVANT_ROLE_KEYS[i]) |
| 70 | +} |
| 71 | + |
| 72 | +/// Look up a savant's role key by name (the `SAVANTS[i].name`). |
| 73 | +/// |
| 74 | +/// Returns `None` if the name does not appear in [`SAVANTS`]. |
| 75 | +pub fn savant_role_key_by_name(name: &str) -> Option<&'static RoleKey> { |
| 76 | + let _ = savant_by_name(name)?; |
| 77 | + SAVANTS |
| 78 | + .iter() |
| 79 | + .position(|s| s.name == name) |
| 80 | + .map(|i| &SAVANT_ROLE_KEYS[i]) |
| 81 | +} |
| 82 | + |
| 83 | +#[cfg(test)] |
| 84 | +mod tests { |
| 85 | + use super::*; |
| 86 | + use crate::grammar::role_keys::VSA_DIMS; |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn slices_disjoint_and_in_bounds() { |
| 90 | + for (i, key) in SAVANT_ROLE_KEYS.iter().enumerate() { |
| 91 | + let expected_start = SAVANT_SLICE_START + i * SAVANT_SLICE_WIDTH; |
| 92 | + let expected_end = expected_start + SAVANT_SLICE_WIDTH; |
| 93 | + assert_eq!(key.slice_start, expected_start, "savant index {i}"); |
| 94 | + assert_eq!(key.slice_end, expected_end, "savant index {i}"); |
| 95 | + assert_eq!(key.slice_width(), SAVANT_SLICE_WIDTH); |
| 96 | + assert!(key.slice_end <= VSA_DIMS, "savant {i} fits VSA space"); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn savant_zone_fits_in_smb_headroom() { |
| 102 | + // grammar::role_keys ends at SMB STEUER_KEY = 14_096 and reserves |
| 103 | + // [14_096 .. 16_384) (= 2288 dims) as headroom. We claim 2250 of |
| 104 | + // those 2288 dims; 38 remain. |
| 105 | + assert_eq!(SAVANT_SLICE_END, 16_346); |
| 106 | + assert!(SAVANT_SLICE_END <= VSA_DIMS); |
| 107 | + assert_eq!(VSA_DIMS - SAVANT_SLICE_END, 38, "headroom remaining"); |
| 108 | + } |
| 109 | + |
| 110 | + #[test] |
| 111 | + fn id_lookup_matches_name_lookup() { |
| 112 | + // FiscalPositionResolver is savant id 1, roster index 0. |
| 113 | + let by_id = savant_role_key(1).expect("id 1"); |
| 114 | + let by_name = savant_role_key_by_name("FiscalPositionResolver").expect("name"); |
| 115 | + assert_eq!(by_id.label, by_name.label); |
| 116 | + assert_eq!(by_id.slice_start, by_name.slice_start); |
| 117 | + assert_eq!(by_id.slice_start, SAVANT_SLICE_START, "first roster slot"); |
| 118 | + } |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn id_16_absent() { |
| 122 | + // SAVANTS.md skips roster id 16. Lookup must return None. |
| 123 | + assert!(savant_role_key(16).is_none()); |
| 124 | + } |
| 125 | + |
| 126 | + #[test] |
| 127 | + fn last_savant_is_backorder_judge() { |
| 128 | + // BackorderJudge has roster id 26 and lives at roster index 24 |
| 129 | + // (the 25th and final savant). |
| 130 | + let key = savant_role_key(26).expect("id 26"); |
| 131 | + assert_eq!(key.label, "BackorderJudge"); |
| 132 | + assert_eq!(key.slice_start, SAVANT_SLICE_START + 24 * SAVANT_SLICE_WIDTH); |
| 133 | + assert_eq!(key.slice_end, SAVANT_SLICE_END); |
| 134 | + } |
| 135 | + |
| 136 | + #[test] |
| 137 | + fn deterministic_pseudo_random_bits() { |
| 138 | + // FNV-64-seeded from the savant's name: same name → same bits. |
| 139 | + // 90-dim slice: roughly half the bits should be set (the LCG is |
| 140 | + // unbiased over a 90-bit window). |
| 141 | + let key = savant_role_key_by_name("FiscalPositionResolver").unwrap(); |
| 142 | + let total_set: u32 = key.words.iter().map(|w| w.count_ones()).sum(); |
| 143 | + assert!(total_set > 20, "some bits set in 90-dim slice: {total_set}"); |
| 144 | + assert!(total_set < 80, "some bits clear in 90-dim slice: {total_set}"); |
| 145 | + } |
| 146 | + |
| 147 | + #[test] |
| 148 | + fn no_overlap_with_grammar_slices() { |
| 149 | + // grammar::role_keys SMB keys end at STEUER_KEY's `14_096`; savants |
| 150 | + // start at `14_096`. SPO core roles and TEKAMOLO slots all sit |
| 151 | + // below `14_096`, so no overlap by construction. |
| 152 | + use crate::grammar::role_keys::{OBJECT_KEY, PREDICATE_KEY, SUBJECT_KEY}; |
| 153 | + assert!(SUBJECT_KEY.slice_end <= SAVANT_SLICE_START); |
| 154 | + assert!(PREDICATE_KEY.slice_end <= SAVANT_SLICE_START); |
| 155 | + assert!(OBJECT_KEY.slice_end <= SAVANT_SLICE_START); |
| 156 | + } |
| 157 | +} |
0 commit comments