Files
Claude-Code-Workflow/.claude/skills/context-search/examples.md
catlog22 b366924ae6 feat: add batch replan mode and comprehensive skill system
- Add batch processing mode to /task:replan command
  - Support for verification report input
  - TodoWrite integration for progress tracking
  - Automatic backup management
- Enhance /workflow:action-plan-verify with batch remediation
  - Save verification report to .process directory
  - Provide batch replan command suggestions
- Add comprehensive skill documentation
  - Codex: autonomous development workflows
  - Gemini/Qwen: code analysis and documentation
  - Context-search: strategic context gathering
  - Prompt-enhancer: ambiguous prompt refinement
- Clean up CLAUDE.md strategy references

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-17 11:44:37 +08:00

8.6 KiB

Context Search Examples

This document provides practical examples of using context search tools in different scenarios.

Quick Command Examples

Semantic File Discovery

# Find files relevant to authentication
~/.claude/scripts/gemini-wrapper --all-files -p "List all files relevant to: user authentication and JWT"

# Find files related to database schema
~/.claude/scripts/gemini-wrapper --all-files -p "List all files relevant to: database schema and migrations"

# In Bash tool (with timeout)
bash(~/.claude/scripts/gemini-wrapper --all-files -p "List all files relevant to: API endpoints")

Program Architecture Analysis

# Analyze program structure (run before planning)
~/.claude/scripts/get_modules_by_depth.sh

# In Bash tool
bash(~/.claude/scripts/get_modules_by_depth.sh)

Content Search with ripgrep

# Search in specific file types
rg "pattern" --type js          # Search in JavaScript files
rg "pattern" --type ts          # Search in TypeScript files
rg "pattern" --type py          # Search in Python files

# Case-insensitive search
rg -i "case-insensitive"

# Show line numbers
rg -n "show-line-numbers"

# Show context lines
rg -A 3 -B 3 "context-lines"    # Show 3 lines before and after

# Exclude directories
rg "pattern" --glob '!node_modules'
rg "pattern" --glob '!*.test.js'

# Literal string search (no regex)
rg -F "exact.string.match"

# Multiple file types
rg "pattern" --type-add 'web:*.{html,css,js}' --type web

File Search with find

# Find TypeScript files
find . -name "*.ts" -type f

# Find files excluding node_modules
find . -path "*/node_modules" -prune -o -name "*.js" -print

# Find files by multiple patterns
find . \( -name "*.ts" -o -name "*.tsx" \) -type f

# Find and execute command on results
find . -name "*.test.js" -exec grep -l "describe" {} \;

grep Alternatives (when rg unavailable)

# Recursive search
grep -r "pattern" .

# Case-insensitive with line numbers
grep -n -i "pattern" file.txt

# Show context
grep -A 3 -B 3 "pattern" file.txt

# Search multiple files
grep "pattern" *.js

Workflow Integration Patterns

Pattern 1: Semantic Discovery → Content Search → Analysis

# Step 1: Discover relevant files
~/.claude/scripts/gemini-wrapper --all-files -p "List all files relevant to: authentication system"

# Step 2: Search within discovered files
rg "jwt|token|auth" --type ts

# Step 3: Read specific files for analysis
# Use Read tool on identified files

Pattern 2: Architecture Analysis for Planning

# MANDATORY before any planning task
~/.claude/scripts/get_modules_by_depth.sh

# Then proceed with planning based on structure

Pattern 3: Workflow System Exploration

# Search for task definitions
rg "IMPL-\d+" .workflow/ --type json

# Locate task files
find .workflow/ -name "*.json" -path "*/.task/*"

# Analyze workflow structure
rg "status.*pending" .workflow/.task/

# Show task dependencies
rg "depends_on" .workflow/.task/ -A 2

# Find active sessions
find .workflow/ -name ".active-*"

# Search for session references
rg "WFS-" .workflow/ --type json

Pattern 4: Code Pattern Analysis

# Find flow control patterns with context
rg "flow_control" .workflow/ -B 2 -A 5

# Find implementation plans
find . -name "IMPL_PLAN.md" -exec grep -l "requirements" {} \;

# Search for specific implementation patterns
rg "async.*function" --type ts -A 5

# Find error handling patterns
rg "try.*catch|throw new Error" --type js -B 2 -A 3

Pattern 5: Test Discovery

# Find all test files
find . -name "*.test.ts" -o -name "*.spec.ts"

# Search for specific test patterns
rg "describe\(|it\(|test\(" --type ts

# Find tests for specific functionality
rg "test.*authentication|describe.*auth" --type ts

# Find test files with specific assertions
rg "expect\(.*\)\.toBe" --type ts

Pattern 6: API Endpoint Discovery

# Find API route definitions
rg "app\.(get|post|put|delete|patch)" --type js

# Find Express router usage
rg "Router\(\)|express\.Router" --type ts

