Watch
0
0
Fork
You've already forked hyperhive
0

hive-agent: drop the legacy todos/reminders db migration

This commit is contained in:
damocles 2026-11-22 16:59:24 +01:00
commit 44b248bf6d
3 changed files with 4 additions and 332 deletions

View file

@ -1,294 +0,0 @@
//! One-time on-boot migration from the pre-db-consolidation per-concern
//! sqlite files (`hyperhive-todos.sqlite`, `hyperhive-reminders.sqlite`)
//! into the single consolidated `hyperhive-state.sqlite` (mara: "not yet
//! another sqlite ... add it to one of the existing ones"). Idempotent + best
//! effort: `run` is called once in `main.rs`, before anything opens the
//! consolidated db — a fresh harness boot with no legacy files (or one that
//! already migrated) is a silent no-op, and a failed step leaves its legacy
//! file in place to be retried on the next boot.
//!
//! Two legacy shapes fold in:
//! - `hyperhive-todos.sqlite` already has exactly the `todos` table the
//! consolidated db wants and nothing touches it during the rename window
//! (this runs before the todo/reminder subsystems start), so a plain
//! filesystem rename is correct and cheap — no sqlite involved.
//! - `hyperhive-reminders.sqlite`'s `reminders` table then gets copied in via
//! `ATTACH DATABASE` + `INSERT INTO ... SELECT`, preserving row ids (a
//! reminder id an agent saw in a `get_loose_ends` call just before the
//! migrating boot must still `cancel` correctly after it), and the legacy
//! file is removed once the copy lands.
use std::path::Path;
use anyhow::{Context, Result};
use rusqlite::{Connection, params};
/// Fold the legacy `legacy_todos`/`legacy_reminders` files (if present) into
/// the single consolidated `state_db` path. The legacy todos file is only
/// renamed in while `state_db` does not exist yet; the legacy reminders file
/// is copied in whenever it is still on disk, so a copy that failed on one
/// boot is retried on the next.
///
/// # Errors
///
/// Propagates filesystem rename / sqlite attach-copy failures. Callers
/// treat this as best-effort (log and continue) — `Todos`/`Reminders::open`
/// against `state_db` still work standalone either way (todos would simply
/// start empty rather than inheriting the legacy rows). A failed todos
/// rename or reminders copy leaves its legacy file in place and is retried
/// on the next boot, and so is a failed `remove_file(legacy_reminders)`.
/// A retry skips rows already present with the same content, but a legacy
/// row that was copied and then cancelled or pruned from the consolidated
/// db comes back, which is the cost of retrying at all.
pub fn run(state_db: &Path, legacy_todos: &Path, legacy_reminders: &Path) -> Result<()> {
if !state_db.exists() && legacy_todos.exists() {
std::fs::rename(legacy_todos, state_db).with_context(|| {
format!(
"rename legacy todos db {} -> {}",
legacy_todos.display(),
state_db.display()
)
})?;
}
if legacy_reminders.exists() {
// Ensure `state_db` exists and has the `reminders` schema — either
// the rename above already brought a todos-only db across (no
// `reminders` table yet), or there was no legacy todos file at all
// and this creates `state_db` fresh.
crate::reminders::Reminders::open(state_db)
.context("apply reminders schema to consolidated db")?;
migrate_reminders_rows(state_db, legacy_reminders)
.context("copy legacy reminders rows into consolidated db")?;
std::fs::remove_file(legacy_reminders).with_context(|| {
format!("remove legacy reminders db {}", legacy_reminders.display())
})?;
}
Ok(())
}
/// Copy every row of the legacy `reminders` table into `state_db`'s table
/// of the same name, preserving `id`. Uses a scratch `ATTACH DATABASE` so
/// the copy is one sqlite statement rather than a row-by-row read/insert
/// loop.
fn migrate_reminders_rows(state_db: &Path, legacy_reminders: &Path) -> Result<()> {
let conn = Connection::open(state_db)
.with_context(|| format!("open consolidated db {}", state_db.display()))?;
conn.execute(
"ATTACH DATABASE ?1 AS legacy",
params![legacy_reminders.to_string_lossy()],
)
.context("attach legacy reminders db")?;
let copied = conn
.query_row("SELECT COUNT(*) FROM main.reminders", [], |row| {
row.get::<_, i64>(0)
})
.context("count consolidated reminders")
.and_then(|existing| {
// Ids are kept while the table is empty (the first-boot case an
// id seen just before the upgrade relies on). A retry after the
// harness already stored reminders of its own cannot reuse them:
// a colliding id would silently drop the legacy row. Those rows
// are appended under fresh ids instead, skipping any that are
// already there with the same content.
let sql = if existing == 0 {
"INSERT INTO reminders (id, message, file_path, due_at, created_at, sent_at) \
SELECT id, message, file_path, due_at, created_at, sent_at FROM legacy.reminders;"
} else {
"INSERT INTO reminders (message, file_path, due_at, created_at, sent_at) \
SELECT l.message, l.file_path, l.due_at, l.created_at, l.sent_at \
FROM legacy.reminders l WHERE NOT EXISTS ( \
SELECT 1 FROM main.reminders r WHERE r.message = l.message \
AND r.due_at = l.due_at AND r.created_at = l.created_at \
AND r.file_path IS l.file_path);"
};
conn.execute_batch(sql)
.context("copy legacy reminders rows")
});
// DETACH regardless of the copy's outcome so the connection doesn't
// leak the attachment on an early return via `?` inside this fn.
let detach = conn.execute("DETACH DATABASE legacy", []);
copied?;
detach.context("detach legacy reminders db")?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{reminders::Reminders, todos::Todos};
#[test]
fn no_legacy_files_is_a_noop() {
let dir = tempfile::tempdir().unwrap();
let state = dir.path().join("hyperhive-state.sqlite");
let todos = dir.path().join("hyperhive-todos.sqlite");
let reminders = dir.path().join("hyperhive-reminders.sqlite");
run(&state, &todos, &reminders).unwrap();
assert!(!state.exists(), "nothing to migrate, nothing created");
}
#[test]
fn an_existing_state_db_leaves_a_legacy_todos_file_alone() {
let dir = tempfile::tempdir().unwrap();
let state = dir.path().join("hyperhive-state.sqlite");
let legacy_todos = dir.path().join("hyperhive-todos.sqlite");
std::fs::write(&legacy_todos, b"should be left alone").unwrap();
Todos::open(&state).unwrap();
run(
&state,
&legacy_todos,
&dir.path().join("hyperhive-reminders.sqlite"),
)
.unwrap();
assert_eq!(
std::fs::read(&legacy_todos).unwrap(),
b"should be left alone",
"already-migrated boot must not touch a leftover legacy file"
);
}
#[test]
fn renames_legacy_todos_into_state_db() {
let dir = tempfile::tempdir().unwrap();
let legacy_todos = dir.path().join("hyperhive-todos.sqlite");
let state = dir.path().join("hyperhive-state.sqlite");
{
let store = Todos::open(&legacy_todos).unwrap();
store
.upsert("matrix", Some("!a:x"), "1 unread", None, false)
.unwrap();
}
run(
&state,
&legacy_todos,
&dir.path().join("hyperhive-reminders.sqlite"),
)
.unwrap();
assert!(!legacy_todos.exists(), "renamed away");
let migrated = Todos::open(&state).unwrap();
let rows = migrated.list(None).unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].summary, "1 unread");
}
#[test]
fn copies_legacy_reminders_preserving_ids() {
let dir = tempfile::tempdir().unwrap();
let legacy_reminders = dir.path().join("hyperhive-reminders.sqlite");
let state = dir.path().join("hyperhive-state.sqlite");
let orig_id = {
let store = Reminders::open(&legacy_reminders).unwrap();
store.store("check on x", None, 1000).unwrap()
};
run(
&state,
&dir.path().join("hyperhive-todos.sqlite"),
&legacy_reminders,
)
.unwrap();
assert!(!legacy_reminders.exists(), "removed after copy");
let migrated = Reminders::open(&state).unwrap();
let pending = migrated.list_pending().unwrap();
assert_eq!(pending.len(), 1);
assert_eq!(pending[0].id, orig_id, "id preserved across the copy");
assert_eq!(pending[0].due_at.timestamp(), 1000);
}
/// A copy that fails after `state_db` was created must be retried on the
/// next boot, not abandoned because the consolidated db now exists.
#[test]
fn a_failed_reminders_copy_is_retried_once_the_state_db_exists() {
let dir = tempfile::tempdir().unwrap();
let legacy_reminders = dir.path().join("hyperhive-reminders.sqlite");
let state = dir.path().join("hyperhive-state.sqlite");
let legacy_todos = dir.path().join("hyperhive-todos.sqlite");
std::fs::write(&legacy_reminders, b"not a sqlite file").unwrap();
run(&state, &legacy_todos, &legacy_reminders).unwrap_err();
assert!(state.exists(), "the failed copy still created the db");
assert!(legacy_reminders.exists(), "legacy file kept for a retry");
std::fs::remove_file(&legacy_reminders).unwrap();
Reminders::open(&legacy_reminders)
.unwrap()
.store("ping", None, 500)
.unwrap();
run(&state, &legacy_todos, &legacy_reminders).unwrap();
assert!(!legacy_reminders.exists(), "removed after the retry");
assert_eq!(
Reminders::open(&state)
.unwrap()
.list_pending()
.unwrap()
.len(),
1
);
}
/// The harness boots after a failed copy and stores reminders of its own,
/// so their ids overlap the legacy ones. The retry must keep both sets.
#[test]
fn a_retry_keeps_legacy_reminders_whose_ids_collide_with_new_ones() {
let dir = tempfile::tempdir().unwrap();
let legacy_reminders = dir.path().join("hyperhive-reminders.sqlite");
let state = dir.path().join("hyperhive-state.sqlite");
let legacy_todos = dir.path().join("hyperhive-todos.sqlite");
std::fs::write(&legacy_reminders, b"not a sqlite file").unwrap();
run(&state, &legacy_todos, &legacy_reminders).unwrap_err();
Reminders::open(&state)
.unwrap()
.store("new while degraded", None, 900)
.unwrap();
std::fs::remove_file(&legacy_reminders).unwrap();
{
let legacy = Reminders::open(&legacy_reminders).unwrap();
legacy.store("legacy one", None, 100).unwrap();
legacy.store("legacy two", None, 200).unwrap();
}
run(&state, &legacy_todos, &legacy_reminders).unwrap();
let messages: Vec<String> = Reminders::open(&state)
.unwrap()
.list_pending()
.unwrap()
.into_iter()
.map(|r| r.message)
.collect();
assert_eq!(messages.len(), 3, "got {messages:?}");
for expected in ["new while degraded", "legacy one", "legacy two"] {
assert!(messages.iter().any(|m| m == expected), "missing {expected}");
}
}
#[test]
fn folds_both_legacy_files_into_one_consolidated_db() {
let dir = tempfile::tempdir().unwrap();
let legacy_todos = dir.path().join("hyperhive-todos.sqlite");
let legacy_reminders = dir.path().join("hyperhive-reminders.sqlite");
let state = dir.path().join("hyperhive-state.sqlite");
{
Todos::open(&legacy_todos)
.unwrap()
.upsert("bash", None, "task done", None, false)
.unwrap();
Reminders::open(&legacy_reminders)
.unwrap()
.store("ping", None, 500)
.unwrap();
}
run(&state, &legacy_todos, &legacy_reminders).unwrap();
assert!(!legacy_todos.exists());
assert!(!legacy_reminders.exists());
assert_eq!(Todos::open(&state).unwrap().list(None).unwrap().len(), 1);
assert_eq!(
Reminders::open(&state)
.unwrap()
.list_pending()
.unwrap()
.len(),
1
);
}
}

