-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevolution_sandbox.py
More file actions
93 lines (73 loc) · 3.46 KB
/
evolution_sandbox.py
File metadata and controls
93 lines (73 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
Evolution Sandbox — A safe, high-speed environment for testing recursive self-evolution.
Part of Phase 12: Galactic Scale & Quantum Logic.
Allows the agent to run infinite self-recursive loops in isolation.
"""
import os
import sys
import time
import shutil
# Add parent dir to path
sys.path.append(os.path.abspath(os.path.curdir))
from self_mod_engine import SelfModEngine
from wasm_sandbox import WasmSandbox
class EvolutionSandbox:
def __init__(self, target_file: str = "sandbox_agent.py"):
self.target_file = target_file
self.source_backup = f"{target_file}.orig"
self._prepare_sandbox()
self.self_mod = SelfModEngine(source_file=target_file, backup_dir="sandbox_backups")
def _prepare_sandbox(self):
"""Create a dummy agent file for the sandbox."""
dummy_code = """
class SandboxAgent:
def __init__(self):
self.version = 1.0
self.capabilities = ["basic_logic"]
def process(self, data):
return f"Processed {data} with version {self.version}"
"""
with open(self.target_file, "w") as f:
f.write(dummy_code.strip())
shutil.copy2(self.target_file, self.source_backup)
print(f"🛠️ Sandbox prepared: {self.target_file}")
def run_evolution_cycle(self, iterations: int = 5):
"""Run multiple generations of self-evolution."""
print(f"🚀 Starting Evolution Sandbox: {iterations} generations...")
for i in range(iterations):
print(f"\n--- Generation {i+1} ---")
# 1. Analyze
analysis = self.self_mod.analyze_source()
print(f" Analysis: {analysis.get('methods')} methods, {analysis.get('lines')} lines.")
# 2. Propose & Apply Modification
# Simulated: Adding a new specialized method in each generation
method_name = f"evolve_capability_{i+1}"
method_code = f"return 'Evolution level {i+1} reached.'"
# --- FEATURE 6: Verify through Wasm Sandbox before applying ---
print(f" [SECURITY] Verifying {method_name} in Wasm Sandbox...")
sandbox = WasmSandbox()
# Creating a test execution script
test_script = f"def {method_name}():\n {method_code}\n\nprint({method_name}())"
verify_result = sandbox.run_python_code(test_script)
if not verify_result.get("success"):
print(f" [BLOCKED] Wasm Sandbox rejected the code: {verify_result.get('error')}")
continue
print(f" [VERIFIED] Wasm Output: {verify_result.get('output').strip()}")
result = self.self_mod.add_method(self, method_name, method_code,
description=f"Generated in Sandbox Gen {i+1}")
if result.get("success"):
print(f" [SUCCESS] Evolved new capability: {method_name}")
print("\n✨ Sandbox Evolution Complete.")
self.self_mod.export_log("sandbox_evolution_log.json")
def cleanup(self):
"""Restore the original sandbox state or delete it."""
if os.path.exists(self.source_backup):
shutil.copy2(self.source_backup, self.target_file)
print("🧹 Sandbox cleanup completed.")
if __name__ == "__main__":
sandbox = EvolutionSandbox()
try:
sandbox.run_evolution_cycle(3)
finally:
# sandbox.cleanup()
pass