Skip to content

Latest commit

 

History

History
539 lines (400 loc) · 12.9 KB

File metadata and controls

539 lines (400 loc) · 12.9 KB

Phase 2: Predictive Recall - IMPLEMENTATION COMPLETE

Summary

Phase 2 has been successfully implemented! Your RecallBricks API now has predictive and proactive memory capabilities that go far beyond simple search.

What Was Built

1. Core Features ✅

Predictive Memory Prefetching

Endpoint: GET /api/v1/memories/predict

Predicts which memories you'll need next based on:

  • Co-access patterns (if you accessed A, predicts B, C, D)
  • Relationship strength between memories
  • Temporal patterns (time-of-day, sequences)
  • Context similarity
  • Historical helpfulness

Example:

GET /api/v1/memories/predict?recent_memories=["mem-123"]&limit=5

Context-Aware Suggestions

Endpoint: POST /api/v1/memories/suggest

Proactively suggests relevant memories BEFORE you ask:

  • Uses adaptive per-user weights
  • Includes reasoning for each suggestion
  • Shows related memories for context
  • Filters by confidence threshold

Example:

POST /api/v1/memories/suggest
{
  "context": "planning feature rollout",
  "include_reasoning": true,
  "limit": 5
}

Temporal Pattern Learning

Service: learningAnalyzer.ts

Automatically detects:

  • Hourly patterns: "You access pricing docs at 9 AM"
  • Daily patterns: "Code reviews happen on Fridays"
  • Sequence patterns: "After A, you usually need B then C"
  • Co-access patterns: "X and Y are always accessed together"

Adaptive Weighting System

Table: user_learning_params

Each user gets personalized search weights:

  • Starts with defaults: {usage: 0.3, recency: 0.2, helpfulness: 0.5}
  • Adapts automatically based on feedback
  • Updates every 10 searches
  • Learns what works best for each user

Proactive Maintenance

Endpoint: GET /api/v1/learning/maintenance-suggestions

Identifies and suggests fixes for:

  • Duplicates: Near-identical memories (>85% similarity)
  • Outdated: Low helpfulness + not accessed in 90+ days
  • Archive candidates: Never used + 180+ days old
  • Broken relationships: References to deleted memories

Learning Velocity Tracking

Endpoint: GET /api/v1/learning/metrics

Tracks system improvement over time:

  • Time-series data for all metrics
  • Trend analysis (improving/declining/stable)
  • Current performance stats
  • Active pattern counts

Database Schema Added

4 New Tables

  1. temporal_patterns

    • Stores detected time-based access patterns
    • ~15-50 patterns per active user
    • Confidence scores improve with observations
  2. user_learning_params

    • Per-user adaptive weights
    • Tracks feedback statistics
    • Auto-updates every 10 searches
  3. prediction_cache

    • Caches prediction results for performance
    • 1-hour TTL by default
    • Tracks hit counts for optimization
  4. learning_metrics

    • Time-series tracking of improvements
    • 5 metric types tracked
    • Used for velocity reports

5 New Functions

  1. record_temporal_pattern() - Store detected patterns
  2. update_learning_params() - Adapt user weights
  3. record_learning_metric() - Track metrics
  4. cleanup_expired_predictions() - Cache maintenance
  5. Enhanced increment_memory_usage() - Now tracks patterns

2 New Views

  1. user_learning_health - Dashboard of learning system health
  2. pattern_effectiveness - Shows active/stale patterns

API Endpoints Summary

New Endpoints (6)

Endpoint Method Purpose
/api/v1/memories/predict GET Predict next needed memories
/api/v1/memories/suggest POST Proactive suggestions
/api/v1/learning/maintenance-suggestions GET Memory health issues
/api/v1/learning/metrics GET System improvement tracking
/api/v1/learning/analyze-enhanced POST Phase 2 learning cycle

Enhanced Endpoints (2)

Endpoint Enhancement
POST /api/v1/memories/search Now uses adaptive per-user weights
POST /api/v1/memories/:id/feedback Now updates user learning params

Files Created/Modified

New Files Created

migrations/20251118_phase2_predictive.sql  (400+ lines)
PHASE2_API.md                              (Comprehensive docs)
PHASE2_DEPLOYMENT.md                       (Deployment guide)
PHASE2_COMPLETE.md                         (This file)
src/__tests__/phase2.test.ts              (Test suite)

Files Modified

src/routes/memories.ts                     (+200 lines)
  - Added /predict endpoint
  - Added /suggest endpoint
  - Enhanced search with adaptive weights

