brainwires-cognition 0.8.0

Unified intelligence layer — knowledge graphs, adaptive prompting, RAG, spectral math, and code analysis for the Brainwires Agent Framework
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
//! Entity Types for Knowledge Graph
//!
//! Types for representing extracted entities and their relationships
//! in conversation memory. Used by the relationship graph and knowledge system.

use std::collections::{HashMap, HashSet};

// Re-export EntityType from core (canonical definition)
pub use brainwires_core::graph::EntityType;

/// A named entity extracted from conversation
#[derive(Debug, Clone)]
pub struct Entity {
    /// Display name of the entity.
    pub name: String,
    /// The kind of entity (file, function, type, etc.).
    pub entity_type: EntityType,
    /// Message IDs where this entity appears.
    pub message_ids: Vec<String>,
    /// Unix timestamp when first seen.
    pub first_seen: i64,
    /// Unix timestamp when last seen.
    pub last_seen: i64,
    /// Total number of mentions.
    pub mention_count: u32,
}

impl Entity {
    /// Create a new entity with its first mention.
    pub fn new(name: String, entity_type: EntityType, message_id: String, timestamp: i64) -> Self {
        Self {
            name,
            entity_type,
            message_ids: vec![message_id],
            first_seen: timestamp,
            last_seen: timestamp,
            mention_count: 1,
        }
    }

    /// Record an additional mention of this entity.
    pub fn add_mention(&mut self, message_id: String, timestamp: i64) {
        if !self.message_ids.contains(&message_id) {
            self.message_ids.push(message_id);
        }
        self.last_seen = timestamp.max(self.last_seen);
        self.mention_count += 1;
    }
}

/// Relationship between entities
#[derive(Debug, Clone)]
pub enum Relationship {
    /// One entity defines another.
    Defines {
        /// The defining entity.
        definer: String,
        /// The entity being defined.
        defined: String,
        /// Context of the definition.
        context: String,
    },
    /// One entity references another.
    References {
        /// Source entity.
        from: String,
        /// Target entity.
        to: String,
    },
    /// One entity modifies another.
    Modifies {
        /// The modifying entity.
        modifier: String,
        /// The modified entity.
        modified: String,
        /// Kind of modification.
        change_type: String,
    },
    /// One entity depends on another.
    DependsOn {
        /// The dependent entity.
        dependent: String,
        /// The dependency.
        dependency: String,
    },
    /// One entity contains another.
    Contains {
        /// The container entity.
        container: String,
        /// The contained entity.
        contained: String,
    },
    /// Two entities co-occur in a message.
    CoOccurs {
        /// First entity.
        entity_a: String,
        /// Second entity.
        entity_b: String,
        /// Message where co-occurrence was observed.
        message_id: String,
    },
}

/// Extraction result from a single message
#[derive(Debug, Clone)]
pub struct ExtractionResult {
    /// Extracted entities as (name, type) pairs.
    pub entities: Vec<(String, EntityType)>,
    /// Extracted relationships between entities.
    pub relationships: Vec<Relationship>,
}

// ── Memory poisoning detection ────────────────────────────────────────────────

/// Why two stored facts were flagged as a potential contradiction.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ContradictionKind {
    /// Two `Defines` relationships share the same definer + defined but have different contexts.
    ConflictingDefinition,
    /// Two `Modifies` relationships describe different change types for the same modifier + target.
    ConflictingModification,
}

/// A potential contradiction detected when inserting a new fact.
///
/// The store does **not** silently overwrite the existing entry; instead it
/// appends both relationships and records this event so callers can surface it
/// for human review.
#[derive(Debug, Clone)]
pub struct ContradictionEvent {
    /// What kind of contradiction was detected.
    pub kind: ContradictionKind,
    /// The entity key (e.g. `"file:main.rs"`) involved.
    pub subject: String,
    /// Context string from the previously stored relationship.
    pub existing_context: String,
    /// Context string from the newly inserted relationship.
    pub new_context: String,
}

// ── Entity store ──────────────────────────────────────────────────────────────

/// Entity store for tracking entities across a conversation
#[derive(Debug, Default)]
pub struct EntityStore {
    entities: HashMap<String, Entity>,
    relationships: Vec<Relationship>,
    /// Contradiction events accumulated since the last call to [`drain_contradictions`].
    contradictions: Vec<ContradictionEvent>,
}

