Skip to content

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

feat(presentation): validate request before resolving images

Steven Enamakelc5531545d649
diff --git a/crates/openhuman-core/src/tools/impl/presentation/mod.rs b/crates/openhuman-core/src/tools/impl/presentation/mod.rs
index 2c81a468db..8dc5d8cb1c 100644
--- a/crates/openhuman-core/src/tools/impl/presentation/mod.rs
+++ b/crates/openhuman-core/src/tools/impl/presentation/mod.rs
@@ -2,9 +2,9 @@
//! structured slide spec via the native-Rust [`engine`] module.
//!
//! Flow:
-//! 1. Validate host-owned image references, resolve readable images, then
-//! validate the resolved bus spec so the agent gets a structured
-//! `InvalidInput` it can self-correct on before artifact production.
+//! 1. Validate source-agnostic request metadata through the bus contract before
+//! touching image references, resolve readable images under host policy,
+//! then validate the resolved bus spec before artifact production.
//! 2. Start a [`Production`]: it reserves `<title>.pptx` in the files folder
//! (the card shows "generating…") and opens the versioned artifact — or,
//! with an `artifact_id` arg or a regenerate in scope, a new version of
@@ -50,8 +50,9 @@ mod tests;
use tinydocs_bus::spec::SlideImage;
use self::types::{
- to_bus_spec, validate_image_references, validate_request, GeneratePresentationInput, GeneratePresentationOutput,
- PresentationError, SlideImageRequest, SlideImageSource, MAX_IMAGE_BYTES,
+ to_bus_spec, validate_image_references, validate_request, GeneratePresentationInput,
+ GeneratePresentationOutput, PresentationError, SlideImageRequest, SlideImageSource,
+ MAX_IMAGE_BYTES,
};
/// Generation timeout. `ppt-rs` typically completes the full 64-slide
@@ -70,6 +71,30 @@ trait ImageInspector: Sync {
async fn inspect(&self, bytes: &[u8]) -> Result<tinydocs_bus::ImageFacts, String>;
}
+#[async_trait]
+trait ImageResolver: Sync {
+ async fn resolve(
+ &self,
+ tool: &PresentationTool,
+ input: &GeneratePresentationInput,
+ ctx: &DispatchCtx,
+ ) -> (Vec<Vec<SlideImage>>, Vec<String>);
+}
+
+struct HostImageResolver;
+
+#[async_trait]
+impl ImageResolver for HostImageResolver {
+ async fn resolve(
+ &self,
+ tool: &PresentationTool,
+ input: &GeneratePresentationInput,
+ ctx: &DispatchCtx,
+ ) -> (Vec<Vec<SlideImage>>, Vec<String>) {
+ tool.resolve_images(input, ctx).await
+ }
+}
+
struct ModuleImageInspector<'a> {
config: Option<&'a crate::config::Config>,
ctx: &'a DispatchCtx,
@@ -322,6 +347,17 @@ impl PresentationTool {
args: Value,
chat: Option<&crate::security::approval::ApprovalChatContext>,
ctx: &DispatchCtx,
+ ) -> anyhow::Result<ToolResult> {
+ self.run_with_resolver(args, chat, ctx, &HostImageResolver)
+ .await
+ }
+
+ async fn run_with_resolver(
+ &self,
+ args: Value,
+ chat: Option<&crate::security::approval::ApprovalChatContext>,
+ ctx: &DispatchCtx,
+ resolver: &impl ImageResolver,
) -> anyhow::Result<ToolResult> {
let input: GeneratePresentationInput = match serde_json::from_value(args.clone()) {
Ok(v) => v,
@@ -332,7 +368,8 @@ impl PresentationTool {
}
};
- if let Err(err) = validate_request(&input).and_then(|()| validate_image_references(&input)) {
+ if let Err(err) = validate_request(&input).and_then(|()| validate_image_references(&input))
+ {
tracing::debug!(target: "presentation", err = %err, "[presentation] validation rejected input");
return Ok(ToolResult::error(err.to_string()));
}
@@ -340,7 +377,7 @@ impl PresentationTool {
// Resolve + validate per-slide images at the async boundary. A
// bad image is skipped with a warning (partial success) rather
// than failing the whole deck — mirrors the #3076 ethos.
- let (resolved_images, image_warnings) = self.resolve_images(&input, ctx).await;
+ let (resolved_images, image_warnings) = resolver.resolve(self, &input, ctx).await;
if !image_warnings.is_empty() {
tracing::info!(
target: "presentation",
@@ -402,14 +439,7 @@ impl PresentationTool {
);
}
- let bytes = match engine::generate(
- &input,
- &spec,
- GENERATION_TIMEOUT,
- self.config.as_ref(),
- )
- .await
- {
+ let bytes = match engine::generate(&spec, GENERATION_TIMEOUT, self.config.as_ref()).await {
Ok(bytes) => bytes,
Err(err) => {
production.fail(&err.to_string()).await;
diff --git a/crates/openhuman-core/src/tools/impl/presentation/presentation_tests.rs b/crates/openhuman-core/src/tools/impl/presentation/presentation_tests.rs
index babc4b13a6..e586f1dfb1 100644
--- a/crates/openhuman-core/src/tools/impl/presentation/presentation_tests.rs
+++ b/crates/openhuman-core/src/tools/impl/presentation/presentation_tests.rs
@@ -116,6 +116,77 @@ async fn execute_rejects_too_many_slides() {
);
}
+#[tokio::test]
+async fn request_preflight_rejects_before_image_resolution_or_artifact_creation() {
+ use std::sync::atomic::{AtomicUsize, Ordering};
+
+ struct CountingResolver(AtomicUsize);
+ #[async_trait]
+ impl ImageResolver for CountingResolver {
+ async fn resolve(
+ &self,
+ _tool: &PresentationTool,
+ input: &GeneratePresentationInput,
+ _ctx: &DispatchCtx,
+ ) -> (Vec<Vec<SlideImage>>, Vec<String>) {
+ self.0.fetch_add(1, Ordering::SeqCst);
+ (vec![Vec::new(); input.slides.len()], Vec::new())
+ }
+ }
+
+ let invalid_text = json!({
+ "title": " ",
+ "slides": [{
+ "title": "Slide",
+ "bullets": ["valid"],
+ "images": [{ "source": { "type": "file", "path": "missing.png" } }]
+ }]
+ });
+ let too_many_slides = json!({
+ "title": "Deck",
+ "slides": (0..=MAX_SLIDES)
+ .map(|i| json!({ "title": format!("Slide {i}"), "bullets": ["valid"] }))
+ .collect::<Vec<_>>()
+ });
+ let too_many_failed_references = json!({
+ "title": "Deck",
+ "slides": [{
+ "title": "Slide",
+ "bullets": ["valid"],
+ "images": (0..9).map(|i| json!({
+ "source": { "type": "file", "path": format!("missing-{i}.png") },
+ "caption": "requested caption"
+ })).collect::<Vec<_>>()
+ }]
+ });
+
+ for (args, expected) in [
+ (invalid_text, "title"),
+ (too_many_slides, "64 slides"),
+ (too_many_failed_references, "8 images total"),
+ ] {
+ let ws = workspace();
+ let tool = make_tool(ws.path());
+ let resolver = CountingResolver(AtomicUsize::new(0));
+ let result = tool
+ .run_with_resolver(args, None, &DispatchCtx::ambient(), &resolver)
+ .await
+ .expect("run returns tool result");
+
+ assert!(
+ result.is_error,
+ "invalid request should fail: {}",
+ result.text()
+ );
+ assert!(result.text().contains(expected), "{}", result.text());
+ assert_eq!(resolver.0.load(Ordering::SeqCst), 0, "image resolution ran");
+ assert!(
+ !ws.path().join("Files").exists(),
+ "invalid request created the artifact output directory"
+ );
+ }
+}
+
#[test]
fn resolved_presentation_uses_bus_validation_and_preserves_its_limit_error() {
let input = GeneratePresentationInput {
diff --git a/crates/openhuman-core/src/tools/impl/presentation/types.rs b/crates/openhuman-core/src/tools/impl/presentation/types.rs
index d005fb7756..01366d06a9 100644
--- a/crates/openhuman-core/src/tools/impl/presentation/types.rs
+++ b/crates/openhuman-core/src/tools/impl/presentation/types.rs
@@ -1,7 +1,9 @@
//! Typed input / output / error contracts for the `generate_presentation` tool.
use serde::{Deserialize, Serialize};
-use tinydocs_bus::spec::presentation::{PresentationRequestSpec, PresentationSpec, SlideRequestSpec, SlideSpec};
+use tinydocs_bus::spec::presentation::{
+ PresentationRequestSpec, PresentationSpec, SlideRequestSpec, SlideSpec,
+};
use crate::modules::documents::DocumentCallError;
@@ -229,13 +231,21 @@ pub(super) fn validate_request(input: &GeneratePresentationInput) -> Result<(),
title: input.title.clone(),
author: input.author.clone(),
theme: input.theme.clone(),
- slides: input.slides.iter().map(|slide| SlideRequestSpec {
- title: slide.title.clone(),
- body: slide.body.clone(),
- bullets: slide.bullets.clone(),
- speaker_notes: slide.speaker_notes.clone(),
- images: slide.images.iter().map(|image| image.caption.clone()).collect(),
- }).collect(),
+ slides: input
+ .slides
+ .iter()
+ .map(|slide| SlideRequestSpec {
+ title: slide.title.clone(),
+ body: slide.body.clone(),
+ bullets: slide.bullets.clone(),
+ speaker_notes: slide.speaker_notes.clone(),
+ images: slide
+ .images
+ .iter()
+ .map(|image| image.caption.clone())
+ .collect(),
+ })
+ .collect(),
};
request.validate().map_err(PresentationError::from)
}