Communitygithub.com

迭代式檢索

一種逐步精煉背景資訊檢索的模式,用於解決子代理的背景資訊不足問題。當子代理缺乏所需的上下文,且需要跨回合精煉檢索時使用。

迭代式檢索 是什麼?

迭代式檢索模式是為了解決多代理工作流程中常見的「背景資訊問題」而設計的。在這種情況下,子代理在開始工作之前,無法預先知道需要哪些程式碼庫上下文。此模式透過一個包含「調度」、「評估」、「精煉」和「循環」四個階段的循環,逐步縮小並優化檢索結果,以在不超出 Token 限制的情況下,為子代理提供最相關的檔案。它支援基於任何語言模型 (LLM) 的代理系統,並可用於提升代碼探索、錯誤修復和功能實作的效率。核心優點在於它能從錯誤中學習:初始搜尋的不足(例如使用了錯誤的術語)會被識別出來,並在下一輪循環中用於修正和擴展搜尋條件,確保系統能夠動態適應程式碼庫的實際結構與命名慣例。最大循環次數預設為 3 次,以平衡檢索品質與效能。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/affaan-m/everything-claude-code/tree/main/skills/iterative-retrieval

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

迭代式檢索模式 (Iterative Retrieval Pattern)

解決多代理工作流程中的「背景資訊問題」,即子代理在開始工作前無法預知所需上下文的情況。

何時啟用

  • 需要為子代理提供其無法事先預測的程式碼庫上下文時
  • 建構需要逐步精煉上下文的多代理工作流程時
  • 遇到因「上下文過大」或「遺失上下文」而導致的代理任務失敗時
  • 設計用於程式碼探索的 RAG(檢索增強生成)檢索管線時
  • 最佳化代理協調過程中的 Token 使用量時

問題

子代理被賦予有限的上下文。它們不知道:

  • 哪些檔案包含相關程式碼
  • 程式碼庫中存在哪些模式
  • 專案使用了哪些術語

標準方法均會失敗:

  • 全部發送:超出上下文限制
  • 什麼都不發送:代理缺乏關鍵資訊
  • 猜測所需內容:通常猜錯

解決方案:迭代式檢索

一個由 4 個階段組成的循環,逐步精煉上下文:

┌─────────────────────────────────────────────┐
│                                             │
│   ┌──────────┐      ┌──────────┐            │
│   │ 調度    │─────│ 評估    │            │
│   └──────────┘      └──────────┘            │
│        ▲                  │                 │
│        │                  ▼                 │
│   ┌──────────┐      ┌──────────┐            │
│   │ 循環    │─────│ 精煉    │            │
│   └──────────┘      └──────────┘            │
│                                             │
│        最多 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)

評估檢索到的內容之相關性:

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)],

    // 排除已確認為不相關的路徑
    excludes: [...previousQuery.excludes, ...evaluation
      .filter(e => e.relevance < 0.2)
      .map(e => e.path)
    ],

    // 針對特定缺口
    focusAreas: evaluation
      .flatMap(e => e.missingContext)
      .filter(unique)
  };
}

階段 4:循環 (LOOP)

使用精煉後的條件重複執行(最多 3 次循環):

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

    // 檢查是否已擁有足夠的上下文
    const highRelevance = evaluation.filter(e => e.relevance >= 0.7);
    if (highRelevance.length >= 3 && !hasCriticalGaps(evaluation)) {
      return highRelevance;
    }

    // 精煉並繼續
    query = refineQuery(evaluation, query);
    bestContext = mergeContext(bestContext, highRelevance);
  }

  return bestContext;
}

實務範例

範例 1:錯誤修正上下文

任務:「修復認證 Token 過期錯誤」

