leindex 1.6.0

LeIndex MCP and semantic code search engine for AI tools and large codebases
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
// Turso configuration for hybrid storage
//
// This module provides configuration for Turso/libsql hybrid storage,
// combining local SQLite with remote Turso vector store capabilities.

use serde::{Deserialize, Serialize};
use thiserror::Error;

/// Turso configuration
///
/// Configures the connection to Turso (remote libsql database) and
/// controls the hybrid storage behavior.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TursoConfig {
    /// Database URL (e.g., libsql://token@db.turso.io)
    /// For local-only mode, use "file:local.db"
    pub database_url: String,

    /// Auth token for Turso
    /// Empty string for local-only mode
    pub auth_token: String,

    /// Enable vector extension in Turso
    /// When true, enables the vec0 extension for vector similarity search
    pub enable_vectors: bool,

    /// Remote-only mode (no local SQLite)
    /// When true, only uses Turso for all storage
    pub remote_only: bool,
}

impl Default for TursoConfig {
    fn default() -> Self {
        Self {
            database_url: "file:local.db".to_string(),
            auth_token: String::new(),
            enable_vectors: false,
            remote_only: false,
        }
    }
}

impl TursoConfig {
    /// Create a new Turso config
    #[must_use]
    pub fn new(database_url: String, auth_token: String) -> Self {
        Self {
            database_url,
            auth_token,
            enable_vectors: false,
            remote_only: false,
        }
    }

    /// Create local-only config
    #[must_use]
    pub fn local_only() -> Self {
        Self {
            database_url: "file:local.db".to_string(),
            auth_token: String::new(),
            enable_vectors: false,
            remote_only: false,
        }
    }

    /// Create remote-only config
    #[must_use]
    pub fn remote_only(database_url: String, auth_token: String) -> Self {
        Self {
            database_url,
            auth_token,
            enable_vectors: false,
            remote_only: true,
        }
    }

    /// Create hybrid config (local + remote)
    #[must_use]
    pub fn hybrid(database_url: String, auth_token: String) -> Self {
        Self {
            database_url,
            auth_token,
            enable_vectors: false,
            remote_only: false,
        }
    }

    /// Enable vector extension
    #[must_use]
    pub fn with_vectors(mut self, enable: bool) -> Self {
        self.enable_vectors = enable;
        self
    }

    /// Check if this is a local-only configuration
    #[must_use]
    pub fn is_local_only(&self) -> bool {
        self.database_url.starts_with("file:") || self.auth_token.is_empty()
    }

    /// Check if this is a remote configuration
    #[must_use]
    pub fn is_remote(&self) -> bool {
        !self.is_local_only()
    }
}

/// Migration statistics
///
/// Tracks the progress and results of a migration operation.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct MigrationStats {
    /// Number of nodes migrated
    pub nodes_migrated: usize,

    /// Number of edges migrated
    pub edges_migrated: usize,

    /// Number of embeddings migrated
    pub embeddings_migrated: usize,

    /// Time taken for migration (milliseconds)
    pub migration_time_ms: u64,
}

/// Storage mode
///
/// Indicates the current storage mode of the HybridStorage.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StorageMode {
    /// No storage backend is configured or available
    None,
    /// Only local SQLite storage is being used
    LocalOnly,
    /// Only remote Turso storage is being used
    RemoteOnly,
    /// Both local and remote storage are being used in a hybrid configuration
    Hybrid,
}

/// Storage errors
///
/// Errors that can occur when working with HybridStorage.
#[derive(Debug, Error)]
pub enum StorageError {
    /// Failed to connect to the storage backend
    #[error("Connection failed: {0}")]
    ConnectionFailed(String),

    /// Failed to migrate data between storage backends
    #[error("Migration failed: {0}")]
    MigrationFailed(String),

    /// The vector search extension is not available on the remote backend
    #[error("Vector extension not available")]
    VectorExtensionNotAvailable,

    /// An error occurred in the local SQLite storage
    #[error("Local storage error: {0}")]
    LocalStorageError(String),

    /// A query executed on the remote backend failed
    #[error("Remote query failed: {0}")]
    RemoteQueryFailed(String),
}