# Find API endpoint handlers
rg "@(Get|Post|Put|Delete|Patch)\(" --type ts

Pattern 7: Configuration File Discovery

# Find configuration files
find . -name "*.config.js" -o -name "*.config.ts" -o -name ".env*"

# Search for environment variables
rg "process\.env\." --type ts

# Find configuration patterns
rg "config\.|configuration" --type ts

Common Scenarios

Scenario 1: Understanding Authentication System

# 1. Find auth-related files
~/.claude/scripts/gemini-wrapper --all-files -p "List all files relevant to: authentication, JWT, sessions"

# 2. Search for auth implementations
rg "authenticate|authorization" --type ts

# 3. Find middleware usage
rg "middleware.*auth|auth.*middleware" --type ts

# 4. Locate test files
rg "auth.*test|describe.*auth" --type ts

Scenario 2: Database Schema Analysis

# 1. Find database files
find . -name "*schema*" -o -name "*migration*" -o -name "*model*"

# 2. Search for schema definitions
rg "Schema|model\.define|createTable" --type ts

# 3. Find database queries
rg "SELECT|INSERT|UPDATE|DELETE" --type sql

# 4. Find ORM usage
rg "findOne|findAll|create|update|destroy" --type ts

Scenario 3: Component Structure Exploration

# 1. Analyze architecture
~/.claude/scripts/get_modules_by_depth.sh

# 2. Find component files
find . -name "*.tsx" -o -name "*.jsx"

# 3. Search for component patterns
rg "export (default )?(function|const).*Component" --type tsx

# 4. Find component imports
rg "import.*from.*components" --type tsx

Scenario 4: Error Handling Audit

# 1. Find error handling patterns
rg "try.*catch|throw new" --type ts -B 1 -A 3

# 2. Find error classes
rg "class.*Error extends|extends Error" --type ts

# 3. Find error middleware
rg "errorHandler|error.*middleware" --type ts

# 4. Find error logging
rg "console\.error|logger\.error|log\.error" --type ts

Scenario 5: Dependency Analysis

# 1. Find package.json files
find . -name "package.json"

# 2. Search for specific package usage
rg "from ['\"]express['\"]|require\(['\"]express['\"]\)" --type ts

# 3. Find import patterns
rg "^import.*from" --type ts | head -50

# 4. Find dynamic imports
rg "import\(|require\(" --type ts

MCP Tool Integration Examples

Using Code Index Tools

# Search code patterns
mcp__code-index__search_code_advanced(pattern="function.*auth", file_pattern="*.ts")

# Find files by pattern
mcp__code-index__find_files(pattern="*.test.js")

# Find files with wildcard
mcp__code-index__find_files(pattern="*async*")

# Refresh index after git operations
mcp__code-index__refresh_index()

# Get file summary
mcp__code-index__get_file_summary(file_path="src/auth/index.ts")

Combined MCP and CLI Workflow

# 1. Find files with MCP
mcp__code-index__find_files(pattern="*auth*")

# 2. Search content with ripgrep
rg "jwt|token" --type ts

# 3. Get file summaries
mcp__code-index__get_file_summary(file_path="src/auth/jwt.ts")

# 4. Semantic discovery with Gemini
~/.claude/scripts/gemini-wrapper --all-files -p "List all files relevant to: authentication"

Performance Optimization Examples

Optimized Searches

# Fast: Use type filters
rg "pattern" --type ts

# Faster: Exclude directories
rg "pattern" --glob '!node_modules' --glob '!dist'

# Fastest: Literal search (no regex)
rg -F "exact.match" --type ts

# Very fast: Limit to specific directory
rg "pattern" src/ --type ts

Avoiding Slow Searches

# ❌ Slow: No type filter, includes node_modules
grep -r "pattern" .

# ✅ Fast: Type filter and exclusions
rg "pattern" --type ts --glob '!node_modules'

# ❌ Slow: Complex regex on large files
rg ".*complex.*regex.*pattern.*" .

# ✅ Fast: Simple patterns, literal when possible
rg -F "simple" --type ts

Troubleshooting Examples

When rg is not available

# Fall back to grep
grep -r "pattern" --include="*.ts" --exclude-dir="node_modules" .

# Or use find + grep
find . -name "*.ts" -not -path "*/node_modules/*" -exec grep -l "pattern" {} \;

When searching produces too many results

# Add more specific patterns
rg "function authenticate" --type ts

# Limit to specific directories
rg "pattern" src/auth/ --type ts

# Use word boundaries
rg "\bpattern\b" --type ts

# Show only filenames
rg "pattern" --type ts -l

When searching is too slow

# Use literal search
rg -F "literal.string" --type ts

# Limit search depth
find . -maxdepth 3 -name "*.ts"

# Search specific directory
rg "pattern" src/ --type ts

# Use file type filters
rg "pattern" --type-list  # List available types
rg "pattern" --type ts --type js