codesearch 0.1.9

A fast, intelligent CLI tool with multiple search modes (regex, fuzzy, semantic), code analysis, and dead code detection for popular programming languages
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
# CodeSearch - Technical Specification

## Overview

CodeSearch is a fast, intelligent CLI tool for searching and analyzing codebases, built in Rust. It provides precise structural understanding that complements semantic search and RAG systems for AI agents.

**Version**: 0.1.8  
**Language**: Rust (Edition 2024)  
**License**: Apache-2.0

## Core Capabilities

### 1. Pattern Search Engine

**Supported Search Modes:**
- **Exact Match**: Direct string matching with line-level precision
- **Regex**: Full regex pattern support with compiled pattern caching
- **Fuzzy**: Typo-tolerant search using Levenshtein distance
- **Semantic**: Context-aware search enhancement

**Features:**
- Parallel file processing with rayon
- Thread-safe LRU caching with automatic eviction
- Relevance scoring and ranking
- Context extraction (surrounding lines)
- Multi-extension filtering
- File/directory exclusion patterns

### 2. Language Support

**48 Languages Supported:**
- **Systems**: Rust, C, C++, Go, Zig, V, Nim
- **Web**: JavaScript, TypeScript, HTML, CSS, SCSS
- **Backend**: Python, Java, Kotlin, C#, PHP, Ruby, Scala, Perl
- **Functional**: Haskell, Elixir, Erlang, Clojure, OCaml, F#
- **Mobile**: Swift, Dart, Objective-C
- **Scripting**: Shell, PowerShell, Lua, R, Julia
- **Data**: SQL, YAML, TOML, JSON, XML
- **Infrastructure**: Dockerfile, Terraform, Makefile
- **Others**: GraphQL, Protobuf, Solidity, WebAssembly, Assembly

**Language-Specific Patterns:**
- Function definitions (e.g., `fn`, `def`, `function`, `func`, `proc`)
- Class/struct definitions
- Import/use statements
- Comment patterns (single-line, multi-line, doc comments)

### 3. Enhanced Dead Code Detection

**Detection Types (6+ categories):**

#### 3.1 Unused Variables
- Detects variables declared but never referenced
- Patterns: `let`, `const`, `var`, `:=`, `<-`
- Excludes: Variables starting with `_`, single-letter vars, `err`
- **Output**: `[var]` marker with line number and reason

#### 3.2 Unreachable Code
- Identifies code after return statements
- Tracks brace depth and control flow
- Detects statements that will never execute
- **Output**: `[!]` marker with truncated code preview

#### 3.3 Empty Functions
- Finds functions with no implementation
- **Brace-based languages**: Detects `{}`
- **Indentation-based languages**: Detects Python `:` with only `pass`
- Excludes special functions (main, test_, constructors, trait implementations)
- **Output**: `[∅]` marker with function name

#### 3.4 TODO/FIXME Markers
- Flags incomplete or problematic code markers
- Markers: TODO, FIXME, HACK, XXX, BUG
- Only detects in comments (not in strings)
- **Output**: `[?]` marker with truncated comment

#### 3.5 Commented-Out Code
- Detects code that has been commented out
- Identifies function/variable declarations in comments
- Excludes documentation comments and standard notes
- **Output**: `[commented code]` with truncated line

#### 3.6 Unused Imports
- Tracks import/use statements
- Counts references across entire file
- Reports imports with ≤1 occurrence
- **Output**: `[imp]` marker with import name

**Special Function Exclusions:**
- Entry points: `main`, `init`, `__init__`
- Test functions: `test_*`, `Test*`
- Lifecycle: `setup`, `teardown`, `drop`, `finalize`
- Trait implementations: `clone`, `fmt`, `eq`, `hash`, `serialize`
- Event handlers: `on*`, `handle*`
- Private functions: `_*`

### 4. Code Complexity Analysis

**Metrics Calculated:**
- **Cyclomatic Complexity**: Number of linearly independent paths
- **Cognitive Complexity**: Measure of code understandability
- **Nesting Depth**: Maximum nesting level
- **Function Count**: Total functions per file
- **Line Count**: Total lines of code

