aprender-verify-ml 0.31.2

Synthetic Data Factory for Domain-Specific Code Intelligence
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
//! Code execution backends for verification
//!
//! Provides executors for running code in different languages
//! with proper I/O capture and timeout handling.

use std::io::Write;
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};

use crate::{Error, Language, Result};

use super::ExecutionResult;

/// Code executor trait for running programs
pub trait Executor: Send + Sync {
    /// Execute code with the given input
    ///
    /// # Errors
    ///
    /// Returns an error if execution fails
    fn execute(&self, code: &str, input: &str, timeout_ms: u64) -> Result<ExecutionResult>;

    /// Get the language this executor handles
    fn language(&self) -> Language;
}

/// Python code executor using system Python interpreter
#[derive(Debug, Default)]
pub struct PythonExecutor {
    /// Path to Python interpreter (default: "python3")
    interpreter: String,
}

impl PythonExecutor {
    /// Create a new Python executor with default interpreter
    #[must_use]
    pub fn new() -> Self {
        Self {
            interpreter: "python3".to_string(),
        }
    }

    /// Create a Python executor with custom interpreter path
    #[must_use]
    pub fn with_interpreter(interpreter: impl Into<String>) -> Self {
        Self {
            interpreter: interpreter.into(),
        }
    }

    /// Check if Python is available
    #[must_use]
    pub fn is_available(&self) -> bool {
        Command::new(&self.interpreter)
            .arg("--version")
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .is_ok()
    }
}

impl Executor for PythonExecutor {
    fn execute(&self, code: &str, input: &str, timeout_ms: u64) -> Result<ExecutionResult> {
        use std::sync::atomic::{AtomicU64, Ordering};
        static COUNTER: AtomicU64 = AtomicU64::new(0);

        let start = Instant::now();

        // Create a temporary file for the code with unique name
        let temp_dir = std::env::temp_dir();
        let unique_id = COUNTER.fetch_add(1, Ordering::SeqCst);
        let temp_file = temp_dir.join(format!("verificar_{}_{}.py", std::process::id(), unique_id));

        std::fs::write(&temp_file, code)
            .map_err(|e| Error::Verification(format!("Failed to write temp file: {e}")))?;

        let mut cmd = Command::new(&self.interpreter);
        cmd.arg(&temp_file)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());

        // Spawn process
        let mut child = cmd
            .spawn()
            .map_err(|e| Error::Verification(format!("Failed to spawn Python: {e}")))?;

        // Write input to stdin
        if let Some(mut stdin) = child.stdin.take() {
            let _ = stdin.write_all(input.as_bytes());
        }

        // Wait with timeout
        let timeout = Duration::from_millis(timeout_ms);
        let output = match wait_with_timeout(child, timeout) {
            Ok(output) => output,
            Err(e) => {
                let _ = std::fs::remove_file(&temp_file);
                return Err(e);
            }
        };

        let _ = std::fs::remove_file(&temp_file);

        let duration_ms = start.elapsed().as_millis() as u64;

        Ok(ExecutionResult {
            stdout: String::from_utf8_lossy(&output.stdout).to_string(),
            stderr: String::from_utf8_lossy(&output.stderr).to_string(),
            exit_code: output.status.code().unwrap_or(-1),
            duration_ms,
        })
    }

    fn language(&self) -> Language {
        Language::Python
    }
}

/// Rust code executor using cargo/rustc
#[derive(Debug, Default)]
pub struct RustExecutor {
    /// Path to rustc (default: "rustc")
    compiler: String,
}

impl RustExecutor {
    /// Create a new Rust executor
    #[must_use]
    pub fn new() -> Self {
        Self {
            compiler: "rustc".to_string(),
        }
    }

    /// Check if rustc is available
    #[must_use]
    pub fn is_available(&self) -> bool {
        Command::new(&self.compiler)
            .arg("--version")
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .is_ok()
    }
}

