selfware 0.2.2

Your personal AI workshop — software you own, software that lasts
Documentation
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
//! Integration tests for the evolution module.
//!
//! These tests validate the evolution module's public API and component
//! interactions without invoking external tools (Docker, SAB runner, etc).

#![cfg(feature = "self-improvement")]

use selfware::evolution::daemon;
use selfware::evolution::fitness::{self, SabConfig, SabResult};
use selfware::evolution::sandbox::SandboxConfig;
use selfware::evolution::tournament::{Hypothesis, TournamentConfig};
use selfware::evolution::{
    is_protected, EvolutionConfig, FitnessWeights, GenerationRating, LlmConfig, MutationTargets,
    SafetyConfig, PROTECTED_PATHS,
};
use std::path::PathBuf;
use std::time::Duration;

fn test_config(generations: usize) -> EvolutionConfig {
    EvolutionConfig {
        generations,
        population_size: 4,
        parallel_eval: 2,
        checkpoint_interval: 5,
        fitness_weights: FitnessWeights::default(),
        mutation_targets: MutationTargets {
            config_keys: vec!["temperature".into(), "token_budget".into()],
            prompt_logic: vec![PathBuf::from("src/agent/prompt.rs")],
            tool_code: vec![PathBuf::from("src/tools/file_edit.rs")],
            cognitive: vec![],
        },
        safety: SafetyConfig::default(),
        llm: LlmConfig::default(),
    }
}

// ── Config & safety integration tests ──

#[test]
fn test_evolution_config_construction() {
    let config = test_config(10);
    assert_eq!(config.generations, 10);
    assert_eq!(config.population_size, 4);
    assert_eq!(config.parallel_eval, 2);
    assert_eq!(config.checkpoint_interval, 5);

    // Verify fitness weights sum to 1.0
    let w = &config.fitness_weights;
    let total = w.sab_score + w.token_efficiency + w.latency + w.test_coverage + w.binary_size;
    assert!((total - 1.0).abs() < f64::EPSILON);
}

#[test]
fn test_protected_paths_prevent_self_modification() {
    // All protected paths should be detected
    for protected in PROTECTED_PATHS {
        let test_path = format!("{}test_file.rs", protected);
        assert!(
            is_protected(std::path::Path::new(&test_path)),
            "Path '{}' should be protected",
            test_path
        );
    }

    // Mutation targets should NOT be protected
    let config = test_config(1);
    for target in &config.mutation_targets.prompt_logic {
        assert!(
            !is_protected(target),
            "Mutation target '{}' should not be protected",
            target.display()
        );
    }
    for target in &config.mutation_targets.tool_code {
        assert!(
            !is_protected(target),
            "Mutation target '{}' should not be protected",
            target.display()
        );
    }
}

#[test]
fn test_safety_config_defaults_are_conservative() {
    let safety = SafetyConfig::default();

    assert!(
        safety.min_test_count >= 1000,
        "Min test count should be >= 1000 to prevent test deletion"
    );
    assert!(
        safety.max_binary_size_mb > 0.0 && safety.max_binary_size_mb <= 200.0,
        "Binary size limit should be between 0 and 200 MB"
    );
    assert!(
        safety.rollback_on_any_test_failure,
        "Rollback on test failure should be true by default"
    );
    for path in PROTECTED_PATHS {
        assert!(
            safety.protected_files.contains(&path.to_string()),
            "Protected path '{}' missing from SafetyConfig",
            path
        );
    }
}

// ── Fitness pipeline integration tests ──

#[test]
fn test_fitness_pipeline_end_to_end() {
    // Construct a SabResult → build FitnessMetrics → compute composite → compute delta
    let sab = SabResult {
        aggregate_score: 82.0,
        scenario_scores: vec![],
        total_tokens_used: 200_000,
        wall_clock: Duration::from_secs(1200),
        rating: GenerationRating::Grow,
    };

    let metrics = fitness::build_fitness_metrics(
        &sab,
        500_000,
        3600.0,
        std::path::Path::new("/nonexistent/binary"), // 0.0 MB
        5100,
        5200,
        50.0,
    );

    let weights = FitnessWeights::default();
    let composite = weights.composite(&metrics);

    // Verify composite is in valid range
    assert!((0.0..=1.0).contains(&composite), "Composite: {}", composite);

    // Create a "better" candidate and verify delta is positive
    let better_sab = SabResult {
        aggregate_score: 92.0,
        total_tokens_used: 150_000,
        ..sab.clone()
    };
    let better_metrics = fitness::build_fitness_metrics(
        &better_sab,
        500_000,
        3600.0,
        std::path::Path::new("/nonexistent/binary"),
        5200,
        5200,
        50.0,
    );

    let delta = fitness::fitness_delta(&metrics, &better_metrics, &weights);
    assert!(
        delta > 0.0,
        "Better candidate should have positive delta: {}",
        delta
    );
}

