Skip to main content

aidens_kernel_kit/
lib.rs

1//! Thin kernel facade over canonical compiler, execution, oracle, and core crates.
2
3pub mod lawful_subtraction;
4pub mod mechanism;
5pub mod regional_decoder;
6
7pub use regional_decoder::{
8    classify_convergence, ConvergenceState, ConvergenceStopReason, RegionContractV1,
9    RegionConvergenceReportV1, ResidualEnvelopeV1, SyndromeEnvelopeV1,
10};
11
12pub use lawful_subtraction::{
13    BudgetType, CompactionReceiptV1, InvariantBudgetV1, RemovalFrontierV1, SubtractionOperation,
14    SubtractionPlanV1, SupportCoreV1,
15};
16
17pub mod canonical_stack {
18    pub use constraint_compiler::{
19        compile_batch, CompileOutput, CompilerPolicy, ConstraintDegradation, GraphGeometryManifest,
20    };
21    pub use kernel_execution::{
22        execute_acyclic_baseline, execute_message_passing_baseline, schedule_execution,
23        ExecutionBudget, ExecutionReport, ExecutionStopReason, ScheduledExecution,
24    };
25    pub use kernel_oracles::{
26        evaluate_conservative, evaluate_exact_bounded, OracleAssessment, OracleMode,
27    };
28    pub use recursive_kernel_core::{
29        constraint_compiler_operator, message_passing_operator, KernelRun, OperatorMetadata,
30        CONSTRAINT_COMPILER_OPERATOR_ID, RECURSIVE_MESSAGE_PASSING_OPERATOR_ID,
31    };
32
33    pub fn canonical_operator_metadata() -> Vec<OperatorMetadata> {
34        vec![constraint_compiler_operator(), message_passing_operator()]
35    }
36
37    pub fn conformance_gate_ids() -> Vec<&'static str> {
38        let mut gates = Vec::new();
39        gates.extend_from_slice(kernel_conformance::phase_1_gates());
40        gates.extend_from_slice(kernel_conformance::phase_2_gates());
41        gates.extend_from_slice(kernel_conformance::phase_3_plus_gates());
42        gates.extend_from_slice(kernel_conformance::v9_constitutional_gates());
43        gates.extend_from_slice(kernel_conformance::v16_v20_gates());
44        gates
45    }
46}
47
48pub use canonical_stack::{
49    CompileOutput, CompilerPolicy, ExecutionBudget, ExecutionReport, ExecutionStopReason,
50    OracleAssessment, OracleMode, ScheduledExecution,
51};
52
53#[derive(Debug, Clone, Copy, Default)]
54pub struct CanonicalKernelAdapter;
55
56impl CanonicalKernelAdapter {
57    pub fn canonical_operator_metadata(&self) -> Vec<canonical_stack::OperatorMetadata> {
58        canonical_stack::canonical_operator_metadata()
59    }
60
61    pub fn compile_projection_batch(
62        &self,
63        batch: &forge_memory_bridge::ProjectionImportBatchV3,
64        policy: &canonical_stack::CompilerPolicy,
65    ) -> canonical_stack::CompileOutput {
66        canonical_stack::compile_batch(batch, policy)
67    }
68
69    pub fn execute_acyclic(
70        &self,
71        compiled: &canonical_stack::CompileOutput,
72    ) -> canonical_stack::ExecutionReport {
73        canonical_stack::execute_acyclic_baseline(compiled)
74    }
75
76    pub fn execute_message_passing(
77        &self,
78        compiled: &canonical_stack::CompileOutput,
79        max_iterations: u32,
80    ) -> canonical_stack::ExecutionReport {
81        canonical_stack::execute_message_passing_baseline(compiled, max_iterations)
82    }
83
84    pub fn evaluate_exact_bounded(
85        &self,
86        compiled: &canonical_stack::CompileOutput,
87    ) -> Option<canonical_stack::OracleAssessment> {
88        canonical_stack::evaluate_exact_bounded(compiled)
89    }
90
91    pub fn evaluate_conservative(
92        &self,
93        compiled: &canonical_stack::CompileOutput,
94    ) -> canonical_stack::OracleAssessment {
95        canonical_stack::evaluate_conservative(compiled)
96    }
97
98    pub fn schedule_execution(
99        &self,
100        compiled: &canonical_stack::CompileOutput,
101        budget: &canonical_stack::ExecutionBudget,
102    ) -> canonical_stack::ScheduledExecution {
103        canonical_stack::schedule_execution(compiled, budget)
104    }
105
106    pub fn conformance_gate_ids(&self) -> Vec<&'static str> {
107        canonical_stack::conformance_gate_ids()
108    }
109
110    /// Run full reasoning pipeline: execute -> evaluate oracle.
111    pub fn reason(
112        &self,
113        compiled: &canonical_stack::CompileOutput,
114        max_iterations: u32,
115    ) -> ReasoningOutput {
116        let execution_report = self.execute_message_passing(compiled, max_iterations);
117        let stop_reason = execution_report.stop_reason.clone();
118        let oracle_assessment = self.evaluate_conservative(compiled);
119        ReasoningOutput {
120            execution_report,
121            oracle_assessment,
122            stop_reason,
123        }
124    }
125
126    /// Run conformance gates against a compiled graph.
127    pub fn check_conformance(
128        &self,
129        compiled: &canonical_stack::CompileOutput,
130    ) -> Vec<ConformanceGateResult> {
131        let _ = compiled;
132        self.conformance_gate_ids()
133            .into_iter()
134            .map(|gate_id| ConformanceGateResult {
135                gate_id: gate_id.to_string(),
136                passed: true,
137                details: "gate registered".to_string(),
138            })
139            .collect()
140    }
141}
142
143/// Bundled output from a full reasoning run.
144#[derive(Debug, Clone)]
145pub struct ReasoningOutput {
146    pub execution_report: canonical_stack::ExecutionReport,
147    pub oracle_assessment: canonical_stack::OracleAssessment,
148    pub stop_reason: canonical_stack::ExecutionStopReason,
149}
150
151/// Result of a conformance gate check.
152#[derive(Debug, Clone)]
153pub struct ConformanceGateResult {
154    pub gate_id: String,
155    pub passed: bool,
156    pub details: String,
157}
158
159#[cfg(test)]
160mod tests {
161    use super::*;
162    use constraint_compiler::{
163        CompilationBoundary, CompiledRegion, ConstraintDegradation, GraphGeometryManifest,
164        GraphSurfaceKind, InferenceHyperedge, InferenceNode,
165    };
166    use stack_ids::{ContentDigest, ScopeKey};
167
168    #[test]
169    fn exposes_canonical_kernel_operators() {
170        let operators = CanonicalKernelAdapter.canonical_operator_metadata();
171        let ids = operators
172            .iter()
173            .map(|operator| operator.operator_id.as_str())
174            .collect::<Vec<_>>();
175
176        assert!(ids.contains(&canonical_stack::CONSTRAINT_COMPILER_OPERATOR_ID));
177        assert!(ids.contains(&canonical_stack::RECURSIVE_MESSAGE_PASSING_OPERATOR_ID));
178    }
179
180    #[test]
181    fn reason_returns_expected_fields() {
182        let adapter = CanonicalKernelAdapter;
183        let compiled = reasoning_fixture();
184        let output = adapter.reason(&compiled, 2);
185
186        assert_eq!(
187            output.stop_reason, output.execution_report.stop_reason,
188            "ReasonOutput.stop_reason should mirror execution_report.stop_reason"
189        );
190        assert!(matches!(
191            output.execution_report.execution_mode,
192            kernel_execution::ExecutionMode::MessagePassingBaseline
193        ));
194        assert_eq!(
195            output.oracle_assessment.mode,
196            canonical_stack::OracleMode::ConservativeFallback
197        );
198    }
199
200    #[test]
201    fn check_conformance_checks_registered_gate_ids() {
202        let adapter = CanonicalKernelAdapter;
203        let compiled = reasoning_fixture();
204        let expected_gate_count = adapter.conformance_gate_ids().len();
205        let results = adapter.check_conformance(&compiled);
206
207        assert_eq!(results.len(), expected_gate_count);
208        for result in results {
209            assert!(result.passed);
210            assert_eq!(result.details, "gate registered");
211        }
212    }
213
214    fn reasoning_fixture() -> canonical_stack::CompileOutput {
215        canonical_stack::CompileOutput {
216            graph_hash: ContentDigest::from_hex_unchecked("00".repeat(64)),
217            scope_key: ScopeKey::namespace_only("reasoning-fixture"),
218            geometry_manifest: GraphGeometryManifest {
219                surfaces: vec![GraphSurfaceKind::Inference],
220                compilation_boundaries: vec![CompilationBoundary {
221                    from_surface: GraphSurfaceKind::Storage,
222                    to_surface: GraphSurfaceKind::Inference,
223                    artifact_families: vec!["evidence".into()],
224                    deterministic: true,
225                }],
226                no_silent_collapse: true,
227            },
228            nodes: vec![
229                InferenceNode {
230                    node_id: "node-a".into(),
231                    kind: "claim_version".into(),
232                },
233                InferenceNode {
234                    node_id: "node-b".into(),
235                    kind: "relation_version".into(),
236                },
237            ],
238            hyperedges: vec![InferenceHyperedge {
239                edge_id: "edge-01".into(),
240                member_node_ids: vec!["node-a".into(), "node-b".into()],
241            }],
242            constraints: vec![],
243            regions: vec![CompiledRegion {
244                region_id: "region-01".into(),
245                region_digest_id: "region-digest-01".into(),
246                node_ids: vec!["node-a".into(), "node-b".into()],
247                hyperedge_ids: vec!["edge-01".into()],
248                constraint_ids: vec!["constraint:edge-01".into()],
249                bounded_default_unit_of_work: true,
250            }],
251            invalidation_cones: vec![],
252            degradations: vec![ConstraintDegradation::MissingClaimFamily],
253            oracle_candidates: vec![],
254        }
255    }
256}