Agent skill
sk-deep-research
Autonomous deep research loop protocol with iterative investigation, externalized state, convergence detection, and fresh context per iteration
Install this agent skill to your Project
npx add-skill https://github.com/MichelKerkmeester/opencode--spec-kit-skilled-agent-orchestration/tree/main/.opencode/skill/sk-deep-research
SKILL.md
Autonomous Deep Research Loop
Iterative research protocol with fresh context per iteration, externalized state, and convergence detection for deep technical investigation.
Runtime path resolution:
- OpenCode/Copilot runtime:
.opencode/agent/*.md - Claude runtime:
.claude/agents/*.md - Codex runtime:
.codex/agents/*.toml
1. WHEN TO USE
When to Use This Skill
Use this skill when:
- Deep investigation requiring multiple rounds of discovery
- Topic spans 3+ technical domains or sources
- Initial findings need progressive refinement
- Overnight or unattended research sessions
- Research where prior findings inform subsequent queries
When NOT to Use
- Simple, single-question research (use direct codebase search or
/spec_kit:plan) - Known-solution documentation (use
/spec_kit:plan) - Implementation tasks (use
/spec_kit:implement) - Quick codebase searches (use
@contextor direct Grep/Glob) - Fewer than 3 sources needed (single-pass research suffices)
Keyword Triggers
autoresearch, deep research, autonomous research, research loop, iterative research, multi-round research, deep investigation, comprehensive research
For iterative code review and quality auditing, see sk-deep-review.
2. SMART ROUTING
Resource Loading Levels
| Level | When to Load | Resources |
|---|---|---|
| ALWAYS | Every skill invocation | Quick reference baseline |
| CONDITIONAL | If intent signals match | Loop protocol, convergence, state format |
| ON_DEMAND | Only on explicit request | Templates, detailed specifications |
Smart Router Pseudocode
from pathlib import Path
SKILL_ROOT = Path(__file__).resolve().parent
RESOURCE_BASES = (SKILL_ROOT / "references", SKILL_ROOT / "assets")
DEFAULT_RESOURCE = "references/quick_reference.md"
INTENT_SIGNALS = {
"LOOP_SETUP": {"weight": 4, "keywords": ["autoresearch", "deep research", "research loop", "autonomous research"]},
"ITERATION": {"weight": 4, "keywords": ["iteration", "next round", "continue research", "research cycle"]},
"CONVERGENCE": {"weight": 3, "keywords": ["convergence", "stop condition", "diminishing returns", "stuck"]},
"STATE": {"weight": 3, "keywords": ["state file", "JSONL", "strategy", "resume", "auto-resume"]},
}
NOISY_SYNONYMS = {
"LOOP_SETUP": {"run research": 2.0, "investigate deeply": 1.8, "overnight research": 1.5},
"ITERATION": {"another pass": 1.5, "keep searching": 1.4, "dig deeper": 1.6},
"CONVERGENCE": {"good enough": 1.4, "stop when": 1.5, "diminishing": 1.6},
"STATE": {"pick up where": 1.5, "continue from": 1.4, "resume": 1.8},
}
RESOURCE_MAP = {
"LOOP_SETUP": ["references/loop_protocol.md", "references/state_format.md", "assets/deep_research_config.json"],
"ITERATION": ["references/loop_protocol.md", "references/convergence.md"],
"CONVERGENCE": ["references/convergence.md"],
"STATE": ["references/state_format.md", "assets/deep_research_strategy.md"],
}
LOADING_LEVELS = {
"ALWAYS": [DEFAULT_RESOURCE],
"ON_DEMAND_KEYWORDS": ["full protocol", "all templates", "complete reference"],
"ON_DEMAND": ["references/loop_protocol.md", "references/state_format.md", "references/convergence.md"],
}
Scoped Guard
def _guard_in_skill():
"""Verify this skill is active before loading resources."""
if not hasattr(_guard_in_skill, '_active'):
_guard_in_skill._active = True
return _guard_in_skill._active
def discover_markdown_resources(base_path: Path) -> list[str]:
"""Discover all .md files in the assets directory."""
return sorted(str(p.relative_to(base_path)) for p in (base_path / "references").glob("*.md"))
Phase Detection
Detect the current research phase from dispatch context to load appropriate resources:
| Phase | Signal | Resources to Load |
|---|---|---|
| Init | No JSONL exists | Loop protocol, state format |
| Iteration | Dispatch context includes iteration number | Loop protocol, convergence |
| Stuck | Dispatch context includes "RECOVERY" | Convergence, loop protocol |
| Synthesis | Convergence triggered STOP | Quick reference |
3. HOW IT WORKS
Architecture: 3-Layer Integration
User invokes: /spec_kit:deep-research "topic"
|
v
┌─────────────────────────────────┐
│ /spec_kit:deep-research command│ Layer 1: Command
│ (YAML workflow + loop config) │ Manages loop lifecycle
└──────────────┬──────────────────┘
|
v
┌─────────────────────────────────┐
│ YAML Loop Engine │ Layer 2: Workflow
│ - Init (config, strategy) │ Dispatch, evaluate, decide
│ - Loop (dispatch + converge) │
│ - Synthesize (final output) │
│ - Save (memory context) │
└──────────────┬──────────────────┘
| dispatches per iteration
v
┌─────────────────────────────────┐
│ @deep-research (LEAF agent) │ Layer 3: Agent
│ - Reads: state + strategy │ Fresh context each time
│ - Executes ONE research cycle │
│ - Writes: findings + state │
│ - Tools: WebFetch, Grep, etc. │
└──────────────┬──────────────────┘
|
v
┌─────────────────────────────────┐
│ State Files (disk) │ Externalized State
│ deep-research-config.json │ Persists across iterations
│ deep-research-state.jsonl │
│ deep-research-strategy.md │
│ research/iterations/iteration-NNN.md │
│ research/research.md (workflow-owned │
│ progressive synthesis) │
└─────────────────────────────────┘
Core Innovation: Fresh Context Per Iteration
Each agent dispatch gets a fresh context window. State continuity comes from files, not memory. This solves context degradation in long research sessions.
Adapted from: karpathy/autoresearch (loop concept), AGR (fresh context "Ralph Loop"), pi-autoresearch (JSONL state), autoresearch-opencode (context injection).
Data Flow
Init --> Create config.json, strategy.md, state.jsonl
|
Loop --> Read state --> Check convergence --> Dispatch @deep-research
| |
| v
| Agent executes:
| 1. Read state files
| 2. Determine focus
| 3. Research (3-5 actions)
| 4. Write iteration-NNN.md
| 5. Update strategy.md
| 6. Append state.jsonl
| |
+<--- Evaluate results <-----------------------+
|
+--- Continue? --> Yes: next iteration
| No: exit loop
v
Synthesize --> Compile final research/research.md
|
Save --> generate-context.js --> verify memory artifact
Key Concepts
| Concept | Description |
|---|---|
| Externalized state | All research continuity via files, not agent memory |
| Fresh context | Each iteration gets a clean agent with no prior context |
| Convergence | Multi-signal detection: newInfoRatio, stuck count, questions answered |
| Strategy file | "Persistent brain" recording what worked, failed, and where to look next |
| JSONL log | Append-only structured log for machine-parseable iteration data |
| Progressive synthesis | progressiveSynthesis defaults to true; the agent may update research/research.md incrementally, and the orchestrator always performs the final consolidation pass |
4. RULES
ALWAYS
- Read state first -- Agent must read JSONL and strategy.md before any research action
- One focus per iteration -- Pick ONE research sub-topic from strategy.md "Next Focus"
- Externalize findings -- Write to iteration-NNN.md, not held in agent context
- Update strategy -- Append to "What Worked"/"What Failed", update "Next Focus"
- Report newInfoRatio -- Every iteration JSONL record must include newInfoRatio
- Respect exhausted approaches -- Never retry approaches in the "Exhausted" list
- Cite sources -- Every finding must cite
[SOURCE: url]or[SOURCE: file:line] - Use generate-context.js for memory saves -- Never manually create memory files
- Treat research/research.md as workflow-owned -- Iteration findings feed synthesis; the workflow owns the canonical
research/research.md - Document ruled-out directions per iteration -- Every iteration must include what was tried and failed
- Report newInfoRatio + 1-sentence novelty justification -- Every JSONL iteration record must include both
- Quality guards must pass before convergence -- Source diversity, focus alignment, and no single-weak-source checks must pass before STOP can trigger
NEVER
- Dispatch sub-agents -- @deep-research is LEAF-only (NDP compliance)
- Hold findings in context -- Write everything to files
- Exceed TCB -- Target 8-11 tool calls per iteration (max 12)
- Ask the user -- Autonomous execution; make best-judgment decisions
- Skip convergence checks -- Every iteration must be evaluated
- Modify config after init -- Config is read-only after initialization
- Overwrite prior findings -- Append to research/research.md, never replace
Iteration Status Enum
complete | timeout | error | stuck | insight | thought
insight: Low newInfoRatio but important conceptual breakthroughthought: Analytical-only iteration, no evidence gathering
EXPERIMENTAL / REFERENCE-ONLY FEATURES
These concepts remain documented for future design work, but they are not part of the live executable contract for /spec_kit:deep-research:
- Wave orchestration -- parallel question fan-out, pruning, and breakthrough logic
- Checkpoint commits -- per-iteration git commits
- Segment transitions /
:restart-- multi-segment session partitioning - Alternate CLI dispatch -- process-isolated
claude -por similar dispatch modes
ESCALATE IF
- 3+ consecutive timeouts -- Infrastructure issue, not research problem
- State file corruption unrecoverable -- Cannot reconstruct from JSONL or iteration files
- All approaches exhausted with questions remaining -- Research may need human guidance
- Security concern in findings -- Proprietary code or credentials discovered
- All recovery tiers exhausted -- No automatic recovery path remaining
5. REFERENCES
Core Documentation
| Document | Purpose | Key Insight |
|---|---|---|
| loop_protocol.md | Loop lifecycle (4 phases) | Init, iterate, synthesize, save |
| state_format.md | State file schemas | JSONL + strategy.md + config.json |
| convergence.md | Stop condition algorithms | shouldContinue(), stuck recovery |
| quick_reference.md | One-page cheat sheet | Commands, tuning, troubleshooting |
Templates
| Template | Purpose | Usage |
|---|---|---|
| deep_research_config.json | Loop configuration | Copied to research/ during research init |
| deep_research_strategy.md | Strategy file | Copied to research/ during research init |
| deep_research_dashboard.md | Dashboard template | Auto-generated each iteration |
6. SUCCESS CRITERIA
Loop Completion
- Research loop ran to convergence or max iterations
- All state files present and consistent (config, JSONL, strategy)
- research/research.md produced with findings from all iterations
- Memory context saved via generate-context.js
Quality Gates
| Gate | Criteria | Blocking |
|---|---|---|
| Pre-loop | Config valid, strategy initialized, state log created | Yes |
| Per-iteration | iteration-NNN.md written, JSONL appended, strategy updated | Yes |
| Post-loop | research/research.md exists with content, convergence report generated | Yes |
| Quality guards | Source diversity (>=2), focus alignment, no single-weak-source | Yes |
| Memory save | memory/*.md created via generate-context.js | No |
Convergence Report
Every completed loop produces a convergence report:
- Stop reason (converged, max_iterations, all_questions_answered, stuck_unrecoverable)
- Total iterations completed
- Questions answered ratio
- Average newInfoRatio trend
7. INTEGRATION POINTS
Framework Integration
This skill operates within the behavioral framework defined in CLAUDE.md.
Key integrations:
- Gate 2: Skill routing via
skill_advisor.py(keywords: autoresearch, deep research) - Gate 3: File modifications require spec folder question per CLAUDE.md Gate 3
- Memory: Context preserved via Spec Kit Memory MCP (generate-context.js)
- Orchestrator: @orchestrate dispatches @deep-research as LEAF agent
Memory Integration
Before research:
memory_context({ input: topic, mode: "deep", intent: "understand" })
--> Loads prior research into strategy.md "Known Context"
During research (each iteration):
Agent writes research/iterations/iteration-NNN.md
Agent updates research/deep-research-strategy.md
Agent appends research/deep-research-state.jsonl
After research:
node .opencode/skill/system-spec-kit/scripts/dist/memory/generate-context.js [spec-folder]
# No additional indexing step is part of the live workflow contract.
Command Integration
| Command | Relationship |
|---|---|
/spec_kit:deep-research |
Primary invocation point |
/spec_kit:plan |
Next step after deep research completes |
/memory:save |
Manual memory save (deep research auto-saves) |
8. RELATED RESOURCES
Worked Examples
Deep Research on Unknown Topic:
/spec_kit:deep-research:auto "WebSocket reconnection strategies across browsers"- Init creates config, strategy with 5 key questions
- Iterations 1-3: Broad survey, official docs, codebase patterns
- Iterations 4-6: Deep dive into specific strategies, edge cases
- Iteration 7: Convergence detected after recent newInfoRatio values stay below the configured threshold
- Synthesis produces 17-section research/research.md
- Memory saved via generate-context.js
Narrow Research with Early Convergence:
/spec_kit:deep-research:auto "What CSS properties trigger GPU compositing?"- Init creates config with 2 key questions
- Iteration 1: Finds definitive answer from official specs
- All questions answered after iteration 1
- Loop stops cleanly, research/research.md produced
Stuck Recovery Example:
- Iterations 4-6 all have newInfoRatio below the configured threshold
- Stuck recovery triggers at iteration 7
- Recovery widens focus to least-explored question
- Iteration 7 finds new angle, newInfoRatio jumps to 0.4
- Loop continues productively
Design Origins
| Innovation | Source | Our Adaptation |
|---|---|---|
| Autonomous loop | karpathy/autoresearch | YAML-driven loop with convergence |
| Fresh context per iteration | AGR (Ralph Loop) | Orchestrator dispatch = fresh context |
| STRATEGY.md persistent brain | AGR | deep-research-strategy.md |
| JSONL state | pi-autoresearch | deep-research-state.jsonl |
| Stuck detection | AGR | 3-consecutive-no-progress recovery |
| Context injection | autoresearch-opencode | Strategy file as agent context |
Agents
| Agent | Purpose |
|---|---|
@deep-research |
Single iteration executor (LEAF) |
@orchestrate |
Loop coordination (when dispatched externally) |
Commands
| Command | Purpose |
|---|---|
/spec_kit:deep-research |
Full loop workflow |
/memory:save |
Manual context preservation |
For one-page cheat sheet: See quick_reference.md
For code review capabilities, see sk-deep-review.
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
cli-copilot
GitHub Copilot CLI orchestrator enabling external AI assistants to invoke the standalone 'copilot' binary for supplementary tasks including collaborative planning, cloud delegation, versatile code generation, and autonomous task execution.
system-spec-kit
Unified documentation and context preservation: spec folder workflow (levels 1-3+), CORE + ADDENDUM template architecture (v2.2), validation, and Spec Kit Memory for context preservation. Mandatory for all file modifications.
sk-code--full-stack
Stack-agnostic development orchestrator guiding developers through implementation, testing, and verification phases with automatic stack detection via marker files and bundled stack-specific knowledge.
cli-gemini
Gemini CLI orchestrator enabling any AI assistant to invoke Google's Gemini CLI for supplementary AI tasks including code generation, web research via Google Search, codebase architecture analysis, cross-AI validation, and parallel task processing.
sk-prompt-improver
Prompt engineering specialist that transforms vague requests into structured, scored AI prompts using 7 proven frameworks (RCAF, COSTAR, RACE, CIDI, TIDD-EC, CRISPE, CRAFT), DEPTH thinking methodology, and CLEAR scoring across text modes.
mcp-figma
Figma design file access via MCP providing 18 tools for file retrieval, image export, component/style extraction, team management, and collaborative commenting. Accessed via Code Mode for token-efficient workflows.
Didn't find tool you were looking for?