lens-core 1.0.0

High-performance code search engine with LSP integration and benchmarking
Documentation
# Rust-Based Semantic Processing Architecture

This document describes the production-ready Rust semantic processing system that replaces the complex TypeScript implementations in the lens code search engine.

## Overview

The new semantic processing architecture provides:

- **High-performance embedding generation** with multiple model backends
- **Intelligent query classification** for intent detection
- **Risk-aware routing decisions** with conformal prediction  
- **Memory-safe implementation** with zero-copy optimizations
- **Seamless integration** with existing search engine
- **Comprehensive testing and benchmarking** infrastructure

## Architecture Components

### 1. Semantic Encoder (`embedding.rs`)
- **Purpose**: High-performance code and query embedding generation
- **Features**: 
  - Multiple backend support (SentenceTransformers, CodeT5, local MLP)
  - SIMD-accelerated similarity computation
  - Memory-optimized vector storage with caching
  - Async/await processing with configurable timeouts

### 2. Query Classifier (`query_classifier.rs`)
- **Purpose**: Intelligent query intent detection and classification
- **Features**:
  - Feature-based ML classification with pattern matching
  - Natural language vs code pattern recognition
  - Language detection and complexity scoring
  - Configurable confidence thresholds

### 3. Intent Router (`intent_router.rs`)
- **Purpose**: Route queries to appropriate search handlers
- **Features**:
  - Confidence-based routing decisions
  - LSP integration support
  - Caching and hot pattern optimization
  - Fallback strategy configuration

### 4. Conformal Router (`conformal_router.rs`)
- **Purpose**: Risk-aware upshift routing with budget constraints
- **Features**:
  - Conformal prediction with calibrated confidence intervals
  - Budget-constrained upshift decisions (≤5% daily budget)
  - Multiple upshift types (semantic, LSP, AST, cross-language)
  - Statistical uncertainty quantification

### 5. Integration Module (`integration.rs`)
- **Purpose**: Seamless integration with existing search engine
- **Features**:
  - Unified semantic processing pipeline
  - Performance monitoring and metrics
  - Graceful fallback handling
  - Health monitoring and diagnostics

## Usage Examples

### Basic Integration

```rust
use crate::search::{SearchEngine, SearchConfig};
use crate::semantic::{initialize_semantic_integration, SemanticConfig};
use crate::semantic::integration::SearchEngineSemanticExt;

// Initialize search engine
let search_engine = SearchEngine::new("./index").await?;

// Initialize semantic integration
let semantic_config = SemanticConfig::default();
let semantic_integration = initialize_semantic_integration(&semantic_config).await?;

// Perform semantic search
let query = "how to implement binary search algorithm";
let response = search_engine.search_auto_semantic(query, &semantic_integration).await?;

println!("Found {} results, semantic enhanced: {}", 
         response.base_response.results.len(), 
         response.semantic_enhanced);
```

### Advanced Configuration

```rust
use crate::semantic::integration::{SemanticSearchIntegration, SemanticIntegrationConfig, SemanticSearchRequest};

// Custom integration configuration
let integration_config = SemanticIntegrationConfig {
    enabled: true,
    nl_upshift_threshold: 0.6,          // Lower threshold for more semantic processing
    max_processing_time_ms: 200,        // Generous time budget
    enable_conformal_routing: true,     // Enable risk-aware routing
    fallback_on_error: true,           // Graceful error handling
    enable_result_caching: true,       // Cache semantic results
    similarity_threshold: 0.4,         // Inclusive similarity matching
};

let semantic_integration = SemanticSearchIntegration::new(integration_config).await?;

// Custom search request
let semantic_request = SemanticSearchRequest {
    base_request: SearchRequest {
        query: "rust async patterns".to_string(),
        max_results: 20,
        timeout_ms: 500,
        ..Default::default()
    },
    force_semantic: true,              // Force semantic processing
    intent_override: None,             // Let classifier determine intent
    skip_conformal: false,             // Use conformal routing
    similarity_threshold: Some(0.3),   // Custom similarity threshold
};

let response = semantic_integration.process_search(&search_engine, semantic_request).await?;
```

### Health Monitoring

```rust
// Check system health
let health_status = semantic_integration.health_check().await?;

if health_status.overall_healthy {
    println!("✅ All semantic components are healthy");
} else {
    println!("⚠️ Some components need attention:");
    if !health_status.encoder_healthy { println!("  - Encoder unhealthy"); }
    if !health_status.classifier_healthy { println!("  - Classifier unhealthy"); }
    if !health_status.intent_router_healthy { println!("  - Intent router unhealthy"); }
    if !health_status.conformal_router_healthy { println!("  - Conformal router unhealthy"); }
}

// Get performance metrics
let metrics = semantic_integration.get_metrics().await;
println!("Processed {} requests with {:.2}ms average time",
         metrics.total_requests, 
         metrics.avg_processing_time_ms);
```

