Skip to main content

ccf_core/
vocabulary.rs

1//! Generic sensor vocabulary — the platform-independent context key system.
2//!
3//! Patent Claims 1 and 8: composite sensor context key as the fundamental unit
4//! of situational awareness.
5//!
6//! # Implementing for a new platform
7//!
8//! ```rust,ignore
9//! use ccf_core::vocabulary::{SensorVocabulary, ContextKey};
10//!
11//! #[derive(Clone, Debug, PartialEq, Eq, Hash)]
12//! pub struct ThreeSensorBot {
13//!     pub light: u8,   // 0=dark, 1=dim, 2=bright
14//!     pub sound: u8,   // 0=quiet, 1=loud
15//!     pub motion: u8,  // 0=still, 1=moving
16//! }
17//!
18//! impl SensorVocabulary<3> for ThreeSensorBot {
19//!     fn to_feature_vec(&self) -> [f32; 3] {
20//!         [self.light as f32 / 2.0, self.sound as f32, self.motion as f32]
21//!     }
22//! }
23//! // Now ContextKey::<ThreeSensorBot, 3> works with the full CCF stack.
24//! ```
25//!
26//! # Invariants
27//! - **I-DIST-001** — no_std compatible; no heap allocation required
28//! - **I-DIST-002** — zero platform-specific bounds on the trait
29//! - **I-DIST-005** — zero unsafe code
30
31use core::hash::Hash;
32
33// ---------------------------------------------------------------------------
34// no_std sqrt via Newton-Raphson (8 iterations, accurate to ~1e-7 for [0, 1])
35// ---------------------------------------------------------------------------
36
37/// Compute the square root of a non-negative f32 using Newton-Raphson iteration.
38/// This is `no_std` compatible and avoids any platform intrinsics.
39fn sqrt_nr(x: f32) -> f32 {
40    if x <= 0.0 {
41        return 0.0;
42    }
43    // Initial guess using integer bit manipulation (fast inverse sqrt seed)
44    let bits = x.to_bits();
45    let guess_bits = 0x1fbd_1df5u32.wrapping_add(bits >> 1);
46    let mut s = f32::from_bits(guess_bits);
47    // Eight Newton-Raphson iterations: s = (s + x/s) / 2
48    for _ in 0..8 {
49        s = 0.5 * (s + x / s);
50    }
51    s
52}
53
54/// Platform-independent sensor vocabulary trait.
55///
56/// Implementors define the discrete sensory space the robot operates in.
57/// CCF is generic over this trait — the same trust accumulation logic
58/// works for any hardware as long as it can produce a discrete, hashable
59/// context key and a float feature vector.
60///
61/// The const generic `N` is the dimensionality of the feature vector.
62/// It must match `FEATURE_DIM` on the implementing type for the full CCF stack.
63///
64/// Patent Claims 1 and 8.
65pub trait SensorVocabulary<const N: usize>: Eq + Hash + Clone + core::fmt::Debug {
66    /// Dimensionality of the feature vector encoding (equal to the const generic `N`).
67    /// Provided as an associated constant for ergonomic access at the type level.
68    const FEATURE_DIM: usize = N;
69
70    /// Encode this vocabulary instance as a normalised float feature vector.
71    ///
72    /// Each element should be in [0.0, 1.0] for cosine similarity to be meaningful.
73    /// The order of dimensions must be consistent across calls.
74    fn to_feature_vec(&self) -> [f32; N];
75}
76
77/// Composite context key — generic over sensor vocabulary.
78///
79/// Wraps any `SensorVocabulary` implementation and adds:
80/// - Deterministic `context_hash_u32()` for HashMap keying
81/// - `cosine_similarity()` for graph edge weights
82///
83/// Patent Claims 1 and 8.
84#[derive(Clone, Debug, PartialEq, Eq, Hash)]
85pub struct ContextKey<V: SensorVocabulary<N>, const N: usize> {
86    /// The sensor vocabulary snapshot for this context.
87    pub vocabulary: V,
88}
89
90impl<V: SensorVocabulary<N>, const N: usize> ContextKey<V, N> {
91    /// Create a new context key from a sensor vocabulary snapshot.
92    pub fn new(vocabulary: V) -> Self {
93        Self { vocabulary }
94    }
95
96    /// Deterministic FNV-1a hash of the feature vector.
97    ///
98    /// Used to key context entries in fixed-size arrays (no_std compatible).
99    /// Deterministic: same vocabulary produces the same hash across restarts.
100    pub fn context_hash_u32(&self) -> u32 {
101        let vec = self.vocabulary.to_feature_vec();
102        let mut h: u32 = 2_166_136_261;
103        for &f in vec.iter() {
104            // Quantise to u16 for stable hashing of float feature vectors.
105            let bits: u16 = (f.clamp(0.0, 1.0) * 65535.0) as u16;
106            h ^= bits as u32;
107            h = h.wrapping_mul(16_777_619);
108        }
109        h
110    }
111
112    /// Cosine similarity between two context keys via their feature vectors.
113    ///
114    /// Returns a value in [0.0, 1.0] (assumes non-negative feature vectors).
115    /// Used as the raw edge weight in the World Shape graph (Graph A).
116    pub fn cosine_similarity(&self, other: &Self) -> f32 {
117        let a = self.vocabulary.to_feature_vec();
118        let b = other.vocabulary.to_feature_vec();
119
120        let dot: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
121        let sq_a: f32 = a.iter().map(|x| x * x).sum();
122        let sq_b: f32 = b.iter().map(|x| x * x).sum();
123        let norm_a: f32 = sqrt_nr(sq_a);
124        let norm_b: f32 = sqrt_nr(sq_b);
125
126        let epsilon: f32 = 1e-9;
127        let tiny_a: bool = norm_a < epsilon;
128        let tiny_b: bool = norm_b < epsilon;
129        if tiny_a || tiny_b {
130            0.0
131        } else {
132            let raw: f32 = dot / (norm_a * norm_b);
133            raw.clamp(0.0, 1.0)
134        }
135    }
136}
137
138#[cfg(test)]
139mod tests {
140    use super::*;
141
142    // Simple two-dimensional test vocabulary used throughout the unit tests.
143    // For a production 6-dimensional vocabulary see `ccf_core::mbot::MbotSensors`.
144    #[derive(Clone, Debug, PartialEq, Eq, Hash)]
145    struct TwoSensor { light: u8, noise: u8 } // 0=low, 1=mid, 2=high
146
147    impl SensorVocabulary<2> for TwoSensor {
148        fn to_feature_vec(&self) -> [f32; 2] {
149            [self.light as f32 / 2.0, self.noise as f32 / 2.0]
150        }
151    }
152
153    fn bright_quiet() -> ContextKey<TwoSensor, 2> {
154        ContextKey::new(TwoSensor { light: 2, noise: 0 })
155    }
156
157    fn dark_loud() -> ContextKey<TwoSensor, 2> {
158        ContextKey::new(TwoSensor { light: 0, noise: 2 })
159    }
160
161    #[test]
162    fn test_claim_1_context_key_is_deterministic() {
163        // Patent Claim 1: discrete context identifier from quantised sensor signals
164        let k1 = bright_quiet();
165        let k2 = bright_quiet();
166        assert_eq!(k1.context_hash_u32(), k2.context_hash_u32());
167    }
168
169    #[test]
170    fn test_claim_1_different_contexts_have_different_hashes() {
171        let k1 = bright_quiet();
172        let k2 = dark_loud();
173        assert_ne!(k1.context_hash_u32(), k2.context_hash_u32());
174    }
175
176    #[test]
177    fn test_claim_8_composite_sensor_context_key() {
178        // Patent Claim 8: composite sensor vocabulary trait
179        let k = bright_quiet();
180        let vec = k.vocabulary.to_feature_vec();
181        assert_eq!(vec.len(), TwoSensor::FEATURE_DIM);
182        // light=2 → 1.0, noise=0 → 0.0
183        assert!((vec[0] - 1.0_f32).abs() < 1e-6);
184        assert!((vec[1] - 0.0_f32).abs() < 1e-6);
185    }
186
187    #[test]
188    fn test_cosine_similarity_identical_contexts() {
189        let k = bright_quiet();
190        assert!((k.cosine_similarity(&k) - 1.0_f32).abs() < 1e-5);
191    }
192
193    #[test]
194    fn test_cosine_similarity_dissimilar_contexts() {
195        let k1 = bright_quiet();
196        let k2 = dark_loud();
197        let sim = k1.cosine_similarity(&k2);
198        // Bright+Quiet vs Dark+Loud should be low similarity
199        assert!(sim < 0.5_f32, "sim={}", sim);
200    }
201
202    #[test]
203    fn test_custom_vocabulary_works_without_modifying_ccf_core() {
204        // Acceptance criterion: custom vocabulary compiles without modifying ccf-core
205        #[derive(Clone, Debug, PartialEq, Eq, Hash)]
206        struct TwoSensor { a: u8, b: u8 }
207        impl SensorVocabulary<2> for TwoSensor {
208            fn to_feature_vec(&self) -> [f32; 2] {
209                [self.a as f32 / 255.0, self.b as f32 / 255.0]
210            }
211        }
212        let k = ContextKey::new(TwoSensor { a: 100, b: 200 });
213        let _hash = k.context_hash_u32(); // just needs to compile and not panic
214        let sim = k.cosine_similarity(&k);
215        assert!((sim - 1.0_f32).abs() < 1e-5, "self-similarity={}", sim);
216    }
217
218    #[test]
219    fn test_sqrt_nr_accuracy() {
220        // Verify our no_std sqrt helper is accurate enough for cosine similarity
221        let cases: &[(f32, f32)] = &[
222            (0.0, 0.0),
223            (1.0, 1.0),
224            (0.25, 0.5),
225            (0.5, 0.7071068),
226            (4.0, 2.0),
227        ];
228        for &(input, expected) in cases {
229            let got = sqrt_nr(input);
230            assert!(
231                (got - expected).abs() < 1e-5,
232                "sqrt_nr({}) = {}, expected {}",
233                input,
234                got,
235                expected
236            );
237        }
238    }
239}