fornix 0.3.2

Knowledge storage, retrieval, and graph infrastructure for cognitive systems
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! GraphRAG search modes: Local, Global, and Hybrid.
//!
//! All three implement [`GraphRagSearch`], the common search trait.
//! The underlying graph and community data are injected at construction.

use crate::graph::{
    adapter::{CausalOptions, EntitySearchOptions, GraphAdapter},
    types::{CausalPath, Community, Entity},
};
use crate::graphrag::{
    config::GraphRagConfig,
    error::{Error, Result},
    types::{SearchContext, SearchResult},
};

/// Common search interface shared by Local, Global, and Hybrid.
pub trait GraphRagSearch: Send + Sync {
    fn name(&self) -> &'static str;

    /// Execute a search and return a [`SearchResult`].
    fn search(&self, query: &str) -> Result<SearchResult>;
}

// ─────────────────────────────────────────────────────────────────
// Causal intent detection (lightweight heuristic)
// ─────────────────────────────────────────────────────────────────

/// Terms that suggest the user wants causal information.
const CAUSAL_SIGNALS: &[&str] = &[
    "cause", "caused", "causes", "why", "because", "lead", "leads", "result",
    "resulted", "effect", "affect", "affects", "trigger", "triggers", "prevent",
    "prevents", "enable", "enables", "require", "requires",
];

const ANCESTOR_SIGNALS: &[&str] = &["why", "because", "cause", "caused", "what led", "root"];

/// Returns `true` when the query likely intends causal traversal.
pub fn has_causal_intent(query: &str) -> bool {
    let lower = query.to_lowercase();
    CAUSAL_SIGNALS.iter().any(|s| lower.contains(s))
}

/// Returns `true` when the causal traversal should go backwards (ancestors).
pub fn wants_ancestors(query: &str) -> bool {
    let lower = query.to_lowercase();
    ANCESTOR_SIGNALS.iter().any(|s| lower.contains(s))
}

// ─────────────────────────────────────────────────────────────────
// Local search
// ─────────────────────────────────────────────────────────────────

/// Seed-entity-based neighbourhood search.
///
/// 1. Resolves a seed entity from the query by exact name, fuzzy name, or
///    token match.
/// 2. Expands to depth-N neighbours.
/// 3. Optionally follows causal paths when the query implies causation.
pub struct LocalSearch<G: GraphAdapter> {
    graph: G,
    config: GraphRagConfig,
}

impl<G: GraphAdapter> LocalSearch<G> {
    pub fn new(graph: G, config: GraphRagConfig) -> Self {
        Self { graph, config }
    }

    async fn resolve_seed(&self, query: &str) -> Result<Option<Entity>> {
        // 1. Exact name match
        if let Ok(Some(e)) = self.graph.find_entity_by_name(query, None).await {
            return Ok(Some(e));
        }
        // 2. Type-agnostic search (limit 1)
        let opts = EntitySearchOptions::new().with_query(query).with_limit(1);
        if let Ok(results) = self.graph.search_entities(opts, None).await
            && let Some(e) = results.into_iter().next() {
                return Ok(Some(e));
            }
        // 3. Token-level fallback — try individual meaningful tokens
        let tokens: Vec<&str> = query
            .split_whitespace()
            .filter(|t| t.len() >= 3)
            .collect();
        for token in &tokens {
            let opts = EntitySearchOptions::new().with_query(*token).with_limit(5);
            if let Ok(results) = self.graph.search_entities(opts, None).await
                && let Some(e) = results.into_iter().next() {
                    return Ok(Some(e));
                }
        }
        Ok(None)
    }

    pub async fn search_async(&self, query: &str) -> Result<SearchResult> {
        let seed = self.resolve_seed(query).await?;
        let seed = match seed {
            Some(e) => e,
            None => return Ok(SearchResult::empty()),
        };

        // Neighbour traversal
        let neighbours = self
            .graph
            .neighbors(seed.id, self.config.local_search_depth, Default::default(), None)
            .await
            .unwrap_or_default();

        // Causal paths when the query implies causation
        let causal_paths: Vec<CausalPath> = if self.config.causal_extraction_enabled
            && has_causal_intent(query)
        {
            let opts = CausalOptions {
                max_depth: self.config.causal_max_depth,
                ..Default::default()
            };
            if wants_ancestors(query) {
                self.graph
                    .causal_ancestors(seed.id, opts, None)
                    .await
                    .unwrap_or_default()
            } else {
                self.graph
                    .causal_descendants(seed.id, opts, None)
                    .await
                    .unwrap_or_default()
            }
        } else {
            Vec::new()
        };

        // Build contexts from neighbours
        let contexts: Vec<SearchContext> = neighbours
            .iter()
            .map(|entity| SearchContext {
                entity: Some(entity.clone()),
                relations: Vec::new(),
                text: None,
                score: entity.confidence.overall,
                metadata: Default::default(),
            })
            .collect();

        let mut all_entities = vec![seed];
        all_entities.extend(neighbours);

        let (avg_confidence, min_confidence) =
            SearchResult::compute_confidence_metrics(&all_entities);

        Ok(SearchResult {
            entities: all_entities,
            contexts,
            communities: Vec::new(),
            paths: causal_paths,
            provenance: Vec::new(),
            answer: None,
            avg_confidence,
            min_confidence,
        })
    }
}

