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
//! Personal Fact Matcher
//!
//! Matches relevant personal facts to current context for injection into prompts.

use super::fact::{PersonalFact, PersonalFactCategory};
use std::collections::HashSet;

/// Matcher for selecting relevant personal facts for context injection
pub struct PersonalFactMatcher {
    /// Minimum confidence threshold for matching
    min_confidence: f32,
    /// Maximum number of facts to include
    max_facts: usize,
    /// Whether to include context facts (which are more transient)
    include_context: bool,
}

impl Default for PersonalFactMatcher {
    fn default() -> Self {
        Self {
            min_confidence: 0.5,
            max_facts: 15,
            include_context: true,
        }
    }
}

impl PersonalFactMatcher {
    /// Create a new matcher with custom settings
    pub fn new(min_confidence: f32, max_facts: usize, include_context: bool) -> Self {
        Self {
            min_confidence,
            max_facts,
            include_context,
        }
    }

    /// Get relevant facts for context injection
    ///
    /// Returns facts sorted by relevance (category priority + confidence)
    pub fn get_relevant_facts<'a>(
        &self,
        facts: impl Iterator<Item = &'a PersonalFact>,
        context: Option<&str>,
    ) -> Vec<&'a PersonalFact> {
        let mut relevant: Vec<&PersonalFact> = facts
            .filter(|f| {
                // Filter by confidence
                if f.decayed_confidence() < self.min_confidence {
                    return false;
                }

                // Optionally filter out context facts
                if !self.include_context && f.category == PersonalFactCategory::Context {
                    return false;
                }

                true
            })
            .collect();

        // Score and sort by relevance
        relevant.sort_by(|a, b| {
            let score_a = self.relevance_score(a, context);
            let score_b = self.relevance_score(b, context);
            score_b
                .partial_cmp(&score_a)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Limit to max facts
        relevant.truncate(self.max_facts);

        relevant
    }

    /// Calculate relevance score for a fact
    fn relevance_score(&self, fact: &PersonalFact, context: Option<&str>) -> f32 {
        let mut score = fact.decayed_confidence();

        // Boost by category priority
        score *= self.category_priority(&fact.category);

        // Boost by reinforcements (but capped)
        let reinforcement_boost = (fact.reinforcements.min(10) as f32) * 0.02;
        score += reinforcement_boost;

        // Boost if matches current context
        if let Some(ctx) = context
            && self.matches_context(fact, ctx)
        {
            score *= 1.3;
        }

        score
    }

    /// Get priority multiplier for category
    fn category_priority(&self, category: &PersonalFactCategory) -> f32 {
        match category {
            // Identity facts are most important for personalization
            PersonalFactCategory::Identity => 1.2,
            // Preferences directly affect assistant behavior
            PersonalFactCategory::Preference => 1.15,
            // Capabilities help adjust explanations
            PersonalFactCategory::Capability => 1.1,
            // Constraints are critical to respect
            PersonalFactCategory::Constraint => 1.2,
            // Context is valuable but transient
            PersonalFactCategory::Context => 1.0,
            // Relationships are supplementary
            PersonalFactCategory::Relationship => 0.9,
            // Ambiguity type preferences affect question quality
            PersonalFactCategory::AmbiguityTypePreference => 1.1,
        }
    }

    /// Check if a fact matches the current context
    fn matches_context(&self, fact: &PersonalFact, context: &str) -> bool {
        let context_lower = context.to_lowercase();

        // Check key matches
        if context_lower.contains(&fact.key.to_lowercase()) {
            return true;
        }

        // Check value matches
        if fact
            .value
            .to_lowercase()
            .split_whitespace()
            .any(|word| word.len() > 3 && context_lower.contains(word))
        {
            return true;
        }

        // Check fact context matches
        if let Some(ref fact_ctx) = fact.context
            && context_lower.contains(&fact_ctx.to_lowercase())
        {
            return true;
        }

        false
    }

    /// Format facts for context injection
    pub fn format_for_context(&self, facts: &[&PersonalFact]) -> String {
        if facts.is_empty() {
            return String::new();
        }

        let mut lines = Vec::new();
        lines.push("[User Profile]".to_string());

        // Group by category for cleaner output
        let mut by_category: std::collections::HashMap<PersonalFactCategory, Vec<&PersonalFact>> =
            std::collections::HashMap::new();

        for fact in facts {
            by_category.entry(fact.category).or_default().push(fact);
        }

        // Output in category order
        let category_order = [
            PersonalFactCategory::Identity,
            PersonalFactCategory::Preference,
            PersonalFactCategory::Capability,
            PersonalFactCategory::Context,
            PersonalFactCategory::Constraint,
            PersonalFactCategory::Relationship,
        ];

        for category in &category_order {
            if let Some(cat_facts) = by_category.get(category) {
                for fact in cat_facts {
                    lines.push(format!("- {}", fact.to_context_string()));
                }
            }
        }

        lines.join("\n")
    }

    /// Get a summary of the user's profile
    pub fn format_profile_summary(&self, facts: &[&PersonalFact]) -> String {
        if facts.is_empty() {
            return "No profile information available.".to_string();
        }

        let mut sections = Vec::new();

        // Find name
        if let Some(name_fact) = facts
            .iter()
            .find(|f| f.key == "name" || f.key == "preferred_name")
        {
            sections.push(format!("Name: {}", name_fact.value));
        }

        // Find role/organization
        let role = facts
            .iter()
            .find(|f| f.key == "role")
            .map(|f| f.value.as_str());
        let org = facts
            .iter()
            .find(|f| f.key == "organization")
            .map(|f| f.value.as_str());
        match (role, org) {
            (Some(r), Some(o)) => sections.push(format!("Role: {} at {}", r, o)),
            (Some(r), None) => sections.push(format!("Role: {}", r)),
            (None, Some(o)) => sections.push(format!("Organization: {}", o)),
            _ => {}
        }

        // Find current project
        if let Some(project) = facts.iter().find(|f| f.key == "current_project") {
            sections.push(format!("Current Project: {}", project.value));
        }

        // Count preferences and capabilities
        let pref_count = facts
            .iter()
            .filter(|f| f.category == PersonalFactCategory::Preference)
            .count();
        let cap_count = facts
            .iter()
            .filter(|f| f.category == PersonalFactCategory::Capability)
            .count();

        if pref_count > 0 {
            sections.push(format!("Preferences: {} recorded", pref_count));
        }
        if cap_count > 0 {
            sections.push(format!("Skills/Capabilities: {} recorded", cap_count));
        }

        if sections.is_empty() {
            return "Profile has some facts but no key information.".to_string();
        }

        sections.join("\n")
    }
}