**Thresholds:**
- Low: < 10
- Medium: 10-20
- High: > 20

### 5. Comprehensive Code Metrics

**Halstead Metrics (11 sub-metrics):**
- n1: Number of distinct operators
- n2: Number of distinct operands
- N1: Total operator count
- N2: Total operand count
- Program length: N = N1 + N2
- Vocabulary: n = n1 + n2
- Volume: V = N × log2(n)
- Difficulty: D = (n1/2) × (N2/n2)
- Effort: E = D × V
- Time: T = E / 18
- Bugs: B = E^(2/3) / 3000

**Additional Metrics:**
- Essential Complexity
- NPath Complexity
- Lines of Code (LOC, SLOC, LLOC)
- Code Density & Comment Ratio
- Maintainability Index (MI)
- Code Churn
- Depth of Inheritance Tree (DIT)
- Coupling Between Objects (CBO)
- Lack of Cohesion in Methods (LCOM)

### 6. Design Metrics

**Metrics Calculated:**
- **Afferent Coupling (Ca)**: Number of incoming dependencies
- **Efferent Coupling (Ce)**: Number of outgoing dependencies
- **Instability (I)**: Ce / (Ca + Ce)
- **Abstractness (A)**: Number of abstract classes / total classes
- **Distance from Main Sequence (D)**: |A + I - 1|
- **Package Cohesion (LCOM)**: Lack of cohesion in methods

### 7. Code Duplication Detection

**Algorithm:**
- Extracts code blocks (minimum configurable lines)
- Calculates string similarity using normalized edit distance
- Configurable similarity threshold (default: 0.9)
- Reports file pairs with similar blocks

### 8. Graph Analysis

**Supported Graph Types:**
- **AST**: Abstract Syntax Tree with syntax edges
- **CFG**: Control Flow Graph with basic blocks and branches
- **DFG**: Data Flow Graph tracking variable usage
- **PDG**: Program Dependency Graph (CFG + DFG)
- **Call Graph**: Function call relationships
- **Dependency Graph**: Module and file dependencies
- **Unified Graph**: Combined AST + CFG + DFG in one structure

**Output Formats:**
- Text (human-readable)
- DOT (Graphviz visualization)
- JSON (structured data)

### 9. Symbol Finding

**Find Types:**
- **definition**: Find symbol definitions
- **references**: Find all references to a symbol
- **callers**: Find all callers of a function

**Features:**
- Multi-language support
- Cross-file analysis
- JSON output for automation

### 10. Health Scoring

**Components:**
- Dead code detection
- Code duplication
- Complexity analysis

**Output:**
- Overall health score (0-100)
- Individual component scores
- Structured JSON for CI/CD integration
- `--fail-under` threshold support

### 11. Security Pattern Scanning

**Patterns Detected:**
- `eval()` usage
- `exec()` calls
- SQL injection patterns
- Command injection patterns
- Insecure deserialization

### 12. MCP Server Integration

**Protocol**: Model Context Protocol (MCP)  
**Transport**: stdio  
**Version**: rmcp 0.12

**Exposed Tools (9 total):**
1. `search` - Pattern search with filters
2. `list` - Directory enumeration
3. `analyze` - Codebase metrics and statistics
4. `complexity` - Complexity analysis
5. `duplicates` - Duplication detection
6. `deadcode` - Dead code analysis
7. `circular` - Circular dependency detection
8. `find_symbol` - Symbol finding (definition, references, callers)
9. `get_health` - Health scoring

**Tool Schemas:**
- JSON schema generation with `schemars`
- Automatic parameter validation
- Structured response formatting

## Architecture

### Module Organization