/// Hybrid storage: local SQLite + remote Turso
///
/// Combines local SQLite storage with optional remote Turso storage.
/// This enables:
/// - Local-first operation for fast development
/// - Optional remote storage for production scale
/// - Vector similarity search via Turso's vec0 extension
/// - Migration from local to remote
pub struct HybridStorage {
    /// Local SQLite storage
    pub local: Option<crate::storage::Storage>,

    /// Configuration
    pub config: TursoConfig,

    /// Whether vector extension is initialized
    pub vectors_initialized: bool,
}

impl HybridStorage {
    /// Create hybrid storage from configuration
    ///
    /// # Arguments
    ///
    /// * `config` - Turso configuration
    ///
    /// # Returns
    ///
    /// `Ok(HybridStorage)` if successful, `Err(StorageError)` if connection fails
    ///
    /// # Example
    ///
    /// ```ignore
    /// let config = TursoConfig::local_only();
    /// let storage = HybridStorage::new(config)?;
    /// ```
    pub fn new(config: TursoConfig) -> Result<Self, StorageError> {
        // Initialize local storage if not remote-only
        let local = if !config.remote_only {
            // Create a temporary path for local storage
            let storage = crate::storage::Storage::open("local.db")
                .map_err(|e| StorageError::LocalStorageError(format!("{:?}", e)))?;
            Some(storage)
        } else {
            None
        };

        Ok(Self {
            local,
            config,
            vectors_initialized: false,
        })
    }

    /// Initialize vector extension in Turso
    ///
    /// This enables the vec0 extension for vector similarity search.
    /// For local SQLite, this sets up vector tables. For remote Turso,
    /// this loads the vec0 extension.
    ///
    /// # Returns
    ///
    /// `Ok(())` if successful, `Err(StorageError)` if initialization fails
    pub fn init_vectors(&mut self) -> Result<(), StorageError> {
        if !self.config.enable_vectors {
            return Ok(());
        }

        // For local storage, set up vector tables
        if let Some(storage) = &self.local {
            self.init_local_vectors(storage)?;
        }

        self.vectors_initialized = true;
        tracing::info!("Vector extension initialized successfully");

        Ok(())
    }

    /// Initialize vector tables in local SQLite storage
    fn init_local_vectors(&self, storage: &crate::storage::Storage) -> Result<(), StorageError> {
        let conn = storage.conn();

        // Create node metadata table
        conn.execute(
            "CREATE TABLE IF NOT EXISTS node_metadata (
                node_id TEXT PRIMARY KEY,
                symbol_name TEXT NOT NULL,
                file_path TEXT NOT NULL,
                node_type TEXT NOT NULL,
                created_at INTEGER DEFAULT (strftime('%s', 'now'))
            )",
            [],
        )
        .map_err(|e| {
            StorageError::LocalStorageError(format!(
                "Failed to create node_metadata table: {:?}",
                e
            ))
        })?;

