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
//! Tournament Selection — Parallel Hypothesis Evaluation
//!
//! Runs multiple mutation hypotheses concurrently in sandboxes,
//! scores them against the fitness function, and selects winners.

use super::fitness::SabResult;
use super::sandbox::{Sandbox, SandboxConfig, SandboxResult};
use super::{FitnessMetrics, FitnessWeights, GenerationRating};
use std::path::{Path, PathBuf};
use std::time::Duration;

/// Simple counting semaphore for thread-pool style concurrency
mod semaphore {
    use std::sync::{Condvar, Mutex};

    pub struct Semaphore {
        count: Mutex<usize>,
        condvar: Condvar,
    }

    impl Semaphore {
        pub fn new(count: usize) -> Self {
            Self {
                count: Mutex::new(count),
                condvar: Condvar::new(),
            }
        }

        pub fn acquire(&self) {
            let mut count = self.count.lock().unwrap();
            while *count == 0 {
                count = self.condvar.wait(count).unwrap();
            }
            *count -= 1;
        }

        pub fn release(&self) {
            let mut count = self.count.lock().unwrap();
            *count += 1;
            self.condvar.notify_one();
        }
    }
}

/// A mutation hypothesis proposed by the agent
#[derive(Debug, Clone)]
pub struct Hypothesis {
    /// Unique identifier
    pub id: String,
    /// Human-readable description of what the mutation does
    pub description: String,
    /// Unified diff (patch format)
    pub patch: String,
    /// Files affected by this mutation
    pub target_files: Vec<PathBuf>,
    /// Optional: property test that should pass after mutation
    pub property_test: Option<String>,
}

/// Result of evaluating a single hypothesis
#[derive(Debug)]
pub struct HypothesisResult {
    pub id: String,
    pub description: String,
    pub compiled: bool,
    pub sandbox_result: Option<SandboxResult>,
    pub sab_result: Option<SabResult>,
    pub fitness: Option<FitnessMetrics>,
    pub composite_score: f64,
    pub rating: GenerationRating,
    pub patch: String,
}

/// Tournament configuration
#[derive(Debug, Clone)]
pub struct TournamentConfig {
    /// Maximum concurrent sandboxes
    pub max_parallel: usize,
    /// Per-hypothesis timeout
    pub timeout: Duration,
    /// Fitness weights for scoring
    pub weights: FitnessWeights,
    /// Sandbox resource config
    pub sandbox: SandboxConfig,
}

impl Default for TournamentConfig {
    fn default() -> Self {
        Self {
            max_parallel: 4,
            timeout: Duration::from_secs(3600),
            weights: FitnessWeights::default(),
            sandbox: SandboxConfig::default(),
        }
    }
}

