Skip to content

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

fix: rehydrate finalized media references

Steven Enamakel30391c34056e
diff --git a/crates/openhuman-core/src/media/generation/artifact_tool.rs b/crates/openhuman-core/src/media/generation/artifact_tool.rs
index e33f6d40a5..51faef7985 100644
--- a/crates/openhuman-core/src/media/generation/artifact_tool.rs
+++ b/crates/openhuman-core/src/media/generation/artifact_tool.rs
@@ -239,23 +239,24 @@ impl<T: Tool> MediaArtifactTool<T> {
};
match stored {
Ok(size_bytes) => {
- let filed_path = dest.display().to_string();
+ let canonical = dest.canonicalize().ok();
+ let filed_path = canonical.as_deref().unwrap_or(&dest).display().to_string();
if let Some(obj) = entry.as_object_mut() {
obj.insert("path".into(), json!(filed_path));
}
- if let Some(allowed) = &self.allowed_references {
- if let Ok(canonical) = dest.canonicalize() {
- allowed
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner)
- .insert(canonical);
- }
- }
match self
.finalize_and_discard_blob(&meta.id, size_bytes, chat, src)
.await
{
Ok(updated) => {
+ if let (Some(allowed), Some(canonical)) =
+ (&self.allowed_references, canonical)
+ {
+ allowed
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .insert(canonical);
+ }
if let Some(obj) = entry.as_object_mut() {
obj.insert("artifact_id".into(), json!(updated.id));
}
@@ -319,6 +320,69 @@ impl<T: Tool> MediaArtifactTool<T> {
Ok(finalized)
}
+ /// Rehydrates finalized media paths after a session rebuild. Only Ready
+ /// image/video artifacts whose files still resolve through the configured
+ /// trusted roots enter the exact-path allowlist.
+ async fn refresh_allowed_references(&self) {
+ let Some(allowed) = &self.allowed_references else {
+ return;
+ };
+
+ let mut recovered = std::collections::HashSet::new();
+ let mut offset = 0usize;
+ loop {
+ let (artifacts, total) = match crate::agent::artifacts::store::list_artifacts(
+ &self.workspace_dir,
+ offset,
+ 200,
+ None,
+ )
+ .await
+ {
+ Ok(page) => page,
+ Err(error) => {
+ tracing::warn!(
+ target: "media_generation",
+ %error,
+ "[media_generation] could not refresh reusable artifact paths"
+ );
+ return;
+ }
+ };
+
+ for meta in artifacts.iter().filter(|meta| {
+ matches!(&meta.kind, ArtifactKind::Image | ArtifactKind::Video)
+ && matches!(&meta.status, crate::agent::artifacts::ArtifactStatus::Ready)
+ }) {
+ let Ok(path) = crate::agent::artifacts::resolve_ready_file(
+ &self.workspace_dir,
+ &self.files_dir,
+ &meta.id,
+ )
+ .await
+ else {
+ continue;
+ };
+ if let Ok(path) = tokio::fs::canonicalize(path).await {
+ recovered.insert(path);
+ }
+ }
+
+ let page_len = artifacts.len();
+ offset = offset.saturating_add(page_len);
+ if page_len == 0 || offset >= total {
+ break;
+ }
+ }
+
+ // Merge rather than replace so an artifact finalized concurrently with
+ // this scan cannot be lost from the current session's allowlist.
+ allowed
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .extend(recovered);
+ }
+
async fn file_blob(&self, key: &Path, dest: &Path) -> Result<u64, String> {
let key = key
.to_str()
@@ -466,6 +530,8 @@ impl<T: Tool> Tool for MediaArtifactTool<T> {
options: ToolCallOptions,
context: Option<&dyn ToolRunContext>,
) -> anyhow::Result<ToolResult> {
+ self.refresh_allowed_references().await;
+
// Generation blocks for minutes; report a heartbeat (with the video
// job's polled state) so the wait is visible on the call.
let label = match self.kind {
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 65d087282a..9a16f89de9 100644
--- a/crates/openhuman-core/src/media/generation/artifact_tool_tests.rs
+++ b/crates/openhuman-core/src/media/generation/artifact_tool_tests.rs
@@ -153,17 +153,66 @@ 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()));
+ let canonical_file = std::path::Path::new(&file).canonicalize().unwrap();
+ assert_eq!(
+ artifact["path"].as_str(),
+ Some(canonical_file.to_str().unwrap())
+ );
assert_eq!(
result.markdown_formatted.as_deref(),
- Some(format!("Saved image at {file}\nCost: $0.01").as_str())
+ Some(format!("Saved image at {}\nCost: $0.01", canonical_file.display()).as_str())
);
- assert_eq!(std::fs::read(file).unwrap(), b"blob-media-bytes");
+ assert_eq!(std::fs::read(&file).unwrap(), b"blob-media-bytes");
assert!(blobs
.get("generated-media/stub-0.png")
.await
.unwrap()
.is_none());
+
+ let (_, failed_file) = crate::agent::artifacts::create_artifact_for_call(
+ &workspace,
+ files_dir.clone(),
+ ArtifactKind::Video,
+ "Failed generated clip",
+ "mp4",
+ None,
+ None,
+ )
+ .await
+ .unwrap();
+ std::fs::write(&failed_file, b"failed output").unwrap();
+ let failed = crate::agent::artifacts::store::list_artifacts(&workspace, 0, 10, None)
+ .await
+ .unwrap()
+ .0
+ .into_iter()
+ .find(|meta| meta.title == "Failed generated clip")
+ .expect("failed artifact record");
+ crate::agent::artifacts::fail_artifact(
+ &workspace,
+ files_dir.clone(),
+ &failed.id,
+ "test finalization failure",
+ None,
+ )
+ .await
+ .unwrap();
+
+ let fresh = MediaArtifactTool::new(
+ StubMediaTool {
+ file_count: 0,
+ fail: false,
+ },
+ ArtifactKind::Image,
+ workspace.clone(),
+ files_dir,
+ )
+ .with_blobs(blobs)
+ .with_reference_allowlist(Arc::new(std::sync::Mutex::new(Default::default())));
+ fresh.refresh_allowed_references().await;
+ let allowed = fresh.allowed_references.as_ref().unwrap().lock().unwrap();
+ assert!(allowed.contains(&std::path::PathBuf::from(file).canonicalize().unwrap()));
+ assert!(!allowed.contains(&failed_file.canonicalize().unwrap()));
}
#[tokio::test]
diff --git a/docs/gitbooks/en/features/native-tools/media-generation.md b/docs/gitbooks/en/features/native-tools/media-generation.md
index 38043a4745..a9490ccd69 100644
--- a/docs/gitbooks/en/features/native-tools/media-generation.md
+++ b/docs/gitbooks/en/features/native-tools/media-generation.md
@@ -19,7 +19,7 @@ OpenHuman can make media, not just read it. Ask for "an image of…", "edit this
The `media_generation` domain (`crates/openhuman-core/src/media/generation/`) gives the agent three tools: generate image, generate video and list models. They use the OpenHuman backend's media-generation provider. The backend owns the provider keys, billing and rate limiting, and your subscription covers it like any other model call.
-The tools submit a job and then poll every 4 seconds, for up to 180 seconds for images and 420 seconds for video. You and the agent see live progress instead of a hung call. Finished files are filed in the Files folder and returned as local file paths, ready to attach, post or edit further.
+Images poll every 4 seconds for up to 180 seconds; video polls every 5 seconds for up to 600 seconds. You and the agent see live progress instead of a hung call. Finished files are filed in the Files folder and returned as local file paths, ready to attach, post or edit further.
## Privacy