        // Create embeddings table
        conn.execute(
            "CREATE TABLE IF NOT EXISTS node_embeddings (
                node_id TEXT PRIMARY KEY,
                embedding BLOB NOT NULL,
                dimension INTEGER NOT NULL,
                FOREIGN KEY (node_id) REFERENCES node_metadata(node_id) ON DELETE CASCADE
            )",
            [],
        )
        .map_err(|e| {
            StorageError::LocalStorageError(format!(
                "Failed to create node_embeddings table: {:?}",
                e
            ))
        })?;

        // Create index for similarity search (using FTS5-style approach)
        conn.execute(
            "CREATE INDEX IF NOT EXISTS idx_node_embeddings_dimension
             ON node_embeddings(dimension)",
            [],
        )
        .map_err(|e| StorageError::LocalStorageError(format!("Failed to create index: {:?}", e)))?;

        Ok(())
    }

    /// Store an embedding in the vector database
    ///
    /// # Arguments
    ///
    /// * `node_id` - Unique identifier for the node
    /// * `symbol_name` - Symbol name (function/class/variable name)
    /// * `file_path` - Path to the source file
    /// * `node_type` - Type of node (function, class, etc.)
    /// * `embedding` - 768-dimensional embedding vector
    ///
    /// # Returns
    ///
    /// `Ok(())` if successful, `Err(StorageError)` if storage fails
    pub fn store_embedding(
        &self,
        node_id: &str,
        symbol_name: &str,
        file_path: &str,
        node_type: &str,
        embedding: &[f32],
    ) -> Result<(), StorageError> {
        if !self.vectors_initialized {
            return Err(StorageError::VectorExtensionNotAvailable);
        }

        let storage = self
            .local
            .as_ref()
            .ok_or(StorageError::VectorExtensionNotAvailable)?;

        self.store_local_embedding(
            storage,
            node_id,
            symbol_name,
            file_path,
            node_type,
            embedding,
        )
    }

    /// Store embedding in local SQLite
    fn store_local_embedding(
        &self,
        storage: &crate::storage::Storage,
        node_id: &str,
        symbol_name: &str,
        file_path: &str,
        node_type: &str,
        embedding: &[f32],
    ) -> Result<(), StorageError> {
        use rusqlite::params;

        // Check embedding dimension
        if embedding.len() != 768 {
            return Err(StorageError::LocalStorageError(format!(
                "Invalid embedding dimension: {}, expected 768",
                embedding.len()
            )));
        }

        let conn = storage.conn();

        // Insert or replace node metadata
        conn.execute(
            "INSERT OR REPLACE INTO node_metadata (node_id, symbol_name, file_path, node_type)
             VALUES (?1, ?2, ?3, ?4)",
            params![node_id, symbol_name, file_path, node_type],
        )
        .map_err(|e| {
            StorageError::LocalStorageError(format!("Failed to insert metadata: {:?}", e))
        })?;

        // Convert embedding to bytes for storage
        let embedding_bytes: Vec<u8> = embedding.iter().flat_map(|v| v.to_le_bytes()).collect();

        // Insert embedding
        conn.execute(
            "INSERT OR REPLACE INTO node_embeddings (node_id, embedding, dimension)
             VALUES (?1, ?2, ?3)",
            params![node_id, embedding_bytes, embedding.len() as i32],
        )
        .map_err(|e| {
            StorageError::LocalStorageError(format!("Failed to insert embedding: {:?}", e))
        })?;

        Ok(())
    }

    /// Search for similar vectors
    ///
    /// # Arguments
    ///
    /// * `query_embedding` - Query embedding vector (768 dimensions)
    /// * `k` - Number of results to return
    ///
    /// # Returns
    ///
    /// Vector of (node_id, similarity_score) tuples
    pub fn search_similar(
        &self,
        query_embedding: &[f32],
        k: usize,
    ) -> Result<Vec<(String, f32)>, StorageError> {
        if !self.vectors_initialized {
            return Err(StorageError::VectorExtensionNotAvailable);
        }

        let storage = self
            .local
            .as_ref()
            .ok_or(StorageError::VectorExtensionNotAvailable)?;

        self.search_local_similar(storage, query_embedding, k)
    }

    /// Search for similar vectors in local SQLite
    fn search_local_similar(
        &self,
        storage: &crate::storage::Storage,
        query_embedding: &[f32],
        k: usize,
    ) -> Result<Vec<(String, f32)>, StorageError> {
        use rusqlite::Row;

        if query_embedding.len() != 768 {
            return Err(StorageError::LocalStorageError(format!(
                "Invalid query embedding dimension: {}, expected 768",
                query_embedding.len()
            )));
        }

        let conn = storage.conn();

        // Get all embeddings
        let mut stmt = conn
            .prepare(
                "SELECT n.node_id, e.embedding
             FROM node_embeddings e
             JOIN node_metadata n ON e.node_id = n.node_id
             WHERE e.dimension = 768",
            )
            .map_err(|e| {
                StorageError::LocalStorageError(format!("Failed to prepare query: {:?}", e))
            })?;

        let rows = stmt
            .query_map([], |row: &Row<'_>| {
                let node_id: String = row.get(0)?;
                let embedding_bytes: Vec<u8> = row.get(1)?;
                Ok((node_id, embedding_bytes))
            })
            .map_err(|e| {
                StorageError::LocalStorageError(format!("Failed to execute query: {:?}", e))
            })?;

        // Calculate cosine similarities
        let mut results: Vec<(String, f32)> = Vec::new();
        for row in rows {
            let (node_id, embedding_bytes) = row.map_err(|e| {
                StorageError::LocalStorageError(format!("Failed to read row: {:?}", e))
            })?;

            // Convert bytes back to f32 vector
            let stored_embedding: Vec<f32> = embedding_bytes
                .chunks_exact(4)
                .map(|chunk| f32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]))
                .collect();

            // Calculate cosine similarity
            let similarity = cosine_similarity(query_embedding, &stored_embedding);
            results.push((node_id, similarity));
        }

        // Sort by similarity (descending) and take top k
        results.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        results.truncate(k);

        Ok(results)
    }

    /// Batch store embeddings
    ///
    /// # Arguments
    ///
    /// * `embeddings` - Vector of (node_id, symbol_name, file_path, node_type, embedding) tuples
    ///
    /// # Returns
    ///
    /// Number of embeddings stored successfully
    pub fn batch_store_embeddings(
        &self,
        embeddings: &[(&str, &str, &str, &str, &[f32])],
    ) -> Result<usize, StorageError> {
        if !self.vectors_initialized {
            return Err(StorageError::VectorExtensionNotAvailable);
        }

        let storage = self
            .local
            .as_ref()
            .ok_or(StorageError::VectorExtensionNotAvailable)?;

        let mut stored = 0;
        for (node_id, symbol_name, file_path, node_type, embedding) in embeddings {
            if self
                .store_local_embedding(
                    storage,
                    node_id,
                    symbol_name,
                    file_path,
                    node_type,
                    embedding,
                )
                .is_ok()
            {
                stored += 1;
            }
        }

        Ok(stored)
    }

    /// Get local storage
    ///
    /// # Returns
    ///
    /// `Some(&Storage)` if local storage is available, `None` otherwise
    #[must_use]
    pub fn local(&self) -> Option<&crate::storage::Storage> {
        self.local.as_ref()
    }

    /// Get mutable local storage
    ///
    /// # Returns
    ///
    /// `Some(&mut Storage)` if local storage is available, `None` otherwise
    pub fn local_mut(&mut self) -> Option<&mut crate::storage::Storage> {
        self.local.as_mut()
    }

    /// Migrate data from local to remote
    ///
    /// This is a placeholder for migration functionality.
    /// The actual implementation will be provided in Task 8.3 (Vector Migration Bridge).
    /// For now, this returns empty migration stats to allow compilation.
    ///
    /// # Returns
    ///
    /// `Ok(MigrationStats)` with migration statistics (empty for now)
    ///
    /// # Example
    ///
    /// ```ignore
    /// let stats = storage.migrate_to_remote()?;
    /// println!("Migrated {} nodes", stats.nodes_migrated);
    /// ```
    pub fn migrate_to_remote(&self) -> Result<MigrationStats, StorageError> {
        // Placeholder implementation - actual migration will be in Task 8.3
        Ok(MigrationStats::default())
    }

    /// Check if local storage is available
    #[must_use]
    pub fn has_local(&self) -> bool {
        self.local.is_some()
    }

    /// Check if remote storage is available
    #[must_use]
    pub fn has_remote(&self) -> bool {
        self.config.is_remote()
    }

    /// Get storage mode
    #[must_use]
    pub fn mode(&self) -> StorageMode {
        match (self.local.is_some(), self.config.is_remote()) {
            (true, false) => StorageMode::LocalOnly,
            (false, true) => StorageMode::RemoteOnly,
            (true, true) => StorageMode::Hybrid,
            (false, false) => StorageMode::None,
        }
    }
}