impl Executor for RustExecutor {
    fn execute(&self, code: &str, input: &str, timeout_ms: u64) -> Result<ExecutionResult> {
        use std::sync::atomic::{AtomicU64, Ordering};
        static COUNTER: AtomicU64 = AtomicU64::new(0);

        let start = Instant::now();

        // Create temp directory for compilation with unique names
        let temp_dir = std::env::temp_dir();
        let unique_id = COUNTER.fetch_add(1, Ordering::SeqCst);
        let source_file =
            temp_dir.join(format!("verificar_{}_{}.rs", std::process::id(), unique_id));
        let binary_file = temp_dir.join(format!("verificar_{}_{}", std::process::id(), unique_id));

        std::fs::write(&source_file, code)
            .map_err(|e| Error::Verification(format!("Failed to write temp file: {e}")))?;

        let compile_output = Command::new(&self.compiler)
            .arg(&source_file)
            .arg("-o")
            .arg(&binary_file)
            .output()
            .map_err(|e| Error::Verification(format!("Failed to compile: {e}")))?;

        if !compile_output.status.success() {
            let _ = std::fs::remove_file(&source_file);
            return Ok(ExecutionResult {
                stdout: String::new(),
                stderr: String::from_utf8_lossy(&compile_output.stderr).to_string(),
                exit_code: compile_output.status.code().unwrap_or(-1),
                duration_ms: start.elapsed().as_millis() as u64,
            });
        }

        // Run binary
        let mut cmd = Command::new(&binary_file);
        cmd.stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());

        let mut child = cmd
            .spawn()
            .map_err(|e| Error::Verification(format!("Failed to run binary: {e}")))?;

        // Write input
        if let Some(mut stdin) = child.stdin.take() {
            let _ = stdin.write_all(input.as_bytes());
        }

        // Wait with timeout
        let timeout = Duration::from_millis(timeout_ms);
        let output = match wait_with_timeout(child, timeout) {
            Ok(output) => output,
            Err(e) => {
                let _ = std::fs::remove_file(&source_file);
                let _ = std::fs::remove_file(&binary_file);
                return Err(e);
            }
        };

        // Cleanup
        let _ = std::fs::remove_file(&source_file);
        let _ = std::fs::remove_file(&binary_file);

        Ok(ExecutionResult {
            stdout: String::from_utf8_lossy(&output.stdout).to_string(),
            stderr: String::from_utf8_lossy(&output.stderr).to_string(),
            exit_code: output.status.code().unwrap_or(-1),
            duration_ms: start.elapsed().as_millis() as u64,
        })
    }

    fn language(&self) -> Language {
        Language::Rust
    }
}

/// Wait for a process with timeout using threaded output capture
fn wait_with_timeout(
    mut child: std::process::Child,
    timeout: Duration,
) -> Result<std::process::Output> {
    use std::io::Read;
    use std::sync::mpsc;
    use std::thread;

    // Take stdout and stderr handles before spawning threads
    let stdout_handle = child.stdout.take();
    let stderr_handle = child.stderr.take();

    // Spawn threads to read stdout and stderr concurrently
    let stdout_thread = thread::spawn(move || {
        let mut buf = Vec::new();
        if let Some(mut stdout) = stdout_handle {
            let _ = stdout.read_to_end(&mut buf);
        }
        buf
    });

    let stderr_thread = thread::spawn(move || {
        let mut buf = Vec::new();
        if let Some(mut stderr) = stderr_handle {
            let _ = stderr.read_to_end(&mut buf);
        }
        buf
    });

    // Wait for process with timeout using a channel
    let (tx, rx) = mpsc::channel();
    let wait_thread = thread::spawn(move || {
        let result = child.wait();
        let _ = tx.send(result);
        child
    });

    // Wait with timeout
    match rx.recv_timeout(timeout) {
        Ok(Ok(status)) => {
            // Process finished, collect output
            // The wait_thread returns the child after wait() completes
            // We join the thread and let the child go out of scope (already waited)
            let _ = wait_thread.join();

            let stdout = stdout_thread.join().unwrap_or_default();
            let stderr = stderr_thread.join().unwrap_or_default();

            Ok(std::process::Output {
                status,
                stdout,
                stderr,
            })
        }
        Ok(Err(e)) => {
            let _ = wait_thread.join();
            let _ = stdout_thread.join();
            let _ = stderr_thread.join();
            Err(Error::Verification(format!("Wait error: {e}")))
        }
        Err(mpsc::RecvTimeoutError::Timeout) => {
            // Timeout - kill the process and wait to avoid zombie
            if let Ok(mut child) = wait_thread.join() {
                let _ = child.kill();
                let _ = child.wait(); // Reap the zombie process
            }
            // Still collect any output that was produced
            let _ = stdout_thread.join();
            let _ = stderr_thread.join();
            Err(Error::Verification("Execution timed out".to_string()))
        }
        Err(mpsc::RecvTimeoutError::Disconnected) => {
            let _ = wait_thread.join();
            let _ = stdout_thread.join();
            let _ = stderr_thread.join();
            Err(Error::Verification(
                "Process wait thread disconnected".to_string(),
            ))
        }
    }
}

