Spaces:
Sleeping

File size: 20,361 Bytes
a44d33b
 
 
 
 
 
 
 
 
 
 
 
 
16c6510
a44d33b
 
 
 
349c764
a44d33b
 
 
 
16c6510
 
 
a44d33b
16c6510
 
a44d33b
 
 
 
 
 
 
 
 
47e0b60
e5f6dca
 
 
 
2475a1e
0ef167d
 
 
e5f6dca
677d855
 
 
 
 
 
 
 
2475a1e
 
16c6510
 
 
 
 
 
 
2475a1e
 
e5f6dca
16c6510
 
 
e5f6dca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a44d33b
e5f6dca
a44d33b
 
e5f6dca
 
 
 
 
 
 
 
a44d33b
 
e5f6dca
 
a44d33b
 
 
 
 
 
 
 
 
2475a1e
0ef167d
 
 
 
 
 
 
 
a44d33b
 
2475a1e
 
 
 
 
0ef167d
2475a1e
 
 
 
 
a44d33b
 
 
 
 
 
 
 
b7ccf77
 
 
0ef167d
b7ccf77
 
 
a44d33b
2b05ca3
 
 
 
 
 
 
 
16c6510
 
2b05ca3
 
16c6510
2b05ca3
 
 
03d34ae
 
 
 
a44d33b
636db5f
 
 
 
 
 
 
b7ccf77
 
 
 
 
 
0ef167d
b7ccf77
 
 
 
 
 
 
 
 
 
 
a44d33b
 
b7ccf77
 
 
2475a1e
b7ccf77
 
0ef167d
2475a1e
 
 
 
 
b7ccf77
 
a44d33b
 
 
 
b7ccf77
 
 
2475a1e
b7ccf77
 
a44d33b
 
 
 
 
 
 
 
 
 
 
16c6510
a44d33b
 
 
 
 
 
 
 
 
 
 
 
b7ccf77
a44d33b
 
 
 
 
 
 
 
 
b7ccf77
 
 
3c26e1e
b7ccf77
 
a44d33b
 
 
 
 
 
 
 
 
b7ccf77
 
 
 
3c26e1e
b7ccf77
 
a44d33b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c26e1e
a44d33b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2475a1e
 
 
 
 
 
a44d33b
 
 
 
2475a1e
 
 
 
 
 
 
 
 
 
 
 
 
a44d33b
 
 
 
2b05ca3
 
 
 
 
 
 
 
16c6510
 
 
 
 
 
 
 
a44d33b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
349c764
 
 
a44d33b
 
 
 
 
 
349c764
 
 
a44d33b
 
 
349c764
 
 
 
 
a44d33b
 
 
 
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# core/memory_graph.py
"""
Graphify-backed shared deal memory for all 5 partner agents.

Every deal evaluation is logged to a shared on-disk graph (JSON namespace under
./fusion_memory). Agents query past deals and learned checklist items
before acting, so the team gets measurably faster on repeat evaluations.

Files:
  - incidents.json:        all past deals with per-partner finding timelines
  - attack_patterns.json:  learned checklists per risk category
  - agent_profiles.json:   per-partner learning stats (findings, deals seen)
"""
import asyncio
import json
import logging
import threading
from pathlib import Path
from typing import Optional, List, Dict
from datetime import datetime, timezone

logger = logging.getLogger("fusion.memory_graph")

# Sync lock for all synchronous callers (create_incident, delete_incident, etc.)
# ponytail: threading.Lock in async functions blocks the event loop thread; use
# _ASYNC_LOCK for async methods instead.
_LOCK = threading.Lock()
# Async lock for log_finding (called concurrently by the five parallel agents).
_ASYNC_LOCK = asyncio.Lock()


def _utcnow() -> str:
    return datetime.now(timezone.utc).isoformat()


