Skip to main content

soma_codemode/artifacts/
prune.rs

1use std::path::Path;
2
3pub async fn prune_old_runs(root: &Path, keep: usize) -> std::io::Result<usize> {
4    if keep == 0 || !root.exists() {
5        return Ok(0);
6    }
7    let mut entries = tokio::fs::read_dir(root).await?;
8    let mut dirs = Vec::new();
9    while let Some(entry) = entries.next_entry().await? {
10        if entry.file_type().await?.is_dir() {
11            dirs.push(entry.path());
12        }
13    }
14    dirs.sort();
15    let remove_count = dirs.len().saturating_sub(keep);
16    let mut removed = 0;
17    for dir in dirs.into_iter().take(remove_count) {
18        tokio::fs::remove_dir_all(dir).await?;
19        removed += 1;
20    }
21    Ok(removed)
22}