/// Get an executor for the specified language
#[must_use]
pub fn executor_for(language: Language) -> Option<Box<dyn Executor>> {
    match language {
        Language::Python => Some(Box::new(PythonExecutor::new())),
        Language::Rust => Some(Box::new(RustExecutor::new())),
        _ => None,
    }
}

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

    #[test]
    fn test_python_executor_simple() {
        let executor = PythonExecutor::new();
        if !executor.is_available() {
            eprintln!("Python not available, skipping test");
            return;
        }

        let result = executor
            .execute("print('hello')", "", 5000)
            .expect("execution should succeed");

        assert_eq!(result.stdout.trim(), "hello");
        assert_eq!(result.exit_code, 0);
    }

    #[test]
    fn test_python_executor_with_input() {
        let executor = PythonExecutor::new();
        if !executor.is_available() {
            eprintln!("Python not available, skipping test");
            return;
        }

        let code = "x = input()\nprint(f'got: {x}')";
        let result = executor
            .execute(code, "test", 5000)
            .expect("execution should succeed");

        assert_eq!(result.stdout.trim(), "got: test");
    }

    #[test]
    fn test_python_executor_error() {
        let executor = PythonExecutor::new();
        if !executor.is_available() {
            eprintln!("Python not available, skipping test");
            return;
        }

        let result = executor
            .execute("raise ValueError('oops')", "", 5000)
            .expect("execution should succeed");

        assert_ne!(result.exit_code, 0);
        assert!(result.stderr.contains("ValueError"));
    }

    #[test]
    fn test_python_executor_arithmetic() {
        let executor = PythonExecutor::new();
        if !executor.is_available() {
            eprintln!("Python not available, skipping test");
            return;
        }

        let code = "print(1 + 2 * 3)";
        let result = executor
            .execute(code, "", 5000)
            .expect("execution should succeed");

        assert_eq!(result.stdout.trim(), "7");
    }

    #[test]
    fn test_executor_for_python() {
        let executor = executor_for(Language::Python);
        assert!(executor.is_some());
        assert_eq!(executor.unwrap().language(), Language::Python);
    }

    #[test]
    fn test_executor_for_rust() {
        let executor = executor_for(Language::Rust);
        assert!(executor.is_some());
        assert_eq!(executor.unwrap().language(), Language::Rust);
    }

    #[test]
    fn test_executor_for_unsupported() {
        let executor = executor_for(Language::Bash);
        assert!(executor.is_none());
    }

    #[test]
    fn test_python_executor_with_interpreter() {
        let executor = PythonExecutor::with_interpreter("python3");
        assert_eq!(executor.interpreter, "python3");
    }

    #[test]
    fn test_python_executor_default() {
        let executor = PythonExecutor::default();
        assert!(executor.interpreter.is_empty() || executor.interpreter == "python3");
    }

    #[test]
    fn test_python_executor_language() {
        let executor = PythonExecutor::new();
        assert_eq!(executor.language(), Language::Python);
    }

    #[test]
    fn test_python_executor_debug() {
        let executor = PythonExecutor::new();
        let debug = format!("{:?}", executor);
        assert!(debug.contains("PythonExecutor"));
    }

    #[test]
    fn test_rust_executor_new() {
        let executor = RustExecutor::new();
        assert_eq!(executor.compiler, "rustc");
    }

    #[test]
    fn test_rust_executor_default() {
        let executor = RustExecutor::default();
        assert!(executor.compiler.is_empty() || executor.compiler == "rustc");
    }

    #[test]
    fn test_rust_executor_language() {
        let executor = RustExecutor::new();
        assert_eq!(executor.language(), Language::Rust);
    }

    #[test]
    fn test_rust_executor_debug() {
        let executor = RustExecutor::new();
        let debug = format!("{:?}", executor);
        assert!(debug.contains("RustExecutor"));
    }

    #[test]
    fn test_rust_executor_is_available() {
        let executor = RustExecutor::new();
        // This may or may not be available depending on the system
        let _ = executor.is_available();
    }

    #[test]
    fn test_rust_executor_simple() {
        let executor = RustExecutor::new();
        if !executor.is_available() {
            eprintln!("rustc not available, skipping test");
            return;
        }

        let code = r#"fn main() { println!("hello from rust"); }"#;
        let result = executor
            .execute(code, "", 10000)
            .expect("execution should succeed");

        assert_eq!(result.stdout.trim(), "hello from rust");
        assert_eq!(result.exit_code, 0);
    }

    #[test]
    fn test_rust_executor_compile_error() {
        let executor = RustExecutor::new();
        if !executor.is_available() {
            eprintln!("rustc not available, skipping test");
            return;
        }

        let code = "fn main() { invalid syntax }";
        let result = executor
            .execute(code, "", 10000)
            .expect("execution should return compile error");

        assert_ne!(result.exit_code, 0);
        assert!(!result.stderr.is_empty());
    }

    #[test]
    fn test_rust_executor_with_input() {
        let executor = RustExecutor::new();
        if !executor.is_available() {
            eprintln!("rustc not available, skipping test");
            return;
        }

        let code = r#"
use std::io::{self, BufRead};
fn main() {
    let stdin = io::stdin();
    let line = stdin.lock().lines().next().unwrap().unwrap();
    println!("got: {}", line);
}
"#;
        let result = executor
            .execute(code, "test input", 10000)
            .expect("execution should succeed");

        assert!(result.stdout.contains("got: test input"));
    }

    #[test]
    fn test_python_executor_timeout() {
        let executor = PythonExecutor::new();
        if !executor.is_available() {
            eprintln!("Python not available, skipping test");
            return;
        }

        // Code that takes too long
        let code = "import time; time.sleep(10)";
        let result = executor.execute(code, "", 100); // 100ms timeout

        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_str = err.to_string();
        assert!(err_str.contains("timeout") || err_str.contains("timed out"));
    }

    #[test]
    fn test_python_executor_multiple_lines_output() {
        let executor = PythonExecutor::new();
        if !executor.is_available() {
            eprintln!("Python not available, skipping test");
            return;
        }

        let code = "for i in range(3): print(i)";
        let result = executor
            .execute(code, "", 5000)
            .expect("execution should succeed");

        assert!(result.stdout.contains("0"));
        assert!(result.stdout.contains("1"));
        assert!(result.stdout.contains("2"));
    }

    #[test]
    fn test_python_executor_syntax_error() {
        let executor = PythonExecutor::new();
        if !executor.is_available() {
            eprintln!("Python not available, skipping test");
            return;
        }

        let code = "def f(: pass"; // syntax error
        let result = executor
            .execute(code, "", 5000)
            .expect("execution should succeed");

        assert_ne!(result.exit_code, 0);
        assert!(result.stderr.contains("SyntaxError"));
    }
}