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
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
//! Enhanced index command implementation with progress tracking and error handling.
//!
//! This module provides the complete index command that coordinates file discovery,
//! chunking, embedding generation, and persistent storage with rich user feedback.

use anyhow::{Context, Result};
use std::path::Path;
use tracing::{info, warn};

use crate::config::TurboPropConfig;
use crate::error_classification::ErrorType;
use crate::git::GitignoreFilter;
use crate::incremental::{IncrementalStats, IncrementalUpdater};
use crate::index::PersistentChunkIndex;
use crate::model_validation::{validate_instruction_compatibility, validate_model_selection};
use crate::pipeline::{IndexingPipeline, PipelineConfig};
use crate::storage::PersistentIndex;
use crate::watcher::{FileWatcher, SignalHandler};

// Error message constants for user-friendly error formatting
const ERROR_ICON: &str = "";
const HELP_ICON: &str = "💡";

/// Execute the index command with the provided configuration
///
/// This is the main entry point for the `tp index` command that replaces the
/// simpler `index_files_with_config` function with enhanced progress tracking,
/// error handling, and user feedback.
///
/// # Arguments
///
/// * `path` - The directory path to index
/// * `config` - Complete TurboProp configuration
/// * `show_progress` - Whether to show interactive progress bars (disable for testing)
///
/// # Returns
///
/// * `Result<()>` - Ok(()) if indexing completed successfully, error otherwise
///
/// # Examples
///
/// ```no_run
/// use std::path::Path;
/// use turboprop::{config::TurboPropConfig, commands::index::execute_index_command};
///
/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
/// let config = TurboPropConfig::default();
/// let result = execute_index_command(Path::new("./src"), &config, true).await;
/// assert!(result.is_ok());
/// # });
/// ```
pub async fn execute_index_command(
    path: &Path,
    config: &TurboPropConfig,
    show_progress: bool,
) -> Result<()> {
    info!("Executing index command for path: {}", path.display());

    // Create pipeline configuration
    let pipeline_config = PipelineConfig::default()
        .with_progress(show_progress)
        .with_error_handling(true) // Always continue on individual file errors
        .with_batch_size(config.embedding.batch_size);

    // Create and execute the indexing pipeline
    let mut pipeline = IndexingPipeline::new(config.clone(), pipeline_config);

    match pipeline.execute_persistent(path).await {
        Ok((persistent_index, stats)) => {
            info!("Index command completed successfully");
            info!("Final index size: {} chunks", persistent_index.len());
            info!(
                "Index saved to: {}",
                persistent_index.storage_path().display()
            );

            // Show warnings if there were any failures, but don't fail the command
            if stats.files_failed > 0 {
                warn!(
                    "⚠️  {} out of {} files failed to process, but indexing completed successfully",
                    stats.files_failed, stats.files_discovered
                );
                warn!("Success rate: {:.1}%", stats.success_rate());
            }

            Ok(())
        }
        Err(e) => {
            Err(e).with_context(|| format!("Index command failed for path: {}", path.display()))
        }
    }
}

/// Build an index for MCP server use
pub async fn build_index(
    repo_path: &Path,
    config: &TurboPropConfig,
) -> Result<PersistentChunkIndex> {
    // Use existing index building logic
    execute_index_command(
        repo_path, config, false, // show_progress
    )
    .await?;

    // Load and return the created index from the repo path
    PersistentChunkIndex::load(repo_path).context("Failed to load newly created index")
}

/// Execute index command with detailed error context for CLI usage
///
/// This wrapper provides additional error context and user-friendly error messages
/// for CLI usage, converting technical errors into actionable user feedback.
pub async fn execute_index_command_cli(
    path: &Path,
    config: &TurboPropConfig,
    show_progress: bool,
) -> Result<()> {
    // Validate inputs before starting
    validate_index_inputs(path, config).await?;

    // Execute the command
    match execute_index_command(path, config, show_progress).await {
        Ok(()) => Ok(()),
        Err(e) => {
            // Provide user-friendly error messages
            let user_message = format_user_error(&e, path);
            eprintln!("{}", user_message);
            Err(e)
        }
    }
}

