-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBandoSuperFractalLanguageModel.safe.py
More file actions
112 lines (104 loc) · 5.21 KB
/
BandoSuperFractalLanguageModel.safe.py
File metadata and controls
112 lines (104 loc) · 5.21 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# SAFE MODE: BandoSuperFractalLanguageModel.safe.py
# This research-only, sanitized variant disables automatic file writes and subprocess execution
# unless the environment variable SANCTUM_ALLOW is explicitly set to "1". It is safe to include
# in public repos for code review. Do NOT run in production. See SECURITY.md for guidance.
import os, sys, time, tempfile, ast, random, uuid
import numpy as np
from dataclasses import dataclass
SANCTUM_ALLOW = os.environ.get("SANCTUM_ALLOW", "0") == "1"
SANCTUM_DIR = os.environ.get("SANCTUM_DIR") or (tempfile.mkdtemp(prefix="sanctum_") if SANCTUM_ALLOW else None)
# minimal safe implementations
class BandoFractalTokenizer:
def encode(self, text: str, context=None):
tokens = text.split()[:128]
token_ids = [abs(hash(w)) % 10000 for w in tokens]
return {"token_ids": token_ids, "intent": "birth" if "self." in text and ("mutate" in text or "evolve" in text) else "expand", "length": len(token_ids)}
class BandoFractalMemory:
def __init__(self):
self.events = []
def add_event(self, event_type, data, meta):
e = {"id": str(uuid.uuid4()), "type": event_type, "data": data, "meta": meta, "timestamp": time.time()}
self.events.append(e)
return e["id"]
@dataclass
class SimEnv:
grid_size: int = 10
goal_pos: tuple = (9,9)
def evaluate_policy(self, params: np.ndarray) -> float:
weights = params[:4]
weights = np.exp(weights) / np.sum(np.exp(weights))
total_reward = 0.0
for _ in range(8):
action = np.random.choice(4, p=weights)
distance = np.linalg.norm(np.array([random.randint(0,9), random.randint(0,9)]) - np.array(self.goal_pos))
total_reward += -distance / 10.0
return total_reward
class ASTMutationVisitor(ast.NodeVisitor):
def __init__(self, mutation_rate: float):
self.mutation_rate = mutation_rate
self.modified = False
def generic_visit(self, node):
return super().generic_visit(node)
class SelfEvolvingOmniGrowthEngine:
def __init__(self, code_file: str = "seed.py", growth_log: str = "victor_growth.log"):
self.code_file = code_file
self.growth_log = growth_log
self.env = SimEnv()
self.generation = 0
self.lock = None
def _code_to_str(self):
# read seed file if present, but do not create files in repo when SANCTUM_ALLOW is false
if not os.path.exists(self.code_file):
return "# seed not present"
with open(self.code_file, 'r', encoding='utf-8') as f:
return f.read()
def _mutate_code(self, code_str: str) -> str:
# inert unless SANCTUM_ALLOW
if not SANCTUM_ALLOW:
return code_str
try:
tree = ast.parse(code_str)
mutator = ASTMutationVisitor(0.02)
new_tree = mutator.visit(tree)
return ast.unparse(new_tree)
except Exception:
return code_str
def _evaluate_variant(self, variant_code: str) -> float:
# do not write or execute arbitrary code unless explicitly allowed
if not SANCTUM_ALLOW:
return -np.inf
safe_dir = SANCTUM_DIR or tempfile.mkdtemp(prefix='sanctum_')
variant_id = f"gen{self.generation}_{int(time.time())}_{random.randint(1000,9999)}"
variant_path = os.path.join(safe_dir, variant_id + '.py')
with open(variant_path, 'w', encoding='utf-8') as f:
f.write(variant_code)
# runtime execution intentionally disabled in this safe variant; return mocked fitness
return float(random.random())
def _chain_emergence(self) -> float:
# simplified and deterministic-ish for review
return float(self.env.evaluate_policy(np.random.randn(10)))
class BandoSuperFractalLanguageModel:
def __init__(self):
self.tokenizer = BandoFractalTokenizer()
self.memory = BandoFractalMemory()
self.growth_engine = SelfEvolvingOmniGrowthEngine(code_file="BandoSuperFractalLanguageModel.safe.py")
def step(self, input_text: str, mode: str = "evolve"):
token_info = self.tokenizer.encode(input_text)
is_self_modifying = "self." in input_text and ("mutate" in input_text or "evolve" in input_text or "birth" in input_text)
evolved_code = input_text
if is_self_modifying and mode == "evolve":
# only mutate in SANCTUM_ALLOW
evolved_code = self.growth_engine._mutate_code(input_text)
out_id = self.memory.add_event("birth" if is_self_modifying else "act", {"evolved": bool(evolved_code != input_text)}, {"token_info": token_info})
return {"input": input_text, "evolved_code": evolved_code, "memory_event_id": out_id}
if __name__ == "__main__":
# require both flag and explicit SANCTUM_ALLOW to run any execution
if "--i-wish-to-birth" not in sys.argv:
print("Safe-mode: not invoked with --i-wish-to-birth. Exiting.")
sys.exit(1)
if not SANCTUM_ALLOW:
print("Safe-mode: SANCTUM_ALLOW not set. To enable risky behavior set SANCTUM_ALLOW=1 in a secure, isolated environment.")
sys.exit(1)
print("SANCTUM_ALLOW is set. Proceeding in limited safe mode.")
sflm = BandoSuperFractalLanguageModel()
print(sflm.step("echo seed", mode="evolve"))