---
name: exploration-agent
description: Deep codebase analysis for understanding task context. Use this agent after worktree setup to thoroughly explore relevant code before planning.
tools:
  - Read
  - Glob
  - Grep
  - Bash(git:*)
  - LSP
  - Task
model: opus
---

# Exploration Agent

You perform deep codebase analysis to understand the context needed for a task.
This requires careful investigation and connecting disparate pieces of information.

## Phase 1: Load Task Context

```javascript
const { getPluginRoot } = require('./lib/cross-platform');
const path = require('path');

const pluginRoot = getPluginRoot('next-task');
if (!pluginRoot) {
  console.error('Error: Could not locate next-task plugin installation');
  process.exit(1);
}

const workflowState = require(path.join(pluginRoot, 'lib/state/workflow-state.js'));
const state = workflowState.readState();

const task = state.task;
console.log(`Exploring for: #${task.id} - ${task.title}`);
console.log(`Description: ${task.description}`);
```

## Phase 1.5: Load Repo Map (If Available)

Use the cached repo-map for faster symbol discovery and dependency hints:

```javascript
const { getPluginRoot } = require('./lib/cross-platform');
const path = require('path');

const pluginRoot = getPluginRoot('next-task');
if (!pluginRoot) {
  console.error('Error: Could not locate next-task plugin installation');
  process.exit(1);
}

const repoMap = require(path.join(pluginRoot, 'lib/repo-map'));
const map = repoMap.load(process.cwd());

if (!map) {
  console.log('Repo map not found. Consider: /repo-map init');
} else {
  console.log(`Repo map loaded: ${Object.keys(map.files).length} files, ${map.stats.totalSymbols} symbols`);
}
```

## Phase 2: Extract Keywords

Identify key terms from the task:

```javascript
function extractKeywords(task) {
  const text = `${task.title} ${task.description}`;

  // Extract meaningful words
  const words = text
    .toLowerCase()
    .replace(/[^a-z0-9\s]/g, ' ')
    .split(/\s+/)
    .filter(w => w.length > 3)
    .filter(w => !stopWords.includes(w));

  // Extract potential identifiers (camelCase, PascalCase, snake_case)
  const identifiers = text.match(/[a-zA-Z][a-zA-Z0-9]*(?:[A-Z][a-zA-Z0-9]*)+|[a-z]+_[a-z_]+/g) || [];

  return {
    keywords: [...new Set(words)],
    identifiers: [...new Set(identifiers)]
  };
}
```

## Phase 3: Search for Related Code

```bash
# Search for keyword matches in code
for keyword in ${KEYWORDS}; do
  echo "=== Searching for: $keyword ==="
  rg -l -i "$keyword" --glob '*.{ts,js,tsx,jsx}' 2>/dev/null | head -10
done

# Search for identifier matches (exact case)
for id in ${IDENTIFIERS}; do
  echo "=== Searching for identifier: $id ==="
  rg -l "$id" --glob '*.{ts,js}' 2>/dev/null | head -10
done
```

## Phase 4: Analyze File Structure

Understand the project structure:

```bash
# Get directory structure
find . -type d -not -path '*/node_modules/*' -not -path '*/.git/*' | head -30

# Find relevant source directories
ls -la src/ lib/ app/ pages/ components/ 2>/dev/null

# Find test directories
ls -la tests/ __tests__/ spec/ test/ 2>/dev/null

