hirn-engine 0.1.0

Engine for the hirn cognitive memory database
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//! Contradiction Gate — flags memories that contradict existing high-confidence knowledge.
//!
//! Uses LLM to evaluate whether a candidate memory contradicts existing
//! semantic records. Contradicting candidates are deferred for review.

use std::sync::Arc;

use hirn_core::HirnResult;
use hirn_core::embed::{ChatMessage, LlmOptions, LlmProvider};
use hirn_core::id::MemoryId;
use hirn_core::metadata::Metadata;
use hirn_core::types::EdgeRelation;
use hirn_storage::PhysicalStore;
use hirn_storage::store::VectorSearchOptions;

use crate::admission::{AdmissionController, AdmissionDecision, MemoryCandidate};
use crate::persistent_graph::PersistentGraph;

/// Checks candidates against existing semantic records for contradictions.
pub struct ContradictionGate {
    storage: Arc<dyn PhysicalStore>,
    llm: Arc<dyn LlmProvider>,
    /// Dataset containing semantic records.
    dataset: String,
    /// Minimum confidence of existing records to compare against.
    confidence_threshold: f32,
    /// Number of existing records to compare against.
    top_k: usize,
    /// Optional graph for creating `contradicts` edges.
    graph: Option<PersistentGraph>,
}

impl ContradictionGate {
    pub fn new(
        storage: Arc<dyn PhysicalStore>,
        llm: Arc<dyn LlmProvider>,
        dataset: impl Into<String>,
        confidence_threshold: f32,
        top_k: usize,
    ) -> Self {
        Self {
            storage,
            llm,
            dataset: dataset.into(),
            confidence_threshold,
            top_k,
            graph: None,
        }
    }

    /// Create with defaults: confidence ≥ 0.7, top 5 records.
    pub fn with_defaults(
        storage: Arc<dyn PhysicalStore>,
        llm: Arc<dyn LlmProvider>,
        dataset: impl Into<String>,
    ) -> Self {
        Self::new(storage, llm, dataset, 0.7, 5)
    }

    /// Attach a persistent graph for creating `contradicts` edges.
    pub fn with_graph(mut self, graph: PersistentGraph) -> Self {
        self.graph = Some(graph);
        self
    }

    /// Build the prompt for contradiction detection.
    fn build_prompt(candidate_content: &str, existing_facts: &[String]) -> Vec<ChatMessage> {
        let facts_block = existing_facts
            .iter()
            .enumerate()
            .map(|(i, f)| format!("{}. {f}", i + 1))
            .collect::<Vec<_>>()
            .join("\n");

        vec![
            ChatMessage {
                role: "system".into(),
                content: "You are a contradiction detector. Given a new statement and a list of \
                          existing facts, determine if the new statement contradicts any existing \
                          fact. Respond with ONLY 'CONTRADICTION: <fact_number>' if a \
                          contradiction exists, or 'NO_CONTRADICTION' if there is none. Be \
                          precise — a contradiction means the two statements cannot both be true."
                    .into(),
            },
            ChatMessage {
                role: "user".into(),
                content: format!(
                    "New statement: {candidate_content}\n\nExisting facts:\n{facts_block}"
                ),
            },
        ]
    }

    /// Parse the LLM response. Returns the 0-based index of the contradicting fact, if any.
    fn parse_response(response: &str) -> Option<usize> {
        let trimmed = response.trim().to_uppercase();
        if !trimmed.starts_with("CONTRADICTION") {
            return None;
        }
        // Try to extract the fact number after the colon (1-based → 0-based).
        trimmed
            .split(':')
            .nth(1)
            .and_then(|s| s.trim().parse::<usize>().ok())
            .map(|n| n.saturating_sub(1))
    }
}

#[async_trait::async_trait]
impl AdmissionController for ContradictionGate {
    fn name(&self) -> &str {
        "contradiction_gate"
    }

