import asyncio import logging import os from typing import Set logger = logging.getLogger(__name__) # The comprehensive trigger list based on the Vault Manifest Requirements AUTO_REFRESH_TRIGGER_EVENTS: Set[str] = { "feature_implemented", "gmail_oauth_completed", "credential_captured", "vault_credentials_added", "rvc_model_updated", "gaming_session_completed", "nvidia_keys_configured", "bridge_sync_established", "daily_checkin_completed", "family_device_enrolled", "codebase_reindexed", "research_alert", # Research Engine Expansion } _debounce_task = None _DEBOUNCE_DELAY_SECONDS = 30 # Wait 30 seconds after the last event before building async def maybe_trigger_vault_refresh(event_type: str): """ Evaluates if an event should trigger a Vault rebuild. If it should, it debounces the request so a flood of events only causes 1 rebuild. """ if event_type in AUTO_REFRESH_TRIGGER_EVENTS: logger.info(f"VAULT AUTO-REFRESH: Event '{event_type}' triggered a rebuild request. Debouncing for {_DEBOUNCE_DELAY_SECONDS}s.") global _debounce_task if _debounce_task is not None: _debounce_task.cancel() _debounce_task = asyncio.create_task(_execute_vault_rebuild_after_delay()) async def _execute_vault_rebuild_after_delay(): try: await asyncio.sleep(_DEBOUNCE_DELAY_SECONDS) await _perform_build() except asyncio.CancelledError: pass # A newer event canceled this task to reset the timer except Exception as e: logger.error(f"VAULT AUTO-REFRESH FAILED: {e}") async def _perform_build(): from backend.security.vault_builder import build_vault logger.info("VAULT AUTO-REFRESH: Executing FULL and CLEAN background rebuilds...") source_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) try: # Build FULL mode full_target = os.path.join(source_path, "OMEGA_CORE_V15_FULL.vault") await build_vault("full", source_path, full_target) # Build CLEAN mode clean_target = os.path.join(source_path, "OMEGA_CORE_V15_CLEAN.vault") await build_vault("sanitized", source_path, clean_target) logger.info("VAULT AUTO-REFRESH: Successfully generated OMEGA_CORE_V15_FULL.vault and OMEGA_CORE_V15_CLEAN.vault.") except Exception as e: logger.error(f"VAULT AUTO-REFRESH: Exception during rebuild: {e}")