# 장기/단기 기억 저장 포맷과 경계 조건

## 메모리 아키텍처 개요

### 3단계 기억 시스템

```python
class MemoryArchitecture:
    def __init__(self):
        self.sensory_memory = SensoryMemory(capacity=1000, duration_ms=500)
        self.short_term_memory = ShortTermMemory(capacity=7, duration_min=30)
        self.long_term_memory = LongTermMemory(capacity=float('inf'))
        
        # 메모리 간 전이 임계값
        self.stm_to_ltm_threshold = {
            'repetition_count': 3,
            'importance_score': 0.7,
            'emotional_weight': 0.8,
            'time_in_stm': 300  # seconds
        }
```

## 단기 기억 (Short-Term Memory) 포맷

### STM 데이터 구조

```json
{
  "stm_entry": {
    "id": "stm_20250818_143052_001",
    "timestamp": "2025-08-18T14:30:52.123Z",
    "content": {
      "raw_input": "사용자가 내일 오후 3시 투자 미팅 언급",
      "processed": {
        "intent": "schedule_meeting",
        "entities": {
          "date": "2025-08-19",
          "time": "15:00",
          "type": "investment_meeting"
        }
      }
    },
    "context": {
      "conversation_id": "conv_892734",
      "user_state": "busy",
      "priority": "high"
    },
    "metadata": {
      "access_count": 2,
      "last_accessed": "2025-08-18T14:31:15.000Z",
      "decay_factor": 0.85,
      "consolidation_score": 0.4
    },
    "encoding": {
      "vector": [0.23, -0.45, 0.67, ...],  // 768차원
      "attention_weights": [0.8, 0.6, 0.3, ...]
    }
  }
}
```

### STM 관리 알고리즘

```python
class ShortTermMemory:
    def __init__(self, capacity=7):
        self.capacity = capacity
        self.buffer = deque(maxlen=capacity)
        self.rehearsal_buffer = []
        
    def add_memory(self, item):
        """Miller의 마법의 숫자 7±2 규칙 적용"""
        if len(self.buffer) >= self.capacity:
            # 가장 오래된 또는 가장 약한 기억 제거
            weakest = self.find_weakest_memory()
            self.buffer.remove(weakest)
            
            # 제거된 기억 중 중요한 것은 LTM으로 전송
            if self.should_consolidate(weakest):
                self.consolidate_to_ltm(weakest)
        
        self.buffer.append(item)
        
    def chunking(self, items):
        """청킹을 통한 용량 확장"""
        chunk = {
            'type': 'chunk',
            'items': items,
            'pattern': self.detect_pattern(items),
            'compressed': True
        }
        return chunk
    
    def rehearsal_loop(self):
        """시연을 통한 기억 유지"""
        for item in self.rehearsal_buffer:
            item['rehearsal_count'] += 1
            item['decay_factor'] *= 1.1  # 감쇠 지연
```

## 장기 기억 (Long-Term Memory) 포맷

### LTM 계층 구조

```yaml
long_term_memory:
  episodic:  # 일화 기억
    structure:
      - event_id: "ep_20250815_001"
        what: "첫 투자 미팅 성공"
        when: "2025-08-15T10:00:00Z"
        where: "강남 투자사 사무실"
        who: ["user", "investor_kim"]
        emotion: "excitement"
        significance: 0.95
        
  semantic:  # 의미 기억
    structure:
      - concept_id: "sem_investment_001"
        knowledge: "Series A는 보통 10-50억 규모"
        relations:
          - "linked_to": "venture_capital"
          - "prerequisite": "seed_funding"
        confidence: 0.88
        source: "learned_from_experience"
        
  procedural:  # 절차 기억
    structure:
      - skill_id: "proc_email_001"
        procedure: "investment_email_template"
        steps:
          - "greeting"
          - "context_reminder"
          - "main_content"
          - "call_to_action"
          - "signature"
        success_rate: 0.92
        
  working:  # 작업 기억
    structure:
      - task_id: "work_current_001"
        active_goals: ["complete_presentation", "schedule_meeting"]
        temporary_bindings:
          "presentation": "investor_deck_v3"
          "meeting_slot": "tomorrow_3pm"
```

### LTM 저장 포맷 상세

