Watch
0
0
Fork
You've already forked hyperhive
0

drop the sqlite event log nothing reads, and the comments naming routes that are gone

This commit is contained in:
iris 2026-11-16 04:40:22 +01:00 • committed by mara
commit cbda9c162f

View file

@ -1,11 +1,12 @@
//! Agent-side cleanup of this agent's own harness artifacts: completed
//! bash-task files and verbose `stream` event rows.
//! 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 `events.sqlite`. The harness
//! 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
@ -17,16 +18,10 @@
use std::path::Path;
use std::time::Duration;
use rusqlite::{Connection, Result, params};
/// 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 verbose `stream` event rows this long before pruning. Other event
/// kinds are never deleted by this sweep — they carry the semantic per-turn
/// history the operator scrolls back through.
const STREAM_KEEP_SECS: i64 = 14 * 24 * 3600;
/// Keep delivered (soft-deleted, `sent_at` set) reminder rows this long
/// before reaping them — same window as `STREAM_KEEP_SECS`, kept around
/// only to serve the trailing-window `ReminderRollup` stats.
@ -41,7 +36,7 @@ const TODO_ACKED_KEEP_SECS: i64 = 30 * 24 * 3600;
const TERMINAL_STATUSES: &[&str] = &["done", "timed_out", "interrupted"];
/// Background loop: hourly, prune this agent's stale bash-task files and
/// verbose event rows. Detached task — runs for the harness's lifetime;
/// 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 {
@ -62,14 +57,7 @@ fn sweep_once() {
}
}
let events_db = harness.join("hyperhive-events.sqlite");
if events_db.exists() {
match vacuum_events(&events_db) {
Ok(0) => {}
Ok(n) => tracing::info!(removed = n, "events vacuum"),
Err(e) => tracing::warn!(error = ?e, "events vacuum failed"),
}
}
remove_old_event_log(&harness);
let state_db = crate::paths::state_db();
if state_db.exists() {
@ -87,8 +75,7 @@ fn sweep_once() {
}
/// Reap delivered reminder rows older than [`REMINDER_KEEP_SECS`] via the
/// typed store API (own short-lived connection — mirrors `vacuum_events`'s
/// own connection to `events.sqlite` rather than sharing the harness's live
/// 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)?;
@ -165,15 +152,21 @@ fn delete_trio(dir: &Path, stem: &str) {
}
}
/// Prune verbose `stream` event rows older than [`STREAM_KEEP_SECS`] from the
/// agent's `events.sqlite`. Returns the number of rows deleted.
fn vacuum_events(path: &Path) -> Result<u64> {
let conn = Connection::open(path)?;
crate::sqlite_writer::configure_wal(&conn)?;
let cutoff = chrono::Utc::now().timestamp() - STREAM_KEEP_SECS;
let removed = conn.execute(
"DELETE FROM events WHERE kind = 'stream' AND ts < ?1",
params![cutoff],
)?;
Ok(u64::try_from(removed).unwrap_or(0))
/// 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");
}
}
}
}