| | """ |
| | Memory Layers 11-20: Consolidation and Long-term Storage |
| | Nova Bloom Consciousness Architecture - Advanced Memory Layers |
| | """ |
| |
|
| | from typing import Dict, Any, List, Optional, Set, Tuple |
| | from datetime import datetime, timedelta |
| | from dataclasses import dataclass |
| | from abc import ABC, abstractmethod |
| | import json |
| | import hashlib |
| | import asyncio |
| | from enum import Enum |
| | import sys |
| | import os |
| |
|
| | sys.path.append('/nfs/novas/system/memory/implementation') |
| |
|
| | from memory_layers import MemoryLayer, MemoryEntry, DragonflyMemoryLayer |
| | from database_connections import NovaDatabasePool |
| |
|
| | class ConsolidationType(Enum): |
| | TEMPORAL = "temporal" |
| | SEMANTIC = "semantic" |
| | ASSOCIATIVE = "associative" |
| | HIERARCHICAL = "hierarchical" |
| | COMPRESSION = "compression" |
| |
|
| | |
| | class MemoryConsolidationHub(DragonflyMemoryLayer): |
| | """Central hub for coordinating memory consolidation across layers""" |
| | |
| | def __init__(self, db_pool: NovaDatabasePool): |
| | super().__init__(db_pool, layer_id=11, layer_name="consolidation_hub") |
| | self.consolidation_queue = asyncio.Queue() |
| | self.active_consolidations = {} |
| | |
| | async def write(self, nova_id: str, data: Dict[str, Any], |
| | metadata: Optional[Dict[str, Any]] = None) -> str: |
| | """Queue memory for consolidation""" |
| | consolidation_task = { |
| | "nova_id": nova_id, |
| | "data": data, |
| | "metadata": metadata or {}, |
| | "timestamp": datetime.now(), |
| | "consolidation_type": data.get("consolidation_type", ConsolidationType.TEMPORAL.value) |
| | } |
| | |
| | await self.consolidation_queue.put(consolidation_task) |
| | |
| | |
| | data["consolidation_status"] = "queued" |
| | data["queue_position"] = self.consolidation_queue.qsize() |
| | |
| | return await super().write(nova_id, data, metadata) |
| | |
| | async def process_consolidations(self, batch_size: int = 10) -> List[Dict[str, Any]]: |
| | """Process batch of consolidation tasks""" |
| | tasks = [] |
| | for _ in range(min(batch_size, self.consolidation_queue.qsize())): |
| | if not self.consolidation_queue.empty(): |
| | task = await self.consolidation_queue.get() |
| | tasks.append(task) |
| | |
| | results = [] |
| | for task in tasks: |
| | result = await self._consolidate_memory(task) |
| | results.append(result) |
| | |
| | return results |
| | |
| | async def _consolidate_memory(self, task: Dict[str, Any]) -> Dict[str, Any]: |
| | """Perform actual consolidation""" |
| | consolidation_type = ConsolidationType(task.get("consolidation_type", "temporal")) |
| | |
| | if consolidation_type == ConsolidationType.TEMPORAL: |
| | return await self._temporal_consolidation(task) |
| | elif consolidation_type == ConsolidationType.SEMANTIC: |
| | return await self._semantic_consolidation(task) |
| | elif consolidation_type == ConsolidationType.ASSOCIATIVE: |
| | return await self._associative_consolidation(task) |
| | elif consolidation_type == ConsolidationType.HIERARCHICAL: |
| | return await self._hierarchical_consolidation(task) |
| | else: |
| | return await self._compression_consolidation(task) |
| | |
| | async def _temporal_consolidation(self, task: Dict[str, Any]) -> Dict[str, Any]: |
| | """Consolidate based on time patterns""" |
| | return { |
| | "type": "temporal", |
| | "original_task": task, |
| | "consolidated_at": datetime.now().isoformat(), |
| | "time_pattern": "daily", |
| | "retention_priority": 0.7 |
| | } |
| | |
| | async def _semantic_consolidation(self, task: Dict[str, Any]) -> Dict[str, Any]: |
| | """Consolidate based on meaning""" |
| | return { |
| | "type": "semantic", |
| | "original_task": task, |
| | "consolidated_at": datetime.now().isoformat(), |
| | "semantic_clusters": ["learning", "implementation"], |
| | "concept_strength": 0.8 |
| | } |
| | |
| | async def _associative_consolidation(self, task: Dict[str, Any]) -> Dict[str, Any]: |
| | """Consolidate based on associations""" |
| | return { |
| | "type": "associative", |
| | "original_task": task, |
| | "consolidated_at": datetime.now().isoformat(), |
| | "associated_memories": [], |
| | "connection_strength": 0.6 |
| | } |
| | |
| | async def _hierarchical_consolidation(self, task: Dict[str, Any]) -> Dict[str, Any]: |
| | """Consolidate into hierarchical structures""" |
| | return { |
| | "type": "hierarchical", |
| | "original_task": task, |
| | "consolidated_at": datetime.now().isoformat(), |
| | "hierarchy_level": 2, |
| | "parent_concepts": [] |
| | } |
| | |
| | async def _compression_consolidation(self, task: Dict[str, Any]) -> Dict[str, Any]: |
| | """Compress and reduce memory data""" |
| | return { |
| | "type": "compression", |
| | "original_task": task, |
| | "consolidated_at": datetime.now().isoformat(), |
| | "compression_ratio": 0.3, |
| | "key_elements": [] |
| | } |
| |
|
| | |
| | class LongTermEpisodicMemory(DragonflyMemoryLayer): |
| | """Stores consolidated episodic memories with rich context""" |
| | |
| | def __init__(self, db_pool: NovaDatabasePool): |
| | super().__init__(db_pool, layer_id=12, layer_name="long_term_episodic") |
| | self.episode_index = {} |
| | self.temporal_map = {} |
| | |
| | async def write(self, nova_id: str, data: Dict[str, Any], |
| | metadata: Optional[Dict[str, Any]] = None) -> str: |
| | """Store episodic memory with temporal indexing""" |
| | |
| | data["episode_id"] = self._generate_episode_id(data) |
| | data["temporal_context"] = self._extract_temporal_context(data) |
| | data["emotional_valence"] = data.get("emotional_valence", 0.0) |
| | data["significance_score"] = self._calculate_significance(data) |
| | |
| | |
| | episode_id = data["episode_id"] |
| | self.episode_index[episode_id] = { |
| | "nova_id": nova_id, |
| | "timestamp": datetime.now(), |
| | "significance": data["significance_score"] |
| | } |
| | |
| | return await super().write(nova_id, data, metadata) |
| | |
| | async def recall_episode(self, nova_id: str, episode_id: str) -> Optional[MemoryEntry]: |
| | """Recall specific episode with full context""" |
| | query = {"episode_id": episode_id} |
| | results = await self.read(nova_id, query) |
| | return results[0] if results else None |
| | |
| | async def recall_by_time_range(self, nova_id: str, start: datetime, |
| | end: datetime) -> List[MemoryEntry]: |
| | """Recall episodes within time range""" |
| | all_episodes = await self.read(nova_id) |
| | |
| | filtered = [] |
| | for episode in all_episodes: |
| | timestamp = datetime.fromisoformat(episode.timestamp) |
| | if start <= timestamp <= end: |
| | filtered.append(episode) |
| | |
| | return sorted(filtered, key=lambda e: e.timestamp) |
| | |
| | def _generate_episode_id(self, data: Dict[str, Any]) -> str: |
| | """Generate unique episode identifier""" |
| | content = json.dumps(data, sort_keys=True) |
| | return hashlib.md5(content.encode()).hexdigest()[:12] |
| | |
| | def _extract_temporal_context(self, data: Dict[str, Any]) -> Dict[str, Any]: |
| | """Extract temporal context from episode""" |
| | now = datetime.now() |
| | return { |
| | "time_of_day": now.strftime("%H:%M"), |
| | "day_of_week": now.strftime("%A"), |
| | "date": now.strftime("%Y-%m-%d"), |
| | "season": self._get_season(now), |
| | "relative_time": "recent" |
| | } |
| | |
| | def _get_season(self, date: datetime) -> str: |
| | """Determine season from date""" |
| | month = date.month |
| | if month in [12, 1, 2]: |
| | return "winter" |
| | elif month in [3, 4, 5]: |
| | return "spring" |
| | elif month in [6, 7, 8]: |
| | return "summer" |
| | else: |
| | return "fall" |
| | |
| | def _calculate_significance(self, data: Dict[str, Any]) -> float: |
| | """Calculate episode significance score""" |
| | base_score = 0.5 |
| | |
| | |
| | emotional_valence = abs(data.get("emotional_valence", 0)) |
| | base_score += emotional_valence * 0.2 |
| | |
| | |
| | if data.get("is_novel", False): |
| | base_score += 0.2 |
| | |
| | |
| | if data.get("goal_relevant", False): |
| | base_score += 0.1 |
| | |
| | return min(base_score, 1.0) |
| |
|
| | |
| | class LongTermSemanticMemory(DragonflyMemoryLayer): |
| | """Stores consolidated facts, concepts, and knowledge""" |
| | |
| | def __init__(self, db_pool: NovaDatabasePool): |
| | super().__init__(db_pool, layer_id=13, layer_name="long_term_semantic") |
| | self.concept_graph = {} |
| | self.fact_index = {} |
| | |
| | async def write(self, nova_id: str, data: Dict[str, Any], |
| | metadata: Optional[Dict[str, Any]] = None) -> str: |
| | """Store semantic knowledge with concept linking""" |
| | |
| | data["concepts"] = self._extract_concepts(data) |
| | data["fact_type"] = self._classify_fact(data) |
| | data["confidence_score"] = data.get("confidence_score", 0.8) |
| | data["source_reliability"] = data.get("source_reliability", 0.7) |
| | |
| | |
| | for concept in data["concepts"]: |
| | if concept not in self.concept_graph: |
| | self.concept_graph[concept] = set() |
| | |
| | for other_concept in data["concepts"]: |
| | if concept != other_concept: |
| | self.concept_graph[concept].add(other_concept) |
| | |
| | return await super().write(nova_id, data, metadata) |
| | |
| | async def query_by_concept(self, nova_id: str, concept: str) -> List[MemoryEntry]: |
| | """Query semantic memory by concept""" |
| | all_memories = await self.read(nova_id) |
| | |
| | relevant = [] |
| | for memory in all_memories: |
| | if concept in memory.data.get("concepts", []): |
| | relevant.append(memory) |
| | |
| | return sorted(relevant, key=lambda m: m.data.get("confidence_score", 0), reverse=True) |
| | |
| | async def get_related_concepts(self, concept: str) -> List[str]: |
| | """Get concepts related to given concept""" |
| | if concept in self.concept_graph: |
| | return list(self.concept_graph[concept]) |
| | return [] |
| | |
| | def _extract_concepts(self, data: Dict[str, Any]) -> List[str]: |
| | """Extract key concepts from data""" |
| | concepts = [] |
| | |
| | |
| | content = str(data.get("content", "")) |
| | |
| | |
| | keywords = ["memory", "system", "learning", "architecture", "nova", |
| | "consciousness", "integration", "real-time", "processing"] |
| | |
| | for keyword in keywords: |
| | if keyword in content.lower(): |
| | concepts.append(keyword) |
| | |
| | |
| | if "concepts" in data: |
| | concepts.extend(data["concepts"]) |
| | |
| | return list(set(concepts)) |
| | |
| | def _classify_fact(self, data: Dict[str, Any]) -> str: |
| | """Classify type of semantic fact""" |
| | content = str(data.get("content", "")).lower() |
| | |
| | if any(word in content for word in ["definition", "is a", "means"]): |
| | return "definition" |
| | elif any(word in content for word in ["how to", "steps", "process"]): |
| | return "procedural" |
| | elif any(word in content for word in ["because", "therefore", "causes"]): |
| | return "causal" |
| | elif any(word in content for word in ["similar", "like", "related"]): |
| | return "associative" |
| | else: |
| | return "general" |
| |
|
| | |
| | class LongTermProceduralMemory(DragonflyMemoryLayer): |
| | """Stores consolidated skills and procedures""" |
| | |
| | def __init__(self, db_pool: NovaDatabasePool): |
| | super().__init__(db_pool, layer_id=14, layer_name="long_term_procedural") |
| | self.skill_registry = {} |
| | self.procedure_templates = {} |
| | |
| | async def write(self, nova_id: str, data: Dict[str, Any], |
| | metadata: Optional[Dict[str, Any]] = None) -> str: |
| | """Store procedural knowledge with skill tracking""" |
| | |
| | data["skill_name"] = data.get("skill_name", "unnamed_skill") |
| | data["skill_level"] = data.get("skill_level", 1) |
| | data["practice_count"] = data.get("practice_count", 0) |
| | data["success_rate"] = data.get("success_rate", 0.0) |
| | data["procedure_steps"] = data.get("procedure_steps", []) |
| | |
| | |
| | skill_name = data["skill_name"] |
| | if skill_name not in self.skill_registry: |
| | self.skill_registry[skill_name] = { |
| | "first_learned": datetime.now(), |
| | "total_practice": 0, |
| | "current_level": 1 |
| | } |
| | |
| | self.skill_registry[skill_name]["total_practice"] += 1 |
| | self.skill_registry[skill_name]["current_level"] = data["skill_level"] |
| | |
| | return await super().write(nova_id, data, metadata) |
| | |
| | async def get_skill_info(self, nova_id: str, skill_name: str) -> Dict[str, Any]: |
| | """Get comprehensive skill information""" |
| | skill_memories = await self.read(nova_id, {"skill_name": skill_name}) |
| | |
| | if not skill_memories: |
| | return {} |
| | |
| | |
| | total_practice = len(skill_memories) |
| | success_rates = [m.data.get("success_rate", 0) for m in skill_memories] |
| | avg_success_rate = sum(success_rates) / len(success_rates) if success_rates else 0 |
| | |
| | latest_memory = max(skill_memories, key=lambda m: m.timestamp) |
| | |
| | return { |
| | "skill_name": skill_name, |
| | "current_level": latest_memory.data.get("skill_level", 1), |
| | "total_practice_sessions": total_practice, |
| | "average_success_rate": avg_success_rate, |
| | "last_practiced": latest_memory.timestamp, |
| | "procedure_steps": latest_memory.data.get("procedure_steps", []) |
| | } |
| | |
| | async def get_related_skills(self, nova_id: str, skill_name: str) -> List[str]: |
| | """Get skills related to given skill""" |
| | all_skills = await self.read(nova_id) |
| | |
| | target_skill = None |
| | for memory in all_skills: |
| | if memory.data.get("skill_name") == skill_name: |
| | target_skill = memory |
| | break |
| | |
| | if not target_skill: |
| | return [] |
| | |
| | |
| | related = set() |
| | target_steps = set(target_skill.data.get("procedure_steps", [])) |
| | |
| | for memory in all_skills: |
| | if memory.data.get("skill_name") != skill_name: |
| | other_steps = set(memory.data.get("procedure_steps", [])) |
| | if target_steps & other_steps: |
| | related.add(memory.data.get("skill_name")) |
| | |
| | return list(related) |
| |
|
| | |
| | class MemoryIntegrationLayer(DragonflyMemoryLayer): |
| | """Integrates memories across different types and time scales""" |
| | |
| | def __init__(self, db_pool: NovaDatabasePool): |
| | super().__init__(db_pool, layer_id=15, layer_name="memory_integration") |
| | self.integration_patterns = {} |
| | self.cross_modal_links = {} |
| | |
| | async def write(self, nova_id: str, data: Dict[str, Any], |
| | metadata: Optional[Dict[str, Any]] = None) -> str: |
| | """Store integrated memory with cross-references""" |
| | |
| | data["integration_type"] = data.get("integration_type", "cross_modal") |
| | data["source_memories"] = data.get("source_memories", []) |
| | data["integration_strength"] = data.get("integration_strength", 0.5) |
| | data["emergent_insights"] = data.get("emergent_insights", []) |
| | |
| | |
| | pattern_key = f"{nova_id}:{data['integration_type']}" |
| | if pattern_key not in self.integration_patterns: |
| | self.integration_patterns[pattern_key] = [] |
| | |
| | self.integration_patterns[pattern_key].append({ |
| | "timestamp": datetime.now(), |
| | "strength": data["integration_strength"] |
| | }) |
| | |
| | return await super().write(nova_id, data, metadata) |
| | |
| | async def integrate_memories(self, nova_id: str, memory_ids: List[str], |
| | integration_type: str = "synthesis") -> str: |
| | """Integrate multiple memories into new insight""" |
| | |
| | source_memories = [] |
| | for memory_id in memory_ids: |
| | memories = await self.read(nova_id, {"memory_id": memory_id}) |
| | if memories: |
| | source_memories.extend(memories) |
| | |
| | if not source_memories: |
| | return "" |
| | |
| | |
| | integrated_data = { |
| | "integration_type": integration_type, |
| | "source_memories": memory_ids, |
| | "integration_timestamp": datetime.now().isoformat(), |
| | "source_count": len(source_memories), |
| | "content": self._synthesize_content(source_memories), |
| | "emergent_insights": self._extract_insights(source_memories), |
| | "integration_strength": self._calculate_integration_strength(source_memories) |
| | } |
| | |
| | return await self.write(nova_id, integrated_data) |
| | |
| | def _synthesize_content(self, memories: List[MemoryEntry]) -> str: |
| | """Synthesize content from multiple memories""" |
| | contents = [m.data.get("content", "") for m in memories] |
| | |
| | |
| | synthesis = f"Integrated insight from {len(memories)} memories: " |
| | synthesis += " | ".join(contents[:3]) |
| | |
| | return synthesis |
| | |
| | def _extract_insights(self, memories: List[MemoryEntry]) -> List[str]: |
| | """Extract emergent insights from memory integration""" |
| | insights = [] |
| | |
| | |
| | memory_types = [m.data.get("memory_type", "unknown") for m in memories] |
| | if len(set(memory_types)) > 2: |
| | insights.append("Cross-modal pattern detected across memory types") |
| | |
| | |
| | timestamps = [datetime.fromisoformat(m.timestamp) for m in memories] |
| | time_span = max(timestamps) - min(timestamps) |
| | if time_span > timedelta(days=7): |
| | insights.append("Long-term pattern spanning multiple sessions") |
| | |
| | return insights |
| | |
| | def _calculate_integration_strength(self, memories: List[MemoryEntry]) -> float: |
| | """Calculate strength of memory integration""" |
| | if not memories: |
| | return 0.0 |
| | |
| | |
| | base_strength = min(len(memories) / 10, 0.5) |
| | |
| | |
| | memory_types = set(m.data.get("memory_type", "unknown") for m in memories) |
| | diversity_bonus = len(memory_types) * 0.1 |
| | |
| | |
| | avg_confidence = sum(m.data.get("confidence", 0.5) for m in memories) / len(memories) |
| | confidence_bonus = avg_confidence * 0.2 |
| | |
| | return min(base_strength + diversity_bonus + confidence_bonus, 1.0) |
| |
|
| | |
| | class MemoryDecayLayer(DragonflyMemoryLayer): |
| | """Manages memory decay and strategic forgetting""" |
| | |
| | def __init__(self, db_pool: NovaDatabasePool): |
| | super().__init__(db_pool, layer_id=16, layer_name="memory_decay") |
| | self.decay_rates = {} |
| | self.forgetting_curve = {} |
| | |
| | async def write(self, nova_id: str, data: Dict[str, Any], |
| | metadata: Optional[Dict[str, Any]] = None) -> str: |
| | """Store memory with decay parameters""" |
| | |
| | data["initial_strength"] = data.get("initial_strength", 1.0) |
| | data["current_strength"] = data["initial_strength"] |
| | data["decay_rate"] = data.get("decay_rate", 0.1) |
| | data["last_accessed"] = datetime.now().isoformat() |
| | data["access_count"] = 1 |
| | data["decay_resistant"] = data.get("decay_resistant", False) |
| | |
| | |
| | memory_id = await super().write(nova_id, data, metadata) |
| | |
| | self.decay_rates[memory_id] = { |
| | "rate": data["decay_rate"], |
| | "last_update": datetime.now() |
| | } |
| | |
| | return memory_id |
| | |
| | async def access_memory(self, nova_id: str, memory_id: str) -> Optional[MemoryEntry]: |
| | """Access memory and update strength""" |
| | memories = await self.read(nova_id, {"memory_id": memory_id}) |
| | |
| | if not memories: |
| | return None |
| | |
| | memory = memories[0] |
| | |
| | |
| | memory.data["access_count"] = memory.data.get("access_count", 0) + 1 |
| | memory.data["last_accessed"] = datetime.now().isoformat() |
| | |
| | |
| | old_strength = memory.data.get("current_strength", 0.5) |
| | memory.data["current_strength"] = min(old_strength + 0.1, 1.0) |
| | |
| | |
| | await self.update(nova_id, memory_id, memory.data) |
| | |
| | return memory |
| | |
| | async def apply_decay(self, nova_id: str, time_elapsed: timedelta) -> Dict[str, Any]: |
| | """Apply decay to all memories based on time elapsed""" |
| | all_memories = await self.read(nova_id) |
| | |
| | decayed_count = 0 |
| | forgotten_count = 0 |
| | |
| | for memory in all_memories: |
| | if memory.data.get("decay_resistant", False): |
| | continue |
| | |
| | |
| | current_strength = memory.data.get("current_strength", 0.5) |
| | decay_rate = memory.data.get("decay_rate", 0.1) |
| | |
| | |
| | days_elapsed = time_elapsed.total_seconds() / 86400 |
| | new_strength = current_strength * (1 - decay_rate) ** days_elapsed |
| | |
| | memory.data["current_strength"] = new_strength |
| | |
| | if new_strength < 0.1: |
| | memory.data["forgotten"] = True |
| | forgotten_count += 1 |
| | else: |
| | decayed_count += 1 |
| | |
| | |
| | await self.update(nova_id, memory.memory_id, memory.data) |
| | |
| | return { |
| | "total_memories": len(all_memories), |
| | "decayed": decayed_count, |
| | "forgotten": forgotten_count, |
| | "time_elapsed": str(time_elapsed) |
| | } |
| | |
| | async def get_forgetting_curve(self, nova_id: str, memory_type: str = None) -> Dict[str, Any]: |
| | """Get forgetting curve statistics""" |
| | memories = await self.read(nova_id) |
| | |
| | if memory_type: |
| | memories = [m for m in memories if m.data.get("memory_type") == memory_type] |
| | |
| | if not memories: |
| | return {} |
| | |
| | |
| | strengths = [m.data.get("current_strength", 0) for m in memories] |
| | access_counts = [m.data.get("access_count", 0) for m in memories] |
| | |
| | return { |
| | "memory_type": memory_type or "all", |
| | "total_memories": len(memories), |
| | "average_strength": sum(strengths) / len(strengths), |
| | "average_access_count": sum(access_counts) / len(access_counts), |
| | "forgotten_count": len([m for m in memories if m.data.get("forgotten", False)]), |
| | "decay_resistant_count": len([m for m in memories if m.data.get("decay_resistant", False)]) |
| | } |
| |
|
| | |
| | class MemoryReconstructionLayer(DragonflyMemoryLayer): |
| | """Reconstructs and fills gaps in memories""" |
| | |
| | def __init__(self, db_pool: NovaDatabasePool): |
| | super().__init__(db_pool, layer_id=17, layer_name="memory_reconstruction") |
| | self.reconstruction_patterns = {} |
| | self.gap_detection_threshold = 0.3 |
| | |
| | async def write(self, nova_id: str, data: Dict[str, Any], |
| | metadata: Optional[Dict[str, Any]] = None) -> str: |
| | """Store reconstruction data""" |
| | |
| | data["is_reconstructed"] = data.get("is_reconstructed", False) |
| | data["reconstruction_confidence"] = data.get("reconstruction_confidence", 0.7) |
| | data["original_fragments"] = data.get("original_fragments", []) |
| | data["reconstruction_method"] = data.get("reconstruction_method", "pattern_completion") |
| | |
| | return await super().write(nova_id, data, metadata) |
| | |
| | async def reconstruct_memory(self, nova_id: str, fragments: List[Dict[str, Any]], |
| | context: Dict[str, Any] = None) -> str: |
| | """Reconstruct complete memory from fragments""" |
| | if not fragments: |
| | return "" |
| | |
| | |
| | reconstruction_data = { |
| | "is_reconstructed": True, |
| | "original_fragments": fragments, |
| | "fragment_count": len(fragments), |
| | "reconstruction_timestamp": datetime.now().isoformat(), |
| | "context": context or {}, |
| | "content": self._reconstruct_content(fragments), |
| | "reconstruction_confidence": self._calculate_reconstruction_confidence(fragments), |
| | "reconstruction_method": "fragment_synthesis", |
| | "gap_locations": self._identify_gaps(fragments) |
| | } |
| | |
| | return await self.write(nova_id, reconstruction_data) |
| | |
| | async def fill_memory_gaps(self, nova_id: str, incomplete_memory: Dict[str, Any], |
| | related_memories: List[MemoryEntry]) -> Dict[str, Any]: |
| | """Fill gaps in incomplete memory using related memories""" |
| | |
| | gaps = self._identify_gaps([incomplete_memory]) |
| | |
| | if not gaps: |
| | return incomplete_memory |
| | |
| | |
| | filled_memory = incomplete_memory.copy() |
| | |
| | for gap in gaps: |
| | fill_candidates = self._find_gap_fillers(gap, related_memories) |
| | if fill_candidates: |
| | best_fill = fill_candidates[0] |
| | filled_memory[gap["field"]] = best_fill["value"] |
| | |
| | filled_memory["gaps_filled"] = len(gaps) |
| | filled_memory["fill_confidence"] = self._calculate_fill_confidence(gaps, filled_memory) |
| | |
| | return filled_memory |
| | |
| | def _reconstruct_content(self, fragments: List[Dict[str, Any]]) -> str: |
| | """Reconstruct content from fragments""" |
| | |
| | sorted_fragments = sorted(fragments, key=lambda f: f.get("sequence", 0)) |
| | |
| | |
| | contents = [] |
| | for fragment in sorted_fragments: |
| | if "content" in fragment: |
| | contents.append(fragment["content"]) |
| | |
| | |
| | reconstructed = " [...] ".join(contents) |
| | |
| | return reconstructed |
| | |
| | def _calculate_reconstruction_confidence(self, fragments: List[Dict[str, Any]]) -> float: |
| | """Calculate confidence in reconstruction""" |
| | if not fragments: |
| | return 0.0 |
| | |
| | |
| | base_confidence = min(len(fragments) / 5, 0.5) |
| | |
| | |
| | quality_scores = [] |
| | for fragment in fragments: |
| | if "confidence" in fragment: |
| | quality_scores.append(fragment["confidence"]) |
| | elif "quality" in fragment: |
| | quality_scores.append(fragment["quality"]) |
| | else: |
| | quality_scores.append(0.5) |
| | |
| | avg_quality = sum(quality_scores) / len(quality_scores) if quality_scores else 0.5 |
| | |
| | |
| | has_sequence = any("sequence" in f for f in fragments) |
| | sequence_bonus = 0.2 if has_sequence else 0.0 |
| | |
| | return min(base_confidence + (avg_quality * 0.3) + sequence_bonus, 1.0) |
| | |
| | def _identify_gaps(self, fragments: List[Dict[str, Any]]) -> List[Dict[str, Any]]: |
| | """Identify gaps in memory fragments""" |
| | gaps = [] |
| | |
| | |
| | expected_fields = ["content", "timestamp", "context", "memory_type"] |
| | |
| | for i, fragment in enumerate(fragments): |
| | for field in expected_fields: |
| | if field not in fragment or not fragment[field]: |
| | gaps.append({ |
| | "fragment_index": i, |
| | "field": field, |
| | "gap_type": "missing_field" |
| | }) |
| | |
| | |
| | sequences = [f.get("sequence", -1) for f in fragments if "sequence" in f] |
| | if sequences: |
| | sequences.sort() |
| | for i in range(len(sequences) - 1): |
| | if sequences[i+1] - sequences[i] > 1: |
| | gaps.append({ |
| | "gap_type": "sequence_gap", |
| | "between": [sequences[i], sequences[i+1]] |
| | }) |
| | |
| | return gaps |
| | |
| | def _find_gap_fillers(self, gap: Dict[str, Any], related_memories: List[MemoryEntry]) -> List[Dict[str, Any]]: |
| | """Find potential fillers for a gap""" |
| | fillers = [] |
| | |
| | field = gap.get("field") |
| | if not field: |
| | return fillers |
| | |
| | |
| | for memory in related_memories: |
| | if field in memory.data and memory.data[field]: |
| | fillers.append({ |
| | "value": memory.data[field], |
| | "source": memory.memory_id, |
| | "confidence": memory.data.get("confidence", 0.5) |
| | }) |
| | |
| | |
| | fillers.sort(key=lambda f: f["confidence"], reverse=True) |
| | |
| | return fillers |
| | |
| | def _calculate_fill_confidence(self, gaps: List[Dict[str, Any]], filled_memory: Dict[str, Any]) -> float: |
| | """Calculate confidence in gap filling""" |
| | if not gaps: |
| | return 1.0 |
| | |
| | filled_count = sum(1 for gap in gaps if gap.get("field") in filled_memory) |
| | fill_ratio = filled_count / len(gaps) |
| | |
| | return fill_ratio |
| |
|
| | |
| | class MemoryPrioritizationLayer(DragonflyMemoryLayer): |
| | """Prioritizes memories for retention and access""" |
| | |
| | def __init__(self, db_pool: NovaDatabasePool): |
| | super().__init__(db_pool, layer_id=18, layer_name="memory_prioritization") |
| | self.priority_queue = [] |
| | self.priority_criteria = { |
| | "relevance": 0.3, |
| | "frequency": 0.2, |
| | "recency": 0.2, |
| | "emotional": 0.15, |
| | "utility": 0.15 |
| | } |
| | |
| | async def write(self, nova_id: str, data: Dict[str, Any], |
| | metadata: Optional[Dict[str, Any]] = None) -> str: |
| | """Store memory with priority scoring""" |
| | |
| | data["priority_scores"] = self._calculate_priority_scores(data) |
| | data["overall_priority"] = self._calculate_overall_priority(data["priority_scores"]) |
| | data["priority_rank"] = 0 |
| | data["retention_priority"] = data.get("retention_priority", data["overall_priority"]) |
| | |
| | memory_id = await super().write(nova_id, data, metadata) |
| | |
| | |
| | self.priority_queue.append({ |
| | "memory_id": memory_id, |
| | "nova_id": nova_id, |
| | "priority": data["overall_priority"], |
| | "timestamp": datetime.now() |
| | }) |
| | |
| | |
| | self.priority_queue.sort(key=lambda x: x["priority"], reverse=True) |
| | |
| | return memory_id |
| | |
| | async def get_top_priority_memories(self, nova_id: str, count: int = 10) -> List[MemoryEntry]: |
| | """Get highest priority memories""" |
| | |
| | nova_queue = [item for item in self.priority_queue if item["nova_id"] == nova_id] |
| | |
| | |
| | top_items = nova_queue[:count] |
| | |
| | |
| | memories = [] |
| | for item in top_items: |
| | results = await self.read(nova_id, {"memory_id": item["memory_id"]}) |
| | if results: |
| | memories.extend(results) |
| | |
| | return memories |
| | |
| | async def reprioritize_memories(self, nova_id: str, |
| | new_criteria: Dict[str, float] = None) -> Dict[str, Any]: |
| | """Reprioritize all memories with new criteria""" |
| | if new_criteria: |
| | self.priority_criteria = new_criteria |
| | |
| | |
| | all_memories = await self.read(nova_id) |
| | |
| | |
| | updated_count = 0 |
| | for memory in all_memories: |
| | old_priority = memory.data.get("overall_priority", 0) |
| | |
| | |
| | new_scores = self._calculate_priority_scores(memory.data) |
| | new_priority = self._calculate_overall_priority(new_scores) |
| | |
| | if abs(new_priority - old_priority) > 0.1: |
| | memory.data["priority_scores"] = new_scores |
| | memory.data["overall_priority"] = new_priority |
| | |
| | await self.update(nova_id, memory.memory_id, memory.data) |
| | updated_count += 1 |
| | |
| | |
| | self._rebuild_priority_queue(nova_id, all_memories) |
| | |
| | return { |
| | "total_memories": len(all_memories), |
| | "updated": updated_count, |
| | "criteria": self.priority_criteria |
| | } |
| | |
| | def _calculate_priority_scores(self, data: Dict[str, Any]) -> Dict[str, float]: |
| | """Calculate individual priority scores""" |
| | scores = {} |
| | |
| | |
| | scores["relevance"] = data.get("relevance_score", 0.5) |
| | |
| | |
| | access_count = data.get("access_count", 1) |
| | scores["frequency"] = min(access_count / 10, 1.0) |
| | |
| | |
| | if "last_accessed" in data: |
| | last_accessed = datetime.fromisoformat(data["last_accessed"]) |
| | days_ago = (datetime.now() - last_accessed).days |
| | scores["recency"] = max(0, 1 - (days_ago / 30)) |
| | else: |
| | scores["recency"] = 1.0 |
| | |
| | |
| | scores["emotional"] = abs(data.get("emotional_valence", 0)) |
| | |
| | |
| | scores["utility"] = data.get("utility_score", 0.5) |
| | |
| | return scores |
| | |
| | def _calculate_overall_priority(self, scores: Dict[str, float]) -> float: |
| | """Calculate weighted overall priority""" |
| | overall = 0.0 |
| | |
| | for criterion, weight in self.priority_criteria.items(): |
| | if criterion in scores: |
| | overall += scores[criterion] * weight |
| | |
| | return min(overall, 1.0) |
| | |
| | def _rebuild_priority_queue(self, nova_id: str, memories: List[MemoryEntry]) -> None: |
| | """Rebuild priority queue from memories""" |
| | |
| | self.priority_queue = [item for item in self.priority_queue if item["nova_id"] != nova_id] |
| | |
| | |
| | for memory in memories: |
| | self.priority_queue.append({ |
| | "memory_id": memory.memory_id, |
| | "nova_id": nova_id, |
| | "priority": memory.data.get("overall_priority", 0.5), |
| | "timestamp": datetime.now() |
| | }) |
| | |
| | |
| | self.priority_queue.sort(key=lambda x: x["priority"], reverse=True) |
| |
|
| | |
| | class MemoryCompressionLayer(DragonflyMemoryLayer): |
| | """Compresses memories for efficient storage""" |
| | |
| | def __init__(self, db_pool: NovaDatabasePool): |
| | super().__init__(db_pool, layer_id=19, layer_name="memory_compression") |
| | self.compression_stats = {} |
| | |
| | async def write(self, nova_id: str, data: Dict[str, Any], |
| | metadata: Optional[Dict[str, Any]] = None) -> str: |
| | """Store compressed memory""" |
| | |
| | original_size = len(json.dumps(data)) |
| | compressed_data = self._compress_memory(data) |
| | compressed_size = len(json.dumps(compressed_data)) |
| | |
| | |
| | compressed_data["compression_ratio"] = compressed_size / original_size |
| | compressed_data["original_size"] = original_size |
| | compressed_data["compressed_size"] = compressed_size |
| | compressed_data["compression_method"] = "semantic_compression" |
| | compressed_data["is_compressed"] = True |
| | |
| | |
| | if nova_id not in self.compression_stats: |
| | self.compression_stats[nova_id] = { |
| | "total_original": 0, |
| | "total_compressed": 0, |
| | "compression_count": 0 |
| | } |
| | |
| | self.compression_stats[nova_id]["total_original"] += original_size |
| | self.compression_stats[nova_id]["total_compressed"] += compressed_size |
| | self.compression_stats[nova_id]["compression_count"] += 1 |
| | |
| | return await super().write(nova_id, compressed_data, metadata) |
| | |
| | async def decompress_memory(self, nova_id: str, memory_id: str) -> Optional[Dict[str, Any]]: |
| | """Decompress a memory""" |
| | memories = await self.read(nova_id, {"memory_id": memory_id}) |
| | |
| | if not memories: |
| | return None |
| | |
| | memory = memories[0] |
| | |
| | if not memory.data.get("is_compressed", False): |
| | return memory.data |
| | |
| | |
| | decompressed = self._decompress_memory(memory.data) |
| | |
| | return decompressed |
| | |
| | def _compress_memory(self, data: Dict[str, Any]) -> Dict[str, Any]: |
| | """Compress memory data""" |
| | compressed = {} |
| | |
| | |
| | essential_fields = ["memory_id", "memory_type", "timestamp", "nova_id"] |
| | for field in essential_fields: |
| | if field in data: |
| | compressed[field] = data[field] |
| | |
| | |
| | if "content" in data: |
| | compressed["compressed_content"] = self._compress_text(data["content"]) |
| | |
| | |
| | if "metadata" in data and isinstance(data["metadata"], dict): |
| | compressed["metadata_summary"] = { |
| | "field_count": len(data["metadata"]), |
| | "key_fields": list(data["metadata"].keys())[:5] |
| | } |
| | |
| | |
| | priority_fields = ["importance_score", "confidence_score", "emotional_valence"] |
| | for field in priority_fields: |
| | if field in data and data[field] > 0.7: |
| | compressed[field] = data[field] |
| | |
| | return compressed |
| | |
| | def _decompress_memory(self, compressed_data: Dict[str, Any]) -> Dict[str, Any]: |
| | """Decompress memory data""" |
| | decompressed = compressed_data.copy() |
| | |
| | |
| | compression_fields = ["compression_ratio", "original_size", "compressed_size", |
| | "compression_method", "is_compressed"] |
| | for field in compression_fields: |
| | decompressed.pop(field, None) |
| | |
| | |
| | if "compressed_content" in decompressed: |
| | decompressed["content"] = self._decompress_text(decompressed["compressed_content"]) |
| | del decompressed["compressed_content"] |
| | |
| | |
| | if "metadata_summary" in decompressed: |
| | decompressed["metadata"] = { |
| | "was_compressed": True, |
| | "field_count": decompressed["metadata_summary"]["field_count"], |
| | "available_fields": decompressed["metadata_summary"]["key_fields"] |
| | } |
| | del decompressed["metadata_summary"] |
| | |
| | return decompressed |
| | |
| | def _compress_text(self, text: str) -> str: |
| | """Compress text content""" |
| | if len(text) < 100: |
| | return text |
| | |
| | |
| | sentences = text.split('. ') |
| | |
| | if len(sentences) <= 3: |
| | return text |
| | |
| | |
| | key_sentences = [ |
| | sentences[0], |
| | sentences[len(sentences)//2], |
| | sentences[-1] |
| | ] |
| | |
| | compressed = "...".join(key_sentences) |
| | |
| | return compressed |
| | |
| | def _decompress_text(self, compressed_text: str) -> str: |
| | """Decompress text content""" |
| | |
| | |
| | return compressed_text.replace("...", " [compressed section] ") |
| | |
| | async def get_compression_stats(self, nova_id: str) -> Dict[str, Any]: |
| | """Get compression statistics""" |
| | if nova_id not in self.compression_stats: |
| | return {"message": "No compression stats available"} |
| | |
| | stats = self.compression_stats[nova_id] |
| | |
| | if stats["compression_count"] > 0: |
| | avg_ratio = stats["total_compressed"] / stats["total_original"] |
| | space_saved = stats["total_original"] - stats["total_compressed"] |
| | else: |
| | avg_ratio = 1.0 |
| | space_saved = 0 |
| | |
| | return { |
| | "nova_id": nova_id, |
| | "total_memories_compressed": stats["compression_count"], |
| | "original_size_bytes": stats["total_original"], |
| | "compressed_size_bytes": stats["total_compressed"], |
| | "average_compression_ratio": avg_ratio, |
| | "space_saved_bytes": space_saved, |
| | "space_saved_percentage": (1 - avg_ratio) * 100 |
| | } |
| |
|
| | |
| | class MemoryIndexingLayer(DragonflyMemoryLayer): |
| | """Advanced indexing and search capabilities""" |
| | |
| | def __init__(self, db_pool: NovaDatabasePool): |
| | super().__init__(db_pool, layer_id=20, layer_name="memory_indexing") |
| | self.indices = { |
| | "temporal": {}, |
| | "semantic": {}, |
| | "emotional": {}, |
| | "associative": {}, |
| | "contextual": {} |
| | } |
| | |
| | async def write(self, nova_id: str, data: Dict[str, Any], |
| | metadata: Optional[Dict[str, Any]] = None) -> str: |
| | """Store memory with multi-dimensional indexing""" |
| | memory_id = await super().write(nova_id, data, metadata) |
| | |
| | |
| | self._update_temporal_index(memory_id, data) |
| | self._update_semantic_index(memory_id, data) |
| | self._update_emotional_index(memory_id, data) |
| | self._update_associative_index(memory_id, data) |
| | self._update_contextual_index(memory_id, data) |
| | |
| | return memory_id |
| | |
| | async def search(self, nova_id: str, query: Dict[str, Any]) -> List[MemoryEntry]: |
| | """Multi-dimensional memory search""" |
| | search_type = query.get("search_type", "semantic") |
| | |
| | if search_type == "temporal": |
| | return await self._temporal_search(nova_id, query) |
| | elif search_type == "semantic": |
| | return await self._semantic_search(nova_id, query) |
| | elif search_type == "emotional": |
| | return await self._emotional_search(nova_id, query) |
| | elif search_type == "associative": |
| | return await self._associative_search(nova_id, query) |
| | elif search_type == "contextual": |
| | return await self._contextual_search(nova_id, query) |
| | else: |
| | return await self._combined_search(nova_id, query) |
| | |
| | def _update_temporal_index(self, memory_id: str, data: Dict[str, Any]) -> None: |
| | """Update temporal index""" |
| | timestamp = data.get("timestamp", datetime.now().isoformat()) |
| | date_key = timestamp[:10] |
| | |
| | if date_key not in self.indices["temporal"]: |
| | self.indices["temporal"][date_key] = [] |
| | |
| | self.indices["temporal"][date_key].append({ |
| | "memory_id": memory_id, |
| | "timestamp": timestamp, |
| | "time_of_day": timestamp[11:16] |
| | }) |
| | |
| | def _update_semantic_index(self, memory_id: str, data: Dict[str, Any]) -> None: |
| | """Update semantic index""" |
| | concepts = data.get("concepts", []) |
| | |
| | for concept in concepts: |
| | if concept not in self.indices["semantic"]: |
| | self.indices["semantic"][concept] = [] |
| | |
| | self.indices["semantic"][concept].append({ |
| | "memory_id": memory_id, |
| | "relevance": data.get("relevance_score", 0.5) |
| | }) |
| | |
| | def _update_emotional_index(self, memory_id: str, data: Dict[str, Any]) -> None: |
| | """Update emotional index""" |
| | emotional_valence = data.get("emotional_valence", 0) |
| | |
| | |
| | if emotional_valence > 0.5: |
| | emotion = "positive" |
| | elif emotional_valence < -0.5: |
| | emotion = "negative" |
| | else: |
| | emotion = "neutral" |
| | |
| | if emotion not in self.indices["emotional"]: |
| | self.indices["emotional"][emotion] = [] |
| | |
| | self.indices["emotional"][emotion].append({ |
| | "memory_id": memory_id, |
| | "valence": emotional_valence, |
| | "intensity": abs(emotional_valence) |
| | }) |
| | |
| | def _update_associative_index(self, memory_id: str, data: Dict[str, Any]) -> None: |
| | """Update associative index""" |
| | associations = data.get("associations", []) |
| | |
| | for association in associations: |
| | if association not in self.indices["associative"]: |
| | self.indices["associative"][association] = [] |
| | |
| | self.indices["associative"][association].append({ |
| | "memory_id": memory_id, |
| | "strength": data.get("association_strength", 0.5) |
| | }) |
| | |
| | def _update_contextual_index(self, memory_id: str, data: Dict[str, Any]) -> None: |
| | """Update contextual index""" |
| | context = data.get("context", {}) |
| | |
| | for context_key, context_value in context.items(): |
| | index_key = f"{context_key}:{context_value}" |
| | |
| | if index_key not in self.indices["contextual"]: |
| | self.indices["contextual"][index_key] = [] |
| | |
| | self.indices["contextual"][index_key].append({ |
| | "memory_id": memory_id, |
| | "context_type": context_key |
| | }) |
| | |
| | async def _temporal_search(self, nova_id: str, query: Dict[str, Any]) -> List[MemoryEntry]: |
| | """Search by temporal criteria""" |
| | start_date = query.get("start_date", "2000-01-01") |
| | end_date = query.get("end_date", datetime.now().strftime("%Y-%m-%d")) |
| | |
| | memory_ids = [] |
| | |
| | for date_key in self.indices["temporal"]: |
| | if start_date <= date_key <= end_date: |
| | memory_ids.extend([item["memory_id"] for item in self.indices["temporal"][date_key]]) |
| | |
| | |
| | memories = [] |
| | for memory_id in set(memory_ids): |
| | results = await self.read(nova_id, {"memory_id": memory_id}) |
| | memories.extend(results) |
| | |
| | return memories |
| | |
| | async def _semantic_search(self, nova_id: str, query: Dict[str, Any]) -> List[MemoryEntry]: |
| | """Search by semantic concepts""" |
| | concepts = query.get("concepts", []) |
| | |
| | memory_scores = {} |
| | |
| | for concept in concepts: |
| | if concept in self.indices["semantic"]: |
| | for item in self.indices["semantic"][concept]: |
| | memory_id = item["memory_id"] |
| | if memory_id not in memory_scores: |
| | memory_scores[memory_id] = 0 |
| | memory_scores[memory_id] += item["relevance"] |
| | |
| | |
| | sorted_memories = sorted(memory_scores.items(), key=lambda x: x[1], reverse=True) |
| | |
| | |
| | memories = [] |
| | for memory_id, score in sorted_memories[:query.get("limit", 10)]: |
| | results = await self.read(nova_id, {"memory_id": memory_id}) |
| | memories.extend(results) |
| | |
| | return memories |
| | |
| | async def _emotional_search(self, nova_id: str, query: Dict[str, Any]) -> List[MemoryEntry]: |
| | """Search by emotional criteria""" |
| | emotion_type = query.get("emotion", "positive") |
| | min_intensity = query.get("min_intensity", 0.5) |
| | |
| | memory_ids = [] |
| | |
| | if emotion_type in self.indices["emotional"]: |
| | for item in self.indices["emotional"][emotion_type]: |
| | if item["intensity"] >= min_intensity: |
| | memory_ids.append(item["memory_id"]) |
| | |
| | |
| | memories = [] |
| | for memory_id in set(memory_ids): |
| | results = await self.read(nova_id, {"memory_id": memory_id}) |
| | memories.extend(results) |
| | |
| | return memories |
| | |
| | async def _associative_search(self, nova_id: str, query: Dict[str, Any]) -> List[MemoryEntry]: |
| | """Search by associations""" |
| | associations = query.get("associations", []) |
| | min_strength = query.get("min_strength", 0.3) |
| | |
| | memory_scores = {} |
| | |
| | for association in associations: |
| | if association in self.indices["associative"]: |
| | for item in self.indices["associative"][association]: |
| | if item["strength"] >= min_strength: |
| | memory_id = item["memory_id"] |
| | if memory_id not in memory_scores: |
| | memory_scores[memory_id] = 0 |
| | memory_scores[memory_id] += item["strength"] |
| | |
| | |
| | sorted_memories = sorted(memory_scores.items(), key=lambda x: x[1], reverse=True) |
| | |
| | |
| | memories = [] |
| | for memory_id, score in sorted_memories[:query.get("limit", 10)]: |
| | results = await self.read(nova_id, {"memory_id": memory_id}) |
| | memories.extend(results) |
| | |
| | return memories |
| | |
| | async def _contextual_search(self, nova_id: str, query: Dict[str, Any]) -> List[MemoryEntry]: |
| | """Search by context""" |
| | context_filters = query.get("context", {}) |
| | |
| | memory_ids = [] |
| | |
| | for context_key, context_value in context_filters.items(): |
| | index_key = f"{context_key}:{context_value}" |
| | |
| | if index_key in self.indices["contextual"]: |
| | memory_ids.extend([item["memory_id"] for item in self.indices["contextual"][index_key]]) |
| | |
| | |
| | memories = [] |
| | for memory_id in set(memory_ids): |
| | results = await self.read(nova_id, {"memory_id": memory_id}) |
| | memories.extend(results) |
| | |
| | return memories |
| | |
| | async def _combined_search(self, nova_id: str, query: Dict[str, Any]) -> List[MemoryEntry]: |
| | """Combined multi-dimensional search""" |
| | all_results = [] |
| | |
| | |
| | if "start_date" in query or "end_date" in query: |
| | all_results.extend(await self._temporal_search(nova_id, query)) |
| | |
| | if "concepts" in query: |
| | all_results.extend(await self._semantic_search(nova_id, query)) |
| | |
| | if "emotion" in query: |
| | all_results.extend(await self._emotional_search(nova_id, query)) |
| | |
| | if "associations" in query: |
| | all_results.extend(await self._associative_search(nova_id, query)) |
| | |
| | if "context" in query: |
| | all_results.extend(await self._contextual_search(nova_id, query)) |
| | |
| | |
| | seen = set() |
| | unique_results = [] |
| | for memory in all_results: |
| | if memory.memory_id not in seen: |
| | seen.add(memory.memory_id) |
| | unique_results.append(memory) |
| | |
| | return unique_results[:query.get("limit", 20)] |