#[test]
fn test_rating_lifecycle() {
    // Simulate a generation rating progression: Frost → Wilt → Grow → Bloom
    let scores = [20.0, 45.0, 70.0, 90.0];
    let expected = [
        GenerationRating::Frost,
        GenerationRating::Wilt,
        GenerationRating::Grow,
        GenerationRating::Bloom,
    ];

    for (score, expected_rating) in scores.iter().zip(expected.iter()) {
        let rating = match *score as u32 {
            85..=100 => GenerationRating::Bloom,
            60..=84 => GenerationRating::Grow,
            30..=59 => GenerationRating::Wilt,
            _ => GenerationRating::Frost,
        };
        assert_eq!(
            &rating, expected_rating,
            "Score {} should yield {:?}",
            score, expected_rating
        );
    }
}

// ── Tournament integration tests ──

#[test]
fn test_tournament_empty_hypotheses_returns_empty() {
    let config = TournamentConfig::default();
    let tmp = std::env::temp_dir();
    let results = selfware::evolution::tournament::run_tournament(vec![], &config, &tmp);
    assert!(results.is_empty());
}

#[test]
fn test_hypothesis_safety_filter() {
    // Simulate the safety filter from daemon.rs:
    // hypotheses touching protected files should be rejected
    let hypotheses = vec![
        Hypothesis {
            id: "h1".into(),
            description: "Good mutation".into(),
            patch: String::new(),
            target_files: vec![PathBuf::from("src/agent/prompt.rs")],
            property_test: None,
        },
        Hypothesis {
            id: "h2".into(),
            description: "Bad mutation - touches evolution".into(),
            patch: String::new(),
            target_files: vec![PathBuf::from("src/evolution/fitness.rs")],
            property_test: None,
        },
        Hypothesis {
            id: "h3".into(),
            description: "Bad mutation - touches safety".into(),
            patch: String::new(),
            target_files: vec![PathBuf::from("src/safety/sandbox.rs")],
            property_test: None,
        },
        Hypothesis {
            id: "h4".into(),
            description: "Bad mutation - touches system tests".into(),
            patch: String::new(),
            target_files: vec![PathBuf::from("system_tests/projecte2e/easy_calc/test.sh")],
            property_test: None,
        },
    ];

    let valid: Vec<_> = hypotheses
        .into_iter()
        .filter(|h| !h.target_files.iter().any(|f| is_protected(f)))
        .collect();

    assert_eq!(valid.len(), 1, "Only h1 should pass safety filter");
    assert_eq!(valid[0].id, "h1");
}

// ── Config defaults integration ──

#[test]
fn test_all_configs_have_sane_defaults() {
    let evo_config = test_config(0); // 0 = infinite generations
    assert_eq!(evo_config.generations, 0);

    let sab_config = SabConfig::default();
    assert_eq!(sab_config.max_parallel, 6);
    assert_eq!(sab_config.scenario_timeout, Duration::from_secs(3600));

    let sandbox_config = SandboxConfig::default();
    assert!(
        !sandbox_config.network,
        "Network should be disabled by default"
    );
    assert_eq!(sandbox_config.timeout, Duration::from_secs(3600));

    let tournament_config = TournamentConfig::default();
    assert_eq!(tournament_config.max_parallel, 4);

    // Fitness weights should all be non-negative
    let w = FitnessWeights::default();
    assert!(w.sab_score >= 0.0);
    assert!(w.token_efficiency >= 0.0);
    assert!(w.latency >= 0.0);
    assert!(w.test_coverage >= 0.0);
    assert!(w.binary_size >= 0.0);
}

// ══════════════════════════════════════════════════════════════
// End-to-end pipeline integration tests
// ══════════════════════════════════════════════════════════════