    async fn evaluate(&self, candidate: &MemoryCandidate) -> HirnResult<AdmissionDecision> {
        let embedding = match &candidate.embedding {
            Some(emb) => emb,
            None => {
                return Ok(AdmissionDecision::Accept {
                    importance_override: None,
                });
            }
        };

        let exists = self
            .storage
            .exists(&self.dataset)
            .await
            .map_err(hirn_core::HirnError::storage)?;
        if !exists {
            return Ok(AdmissionDecision::Accept {
                importance_override: None,
            });
        }

        // Find the most similar high-confidence semantic records.
        let options = VectorSearchOptions {
            query: embedding.clone(),
            column: "embedding".into(),
            limit: self.top_k,
            filter: Some(format!(
                "confidence >= {} AND (archived IS NULL OR archived = false)",
                self.confidence_threshold
            )),
            ..Default::default()
        };

        let batches = self
            .storage
            .vector_search(&self.dataset, options)
            .await
            .map_err(hirn_core::HirnError::storage)?;

        let existing_facts = extract_descriptions(&batches);
        let existing_ids = extract_ids(&batches);

        if existing_facts.is_empty() {
            return Ok(AdmissionDecision::Accept {
                importance_override: None,
            });
        }

        // Ask LLM about contradictions.
        let messages = Self::build_prompt(&candidate.content, &existing_facts);
        let llm_options = LlmOptions {
            temperature: 0.0,
            max_tokens: 64,
            ..Default::default()
        };

        let response = self.llm.generate_text(&messages, &llm_options).await?;
        let contradiction_idx = Self::parse_response(&response);

        if contradiction_idx.is_some() {
            // Create `contradicts` edge if graph is available and we know the target.
            if let (Some(graph), Some(idx)) = (&self.graph, contradiction_idx) {
                if let Some(target_id) = existing_ids.get(idx) {
                    // Best-effort edge creation — don't fail admission on edge error.
                    let _ = graph
                        .add_edge(
                            candidate.id,
                            *target_id,
                            EdgeRelation::Contradicts,
                            1.0,
                            Metadata::default(),
                        )
                        .await;
                }
            }

            // Defer for manual review / consolidation.
            let now = hirn_core::timestamp::Timestamp::now();
            Ok(AdmissionDecision::Defer {
                until: now.timestamp_ms() + 3_600_000, // 1 hour in ms
            })
        } else {
            Ok(AdmissionDecision::Accept {
                importance_override: None,
            })
        }
    }
}

/// Extract description strings from result batches.
fn extract_descriptions(batches: &[arrow_array::RecordBatch]) -> Vec<String> {
    use arrow_array::Array;
    let mut out = Vec::new();
    for batch in batches {
        if let Some(col) = batch.column_by_name("description") {
            if let Some(arr) = col.as_any().downcast_ref::<arrow_array::StringArray>() {
                for i in 0..arr.len() {
                    if !arr.is_null(i) {
                        out.push(arr.value(i).to_string());
                    }
                }
            }
            if let Some(arr) = col.as_any().downcast_ref::<arrow_array::LargeStringArray>() {
                for i in 0..arr.len() {
                    if !arr.is_null(i) {
                        out.push(arr.value(i).to_string());
                    }
                }
            }
        }
    }
    out
}