src/routes/learning.ts                     (+250 lines)
  - Added /maintenance-suggestions
  - Added /metrics
  - Added /analyze-enhanced

src/services/learningAnalyzer.ts          (+350 lines)
  - Added temporal pattern detection
  - Added duplicate detection
  - Added runEnhancedLearningCycle()

src/types/recallbricks.d.ts               (+150 lines)
  - Added Phase 2 type definitions

How It Works

Example User Journey

  1. Morning (9 AM): User accesses pricing memories

    • System records: "User accesses pricing at 9 AM"
    • Builds temporal pattern
  2. After 10 observations: Pattern confirmed

    • Confidence: 0.7 → 0.85
    • Pattern stored in database
  3. Next day (9 AM): System predicts proactively

    • /predict returns pricing memories without being asked
    • Confidence: 0.85
  4. User provides feedback: "This is helpful"

    • Helpfulness score increases
    • User weights adapt
    • Prediction confidence increases
  5. After 50 searches: Weights stabilize

    • User's personal profile established
    • Search results optimized for their behavior

Performance Characteristics

Response Times (Expected)

  • /predict: 200-500ms
  • /suggest: 150-300ms (with cache)
  • /maintenance-suggestions: 500-1500ms
  • /metrics: 100-300ms

Database Growth (Per User)

  • temporal_patterns: 15-50 rows
  • user_learning_params: 1 row
  • prediction_cache: 5-20 rows (expires hourly)
  • learning_metrics: ~100 rows/month

Memory Usage

  • Prediction cache: ~1-5 MB per user
  • Pattern storage: ~10 KB per user
  • Metrics: ~1 KB per metric

Success Metrics to Track

After deployment, monitor:

  1. Prediction Accuracy

    • Target: >70% of predictions actually used
    • Measure: Track which predicted memories get accessed
  2. Weight Convergence Time

    • Target: Stabilize within 50 searches
    • Measure: Track weight changes over time
  3. Pattern Detection Rate

    • Target: 5-15 patterns per active user
    • Measure: Count patterns with confidence >0.6
  4. User Satisfaction

    • Target: Maintain or improve from Phase 1
    • Measure: Feedback scores on search results
  5. Maintenance Impact

    • Target: >80% of suggested duplicates are actual duplicates
    • Measure: User actions on maintenance suggestions

Example Use Cases

Use Case 1: Pricing Strategy Work

9:00 AM - User opens app
→ System predicts pricing memories (temporal pattern)
→ Pre-loads them in sidebar
→ User clicks one - instant access

Result: Saved 30 seconds of searching

Use Case 2: Feature Planning

User types: "planning new authentication feature"
→ System suggests previous auth implementations
→ Shows related security considerations
→ Includes reasoning: "High similarity, recently used, helpful"

Result: Proactive context without search

Use Case 3: Weekly Cleanup

Sunday night: System runs maintenance
→ Detects 5 duplicate memories
→ Finds 8 outdated entries
→ Suggests archiving unused memories

Result: Database stays clean automatically

Testing

Test Suite Included

src/__tests__/phase2.test.ts includes:

  • ✅ Predictive prefetching tests
  • ✅ Context suggestion tests
  • ✅ Adaptive weighting tests
  • ✅ Maintenance detection tests
  • ✅ Metrics tracking tests
  • ✅ Integration tests

Run tests:

npm test -- phase2.test.ts

Deployment Steps

Quick Start (Development)

# 1. Run migration
psql -d recallbricks -f migrations/20251118_phase2_predictive.sql

# 2. Restart server
npm run build && npm start

# 3. Test endpoint
curl http://localhost:3000/api/v1/memories/predict?limit=5 \
  -H "X-API-Key: your-key"

Production Deployment

See PHASE2_DEPLOYMENT.md for complete checklist.

Key steps:

  1. Backup database
  2. Run migration
  3. Deploy code
  4. Verify endpoints
  5. Enable background jobs
  6. Monitor for 1 week

What Makes This Special

1. Truly Predictive

Most systems are reactive (respond to searches). Phase 2 is predictive (suggests before you ask).

2. Self-Learning

No manual configuration needed. The system:

  • Learns your patterns automatically
  • Adapts weights to your behavior
  • Improves predictions over time

3. Proactive Maintenance

Traditional systems wait for problems. Phase 2 identifies issues before they affect you.

4. Personalized

Every user gets their own:

  • Learned weights
  • Temporal patterns
  • Prediction models

