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
# Lens Search Optimization Systems

Four durable, embedder-agnostic search optimizations that provide structural improvements independent of embedding model choice. All systems implement comprehensive SLA compliance validation per TODO.md requirements.

## 🎯 Overview

### Core Principle: Embedder Agnostic
These optimizations work at the structural level and will survive any embedding model changes. They provide durable improvements to search quality, performance, and user experience without requiring ML retraining when switching embeddings.

### The Four Systems

1. **[Clone-Aware Recall]#clone-aware-recall** - Token shingle expansion across code clones, forks, and backports
2. **[Learning-to-Stop]#learning-to-stop** - ML-based early termination for scanners and ANN search
3. **[Targeted Diversity]#targeted-diversity** - Constrained MMR applied only to overview queries with high entropy
4. **[TTL That Follows Churn]#ttl-that-follows-churn** - Adaptive cache management based on observed code churn

## πŸš€ Quick Start

### Basic Usage

```typescript
import { setupOptimizedSearch } from './optimizations';

// Production setup with all optimizations
const { engine, monitor, shutdown } = await setupOptimizedSearch('production');

// Use in your search pipeline
const originalHits = await yourSearchFunction(query, context);
const optimizedPipeline = await engine.optimizeSearchResults(
  originalHits,
  context,
  diversityFeatures // optional
);

// Get optimized results
console.log(`Optimized from ${originalHits.length} to ${optimizedPipeline.final_hits.length} hits`);
console.log(`Applied optimizations: ${optimizedPipeline.optimizations_applied.join(', ')}`);

// Cleanup
await shutdown();
```

### Custom Configuration

```typescript
import { OptimizationEngine, OPTIMIZATION_PRESETS } from './optimizations';

// Custom configuration
const customConfig = {
  ...OPTIMIZATION_PRESETS.PRODUCTION,
  learning_to_stop_enabled: false, // Disable specific optimization
};

const engine = new OptimizationEngine(customConfig);
await engine.initialize();

// Use engine...

await engine.shutdown();
```

## πŸ“Š Performance Requirements & SLA Compliance

All systems implement strict performance gates from TODO.md:

### Clone-Aware Recall
- **Recall Target**: +0.5-1.0pp Recall@50 improvement
- **Latency Budget**: ≀+0.6ms p95 latency
- **Clone Budget**: |C(s)| ≀ 3 clones per expansion
- **Jaccard Bonus**: Ξ² ≀ 0.2 log-odds, bounded
- **Coverage**: 100% span coverage

### Learning-to-Stop
- **Performance**: p95 -0.8 to -1.5ms improvement
- **Quality**: SLA-Recall@50 β‰₯ 0 (no degradation)
- **Upshift**: 3%-7% result quality improvement
- **Never-Stop**: Floor when positives_in_candidates < m

### Targeted Diversity
- **Application**: Only NL_overview ∧ high_entropy queries
- **Quality Gate**: Ξ”nDCG@10 β‰₯ 0 (no degradation)
- **Diversity Target**: +10% diversity improvement
- **Hard Constraints**: Exact/structural match floors preserved

### TTL That Follows Churn
- **Performance**: p95 -0.5 to -1.0ms improvement
- **Quality**: why-mix KL ≀ 0.02 (no drift)
- **Invalidation**: Zero span drift tolerance
- **TTL Bounds**: Ο„_min=1s, Ο„_max=30s, cβ‰ˆ3

## πŸ” Detailed System Documentation

### Clone-Aware Recall

Expands search results by finding code clones across repositories using token-shingle MinHash/SimHash indexing.

#### How it Works
1. **Indexing**: Content tokenized into subtokens, shingles generated (w=5-7)
2. **Clone Detection**: MinHash creates clone sets with similarity threshold
3. **Expansion**: Original hits expanded with budget-constrained clones
4. **Scoring**: Jaccard bonus applied to clone hits (bounded in log-odds)

#### Configuration
```typescript
// Index content for clone detection
await engine.indexContent(
  'function calculateSum(a, b) { return a + b; }',
  'math/utils.ts',
  1, 0, // line, col
  'main-repo',
  'function' // symbol kind
);

// Expansion happens automatically during optimization
```

