""" backend/services/master_vault_ledger.py The Master Vault Ledger is the single source of truth for all major features, system events, and architectural changes in F.R.I.D.A.Y OMEGA. It auto-registers itself into the SQLite database and provides: - record_feature() : Called by any service when a major feature is added/updated. - get_full_ledger() : Returns the full ledger as a structured dict. - get_vault_summary(): Returns a compressed string for injection into LLM prompts. Auto-seeded with all known features at startup so the vault is always current. """ import sqlite3 import time import logging import os def _get_db_path(): try: from backend.services.usb_monitor import get_db_path return get_db_path() except Exception: return os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "memory.db") # ───────────────────────────────────────────────────────────────────────────── # SCHEMA INIT # ───────────────────────────────────────────────────────────────────────────── def _init_ledger_table(): try: db = _get_db_path() with sqlite3.connect(db) as conn: conn.execute("PRAGMA journal_mode=WAL") conn.execute(""" CREATE TABLE IF NOT EXISTS master_vault_ledger ( id INTEGER PRIMARY KEY AUTOINCREMENT, feature_key TEXT NOT NULL UNIQUE, category TEXT NOT NULL, title TEXT NOT NULL, description TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'active', added_at INTEGER NOT NULL, updated_at INTEGER NOT NULL ) """) conn.commit() except Exception as e: logging.error(f"MasterVaultLedger: Failed to init table: {e}") _init_ledger_table() # ───────────────────────────────────────────────────────────────────────────── # CORE API # ───────────────────────────────────────────────────────────────────────────── def record_feature( feature_key: str, category: str, title: str, description: str, status: str = "active" ): """ Upserts a feature record into the Master Vault Ledger. Safe to call repeatedly — uses INSERT OR REPLACE with updated_at refresh. """ try: db = _get_db_path() now = int(time.time()) with sqlite3.connect(db) as conn: conn.execute("PRAGMA journal_mode=WAL") existing = conn.execute( "SELECT added_at FROM master_vault_ledger WHERE feature_key = ?", (feature_key,) ).fetchone() added_at = existing[0] if existing else now conn.execute(""" INSERT INTO master_vault_ledger (feature_key, category, title, description, status, added_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?) ON CONFLICT(feature_key) DO UPDATE SET category = excluded.category, title = excluded.title, description = excluded.description, status = excluded.status, updated_at = excluded.updated_at """, (feature_key, category, title, description, status, added_at, now)) conn.commit() logging.info(f"MasterVaultLedger: Recorded [{category}] '{title}'") except Exception as e: logging.error(f"MasterVaultLedger: Failed to record feature '{feature_key}': {e}") def get_full_ledger() -> list: """Returns the complete ledger as a list of dicts, ordered by category.""" try: db = _get_db_path() with sqlite3.connect(db) as conn: conn.row_factory = sqlite3.Row rows = conn.execute(""" SELECT * FROM master_vault_ledger ORDER BY category, added_at """).fetchall() return [dict(r) for r in rows] except Exception as e: logging.error(f"MasterVaultLedger: Failed to get ledger: {e}") return [] def get_vault_summary() -> str: """Returns a compressed LLM-injectable summary of the vault state.""" ledger = get_full_ledger() if not ledger: return "[MASTER VAULT] No records found." lines = ["[MASTER VAULT LEDGER — F.R.I.D.A.Y OMEGA FEATURE REGISTRY]"] current_cat = None for r in ledger: if r["category"] != current_cat: current_cat = r["category"] lines.append(f"\n [{current_cat.upper()}]") status_icon = "[OK]" if r["status"] == "active" else "[--]" lines.append(f" {status_icon} {r['title']}: {r['description'][:120]}") return "\n".join(lines) # ───────────────────────────────────────────────────────────────────────────── # AUTO-SEED: All known features seeded at module import time # Add new features here — they will be auto-registered on every startup. # ───────────────────────────────────────────────────────────────────────────── def _auto_seed(): KNOWN_FEATURES = [ # ── VOICE & AUDIO ─────────────────────────────────────────────────── ("emergency_replay", "Voice & Audio", "Emergency Voice Replay", "60-second LiveAudioBuffer (PC) + CircularAudioBuffer (Android) with bare-metal AudioTrack PCM playback. LLM-bypass interception in router.py."), ("greeting_intelligence", "Voice & Audio", "Intelligent Day Review Greeting", "Dynamic boot greeting via greeting_intelligence.py analyzing 5 metrics: conversation history, daily event volume, productivity focus, tone/identity, and Master Vault context. Injected post-TTS, saves to memory."), # ── AI ENGINE & ROUTING ──────────────────────────────────────────── ("token_manager_v2", "AI Engine", "Token Manager v2 (Head of System)", "Unified primary/fallback engine in token_manager.py. Gemini 3.5 Flash is primary. Catches ResourceExhausted/429 and routes to NVIDIA Vault. Broadcasts live_key_status via WebSocket."), ("nvidia_vault", "AI Engine", "NVIDIA 15-Key Vault (Full)", "15 NVIDIA API keys injected across 4 tiers: HEAVY_COMPUTE (Keys 1-4: GLM, DeepSeek, MiniMax, Nemotron), AGENTIC (Keys 5-8: Qwen, Kimi, Mistral, Gemma), MULTIMODAL (Keys 9-12: Cosmos, Nemotron Omni), SPECIALIZED (Keys 13-15: Content Safety, PII, Video Detection). All keys healthy and verified."), ("model_routing_rules", "AI Engine", "Intelligent Model Routing Rules", "Task classifier in router.py: Heavy Coding -> GLM-5.1 -> MiniMax M3 -> DeepSeek V4 Pro. Agentic -> Kimi K2.6 -> Nemotron Super. Vision -> Nemotron Nano Omni, Cosmos. Safety -> GLiNER PII, Synthetic Video Detector."), ("omni_memory_sync", "AI Engine", "Global Omni-Memory Hive Mind Sync", "memory_service.py with global_omni_memory SQLite ledger. All 15 NVIDIA + 15 Google keys share a hive mind. Last 5 events injected into every prompt as [RECENT OMNI-MEMORY EVENTS] block. Hard-capped at ~150-200 tokens overhead."), ("nvidia_call_function", "AI Engine", "call_nvidia_model() Live API Bridge", "nvidia_vault.py exposes call_nvidia_model() using OpenAI-compatible integrate.api.nvidia.com endpoint. Token Manager no longer mocks fallback — uses live NVIDIA inference."), # ── SYNCHRONIZATION ──────────────────────────────────────────────── ("cloud_sync", "Synchronization", "Cloud Synchronization Records", "14-step research_workflow.py binds ws_manager.py to Cloud. Events flow: Jarvis -> Backend -> Cloud (permanent record) -> WebSocket broadcast. Cloud sync key designated as CLOUD_SYNC_DOMAIN in usb_vault.py."), ("backend_sync", "Synchronization", "Backend Synchronization Records", "usb_vault.py manages 17 Google API keys including HEAD_SUPERVISOR_KEY with KeyDomain isolation. key_usage_tracker.py monitors per-domain token usage. vault_sanitizer.py strips PII before any external sync."), ("research_approval", "Synchronization", "Research Approval Workflow", "research_workflow.py step 5-7: Backend emits exe:request_approval + apk:request_approval via WebSocket simultaneously. Both EXE and APK must receive and display approval UI before step 8 executes."), ("exe_approval", "Synchronization", "EXE Approval Workflow", "Tauri EXE frontend listens to exe:request_approval WebSocket events. LiveKeyBanner in App.tsx updated to also render approval UI and approval telemetry (Cyan=Google, Green=NVIDIA fallback)."), ("apk_approval", "Synchronization", "APK Approval Workflow", "Android jarvis-mobile-guardian listens to apk:request_approval WebSocket events. Native Kotlin handler updates approval notification UI in real-time."), # ── FRONTEND TELEMETRY ────────────────────────────────────────────── ("live_key_banner", "Frontend Telemetry", "Live Key Status Banner (EXE + APK)", "Token Manager broadcasts live_key_status JSON events via WebSocket. Tauri App.tsx LiveKeyBanner and Android client both react in real-time. UI turns Cyan for Google Primary, Neon Green for NVIDIA Fallback."), ("auto_update", "Frontend Telemetry", "Auto-Update Records", "Vault builder in vault_builder.py stages full environment (codebase, HF server, cloud server, Android APK, SQLite DB, allowlist, config) and encrypts to OMEGA_CORE_V15.vault. Decrypt restores full environment on any new machine."), # ── GAMING COACH ─────────────────────────────────────────────────── ("gaming_coach", "Gaming Coach", "Gaming Coach (Active)", "Gaming Coach module active. Routes gaming session queries through AGENTIC tier keys for low-latency real-time coaching. Session data stored in memory.db gaming_sessions table."), ("gaming_coach_history", "Gaming Coach", "Historical Gaming Coach Memories", "Historical game sessions, strategies, and performance metrics stored in global_omni_memory and memory.db. Hive Mind Sync ensures all AI models aware of past gaming context on every new session."), # ── SECURITY & VAULT ─────────────────────────────────────────────── ("usb_vault", "Security", "USB Vault & Encryption System", "usb_vault.py manages Fernet symmetric encryption for all secrets. encrypt_directory() and decrypt_directory() handle AES-GCM vault files. credential_vault.py uses Argon2id KDF + AES-GCM for per-credential encryption."), ("vault_builder", "Security", "Vault Builder & Backup System", "vault_builder.py stages full environment into temp dir (codebase, HF deploy, cloud deploy, Android APK, AppData SQLite, Chroma, Mongo backup) then encrypts to .vault file. Routes expose /vault/build and /vault/encrypt_to_usb."), ("security_routes", "Security", "Security Routes (Full Coverage)", "security_routes.py exposes: /usb/authorized, /usb/active, /usb/policy, /vault/build, /vault/encrypt_to_usb, /vault/decrypt_from_usb, /vault/secrets, /vault/domain_keys, /vault/key_usage, /vault/credentials, /usb/eject."), ("master_vault_ledger", "Security", "Master Vault Ledger (Self-Updating)", "master_vault_ledger.py auto-seeds all known features into master_vault_ledger SQLite table on every startup. record_feature() is called by services when major features are added. get_vault_summary() injects into LLM prompts."), ] for (key, cat, title, desc) in KNOWN_FEATURES: record_feature(key, cat, title, desc) _auto_seed()