Layer 4 provides relationship inference by analyzing memory patterns
and user personality. This is an optional layer used when game or
companion features are active.
Core Philosophy:
- Independent from Layers 1-3.5 (optional feature)
- Inference-based, not stored (computed on-demand)
- Simple calculation using existing data
- Foundation for external applications (games, companions, etc.)
Implementation (src/core/relationship.rs):
- RelationshipInference struct with key metrics:
* interaction_count: number of memories with entity
* avg_priority: average priority of those memories
* days_since_last: recency of interaction
* bond_strength: inferred strength (0.0-1.0)
* relationship_type: close_friend, friend, acquaintance, etc.
* confidence: data quality indicator (0.0-1.0)
Inference Logic:
- Personality-aware: introverts favor count, extroverts favor quality
- Simple rules: bond_strength from interaction patterns
- Automatic type classification based on metrics
- Confidence increases with more data
MCP Tools (src/mcp/base.rs):
- get_relationship(entity_id): Get specific relationship
- list_relationships(limit): List all relationships sorted by strength
Design Decisions:
- No caching: compute on-demand for simplicity
- No persistence: relationships are derived, not stored
- Leverages Layer 1 (related_entities) and Layer 3.5 (profile)
- Can be extended later with caching if needed
Usage Pattern:
- Normal use: Layers 1-3.5 only
- Game/Companion mode: Enable Layer 4 tools
- Frontend calls get_relationship() for character interactions
- aigpt backend provides inference, frontend handles presentation
Extended Memory struct and database schema to support entity
tracking, which is foundation for Layer 4 relationship system.
Changes to Memory struct (src/core/memory.rs):
- Added related_entities: Option<Vec<String>> field
- Added new_with_entities() constructor for Layer 4
- Added set_related_entities() setter method
- Added has_entity() helper method to check entity membership
- All fields are optional for backward compatibility
Changes to database (src/core/store.rs):
- Added related_entities column to memories table
- Automatic migration for existing databases
- Store as JSON array in TEXT column
- Updated all CRUD operations (create, get, update, list, search)
- Parse JSON to Vec<String> when reading from database
Design rationale:
- "Who with" is fundamental attribute of memory
- Enables efficient querying by entity
- Foundation for Layer 4 relationship inference
- Optional field maintains backward compatibility
- Simple JSON serialization for flexibility
Usage:
Memory::new_with_entities(
content,
ai_interpretation,
priority_score,
Some(vec!["alice".to_string(), "bob".to_string()])
)
Layer 3.5 provides a unified, essential summary of the user by integrating
data from Layers 1-3. This addresses the product design gap where individual
layers work correctly but lack a cohesive, simple output.
Design Philosophy:
- "Internal complexity, external simplicity"
- AI references get_profile() as primary tool (efficient)
- Detailed data still accessible when needed (flexible)
- Auto-caching with smart update triggers (performant)
Implementation:
- UserProfile struct: dominant traits, core interests, core values
- Automatic data aggregation from Layer 1-3
- Frequency analysis for topics/values extraction
- SQLite caching (user_profiles table, single row)
- Update triggers: 10+ new memories, new analysis, or 7+ days old
- MCP tool: get_profile (primary), others available for details
Data Structure:
- dominant_traits: Top 3 Big Five traits
- core_interests: Top 5 frequent topics from memories
- core_values: Top 5 values from high-priority memories
- key_memory_ids: Top 10 priority memories as evidence
- data_quality: 0.0-1.0 confidence score
Usage Pattern:
- Normal: AI calls get_profile() only when needed
- Deep dive: AI calls list_memories(), get_memory(id) for details
- Efficient: Profile cached, regenerates only when necessary
Layer 3 evaluates the user based on their Layer 2 memories using the
Big Five personality model (OCEAN), which is the most reliable
psychological model for personality assessment.
Changes:
- Add UserAnalysis struct with Big Five traits (Openness,
Conscientiousness, Extraversion, Agreeableness, Neuroticism)
- Create user_analyses table in SQLite for storing analyses
- Add storage methods: save_analysis, get_latest_analysis, list_analyses
- Add MCP tools: save_user_analysis and get_user_analysis
- Include helper methods: dominant_trait(), is_high()
- All scores are f32 clamped to 0.0-1.0 range
Architecture:
- Layer 3 analyzes patterns from Layer 2 memories (AI interpretations
and priority scores) to build psychological profile
- AI judges personality, tool records the analysis
- Independent from memory storage but references Layer 2 data
Add AI interpretation and priority scoring capabilities to the memory system.
Simple, efficient implementation following the principle: "AI judges, tool records."
## Core Changes
### Memory struct (src/core/memory.rs)
- Added `ai_interpretation: Option<String>` - AI's creative interpretation
- Added `priority_score: Option<f32>` - Priority from 0.0 to 1.0
- New constructor: `Memory::new_ai()` for Layer 2
- Helper methods: `set_ai_interpretation()`, `set_priority_score()`
- Comprehensive tests for all new functionality
### SQLite storage (src/core/store.rs)
- Extended schema with `ai_interpretation` and `priority_score` columns
- Automatic migration for existing databases
- Added index on `priority_score` for future sorting
- Updated all queries to include new fields
- Search now includes `ai_interpretation` in results
### MCP Server (src/mcp/base.rs)
- New tool: `create_ai_memory` with optional interpretation and score
- Updated all existing tools to return Layer 2 fields
- Backward compatible: `create_memory` still works (Layer 1)
## Design Philosophy
**"AI judges, tool records"**
- Tool provides simple storage, no complex logic
- AI (Claude) decides interpretation and importance
- Both fields are optional for flexibility
- Natural integration: interpretation + evaluation happen together
## Usage Example
```javascript
// Layer 1: Simple storage (still works)
create_memory({ content: "Tokyo weather is sunny" })
// Layer 2: AI-enhanced storage
create_ai_memory({
content: "Tokyo weather is sunny",
ai_interpretation: "User planning outdoor activities in Tokyo. Weather info important for travel decisions.",
priority_score: 0.75
})
```
## Backward Compatibility
- Layer 1 functionality unchanged
- Existing databases auto-migrate
- All Layer 2 fields are Optional<T>
- Old tools continue to work
## Testing
- All unit tests passing
- Schema migration tested
- Score clamping (0.0-1.0) tested
- Optional fields tested
Version: 0.2.0 (Layer 2)
Status: Implementation complete, ready for local testing