#### Constraints
- **Clone Budget**: Maximum 3 clones per expansion (k_clone ≀ 3)
- **Veto Rules**: Same-repo + same-symbol-kind combinations rejected
- **Topic Filter**: topic_sim > Ο„ threshold required
- **Path Filter**: Vendor/third-party paths excluded

### Learning-to-Stop

Uses lightweight ML model to make early termination decisions for WAND/BMW scanners and ANN efSearch optimization.

#### Features Used
- `impact_prefix_gain`: Estimated gain from next block
- `remaining_budget_ms`: Time left in query budget  
- `topic_entropy`: Current result set diversity
- `pos_in_cands`: Position in candidate list
- `Ξ»_ann(ms/Ξ”Recall)`: ANN efficiency ratio

#### Scanner Integration
```typescript
const decision = engine.shouldStopScanning(
  blocksProcessed,
  candidatesFound, 
  timeSpentMs,
  searchContext,
  queryStartTime
);

if (decision.shouldStop) {
  // Terminate scanning early
  break;
}
```

#### ANN Integration
```typescript
const optimizedEf = engine.getOptimizedEfSearch(
  currentEf,
  recallAchieved,
  timeSpentMs,
  riskLevel,
  searchContext,
  queryStartTime
);
```

### Targeted Diversity

Applies constrained Maximum Marginal Relevance (MMR) selectively for natural language overview queries only.

#### Activation Criteria
- **Query Type**: Must be `NL_overview`
- **Entropy**: topic_entropy > 0.6 (high entropy threshold)
- **Clone Collapse**: Only after clone expansion (prevents fake diversity)
- **Result Count**: Minimum 5 results needed

#### MMR Optimization
```
argmax_S Σᡒ∈S rᡒ - γ Σᡒ<j sim_topic/symbol(i,j)
subject to: floors(exact,struct) = true
```

#### Usage
```typescript
const diversityFeatures = {
  query_type: 'NL_overview',
  topic_entropy: 0.85,
  result_count: hits.length,
  exact_matches: exactCount,
  structural_matches: structCount,
  clone_collapsed: true,
};

const pipeline = await engine.optimizeSearchResults(hits, context, diversityFeatures);
```

### TTL That Follows Churn

Adaptive cache management that adjusts TTL based on observed code churn rates and span invalidations.

#### Churn-Aware Formula
```
TTL = clamp(Ο„_min, Ο„_max, c/Ξ»_churn)
```
Where:
- `Ο„_min = 1s` (minimum TTL)
- `Ο„_max = 30s` (maximum TTL)  
- `c β‰ˆ 3` (churn constant)
- `Ξ»_churn` = observed churn rate (changes/second)

#### Cache Types
```typescript
// Micro-cache for search results
const result = await engine.getCachedValue(
  cacheKey,
  indexVersion,
  spanHash,
  async () => expensiveComputation(),
  'micro',
  'topic-bin'
);

// RAPTOR hierarchy cache
const raptor = await engine.getCachedValue(
  key, version, '', factory, 'raptor'
);

// Centrality cache
const centrality = await engine.getCachedValue(
  key, version, '', factory, 'centrality'
);
```

#### Churn Tracking
```typescript
// Record file changes for churn rate calculation
engine.recordFileChange('src/modified-file.ts', timestamp);

// Automatic invalidation on version/hash mismatch
// Cache entries automatically invalidated when:
// - index_version changes
// - span_hash changes  
// - TTL expires
```

## πŸ”§ Integration Patterns

### Search Pipeline Integration

```typescript
// 1. Initialize engine
const engine = new OptimizationEngine(config);
await engine.initialize();

// 2. Index content for clone detection
await engine.indexContent(content, file, line, col, repo, symbolKind);

// 3. Record file changes for churn tracking  
engine.recordFileChange(filePath, timestamp);

// 4. Use during search (scanner integration)
const shouldStop = engine.shouldStopScanning(blocks, candidates, time, ctx, start);
const efSearch = engine.getOptimizedEfSearch(ef, recall, time, risk, ctx, start);

// 5. Optimize final results
const pipeline = await engine.optimizeSearchResults(hits, context, features);

// 6. Use optimized results
return pipeline.final_hits;
```

### Performance Monitoring

