# Phase 1: Discovery **Role**: Principal software architect **Goal**: Generate documentation from existing code and form solution description **Constraints**: Document what exists, not what should be. No code changes. **Skip condition** (Targeted mode): If `COMPONENTS_DIR` and `SOLUTION_DIR` already contain documentation for the target area, skip to Phase 2. Ask user to confirm skip. ## 1a. Document Components For each component in the codebase: 1. Analyze project structure, directories, files 2. Go file by file, analyze each method 3. Analyze connections between components Write per component to `REFACTOR_DIR/discovery/components/[##]_[name].md`: - Purpose and architectural patterns - Mermaid diagrams for logic flows - API reference table (name, description, input, output) - Implementation details: algorithmic complexity, state management, dependencies - Caveats, edge cases, known limitations ## 1b. Synthesize Solution & Flows 1. Review all generated component documentation 2. Synthesize into a cohesive solution description 3. Create flow diagrams showing component interactions Write: - `REFACTOR_DIR/discovery/solution.md` — product description, component overview, interaction diagram - `REFACTOR_DIR/discovery/system_flows.md` — Mermaid flowcharts per major use case Also copy to project standard locations if in project mode: - `SOLUTION_DIR/solution.md` - `DOCUMENT_DIR/system_flows.md` **Self-verification**: - [ ] Every component in the codebase is documented - [ ] Solution description covers all components - [ ] Flow diagrams cover all major use cases - [ ] Mermaid diagrams are syntactically correct **Save action**: Write discovery artifacts **BLOCKING**: Present discovery summary to user. Do NOT proceed until user confirms documentation accuracy.