lens-core 1.0.0

High-performance code search engine with LSP integration and benchmarking
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
//! Pinned Dataset Loader Implementation
//!
//! Handles loading and validation of pinned golden datasets for consistent benchmarking.
//! Supports the format described in CLAUDE.md with 390 golden queries in version
//! "08653c1e-2025-09-01T21-51-35-302Z".

use super::types::{
    PinnedDataset, DatasetMetadata, DatasetVersion, ValidationResult, 
    InconsistentQuery, LoadingError, BenchmarkConfig, GoldenQuery
};
use anyhow::{anyhow, Result};
use serde_json;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tokio::fs;
use tokio::sync::RwLock;
use tracing::{info, warn, error, debug};
use chrono::{DateTime, Utc};

/// Pinned dataset loader with caching and validation capabilities
pub struct PinnedDatasetLoader {
    config: BenchmarkConfig,
    dataset_cache: Arc<RwLock<HashMap<String, Arc<PinnedDataset>>>>,
    available_versions: Arc<RwLock<Vec<DatasetVersion>>>,
}

impl PinnedDatasetLoader {
    /// Create new loader with default configuration
    pub async fn new() -> Result<Self> {
        let config = BenchmarkConfig::default();
        Self::with_config(config).await
    }

    /// Create loader with custom configuration
    pub async fn with_config(config: BenchmarkConfig) -> Result<Self> {
        info!("๐Ÿ”ง Initializing pinned dataset loader");
        debug!("Dataset path: {}", config.dataset_path);

        let loader = Self {
            config,
            dataset_cache: Arc::new(RwLock::new(HashMap::new())),
            available_versions: Arc::new(RwLock::new(Vec::new())),
        };

        // Discover available datasets if enabled
        if loader.config.auto_discover_datasets {
            if let Err(e) = loader.discover_available_versions().await {
                warn!("Failed to discover datasets: {}", e);
                // Continue without discovery - we'll try direct loading
            }
        }

        info!("โœ… Pinned dataset loader initialized");
        Ok(loader)
    }

    /// Load the current pinned dataset (golden-pinned-current.json or default version)
    pub async fn load_current_pinned_dataset(&self) -> Result<PinnedDataset> {
        debug!("๐Ÿ” Loading current pinned dataset");

        // Try to load current dataset symlink first
        let current_path = PathBuf::from(&self.config.dataset_path)
            .join("golden-pinned-current.json");

        if current_path.exists() {
            info!("๐Ÿ“„ Loading current dataset from: {}", current_path.display());
            return self.load_dataset_from_file(&current_path).await;
        }

        // Fall back to default version
        if let Some(ref default_version) = self.config.default_version {
            info!("๐Ÿ“„ Loading default version: {}", default_version);
            return self.load_pinned_dataset_version(default_version).await;
        }

        // Try to find any available pinned dataset
        let versions = self.list_available_versions().await?;
        if !versions.is_empty() {
            let latest = &versions[0]; // Assume first is latest
            info!("๐Ÿ“„ Loading latest available version: {}", latest.version);
            return self.load_dataset_from_file(&latest.file_path).await;
        }

        // Last resort: create a mock dataset for development
        warn!("โš ๏ธ No pinned datasets found, creating mock dataset for development");
        Ok(self.create_mock_dataset())
    }

    /// Load specific pinned dataset version
    pub async fn load_pinned_dataset_version(&self, version: &str) -> Result<PinnedDataset> {
        debug!("๐Ÿ” Loading pinned dataset version: {}", version);

        // Check cache first if caching is enabled
        if self.config.enable_caching {
            let cache = self.dataset_cache.read().await;
            if let Some(cached) = cache.get(version) {
                info!("๐Ÿ’พ Loaded dataset from cache: {}", version);
                return Ok((**cached).clone());
            }
        }

        // Construct file path for the version
        let filename = format!("golden-pinned-{}.json", version);
        let file_path = PathBuf::from(&self.config.dataset_path).join(&filename);

        if !file_path.exists() {
            return Err(anyhow!(LoadingError::VersionNotFound {
                version: version.to_string()
            }));
        }

        info!("๐Ÿ“„ Loading dataset from: {}", file_path.display());
        let dataset = self.load_dataset_from_file(&file_path).await?;

        // Cache the loaded dataset if caching is enabled
        if self.config.enable_caching {
            self.cache_dataset(version, &dataset).await;
        }

        Ok(dataset)
    }

