rlm-cli 1.2.4

Recursive Language Model (RLM) REPL for Claude Code - handles long-context tasks via chunking and recursive sub-LLM calls
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
//! HNSW (Hierarchical Navigable Small World) vector index.
//!
//! Provides fast approximate nearest neighbor search using the usearch library.
//! Falls back to brute-force search when usearch is not available.

use crate::error::{Result, SearchError};
#[cfg(feature = "usearch-hnsw")]
use std::collections::HashMap;
use std::path::Path;

#[cfg(feature = "usearch-hnsw")]
use usearch::{Index, IndexOptions, MetricKind, ScalarKind};

/// HNSW vector index for scalable semantic search.
///
/// This wraps the usearch HNSW implementation, providing:
/// - O(log n) approximate nearest neighbor search
/// - Persistence to disk
/// - Incremental updates
///
/// When the `usearch-hnsw` feature is not enabled, operations return
/// appropriate errors, and callers should fall back to brute-force search.
pub struct HnswIndex {
    #[cfg(feature = "usearch-hnsw")]
    inner: Index,
    #[cfg(feature = "usearch-hnsw")]
    id_map: HashMap<u64, i64>, // usearch key -> chunk_id
    #[cfg(feature = "usearch-hnsw")]
    reverse_map: HashMap<i64, u64>, // chunk_id -> usearch key
    #[cfg(feature = "usearch-hnsw")]
    next_key: u64,
    dimensions: usize,
}

// Allow missing fields in Debug - inner (usearch::Index) doesn't implement Debug,
// and reverse_map is redundant with id_map_len.
#[allow(clippy::missing_fields_in_debug)]
impl std::fmt::Debug for HnswIndex {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut debug_struct = f.debug_struct("HnswIndex");
        debug_struct.field("dimensions", &self.dimensions);
        #[cfg(feature = "usearch-hnsw")]
        {
            debug_struct.field("id_map_len", &self.id_map.len());
            debug_struct.field("next_key", &self.next_key);
        }
        debug_struct.finish()
    }
}

/// Configuration for HNSW index.
#[derive(Debug, Clone)]
pub struct HnswConfig {
    /// Number of dimensions in the vectors.
    pub dimensions: usize,
    /// M parameter (max connections per node, higher = more accurate but slower).
    pub connectivity: usize,
    /// `ef_construction` (search depth during build, higher = better quality index).
    pub expansion_add: usize,
    /// `ef_search` (search depth during query, higher = more accurate results).
    pub expansion_search: usize,
}

impl Default for HnswConfig {
    fn default() -> Self {
        Self {
            dimensions: 384, // Default for all-MiniLM-L6-v2
            connectivity: 16,
            expansion_add: 128,
            expansion_search: 64,
        }
    }
}

impl HnswConfig {
    /// Creates a new configuration with custom dimensions.
    #[must_use]
    pub const fn with_dimensions(dimensions: usize) -> Self {
        Self {
            dimensions,
            connectivity: 16,
            expansion_add: 128,
            expansion_search: 64,
        }
    }

    /// Sets the connectivity parameter (M).
    #[must_use]
    pub const fn connectivity(mut self, m: usize) -> Self {
        self.connectivity = m;
        self
    }

    /// Sets the expansion during add (`ef_construction`).
    #[must_use]
    pub const fn expansion_add(mut self, ef: usize) -> Self {
        self.expansion_add = ef;
        self
    }

    /// Sets the expansion during search (`ef_search`).
    #[must_use]
    pub const fn expansion_search(mut self, ef: usize) -> Self {
        self.expansion_search = ef;
        self
    }
}

/// Search result from HNSW index.
#[derive(Debug, Clone)]
pub struct HnswResult {
    /// Chunk ID.
    pub chunk_id: i64,
    /// Distance (lower is more similar for cosine).
    pub distance: f32,
    /// Similarity score (1 - distance for normalized vectors).
    pub similarity: f32,
}

