Iterative Retrieval

affaan-m/ECC/skills/iterative-retrieval

作者 affaan-mef648e01899ba3e8dc6371642deaaf64b4477775無授權條款275K 個星標收錄於 2026年10月9日更新於 2026年10月9日儲存庫4 天前更新

Pattern for progressively refining context retrieval to solve the subagent context problem. Use when a subagent lacks the context it needs and retrieval must be refined across passes.

僅含說明AI & Agents
AI 產生的概覽

說明一套四階段迭代檢索模式,用於在多代理工作流程中逐步精煉提供給子代理的上下文。

功能
此技能記錄了一套在多代理工作流程中逐步精煉上下文檢索的模式,用來處理子代理在開始工作前並不知道自己需要哪些上下文的問題。它定義了四階段循環——DISPATCH、EVALUATE、REFINE、LOOP——並提供相關性評分標準、查詢精煉邏輯,以及最多三輪的限制。它也提供缺陷修正與功能實作兩類上下文蒐集的範例,並說明如何把此模式嵌入代理提示詞中。
適用情境
當需要派生子代理、而這些子代理無法事先預測所需的程式碼庫上下文時使用;也適用於上下文會逐步精煉的多代理工作流程。它同樣適用於因上下文過大或缺少上下文而失敗的代理任務,以及為程式碼探索設計類 RAG 檢索流程。
執行需求
不需要指令碼或執行環境相依項目,僅為純指示型的模式文件。範例中的檢索與評分函式屬於概念性說明;文件包含外部連結,並提及一個選用的相關技能與隨附的代理定義。

Iterative Retrieval Pattern

Solves the "context problem" in multi-agent workflows where subagents don't know what context they need until they start working.

When to Activate

  • Spawning subagents that need codebase context they cannot predict upfront
  • Building multi-agent workflows where context is progressively refined
  • Encountering "context too large" or "missing context" failures in agent tasks
  • Designing RAG-like retrieval pipelines for code exploration
  • Optimizing token usage in agent orchestration

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

Standard approaches fail:

  • Send everything: Exceeds context limits
  • Send nothing: Agent lacks critical information
  • Guess what's needed: Often wrong

The Solution: Iterative Retrieval

A 4-phase loop that progressively refines context:

┌─────────────────────────────────────────────┐│                                             ││   ┌──────────┐      ┌──────────┐            ││   │ DISPATCH │─────│ EVALUATE │            ││   └──────────┘      └──────────┘            ││        ▲                  │                 ││        │                  ▼                 ││   ┌──────────┐      ┌──────────┐            ││   │   LOOP   │─────│  REFINE  │            ││   └──────────┘      └──────────┘            ││                                             ││        Max 3 cycles, then proceed           │└─────────────────────────────────────────────┘

Phase 1: DISPATCH

Initial broad query to gather candidate files:

javascript
// Start with high-level intentconst initialQuery = {  patterns: ['src/**/*.ts', 'lib/**/*.ts'],  keywords: ['authentication', 'user', 'session'],  excludes: ['*.test.ts', '*.spec.ts']};
// Dispatch to retrieval agentconst candidates = await retrieveFiles(initialQuery);

Phase 2: EVALUATE

Assess retrieved content for relevance:

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)  }));}

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

Phase 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)  };}

Phase 4: LOOP

Repeat with refined criteria (max 3 cycles):

javascript
async function iterativeRetrieve(task, maxCycles = 3) {  let query = createInitialQuery(task);  let bestContext = [];
  for (let cycle = 0; cycle < maxCycles; cycle++) {    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);  }
  return bestContext;}

Practical Examples

Example 1: Bug Fix Context

Task: "Fix the authentication token expiry 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
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)
Result: auth.ts, tokens.ts, session-manager.ts, jwt-utils.ts

Example 2: Feature Implementation

Task: "Add rate limiting to API endpoints"
Cycle 1:  DISPATCH: Search "rate", "limit", "api" in routes/**  EVALUATE: No matches - codebase uses "throttle" terminology  REFINE: Add "throttle", "middleware" keywords
Cycle 2:  DISPATCH: Search refined terms  EVALUATE: Found throttle.ts (0.9), middleware/index.ts (0.7)  REFINE: Need router patterns
Cycle 3:  DISPATCH: Search "router", "express" patterns  EVALUATE: Found router-setup.ts (0.8)  REFINE: Sufficient context
Result: throttle.ts, middleware/index.ts, router-setup.ts

Integration with Agents

Use in agent prompts:

markdown
When retrieving context for this task:1. Start with broad keyword search2. Evaluate each file's relevance (0-1 scale)3. Identify what context is still missing4. Refine search criteria and repeat (max 3 cycles)5. Return files with relevance >= 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

Related

  • The Longform Guide - Subagent orchestration section
  • continuous-learning skill - For patterns that improve over time
  • Agent definitions bundled with ECC (manual install path: agents/)

來源與署名

來源:affaan-m/ECC位於skills/iterative-retrieval提交ef648e0

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架