关于 search-hierarchy
search-hierarchy 是一项按查询类型和输出规模选择搜索方法的编码技能,指导使用 AST-grep 查找代码结构、LEANN 探索概念问题、Grep 搜索字面匹配。
主要功能
- 01结构查询使用 AST-grep
- 02概念查询使用 LEANN
- 03Grep 用于字面匹配
- 04定位文件后再使用 Read
- 05Hook 拦截并推荐搜索工具
适用场景
- 01查找函数或类定义
- 02了解架构或行为
- 03定位确切标识符或路径
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ parcadei/ continuous-claude-v3 --skill search-hierarchy安装后在 Claude Code 中输入/search-hierarchy确认已加载。
详细介绍
搜索工具层级
针对每类查询,使用令牌效率最高的搜索工具。
决策树
Query Type?
├── STRUCTURAL (code patterns)
│ → AST-grep (~50 tokens output)
│ Examples: "def foo", "class Bar", "import X", "@decorator"
│
├── SEMANTIC (conceptual questions)
│ → LEANN (~100 tokens if path-only)
│ Examples: "how does auth work", "find error handling patterns"
│
├── LITERAL (exact identifiers)
│ → Grep (variable output)
│ Examples: "TemporalMemory", "check_evocation", regex patterns
│
└── FULL CONTEXT (need complete understanding)
→ Read (1500+ tokens)
Last resort after finding the right file
令牌效率对比
| 工具 | 输出大小 | 最适用场景 |
|---|---|---|
| AST-grep | ~50 tokens | 函数/类定义、导入、装饰器 |
| LEANN | ~100 tokens | 概念性问题、架构、模式 |
| Grep | ~200-2000 | 精确标识符、正则表达式、文件路径 |
| Read | ~1500+ | 找到文件后的完整理解 |
Hook 强制规则
grep-to-leann.sh hook 会自动:
- 检测查询类型(结构性/语义/字面量)
- 拦截结构性查询,并建议使用 AST-grep
- 拦截语义查询,并建议使用 LEANN
- 允许字面量模式通过并交给 Grep
应做事项
- 查询代码结构时,从 AST-grep 开始
- 对于“X 如何运作”这类问题,使用 LEANN
- 仅在精确匹配标识符时使用 Grep
- 只有通过搜索找到文件后才读取文件
不应做事项
- 不要用 Grep 查询概念性问题(不会返回结果)
- 不要在确定哪些文件相关之前读取文件
- 当 AST-grep 能提供 file:line 时,不要使用 Read
- 不要忽略 hook 的建议
示例
# STRUCTURAL → AST-grep
ast-grep --pattern "async def $FUNC($$$):" --lang python
# SEMANTIC → LEANN
leann search opc-dev "how does authentication work" --top-k 3
# LITERAL → Grep
Grep pattern="check_evocation" path=opc/scripts
# FULL CONTEXT → Read (after finding file)
Read file_path=opc/scripts/z3_erotetic.py
最佳流程
1. AST-grep: "Find async functions" → 3 file:line matches
2. Read: Top match only → Full understanding
3. Skip: 4 irrelevant files → 6000 tokens saved
评论
支持 Markdown