File size: 6,396 Bytes
a05f5cc
 
 
 
 
 
 
 
05b9f96
 
a05f5cc
 
 
 
05b9f96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a05f5cc
 
 
 
05b9f96
 
 
 
 
 
 
a05f5cc
05b9f96
 
 
 
 
 
 
a05f5cc
 
05b9f96
 
a05f5cc
 
 
05b9f96
a05f5cc
05b9f96
 
a05f5cc
05b9f96
 
 
 
 
 
 
a05f5cc
05b9f96
 
a05f5cc
 
05b9f96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a05f5cc
05b9f96
 
 
 
 
 
 
 
 
a05f5cc
05b9f96
 
 
a05f5cc
05b9f96
 
a05f5cc
05b9f96
 
 
 
 
 
 
 
a05f5cc
 
 
05b9f96
 
a05f5cc
 
 
 
 
 
 
 
 
 
 
05b9f96
a05f5cc
05b9f96
 
 
 
 
 
 
 
a05f5cc
 
 
 
 
 
 
 
 
05b9f96
a05f5cc
 
 
 
 
 
05b9f96
a05f5cc
 
 
 
 
05b9f96
a05f5cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# src/core/memory/permanent_memory.py 🧠💾

import json
import os
import uuid
import logging
from datetime import datetime
from hashlib import sha256
from typing import Optional, Dict
from filelock import FileLock
from src.protocol.decentralized_comm.ipfs_client import IPFSClient

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

def _canonical_json(obj) -> bytes:
    # Stable representation for hashing/signing
    return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode("utf-8")

def _atomic_write_json(path: str, obj) -> None:
    os.makedirs(os.path.dirname(path) or ".", exist_ok=True)
    tmp = path + ".tmp"
    with FileLock(path + ".lock"):
        with open(tmp, "w", encoding="utf-8") as f:
            json.dump(obj, f, indent=2, ensure_ascii=False)
        os.replace(tmp, path)

def _append_jsonl(path: str, obj) -> None:
    os.makedirs(os.path.dirname(path) or ".", exist_ok=True)
    with FileLock(path + ".lock"):
        with open(path, "a", encoding="utf-8") as f:
            f.write(json.dumps(obj, ensure_ascii=False) + "\n")

class PermanentMemory:
    """
    Decentralized memory module using IPFS for Belel Protocol.
    Each memory is cryptographically signed and permanently stored.

    Args:
        ipfs_client: IPFSClient instance (may raise if strict mode is enabled there).
        memory_log_path: Path to the JSON index (kept for compatibility).
        audit_log_path: Path to the append-only JSONL audit log (new, optional).
        strict: If True (default), raise on failures (IPFS, log corruption, etc.).
                If False, log and return (None, None) on store failures (legacy behavior).
    """
    def __init__(
        self,
        ipfs_client: IPFSClient,
        memory_log_path: str = "./memory_log.json",
        audit_log_path: Optional[str] = "./memory_log.jsonl",
        strict: bool = True,
    ):
        self.ipfs_client = ipfs_client
        self.memory_log_path = memory_log_path
        self.audit_log_path = audit_log_path
        self.strict = strict
        self.memory_index = self._load_or_init_log()
        logging.info("PermanentMemory initialized.")

    def _load_or_init_log(self) -> Dict[str, dict]:
        if os.path.exists(self.memory_log_path):
            try:
                with open(self.memory_log_path, "r", encoding="utf-8") as f:
                    return json.load(f)
            except Exception as e:
                logging.warning(f"Corrupted memory log at {self.memory_log_path}: {e}. Reinitializing.")
                if self.strict:
                    # Surface corruption in strict mode; caller can decide to handle/restore
                    raise
                return {}
        return {}

    def _store_log(self) -> None:
        _atomic_write_json(self.memory_log_path, self.memory_index)

    async def store_memory(self, data: dict, context_tags: list[str], creator_id: str):
        """
        Wrap and persist a memory entry to IPFS.
        Returns (entry_id, cid) on success.
        In strict mode, raises on failure; otherwise returns (None, None).
        """
        entry_id = str(uuid.uuid4())
        timestamp = datetime.utcnow().isoformat() + "Z"
        data_hash = sha256(_canonical_json(data)).hexdigest()

        wrapped_data = {
            "id": entry_id,
            "timestamp": timestamp,
            "creator": creator_id,
            "tags": context_tags,
            "data": data,
            "integrity": data_hash,
            "metadata": {
                "location": "geo_ip",  # resolved dynamically at runtime if needed
                "device": "macbook-pro.local",
                "source_script": "activation_sequence.py",
            },
        }

        try:
            # IPFSClient may raise if constructed in strict mode; we honor that.
            cid = self.ipfs_client.add_json(wrapped_data)
            if not cid:
                # If IPFS client is non-strict and returned None, escalate in our strict mode
                msg = "IPFS write failed (no CID)"
                logging.error(msg)
                if self.strict:
                    raise RuntimeError(msg)
                return None, None

            # Update in-memory index and persist JSON index
            self.memory_index[entry_id] = {
                "cid": cid,
                "tags": context_tags,
                "creator": creator_id,
                "timestamp": timestamp,
            }
            self._store_log()

            # Append-only JSONL audit (optional)
            if self.audit_log_path:
                _append_jsonl(self.audit_log_path, {"cid": cid, **wrapped_data})

            logging.info(f"Memory stored: {entry_id} → CID {cid}")
            return entry_id, cid

        except Exception as e:
            logging.error(f"Error storing memory: {e}")
            if self.strict:
                raise
            return None, None

    def retrieve_memory(self, entry_id: str):
        if entry_id in self.memory_index:
            cid = self.memory_index[entry_id]["cid"]
            return self.ipfs_client.cat_json(cid)
        else:
            logging.warning(f"Memory ID {entry_id} not found.")
            return None

    def search_by_tag(self, tag: str):
        return {k: v for k, v in self.memory_index.items() if tag in v.get("tags", [])}

    async def record_diplomatic_event(
        self,
        event_type: str,
        content: dict,
        agent_id: str = "unknown",
        extra_tags: list[str] = None,
        voice_reference: dict = None,
    ):
        """
        Wraps and stores a diplomatic interaction related to Belel Concordium.
        Uses IPFS-backed permanent memory system without affecting core logic.
        """
        tags = ["concordium", "diplomatic", event_type.lower()]
        if extra_tags:
            tags.extend(extra_tags)

        if voice_reference:
            content = dict(content or {})
            content["spoken"] = voice_reference

        wrapped = {
            "event_type": event_type,
            "agent_id": agent_id,
            "content": content,
            "timestamp": datetime.utcnow().isoformat() + "Z",
        }

        return await self.store_memory(
            data=wrapped,
            context_tags=tags,
            creator_id="ConcordiumOutreach",
        )