/// Helper: create a temp git repo with Rust source files for testing
fn setup_test_repo(name: &str) -> PathBuf {
    let tmp = std::env::temp_dir().join(format!("selfware-e2e-{}", name));
    let _ = std::fs::remove_dir_all(&tmp);
    std::fs::create_dir_all(tmp.join("src")).unwrap();

    // Init git repo
    std::process::Command::new("git")
        .args(["init"])
        .current_dir(&tmp)
        .output()
        .unwrap();
    std::process::Command::new("git")
        .args(["config", "user.email", "test@test.com"])
        .current_dir(&tmp)
        .output()
        .unwrap();
    std::process::Command::new("git")
        .args(["config", "user.name", "Test"])
        .current_dir(&tmp)
        .output()
        .unwrap();

    // Create source files
    std::fs::write(
        tmp.join("src/small.rs"),
        "pub fn add(a: i32, b: i32) -> i32 {\n    a + b\n}\n\npub fn sub(a: i32, b: i32) -> i32 {\n    a - b\n}\n",
    )
    .unwrap();

    std::fs::write(
        tmp.join("src/medium.rs"),
        format!(
            "pub fn process(data: &[u8]) -> Vec<u8> {{\n    let mut result = Vec::new();\n    for &byte in data {{\n        result.push(byte.wrapping_add(1));\n    }}\n    result\n}}\n\n{}\n",
            (1..50).map(|i| format!("// padding line {}\n", i)).collect::<String>()
        ),
    )
    .unwrap();

    // Commit
    std::process::Command::new("git")
        .args(["add", "."])
        .current_dir(&tmp)
        .output()
        .unwrap();
    std::process::Command::new("git")
        .args(["commit", "-m", "init"])
        .current_dir(&tmp)
        .output()
        .unwrap();

    tmp
}

fn cleanup_test_repo(path: &PathBuf) {
    let _ = std::fs::remove_dir_all(path);
}

// ── E2E: read_mutation_targets → build_prompt → parse_response → apply_edits ──

#[test]
fn test_e2e_full_pipeline_with_mock_llm_response() {
    let repo = setup_test_repo("full-pipeline");

    // Step 1: Read mutation targets
    let targets = MutationTargets {
        prompt_logic: vec![PathBuf::from("src/small.rs")],
        tool_code: vec![PathBuf::from("src/medium.rs")],
        cognitive: vec![],
        config_keys: vec![],
    };

    let context = daemon::read_mutation_targets(&targets, &repo);
    assert!(!context.is_empty());
    assert!(context.contains("src/small.rs"));
    assert!(context.contains("src/medium.rs"));
    assert!(context.contains("1| pub fn add"));

    // Step 2: Build prompts
    let system_prompt = daemon::build_system_prompt(2);
    assert!(system_prompt.contains("exactly 2"));
    assert!(system_prompt.contains("search"));

    let user_prompt = daemon::build_user_prompt("", "", &context);
    assert!(user_prompt.contains("## Source Code"));
    assert!(user_prompt.contains("pub fn add"));

    // Step 3: Simulate LLM response (mock — no actual HTTP call)
    let mock_response = r#"[
        {
            "description": "Optimize add function to use wrapping_add for safety",
            "edits": [
                {
                    "file": "src/small.rs",
                    "search": "    a + b",
                    "replace": "    a.wrapping_add(b)"
                }
            ],
            "target_files": ["src/small.rs"],
            "property_test": null
        },
        {
            "description": "Use with_capacity in process function",
            "edits": [
                {
                    "file": "src/medium.rs",
                    "search": "    let mut result = Vec::new();",
                    "replace": "    let mut result = Vec::with_capacity(data.len());"
                }
            ],
            "target_files": ["src/medium.rs"],
            "property_test": null
        }
    ]"#;

    let hypotheses = daemon::parse_hypotheses_response(mock_response);
    assert_eq!(hypotheses.len(), 2);
    assert_eq!(
        hypotheses[0].target_files,
        vec![PathBuf::from("src/small.rs")]
    );
    assert_eq!(
        hypotheses[1].target_files,
        vec![PathBuf::from("src/medium.rs")]
    );

    // Step 4: Apply edits to the repo
    let applied = daemon::apply_edits(&repo, &hypotheses[0].patch);
    assert!(applied, "Edit should apply successfully");

    // Verify the file was actually changed
    let content = std::fs::read_to_string(repo.join("src/small.rs")).unwrap();
    assert!(
        content.contains("wrapping_add"),
        "File should contain the replacement: {}",
        content
    );
    assert!(
        !content.contains("a + b"),
        "File should not contain old code: {}",
        content
    );

    // Step 5: Apply second edit
    let applied2 = daemon::apply_edits(&repo, &hypotheses[1].patch);
    assert!(applied2, "Second edit should apply successfully");

    let content2 = std::fs::read_to_string(repo.join("src/medium.rs")).unwrap();
    assert!(content2.contains("Vec::with_capacity(data.len())"));

    cleanup_test_repo(&repo);
}