    /// Load dataset from a specific file path
    async fn load_dataset_from_file(&self, file_path: &Path) -> Result<PinnedDataset> {
        let start_time = std::time::Instant::now();

        let content = fs::read_to_string(file_path).await
            .map_err(|e| anyhow!(LoadingError::IoError { source: e }))?;

        // Try to parse as PinnedDataset format first
        if let Ok(dataset) = serde_json::from_str::<PinnedDataset>(&content) {
            let duration = start_time.elapsed();
            info!("โœ… Loaded pinned dataset: {} queries in {:?}", 
                  dataset.queries.len(), duration);
            return Ok(dataset);
        }

        // Fall back to simple JSON array format (legacy compatibility)
        match serde_json::from_str::<Vec<GoldenQuery>>(&content) {
            Ok(queries) => {
                info!("๐Ÿ“ Converting legacy dataset format to PinnedDataset");
                let dataset = self.convert_legacy_to_pinned(queries, file_path)?;
                
                let duration = start_time.elapsed();
                info!("โœ… Converted and loaded dataset: {} queries in {:?}", 
                      dataset.queries.len(), duration);
                Ok(dataset)
            }
            Err(e) => {
                error!("โŒ Failed to parse dataset from {}: {}", file_path.display(), e);
                Err(anyhow!(LoadingError::JsonParseError { source: e }))
            }
        }
    }

    /// Convert legacy query array format to full PinnedDataset
    fn convert_legacy_to_pinned(&self, queries: Vec<GoldenQuery>, file_path: &Path) -> Result<PinnedDataset> {
        let version = file_path.file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or("unknown")
            .replace("golden-pinned-", "");

        let mut query_distribution = HashMap::new();
        let mut languages = std::collections::HashSet::new();

        for query in &queries {
            *query_distribution.entry(query.query_type.clone()).or_insert(0) += 1;
            if let Some(ref lang) = query.language {
                languages.insert(lang.clone());
            }
        }

        let metadata = DatasetMetadata {
            version: version.clone(),
            name: "storyviz-pinned".to_string(),
            created_at: Utc::now(),
            total_queries: queries.len(),
            query_distribution,
            languages: languages.into_iter().collect(),
            git_sha: None,
            additional_metadata: HashMap::new(),
        };

        // Create basic slices
        let mut slices = HashMap::new();
        let all_indices: Vec<usize> = (0..queries.len()).collect();
        slices.insert("ALL".to_string(), all_indices.clone());
        
        // Create SMOKE_DEFAULT slice (first 40 queries as mentioned in CLAUDE.md)
        let smoke_size = std::cmp::min(40, queries.len());
        slices.insert("SMOKE_DEFAULT".to_string(), (0..smoke_size).collect());

        Ok(PinnedDataset {
            metadata,
            queries,
            slices,
            corpus_info: None,
        })
    }

    /// Validate dataset consistency against corpus
    pub async fn validate_dataset_consistency(&self, dataset: &PinnedDataset) -> Result<ValidationResult> {
        info!("๐Ÿ” Validating corpus consistency for {} queries", dataset.queries.len());
        
        let mut valid_queries = 0;
        let mut inconsistent_queries = Vec::new();

        // Check for indexed-content or similar corpus directory
        let corpus_paths = [
            "indexed-content",
            "benchmark-corpus", 
            "src",
            "rust-core/src"
        ];

        let corpus_path = corpus_paths.iter()
            .find(|path| std::path::Path::new(path).exists())
            .map(|&s| s.to_string());

        if corpus_path.is_none() {
            warn!("โš ๏ธ No corpus directory found for validation");
            return Ok(ValidationResult {
                is_consistent: false,
                valid_queries: 0,
                total_queries: dataset.queries.len(),
                inconsistent_queries,
                validated_at: Utc::now(),
            });
        }

        let corpus_path = corpus_path.unwrap();
        debug!("๐Ÿ“ Using corpus path: {}", corpus_path);

        // Validate each query
        for query in &dataset.queries {
            let mut query_valid = true;
            let mut missing_files = Vec::new();

            // Check if expected files exist in corpus
            for expected_file in &query.expected_files {
                // Handle different path formats
                let file_paths_to_check = vec![
                    PathBuf::from(&corpus_path).join(expected_file),
                    PathBuf::from(expected_file),
                    PathBuf::from(&corpus_path).join(
                        expected_file.strip_prefix("indexed-content/").unwrap_or(expected_file)
                    ),
                ];

                let file_exists = file_paths_to_check.iter().any(|path| path.exists());
                
                if !file_exists {
                    query_valid = false;
                    missing_files.push(expected_file.clone());
                }
            }

            if query_valid {
                valid_queries += 1;
            } else {
                inconsistent_queries.push(InconsistentQuery {
                    query: query.clone(),
                    failure_reason: "Expected files not found in corpus".to_string(),
                    missing_files,
                });
            }
        }

        let is_consistent = valid_queries == dataset.queries.len();
        let consistency_rate = valid_queries as f64 / dataset.queries.len() as f64 * 100.0;

        if is_consistent {
            info!("โœ… Perfect corpus consistency: {}/{} queries (100%)", 
                  valid_queries, dataset.queries.len());
        } else {
            warn!("โš ๏ธ Partial corpus consistency: {}/{} queries ({:.1}%)", 
                  valid_queries, dataset.queries.len(), consistency_rate);
        }

        Ok(ValidationResult {
            is_consistent,
            valid_queries,
            total_queries: dataset.queries.len(),
            inconsistent_queries,
            validated_at: Utc::now(),
        })
    }

