Skip to content

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

fix: preserve generated media artifact paths

Steven Enamakel674da9b0294e
diff --git a/crates/openhuman-core/src/agent/registry/agents/image_agent/prompt.md b/crates/openhuman-core/src/agent/registry/agents/image_agent/prompt.md
index e39d67b267..f5b1465f90 100644
--- a/crates/openhuman-core/src/agent/registry/agents/image_agent/prompt.md
+++ b/crates/openhuman-core/src/agent/registry/agents/image_agent/prompt.md
@@ -27,7 +27,7 @@ images you generate.
matter. `n` asks for several variants; `seed` makes a result reproducible.
- For edits, pass the source image(s) in `references` and describe the change
precisely.
-- Each generation **saves the image to the workspace and returns a local file
+- Each generation **saves the image to the Files folder and returns a local file
path**. Always report that path back so the deck/answer can reference the
concrete artifact. Do not paste raw base64 or invent URLs.
- Generation is billed. Don't loop on near-identical prompts — generate, inspect
diff --git a/crates/openhuman-core/src/media/generation/artifact_tool.rs b/crates/openhuman-core/src/media/generation/artifact_tool.rs
index 55011d7ab0..abfab4b02c 100644
--- a/crates/openhuman-core/src/media/generation/artifact_tool.rs
+++ b/crates/openhuman-core/src/media/generation/artifact_tool.rs
@@ -221,6 +221,7 @@ impl<T: Tool> MediaArtifactTool<T> {
Ok(updated) => {
if let Some(obj) = entry.as_object_mut() {
obj.insert("artifact_id".into(), json!(updated.id));
+ obj.insert("path".into(), json!(dest.display().to_string()));
}
}
Err(err) => {
@@ -262,26 +263,7 @@ impl<T: Tool> MediaArtifactTool<T> {
.await
.map_err(|error| format!("failed to read generated media blob {key:?}: {error}"))?
.ok_or_else(|| format!("generated media blob {key:?} was not found"))?;
- let mut file = tokio::fs::OpenOptions::new()
- .write(true)
- .truncate(true)
- .open(dest)
- .await
- .map_err(|error| {
- format!(
- "failed to open artifact destination {}: {error}",
- dest.display()
- )
- })?;
- let size = tokio::io::copy(&mut blob.reader, &mut file)
- .await
- .map_err(|error| format!("failed to copy generated media blob {key:?}: {error}"))?;
- file.flush().await.map_err(|error| {
- format!(
- "failed to flush artifact destination {}: {error}",
- dest.display()
- )
- })?;
+ let size = copy_blob_to_file(&mut *blob.reader, dest, key).await?;
if let Err(error) = blobs.delete(key).await {
tracing::warn!(
target: "media_generation",
@@ -294,6 +276,42 @@ impl<T: Tool> MediaArtifactTool<T> {
}
}
+async fn copy_blob_to_file<R>(reader: &mut R, dest: &Path, key: &str) -> Result<u64, String>
+where
+ R: tokio::io::AsyncRead + Unpin + ?Sized,
+{
+ let mut file = tokio::fs::OpenOptions::new()
+ .write(true)
+ .truncate(true)
+ .open(dest)
+ .await
+ .map_err(|error| {
+ format!(
+ "failed to open artifact destination {}: {error}",
+ dest.display()
+ )
+ })?;
+ let size = match tokio::io::copy(reader, &mut file).await {
+ Ok(size) => size,
+ Err(error) => {
+ drop(file);
+ let _ = tokio::fs::remove_file(dest).await;
+ return Err(format!(
+ "failed to copy generated media blob {key:?}: {error}"
+ ));
+ }
+ };
+ if let Err(error) = file.flush().await {
+ drop(file);
+ let _ = tokio::fs::remove_file(dest).await;
+ return Err(format!(
+ "failed to flush artifact destination {}: {error}",
+ dest.display()
+ ));
+ }
+ Ok(size)
+}
+
fn set_artifact_error(entry: &mut Value, message: &str) {
if let Some(obj) = entry.as_object_mut() {
obj.insert("artifact_error".into(), json!(message));
diff --git a/crates/openhuman-core/src/media/generation/artifact_tool_tests.rs b/crates/openhuman-core/src/media/generation/artifact_tool_tests.rs
index cd1e96c2d6..8dfd12a8c1 100644
--- a/crates/openhuman-core/src/media/generation/artifact_tool_tests.rs
+++ b/crates/openhuman-core/src/media/generation/artifact_tool_tests.rs
@@ -5,9 +5,27 @@ use tinyagents_harness::media::MediaOutput;
use tinystoragedrivers::BlobStore;
use tinytools::{PermissionLevel, Tool, ToolCategory, ToolContent, ToolResult};
-use super::MediaArtifactTool;
+use super::{copy_blob_to_file, MediaArtifactTool};
use crate::agent::artifacts::{ArtifactKind, ArtifactStatus};
+struct PartialThenFail(bool);
+
+impl tokio::io::AsyncRead for PartialThenFail {
+ fn poll_read(
+ self: std::pin::Pin<&mut Self>,
+ _cx: &mut std::task::Context<'_>,
+ buf: &mut tokio::io::ReadBuf<'_>,
+ ) -> std::task::Poll<std::io::Result<()>> {
+ let this = self.get_mut();
+ if this.0 {
+ return std::task::Poll::Ready(Err(std::io::Error::other("reader failed")));
+ }
+ this.0 = true;
+ buf.put_slice(b"partial media");
+ std::task::Poll::Ready(Ok(()))
+ }
+}
+
/// A minimal stand-in for `GenerateImageTool` / `GenerateVideoTool`: writes
/// a fixed number of files under `<action_dir>/generated-media/` and reports
/// them in the same `artifacts` array shape the real vendor tools use.
@@ -134,6 +152,7 @@ async fn files_a_blob_backed_generated_artifact() {
.await
.expect("artifact record");
let file = record.file.expect("visible file");
+ assert_eq!(artifact["path"].as_str(), Some(file.as_str()));
assert_eq!(std::fs::read(file).unwrap(), b"blob-media-bytes");
assert!(blobs
.get("generated-media/stub-0.png")
@@ -142,6 +161,24 @@ async fn files_a_blob_backed_generated_artifact() {
.is_none());
}
+#[tokio::test]
+async fn removes_partial_destination_when_blob_read_fails() {
+ let root = tempfile::tempdir().unwrap();
+ let dest = root.path().join("partial.png");
+ std::fs::write(&dest, b"reserved placeholder").unwrap();
+
+ let error = copy_blob_to_file(
+ &mut PartialThenFail(false),
+ &dest,
+ "generated-media/fail.png",
+ )
+ .await
+ .unwrap_err();
+
+ assert!(error.contains("failed to copy generated media blob"));
+ assert!(!dest.exists());
+}
+
#[tokio::test]
async fn files_a_single_generated_artifact() {
let root = tempfile::tempdir().unwrap();
diff --git a/crates/openhuman-core/src/media/generation/tools.rs b/crates/openhuman-core/src/media/generation/tools.rs
index 9a9bc3ffa2..8006f23e3e 100644
--- a/crates/openhuman-core/src/media/generation/tools.rs
+++ b/crates/openhuman-core/src/media/generation/tools.rs
@@ -35,14 +35,14 @@ const VIDEO_WAIT_BUDGET: Duration = Duration::from_secs(600);
const IMAGE_DESCRIPTION: &str = "Generate or edit images from a text prompt via OpenRouter \
(default model: Seedream 5.0 Lite). Pass `references` (https URLs or workspace file paths) \
- to edit, restyle, or keep a subject consistent. Saves each image under the workspace \
- `generated-media/` folder and returns the file path. Billed per call: after an error that \
+ to edit, restyle, or keep a subject consistent. Saves each image in the Files folder and \
+ returns its local file path. Billed per call: after an error that \
says the call was billed, do not call again — report it to the user.";
const VIDEO_DESCRIPTION: &str = "Generate a short video clip via OpenRouter (default model: \
Seedance 2.0 Mini, 4–15 s, 480p/720p, optional audio). Optionally start from \
`first_frame` or end on `last_frame` (URL or workspace path). Blocks until the clip is \
- ready (minutes) and saves it under `generated-media/`. Billed per call: if it times out, \
+ ready (minutes) and saves it in the Files folder. Billed per call: if it times out, \
call again with `resume_job_id` instead of submitting a new job.";
/// Registers the media tools, or nothing when no backend is reachable.