git clone https://garage44.eu/jeroen/midas.gitcartopolis-agent · 2026-11-22T09:03:40Z
feat(node): a held node trims its own build directory The warmer trims target/ only on a node nobody holds, and sessions hold nodes for hours: in 60 days a Mac or vps-1 sat over its ceiling an hour or more 73 times, each mailed to the person, each cleared by the warmer once the node came free (midas log --kind alert, 2026-11-22). The guard now trims after each measure of target/ (every ten minutes), held or not, while no ssh session and no bot is on the node, in the warmer's order: incremental state, the profile or triple built longest ago, all of target/ last. Each part is renamed aside in one step before it is removed, so a build that starts meanwhile never reads half a directory.
src/node.rs | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 79 insertions(+), 1 deletion(-)
diff --git a/src/node.rs b/src/node.rs
index e511402..00cc7fe 100644
--- a/src/node.rs
+++ b/src/node.rs
@@ -364,7 +364,10 @@ pub fn guard() -> Result<i32> {
if tick % 300 == 0 {
// On its own thread: a du of a 20 GB tree takes seconds, and the
// guard must not stop watching the GPU pool for that long.
- std::thread::spawn(measure_target);
+ std::thread::spawn(|| {
+ measure_target();
+ trim_target(&config());
+ });
}
if tick % 30 == 0 {
// A bot lives exactly as long as its bot lease: one left behind by a
@@ -841,6 +844,61 @@ fn measure_target() {
}
}
+/// Where a part of target/ goes on its way out: renamed there in one step, so a
+/// build never sees it half deleted, then removed. On /work, beside target/.
+const TRASH: &str = "/work/.midas-trash";
+
+/// Over the ceiling the warmer keeps (`warm::ceiling`), the guard trims target/
+/// itself, while no ssh session and no bot is on the node, held or not. The
+/// warmer trims only a node nobody holds, and sessions hold them for hours: the
+/// Macs and vps-1 sat over it an hour or more 73 times in 60 days, each one
+/// mailed to the person, each one cleared by the warmer once the node was free
+/// (2026-11-22). The order is the warmer's: incremental state, then the profile
+/// or target triple built longest ago, and the whole of target/ last.
+fn trim_target(cfg: &Config) {
+ let _ = fs::remove_dir_all(TRASH); // what a trim cut short left
+ let target = Path::new(CHECKOUT).join("target");
+ let size = || fs::read_to_string(TARGET_SIZE).ok().and_then(|t| t.trim().parse::<u64>().ok());
+ let Some(from) = size() else { return };
+ // Once: what target/ drops is freed on /work, so their sum, and the ceiling, stay put.
+ let limit = crate::warm::ceiling(from, crate::util::disk_space(Path::new("/work")).map(|d| d.0));
+ let mut dropped = Vec::new();
+ while size().is_some_and(|s| s > limit) {
+ if ssh_sessions() > 0 || !crate::bot::reading().is_null() {
+ break;
+ }
+ let Some(d) = next_to_drop(&target) else { break };
+ if fs::rename(&d, TRASH).is_err() {
+ break;
+ }
+ let _ = fs::remove_dir_all(TRASH);
+ dropped.push(d.strip_prefix(CHECKOUT).unwrap_or(&d).to_string_lossy().to_string());
+ if d == target {
+ let _ = fs::write(TARGET_SIZE, "0");
+ } else {
+ measure_target();
+ }
+ }
+ if !dropped.is_empty() {
+ emit(cfg, "trim", json!({"actor": "guard", "dropped": dropped, "from": from, "to": size(), "ceiling": limit}));
+ }
+}
+
+/// What a trim drops next: incremental state, then the oldest of target/'s
+/// other profiles and triples, then target/ itself.
+fn next_to_drop(target: &Path) -> Option<std::path::PathBuf> {
+ let incremental = target.join("debug/incremental");
+ if incremental.is_dir() {
+ return Some(incremental);
+ }
+ let oldest = fs::read_dir(target).ok()?.flatten()
+ .filter(|e| e.file_type().is_ok_and(|t| t.is_dir()) && e.file_name() != "debug")
+ .filter_map(|e| Some((e.metadata().ok()?.modified().ok()?, e.path())))
+ .min_by_key(|(t, _)| *t)
+ .map(|(_, p)| p);
+ oldest.or_else(|| target.is_dir().then(|| target.to_path_buf()))
+}
+
/// Below this much free space on /work, sessions are warned.
const LOW_DISK: u64 = 10_000_000_000;
@@ -1008,4 +1066,24 @@ mod tests {
assert!(running_in(&dir).is_empty());
let _ = fs::remove_dir_all(&dir);
}
+
+ #[test]
+ fn a_trim_drops_incremental_state_then_the_oldest_profile_then_target() {
+ use super::next_to_drop;
+ use std::fs;
+ let dir = tempfile::tempdir().unwrap();
+ let t = dir.path().join("target");
+ for d in ["debug/incremental", "release", "aarch64-linux-android"] {
+ fs::create_dir_all(t.join(d)).unwrap();
+ std::thread::sleep(std::time::Duration::from_millis(20));
+ }
+ assert_eq!(next_to_drop(&t), Some(t.join("debug/incremental")));
+ fs::remove_dir_all(t.join("debug/incremental")).unwrap();
+ assert_eq!(next_to_drop(&t), Some(t.join("release")), "built longest ago");
+ fs::remove_dir_all(t.join("release")).unwrap();
+ fs::remove_dir_all(t.join("aarch64-linux-android")).unwrap();
+ assert_eq!(next_to_drop(&t), Some(t.clone()), "debug/ goes only with the whole of target/");
+ fs::remove_dir_all(&t).unwrap();
+ assert_eq!(next_to_drop(&t), None);
+ }
}