File size: 26,799 Bytes
caa3f8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11c3a3a
 
caa3f8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11c3a3a
 
caa3f8b
11c3a3a
caa3f8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11c3a3a
caa3f8b
11c3a3a
caa3f8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11c3a3a
caa3f8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11c3a3a
 
 
caa3f8b
 
 
 
11c3a3a
 
 
caa3f8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11c3a3a
 
caa3f8b
11c3a3a
 
 
 
 
 
 
 
 
 
 
 
caa3f8b
11c3a3a
caa3f8b
11c3a3a
caa3f8b
 
 
11c3a3a
 
 
caa3f8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11c3a3a
 
caa3f8b
11c3a3a
 
 
 
 
 
 
 
caa3f8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11c3a3a
caa3f8b
11c3a3a
caa3f8b
11c3a3a
 
 
 
 
 
 
 
 
 
 
 
 
caa3f8b
 
 
11c3a3a
 
 
 
 
 
 
 
caa3f8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11c3a3a
 
 
 
 
 
 
 
caa3f8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
"""
Self-Evolving Scenario Generation Engine

Implements:
1. Ξ±-Curriculum Reward (from GenEnv): R_env(pΜ‚) = -|pΜ‚ - Ξ±| where Ξ±β‰ˆ0.5
   Rewards environment for generating scenarios in agent's "zone of proximal development"
2. POET-inspired mutation operators: parametric mutation of scenario attributes
3. Fitness-proportionate selection with novelty bonus
4. Difficulty calibration via Elo-like rating system

Mathematical Framework:
- Each scenario S has a difficulty vector d ∈ R^k (k dimensions of difficulty)
- Agent competence vector c ∈ R^k estimated from performance history
- Ξ±-Curriculum: optimal scenario difficulty where P(agent solves | S) β‰ˆ Ξ±
- Mutation: S' = mutate(S, Οƒ) where Οƒ is mutation strength adapted by fitness
- Fitness: F(S) = -|success_rate(S) - Ξ±| + Ξ» * novelty(S)
- Novelty: measured as distance to k-nearest scenarios in behavior space
"""

import copy
import hashlib
import json
import math
import random
import time
import uuid
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional, Set, Tuple

# IMPORTANT: Use relative imports that work both ways
try:
    from ..tasks.base import (
        EndpointInfo, LogEntry, Scenario, ThreatIntelEntry, UserProfile
    )
    from ..models import ContainmentAction, Severity, ThreatCategory
except ImportError:
    import sys, os
    _parent = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    if _parent not in sys.path:
        sys.path.insert(0, _parent)
    from tasks.base import (
        EndpointInfo, LogEntry, Scenario, ThreatIntelEntry, UserProfile
    )
    from models import ContainmentAction, Severity, ThreatCategory


# ═══════════════════════════════════════════════════════════════════
# Data Structures
# ═══════════════════════════════════════════════════════════════════

@dataclass
class ScenarioGenome:
    """
    Genetic representation of a scenario for evolution.
    Maps scenario parameters to a mutable genome vector.
    """
    # Core parameters (these get mutated)
    num_log_entries: int = 12        # How many log entries (complexity)
    num_critical_evidence: int = 5   # Critical evidence items to find
    num_iocs: int = 4                # Number of IOCs
    num_endpoints: int = 3           # Network endpoints
    num_users: int = 2               # User profiles
    num_threat_intel: int = 3        # Threat intel entries
    num_containment_targets: int = 3 # Things to contain
    max_steps: int = 25              # Steps allowed
    noise_ratio: float = 0.3        # Ratio of noise/decoy evidence
    correlation_depth: int = 2       # How many cross-source correlations
    escalation_required: bool = True
    multi_stage_attack: bool = False # Whether attack has multiple phases

    # Difficulty dimensions (0.0 = easiest, 1.0 = hardest)
    evidence_obscurity: float = 0.3   # How hidden is critical evidence
    ioc_complexity: float = 0.3       # Sophistication of IOCs
    containment_complexity: float = 0.3 # Number/precision of containment
    report_detail_required: float = 0.3 # How detailed report must be
    time_pressure: float = 0.3        # Steps vs required actions ratio

    # Metadata
    generation: int = 0
    parent_id: Optional[str] = None
    genome_id: str = ""

    def __post_init__(self):
        if not self.genome_id:
            # Include random salt to prevent cross-session collisions (Bug E fix)
            salt = uuid.uuid4().hex[:8]
            self.genome_id = hashlib.md5(
                f"{salt}_{json.dumps(self.__dict__, default=str)}".encode()
            ).hexdigest()[:12]

    @property
    def difficulty_vector(self) -> List[float]:
        """k-dimensional difficulty vector."""
        return [
            self.evidence_obscurity,
            self.ioc_complexity,
            self.containment_complexity,
            self.report_detail_required,
            self.time_pressure,
        ]

    @property
    def aggregate_difficulty(self) -> float:
        """Single scalar difficulty score in [0, 1]."""
        return sum(self.difficulty_vector) / len(self.difficulty_vector)


