Skip to main content

qualia_core_db/inference/conditioning/
select.rs

1//! Deterministic evidence selection and dependency closure.
2
3use super::authority::AuthorityView;
4use super::spec::ConditioningError;
5
6#[derive(Debug, Clone, Copy)]
7pub struct EvidencePart<'a> {
8    pub source_id: &'a str,
9    pub scope: u64,
10    pub sensitivity: u8,
11    pub content: &'a str,
12    pub qualifier: Option<&'a str>,
13}
14
15/// Select authorized evidence parts into a caller-supplied fixed-size output slice.
16/// Returns the number of selected parts. Zero heap allocation in hot paths.
17pub fn select_evidence_into<'a>(
18    available: &'a [EvidencePart<'a>],
19    authority: &AuthorityView<'a>,
20    out: &mut [EvidencePart<'a>],
21) -> Result<usize, ConditioningError> {
22    let mut count = 0;
23
24    for part in available {
25        // Enforce disclosure ceiling: skip any fact exceeding the principal's clearance
26        if part.sensitivity > authority.disclosure_ceiling {
27            continue;
28        }
29
30        // Enforce allowed graph scopes if non-empty
31        if !authority.allowed_graph_scopes.is_empty()
32            && !authority.allowed_graph_scopes.contains(&part.scope)
33        {
34            continue;
35        }
36
37        if count >= out.len() {
38            return Err(ConditioningError::OutputBufferFull);
39        }
40
41        out[count] = *part;
42        count += 1;
43    }
44
45    Ok(count)
46}