---
phase: 04-memory-context-management
plan: 04
type: execute
wave: 3
depends_on: ["04-01", "04-02", "04-03"]
files_modified: ["src/memory/personality/__init__.py", "src/memory/personality/pattern_extractor.py", "src/memory/personality/layer_manager.py", "src/memory/personality/adaptation.py", "src/memory/__init__.py", "src/personality.py"]
autonomous: true

must_haves:
  truths:
    - "Personality layers learn from conversation patterns"
    - "Multi-dimensional learning covers topics, sentiment, interaction patterns"
    - "Personality overlays enhance rather than replace core values"
    - "Learning algorithms prevent overfitting to recent conversations"
    - "Personality system integrates with existing personality.py"
  artifacts:
    - path: "src/memory/personality/pattern_extractor.py"
      provides: "Pattern extraction from conversations"
      min_lines: 80
    - path: "src/memory/personality/layer_manager.py"
      provides: "Personality overlay system"
      min_lines: 60
    - path: "src/memory/personality/adaptation.py"
      provides: "Dynamic personality updates"
      min_lines: 50
    - path: "src/memory/__init__.py"
      provides: "Complete MemoryManager with personality learning"
      exports: ["MemoryManager", "PersonalityLearner"]
    - path: "src/personality.py"
      provides: "Updated personality system with memory integration"
      min_lines: 20
  key_links:
    - from: "src/memory/personality/pattern_extractor.py"
      to: "src/memory/storage/sqlite_manager.py"
      via: "conversation data for pattern analysis"
      pattern: "sqlite_manager\\.get_conversations_for_analysis"
    - from: "src/memory/personality/layer_manager.py"
      to: "src/memory/personality/pattern_extractor.py"
      via: "pattern data for layer creation"
      pattern: "pattern_extractor\\.extract_patterns"
    - from: "src/personality.py"
      to: "src/memory/personality/layer_manager.py"
      via: "personality overlay application"
      pattern: "layer_manager\\.get_active_layers"
---

<objective>
Implement personality learning system that extracts patterns from conversations and creates adaptive personality layers. This enables Mai to learn and adapt communication patterns while maintaining core personality values.

Purpose: Enable Mai to learn from user interactions and adapt personality while preserving core values
Output: Working personality learning system with pattern extraction, layer management, and dynamic adaptation
</objective>

<execution_context>
@~/.opencode/get-shit-done/workflows/execute-plan.md
@~/.opencode/get-shit-done/templates/summary.md
</execution_context>

<context>
@.planning/phases/04-memory-context-management/04-CONTEXT.md
@.planning/phases/04-memory-context-management/04-RESEARCH.md
@.planning/PROJECT.md
@.planning/ROADMAP.md
@.planning/STATE.md

# Reference existing personality system
@src/personality.py
@src/resource/personality.py

# Reference memory components
@.planning/phases/04-memory-context-management/04-01-SUMMARY.md
@.planning/phases/04-memory-context-management/04-02-SUMMARY.md
@.planning/phases/04-memory-context-management/04-03-SUMMARY.md
</context>

<tasks>

<task type="auto">
  <name>Task 1: Create pattern extraction system</name>
  <files>src/memory/personality/__init__.py, src/memory/personality/pattern_extractor.py</files>
  <action>
Create src/memory/personality/pattern_extractor.py with PatternExtractor class:

1. Implement multi-dimensional pattern extraction following research:
   - Topics: Track frequently discussed subjects and user interests
   - Sentiment: Analyze emotional tone and sentiment patterns
   - Interaction patterns: Response times, question asking, information sharing
   - Time-based preferences: Communication style by time of day/week
   - Response styles: Formality level, verbosity, use of emojis/humor

2. Pattern extraction methods:
   - extract_topic_patterns(conversations: List[Conversation]) -> TopicPatterns
   - extract_sentiment_patterns(conversations: List[Conversation]) -> SentimentPatterns
   - extract_interaction_patterns(conversations: List[Conversation]) -> InteractionPatterns
   - extract_temporal_patterns(conversations: List[Conversation]) -> TemporalPatterns
   - extract_response_style_patterns(conversations: List[Conversation]) -> ResponseStylePatterns

3. Analysis techniques:
   - Simple frequency analysis for topics
   - Basic sentiment analysis using keyword lists or simple models
   - Statistical analysis for interaction patterns
   - Time series analysis for temporal patterns
   - Linguistic analysis for response styles

4. Pattern validation:
   - Confidence scoring for extracted patterns
   - Pattern stability tracking over time
   - Outlier detection for unusual patterns

Follow existing error handling patterns. Keep analysis lightweight to avoid heavy computational overhead.
  </action>
  <verify>python -c "from src.memory.personality.pattern_extractor import PatternExtractor; pe = PatternExtractor(); print('Pattern extractor created successfully')"</verify>
  <done>Pattern extractor can analyze conversations and extract patterns</done>
</task>

<task type="auto">
  <name>Task 2: Implement personality layer management and adaptation</name>
  <files>src/memory/personality/layer_manager.py, src/memory/personality/adaptation.py, src/memory/__init__.py, src/personality.py</files>
  <action>
Create personality management system:

1. Create src/memory/personality/layer_manager.py with LayerManager:
   - PersonalityLayer dataclass with weights and application rules
   - Layer creation from extracted patterns
   - Layer conflict resolution (when patterns contradict)
   - Layer activation based on conversation context
   - Methods: create_layer_from_patterns(), get_active_layers(), apply_layers()

2. Create src/memory/personality/adaptation.py with PersonalityAdaptation:
   - Time-weighted learning (recent patterns have less influence)
   - Gradual adaptation with stability controls
   - Feedback integration for user preferences
   - Adaptation rate limiting to prevent rapid changes
   - Methods: update_personality_layer(), calculate_adaptation_rate(), apply_stability_controls()

3. Update src/memory/__init__.py to integrate personality learning:
   - Add PersonalityLearner to MemoryManager
   - Implement learning triggers (after conversations, periodically)
   - Add personality data persistence
   - Provide learning controls and configuration

4. Update src/personality.py to integrate with memory:
   - Import and use PersonalityLearner from memory system
   - Apply personality layers during conversation responses
   - Maintain separation between core personality and learned layers
   - Add configuration for learning enable/disable

5. Personality layer application:
   - Hybrid system prompt + behavior configuration
   - Context-aware layer activation
   - Core value enforcement (learned layers cannot override core values)
   - Layer priority and conflict resolution

Follow existing patterns from src/resource/personality.py for personality management. Ensure core personality values remain protected from learned modifications.
  </action>
  <verify>python -c "from src.memory.personality.layer_manager import LayerManager; lm = LayerManager(); print('Layer manager created successfully')"</verify>
  <done>Personality system can learn patterns and apply adaptive layers</done>
</task>

</tasks>

<verification>
After completion, verify:
1. Pattern extractor analyzes conversations across multiple dimensions
2. Layer manager creates personality overlays from patterns
3. Adaptation system prevents overfitting and maintains stability
4. Personality learning integrates with existing personality.py
5. Core personality values are protected from learned modifications
6. Learning system can be enabled/disabled through configuration
</verification>

<success_criteria>
- Pattern extraction covers topics, sentiment, interaction, temporal, and style patterns
- Personality layers work as adaptive overlays that enhance core personality
- Time-weighted learning prevents overfitting to recent conversations
- Stability controls maintain personality consistency
- Integration with existing personality system preserves core values
- Learning system is configurable and can be controlled by user
</success_criteria>

<output>
After completion, create `.planning/phases/04-memory-context-management/04-04-SUMMARY.md`
</output>