impl<G: GraphAdapter + 'static> GraphRagSearch for LocalSearch<G> {
    fn name(&self) -> &'static str { "local" }

    fn search(&self, _query: &str) -> Result<SearchResult> {
        // Synchronous shim — callers with a Tokio runtime should use search_async.
        Err(Error::search("LocalSearch requires an async runtime; use search_async"))
    }
}

// ─────────────────────────────────────────────────────────────────
// Global search
// ─────────────────────────────────────────────────────────────────

/// Community-summary-based global search.
///
/// Ranks community summaries by lexical overlap with the query and
/// returns the top-N contexts. An optional LLM can synthesise a final
/// answer from the summaries; without one the raw summaries are returned.
pub struct GlobalSearch {
    communities: Vec<Community>,
    /// community key → pre-generated summary text
    summaries: std::collections::HashMap<String, String>,
    config: GraphRagConfig,
}

impl GlobalSearch {
    pub fn new(
        communities: Vec<Community>,
        summaries: std::collections::HashMap<String, String>,
        config: GraphRagConfig,
    ) -> Self {
        Self { communities, summaries, config }
    }

    /// Lexical overlap score: fraction of query tokens found in the summary.
    fn relevance_score(query: &str, summary: &str) -> f32 {
        let query_tokens: Vec<&str> = query.split_whitespace().collect();
        if query_tokens.is_empty() { return 0.0; }
        let lower_summary = summary.to_lowercase();
        let matched = query_tokens
            .iter()
            .filter(|t| lower_summary.contains(&t.to_lowercase()))
            .count();
        matched as f32 / query_tokens.len() as f32
    }

    fn community_key(community: &Community) -> String {
        let mut ids: Vec<u64> = community.entities.iter().map(|e| e.id).collect();
        ids.sort_unstable();
        ids.iter().map(|id| id.to_string()).collect::<Vec<_>>().join(":")
    }

    pub fn search_communities(&self, query: &str) -> SearchResult {
        let limit = self.config.max_community_summaries;

        let mut scored: Vec<(&Community, f32, String)> = self
            .communities
            .iter()
            .filter_map(|community| {
                let key = Self::community_key(community);
                let summary = self.summaries.get(&key)?;
                let score = Self::relevance_score(query, summary);
                Some((community, score, summary.clone()))
            })
            .collect();

        scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        scored.truncate(limit);

        let contexts: Vec<SearchContext> = scored
            .iter()
            .map(|(_, score, summary)| SearchContext {
                entity: None,
                relations: Vec::new(),
                text: Some(summary.clone()),
                score: Some(*score),
                metadata: Default::default(),
            })
            .collect();

        let communities: Vec<Community> = scored
            .into_iter()
            .map(|(c, _, _)| c.clone())
            .collect();

        SearchResult {
            entities: Vec::new(),
            contexts,
            communities,
            paths: Vec::new(),
            provenance: Vec::new(),
            answer: None,
            avg_confidence: None,
            min_confidence: None,
        }
    }
}

impl GraphRagSearch for GlobalSearch {
    fn name(&self) -> &'static str { "global" }

    fn search(&self, query: &str) -> Result<SearchResult> {
        Ok(self.search_communities(query))
    }
}

// ─────────────────────────────────────────────────────────────────
// Hybrid search
// ─────────────────────────────────────────────────────────────────

/// Combines local (entity-seeded) and global (community) results.
pub struct HybridSearch<L: GraphRagSearch, G: GraphRagSearch> {
    local: L,
    global: G,
}

impl<L: GraphRagSearch, G: GraphRagSearch> HybridSearch<L, G> {
    pub fn new(local: L, global: G) -> Self {
        Self { local, global }
    }
}