impl EntityStore {
    /// Create a new empty entity store.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add an extraction result, recording entities and relationships.
    pub fn add_extraction(&mut self, result: ExtractionResult, message_id: &str, timestamp: i64) {
        for (name, entity_type) in result.entities {
            let key = format!("{}:{}", entity_type.as_str(), name);
            if let Some(entity) = self.entities.get_mut(&key) {
                entity.add_mention(message_id.to_string(), timestamp);
            } else {
                self.entities.insert(
                    key,
                    Entity::new(name, entity_type, message_id.to_string(), timestamp),
                );
            }
        }

        // Check for contradictions before appending each relationship.
        for new_rel in result.relationships {
            self.check_and_record_contradiction(&new_rel);
            self.relationships.push(new_rel);
        }
    }

    /// Inspect `new_rel` against previously stored relationships and record any
    /// contradiction events.  Both the existing and the new relationship are kept
    /// so no information is silently discarded.
    fn check_and_record_contradiction(&mut self, new_rel: &Relationship) {
        match new_rel {
            Relationship::Defines {
                definer,
                defined,
                context: new_ctx,
            } => {
                for existing in &self.relationships {
                    if let Relationship::Defines {
                        definer: ex_definer,
                        defined: ex_defined,
                        context: ex_ctx,
                    } = existing
                        && ex_definer == definer
                        && ex_defined == defined
                        && ex_ctx != new_ctx
                    {
                        self.contradictions.push(ContradictionEvent {
                            kind: ContradictionKind::ConflictingDefinition,
                            subject: format!("{}::{}", definer, defined),
                            existing_context: ex_ctx.clone(),
                            new_context: new_ctx.clone(),
                        });
                        break;
                    }
                }
            }
            Relationship::Modifies {
                modifier,
                modified,
                change_type: new_change,
            } => {
                for existing in &self.relationships {
                    if let Relationship::Modifies {
                        modifier: ex_modifier,
                        modified: ex_modified,
                        change_type: ex_change,
                    } = existing
                        && ex_modifier == modifier
                        && ex_modified == modified
                        && ex_change != new_change
                    {
                        self.contradictions.push(ContradictionEvent {
                            kind: ContradictionKind::ConflictingModification,
                            subject: format!("{}::{}", modifier, modified),
                            existing_context: ex_change.clone(),
                            new_context: new_change.clone(),
                        });
                        break;
                    }
                }
            }
            _ => {}
        }
    }

    /// Returns all contradiction events accumulated so far (for inspection /
    /// human review) without removing them.
    pub fn pending_contradictions(&self) -> &[ContradictionEvent] {
        &self.contradictions
    }

    /// Drains and returns all accumulated contradiction events, clearing the
    /// internal buffer.
    pub fn drain_contradictions(&mut self) -> Vec<ContradictionEvent> {
        std::mem::take(&mut self.contradictions)
    }

    /// Look up an entity by name and type.
    pub fn get(&self, name: &str, entity_type: &EntityType) -> Option<&Entity> {
        let key = format!("{}:{}", entity_type.as_str(), name);
        self.entities.get(&key)
    }

    /// Get all entities of a given type.
    pub fn get_by_type(&self, entity_type: &EntityType) -> Vec<&Entity> {
        self.entities
            .values()
            .filter(|e| &e.entity_type == entity_type)
            .collect()
    }

    /// Get the most-mentioned entities, up to `limit`.
    pub fn get_top_entities(&self, limit: usize) -> Vec<&Entity> {
        let mut entities: Vec<_> = self.entities.values().collect();
        entities.sort_by(|a, b| b.mention_count.cmp(&a.mention_count));
        entities.into_iter().take(limit).collect()
    }