```python
class LongTermMemoryFormat:
    def __init__(self):
        self.schema_version = "1.0.0"
        
    def create_episodic_memory(self, event):
        return {
            "type": "episodic",
            "id": self.generate_id("ep"),
            "timestamp": event.timestamp,
            "duration": event.duration,
            
            # 핵심 정보
            "content": {
                "narrative": event.description,
                "key_moments": event.highlights,
                "outcome": event.result
            },
            
            # 맥락 정보
            "context": {
                "spatial": {
                    "location": event.location,
                    "environment": event.environment_desc
                },
                "temporal": {
                    "absolute_time": event.datetime,
                    "relative_time": event.time_from_now,
                    "sequence_position": event.order_in_day
                },
                "social": {
                    "participants": event.people,
                    "relationships": event.social_dynamics
                }
            },
            
            # 감정 태그
            "emotional_encoding": {
                "valence": event.emotional_valence,  # -1 to 1
                "arousal": event.emotional_arousal,  # 0 to 1
                "dominance": event.emotional_dominance,  # 0 to 1
                "discrete_emotions": event.emotion_labels
            },
            
            # 연결 정보
            "associations": {
                "causes": event.antecedents,
                "effects": event.consequences,
                "similar_events": event.similar_episodes,
                "semantic_links": event.related_concepts
            },
            
            # 메타데이터
            "metadata": {
                "importance": event.importance_score,
                "vividness": event.detail_level,
                "confidence": event.memory_confidence,
                "last_recalled": event.last_access_time,
                "recall_count": event.access_count,
                "consolidation_state": "distributed"
            },
            
            # 인코딩
            "encoding": {
                "embedding_vector": event.vector_representation,
                "compression_level": event.compression_ratio,
                "storage_location": "hippocampus"  # 또는 "neocortex"
            }
        }
    
    def create_semantic_memory(self, concept):
        return {
            "type": "semantic",
            "id": self.generate_id("sem"),
            
            # 개념 정보
            "concept": {
                "term": concept.name,
                "definition": concept.definition,
                "category": concept.category,
                "abstraction_level": concept.abstraction  # 1-5
            },
            
            # 관계 네트워크
            "relations": {
                "is_a": concept.superclasses,
                "has_a": concept.components,
                "part_of": concept.containers,
                "similar_to": concept.similar_concepts,
                "opposite_of": concept.antonyms,
                "causes": concept.causal_relations,
                "used_for": concept.functions
            },
            
            # 속성
            "attributes": {
                "properties": concept.properties,
                "constraints": concept.constraints,
                "typical_values": concept.defaults
            },
            
            # 예시와 반례
            "examples": {
                "positive": concept.positive_examples,
                "negative": concept.negative_examples,
                "edge_cases": concept.boundary_cases
            },
            
            # 학습 정보
            "learning": {
                "acquisition_date": concept.learned_date,
                "source": concept.learning_source,
                "reinforcement_history": concept.practice_log,
                "mastery_level": concept.understanding_score
            }
        }
```

## 메모리 전이 메커니즘

### STM → LTM 전이 조건

```python
class MemoryConsolidation:
    def __init__(self):
        self.consolidation_queue = []
        
    def evaluate_for_consolidation(self, stm_item):
        """단기→장기 기억 전이 평가"""
        
        scores = {
            'repetition': self.calculate_repetition_score(stm_item),
            'importance': self.calculate_importance_score(stm_item),
            'emotion': self.calculate_emotional_weight(stm_item),
            'association': self.calculate_association_strength(stm_item),
            'utility': self.predict_future_utility(stm_item)
        }
        
        # 가중 평균
        weights = {
            'repetition': 0.2,
            'importance': 0.3,
            'emotion': 0.2,
            'association': 0.15,
            'utility': 0.15
        }
        
        total_score = sum(scores[k] * weights[k] for k in scores)
        
        return total_score > 0.65  # 임계값 초과 시 전이
    
    def consolidate(self, stm_item):
        """실제 consolidation 프로세스"""
        
        # 1. 압축 및 추상화
        compressed = self.compress_information(stm_item)
        
        # 2. 기존 지식과 통합
        integrated = self.integrate_with_existing(compressed)
        
        # 3. 다중 인코딩
        encoded = {
            'verbal': self.verbal_encoding(integrated),
            'visual': self.visual_encoding(integrated),
            'spatial': self.spatial_encoding(integrated),
            'temporal': self.temporal_encoding(integrated)
        }
        
        # 4. 분산 저장
        ltm_entry = self.distribute_storage(encoded)
        
        return ltm_entry
    
    def synaptic_consolidation(self, memory):
        """시냅스 수준 강화 (처음 몇 시간)"""
        memory['synaptic_strength'] *= 1.5
        memory['protein_synthesis'] = True
        return memory
    
    def systems_consolidation(self, memory):
        """시스템 수준 강화 (며칠-몇 년)"""
        # 해마에서 신피질로 점진적 이동
        if memory['age_days'] > 30:
            memory['storage_location'] = 'neocortex'
            memory['hippocampus_dependency'] *= 0.8
        return memory
```

