Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(storage): move layout state helpers into state module

Steven Enamakeldcb82d45bffc
diff --git a/crates/openhuman-core/src/storage/layout/db.rs b/crates/openhuman-core/src/storage/layout/db.rs
new file mode 100644
index 0000000000..4a7ad09e59
--- /dev/null
+++ b/crates/openhuman-core/src/storage/layout/db.rs
@@ -0,0 +1,141 @@
+//! Step 4: the v1 database becomes the profile's `storage.db`.
+//!
+//! The v1 default is one SQLite file, `workspace/storage/storage.db`, with
+//! every scope's rows in it. `VACUUM INTO` writes a consistent, compacted copy
+//! (it reads through the WAL), to `storage.db.tmp`; the copy is verified table
+//! by table against the source (the driver keeps documents, streams and blobs
+//! in tables, so equal row counts mean equal collection, stream and blob
+//! counts) and only then renamed into place. The source is never modified.
+
+use std::collections::BTreeMap;
+use std::path::{Path, PathBuf};
+
+use super::state::LayoutError;
+use crate::storage::rusqlite::{self, Connection, OpenFlags};
+
+/// Row counts per table.
+pub type Counts = BTreeMap<String, u64>;
+
+/// What a copy did.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct DbCopy {
+ /// Row counts of the copy (equal to the source's).
+ pub counts: Counts,
+ /// Whether an existing, matching `storage.db` was kept instead of copying.
+ pub reused: bool,
+}
+
+fn sql(error: rusqlite::Error) -> LayoutError {
+ LayoutError::Other(anyhow::anyhow!("sqlite: {error}"))
+}
+
+/// Row counts of every table of the database at `path`.
+///
+/// # Errors
+///
+/// When the file cannot be opened or read.
+pub fn table_counts(path: &Path) -> Result<Counts, LayoutError> {
+ let conn = Connection::open_with_flags(path, OpenFlags::SQLITE_OPEN_READ_WRITE).map_err(sql)?;
+ counts_of(&conn)
+}
+
+fn counts_of(conn: &Connection) -> Result<Counts, LayoutError> {
+ let mut tables = Vec::new();
+ {
+ let mut stmt = conn
+ .prepare("SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' ORDER BY name")
+ .map_err(sql)?;
+ let rows = stmt.query_map([], |row| row.get::<_, String>(0)).map_err(sql)?;
+ for row in rows {
+ tables.push(row.map_err(sql)?);
+ }
+ }
+ let mut counts = Counts::new();
+ for table in tables {
+ let quoted = format!("\"{}\"", table.replace('"', "\"\""));
+ let n: i64 = conn
+ .query_row(&format!("SELECT COUNT(*) FROM {quoted}"), [], |row| row.get(0))
+ .map_err(sql)?;
+ counts.insert(table, u64::try_from(n).unwrap_or(0));
+ }
+ Ok(counts)
+}
+
+fn tmp_path(dest: &Path) -> PathBuf {
+ let mut name = dest.file_name().unwrap_or_default().to_os_string();
+ name.push(".tmp");
+ dest.with_file_name(name)
+}
+
+fn mismatch(source: &Counts, copy: &Counts) -> Option<String> {
+ if source == copy {
+ return None;
+ }
+ let mut tables: Vec<&String> = source.keys().chain(copy.keys()).collect();
+ tables.sort();
+ tables.dedup();
+ let diffs: Vec<String> = tables
+ .into_iter()
+ .filter(|t| source.get(*t) != copy.get(*t))
+ .map(|t| format!("{t}: source {:?} copy {:?}", source.get(t), copy.get(t)))
+ .collect();
+ Some(diffs.join("; "))
+}
+
+/// Copies `source` to `dest` (see the module docs). Idempotent: a `dest` whose
+/// counts already equal the source's is kept; one that differs is replaced (it
+/// can only be left over from an interrupted run, because nothing opens it
+/// before the layout marker exists).
+///
+/// # Errors
+///
+/// [`LayoutError::Verify`] when the copy's counts differ from the source's, or
+/// an I/O or SQLite error. The source is untouched either way.
+pub fn copy(source: &Path, dest: &Path) -> Result<DbCopy, LayoutError> {
+ let conn = Connection::open_with_flags(source, OpenFlags::SQLITE_OPEN_READ_WRITE).map_err(sql)?;
+ let source_counts = counts_of(&conn)?;
+ if dest.is_file() {
+ if let Ok(existing) = table_counts(dest) {
+ if existing == source_counts {
+ tracing::debug!(target: "openhuman::storage", dest = %dest.display(), "[layout] storage.db already matches the source; kept");
+ return Ok(DbCopy { counts: existing, reused: true });
+ }
+ }
+ }
+ let tmp = tmp_path(dest);
+ for stale in [tmp.clone(), tmp.with_extension("tmp-wal"), tmp.with_extension("tmp-shm")] {
+ let _ = std::fs::remove_file(stale);
+ }
+ conn.execute("VACUUM INTO ?1", [tmp.to_string_lossy().as_ref()]).map_err(sql)?;
+ drop(conn);
+ let copy_counts = table_counts(&tmp)?;
+ if let Some(diff) = mismatch(&source_counts, &copy_counts) {
+ let _ = std::fs::remove_file(&tmp);
+ return Err(LayoutError::Verify(format!("the database copy does not match its source ({diff})")));
+ }
+ std::fs::File::open(&tmp)?.sync_all()?;
+ std::fs::rename(&tmp, dest)?;
+ tracing::info!(
+ target: "openhuman::storage",
+ tables = copy_counts.len(),
+ rows = copy_counts.values().sum::<u64>(),
+ "[layout] database copied and verified"
+ );
+ Ok(DbCopy { counts: copy_counts, reused: false })
+}
+
+/// Checkpoints and truncates the WAL of `path`, so the file set the archive
+/// reads is complete in the `.db` file.
+///
+/// # Errors
+///
+/// A SQLite error.
+pub fn checkpoint(path: &Path) -> Result<(), LayoutError> {
+ let conn = Connection::open_with_flags(path, OpenFlags::SQLITE_OPEN_READ_WRITE).map_err(sql)?;
+ conn.query_row("PRAGMA wal_checkpoint(TRUNCATE)", [], |_| Ok(())).map_err(sql)?;
+ Ok(())
+}
+
+#[cfg(test)]
+#[path = "db_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/storage/layout/state.rs b/crates/openhuman-core/src/storage/layout/state.rs
new file mode 100644
index 0000000000..5a1c7689a2
--- /dev/null
+++ b/crates/openhuman-core/src/storage/layout/state.rs
@@ -0,0 +1,236 @@
+//! The migration's lock and its progress record.
+//!
+//! - **Lock**: an OS advisory lock on `users/<id>/.layout.lock`
+//! ([`Lock`]). A second migrator for the same profile, in this or another
+//! process, is refused with [`LayoutError::Locked`]; the lock dies with its
+//! holder, so a crash never leaves a stale one behind.
+//! - **Progress**: a small JSON file, `users/<id>/.layout.state`
+//! ([`LayoutState`]), written atomically (temp file, fsync, rename) after
+//! every finished step. Its existence means "a migration started and did not
+//! finish": the next run resumes from it even when the `.layout` marker is
+//! already written. It is removed as the very last act of a migration. A step
+//! is recorded only after its work finished and every step is idempotent, so
+//! a crash anywhere re-runs at most one step from its start.
+
+use std::fs::{File, OpenOptions};
+use std::io::Write;
+use std::path::{Path, PathBuf};
+
+use fs2::FileExt;
+use serde::{Deserialize, Serialize};
+
+use super::plan::Plan;
+
+/// The lock file, beside `config.toml`.
+pub const LOCK_FILE: &str = ".layout.lock";
+/// The progress file, beside `config.toml`.
+pub const STATE_FILE: &str = ".layout.state";
+
+/// A migration step, in the order they run.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
+#[serde(rename_all = "snake_case")]
+pub enum Step {
+ /// Legacy importers write the v1 files into the v1 database.
+ Import,
+ /// The schema migrations run over the imported database; the plan is
+ /// classified.
+ Migrate,
+ /// The database is copied to `storage.db` and verified.
+ CopyDb,
+ /// User files move into `files/`.
+ MoveFiles,
+ /// The archive is written and verified.
+ Archive,
+ /// The marker is written and the archived originals are deleted.
+ Finish,
+}
+
+impl Step {
+ /// Every step, in order.
+ pub const ALL: [Step; 6] = [
+ Step::Import,
+ Step::Migrate,
+ Step::CopyDb,
+ Step::MoveFiles,
+ Step::Archive,
+ Step::Finish,
+ ];
+}
+
+/// Why a migration did not complete.
+#[derive(Debug, thiserror::Error)]
+pub enum LayoutError {
+ /// Another migrator holds the profile's lock.
+ #[error("LAYOUT_LOCKED: another migration holds {0}")]
+ Locked(PathBuf),
+ /// The running core serves this profile's v1 database.
+ #[error("LAYOUT_LIVE: this process serves {0}; stop the core and migrate offline, or set [storage] auto_migrate_layout and restart")]
+ Live(String),
+ /// A test stopped the migration after `Step`'s work, before recording it.
+ #[error("LAYOUT_CRASH: injected crash after {0:?}")]
+ InjectedCrash(Step),
+ /// A moved entry's destination already holds something else.
+ #[error("LAYOUT_CONFLICT: {0}")]
+ Conflict(String),
+ /// A verification failed (counts, archive).
+ #[error("LAYOUT_VERIFY: {0}")]
+ Verify(String),
+ /// Anything else.
+ #[error("{0:#}")]
+ Other(#[from] anyhow::Error),
+}
+
+impl From<std::io::Error> for LayoutError {
+ fn from(error: std::io::Error) -> Self {
+ Self::Other(error.into())
+ }
+}
+
+/// The held lock. Dropping it releases it.
+#[derive(Debug)]
+pub struct Lock {
+ file: File,
+}
+
+impl Lock {
+ /// Takes the lock of `profile_dir`, creating the lock file.
+ ///
+ /// # Errors
+ ///
+ /// [`LayoutError::Locked`] when another migrator holds it, or an I/O error.
+ pub fn acquire(profile_dir: &Path) -> Result<Self, LayoutError> {
+ let path = profile_dir.join(LOCK_FILE);
+ let file = OpenOptions::new()
+ .create(true)
+ .truncate(false)
+ .write(true)
+ .open(&path)?;
+ match file.try_lock_exclusive() {
+ Ok(()) => {
+ tracing::debug!(target: "openhuman::storage", path = %path.display(), "[layout] lock taken");
+ Ok(Self { file })
+ }
+ Err(error) if error.kind() == fs2::lock_contended_error().kind() => {
+ Err(LayoutError::Locked(path))
+ }
+ Err(error) => Err(error.into()),
+ }
+ }
+}
+
+impl Drop for Lock {
+ fn drop(&mut self) {
+ let _ = FileExt::unlock(&self.file);
+ }
+}
+
+/// What a migration has done so far.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct LayoutState {
+ /// Format version of this file.
+ pub version: u32,
+ /// When the migration started (names the archive).
+ pub started_ms: i64,
+ /// The archive's file name under `<root>/backups`.
+ pub archive: String,
+ /// Finished steps.
+ pub done: Vec<Step>,
+ /// The classified workspace, taken once the schema step finished and kept
+ /// for every later step, so a resume agrees with the first run even after
+ /// entries have moved.
+ #[serde(default)]
+ pub plan: Option<Plan>,
+}
+
+impl LayoutState {
+ /// A fresh state started at `started_ms` for `user`.
+ pub fn new(user: &str, started_ms: i64) -> Self {
+ let stamp = chrono::DateTime::from_timestamp_millis(started_ms)
+ .unwrap_or_default()
+ .format("%Y%m%dT%H%M%S");
+ Self {
+ version: 1,
+ started_ms,
+ archive: format!("{}-{stamp}.tar.zst", sanitize(user)),
+ done: Vec::new(),
+ plan: None,
+ }
+ }
+
+ /// Whether `step` finished.
+ pub fn has(&self, step: Step) -> bool {
+ self.done.contains(&step)
+ }
+
+ /// The state in `profile_dir`, `None` when no migration is in progress.
+ ///
+ /// # Errors
+ ///
+ /// An unreadable or unparsable file: the migration must not guess.
+ pub fn load(profile_dir: &Path) -> Result<Option<Self>, LayoutError> {
+ match std::fs::read(profile_dir.join(STATE_FILE)) {
+ Ok(bytes) => serde_json::from_slice(&bytes)
+ .map(Some)
+ .map_err(|e| LayoutError::Other(anyhow::anyhow!("the layout state file is damaged: {e}"))),
+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
+ Err(e) => Err(e.into()),
+ }
+ }
+
+ /// Writes the state atomically.
+ ///
+ /// # Errors
+ ///
+ /// An I/O error.
+ pub fn save(&self, profile_dir: &Path) -> Result<(), LayoutError> {
+ let tmp = profile_dir.join(format!("{STATE_FILE}.tmp"));
+ let bytes = serde_json::to_vec(self).map_err(|e| LayoutError::Other(e.into()))?;
+ let mut file = File::create(&tmp)?;
+ file.write_all(&bytes)?;
+ file.sync_all()?;
+ std::fs::rename(&tmp, profile_dir.join(STATE_FILE))?;
+ Ok(())
+ }
+
+ /// Records `step` as finished and saves.
+ ///
+ /// # Errors
+ ///
+ /// An I/O error.
+ pub fn finish(&mut self, step: Step, profile_dir: &Path) -> Result<(), LayoutError> {
+ if !self.has(step) {
+ self.done.push(step);
+ }
+ self.save(profile_dir)
+ }
+
+ /// Removes the state file (the last act of a migration).
+ ///
+ /// # Errors
+ ///
+ /// An I/O error other than "not found".
+ pub fn clear(profile_dir: &Path) -> Result<(), LayoutError> {
+ match std::fs::remove_file(profile_dir.join(STATE_FILE)) {
+ Ok(()) => Ok(()),
+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
+ Err(e) => Err(e.into()),
+ }
+ }
+}
+
+/// A user id as one safe file-name segment.
+fn sanitize(user: &str) -> String {
+ let clean: String = user
+ .chars()
+ .map(|c| if c.is_ascii_alphanumeric() || c == '-' || c == '_' { c } else { '_' })
+ .collect();
+ if clean.is_empty() {
+ "user".to_owned()
+ } else {
+ clean
+ }
+}
+
+#[cfg(test)]
+#[path = "state_tests.rs"]
+mod tests;