lunaris-memory 0.8.0

Lunaris agent memory engine — umbrella crate (Apache-2.0)
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
//! `Lunaris::invalidate_range` — bulk FT invalidation for Helios force-push recovery.
//!
//! ## Design rationale
//!
//! When `helios-git` detects a force-push or rebase, it calls
//! `Lunaris::invalidate_range(scope, node_id, hlc_lo, hlc_hi)` to evict stale
//! recall from the agent's memory. This module implements the fan-out: it issues
//! `FT.INVALIDATE_RANGE` against every known Lunaris collection in parallel and
//! aggregates the delete counts.
//!
//! ## Collections targeted
//!
//! Lunaris manages four FT indices per scope on Moon:
//! `chunks`, `entities`, `facts`, `communities`.
//!
//! Note: `helios-index` may add additional indices (`symbols`, `commits`) on top
//! of these; those are NOT managed by this module. See SUMMARY for the schema
//! roadmap.
//!
//! ## Degraded mode
//!
//! If a collection's index is missing (`WRONGTYPE` from Moon — index does not exist)
//! or the backend does not implement the primitive (`NotSupported`), that collection
//! is skipped with a `WARN` log. The method still returns `Ok(partial_count)`.
//!
//! ## IO failure surface (CONTEXT.md §5)
//!
//! | Surface          | Timeout | Retry | Fallback                          |
//! |------------------|---------|-------|-----------------------------------|
//! | Per-index call   | 250 ms  | none  | warn + skip (degraded mode count) |
//!
//! ## Closed-interval semantics
//!
//! Moon's `FT.INVALIDATE_RANGE` uses a closed `[lo, hi]` interval. Both
//! `hlc_wall_lo_inclusive` and `hlc_wall_hi_inclusive` are inclusive millisecond
//! timestamps. Callers with half-open ranges (`lo..hi`) must pass `hi - 1`.

use std::sync::Arc;
use std::time::Duration;

use futures::future::join_all;
use lunaris_core::error::LunarisError;
use lunaris_core::{Scope, StoragePort};
use tracing::{instrument, warn};

/// Known Lunaris FT index kinds. These map to Moon indices via
/// `lunaris_storage_moon::keyspace::ft_index_name(scope, kind)`.
///
/// Note: `symbols` and `commits` are added by `helios-index` and are NOT
/// present in this list — they require schema additions not yet landed.
/// See `.planning/W2-L2-INVALIDATE-RANGE-SUMMARY.md`.
const KNOWN_COLLECTIONS: &[&str] = &["chunks", "entities", "facts", "communities"];

/// Field name for the node-id TAG field expected in FT index schemas.
///
/// ## Schema precondition
///
/// This field must be declared as `TAG` in the `FT.CREATE` schema. Indices
/// predating the `helios-git` schema additions (see SUMMARY) lack it and
/// silently return 0 (Moon bitmap intersect is empty — not an error).
pub(crate) const HLC_NODE_ID_FIELD: &str = "hlc_node_id";

/// Field name for the HLC wall-clock NUMERIC field expected in FT index schemas.
///
/// ## Schema precondition
///
/// This field must be declared as `NUMERIC` in the `FT.CREATE` schema. Indices
/// predating the `helios-git` schema additions silently return 0.
pub(crate) const HLC_WALL_FIELD: &str = "hlc_wall";

/// Per-index call timeout (CONTEXT.md §5 IO failure surface).
const PER_INDEX_TIMEOUT: Duration = Duration::from_millis(250);