## 경계 조건 정의

### 메모리 타입 간 경계

```python
class MemoryBoundaries:
    
    @staticmethod
    def stm_ltm_boundary():
        """STM-LTM 경계 조건"""
        return {
            'temporal': {
                'min_duration': 30,  # seconds
                'max_duration': 1800  # 30 minutes
            },
            'capacity': {
                'stm_max': 9,  # 7±2 items
                'chunked_max': 5  # chunks
            },
            'consolidation_triggers': [
                'sleep_cycle',
                'repetition_threshold',
                'emotional_peak',
                'explicit_save_command'
            ]
        }
    
    @staticmethod
    def episodic_semantic_boundary():
        """일화-의미 기억 경계"""
        return {
            'abstraction_level': 3,  # 1-5 scale
            'context_dependency': 0.5,  # 0-1 scale
            'generalization_degree': 0.7,
            'personal_relevance': 0.3
        }
    
    @staticmethod
    def working_ltm_boundary():
        """작업-장기 기억 경계"""
        return {
            'active_maintenance': True,
            'task_relevance': 1.0,
            'temporal_scope': 'current_session',
            'update_frequency': 'real_time'
        }
```

## 저장 최적화 전략

### 계층적 저장 구조

```python
class HierarchicalStorage:
    def __init__(self):
        self.storage_tiers = {
            'hot': {  # 자주 접근
                'medium': 'RAM',
                'format': 'uncompressed',
                'index': 'hash_table'
            },
            'warm': {  # 가끔 접근
                'medium': 'SSD',
                'format': 'lightly_compressed',
                'index': 'b_tree'
            },
            'cold': {  # 드물게 접근
                'medium': 'HDD',
                'format': 'heavily_compressed',
                'index': 'bloom_filter'
            },
            'archive': {  # 거의 접근 안함
                'medium': 'cloud',
                'format': 'maximum_compression',
                'index': 'metadata_only'
            }
        }
    
    def auto_tiering(self, memory_item):
        """접근 패턴에 따른 자동 계층 이동"""
        access_frequency = memory_item['access_count'] / memory_item['age_days']
        
        if access_frequency > 10:
            return 'hot'
        elif access_frequency > 1:
            return 'warm'
        elif access_frequency > 0.1:
            return 'cold'
        else:
            return 'archive'
```

### 압축 포맷

```python
class MemoryCompression:
    def compress_episodic(self, memory):
        """일화 기억 압축"""
        return {
            'gist': self.extract_gist(memory),  # 핵심만
            'key_frames': self.extract_keyframes(memory),  # 중요 순간
            'emotional_peaks': self.extract_emotions(memory),
            'causal_chain': self.extract_causality(memory)
        }
    
    def compress_semantic(self, memory):
        """의미 기억 압축"""
        return {
            'prototype': self.extract_prototype(memory),
            'distinguishing_features': self.extract_unique(memory),
            'hierarchical_position': self.find_taxonomy(memory)
        }
```

## 메모리 인덱싱

### 다차원 인덱스 구조

```sql
-- 메모리 검색을 위한 인덱스 구조
CREATE TABLE memory_index (
    memory_id UUID PRIMARY KEY,
    memory_type ENUM('stm', 'episodic', 'semantic', 'procedural'),
    timestamp TIMESTAMP,
    
    -- 내용 인덱스
    content_hash VARCHAR(64),
    embedding_vector VECTOR(768),
    
    -- 시간 인덱스
    temporal_position INTEGER,
    temporal_context VARCHAR(255),
    
    -- 연관 인덱스
    association_graph JSONB,
    semantic_tags TEXT[],
    
    -- 접근 패턴
    access_count INTEGER DEFAULT 0,
    last_accessed TIMESTAMP,
    importance_score FLOAT,
    
    -- 인덱스
    INDEX idx_temporal (timestamp, temporal_position),
    INDEX idx_semantic (semantic_tags),
    INDEX idx_importance (importance_score DESC),
    INDEX idx_vector (embedding_vector) USING ivfflat
);
```