Back to Skills

iterative-retrieval

verified

用于逐步优化上下文检索以解决子智能体(subagent)上下文问题的模式

View on GitHub

Marketplace

everything-claude-code

xu-xiang/everything-claude-code-zh

Plugin

everything-claude-code

workflow

Repository

xu-xiang/everything-claude-code-zh
25stars

skills/iterative-retrieval/SKILL.md

Last Verified

February 5, 2026

Install Skill

Select agents to install to:

Scope:
npx add-skill https://github.com/xu-xiang/everything-claude-code-zh/blob/main/skills/iterative-retrieval/SKILL.md -a claude-code --skill iterative-retrieval

Installation paths:

Claude
.claude/skills/iterative-retrieval/
Powered by add-skill CLI

Instructions

# 迭代检索模式(Iterative Retrieval Pattern)

解决多智能体工作流中的“上下文问题”,即子智能体(subagent)在开始工作前不知道自己需要哪些上下文。

## 问题(The Problem)

子智能体启动时只有有限的上下文。它们不知道:
- 哪些文件包含相关的代码
- 代码库中存在哪些模式(Patterns)
- 项目使用了哪些术语

标准方法往往会失败:
- **发送所有内容**:超出上下文限制
- **什么都不发**:智能体(Agent)缺乏关键信息
- **猜测需要什么**:经常出错

## 解决方案:迭代检索(Iterative Retrieval)

一个分为 4 个阶段的循环,用于逐步优化上下文:

```
┌─────────────────────────────────────────────┐
│                                             │
│   ┌──────────┐      ┌──────────┐            │
│   │ DISPATCH │─────▶│ EVALUATE │            │
│   └──────────┘      └──────────┘            │
│        ▲                  │                 │
│        │                  ▼                 │
│   ┌──────────┐      ┌──────────┐            │
│   │   LOOP   │◀─────│  REFINE  │            │
│   └──────────┘      └──────────┘            │
│                                             │
│        最多 3 个循环,然后继续执行          │
└─────────────────────────────────────────────┘
```

### 阶段 1:分发(DISPATCH)

初始的广泛查询,用于收集候选文件:

```javascript
// 从高层意图开始
const initialQuery = {
  patterns: ['src/**/*.ts', 'lib/**/*.ts'],
  keywords: ['authentication', 'user', 'session'],
  excludes: ['*.test.ts', '*.spec.ts']
};

// 分发给检索智能体
const candidates = await retrieveFiles(initialQuery);
```

### 阶段 2:评估(EVALUATE)

评估检索到的内容的关联度:

```javascript
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)

根据评估结果更新搜索标准:

```javascript
function refineQuery(evaluation, previousQuery) {
  return {
    // 添加在高关联度文件中发现的新模式
    patterns: [...previousQuery.patterns, ...extractPatterns(evaluation)],

    // 添加在代码库中发现的术语
    keywords: [...previousQuery.keywords, ...extractKeywords(evaluation)],

    // 排除已确认的无关路径
    

Validation Details

Front Matter
Required Fields
Valid Name Format
Valid Description
Has Sections
Allowed Tools
Instruction Length:
3833 chars