/// Extract memory IDs from result batches.
fn extract_ids(batches: &[arrow_array::RecordBatch]) -> Vec<MemoryId> {
    use arrow_array::Array;
    let mut out = Vec::new();
    for batch in batches {
        if let Some(col) = batch.column_by_name("id") {
            if let Some(arr) = col.as_any().downcast_ref::<arrow_array::StringArray>() {
                for i in 0..arr.len() {
                    if !arr.is_null(i) {
                        if let Ok(id) = MemoryId::parse(arr.value(i)) {
                            out.push(id);
                        }
                    }
                }
            }
            if let Some(arr) = col.as_any().downcast_ref::<arrow_array::LargeStringArray>() {
                for i in 0..arr.len() {
                    if !arr.is_null(i) {
                        if let Ok(id) = MemoryId::parse(arr.value(i)) {
                            out.push(id);
                        }
                    }
                }
            }
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use hirn_core::embed::{ChatMessage, LlmOptions};
    use hirn_core::id::MemoryId;
    use hirn_core::metadata::Metadata;
    use hirn_core::types::{AgentId, Namespace};
    use hirn_storage::{HirnDb, HirnDbConfig};
    use std::sync::atomic::{AtomicUsize, Ordering};

    fn candidate(content: &str, embedding: Vec<f32>) -> MemoryCandidate {
        MemoryCandidate {
            id: MemoryId::new(),
            content: content.into(),
            entities: vec![],
            embedding: Some(embedding),
            agent_id: AgentId::new("test").unwrap(),
            namespace: Namespace::shared(),
            importance: 0.5,
            surprise: 0.5,
            metadata: Metadata::default(),
        }
    }

    fn rand_vec(seed: u128) -> Vec<f32> {
        (0..32)
            .map(|i| (seed as f64 * 0.618_033 + i as f64 * 0.414_213).sin() as f32)
            .collect()
    }

    /// MockProvider that returns a configurable response.
    struct MockLlm {
        response: String,
        call_count: AtomicUsize,
    }

    impl MockLlm {
        fn new(response: &str) -> Self {
            Self {
                response: response.into(),
                call_count: AtomicUsize::new(0),
            }
        }

        fn calls(&self) -> usize {
            self.call_count.load(Ordering::SeqCst)
        }
    }

    #[async_trait::async_trait]
    impl LlmProvider for MockLlm {
        async fn generate_text(
            &self,
            _messages: &[ChatMessage],
            _options: &LlmOptions,
        ) -> HirnResult<String> {
            self.call_count.fetch_add(1, Ordering::SeqCst);
            Ok(self.response.clone())
        }

        fn model_id(&self) -> &str {
            "mock-llm"
        }
    }

    async fn temp_storage() -> (Arc<dyn PhysicalStore>, tempfile::TempDir) {
        let dir = tempfile::tempdir().unwrap();
        let lance_path = dir.path().join("lance");
        let config = HirnDbConfig::local(lance_path.to_str().unwrap());
        let backend = HirnDb::open(config.clone()).await.unwrap();
        (backend.store_arc(), dir)
    }

    async fn insert_semantic(
        storage: &Arc<dyn PhysicalStore>,
        description: &str,
        emb: Vec<f32>,
        confidence: f32,
    ) {
        let rec = hirn_core::semantic::SemanticRecord::builder()
            .concept("test-concept")
            .description(description)
            .embedding(emb)
            .confidence(confidence)
            .agent_id(AgentId::new("test").unwrap())
            .build()
            .unwrap();
        let batch =
            hirn_storage::datasets::semantic::to_batch(std::slice::from_ref(&rec), 32).unwrap();
        storage.append("semantic", batch).await.unwrap();
    }

    #[tokio::test]
    async fn no_embedding_accepts() {
        let (storage, _dir) = temp_storage().await;
        let llm: Arc<dyn LlmProvider> = Arc::new(MockLlm::new("NO_CONTRADICTION"));
        let gate = ContradictionGate::with_defaults(storage, llm, "semantic");
        let mut c = candidate("anything", rand_vec(1));
        c.embedding = None;
        let result = gate.evaluate(&c).await.unwrap();
        assert!(result.is_accept());
    }

    #[tokio::test]
    async fn empty_database_accepts() {
        let (storage, _dir) = temp_storage().await;
        let llm: Arc<dyn LlmProvider> = Arc::new(MockLlm::new("NO_CONTRADICTION"));
        let gate = ContradictionGate::with_defaults(storage, llm, "semantic");
        let result = gate
            .evaluate(&candidate("test", rand_vec(1)))
            .await
            .unwrap();
        assert!(result.is_accept());
    }

    #[tokio::test]
    async fn contradiction_detected_defers() {
        let (storage, _dir) = temp_storage().await;
        let emb = rand_vec(1);
        insert_semantic(&storage, "The sky is blue", emb.clone(), 0.9).await;

        let llm: Arc<dyn LlmProvider> = Arc::new(MockLlm::new("CONTRADICTION: 1"));
        let gate = ContradictionGate::with_defaults(storage, llm, "semantic");
        let result = gate
            .evaluate(&candidate("The sky is green", emb))
            .await
            .unwrap();
        assert!(matches!(result, AdmissionDecision::Defer { .. }));
    }

    #[tokio::test]
    async fn no_contradiction_accepts() {
        let (storage, _dir) = temp_storage().await;
        let emb = rand_vec(1);
        insert_semantic(&storage, "The sky is blue", emb.clone(), 0.9).await;

        let llm: Arc<dyn LlmProvider> = Arc::new(MockLlm::new("NO_CONTRADICTION"));
        let gate = ContradictionGate::with_defaults(storage, llm, "semantic");
        let result = gate
            .evaluate(&candidate("Water is wet", emb))
            .await
            .unwrap();
        assert!(result.is_accept());
    }

    #[tokio::test]
    async fn low_confidence_records_skipped() {
        let (storage, _dir) = temp_storage().await;
        let emb = rand_vec(1);
        // Insert with low confidence (below threshold).
        insert_semantic(&storage, "The sky is blue", emb.clone(), 0.3).await;

        let llm = Arc::new(MockLlm::new("CONTRADICTION: 1"));
        let gate = ContradictionGate::with_defaults(
            storage,
            llm.clone() as Arc<dyn LlmProvider>,
            "semantic",
        );
        let result = gate
            .evaluate(&candidate("The sky is green", emb))
            .await
            .unwrap();
        // LLM should not be called since no records meet the confidence threshold.
        assert!(result.is_accept());
        assert_eq!(llm.calls(), 0);
    }

    #[tokio::test]
    async fn causal_contradiction_flagged() {
        let (storage, _dir) = temp_storage().await;
        let emb = rand_vec(1);
        insert_semantic(&storage, "X causes Y", emb.clone(), 0.9).await;

        let llm: Arc<dyn LlmProvider> = Arc::new(MockLlm::new("CONTRADICTION: 1"));
        let gate = ContradictionGate::with_defaults(storage, llm, "semantic");
        let result = gate
            .evaluate(&candidate("X does not cause Y", emb))
            .await
            .unwrap();
        assert!(matches!(result, AdmissionDecision::Defer { .. }));
    }

    #[tokio::test]
    async fn contradiction_creates_edge_in_graph() {
        let (storage, _dir) = temp_storage().await;
        let emb = rand_vec(1);
        insert_semantic(&storage, "The sky is blue", emb.clone(), 0.9).await;

        let graph = PersistentGraph::open(Arc::clone(&storage)).await.unwrap();
        let llm: Arc<dyn LlmProvider> = Arc::new(MockLlm::new("CONTRADICTION: 1"));
        let gate = ContradictionGate::with_defaults(Arc::clone(&storage), llm, "semantic")
            .with_graph(graph);

        let c = candidate("The sky is green", emb);
        let candidate_id = c.id;
        let result = gate.evaluate(&c).await.unwrap();
        assert!(matches!(result, AdmissionDecision::Defer { .. }));

        // Verify a `Contradicts` edge was created.
        let graph = PersistentGraph::open(storage).await.unwrap();
        let edges = graph.get_edges_from(candidate_id).await.unwrap();
        assert_eq!(edges.len(), 1);
        assert_eq!(edges[0].relation, EdgeRelation::Contradicts);
    }

    #[test]
    fn prompt_format() {
        let messages = ContradictionGate::build_prompt(
            "The sky is green",
            &["The sky is blue".into(), "Water is wet".into()],
        );
        assert_eq!(messages.len(), 2);
        assert_eq!(messages[0].role, "system");
        assert!(messages[1].content.contains("The sky is green"));
        assert!(messages[1].content.contains("1. The sky is blue"));
        assert!(messages[1].content.contains("2. Water is wet"));
    }

    #[test]
    fn parse_contradiction_response() {
        assert_eq!(
            ContradictionGate::parse_response("CONTRADICTION: 1"),
            Some(0)
        );
        assert_eq!(
            ContradictionGate::parse_response("  contradiction: 2  "),
            Some(1)
        );
        assert_eq!(ContradictionGate::parse_response("NO_CONTRADICTION"), None);
        assert_eq!(
            ContradictionGate::parse_response("no contradiction found"),
            None
        );
    }
}