/// Run a tournament: evaluate all hypotheses in parallel sandboxes,
/// sort by fitness, return ranked results.
///
/// In the async version, this would use tokio::spawn + Semaphore.
/// For now, we use a thread pool approach.
pub fn run_tournament(
    hypotheses: Vec<Hypothesis>,
    config: &TournamentConfig,
    repo_root: &Path,
) -> Vec<HypothesisResult> {
    use std::sync::{Arc, Mutex};
    use std::thread;

    let results = Arc::new(Mutex::new(Vec::new()));
    let semaphore = Arc::new(semaphore::Semaphore::new(config.max_parallel));

    let handles: Vec<_> = hypotheses
        .into_iter()
        .map(|h| {
            let sem = semaphore.clone();
            let res = results.clone();
            let cfg = config.clone();
            let root = repo_root.to_path_buf();

            thread::spawn(move || {
                // Acquire semaphore slot
                sem.acquire();
                let result = evaluate_hypothesis(h, &cfg, &root);
                sem.release();
                res.lock().unwrap().push(result);
            })
        })
        .collect();

    // Wait for all evaluations
    for h in handles {
        let _ = h.join();
    }

    let mut results = Arc::try_unwrap(results)
        .unwrap_or_else(|_| panic!("Failed to unwrap results"))
        .into_inner()
        .unwrap();

    // Sort by composite score (highest first)
    results.sort_by(|a, b| {
        b.composite_score
            .partial_cmp(&a.composite_score)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    results
}

fn evaluate_hypothesis(
    hypothesis: Hypothesis,
    config: &TournamentConfig,
    repo_root: &Path,
) -> HypothesisResult {
    // 1. Create sandbox
    let sandbox = match Sandbox::create(&hypothesis.id, repo_root, config.sandbox.clone()) {
        Ok(s) => s,
        Err(_e) => {
            return HypothesisResult {
                id: hypothesis.id,
                description: hypothesis.description,
                compiled: false,
                sandbox_result: None,
                sab_result: None,
                fitness: None,
                composite_score: 0.0,
                rating: GenerationRating::Frost,
                patch: hypothesis.patch,
            };
        }
    };

    // 2. Apply patch
    if !sandbox.apply_patch(&hypothesis.patch).unwrap_or(false) {
        let _ = sandbox.destroy();
        return HypothesisResult {
            id: hypothesis.id,
            description: hypothesis.description,
            compiled: false,
            sandbox_result: None,
            sab_result: None,
            fitness: None,
            composite_score: 0.0,
            rating: GenerationRating::Frost,
            patch: hypothesis.patch,
        };
    }

    // 3. Evaluate (compile + test + bench)
    let sandbox_result = match sandbox.evaluate() {
        Ok(r) => r,
        Err(_) => {
            let _ = sandbox.destroy();
            return HypothesisResult {
                id: hypothesis.id,
                description: hypothesis.description,
                compiled: false,
                sandbox_result: None,
                sab_result: None,
                fitness: None,
                composite_score: 0.0,
                rating: GenerationRating::Frost,
                patch: hypothesis.patch,
            };
        }
    };

    let compiled = sandbox_result.compiled;
    let tests_passed = sandbox_result.tests_passed;
    let tests_total = sandbox_result.tests_total;

    // 4. Cleanup
    let _ = sandbox.destroy();

    // 5. Score
    let rating = if !compiled {
        GenerationRating::Frost
    } else if tests_passed == tests_total && tests_total > 0 {
        GenerationRating::Bloom
    } else if tests_passed as f64 / tests_total.max(1) as f64 > 0.95 {
        GenerationRating::Grow
    } else if tests_passed as f64 / tests_total.max(1) as f64 > 0.50 {
        GenerationRating::Wilt
    } else {
        GenerationRating::Frost
    };

    // Composite score (simplified — full version uses SAB)
    let composite = if compiled {
        (tests_passed as f64 / tests_total.max(1) as f64) * 100.0
    } else {
        0.0
    };

    HypothesisResult {
        id: hypothesis.id,
        description: hypothesis.description,
        compiled,
        sandbox_result: Some(sandbox_result),
        sab_result: None, // Full SAB runs separately for winners
        fitness: None,
        composite_score: composite,
        rating,
        patch: hypothesis.patch,
    }
}

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

    #[test]
    fn test_hypothesis_result_frost_on_compile_failure() {
        let result = HypothesisResult {
            id: "test".into(),
            description: "test".into(),
            compiled: false,
            sandbox_result: None,
            sab_result: None,
            fitness: None,
            composite_score: 0.0,
            rating: GenerationRating::Frost,
            patch: String::new(),
        };
        assert_eq!(result.rating, GenerationRating::Frost);
        assert_eq!(result.composite_score, 0.0);
    }

    #[test]
    fn test_tournament_config_default() {
        let cfg = TournamentConfig::default();
        assert_eq!(cfg.max_parallel, 4);
        assert_eq!(cfg.timeout, Duration::from_secs(3600));
    }

    #[test]
    fn test_semaphore_basic() {
        let sem = semaphore::Semaphore::new(2);
        sem.acquire();
        sem.acquire();
        sem.release();
        sem.acquire(); // Should not deadlock
        sem.release();
        sem.release();
    }

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

    #[test]
    fn test_semaphore_concurrent_threads() {
        use std::sync::{Arc, Mutex};
        use std::thread;

        let sem = Arc::new(semaphore::Semaphore::new(3));
        let counter = Arc::new(Mutex::new(0usize));
        let max_concurrent = Arc::new(Mutex::new(0usize));

        let handles: Vec<_> = (0..10)
            .map(|_| {
                let s = sem.clone();
                let c = counter.clone();
                let m = max_concurrent.clone();
                thread::spawn(move || {
                    s.acquire();
                    {
                        let mut count = c.lock().unwrap();
                        *count += 1;
                        let mut max = m.lock().unwrap();
                        if *count > *max {
                            *max = *count;
                        }
                    }
                    // Simulate work
                    thread::sleep(std::time::Duration::from_millis(10));
                    {
                        let mut count = c.lock().unwrap();
                        *count -= 1;
                    }
                    s.release();
                })
            })
            .collect();

        for h in handles {
            h.join().unwrap();
        }

        let max = *max_concurrent.lock().unwrap();
        assert!(max <= 3, "Max concurrent should be <= 3, got {}", max);
    }

    #[test]
    fn test_semaphore_single_slot() {
        use std::sync::{Arc, Mutex};
        use std::thread;

        let sem = Arc::new(semaphore::Semaphore::new(1));
        let counter = Arc::new(Mutex::new(0usize));
        let max_concurrent = Arc::new(Mutex::new(0usize));

        let handles: Vec<_> = (0..5)
            .map(|_| {
                let s = sem.clone();
                let c = counter.clone();
                let m = max_concurrent.clone();
                thread::spawn(move || {
                    s.acquire();
                    {
                        let mut count = c.lock().unwrap();
                        *count += 1;
                        let mut max = m.lock().unwrap();
                        if *count > *max {
                            *max = *count;
                        }
                    }
                    thread::sleep(std::time::Duration::from_millis(5));
                    {
                        let mut count = c.lock().unwrap();
                        *count -= 1;
                    }
                    s.release();
                })
            })
            .collect();

        for h in handles {
            h.join().unwrap();
        }

        let max = *max_concurrent.lock().unwrap();
        assert_eq!(max, 1, "Max concurrent should be exactly 1");
    }

    #[test]
    fn test_hypothesis_result_bloom_rating() {
        let result = HypothesisResult {
            id: "bloom-test".into(),
            description: "All tests pass".into(),
            compiled: true,
            sandbox_result: None,
            sab_result: None,
            fitness: None,
            composite_score: 100.0,
            rating: GenerationRating::Bloom,
            patch: "--- a/file\n+++ b/file".into(),
        };
        assert_eq!(result.rating, GenerationRating::Bloom);
        assert_eq!(result.composite_score, 100.0);
        assert!(result.compiled);
    }

    #[test]
    fn test_hypothesis_result_grow_rating() {
        let result = HypothesisResult {
            id: "grow-test".into(),
            description: "Most tests pass".into(),
            compiled: true,
            sandbox_result: None,
            sab_result: None,
            fitness: None,
            composite_score: 96.0,
            rating: GenerationRating::Grow,
            patch: String::new(),
        };
        assert_eq!(result.rating, GenerationRating::Grow);
        assert!(result.composite_score > 0.0);
    }

    #[test]
    fn test_tournament_config_clone() {
        let cfg = TournamentConfig {
            max_parallel: 8,
            timeout: Duration::from_secs(7200),
            weights: FitnessWeights::default(),
            sandbox: SandboxConfig::default(),
        };
        let cloned = cfg.clone();
        assert_eq!(cloned.max_parallel, 8);
        assert_eq!(cloned.timeout, Duration::from_secs(7200));
    }

    #[test]
    fn test_hypothesis_clone() {
        let h = Hypothesis {
            id: "h1".into(),
            description: "Test mutation".into(),
            patch: "diff content".into(),
            target_files: vec![PathBuf::from("src/lib.rs")],
            property_test: Some("test_prop".into()),
        };
        let cloned = h.clone();
        assert_eq!(cloned.id, "h1");
        assert_eq!(cloned.target_files.len(), 1);
        assert_eq!(cloned.property_test, Some("test_prop".into()));
    }
}