turboprop 0.1.2

Fast semantic code search and indexing tool
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
//! Index recovery and validation functionality.
//!
//! This module provides utilities for detecting corrupted indices, cleaning them up,
//! and rebuilding them automatically when needed.

use crate::config::TurboPropConfig;
use crate::error::{TurboPropError, TurboPropResult};
use crate::index::PersistentChunkIndex;
use serde_json;
use std::fs;
use std::path::{Path, PathBuf};
use tracing::{debug, info, warn};

/// Strategy for recovering from index corruption.
#[derive(Debug, Clone, PartialEq)]
pub enum RecoveryStrategy {
    /// Clean up the corrupted index without rebuilding.
    Clean,
    /// Clean up and rebuild the index from source files.
    Rebuild,
    /// Try to repair in-place if possible, otherwise rebuild.
    Repair,
}

/// Result of index validation.
#[derive(Debug, Clone, PartialEq)]
pub enum ValidationResult {
    /// Index is healthy and ready to use.
    Healthy,
    /// Index is missing (needs to be created).
    Missing,
    /// Index is corrupted and needs recovery.
    Corrupted { issues: Vec<String> },
}

/// Index recovery coordinator.
pub struct IndexRecovery {
    index_path: PathBuf,
}

impl IndexRecovery {
    /// Create a new index recovery instance.
    pub fn new(index_path: &Path) -> Self {
        Self {
            index_path: index_path.to_path_buf(),
        }
    }

    /// Validate the index and detect any issues.
    pub async fn validate_index(&self) -> TurboPropResult<ValidationResult> {
        let turboprop_dir = self.index_path.join(".turboprop");

        // Check if index directory exists
        if !turboprop_dir.exists() {
            debug!("Index directory not found: {}", turboprop_dir.display());
            return Ok(ValidationResult::Missing);
        }

        let mut issues = Vec::new();

        // Check for required files
        let metadata_file = turboprop_dir.join("metadata.json");
        let chunks_file = turboprop_dir.join("chunks.db");
        let embeddings_file = turboprop_dir.join("embeddings.bin");

        if !metadata_file.exists() {
            issues.push("Missing metadata.json file".to_string());
        } else {
            // Validate metadata file
            if let Err(e) = self.validate_metadata_file(&metadata_file).await {
                issues.push(format!("Invalid metadata.json: {}", e));
            }
        }

        if !chunks_file.exists() {
            issues.push("Missing chunks.db file".to_string());
        }

        if !embeddings_file.exists() {
            issues.push("Missing embeddings.bin file".to_string());
        }

        // Check file permissions
        for file in &[&metadata_file, &chunks_file, &embeddings_file] {
            if file.exists() {
                match fs::metadata(file) {
                    Ok(metadata) => {
                        if metadata.len() == 0 {
                            issues.push(format!("Empty file: {}", file.display()));
                        }
                    }
                    Err(e) => {
                        issues.push(format!("Cannot access {}: {}", file.display(), e));
                    }
                }
            }
        }

        if issues.is_empty() {
            Ok(ValidationResult::Healthy)
        } else {
            Ok(ValidationResult::Corrupted { issues })
        }
    }

    /// Validate metadata file structure and content.
    async fn validate_metadata_file(&self, metadata_file: &Path) -> TurboPropResult<()> {
        let content = fs::read_to_string(metadata_file).map_err(|e| {
            TurboPropError::file_system(
                format!("Cannot read metadata file: {}", e),
                Some(metadata_file.to_path_buf()),
            )
        })?;

        let metadata: serde_json::Value = serde_json::from_str(&content).map_err(|e| {
            TurboPropError::corrupted_index(
                metadata_file.to_path_buf(),
                format!("Invalid JSON: {}", e),
            )
        })?;

        // Check required fields
        let required_fields = [
            "version",
            "created_at",
            "chunk_count",
            "embedding_dimensions",
        ];
        for field in &required_fields {
            if metadata.get(field).is_none() {
                return Err(TurboPropError::corrupted_index(
                    metadata_file.to_path_buf(),
                    format!("Missing required field: {}", field),
                ));
            }
        }

        Ok(())
    }

