Skip to content

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

feat(artifacts): add document artifact store

Steven Enamakel5805dd515c75
diff --git a/crates/openhuman-core/src/agent/artifacts/store.rs b/crates/openhuman-core/src/agent/artifacts/store.rs
index 8527d84144..727eff7d11 100644
--- a/crates/openhuman-core/src/agent/artifacts/store.rs
+++ b/crates/openhuman-core/src/agent/artifacts/store.rs
@@ -6,7 +6,6 @@ use super::types::{ArtifactMeta, ArtifactStatus};
const ARTIFACTS_SUBDIR: &str = "artifacts";
const META_FILENAME: &str = "meta.json";
-const META_TMP_FILENAME: &str = "meta.json.tmp";
/// Sidecar file holding the verbatim producer-tool arguments that
/// generated the artifact, persisted next to `meta.json` so a failed
/// card's Retry button can re-dispatch the exact same generation
@@ -97,49 +96,9 @@ pub(crate) async fn save_artifact_meta(
) -> Result<(), String> {
log::debug!("[artifacts] save_artifact_meta: id={}", meta.id);
validate_artifact_id(&meta.id)?;
- if let Some(docs) = documents(workspace_dir).await? {
- let owned = meta.clone();
- return on_docs(docs, move |docs| docs.put_meta(&owned)).await;
- }
- let root = artifacts_root(workspace_dir).await?;
- let artifact_dir = root.join(&meta.id);
- // Verify sandboxing before writing
- assert_within_root(&root, &artifact_dir)?;
- tokio::fs::create_dir_all(&artifact_dir)
- .await
- .map_err(|e| {
- format!(
- "[artifacts] failed to create artifact dir {:?}: {e}",
- artifact_dir
- )
- })?;
- let meta_path = artifact_dir.join(META_FILENAME);
- let json = serde_json::to_string_pretty(meta).map_err(|e| {
- format!(
- "[artifacts] failed to serialize meta for id={}: {e}",
- meta.id
- )
- })?;
- // Write-then-rename so a crash mid-write never leaves a truncated
- // meta.json: the migration relies on the record flipping atomically
- // from the legacy path to the moved file.
- let tmp_path = artifact_dir.join(META_TMP_FILENAME);
- tokio::fs::write(&tmp_path, json).await.map_err(|e| {
- format!(
- "[artifacts] failed to write meta.json for id={}: {e}",
- meta.id
- )
- })?;
- tokio::fs::rename(&tmp_path, &meta_path)
- .await
- .map_err(|e| {
- format!(
- "[artifacts] failed to commit meta.json for id={}: {e}",
- meta.id
- )
- })?;
- log::debug!("[artifacts] saved meta.json for id={}", meta.id);
- Ok(())
+ let docs = documents(workspace_dir).await?;
+ let owned = meta.clone();
+ on_docs(docs, move |docs| docs.put_meta(&owned)).await
}
/// List artifacts in the workspace, sorted by `created_at` descending.
@@ -165,71 +124,8 @@ pub(crate) async fn list_artifacts(
thread_id,
workspace_dir
);
- if let Some(docs) = documents(workspace_dir).await? {
- let all = on_docs(docs, |docs| docs.list_meta()).await?;
- return Ok(page_of(all, offset, limit, thread_id));
- }
- let root = artifacts_root(workspace_dir).await?;
-
- let mut read_dir = match tokio::fs::read_dir(&root).await {
- Ok(rd) => rd,
- Err(e) => {
- return Err(format!(
- "[artifacts] failed to read artifacts dir {:?}: {e}",
- root
- ))
- }
- };
-
- let mut all: Vec<ArtifactMeta> = Vec::new();
-
- loop {
- let entry = match read_dir.next_entry().await {
- Ok(Some(e)) => e,
- Ok(None) => break,
- Err(e) => {
- log::warn!("[artifacts] error reading directory entry: {e}");
- continue;
- }
- };
-
- let entry_path = entry.path();
- // Only process directories
- match entry.file_type().await {
- Ok(ft) if ft.is_dir() => {}
- Ok(_) => continue,
- Err(e) => {
- log::warn!(
- "[artifacts] failed to get file type for {:?}: {e}",
- entry_path
- );
- continue;
- }
- }
-
- let meta_path = entry_path.join(META_FILENAME);
- let contents = match tokio::fs::read_to_string(&meta_path).await {
- Ok(c) => c,
- Err(e) => {
- log::warn!(
- "[artifacts] skipping {:?}: failed to read meta.json: {e}",
- entry_path
- );
- continue;
- }
- };
-
- match serde_json::from_str::<ArtifactMeta>(&contents) {
- Ok(meta) => all.push(meta),
- Err(e) => {
- log::warn!(
- "[artifacts] skipping {:?}: corrupt meta.json: {e}",
- entry_path
- );
- }
- }
- }
-
+ let docs = documents(workspace_dir).await?;
+ let all = on_docs(docs, |docs| docs.list_meta()).await?;
Ok(page_of(all, offset, limit, thread_id))
}
@@ -240,23 +136,11 @@ pub(crate) async fn get_artifact(
) -> Result<ArtifactMeta, String> {
log::debug!("[artifacts] get_artifact: id={artifact_id}");
validate_artifact_id(artifact_id)?;
- if let Some(docs) = documents(workspace_dir).await? {
- let id = artifact_id.to_string();
- return on_docs(docs, move |docs| docs.get_meta(&id))
- .await?
- .ok_or_else(|| format!("[artifacts] artifact not found id={artifact_id}"));
- }
- let root = artifacts_root(workspace_dir).await?;
- let artifact_dir = root.join(artifact_id);
- assert_within_root(&root, &artifact_dir)?;
- let meta_path = artifact_dir.join(META_FILENAME);
- let contents = tokio::fs::read_to_string(&meta_path).await.map_err(|e| {
- format!("[artifacts] artifact not found or unreadable id={artifact_id}: {e}")
- })?;
- let meta: ArtifactMeta = serde_json::from_str(&contents)
- .map_err(|e| format!("[artifacts] corrupt meta.json for id={artifact_id}: {e}"))?;
- log::debug!("[artifacts] get_artifact: found id={artifact_id}");
- Ok(meta)
+ let docs = documents(workspace_dir).await?;
+ let id = artifact_id.to_string();
+ on_docs(docs, move |docs| docs.get_meta(&id))
+ .await?
+ .ok_or_else(|| format!("[artifacts] artifact not found id={artifact_id}"))
}
/// Persist the verbatim producer-tool arguments alongside an artifact's
@@ -276,29 +160,9 @@ pub(crate) async fn save_artifact_args(
) -> Result<(), String> {
log::debug!("[artifacts] save_artifact_args: id={artifact_id}");
validate_artifact_id(artifact_id)?;
- if let Some(docs) = documents(workspace_dir).await? {
- let (id, owned) = (artifact_id.to_string(), args.clone());
- return on_docs(docs, move |docs| docs.put_args(&id, &owned)).await;
- }
- let root = artifacts_root(workspace_dir).await?;
- let artifact_dir = root.join(artifact_id);
- assert_within_root(&root, &artifact_dir)?;
- tokio::fs::create_dir_all(&artifact_dir)
- .await
- .map_err(|e| {
- format!(
- "[artifacts] failed to create artifact dir {:?}: {e}",
- artifact_dir
- )
- })?;
- let args_path = artifact_dir.join(ARGS_FILENAME);
- let json = serde_json::to_string_pretty(args)
- .map_err(|e| format!("[artifacts] failed to serialize args for id={artifact_id}: {e}"))?;
- tokio::fs::write(&args_path, json)
- .await
- .map_err(|e| format!("[artifacts] failed to write args.json for id={artifact_id}: {e}"))?;
- log::debug!("[artifacts] saved args.json for id={artifact_id}");
- Ok(())
+ let docs = documents(workspace_dir).await?;
+ let (id, owned) = (artifact_id.to_string(), args.clone());
+ on_docs(docs, move |docs| docs.put_args(&id, &owned)).await
}
/// Load the persisted producer-tool arguments for an artifact (#3162).
@@ -313,23 +177,13 @@ pub(crate) async fn read_artifact_args(
) -> Result<serde_json::Value, String> {
log::debug!("[artifacts] read_artifact_args: id={artifact_id}");
validate_artifact_id(artifact_id)?;
- if let Some(docs) = documents(workspace_dir).await? {
- let id = artifact_id.to_string();
- return on_docs(docs, move |docs| docs.get_args(&id))
- .await?
- .ok_or_else(|| {
- format!("[artifacts] no persisted args for id={artifact_id} (not regenerable)")
- });
- }
- let root = artifacts_root(workspace_dir).await?;
- let artifact_dir = root.join(artifact_id);
- assert_within_root(&root, &artifact_dir)?;
- let args_path = artifact_dir.join(ARGS_FILENAME);
- let contents = tokio::fs::read_to_string(&args_path).await.map_err(|e| {
- format!("[artifacts] no persisted args for id={artifact_id} (not regenerable): {e}")
- })?;
- serde_json::from_str(&contents)
- .map_err(|e| format!("[artifacts] corrupt args.json for id={artifact_id}: {e}"))
+ let docs = documents(workspace_dir).await?;
+ let id = artifact_id.to_string();
+ on_docs(docs, move |docs| docs.get_args(&id))
+ .await?
+ .ok_or_else(|| {
+ format!("[artifacts] no persisted args for id={artifact_id} (not regenerable)")
+ })
}
/// Read the raw bytes of a finalized artifact's output file.
@@ -389,14 +243,8 @@ pub(crate) async fn delete_artifact(
}
}
}
- if let Some(docs) = documents(workspace_dir).await? {
- return store_documents::delete_record(docs, &artifact_dir, artifact_id).await;
- }
- tokio::fs::remove_dir_all(&artifact_dir)
- .await
- .map_err(|e| format!("[artifacts] failed to delete artifact id={artifact_id}: {e}"))?;
- log::debug!("[artifacts] delete_artifact: deleted id={artifact_id}");
- Ok(())
+ let docs = documents(workspace_dir).await?;
+ store_documents::delete_record(docs, &artifact_dir, artifact_id).await
}
// Mark a status as unused — referenced only in tests via the store
@@ -524,21 +372,7 @@ pub async fn create_artifact_for_call(
let root = artifacts_root(workspace_dir).await?;
let artifact_dir = root.join(&id);
assert_within_root(&root, &artifact_dir)?;
- // With a storage backend the record is a document, so there is no
- // per-artifact directory to make.
- if store_documents::current()
- .map_err(|e| format!("[artifacts] storage: {e:#}"))?
- .is_none()
- {
- tokio::fs::create_dir_all(&artifact_dir)
- .await
- .map_err(|e| {
- format!(
- "[artifacts] create_artifact: failed to mkdir {:?}: {e}",
- artifact_dir
- )
- })?;
- }
+ // The record is a document, so there is no per-artifact directory to make.
// Capture the originating chat thread (if any) at create-time so the
// panel can repopulate from disk after a redux-persist purge — see
diff --git a/crates/openhuman-core/src/agent/artifacts/store_documents.rs b/crates/openhuman-core/src/agent/artifacts/store_documents.rs
index eff5960b63..6a72535857 100644
--- a/crates/openhuman-core/src/agent/artifacts/store_documents.rs
+++ b/crates/openhuman-core/src/agent/artifacts/store_documents.rs
@@ -1,9 +1,10 @@
//! Artifact records on the `tinystoragedrivers` document port.
//!
-//! Used instead of `<workspace>/artifacts/<id>/meta.json` and `args.json`
-//! when the host configured a storage backend ([`crate::storage`]);
-//! `store.rs` picks it per call, so the records live under the acting agent's
-//! storage scope (`local` on a single-user host).
+//! The only record store (`<workspace>/artifacts/<id>/meta.json` and
+//! `args.json` are legacy, imported once): `store.rs` opens it per call, so the
+//! records live under the acting agent's storage scope (`local` on a
+//! single-user host) in the installed backend, or the workspace default when
+//! none is installed ([`crate::storage::local`]).
//!
//! # Layout
//!
@@ -47,13 +48,14 @@ fn collections() -> Vec<CollectionSpec> {
vec![CollectionSpec::new(ARTIFACTS), CollectionSpec::new(ARGS)]
}
-/// The document store for this call, when the host configured one.
-pub(super) fn current() -> Result<Option<Docs>> {
+/// The document store for this call: the installed backend, else the
+/// workspace default, in the acting scope.
+pub(super) fn current(workspace_dir: &Path) -> Result<Docs> {
#[cfg(test)]
if let Some(docs) = TEST_OVERRIDE.with(|slot| slot.borrow().clone()) {
- return Ok(Some(docs));
+ return Ok(docs);
}
- Ok(Repo::current(DOMAIN, collections)?.map(Docs))
+ Ok(Docs(Repo::current_for(workspace_dir, DOMAIN, collections)?))
}
/// Runs `f` with `docs` standing in for the installed backend, on this thread
@@ -271,14 +273,11 @@ pub(super) async fn on_docs<T: Send + 'static>(
.map_err(|e| format!("[artifacts] storage: {e:#}"))
}
-/// The document store for this call, when a storage backend is configured.
-/// The first call of a process for this backend instance, scope and workspace
+/// The document store for this call. The first call of a process for this backend instance, scope and workspace
/// imports the workspace's legacy `meta.json` / `args.json` files (recorded
/// only once the import succeeded, so a failed one is retried).
-pub(super) async fn documents(workspace_dir: &Path) -> Result<Option<Docs>, String> {
- let Some(docs) = current().map_err(|e| format!("[artifacts] storage: {e:#}"))? else {
- return Ok(None);
- };
+pub(super) async fn documents(workspace_dir: &Path) -> Result<Docs, String> {
+ let docs = current(workspace_dir).map_err(|e| format!("[artifacts] storage: {e:#}"))?;
let legacy = workspace_dir.join("artifacts");
on_docs(docs.clone(), move |docs| {
let key = legacy.display().to_string();
@@ -286,7 +285,7 @@ pub(super) async fn documents(workspace_dir: &Path) -> Result<Option<Docs>, Stri
.once_per_backend(&key, || docs.import_legacy(&legacy).map(|_| ()))
})
.await?;
- Ok(Some(docs))
+ Ok(docs)
}
/// Deletes artifact `id` in document mode. The legacy directory