Skip to content

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

chore: files changed crates/openhuman-core/src/agent/artifacts/store_documents.rs,crates/openhuman-c

Steven Enamakelfd6236b33d6a
diff --git a/crates/openhuman-core/src/agent/artifacts/store_documents.rs b/crates/openhuman-core/src/agent/artifacts/store_documents.rs
index 4b6b021bd0..4e01c9391d 100644
--- a/crates/openhuman-core/src/agent/artifacts/store_documents.rs
+++ b/crates/openhuman-core/src/agent/artifacts/store_documents.rs
@@ -88,6 +88,15 @@ impl Docs {
})
}
+ /// Writes the record only when none exists (an import must not overwrite
+ /// a record created since).
+ fn put_meta_if_absent(&self, meta: &ArtifactMeta) -> Result<()> {
+ if self.get_meta(&meta.id)?.is_some() {
+ return Ok(());
+ }
+ self.put_meta(meta)
+ }
+
pub(super) fn get_meta(&self, id: &str) -> Result<Option<ArtifactMeta>> {
let id = id.to_string();
let stored = self
@@ -152,10 +161,13 @@ impl Docs {
})
}
- /// Imports the workspace's legacy `meta.json` / `args.json` files when the
- /// collection is still empty. Returns how many records were imported.
+ /// Imports the workspace's legacy `meta.json` / `args.json` files, once.
+ /// A marker file next to them is written only after every record went in,
+ /// so a failed import is retried and a record deleted afterwards is not
+ /// brought back by a restart. The files themselves are left in place.
pub(super) fn import_legacy(&self, artifacts_dir: &Path) -> Result<usize> {
- if !self.list_meta()?.is_empty() {
+ let marker = artifacts_dir.join(IMPORTED_MARKER);
+ if marker.exists() {
return Ok(0);
}
let Ok(entries) = std::fs::read_dir(artifacts_dir) else {
@@ -173,20 +185,27 @@ impl Docs {
);
continue;
};
- self.put_meta(&meta)?;
+ self.put_meta_if_absent(&meta)?;
if let Some(args) = std::fs::read_to_string(entry.path().join("args.json"))
.ok()
.and_then(|raw| serde_json::from_str::<Value>(&raw).ok())
{
- self.put_args(&meta.id, &args)?;
+ if self.get_args(&meta.id)?.is_none() {
+ self.put_args(&meta.id, &args)?;
+ }
}
imported += 1;
}
+ std::fs::write(&marker, b"legacy artifact records were imported into the storage backend\n")
+ .with_context(|| format!("write {}", marker.display()))?;
log::debug!("[artifacts] legacy import: imported={imported}");
Ok(imported)
}
}
+/// Written beside the legacy records once they are in the backend.
+const IMPORTED_MARKER: &str = ".imported-to-storage";
+
/// Runs a document-store call off the async worker (the port is reached
/// through a blocking bridge).
pub(super) async fn on_docs<T: Send + 'static>(
@@ -206,28 +225,20 @@ pub(super) async fn documents(workspace_dir: &Path) -> Result<Option<Docs>, Stri
let Some(docs) = current().map_err(|e| format!("[artifacts] storage: {e:#}"))? else {
return Ok(None);
};
- // Once per scope and workspace: the import only matters the first time.
+ // Once per process and workspace; the marker file makes it once overall.
+ // Serialized, and recorded only after the import succeeded.
static IMPORTED: std::sync::Mutex<Vec<String>> = std::sync::Mutex::new(Vec::new());
- let scope = crate::storage::current_scope().map_err(|e| format!("[artifacts] storage: {e}"))?;
- let key = format!("{scope}|{}", workspace_dir.display());
- let first = {
- let mut seen = IMPORTED
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner);
- if seen.contains(&key) {
- false
- } else {
+ let key = workspace_dir.display().to_string();
+ let legacy = workspace_dir.join("artifacts");
+ on_docs(docs.clone(), move |docs| {
+ let mut seen = IMPORTED.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
+ if !seen.contains(&key) {
+ docs.import_legacy(&legacy)?;
seen.push(key);
- true
}
- };
- if first {
- let legacy = workspace_dir.join("artifacts");
- on_docs(docs.clone(), move |docs| {
- docs.import_legacy(&legacy).map(|_| ())
- })
- .await?;
- }
+ Ok(())
+ })
+ .await?;
Ok(Some(docs))
}
diff --git a/crates/openhuman-core/src/agent/artifacts/store_documents_tests.rs b/crates/openhuman-core/src/agent/artifacts/store_documents_tests.rs
index 4411677414..ecdf8298fe 100644
--- a/crates/openhuman-core/src/agent/artifacts/store_documents_tests.rs
+++ b/crates/openhuman-core/src/agent/artifacts/store_documents_tests.rs
@@ -69,29 +69,30 @@ fn scopes_do_not_see_each_others_artifacts() {
}
#[test]
-fn the_legacy_files_are_imported_once_into_an_empty_collection() {
+fn the_legacy_files_are_imported_once_and_a_delete_sticks() {
let dir = tempfile::tempdir().unwrap();
let a = dir.path().join("a");
std::fs::create_dir_all(&a).unwrap();
- std::fs::write(
- a.join("meta.json"),
- serde_json::to_string(&meta("a", 1, Some("t"))).unwrap(),
- )
- .unwrap();
+ std::fs::write(a.join("meta.json"), serde_json::to_string(&meta("a", 1, Some("t"))).unwrap())
+ .unwrap();
std::fs::write(a.join("args.json"), r#"{"x":2}"#).unwrap();
let bad = dir.path().join("bad");
std::fs::create_dir_all(&bad).unwrap();
std::fs::write(bad.join("meta.json"), "{nope").unwrap();
let docs = docs_in(&MemoryStorage::new(), "local");
+ // A record created before the import is not overwritten.
+ let mut existing = meta("a", 1, Some("t"));
+ existing.title = "kept".into();
+ docs.put_meta(&existing).unwrap();
assert_eq!(docs.import_legacy(dir.path()).unwrap(), 1);
- assert_eq!(
- docs.get_meta("a").unwrap().unwrap().thread_id.as_deref(),
- Some("t")
- );
+ assert_eq!(docs.get_meta("a").unwrap().unwrap().title, "kept");
assert_eq!(docs.get_args("a").unwrap().unwrap()["x"], 2);
- // A non-empty collection is not imported over.
+
+ // Deleted afterwards, it is not imported again by a later run.
+ assert!(docs.delete("a").unwrap());
assert_eq!(docs.import_legacy(dir.path()).unwrap(), 0);
+ assert!(docs.get_meta("a").unwrap().is_none());
assert_eq!(docs.import_legacy(&dir.path().join("none")).unwrap(), 0);
}
diff --git a/crates/openhuman-core/src/platform/cost/tracker.rs b/crates/openhuman-core/src/platform/cost/tracker.rs
index 16e5c09402..dfeec36169 100644
--- a/crates/openhuman-core/src/platform/cost/tracker.rs
+++ b/crates/openhuman-core/src/platform/cost/tracker.rs
@@ -613,6 +613,9 @@ impl CostStorage {
// are not kept for a document ledger, whose scope changes per call;
// `get_aggregated_costs` recomputes them from the scope's records.
if let Some(docs) = super::tracker_documents::current()? {
+ // The legacy ledger goes in before the first document, so history
+ // is never split across the two.
+ self.import_legacy_once(&docs)?;
return docs.add(&record);
}