    /// Clean up the corrupted index.
    pub async fn cleanup_index(&self) -> TurboPropResult<()> {
        let turboprop_dir = self.index_path.join(".turboprop");

        if !turboprop_dir.exists() {
            debug!("Index directory already clean: {}", turboprop_dir.display());
            return Ok(());
        }

        info!(
            "Cleaning up corrupted index at: {}",
            turboprop_dir.display()
        );

        fs::remove_dir_all(&turboprop_dir).map_err(|e| {
            TurboPropError::file_system(
                format!("Failed to remove index directory: {}", e),
                Some(turboprop_dir),
            )
        })?;

        info!("Index cleanup completed");
        Ok(())
    }

    /// Create a backup of the corrupted index before cleaning.
    pub async fn backup_corrupted_index(&self) -> TurboPropResult<PathBuf> {
        let turboprop_dir = self.index_path.join(".turboprop");

        if !turboprop_dir.exists() {
            return Err(TurboPropError::index_not_found(turboprop_dir));
        }

        // Create backup directory name with timestamp
        let timestamp = chrono::Utc::now().format("%Y%m%d_%H%M%S");
        let backup_name = format!(".turboprop_backup_{}", timestamp);
        let backup_path = self.index_path.join(&backup_name);

        info!("Backing up corrupted index to: {}", backup_path.display());

        // Copy the corrupted index to backup location
        self.copy_dir_recursive(&turboprop_dir, &backup_path)
            .await?;

        // Remove the original corrupted index
        fs::remove_dir_all(&turboprop_dir).map_err(|e| {
            TurboPropError::file_system(
                format!("Failed to remove original index: {}", e),
                Some(turboprop_dir),
            )
        })?;

        info!("Backup created successfully at: {}", backup_path.display());
        Ok(backup_path)
    }

    /// Recursively copy a directory.
    async fn copy_dir_recursive(&self, src: &Path, dst: &Path) -> TurboPropResult<()> {
        fs::create_dir_all(dst).map_err(|e| {
            TurboPropError::file_system(
                format!("Failed to create backup directory: {}", e),
                Some(dst.to_path_buf()),
            )
        })?;

        for entry in fs::read_dir(src).map_err(|e| {
            TurboPropError::file_system(
                format!("Failed to read directory: {}", e),
                Some(src.to_path_buf()),
            )
        })? {
            let entry = entry.map_err(|e| {
                TurboPropError::file_system(
                    format!("Failed to read directory entry: {}", e),
                    Some(src.to_path_buf()),
                )
            })?;

            let src_path = entry.path();
            let dst_path = dst.join(entry.file_name());

            if src_path.is_dir() {
                Box::pin(self.copy_dir_recursive(&src_path, &dst_path)).await?;
            } else {
                fs::copy(&src_path, &dst_path).map_err(|e| {
                    TurboPropError::file_system(
                        format!("Failed to copy file: {}", e),
                        Some(src_path),
                    )
                })?;
            }
        }

        Ok(())
    }

    /// Check if there's sufficient disk space for index operations.
    pub async fn check_disk_space(&self, _required_bytes: u64) -> TurboPropResult<bool> {
        match fs::metadata(&self.index_path) {
            Ok(_) => {
                // For simplicity, we'll assume there's enough space
                // In a real implementation, you'd use platform-specific APIs
                // to check available disk space
                debug!("Disk space check passed (simplified implementation)");
                Ok(true)
            }
            Err(e) => Err(TurboPropError::file_system(
                format!("Cannot check disk space: {}", e),
                Some(self.index_path.clone()),
            )),
        }
    }

    /// Validate that we have the necessary permissions for index operations.
    pub async fn validate_permissions(&self) -> TurboPropResult<bool> {
        // Check if we can create the .turboprop directory
        let turboprop_dir = self.index_path.join(".turboprop");

        match fs::create_dir_all(&turboprop_dir) {
            Ok(_) => {
                // Clean up test directory if it was created
                if turboprop_dir.exists() && fs::read_dir(&turboprop_dir).unwrap().next().is_none()
                {
                    let _ = fs::remove_dir(&turboprop_dir);
                }
                Ok(true)
            }
            Err(e) => {
                warn!("Permission check failed: {}", e);
                Ok(false)
            }
        }
    }