@dataclass
class AgentPerformanceRecord:
    """Tracks agent performance for fitness evaluation."""
    scenario_id: str
    genome_id: str
    score: float
    steps_used: int
    max_steps: int
    evidence_found_ratio: float
    iocs_found_ratio: float
    correct_severity: bool
    correct_category: bool
    containment_score: float
    report_quality: float
    timestamp: float = 0.0


@dataclass
class EvolutionState:
    """Persistent state of the evolution engine."""
    generation: int = 0
    population: List[ScenarioGenome] = field(default_factory=list)
    archive: List[ScenarioGenome] = field(default_factory=list)  # Hall of fame
    performance_history: List[AgentPerformanceRecord] = field(default_factory=list)
    agent_elo: float = 1000.0  # Agent Elo rating
    scenario_elos: Dict[str, float] = field(default_factory=dict)  # Per-scenario Elo


# ═══════════════════════════════════════════════════════════════════
# Mutation Operators
# ═══════════════════════════════════════════════════════════════════

class MutationOperator:
    """
    POET-inspired parametric mutation operators for scenario genomes.

    Mutations are applied with adaptive strength Οƒ based on the
    Ξ±-curriculum signal: if scenarios are too easy, increase difficulty;
    if too hard, decrease.
    """

    # Bounds for genome parameters
    PARAM_BOUNDS = {
        'num_log_entries': (6, 30),
        'num_critical_evidence': (3, 12),
        'num_iocs': (2, 10),
        'num_endpoints': (2, 8),
        'num_users': (1, 5),
        'num_threat_intel': (2, 8),
        'num_containment_targets': (2, 8),
        'max_steps': (15, 40),
        'noise_ratio': (0.0, 0.6),
        'correlation_depth': (1, 5),
        'evidence_obscurity': (0.0, 1.0),
        'ioc_complexity': (0.0, 1.0),
        'containment_complexity': (0.0, 1.0),
        'report_detail_required': (0.0, 1.0),
        'time_pressure': (0.0, 1.0),
    }

    @staticmethod
    def mutate(genome: ScenarioGenome, sigma: float = 0.15) -> ScenarioGenome:
        """
        Apply Gaussian mutation to genome parameters.

        Οƒ (sigma) controls mutation strength:
        - Higher Οƒ β†’ more exploration (when agent is in comfort zone)
        - Lower Οƒ β†’ fine-tuning (when near optimal difficulty)
        """
        child = copy.deepcopy(genome)
        child.generation = genome.generation + 1
        child.parent_id = genome.genome_id

        # Mutate numeric parameters with Gaussian noise
        for param, (lo, hi) in MutationOperator.PARAM_BOUNDS.items():
            current = getattr(child, param)
            if isinstance(current, float):
                noise = random.gauss(0, sigma * (hi - lo))
                new_val = max(lo, min(hi, current + noise))
                setattr(child, param, round(new_val, 3))
            elif isinstance(current, int):
                noise = random.gauss(0, sigma * (hi - lo))
                new_val = max(lo, min(hi, round(current + noise)))
                setattr(child, param, int(new_val))

        # Flip boolean traits with small probability
        if random.random() < 0.15 * sigma:
            child.escalation_required = not child.escalation_required
        if random.random() < 0.15 * sigma:
            child.multi_stage_attack = not child.multi_stage_attack

        # Regenerate ID with uuid salt to prevent collisions (Bug Q fix)
        child.genome_id = hashlib.md5(
            f"{uuid.uuid4().hex[:8]}_{json.dumps(child.__dict__, default=str)}".encode()
        ).hexdigest()[:12]

        return child

    @staticmethod
    def crossover(parent_a: ScenarioGenome, parent_b: ScenarioGenome) -> ScenarioGenome:
        """Uniform crossover between two parent genomes."""
        child = copy.deepcopy(parent_a)
        child.generation = max(parent_a.generation, parent_b.generation) + 1
        child.parent_id = f"{parent_a.genome_id}x{parent_b.genome_id}"

        for param in MutationOperator.PARAM_BOUNDS:
            if random.random() < 0.5:
                setattr(child, param, getattr(parent_b, param))

        if random.random() < 0.5:
            child.escalation_required = parent_b.escalation_required
        if random.random() < 0.5:
            child.multi_stage_attack = parent_b.multi_stage_attack

        child.genome_id = hashlib.md5(
            f"{uuid.uuid4().hex[:8]}_{json.dumps(child.__dict__, default=str)}".encode()
        ).hexdigest()[:12]
        return child


