关于 investigate
investigate 是供开发者通过源码追踪问题的调试技能。它结合 ripgrep 搜索和 Git 历史检查,并借助 Gemini 分析错误、形成假设和梳理代码上下文。
主要功能
- 01使用 ripgrep 搜索代码和定位定义
- 02追踪函数调用及执行流程
- 03检查错误、API、查询和配置
- 04搜索 Git 历史中的变更
- 05使用 Gemini 分析错误并验证假设
适用场景
- 01追踪已报告错误的来源
- 02查找函数定义及其调用位置
- 03调查 API 端点或数据库查询
- 04针对 Bug 提出并验证假设
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ johnlindquist/ claude --skill investigate安装后在 Claude Code 中输入/investigate确认已加载。
详细介绍
调查工具包
通过系统化搜索和 AI 驱动的分析来调试问题。
前提条件
# ripgrep for fast search
brew install ripgrep
# Gemini for analysis
pip install google-generativeai
export GEMINI_API_KEY=your_api_key
搜索命令
ripgrep 基础
# Search for pattern
rg "pattern" src/
# Case insensitive
rg -i "error" src/
# Whole word
rg -w "user" src/
# File types
rg -t ts "function" src/
rg -t py "def " src/
# Exclude patterns
rg "TODO" --glob "!node_modules"
# Show context
rg -C 3 "error" src/ # 3 lines before and after
rg -B 5 "crash" src/ # 5 lines before
rg -A 5 "crash" src/ # 5 lines after
# Just filenames
rg -l "pattern" src/
# Count matches
rg -c "pattern" src/
查找定义
# Function definitions (TypeScript)
rg "function\s+functionName" src/
rg "(const|let|var)\s+functionName\s*=" src/
rg "export\s+(async\s+)?function\s+\w+" src/
# Class definitions
rg "class\s+ClassName" src/
# Interface/Type definitions
rg "(interface|type)\s+TypeName" src/
追踪用法
# Where is this function called?
rg "functionName\(" src/
# Where is this imported?
rg "import.*functionName" src/
# Where is this exported?
rg "export.*functionName" src/
调查模式
Bug 调查
# 1. Search for error message
rg "exact error message" .
# 2. Find where error is thrown
rg "throw.*Error" src/ -C 3
# 3. Trace the function
rg "functionThatFails" src/ -C 5
# 4. Check recent changes
git log --oneline -20 --all -- src/problematic-file.ts
git diff HEAD~5 -- src/problematic-file.ts
追踪执行流程
#!/bin/bash
ENTRY_POINT=$1
echo "=== Entry Point ==="
rg -A 10 "export.*$ENTRY_POINT" src/
echo "=== Called Functions ==="
rg -o "\w+\(" src/$ENTRY_POINT*.ts | sort -u
echo "=== Dependencies ==="
rg "^import" src/$ENTRY_POINT*.ts
AI 辅助调试
# Analyze error with context
ERROR="Your error message here"
CODE=$(cat problematic-file.ts)
gemini -m pro -o text -e "" "Debug this error:
ERROR: $ERROR
CODE:
$CODE
Provide:
1. Most likely cause
2. How to verify
3. How to fix
4. How to prevent in future"
假设检验
# Generate hypotheses
gemini -m pro -o text -e "" "Given this bug symptom:
SYMPTOM: [describe what's happening]
CONTEXT: [relevant code/system info]
Generate 5 hypotheses ranked by likelihood, with a test for each."
# Then test each hypothesis
rg "hypothesis-related-pattern" src/
常见调查
查找所有错误处理
rg "catch|\.catch|try\s*{" src/ -t ts
rg "throw\s+new" src/ -t ts
查找 API 端点
rg "(get|post|put|delete|patch)\s*\(" src/ -i
rg "router\.(get|post|put|delete)" src/
rg "@(Get|Post|Put|Delete)" src/
查找数据库查询
rg "(SELECT|INSERT|UPDATE|DELETE)" src/ -i
rg "\.query\(|\.execute\(" src/
rg "prisma\.\w+\.(find|create|update|delete)" src/
查找配置
rg "process\.env\." src/
rg "(config|settings)\[" src/
rg "getenv|os\.environ" src/ -t py
查找安全问题
# SQL injection potential
rg "query.*\+.*\"|'.*\+" src/
# Hardcoded secrets
rg "(password|secret|key|token)\s*=\s*['\"]" src/ -i
# Unsafe eval
rg "eval\(" src/
深入调查脚本
#!/bin/bash
# investigate.sh - Comprehensive code investigation
TERM=$1
echo "=== Investigating: $TERM ==="
echo ""
echo "### Definitions ###"
rg "^(export\s+)?(function|const|class|interface|type)\s+$TERM" src/
echo ""
echo "### Usage ###"
rg "$TERM" src/ --stats | head -50
echo ""
echo "### Recent Changes ###"
git log --oneline -10 -S "$TERM"
echo ""
echo "### Blame ###"
for f in $(rg -l "$TERM" src/); do
echo "--- $f ---"
git blame -L "/$TERM/,+5" "$f" 2>/dev/null | head -10
done
最佳实践
- 从错误开始 - 先搜索确切的错误消息
- 扩展上下文 - 使用
-C、-B、-A查看周围代码 - 检查历史记录 -
git log -S可找到代码是何时引入的 - 复杂情况使用 AI - 当模式匹配不够用时
- 记录发现 - 记下你的发现
- 检验假设 - 先验证再下结论
评论
支持 Markdown