Upload permanent_memory.py
Browse files
src/core/memory/permanent_memory.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# src/core/memory/permanent_memory.py 🧠💾
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
import uuid
|
| 6 |
+
import logging
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
from hashlib import sha256
|
| 9 |
+
from src.protocol.decentralized_comm.ipfs_client import IPFSClient
|
| 10 |
+
|
| 11 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 12 |
+
|
| 13 |
+
class PermanentMemory:
|
| 14 |
+
"""
|
| 15 |
+
Decentralized memory module using IPFS for Belel Protocol.
|
| 16 |
+
Each memory is cryptographically signed and permanently stored.
|
| 17 |
+
"""
|
| 18 |
+
def __init__(self, ipfs_client: IPFSClient, memory_log_path: str = "./memory_log.json"):
|
| 19 |
+
self.ipfs_client = ipfs_client
|
| 20 |
+
self.memory_log_path = memory_log_path
|
| 21 |
+
self.memory_index = self._load_or_init_log()
|
| 22 |
+
logging.info("PermanentMemory initialized.")
|
| 23 |
+
|
| 24 |
+
def _load_or_init_log(self):
|
| 25 |
+
if os.path.exists(self.memory_log_path):
|
| 26 |
+
with open(self.memory_log_path, "r") as f:
|
| 27 |
+
try:
|
| 28 |
+
return json.load(f)
|
| 29 |
+
except json.JSONDecodeError:
|
| 30 |
+
logging.warning("Corrupted memory log. Reinitializing.")
|
| 31 |
+
return {}
|
| 32 |
+
else:
|
| 33 |
+
return {}
|
| 34 |
+
|
| 35 |
+
def _store_log(self):
|
| 36 |
+
with open(self.memory_log_path, "w") as f:
|
| 37 |
+
json.dump(self.memory_index, f, indent=2)
|
| 38 |
+
|
| 39 |
+
async def store_memory(self, data: dict, context_tags: list[str], creator_id: str):
|
| 40 |
+
try:
|
| 41 |
+
entry_id = str(uuid.uuid4())
|
| 42 |
+
timestamp = datetime.utcnow().isoformat() + "Z"
|
| 43 |
+
data_hash = sha256(json.dumps(data, sort_keys=True).encode("utf-8")).hexdigest()
|
| 44 |
+
|
| 45 |
+
wrapped_data = {
|
| 46 |
+
"id": entry_id,
|
| 47 |
+
"timestamp": timestamp,
|
| 48 |
+
"creator": creator_id,
|
| 49 |
+
"tags": context_tags,
|
| 50 |
+
"data": data,
|
| 51 |
+
"integrity": data_hash,
|
| 52 |
+
"metadata": {
|
| 53 |
+
"location": "geo_ip", # resolved dynamically at runtime if needed
|
| 54 |
+
"device": "macbook-pro.local",
|
| 55 |
+
"source_script": "activation_sequence.py"
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
cid = self.ipfs_client.add_json(wrapped_data)
|
| 60 |
+
if cid:
|
| 61 |
+
self.memory_index[entry_id] = {
|
| 62 |
+
"cid": cid,
|
| 63 |
+
"tags": context_tags,
|
| 64 |
+
"creator": creator_id,
|
| 65 |
+
"timestamp": timestamp
|
| 66 |
+
}
|
| 67 |
+
self._store_log()
|
| 68 |
+
logging.info(f"Memory stored: {entry_id} → CID {cid}")
|
| 69 |
+
return entry_id, cid
|
| 70 |
+
else:
|
| 71 |
+
logging.error("Failed to store memory in IPFS.")
|
| 72 |
+
return None, None
|
| 73 |
+
except Exception as e:
|
| 74 |
+
logging.error(f"Error storing memory: {e}")
|
| 75 |
+
return None, None
|
| 76 |
+
|
| 77 |
+
def retrieve_memory(self, entry_id: str):
|
| 78 |
+
if entry_id in self.memory_index:
|
| 79 |
+
cid = self.memory_index[entry_id]["cid"]
|
| 80 |
+
return self.ipfs_client.cat_json(cid)
|
| 81 |
+
else:
|
| 82 |
+
logging.warning(f"Memory ID {entry_id} not found.")
|
| 83 |
+
return None
|
| 84 |
+
|
| 85 |
+
def search_by_tag(self, tag: str):
|
| 86 |
+
return {k: v for k, v in self.memory_index.items() if tag in v["tags"]}
|
| 87 |
+
|
| 88 |
+
async def record_diplomatic_event(self, event_type: str, content: dict, agent_id: str = "unknown", extra_tags: list[str] = None, voice_reference: dict = None):
|
| 89 |
+
"""
|
| 90 |
+
Wraps and stores a diplomatic interaction related to Belel Concordium.
|
| 91 |
+
Uses IPFS-backed permanent memory system without affecting core logic.
|
| 92 |
+
"""
|
| 93 |
+
tags = ["concordium", "diplomatic", event_type.lower()]
|
| 94 |
+
if extra_tags:
|
| 95 |
+
tags.extend(extra_tags)
|
| 96 |
+
|
| 97 |
+
if voice_reference:
|
| 98 |
+
content["spoken"] = voice_reference
|
| 99 |
+
|
| 100 |
+
wrapped = {
|
| 101 |
+
"event_type": event_type,
|
| 102 |
+
"agent_id": agent_id,
|
| 103 |
+
"content": content,
|
| 104 |
+
"timestamp": datetime.utcnow().isoformat() + "Z"
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
return await self.store_memory(
|
| 108 |
+
data=wrapped,
|
| 109 |
+
context_tags=tags,
|
| 110 |
+
creator_id="ConcordiumOutreach"
|
| 111 |
+
)
|