Skip to main content

holix_rs/
lib.rs

1pub mod blackboard;
2pub mod hydrate;
3pub mod ingest;
4pub mod lattice_index;
5pub mod persist;
6pub mod prose;
7#[cfg(feature = "semantic")]
8pub mod semantic;
9pub mod store;
10
11use holix_core_verified::checked_span_end;
12pub use hydrate::{
13    hydrate_projection, hydrate_projection_with_source, BlobSource, FsBlobSource, HydrateError,
14    DEFAULT_MAX_HYDRATE_BYTES,
15};
16pub use ingest::ingest_bytes;
17#[cfg(feature = "sqlite")]
18pub use persist::{search_fts, FtsHit};
19use holix_core_verified::retrieval::RetrievalQueryCore;
20
21use crate::blackboard::{retrieve_context, Blackboard, RetrievalBundle};
22use crate::lattice_index::ConceptLatticeIndex;
23use crate::store::IndexStore;
24
25/// Unified Layer 2 index runtime combining the asset store, concept lattice, and discovery blackboard.
26#[derive(Debug, Default)]
27pub struct HolixRuntime {
28    pub store: IndexStore,
29    pub lattice: ConceptLatticeIndex,
30    pub blackboard: Blackboard,
31    next_proj_id: u32,
32}
33
34impl HolixRuntime {
35    /// Creates an empty `HolixRuntime` starting projection IDs at `1`.
36    pub fn new() -> Self {
37        Self {
38            store: IndexStore::new(),
39            lattice: ConceptLatticeIndex::new(),
40            blackboard: Blackboard::new(),
41            next_proj_id: 1,
42        }
43    }
44
45    /// Returns a mutable cursor for allocating sequential projection IDs during ingestion.
46    pub fn next_proj_id_mut(&mut self) -> &mut u32 {
47        &mut self.next_proj_id
48    }
49
50    /// Executes a context-lean, budget-bounded retrieval query against `self.store`.
51    pub fn query(&self, query: RetrievalQueryCore, max_results: usize) -> Option<RetrievalBundle> {
52        retrieve_context(&self.store, query, max_results)
53    }
54}
55
56/// Layer 2 runtime smoke boundary delegating span arithmetic to Layer 1.
57pub fn validate_runtime_span(offset: u32, len: u32, total: u32) -> bool {
58    matches!(checked_span_end(offset, len), Some(end) if end <= total)
59}
60
61#[cfg(kani)]
62#[kani::proof]
63fn verify_runtime_span_no_panic() {
64    let offset: u32 = kani::any();
65    let len: u32 = kani::any();
66    let total: u32 = kani::any();
67    let ok = validate_runtime_span(offset, len, total);
68    if ok {
69        assert!(len <= total);
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use super::{validate_runtime_span, HolixRuntime};
76    use crate::blackboard::BlackboardEntry;
77    use crate::ingest::ingest_markdown_or_code;
78    use crate::lattice_index::ConceptNode;
79    use holix_core_verified::coords::Modality;
80    use holix_core_verified::lattice::{EdgeRole, NodeRefCore};
81    use holix_core_verified::retrieval::{MetadataFilterCore, RetrievalQueryCore};
82
83    #[test]
84    fn smoke_validate_runtime_span() {
85        assert!(validate_runtime_span(0, 10, 10));
86        assert!(!validate_runtime_span(5, 10, 10));
87    }
88
89    #[test]
90    fn runs_end_to_end_index_ingest_lattice_and_blackboard_query() {
91        let mut rt = HolixRuntime::new();
92        let mut next_id = *rt.next_proj_id_mut();
93        let md_ids = ingest_markdown_or_code(
94            &mut rt.store,
95            1,
96            "doc.md".to_string(),
97            Modality::Text,
98            1,
99            "# Heading\nDetails",
100            &mut next_id,
101        )
102        .unwrap();
103        let code_ids = ingest_markdown_or_code(
104            &mut rt.store,
105            2,
106            "main.rs".to_string(),
107            Modality::Text,
108            2,
109            "fn main() {}",
110            &mut next_id,
111        )
112        .unwrap();
113        *rt.next_proj_id_mut() = next_id;
114
115        rt.lattice
116            .add_node(ConceptNode {
117                node_id: 42,
118                rank: 1,
119                label: "Entry".to_string(),
120            })
121            .unwrap();
122        rt.lattice
123            .add_upward_edge(
124                NodeRefCore::Projection {
125                    proj_id: md_ids[0],
126                    is_only_opaque: false,
127                },
128                42,
129                EdgeRole::Defines,
130            )
131            .unwrap();
132        rt.lattice
133            .add_upward_edge(
134                NodeRefCore::Projection {
135                    proj_id: code_ids[0],
136                    is_only_opaque: false,
137                },
138                42,
139                EdgeRole::Evidences,
140            )
141            .unwrap();
142
143        assert_eq!(
144            rt.lattice.find_cross_document_intersections(&rt.store),
145            vec![(md_ids[0], code_ids[0], 42)]
146        );
147
148        let bundle = rt
149            .query(
150                RetrievalQueryCore {
151                    filter: MetadataFilterCore {
152                        required_asset_id: None,
153                        required_modality: None,
154                        required_extractor: None,
155                    },
156                    requested_depth: 1,
157                    max_token_budget: 100,
158                    discovery_mode: true,
159                },
160                8,
161            )
162            .unwrap();
163        assert_eq!(bundle.summary.selected_count, 3);
164        assert!(!bundle.raw_hydration_allowed);
165
166        rt.blackboard.post(BlackboardEntry {
167            author: "coordinator".to_string(),
168            concept_node_id: Some(42),
169            asset_id: Some(2),
170            summary: "Cross-doc match verified".to_string(),
171            evidence_proj_ids: vec![md_ids[0], code_ids[0]],
172        });
173        assert_eq!(rt.blackboard.by_concept(42).len(), 1);
174    }
175}