/// Validate inputs for the index command
async fn validate_index_inputs(path: &Path, config: &TurboPropConfig) -> Result<()> {
    // Validate path
    if !path.exists() {
        anyhow::bail!("Directory does not exist: {}", path.display());
    }

    if !path.is_dir() {
        anyhow::bail!("Path is not a directory: {}", path.display());
    }

    // Validate configuration
    config
        .validate()
        .with_context(|| "Invalid configuration provided")?;

    // Check if embedding model is reasonable
    if config.embedding.embedding_dimensions == 0 {
        anyhow::bail!("Invalid embedding dimensions: cannot be zero");
    }

    if config.embedding.batch_size == 0 {
        anyhow::bail!("Invalid batch size: cannot be zero");
    }

    // Warn about very large batch sizes that might cause memory issues
    if config.embedding.batch_size > config.embedding.batch_size_warning_threshold {
        warn!(
            "Large batch size ({}) exceeds threshold ({}), this may cause memory issues",
            config.embedding.batch_size, config.embedding.batch_size_warning_threshold
        );
    }

    // Validate the specified embedding model
    let model_info = validate_model_selection(&config.embedding.model_name)
        .await
        .with_context(|| {
            format!(
                "Model validation failed for '{}'",
                config.embedding.model_name
            )
        })?;

    // Validate instruction compatibility if instruction is provided
    validate_instruction_compatibility(&model_info, config.current_instruction.as_deref())
        .with_context(|| "Instruction validation failed")?;

    Ok(())
}

/// Format a user error message with optional help tips and technical details
fn format_error_message(
    main_message: &str,
    help_tips: &[&str],
    technical_details: Option<&anyhow::Error>,
) -> String {
    let mut message = format!("{} {}", ERROR_ICON, main_message);

    for tip in help_tips {
        message.push_str(&format!("\n{} {}", HELP_ICON, tip));
    }

    if let Some(details) = technical_details {
        message.push_str(&format!("\n\nTechnical details: {}", details));
    }

    message
}

/// Execute the watch command with file system monitoring
///
/// This function starts the index in watch mode, continuously monitoring
/// for file changes and updating the index incrementally.
///
/// # Arguments
///
/// * `path` - The directory path to watch
/// * `config` - Complete TurboProp configuration
/// * `show_progress` - Whether to show progress output
///
/// # Returns
///
/// * `Result<()>` - Ok(()) when gracefully shut down, error otherwise
pub async fn execute_watch_command(
    path: &Path,
    config: &TurboPropConfig,
    show_progress: bool,
) -> Result<()> {
    info!("Starting watch mode for path: {}", path.display());

    // First, build initial index if it doesn't exist
    let mut persistent_index = if PersistentIndex::exists(path) {
        info!("Loading existing index...");
        PersistentIndex::load(path)
            .with_context(|| format!("Failed to load existing index for: {}", path.display()))?
    } else {
        info!("Building initial index...");
        // Build the initial index using the same logic as the regular index command
        execute_index_command(path, config, show_progress)
            .await
            .context("Failed to build initial index for watch mode")?;

        // Now load the index that was just created
        PersistentIndex::load(path).with_context(|| {
            format!("Failed to load newly created index for: {}", path.display())
        })?
    };

    // Initialize file watcher
    let gitignore_filter = GitignoreFilter::new(path)
        .with_context(|| format!("Failed to create gitignore filter for: {}", path.display()))?;

    let mut file_watcher = FileWatcher::new(path, gitignore_filter)
        .with_context(|| format!("Failed to create file watcher for: {}", path.display()))?;

    // Initialize incremental updater
    let mut incremental_updater = IncrementalUpdater::new(config.clone(), path)
        .await
        .context("Failed to initialize incremental updater")?;

    // Set up signal handler for graceful shutdown
    let signal_handler = SignalHandler::new().context("Failed to set up signal handler")?;

    if show_progress {
        println!(
            "✓ Watching for changes in {} (Press Ctrl+C to stop)",
            path.display()
        );
        println!("  Initial index: {} chunks", persistent_index.len());
    }

    // Main watch loop
    let mut total_stats = IncrementalStats::default();

    tokio::select! {
        _ = signal_handler.wait_for_shutdown() => {
            info!("Received shutdown signal, stopping watch mode");
        }
        _ = async {
            while let Some(batch) = file_watcher.next_batch().await {
                if show_progress {
                    println!("🔄 Processing {} file changes...", batch.events.len());
                }

                match incremental_updater.process_batch(&batch, &mut persistent_index).await {
                    Ok(stats) => {
                        if show_progress && (stats.files_added > 0 || stats.files_modified > 0 || stats.files_removed > 0) {
                            println!(
                                "✓ Updated index - {} added, {} modified, {} removed ({} chunks)",
                                stats.files_added,
                                stats.files_modified,
                                stats.files_removed,
                                persistent_index.len()
                            );
                        }
                        total_stats.merge(stats);
                    }
                    Err(e) => {
                        warn!("Failed to process file changes: {}", e);
                        if show_progress {
                            eprintln!("⚠️  Error processing changes: {}", e);
                        }
                    }
                }
            }
        } => {
            info!("File watcher closed, stopping watch mode");
        }
    }

    info!("Watch mode shutting down...");
    info!(
        "Total changes processed - added: {}, modified: {}, removed: {}",
        total_stats.files_added, total_stats.files_modified, total_stats.files_removed
    );

    if show_progress {
        println!("👋 Watch mode stopped");
        println!("   Total files processed: {}", total_stats.files_processed);
        println!("   Files added: {}", total_stats.files_added);
        println!("   Files modified: {}", total_stats.files_modified);
        println!("   Files removed: {}", total_stats.files_removed);
        println!("   Final index size: {} chunks", persistent_index.len());
    }

    Ok(())
}

