Skip to main content

qualia_core_db/inference/conditioning/
compile.rs

1//! Immutable plan assembly and requirement disposition compilation.
2
3use super::authority::AuthorityView;
4use super::budget::ConditioningBudget;
5use super::capabilities::{BackendCapabilities, SupportLevel};
6use super::identity::plan_identity;
7use super::receipt::RequirementOutcome;
8use super::requirement::RequirementClass;
9use super::select::{select_evidence_into, EvidencePart};
10use super::spec::{ConditioningError, ConditioningSpec};
11use super::validate::validate_spec;
12
13/// Fixed scratch buffers provided by caller for zero-heap compilation.
14///
15/// `'a` is the data lifetime (profile/spec strings stored inside elements);
16/// `'s` is the caller's scratch-slice borrow. Keeping them separate lets the
17/// caller re-read the buffers after compilation — e.g. rendering the
18/// `selected_evidence` the compiler just filled — while the returned plan
19/// still borrows source data.
20pub struct CompileBuffers<'a, 's> {
21    pub outcomes: &'s mut [RequirementOutcome<'a>],
22    pub selected_evidence: &'s mut [EvidencePart<'a>],
23}
24
25/// Bounded summary of a compiled conditioning plan.
26#[derive(Debug, Clone, Copy)]
27pub struct CompiledPlanSummary<'a> {
28    pub plan_id: u64,
29    pub profile_id: &'a str,
30    pub objective: &'a str,
31    pub requirements_count: usize,
32    pub outcomes_count: usize,
33    pub evidence_count: usize,
34    pub budget: ConditioningBudget,
35}
36
37/// Compile a conditioning spec and authority view into caller-supplied fixed buffers.
38/// Adheres to Tier-1 zero allocation in hot paths.
39pub fn compile_into<'a, 's>(
40    spec: &ConditioningSpec<'a>,
41    authority: &AuthorityView<'a>,
42    capabilities: &BackendCapabilities,
43    available_evidence: &'a [EvidencePart<'a>],
44    buffers: &mut CompileBuffers<'a, 's>,
45) -> Result<CompiledPlanSummary<'a>, ConditioningError> {
46    // 1. Validate spec bounds, schema, references, and authority
47    validate_spec(spec, authority)?;
48
49    // 2. Select authorized evidence
50    let evidence_count =
51        select_evidence_into(available_evidence, authority, buffers.selected_evidence)?;
52
53    // 3. Process requirements and record explicit disposition
54    if buffers.outcomes.len() < spec.requirements.len() {
55        return Err(ConditioningError::OutputBufferFull);
56    }
57
58    let mut outcomes_count = 0;
59    for req in spec.requirements {
60        let outcome = match req.class {
61            RequirementClass::Enforced => {
62                // If grammar or validation is unsupported
63                if capabilities.output_grammar == SupportLevel::Unsupported {
64                    if req.required {
65                        return Err(ConditioningError::RequiredCapabilityMissing);
66                    } else {
67                        RequirementOutcome::degraded(req.id, "output_grammar unsupported")
68                    }
69                } else {
70                    RequirementOutcome::enforced(req.id)
71                }
72            }
73            RequirementClass::EvidenceObligation => {
74                if evidence_count == 0 {
75                    if req.required {
76                        RequirementOutcome::rejected(req.id, "no authorized evidence selected")
77                    } else {
78                        RequirementOutcome::degraded(req.id, "evidence absent")
79                    }
80                } else {
81                    RequirementOutcome::applied(req.id)
82                }
83            }
84            RequirementClass::Guidance => {
85                if capabilities.role_separation == SupportLevel::Unsupported {
86                    RequirementOutcome::degraded(req.id, "role separation unsupported; flattened")
87                } else {
88                    RequirementOutcome::applied(req.id)
89                }
90            }
91        };
92
93        buffers.outcomes[outcomes_count] = outcome;
94        outcomes_count += 1;
95    }
96
97    // 4. Compute canonical identity
98    let plan_id = plan_identity(
99        spec,
100        &buffers.selected_evidence[..evidence_count],
101        "native-target",
102    );
103
104    Ok(CompiledPlanSummary {
105        plan_id,
106        profile_id: spec.profile_id,
107        objective: spec.objective,
108        requirements_count: spec.requirements.len(),
109        outcomes_count,
110        evidence_count,
111        budget: spec.budget,
112    })
113}