|
| 1 | +//! `codebook` — per-family codebook (D-GV2-2, feature `guid-v2-tail`). |
| 2 | +//! |
| 3 | +//! The finer sibling of [`class_view`](crate::class_view) (`classid → ClassView`): |
| 4 | +//! here **`family → Codebook`**. Each family owns a ≤256-entry vocabulary that its |
| 5 | +//! nodes index by a **1-byte in-family adapter** — the 256×256 Morton centroid |
| 6 | +//! tile of `E-UNIFORM-MORTON-TILE-PYRAMID`, with ≤256 leaves for the 1-byte index. |
| 7 | +//! |
| 8 | +//! Why per-family (not one global codebook): it dissolves the aiwar "60 noisy |
| 9 | +//! families" at the root — each family's vocabulary is small and clean, and a |
| 10 | +//! within-family reference is an **exact** index into that family's codebook (no |
| 11 | +//! `& 0xFF` low-byte aliasing). The `family` tier (u16) selects the codebook |
| 12 | +//! (head-only routing); the 1-byte index resolves within it. Cross-family edges |
| 13 | +//! carry `(family, index)` and decode via [`FamilyCodebookRegistry::resolve`]. |
| 14 | +//! |
| 15 | +//! A family that outgrows 256 entries **splits** (mint a sub-family — cheap with |
| 16 | +//! the v2 16-bit family tier), never widens the byte ([`Codebook::intern`] |
| 17 | +//! returns `None` on overflow as the split signal). The codebook is the family |
| 18 | +//! node's **episodic basin** content (`E-MIXIN-IS-AN-ADDRESS-REFERENCE-NOT-A-COPY`): |
| 19 | +//! members reference it by the 1-byte index, the shared vocabulary lives once. |
| 20 | +//! |
| 21 | +//! This module is the TYPE + in-memory registry (the `LazyLock` tier). The |
| 22 | +//! Lance-backed persistence + `OntologyRegistry` integration are deferred to the |
| 23 | +//! ontology-crate wiring step (see plan `guid-v2-tail-per-family-codebook-v1.md`). |
| 24 | +
|
| 25 | +use std::collections::HashMap; |
| 26 | + |
| 27 | +/// Max entries per family codebook — the 1-byte in-family index cap. A family |
| 28 | +/// that needs more SPLITS (mint a sub-family), never widens the byte. |
| 29 | +pub const CODEBOOK_CAP: usize = 256; |
| 30 | + |
| 31 | +/// A per-family codebook: insertion-ordered label interning, `index ↔ label`. |
| 32 | +/// `index` is the 1-byte in-family adapter value (`0..len`). ≤[`CODEBOOK_CAP`]. |
| 33 | +#[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 34 | +pub struct Codebook { |
| 35 | + entries: Vec<String>, // index → label (insertion order) |
| 36 | + by_label: HashMap<String, u8>, // label → index |
| 37 | +} |
| 38 | + |
| 39 | +impl Codebook { |
| 40 | + /// An empty codebook. |
| 41 | + pub fn new() -> Self { |
| 42 | + Self::default() |
| 43 | + } |
| 44 | + |
| 45 | + /// Intern `label` → its 1-byte index (insertion order, deduped). Returns |
| 46 | + /// `None` if the codebook is full (256) and `label` is new — the caller must |
| 47 | + /// SPLIT the family (the `CODEBOOK_CAP` overflow signal). An already-present |
| 48 | + /// label always resolves (even at capacity). |
| 49 | + pub fn intern(&mut self, label: &str) -> Option<u8> { |
| 50 | + if let Some(&i) = self.by_label.get(label) { |
| 51 | + return Some(i); |
| 52 | + } |
| 53 | + if self.entries.len() >= CODEBOOK_CAP { |
| 54 | + return None; |
| 55 | + } |
| 56 | + let i = self.entries.len() as u8; |
| 57 | + self.entries.push(label.to_string()); |
| 58 | + self.by_label.insert(label.to_string(), i); |
| 59 | + Some(i) |
| 60 | + } |
| 61 | + |
| 62 | + /// The 1-byte index of `label`, if interned. |
| 63 | + pub fn index_of(&self, label: &str) -> Option<u8> { |
| 64 | + self.by_label.get(label).copied() |
| 65 | + } |
| 66 | + |
| 67 | + /// The label at `index`, if present. |
| 68 | + pub fn label(&self, index: u8) -> Option<&str> { |
| 69 | + self.entries.get(index as usize).map(String::as_str) |
| 70 | + } |
| 71 | + |
| 72 | + /// Number of interned entries. |
| 73 | + pub fn len(&self) -> usize { |
| 74 | + self.entries.len() |
| 75 | + } |
| 76 | + |
| 77 | + /// Whether the codebook holds no entries. |
| 78 | + pub fn is_empty(&self) -> bool { |
| 79 | + self.entries.is_empty() |
| 80 | + } |
| 81 | + |
| 82 | + /// Whether the codebook is at [`CODEBOOK_CAP`] (a new label would overflow → |
| 83 | + /// split the family). |
| 84 | + pub fn is_full(&self) -> bool { |
| 85 | + self.entries.len() >= CODEBOOK_CAP |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/// `family → Codebook` — the per-family codebook registry, the finer sibling of |
| 90 | +/// `classid → ClassView`. In-memory (the `LazyLock` tier); a Lance-backed, |
| 91 | +/// `OntologyRegistry`-integrated variant is deferred. |
| 92 | +#[derive(Debug, Clone, Default)] |
| 93 | +pub struct FamilyCodebookRegistry { |
| 94 | + books: HashMap<u16, Codebook>, |
| 95 | +} |
| 96 | + |
| 97 | +impl FamilyCodebookRegistry { |
| 98 | + /// An empty registry. |
| 99 | + pub fn new() -> Self { |
| 100 | + Self::default() |
| 101 | + } |
| 102 | + |
| 103 | + /// The codebook for `family`, creating an empty one if absent. |
| 104 | + pub fn entry(&mut self, family: u16) -> &mut Codebook { |
| 105 | + self.books.entry(family).or_default() |
| 106 | + } |
| 107 | + |
| 108 | + /// The codebook for `family`, if it exists (read-only). |
| 109 | + pub fn get(&self, family: u16) -> Option<&Codebook> { |
| 110 | + self.books.get(&family) |
| 111 | + } |
| 112 | + |
| 113 | + /// Intern `label` into `family`'s codebook → its 1-byte index. `None` on |
| 114 | + /// codebook overflow (split the family). |
| 115 | + pub fn intern(&mut self, family: u16, label: &str) -> Option<u8> { |
| 116 | + self.entry(family).intern(label) |
| 117 | + } |
| 118 | + |
| 119 | + /// Resolve a cross-family reference `(family, index)` → label — the decode of |
| 120 | + /// an out-of-family adapter / `references` edge. |
| 121 | + pub fn resolve(&self, family: u16, index: u8) -> Option<&str> { |
| 122 | + self.books.get(&family).and_then(|cb| cb.label(index)) |
| 123 | + } |
| 124 | + |
| 125 | + /// Number of families with a codebook. |
| 126 | + pub fn families(&self) -> usize { |
| 127 | + self.books.len() |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +#[cfg(test)] |
| 132 | +mod tests { |
| 133 | + use super::*; |
| 134 | + |
| 135 | + #[test] |
| 136 | + fn intern_dedups_and_assigns_sequential_indices() { |
| 137 | + let mut cb = Codebook::new(); |
| 138 | + assert_eq!(cb.intern("Nation"), Some(0)); |
| 139 | + assert_eq!(cb.intern("TechCompany"), Some(1)); |
| 140 | + assert_eq!(cb.intern("Nation"), Some(0)); // dedup |
| 141 | + assert_eq!(cb.len(), 2); |
| 142 | + assert_eq!(cb.index_of("TechCompany"), Some(1)); |
| 143 | + assert_eq!(cb.label(0), Some("Nation")); |
| 144 | + assert_eq!(cb.label(9), None); |
| 145 | + } |
| 146 | + |
| 147 | + #[test] |
| 148 | + fn codebook_overflow_signals_split() { |
| 149 | + let mut cb = Codebook::new(); |
| 150 | + for i in 0..CODEBOOK_CAP { |
| 151 | + assert!(cb.intern(&format!("e{i}")).is_some()); |
| 152 | + } |
| 153 | + assert!(cb.is_full()); |
| 154 | + // a NEW label overflows → None (split the family)… |
| 155 | + assert_eq!(cb.intern("one_too_many"), None); |
| 156 | + // …but an already-interned label still resolves at capacity. |
| 157 | + assert_eq!(cb.intern("e0"), Some(0)); |
| 158 | + } |
| 159 | + |
| 160 | + #[test] |
| 161 | + fn registry_scopes_codebooks_per_family() { |
| 162 | + // The SAME label gets INDEPENDENT indices in different families — the |
| 163 | + // whole point of per-family scoping (no global contamination). |
| 164 | + let mut reg = FamilyCodebookRegistry::new(); |
| 165 | + assert_eq!(reg.intern(0x0001, "Issue"), Some(0)); |
| 166 | + assert_eq!(reg.intern(0x0001, "Bug"), Some(1)); |
| 167 | + assert_eq!(reg.intern(0x0002, "Issue"), Some(0)); // family 2's own index 0 |
| 168 | + assert_eq!(reg.families(), 2); |
| 169 | + // cross-family resolve (family, index) → label |
| 170 | + assert_eq!(reg.resolve(0x0001, 1), Some("Bug")); |
| 171 | + assert_eq!(reg.resolve(0x0002, 0), Some("Issue")); |
| 172 | + assert_eq!(reg.resolve(0x0099, 0), None); // unknown family |
| 173 | + } |
| 174 | +} |
0 commit comments