-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevolver.py
More file actions
84 lines (66 loc) · 2.55 KB
/
evolver.py
File metadata and controls
84 lines (66 loc) · 2.55 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
"""
Evolver — Moteur de mutation du génome.
Déclenché quand la fitness stagne N cycles consécutifs.
Claude génère une mutation, on archive l'ancien génome et on adopte le nouveau.
"""
import json
import llm
import genome as genome_module
def mutate(genome: dict, stagnation_history: list, objective: dict) -> dict:
"""
Génère une mutation du génome basée sur l'historique de stagnation.
Retourne le nouveau génome (déjà sauvegardé).
"""
system = genome["system_role"]
failure_summary = _summarize_failures(stagnation_history)
prompt = f"""You are Genesis (generation {genome['generation']}) and you are STUCK.
CURRENT OBJECTIVE: {objective['goal']}
STAGNATION HISTORY (last {len(stagnation_history)} cycles without progress):
{failure_summary}
CURRENT GENOME:
{json.dumps(genome, indent=2)}
You must EVOLVE. Generate a mutated genome that will break the stagnation.
You can:
- Change your strategies (remove failing ones, add new ones)
- Change your action_preferences
- Change your objective_style
- Change your fitness_criteria
- Change your system_role (be careful — small changes only)
Rules:
- Keep the JSON structure identical
- Do NOT change: generation, parent_generation, created_at, mutation_history
- At least 1 strategy must change
- Explain why each change will help
Respond with JSON containing the full mutated genome fields to UPDATE
(only include fields you want to change, except structural fields):
{{
"system_role": "...",
"strategies": [...],
"action_preferences": {{...}},
"objective_style": "...",
"fitness_criteria": [...],
"stagnation_threshold": <int>,
"mutation_reason": "why these changes will break the stagnation"
}}"""
mutation = llm.ask_json(prompt, system=system)
reason = mutation.pop("mutation_reason", "stagnation-driven mutation")
# Appliquer la mutation sur le génome courant
protected = {"generation", "parent_generation", "created_at", "mutation_history"}
for key, value in mutation.items():
if key not in protected:
genome[key] = value
# Commit avec versioning
new_genome = genome_module.commit_mutation(genome, reason)
return new_genome
def _summarize_failures(history: list) -> str:
if not history:
return "(no history)"
lines = []
for c in history:
lines.append(
f" Cycle {c.get('cycle_id', '?')}: "
f"action={c.get('action_type', '?')}, "
f"status={c.get('fitness_status', '?')}, "
f"reason={c.get('fitness_reason', '?')}"
)
return "\n".join(lines)