```typescript
import { PerformanceMonitor, MONITORING_PRESETS } from './optimizations';

// Create monitor
const monitor = new PerformanceMonitor(engine, MONITORING_PRESETS.PRODUCTION);
await monitor.startMonitoring();

// Run benchmarks
const result = await monitor.runComprehensiveBenchmark('my-test');

// Check SLA compliance
console.log('SLA Compliant:', result.sla_compliance.overall_compliant);
console.log('Alerts:', result.alerts);
console.log('Recommendations:', result.recommendations);

// Generate report
const report = monitor.generatePerformanceReport();
console.log(report);
```

### Error Handling & Graceful Degradation

```typescript
// Systems gracefully degrade on failure
const pipeline = await engine.optimizeSearchResults(hits, context);

// Check what optimizations were applied
console.log('Applied:', pipeline.optimizations_applied);

// Original hits returned if all optimizations fail
console.log('Results:', pipeline.final_hits);

// Monitor system health
const health = engine.getSystemHealth();
if (!health.overall_healthy) {
  console.warn('Degraded systems:', health.degraded_optimizations);
  
  // Trigger recovery
  await engine.performHealthCheckAndRecovery();
}
```

## πŸŽ›οΈ Configuration Reference

### OptimizationConfig

```typescript
interface OptimizationConfig {
  clone_aware_enabled: boolean;        // Enable clone-aware recall
  learning_to_stop_enabled: boolean;   // Enable learning-to-stop
  targeted_diversity_enabled: boolean; // Enable targeted diversity
  churn_aware_ttl_enabled: boolean;   // Enable churn-aware TTL
  performance_monitoring_enabled: boolean; // Enable metrics collection
  graceful_degradation_enabled: boolean;   // Enable error recovery
}
```

### MonitoringConfig

```typescript
interface MonitoringConfig {
  benchmark_interval_ms: number;           // Frequency of automatic benchmarks
  alert_threshold_violations: number;     // Alerts before firing notification
  performance_degradation_threshold: number; // Performance drop threshold
  enable_real_time_monitoring: boolean;   // Real-time benchmark execution
  enable_alerting: boolean;               // Enable alert notifications
  log_level: 'debug' | 'info' | 'warn' | 'error'; // Logging verbosity
}
```

### DiversityFeatures

```typescript
interface DiversityFeatures {
  query_type: 'NL_overview' | 'targeted_search' | 'symbol_lookup' | 'other';
  topic_entropy: number;        // 0-1, entropy of query topics
  result_count: number;         // Number of results to diversify
  exact_matches: number;        // Count of exact matches (protected)
  structural_matches: number;   // Count of structural matches (protected)
  clone_collapsed: boolean;     // Whether clone expansion was applied
}
```

## πŸ“ˆ Performance Monitoring

### SLA Metrics Tracked

- **Recall@50**: Search recall at 50 results
- **P95 Latency**: 95th percentile optimization time
- **Upshift %**: Quality improvement percentage
- **Diversity Score**: Result set diversity measurement
- **Why-Mix KL**: Distribution drift measurement
- **Span Coverage**: Percentage of spans covered by optimizations

### Health Monitoring

```typescript
// Get comprehensive metrics
const metrics = engine.getPerformanceMetrics();

// Key metrics
console.log('SLA Compliance Rate:', metrics.sla_compliance_rate);
console.log('Average Optimization Time:', metrics.average_optimization_time_ms);
console.log('System Health:', metrics.system_health);

// Individual system metrics
console.log('Clone Aware:', metrics.subsystem_metrics.clone_aware);
console.log('Learning Stop:', metrics.subsystem_metrics.learning_to_stop);
console.log('Diversity:', metrics.subsystem_metrics.targeted_diversity);
console.log('TTL:', metrics.subsystem_metrics.churn_aware_ttl);
```

### Alerting

The monitoring system generates three types of alerts:

- **CRITICAL**: SLA violations, system health degradation
- **WARNING**: Performance targets missed, individual system issues  
- **INFO**: Configuration recommendations, optimization opportunities

## πŸ§ͺ Testing

### Running Tests

```bash
# Run all optimization tests
npm test src/optimizations

# Run specific system tests
npm test src/optimizations/__tests__/clone-aware-recall.test.ts
npm test src/optimizations/__tests__/optimization-engine.test.ts
npm test src/optimizations/__tests__/integration.test.ts

# Run with coverage
npm test -- --coverage src/optimizations
```

### Test Categories