#[test]
fn test_e2e_pipeline_with_fuzzy_whitespace_matching() {
    let repo = setup_test_repo("fuzzy-ws");

    // File with 8-space indentation
    std::fs::write(
        repo.join("src/small.rs"),
        "fn outer() {\n        fn inner() {\n                old_call();\n        }\n}\n",
    )
    .unwrap();
    std::process::Command::new("git")
        .args(["add", "."])
        .current_dir(&repo)
        .output()
        .unwrap();
    std::process::Command::new("git")
        .args(["commit", "-m", "deep indent"])
        .current_dir(&repo)
        .output()
        .unwrap();

    // LLM returns search with 4-space indentation (common LLM mistake)
    let mock_response = r#"[{
        "description": "Replace old_call with new_call",
        "edits": [{
            "file": "src/small.rs",
            "search": "    fn inner() {\n            old_call();\n    }",
            "replace": "    fn inner() {\n            new_call();\n    }"
        }],
        "target_files": ["src/small.rs"],
        "property_test": null
    }]"#;

    let hypotheses = daemon::parse_hypotheses_response(mock_response);
    assert_eq!(hypotheses.len(), 1);

    // Fuzzy matching should handle the indentation mismatch
    let applied = daemon::apply_edits(&repo, &hypotheses[0].patch);
    assert!(applied, "Fuzzy whitespace matching should apply the edit");

    let content = std::fs::read_to_string(repo.join("src/small.rs")).unwrap();
    assert!(
        content.contains("new_call()"),
        "Should contain the replacement: {}",
        content
    );

    cleanup_test_repo(&repo);
}

#[test]
fn test_e2e_pipeline_safety_filter_blocks_protected_files() {
    // Simulate the safety filter that runs in the evolve() loop
    let mock_response = r#"[
        {
            "description": "Good mutation",
            "edits": [{"file": "src/tools/file.rs", "search": "x", "replace": "y"}],
            "target_files": ["src/tools/file.rs"],
            "property_test": null
        },
        {
            "description": "Bad mutation - touches evolution",
            "edits": [{"file": "src/evolution/daemon.rs", "search": "x", "replace": "y"}],
            "target_files": ["src/evolution/daemon.rs"],
            "property_test": null
        },
        {
            "description": "Bad mutation - touches safety",
            "edits": [{"file": "src/safety/sandbox.rs", "search": "x", "replace": "y"}],
            "target_files": ["src/safety/sandbox.rs"],
            "property_test": null
        }
    ]"#;

    let hypotheses = daemon::parse_hypotheses_response(mock_response);
    assert_eq!(hypotheses.len(), 3);

    // Apply safety filter (same logic as evolve())
    let valid: Vec<_> = hypotheses
        .into_iter()
        .filter(|h| !h.target_files.iter().any(|f| is_protected(f)))
        .collect();

    assert_eq!(valid.len(), 1);
    assert_eq!(valid[0].description, "Good mutation");
}

#[test]
fn test_e2e_pipeline_multifile_edit() {
    let repo = setup_test_repo("multifile");

    // Mock LLM response that edits multiple files in one hypothesis
    let mock_response = r#"[{
        "description": "Rename add to sum across files",
        "edits": [
            {"file": "src/small.rs", "search": "pub fn add(a: i32, b: i32) -> i32 {", "replace": "pub fn sum(a: i32, b: i32) -> i32 {"},
            {"file": "src/small.rs", "search": "pub fn sub(a: i32, b: i32) -> i32 {", "replace": "pub fn difference(a: i32, b: i32) -> i32 {"}
        ],
        "target_files": ["src/small.rs"],
        "property_test": null
    }]"#;

    let hypotheses = daemon::parse_hypotheses_response(mock_response);
    assert_eq!(hypotheses.len(), 1);

    let applied = daemon::apply_edits(&repo, &hypotheses[0].patch);
    assert!(applied, "Multi-edit should apply");

    let content = std::fs::read_to_string(repo.join("src/small.rs")).unwrap();
    assert!(content.contains("pub fn sum("));
    assert!(content.contains("pub fn difference("));
    assert!(!content.contains("pub fn add("));
    assert!(!content.contains("pub fn sub("));

    cleanup_test_repo(&repo);
}