/// Execute watch command with detailed error context for CLI usage
///
/// This wrapper provides additional error context and user-friendly error messages
/// for CLI usage of the watch command.
pub async fn execute_watch_command_cli(
    path: &Path,
    config: &TurboPropConfig,
    show_progress: bool,
) -> Result<()> {
    // Validate inputs before starting watch mode
    validate_index_inputs(path, config).await?;

    // Execute the watch command
    match execute_watch_command(path, config, show_progress).await {
        Ok(()) => Ok(()),
        Err(e) => {
            // Provide user-friendly error messages
            let user_message = format_watch_error(&e, path);
            eprintln!("{}", user_message);
            Err(e)
        }
    }
}

/// Format technical errors into user-friendly messages for watch mode
fn format_watch_error(error: &anyhow::Error, path: &Path) -> String {
    let error_str = error.to_string().to_lowercase();

    if error_str.contains("permission denied") {
        format_error_message(
            &format!("Cannot watch directory: {}", path.display()),
            &[
                "File system watching requires read permissions for the directory and its contents",
                "Make sure you have appropriate permissions to watch this directory",
                "Try running with elevated permissions if necessary",
            ],
            Some(error),
        )
    } else if error_str.contains("too many open files") {
        format_error_message(
            "File system resource limit reached",
            &[
                "Your system has reached the maximum number of file handles",
                "Try watching a smaller directory or increase the system file handle limit",
                "On macOS/Linux, you can increase limits with 'ulimit -n'",
            ],
            Some(error),
        )
    } else if error_str.contains("no such file") || error_str.contains("not found") {
        format_error_message(
            &format!("Directory not found: {}", path.display()),
            &[
                "The specified directory does not exist",
                "Make sure the path is correct and the directory exists",
                "Check that the directory wasn't moved or deleted",
            ],
            Some(error),
        )
    } else {
        format_error_message(
            &format!("Watch mode failed for '{}'", path.display()),
            &[
                "An unexpected error occurred while setting up file watching",
                "Try running with --verbose for more detailed error information",
                "Make sure the directory is accessible and you have read permissions",
            ],
            Some(error),
        )
    }
}

