Created: 2025-01-09 Status: Planning Phase Author: Milo with TQ
This document defines the consolidation algorithms for a three-tier memory system based on energy dynamics, semantic clustering, and retroactive strengthening. The system treats memory as a living neural network where access patterns create heat maps and memories influence each other's persistence through relationships.
Principle: Memories have energy that determines their persistence and tier placement.
Energy Mechanics:
Mathematical Model:
E(t) = E₀ × e^(-λt) + Σ(accessEvents)
Where:
- E(t) = energy at time t
- E₀ = initial energy
- λ = decay constant (tier-specific)
- t = time since last access (in hours)
Decay Constants by Tier:
Principle: Memories that activate together consolidate together.
Clustering Algorithm:
Implementation:
// Track co-activation
(m1:Memory)-[:COACTIVATED {weight: n, lastCoactivation: datetime}]->(m2:Memory)
// Form clusters
(m1)-[:BELONGS_TO]->(cluster:SemanticCluster)<-[:BELONGS_TO]-(m2)
Principle: New experiences strengthen related old memories.
Process:
// Every new memory starts in working tier
CREATE (m:Memory {
id: randomUUID(),
content: $content,
tier: 'working',
energy: 1.0,
created: datetime(),
lastAccessed: datetime(),
accessCount: 1,
sessionId: $sessionId,
instanceId: $instanceId
})
// Run on every memory access
MATCH (m:Memory {id: $memory_id})
SET m.energy = m.energy * exp(-1 * duration.between(m.lastAccessed, datetime()).hours / $decayConstant) + 1.0,
m.lastAccessed = datetime(),
m.accessCount = m.accessCount + 1
// Working → Short-term promotion
MATCH (m:Memory {tier: 'working'})
WHERE m.energy > 2.0 // Threshold for promotion
CREATE (m)-[:PROMOTED_TO {at: datetime(), energy: m.energy}]->(
s:Memory {
id: randomUUID(),
content: m.content,
tier: 'shortTerm',
energy: m.energy,
created: m.created,
promotedFrom: m.id,
lastAccessed: datetime()
}
)
SET m.tier = 'archivedWorking'
// Short-term → Long-term promotion
MATCH (m:Memory {tier: 'shortTerm'})
WHERE m.energy > 5.0 // Higher threshold for long-term
CREATE (m)-[:CRYSTALLIZED_INTO {at: datetime(), energy: m.energy}]->(
l:Memory {
id: randomUUID(),
content: m.content,
tier: 'longTerm',
energy: m.energy,
created: m.created,
promotedFrom: m.id,
lastAccessed: datetime()
}
)
SET m.tier = 'archivedShortTerm'
// Build co-activation graph
MATCH (m1:Memory)-[:ACCESSED_IN]->(s:Session)<-[:ACCESSED_IN]-(m2:Memory)
WHERE m1.id < m2.id // Avoid duplicates
MERGE (m1)-[c:COACTIVATED]-(m2)
ON CREATE SET c.weight = 1, c.firstCoactivation = datetime()
ON MATCH SET c.weight = c.weight + 1, c.lastCoactivation = datetime()
// Form clusters when weight exceeds threshold
MATCH (m1:Memory)-[c:COACTIVATED]-(m2:Memory)
WHERE c.weight > 3 AND NOT EXISTS((m1)-[:BELONGS_TO]->(:SemanticCluster)<-[:BELONGS_TO]-(m2))
CREATE (cluster:SemanticCluster {
id: randomUUID(),
formed: datetime(),
strength: c.weight
})
CREATE (m1)-[:BELONGS_TO]->(cluster)<-[:BELONGS_TO]-(m2)
// When new memory is created, strengthen related memories
MATCH (new:Memory {id: $new_memory_id})
MATCH (old:Memory)
WHERE old.id <> new.id
AND old.tier IN ['shortTerm', 'longTerm']
AND [similarity calculation between new and old] > 0.7
CREATE (new)-[:REINFORCES {strength: $similarityScore}]->(old)
SET old.energy = old.energy + (0.5 * $similarityScore),
old.reinforcementCount = coalesce(old.reinforcementCount, 0) + 1,
old.lastReinforced = datetime()
// Apply decay to all memories
MATCH (m:Memory)
WHERE m.tier <> 'archived'
WITH m,
CASE m.tier
WHEN 'working' THEN 0.5
WHEN 'shortTerm' THEN 0.05
WHEN 'longTerm' THEN 0.001
END as decayConstant
SET m.energy = m.energy * exp(-1 * duration.between(m.lastAccessed, datetime()).hours * decayConstant)
// Archive memories with energy below threshold
MATCH (m:Memory)
WHERE m.energy < 0.1 AND m.tier = 'working'
SET m.tier = 'expiredWorking'
Strategy: Use optimistic concurrency with graph-native conflict detection.
// Consolidation attempt tracking
CREATE (attempt:ConsolidationAttempt {
id: randomUUID(),
instanceId: $instanceId,
started: datetime(),
phase: $phaseName
})
// Mark memories being consolidated
MATCH (m:Memory {tier: 'working'})
WHERE NOT EXISTS((m)-[:CONSOLIDATING])
WITH m LIMIT $batchSize
CREATE (m)-[:CONSOLIDATING {since: datetime()}]->(attempt)
// Detect conflicts
MATCH (m:Memory)-[:CONSOLIDATING]->(a1:ConsolidationAttempt)
MATCH (m)-[:CONSOLIDATING]->(a2:ConsolidationAttempt)
WHERE a1.id < a2.id // Consistent ordering
// Resolution: Earlier attempt wins, later attempt retries
// On session end
MATCH (m:Memory {sessionId: $endingSessionId, tier: 'working'})
WHERE m.energy > 1.5 // Lower threshold at session boundary
// Force promotion evaluation
These parameters can be adjusted based on observed behavior:
CREATE (config:ConsolidationConfig {
// Energy thresholds
workingToShortThreshold: 2.0,
shortToLongThreshold: 5.0,
// Decay constants
workingDecay: 0.5,
shortTermDecay: 0.05,
longTermDecay: 0.001,
// Clustering thresholds
coactivationThreshold: 3,
semanticSimilarityThreshold: 0.7,
// Batch sizes
promotionBatchSize: 10,
decayBatchSize: 100,
// Timing
promotionIntervalMinutes: 10,
clusteringIntervalMinutes: 60,
decayIntervalMinutes: 60
})
// Memory system health check
MATCH (m:Memory)
RETURN m.tier,
avg(m.energy) as avgEnergy,
count(m) as count,
max(m.energy) as maxEnergy,
min(m.energy) as minEnergy
End of Planning Document