    /// Discover available dataset versions in the dataset directory
    async fn discover_available_versions(&self) -> Result<()> {
        debug!("๐Ÿ” Discovering available dataset versions");
        
        let dataset_dir = Path::new(&self.config.dataset_path);
        if !dataset_dir.exists() {
            warn!("Dataset directory does not exist: {}", dataset_dir.display());
            return Ok(());
        }

        let mut entries = fs::read_dir(dataset_dir).await?;
        let mut versions = Vec::new();

        while let Some(entry) = entries.next_entry().await? {
            let path = entry.path();
            let filename = path.file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("");

            // Look for pinned dataset files
            if filename.starts_with("golden-pinned-") && filename.ends_with(".json") {
                let version = filename
                    .strip_prefix("golden-pinned-")
                    .and_then(|s| s.strip_suffix(".json"))
                    .unwrap_or("unknown")
                    .to_string();

                if version != "current" { // Skip the symlink
                    let metadata = entry.metadata().await?;
                    let created_at = metadata.created().ok()
                        .and_then(|t| DateTime::from_timestamp(
                            t.duration_since(std::time::UNIX_EPOCH).ok()?.as_secs() as i64, 0
                        ))
                        .unwrap_or_else(|| Utc::now());

                    // Try to get query count by loading the dataset
                    let query_count = match self.load_dataset_from_file(&path).await {
                        Ok(dataset) => dataset.queries.len(),
                        Err(_) => 0,
                    };

                    versions.push(DatasetVersion {
                        version: version.clone(),
                        created_at,
                        query_count,
                        file_path: path,
                        is_current: version == self.config.default_version.as_deref().unwrap_or(""),
                    });
                }
            }
        }

        // Sort by creation time (newest first)
        versions.sort_by(|a, b| b.created_at.cmp(&a.created_at));

        let count = versions.len();
        *self.available_versions.write().await = versions;
        
        info!("๐Ÿ“Š Discovered {} dataset versions", count);
        Ok(())
    }

    /// List available dataset versions
    pub async fn list_available_versions(&self) -> Result<Vec<DatasetVersion>> {
        Ok(self.available_versions.read().await.clone())
    }

    /// Cache a dataset in memory
    async fn cache_dataset(&self, version: &str, dataset: &PinnedDataset) {
        if !self.config.enable_caching {
            return;
        }

        let mut cache = self.dataset_cache.write().await;
        
        // Enforce cache size limit
        if cache.len() >= self.config.max_cache_size {
            // Remove oldest entry (simple LRU approximation)
            if let Some(oldest_key) = cache.keys().next().cloned() {
                cache.remove(&oldest_key);
            }
        }

        cache.insert(version.to_string(), Arc::new(dataset.clone()));
        debug!("๐Ÿ’พ Cached dataset version: {}", version);
    }

    /// Create a mock dataset for development when no pinned datasets exist
    fn create_mock_dataset(&self) -> PinnedDataset {
        let mock_queries = vec![
            GoldenQuery {
                query: "function".to_string(),
                expected_files: vec!["src/main.rs".to_string()],
                query_type: super::types::QueryType::Identifier,
                metadata: HashMap::new(),
                language: Some("rust".to_string()),
                confidence: Some(0.8),
            },
            GoldenQuery {
                query: "struct SearchEngine".to_string(),
                expected_files: vec!["src/search.rs".to_string()],
                query_type: super::types::QueryType::Identifier,
                metadata: HashMap::new(),
                language: Some("rust".to_string()),
                confidence: Some(0.9),
            },
        ];

        let mut query_distribution = HashMap::new();
        query_distribution.insert(super::types::QueryType::Identifier, mock_queries.len());

        let metadata = DatasetMetadata {
            version: "mock-dev".to_string(),
            name: "mock-development-dataset".to_string(),
            created_at: Utc::now(),
            total_queries: mock_queries.len(),
            query_distribution,
            languages: vec!["rust".to_string()],
            git_sha: None,
            additional_metadata: HashMap::new(),
        };

        let mut slices = HashMap::new();
        slices.insert("ALL".to_string(), vec![0, 1]);
        slices.insert("SMOKE_DEFAULT".to_string(), vec![0, 1]);

        PinnedDataset {
            metadata,
            queries: mock_queries,
            slices,
            corpus_info: None,
        }
    }

