Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/hive-agent/src/vacuum.rs
2026-11-16 21:13:14 +01:00

388 lines
15 KiB
Rust

//! Agent-side cleanup of this agent's own harness artifacts: completed
//! bash-task files, delivered reminders and acked todos, and the event log an
//! earlier harness wrote.
//!
//! Runs IN the harness (not host-side in hive-c0re) because the files are
//! owned by the agent user. Under privsep hive-c0re runs as the unprivileged
//! `hive-core` user and cannot delete agent-owned files — the old host-side
//! sweeps hit `PermissionDenied` on the bash-task trio and an
//! attempt-to-write-a-readonly-database error on a harness database. The harness
//! owns these paths, so the deletes succeed here.
//!
//! Trade-off (accepted — issue tracker "perms borked"): a misbehaving harness
//! could skip its own cleanup, which the host-side version was meant to
//! prevent. But a compromised harness is already inside the container trust
//! boundary (`docs/2-handbook/agents/security.md`), and these are ephemeral local artifacts — so
//! the honest fix is to clean them up where they live.
use std::path::Path;
use std::time::Duration;
/// How often the sweep runs.
const VACUUM_INTERVAL: Duration = Duration::from_hours(1);
/// Keep completed bash-task files this long before deleting their trio.
const BASH_KEEP_SECS: i64 = 48 * 3600;
/// Keep delivered (soft-deleted, `sent_at` set) reminder rows this long
/// before reaping them.
const REMINDER_KEEP_SECS: i64 = 14 * 24 * 3600;
/// Keep acked (agent-dismissed) todo rows this long before reaping them —
/// see `todos.rs`'s module doc for why acking doesn't delete outright. Long
/// enough that a genuinely quiet month is the only way to trigger the "one
/// spurious re-announcement" fallback path, short enough the table doesn't
/// grow meaningfully from one-shot todos that will never be upserted again.
const TODO_ACKED_KEEP_SECS: i64 = 30 * 24 * 3600;
/// Terminal bash-task statuses whose files are eligible for deletion.
const TERMINAL_STATUSES: &[&str] = &["done", "timed_out", "interrupted"];
/// Background loop: hourly, prune this agent's stale bash-task files and
/// the event log an earlier harness left behind. Detached task — runs for the harness's lifetime;
/// errors are logged, never fatal.
pub async fn run() {
loop {
sweep_once();
tokio::time::sleep(VACUUM_INTERVAL).await;
}
}
fn sweep_once() {
let harness = crate::paths::harness_dir();
let tasks_dir = harness.join("bash-tasks");
if tasks_dir.is_dir() {
let removed =
vacuum_bash_tasks(&tasks_dir, chrono::Utc::now().timestamp() - BASH_KEEP_SECS);
if removed > 0 {
tracing::info!(removed, "bash-tasks vacuum");
}
}
remove_old_event_log(&harness);
let state_db = crate::paths::state_db();
if state_db.exists() {
match vacuum_reminders(&state_db) {
Ok(0) => {}
Ok(n) => tracing::info!(removed = n, "reminders vacuum"),
Err(e) => tracing::warn!(error = ?e, "reminders vacuum failed"),
}
match vacuum_todos(&state_db) {
Ok(0) => {}
Ok(n) => tracing::info!(removed = n, "todos vacuum"),
Err(e) => tracing::warn!(error = ?e, "todos vacuum failed"),
}
}
}
/// Reap delivered reminder rows older than [`REMINDER_KEEP_SECS`] via the
/// typed store API (own short-lived connection rather than the harness's live
/// `Reminders` handle).
fn vacuum_reminders(path: &Path) -> anyhow::Result<usize> {
let store = crate::reminders::Reminders::open(path)?;
store.prune_delivered_older_than(chrono::Utc::now().timestamp() - REMINDER_KEEP_SECS)
}
/// Reap acked todo rows older than [`TODO_ACKED_KEEP_SECS`] via the typed
/// store API — same own-short-lived-connection shape as `vacuum_reminders`.
fn vacuum_todos(path: &Path) -> anyhow::Result<usize> {
let store = crate::todos::Todos::open(path)?;
store.reap_acked(chrono::Utc::now().timestamp() - TODO_ACKED_KEEP_SECS)
}
/// Delete eligible bash-task trios in `dir`. Returns the count of `.json`
/// sentinels removed (each represents one task; `.out`/`.err` deletions are
/// not counted separately).
fn vacuum_bash_tasks(dir: &Path, cutoff: i64) -> u64 {
let Ok(rd) = std::fs::read_dir(dir) else {
return 0;
};
let mut removed: u64 = 0;
for entry in rd.flatten() {
let path = entry.path();
// Only process the .json sentinel; derive sibling paths from it.
if path.extension().and_then(|e| e.to_str()) != Some("json") {
continue;
}
let Some(stem) = path.file_stem().and_then(|s| s.to_str()).map(str::to_owned) else {
continue;
};
if should_delete(&path, cutoff) {
delete_trio(dir, &stem);
removed += 1;
}
}
removed
}
/// Return `true` when the task file has a terminal status and a
/// `completed_at` older than `cutoff`.
fn should_delete(json_path: &Path, cutoff: i64) -> bool {
let Ok(raw) = std::fs::read_to_string(json_path) else {
return false;
};
let Ok(v) = serde_json::from_str::<serde_json::Value>(&raw) else {
return false;
};
let status = v.get("status").and_then(|s| s.as_str()).unwrap_or("");
if !TERMINAL_STATUSES.contains(&status) {
return false;
}
// `completed_at` serializes as an RFC3339 string (`TaskFile.completed_at`
// is `Option<DateTime<Utc>>`), not a bare epoch-seconds integer — parse
// it the same way, falling back to "never expired" on anything
// unparseable so a corrupt/legacy field never causes a premature delete.
let completed_at = v
.get("completed_at")
.and_then(serde_json::Value::as_str)
.and_then(|s| chrono::DateTime::parse_from_rfc3339(s).ok())
.map_or(i64::MAX, |dt| dt.timestamp());
completed_at < cutoff
}
/// Delete the `.json`, `.out`, and `.err` files for a task. Errors are
/// logged but do not abort the sweep.
fn delete_trio(dir: &Path, stem: &str) {
for ext in ["json", "out", "err"] {
let path = dir.join(format!("{stem}.{ext}"));
if path.exists()
&& let Err(e) = std::fs::remove_file(&path)
{
tracing::warn!(path = %path.display(), error = ?e, "bash-tasks vacuum: remove failed");
}
}
}
/// Delete the sqlite event log (and its WAL files) that harnesses used to
/// write for the per-agent page's history. Nothing reads or writes it now.
fn remove_old_event_log(harness: &Path) {
for name in [
"hyperhive-events.sqlite",
"hyperhive-events.sqlite-wal",
"hyperhive-events.sqlite-shm",
] {
let path = harness.join(name);
match std::fs::remove_file(&path) {
Ok(()) => tracing::info!(path = %path.display(), "removed the old event log"),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
Err(e) => {
tracing::warn!(path = %path.display(), error = %e, "removing the old event log failed");
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn write_task(dir: &Path, stem: &str, json: &str) {
std::fs::write(dir.join(format!("{stem}.json")), json).unwrap();
std::fs::write(dir.join(format!("{stem}.out")), b"out").unwrap();
std::fs::write(dir.join(format!("{stem}.err")), b"err").unwrap();
}
fn present(dir: &Path, stem: &str) -> [bool; 3] {
["json", "out", "err"].map(|e| dir.join(format!("{stem}.{e}")).exists())
}
const CUTOFF: i64 = 1_800_000_000;
const OLD: &str = "2026-01-01T00:00:00Z";
const NEW: &str = "2999-01-01T00:00:00Z";
/// A finished task past the cutoff loses all three of its files, and is
/// counted once; everything that is not a clearly expired finished task
/// stays, including the files it cannot read.
#[test]
fn only_expired_finished_tasks_lose_their_files() {
let dir = tempfile::tempdir().unwrap();
let d = dir.path();
for (stem, status) in [
("old-done", "done"),
("old-timed", "timed_out"),
("old-int", "interrupted"),
] {
write_task(
d,
stem,
&format!(r#"{{"status":"{status}","completed_at":"{OLD}"}}"#),
);
}
write_task(
d,
"recent",
&format!(r#"{{"status":"done","completed_at":"{NEW}"}}"#),
);
write_task(
d,
"running",
&format!(r#"{{"status":"running","completed_at":"{OLD}"}}"#),
);
write_task(
d,
"killed",
&format!(r#"{{"status":"killed","completed_at":"{OLD}"}}"#),
);
write_task(d, "no-time", r#"{"status":"done"}"#);
write_task(
d,
"bad-time",
r#"{"status":"done","completed_at":"yesterday"}"#,
);
write_task(d, "epoch-int", r#"{"status":"done","completed_at":1}"#);
write_task(d, "corrupt", "not json");
std::fs::write(d.join("notes.txt"), b"x").unwrap();
assert_eq!(vacuum_bash_tasks(d, CUTOFF), 3);
for gone in ["old-done", "old-timed", "old-int"] {
assert_eq!(present(d, gone), [false; 3], "{gone}");
}
for kept in [
"recent",
"running",
"killed",
"no-time",
"bad-time",
"epoch-int",
"corrupt",
] {
assert_eq!(present(d, kept), [true; 3], "{kept}");
}
assert!(d.join("notes.txt").exists());
}
/// A trio missing its `.out` or `.err` still goes, and a missing directory
/// is an empty sweep, not an error.
#[test]
fn a_partial_trio_goes_and_a_missing_dir_sweeps_nothing() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(
dir.path().join("half.json"),
format!(r#"{{"status":"done","completed_at":"{OLD}"}}"#),
)
.unwrap();
assert_eq!(vacuum_bash_tasks(dir.path(), CUTOFF), 1);
assert_eq!(present(dir.path(), "half"), [false; 3]);
assert_eq!(vacuum_bash_tasks(&dir.path().join("absent"), CUTOFF), 0);
}
/// Delivered reminders and acked todos past their keep time are reaped;
/// pending reminders and open todos never are.
#[test]
fn the_stores_lose_only_old_delivered_and_acked_rows() {
let dir = tempfile::tempdir().unwrap();
let db = dir.path().join("state.sqlite");
let reminders = crate::reminders::Reminders::open(&db).unwrap();
let old_sent = reminders.store("old", None, 0).unwrap();
let new_sent = reminders.store("new", None, 0).unwrap();
let pending = reminders.store("pending", None, i64::MAX / 2).unwrap();
reminders.mark_delivered(old_sent).unwrap();
reminders.mark_delivered(new_sent).unwrap();
let todos = crate::todos::Todos::open(&db).unwrap();
let (old_acked, _) = todos.upsert("a", Some("1"), "old", None, false).unwrap();
let (new_acked, _) = todos.upsert("a", Some("2"), "new", None, false).unwrap();
todos.upsert("a", Some("3"), "open", None, false).unwrap();
todos.mark_done(old_acked).unwrap();
todos.mark_done(new_acked).unwrap();
let conn = rusqlite::Connection::open(&db).unwrap();
conn.execute("UPDATE reminders SET sent_at = 1 WHERE id = ?1", [old_sent])
.unwrap();
conn.execute("UPDATE todos SET acked_at = 1 WHERE id = ?1", [old_acked])
.unwrap();
assert_eq!(vacuum_reminders(&db).unwrap(), 1);
assert_eq!(vacuum_todos(&db).unwrap(), 1);
assert_eq!(reminders.count_pending().unwrap(), 1);
assert_eq!(todos.open_count().unwrap(), 1);
let left: i64 = conn
.query_row("SELECT COUNT(*) FROM reminders", [], |r| r.get(0))
.unwrap();
assert_eq!(left, 2, "the pending and the freshly delivered stay");
let left: i64 = conn
.query_row("SELECT COUNT(*) FROM todos", [], |r| r.get(0))
.unwrap();
assert_eq!(left, 2, "the open and the freshly acked stay");
let _ = pending;
}
/// One sweep does all of it under the harness dir: the bash tasks, the old
/// event log and the state db's rows.
#[test]
fn a_sweep_prunes_the_harness_dir() {
let _dir = crate::paths::harness_dir_guard();
let harness = crate::paths::harness_dir();
let tasks = harness.join("bash-tasks");
std::fs::create_dir_all(&tasks).unwrap();
write_task(
&tasks,
"old",
&format!(r#"{{"status":"done","completed_at":"{OLD}"}}"#),
);
write_task(&tasks, "live", r#"{"status":"running"}"#);
std::fs::write(harness.join("hyperhive-events.sqlite"), b"x").unwrap();
let reminders = crate::reminders::Reminders::open(&crate::paths::state_db()).unwrap();
let id = reminders.store("x", None, 0).unwrap();
reminders.mark_delivered(id).unwrap();
rusqlite::Connection::open(crate::paths::state_db())
.unwrap()
.execute("UPDATE reminders SET sent_at = 1", [])
.unwrap();
sweep_once();
assert_eq!(present(&tasks, "old"), [false; 3]);
assert_eq!(present(&tasks, "live"), [true; 3]);
assert!(!harness.join("hyperhive-events.sqlite").exists());
assert_eq!(reminders.prune_delivered_older_than(i64::MAX).unwrap(), 0);
}
/// A sweep over a harness dir with nothing in it does nothing and does not
/// create anything.
#[test]
fn a_sweep_of_an_empty_harness_dir_is_a_no_op() {
let _dir = crate::paths::harness_dir_guard();
sweep_once();
assert_eq!(
std::fs::read_dir(crate::paths::harness_dir())
.unwrap()
.count(),
0
);
}
#[test]
fn the_old_event_log_and_its_wal_files_are_removed_and_nothing_else() {
let dir = tempfile::tempdir().unwrap();
for name in [
"hyperhive-events.sqlite",
"hyperhive-events.sqlite-wal",
"hyperhive-events.sqlite-shm",
"hyperhive-turn-stats.sqlite",
] {
std::fs::write(dir.path().join(name), b"x").unwrap();
}
remove_old_event_log(dir.path());
assert!(!dir.path().join("hyperhive-events.sqlite").exists());
assert!(!dir.path().join("hyperhive-events.sqlite-wal").exists());
assert!(!dir.path().join("hyperhive-events.sqlite-shm").exists());
assert!(dir.path().join("hyperhive-turn-stats.sqlite").exists());
}
#[test]
fn a_harness_dir_without_the_log_is_left_alone() {
let dir = tempfile::tempdir().unwrap();
remove_old_event_log(dir.path());
assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 0);
}
/// A removal that fails for another reason is logged and the rest still go.
#[test]
fn a_failed_removal_does_not_stop_the_others() {
let dir = tempfile::tempdir().unwrap();
std::fs::create_dir(dir.path().join("hyperhive-events.sqlite")).unwrap();
std::fs::write(dir.path().join("hyperhive-events.sqlite-wal"), b"x").unwrap();
remove_old_event_log(dir.path());
assert!(dir.path().join("hyperhive-events.sqlite").is_dir());
assert!(!dir.path().join("hyperhive-events.sqlite-wal").exists());
}
}