    /// Get names of entities related to the given entity.
    pub fn get_related(&self, entity_name: &str) -> Vec<String> {
        let mut related = HashSet::new();
        for rel in &self.relationships {
            match rel {
                Relationship::CoOccurs {
                    entity_a, entity_b, ..
                } => {
                    if entity_a == entity_name {
                        related.insert(entity_b.clone());
                    } else if entity_b == entity_name {
                        related.insert(entity_a.clone());
                    }
                }
                Relationship::Contains {
                    container,
                    contained,
                } => {
                    if container == entity_name {
                        related.insert(contained.clone());
                    } else if contained == entity_name {
                        related.insert(container.clone());
                    }
                }
                Relationship::References { from, to } => {
                    if from == entity_name {
                        related.insert(to.clone());
                    } else if to == entity_name {
                        related.insert(from.clone());
                    }
                }
                Relationship::DependsOn {
                    dependent,
                    dependency,
                } => {
                    if dependent == entity_name {
                        related.insert(dependency.clone());
                    } else if dependency == entity_name {
                        related.insert(dependent.clone());
                    }
                }
                Relationship::Modifies {
                    modifier, modified, ..
                } => {
                    if modifier == entity_name {
                        related.insert(modified.clone());
                    } else if modified == entity_name {
                        related.insert(modifier.clone());
                    }
                }
                Relationship::Defines {
                    definer, defined, ..
                } => {
                    if definer == entity_name {
                        related.insert(defined.clone());
                    } else if defined == entity_name {
                        related.insert(definer.clone());
                    }
                }
            }
        }
        related.into_iter().collect()
    }

    /// Get all message IDs associated with an entity name.
    pub fn get_message_ids(&self, entity_name: &str) -> Vec<String> {
        self.entities
            .values()
            .filter(|e| e.name == entity_name)
            .flat_map(|e| e.message_ids.clone())
            .collect()
    }

    /// Iterate over all stored entities.
    pub fn all_entities(&self) -> impl Iterator<Item = &Entity> {
        self.entities.values()
    }

    /// Get all stored relationships.
    pub fn all_relationships(&self) -> &[Relationship] {
        &self.relationships
    }

    /// Get statistics about the entity store.
    pub fn stats(&self) -> EntityStoreStats {
        let mut by_type = HashMap::new();
        for entity in self.entities.values() {
            *by_type.entry(entity.entity_type.as_str()).or_insert(0) += 1;
        }
        EntityStoreStats {
            total_entities: self.entities.len(),
            total_relationships: self.relationships.len(),
            entities_by_type: by_type,
        }
    }
}

impl brainwires_core::graph::EntityStoreT for EntityStore {
    fn entity_names_by_type(&self, entity_type: &EntityType) -> Vec<String> {
        self.get_by_type(entity_type)
            .iter()
            .map(|e| e.name.clone())
            .collect()
    }

    fn top_entity_info(&self, limit: usize) -> Vec<(String, EntityType)> {
        self.get_top_entities(limit)
            .iter()
            .map(|e| (e.name.clone(), e.entity_type.clone()))
            .collect()
    }
}

