Skip to content

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

fix(artifacts): read stored file versions by key after tiny-artifacts file store

Steven Enamakelc81474ba1429
diff --git a/crates/openhuman-core/src/artifacts/preview.rs b/crates/openhuman-core/src/artifacts/preview.rs
index 9084f9176a..95b2dadc1c 100644
--- a/crates/openhuman-core/src/artifacts/preview.rs
+++ b/crates/openhuman-core/src/artifacts/preview.rs
@@ -59,7 +59,7 @@ pub async fn read_body(config: &Config, id: &str) -> Result<ArtifactBody, String
.get(id)
.await
.map_err(|e| e.to_string())?;
- let record = match versioned {
+ let mut record = match versioned {
Some(record) => record,
// An artifact made before versioning has only its legacy record: show
// the file that record vouches for.
@@ -68,13 +68,20 @@ pub async fn read_body(config: &Config, id: &str) -> Result<ArtifactBody, String
if let Some(folder) = record
.latest()
.and_then(|v| v.file.as_ref())
- .and_then(|f| Path::new(&f.path).parent())
+ .and_then(|f| Path::new(&f.key).parent())
{
roots.push(folder.to_path_buf());
}
record
}
};
+ // A version's file is kept under a storage key inside the version files
+ // folder; show it by the path it has on disk. A legacy absolute key stays
+ // as it is, since joining an absolute path replaces the base.
+ let kept = super::tiny_store::version_files_dir(&config.workspace_dir);
+ if let Some(file) = record.versions.last_mut().and_then(|v| v.file.as_mut()) {
+ file.key = kept.join(&file.key).display().to_string();
+ }
let converter = DocumentConverter::for_config(config);
Ok(body_of(
&record,
@@ -108,7 +115,7 @@ async fn legacy_record(
version.file = Some(tiny_artifacts::FileRef {
size_bytes: meta.size_bytes,
media_type: None,
- path,
+ key: path,
});
let mut record = ArtifactRecord::pending(id, meta.title, ArtifactKind::File, at);
record.versions.push(version);
@@ -188,7 +195,7 @@ pub(crate) async fn body_of(
}
let path = latest
.and_then(|v| v.file.as_ref())
- .map_or(stored, |f| f.path.clone());
+ .map_or(stored, |f| f.key.clone());
preview.body = path.clone();
// Inside the trusted folders, an existing regular file: anything else is
// shown by its path only.
diff --git a/crates/openhuman-core/src/artifacts/preview_tests.rs b/crates/openhuman-core/src/artifacts/preview_tests.rs
index 6ab5f6ff71..bca878d4a6 100644
--- a/crates/openhuman-core/src/artifacts/preview_tests.rs
+++ b/crates/openhuman-core/src/artifacts/preview_tests.rs
@@ -13,7 +13,7 @@ fn file_record(path: &Path) -> ArtifactRecord {
1,
);
version.file = Some(FileRef {
- path: path.display().to_string(),
+ key: path.display().to_string(),
size_bytes: std::fs::metadata(path).map(|m| m.len()).unwrap_or(0),
media_type: None,
});
diff --git a/crates/openhuman-core/src/artifacts/tiny_store.rs b/crates/openhuman-core/src/artifacts/tiny_store.rs
index d8c11aa13c..860b8a2961 100644
--- a/crates/openhuman-core/src/artifacts/tiny_store.rs
+++ b/crates/openhuman-core/src/artifacts/tiny_store.rs
@@ -139,7 +139,8 @@ impl LegacyUiBridge {
== Some(ext.clone())
});
if let Some(owned) = owned {
- let source = Path::new(&file.path);
+ let source_buf = stored_file_path(&self.workspace_dir, file);
+ let source = source_buf.as_path();
let size = if same_file(owned, source) {
tokio::fs::metadata(source).await?.len()
} else {
@@ -170,10 +171,10 @@ impl LegacyUiBridge {
.await
.map_err(anyhow::Error::msg)?;
let size = if record.holds_file() {
- let source = latest
- .file
- .as_ref()
- .map_or_else(|| PathBuf::from(&latest.body), |f| PathBuf::from(&f.path));
+ let source = latest.file.as_ref().map_or_else(
+ || PathBuf::from(&latest.body),
+ |f| stored_file_path(&self.workspace_dir, f),
+ );
if same_file(&source, &dest) {
tokio::fs::metadata(&dest).await?.len()
} else {
@@ -317,6 +318,13 @@ pub fn version_files_dir(workspace_dir: &Path) -> PathBuf {
.join("files")
}
+/// Where a version's stored file lives on disk: its storage key under
+/// [`version_files_dir`]. A key that is an absolute path (a record written
+/// before files were kept behind a store) names that path itself.
+pub fn stored_file_path(workspace_dir: &Path, file: &tiny_artifacts::FileRef) -> PathBuf {
+ version_files_dir(workspace_dir).join(&file.key)
+}
+
/// Build the artifact tools bound to this workspace's service: the crate's
/// default set (`create_artifact`, `read_artifact`, `update_artifact`,
/// `list_artifacts`), `delete_artifact` (default-off through its
diff --git a/crates/openhuman-core/src/artifacts/tiny_store_tests.rs b/crates/openhuman-core/src/artifacts/tiny_store_tests.rs
index 0060f9fa95..d726beb6fc 100644
--- a/crates/openhuman-core/src/artifacts/tiny_store_tests.rs
+++ b/crates/openhuman-core/src/artifacts/tiny_store_tests.rs
@@ -284,3 +284,25 @@ fn a_protected_location_is_refused_under_its_macos_canonical_name() {
assert!(refused_path(Path::new("/private/tmp/notes.md")).is_none());
assert!(refused_path(Path::new("/Users/me/OpenHuman/notes.md")).is_none());
}
+
+#[test]
+fn a_stored_file_key_resolves_under_the_version_files_folder_and_an_absolute_one_stays() {
+ let workspace = Path::new("/ws");
+ let kept = tiny_artifacts::FileRef {
+ key: "artifacts/files/a1/v2.pptx".into(),
+ size_bytes: 1,
+ media_type: None,
+ };
+ assert_eq!(
+ stored_file_path(workspace, &kept),
+ version_files_dir(workspace).join("artifacts/files/a1/v2.pptx")
+ );
+ let legacy = tiny_artifacts::FileRef {
+ key: "/elsewhere/old.pptx".into(),
+ ..kept
+ };
+ assert_eq!(
+ stored_file_path(workspace, &legacy),
+ PathBuf::from("/elsewhere/old.pptx")
+ );
+}
diff --git a/crates/openhuman-embed/src/host_internals.rs b/crates/openhuman-embed/src/host_internals.rs
index 0a66c73ee0..7ba923611c 100644
--- a/crates/openhuman-embed/src/host_internals.rs
+++ b/crates/openhuman-embed/src/host_internals.rs
@@ -298,7 +298,9 @@ pub mod artifact_publisher {
};
// tinyhumans file-storage publisher: the protected-path guard it re-checks
// before upload, and the store its tests save artifacts into.
- pub use openhuman_core::artifacts::tiny_store::{refused_path, versioned_store};
+ pub use openhuman_core::artifacts::tiny_store::{
+ refused_path, version_files_dir, versioned_store,
+ };
}
/// Egress disclosure, for hosted calls that leave the machine.
diff --git a/crates/openhuman-tinyhumans/src/integrations/file_storage/artifact_publisher.rs b/crates/openhuman-tinyhumans/src/integrations/file_storage/artifact_publisher.rs
index 01d00b9456..1115f974c6 100644
--- a/crates/openhuman-tinyhumans/src/integrations/file_storage/artifact_publisher.rs
+++ b/crates/openhuman-tinyhumans/src/integrations/file_storage/artifact_publisher.rs
@@ -12,7 +12,7 @@
//! autonomy, and the publisher only exists when the integration client does
//! (a backend is configured and the user is signed in).
-use std::path::Path;
+use std::path::{Path, PathBuf};
use std::sync::Arc;
use async_trait::async_trait;
@@ -39,6 +39,9 @@ pub struct StorageArtifactPublisher {
/// The turn the backend requests are reported to. A publisher is built
/// once per tool set, not per call, so it reports outside any chat.
approval: ApprovalTurnContext,
+ /// The folder a version's storage key is relative to. `None` reads keys
+ /// as the paths they are (records from before versions were kept).
+ version_files: Option<PathBuf>,
}
impl StorageArtifactPublisher {
@@ -49,8 +52,17 @@ impl StorageArtifactPublisher {
client,
security,
approval: ApprovalTurnContext::default(),
+ version_files: None,
}
}
+
+ /// Resolve a version's file key against `dir`, the folder its copies are
+ /// kept in. An absolute key still names itself.
+ #[must_use]
+ pub fn with_version_files(mut self, dir: impl Into<PathBuf>) -> Self {
+ self.version_files = Some(dir.into());
+ self
+ }
}
/// The publisher for `config`, or `None` when there is no integration client
@@ -63,20 +75,26 @@ pub fn artifact_publisher(config: &Config) -> Option<Arc<dyn ArtifactPublisher>>
&config.workspace_dir,
&config.action_dir,
));
- Some(Arc::new(StorageArtifactPublisher::new(client, security)))
+ Some(Arc::new(
+ StorageArtifactPublisher::new(client, security).with_version_files(
+ openhuman_embed::__host::artifact_publisher::version_files_dir(&config.workspace_dir),
+ ),
+ ))
}
/// The bytes, file name and media type a version uploads as.
async fn upload_body(
record: &ArtifactRecord,
version: &ArtifactVersion,
+ version_files: Option<&Path>,
) -> anyhow::Result<(Vec<u8>, String, &'static str)> {
if record.holds_file() {
let path = version
.file
.as_ref()
- .map_or(version.body.as_str(), |f| f.path.as_str());
- let path = Path::new(path);
+ .map_or(version.body.as_str(), |f| f.key.as_str());
+ let joined = version_files.map(|dir| dir.join(path));
+ let path = joined.as_deref().unwrap_or_else(|| Path::new(path));
// The record was checked when written; check again on what the path
// resolves to now, so nothing protected leaves the machine.
let resolved = tokio::fs::canonicalize(path)
@@ -217,7 +235,8 @@ impl ArtifactPublisher for StorageArtifactPublisher {
if !self.security.can_act() {
anyhow::bail!("[policy-blocked] Action blocked: autonomy is read-only");
}
- let (bytes, filename, mime) = upload_body(record, version).await?;
+ let (bytes, filename, mime) =
+ upload_body(record, version, self.version_files.as_deref()).await?;
tracing::debug!(
artifact_id = %record.id,
version = version.version,
diff --git a/crates/openhuman-tinyhumans/src/integrations/file_storage/artifact_publisher_tests.rs b/crates/openhuman-tinyhumans/src/integrations/file_storage/artifact_publisher_tests.rs
index bddcf2a572..8b9a9dfecf 100644
--- a/crates/openhuman-tinyhumans/src/integrations/file_storage/artifact_publisher_tests.rs
+++ b/crates/openhuman-tinyhumans/src/integrations/file_storage/artifact_publisher_tests.rs
@@ -123,7 +123,7 @@ async fn a_public_file_share_uses_the_public_url_and_makes_no_link() {
1,
);
version.file = Some(FileRef {
- path: file.display().to_string(),
+ key: file.display().to_string(),
size_bytes: 9,
media_type: Some("image/png".into()),
});
@@ -354,7 +354,7 @@ async fn a_file_in_a_protected_location_is_never_uploaded() {
1,
);
version.file = Some(FileRef {
- path: key.display().to_string(),
+ key: key.display().to_string(),
size_bytes: 7,
media_type: None,
});