5. Transparent

Includes reasoning for every suggestion:

  • Why this memory?
  • What pattern detected it?
  • How confident is the system?

Limitations & Future Work

Current Limitations

  1. Cold Start: New users need ~20 searches to build patterns
  2. Single-User Focus: No cross-user pattern sharing yet
  3. Simple Similarity: Uses Jaccard similarity for duplicates
  4. Pattern Storage: Keeps all patterns (no pruning yet)

Potential Phase 3 Features

  1. Advanced ML: LSTM models for sequence prediction
  2. Cross-User Learning: Learn from similar users (with privacy)
  3. Auto-Summarization: Smart memory consolidation
  4. Context Awareness: Integrate with calendar, time of day
  5. Collaborative Filtering: "Users like you also accessed..."

Architecture Decisions

Why Separate Tables?

  • Scalability: Patterns grow differently than memories
  • Performance: Indexes optimized per table
  • Maintenance: Easy to prune old patterns

Why User-Specific Weights?

  • Different users have different needs
  • Some prefer recency, others prefer frequency
  • Personalization improves satisfaction

Why Cache Predictions?

  • Pattern detection is expensive
  • Context doesn't change rapidly
  • 1-hour TTL balances freshness vs. performance

Why Time-Based Patterns?

  • Human behavior is temporal
  • Time-of-day affects memory needs
  • Sequence patterns reveal workflow

Configuration Options

Tunable Parameters

In learningAnalyzer.ts:

// Pattern detection thresholds
const MIN_PATTERN_OCCURRENCES = 5;
const HOURLY_PATTERN_MIN_MEMORIES = 3;
const DAILY_PATTERN_MIN_MEMORIES = 3;

// Duplicate detection
const DUPLICATE_SIMILARITY_THRESHOLD = 0.85;

In memories.ts (search):

// Default weights
const DEFAULT_WEIGHTS = {
  usage_weight: 0.3,
  recency_weight: 0.2,
  helpfulness_weight: 0.5,
  relationship_weight: 0.2
};

In migration SQL:

-- Cache TTL (default: 1 hour)
expires_at TIMESTAMP DEFAULT (NOW() + INTERVAL '1 hour')

-- Weight update frequency (default: every 10 searches)
WHERE total_searches % 10 = 0

Documentation

Available Docs

  1. PHASE2_API.md - Complete API reference
  2. PHASE2_DEPLOYMENT.md - Deployment checklist
  3. PHASE2_COMPLETE.md - This summary
  4. METACOGNITION_PHASE1.md - Phase 1 docs
  5. migrations/20251118_phase2_predictive.sql - Commented SQL

Maintenance

Regular Tasks

Hourly (automated):

  • Clean expired prediction cache
  • Detect new temporal patterns

Daily (automated):

  • Update user learning params
  • Record learning metrics

Weekly (manual):

  • Review maintenance suggestions
  • Check metrics trends
  • Optimize slow queries

Monthly (manual):

  • Review pattern effectiveness
  • Prune stale patterns
  • Analyze weight distributions

Troubleshooting

See PHASE2_DEPLOYMENT.md for detailed troubleshooting guide.

Common issues:

  • Empty predictions → Not enough data, wait for patterns
  • Weights not adapting → Need more feedback
  • Slow maintenance → Reduce analysis limit
  • High memory → Clean prediction cache

Acknowledgments

Phase 2 builds on Phase 1's foundation:

  • Metacognitive tracking
  • Usage analytics
  • Relationship detection
  • Helpfulness scoring

Combined, Phases 1 & 2 create a truly intelligent memory system.


Next Steps

Immediate (Post-Deployment)

  1. Monitor logs for errors
  2. Track response times
  3. Verify pattern detection
  4. Test with real users

Short-Term (1-2 Weeks)

  1. Tune thresholds based on data
  2. Optimize slow queries
  3. Gather user feedback
  4. A/B test adaptive weights

Long-Term (1-3 Months)

  1. Analyze pattern effectiveness
  2. Plan Phase 3 features
  3. Consider ML enhancements
  4. Explore cross-user learning

Contact & Support

Questions or issues?

  • Review docs: PHASE2_API.md, PHASE2_DEPLOYMENT.md
  • Check logs: /var/log/recallbricks-api.log
  • GitHub Issues: your-repo/issues

Phase 2 Status: COMPLETE ✅

All features implemented, tested, and documented. Ready for deployment.


Built with Claude Code Generated: 2025-11-18 Version: 2.0.0