class MemoryGraph:
    """Shared deal memory graph used by every FUSION agent."""

    def __init__(self, graphify_dir: str = str(Path(__file__).parent.parent / "fusion_memory"), uid: str | None = None):
        self.graphify_dir = graphify_dir
        self._explicit_uid = uid

    @property
    def _resolved_uid(self) -> "str | None":
        """The effective uid — explicit override, else current_uid ContextVar, else None.
        ponytail: sim_state.active_uid fallback removed — it caused cross-session namespace leakage
        because the global sim_state carries the last active uid, not the requesting user's uid."""
        uid = self._explicit_uid
        if uid is None:
            try:
                from core.auth import current_uid
                val = current_uid.get(None)
                if val and val not in ("__mcp_client__", "__public__"):
                    uid = val
            except Exception:
                pass
        return uid

    @staticmethod
    def _sanitize_uid(uid: str) -> str:
        # Allow only characters safe for a directory component — same set as session_id.
        # ponytail: Firebase UIDs are alphanumeric in practice, but a crafted or
        # compromised token containing '..' or '/' would escape fusion_memory/ without this.
        return "".join(c for c in uid if c.isalnum() or c in ("-", "_"))

    @property
    def base_path(self) -> Path:
        base = Path(self.graphify_dir)
        raw = self._resolved_uid or "__public__"
        safe = self._sanitize_uid(raw) or "__public__"
        p = base / safe
        p.mkdir(parents=True, exist_ok=True)
        return p

    @property
    def incidents_file(self) -> Path:
        self._ensure_files()
        return self.base_path / "incidents.json"

    @property
    def patterns_file(self) -> Path:
        self._ensure_files()
        return self.base_path / "attack_patterns.json"

    @property
    def agent_profiles_file(self) -> Path:
        self._ensure_files()
        return self.base_path / "agent_profiles.json"

    @property
    def chat_file(self) -> Path:
        self._ensure_files()
        return self.base_path / "chat_history.json"

    def _ensure_files(self):
        # Use base_path directly to prevent infinite recursion
        base = self.base_path
        inc = base / "incidents.json"
        pat = base / "attack_patterns.json"
        prof = base / "agent_profiles.json"
        chat = base / "chat_history.json"
        
        for f in [inc, pat, prof]:
            if not f.exists():
                f.write_text(json.dumps({}, indent=2))
        if not chat.exists():
            chat.write_text(json.dumps([], indent=2))

    # ── Incidents ─────────────────────────────────────────────────────────

    def create_incident(self, incident_id: str, metadata: dict) -> dict:
        """Start a new incident record.

        incident_id: e.g. "INC-20260610-084500"
        metadata:    e.g. {"trigger": "phishing_email", "threat_level": 7}
        """
        ruid = self._resolved_uid
        fs_uid = ruid or "__public__"
        # Only register real authenticated uids — prevents None → None poisoning the registry
        if ruid:
            try:
                from api.state import register_incident_uid
                register_incident_uid(incident_id, ruid)
            except Exception:
                pass
        with _LOCK:
            incidents = self._read_file(self.incidents_file)
            # If local is empty (e.g. after a server restart), seed from Firestore first
            # so that creating a new deal doesn't erase prior history from the dashboard.
            if not incidents:
                try:
                    from core.firestore_memory import fs_list_incidents
                    remote = fs_list_incidents(fs_uid)
                    if remote:
                        incidents = remote
                        logger.info("Memory: seeded local cache from Firestore (%d incidents)", len(remote))
                except Exception:
                    pass
            incidents[incident_id] = {
                "metadata": metadata,
                "timeline": [],
                "final_decision": None,
                "created_at": _utcnow(),
            }
            self._write_file(self.incidents_file, incidents)
            logger.info(f"Memory: created incident {incident_id}")
        # Mirror to Firestore so it survives server restarts
        try:
            from core.firestore_memory import fs_save_incident
            fs_save_incident(fs_uid, incident_id, incidents[incident_id])
        except Exception:
            pass
        return incidents[incident_id]

    def delete_incident(self, incident_id: str):
        """Delete an incident record locally and from Firestore."""
        with _LOCK:
            incidents = self._read_file(self.incidents_file)
            if incident_id in incidents:
                del incidents[incident_id]
                self._write_file(self.incidents_file, incidents)
                logger.info(f"Memory: deleted incident {incident_id}")
        # Mirror deletion to Firestore — use resolved uid with explicit fallback so
        # a None uid doesn't silently target __public__ on behalf of a real user.
        try:
            from core.firestore_memory import fs_delete_incident
            fs_delete_incident(self._resolved_uid or "__public__", incident_id)
        except Exception as e:
            logger.debug(f"Failed to delete incident from Firestore: {e}")

    def get_all_incidents(self) -> dict:
        """Return all incidents keyed by incident_id."""
        return self._read_file(self.incidents_file)

    def get_incident(self, incident_id: str) -> Optional[dict]:
        ruid = self._resolved_uid
        if ruid:
            try:
                from api.state import register_incident_uid
                register_incident_uid(incident_id, ruid)
            except Exception:
                pass
        data = self._read_file(self.incidents_file).get(incident_id)
        if data:
            return data
        # Fallback: local file was wiped (server restart) — try Firestore
        try:
            from core.firestore_memory import fs_get_incident
            fs_data = fs_get_incident(self._resolved_uid or "__public__", incident_id)
            if fs_data:
                logger.info(f"[Memory] Recovered incident {incident_id} from Firestore")
                # Write back to local file so subsequent reads are fast
                with _LOCK:
                    incidents = self._read_file(self.incidents_file)
                    incidents[incident_id] = fs_data
                    self._write_file(self.incidents_file, incidents)
                return fs_data
        except Exception:
            pass
        return None

    def list_incidents(self) -> Dict[str, dict]:
        local = self._read_file(self.incidents_file)
        if local:
            return local
        # Fallback to Firestore on a fresh container (local wiped by restart)
        try:
            from core.firestore_memory import fs_list_incidents
            remote = fs_list_incidents(self._resolved_uid or "__public__")
            if remote:
                # Cache locally so subsequent reads don't hit Firestore every time
                with _LOCK:
                    self._write_file(self.incidents_file, remote)
            return remote
        except Exception:
            return {}

    def get_latest_incident_id(self) -> Optional[str]:
        incidents = self._read_file(self.incidents_file)
        if not incidents:
            # Fallback to Firestore
            try:
                from core.firestore_memory import fs_get_latest_incident_id
                return fs_get_latest_incident_id(self._resolved_uid)
            except Exception:
                return None
        return max(incidents, key=lambda k: incidents[k].get("created_at", ""))

    async def log_finding(
        self,
        incident_id: str,
        agent_name: str,
        finding: str,
        severity: int = 5,
        tags: Optional[list] = None,
    ) -> bool:
        """Agent logs a finding. Shared across all agents."""
        async with _ASYNC_LOCK:
            incidents = self._read_file(self.incidents_file)
            if incident_id not in incidents:
                logger.warning(f"Memory: incident {incident_id} not found for finding")
                return False
            incidents[incident_id]["timeline"].append({
                "agent": agent_name,
                "finding": finding,
                "severity": severity,
                "tags": tags or [],
                "timestamp": _utcnow(),
            })
            self._write_file(self.incidents_file, incidents)
            updated = incidents[incident_id]

            # Update agent learning profile
            profiles = self._read_file(self.agent_profiles_file)
            profile = profiles.setdefault(agent_name, {"findings_logged": 0, "incidents": []})
            profile["findings_logged"] += 1
            if incident_id not in profile["incidents"]:
                profile["incidents"].append(incident_id)
            profile["last_active"] = _utcnow()
            self._write_file(self.agent_profiles_file, profiles)
        # Mirror updated incident to Firestore
        try:
            from core.firestore_memory import fs_save_incident
            fs_save_incident(self._resolved_uid or "__public__", incident_id, updated)
        except Exception:
            pass
        return True

    def set_final_decision(self, incident_id: str, decision: str) -> bool:
        with _LOCK:
            incidents = self._read_file(self.incidents_file)
            if incident_id not in incidents:
                return False
            incidents[incident_id]["final_decision"] = decision
            self._write_file(self.incidents_file, incidents)
            updated = incidents[incident_id]
        # Mirror to Firestore
        try:
            from core.firestore_memory import fs_save_incident
            fs_save_incident(self._resolved_uid or "__public__", incident_id, updated)
        except Exception:
            pass
        return True

    async def query_similar_incidents(
        self,
        attack_technique: str,
        limit: int = 5,
    ) -> List[dict]:
        """Search memory for past incidents tagged with a MITRE technique."""
        incidents = self._read_file(self.incidents_file)
        results = []
        needle = attack_technique.strip().upper()
        for inc_id, inc in incidents.items():
            for event in inc.get("timeline", []):
                tags = [str(t).upper() for t in event.get("tags", [])]
                if needle in tags or needle in str(event.get("finding", "")).upper():
                    results.append({
                        "incident_id": inc_id,
                        "agent": event["agent"],
                        "finding": event["finding"],
                        "severity": event.get("severity", 5),
                        "timestamp": event["timestamp"],
                    })
        # Most recent matches first
        results.sort(key=lambda r: r["timestamp"], reverse=True)
        return results[:limit]

    # ── Learned attack patterns ───────────────────────────────────────────

    async def record_attack_pattern(
        self,
        mitre_id: str,
        detection_method: str,
        defense_action: str,
        success_rate: float = 0.8,
    ):
        """Team learns: this MITRE technique is best blocked by X."""
        async with _ASYNC_LOCK:
            patterns = self._read_file(self.patterns_file)
            patterns.setdefault(mitre_id, []).append({
                "detection": detection_method,
                "defense": defense_action,
                "success_rate": success_rate,
                "learned_at": _utcnow(),
            })
            self._write_file(self.patterns_file, patterns)
            logger.info(f"Memory: learned defense recipe for {mitre_id}")

    async def get_defense_recipe(self, mitre_id: str) -> Optional[dict]:
        """Retrieve the team's best-known defense for a MITRE technique."""
        patterns = self._read_file(self.patterns_file)
        methods = patterns.get(mitre_id, [])
        if not methods:
            return None
        return max(methods, key=lambda x: x.get("success_rate", 0))

    # ── Commander chat history ────────────────────────────────────────────

    def _get_chat_file_for_session(self, session_id: Optional[str]) -> Path:
        if session_id:
            safe_id = "".join([c for c in session_id if c.isalnum() or c in ("-", "_")])
            if safe_id:
                return self.base_path / f"chat_history_{safe_id}.json"
        return self.chat_file

    def append_chat(self, role: str, content: str, meta: Optional[dict] = None, session_id: Optional[str] = None) -> dict:
        """Persist one chat turn (user or commander) so the conversation
        survives reloads and shows up in the Memory tab."""
        turn = {
            "role": role,
            "content": content,
            "meta": meta or {},
            "timestamp": _utcnow(),
        }
        chat_file = self._get_chat_file_for_session(session_id)
        with _LOCK:
            history = self._read_list(chat_file)
            history.append(turn)
            # Keep the on-disk log bounded; the UI only needs recent context
            self._write_file(chat_file, history[-500:])
        # Mirror to Firestore so chat survives server restarts
        try:
            from core.firestore_memory import fs_append_chat
            fs_append_chat(self._resolved_uid, session_id, turn)
        except Exception:
            pass
        return turn

    def get_chat_history(self, limit: int = 100, session_id: Optional[str] = None) -> List[dict]:
        chat_file = self._get_chat_file_for_session(session_id)
        local = self._read_list(chat_file)
        if local:
            return local[-limit:]
        # Fallback to Firestore on a fresh container
        try:
            from core.firestore_memory import fs_get_chat_history
            remote = fs_get_chat_history(self._resolved_uid, session_id, limit)
            if remote:
                with _LOCK:
                    self._write_file(chat_file, remote[-500:])
            return remote
        except Exception:
            return []

    def clear_chat_history(self, session_id: Optional[str] = None):
        chat_file = self._get_chat_file_for_session(session_id)
        with _LOCK:
            if session_id and chat_file.exists():
                try:
                    chat_file.unlink()
                    logger.info(f"Memory: unlinked session file {chat_file}")
                except Exception as e:
                    logger.warning(f"Failed to delete session file {chat_file}: {e}")
            else:
                self._write_file(chat_file, [])
        # Mirror deletion to Firestore so deleted sessions don't resurrect on restart
        try:
            from core.firestore_memory import _chat_turns
            for doc in _chat_turns(self._resolved_uid, session_id).stream():
                doc.reference.delete()
            logger.info(f"Memory: cleared Firestore chat turns for session={session_id or 'default'}")
        except Exception as e:
            logger.debug(f"Memory: Firestore chat clear skipped: {e}")

    def clear_all(self):
        """Wipe everything: all deals, learned patterns, agent profiles, and chat history.
        Used by the Settings 'Reset & Clear All History' control."""
        with _LOCK:
            self._write_file(self.incidents_file, {})
            self._write_file(self.patterns_file, {})
            self._write_file(self.agent_profiles_file, {})
            self._write_file(self.chat_file, [])
            for p in self.base_path.glob("chat_history_*.json"):
                try:
                    p.unlink()
                except Exception as e:
                    logger.warning(f"Failed to delete session file {p}: {e}")
        logger.info("Memory: cleared ALL history (incidents, patterns, profiles, chat).")

    # ── Summaries ─────────────────────────────────────────────────────────

    def get_team_summary(self, incident_id: str) -> str:
        """Generate a plain-English summary of team findings."""
        inc = self.get_incident(incident_id)
        if not inc:
            return "Incident not found."
        summary = f"Incident {incident_id}:\n"
        summary += f"Threat Level: {inc['metadata'].get('threat_level', '?')}/10\n\n"
        summary += "Agent Findings:\n"
        for event in inc["timeline"]:
            summary += f"- {event['agent']}: {event['finding']} (severity: {event['severity']})\n"
        if inc.get("final_decision"):
            summary += f"\nFinal Decision: {inc['final_decision']}\n"
        return summary

    def get_memory_stats(self) -> dict:
        """High-level stats: how much the team has learned so far."""
        incidents = self._read_file(self.incidents_file)
        patterns = self._read_file(self.patterns_file)
        profiles = self._read_file(self.agent_profiles_file)
        return {
            "total_incidents": len(incidents),
            "total_findings": sum(len(i.get("timeline", [])) for i in incidents.values()),
            "learned_patterns": {k: len(v) for k, v in patterns.items()},
            "agent_profiles": profiles,
        }

    # ── IO ────────────────────────────────────────────────────────────────

    def _read_file(self, path: Path) -> dict:
        try:
            return json.loads(path.read_text())
        except Exception as e:
            if path.exists():
                logger.warning("_read_file: corrupt or unreadable %s — %s", path, e)
            return {}

    def _read_list(self, path: Path) -> list:
        try:
            data = json.loads(path.read_text())
            return data if isinstance(data, list) else []
        except Exception as e:
            if path.exists():
                logger.warning("_read_list: corrupt or unreadable %s — %s", path, e)
            return []

    def _write_file(self, path: Path, data):
        # Write to a temp file first, then rename atomically to prevent
        # a server crash mid-write from leaving a corrupt JSON file.
        tmp = path.with_suffix('.tmp')
        tmp.write_text(json.dumps(data, indent=2))
        tmp.replace(path)


# Global singleton shared by the API, the agents, and the MCP server
memory_graph = MemoryGraph()