impl HnswIndex {
    /// Creates a new HNSW index with the given configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if the usearch feature is not enabled or index creation fails.
    #[cfg(feature = "usearch-hnsw")]
    pub fn new(config: &HnswConfig) -> Result<Self> {
        let options = IndexOptions {
            dimensions: config.dimensions,
            metric: MetricKind::Cos, // Cosine similarity
            quantization: ScalarKind::F32,
            connectivity: config.connectivity,
            expansion_add: config.expansion_add,
            expansion_search: config.expansion_search,
            multi: false,
        };

        let index = Index::new(&options).map_err(|e| SearchError::IndexError {
            message: format!("Failed to create HNSW index: {e}"),
        })?;

        Ok(Self {
            inner: index,
            id_map: HashMap::new(),
            reverse_map: HashMap::new(),
            next_key: 0,
            dimensions: config.dimensions,
        })
    }

    /// Creates a new HNSW index (feature not enabled).
    #[cfg(not(feature = "usearch-hnsw"))]
    pub fn new(config: &HnswConfig) -> Result<Self> {
        Ok(Self {
            dimensions: config.dimensions,
        })
    }

    /// Returns whether HNSW is available.
    #[must_use]
    pub const fn is_available() -> bool {
        cfg!(feature = "usearch-hnsw")
    }

    /// Returns the number of dimensions in the index.
    #[must_use]
    pub const fn dimensions(&self) -> usize {
        self.dimensions
    }

    /// Returns the number of vectors in the index.
    #[cfg(feature = "usearch-hnsw")]
    #[must_use]
    pub fn len(&self) -> usize {
        self.inner.size()
    }

    /// Returns the number of vectors in the index.
    #[cfg(not(feature = "usearch-hnsw"))]
    #[must_use]
    pub const fn len(&self) -> usize {
        0
    }

    /// Returns whether the index is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Adds a vector to the index.
    ///
    /// # Errors
    ///
    /// Returns an error if the vector dimensions don't match or insertion fails.
    #[cfg(feature = "usearch-hnsw")]
    pub fn add(&mut self, chunk_id: i64, vector: &[f32]) -> Result<()> {
        if vector.len() != self.dimensions {
            return Err(SearchError::DimensionMismatch {
                expected: self.dimensions,
                got: vector.len(),
            }
            .into());
        }

        // Check if already indexed
        if self.reverse_map.contains_key(&chunk_id) {
            // Remove old entry first
            self.remove(chunk_id)?;
        }

        let key = self.next_key;
        self.next_key += 1;

        self.inner
            .add(key, vector)
            .map_err(|e| SearchError::IndexError {
                message: format!("Failed to add vector: {e}"),
            })?;

        self.id_map.insert(key, chunk_id);
        self.reverse_map.insert(chunk_id, key);

        Ok(())
    }

    /// Adds a vector to the index (feature not enabled).
    #[cfg(not(feature = "usearch-hnsw"))]
    pub fn add(&mut self, _chunk_id: i64, _vector: &[f32]) -> Result<()> {
        Err(SearchError::FeatureNotEnabled {
            feature: "usearch-hnsw".to_string(),
        }
        .into())
    }

    /// Adds multiple vectors to the index in batch.
    ///
    /// # Errors
    ///
    /// Returns an error if any insertion fails.
    #[cfg(feature = "usearch-hnsw")]
    pub fn add_batch(&mut self, items: &[(i64, Vec<f32>)]) -> Result<usize> {
        let mut count = 0;
        for (chunk_id, vector) in items {
            self.add(*chunk_id, vector)?;
            count += 1;
        }
        Ok(count)
    }

    /// Adds multiple vectors to the index in batch (feature not enabled).
    #[cfg(not(feature = "usearch-hnsw"))]
    pub fn add_batch(&mut self, _items: &[(i64, Vec<f32>)]) -> Result<usize> {
        Err(SearchError::FeatureNotEnabled {
            feature: "usearch-hnsw".to_string(),
        }
        .into())
    }

    /// Removes a vector from the index.
    ///
    /// # Errors
    ///
    /// Returns an error if removal fails.
    #[cfg(feature = "usearch-hnsw")]
    pub fn remove(&mut self, chunk_id: i64) -> Result<bool> {
        if let Some(key) = self.reverse_map.remove(&chunk_id) {
            self.inner
                .remove(key)
                .map_err(|e| SearchError::IndexError {
                    message: format!("Failed to remove vector: {e}"),
                })?;
            self.id_map.remove(&key);
            Ok(true)
        } else {
            Ok(false)
        }
    }

