Skip to main content

ito_core/ralph/
validation.rs

1//! Completion validation for the Ralph loop.
2//!
3//! These helpers are invoked when a completion promise is detected. They verify:
4//! - Ito task status (all tasks complete or shelved)
5//! - Project validation commands (build/tests/lints)
6//! - Optional extra validation command provided via CLI
7
8use crate::error_bridge::IntoCoreResult;
9use crate::errors::{CoreError, CoreResult};
10use serde_json::Value;
11use std::fs;
12use std::path::{Path, PathBuf};
13use std::time::Duration;
14
15use ito_domain::tasks::{DiagnosticLevel, TaskRepository as DomainTaskRepository};
16
17use crate::process::{ProcessRequest, ProcessRunner, SystemProcessRunner};
18
19/// Result of one validation step.
20#[derive(Debug, Clone)]
21pub struct ValidationResult {
22    /// Whether the step succeeded.
23    pub success: bool,
24    /// Human-readable message summary.
25    pub message: String,
26    /// Optional verbose output (stdout/stderr, details).
27    pub output: Option<String>,
28}
29
30/// Which validation step produced a [`ValidationResult`].
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub enum ValidationStep {
33    /// Ito task status validation.
34    TaskStatus,
35    /// Project validation commands.
36    ProjectCheck,
37    /// Extra command provided via `--validation-command`.
38    ExtraCommand,
39}
40
41/// Check that all tasks for `change_id` are complete or shelved.
42///
43/// Missing tasks file is treated as success.
44pub fn check_task_completion(
45    task_repo: &impl DomainTaskRepository,
46    change_id: &str,
47) -> CoreResult<ValidationResult> {
48    let repo = task_repo;
49    let parsed = repo.load_tasks(change_id).into_core()?;
50
51    if parsed.progress.total == 0 {
52        return Ok(ValidationResult {
53            success: true,
54            message: "No tasks configured; skipping task status validation".to_string(),
55            output: None,
56        });
57    }
58
59    let mut parse_errors: usize = 0;
60    for diagnostic in &parsed.diagnostics {
61        if diagnostic.level == DiagnosticLevel::Error {
62            parse_errors += 1;
63        }
64    }
65
66    let remaining = parsed.progress.remaining;
67    let success = remaining == 0 && parse_errors == 0;
68
69    let mut lines: Vec<String> = Vec::new();
70    lines.push(format!("Total: {}", parsed.progress.total));
71    lines.push(format!("Complete: {}", parsed.progress.complete));
72    lines.push(format!("Shelved: {}", parsed.progress.shelved));
73    lines.push(format!("In-progress: {}", parsed.progress.in_progress));
74    lines.push(format!("Pending: {}", parsed.progress.pending));
75    lines.push(format!("Remaining: {}", parsed.progress.remaining));
76
77    if parse_errors > 0 {
78        lines.push(format!("Parse errors: {parse_errors}"));
79    }
80
81    if !success {
82        lines.push(String::new());
83        lines.push("Incomplete tasks:".to_string());
84        for t in &parsed.tasks {
85            if t.status.is_done() {
86                continue;
87            }
88            lines.push(format!(
89                "- {id} ({status}) {name}",
90                id = t.id,
91                status = t.status.as_enhanced_label(),
92                name = t.name
93            ));
94        }
95    }
96
97    let output = Some(lines.join("\n"));
98
99    let message = if success {
100        "All tasks are complete or shelved".to_string()
101    } else {
102        "Tasks remain pending or in-progress".to_string()
103    };
104
105    Ok(ValidationResult {
106        success,
107        message,
108        output,
109    })
110}
111
112/// Run project validation commands discovered from configuration sources.
113///
114/// If no validation is configured, returns success with a warning message.
115pub fn run_project_validation(ito_path: &Path, timeout: Duration) -> CoreResult<ValidationResult> {
116    let project_root = ito_path.parent().unwrap_or_else(|| Path::new("."));
117    let commands = discover_project_validation_commands(project_root, ito_path)?;
118
119    if commands.is_empty() {
120        return Ok(ValidationResult {
121            success: true,
122            message: "Warning: no project validation configured; skipping".to_string(),
123            output: None,
124        });
125    }
126
127    let mut combined: Vec<String> = Vec::new();
128    for cmd in commands {
129        let out = run_shell_with_timeout(project_root, &cmd, timeout)?;
130        combined.push(out.render());
131        if !out.success {
132            return Ok(ValidationResult {
133                success: false,
134                message: format!("Project validation failed: `{cmd}`"),
135                output: Some(combined.join("\n\n")),
136            });
137        }
138    }
139
140    Ok(ValidationResult {
141        success: true,
142        message: "Project validation passed".to_string(),
143        output: Some(combined.join("\n\n")),
144    })
145}
146
147/// Run an extra validation command provided explicitly by the user.
148pub fn run_extra_validation(
149    project_root: &Path,
150    command: &str,
151    timeout: Duration,
152) -> CoreResult<ValidationResult> {
153    let out = run_shell_with_timeout(project_root, command, timeout)?;
154    Ok(ValidationResult {
155        success: out.success,
156        message: if out.success {
157            format!("Extra validation passed: `{command}`")
158        } else {
159            format!("Extra validation failed: `{command}`")
160        },
161        output: Some(out.render()),
162    })
163}
164
165fn discover_project_validation_commands(
166    project_root: &Path,
167    ito_path: &Path,
168) -> CoreResult<Vec<String>> {
169    let candidates: Vec<(ProjectSource, PathBuf)> = vec![
170        (ProjectSource::RepoJson, project_root.join("ito.json")),
171        (ProjectSource::ItoConfigJson, ito_path.join("config.json")),
172        (ProjectSource::AgentsMd, project_root.join("AGENTS.md")),
173        (ProjectSource::ClaudeMd, project_root.join("CLAUDE.md")),
174    ];
175
176    for (source, path) in candidates {
177        if !path.exists() {
178            continue;
179        }
180        let contents = fs::read_to_string(&path)
181            .map_err(|e| CoreError::io(format!("Failed to read {}", path.display()), e))?;
182        let commands = match source {
183            ProjectSource::RepoJson | ProjectSource::ItoConfigJson => {
184                extract_commands_from_json_str(&contents)
185            }
186            ProjectSource::AgentsMd | ProjectSource::ClaudeMd => {
187                extract_commands_from_markdown(&contents)
188            }
189        };
190        if !commands.is_empty() {
191            return Ok(commands);
192        }
193    }
194
195    Ok(Vec::new())
196}
197
198#[derive(Debug, Clone, Copy)]
199enum ProjectSource {
200    RepoJson,
201    ItoConfigJson,
202    AgentsMd,
203    ClaudeMd,
204}
205
206fn extract_commands_from_json_str(contents: &str) -> Vec<String> {
207    let v: Value = match serde_json::from_str(contents) {
208        Ok(v) => v,
209        Err(_) => return Vec::new(),
210    };
211    extract_commands_from_json_value(&v)
212}
213
214fn extract_commands_from_json_value(v: &Value) -> Vec<String> {
215    let pointers = [
216        "/ralph/validationCommands",
217        "/ralph/validationCommand",
218        "/ralph/validation/commands",
219        "/ralph/validation/command",
220        "/validationCommands",
221        "/validationCommand",
222        "/project/validationCommands",
223        "/project/validationCommand",
224        "/project/validation/commands",
225        "/project/validation/command",
226    ];
227
228    for p in pointers {
229        if let Some(v) = v.pointer(p) {
230            let commands = normalize_commands_value(v);
231            if !commands.is_empty() {
232                return commands;
233            }
234        }
235    }
236
237    Vec::new()
238}
239
240fn normalize_commands_value(v: &Value) -> Vec<String> {
241    match v {
242        Value::String(s) => {
243            let s = s.trim();
244            if s.is_empty() {
245                Vec::new()
246            } else {
247                vec![s.to_string()]
248            }
249        }
250        Value::Array(items) => {
251            let mut out: Vec<String> = Vec::new();
252            for item in items {
253                if let Value::String(s) = item {
254                    let s = s.trim();
255                    if !s.is_empty() {
256                        out.push(s.to_string());
257                    }
258                }
259            }
260            out
261        }
262        Value::Null => Vec::new(),
263        Value::Bool(_b) => Vec::new(),
264        Value::Number(_n) => Vec::new(),
265        Value::Object(_obj) => Vec::new(),
266    }
267}
268
269fn extract_commands_from_markdown(contents: &str) -> Vec<String> {
270    // Heuristic: accept `make check` / `make test` lines anywhere.
271    let mut out: Vec<String> = Vec::new();
272    for line in contents.lines() {
273        let l = line.trim();
274        if l == "make check" || l == "make test" {
275            out.push(l.to_string());
276        }
277    }
278    out.dedup();
279    out
280}
281
282#[derive(Debug)]
283struct ShellRunOutput {
284    command: String,
285    success: bool,
286    exit_code: i32,
287    timed_out: bool,
288    stdout: String,
289    stderr: String,
290}
291
292impl ShellRunOutput {
293    fn render(&self) -> String {
294        let mut s = String::new();
295        s.push_str(&format!("Command: {}\n", self.command));
296        if self.timed_out {
297            s.push_str("Result: TIMEOUT\n");
298        } else if self.success {
299            s.push_str("Result: PASS\n");
300        } else {
301            s.push_str(&format!("Result: FAIL (exit {})\n", self.exit_code));
302        }
303        if !self.stdout.trim().is_empty() {
304            s.push_str("\nStdout:\n");
305            s.push_str(&truncate_for_context(&self.stdout, 12_000));
306            s.push('\n');
307        }
308        if !self.stderr.trim().is_empty() {
309            s.push_str("\nStderr:\n");
310            s.push_str(&truncate_for_context(&self.stderr, 12_000));
311            s.push('\n');
312        }
313        s
314    }
315}
316
317fn run_shell_with_timeout(cwd: &Path, cmd: &str, timeout: Duration) -> CoreResult<ShellRunOutput> {
318    let runner = SystemProcessRunner;
319    let request = ProcessRequest::new("sh")
320        .args(["-lc", cmd])
321        .current_dir(cwd.to_path_buf());
322    let output = runner.run_with_timeout(&request, timeout).map_err(|e| {
323        CoreError::Process(format!("Failed to run validation command '{cmd}': {e}"))
324    })?;
325
326    Ok(ShellRunOutput {
327        command: cmd.to_string(),
328        success: output.success,
329        exit_code: output.exit_code,
330        timed_out: output.timed_out,
331        stdout: output.stdout,
332        stderr: output.stderr,
333    })
334}
335
336fn truncate_for_context(s: &str, max_bytes: usize) -> String {
337    if s.len() <= max_bytes {
338        return s.to_string();
339    }
340    let mut out = s[..max_bytes].to_string();
341    out.push_str("\n... (truncated) ...");
342    out
343}
344
345#[cfg(test)]
346mod tests {
347    use super::*;
348    use std::fs;
349
350    fn write(path: &Path, contents: &str) {
351        if let Some(parent) = path.parent() {
352            fs::create_dir_all(parent).unwrap();
353        }
354        fs::write(path, contents).unwrap();
355    }
356
357    #[test]
358    fn task_completion_passes_when_no_tasks() {
359        let td = tempfile::tempdir().unwrap();
360        let ito = td.path().join(".ito");
361        fs::create_dir_all(&ito).unwrap();
362        let task_repo = crate::task_repository::FsTaskRepository::new(&ito);
363        let r = check_task_completion(&task_repo, "001-01_missing").unwrap();
364        assert!(r.success);
365    }
366
367    #[test]
368    fn task_completion_fails_when_remaining() {
369        let td = tempfile::tempdir().unwrap();
370        let ito = td.path().join(".ito");
371        fs::create_dir_all(ito.join("changes/001-01_test")).unwrap();
372        write(
373            &ito.join("changes/001-01_test/tasks.md"),
374            "# Tasks\n\n- [x] done\n- [ ] todo\n",
375        );
376        let task_repo = crate::task_repository::FsTaskRepository::new(&ito);
377        let r = check_task_completion(&task_repo, "001-01_test").unwrap();
378        assert!(!r.success);
379    }
380
381    #[test]
382    fn project_validation_discovers_commands_from_repo_json() {
383        let td = tempfile::tempdir().unwrap();
384        let project_root = td.path();
385        let ito = project_root.join(".ito");
386        fs::create_dir_all(&ito).unwrap();
387        write(
388            &project_root.join("ito.json"),
389            r#"{ "ralph": { "validationCommands": ["true"] } }"#,
390        );
391        let cmds = discover_project_validation_commands(project_root, &ito).unwrap();
392        assert_eq!(cmds, vec!["true".to_string()]);
393    }
394
395    #[test]
396    fn shell_timeout_is_failure() {
397        let td = tempfile::tempdir().unwrap();
398        let out =
399            run_shell_with_timeout(td.path(), "sleep 0.1", Duration::from_millis(50)).unwrap();
400        assert!(out.timed_out);
401        assert!(!out.success);
402    }
403}