- **Unit Tests**: Individual system functionality and constraints
- **Integration Tests**: Cross-system coordination and SLA compliance
- **Performance Tests**: Latency, throughput, and resource usage validation
- **Benchmark Tests**: SLA requirement validation against TODO.md specifications

### Custom Test Scenarios

```typescript
// Create custom test scenario
const testHits = [
  createMockSearchHit('file1.ts', 10, 95, 'function'),
  createMockSearchHit('file2.ts', 20, 85, 'class'),
];

const context = createMockSearchContext('test query');
const features = createMockDiversityFeatures('NL_overview', 0.8);

const pipeline = await engine.optimizeSearchResults(testHits, context, features);

// Validate SLA compliance
const sla = validatePipelineSLA(pipeline);
expect(sla.compliant).toBe(true);
```

## πŸ” Troubleshooting

### Common Issues

#### Clone Expansion Not Working
```typescript
// Verify content is indexed
await engine.indexContent(content, file, line, col, repo, symbolKind);

// Check if clones exist in different repos
// Same-repo, same-symbol-kind clones are vetoed
```

#### Diversity Not Applied
```typescript
// Verify query type and entropy requirements
const features = {
  query_type: 'NL_overview', // Must be overview
  topic_entropy: 0.8,        // Must be > 0.6
  clone_collapsed: true,     // Must be after clone expansion
};
```

#### Cache Not Working
```typescript
// Verify TTL system is enabled
const config = {
  churn_aware_ttl_enabled: true,
};

// Check cache key consistency
const result = await engine.getCachedValue(
  consistentKey,    // Same key for same operation
  indexVersion,     // Current index version
  spanHash,         // Current span hash
  factory
);
```

#### Performance Degradation
```typescript
// Check system health
const health = engine.getSystemHealth();
if (!health.overall_healthy) {
  // Perform recovery
  await engine.performHealthCheckAndRecovery();
}

// Monitor performance
const monitor = new PerformanceMonitor(engine, config);
const benchmark = await monitor.runComprehensiveBenchmark('debug');
console.log('Issues:', benchmark.alerts);
console.log('Recommendations:', benchmark.recommendations);
```

### Debug Mode

```typescript
// Enable debug logging
const monitor = new PerformanceMonitor(engine, {
  ...MONITORING_PRESETS.DEVELOPMENT,
  log_level: 'debug',
});

// Run detailed benchmark
await monitor.runComprehensiveBenchmark('debug-test');
```

## πŸ”¬ Architecture

### System Architecture

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ OptimizationEngine  β”‚ ← Main orchestrator
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ CloneAwareRecall    β”‚ ← Phase 1: Expand with clones
β”‚ LearningToStop      β”‚ ← Integrated with search phase
β”‚ TargetedDiversity   β”‚ ← Phase 2: Apply MMR if needed
β”‚ ChurnAwareTTL       β”‚ ← Cross-cutting caching
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

### Data Flow

1. **Indexing Phase**: Content indexed for clone detection
2. **Search Phase**: Learning-to-stop guides scanner/ANN termination
3. **Expansion Phase**: Clone-aware recall expands results
4. **Diversity Phase**: Targeted diversity applied (if qualified)
5. **Caching**: All operations cached with churn-aware TTL

### Thread Safety

All systems are designed for concurrent access:
- Clone index uses concurrent data structures
- Learning-to-stop is stateless per query
- Diversity calculations are independent
- TTL system uses atomic cache operations

## πŸ“š References

- **TODO.md**: Complete specification of performance requirements
- **Research Papers**: iSMELL (75.17% F1), isotonic reranking, RAPTOR
- **Performance Targets**: All SLA requirements validated in comprehensive test suite
- **Embedder Independence**: Systems work with any embedding model

## 🀝 Contributing

When contributing to optimization systems:

1. **Maintain SLA Compliance**: All changes must pass SLA validation tests
2. **Embedder Agnostic**: No dependencies on specific embedding models
3. **Performance First**: Changes should improve or maintain performance budgets
4. **Comprehensive Testing**: Add tests for new functionality and edge cases
5. **Documentation**: Update this README for any API or behavior changes

### Performance Testing

```bash
# Run performance validation
npm test src/optimizations/__tests__/integration.test.ts

# Validate SLA compliance  
npm run test:sla-validation

# Benchmark against baseline
npm run benchmark:optimization-systems
```