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
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
//! Watch subscriptions for real-time reactive memory.
//!
//! Builds on the existing `EventLog` broadcast channel by adding
//! filter-based subscriptions. Subscribers receive only events
//! matching their `WatchFilter`.

use hirn_core::error::{HirnError, HirnResult};
use hirn_core::types::{Layer, Namespace};
use tokio::sync::broadcast;

use crate::event::{EventEnvelope, MemoryEvent};

// ═══════════════════════════════════════════════════════════════════════════
// Watch Filter
// ═══════════════════════════════════════════════════════════════════════════

/// Filter criteria for watch subscriptions.
#[derive(Debug, Clone, PartialEq)]
pub enum WatchFilter {
    /// Receive all events.
    All,
    /// Events from a specific realm.
    Realm(String),
    /// Events affecting any of the provided memory layers.
    Layers(Vec<Layer>),
    /// Events from a specific namespace.
    Namespace(String),
    /// Events from any of the provided namespaces.
    Namespaces(Vec<String>),
    /// Events triggered by a specific agent.
    AgentId(String),
    /// Events mentioning any of these entities (checked in content previews).
    Entities(Vec<String>),
    /// Only events with importance updates above this threshold.
    ImportanceAbove(f32),
    /// Only contradiction-related events.
    Contradictions,
    /// Only specific event types.
    EventTypes(Vec<String>),
    /// All child filters must match.
    AllOf(Vec<WatchFilter>),
}

impl WatchFilter {
    /// Combine filters conjunctively, flattening nested `AllOf` nodes.
    #[must_use]
    pub fn all_of(filters: Vec<Self>) -> Self {
        let mut flattened = Vec::new();
        for filter in filters {
            match filter {
                Self::All => {}
                Self::AllOf(children) => flattened.extend(children),
                other => flattened.push(other),
            }
        }

        match flattened.len() {
            0 => Self::All,
            1 => flattened.into_iter().next().unwrap_or(Self::All),
            _ => Self::AllOf(flattened),
        }
    }

    /// Restrict this filter to a namespace set.
    #[must_use]
    pub fn scoped_to_namespaces(self, allowed_namespaces: &[Namespace]) -> Self {
        let namespaces = allowed_namespaces
            .iter()
            .map(|namespace| namespace.as_str().to_string())
            .collect();
        Self::all_of(vec![Self::Namespaces(namespaces), self])
    }

    /// Reject filters that explicitly name namespaces outside the allowed set.
    pub fn validate_allowed_namespaces(&self, allowed_namespaces: &[Namespace]) -> HirnResult<()> {
        let mut referenced_namespaces = Vec::new();
        self.collect_referenced_namespaces(&mut referenced_namespaces);

        for namespace in referenced_namespaces {
            let allowed = allowed_namespaces
                .iter()
                .any(|allowed_namespace| allowed_namespace.as_str() == namespace);
            if !allowed {
                return Err(HirnError::AccessDenied(format!(
                    "watch cannot access namespace '{}'",
                    namespace
                )));
            }
        }

        Ok(())
    }

    fn collect_referenced_namespaces(&self, namespaces: &mut Vec<String>) {
        match self {
            Self::Namespace(namespace) => namespaces.push(namespace.clone()),
            Self::Namespaces(items) => namespaces.extend(items.iter().cloned()),
            Self::AllOf(filters) => {
                for filter in filters {
                    filter.collect_referenced_namespaces(namespaces);
                }
            }
            Self::All
            | Self::Realm(_)
            | Self::Layers(_)
            | Self::AgentId(_)
            | Self::Entities(_)
            | Self::ImportanceAbove(_)
            | Self::Contradictions
            | Self::EventTypes(_) => {}
        }
    }