/// Calculate cosine similarity between two vectors
fn cosine_similarity(a: &[f32], b: &[f32]) -> f32 {
    if a.len() != b.len() || a.is_empty() {
        return 0.0;
    }

    let dot_product: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();

    let norm_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
    let norm_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();

    if norm_a == 0.0 || norm_b == 0.0 {
        return 0.0;
    }

    dot_product / (norm_a * norm_b)
}

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

    #[test]
    fn test_turso_config_default() {
        let config = TursoConfig::default();
        assert_eq!(config.database_url, "file:local.db");
        assert!(config.auth_token.is_empty());
        assert!(!config.enable_vectors);
        assert!(!config.remote_only);
    }

    #[test]
    fn test_turso_config_local_only() {
        let config = TursoConfig::local_only();
        assert!(config.is_local_only());
        assert!(!config.is_remote());
    }

    #[test]
    fn test_turso_config_remote_only() {
        let config = TursoConfig::remote_only(
            "libsql://token@db.turso.io".to_string(),
            "auth_token".to_string(),
        );
        assert!(config.is_remote());
        assert!(!config.is_local_only());
        assert!(config.remote_only);
    }

    #[test]
    fn test_turso_config_hybrid() {
        let config = TursoConfig::hybrid(
            "libsql://token@db.turso.io".to_string(),
            "auth_token".to_string(),
        );
        assert!(config.is_remote());
        assert!(!config.is_local_only());
        assert!(!config.remote_only);
    }

    #[test]
    fn test_turso_config_with_vectors() {
        let config = TursoConfig::local_only().with_vectors(true);
        assert!(config.enable_vectors);
    }

    #[test]
    fn test_migration_stats_default() {
        let stats = MigrationStats::default();
        assert_eq!(stats.nodes_migrated, 0);
        assert_eq!(stats.edges_migrated, 0);
        assert_eq!(stats.embeddings_migrated, 0);
        assert_eq!(stats.migration_time_ms, 0);
    }

    #[test]
    fn test_hybrid_storage_local_only() {
        let config = TursoConfig::local_only();
        let storage = HybridStorage::new(config);
        assert!(storage.is_ok());
        let storage = storage.unwrap();
        assert!(storage.has_local());
        assert!(!storage.has_remote());
        assert_eq!(storage.mode(), StorageMode::LocalOnly);
    }

    #[test]
    fn test_hybrid_storage_remote_only_fails_without_url() {
        let config = TursoConfig::remote_only("".to_string(), "".to_string());
        // This should fail since it's not a valid remote URL
        let result = HybridStorage::new(config);
        // The connection will fail since there's no actual Turso server
        // but the struct should be created with remote = None
        assert!(result.is_ok());
        let storage = result.unwrap();
        assert!(!storage.has_local());
        // Since the URL is empty, is_remote() returns false
        assert!(!storage.has_remote());
        assert_eq!(storage.mode(), StorageMode::None);
    }

    #[test]
    fn test_storage_mode_display() {
        assert_eq!(format!("{:?}", StorageMode::LocalOnly), "LocalOnly");
        assert_eq!(format!("{:?}", StorageMode::RemoteOnly), "RemoteOnly");
        assert_eq!(format!("{:?}", StorageMode::Hybrid), "Hybrid");
        assert_eq!(format!("{:?}", StorageMode::None), "None");
    }

    #[test]
    fn test_turso_config_is_local_only() {
        let config = TursoConfig::local_only();
        assert!(config.is_local_only());

        let config = TursoConfig {
            database_url: "file:test.db".to_string(),
            ..Default::default()
        };
        assert!(config.is_local_only());
    }

    #[test]
    fn test_turso_config_is_remote() {
        let config = TursoConfig {
            database_url: "libsql://token@db.turso.io".to_string(),
            auth_token: "some_token".to_string(),
            ..Default::default()
        };
        assert!(config.is_remote());
        assert!(!config.is_local_only());
    }

    #[test]
    fn test_storage_error_messages() {
        let err = StorageError::ConnectionFailed("test".to_string());
        assert_eq!(format!("{}", err), "Connection failed: test");

        let err = StorageError::MigrationFailed("test".to_string());
        assert_eq!(format!("{}", err), "Migration failed: test");

        let err = StorageError::VectorExtensionNotAvailable;
        assert_eq!(format!("{}", err), "Vector extension not available");
    }

    #[test]
    fn test_cosine_similarity() {
        let a = vec![1.0, 2.0, 3.0];
        let b = vec![1.0, 2.0, 3.0];
        let sim = cosine_similarity(&a, &b);
        assert!((sim - 1.0).abs() < 0.001);

        let c = vec![1.0, 0.0, 0.0];
        let d = vec![0.0, 1.0, 0.0];
        let sim = cosine_similarity(&c, &d);
        assert!((sim - 0.0).abs() < 0.001);

        let e: Vec<f32> = vec![];
        let sim = cosine_similarity(&a, &e);
        assert_eq!(sim, 0.0);
    }

    #[test]
    fn test_cosine_similarity_parallel() {
        let a = vec![1.0, 2.0, 3.0, 4.0];
        let b = vec![2.0, 4.0, 6.0, 8.0];
        let sim = cosine_similarity(&a, &b);
        // b is 2*a, so they should be perfectly similar
        assert!((sim - 1.0).abs() < 0.001);
    }
}