""" logos/oversight.py - The "Context Graph" Manager Protocol 26: Autonomous Persistence & Validation Functions: 1. Process Persistence: Keeps 'server.py' alive. 2. Context Monitoring: Traces health/decisions (Video 14). 3. Continuous Validation: Runs 'verify_loop.py' on schedule/change. """ import subprocess import time import sys import os import logging from datetime import datetime # Configure Logging (The Context Graph) logging.basicConfig( filename='logos_context.log', level=logging.INFO, format='%(asctime)s | %(levelname)s | %(message)s' ) logger = logging.getLogger("OVERSIGHT") SERVER_CMD = [sys.executable, "-m", "logos.server"] TEST_CMD = [sys.executable, "tests/verify_loop.py"] class OversightDaemon: def __init__(self): self.server_process = None self.health_score = 100.0 self.context_trace = [] # In-memory context graph def start_server(self): logger.info("[OP] Starting Manifold Server...") self.server_process = subprocess.Popen(SERVER_CMD) self.log_context("SERVER_START", "Process initiated") def run_tests(self): logger.info("[QA] Running Validation Suite...") try: # Force UTF-8 to avoid Windows charmap errors result = subprocess.run( TEST_CMD, capture_output=True, text=True, encoding='utf-8', errors='replace', timeout=30 ) if result.returncode == 0: logger.info(" ✅ Tests Passed.") self.log_context("TEST_PASS", "Verification successful") return True else: logger.error(f" ❌ Tests Failed:\n{result.stderr}") self.log_context("TEST_FAIL", f"Exit Code {result.returncode}") return False except Exception as e: logger.error(f" ❌ Test Execution Error: {e}") return False def log_context(self, event_type, details): """Builds the Context Graph node.""" node = { "timestamp": time.time(), "type": event_type, "details": details, "server_pid": self.server_process.pid if self.server_process else None } self.context_trace.append(node) # Prune old context if len(self.context_trace) > 100: self.context_trace.pop(0) def monitor_loop(self): self.start_server() try: while True: # 1. Check Server Liveness if self.server_process.poll() is not None: logger.warning("[⚠️] Server died! Restarting...") self.log_context("SERVER_CRASH", "Unexpected exit") self.start_server() # 2. Periodic Validation (Simulation of "Active Oversight") # In real world, trigger this on file events. Here, every 60s. if int(time.time()) % 60 == 0: success = self.run_tests() if not success: logger.warning("[⚠️] System Unstable (Tests Failed).") # Implementation detail: Could auto-rollback or alert user time.sleep(1) except KeyboardInterrupt: logger.info("[OP] Stopping Oversight...") if self.server_process: self.server_process.terminate() if __name__ == "__main__": print("CORE: Starting Logos Oversight Daemon (Protocol 26)...") daemon = OversightDaemon() daemon.monitor_loop()