clawft-core 0.8.3

Core engine for clawft (agent loop, message bus, context management)
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
//! Genetic-algorithm population loop for GEPA prompt evolution (WEFT-38).
//!
//! Provides a deterministic, LLM-free generation cycle:
//! selection (tournament) → mutation / crossover → fitness → elitism.
//!
//! Triggered from [`crate::pipeline::learner::TrajectoryLearner::evolve_prompt`]
//! when `evolution_ready` is set (ADR-017 flywheel / production stage 3.5).

use std::path::Path;

use serde::{Deserialize, Serialize};

use super::{
    MutationStrategy, TrajectoryHint, auto_select_strategy, mutate_prompt,
};

/// Configuration for one GA evolution run.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GaConfig {
    /// Active population size (elites + offspring). Default 8.
    pub population_size: usize,
    /// Number of top candidates retained each generation. Default 2.
    pub elite_count: usize,
    /// Generations to run per `evolve` call. Default 1.
    pub generations: u32,
    /// Probability (0.0–1.0) of applying crossover instead of pure mutation.
    /// Implemented as a deterministic threshold on candidate id. Default 0.3.
    pub crossover_rate: f32,
    /// Minimum fitness delta vs seed required to accept an evolved prompt.
    /// Default 0.0 (any non-worse best is accepted; caller may still prefer
    /// the original when improvement is zero).
    pub min_improvement: f32,
}

impl Default for GaConfig {
    fn default() -> Self {
        Self {
            population_size: 8,
            elite_count: 2,
            generations: 1,
            crossover_rate: 0.3,
            min_improvement: 0.0,
        }
    }
}

/// A prompt candidate in the evolution population.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PromptCandidate {
    /// Unique identifier within a population (stable across generations).
    pub id: u64,
    /// Prompt text being evolved.
    pub content: String,
    /// Parent candidate id (`None` for the seed).
    pub parent_id: Option<u64>,
    /// Generation number (0 = seed).
    pub generation: u32,
    /// Aggregate fitness score in `[0.0, 1.0]`.
    pub fitness: f32,
    /// Strategy label that produced this candidate (`None` for seed).
    pub strategy: Option<String>,
}

/// Result of a GA evolution run.
#[derive(Debug, Clone)]
pub struct EvolutionResult {
    /// Seed / original candidate that opened the run.
    pub original: PromptCandidate,
    /// Best candidate after evolution (may equal original if no improvement).
    pub best: PromptCandidate,
    /// `best.fitness - original.fitness`.
    pub improvement: f32,
    /// Generations actually executed.
    pub generations: u32,
    /// Population size after the last generation.
    pub population_size: usize,
}

/// Active set of prompt candidates under evaluation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Population {
    candidates: Vec<PromptCandidate>,
    next_id: u64,
    generation: u32,
    config: GaConfig,
}

impl Population {
    /// Seed a population from a base system prompt.
    ///
    /// Fills the population with the seed plus deterministic mutants of it
    /// (one per [`MutationStrategy`], then cycling) so the first generation
    /// has diversity without requiring prior trajectory data.
    pub fn seed(base_prompt: &str, config: GaConfig) -> Self {
        let size = config.population_size.max(1);
        let mut next_id = 0u64;
        let seed = PromptCandidate {
            id: next_id,
            content: base_prompt.to_string(),
            parent_id: None,
            generation: 0,
            fitness: 0.5, // neutral until scored
            strategy: None,
        };
        next_id += 1;

        let strategies = [
            MutationStrategy::Rephrase,
            MutationStrategy::AddExamples,
            MutationStrategy::RemoveIneffective,
            MutationStrategy::Emphasize,
        ];
        let mut candidates = Vec::with_capacity(size);
        candidates.push(seed);

        let mut i = 0usize;
        while candidates.len() < size {
            let strategy = strategies[i % strategies.len()];
            let parent = &candidates[0];
            let content = mutate_prompt(&parent.content, &[], strategy);
            candidates.push(PromptCandidate {
                id: next_id,
                content,
                parent_id: Some(parent.id),
                generation: 0,
                fitness: 0.5,
                strategy: Some(format!("{strategy:?}")),
            });
            next_id += 1;
            i += 1;
        }

        Self {
            candidates,
            next_id,
            generation: 0,
            config,
        }
    }

    /// Current generation index.
    pub fn generation(&self) -> u32 {
        self.generation
    }