/// Extract keywords from a message for context matching
pub fn extract_keywords(text: &str) -> HashSet<String> {
    let stopwords: HashSet<&str> = [
        "a",
        "an",
        "the",
        "is",
        "are",
        "was",
        "were",
        "be",
        "been",
        "being",
        "have",
        "has",
        "had",
        "do",
        "does",
        "did",
        "will",
        "would",
        "could",
        "should",
        "may",
        "might",
        "can",
        "to",
        "of",
        "in",
        "for",
        "on",
        "with",
        "at",
        "by",
        "from",
        "as",
        "into",
        "through",
        "during",
        "before",
        "after",
        "above",
        "below",
        "between",
        "under",
        "again",
        "further",
        "then",
        "once",
        "here",
        "there",
        "when",
        "where",
        "why",
        "how",
        "all",
        "each",
        "few",
        "more",
        "most",
        "other",
        "some",
        "such",
        "no",
        "nor",
        "not",
        "only",
        "own",
        "same",
        "so",
        "than",
        "too",
        "very",
        "just",
        "and",
        "but",
        "if",
        "or",
        "because",
        "until",
        "while",
        "of",
        "about",
        "against",
        "between",
        "into",
        "through",
        "during",
        "before",
        "after",
        "above",
        "below",
        "to",
        "from",
        "up",
        "down",
        "in",
        "out",
        "on",
        "off",
        "over",
        "under",
        "again",
        "i",
        "me",
        "my",
        "myself",
        "we",
        "our",
        "ours",
        "ourselves",
        "you",
        "your",
        "yours",
        "yourself",
        "yourselves",
        "he",
        "him",
        "his",
        "himself",
        "she",
        "her",
        "hers",
        "herself",
        "it",
        "its",
        "itself",
        "they",
        "them",
        "their",
        "theirs",
        "themselves",
        "what",
        "which",
        "who",
        "whom",
        "this",
        "that",
        "these",
        "those",
        "am",
        "is",
        "are",
        "was",
        "were",
        "be",
        "been",
        "being",
    ]
    .iter()
    .cloned()
    .collect();

    text.to_lowercase()
        .split(|c: char| !c.is_alphanumeric() && c != '_' && c != '-')
        .filter(|word| word.len() > 2 && !stopwords.contains(word))
        .map(String::from)
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::knowledge::bks_pks::personal::fact::PersonalFactSource;

    fn create_test_fact(category: PersonalFactCategory, key: &str, value: &str) -> PersonalFact {
        PersonalFact::new(
            category,
            key.to_string(),
            value.to_string(),
            None,
            PersonalFactSource::ExplicitStatement,
            false,
        )
    }

    #[test]
    fn test_matcher_creation() {
        let matcher = PersonalFactMatcher::default();
        assert_eq!(matcher.min_confidence, 0.5);
        assert_eq!(matcher.max_facts, 15);
    }

    #[test]
    fn test_get_relevant_facts() {
        let matcher = PersonalFactMatcher::default();

        let facts = vec![
            create_test_fact(PersonalFactCategory::Identity, "name", "John"),
            create_test_fact(PersonalFactCategory::Preference, "language", "Rust"),
            create_test_fact(PersonalFactCategory::Context, "project", "brainwires"),
        ];

        let relevant: Vec<_> = matcher
            .get_relevant_facts(facts.iter(), None)
            .into_iter()
            .collect();

        assert_eq!(relevant.len(), 3);
    }

    #[test]
    fn test_context_matching() {
        let matcher = PersonalFactMatcher::default();

        let rust_fact = create_test_fact(PersonalFactCategory::Capability, "language", "Rust");
        let python_fact = create_test_fact(PersonalFactCategory::Capability, "language", "Python");

        let facts = vec![rust_fact.clone(), python_fact.clone()];

        let relevant: Vec<_> = matcher
            .get_relevant_facts(facts.iter(), Some("working with Rust"))
            .into_iter()
            .collect();

        // Rust fact should come first due to context match
        assert!(!relevant.is_empty());
        assert_eq!(relevant[0].value, "Rust");
    }

    #[test]
    fn test_format_for_context() {
        let matcher = PersonalFactMatcher::default();

        let facts = vec![
            create_test_fact(PersonalFactCategory::Identity, "name", "John"),
            create_test_fact(PersonalFactCategory::Preference, "editor", "VSCode"),
        ];

        let refs: Vec<&PersonalFact> = facts.iter().collect();
        let formatted = matcher.format_for_context(&refs);

        assert!(formatted.contains("[User Profile]"));
        assert!(formatted.contains("name: John"));
        assert!(formatted.contains("editor: VSCode"));
    }

    #[test]
    fn test_extract_keywords() {
        let keywords = extract_keywords("I am working on a Rust project called brainwires");

        assert!(keywords.contains("working"));
        assert!(keywords.contains("rust"));
        assert!(keywords.contains("project"));
        assert!(keywords.contains("brainwires"));
        // Stopwords should be filtered
        assert!(!keywords.contains("i"));
        assert!(!keywords.contains("am"));
        assert!(!keywords.contains("on"));
        assert!(!keywords.contains("a"));
    }
}