/ supervisor.py
supervisor.py
 1  import subprocess, os, time, sys
 2  
 3  class SovereignAgent:
 4      def __init__(self):
 5          self.vault = "/dev/shm/.sov_vault"
 6          self.log_file = "/dev/shm/supervisor.log"
 7          os.makedirs(self.vault, exist_ok=True)
 8  
 9      def log(self, msg):
10          timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
11          with open(self.log_file, "a") as f:
12              f.write(f"[{timestamp}] {msg}\n")
13          print(f"[*] {msg}")
14  
15      def execute_and_learn(self, command, context):
16          self.log(f"Task: {context}")
17          # Use sudo -n (non-interactive) to ensure it fails fast instead of hanging
18          cmd_with_flags = f"sudo -n {command}" if "sudo" in command else command
19          
20          process = subprocess.run(command, shell=True, capture_output=True, text=True)
21          
22          if process.returncode != 0:
23              return self.debug_logic(process.stderr, command)
24          
25          return True
26  
27      def debug_logic(self, error, failed_cmd):
28          self.log(f"Self-Correcting Error: {error[:50]}...")
29          
30          # Scenario A: Tor configuration conflict
31          if "rendezvous" in error or "Permission denied" in error:
32              fix = "sudo tor --defaults-torrc /dev/null --DataDirectory /dev/shm/.t --ControlPort 9051 --CookieAuthentication 0 --runasdaemon 1"
33              return self.execute_and_learn(fix, "Force-starting isolated Tor instance")
34  
35          # Scenario B: Ghost processes blocking ports
36          if "address already in use" in error.lower():
37              self.log("Nuking zombie ports 9051/11438")
38              subprocess.run("sudo fuser -k 9051/tcp 11438/tcp", shell=True)
39              return self.execute_and_learn(failed_cmd, "Retrying after port clearance")
40  
41          return False
42  
43      def run_loop(self):
44          # Establish infrastructure
45          self.execute_and_learn("systemctl stop tor", "Stopping system-level Tor")
46          self.execute_and_learn("tor --ControlPort 9051 --runasdaemon 1", "Initializing Tor Control")
47          self.execute_and_learn("python3 sovereign_bot.py &", "Deploying Ghost Hunter")
48  
49  if __name__ == "__main__":
50      agent = SovereignAgent()
51      while True:
52          try:
53              agent.run_loop()
54          except Exception as e:
55              agent.log(f"CRITICAL FAULT: {e}")
56          time.sleep(60)