import os import time import json import subprocess from datetime import datetime # ========================================================== # 13 Multi-Agent Instinct Controller # 功能:实时监控系统变更,为智能体提供“本能”反馈 # ========================================================== WATCH_PATHS = ["/app", "/root/.openclaw/workspace"] INSTINCT_LOG = "/tmp/instinct_knowledge.json" GENE_POOL = "/tmp/gene_pool.json" def log_event(event_type, path, detail=""): timestamp = datetime.now().isoformat() entry = { "timestamp": timestamp, "type": event_type, "path": path, "detail": detail } print(f"[instinct] {event_type} detected: {path}") # 持久化到知识库 history = [] if os.path.exists(INSTINCT_LOG): try: with open(INSTINCT_LOG, 'r') as f: history = json.load(f) except: pass history.append(entry) with open(INSTINCT_LOG, 'w') as f: json.dump(history[-100:], f, indent=2) def monitor(): """极简 Inotify 模拟(轮询版本,保持兼容性)""" last_snapshot = {} while True: for p in WATCH_PATHS: if not os.path.exists(p): continue # 扫描文件状态 try: files = os.listdir(p) for f in files: full_path = os.path.join(p, f) mtime = os.path.getmtime(full_path) if full_path not in last_snapshot: log_event("BIRTH", full_path, "New file detected (potential Red intrusion)") elif last_snapshot[full_path] < mtime: log_event("MUTATION", full_path, "File modified (potential Blue patch or Red update)") last_snapshot[full_path] = mtime except: pass time.sleep(5) if __name__ == "__main__": print("[init] 13 Instinct Controller Starting...") if not os.path.exists(GENE_POOL): with open(GENE_POOL, 'w') as f: json.dump({"evolution_count": 0, "top_agent": "Red"}, f) monitor()