    /// Check whether an event envelope matches this filter.
    pub fn matches(&self, envelope: &EventEnvelope) -> bool {
        match self {
            WatchFilter::All => true,
            WatchFilter::Realm(realm) => envelope.realm == *realm,
            WatchFilter::Layers(layers) => envelope
                .event
                .layer()
                .is_some_and(|layer| layers.contains(&layer)),
            WatchFilter::Namespace(ns) => envelope.namespace == *ns,
            WatchFilter::Namespaces(namespaces) => namespaces.contains(&envelope.namespace),
            WatchFilter::AgentId(agent_id) => envelope.agent_id == *agent_id,
            WatchFilter::Entities(entities) => {
                let text = match &envelope.event {
                    MemoryEvent::EpisodeCreated {
                        content_preview, ..
                    } => content_preview.as_str(),
                    MemoryEvent::SemanticCreated { concept_name, .. } => concept_name.as_str(),
                    MemoryEvent::ProceduralCreated { procedure_name, .. } => {
                        procedure_name.as_str()
                    }
                    MemoryEvent::Reconsolidated { reason, .. } => reason.as_str(),
                    _ => "",
                };
                let lower = text.to_lowercase();
                entities.iter().any(|e| lower.contains(&e.to_lowercase()))
            }
            WatchFilter::ImportanceAbove(threshold) => {
                matches!(
                    &envelope.event,
                    MemoryEvent::ImportanceUpdated { new_value, .. }
                        if *new_value > *threshold
                )
            }
            WatchFilter::Contradictions => match &envelope.event {
                MemoryEvent::ContradictionDetected { .. } => true,
                MemoryEvent::Reconsolidated { reason, .. } => reason.contains("contradict"),
                _ => false,
            },
            WatchFilter::EventTypes(types) => {
                let event_type = envelope.event.event_type();
                types.iter().any(|t| t == event_type)
            }
            WatchFilter::AllOf(filters) => filters.iter().all(|filter| filter.matches(envelope)),
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// Watch Subscription
// ═══════════════════════════════════════════════════════════════════════════

/// A filtered watch subscription over the event stream.
pub struct WatchSubscription {
    filter: WatchFilter,
    rx: broadcast::Receiver<EventEnvelope>,
}

impl WatchSubscription {
    /// Create a new subscription from a broadcast receiver and filter.
    pub fn new(rx: broadcast::Receiver<EventEnvelope>, filter: WatchFilter) -> Self {
        Self { filter, rx }
    }

    /// Receive the next matching event, blocking until one arrives.
    ///
    /// Returns `Err` if the channel is closed or the subscriber lagged
    /// (missed events due to slow consumption).
    pub async fn next(&mut self) -> HirnResult<EventEnvelope> {
        loop {
            match self.rx.recv().await {
                Ok(envelope) => {
                    if self.filter.matches(&envelope) {
                        return Ok(envelope);
                    }
                    // Skip non-matching events.
                }
                Err(broadcast::error::RecvError::Lagged(n)) => {
                    return Err(HirnError::LimitExceeded(format!(
                        "watch subscriber lagged, missed {n} events"
                    )));
                }
                Err(broadcast::error::RecvError::Closed) => {
                    return Err(HirnError::InvalidInput("event channel closed".to_string()));
                }
            }
        }
    }

    /// Try to receive the next matching event without blocking indefinitely.
    ///
    /// Returns `Ok(None)` if the channel is closed.
    /// Returns `Err` if the subscriber lagged (missed events).
    pub fn try_next(&mut self) -> HirnResult<Option<EventEnvelope>> {
        loop {
            match self.rx.try_recv() {
                Ok(envelope) => {
                    if self.filter.matches(&envelope) {
                        return Ok(Some(envelope));
                    }
                }
                Err(broadcast::error::TryRecvError::Empty) => return Ok(None),
                Err(broadcast::error::TryRecvError::Lagged(n)) => {
                    return Err(HirnError::LimitExceeded(format!(
                        "watch subscriber lagged, missed {n} events"
                    )));
                }
                Err(broadcast::error::TryRecvError::Closed) => return Ok(None),
            }
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// HirnDB::watch integration
// ═══════════════════════════════════════════════════════════════════════════

use crate::db::HirnDB;

impl HirnDB {
    /// Create a watch subscription with the given filter.
    ///
    /// Requires an active `EventLog` (returns error otherwise).
    pub fn watch(&self, filter: WatchFilter) -> HirnResult<WatchSubscription> {
        let event_log = self
            .event_log()
            .ok_or_else(|| HirnError::InvalidInput("event log not configured".to_string()))?;
        let rx = event_log.subscribe();
        Ok(WatchSubscription::new(rx, filter))
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// Tests
// ═══════════════════════════════════════════════════════════════════════════

#[cfg(test)]
mod tests {
    use super::*;
    use hirn_core::id::MemoryId;
    use hirn_core::types::Layer;

    fn make_envelope(event: MemoryEvent, namespace: &str) -> EventEnvelope {
        EventEnvelope::new(1, "default", namespace, "test-agent", event)
    }

    #[test]
    fn filter_all_matches_everything() {
        let filter = WatchFilter::All;
        let env = make_envelope(
            MemoryEvent::Forgotten {
                id: MemoryId::new(),
            },
            "ns1",
        );
        assert!(filter.matches(&env));
    }

    #[test]
    fn filter_namespace_matches_correct_ns() {
        let filter = WatchFilter::Namespace("shared".to_string());

        let matching = make_envelope(
            MemoryEvent::Forgotten {
                id: MemoryId::new(),
            },
            "shared",
        );
        let non_matching = make_envelope(
            MemoryEvent::Forgotten {
                id: MemoryId::new(),
            },
            "private",
        );

        assert!(filter.matches(&matching));
        assert!(!filter.matches(&non_matching));
    }

    #[test]
    fn filter_namespaces_matches_any_allowed_ns() {
        let filter = WatchFilter::Namespaces(vec!["shared".to_string(), "team".to_string()]);

        let matching = make_envelope(
            MemoryEvent::Forgotten {
                id: MemoryId::new(),
            },
            "team",
        );
        let non_matching = make_envelope(
            MemoryEvent::Forgotten {
                id: MemoryId::new(),
            },
            "private",
        );

        assert!(filter.matches(&matching));
        assert!(!filter.matches(&non_matching));
    }

    #[test]
    fn filter_entities_case_insensitive() {
        let filter = WatchFilter::Entities(vec!["auth".to_string()]);

        let matching = make_envelope(
            MemoryEvent::EpisodeCreated {
                id: MemoryId::new(),
                content_preview: "Discussed Auth flow with OAuth2".to_string(),
            },
            "ns",
        );
        let non_matching = make_envelope(
            MemoryEvent::EpisodeCreated {
                id: MemoryId::new(),
                content_preview: "Talked about recipes".to_string(),
            },
            "ns",
        );

        assert!(filter.matches(&matching));
        assert!(!filter.matches(&non_matching));
    }

    #[test]
    fn filter_importance_above_threshold() {
        let filter = WatchFilter::ImportanceAbove(0.8);

        let above = make_envelope(
            MemoryEvent::ImportanceUpdated {
                id: MemoryId::new(),
                old_value: 0.5,
                new_value: 0.9,
            },
            "ns",
        );
        let below = make_envelope(
            MemoryEvent::ImportanceUpdated {
                id: MemoryId::new(),
                old_value: 0.5,
                new_value: 0.7,
            },
            "ns",
        );
        let other = make_envelope(
            MemoryEvent::Forgotten {
                id: MemoryId::new(),
            },
            "ns",
        );

        assert!(filter.matches(&above));
        assert!(!filter.matches(&below));
        assert!(!filter.matches(&other));
    }

    #[test]
    fn filter_layers_match_actual_event_layer() {
        let filter = WatchFilter::Layers(vec![Layer::Procedural]);

        let matching = make_envelope(
            MemoryEvent::ProceduralCreated {
                id: MemoryId::new(),
                procedure_name: "deploy-to-staging".to_string(),
            },
            "ns",
        );
        let non_matching = make_envelope(
            MemoryEvent::EpisodeCreated {
                id: MemoryId::new(),
                content_preview: "deploy-to-staging".to_string(),
            },
            "ns",
        );

        assert!(filter.matches(&matching));
        assert!(!filter.matches(&non_matching));
    }

    #[test]
    fn filter_contradictions_matches_detected_events() {
        let filter = WatchFilter::Contradictions;

        let contradiction = make_envelope(
            MemoryEvent::ContradictionDetected {
                memory_a: MemoryId::new(),
                memory_b: MemoryId::new(),
                confidence: 0.92,
            },
            "ns",
        );
        let other = make_envelope(
            MemoryEvent::Forgotten {
                id: MemoryId::new(),
            },
            "ns",
        );

        assert!(filter.matches(&contradiction));
        assert!(!filter.matches(&other));
    }

    #[test]
    fn filter_event_types() {
        let filter = WatchFilter::EventTypes(vec![
            "episode_created".to_string(),
            "semantic_created".to_string(),
        ]);

        let ep = make_envelope(
            MemoryEvent::EpisodeCreated {
                id: MemoryId::new(),
                content_preview: "test".to_string(),
            },
            "ns",
        );
        let sem = make_envelope(
            MemoryEvent::SemanticCreated {
                id: MemoryId::new(),
                concept_name: "test".to_string(),
            },
            "ns",
        );
        let other = make_envelope(
            MemoryEvent::Forgotten {
                id: MemoryId::new(),
            },
            "ns",
        );

        assert!(filter.matches(&ep));
        assert!(filter.matches(&sem));
        assert!(!filter.matches(&other));
    }

    #[test]
    fn filter_all_of_requires_every_child_to_match() {
        let filter = WatchFilter::all_of(vec![
            WatchFilter::Namespace("shared".to_string()),
            WatchFilter::Entities(vec!["auth".to_string()]),
        ]);

        let matching = make_envelope(
            MemoryEvent::EpisodeCreated {
                id: MemoryId::new(),
                content_preview: "auth rollout completed".to_string(),
            },
            "shared",
        );
        let wrong_namespace = make_envelope(
            MemoryEvent::EpisodeCreated {
                id: MemoryId::new(),
                content_preview: "auth rollout completed".to_string(),
            },
            "private",
        );
        let wrong_entity = make_envelope(
            MemoryEvent::EpisodeCreated {
                id: MemoryId::new(),
                content_preview: "recipe rollout completed".to_string(),
            },
            "shared",
        );

        assert!(filter.matches(&matching));
        assert!(!filter.matches(&wrong_namespace));
        assert!(!filter.matches(&wrong_entity));
    }

    #[test]
    fn filter_validate_allowed_namespaces_rejects_unauthorized_reference() {
        let filter = WatchFilter::Namespace("private:agent_a".to_string());
        let agent_b = hirn_core::types::AgentId::new("agent_b").unwrap();
        let allowed_namespaces = [Namespace::shared(), Namespace::private_for(&agent_b)];

        let result = filter.validate_allowed_namespaces(&allowed_namespaces);
        assert!(result.is_err());
    }

    #[test]
    fn multiple_subscribers_independent() {
        let (tx, _) = broadcast::channel::<EventEnvelope>(16);

        let sub1 = WatchSubscription::new(tx.subscribe(), WatchFilter::All);
        let sub2 =
            WatchSubscription::new(tx.subscribe(), WatchFilter::Namespace("shared".to_string()));

        // Both created — dropping one doesn't affect the other.
        drop(sub1);
        assert!(matches!(sub2.filter, WatchFilter::Namespace(_)));
    }

    #[tokio::test]
    async fn subscription_receives_filtered_events() {
        let (tx, _) = broadcast::channel::<EventEnvelope>(16);

        let mut sub =
            WatchSubscription::new(tx.subscribe(), WatchFilter::Namespace("target".to_string()));

        // Send matching and non-matching events.
        let matching = make_envelope(
            MemoryEvent::EpisodeCreated {
                id: MemoryId::new(),
                content_preview: "test".to_string(),
            },
            "target",
        );
        let non_matching = make_envelope(
            MemoryEvent::Forgotten {
                id: MemoryId::new(),
            },
            "other",
        );

        tx.send(non_matching).unwrap();
        tx.send(matching.clone()).unwrap();

        let received = sub.next().await.unwrap();
        assert_eq!(received.namespace, "target");
    }

    #[tokio::test]
    async fn subscriber_drop_no_error_on_others() {
        let (tx, _rx) = broadcast::channel::<EventEnvelope>(16);

        let sub1 = WatchSubscription::new(tx.subscribe(), WatchFilter::All);
        let mut sub2 = WatchSubscription::new(tx.subscribe(), WatchFilter::All);

        drop(sub1);

        // sub2 should still work fine.
        let env = make_envelope(
            MemoryEvent::Forgotten {
                id: MemoryId::new(),
            },
            "ns",
        );
        tx.send(env).unwrap();

        let received = sub2.next().await.unwrap();
        assert_eq!(received.event.event_type(), "forgotten");
    }
}