
Iterative Retrieval
为了解决子智能体(Sub-agent)的上下文问题,逐步优化上下文获取的模式
Install
npx skills add xu-xiang/everything-claude-code-zh --skill iterative-retrieval -g -a claude-code -y- Repository
- xu-xiang/everything-claude-code-zh2k
- Category
- AI and agents
- Updated
- 8mo ago
- License
- MIT
- Context cost
- Lightabout 1,080 tokens
- Runs scripts
- No, instructions only
What it does
为了解决子智能体(Sub-agent)的上下文问题,逐步优化上下文获取的模式
反复检索模式 (Iterative Retrieval Pattern)
解决多智能体工作流(Multi-agent Workflow)中的“上下文问题”。子智能体在开始工作之前,通常不知道需要哪些上下文。
问题
子智能体(Sub-agent)在启动时上下文有限。它们通常不知道:
- 哪些文件包含相关的代码
- 代码库中存在哪些模式(Patterns)
- 项目使用什么术语
标准方法往往会失败:
- 发送全部:超出上下文限制(Context Limit)
- 什么都不发:智能体缺乏关键信息
- 猜测所需内容:经常出错
解决方案:反复检索
分为 4 个阶段的循环,逐步优化上下文:
┌─────────────────────────────────────────────┐
│ │
│ ┌──────────┐ ┌──────────┐ │
│ │ DISPATCH │─────▶│ EVALUATE │ │
│ └──────────┘ └──────────┘ │
│ ▲ │ │
│ │ ▼ │
│ ┌──────────┐ ┌──────────┐ │
│ │ LOOP │◀─────│ REFINE │ │
│ └──────────┘ └──────────┘ │
│ │
│ 最大 3 个循环,之后继续执行 │
└─────────────────────────────────────────────┘
阶段 1: 派发 (DISPATCH)
收集候选文件的初始宽泛查询:
// 从高层意图开始
const initialQuery = {
patterns: ['src/**/*.ts', 'lib/**/*.ts'],
keywords: ['authentication', 'user', 'session'],
excludes: ['*.test.ts', '*.spec.ts']
};
// 派发给检索智能体
const candidates = await retrieveFiles(initialQuery);
阶段 2: 评估 (EVALUATE)
评估获取内容的关联性(Relevance):
function evaluateRelevance(files, task) {
return files.map(file => ({
path: file.path,
relevance: scoreRelevance(file.content, task),
reason: explainRelevance(file.content, task),
missingContext: identifyGaps(file.content, task)
}));
}
评分标准:
- 高 (0.8-1.0):直接实现目标功能
- 中 (0.5-0.7):包含相关的模式或类型
- 低 (0.2-0.4):间接相关
- 无 (0-0.2):不相关,排除
阶段 3: 优化 (REFINE)
根据评估更新检索标准:
function refineQuery(evaluation, previousQuery) {
return {
// 添加在高关联性文件中发现的新模式
patterns: [...previousQuery.patterns, ...extractPatterns(evaluation)],
// 添加在代码库中发现的术语
keywords: [...previousQuery.keywords, ...extractKeywords(evaluation)],