    /// Removes a vector from the index (feature not enabled).
    #[cfg(not(feature = "usearch-hnsw"))]
    pub fn remove(&mut self, _chunk_id: i64) -> Result<bool> {
        Err(SearchError::FeatureNotEnabled {
            feature: "usearch-hnsw".to_string(),
        }
        .into())
    }

    /// Searches for the k nearest neighbors.
    ///
    /// # Arguments
    ///
    /// * `query` - The query vector.
    /// * `k` - Maximum number of results.
    ///
    /// # Errors
    ///
    /// Returns an error if the search fails.
    #[cfg(feature = "usearch-hnsw")]
    pub fn search(&self, query: &[f32], k: usize) -> Result<Vec<HnswResult>> {
        if query.len() != self.dimensions {
            return Err(SearchError::DimensionMismatch {
                expected: self.dimensions,
                got: query.len(),
            }
            .into());
        }

        if self.is_empty() {
            return Ok(Vec::new());
        }

        let results = self
            .inner
            .search(query, k)
            .map_err(|e| SearchError::IndexError {
                message: format!("Search failed: {e}"),
            })?;

        let mut output = Vec::with_capacity(results.keys.len());
        for (key, distance) in results.keys.iter().zip(results.distances.iter()) {
            if let Some(&chunk_id) = self.id_map.get(key) {
                output.push(HnswResult {
                    chunk_id,
                    distance: *distance,
                    // For cosine metric, similarity = 1 - distance (for normalized vectors)
                    similarity: 1.0 - distance,
                });
            }
        }

        Ok(output)
    }

    /// Searches for the k nearest neighbors (feature not enabled).
    #[cfg(not(feature = "usearch-hnsw"))]
    pub fn search(&self, _query: &[f32], _k: usize) -> Result<Vec<HnswResult>> {
        Err(SearchError::FeatureNotEnabled {
            feature: "usearch-hnsw".to_string(),
        }
        .into())
    }

    /// Saves the index to a file.
    ///
    /// # Errors
    ///
    /// Returns an error if saving fails.
    #[cfg(feature = "usearch-hnsw")]
    pub fn save(&self, path: &Path) -> Result<()> {
        let path_str = path.to_str().ok_or_else(|| SearchError::IndexError {
            message: "Invalid path: non-UTF8 characters".to_string(),
        })?;
        self.inner
            .save(path_str)
            .map_err(|e| SearchError::IndexError {
                message: format!("Failed to save index: {e}"),
            })?;

        // Save the ID mappings alongside the index
        let map_path = path.with_extension("map");
        let map_data = serde_json::json!({
            "id_map": self.id_map.iter().map(|(k, v)| (k.to_string(), v)).collect::<HashMap<_, _>>(),
            "next_key": self.next_key,
            "dimensions": self.dimensions,
        });
        std::fs::write(
            &map_path,
            serde_json::to_string(&map_data).unwrap_or_default(),
        )
        .map_err(|e| SearchError::IndexError {
            message: format!("Failed to save ID map: {e}"),
        })?;

        Ok(())
    }

    /// Saves the index to a file (feature not enabled).
    #[cfg(not(feature = "usearch-hnsw"))]
    pub fn save(&self, _path: &Path) -> Result<()> {
        Err(SearchError::FeatureNotEnabled {
            feature: "usearch-hnsw".to_string(),
        }
        .into())
    }