    /// Recover the index using the specified strategy.
    pub async fn recover(
        &self,
        strategy: RecoveryStrategy,
        config: &TurboPropConfig,
    ) -> TurboPropResult<()> {
        info!("Starting index recovery with strategy: {:?}", strategy);

        match strategy {
            RecoveryStrategy::Clean => {
                self.cleanup_index().await?;
                info!("Index cleaned successfully");
            }
            RecoveryStrategy::Rebuild => {
                self.cleanup_index().await?;
                info!("Rebuilding index...");

                // Rebuild the index
                let _index = PersistentChunkIndex::build(&self.index_path, config).await?;
                info!("Index rebuilt successfully");
            }
            RecoveryStrategy::Repair => {
                // For now, repair just does a rebuild
                // In the future, this could try more sophisticated repair strategies
                warn!("Repair strategy not fully implemented, falling back to rebuild");
                self.cleanup_index().await?;
                let _index = PersistentChunkIndex::build(&self.index_path, config).await?;
                info!("Index repaired successfully");
            }
        }

        Ok(())
    }

    /// Automatically determine the best recovery strategy and execute it.
    pub async fn recover_automatically(&self, config: &TurboPropConfig) -> TurboPropResult<()> {
        let validation_result = self.validate_index().await?;

        match validation_result {
            ValidationResult::Healthy => {
                info!("Index is healthy, no recovery needed");
                Ok(())
            }
            ValidationResult::Missing => {
                info!("Index is missing, creating new index");
                let _index = PersistentChunkIndex::build(&self.index_path, config).await?;
                info!("New index created successfully");
                Ok(())
            }
            ValidationResult::Corrupted { issues } => {
                warn!("Index is corrupted: {:?}", issues);

                // Check permissions and disk space
                let has_permissions = self.validate_permissions().await?;
                if !has_permissions {
                    return Err(TurboPropError::file_permission(
                        self.index_path.clone(),
                        "write".to_string(),
                    ));
                }

                let has_space = self.check_disk_space(100 * 1024 * 1024).await?; // 100MB
                if !has_space {
                    return Err(TurboPropError::insufficient_disk_space(
                        100 * 1024 * 1024,
                        0, // We don't know the actual available space
                        self.index_path.clone(),
                    ));
                }

                // Create backup before recovery
                let _backup_path = self.backup_corrupted_index().await?;

                // Rebuild the index
                let _index = PersistentChunkIndex::build(&self.index_path, config).await?;
                info!("Index recovered successfully");
                Ok(())
            }
        }
    }
}

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

    #[tokio::test]
    async fn test_missing_index_detection() {
        let temp_dir = TempDir::new().unwrap();
        let recovery = IndexRecovery::new(temp_dir.path());

        let result = recovery.validate_index().await.unwrap();
        assert_eq!(result, ValidationResult::Missing);
    }

    #[tokio::test]
    async fn test_healthy_index_detection() {
        let temp_dir = TempDir::new().unwrap();
        let index_path = temp_dir.path();

        // Create a valid index structure
        let turboprop_dir = index_path.join(".turboprop");
        fs::create_dir_all(&turboprop_dir).unwrap();

        let metadata = serde_json::json!({
            "version": "1.0",
            "created_at": "2024-01-01T00:00:00Z",
            "chunk_count": 5,
            "embedding_dimensions": 384
        });

        fs::write(
            turboprop_dir.join("metadata.json"),
            serde_json::to_string_pretty(&metadata).unwrap(),
        )
        .unwrap();
        fs::write(turboprop_dir.join("chunks.db"), b"chunk data").unwrap();
        fs::write(turboprop_dir.join("embeddings.bin"), b"embedding data").unwrap();

        let recovery = IndexRecovery::new(index_path);
        let result = recovery.validate_index().await.unwrap();

        assert_eq!(result, ValidationResult::Healthy);
    }

    #[tokio::test]
    async fn test_corrupted_index_detection() {
        let temp_dir = TempDir::new().unwrap();
        let index_path = temp_dir.path();

        // Create corrupted index structure
        let turboprop_dir = index_path.join(".turboprop");
        fs::create_dir_all(&turboprop_dir).unwrap();

        // Invalid JSON metadata
        fs::write(turboprop_dir.join("metadata.json"), b"invalid json").unwrap();

        let recovery = IndexRecovery::new(index_path);
        let result = recovery.validate_index().await.unwrap();

        match result {
            ValidationResult::Corrupted { issues } => {
                assert!(!issues.is_empty());
            }
            _ => panic!("Expected corrupted index"),
        }
    }
}