File size: 12,711 Bytes
815b2e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
"""
global_state_sync_optimized.py β€” Sincronizzazione Selettiva Ottimizzata per Workload Separati

Architettura Separazione Workload:
  A: Analytics/Cache/Read-Heavy (NO sincronizzazione, solo letture)
  B: Sync/State/Transazioni (PRIMARY β€” sincronizzazione completa)
  C: Memory/RAG/Embeddings (sincronizzazione memoria e embeddings)
  D: Audit/Logging/Compliance (sincronizzazione audit-only, append-only)

Strategia Sincronizzazione:
  1. B ← A: Nessuna (A Γ¨ read-only, non genera stato)
  2. B ← C: Sincronizzazione memoria/skill (bidirezionale)
  3. B ← D: Sincronizzazione audit (unidirezionale, D β†’ B)
  4. C ← B: Sincronizzazione stato globale (unidirezionale, B β†’ C)
  5. D ← B: Sincronizzazione eventi (unidirezionale, B β†’ D)

Benefici:
  - Riduce traffico di sincronizzazione (solo tabelle rilevanti)
  - Evita conflitti di write (ogni nodo scrive su tabelle specifiche)
  - Mantiene audit immutabile (D append-only)
  - Massimizza throughput (parallelizzazione per nodo)
"""

import os
import asyncio
import logging
from typing import Optional, Dict, List, Any, Literal
from datetime import datetime, timedelta
from enum import Enum
import json

_logger = logging.getLogger("global_state_sync_optimized")

# ── Enumerazione Nodi ──────────────────────────────────────────────────────
class SupabaseNode(str, Enum):
    A = "A"  # Analytics/Cache
    B = "B"  # PRIMARY (Sync/State)
    C = "C"  # Memory/RAG
    D = "D"  # Audit/Logging


# ── Configurazione Nodi ───────────────────────────────────────────────────
SUPABASE_CONFIG = {
    "A": {
        "url": os.getenv("SUPABASE_URL_A", ""),
        "key": os.getenv("SUPABASE_KEY_A", ""),
        "role": "Analytics/Cache",
        "sync_enabled": False,  # A non sincronizza (read-only)
    },
    "B": {
        "url": os.getenv("SUPABASE_URL", ""),  # PRIMARY
        "key": os.getenv("SUPABASE_KEY", ""),
        "role": "Sync/State (PRIMARY)",
        "sync_enabled": True,
    },
    "C": {
        "url": os.getenv("SUPABASE_URL_C", ""),
        "key": os.getenv("SUPABASE_KEY_C", ""),
        "role": "Memory/RAG",
        "sync_enabled": True,
    },
    "D": {
        "url": os.getenv("SUPABASE_URL_D", ""),
        "key": os.getenv("SUPABASE_KEY_D", ""),
        "role": "Audit/Logging",
        "sync_enabled": True,
    },
}

# ── Mapping Tabelle β†’ Nodi ─────────────────────────────────────────────────
TABLE_NODE_MAPPING = {
    # Nodo B (PRIMARY) β€” Stato Globale
    "cluster_state": "B",
    "global_state": "B",
    "sync_state": "B",
    "agent_state": "B",
    "daemon_status": "B",
    
    # Nodo C β€” Memoria e RAG
    "agent_memory": "C",
    "skill_memory": "C",
    "embeddings": "C",
    "conversations": "C",
    "rag_index": "C",
    "vector_store": "C",
    
    # Nodo D β€” Audit e Logging
    "audit_events": "D",
    "audit_log": "D",
    "compliance_log": "D",
    "event_log": "D",
    "activity_log": "D",
}

# ── Configurazione Sincronizzazione ────────────────────────────────────────
SYNC_RULES = {
    # (source_node, target_node): [tabelle da sincronizzare]
    ("B", "C"): ["agent_memory", "skill_memory", "cluster_state"],  # B β†’ C: stato globale
    ("C", "B"): ["agent_memory", "skill_memory"],  # C β†’ B: memoria aggiornata
    ("B", "D"): ["audit_events", "event_log"],  # B β†’ D: eventi
    ("D", "B"): [],  # D β†’ B: nessuna (audit Γ¨ append-only)
}

SYNC_ENABLED = os.getenv("GLOBAL_STATE_SYNC_ENABLED", "true").lower() == "true"
SYNC_INTERVAL_SECONDS = int(os.getenv("SYNC_INTERVAL_SECONDS", "30"))


# ── Client Supabase ───────────────────────────────────────────────────────
class SupabaseClient:
    """Client per accedere a un singolo database Supabase."""
    
    def __init__(self, url: str, key: str, node_id: str):
        self.url = url
        self.key = key
        self.node_id = node_id
        self.base_url = f"{url}/rest/v1" if url else None
    
    async def query(self, table: str, filters: Optional[Dict] = None) -> List[Dict]:
        """Esegue una query SELECT su una tabella."""
        if not self.base_url:
            return []
        
        import httpx
        
        url = f"{self.base_url}/{table}"
        headers = {
            "apikey": self.key,
            "Authorization": f"Bearer {self.key}",
            "Content-Type": "application/json",
        }
        
        try:
            async with httpx.AsyncClient() as client:
                response = await client.get(url, headers=headers, timeout=10.0)
                if response.status_code == 200:
                    return response.json()
                else:
                    _logger.warning(f"Supabase {self.node_id} query failed: {response.status_code}")
                    return []
        except Exception as exc:
            _logger.error(f"Supabase {self.node_id} error: {exc}")
            return []
    
    async def insert(self, table: str, data: Dict) -> bool:
        """Inserisce un record in una tabella."""
        if not self.base_url:
            return False
        
        import httpx
        
        url = f"{self.base_url}/{table}"
        headers = {
            "apikey": self.key,
            "Authorization": f"Bearer {self.key}",
            "Content-Type": "application/json",
        }
        
        try:
            async with httpx.AsyncClient() as client:
                response = await client.post(url, json=data, headers=headers, timeout=10.0)
                return response.status_code in (200, 201)
        except Exception as exc:
            _logger.error(f"Supabase {self.node_id} insert error: {exc}")
            return False