```
codesearch/
├── src/
│   ├── lib.rs                    # Library exports
│   ├── main.rs                   # CLI entry point
│   ├── cli.rs                    # CLI definitions
│   │
│   ├── commands/                 # Command handlers
│   │   ├── mod.rs
│   │   ├── search.rs
│   │   ├── analysis.rs
│   │   ├── graph.rs
│   │   └── util.rs
│   │
│   ├── search/                   # Search functionality
│   │   ├── mod.rs
│   │   ├── core.rs
│   │   ├── fuzzy.rs
│   │   ├── semantic.rs
│   │   ├── utilities.rs
│   │   ├── engine.rs
│   │   └── pure.rs
│   │
│   ├── deadcode/                 # Dead code detection
│   │   ├── mod.rs
│   │   ├── detectors.rs
│   │   ├── helpers.rs
│   │   └── types.rs
│   │
│   ├── duplicates/               # Duplication detection
│   ├── circular/                 # Circular dependencies
│   ├── codemetrics/              # Code metrics
│   ├── designmetrics/            # Design metrics
│   │
│   ├── graphs.rs                 # Graph analysis interface
│   ├── ast.rs                    # AST
│   ├── cfg.rs                    # CFG
│   ├── dfg.rs                    # DFG
│   ├── pdg.rs                    # PDG
│   ├── callgraph.rs              # Call graph
│   ├── depgraph.rs               # Dependency graph
│   ├── unified.rs                # Unified graph
│   │
│   ├── find.rs                   # Symbol finding
│   ├── health.rs                 # Health scoring
│   ├── security.rs               # Security scanning
│   │
│   ├── language/                 # Language support
│   ├── parser/                   # Code parsers
│   ├── extract/                  # Code extraction
│   │
│   ├── cache.rs                  # Simple cache
│   ├── cache_lru.rs              # LRU cache
│   ├── index.rs                  # Code indexing
│   ├── watcher.rs                # File watching
│   │
│   ├── githistory.rs             # Git history
│   ├── remote.rs                 # Remote search
│   │
│   ├── export.rs                 # Export functionality
│   ├── interactive.rs            # REPL mode
│   │
│   ├── mcp/                      # MCP integration
│   │   ├── mod.rs
│   │   ├── tools.rs
│   │   ├── schemas.rs
│   │   └── params.rs
│   │
│   ├── types.rs                  # Shared types
│   ├── traits.rs                 # Core traits
│   ├── errors.rs                 # Custom errors
│   └── fs.rs                     # File system abstraction
│
├── tests/
│   ├── integration_e2e.rs        # Integration tests
│   ├── cross_file_tests.rs       # Cross-file tests
│   └── fixtures/                 # Test fixtures
│
└── benches/
    ├── search_benchmark.rs       # Search benchmarks
    └── parser_benchmarks.rs      # Parser benchmarks
```

**Total**: ~6000+ lines of Rust code across 40+ modules

### Data Structures

```rust
// Search options (parameter object pattern)
pub struct SearchOptions {
    pub extensions: Option<Vec<String>>,
    pub ignore_case: bool,
    pub fuzzy: bool,
    pub fuzzy_threshold: f64,
    pub max_results: usize,
    pub exclude: Option<Vec<String>>,
    pub rank: bool,
    pub cache: bool,
    pub semantic: bool,
    pub benchmark: bool,
    pub vs_grep: bool,
}

// Dead code detection result
pub struct DeadCodeItem {
    pub file: String,
    pub line_number: usize,
    pub item_type: String,
    pub name: String,
    pub reason: String,
}

// Search result
pub struct SearchResult {
    pub file: String,
    pub line_number: usize,
    pub content: String,
    pub matches: Vec<Match>,
    pub score: f64,
    pub relevance: String,
}

// Complexity metrics
pub struct ComplexityMetrics {
    pub file_path: String,
    pub cyclomatic_complexity: u32,
    pub cognitive_complexity: u32,
    pub lines_of_code: usize,
    pub function_count: usize,
    pub max_nesting_depth: u32,
}

// Design metrics
pub struct ModuleMetrics {
    pub name: String,
    pub afferent_coupling: usize,  // Ca
    pub efferent_coupling: usize,  // Ce
    pub instability: f64,          // I
    pub abstractness: f64,         // A
    pub distance: f64,             // D
    pub cohesion: f64,             // LCOM
}
```

## Performance Characteristics

### Optimization Strategies

1. **Parallel Processing**
   - Uses rayon for multi-threaded file processing
   - Scales to available CPU cores
   - Thread-safe operations throughout

