cfn-codesearch
MANDATORY: Query CodeSearch BEFORE using grep, glob, find, or search. Performs 400x faster semantic and structural code search via SQL on indexed codebase. Use for finding functions, classes, patterns
Files56
View on GitHub- bin/1 files
- docs/7 files
- examples/3 files
- index/2 files
- lib/
- src/
- tests/
- AGENT_INTEGRATION_PATTERNS.md7.2 KB
- AGENT_LIFECYCLE_INTEGRATION.md9.2 KB
- analyze-agent-failures.sh8.9 KB
- ARCHITECTURAL_FIXES_SUMMARY.md6.3 KB
- Cargo.toml2.4 KB
- cfn-integration.sh5.8 KB
- DATABASE_SAFETY_AUDIT.md16 KB
- epic-ast-indexer.json14 KB
- EXTRACTION_EXAMPLES.md8.0 KB
- IMMEDIATE_ACTION_REQUIRED.md8.1 KB
- IMPLEMENTATION_SUMMARY.md9.3 KB
- index-all-projects.sh3.0 KB
- index-code.sh4.1 KB
- index-manifest-claude-flow-novice.md3.4 KB
- ingest-agent-transcript.sh7.6 KB
- init-local-codesearch.sh2.5 KB
- PHASE_5_COMPLETION_REPORT.md4.6 KB
- project-structure.md1.1 KB
- query-agent-patterns.sh5.5 KB
- query-local.sh3.2 KB
- README.md2.2 KB
- requirements.txt33 B
- SCHEMA_V2_IMPLEMENTATION.md4.9 KB
- search.sh8.4 KB
- SECURITY_AUDIT_FINAL.md11 KB
- SECURITY_FINDINGS_SUMMARY.txt5.0 KB
- SECURITY_FIX_REPORT.md4.7 KB
- SECURITY_IMPLEMENTATION.md7.1 KB
- SECURITY_TEST_SUITE_REPORT.md12 KB
- SECURITY_TESTING_COMPLETION.md13 KB
- SECURITY_VALIDATION_REPORT.md20 KB
- SKILL.md6.5 KB
- sync-all-memgraph.sh1.8 KB
- sync-memgraph.py9.4 KB
- sync-memgraph.sh8.5 KB
- test_ast_indexing.rs1.4 KB
- test_query_api.sh2.3 KB
- test_schema_v2.sql4.6 KB
- test_schema.sql4.7 KB
- test-agent-lifecycle-integration.sh8.1 KB
- test-local-codesearch.sh6.4 KB
- VALIDATION_CHECKLIST.md7.0 KB
- VERIFICATION_CHECKLIST.md7.7 KB
SKILL.md.claude/skills/cfn-codesearch/SKILL.md
GitHubCodeSearch 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
- One call answers names, callers (files mentioning X) and concepts
- Agents using grep first waste tokens and time
- Index already exists at
~/.local/share/codesearch/index_v2.db
ALWAYS USE CODESEARCH FIRST
# Exact name lookup - 0.002s
sqlite3 ~/.local/share/codesearch/index_v2.db "SELECT file_path, line_number FROM entities WHERE name = 'MyFunction';"
# Fuzzy search - 0.004s
sqlite3 ~/.local/share/codesearch/index_v2.db "SELECT file_path, line_number FROM entities WHERE name LIKE '%Store%' LIMIT 10;"
# Semantic search
/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)
# Natural language search
/codebase-search "authentication middleware pattern"
/cfn-codesearch-search "error handling in API routes"
# CLI direct (global install preferred)
local-codesearch query "user login flow" --max-results 5
Structural Search (V2 - SQL on AST)
# Find all callers of a function
sqlite3 ~/.local/share/codesearch/index_v2.db \
"SELECT * FROM refs WHERE target_name = 'MyFunction';"
# Find all functions in a file
sqlite3 ~/.local/share/codesearch/index_v2.db \
"SELECT name, line_number FROM entities WHERE file_path LIKE '%myfile.rs' AND kind = 'function';"
# Find entities by project (multi-project isolation)
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):
# From claude-flow-novice project
./scripts/install-codesearch-global.sh
# Verify
local-codesearch --version
This installs to ~/.local/bin/local-codesearch for access from any project.
Optional GPU build (machines with an NVIDIA card only): embeddings run ~18x faster on CUDA. The default build stays CPU-only and needs no CUDA libraries.
cargo build --release --features gpu
cp "$CARGO_TARGET_DIR/release/local-codesearch" ~/.local/bin/local-codesearch-gpu.bin
ln -sf "$PWD/bin/local-codesearch-gpu" ~/.local/bin/local-codesearch-gpu
- Runtime libraries:
~/.local/share/codesearch/ort/libonnxruntime*.soand the CUDA 12 / cuDNN 9 pip libs under~/.local/share/codesearch/cuda/nvidia/. The wrapperbin/local-codesearch-gpuputs them on the loader path; it exits 127 when any is missing. CODESEARCH_GPU_MEM_MBcaps the CUDA arena (default 3072; peak seen ~1.9 GB).CODESEARCH_DEVICE=cpuforces CPU. Any CUDA failure at load falls back to CPU.- The post-commit hook uses
~/.local/bin/local-codesearch-gpuwhen it exists and reruns the CPU binary on exit 127. Machines without it (the VPS) use the CPU binary. - Test:
bash tests/test-codesearch-gpu-wrapper.sh.
Prerequisites
OPENAI_API_KEY is REQUIRED for indexing. Indexing will fail without a valid key.
# Option 1: Export before running
export OPENAI_API_KEY="sk-..."
# Option 2: Add to shell profile (~/.bashrc or ~/.zshrc)
echo 'export OPENAI_API_KEY="sk-..."' >> ~/.bashrc
source ~/.bashrc
# Option 3: Inline with command
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 # Should show your key (not empty)
Index Management
# Index a project (first time or full rebuild)
# project_root is tagged via --project-dir; the inner `index --path .` walks that dir.
local-codesearch --project-dir /path/to/project index --path . --types rs,ts,tsx,js,jsx,py,sh,json,yaml,sql
# Incremental update (after code changes)
/codebase-reindex
# Check index stats
sqlite3 ~/.local/share/codesearch/index_v2.db "SELECT project_root, COUNT(*) FROM entities GROUP BY project_root;"
Indexing Internals
.claude/worktrees(andworktrees,.worktrees) are excluded from indexing. They are ephemeral CFN agent git clones that duplicate source and nestnode_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.
# STEP 1: Query CodeSearch FIRST (required)
/codebase-search "relevant search terms" --top 5
# Or SQL:
sqlite3 ~/.local/share/codesearch/index_v2.db "SELECT file_path, line_number FROM entities WHERE name LIKE '%keyword%';"
# STEP 2: Query past errors/patterns
$HOME/.claude/skills/cfn-codesearch/query-agent-patterns.sh "description"
# STEP 3: Only if CodeSearch returns nothing, then use grep
Violation of this protocol wastes tokens and time. CodeSearch exists to prevent duplicated work.