    /// Snapshot of all candidates (sorted by fitness descending).
    pub fn candidates(&self) -> Vec<PromptCandidate> {
        let mut c = self.candidates.clone();
        c.sort_by(|a, b| {
            b.fitness
                .partial_cmp(&a.fitness)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        c
    }

    /// Best candidate by fitness (stable: first on ties = seed if equal).
    pub fn best(&self) -> &PromptCandidate {
        self.candidates
            .iter()
            .max_by(|a, b| {
                a.fitness
                    .partial_cmp(&b.fitness)
                    .unwrap_or(std::cmp::Ordering::Equal)
                    .then_with(|| b.id.cmp(&a.id)) // prefer lower id (seed) on ties
            })
            .expect("population always has ≥1 candidate")
    }

    /// Re-score every candidate against the current trajectory hints.
    pub fn rescore(&mut self, trajectories: &[TrajectoryHint]) {
        for c in &mut self.candidates {
            c.fitness = score_candidate(&c.content, trajectories);
        }
    }

    /// Run `config.generations` of selection → mutate/crossover → elitism.
    ///
    /// Returns the evolution result relative to the best candidate **before**
    /// this call (the “original” for the run — usually the prior champion or
    /// the seed, not necessarily the raw assembler baseline). When
    /// improvement is below `min_improvement`, `best` is still the post-run
    /// champion (caller may reject it).
    pub fn evolve(&mut self, trajectories: &[TrajectoryHint]) -> EvolutionResult {
        self.rescore(trajectories);
        let original = self.best().clone();
        let gens = self.config.generations.max(1);

        for _ in 0..gens {
            self.evolve_one_generation(trajectories);
        }

        let best = self.best().clone();
        let improvement = best.fitness - original.fitness;
        EvolutionResult {
            original,
            best,
            improvement,
            generations: gens,
            population_size: self.candidates.len(),
        }
    }

    /// One-shot helper: seed from `prompt`, then [`Self::evolve`].
    ///
    /// `EvolutionResult::original` is the fitness-best member of the
    /// **seeded** population (may already include structure mutations from
    /// seed diversification), not always the raw `prompt` string.
    pub fn seed_and_evolve(
        prompt: &str,
        trajectories: &[TrajectoryHint],
        config: GaConfig,
    ) -> (Self, EvolutionResult) {
        let mut pop = Self::seed(prompt, config);
        let result = pop.evolve(trajectories);
        (pop, result)
    }

    fn evolve_one_generation(&mut self, trajectories: &[TrajectoryHint]) {
        self.rescore(trajectories);
        self.generation = self.generation.saturating_add(1);
        let elite_n = self.config.elite_count.min(self.candidates.len()).max(1);
        let size = self.config.population_size.max(elite_n);

        // Elitism: keep top elite_n
        let mut ranked = self.candidates.clone();
        ranked.sort_by(|a, b| {
            b.fitness
                .partial_cmp(&a.fitness)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        let mut next: Vec<PromptCandidate> = ranked.into_iter().take(elite_n).collect();

        // Fill with offspring from tournament selection
        let strategy_cycle = [
            MutationStrategy::Rephrase,
            MutationStrategy::AddExamples,
            MutationStrategy::RemoveIneffective,
            MutationStrategy::Emphasize,
        ];
        let mut offspring_i = 0usize;
        while next.len() < size {
            let parent_a = tournament_select(&self.candidates, offspring_i);
            let use_crossover = ((parent_a.id as f32 * 0.618_034) % 1.0)
                < self.config.crossover_rate
                && self.candidates.len() >= 2;

            let (content, strategy_label, parent_id) = if use_crossover {
                let parent_b = tournament_select(&self.candidates, offspring_i + 1);
                let merged = crossover(&parent_a.content, &parent_b.content);
                (
                    merged,
                    "Crossover".to_string(),
                    Some(parent_a.id),
                )
            } else {
                // Prefer trajectory-informed strategy, then cycle for diversity
                let preferred = auto_select_strategy(trajectories);
                let strategy = if offspring_i.is_multiple_of(2) {
                    preferred
                } else {
                    strategy_cycle[offspring_i % strategy_cycle.len()]
                };
                let mutated = mutate_prompt(&parent_a.content, trajectories, strategy);
                (mutated, format!("{strategy:?}"), Some(parent_a.id))
            };

            let fitness = score_candidate(&content, trajectories);
            next.push(PromptCandidate {
                id: self.next_id,
                content,
                parent_id,
                generation: self.generation,
                fitness,
                strategy: Some(strategy_label),
            });
            self.next_id = self.next_id.saturating_add(1);
            offspring_i += 1;
        }

        self.candidates = next;
    }

    /// Persist population as JSON to `path` (parent dirs must exist).
    pub fn save(&self, path: &Path) -> std::io::Result<()> {
        let json = serde_json::to_string_pretty(self).map_err(|e| {
            std::io::Error::new(std::io::ErrorKind::InvalidData, e)
        })?;
        std::fs::write(path, json)
    }

    /// Load a previously saved population.
    pub fn load(path: &Path) -> std::io::Result<Self> {
        let bytes = std::fs::read(path)?;
        serde_json::from_slice(&bytes).map_err(|e| {
            std::io::Error::new(std::io::ErrorKind::InvalidData, e)
        })
    }
}

/// Run a one-shot GA cycle over a fresh population seeded from `prompt`.
///
/// Production entry point for the ADR-017 flywheel: call when the learner
/// has set `evolution_ready`. Returns the evolved best prompt content when
/// improvement ≥ `config.min_improvement`; otherwise returns the original
/// prompt text.
pub fn evolve(
    prompt: &str,
    trajectories: &[TrajectoryHint],
    config: &GaConfig,
) -> EvolutionResult {
    let mut pop = Population::seed(prompt, config.clone());
    pop.evolve(trajectories)
}

/// Accept the evolved prompt only when fitness improves enough.
pub fn select_evolved_prompt(result: &EvolutionResult, min_improvement: f32) -> String {
    if result.improvement >= min_improvement && result.best.content != result.original.content {
        result.best.content.clone()
    } else if result.improvement >= min_improvement {
        // Same text but scored higher — still return best content
        result.best.content.clone()
    } else {
        result.original.content.clone()
    }
}

// ─── Internals ────────────────────────────────────────────────────────

/// Deterministic tournament of size 2: pick two candidates by rotating
/// index and keep the fitter one.
fn tournament_select(candidates: &[PromptCandidate], salt: usize) -> PromptCandidate {
    let n = candidates.len();
    debug_assert!(n > 0);
    let a = &candidates[salt % n];
    let b = &candidates[(salt.wrapping_mul(3).wrapping_add(1)) % n];
    if b.fitness > a.fitness {
        b.clone()
    } else {
        a.clone()
    }
}

/// Simple line-level crossover: first half of lines from `a`, second half
/// from `b`. Falls back to `a` when either side is empty.
fn crossover(a: &str, b: &str) -> String {
    let a_lines: Vec<&str> = a.lines().collect();
    let b_lines: Vec<&str> = b.lines().collect();
    if a_lines.is_empty() {
        return b.to_string();
    }
    if b_lines.is_empty() {
        return a.to_string();
    }
    let mid_a = a_lines.len().div_ceil(2);
    let mid_b = b_lines.len() / 2;
    let mut out = Vec::with_capacity(mid_a + (b_lines.len() - mid_b));
    out.extend_from_slice(&a_lines[..mid_a]);
    out.extend_from_slice(&b_lines[mid_b..]);
    // Deduplicate consecutive identical lines after the join.
    let mut deduped = Vec::new();
    for line in out {
        if deduped.last().copied() != Some(line) {
            deduped.push(line);
        }
    }
    let joined = deduped.join("\n");
    if joined.trim().is_empty() {
        a.to_string()
    } else {
        joined
    }
}

/// Heuristic fitness of a prompt given trajectory history.
///
/// Higher is better. Components (clamped to `[0, 1]`):
/// - Base 0.5
/// - Bonus for structure markers (`IMPORTANT:`, `Step N:`, Successful Examples)
/// - Bonus for embedding high-quality request snippets
/// - Penalty for embedding low-quality feedback phrases
fn score_candidate(content: &str, trajectories: &[TrajectoryHint]) -> f32 {
    let mut score: f32 = 0.5;
    let lower = content.to_lowercase();

    if lower.contains("important:") {
        score += 0.08;
    }
    if lower.contains("step ") {
        score += 0.06;
    }
    if lower.contains("successful examples") {
        score += 0.12;
    }

    for t in trajectories {
        if t.quality_score >= 0.8 {
            let snippet = t.request_content.chars().take(40).collect::<String>();
            if !snippet.is_empty() && content.contains(&snippet) {
                score += 0.05 * t.quality_score;
            }
        }
        if t.quality_score < 0.5 {
            for phrase in t
                .feedback
                .split(['.', ',', ';'])
                .map(str::trim)
                .filter(|s| s.len() > 5 && s.len() < 60)
            {
                if lower.contains(&phrase.to_lowercase()) {
                    score -= 0.08;
                }
            }
        }
    }

    // Mild preference for non-empty, non-huge prompts
    let len = content.len();
    if len == 0 {
        score = 0.0;
    } else if len > 8_000 {
        score -= 0.1;
    }

    score.clamp(0.0, 1.0)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn sample_hints() -> Vec<TrajectoryHint> {
        vec![
            TrajectoryHint {
                request_content: "Write a function to sort a list".into(),
                quality_score: 0.9,
                feedback: "Excellent quality.".into(),
            },
            TrajectoryHint {
                request_content: "Explain recursion with examples".into(),
                quality_score: 0.85,
                feedback: "Good explanation.".into(),
            },
            TrajectoryHint {
                request_content: "Fix the bug".into(),
                quality_score: 0.2,
                feedback: "Low relevance: response may not address the request.".into(),
            },
        ]
    }

    #[test]
    fn seed_fills_population() {
        let pop = Population::seed("Use the tools carefully\nAlways verify output", GaConfig {
            population_size: 6,
            ..Default::default()
        });
        assert_eq!(pop.candidates().len(), 6);
        assert_eq!(pop.best().generation, 0);
        assert!(pop.candidates().iter().any(|c| c.parent_id.is_none()));
    }

    #[test]
    fn evolve_runs_and_returns_result() {
        let prompt = "Use the read tool\nCheck results\nWrite the answer";
        let hints = sample_hints();
        let result = evolve(
            prompt,
            &hints,
            &GaConfig {
                population_size: 6,
                elite_count: 2,
                generations: 2,
                crossover_rate: 0.5,
                min_improvement: 0.0,
            },
        );
        assert_eq!(result.generations, 2);
        assert_eq!(result.population_size, 6);
        assert!(!result.best.content.trim().is_empty());
        // Original is the pre-generation champion (may already be a seed
        // mutant with higher heuristic fitness than the raw prompt).
        assert!(!result.original.content.trim().is_empty());
        assert!(result.best.fitness >= result.original.fitness - f32::EPSILON);
    }

    #[test]
    fn select_evolved_prompt_respects_min_improvement() {
        let original = PromptCandidate {
            id: 0,
            content: "orig".into(),
            parent_id: None,
            generation: 0,
            fitness: 0.5,
            strategy: None,
        };
        let best = PromptCandidate {
            id: 1,
            content: "better".into(),
            parent_id: Some(0),
            generation: 1,
            fitness: 0.52,
            strategy: Some("Rephrase".into()),
        };
        let result = EvolutionResult {
            original: original.clone(),
            best: best.clone(),
            improvement: 0.02,
            generations: 1,
            population_size: 4,
        };
        assert_eq!(select_evolved_prompt(&result, 0.05), "orig");
        assert_eq!(select_evolved_prompt(&result, 0.01), "better");
    }

    #[test]
    fn population_persist_roundtrip() {
        let dir = std::env::temp_dir().join(format!(
            "weft38-pop-{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("population.json");

        let mut pop = Population::seed(
            "Use tools\nAlways verify",
            GaConfig {
                population_size: 4,
                ..Default::default()
            },
        );
        let _ = pop.evolve(&sample_hints());
        pop.save(&path).unwrap();

        let loaded = Population::load(&path).unwrap();
        assert_eq!(loaded.candidates().len(), pop.candidates().len());
        assert_eq!(loaded.generation(), pop.generation());
        assert_eq!(loaded.best().content, pop.best().content);

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn crossover_merges_lines() {
        let a = "line a1\nline a2\nline a3";
        let b = "line b1\nline b2\nline b3";
        let m = crossover(a, b);
        assert!(m.contains("line a1"));
        assert!(m.contains("line b"));
        assert!(!m.trim().is_empty());
    }

    #[test]
    fn score_prefers_structured_prompts() {
        let hints = sample_hints();
        let plain = score_candidate("You are helpful.", &hints);
        let structured = score_candidate(
            "IMPORTANT: Use tools\nStep 1: Check output\n## Successful Examples\nExample 1: Write a function to sort a list",
            &hints,
        );
        assert!(structured > plain);
    }
}