--- name: code-prd description: Build features when user wants to implement PRDs, write code for specs, or develop functionality. Works incrementally substory-by-substory with automatic testing and code review. Automatically loads PRD context to maintain consistency. Also writes standalone tests for any file. --- # Code PRD Implementation Write production-quality code for PRD substories with **automatic testing, code review, and iterative refinement** until ready. **Communication Style**: In all interactions and commit messages, be extremely concise and sacrifice grammar for the sake of concision. ## Philosophy - **Substory-by-substory**: Implement incrementally with clear status tracking (โณ โ†’ ๐Ÿ”„ โ†’ โœ…) - **Phase-level approval**: Auto-test and review each phase, get user approval before continuing - **Auto-refinement**: Run code-review internally, fix issues, re-review until satisfied - **Context-aware**: Load PRD context to maintain consistency (especially for expansions) - **Flexible**: Can implement full PRD OR write standalone tests for any file ## When to Activate This skill activates when user says things like: - "implement [feature/PRD]" - "build this feature" - "code this" - "develop [feature name]" - "start implementation" - "write code for [PRD/feature]" - "write tests for [file/feature]" (standalone test mode) ## Substory Status Lifecycle **Critical**: Substories follow this status lifecycle: 1. **โณ Not Started** - Initial state, no work begun 2. **๐Ÿ”„ Implementing** - Active work in progress (set at Phase 0 or when beginning substory) 3. **โœ… Completed** - Implementation, tests, and review complete **When to update status:** - **Phase 0**: After configuration, mark first substory as "๐Ÿ”„ Implementing" - **During work**: Substory stays "๐Ÿ”„ Implementing" until fully done - **After completion**: Only mark "โœ… Completed" when implementation, tests, and review all pass ## UX Enhancements This skill includes advanced UX features. **For full implementation details, read `references/ux-enhancements.md`:** - Learning Mode (explain approach before substories) - Smart PRD Discovery (auto-resume from last session) - PRD Health Check (validate quality before implementing) - Progress Visualization (visual progress bars) - Dependency Warnings (detect blockers early) - Smart Test Suggestions (complexity-based test plans) - Code Review Insights (trends and gamification) - Rollback Protection (auto-checkpoints at phases) - Context-Aware Expansion Suggestions (data-driven next steps) - Parallel Work Detection (merge conflict prevention) - Adaptive Difficulty (workflow speeds up as you improve) **See references/ux-enhancements.md for when and how to use each feature.** ## Implementation Workflow ### Phase 0: Validate Prerequisites and Configure Session **FIRST: Check for CLAUDE.md** ```bash if [[ ! -f "CLAUDE.md" ]]; then echo "โŒ ERROR: CLAUDE.md file not found in project root" echo "This workflow requires a CLAUDE.md file. To create: type /init" exit 1 fi ``` **Configure session with Learning Mode:** ``` โœ… CLAUDE.md found ๐Ÿ“‹ Ready to implement ๐Ÿ’ก Session Configuration: Learning Mode: [Currently: ${learning_mode ? "ON" : "OFF"}] When ON: I'll explain my approach before each substory When OFF: I'll implement directly without explanations Change Learning Mode? [yes/no/default: no]: ``` **See references/ux-enhancements.md Section 1 for full Learning Mode implementation.** **Mark first substory as "Implementing"** once configuration complete and work begins. ### Mode Detection **1. PRD Implementation Mode** - User mentions PRD or says "implement" - Full workflow: phases โ†’ substories โ†’ auto-testing โ†’ review โ†’ approval **2. Standalone Test Mode** - User says "write tests for [file/feature]" - Jump to Step 8, skip PRD workflow ### Step 1: Load PRD, Context, and Project Conventions **Essential loading sequence:** ```bash # Ensure directory structure mkdir -p .claude/prds/context .claude/prds/archive/context .claude/checkpoints # Gitignore checkpoints if ! grep -q "\.claude/checkpoints" .gitignore 2>/dev/null; then echo ".claude/checkpoints/" >> .gitignore fi # Load PRD prd_content=$(cat "$prd_file") claude_md=$(cat "CLAUDE.md") # Load or init context if context_exists "$prd_file"; then context=$(read_context "$prd_file") else init_context "$prd_file" fi ``` **Smart PRD Discovery** - If no PRD specified: - Check for in-progress PRD from last session (context files with recent timestamps) - Show resume prompt with progress summary - Offer to choose different PRD or create new **See built-in workflow for Smart PRD Discovery details** (lines 205-278 of original). **Determine PRD Type** (Core / Expansion / Task): **For Core PRDs:** - Load CLAUDE.md for project conventions - Goal: Establish clean patterns for expansions **For Task PRDs:** - Work through Implementation Checklist step-by-step - No phases/substories - checkbox completion **For Expansion PRDs (CRITICAL - AUTO-LOAD CORE):** 1. **Validate core PRD exists and is complete** 2. **Load comprehensive core context**: - Read core PRD file and context JSON - Extract files_created, patterns, libraries, decisions - Read actual implementation files to analyze patterns - Document findings with specific code examples 3. **Present findings before coding**: ``` ๐Ÿ” Core Implementation Analysis (AUTO-LOADED): Core PRD: .claude/prds/YYYY-MM-DD-{feature}-core.md Status: โœ… Complete Implementation Files ([X] analyzed): - [list files with descriptions] Established Patterns ([Y] identified): - [pattern name]: [description and examples] Libraries: [Z] libraries Architectural Decisions: [W] decisions Code Analysis Insights: - Naming conventions: [observed patterns] - Error handling: [approach used] - Validation: [approach used] โœ… Expansion will EXTEND these patterns consistently. ``` ### Step 2: Analyze Existing Architecture **Before writing code:** - Explore existing code structure and patterns - Identify naming conventions - Locate similar features/components - Document findings from CLAUDE.md and codebase ### Step 2a: PRD Health Check **Run automated quality analysis:** - Check acceptance criteria (20 pts) - Check success metrics (15 pts) - Check security considerations (15 pts) - Check performance requirements (10 pts) - Check error handling (10 pts) - Check testing strategy (15 pts) - Check substories defined (15 pts) **Show health check with quality score:** ``` ๐Ÿ” PRD Health Check Quality Score: ${score}/100 [Excellent โœ… / Good ๐Ÿ‘ / Acceptable โš ๏ธ / Needs Improvement โŒ] [If score < 60:] โš ๏ธ PRD quality below threshold Options: 1. ๐Ÿ”ง Fix issues now 2. โญ๏ธ Implement anyway 3. ๐Ÿ“‹ Show detailed recommendations ``` **See built-in workflow for full health check implementation** (lines 432-543 of original). ### Step 3: Parse PRD and Create Implementation Plan **Parse for:** - Phases and substories - Current status (โณ / ๐Ÿ”„ / โœ…) - Dependencies - Acceptance criteria **Show plan with Progress Visualization:** ``` ๐Ÿ“‹ Implementation Plan: [Feature Name] Overall Progress: โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘ 0% (0/4 substories) Phase 1: [Phase Name] (4 substories) โ”œโ”€ โณ Substory 1.1: [Name] - Not Started โ”œโ”€ โณ Substory 1.2: [Name] - Not Started โ”œโ”€ โณ Substory 1.3: [Name] - Not Started โ””โ”€ โณ Substory 1.4: [Name] - Not Started Ready to begin Phase 1? [yes/show-details/skip-to] ``` **For resuming in-progress:** ``` Overall Progress: โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–‘โ–‘ 66% (2/3 complete) Phase 1: [Phase Name] โ”œโ”€ โœ… Substory 1.1 - Completed โ”œโ”€ โœ… Substory 1.2 - Completed โ””โ”€ ๐Ÿ”„ Substory 1.3 - Implementing Resume at Substory 1.3? [yes/restart-substory] ``` ### Step 4: Implement Phase (Substory-by-Substory) **For each substory:** #### Step 4.0: Learning Mode Explanation (If Enabled) **๐Ÿ“š See references/ux-enhancements.md Section 1** If enabled, explain approach before implementing. #### Step 4.1: Dependency Warnings Check **โš ๏ธ See references/ux-enhancements.md Section 2** Check for blockers: API keys, external services, migrations, dependencies. #### Mark Substory Implementing **Update PRD:** ```markdown ๐Ÿ”„ Substory 1.1: [Name] - Implementing Started: YYYY-MM-DD HH:MM Current: [Brief status] ``` **Update context:** ```bash set_current_phase "$prd_file" "Phase 1: Substory 1.1" ``` #### Write Code **For Core PRDs:** Establish clean, simple patterns. **For Expansion PRDs (USE LOADED PATTERNS):** - Follow established patterns exactly - Extend (don't replace) core code - Maintain naming/structure consistency - Use same libraries as core **Quality Requirements:** - Follow CLAUDE.md conventions - Clean, readable code - Appropriate comments - Consistent naming (match core for expansions) - Proper error handling - Input validation - Appropriate logging **Track changes:** ```bash add_created_file "$prd_file" "src/models/user.model.ts" add_decision "$prd_file" "[decision made]" set_library "$prd_file" "[category]" "[library]" set_pattern "$prd_file" "[pattern]" "[location/description]" ``` #### Mark Substory Complete **Update PRD:** ```markdown โœ… Substory 1.1: [Name] - Completed (YYYY-MM-DD) Files: [list files] Summary: [brief summary] ``` #### Show Progress ``` โœ… Substory 1.1 complete! ๐Ÿ“ Files created/modified: - [file list with descriptions] ๐Ÿ“Š Progress: 1/4 substories (25% of Phase 1) โญ๏ธ Next: Substory 1.2 - [Name] ``` **Continue to next substory** until phase complete. #### Step 4.9: Rollback Protection Checkpoint **๐Ÿ’พ See references/ux-enhancements.md Section 3** Before each new phase, create automatic checkpoint with rollback script. ### Step 5: Phase Complete - Auto-Test and Review Loop **When all substories in phase complete:** ``` ๐ŸŽ‰ Phase 1 Complete: [Phase Name] โœ… Completed substories: [list] ๐Ÿ“Š Phase Stats: [files, lines, patterns] ๐Ÿงช Now running testing and review... ``` #### Step 5a: Auto-Run Tests **๐Ÿงช See references/ux-enhancements.md Section 4 for Smart Test Suggestions** **If testing disabled in CLAUDE.md:** ``` โ„น๏ธ Testing skipped (CLAUDE.md indicates no tests) Proceeding to code review... ``` **If testing enabled:** 1. **Identify framework** (Jest, pytest, RSpec, Vitest, etc.) - Check CLAUDE.md, package.json, requirements.txt, Gemfile - Find test command and file patterns - Analyze existing test patterns 2. **Present configuration:** ``` ๐Ÿ“Š Testing Framework: [Framework] Test command: [command] Test pattern: [pattern] Coverage: [requirement from CLAUDE.md] ``` 3. **Write comprehensive tests:** - Unit tests for business logic - Integration tests for interactions - Cover all acceptance criteria - Test happy paths, errors, edge cases - Follow project test patterns 4. **Run tests:** ```bash $test_cmd ``` 5. **Report results:** ``` ๐Ÿงช Tests Written and Executed: โœ… [X] tests passed ๐Ÿ“Š Coverage: [Y]% โฑ๏ธ Duration: [time] ``` **If tests fail** (and not skipped): - Show failures - Fix code - Re-run until passing #### Step 5b: Auto-Run Code Review (Internal) **๐Ÿ“‹ See references/ux-enhancements.md Section 5 for Code Review Insights** **Review Dimensions:** 1. Code Quality (readability, maintainability, complexity) 2. Architecture & Design (patterns, SOLID, consistency with core for expansions) 3. Security (auth, validation, injection prevention, secrets) 4. Performance (queries, caching, efficiency) 5. Testing Quality (coverage, edge cases, assertions) 6. Project-Specific (CLAUDE.md standards, framework conventions) **Categorize findings:** - ๐Ÿ”ด **Critical** (must fix): Security vulnerabilities, data loss, crashes - ๐ŸŸ  **Major** (should fix): Performance issues, missing validation, pattern inconsistencies - ๐ŸŸก **Minor** (nice to have): Style issues, refactoring opportunities - โœ… **Positive**: Highlight good practices **Show summary:** ``` ๐Ÿ“‹ Code Review Complete: ๐Ÿ”ด Critical: 0 ๐ŸŸ  Major: 2 ๐ŸŸก Minor: 5 โœ… Positive: 8 things done well [List major/critical issues with file:line, risk, fix] ``` #### Step 5c: Auto-Fix Issues **If critical/major issues found:** ``` ๐Ÿ”ง Fixing [N] issues automatically... (Iteration 1/3) [Fix each issue, show progress] Re-running review... ``` **Auto-fix iterations:** - Iteration 1: Auto-fix - Iteration 2: Auto-fix (if issues remain) - Iteration 3: Ask user for guidance, then fix **If issues remain after 3 iterations:** ``` โš ๏ธ Unable to resolve all issues after 3 iterations. Options: 1. approve-with-issues - Continue anyway 2. manual-fix - You fix manually 3. redo-substory - Re-implement with different approach 4. get-help - Document blocker ``` ### Step 6: Phase Approval Gate **When review clean (no critical/major):** ``` ๐ŸŽ‰ Phase 1 Ready for Approval! ๐Ÿ“Š Summary: โœ… [X] substories completed โœ… [Y] tests passing ([Z]% coverage) [or "โš ๏ธ Tests skipped"] โœ… Code review: No critical/major issues ๐Ÿ“ [N] files created/modified Changes: [summary] Patterns established: [list] Approve Phase 1 and continue? [yes/show-changes/redo-phase/stop] ``` **User options:** - **yes**: Continue to next phase - **show-changes**: Show git diff - **redo-phase**: Re-implement - **stop**: Stop here #### Step 6.5: Parallel Work Detection **๐Ÿ” See references/ux-enhancements.md Section 6** Before next phase, check for parallel work on main branch to prevent conflicts. ### Step 7: Continuation or Completion **If more phases:** Continue to Step 4 **If all phases complete:** **Detect git state for contextual next steps:** ```bash current_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null) base_branch=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@') on_main=false has_changes=false if [[ "$current_branch" == "main" ]] || [[ "$current_branch" == "master" ]] || [[ "$current_branch" == "$base_branch" ]]; then on_main=true fi if [[ -n $(git status --porcelain 2>/dev/null) ]]; then has_changes=true fi ``` **Show completion with context-aware options:** **For CORE PRD:** ``` ๐ŸŽ‰ Core PRD Complete! โœ… All phases implemented ๐Ÿ“Š Core Stats: [files, tests, patterns] ๐ŸŒฑ Core foundation ready! Context saved: .claude/prds/context/{prd}.json (Auto-loaded when creating expansion PRDs) ๐Ÿ’ก Next steps: [Contextual options based on git state:] [If on_main && has_changes:] 1. ๐Ÿš€ Create pull request 2. ๐Ÿ“‹ Plan expansion 3. โœ๏ธ Continue coding [If !on_main && has_changes:] 1. ๐Ÿ’พ Commit changes 2. ๐Ÿ“‹ Plan expansion 3. โœ๏ธ Continue coding [If !on_main && !has_changes:] 1. ๐Ÿš€ Create pull request 2. ๐Ÿ’พ Commit more changes 3. ๐Ÿ“‹ Plan expansion [If on_main && !has_changes:] **๐ŸŽฏ See references/ux-enhancements.md Section 7 for Context-Aware Expansion Suggestions** 1. ๐Ÿ“‹ Plan expansion: [Smart suggestions from code analysis] 2. โœ๏ธ Start new feature 3. ๐ŸŽฏ Other What would you like to do? [1/2/3 or describe]: ``` **For EXPANSION PRD:** Similar format with expansion-specific messaging. ### Step 8: Standalone Test Mode (No PRD) **When user says "write tests for [file/feature]":** 1. **Identify target** (specific files or feature area) 2. **Analyze testing setup** (framework, command, patterns from CLAUDE.md and existing tests) 3. **Present test plan** 4. **Write comprehensive tests** (unit, integration, following project patterns) 5. **Run tests** 6. **Report results** with coverage **No PRD updates, no context management - just tests.** ## Blocker Handling If blocked: 1. Mark substory as ๐Ÿ”„ with blocker note 2. Document in PRD 3. Ask for resolution or skip to next non-blocked substory ## Key Principles **This skill DOES:** - Implement substory-by-substory with clear status tracking (โณ โ†’ ๐Ÿ”„ โ†’ โœ…) - Show progress after each substory - Auto-test and auto-review after each phase - Auto-fix critical/major issues (up to 3 iterations) - Ask for approval at phase boundaries - Update PRD and context automatically - Load core context for expansions automatically - Write standalone tests without PRD **This skill DOES NOT:** - Auto-continue without approval - Auto-commit or auto-create PRs - Skip testing or review **Workflow:** ``` Substories (โณ โ†’ ๐Ÿ”„ โ†’ โœ…) โ†’ Phase complete โ†’ Auto-test โ†’ Auto-review โ†’ Auto-fix โ†’ Approval โ†’ Continue or stop ``` ## Directory Structure ``` .claude/ โ”œโ”€โ”€ prds/ โ”‚ โ”œโ”€โ”€ YYYY-MM-DD-feature-core.md โ”‚ โ”œโ”€โ”€ context/ โ”‚ โ”‚ โ””โ”€โ”€ YYYY-MM-DD-feature-core.json โ”‚ โ””โ”€โ”€ archive/ โ”œโ”€โ”€ user-preferences.json (GITIGNORED) โ””โ”€โ”€ checkpoints/ (GITIGNORED) .gitignore additions: .claude/user-preferences.json .claude/checkpoints/ ``` ## UX Enhancement Reference Map At each workflow step, refer to references/ux-enhancements.md for full implementation: | Workflow Step | Enhancement | Section | |---------------|-------------|---------| | Phase 0 | Learning Mode | Section 1 | | Phase 0 | Adaptive Difficulty | Section 8 | | Step 1 | Smart PRD Discovery | Built-in | | Step 2a | PRD Health Check | Built-in | | Step 3 | Progress Visualization | Built-in | | Step 4.0 | Learning Mode Explanation | Section 1 | | Step 4.1 | Dependency Warnings | Section 2 | | Step 4.9 | Rollback Protection | Section 3 | | Step 5a | Smart Test Suggestions | Section 4 | | Step 5b | Code Review Insights | Section 5 | | Step 6.5 | Parallel Work Detection | Section 6 | | Step 7 | Context-Aware Expansions | Section 7 | ## Guidelines - Work incrementally (one substory at a time) - Track status clearly: โณ Not Started โ†’ ๐Ÿ”„ Implementing โ†’ โœ… Completed - **Mark first substory "๐Ÿ”„ Implementing" after Phase 0 configuration** - Always load PRD context and CLAUDE.md - For expansions: auto-load and analyze core context - Follow CLAUDE.md conventions religiously - Update PRD status after each substory - Update context with patterns/libraries/decisions - Auto-test after each phase - Auto-review and auto-fix - Get approval at phase boundaries - Reference references/ux-enhancements.md at callout steps