    /// Get dataset slice by name
    pub fn get_dataset_slice(&self, dataset: &PinnedDataset, slice_name: &str) -> Option<Vec<GoldenQuery>> {
        if let Some(indices) = dataset.slices.get(slice_name) {
            let slice_queries = indices.iter()
                .filter_map(|&idx| dataset.queries.get(idx).cloned())
                .collect();
            Some(slice_queries)
        } else {
            None
        }
    }

    /// Get SMOKE test dataset (subset for quick validation)
    pub fn get_smoke_dataset(&self, dataset: &PinnedDataset) -> Vec<GoldenQuery> {
        self.get_dataset_slice(dataset, "SMOKE_DEFAULT")
            .unwrap_or_else(|| {
                // Fallback: take first 40 queries
                let smoke_size = std::cmp::min(40, dataset.queries.len());
                dataset.queries.iter().take(smoke_size).cloned().collect()
            })
    }

    /// Clear dataset cache
    pub async fn clear_cache(&self) {
        let mut cache = self.dataset_cache.write().await;
        cache.clear();
        info!("๐Ÿงน Cleared dataset cache");
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;
    use tokio::fs::File;
    use tokio::io::AsyncWriteExt;

    async fn create_test_dataset_file(dir: &Path, filename: &str, queries: Vec<GoldenQuery>) -> Result<PathBuf> {
        let file_path = dir.join(filename);
        let mut file = File::create(&file_path).await?;
        
        // Extract version from filename (e.g., "golden-pinned-test-version.json" -> "test-version")
        let version = filename
            .strip_prefix("golden-pinned-")
            .and_then(|s| s.strip_suffix(".json"))
            .unwrap_or("test");
        
        let dataset = PinnedDataset {
            metadata: DatasetMetadata {
                version: version.to_string(),
                name: "test-dataset".to_string(),
                created_at: Utc::now(),
                total_queries: queries.len(),
                query_distribution: HashMap::new(),
                languages: vec!["rust".to_string()],
                git_sha: None,
                additional_metadata: HashMap::new(),
            },
            queries,
            slices: HashMap::new(),
            corpus_info: None,
        };

        let json = serde_json::to_string_pretty(&dataset)?;
        file.write_all(json.as_bytes()).await?;
        
        Ok(file_path)
    }

    #[tokio::test]
    async fn test_pinned_loader_creation() {
        let temp_dir = TempDir::new().unwrap();
        let config = BenchmarkConfig {
            dataset_path: temp_dir.path().to_string_lossy().to_string(),
            auto_discover_datasets: false,
            ..Default::default()
        };

        let loader = PinnedDatasetLoader::with_config(config).await.unwrap();
        assert!(!loader.config.dataset_path.is_empty());
    }

    #[tokio::test]
    async fn test_mock_dataset_creation() {
        let temp_dir = TempDir::new().unwrap();
        let config = BenchmarkConfig {
            dataset_path: temp_dir.path().to_string_lossy().to_string(),
            auto_discover_datasets: false,
            default_version: None,  // No default version in temp dir, should fall back to mock
            ..Default::default()
        };

        let loader = PinnedDatasetLoader::with_config(config).await.unwrap();
        let dataset = loader.load_current_pinned_dataset().await.unwrap();
        
        assert!(!dataset.queries.is_empty());
        assert_eq!(dataset.metadata.version, "mock-dev");
    }

    #[tokio::test]
    async fn test_dataset_loading_and_caching() {
        let temp_dir = TempDir::new().unwrap();
        let config = BenchmarkConfig {
            dataset_path: temp_dir.path().to_string_lossy().to_string(),
            enable_caching: true,
            auto_discover_datasets: false,
            default_version: None,  // No default version in temp dir
            ..Default::default()
        };

        // Create a test dataset file
        let test_queries = vec![
            GoldenQuery {
                query: "test query".to_string(),
                expected_files: vec!["test.rs".to_string()],
                query_type: super::super::types::QueryType::Identifier,
                metadata: HashMap::new(),
                language: None,
                confidence: None,
            }
        ];

        let _file_path = create_test_dataset_file(
            temp_dir.path(), 
            "golden-pinned-test-version.json", 
            test_queries
        ).await.unwrap();

        let loader = PinnedDatasetLoader::with_config(config).await.unwrap();
        
        // Load dataset twice - second should come from cache
        let dataset1 = loader.load_pinned_dataset_version("test-version").await.unwrap();
        let dataset2 = loader.load_pinned_dataset_version("test-version").await.unwrap();
        
        assert_eq!(dataset1.queries.len(), dataset2.queries.len());
        assert_eq!(dataset1.metadata.version, "test-version");
    }
}