impl<L: GraphRagSearch, G: GraphRagSearch> GraphRagSearch for HybridSearch<L, G> {
    fn name(&self) -> &'static str { "hybrid" }

    fn search(&self, query: &str) -> Result<SearchResult> {
        let local_result = self.local.search(query)?;
        let global_result = self.global.search(query)?;

        let mut contexts = local_result.contexts;
        contexts.extend(global_result.contexts);

        Ok(SearchResult {
            entities: local_result.entities,
            contexts,
            communities: global_result.communities,
            paths: local_result.paths,
            provenance: local_result.provenance,
            answer: global_result.answer,
            avg_confidence: local_result.avg_confidence,
            min_confidence: local_result.min_confidence,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use crate::graph::types::Community;

    // ── Causal intent detection ──

    #[test]
    fn causal_intent_detected_in_why_query() {
        assert!(has_causal_intent("why did the market crash?"));
    }

    #[test]
    fn causal_intent_detected_in_causes_query() {
        assert!(has_causal_intent("what causes inflation?"));
    }

    #[test]
    fn no_causal_intent_in_factual_query() {
        assert!(!has_causal_intent("what is the capital of France?"));
    }

    #[test]
    fn ancestor_intent_detected() {
        assert!(wants_ancestors("why did this happen?"));
    }

    #[test]
    fn descendant_intent_not_ancestor() {
        assert!(!wants_ancestors("what does this lead to?"));
    }

    // ── GlobalSearch ──

    fn community(ids: &[u64]) -> Community {
        use crate::graph::types::{AssertionState, ConfidenceScores, Entity};
        let entities = ids.iter().map(|&id| Entity {
            id,
            name: format!("e{}", id),
            entity_type: "T".to_string(),
            properties: Default::default(),
            valid_from: None,
            valid_to: None,
            system_from: None,
            system_to: None,
            superseded_by: None,
            assertion_state: AssertionState::Active,
            confidence: ConfidenceScores::default(),
        }).collect();
        Community { entities, density: 0.5, central_entity: None }
    }

    fn global_search() -> GlobalSearch {
        let c1 = community(&[1, 2]);
        let c2 = community(&[3, 4]);
        let key1 = GlobalSearch::community_key(&c1);
        let key2 = GlobalSearch::community_key(&c2);
        let mut summaries = HashMap::new();
        summaries.insert(key1, "Rust programming language systems".to_string());
        summaries.insert(key2, "Python scripting data science".to_string());
        GlobalSearch::new(vec![c1, c2], summaries, GraphRagConfig::default())
    }

    #[test]
    fn global_search_returns_relevant_community_first() {
        let gs = global_search();
        let result = gs.search("rust systems").unwrap();
        assert!(!result.communities.is_empty());
        // Rust-related community should score higher
        let top_score = result.contexts[0].score.unwrap_or(0.0);
        assert!(top_score > 0.0);
    }

    #[test]
    fn global_search_empty_query_returns_result() {
        let gs = global_search();
        // Empty query matches nothing, but shouldn't error
        let result = gs.search("").unwrap();
        // Scores should be 0 for empty query
        assert!(result.contexts.iter().all(|c| c.score.unwrap_or(0.0) == 0.0));
    }

    #[test]
    fn relevance_score_full_match_is_one() {
        let score = GlobalSearch::relevance_score("rust systems", "rust systems programming");
        assert!((score - 1.0).abs() < 1e-6);
    }

    #[test]
    fn relevance_score_no_match_is_zero() {
        let score = GlobalSearch::relevance_score("haskell functional", "python data science");
        assert_eq!(score, 0.0);
    }

    #[test]
    fn relevance_score_partial_match() {
        let score = GlobalSearch::relevance_score("rust python", "rust programming");
        assert!((score - 0.5).abs() < 1e-6);
    }

    #[test]
    fn global_search_respects_max_community_summaries_limit() {
        let config = GraphRagConfig { max_community_summaries: 1, ..Default::default() };
        let c1 = community(&[1, 2]);
        let c2 = community(&[3, 4]);
        let key1 = GlobalSearch::community_key(&c1);
        let key2 = GlobalSearch::community_key(&c2);
        let mut summaries = HashMap::new();
        summaries.insert(key1, "alpha".to_string());
        summaries.insert(key2, "beta".to_string());
        let gs = GlobalSearch::new(vec![c1, c2], summaries, config);
        let result = gs.search("alpha").unwrap();
        assert!(result.contexts.len() <= 1);
    }
}