# ═══════════════════════════════════════════════════════════════════
# Fitness & Selection
# ═══════════════════════════════════════════════════════════════════

class FitnessEvaluator:
    """
    Evaluates scenario fitness using Ξ±-Curriculum reward.

    Core formula: F(S) = -|pΜ‚(S) - Ξ±| + Ξ» * novelty(S) + Ξ² * info_gain(S)

    Where:
    - pΜ‚(S) = estimated agent success probability on scenario S
    - Ξ± = target success rate (0.5 = zone of proximal development)
    - novelty(S) = average distance to k-nearest neighbors in archive
    - info_gain(S) = how much new the scenario teaches the agent
    """

    def __init__(self, alpha: float = 0.5, lambda_novelty: float = 0.2,
                 beta_info: float = 0.1, k_nearest: int = 5):
        self.alpha = alpha          # Target success rate
        self.lambda_novelty = lambda_novelty  # Novelty weight
        self.beta_info = beta_info  # Information gain weight
        self.k_nearest = k_nearest  # For novelty computation

    def compute_fitness(
        self,
        genome: ScenarioGenome,
        performance_records: List[AgentPerformanceRecord],
        archive: List[ScenarioGenome],
    ) -> float:
        """
        Compute composite fitness score for a scenario genome.

        Returns value in approximately [-1, 1] range.
        Higher is better (more useful for training).
        """
        # 1. Ξ±-Curriculum component
        alpha_reward = self._alpha_curriculum_reward(genome, performance_records)

        # 2. Novelty component
        novelty = self._compute_novelty(genome, archive)

        # 3. Information gain estimate
        info_gain = self._estimate_info_gain(genome, performance_records)

        fitness = alpha_reward + self.lambda_novelty * novelty + self.beta_info * info_gain
        return fitness

    def _alpha_curriculum_reward(
        self,
        genome: ScenarioGenome,
        records: List[AgentPerformanceRecord],
    ) -> float:
        """
        Ξ±-Curriculum: R = -|pΜ‚ - Ξ±|

        Scenarios where the agent succeeds ~50% of the time are most useful
        for learning (zone of proximal development).
        """
        # Estimate success probability from performance records
        matching = [r for r in records if r.genome_id == genome.genome_id]
        if not matching:
            # No data β€” use difficulty as proxy
            # Assume harder scenarios have lower success probability
            estimated_p = 1.0 - genome.aggregate_difficulty
        else:
            estimated_p = sum(r.score for r in matching) / len(matching)

        return -abs(estimated_p - self.alpha)

    def _compute_novelty(
        self, genome: ScenarioGenome, archive: List[ScenarioGenome]
    ) -> float:
        """
        Novelty search: distance to k-nearest neighbors in difficulty space.
        Encourages diverse scenario population.
        """
        if not archive:
            return 1.0  # Maximum novelty if archive is empty

        gv = genome.difficulty_vector
        distances = []
        for other in archive:
            # Bug B fix: exclude self from distance computation
            if other.genome_id == genome.genome_id:
                continue
            ov = other.difficulty_vector
            dist = math.sqrt(sum((a - b) ** 2 for a, b in zip(gv, ov)))
            distances.append(dist)

        if not distances:
            return 1.0  # Only self in archive β†’ max novelty

        distances.sort()
        k = min(self.k_nearest, len(distances))
        avg_dist = sum(distances[:k]) / k if k > 0 else 0.0

        # Normalize to [0, 1] (max possible distance in unit hypercube is sqrt(k_dims))
        max_dist = math.sqrt(len(gv))
        return min(avg_dist / max_dist, 1.0)

    def _estimate_info_gain(
        self,
        genome: ScenarioGenome,
        records: List[AgentPerformanceRecord],
    ) -> float:
        """
        Estimate how much new information a scenario provides.
        Scenarios that expose agent weaknesses score higher.
        """
        if not records:
            return 0.5

        # Look at what the agent is weak at
        recent = records[-20:]  # Last 20 episodes

        weakness_dimensions = {
            'evidence_obscurity': 1.0 - (sum(r.evidence_found_ratio for r in recent) / len(recent)),
            'ioc_complexity': 1.0 - (sum(r.iocs_found_ratio for r in recent) / len(recent)),
            'containment_complexity': 1.0 - (sum(r.containment_score for r in recent) / len(recent)),
            'report_detail_required': 1.0 - (sum(r.report_quality for r in recent) / len(recent)),
        }

        # Scenarios that target agent weaknesses have higher info gain
        dv = genome.difficulty_vector
        dim_names = ['evidence_obscurity', 'ioc_complexity', 'containment_complexity',
                      'report_detail_required', 'time_pressure']

        info = 0.0
        for i, dim_name in enumerate(dim_names):
            if dim_name in weakness_dimensions:
                # Higher difficulty in weak dimensions = more info gain
                info += dv[i] * weakness_dimensions[dim_name]

        return info / len(dim_names) if dim_names else 0.0


