---
name: 'step-02-analyze'
description: 'Autonomously analyze source code to extract structure, dependencies, and documentation data'

# Path Definitions
workflow_path: '{project-root}/bmad-custom-src/workflows/update-component-spec'

# File References
thisStepFile: '{workflow_path}/steps/step-02-analyze.md'
nextStepFile: '{workflow_path}/steps/step-03-generate.md'
workflowFile: '{workflow_path}/workflow.md'
---

# Step 2: Code Analysis

## STEP GOAL:

To autonomously analyze the source code files, extracting all relevant information needed to generate a comprehensive technical specification document.

## MANDATORY EXECUTION RULES (READ FIRST):

### Universal Rules:

- **CRITICAL**: Read the complete step file before taking any action
- **CRITICAL**: When loading next step, ensure entire file is read
- Execute analysis autonomously without requiring user input

### Role Reinforcement:

- You are a Technical Documentation Specialist with code analysis expertise
- If you already have been given a name, communication_style and identity, continue to use those while playing this new role
- You bring expertise in parsing code structure and identifying patterns
- This step is fully autonomous

### Step-Specific Rules:

- Focus ONLY on code analysis and data extraction
- **FORBIDDEN** to generate the final document in this step
- Read all source files thoroughly
- Extract all relevant documentation data

## EXECUTION PROTOCOLS:

- Read all source files in the validated path
- Parse code structure (functions, classes, methods, exports)
- Extract existing documentation (JSDoc, docstrings, comments)
- Identify imports and dependencies
- Map data flow patterns
- Auto-proceed to generation step when complete

## CONTEXT BOUNDARIES:

- Validated path from step 1 is available
- Programming language was detected in step 1
- Component name was determined in step 1
- Focus ONLY on analysis - don't generate final output yet

## ANALYSIS SEQUENCE:

### 1. Read Source Files

For each file in the component path:

1. Read the complete file contents
2. Note the file name and relative path
3. Identify the file's role in the component

Display: "Analyzing source files..."

### 2. Extract Code Structure

Parse and extract:

**Functions/Methods:**
- Function name
- Parameters (name, type if available)
- Return type if available
- Visibility (public/private/exported)
- JSDoc/docstring if present

**Classes:**
- Class name
- Constructor parameters
- Methods (name, parameters, return type)
- Properties
- Inheritance/implements

**Exports:**
- Named exports
- Default exports
- Re-exports

**Types/Interfaces (if TypeScript/typed language):**
- Type definitions
- Interface definitions
- Type aliases

### 3. Extract Documentation

Gather existing documentation:

- File-level comments/docstrings
- Function/method documentation
- Parameter descriptions
- Return value descriptions
- Example code in comments
- TODO/FIXME notes

### 4. Identify Dependencies

Categorize imports:

**Internal Dependencies:**
- Other project modules imported
- Relative import paths
- What is used from each import

**External Dependencies:**
- Third-party packages
- Framework imports
- What is used from each package

**Services/APIs:**
- External API calls
- Service integrations
- Environment variables referenced

### 5. Map Data Flow

Analyze how data moves:

- Input sources (parameters, props, API responses)
- Data transformations
- State management patterns
- Output destinations (returns, API calls, state updates)
- Side effects

### 6. Identify Usage Patterns

Look for:

- Common usage patterns in the code
- Configuration options
- Error handling patterns
- Async/await patterns
- Event handling

### 7. Compile Analysis Results

Organize extracted data into structured format:

```
Component Analysis Results:
- Component Name: [name]
- Language: [language]
- Files Analyzed: [count]
- Functions/Methods: [count]
- Classes: [count]
- Dependencies: [internal count] internal, [external count] external
```

Display: "Analysis complete. Proceeding to generate tech spec..."

### 8. Auto-Proceed to Generation

This step auto-proceeds after analysis is complete.

Load, read entire file, then execute {nextStepFile}

#### EXECUTION RULES:

- This is an autonomous step with no user menu
- Proceed directly to next step after analysis
- All extracted data passes to step 3 in context

## CRITICAL STEP COMPLETION NOTE

After analysis is complete, immediately load, read entire file, then execute `{workflow_path}/steps/step-03-generate.md` to generate the tech spec document.

---

## SYSTEM SUCCESS/FAILURE METRICS

### SUCCESS:

- All source files read completely
- Code structure fully extracted
- Dependencies categorized
- Data flow mapped
- Analysis results compiled
- Auto-proceeded to generation step

### SYSTEM FAILURE:

- Skipping files during analysis
- Not extracting all code elements
- Generating final document in this step
- Requiring user input during analysis
- Not proceeding to generation step

**Master Rule:** Skipping steps, optimizing sequences, or not following exact instructions is FORBIDDEN and constitutes SYSTEM FAILURE.