## Performance Characteristics

### Latency Targets
- **Query Classification**: <10ms p95
- **Intent Routing**: <5ms p95  
- **Conformal Prediction**: <15ms p95
- **Semantic Encoding**: <50ms p95 (batch processing)
- **Total Processing**: <100ms p95 (within search SLA)

### Memory Optimization
- **Zero-copy operations** where possible
- **Memory pooling** for embeddings
- **Configurable cache sizes** (default: 10K embeddings)
- **SIMD acceleration** for similarity computation

### Throughput Targets
- **>1000 queries/second** on modern hardware
- **Horizontal scaling** through stateless design
- **Async processing** with configurable concurrency

## Migration from TypeScript

The Rust implementation provides these advantages over TypeScript:

### Performance Improvements
- **5-10x faster** embedding computation
- **2-3x lower** memory usage
- **Zero garbage collection** pauses
- **Better CPU utilization** with SIMD

### Safety and Reliability
- **Memory safety** without garbage collection
- **Thread safety** with Rust's ownership model
- **Compile-time error detection**
- **Comprehensive error handling**

### Operational Benefits
- **Lower resource costs** (CPU, memory)
- **Better predictability** (no GC pauses)
- **Easier deployment** (single binary)
- **Better observability** (structured metrics)

## Testing and Validation

### Unit Tests
```bash
# Run unit tests for all semantic modules
cargo test semantic:: --lib

# Run integration tests
cargo test semantic::integration::tests

# Run examples (requires index setup)
cargo test semantic::examples::tests -- --ignored
```

### Benchmarking
```bash
# Run semantic processing benchmarks
cargo test semantic::benchmarks:: --release

# Performance comparison with TypeScript baseline
cargo run --bin semantic_benchmark --release
```

### Health Checks
```rust
// Automated health monitoring
let health_status = semantic_integration.health_check().await?;
assert!(health_status.overall_healthy);

// Performance regression testing
let metrics = semantic_integration.get_metrics().await;
assert!(metrics.avg_processing_time_ms < 100.0);
```

## Configuration Reference

### SemanticIntegrationConfig

```rust
pub struct SemanticIntegrationConfig {
    /// Enable semantic processing (default: true)
    pub enabled: bool,
    
    /// Natural language upshift threshold 0.0-1.0 (default: 0.7)
    pub nl_upshift_threshold: f32,
    
    /// Maximum processing time budget in ms (default: 100)
    pub max_processing_time_ms: u64,
    
    /// Enable conformal prediction routing (default: true)
    pub enable_conformal_routing: bool,
    
    /// Fallback to lexical search on errors (default: true)
    pub fallback_on_error: bool,
    
    /// Cache semantic results (default: true)
    pub enable_result_caching: bool,
    
    /// Similarity threshold for reranking 0.0-1.0 (default: 0.5)
    pub similarity_threshold: f32,
}
```

## Troubleshooting

### Common Issues

**High Latency**
- Check `max_processing_time_ms` configuration
- Monitor `avg_processing_time_ms` metric
- Consider disabling conformal routing for faster processing

**Low Accuracy**
- Adjust `nl_upshift_threshold` for more/less semantic processing
- Tune `similarity_threshold` for semantic reranking
- Check query classification confidence scores

**Memory Usage**
- Reduce embedding cache sizes in individual components
- Monitor memory metrics and adjust accordingly
- Use smaller embedding models if needed

**Integration Failures**
- Check health status of individual components
- Review error logs for specific failure points
- Ensure proper async context and timeouts

### Debug Logging

```rust
// Enable debug logging for semantic components
RUST_LOG=lens::semantic=debug cargo run

// Specific component logging
RUST_LOG=lens::semantic::integration=trace cargo run
```

## Future Enhancements

- **GPU acceleration** for embedding computation
- **Advanced reranking** with cross-encoder models
- **Multi-language** specialized embeddings
- **Online learning** for query classification
- **Distributed processing** for large-scale deployments

## Conclusion

The new Rust-based semantic processing architecture provides a production-ready, high-performance replacement for the TypeScript implementations while maintaining full backward compatibility with the existing search engine. The modular design enables incremental adoption and easy extensibility for future enhancements.