# ── Sincronizzazione Selettiva ─────────────────────────────────────────────
class SelectiveSyncManager:
    """Gestisce la sincronizzazione selettiva tra nodi."""
    
    def __init__(self):
        self.clients = {}
        self._enabled = SYNC_ENABLED
        self._last_sync = {}
        
        for node_id, config in SUPABASE_CONFIG.items():
            if config["url"] and config["key"]:
                self.clients[node_id] = SupabaseClient(config["url"], config["key"], node_id)
    
    async def sync_all(self) -> Dict[str, Any]:
        """Esegue la sincronizzazione completa tra tutti i nodi."""
        if not self._enabled or not self.clients:
            return {"ok": False, "error": "Sync disabled or no clients configured"}
        
        results = {
            "ok": True,
            "synced_at": datetime.now().isoformat(),
            "syncs": [],
        }
        
        # Esegui sincronizzazioni secondo le regole
        for (source, target), tables in SYNC_RULES.items():
            if not tables:
                continue  # Salta se nessuna tabella da sincronizzare
            
            source_client = self.clients.get(source)
            target_client = self.clients.get(target)
            
            if not source_client or not target_client:
                continue
            
            sync_result = await self._sync_nodes(source_client, target_client, tables)
            results["syncs"].append(sync_result)
        
        return results
    
    async def _sync_nodes(
        self,
        source: SupabaseClient,
        target: SupabaseClient,
        tables: List[str],
    ) -> Dict[str, Any]:
        """Sincronizza tabelle specifiche da source a target."""
        result = {
            "source": source.node_id,
            "target": target.node_id,
            "tables": tables,
            "synced_count": 0,
            "errors": [],
        }
        
        for table in tables:
            try:
                # Leggi da source
                rows = await source.query(table)
                if not rows:
                    continue
                
                # Scrivi su target (upsert logic)
                for row in rows:
                    success = await target.insert(table, row)
                    if success:
                        result["synced_count"] += 1
                    else:
                        result["errors"].append(f"Failed to sync {table} row")
            
            except Exception as exc:
                result["errors"].append(f"Error syncing {table}: {str(exc)}")
                _logger.error(f"Sync error {source.node_id} β†’ {target.node_id} ({table}): {exc}")
        
        return result
    
    async def sync_memory(self, session_id: str) -> Dict[str, Any]:
        """Sincronizza memoria per una sessione specifica (B ↔ C)."""
        if not self._enabled:
            return {"ok": False}
        
        source_client = self.clients.get("B")
        target_client = self.clients.get("C")
        
        if not source_client or not target_client:
            return {"ok": False, "error": "B or C not configured"}
        
        try:
            # Leggi memoria da B
            rows = await source_client.query("agent_memory", {"session_id": session_id})
            
            # Scrivi su C
            for row in rows:
                await target_client.insert("agent_memory", row)
            
            return {
                "ok": True,
                "session_id": session_id,
                "synced_rows": len(rows),
            }
        except Exception as exc:
            _logger.error(f"Memory sync error: {exc}")
            return {"ok": False, "error": str(exc)}
    
    async def log_audit_event(self, event: Dict) -> bool:
        """Registra un evento di audit su D (append-only)."""
        if not self._enabled:
            return False
        
        target_client = self.clients.get("D")
        if not target_client:
            return False
        
        event["timestamp"] = datetime.now().isoformat()
        return await target_client.insert("audit_events", event)


# ── Scheduler Sincronizzazione ─────────────────────────────────────────────
class SyncScheduler:
    """Scheduler per sincronizzazione periodica."""
    
    def __init__(self, manager: SelectiveSyncManager):
        self.manager = manager
        self._running = False
    
    async def start(self):
        """Avvia il scheduler di sincronizzazione."""
        self._running = True
        _logger.info(f"Starting sync scheduler (interval: {SYNC_INTERVAL_SECONDS}s)")
        
        while self._running:
            try:
                result = await self.manager.sync_all()
                if result["ok"]:
                    _logger.debug(f"Sync completed: {len(result['syncs'])} operations")
                else:
                    _logger.warning(f"Sync failed: {result.get('error')}")
            except Exception as exc:
                _logger.error(f"Sync scheduler error: {exc}")
            
            await asyncio.sleep(SYNC_INTERVAL_SECONDS)
    
    def stop(self):
        """Ferma il scheduler."""
        self._running = False
        _logger.info("Sync scheduler stopped")


# ── Istanza Globale ───────────────────────────────────────────────────────
_sync_manager = SelectiveSyncManager()
_sync_scheduler = SyncScheduler(_sync_manager)


# ── Funzioni Pubbliche ────────────────────────────────────────────────────
async def get_unified_memory(session_id: str) -> Dict[str, Any]:
    """Recupera memoria unificata per una sessione."""
    return await _sync_manager.sync_memory(session_id)


async def sync_all() -> Dict[str, Any]:
    """Esegue sincronizzazione completa."""
    return await _sync_manager.sync_all()


async def log_audit(event: Dict) -> bool:
    """Registra un evento di audit."""
    return await _sync_manager.log_audit_event(event)


async def start_scheduler():
    """Avvia il scheduler di sincronizzazione."""
    await _sync_scheduler.start()


def stop_scheduler():
    """Ferma il scheduler."""
    _sync_scheduler.stop()