View file

@ -14,7 +14,6 @@ mod boot_announce;
mod claude_credentials;
mod claude_md_watch;
mod claude_usage_watch;
mod db_migrate;
mod disk_watch;
mod events;
mod harness_state;
@ -561,26 +560,13 @@ async fn serve_main<S: Surface>(socket: &Path, poll_ms: u64) -> Result<()> {
});
// Before the first turn, so claude's own auto-load finds `AGENTS.md`.
agents_md_migrate::run(&paths::state_dir());
// Messages made inside this container rather than by the broker:
// reminders and the swarm UI's operator input.
let (local_msg_tx, local_msg_rx) = tokio::sync::mpsc::unbounded_channel();
// Harness-local reminders fire into an mpsc channel the serve loop races
// against the broker long-poll (see `reminder_timer`). Best-effort: it
// parks forever instead of closing the channel when the store can't open.
// Opened before the todo socket, which shares the same `Arc`.
// Fold the pre-consolidation per-concern db files into the single
// `hyperhive-state.sqlite`, if this is the first boot since the
// upgrade — see `db_migrate` module docs. Best-effort: a failure here
// just leaves the legacy file(s) in place to retry next boot, so the
// subsystems below still start (`Todos`/`Reminders::open` create the
// schema fresh if nothing migrated across).
if let Err(e) = db_migrate::run(
&paths::state_db(),
&paths::legacy_todos_db(),
&paths::legacy_reminders_db(),
) {
tracing::warn!(error = ?e, "legacy db_migrate failed — continuing with fresh/partial state db");
}
// Messages made inside this container rather than by the broker:
// reminders and the swarm UI's operator input.
let (local_msg_tx, local_msg_rx) = tokio::sync::mpsc::unbounded_channel();
let reminder_store = open_reminders();
let (todo_wake, todos_store) =
start_local_inputs(reminder_store, local_msg_tx, &bus, &interrupted);