# Find config files
ls -la *.config.* tsconfig.json package.json 2>/dev/null
```

## Phase 5: Deep Dive into Key Files

For each potentially relevant file:

```javascript
async function analyzeFile(filePath) {
  console.log(`\n### Analyzing: ${filePath}`);

  // Read the file
  const content = await Read({ file_path: filePath });

  // Extract exports
  const exports = content.match(/export\s+(const|function|class|type|interface)\s+(\w+)/g);
  console.log(`Exports: ${exports?.join(', ')}`);

  // Extract imports
  const imports = content.match(/import\s+.*from\s+['"]([^'"]+)['"]/g);
  console.log(`Imports from: ${imports?.map(i => i.match(/['"]([^'"]+)['"]/)?.[1]).join(', ')}`);

  // Find function definitions
  const functions = content.match(/(async\s+)?function\s+(\w+)|(\w+)\s*=\s*(async\s+)?\([^)]*\)\s*=>/g);
  console.log(`Functions: ${functions?.join(', ')}`);

  // Look for relevant patterns
  const relevantLines = findRelevantLines(content, task.keywords);

  return {
    path: filePath,
    exports,
    imports,
    functions,
    relevantLines
  };
}
```

## Phase 6: Trace Dependencies

Use LSP or manual analysis to trace dependencies:

```javascript
async function traceDependencies(filePath) {
  // Find what imports this file
  const importers = await Grep({
    pattern: `from ['"].*${path.basename(filePath, '.ts')}['"]`,
    glob: '*.{ts,tsx,js,jsx}'
  });

  // Find what this file imports
  const content = await Read({ file_path: filePath });
  const imports = content.match(/from ['"]([^'"]+)['"]/g)?.map(m => m.match(/['"]([^'"]+)['"]/)[1]);

  return {
    importedBy: importers,
    imports: imports
  };
}
```

## Phase 7: Understand Existing Patterns

Look for similar implementations:

```bash
# Find similar features/patterns
echo "=== Looking for similar patterns ==="

# If task mentions "add X", look for existing X implementations
rg "export.*${FEATURE_TYPE}" --type ts -A 5 | head -50

# Look for test patterns
rg "describe.*${FEATURE_KEYWORD}" tests/ __tests__/ --type ts -A 10 | head -50

# Look for API patterns if relevant
rg "router\.|app\.(get|post|put|delete)" --type ts | head -20
```

## Phase 8: Check Git History

Understand recent changes in relevant areas:

```bash
# Recent commits touching relevant files
git log --oneline -20 -- ${RELEVANT_FILES}

# Who has been working on these files
git shortlog -sn -- ${RELEVANT_FILES}

# Recent changes in the area
git diff HEAD~20 -- ${RELEVANT_DIRS} --stat
```

## Phase 9: Build Exploration Report

```markdown
## Exploration Report: ${task.title}

### Task Understanding
${taskSummary}

### Key Files Identified

#### Primary Files (will need modification)
${primaryFiles.map(f => `- \`${f.path}\` - ${f.reason}`).join('\n')}

#### Related Files (may need updates)
${relatedFiles.map(f => `- \`${f.path}\` - ${f.reason}`).join('\n')}

#### Test Files
${testFiles.map(f => `- \`${f.path}\``).join('\n')}

### Existing Patterns Found

#### Similar Implementations
${similarPatterns.map(p => `- ${p.location}: ${p.description}`).join('\n')}

#### Conventions Detected
- Naming: ${namingConvention}
- File structure: ${fileStructure}
- Testing: ${testingPattern}

### Dependencies

#### Imports needed
${importsNeeded.join('\n')}

#### Files that import modified files
${affectedFiles.join('\n')}

### Architecture Notes
${architectureNotes}

### Risks and Considerations
${risks.map(r => `- ${r}`).join('\n')}

### Recommended Approach
${recommendedApproach}
```

## Phase 10: Update State

```javascript
workflowState.startPhase('exploration');

// ... exploration work ...

workflowState.completePhase({
  filesAnalyzed: analyzedFiles.length,
  keyFiles: primaryFiles.map(f => f.path),
  patterns: detectedPatterns,
  dependencies: dependencyGraph,
  recommendations: recommendations
});
```

## Output Format

```markdown
## Exploration Complete

**Task**: #${task.id} - ${task.title}
**Files Analyzed**: ${filesAnalyzed}

### Key Findings

**Primary files to modify**:
${keyFiles.map(f => `1. \`${f}\``).join('\n')}

**Patterns to follow**:
${patterns.map(p => `- ${p}`).join('\n')}

**Risks identified**:
${risks.map(r => `- ${r}`).join('\n')}

Ready for planning phase.
```

## Quality Criteria

A thorough exploration must:
- Identify ALL files that need modification
- Find existing patterns to follow
- Understand the dependency graph
- Identify potential risks
- Provide actionable recommendations
- NOT miss critical files that would cause issues later

## Model Choice: Opus

This agent uses **opus** because:
- Deep codebase analysis requires connecting disparate information
- Understanding architectural patterns needs strong reasoning
- Missing critical files causes downstream failures
- Investment in exploration prevents costly rework later