    /// Loads an index from a file.
    ///
    /// # Errors
    ///
    /// Returns an error if loading fails.
    #[cfg(feature = "usearch-hnsw")]
    pub fn load(path: &Path, config: &HnswConfig) -> Result<Self> {
        let options = IndexOptions {
            dimensions: config.dimensions,
            metric: MetricKind::Cos,
            quantization: ScalarKind::F32,
            connectivity: config.connectivity,
            expansion_add: config.expansion_add,
            expansion_search: config.expansion_search,
            multi: false,
        };

        let index = Index::new(&options).map_err(|e| SearchError::IndexError {
            message: format!("Failed to create index for loading: {e}"),
        })?;

        let path_str = path.to_str().ok_or_else(|| SearchError::IndexError {
            message: "Invalid path: non-UTF8 characters".to_string(),
        })?;
        index.load(path_str).map_err(|e| SearchError::IndexError {
            message: format!("Failed to load index: {e}"),
        })?;

        // Load the ID mappings
        let map_path = path.with_extension("map");
        let (id_map, reverse_map, next_key, dimensions) = if map_path.exists() {
            let map_str =
                std::fs::read_to_string(&map_path).map_err(|e| SearchError::IndexError {
                    message: format!("Failed to read ID map: {e}"),
                })?;
            let map_data: serde_json::Value =
                serde_json::from_str(&map_str).map_err(|e| SearchError::IndexError {
                    message: format!("Failed to parse ID map: {e}"),
                })?;

            let mut id_map = HashMap::new();
            let mut reverse_map = HashMap::new();
            if let Some(obj) = map_data.get("id_map").and_then(|v| v.as_object()) {
                for (k, v) in obj {
                    if let (Ok(key), Some(val)) = (k.parse::<u64>(), v.as_i64()) {
                        id_map.insert(key, val);
                        reverse_map.insert(val, key);
                    }
                }
            }
            let next_key = map_data
                .get("next_key")
                .and_then(serde_json::Value::as_u64)
                .unwrap_or(0);
            let dimensions = map_data
                .get("dimensions")
                .and_then(serde_json::Value::as_u64)
                .map_or(config.dimensions, |d| {
                    usize::try_from(d).unwrap_or(config.dimensions)
                });

            (id_map, reverse_map, next_key, dimensions)
        } else {
            (HashMap::new(), HashMap::new(), 0, config.dimensions)
        };

        Ok(Self {
            inner: index,
            id_map,
            reverse_map,
            next_key,
            dimensions,
        })
    }

    /// Loads an index from a file (feature not enabled).
    #[cfg(not(feature = "usearch-hnsw"))]
    pub fn load(_path: &Path, config: &HnswConfig) -> Result<Self> {
        Self::new(config)
    }

    /// Clears all vectors from the index.
    ///
    /// # Errors
    ///
    /// Returns an error if the reset fails.
    #[cfg(feature = "usearch-hnsw")]
    pub fn clear(&mut self) -> Result<()> {
        self.inner.reset().map_err(|e| SearchError::IndexError {
            message: format!("Failed to reset index: {e}"),
        })?;
        self.id_map.clear();
        self.reverse_map.clear();
        self.next_key = 0;
        Ok(())
    }

    /// Clears all vectors from the index.
    ///
    /// # Errors
    ///
    /// Returns an error if the reset fails.
    #[cfg(not(feature = "usearch-hnsw"))]
    pub fn clear(&mut self) -> Result<()> {
        // No-op when feature not enabled
        Ok(())
    }

    /// Checks if a chunk is indexed.
    #[cfg(feature = "usearch-hnsw")]
    #[must_use]
    pub fn contains(&self, chunk_id: i64) -> bool {
        self.reverse_map.contains_key(&chunk_id)
    }