循環 1:
  調度:在 src/** 中搜尋 "token"、"auth"、"expiry"
  評估:找到 auth.ts (0.9)、tokens.ts (0.8)、user.ts (0.3)
  精煉:新增 "refresh"、"jwt" 關鍵字;排除 user.ts

循環 2:
  調度:使用精煉後的術語搜尋
  評估:找到 session-manager.ts (0.95)、jwt-utils.ts (0.85)
  精煉:上下文已足夠(2 個高相關性檔案)

結果:auth.ts、tokens.ts、session-manager.ts、jwt-utils.ts

範例 2:功能實作

任務:「為 API 端點新增速率限制」

循環 1:
  調度:在 routes/** 中搜尋 "rate"、"limit"、"api"
  評估:無相符結果 - 程式碼庫使用 "throttle" 術語
  精煉:新增 "throttle"、"middleware" 關鍵字

循環 2:
  調度:使用精煉後的術語搜尋
  評估:找到 throttle.ts (0.9)、middleware/index.ts (0.7)
  精煉:需要路由器模式

循環 3:
  調度:搜尋 "router"、"express" 模式
  評估:找到 router-setup.ts (0.8)
  精煉:上下文已足夠

結果:throttle.ts、middleware/index.ts、router-setup.ts

與代理的整合

在代理提示中使用:

為此任務檢索上下文時:
1. 從廣泛的關鍵字搜尋開始
2. 評估每個檔案的相關性(0-1 級別)
3. 識別仍缺少哪些上下文
4. 精煉搜尋條件並重複(最多 3 次循環)
5. 回傳相關性 >= 0.7 的檔案

最佳實踐

  1. 由廣到窄,逐步收斂 - 不要過度指定初始查詢
  2. 學習程式碼庫術語 - 第一個循環通常能揭示命名慣例
  3. 追蹤遺漏內容 - 明確識別缺口以驅動精煉方向
  4. 在「夠好」時停止 - 3 個高度相關的檔案勝過 10 個平庸的
  5. 果斷排除 - 低相關性的檔案不太可能變得相關

相關資源

  • The Longform Guide - 子代理協調章節
  • continuous-learning 技能 - 針對隨時間改進的模式
  • 與 ECC 一同定義的代理(手動安裝路徑:agents/)

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

accessibility

Design, implement, and audit inclusive digital products using WCAG 2.2 Level AA. Use when building or auditing UI that must meet WCAG 2.2 Level AA, or when reviewing a change for keyboard, contrast, or screen-reader support.

affaan-m/claude-api

Anthropic Claude API patterns for Python and TypeScript. Covers Messages API, streaming, tool use, vision, extended thinking, batches, prompt caching, and Claude Agent SDK. Use when building applications with the Claude API or Anthropic SDKs.

affaan-m/everything-claude-code

End-to-end marketing campaign planning and execution. Covers audience research, positioning, campaign angle definition, landing page copy, email sequences, social posts, ad copy, short-form video scripts, and content calendars. Use as the orchestration layer for multi-channel product launches. Use when planning or executing a multi-channel product launch, or producing landing page, email, social, or ad copy.

affaan-m/everything-claude-code

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/everything-claude-code-conventions

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/frontend-design

Create distinctive, production-grade frontend interfaces with high design quality. Use when the user asks to build web components, pages, or applications and the visual direction matters as much as the code quality.

affaan-m/gget

gget CLI and Python workflow for quick genomic database queries, sequence lookup, BLAST-style searches, enrichment checks, and reproducible bioinformatics evidence logs.

affaan-m/literature-review

Systematic literature-review workflow for academic, biomedical, technical, and scientific topics, including search planning, source screening, synthesis, citation checks, and evidence logging.

affaan-m/motion-ui

Production-ready UI motion system for React/Next.js. Use when implementing animations, transitions, or motion patterns.

affaan-m/project-guidelines-example

Example project-specific skill template based on a real production application.

affaan-m/pubmed-database

Direct PubMed and NCBI E-utilities search workflows for biomedical literature, MeSH queries, PMID lookup, citation retrieval, and API-backed literature monitoring.

affaan-m/scholar-evaluation

Structured scholarly-work evaluation for papers, proposals, literature reviews, methods sections, evidence quality, citation support, and research-writing feedback.

affaan-m/uspto-database

USPTO patent and trademark data workflow for official record lookup, PatentSearch queries, TSDR checks, assignment data, and reproducible IP research logs.

agent-architecture-audit

Full-stack diagnostic for agent and LLM applications. Audits the 12-layer agent stack for wrapper regression, memory pollution, tool discipline failures, hidden repair loops, and rendering corruption. Produces severity-ranked findings with code-first fixes. Essential for developers building agent applications, autonomous loops, or any LLM-powered feature. Use when an agent or LLM feature misbehaves and the failing layer is unknown, or before shipping an agent stack.

agent-eval

Head-to-head comparison of coding agents (Claude Code, Aider, Codex, etc.) on custom tasks with pass rate, cost, time, and consistency metrics. Use when choosing between coding agents, or when a change to an agent setup needs measured pass rate, cost, and time rather than an impression.

agent-harness-construction

Design and optimize AI agent action spaces, tool definitions, and observation formatting for higher completion rates. Use when defining or revising an agent

agentic-engineering

Operate as an agentic engineer using eval-first execution, decomposition, and cost-aware model routing. Use when planning or executing engineering work that agents will carry out end to end.

agentic-os

Build persistent multi-agent operating systems on Claude Code. Covers kernel architecture, specialist agents, slash commands, file-based memory, scheduled automation, and state management without external databases. Use when building a persistent multi-agent system on Claude Code with its own memory, commands, and scheduling.

agent-introspection-debugging

Structured self-debugging workflow for AI agent failures using capture, diagnosis, contained recovery, and introspection reports. Use when an agent run fails and you need a reproducible diagnosis instead of a retry.

agent-payment-x402

Add x402 payment execution to AI agents with per-task budgets, spending controls, and non-custodial wallets. Supports Base through agentwallet-sdk and X Layer through OKX Payments / OKX Agent Payments Protocol. Use when an agent must pay for something itself and needs per-task budgets, spending controls, and a non-custodial wallet.

相關技能