2. **Caching**
   - LRU cache with automatic eviction
   - Query-based caching with file modification tracking
   - Thread-safe DashMap implementation
   - Configurable cache capacity

3. **Memory Efficiency**
   - Streaming file reading (no full file loads)
   - Efficient data structures (DashMap, ahash)
   - Lazy evaluation where possible

4. **Regex Optimization**
   - Compiled patterns cached
   - Reused across file processing
   - Regex compilation outside loops

### Performance Targets

- **Search Latency**: < 50ms for typical queries (< 1000 files)
- **Memory Usage**: < 100MB for moderate codebases (< 10K files)
- **Parallel Efficiency**: 70%+ CPU utilization on multi-core systems
- **Cache Hit Rate**: 70-90% for repeated searches

*Note: Actual performance depends on codebase size, hardware, and query complexity.*

## Testing Strategy

### Test Coverage (173 unit + 36 integration + 23 MCP = 232 tests)

**Unit Tests (173 tests):**
- Co-located with implementation code
- Test individual functions in isolation
- Use temporary directories for file operations
- Pure function testing

**Integration Tests (36 tests):**
- End-to-end CLI command testing
- Output format validation
- Error handling verification
- Cross-file analysis testing

**MCP Tests (23 tests):**
- Tool invocation testing
- Parameter validation
- Response format verification

**Property-Based Tests:**
- `proptest` for fuzzing
- Test invariants
- Generate random inputs

### Test Execution

```bash
# All tests
cargo test --features mcp

# Specific module
cargo test deadcode --lib

# With output
cargo test -- --nocapture

# Run benchmarks
cargo bench

# Generate coverage
cargo tarpaulin --out Html
```

## CLI Interface

### Commands

```bash
# Search
codesearch "<query>" [path] [options]
codesearch interactive

# Analysis
codesearch analyze [path] [--metrics]
codesearch complexity [path] [--threshold N] [--sort]
codesearch deadcode [path] [-e extensions] [--exclude dirs]
codesearch duplicates [path] [--min-lines N] [--similarity N]
codesearch circular [path] [-e extensions]

# Graph
codesearch graph <ast|cfg|dfg|pdg|callgraph|depgraph|unified> [file]
codesearch find <symbol> [path] --type <definition|references|callers>

# Health
codesearch health [path] [--fail-under N]

# Security
codesearch security [path] [--extensions]

# Utilities
codesearch files [path] [--extensions]
codesearch languages
codesearch index [path]
codesearch watch [path]
codesearch git-history <query> [path]
codesearch remote --github <query> owner/repo

# MCP Server
codesearch mcp
```

### Options

- `-e, --extensions`: Filter by file extensions (comma-separated)
- `-x, --exclude`: Exclude directories/patterns (comma-separated)
- `-f, --fuzzy`: Enable fuzzy matching
- `-r, --regex`: Enable regex mode
- `-i, --ignore-case`: Case-insensitive search
- `--case-sensitive`: Case-sensitive search
- `--rank`: Rank results by relevance
- `--format`: Output format (text, csv, markdown, json, dot)
- `--output, -o`: Output file path
- `--threshold`: Complexity/similarity threshold
- `--sort`: Sort results
- `--cache`: Enable caching
- `--semantic`: Enable semantic search
- `--benchmark`: Benchmark mode
- `--fail-under`: Fail if score below threshold

## Output Formats

### Dead Code Detection Output

```
🔍 Dead Code Detection
─────────────────────────────

Found 12 potential dead code items:

[src/example.rs]
   [var] L  10: variable 'unused_var' - Variable declared but never used
   [!]   L  25: unreachable - Code after return statement is unreachable
   [∅]   L  42: empty_helper - Empty function with no implementation
   [?]   L  58: // TODO: implement this - TODO marker
   [imp] L  72: import 'HashMap' - Imported but never used

📊 Summary:
   • variable: 3
   • unreachable: 2
   • empty: 2
   • todo: 3
   • import: 2
```

### Health Score Output