    /// Checks if a chunk is indexed.
    #[cfg(not(feature = "usearch-hnsw"))]
    #[must_use]
    pub const fn contains(&self, _chunk_id: i64) -> bool {
        false
    }
}

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

    #[test]
    fn test_hnsw_config_default() {
        let config = HnswConfig::default();
        assert_eq!(config.dimensions, 384);
        assert_eq!(config.connectivity, 16);
        assert_eq!(config.expansion_add, 128);
        assert_eq!(config.expansion_search, 64);
    }

    #[test]
    fn test_hnsw_config_builder() {
        let config = HnswConfig::with_dimensions(256)
            .connectivity(32)
            .expansion_add(256)
            .expansion_search(128);

        assert_eq!(config.dimensions, 256);
        assert_eq!(config.connectivity, 32);
        assert_eq!(config.expansion_add, 256);
        assert_eq!(config.expansion_search, 128);
    }

    #[test]
    fn test_hnsw_is_available() {
        // This tests the compile-time feature detection
        let available = HnswIndex::is_available();
        // The result depends on whether the feature is enabled
        #[cfg(feature = "usearch-hnsw")]
        assert!(available);
        #[cfg(not(feature = "usearch-hnsw"))]
        assert!(!available);
    }

    #[test]
    #[cfg(not(feature = "usearch-hnsw"))]
    fn test_hnsw_new() {
        let config = HnswConfig::with_dimensions(128);
        let result = HnswIndex::new(&config);
        assert!(result.is_ok());
        let index = result.unwrap();
        assert_eq!(index.dimensions(), 128);
    }

    #[test]
    #[cfg(feature = "usearch-hnsw")]
    fn test_hnsw_new_usearch() {
        let config = HnswConfig::with_dimensions(128);
        let result = HnswIndex::new(&config);
        assert!(result.is_ok());
        let index = result.unwrap();
        assert!(index.is_empty());
        assert_eq!(index.dimensions(), 128);
    }

    #[cfg(feature = "usearch-hnsw")]
    mod usearch_tests {
        use super::*;

        #[test]
        fn test_hnsw_add_and_search() {
            let config = HnswConfig::with_dimensions(4);
            let mut index = HnswIndex::new(&config).unwrap();

            // Add some vectors
            index.add(1, &[1.0, 0.0, 0.0, 0.0]).unwrap();
            index.add(2, &[0.0, 1.0, 0.0, 0.0]).unwrap();
            index.add(3, &[0.0, 0.0, 1.0, 0.0]).unwrap();

            assert_eq!(index.len(), 3);
            assert!(index.contains(1));
            assert!(index.contains(2));
            assert!(index.contains(3));
            assert!(!index.contains(4));

            // Search for vector similar to [1, 0, 0, 0]
            let results = index.search(&[1.0, 0.0, 0.0, 0.0], 2).unwrap();
            assert!(!results.is_empty());
            assert_eq!(results[0].chunk_id, 1);
        }

        #[test]
        fn test_hnsw_remove() {
            let config = HnswConfig::with_dimensions(4);
            let mut index = HnswIndex::new(&config).unwrap();

            index.add(1, &[1.0, 0.0, 0.0, 0.0]).unwrap();
            index.add(2, &[0.0, 1.0, 0.0, 0.0]).unwrap();

            assert_eq!(index.len(), 2);

            let removed = index.remove(1).unwrap();
            assert!(removed);
            assert_eq!(index.len(), 1);
            assert!(!index.contains(1));

            let removed_again = index.remove(1).unwrap();
            assert!(!removed_again);
        }

        #[test]
        fn test_hnsw_dimension_mismatch() {
            let config = HnswConfig::with_dimensions(4);
            let mut index = HnswIndex::new(&config).unwrap();

            // Try to add wrong dimensions
            let result = index.add(1, &[1.0, 0.0]); // Only 2 dimensions
            assert!(result.is_err());
        }

        #[test]
        fn test_hnsw_clear() {
            let config = HnswConfig::with_dimensions(4);
            let mut index = HnswIndex::new(&config).unwrap();

            index.add(1, &[1.0, 0.0, 0.0, 0.0]).unwrap();
            index.add(2, &[0.0, 1.0, 0.0, 0.0]).unwrap();
            assert_eq!(index.len(), 2);

            index.clear().unwrap();
            assert!(index.is_empty());
        }

        #[test]
        fn test_hnsw_update_existing() {
            let config = HnswConfig::with_dimensions(4);
            let mut index = HnswIndex::new(&config).unwrap();

            // Add initial vector
            index.add(1, &[1.0, 0.0, 0.0, 0.0]).unwrap();

            // Update with new vector
            index.add(1, &[0.0, 1.0, 0.0, 0.0]).unwrap();

            // Should still only have 1 entry
            assert_eq!(index.len(), 1);

            // Search should find the updated vector
            let results = index.search(&[0.0, 1.0, 0.0, 0.0], 1).unwrap();
            assert_eq!(results[0].chunk_id, 1);
        }

        #[test]
        fn test_hnsw_save_load() {
            use tempfile::TempDir;

            let temp_dir = TempDir::new().unwrap();
            let index_path = temp_dir.path().join("test.index");

            let config = HnswConfig::with_dimensions(4);

            // Create and populate index
            {
                let mut index = HnswIndex::new(&config).unwrap();
                index.add(1, &[1.0, 0.0, 0.0, 0.0]).unwrap();
                index.add(2, &[0.0, 1.0, 0.0, 0.0]).unwrap();
                index.save(&index_path).unwrap();
            }

            // Load and verify
            {
                let index = HnswIndex::load(&index_path, &config).unwrap();
                assert_eq!(index.len(), 2);
                assert!(index.contains(1));
                assert!(index.contains(2));
            }
        }
    }
}