# ═══════════════════════════════════════════════════════════════════
# Evolution Engine (Main Class)
# ═══════════════════════════════════════════════════════════════════

class EvolutionEngine:
    """
    Self-evolving environment engine using POET + Ξ±-Curriculum.

    Maintains a population of scenario genomes, evolves them based on
    agent performance, and provides the next scenario to train on.

    Usage:
        engine = EvolutionEngine(population_size=10, alpha=0.5)
        genome = engine.get_next_scenario()
        scenario = engine.genome_to_scenario(genome)
        # ... run agent on scenario ...
        engine.record_performance(genome, performance_record)
        engine.evolve()  # Create next generation
    """

    def __init__(
        self,
        population_size: int = 10,
        alpha: float = 0.5,
        mutation_sigma: float = 0.15,
        elite_fraction: float = 0.2,
        archive_size: int = 50,
    ):
        self.population_size = population_size
        self.mutation_sigma = mutation_sigma
        self.elite_fraction = elite_fraction
        self.archive_size = archive_size

        self.fitness_evaluator = FitnessEvaluator(alpha=alpha)
        self.state = EvolutionState()

        # Initialize population with diverse seeds
        self._initialize_population()

    def _initialize_population(self):
        """Create initial diverse population spanning difficulty space."""
        templates = [
            # Easy
            ScenarioGenome(num_log_entries=8, num_critical_evidence=3, num_iocs=2,
                          num_endpoints=2, max_steps=25, noise_ratio=0.1,
                          evidence_obscurity=0.1, ioc_complexity=0.1,
                          containment_complexity=0.1, time_pressure=0.1),
            # Medium
            ScenarioGenome(num_log_entries=12, num_critical_evidence=5, num_iocs=4,
                          num_endpoints=3, max_steps=25, noise_ratio=0.25,
                          evidence_obscurity=0.35, ioc_complexity=0.35,
                          containment_complexity=0.35, time_pressure=0.3),
            # Hard
            ScenarioGenome(num_log_entries=18, num_critical_evidence=7, num_iocs=6,
                          num_endpoints=4, max_steps=30, noise_ratio=0.4,
                          evidence_obscurity=0.6, ioc_complexity=0.6,
                          containment_complexity=0.6, time_pressure=0.5),
            # Expert
            ScenarioGenome(num_log_entries=25, num_critical_evidence=10, num_iocs=8,
                          num_endpoints=6, max_steps=35, noise_ratio=0.5,
                          multi_stage_attack=True,
                          evidence_obscurity=0.85, ioc_complexity=0.85,
                          containment_complexity=0.85, time_pressure=0.7),
        ]

        # Bug O fix: always include all 4 template seeds first, then fill with mutations
        # This ensures the archive can discover all difficulty levels even with small populations
        self.state.population = []

        # Seed with all templates first (guaranteed diversity)
        for i, template in enumerate(templates):
            genome = copy.deepcopy(template)
            genome.genome_id = hashlib.md5(
                f"init_{i}_{uuid.uuid4().hex[:8]}_{json.dumps(genome.__dict__, default=str)}".encode()
            ).hexdigest()[:12]
            self.state.population.append(genome)

        # Fill remaining slots with mutations of templates
        while len(self.state.population) < self.population_size:
            i = len(self.state.population)
            template = templates[i % len(templates)]
            genome = MutationOperator.mutate(template, sigma=0.3)
            genome.genome_id = hashlib.md5(
                f"init_{i}_{uuid.uuid4().hex[:8]}_{json.dumps(genome.__dict__, default=str)}".encode()
            ).hexdigest()[:12]
            self.state.population.append(genome)

        # If population_size < 4, trim back to requested size
        self.state.population = self.state.population[:max(self.population_size, len(templates))]

    def get_next_scenario_genome(self) -> ScenarioGenome:
        """
        Select the next scenario genome for the agent to train on.
        Uses fitness-proportionate selection favoring scenarios near Ξ±.
        """
        if not self.state.population:
            self._initialize_population()

        # Compute fitness for each genome
        fitnesses = []
        for genome in self.state.population:
            f = self.fitness_evaluator.compute_fitness(
                genome, self.state.performance_history, self.state.archive
            )
            fitnesses.append(f)

        # Softmax selection (temperature-based)
        temperature = 0.5
        max_f = max(fitnesses) if fitnesses else 0
        exp_f = [math.exp((f - max_f) / temperature) for f in fitnesses]
        total = sum(exp_f)
        probs = [e / total for e in exp_f]

        # Weighted random selection
        selected = random.choices(self.state.population, weights=probs, k=1)[0]
        return selected

    def record_performance(self, genome: ScenarioGenome, record: AgentPerformanceRecord):
        """Record agent performance on a scenario for fitness evaluation."""
        self.state.performance_history.append(record)

        # Update Elo ratings
        self._update_elo(genome, record)

        # Keep history bounded, but preserve records for elite genomes
        # so their fitness scores don't go stale (NEW-3 fix)
        if len(self.state.performance_history) > 500:
            # Identify elite genome IDs
            elite_ids = {g.genome_id for g in self.state.population[:max(1, len(self.state.population) // 5)]}
            elite_ids.update(g.genome_id for g in self.state.archive[-10:])
            # Keep recent + any records for elite genomes
            recent = self.state.performance_history[-300:]
            old = self.state.performance_history[:-300]
            elite_old = [r for r in old if r.genome_id in elite_ids]
            self.state.performance_history = elite_old + recent

    def evolve(self) -> List[ScenarioGenome]:
        """
        Evolve the scenario population using:
        1. Fitness evaluation
        2. Elite preservation
        3. Mutation + crossover
        4. Archive update (novelty-based hall of fame)

        Returns the new population.
        """
        self.state.generation += 1

        # Evaluate fitness
        scored = []
        for genome in self.state.population:
            f = self.fitness_evaluator.compute_fitness(
                genome, self.state.performance_history, self.state.archive
            )
            scored.append((genome, f))

        scored.sort(key=lambda x: x[1], reverse=True)

        # Elite preservation
        n_elite = max(1, int(self.population_size * self.elite_fraction))
        elites = [g for g, _ in scored[:n_elite]]

        # Add best to archive
        for genome, fitness in scored[:2]:
            new_entry = copy.deepcopy(genome)
            if len(self.state.archive) < self.archive_size:
                self.state.archive.append(new_entry)
            elif fitness > 0:  # Only archive reasonably fit scenarios
                # Replace least novel EXISTING member (excluding just-inserted)
                # NEW-2 fix: compute novelty on archive BEFORE inserting,
                # so the new genome can't evict itself.
                novelties = [
                    self.fitness_evaluator._compute_novelty(g, self.state.archive)
                    for g in self.state.archive
                ]
                min_idx = novelties.index(min(novelties))
                self.state.archive[min_idx] = new_entry

        # Adaptive mutation strength (NEW-4 fix: directional, not symmetric)
        # If scenarios are too easy (high avg score) β†’ increase Οƒ to explore harder
        # If scenarios are too hard (low avg score) β†’ DECREASE Οƒ to fine-tune easier
        recent_scores = [r.score for r in self.state.performance_history[-20:]]
        if recent_scores:
            avg_score = sum(recent_scores) / len(recent_scores)
            alpha = self.fitness_evaluator.alpha
            gap = avg_score - alpha  # positive = too easy, negative = too hard
            if gap > 0:
                # Too easy: increase sigma to explore harder scenarios
                sigma = self.mutation_sigma * (1.0 + gap)
            else:
                # Too hard: decrease sigma to fine-tune toward easier
                sigma = self.mutation_sigma * max(0.3, 1.0 + gap)  # floor at 0.3Γ—base
        else:
            sigma = self.mutation_sigma

        # Generate children
        new_population = list(elites)
        while len(new_population) < self.population_size:
            if random.random() < 0.7:
                # Mutation
                parent = random.choice(scored[:max(3, len(scored) // 2)])[0]
                child = MutationOperator.mutate(parent, sigma=sigma)
            else:
                # Crossover
                p1, p2 = random.sample(scored[:max(3, len(scored) // 2)], 2)
                child = MutationOperator.crossover(p1[0], p2[0])
                child = MutationOperator.mutate(child, sigma=sigma * 0.5)

            new_population.append(child)

        self.state.population = new_population[:self.population_size]
        return self.state.population

    def _update_elo(self, genome: ScenarioGenome, record: AgentPerformanceRecord):
        """
        Update Elo ratings for agent and scenario.

        Agent 'wins' if score > 0.7, 'loses' if score < 0.3, 'draw' otherwise.
        This gives a natural difficulty calibration system.
        """
        K = 32  # Elo K-factor

        agent_elo = self.state.agent_elo
        scenario_elo = self.state.scenario_elos.get(genome.genome_id, 1000.0)

        # Expected scores
        ea = 1.0 / (1.0 + 10 ** ((scenario_elo - agent_elo) / 400))
        es = 1.0 - ea

        # Actual outcome
        if record.score > 0.7:
            sa, ss = 1.0, 0.0  # Agent wins
        elif record.score < 0.3:
            sa, ss = 0.0, 1.0  # Scenario wins
        else:
            sa, ss = 0.5, 0.5  # Draw

        self.state.agent_elo = agent_elo + K * (sa - ea)
        self.state.scenario_elos[genome.genome_id] = scenario_elo + K * (ss - es)

        # Bug D fix: prune elos for genomes no longer in population or archive
        if len(self.state.scenario_elos) > self.archive_size + self.population_size + 20:
            active_ids = {g.genome_id for g in self.state.population}
            active_ids.update(g.genome_id for g in self.state.archive)
            stale_ids = [gid for gid in self.state.scenario_elos if gid not in active_ids]
            for gid in stale_ids:
                del self.state.scenario_elos[gid]

    def get_evolution_stats(self) -> Dict[str, Any]:
        """Get statistics about the current evolution state."""
        recent = self.state.performance_history[-20:]
        return {
            "generation": self.state.generation,
            "population_size": len(self.state.population),
            "archive_size": len(self.state.archive),
            "total_episodes": len(self.state.performance_history),
            "agent_elo": round(self.state.agent_elo, 1),
            "avg_recent_score": round(
                sum(r.score for r in recent) / len(recent), 4
            ) if recent else None,
            "avg_difficulty": round(
                sum(g.aggregate_difficulty for g in self.state.population)
                / len(self.state.population), 3
            ) if self.state.population else None,
            "difficulty_range": {
                "min": round(min(g.aggregate_difficulty for g in self.state.population), 3),
                "max": round(max(g.aggregate_difficulty for g in self.state.population), 3),
            } if self.state.population else None,
        }