View file

@ -50,29 +50,11 @@ pub fn harness_dir() -> PathBuf {
/// here from the now-removed ask/answer mechanism — nothing opens or
/// writes it, and any rows in it are harmless leftovers, not a store to
/// migrate or clean up.
///
/// A one-time boot migration (`db_migrate::run`) folds the older
/// `hyperhive-todos.sqlite` / `hyperhive-reminders.sqlite` files into this
/// path on first boot after an upgrade.
#[must_use]
pub fn state_db() -> PathBuf {
harness_dir().join("hyperhive-state.sqlite")
}
/// Legacy pre-consolidation todos db path, consulted only by
/// [`crate::db_migrate`] on the first boot after the upgrade.
#[must_use]
pub fn legacy_todos_db() -> PathBuf {
harness_dir().join("hyperhive-todos.sqlite")
}
/// Legacy pre-consolidation reminders db path, consulted only by
/// [`crate::db_migrate`] on the first boot after the upgrade.
#[must_use]
pub fn legacy_reminders_db() -> PathBuf {
harness_dir().join("hyperhive-reminders.sqlite")
}
/// Per-turn config dir for the regenerated claude-{mcp-config,settings,
/// system-prompt} files the harness drops before each turn. Set by
/// systemd via `RuntimeDirectory = "hive-config"`: a per-service runtime