/// Statistics about the entity store.
#[derive(Debug)]
pub struct EntityStoreStats {
    /// Total number of entities.
    pub total_entities: usize,
    /// Total number of relationships.
    pub total_relationships: usize,
    /// Entity counts grouped by type.
    pub entities_by_type: HashMap<&'static str, usize>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_entity_type_as_str() {
        assert_eq!(EntityType::File.as_str(), "file");
        assert_eq!(EntityType::Function.as_str(), "function");
    }

    #[test]
    fn test_entity_lifecycle() {
        let mut entity = Entity::new("main.rs".into(), EntityType::File, "msg-1".into(), 100);
        assert_eq!(entity.mention_count, 1);
        entity.add_mention("msg-2".into(), 200);
        assert_eq!(entity.mention_count, 2);
        assert_eq!(entity.last_seen, 200);
    }

    #[test]
    fn test_entity_store() {
        let mut store = EntityStore::new();
        let result = ExtractionResult {
            entities: vec![
                ("main.rs".into(), EntityType::File),
                ("process".into(), EntityType::Function),
            ],
            relationships: vec![],
        };
        store.add_extraction(result, "msg-1", 100);
        assert_eq!(store.stats().total_entities, 2);
    }

    // ── Memory poisoning detection ─────────────────────────────────────────

    #[test]
    fn test_no_contradiction_on_fresh_store() {
        let mut store = EntityStore::new();
        let result = ExtractionResult {
            entities: vec![],
            relationships: vec![Relationship::Defines {
                definer: "main".into(),
                defined: "return_type".into(),
                context: "returns i32".into(),
            }],
        };
        store.add_extraction(result, "msg-1", 100);
        assert!(store.pending_contradictions().is_empty());
    }

    #[test]
    fn test_contradicting_definitions_flagged() {
        let mut store = EntityStore::new();

        store.add_extraction(
            ExtractionResult {
                entities: vec![],
                relationships: vec![Relationship::Defines {
                    definer: "main".into(),
                    defined: "return_type".into(),
                    context: "returns i32".into(),
                }],
            },
            "msg-1",
            100,
        );

        // Same definer/defined, different context → contradiction
        store.add_extraction(
            ExtractionResult {
                entities: vec![],
                relationships: vec![Relationship::Defines {
                    definer: "main".into(),
                    defined: "return_type".into(),
                    context: "returns String".into(),
                }],
            },
            "msg-2",
            200,
        );

        let contradictions = store.pending_contradictions();
        assert_eq!(contradictions.len(), 1);
        assert_eq!(
            contradictions[0].kind,
            ContradictionKind::ConflictingDefinition
        );
        assert_eq!(contradictions[0].subject, "main::return_type");
        assert_eq!(contradictions[0].existing_context, "returns i32");
        assert_eq!(contradictions[0].new_context, "returns String");
    }

    #[test]
    fn test_identical_definitions_not_flagged() {
        let mut store = EntityStore::new();

        for msg_id in ["msg-1", "msg-2"] {
            store.add_extraction(
                ExtractionResult {
                    entities: vec![],
                    relationships: vec![Relationship::Defines {
                        definer: "Config".into(),
                        defined: "timeout".into(),
                        context: "30 seconds".into(),
                    }],
                },
                msg_id,
                100,
            );
        }

        assert!(store.pending_contradictions().is_empty());
    }

    #[test]
    fn test_contradicting_modifications_flagged() {
        let mut store = EntityStore::new();

        store.add_extraction(
            ExtractionResult {
                entities: vec![],
                relationships: vec![Relationship::Modifies {
                    modifier: "patch_v2".into(),
                    modified: "timeout".into(),
                    change_type: "increase".into(),
                }],
            },
            "msg-1",
            100,
        );

        store.add_extraction(
            ExtractionResult {
                entities: vec![],
                relationships: vec![Relationship::Modifies {
                    modifier: "patch_v2".into(),
                    modified: "timeout".into(),
                    change_type: "decrease".into(),
                }],
            },
            "msg-2",
            200,
        );

        let contradictions = store.pending_contradictions();
        assert_eq!(contradictions.len(), 1);
        assert_eq!(
            contradictions[0].kind,
            ContradictionKind::ConflictingModification
        );
    }

    #[test]
    fn test_drain_contradictions_clears_buffer() {
        let mut store = EntityStore::new();

        for ctx in ["returns i32", "returns String"] {
            store.add_extraction(
                ExtractionResult {
                    entities: vec![],
                    relationships: vec![Relationship::Defines {
                        definer: "main".into(),
                        defined: "return_type".into(),
                        context: ctx.into(),
                    }],
                },
                "msg-1",
                100,
            );
        }

        assert!(!store.pending_contradictions().is_empty());
        let drained = store.drain_contradictions();
        assert_eq!(drained.len(), 1);
        assert!(store.pending_contradictions().is_empty());
    }

    #[test]
    fn test_both_relationships_retained_after_contradiction() {
        let mut store = EntityStore::new();

        store.add_extraction(
            ExtractionResult {
                entities: vec![],
                relationships: vec![Relationship::Defines {
                    definer: "fn".into(),
                    defined: "x".into(),
                    context: "old".into(),
                }],
            },
            "msg-1",
            100,
        );

        store.add_extraction(
            ExtractionResult {
                entities: vec![],
                relationships: vec![Relationship::Defines {
                    definer: "fn".into(),
                    defined: "x".into(),
                    context: "new".into(),
                }],
            },
            "msg-2",
            200,
        );

        // Both relationships are kept — no silent overwrite
        assert_eq!(store.all_relationships().len(), 2);
        let event = &store.pending_contradictions()[0];
        assert_eq!(event.existing_context, "old");
        assert_eq!(event.new_context, "new");
    }
}