/// Format technical errors into user-friendly messages using structured error classification
fn format_user_error(error: &anyhow::Error, path: &Path) -> String {
    let error_type = ErrorType::classify(error);
    let error_info = error_type.get_info();

    let message = match error_type {
        ErrorType::NoFilesFound => format!("No files found to index in '{}'", path.display()),
        ErrorType::Generic => format!("Indexing failed for '{}'", path.display()),
        _ => error_info.message.to_string(),
    };

    // For permission errors, we need to add path-specific help
    if matches!(error_type, ErrorType::PermissionDenied) {
        let path_help = format!(
            "Make sure you have read permissions for the directory: {}",
            path.display()
        );
        let mut help_with_path = vec![path_help.as_str()];
        help_with_path.extend_from_slice(error_info.help_texts);
        format_error_message(&message, &help_with_path, Some(error))
    } else {
        let error_ref = if matches!(error_type, ErrorType::NoFilesFound) {
            None
        } else {
            Some(error)
        };
        format_error_message(&message, error_info.help_texts, error_ref)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::TurboPropConfig;
    use std::fs;
    use tempfile::TempDir;

    fn create_test_file(dir: &Path, name: &str, content: &str) -> std::path::PathBuf {
        let file_path = dir.join(name);
        fs::write(&file_path, content).unwrap();
        file_path
    }

    #[tokio::test]
    async fn test_validate_index_inputs() {
        let temp_dir = TempDir::new().unwrap();
        let config = TurboPropConfig::default();

        // Valid inputs should pass
        assert!(validate_index_inputs(temp_dir.path(), &config)
            .await
            .is_ok());

        // Non-existent directory should fail
        let non_existent = temp_dir.path().join("non_existent");
        assert!(validate_index_inputs(&non_existent, &config).await.is_err());

        // File instead of directory should fail
        let file_path = create_test_file(temp_dir.path(), "test.txt", "content");
        assert!(validate_index_inputs(&file_path, &config).await.is_err());
    }

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

        // Config with zero embedding dimensions should fail
        let mut bad_config = TurboPropConfig::default();
        bad_config.embedding.embedding_dimensions = 0;
        assert!(validate_index_inputs(temp_dir.path(), &bad_config)
            .await
            .is_err());

        // Config with zero batch size should fail
        let mut bad_config = TurboPropConfig::default();
        bad_config.embedding.batch_size = 0;
        assert!(validate_index_inputs(temp_dir.path(), &bad_config)
            .await
            .is_err());
    }

    #[tokio::test]
    async fn test_execute_index_command_with_empty_dir() {
        let temp_dir = TempDir::new().unwrap();
        let config = TurboPropConfig::default();

        // Empty directory should fail with "no files found" error
        let result = execute_index_command(temp_dir.path(), &config, false).await;
        assert!(result.is_err());

        // Check the full error chain, not just the top-level context
        let error = result.unwrap_err();
        let mut error_chain = vec![error.to_string()];
        let mut current = error.source();
        while let Some(source) = current {
            error_chain.push(source.to_string());
            current = source.source();
        }

        // Look for "No files found" in any part of the error chain
        let contains_no_files_found = error_chain.iter().any(|msg| msg.contains("No files found"));
        assert!(
            contains_no_files_found,
            "Expected error chain to contain 'No files found', got: {:?}",
            error_chain
        );
    }

    #[test]
    fn test_format_user_error() {
        let temp_path = Path::new("/test/path");

        // Test no files found error
        let error = anyhow::anyhow!("No files found to index");
        let formatted = format_user_error(&error, temp_path);
        assert!(formatted.contains("No files found"));
        assert!(formatted.contains("💡"));

        // Test permission error
        let error = anyhow::anyhow!("Permission denied accessing files");
        let formatted = format_user_error(&error, temp_path);
        assert!(formatted.contains("Permission denied"));
        assert!(formatted.contains("💡"));

        // Test generic error
        let error = anyhow::anyhow!("Some unknown error");
        let formatted = format_user_error(&error, temp_path);
        assert!(formatted.contains("Indexing failed"));
        assert!(formatted.contains("💡"));
    }

    #[tokio::test]
    async fn test_execute_index_command_with_files() {
        // Skip this test in test environments where model downloads may be unreliable
        // The functionality is validated by integration tests and individual test runs
        if std::env::var("OFFLINE_TESTS").is_ok() || std::env::var("CI").is_ok() {
            return;
        }

        let temp_dir = TempDir::new().unwrap();
        let config = TurboPropConfig::default();

        // Create test files
        create_test_file(
            temp_dir.path(),
            "test1.txt",
            "Hello world this is test content",
        );
        create_test_file(
            temp_dir.path(),
            "test2.rs",
            "fn main() { println!(\"Hello\"); }",
        );

        // This test requires network access for embedding model
        // In a real environment, this should work
        let result = execute_index_command(temp_dir.path(), &config, false).await;

        // We expect this might fail due to network/model requirements in test environment
        // So we just verify the error message is reasonable if it fails
        if let Err(e) = result {
            let error_str = e.to_string();
            // Common acceptable errors in test environment
            // For debugging: print the actual error message
            eprintln!("DEBUG: Actual error message: '{}'", error_str);

            // Accept any error in test environment since model initialization can fail in many ways
            // The test validates that the command handles errors gracefully
            assert!(
                !error_str.is_empty(),
                "Error message should not be empty: {}",
                error_str
            );
        }
    }
}