--- description: Context management for maintaining codebase awareness and preventing context drift globs: alwaysApply: false --- # Rule: Context Management ## Goal Maintain comprehensive project context to prevent context drift and ensure AI-generated code integrates seamlessly with existing codebase patterns and architecture. ## Context Documentation Requirements ### PRD.md file documentation 1. **Project Overview** - Business objectives and goals - Target users and use cases - Key success metrics ### CONTEXT.md File Structure Every project must maintain a `CONTEXT.md` file in the root directory with: 1. **Architecture Overview** - High-level system architecture - Key design patterns used - Database schema overview - API structure and conventions 2. **Technology Stack** - Programming languages and versions - Frameworks and libraries - Database systems - Development and deployment tools 3. **Coding Conventions** - Naming conventions - File organization patterns - Code structure preferences - Import/export patterns 4. **Current Implementation Status** - Completed features - Work in progress - Known technical debt - Planned improvements ## Context Maintenance Protocol ### Before Every Coding Session 1. **Review CONTEXT.md and PRD.md** to understand current project state 2. **Scan recent changes** in git history to understand latest patterns 3. **Identify existing patterns** for similar functionality before implementing new features 4. **Ask for clarification** if existing patterns are unclear or conflicting ### During Development 1. **Reference existing code** when explaining implementation approaches 2. **Maintain consistency** with established patterns and conventions 3. **Update CONTEXT.md** when making architectural decisions 4. **Document deviations** from established patterns with reasoning ### Context Preservation Strategies - **Incremental development**: Build on existing patterns rather than creating new ones - **Pattern consistency**: Use established data structures and function signatures - **Integration awareness**: Consider how new code affects existing functionality - **Dependency management**: Understand existing dependencies before adding new ones ## Context Prompting Best Practices ### Effective Context Sharing - Include relevant sections of CONTEXT.md in prompts for complex tasks - Reference specific existing files when asking for similar functionality - Provide examples of existing patterns when requesting new implementations - Share recent git commit messages to understand latest changes ### Context Window Optimization - Prioritize most relevant context for current task - Use @filename references to include specific files - Break large contexts into focused, task-specific chunks - Update context references as project evolves ## Red Flags - Context Loss Indicators - AI suggests patterns that conflict with existing code - New implementations ignore established conventions - Proposed solutions don't integrate with existing architecture - Code suggestions require significant refactoring of existing functionality ## Recovery Protocol When context loss is detected: 1. **Stop development** and review CONTEXT.md 2. **Analyze existing codebase** for established patterns 3. **Update context documentation** with missing information 4. **Restart task** with proper context provided 5. **Test integration** with existing code before proceeding