```
🏥 Code Health Report
─────────────────────────────

Overall Health Score: 85/100 ✅

Components:
  • Dead Code: 90/100 (3 issues)
  • Duplicates: 95/100 (2 duplicates)
  • Complexity: 70/100 (5 high-complexity functions)

Recommendations:
  1. Review high-complexity functions in src/auth.rs
  2. Remove duplicate code in src/utils.rs
  3. Clean up unused variables in src/main.rs
```

## Dependencies

### Production Dependencies
- clap 4.4 - CLI parsing
- regex 1.10 - Pattern matching
- walkdir 2.4 - Directory traversal
- serde 1.0 - Serialization
- serde_json 1.0 - JSON serialization
- colored 2.1 - Terminal colors
- rayon 1.8 - Parallel processing
- dashmap 5.5 - Thread-safe maps
- ahash 0.8 - Fast hashing
- fuzzy-matcher 0.3 - Fuzzy search
- thiserror 1.0 - Custom error types
- anyhow 1.0 - Error propagation

### Optional Dependencies (MCP)
- rmcp 0.12 - MCP protocol
- tokio 1.0 - Async runtime
- schemars 1.2 - JSON schema

### Development Dependencies
- tempfile 3.8 - Temporary files for tests
- proptest 1.4 - Property-based testing
- criterion 0.5 - Benchmarking

## Build Configuration

```toml
[package]
name = "codesearch"
version = "0.1.8"
edition = "2024"
license = "Apache-2.0"

[features]
default = []
mcp = ["rmcp", "tokio", "schemars"]

[dependencies]
# ... dependencies listed above
```

**Rust Edition**: 2024  
**MSRV**: Rust 1.70+  
**Target**: Native binary (CLI-only, no WASM)

## Quality Standards

### Code Quality
- **100% test pass rate** (232 tests)
-**Zero clippy warnings**
-**Modular architecture** (40+ focused modules)
-**Thread-safe** parallel processing
-**Comprehensive error handling**

### Maintainability
- Trait abstractions for extensibility
- Parameter object pattern
- Dependency injection for testability
- Clear separation of concerns

### Performance
- Fast: < 50ms for typical searches
- Parallel: Auto-scales to available CPU cores
- Smart caching: LRU with automatic eviction
- Memory efficient: Streaming file reading

## Future Enhancements

### Planned Features
- AST-based code analysis for more languages
- Incremental indexing for very large codebases
- Enhanced git history search
- Plugin system for custom analyzers
- Web UI for visualization
- ML-based code pattern recognition

### Performance Improvements
- File watching for real-time updates
- Optimized memory usage for large files
- AST caching for frequently accessed files
- Query warming for common searches

## Version History

### 0.1.8 (Current)
- Comprehensive code metrics (Halstead, maintainability, etc.)
- Design metrics (coupling, cohesion, instability)
- Enhanced dead code detection (6+ types)
- MCP server with 9 tools
- Health scoring with CI/CD integration
- Symbol finding (definition, references, callers)
- Security pattern scanning
- Graph analysis (7 graph types)
- Trait abstractions for testability
- LRU cache with automatic eviction

### 0.1.7
- Modular architecture (40+ modules)
- Command handlers extracted from main.rs
- Search engine refactored with traits
- Dependency injection for file system
- Custom error types
- Parameter object pattern

### 0.1.6
- Graph analysis (AST, CFG, DFG, PDG, Call, Dependency, Unified)
- DOT format export
- Find symbol command
- Health scoring

### 0.1.5
- Design metrics module
- Comprehensive code metrics
- Dead code detection enhancements
- Property-based tests
- Benchmark suite

### 0.1.4
- Enhanced dead code detection
- 11 new unit tests
- Updated documentation

### 0.1.3
- MCP server support
- 48 language support
- Complexity metrics
- Code duplication detection

### 0.1.2
- Interactive mode
- Fuzzy search
- Export functionality

### 0.1.1
- Basic search functionality
- Regex support
- Multi-extension filtering

## License

Apache-2.0 License

---

**Built with ❤️ in Rust** | **Precise** | **Fast** | **Agent-Ready**