Skip to content

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

feat(import): expose layout_status module for legacy import

Steven Enamakel2acab32c2886
diff --git a/crates/openhuman-core/src/storage/import/layout_status.rs b/crates/openhuman-core/src/storage/import/layout_status.rs
new file mode 100644
index 0000000000..926fa61f49
--- /dev/null
+++ b/crates/openhuman-core/src/storage/import/layout_status.rs
@@ -0,0 +1,63 @@
+//! Read-only compatibility reader for migration progress in old workspace DBs.
+//!
+//! The v2 driver no longer exposes the v1 schema registry. This reader stays
+//! inside the legacy-import boundary and inspects only the historical
+//! `tsd.migrations` document rows needed by `storage.status`.
+
+use std::collections::BTreeMap;
+use std::path::Path;
+
+use anyhow::{Context, Result};
+use serde::Deserialize;
+
+use super::Snapshot;
+use crate::storage::rusqlite;
+
+#[derive(Debug, Deserialize)]
+struct LegacyProgress {
+ owner: String,
+ to: u32,
+ done: bool,
+}
+
+/// Reads completed owner versions and the last completion timestamp from a
+/// historical Tiny Storage Drivers database without opening it through the
+/// current backend or modifying the source file.
+pub(crate) fn read(path: &Path) -> Result<(Vec<(String, u32)>, Option<i64>)> {
+ let snapshot = Snapshot::of(path, "layout-status/source.db")?;
+ let connection = rusqlite::Connection::open_with_flags(
+ snapshot.db(),
+ rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY,
+ )
+ .with_context(|| format!("open snapshot {} read-only", snapshot.db().display()))?;
+ let mut statement = connection
+ .prepare(
+ "SELECT body, updated_ms FROM docs \
+ WHERE scope = ?1 AND coll = ?2 AND deleted = 0",
+ )
+ .context("prepare legacy migration progress query")?;
+ let mut rows = statement
+ .query(["local", "tsd.migrations"])
+ .context("query legacy migration progress")?;
+ let mut owners = BTreeMap::<String, u32>::new();
+ let mut last = None::<i64>;
+ while let Some(row) = rows.next().context("read legacy migration progress")? {
+ let body: String = row.get(0).context("read migration progress body")?;
+ let updated_ms: i64 = row.get(1).context("read migration update timestamp")?;
+ let Ok(progress) = serde_json::from_str::<LegacyProgress>(&body) else {
+ // Match the old status view: malformed or unrelated rows do not
+ // prevent reporting valid migration progress.
+ continue;
+ };
+ if progress.done && progress.to > 0 {
+ let version = owners.entry(progress.owner).or_default();
+ *version = (*version).max(progress.to);
+ last = last.max((updated_ms > 0).then_some(updated_ms));
+ }
+ }
+ Ok((owners.into_iter().collect(), last))
+}
+
+#[cfg(test)]
+#[path = "layout_status_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/storage/import/layout_status_tests.rs b/crates/openhuman-core/src/storage/import/layout_status_tests.rs
new file mode 100644
index 0000000000..4e37709e60
--- /dev/null
+++ b/crates/openhuman-core/src/storage/import/layout_status_tests.rs
@@ -0,0 +1,60 @@
+use super::*;
+
+#[test]
+fn reads_completed_versions_from_a_read_only_database_snapshot() {
+ let dir = tempfile::tempdir().unwrap();
+ let db = dir.path().join("storage.db");
+ let connection = rusqlite::Connection::open(&db).unwrap();
+ connection
+ .execute_batch(
+ "CREATE TABLE docs (
+ scope TEXT NOT NULL,
+ coll TEXT NOT NULL,
+ id TEXT NOT NULL,
+ updated_ms INTEGER NOT NULL,
+ deleted INTEGER NOT NULL DEFAULT 0,
+ body TEXT NOT NULL,
+ PRIMARY KEY (scope, coll, id)
+ ) WITHOUT ROWID;",
+ )
+ .unwrap();
+ for (id, owner, to, done, updated_ms) in [
+ ("a", "threads", 1, true, 100),
+ ("b", "threads", 3, true, 300),
+ ("c", "profiles", 2, true, 200),
+ ("d", "incomplete", 9, false, 900),
+ ("e", "zero", 0, true, 1000),
+ ] {
+ let body = serde_json::json!({
+ "owner": owner,
+ "step": "step",
+ "from": 0,
+ "to": to,
+ "cursor": null,
+ "written": 1,
+ "done": done,
+ });
+ connection
+ .execute(
+ "INSERT INTO docs (scope, coll, id, updated_ms, body) \
+ VALUES ('local', 'tsd.migrations', ?1, ?2, ?3)",
+ rusqlite::params![id, updated_ms, body.to_string()],
+ )
+ .unwrap();
+ }
+ connection
+ .execute(
+ "INSERT INTO docs (scope, coll, id, updated_ms, body) \
+ VALUES ('cluster', 'tsd.migrations', 'other-scope', 5000, ?1)",
+ [r#"{"owner":"ignored","to":7,"done":true}"#],
+ )
+ .unwrap();
+ drop(connection);
+ let original = std::fs::read(&db).unwrap();
+
+ let (versions, last) = read(&db).unwrap();
+
+ assert_eq!(versions, vec![("profiles".into(), 2), ("threads".into(), 3)]);
+ assert_eq!(last, Some(300));
+ assert_eq!(std::fs::read(&db).unwrap(), original);
+}
diff --git a/crates/openhuman-core/src/storage/import/mod.rs b/crates/openhuman-core/src/storage/import/mod.rs
index 1318ee510b..4c6ec65271 100644
--- a/crates/openhuman-core/src/storage/import/mod.rs
+++ b/crates/openhuman-core/src/storage/import/mod.rs
@@ -51,6 +51,8 @@
pub mod live;
mod schemas;
#[cfg(feature = "legacy-import")]
+pub(crate) mod layout_status;
+#[cfg(feature = "legacy-import")]
pub mod sessions;
#[cfg(feature = "legacy-import")]
mod snapshot;