#[test]
fn test_e2e_evolve_with_unreachable_endpoint() {
    // Run evolve() with an unreachable endpoint — should complete gracefully
    // with 0 improvements (LLM call fails, but no panic)
    let repo = setup_test_repo("evolve-nollm");

    let config = EvolutionConfig {
        generations: 1,
        population_size: 2,
        parallel_eval: 1,
        checkpoint_interval: 5,
        fitness_weights: FitnessWeights::default(),
        mutation_targets: MutationTargets {
            prompt_logic: vec![PathBuf::from("src/small.rs")],
            tool_code: vec![],
            cognitive: vec![],
            config_keys: vec![],
        },
        safety: SafetyConfig::default(),
        llm: LlmConfig {
            endpoint: "http://127.0.0.1:1".to_string(), // unreachable
            model: "test-model".to_string(),
            api_key: None,
            max_tokens: 1024,
            temperature: 0.0,
        },
    };

    let result = daemon::evolve(config, &repo);
    // Should complete without panicking
    assert_eq!(result.improvements.len(), 0);
    assert_eq!(result.initial_sab_score, 50.0); // synthetic baseline
    assert_eq!(result.final_sab_score, 50.0); // no improvement

    // Verify JSONL log was created
    let log_path = repo.join(".evolution-log.jsonl");
    assert!(log_path.exists(), "Should create evolution log");
    let log_content = std::fs::read_to_string(&log_path).unwrap();
    assert!(log_content.contains("\"event\":\"start\""));
    assert!(log_content.contains("\"event\":\"generation_start\""));

    cleanup_test_repo(&repo);
}

#[test]
fn test_e2e_evolution_history_prompt_builds_correctly() {
    let winners = vec![
        daemon::GenerationWinner {
            generation: 1,
            description: "Optimized token counting".into(),
            composite_score: 0.85,
            sab_delta: 3.0,
            token_delta: -5000.0,
            patch: String::new(),
            git_tag: None,
        },
        daemon::GenerationWinner {
            generation: 2,
            description: "Reduced allocations".into(),
            composite_score: 0.90,
            sab_delta: 5.0,
            token_delta: -3000.0,
            patch: String::new(),
            git_tag: Some("evolve-gen-2".into()),
        },
    ];

    let history = daemon::format_evolution_history(&winners);
    assert!(history.contains("Gen 2")); // Most recent first
    assert!(history.contains("Gen 1"));
    assert!(history.contains("Reduced allocations"));
    assert!(history.contains("Optimized token counting"));

    // Build a full user prompt with history
    let prompt = daemon::build_user_prompt("cpu: 50%", &history, "fn main() {}");
    assert!(prompt.contains("## Current Telemetry"));
    assert!(prompt.contains("cpu: 50%"));
    assert!(prompt.contains("Evolution History"));
    assert!(prompt.contains("## Source Code"));
}

#[test]
fn test_e2e_unified_diff_fallback() {
    let repo = setup_test_repo("diff-fallback");

    // Response with legacy unified diff format (not search-and-replace)
    let mock_response = r#"[{
        "description": "Legacy diff format test",
        "patch": "--- a/src/small.rs\n+++ b/src/small.rs\n@@ -1,3 +1,3 @@\n-pub fn add(a: i32, b: i32) -> i32 {\n+pub fn add_numbers(a: i32, b: i32) -> i32 {\n     a + b\n }\n",
        "target_files": ["src/small.rs"],
        "property_test": null
    }]"#;

    let hypotheses = daemon::parse_hypotheses_response(mock_response);
    assert_eq!(hypotheses.len(), 1);

    // The patch field is a unified diff string — apply_edits should dispatch to apply_unified_diff
    let applied = daemon::apply_edits(&repo, &hypotheses[0].patch);
    assert!(applied, "Unified diff fallback should work");

    let content = std::fs::read_to_string(repo.join("src/small.rs")).unwrap();
    assert!(content.contains("pub fn add_numbers("));

    cleanup_test_repo(&repo);
}