Skip to main content

vtcode_eval/
environment.rs

1//! Environment probes used to verify eval-task outcomes.
2//!
3//! The probe set is closed (external command, file existence, clean git tree),
4//! so it is modelled as an enum instead of `Box<dyn EnvironmentProbe>`. That
5//! removes a heap allocation and a vtable dispatch per probe while keeping the
6//! public API easy to extend with a new variant.
7
8use std::path::{Path, PathBuf};
9
10/// A single verification check run against an eval workspace.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum EnvironmentProbe {
13    /// Runs an external command in the workspace; passes on exit status 0.
14    Command { command: String, args: Vec<String> },
15    /// Passes when `path` exists; relative paths resolve against the workspace.
16    FileExists { path: PathBuf },
17    /// Passes when the workspace git tree has no uncommitted changes.
18    GitClean,
19}
20
21impl EnvironmentProbe {
22    /// Build a command probe from a command name and its arguments.
23    pub fn command(command: String, args: Vec<String>) -> Self {
24        Self::Command { command, args }
25    }
26
27    /// Build a file-existence probe.
28    pub fn file_exists(path: PathBuf) -> Self {
29        Self::FileExists { path }
30    }
31
32    /// Build a clean-git-tree probe.
33    pub fn git_clean() -> Self {
34        Self::GitClean
35    }
36
37    /// Run this probe against `workspace`, returning `true` on success.
38    ///
39    /// Probe failures are reported as `false` rather than errors: a missing
40    /// file or a failing command is a failed verification, not a broken runner.
41    pub fn check(&self, workspace: &Path) -> bool {
42        match self {
43            Self::Command { command, args } => std::process::Command::new(command)
44                .args(args)
45                .current_dir(workspace)
46                .output()
47                .is_ok_and(|output| output.status.success()),
48            // `join` keeps absolute paths unchanged and anchors relative ones
49            // to the eval workspace (each attempt may run in its own worktree).
50            Self::FileExists { path } => workspace.join(path).exists(),
51            Self::GitClean => std::process::Command::new("git")
52                .args(["diff", "--quiet"])
53                .current_dir(workspace)
54                .output()
55                .is_ok_and(|output| output.status.success()),
56        }
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn file_exists_probe_checks_both_sides() {
66        let workspace = std::env::current_dir().expect("current dir");
67        assert!(EnvironmentProbe::file_exists(workspace.clone()).check(&workspace));
68
69        let missing = workspace.join(format!("missing-probe-{}", std::process::id()));
70        assert!(!EnvironmentProbe::file_exists(missing).check(&workspace));
71    }
72
73    #[test]
74    fn file_exists_probe_resolves_relative_paths_against_workspace() {
75        let marker = format!("vtcode-probe-marker-{}", std::process::id());
76        let root = std::env::temp_dir().join(format!("vtcode-probe-root-{}", std::process::id()));
77        let workspace = root.join("worktree");
78        std::fs::create_dir_all(&workspace).expect("create probe workspace");
79        std::fs::write(workspace.join(&marker), b"present").expect("create probe marker");
80
81        // Relative path: only present under the workspace, never the process CWD.
82        assert!(EnvironmentProbe::file_exists(PathBuf::from(&marker)).check(&workspace));
83        // Absolute path: still accepted unchanged.
84        assert!(EnvironmentProbe::file_exists(workspace.join(&marker)).check(&workspace));
85        // Missing relative path: rejected on both sides of the boundary.
86        assert!(!EnvironmentProbe::file_exists(PathBuf::from(format!("{marker}-missing"))).check(&workspace));
87
88        std::fs::remove_dir_all(&root).expect("clean up probe workspace");
89    }
90
91    #[cfg(unix)]
92    #[test]
93    fn command_probe_uses_exit_status() {
94        let workspace = std::env::current_dir().expect("current dir");
95        assert!(EnvironmentProbe::command("true".into(), Vec::new()).check(&workspace));
96        assert!(!EnvironmentProbe::command("false".into(), Vec::new()).check(&workspace));
97    }
98}