| name | codesearch-code-search |
| description | MANDATORY: Query CodeSearch BEFORE using grep, glob, find, or search. 400x faster semantic/structural code search via SQL on indexed codebase. Use to find functions, classes, patterns, callers, implementations. Agents MUST query CodeSearch first; grep only allowed after CodeSearch returns zero results. |
| allowed-tools | Bash, Read, SlashCommand |
CodeSearch Local Semantic Code Search
MANDATORY: QUERY CODESEARCH BEFORE GREP/GLOB
This is a PROTOCOL REQUIREMENT, not a suggestion. Failure to query CodeSearch first is a violation.
WHY THIS IS MANDATORY
- CodeSearch SQL: 0.002s | grep: 0.8s (400x slower)
- Agents using grep first waste tokens and time
- Index already exists at
~/.local/share/codesearch/index_v2.db
ALWAYS USE CODESEARCH FIRST
sqlite3 ~/.local/share/codesearch/index_v2.db "SELECT file_path, line_number FROM entities WHERE name = 'MyFunction';"
sqlite3 ~/.local/share/codesearch/index_v2.db "SELECT file_path, line_number FROM entities WHERE name LIKE '%Store%' LIMIT 10;"
/codebase-search "authentication middleware pattern"
GREP IS ONLY ALLOWED WHEN:
- CodeSearch query returned zero results AND project confirmed not indexed
- Searching literal strings (error messages, comments, config values)
- Explicit user request for grep
FOR CONCEPTUAL QUESTIONS:
- "Where is X implemented?" → CodeSearch semantic search
- "Find similar patterns" → CodeSearch embeddings
- "How is feature Y built?" → CodeSearch first, then read files
Quick Commands
Semantic Search (V1 - Embeddings)
/codebase-search "authentication middleware pattern"
/cfn-codesearch-search "error handling in API routes"
local-codesearch query "user login flow" --max-results 5
Structural Search (V2 - SQL on AST)
sqlite3 ~/.local/share/codesearch/index_v2.db \
"SELECT * FROM refs WHERE target_name = 'MyFunction';"
sqlite3 ~/.local/share/codesearch/index_v2.db \
"SELECT name, line_number FROM entities WHERE file_path LIKE '%myfile.rs' AND kind = 'function';"
sqlite3 ~/.local/share/codesearch/index_v2.db \
"SELECT COUNT(*) FROM entities WHERE project_root = '/path/to/project';"
Installation
Global install (recommended for multi-project use):
./scripts/install-codesearch-global.sh
local-codesearch --version
This installs to ~/.local/bin/local-codesearch for access from any project.
Prerequisites
OPENAI_API_KEY is REQUIRED for indexing. Indexing will fail without a valid key.
export OPENAI_API_KEY="sk-..."
echo 'export OPENAI_API_KEY="sk-..."' >> ~/.bashrc
source ~/.bashrc
OPENAI_API_KEY="sk-..." ./local-codesearch --project-dir /project index --path . --types rs,ts,py,sh,json,yaml,sql
Verify key is set:
echo $OPENAI_API_KEY
Index Management
local-codesearch --project-dir /path/to/project index --path . --types rs,ts,tsx,js,jsx,py,sh,json,yaml,sql
/codebase-reindex
sqlite3 ~/.local/share/codesearch/index_v2.db "SELECT project_root, COUNT(*) FROM entities GROUP BY project_root;"
Indexing Internals
.claude/worktrees (and worktrees, .worktrees) are excluded from indexing. They are ephemeral CFN agent git clones that duplicate source and nest node_modules; indexing them caused data collisions and memory blowup.
- Python (
.py) is supported as a first-class indexed language, alongside rs/ts/tsx/js/jsx/sh/json/yaml/sql.
- No memory runaway: the post-commit indexer enqueues each commit's changes as a job file, then a single worker drains the queue under an
flock. Concurrent commits never stack indexers; jobs are aggregated and deduped before indexing.
- Index stays fresh automatically via the post-commit hook
.claude/hooks/cfn-post-commit-codesearch-index.sh (enqueue on every commit, single locked drain worker).
Key Features
- Multi-project isolation: Index multiple projects in single database without data collision
- Non-destructive: Indexing one project never deletes data from other projects
- Centralized storage:
~/.local/share/codesearch/index_v2.db
- Dual search: V1 semantic (embeddings) + V2 structural (SQL on AST)
- Fast: Rust binary with SQLite backend
Database Location
~/.local/share/codesearch/index_v2.db
For Agents (MANDATORY PROTOCOL)
DO NOT use grep/glob until you have queried CodeSearch. This is enforced.
/codebase-search "relevant search terms" --top 5
sqlite3 ~/.local/share/codesearch/index_v2.db "SELECT file_path, line_number FROM entities WHERE name LIKE '%keyword%';"
./.claude/skills/cfn-codesearch/query-agent-patterns.sh "description"
Violation of this protocol wastes tokens and time. CodeSearch exists to prevent duplicated work.