mirror of
https://github.com/sweetwisdom/everything-claude-code-zh.git
synced 2026-03-22 06:20:10 +00:00
docs: 完成所有文档的中文翻译并应用到项目
This commit is contained in:
@@ -1,27 +1,27 @@
|
||||
---
|
||||
name: iterative-retrieval
|
||||
description: Pattern for progressively refining context retrieval to solve the subagent context problem
|
||||
description: 用于逐步优化上下文检索以解决子智能体(subagent)上下文问题的模式
|
||||
---
|
||||
|
||||
# Iterative Retrieval Pattern
|
||||
# 迭代检索模式(Iterative Retrieval Pattern)
|
||||
|
||||
Solves the "context problem" in multi-agent workflows where subagents don't know what context they need until they start working.
|
||||
解决多智能体工作流中的“上下文问题”,即子智能体(subagent)在开始工作前不知道自己需要哪些上下文。
|
||||
|
||||
## The Problem
|
||||
## 问题(The Problem)
|
||||
|
||||
Subagents are spawned with limited context. They don't know:
|
||||
- Which files contain relevant code
|
||||
- What patterns exist in the codebase
|
||||
- What terminology the project uses
|
||||
子智能体启动时只有有限的上下文。它们不知道:
|
||||
- 哪些文件包含相关的代码
|
||||
- 代码库中存在哪些模式(Patterns)
|
||||
- 项目使用了哪些术语
|
||||
|
||||
Standard approaches fail:
|
||||
- **Send everything**: Exceeds context limits
|
||||
- **Send nothing**: Agent lacks critical information
|
||||
- **Guess what's needed**: Often wrong
|
||||
标准方法往往会失败:
|
||||
- **发送所有内容**:超出上下文限制
|
||||
- **什么都不发**:智能体(Agent)缺乏关键信息
|
||||
- **猜测需要什么**:经常出错
|
||||
|
||||
## The Solution: Iterative Retrieval
|
||||
## 解决方案:迭代检索(Iterative Retrieval)
|
||||
|
||||
A 4-phase loop that progressively refines context:
|
||||
一个分为 4 个阶段的循环,用于逐步优化上下文:
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────┐
|
||||
@@ -35,29 +35,29 @@ A 4-phase loop that progressively refines context:
|
||||
│ │ LOOP │◀─────│ REFINE │ │
|
||||
│ └──────────┘ └──────────┘ │
|
||||
│ │
|
||||
│ Max 3 cycles, then proceed │
|
||||
│ 最多 3 个循环,然后继续执行 │
|
||||
└─────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
### Phase 1: DISPATCH
|
||||
### 阶段 1:分发(DISPATCH)
|
||||
|
||||
Initial broad query to gather candidate files:
|
||||
初始的广泛查询,用于收集候选文件:
|
||||
|
||||
```javascript
|
||||
// Start with high-level intent
|
||||
// 从高层意图开始
|
||||
const initialQuery = {
|
||||
patterns: ['src/**/*.ts', 'lib/**/*.ts'],
|
||||
keywords: ['authentication', 'user', 'session'],
|
||||
excludes: ['*.test.ts', '*.spec.ts']
|
||||
};
|
||||
|
||||
// Dispatch to retrieval agent
|
||||
// 分发给检索智能体
|
||||
const candidates = await retrieveFiles(initialQuery);
|
||||
```
|
||||
|
||||
### Phase 2: EVALUATE
|
||||
### 阶段 2:评估(EVALUATE)
|
||||
|
||||
Assess retrieved content for relevance:
|
||||
评估检索到的内容的关联度:
|
||||
|
||||
```javascript
|
||||
function evaluateRelevance(files, task) {
|
||||
@@ -70,32 +70,32 @@ function evaluateRelevance(files, task) {
|
||||
}
|
||||
```
|
||||
|
||||
Scoring criteria:
|
||||
- **High (0.8-1.0)**: Directly implements target functionality
|
||||
- **Medium (0.5-0.7)**: Contains related patterns or types
|
||||
- **Low (0.2-0.4)**: Tangentially related
|
||||
- **None (0-0.2)**: Not relevant, exclude
|
||||
评分标准:
|
||||
- **高 (0.8-1.0)**:直接实现了目标功能
|
||||
- **中 (0.5-0.7)**:包含相关的模式或类型
|
||||
- **低 (0.2-0.4)**:有间接关联
|
||||
- **无 (0-0.2)**:无关,排除
|
||||
|
||||
### Phase 3: REFINE
|
||||
### 阶段 3:优化(REFINE)
|
||||
|
||||
Update search criteria based on evaluation:
|
||||
根据评估结果更新搜索标准:
|
||||
|
||||
```javascript
|
||||
function refineQuery(evaluation, previousQuery) {
|
||||
return {
|
||||
// Add new patterns discovered in high-relevance files
|
||||
// 添加在高关联度文件中发现的新模式
|
||||
patterns: [...previousQuery.patterns, ...extractPatterns(evaluation)],
|
||||
|
||||
// Add terminology found in codebase
|
||||
// 添加在代码库中发现的术语
|
||||
keywords: [...previousQuery.keywords, ...extractKeywords(evaluation)],
|
||||
|
||||
// Exclude confirmed irrelevant paths
|
||||
// 排除已确认的无关路径
|
||||
excludes: [...previousQuery.excludes, ...evaluation
|
||||
.filter(e => e.relevance < 0.2)
|
||||
.map(e => e.path)
|
||||
],
|
||||
|
||||
// Target specific gaps
|
||||
// 针对特定缺口
|
||||
focusAreas: evaluation
|
||||
.flatMap(e => e.missingContext)
|
||||
.filter(unique)
|
||||
@@ -103,9 +103,9 @@ function refineQuery(evaluation, previousQuery) {
|
||||
}
|
||||
```
|
||||
|
||||
### Phase 4: LOOP
|
||||
### 阶段 4:循环(LOOP)
|
||||
|
||||
Repeat with refined criteria (max 3 cycles):
|
||||
使用优化后的标准重复该过程(最多 3 个循环):
|
||||
|
||||
```javascript
|
||||
async function iterativeRetrieve(task, maxCycles = 3) {
|
||||
@@ -116,13 +116,13 @@ async function iterativeRetrieve(task, maxCycles = 3) {
|
||||
const candidates = await retrieveFiles(query);
|
||||
const evaluation = evaluateRelevance(candidates, task);
|
||||
|
||||
// Check if we have sufficient context
|
||||
// 检查我们是否已经拥有足够的上下文
|
||||
const highRelevance = evaluation.filter(e => e.relevance >= 0.7);
|
||||
if (highRelevance.length >= 3 && !hasCriticalGaps(evaluation)) {
|
||||
return highRelevance;
|
||||
}
|
||||
|
||||
// Refine and continue
|
||||
// 优化并继续
|
||||
query = refineQuery(evaluation, query);
|
||||
bestContext = mergeContext(bestContext, highRelevance);
|
||||
}
|
||||
@@ -131,72 +131,72 @@ async function iterativeRetrieve(task, maxCycles = 3) {
|
||||
}
|
||||
```
|
||||
|
||||
## Practical Examples
|
||||
## 实践示例
|
||||
|
||||
### Example 1: Bug Fix Context
|
||||
### 示例 1:Bug 修复上下文
|
||||
|
||||
```
|
||||
Task: "Fix the authentication token expiry bug"
|
||||
任务:“修复身份验证令牌过期 bug”
|
||||
|
||||
Cycle 1:
|
||||
DISPATCH: Search for "token", "auth", "expiry" in src/**
|
||||
EVALUATE: Found auth.ts (0.9), tokens.ts (0.8), user.ts (0.3)
|
||||
REFINE: Add "refresh", "jwt" keywords; exclude user.ts
|
||||
循环 1:
|
||||
分发(DISPATCH):在 src/** 中搜索 "token"、"auth"、"expiry"
|
||||
评估(EVALUATE):发现 auth.ts (0.9)、tokens.ts (0.8)、user.ts (0.3)
|
||||
优化(REFINE):添加 "refresh"、"jwt" 关键字;排除 user.ts
|
||||
|
||||
Cycle 2:
|
||||
DISPATCH: Search refined terms
|
||||
EVALUATE: Found session-manager.ts (0.95), jwt-utils.ts (0.85)
|
||||
REFINE: Sufficient context (2 high-relevance files)
|
||||
循环 2:
|
||||
分发(DISPATCH):搜索优化后的术语
|
||||
评估(EVALUATE):发现 session-manager.ts (0.95)、jwt-utils.ts (0.85)
|
||||
优化(REFINE):上下文已足够(2 个高关联度文件)
|
||||
|
||||
Result: auth.ts, tokens.ts, session-manager.ts, jwt-utils.ts
|
||||
结果:auth.ts, tokens.ts, session-manager.ts, jwt-utils.ts
|
||||
```
|
||||
|
||||
### Example 2: Feature Implementation
|
||||
### 示例 2:功能实现
|
||||
|
||||
```
|
||||
Task: "Add rate limiting to API endpoints"
|
||||
任务:“为 API 端点添加速率限制(rate limiting)”
|
||||
|
||||
Cycle 1:
|
||||
DISPATCH: Search "rate", "limit", "api" in routes/**
|
||||
EVALUATE: No matches - codebase uses "throttle" terminology
|
||||
REFINE: Add "throttle", "middleware" keywords
|
||||
循环 1:
|
||||
分发(DISPATCH):在 routes/** 中搜索 "rate"、"limit"、"api"
|
||||
评估(EVALUATE):无匹配项 —— 代码库使用了 "throttle" 术语
|
||||
优化(REFINE):添加 "throttle"、"middleware" 关键字
|
||||
|
||||
Cycle 2:
|
||||
DISPATCH: Search refined terms
|
||||
EVALUATE: Found throttle.ts (0.9), middleware/index.ts (0.7)
|
||||
REFINE: Need router patterns
|
||||
循环 2:
|
||||
分发(DISPATCH):搜索优化后的术语
|
||||
评估(EVALUATE):发现 throttle.ts (0.9)、middleware/index.ts (0.7)
|
||||
优化(REFINE):需要路由模式
|
||||
|
||||
Cycle 3:
|
||||
DISPATCH: Search "router", "express" patterns
|
||||
EVALUATE: Found router-setup.ts (0.8)
|
||||
REFINE: Sufficient context
|
||||
循环 3:
|
||||
分发(DISPATCH):搜索 "router"、"express" 模式
|
||||
评估(EVALUATE):发现 router-setup.ts (0.8)
|
||||
优化(REFINE):上下文已足够
|
||||
|
||||
Result: throttle.ts, middleware/index.ts, router-setup.ts
|
||||
结果:throttle.ts, middleware/index.ts, router-setup.ts
|
||||
```
|
||||
|
||||
## Integration with Agents
|
||||
## 与智能体(Agents)集成
|
||||
|
||||
Use in agent prompts:
|
||||
在智能体提示词(Prompts)中使用:
|
||||
|
||||
```markdown
|
||||
When retrieving context for this task:
|
||||
1. Start with broad keyword search
|
||||
2. Evaluate each file's relevance (0-1 scale)
|
||||
3. Identify what context is still missing
|
||||
4. Refine search criteria and repeat (max 3 cycles)
|
||||
5. Return files with relevance >= 0.7
|
||||
为此任务检索上下文时:
|
||||
1. 从广泛的关键字搜索开始
|
||||
2. 评估每个文件的关联度(0-1 评分)
|
||||
3. 识别仍缺失的上下文
|
||||
4. 优化搜索标准并重复(最多 3 个循环)
|
||||
5. 返回关联度 >= 0.7 的文件
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
## 最佳实践
|
||||
|
||||
1. **Start broad, narrow progressively** - Don't over-specify initial queries
|
||||
2. **Learn codebase terminology** - First cycle often reveals naming conventions
|
||||
3. **Track what's missing** - Explicit gap identification drives refinement
|
||||
4. **Stop at "good enough"** - 3 high-relevance files beats 10 mediocre ones
|
||||
5. **Exclude confidently** - Low-relevance files won't become relevant
|
||||
1. **先广后窄,逐步收敛** —— 不要过度设定初始查询。
|
||||
2. **学习代码库术语** —— 第一个循环通常能揭示命名规范。
|
||||
3. **跟踪缺失内容** —— 明确地识别差距(Gap)是优化的动力。
|
||||
4. **见好就收** —— 3 个高关联度的文件优于 10 个平庸的文件。
|
||||
5. **果断排除** —— 低关联度的文件不会突然变得相关。
|
||||
|
||||
## Related
|
||||
## 相关资源
|
||||
|
||||
- [The Longform Guide](https://x.com/affaanmustafa/status/2014040193557471352) - Subagent orchestration section
|
||||
- `continuous-learning` skill - For patterns that improve over time
|
||||
- Agent definitions in `~/.claude/agents/`
|
||||
- [长篇指南(The Longform Guide)](https://x.com/affaanmustafa/status/2014040193557471352) —— 子智能体编排部分
|
||||
- `continuous-learning` 技能 —— 用于随时间改进的模式
|
||||
- `~/.claude/agents/` 中的智能体定义
|
||||
|
||||
Reference in New Issue
Block a user