Skip to main content

qualia_core_db/inference/conditioning/
identity.rs

1//! Canonical content and prefix cache identity binding.
2
3use super::select::EvidencePart;
4use super::spec::ConditioningSpec;
5
6const FNV_OFFSET_BASIS: u64 = 0xcbf29ce484222325;
7const FNV_PRIME: u64 = 0x100000001b3;
8
9#[inline]
10fn fnv1a_step(hash: &mut u64, bytes: &[u8]) {
11    for &b in bytes {
12        *hash ^= b as u64;
13        *hash = hash.wrapping_mul(FNV_PRIME);
14    }
15}
16
17/// Compute canonical plan identity digest across spec, requirements, and selected evidence.
18pub fn plan_identity<'a>(
19    spec: &ConditioningSpec<'a>,
20    selected_evidence: &[EvidencePart<'a>],
21    target_name: &str,
22) -> u64 {
23    let mut h = FNV_OFFSET_BASIS;
24
25    fnv1a_step(&mut h, &spec.schema_version.to_le_bytes());
26    fnv1a_step(&mut h, spec.profile_id.as_bytes());
27    fnv1a_step(&mut h, spec.objective.as_bytes());
28    fnv1a_step(&mut h, target_name.as_bytes());
29
30    for r in spec.requirements {
31        fnv1a_step(&mut h, r.id.as_bytes());
32        fnv1a_step(&mut h, &[r.class as u8]);
33        fnv1a_step(&mut h, r.rule.as_bytes());
34        if let Some(v) = r.validator {
35            fnv1a_step(&mut h, v.as_bytes());
36        }
37    }
38
39    for e in selected_evidence {
40        fnv1a_step(&mut h, e.source_id.as_bytes());
41        fnv1a_step(&mut h, &e.scope.to_le_bytes());
42        fnv1a_step(&mut h, &[e.sensitivity]);
43        fnv1a_step(&mut h, e.content.as_bytes());
44    }
45
46    h
47}