/// Bulk-invalidate FT index records authored by `node_id` within
/// `[hlc_wall_lo_inclusive, hlc_wall_hi_inclusive]` across all known collections.
///
/// Returns the total count of invalidated records. Individual collection errors
/// (missing index, unsupported backend) are warned and skipped (degraded mode).
#[instrument(skip(storage), fields(node_id, hlc_wall_lo_inclusive, hlc_wall_hi_inclusive))]
pub(crate) async fn invalidate_range(
    storage: &Arc<dyn StoragePort>,
    scope: &Scope,
    node_id: &str,
    hlc_wall_lo_inclusive: i64,
    hlc_wall_hi_inclusive: i64,
) -> Result<u64, LunarisError> {
    // Short-circuit: empty or inverted range — no wire calls needed.
    if hlc_wall_lo_inclusive > hlc_wall_hi_inclusive {
        return Ok(0);
    }

    // Fan-out: issue FT.INVALIDATE_RANGE for each collection in parallel.
    // We use `join_all` (not `try_join_all`) so a missing index on one
    // collection does NOT abort the others — degraded mode continues.
    let futs: Vec<_> = KNOWN_COLLECTIONS
        .iter()
        .map(|&kind| {
            let storage = Arc::clone(storage);
            let scope = scope.clone();
            let node_id = node_id.to_owned();
            async move {
                let result = tokio::time::timeout(
                    PER_INDEX_TIMEOUT,
                    storage.invalidate_range(
                        &scope,
                        kind,
                        HLC_NODE_ID_FIELD,
                        &node_id,
                        HLC_WALL_FIELD,
                        hlc_wall_lo_inclusive,
                        hlc_wall_hi_inclusive,
                    ),
                )
                .await;

                match result {
                    // Timeout — warn and skip.
                    Err(_elapsed) => {
                        warn!(
                            collection = kind,
                            timeout_ms = PER_INDEX_TIMEOUT.as_millis(),
                            "invalidate_range: per-index call timed out (degraded mode)"
                        );
                        0u64
                    }
                    // Backend error — check for degraded-mode cases.
                    Ok(Err(e)) => {
                        let msg = e.to_string();
                        // WRONGTYPE = index does not exist on Moon.
                        // NotSupported = backend (Postgres/Embedded) has no primitive.
                        // Both are expected in degraded mode — warn and skip.
                        if msg.contains("WRONGTYPE")
                            || msg.contains("not supported")
                            || msg.contains("NotSupported")
                            || msg.contains("no such index")
                        {
                            warn!(
                                collection = kind,
                                error = %e,
                                "invalidate_range: index missing or unsupported — skipping (degraded mode)"
                            );
                        } else {
                            warn!(
                                collection = kind,
                                error = %e,
                                "invalidate_range: backend error — skipping (degraded mode)"
                            );
                        }
                        0u64
                    }
                    // Success — collect count.
                    Ok(Ok(count)) => count,
                }
            }
        })
        .collect();

    let counts = join_all(futs).await;
    let total: u64 = counts.into_iter().sum();
    Ok(total)
}

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

    use async_trait::async_trait;
    use bytes::Bytes;
    use futures::stream::BoxStream;
    use lunaris_core::storage::capabilities::{CypherDialect, StorageCapabilities};
    use lunaris_core::{
        CypherQuery, Filter, GraphResult, Hlc, Lsn, QueueMsg, Row, StorageError, VectorHit, WriteOp,
    };
    use std::sync::Arc;
    use std::sync::atomic::{AtomicU64, Ordering};
    use tokio::time::Instant;

    // ── Capabilities helper ───────────────────────────────────────────────────

    fn no_capabilities() -> StorageCapabilities {
        StorageCapabilities {
            bi_temporal_native: false,
            graph_native: false,
            rerank_native: false,
            queue_native: false,
            max_vector_dim: 0,
            native_rrf: false,
            max_scopes_recommended: 0,
            cypher_dialect: CypherDialect::Legacy,
            graph_decay_native: false,
            graph_navigate_native: false,
        }
    }

    // ── MockStorage: uniform behavior + shared call counter ───────────────────

    struct MockStorage {
        call_count: Arc<AtomicU64>,
        behavior: MockBehavior,
    }

    enum MockBehavior {
        Returns(u64),
        MissingIndex,
        NotSupported,
        Timeout,
    }

    impl MockStorage {
        fn new(behavior: MockBehavior) -> (Arc<Self>, Arc<AtomicU64>) {
            let counter = Arc::new(AtomicU64::new(0));
            let mock = Arc::new(MockStorage { call_count: Arc::clone(&counter), behavior });
            (mock, counter)
        }
    }

    #[async_trait]
    impl StoragePort for MockStorage {
        async fn atomic_write(&self, _: &Scope, _: &[WriteOp]) -> Result<Lsn, StorageError> {
            unimplemented!("stub")
        }
        #[allow(clippy::too_many_arguments)]
        async fn vector_search(
            &self,
            _: &Scope,
            _: &str,
            _: &[f32],
            _: usize,
            _: Option<&Filter>,
            _: Option<Hlc>,
            _: bool,
        ) -> Result<Vec<VectorHit>, StorageError> {
            unimplemented!("stub")
        }
        async fn graph_traverse(
            &self,
            _: &Scope,
            _: &CypherQuery,
            _: Option<Hlc>,
        ) -> Result<GraphResult, StorageError> {
            unimplemented!("stub")
        }
        async fn scan_range(
            &self,
            _: &Scope,
            _: &[u8],
            _: Option<Hlc>,
        ) -> Result<BoxStream<'_, Result<(Bytes, Bytes), StorageError>>, StorageError> {
            unimplemented!("stub")
        }
        async fn read_as_of(
            &self,
            _: &Scope,
            _: &[u8],
            _: Hlc,
        ) -> Result<Option<Row<Bytes>>, StorageError> {
            unimplemented!("stub")
        }
        async fn publish(&self, _: &Scope, _: &str, _: u16, _: Bytes) -> Result<u64, StorageError> {
            unimplemented!("stub")
        }
        async fn subscribe(
            &self,
            _: &Scope,
            _: &str,
            _: &str,
            _: u16,
        ) -> Result<BoxStream<'static, Result<QueueMsg, StorageError>>, StorageError> {
            unimplemented!("stub")
        }
        fn capabilities(&self) -> StorageCapabilities {
            no_capabilities()
        }

        async fn invalidate_range(
            &self,
            _scope: &Scope,
            _index: &str,
            _node_id_field: &str,
            _node_id_value: &str,
            _hlc_wall_field: &str,
            _lo: i64,
            _hi: i64,
        ) -> Result<u64, StorageError> {
            self.call_count.fetch_add(1, Ordering::SeqCst);
            match self.behavior {
                MockBehavior::Returns(n) => Ok(n),
                MockBehavior::MissingIndex => {
                    Err(StorageError::Backend("moon: WRONGTYPE no such index".into()))
                }
                MockBehavior::NotSupported => Err(StorageError::NotSupported(
                    "invalidate_range not implemented for this StoragePort backend",
                )),
                MockBehavior::Timeout => {
                    tokio::time::sleep(Duration::from_millis(500)).await;
                    Ok(0)
                }
            }
        }
    }

    // ── PerCollectionMock: different count per index name ─────────────────────

    struct PerCollectionMock;

    #[async_trait]
    impl StoragePort for PerCollectionMock {
        async fn atomic_write(&self, _: &Scope, _: &[WriteOp]) -> Result<Lsn, StorageError> {
            unimplemented!("stub")
        }
        #[allow(clippy::too_many_arguments)]
        async fn vector_search(
            &self,
            _: &Scope,
            _: &str,
            _: &[f32],
            _: usize,
            _: Option<&Filter>,
            _: Option<Hlc>,
            _: bool,
        ) -> Result<Vec<VectorHit>, StorageError> {
            unimplemented!("stub")
        }
        async fn graph_traverse(
            &self,
            _: &Scope,
            _: &CypherQuery,
            _: Option<Hlc>,
        ) -> Result<GraphResult, StorageError> {
            unimplemented!("stub")
        }
        async fn scan_range(
            &self,
            _: &Scope,
            _: &[u8],
            _: Option<Hlc>,
        ) -> Result<BoxStream<'_, Result<(Bytes, Bytes), StorageError>>, StorageError> {
            unimplemented!("stub")
        }
        async fn read_as_of(
            &self,
            _: &Scope,
            _: &[u8],
            _: Hlc,
        ) -> Result<Option<Row<Bytes>>, StorageError> {
            unimplemented!("stub")
        }
        async fn publish(&self, _: &Scope, _: &str, _: u16, _: Bytes) -> Result<u64, StorageError> {
            unimplemented!("stub")
        }
        async fn subscribe(
            &self,
            _: &Scope,
            _: &str,
            _: &str,
            _: u16,
        ) -> Result<BoxStream<'static, Result<QueueMsg, StorageError>>, StorageError> {
            unimplemented!("stub")
        }
        fn capabilities(&self) -> StorageCapabilities {
            no_capabilities()
        }

        async fn invalidate_range(
            &self,
            _scope: &Scope,
            index: &str,
            _node_id_field: &str,
            _node_id_value: &str,
            _hlc_wall_field: &str,
            _lo: i64,
            _hi: i64,
        ) -> Result<u64, StorageError> {
            // chunks=5, entities=3, facts=0, communities=0 → sum=8
            match index {
                "chunks" => Ok(5),
                "entities" => Ok(3),
                _ => Ok(0),
            }
        }
    }

    // ── SlowMock: 100ms sleep per call — proves parallel fan-out ──────────────

    struct SlowMock;

    #[async_trait]
    impl StoragePort for SlowMock {
        async fn atomic_write(&self, _: &Scope, _: &[WriteOp]) -> Result<Lsn, StorageError> {
            unimplemented!("stub")
        }
        #[allow(clippy::too_many_arguments)]
        async fn vector_search(
            &self,
            _: &Scope,
            _: &str,
            _: &[f32],
            _: usize,
            _: Option<&Filter>,
            _: Option<Hlc>,
            _: bool,
        ) -> Result<Vec<VectorHit>, StorageError> {
            unimplemented!("stub")
        }
        async fn graph_traverse(
            &self,
            _: &Scope,
            _: &CypherQuery,
            _: Option<Hlc>,
        ) -> Result<GraphResult, StorageError> {
            unimplemented!("stub")
        }
        async fn scan_range(
            &self,
            _: &Scope,
            _: &[u8],
            _: Option<Hlc>,
        ) -> Result<BoxStream<'_, Result<(Bytes, Bytes), StorageError>>, StorageError> {
            unimplemented!("stub")
        }
        async fn read_as_of(
            &self,
            _: &Scope,
            _: &[u8],
            _: Hlc,
        ) -> Result<Option<Row<Bytes>>, StorageError> {
            unimplemented!("stub")
        }
        async fn publish(&self, _: &Scope, _: &str, _: u16, _: Bytes) -> Result<u64, StorageError> {
            unimplemented!("stub")
        }
        async fn subscribe(
            &self,
            _: &Scope,
            _: &str,
            _: &str,
            _: u16,
        ) -> Result<BoxStream<'static, Result<QueueMsg, StorageError>>, StorageError> {
            unimplemented!("stub")
        }
        fn capabilities(&self) -> StorageCapabilities {
            no_capabilities()
        }

        async fn invalidate_range(
            &self,
            _scope: &Scope,
            _index: &str,
            _node_id_field: &str,
            _node_id_value: &str,
            _hlc_wall_field: &str,
            _lo: i64,
            _hi: i64,
        ) -> Result<u64, StorageError> {
            tokio::time::sleep(Duration::from_millis(100)).await;
            Ok(1)
        }
    }

    // ── Helper ────────────────────────────────────────────────────────────────

    fn test_scope() -> Scope {
        Scope::new("test.worktree-abc").unwrap()
    }

    // ── Tests ─────────────────────────────────────────────────────────────────

    /// Fan-out: `invalidate_range` calls the storage for every known collection.
    #[tokio::test]
    async fn invalidate_range_fans_out_to_all_collections() {
        let (mock, counter) = MockStorage::new(MockBehavior::Returns(1));
        let storage: Arc<dyn StoragePort> = mock;
        let scope = test_scope();

        invalidate_range(&storage, &scope, "helios-git@aabbcc", 1_000, 2_000)
            .await
            .expect("should succeed");

        assert_eq!(
            counter.load(Ordering::SeqCst),
            KNOWN_COLLECTIONS.len() as u64,
            "expected one call per collection ({} total)",
            KNOWN_COLLECTIONS.len()
        );
    }

    /// Count aggregation: partial counts from each collection are summed.
    #[tokio::test]
    async fn invalidate_range_aggregates_counts_correctly() {
        let storage: Arc<dyn StoragePort> = Arc::new(PerCollectionMock);
        let scope = test_scope();

        let total = invalidate_range(&storage, &scope, "helios-git@aabbcc", 1_000, 2_000)
            .await
            .expect("should succeed");

        // chunks=5, entities=3, facts=0, communities=0 → total=8
        assert_eq!(total, 8, "should sum all collection counts");
    }

    /// Missing-index tolerance: WRONGTYPE errors are warn-and-skipped (degraded mode).
    #[tokio::test]
    async fn invalidate_range_tolerates_missing_index_with_warn() {
        let (mock, _counter) = MockStorage::new(MockBehavior::MissingIndex);
        let storage: Arc<dyn StoragePort> = mock;
        let scope = test_scope();

        let total = invalidate_range(&storage, &scope, "helios-git@aabbcc", 1_000, 2_000)
            .await
            .expect("should succeed in degraded mode");

        assert_eq!(total, 0, "missing index should degrade to 0, not error");
    }

    /// NotSupported backend: same degraded-mode behavior as missing index.
    #[tokio::test]
    async fn invalidate_range_tolerates_not_supported_backend() {
        let (mock, _counter) = MockStorage::new(MockBehavior::NotSupported);
        let storage: Arc<dyn StoragePort> = mock;
        let scope = test_scope();

        let total = invalidate_range(&storage, &scope, "helios-git@aabbcc", 1_000, 2_000)
            .await
            .expect("degraded mode: NotSupported should not propagate as error");

        assert_eq!(total, 0, "NotSupported should degrade to 0");
    }

    /// Timeout: per-index calls exceeding 250ms are skipped with 0 contribution.
    #[tokio::test]
    async fn invalidate_range_timeout_skips_slow_collections() {
        let (mock, _counter) = MockStorage::new(MockBehavior::Timeout);
        let storage: Arc<dyn StoragePort> = mock;
        let scope = test_scope();

        let total = invalidate_range(&storage, &scope, "helios-git@aabbcc", 1_000, 2_000)
            .await
            .expect("timeout should produce Ok(0), not Err");

        assert_eq!(total, 0, "timed-out collections should contribute 0");
    }

    /// Empty range (lo > hi): returns Ok(0) immediately without any wire calls.
    #[tokio::test]
    async fn invalidate_range_empty_range_returns_zero_without_calls() {
        let (mock, counter) = MockStorage::new(MockBehavior::Returns(99));
        let storage: Arc<dyn StoragePort> = mock;
        let scope = test_scope();

        let total = invalidate_range(&storage, &scope, "helios-git@aabbcc", 2_000, 1_000)
            .await
            .expect("empty range should return Ok(0)");

        assert_eq!(total, 0, "inverted range must return 0");
        assert_eq!(
            counter.load(Ordering::SeqCst),
            0,
            "empty range must not issue any storage calls"
        );
    }

    /// Equal bounds (lo == hi): valid single-point closed range — calls ARE made.
    #[tokio::test]
    async fn invalidate_range_equal_bounds_is_valid_single_point() {
        let (mock, _counter) = MockStorage::new(MockBehavior::Returns(2));
        let storage: Arc<dyn StoragePort> = mock;
        let scope = test_scope();

        let total = invalidate_range(&storage, &scope, "helios-git@aabbcc", 1_500, 1_500)
            .await
            .expect("single-point range should succeed");

        assert_eq!(
            total,
            (KNOWN_COLLECTIONS.len() as u64) * 2,
            "each of {} collections returns 2 → total {}",
            KNOWN_COLLECTIONS.len(),
            KNOWN_COLLECTIONS.len() * 2
        );
    }

    /// Parallel fan-out: all collections run concurrently, not sequentially.
    ///
    /// Proof: 4 collections each sleep 100ms. If parallel, wall time < 200ms.
    /// If sequential, wall time ≥ 400ms.
    #[tokio::test]
    async fn invalidate_range_fan_out_is_parallel() {
        let storage: Arc<dyn StoragePort> = Arc::new(SlowMock);
        let scope = test_scope();

        let t0 = Instant::now();
        let total = invalidate_range(&storage, &scope, "helios-git@aabbcc", 1_000, 2_000)
            .await
            .expect("parallel fan-out should succeed");
        let elapsed = t0.elapsed();

        assert_eq!(total, KNOWN_COLLECTIONS.len() as u64, "all collections contribute 1 each");
        // Parallel: 4 × 100ms calls complete in well under 300ms.
        // Sequential would take ≥ 400ms.
        assert!(
            elapsed < Duration::from_millis(300),
            "fan-out must be parallel: elapsed {}ms ≥ 300ms",
            elapsed.as_millis()
        );
    }
}