-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevolving_opcode_swarm.py
More file actions
200 lines (147 loc) · 6.05 KB
/
evolving_opcode_swarm.py
File metadata and controls
200 lines (147 loc) · 6.05 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import time
import random
from execution_fitness import ExecutionFitness
from adversarial_tester import AdversarialTester
from self_patcher import SelfPatcher
from repair_memory import RepairMemory
class EvolvingOpcodeSwarm:
"""
Genetic evolution system for opcode implementations.
Pipeline:
generate → mutate → validate → score → select → repeat
"""
def __init__(self, swarm):
self.swarm = swarm
# (opcode, language) -> list of candidates
self.population = {}
# best survivors
self.elite = {}
self.patcher = SelfPatcher(self.swarm.llm_router)
# executor for fitness evaluation
self.executor = ExecutionFitness(timeout=2)
self.tester = AdversarialTester(self.swarm.llm_router) # Initialize the adversarial tester
self.repair_memory = RepairMemory()
# =====================================================
# FITNESS FUNCTION
# =====================================================
def fitness(self, code, language, executor, expected_output=None):
if language != "python":
# fallback for now (you can extend later per-language sandboxes)
return 0.0
result = executor.run_python(code)
return executor.score(result, expected_output)
# =====================================================
# INITIAL GENERATION
# =====================================================
def seed(self, opcode, language, n=3):
key = (opcode, language)
self.population[key] = []
for _ in range(n):
code = self.swarm.generate(opcode, language)
mutated = self.swarm.mutate(code, language)
validation = self.swarm.validate(mutated, language)
self.population[key].append({
"code": mutated,
"validation": validation,
"score": self.fitness(mutated, validation)
})
# =====================================================
# SELECTION (KEEP BEST)
# =====================================================
def select(self, opcode, language, top_k=2):
key = (opcode, language)
pop = self.population.get(key, [])
pop = sorted(pop, key=lambda x: x["score"], reverse=True)
survivors = pop[:top_k]
self.elite[key] = survivors[0] if survivors else None
return survivors
# =====================================================
# MUTATION STEP
# =====================================================
def evolve_step(self, opcode, language):
key = (opcode, language)
survivors = self.select(opcode, language)
new_population = []
code = self.swarm.generate(opcode, language)
code = self.swarm.mutate(code, language)
tests = self.tester.generate_tests(opcode, language, code)
results = self.executor.run_with_tests(code, tests)
# -----------------------------------------------------
# DETECT FAILURE
# -----------------------------------------------------
execution = self.executor.run_with_tests(code, tests)
score = self.executor.score(execution)
error_log = "\n".join(
r["stderr"] for r in execution if r["stderr"]
)
# -----------------------------------------------------
# STEP 1: CHECK REPAIR MEMORY FIRST
# -----------------------------------------------------
cached_fix = self.repair_memory.lookup(error_log, language)
if cached_fix:
repaired = cached_fix
else:
# STEP 2: LLM REPAIR
repaired = self.patcher.repair(
code,
language,
error_log
)
# -----------------------------------------------------
# RE-TEST REPAIR
# -----------------------------------------------------
new_execution = self.executor.run_with_tests(repaired, tests)
new_score = self.executor.score(new_execution)
# -----------------------------------------------------
# STORE NEW KNOWLEDGE
# -----------------------------------------------------
if new_score > score:
self.repair_memory.record(
error_log,
code,
repaired,
language
)
code = repaired
score = new_score
# keep elite
for s in survivors:
new_population.append(s)
# mutate elite
mutated = self.swarm.mutate(s["code"], language)
validation = self.swarm.validate(mutated, language)
new_population.append({
"code": mutated,
"validation": validation,
"score": self.fitness(mutated, validation)
})
# refill population if needed
while len(new_population) < 5:
code = self.swarm.generate(opcode, language)
mutated = self.swarm.mutate(code, language)
validation = self.swarm.validate(mutated, language)
new_population.append({
"code": mutated,
"validation": validation,
"score": self.fitness(
mutated,
language,
self.executor
)
})
self.population[key] = new_population
# =====================================================
# FULL EVOLUTION LOOP
# =====================================================
def run_generations(self, opcodes, languages, generations=3):
for gen in range(generations):
for opcode in opcodes:
for lang in languages:
key = (opcode, lang)
# seed if first run
if key not in self.population:
self.seed(opcode, lang)
# evolve population
self.evolve_step(opcode, lang)
print(f"Generation {gen} complete")
return self.elite