Skip to content

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

Merge branch 'epic/7964-channel-v2' of github.com:senamakel/openhuman into epic-7964-547-native-rich-ingress

Steven Enamakel128aca155049
diff --cc crates/openhuman-core/src/agent/queued_turn.rs
index b5e997380f,31c1f60c0f..923797d502
--- a/crates/openhuman-core/src/agent/queued_turn.rs
+++ b/crates/openhuman-core/src/agent/queued_turn.rs
@@@ -83,13 -69,6 +83,19 @@@ impl std::fmt::Debug for QueuedPayload
"has_pending_attachments",
&self.pending_attachments.is_some(),
)
+ .field(
+ "has_native_message_input",
+ &self.native_message_input.is_some(),
+ )
+ .field("native_staged_parts", &self.native_staged_parts.len())
- .field("has_native_staged_lease", &self.native_staged_lease.is_some())
- .field("has_native_preparation_error", &self.native_preparation_error.is_some())
++ .field(
++ "has_native_staged_lease",
++ &self.native_staged_lease.is_some(),
++ )
++ .field(
++ "has_native_preparation_error",
++ &self.native_preparation_error.is_some(),
++ )
.finish_non_exhaustive()
}
}
diff --cc crates/openhuman-core/src/agent/session_host/runtime_session/post_commit.rs
index 75db86e5ff,f7753d4f4d..6c2cf45aad
--- a/crates/openhuman-core/src/agent/session_host/runtime_session/post_commit.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session/post_commit.rs
@@@ -135,43 -135,6 +135,42 @@@ pub(super) async fn finalize_committed_
let commit_started = std::time::Instant::now();
// The turn's own context, taken before the receipt is dropped.
let dispatch = receipt.options.context.dispatch_ctx();
+ if !receipt.options.context.native_staged_parts.is_empty() {
+ if let Some(message_id) = receipt.options.context.native_message_id.as_deref() {
- if let Some(user) = tinyagents_harness::summarization::last_user_message(
- &receipt.outcome.history,
- ) {
++ if let Some(user) =
++ tinyagents_harness::summarization::last_user_message(&receipt.outcome.history)
++ {
+ let committed = crate::threads::attachments::native_parts_in_committed_message(
+ &receipt.options.context.native_staged_parts,
+ user,
+ );
+ if !committed.is_empty() {
+ let thread_id = receipt
+ .options
+ .context
+ .thread_id
+ .as_deref()
+ .unwrap_or_default();
+ match crate::threads::attachments::commit_native_channel_parts(
- &dispatch,
- thread_id,
- &committed,
- message_id,
++ &dispatch, thread_id, &committed, message_id,
+ )
+ .await
+ {
+ Ok(()) => {
- if let Some(lease) = receipt.options.context.native_staged_lease.as_ref() {
++ if let Some(lease) =
++ receipt.options.context.native_staged_lease.as_ref()
++ {
+ lease.mark_committed();
+ }
+ }
+ Err(error) => {
+ tracing::warn!("[session-runtime] native attachment claim failed after commit: {error}");
+ }
+ }
+ }
+ }
+ }
+ }
let output = receipt.outcome.output.clone().unwrap_or_default();
// Skips compaction checkpoints (user-role, not the user's words).
let input = tinyagents_harness::summarization::last_user_message(&receipt.outcome.history)
diff --cc crates/openhuman-core/src/channels/host/channel_events.rs
index 90b6e7641e,a652e810d9..58d2c7987b
--- a/crates/openhuman-core/src/channels/host/channel_events.rs
+++ b/crates/openhuman-core/src/channels/host/channel_events.rs
@@@ -83,8 -83,8 +83,9 @@@ impl EventHandler<DomainEvent> for Chan
&self.client().await,
tinychannels_bus::module_inbound::PrepareChannelInboundRequest {
namespace: "main".into(),
+ preparation_mode: Default::default(),
session_policy: Default::default(),
+ preparation_mode: Default::default(),
source: tinychannels_bus::module_inbound::ChannelInboundSource::Legacy {
message: ChannelMessage {
reply_to_id: None,
diff --cc crates/openhuman-core/src/channels/module_fixture_tests.rs
index b94db16aed,8ddaefba14..b361318d35
--- a/crates/openhuman-core/src/channels/module_fixture_tests.rs
+++ b/crates/openhuman-core/src/channels/module_fixture_tests.rs
@@@ -166,8 -166,8 +166,9 @@@ async fn linked_module_prepares_legacy_
envelope: None,
},
namespace: "openhuman".into(),
+ preparation_mode: Default::default(),
session_policy: Default::default(),
+ preparation_mode: Default::default(),
},),
)
.await
diff --cc crates/openhuman-core/src/channels/providers/relay/ops.rs
index 5957be6dc2,0b7c493972..f988439565
--- a/crates/openhuman-core/src/channels/providers/relay/ops.rs
+++ b/crates/openhuman-core/src/channels/providers/relay/ops.rs
@@@ -263,8 -263,8 +263,9 @@@ pub(crate) async fn run_relay_turn
envelope: None,
},
namespace: "main".into(),
+ preparation_mode: Default::default(),
session_policy: Default::default(),
+ preparation_mode: Default::default(),
},
)
.await
diff --cc crates/openhuman-core/src/channels/runtime/dispatch/processor/native_media.rs
index 8dd1a2f183,0000000000..e872c48a1c
mode 100644,000000..100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/processor/native_media.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/processor/native_media.rs
@@@ -1,457 -1,0 +1,459 @@@
+//! Resolve typed native-channel media through TinyChannels' scoped media port.
+
+use tinybus::module::ModuleClient;
+use tinychannels_bus::{
- ChannelInboundEnvelope, ContentPart, HostVoiceScope, MediaKind, MediaReference,
+ module_media::{MediaReadBegin, MediaReadChunk, MediaReadFinish},
++ ChannelInboundEnvelope, ContentPart, HostVoiceScope, MediaKind, MediaReference,
+};
+use tinyinference_llm::message::Message;
+
+use crate::{
- channels::context::{ChannelRuntimeContext, module_client},
++ channels::context::{module_client, ChannelRuntimeContext},
+ core::runtime::DispatchCtx,
+};
+
+const MAX_MEDIA_BYTES: usize = tinychannels_bus::module_media::MEDIA_UPLOAD_MAX_BYTES;
+
+pub(super) fn requires_typed_preparation(envelope: &ChannelInboundEnvelope) -> bool {
+ envelope.parts.iter().any(|part| {
+ matches!(
+ part,
+ ContentPart::Image { .. }
+ | ContentPart::File { .. }
+ | ContentPart::Audio { .. }
+ | ContentPart::Video { .. }
+ | ContentPart::VoiceNote { .. }
+ )
+ })
+}
+
+/// Resolve only opaque references carried by the trusted module envelope.
+/// Provider URLs and paths are never used as a filesystem or network source.
+pub(super) async fn resolve_typed_message(
+ ctx: &ChannelRuntimeContext,
+ host_dispatch: &DispatchCtx,
+ config: &crate::config::Config,
+ envelope: &ChannelInboundEnvelope,
+ thread_id: &str,
+ channel_context: &str,
+) -> Result<Option<ResolvedNativeMessage>, String> {
+ if !requires_typed_preparation(envelope) {
+ return Ok(None);
+ }
+ validate_native_parts(&envelope.parts)?;
+ let scope = media_scope(envelope)?;
+ let mut parts = envelope.parts.clone();
+ let mut staged_parts = Vec::new();
+ let mut image_count = 0usize;
+ let mut file_count = 0usize;
+ let (max_images, image_mb) = config.multimodal.effective_limits();
+ let (configured_files, file_mb, _) = config.multimodal_files.effective_limits();
+ let max_files = if config.multimodal_files.max_files == 0 {
+ 0
+ } else {
+ configured_files
+ };
+ let image_limit = image_mb.saturating_mul(1024 * 1024).min(MAX_MEDIA_BYTES);
+ let file_limit = file_mb.saturating_mul(1024 * 1024).min(MAX_MEDIA_BYTES);
+ for part in &parts {
+ match part {
+ ContentPart::Image { media, .. } => {
+ image_count += 1;
+ validate_reference(
+ media,
+ true,
+ image_limit,
+ &config.multimodal_files.allowed_mime_types,
+ )?;
+ }
+ ContentPart::File { media, .. } => {
+ file_count += 1;
+ validate_reference(
+ media,
+ false,
+ file_limit,
+ &config.multimodal_files.allowed_mime_types,
+ )?;
+ }
+ _ => {}
+ }
+ }
+ if (config.multimodal.max_images == 0 && image_count > 0) || image_count > max_images {
+ return Err("native image attachments exceed their count limit".into());
+ }
+ if file_count > max_files {
+ return Err("native file attachments are disabled or exceed their count limit".into());
+ }
+ let client = module_client(ctx).await;
+ let module_dispatch = ctx.dispatch_ctx();
+ let mut media_payloads = Vec::new();
+ for (index, part) in parts.iter_mut().enumerate() {
+ let (media, image, name) = match part {
+ ContentPart::Image { media, .. } => (
+ media,
+ true,
+ media.file_name.clone().unwrap_or_else(|| "image".into()),
+ ),
+ ContentPart::File {
+ media, file_name, ..
+ } => (
+ media,
+ false,
+ file_name
+ .clone()
+ .or_else(|| media.file_name.clone())
+ .unwrap_or_else(|| "attachment".into()),
+ ),
+ ContentPart::Audio { .. } | ContentPart::Video { .. } => {
+ return Err("native audio/video attachments are not supported".into());
+ }
+ ContentPart::VoiceNote { voice_note } => {
+ let transcript = voice_note
+ .transcript
+ .as_ref()
+ .expect("validated transcript");
+ let replacement = ContentPart::Text {
+ text: transcript.text.trim().to_owned(),
+ markdown_dialect: None,
+ };
+ *part = replacement;
+ continue;
+ }
+ ContentPart::Text { .. } => continue,
+ _ => return Err("native channel content kind is not supported".into()),
+ };
+ let byte_limit = if image { image_limit } else { file_limit };
+ let bytes = read_scoped_media(
+ &client,
+ &module_dispatch,
+ envelope.channel.id.as_str(),
+ &scope,
+ media,
+ byte_limit,
+ )
+ .await?;
+ let mime = media
+ .content_type
+ .as_deref()
+ .ok_or_else(|| "native media has no content type".to_owned())?;
+ media_payloads.push((index, part.clone(), name, mime.to_owned(), bytes));
+ }
+ for (index, input, name, mime, bytes) in media_payloads {
+ let staged = match crate::threads::attachments::stage_native_channel_part(
+ host_dispatch,
+ thread_id,
+ config,
+ &input,
+ &name,
+ &mime,
+ &bytes,
+ )
+ .await
+ {
+ Ok(staged) => staged,
+ Err(error) => {
+ let _ = crate::threads::attachments::discard_native_channel_parts(
+ host_dispatch,
+ thread_id,
+ config,
+ &staged_parts,
+ )
+ .await;
+ return Err(error);
+ }
+ };
+ let opaque_id = media_opaque_id(&staged)
+ .ok_or_else(|| "staged native media has no host reference".to_owned())?;
+ let mut model_part = staged.clone();
+ let model_media = match &mut model_part {
+ ContentPart::Image { media, .. } | ContentPart::File { media, .. } => media,
+ _ => return Err("native staged media kind changed unexpectedly".into()),
+ };
+ model_media.path = Some(format!("openhuman-staged://{opaque_id}"));
+ parts[index] = model_part;
+ staged_parts.push(staged);
+ }
+
- let message = match crate::agent::message_convert::user_message_from_parts(
- channel_context,
- &parts,
- ) {
- Ok(message) => message,
- Err(error) => {
- let _ = crate::threads::attachments::discard_native_channel_parts(
- host_dispatch,
- thread_id,
- config,
- &staged_parts,
- )
- .await;
- return Err(error);
- }
- };
++ let message =
++ match crate::agent::message_convert::user_message_from_parts(channel_context, &parts) {
++ Ok(message) => message,
++ Err(error) => {
++ let _ = crate::threads::attachments::discard_native_channel_parts(
++ host_dispatch,
++ thread_id,
++ config,
++ &staged_parts,
++ )
++ .await;
++ return Err(error);
++ }
++ };
+ Ok(Some(ResolvedNativeMessage {
+ message,
+ staged_parts,
+ }))
+}
+
+fn media_opaque_id(part: &ContentPart) -> Option<&str> {
+ match part {
+ ContentPart::Image { media, .. } | ContentPart::File { media, .. } => {
+ media.opaque_id.as_deref()
+ }
+ _ => None,
+ }
+}
+
+pub(super) struct ResolvedNativeMessage {
+ pub(super) message: Message,
+ pub(super) staged_parts: Vec<ContentPart>,
+}
+
+fn media_scope(envelope: &ChannelInboundEnvelope) -> Result<HostVoiceScope, String> {
+ let configured_instance = envelope
+ .channel
+ .account_id
+ .as_deref()
+ .filter(|account| !account.trim().is_empty())
+ .ok_or_else(|| "native media has no configured account scope".to_owned())?;
+ if envelope.channel.id.trim().is_empty()
+ || envelope.conversation.id.trim().is_empty()
+ || envelope.message_id.trim().is_empty()
+ {
+ return Err("native media has an incomplete conversation scope".into());
+ }
+ Ok(HostVoiceScope {
+ configured_instance: configured_instance.to_owned(),
+ channel: envelope.channel.clone(),
+ conversation: envelope.conversation.clone(),
+ turn_id: envelope.message_id.clone(),
+ })
+}
+
+fn validate_native_parts(parts: &[ContentPart]) -> Result<(), String> {
+ for part in parts {
+ match part {
+ ContentPart::Text { .. } | ContentPart::Image { .. } | ContentPart::File { .. } => {}
+ ContentPart::VoiceNote { voice_note } => {
+ let transcript = voice_note
+ .transcript
+ .as_ref()
+ .filter(|transcript| {
+ transcript.state == tinychannels_bus::VoiceTranscriptState::Complete
+ })
+ .map(|transcript| transcript.text.trim())
+ .filter(|text| !text.is_empty());
+ if transcript.is_none() {
+ return Err("native voice transcript is unavailable".into());
+ }
+ }
+ ContentPart::Audio { .. } | ContentPart::Video { .. } => {
+ return Err("native audio/video attachments are not supported".into());
+ }
+ _ => return Err("native channel content kind is not supported".into()),
+ }
+ }
+ Ok(())
+}
+
+fn validate_reference(
+ media: &MediaReference,
+ image: bool,
+ byte_limit: usize,
+ allowed_file_mimes: &[String],
+) -> Result<(), String> {
+ let opaque_id = media
+ .opaque_id
+ .as_deref()
+ .filter(|id| !id.trim().is_empty())
+ .ok_or_else(|| "native media reference is missing its opaque id".to_owned())?;
+ if opaque_id.len() > tinychannels_bus::module_media::MEDIA_UPLOAD_BEGIN_MAX_BYTES
+ || media.path.is_some()
+ || media.url.is_some()
+ || media.local_cache_path.is_some()
+ {
+ return Err("native media reference is not an opaque module reference".into());
+ }
+ if media
+ .size_bytes
+ .is_some_and(|size| size == 0 || size > byte_limit as u64)
+ {
+ return Err("native media exceeds its declared byte limit".into());
+ }
+ let mime = media
+ .content_type
+ .as_deref()
+ .filter(|mime| !mime.trim().is_empty())
+ .ok_or_else(|| "native media has no content type".to_owned())?;
+ if image {
+ if media.kind != MediaKind::Image {
+ return Err("native media kind does not match its image part".into());
+ }
+ if !matches!(
+ mime,
+ "image/png" | "image/jpeg" | "image/webp" | "image/gif"
+ ) {
+ return Err("native image MIME type is unsupported".into());
+ }
+ } else {
+ if !matches!(media.kind, MediaKind::Document | MediaKind::Unknown) {
+ return Err("native media kind does not match its file part".into());
+ }
+ if !allowed_file_mimes.iter().any(|allowed| allowed == mime) {
+ return Err("native file MIME type is unsupported".into());
+ }
+ }
+ Ok(())
+}
+
- fn validate_actual_media_size(actual: usize, declared: usize, maximum: usize) -> Result<(), String> {
++fn validate_actual_media_size(
++ actual: usize,
++ declared: usize,
++ maximum: usize,
++) -> Result<(), String> {
+ if actual == 0 || actual != declared || actual > maximum || actual > MAX_MEDIA_BYTES {
+ return Err("native media bytes exceed or disagree with their declared limit".into());
+ }
+ Ok(())
+}
+
+async fn read_scoped_media(
+ client: &ModuleClient,
+ dispatch: &DispatchCtx,
+ instance: &str,
+ scope: &HostVoiceScope,
+ media: &MediaReference,
+ max_bytes: usize,
+) -> Result<Vec<u8>, String> {
+ let begin = client
+ .call_confidential::<tinychannels_bus::MediaReadInfo>(
+ "tinychannels",
+ tinychannels_bus::methods::BEGIN_MEDIA_READ,
+ (MediaReadBegin {
+ instance: instance.to_owned(),
+ scope: scope.clone(),
+ media: media.clone(),
+ max_bytes: max_bytes as u64,
+ },),
+ )
+ .await
+ .map_err(|_| "native media could not be resolved in this turn".to_owned())?;
+ let mut lease = MediaReadLease::new(client.clone(), dispatch.clone(), begin.handle.clone());
+ if begin.byte_count == 0
+ || begin.byte_count > max_bytes
+ || begin.byte_count > MAX_MEDIA_BYTES
+ || begin.media.opaque_id != media.opaque_id
+ || begin.media.kind != media.kind
+ || begin.media.path.is_some()
+ || begin.media.url.is_some()
+ || begin.media.local_cache_path.is_some()
+ || begin.content_type != media.content_type.as_deref().unwrap_or_default()
+ || media
+ .size_bytes
+ .is_some_and(|size| size != begin.byte_count as u64)
+ {
+ return Err("native media read metadata does not match its scoped reference".into());
+ }
+ let expected_chunks = begin
+ .byte_count
+ .div_ceil(tinychannels_bus::module_media::MEDIA_READ_CHUNK_BYTES);
+ if begin.chunk_count as usize != expected_chunks {
+ return Err("native media read chunk count is invalid".into());
+ }
+ let mut bytes = Vec::with_capacity(begin.byte_count);
+ for sequence in 0..begin.chunk_count {
+ let chunk = client
+ .call_confidential::<tinychannels_bus::MediaReadChunkResult>(
+ "tinychannels",
+ tinychannels_bus::methods::READ_MEDIA_CHUNK,
+ (MediaReadChunk {
+ handle: begin.handle.clone(),
+ sequence,
+ },),
+ )
+ .await
+ .map_err(|_| "native media read was interrupted".to_owned())?;
+ let expected_done = sequence + 1 == begin.chunk_count;
+ if chunk.sequence != sequence || chunk.done != expected_done {
+ return Err("native media read returned an invalid chunk sequence".into());
+ }
+ if chunk.bytes.len() > tinychannels_bus::module_media::MEDIA_READ_CHUNK_BYTES {
+ return Err("native media read returned an oversized chunk".into());
+ }
+ if bytes.len().saturating_add(chunk.bytes.len()) > begin.byte_count
+ || bytes.len().saturating_add(chunk.bytes.len()) > max_bytes
+ {
+ return Err("native media bytes exceed their declared limit".into());
+ }
+ bytes.extend(chunk.bytes);
+ }
+ validate_actual_media_size(bytes.len(), begin.byte_count, max_bytes)?;
+ lease.abort().await?;
+ Ok(bytes)
+}
+
+struct MediaReadLease {
+ client: ModuleClient,
+ dispatch: DispatchCtx,
+ handle: Option<tinychannels_bus::MediaReadHandle>,
+}
+
+impl MediaReadLease {
+ fn new(
+ client: ModuleClient,
+ dispatch: DispatchCtx,
+ handle: tinychannels_bus::MediaReadHandle,
+ ) -> Self {
+ Self {
+ client,
+ dispatch,
+ handle: Some(handle),
+ }
+ }
+
+ async fn abort(&mut self) -> Result<(), String> {
+ let Some(handle) = self.handle.as_ref().cloned() else {
+ return Ok(());
+ };
+ self.client
+ .call_confidential::<()>(
+ "tinychannels",
+ tinychannels_bus::methods::ABORT_MEDIA_READ,
+ (MediaReadFinish(handle),),
+ )
+ .await
+ .map_err(|_| "native media read cleanup failed".to_owned())?;
+ self.handle = None;
+ Ok(())
+ }
+}
+
+impl Drop for MediaReadLease {
+ fn drop(&mut self) {
+ let Some(handle) = self.handle.take() else {
+ return;
+ };
+ let client = self.client.clone();
+ self.dispatch.spawn(async move {
+ let _ = client
+ .call_confidential::<()>(
+ "tinychannels",
+ tinychannels_bus::methods::ABORT_MEDIA_READ,
+ (MediaReadFinish(handle),),
+ )
+ .await;
+ });
+ }
+}
+
+#[cfg(test)]
+pub(crate) fn drop_media_read_lease_for_test(
+ client: ModuleClient,
+ dispatch: DispatchCtx,
+ handle: tinychannels_bus::MediaReadHandle,
+) {
+ drop(MediaReadLease::new(client, dispatch, handle));
+}
+
+#[cfg(test)]
+#[path = "native_media_tests.rs"]
+mod tests;
diff --cc crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
index 70a3660f49,0b68b13a7e..220e2c8597
--- a/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
@@@ -15,25 -15,31 +15,43 @@@ use tokio_util::sync::CancellationToken
use tokio_util::task::AbortOnDropHandle;
use super::super::helpers::{
- REPLY_LOG_TRUNCATE_CHARS, build_channel_context_block, log_worker_join_result,
- spawn_typing_task,
+ build_channel_context_block, log_worker_join_result, spawn_typing_task,
+ REPLY_LOG_TRUNCATE_CHARS,
};
use super::super::host_agent::{self, HostRoute};
- use super::RuntimeChannelMessage;
use super::approval::{channel_has_approval_surface, try_route_approval_reply};
+ use super::RuntimeChannelMessage;
+fn channel_inbound_preparation_request(
+ source: tinychannels_bus::module_inbound::ChannelInboundSource,
+ preparation_mode: tinychannels_bus::module_inbound::ChannelInboundPreparationMode,
+) -> tinychannels_bus::module_inbound::PrepareChannelInboundRequest {
+ tinychannels_bus::module_inbound::PrepareChannelInboundRequest {
+ source,
+ namespace: "main".into(),
+ session_policy: Default::default(),
+ preparation_mode,
+ }
+}
+
+ fn inbound_console_preview(
+ envelope: &tinychannels_bus::ChannelInboundEnvelope,
+ text: &str,
+ ) -> String {
+ let contains_audio = envelope.parts.iter().any(|part| {
+ matches!(
+ part,
+ tinychannels_bus::ContentPart::VoiceNote { .. }
+ | tinychannels_bus::ContentPart::Audio { .. }
+ )
+ });
+ if contains_audio {
+ "[voice message]".to_owned()
+ } else {
+ truncate_with_ellipsis(text, 80)
+ }
+ }
+
pub(crate) async fn process_channel_runtime_message(
ctx: Arc<ChannelRuntimeContext>,
runtime_msg: RuntimeChannelMessage,
@@@ -93,8 -93,22 +111,23 @@@
let msg = prepared.message;
let history_key = prepared.history_key;
let canonical_session_key = prepared.session_key.clone();
+ let prepared_thread_id = prepared.thread_id.clone();
let mut envelope = prepared.envelope;
+ tracing::debug!(
+ provider_matches_account = envelope
+ .channel
+ .account_id
+ .as_deref()
+ .is_some_and(|account| tinychannels_bus::provider_id(account) == envelope.channel.id),
+ account_present = envelope
+ .channel
+ .account_id
+ .as_deref()
+ .is_some_and(|account| !account.trim().is_empty()),
+ conversation_present = !envelope.conversation.id.trim().is_empty(),
+ message_present = !envelope.message_id.trim().is_empty(),
+ "prepared inbound scope for native channel turn"
+ );
envelope.access.delivered_via_upstream_relay = authenticated_relay;
let remote_reset_scope = tinychannels_bus::module_remote::RemoteSessionScope {
channel: envelope.channel.clone(),
@@@ -374,36 -387,6 +406,37 @@@
}
None => None,
};
+ let native_preparation = if typed_envelope_input {
+ if let Some(host) = selected_host.as_ref() {
- let host_dispatch = crate::core::runtime::DispatchCtx::new(Arc::clone(&host.context));
- match super::native_media::resolve_typed_message(
- &ctx,
- &host_dispatch,
- &host.config,
- &envelope,
- &canonical_session_key,
- &channel_context,
- )
- .await
- {
- Ok(input) => Some(Ok(input)),
- Err(error) => {
- tracing::warn!("[dispatch] typed native media was rejected safely");
- Some(Err(error))
++ let host_dispatch = crate::core::runtime::DispatchCtx::new(Arc::clone(&host.context));
++ match super::native_media::resolve_typed_message(
++ &ctx,
++ &host_dispatch,
++ &host.config,
++ &envelope,
++ &canonical_session_key,
++ &channel_context,
++ )
++ .await
++ {
++ Ok(input) => Some(Ok(input)),
++ Err(error) => {
++ tracing::warn!("[dispatch] typed native media was rejected safely");
++ Some(Err(error))
++ }
+ }
- }
+ } else {
+ Some(Err("native media has no selected host agent".to_owned()))
+ }
+ } else {
+ None
+ };
- let (native_message_input, native_staged_parts, native_preparation_error) = match native_preparation {
- Some(Ok(Some(prepared))) => (Some(prepared.message), prepared.staged_parts, None),
- Some(Ok(None)) | None => (None, Vec::new(), None),
- Some(Err(error)) => (None, Vec::new(), Some(error)),
- };
++ let (native_message_input, native_staged_parts, native_preparation_error) =
++ match native_preparation {
++ Some(Ok(Some(prepared))) => (Some(prepared.message), prepared.staged_parts, None),
++ Some(Ok(None)) | None => (None, Vec::new(), None),
++ Some(Err(error)) => (None, Vec::new(), Some(error)),
++ };
let channel_session = selected_host.as_ref().and_then(|host| {
let account_id = envelope
.channel
diff --cc crates/openhuman-core/src/channels/runtime/dispatch/processor/turn_tests.rs
index b6989b822e,1560afe2f8..f9f4f5e8c0
--- a/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn_tests.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn_tests.rs
@@@ -1,176 -1,31 +1,206 @@@
use super::*;
+#[tokio::test]
+async fn native_inbound_preparation_preserves_image_envelope_for_typed_turns() {
+ use tinychannels_bus::{
+ channel::{
+ ChannelRef, ContentPart, ConversationKind, ConversationRef, MediaKind, MediaReference,
+ SenderRef,
+ },
+ module_inbound::{ChannelInboundSource, PreparedChannelInbound},
+ };
+
+ let envelope = tinychannels_bus::ChannelInboundEnvelope {
+ channel: ChannelRef {
+ id: "telegram".into(),
+ account_id: Some("work".into()),
+ },
+ message_id: "provider-message-7".into(),
+ conversation: ConversationRef {
+ kind: ConversationKind::Dm,
+ id: "chat-12".into(),
+ thread_id: Some("thread-3".into()),
+ topic_id: Some("topic-4".into()),
+ ..Default::default()
+ },
+ sender: SenderRef {
+ id: "sender-2".into(),
+ ..Default::default()
+ },
+ text: String::new(),
+ parts: vec![ContentPart::Image {
+ media: MediaReference {
+ opaque_id: Some("opaque-module-media".into()),
+ content_type: Some("image/png".into()),
+ size_bytes: Some(64),
+ kind: MediaKind::Image,
+ ..Default::default()
+ },
+ caption: None,
+ alt_text: Some("diagram".into()),
+ }],
+ ..Default::default()
+ };
+
+ let request = channel_inbound_preparation_request(
+ ChannelInboundSource::Envelope {
+ envelope: envelope.clone(),
+ timestamp: 1,
+ },
+ tinychannels_bus::module_inbound::ChannelInboundPreparationMode::Typed,
+ );
+ let prepared: PreparedChannelInbound = crate::modules::channel_controller::prepare_inbound(
+ &crate::channels::module_fixture::client().await,
+ request,
+ )
+ .await
+ .expect("native channel preparation must retain typed image input");
+
+ assert_eq!(prepared.envelope.parts, envelope.parts);
+ assert_eq!(prepared.envelope.channel, envelope.channel);
+ assert_eq!(prepared.envelope.conversation, envelope.conversation);
+ assert_eq!(prepared.envelope.message_id, "provider-message-7");
+ assert!(prepared.message.content.is_empty());
+
+ let legacy = crate::modules::channel_controller::prepare_inbound(
+ &crate::channels::module_fixture::client().await,
+ channel_inbound_preparation_request(
+ ChannelInboundSource::Envelope {
+ envelope,
+ timestamp: 1,
+ },
+ Default::default(),
+ ),
+ )
+ .await;
+ assert!(
+ legacy.is_err(),
+ "legacy callers must still reject rich-only input"
+ );
+}
+
+#[tokio::test]
+async fn native_typed_preparation_preserves_ordered_voice_and_file_parts() {
+ use tinychannels_bus::{
+ channel::{
+ ChannelRef, ContentPart, ConversationKind, ConversationRef, MediaKind, MediaReference,
+ SenderRef, VoiceNote, VoiceTranscript, VoiceTranscriptSource, VoiceTranscriptState,
+ },
+ module_inbound::{ChannelInboundPreparationMode, ChannelInboundSource},
+ };
+
+ let envelope = tinychannels_bus::ChannelInboundEnvelope {
+ channel: ChannelRef {
+ id: "telegram".into(),
+ account_id: Some("work".into()),
+ },
+ message_id: "provider-message-voice-file".into(),
+ conversation: ConversationRef {
+ kind: ConversationKind::Dm,
+ id: "chat-12".into(),
+ thread_id: Some("thread-3".into()),
+ topic_id: Some("topic-4".into()),
+ ..Default::default()
+ },
+ sender: SenderRef {
+ id: "sender-2".into(),
+ ..Default::default()
+ },
+ text: "before".into(),
+ parts: vec![
+ ContentPart::Text {
+ text: "before".into(),
+ markdown_dialect: None,
+ },
+ ContentPart::VoiceNote {
+ voice_note: VoiceNote {
+ transcript: Some(VoiceTranscript {
+ text: "spoken words".into(),
+ language: Some("en".into()),
+ state: VoiceTranscriptState::Complete,
+ source: VoiceTranscriptSource::HostStt,
+ }),
+ ..Default::default()
+ },
+ },
+ ContentPart::File {
+ media: MediaReference {
+ opaque_id: Some("scoped-file-ref".into()),
+ file_name: Some("notes.txt".into()),
+ content_type: Some("text/plain".into()),
+ kind: MediaKind::Document,
+ ..Default::default()
+ },
+ file_name: Some("notes.txt".into()),
+ caption: Some("meeting notes".into()),
+ },
+ ContentPart::Text {
+ text: "after".into(),
+ markdown_dialect: None,
+ },
+ ],
+ ..Default::default()
+ };
+
+ let prepared = crate::modules::channel_controller::prepare_inbound(
+ &crate::channels::module_fixture::client().await,
+ channel_inbound_preparation_request(
+ ChannelInboundSource::Envelope {
+ envelope: envelope.clone(),
+ timestamp: 2,
+ },
+ ChannelInboundPreparationMode::Typed,
+ ),
+ )
+ .await
+ .expect("typed preparation retains the full provider envelope");
+
+ assert_eq!(prepared.envelope, envelope);
+ assert_eq!(prepared.message.content, "before\nspoken words\nafter");
+ assert_eq!(
+ prepared.envelope.channel.account_id.as_deref(),
+ Some("work")
+ );
+ assert_eq!(
+ prepared.envelope.conversation.thread_id.as_deref(),
+ Some("thread-3")
+ );
+ assert_eq!(
+ prepared.envelope.conversation.topic_id.as_deref(),
+ Some("topic-4")
+ );
+ assert!(matches!(
+ prepared.envelope.parts.get(2),
+ Some(ContentPart::File { media, .. }) if media.opaque_id.as_deref() == Some("scoped-file-ref")
+ ));
+}
++
+ #[test]
+ fn voice_message_console_preview_never_includes_transcript_text() {
+ let envelope = tinychannels_bus::ChannelInboundEnvelope {
+ parts: vec![tinychannels_bus::ContentPart::VoiceNote {
+ voice_note: tinychannels_bus::VoiceNote {
+ transcript: Some(tinychannels_bus::VoiceTranscript {
+ text: "private dictated words".into(),
+ language: Some("en".into()),
+ state: tinychannels_bus::VoiceTranscriptState::Complete,
+ source: tinychannels_bus::VoiceTranscriptSource::HostStt,
+ }),
+ ..Default::default()
+ },
+ }],
+ ..Default::default()
+ };
+
+ assert_eq!(
+ inbound_console_preview(&envelope, "private dictated words"),
+ "[voice message]"
+ );
+ }
+
+ #[test]
+ fn ordinary_text_console_preview_remains_readable() {
+ let envelope = tinychannels_bus::ChannelInboundEnvelope::default();
+
+ assert_eq!(inbound_console_preview(&envelope, "hello"), "hello");
+ }
diff --cc crates/openhuman-core/src/channels/runtime/startup/module_providers_tests.rs
index 0c3c5196b4,ccfe538548..a2a26b4502
--- a/crates/openhuman-core/src/channels/runtime/startup/module_providers_tests.rs
+++ b/crates/openhuman-core/src/channels/runtime/startup/module_providers_tests.rs
@@@ -352,181 -407,422 +407,597 @@@ async fn compiled_module_hosted_progres
.expect("hosted module runtime should close after the turn");
}
+ #[tokio::test]
+ async fn hosted_target_projects_to_the_canonical_thread_and_keeps_one_gateway_terminal() {
+ use crate::core::runtime::{ContextOverlay, CoreContext, DispatchCtx, DomainSet};
+ use crate::tools::toolpacks::ToolGroups;
+ let _env_lock = EnvVarGuard::locked_async().await;
+ let _storage = crate::channels::module_fixture::install_memory_storage();
+ crate::channels::module_fixture::client().await;
+
+ let request_id = "relay-target-projection-request";
+ let client_id = "relay-target-projection-client";
+ let thread_id = "channel:telegram/sender-7/chat-42";
+ let mut web_events = crate::web_chat::subscribe_web_channel_events();
+ let relay = Arc::new(crate::channels::providers::relay::RelayChannel::new(
+ "telegram",
+ client_id,
+ thread_id,
+ request_id,
+ "provider-message-9",
+ ));
+ let channel = tinychannels_bus::ChannelRef {
+ id: "telegram".into(),
+ account_id: Some("trusted-account-7".into()),
+ };
+ let conversation = tinychannels_bus::ConversationRef {
+ kind: tinychannels_bus::ConversationKind::Group,
+ id: "chat-42".into(),
+ thread_id: Some("topic-3".into()),
+ topic_id: Some("topic-3".into()),
+ ..Default::default()
+ };
+ let config = Config::default();
+ let mut overlay = ContextOverlay::new(config.clone(), DomainSet::kernel(), ToolGroups::none())
+ .session_agent("relay-target-agent");
+ overlay.profile = Some("relay-target-profile".into());
+ let ctx = DispatchCtx::new(CoreContext::for_test(DomainSet::full(), None).derive_with(overlay));
+ let target = crate::channels::runtime::dispatch::hosted_relay_target(
+ &ctx,
+ &config,
+ Arc::clone(&relay),
+ channel,
+ conversation,
+ client_id.into(),
+ thread_id.into(),
+ request_id.into(),
+ );
+ let reply = target
+ .open("relay-target-projection-turn")
+ .await
+ .expect("hosted target opens the compiled progressive module");
+ let delivery = reply
+ .finish("projected hosted reply")
+ .await
+ .expect("compiled relay output is delivered");
+ assert!(crate::channels::runtime::dispatch::delivery_succeeded(
+ &delivery
+ ));
+ assert_eq!(relay.sent(), vec!["projected hosted reply"]);
+
+ let mut projected = None;
+ let mut gateway_terminal = None;
+ let both_events = tokio::time::timeout(Duration::from_secs(5), async {
+ while projected.is_none() || gateway_terminal.is_none() {
+ let event = web_events.recv().await.expect("web event bus");
+ if event.context().request_id != request_id {
+ continue;
+ }
+ match event.event_name() {
+ "chat_done" => projected = Some(event),
+ crate::channels::providers::relay::CHANNEL_OUTBOUND_EVENT => {
+ gateway_terminal = Some(event)
+ }
+ _ => panic!("unexpected relayed output event: {}", event.event_name()),
+ }
+ }
+ })
+ .await;
+ if both_events.is_err() {
+ assert!(
+ gateway_terminal.is_some(),
+ "the scoped provider terminal should still arrive independently"
+ );
+ panic!("provider terminal arrived, but the web projector did not emit chat_done");
+ }
+
+ let projected = projected.expect("canonical projector output");
+ let projected_wire = serde_json::to_value(&projected).expect("web renderer wire");
+ assert_eq!(projected.context().client_id, client_id);
+ assert_eq!(projected.context().thread_id, thread_id);
+ assert_eq!(projected.context().seq, Some(1));
+ assert!(projected.belongs_to("relay-target-agent"));
+ assert!(projected.belongs_to_profile("relay-target-profile"));
+ assert_eq!(projected_wire["full_response"], "projected hosted reply");
+
+ let gateway_terminal = gateway_terminal.expect("legacy SaaS terminal");
+ let terminal_wire = serde_json::to_value(&gateway_terminal).expect("gateway wire");
+ assert_eq!(gateway_terminal.context().client_id, client_id);
+ assert_eq!(gateway_terminal.context().thread_id, thread_id);
+ assert!(gateway_terminal.belongs_to("relay-target-agent"));
+ assert!(gateway_terminal.belongs_to_profile("relay-target-profile"));
+ assert_eq!(terminal_wire["structured"]["kind"], "channel_outbound");
+ assert_eq!(relay.sent().len(), 1, "one provider terminal is published");
+
+ while let Ok(event) = web_events.try_recv() {
+ assert_ne!(
+ event.context().request_id,
+ request_id,
+ "no duplicate output"
+ );
+ }
+ reply
+ .close()
+ .await
+ .expect("hosted projection runtime closes");
+ }
+
+ #[tokio::test]
+ async fn hosted_target_projects_typed_media_and_voice_to_web_and_gateway_once() {
+ use crate::core::runtime::{ContextOverlay, CoreContext, DispatchCtx, DomainSet};
+ use crate::tools::toolpacks::ToolGroups;
+ use tinychannels_bus::{ContentPart, MediaKind, MediaReference, VoiceNote};
+
+ let _env_lock = EnvVarGuard::locked_async().await;
+ let _storage = crate::channels::module_fixture::install_memory_storage();
+ crate::channels::module_fixture::client().await;
+
+ let request_id = "relay-typed-output-request";
+ let client_id = "relay-typed-output-client";
+ let thread_id = "channel:telegram/sender-7/chat-42";
+ let mut web_events = crate::web_chat::subscribe_web_channel_events();
+ let relay = Arc::new(crate::channels::providers::relay::RelayChannel::new(
+ "telegram",
+ client_id,
+ thread_id,
+ request_id,
+ "provider-message-9",
+ ));
+ let channel = tinychannels_bus::ChannelRef {
+ id: "telegram".into(),
+ account_id: Some("trusted-account-7".into()),
+ };
+ let conversation = tinychannels_bus::ConversationRef {
+ kind: tinychannels_bus::ConversationKind::Group,
+ id: "chat-42".into(),
+ thread_id: Some("topic-3".into()),
+ topic_id: Some("topic-3".into()),
+ ..Default::default()
+ };
+ let config = Config::default();
+ let mut overlay = ContextOverlay::new(config.clone(), DomainSet::kernel(), ToolGroups::none())
+ .session_agent("relay-typed-output-agent");
+ overlay.profile = Some("relay-typed-output-profile".into());
+ let ctx = DispatchCtx::new(CoreContext::for_test(DomainSet::full(), None).derive_with(overlay));
+ let target = crate::channels::runtime::dispatch::hosted_relay_target(
+ &ctx,
+ &config,
+ Arc::clone(&relay),
+ channel.clone(),
+ conversation.clone(),
+ client_id.into(),
+ thread_id.into(),
+ request_id.into(),
+ );
+ let reply = target
+ .open("relay-typed-output-turn")
+ .await
+ .expect("hosted target opens the compiled progressive module");
+
+ let image_ref = MediaReference {
+ opaque_id: Some("media:tenant7/thread42/image1".into()),
+ content_type: Some("image/png".into()),
+ kind: MediaKind::Image,
+ ..Default::default()
+ };
+ let voice_ref = MediaReference {
+ opaque_id: Some("media:tenant7/thread42/voice1".into()),
+ content_type: Some("audio/ogg".into()),
+ kind: MediaKind::Audio,
+ ..Default::default()
+ };
+ let media_result = reply
+ .feed(vec![
+ ChannelOutputEvent::Media {
+ text: Some("Generated image".into()),
+ parts: vec![ContentPart::Image {
+ media: image_ref.clone(),
+ caption: None,
+ alt_text: Some("generated scene".into()),
+ }],
+ },
+ ChannelOutputEvent::VoiceNote {
+ text: None,
+ voice: VoiceNote {
+ media: voice_ref.clone(),
+ codec: Some("opus".into()),
+ duration_ms: Some(900),
+ ..Default::default()
+ },
+ },
+ ])
+ .await
+ .expect("typed media and voice are accepted by the hosted progressive target");
+ assert!(crate::channels::runtime::dispatch::delivery_succeeded(
+ &media_result
+ ));
+ let final_result = reply
+ .finish("Final hosted reply")
+ .await
+ .expect("the provider terminal is delivered");
+ assert!(crate::channels::runtime::dispatch::delivery_succeeded(
+ &final_result
+ ));
+
+ let mut projected: Vec<crate::web_chat::ScopedWebEvent> = Vec::new();
+ let mut gateway_terminals = Vec::new();
+ tokio::time::timeout(Duration::from_secs(5), async {
+ while projected
+ .iter()
+ .filter(|e: &&crate::web_chat::ScopedWebEvent| e.event_name() == "chat_message")
+ .count()
+ < 2
+ || projected
+ .iter()
+ .filter(|e: &&crate::web_chat::ScopedWebEvent| e.event_name() == "chat_done")
+ .count()
+ < 1
+ || gateway_terminals.len() < 3
+ {
+ let event = web_events.recv().await.expect("web event bus");
+ if event.context().request_id != request_id {
+ continue;
+ }
+ match event.event_name() {
+ "chat_message" | "chat_done" => projected.push(event),
+ crate::channels::providers::relay::CHANNEL_OUTBOUND_EVENT => {
+ gateway_terminals.push(event)
+ }
+ name => panic!("unexpected typed relay output event: {name}"),
+ }
+ }
+ })
+ .await
+ .expect("both typed projections and all provider sends arrive");
+
+ let image_projection = projected
+ .iter()
+ .find(|event| {
+ serde_json::to_value(event)
+ .is_ok_and(|wire| wire.to_string().contains("media:tenant7/thread42/image1"))
+ })
+ .expect("image part reaches the shared web projection");
+ let image_projection_wire = serde_json::to_value(image_projection).expect("image wire");
+ assert_eq!(image_projection.context().client_id, client_id);
+ assert_eq!(image_projection.context().thread_id, thread_id);
+ assert!(image_projection.belongs_to("relay-typed-output-agent"));
+ assert!(image_projection.belongs_to_profile("relay-typed-output-profile"));
+ assert_eq!(
+ image_projection_wire["parts"][0]["part"]["media"]["opaqueId"],
+ "media:tenant7/thread42/image1"
+ );
+
+ let voice_projection = projected
+ .iter()
+ .find(|event| {
+ serde_json::to_value(event)
+ .is_ok_and(|wire| wire.to_string().contains("media:tenant7/thread42/voice1"))
+ })
+ .expect("voice note reaches the shared web projection");
+ let voice_projection_wire = serde_json::to_value(voice_projection).expect("voice wire");
+ assert_eq!(
+ voice_projection_wire["parts"][0]["part"]["kind"],
+ "voice_note"
+ );
+ assert_eq!(
+ voice_projection_wire["parts"][0]["part"]["voiceNote"]["media"]["opaqueId"],
+ "media:tenant7/thread42/voice1"
+ );
+
+ assert_eq!(
+ projected
+ .iter()
+ .filter(|event| event.event_name() == "chat_done")
+ .count(),
+ 1
+ );
+ assert_eq!(
+ gateway_terminals.len(),
+ 3,
+ "one provider terminal per typed send"
+ );
+ let image_gateway = gateway_terminals
+ .iter()
+ .find(|event| {
+ serde_json::to_value(event)
+ .is_ok_and(|wire| wire.to_string().contains("media:tenant7/thread42/image1"))
+ })
+ .expect("image part reaches the scoped provider terminal");
+ let image_gateway_wire = serde_json::to_value(image_gateway).expect("gateway image wire");
+ assert_eq!(image_gateway.context().client_id, client_id);
+ assert_eq!(image_gateway.context().thread_id, thread_id);
+ assert!(image_gateway.belongs_to("relay-typed-output-agent"));
+ assert!(image_gateway.belongs_to_profile("relay-typed-output-profile"));
+ assert!(image_gateway_wire["structured"]["parts"]
+ .as_array()
+ .expect("typed provider parts")
+ .iter()
+ .any(|part| {
+ part["part"]["kind"] == "image"
+ && part["part"]["media"]["opaqueId"] == "media:tenant7/thread42/image1"
+ }));
+ let voice_gateway = gateway_terminals
+ .iter()
+ .find(|event| {
+ serde_json::to_value(event)
+ .is_ok_and(|wire| wire.to_string().contains("media:tenant7/thread42/voice1"))
+ })
+ .expect("voice part reaches the scoped provider terminal");
+ let voice_gateway_wire = serde_json::to_value(voice_gateway).expect("gateway voice wire");
+ assert_eq!(voice_gateway.context().client_id, client_id);
+ assert_eq!(voice_gateway.context().thread_id, thread_id);
+ assert!(voice_gateway.belongs_to("relay-typed-output-agent"));
+ assert!(voice_gateway.belongs_to_profile("relay-typed-output-profile"));
+ assert!(voice_gateway_wire["structured"]["parts"]
+ .as_array()
+ .expect("typed voice provider parts")
+ .iter()
+ .any(|part| {
+ part["kind"] == "voice"
+ && part["voice"]["kind"] == "media"
+ && part["voice"]["media"]["opaqueId"] == "media:tenant7/thread42/voice1"
+ }));
+ let expected_channel_ref = serde_json::to_value(&channel).expect("expected channel ref");
+ let expected_conversation_ref =
+ serde_json::to_value(&conversation).expect("expected conversation ref");
+ assert_eq!(
+ voice_gateway_wire["structured"]["channel_ref"],
+ expected_channel_ref
+ );
+ assert_eq!(
+ voice_gateway_wire["structured"]["conversation_ref"],
+ expected_conversation_ref
+ );
+ assert_eq!(
+ image_gateway_wire["structured"]["channel_ref"],
+ expected_channel_ref
+ );
+ assert_eq!(
+ image_gateway_wire["structured"]["conversation_ref"],
+ expected_conversation_ref
+ );
+ assert_eq!(
+ gateway_terminals
+ .iter()
+ .filter(|event| {
+ serde_json::to_value(event)
+ .is_ok_and(|wire| wire.to_string().contains("media:tenant7/thread42/image1"))
+ })
+ .count(),
+ 1,
+ "image delivery has one provider terminal"
+ );
+ assert!(
+ image_gateway_wire["structured"]["parts"]
+ .as_array()
+ .expect("typed provider parts")
+ .iter()
+ .any(|part| {
+ part["part"]["kind"] == "text" && part["part"]["text"] == "Generated image"
+ }),
+ "the media caption remains a typed text part in the provider terminal"
+ );
+ assert_eq!(
+ gateway_terminals
+ .iter()
+ .filter(|event| {
+ serde_json::to_value(event)
+ .is_ok_and(|wire| wire.to_string().contains("media:tenant7/thread42/voice1"))
+ })
+ .count(),
+ 1,
+ "voice delivery has one provider terminal"
+ );
+ assert_eq!(
+ gateway_terminals
+ .iter()
+ .filter(|event| {
+ serde_json::to_value(event)
+ .is_ok_and(|wire| wire["full_response"] == "Final hosted reply")
+ })
+ .count(),
+ 1,
+ "final text has one provider terminal"
+ );
+ assert_eq!(
+ relay.sent().len(),
+ 3,
+ "provider output is delivered once per send"
+ );
+ assert_eq!(
+ projected.len(),
+ 3,
+ "two media messages and one turn terminal"
+ );
+
+ reply
+ .close()
+ .await
+ .expect("hosted projection runtime closes");
+ while let Ok(event) = web_events.try_recv() {
+ assert_ne!(
+ event.context().request_id,
+ request_id,
+ "no duplicate typed projection or provider terminal"
+ );
+ }
+ }
+
+#[tokio::test]
+async fn dropped_scoped_media_read_lease_aborts_the_compiled_module_handle() {
+ let env_lock = EnvVarGuard::locked_async().await;
+ let server = MockServer::start().await;
+ let _env = env_lock.with("OPENHUMAN_TELEGRAM_BOT_API_BASE", server.uri());
+ let _storage = crate::channels::module_fixture::install_memory_storage();
+ crate::channels::module_fixture::client().await;
+
+ for endpoint in ["getUpdates", "deleteWebhook"] {
+ Mock::given(method("POST"))
+ .and(path(format!("/bottoken/{endpoint}")))
+ .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
+ "ok": true,
+ "result": if endpoint == "getUpdates" { serde_json::json!([]) } else { serde_json::json!(true) }
+ })))
+ .mount(&server)
+ .await;
+ }
+
+ let workspace = tempfile::tempdir().expect("temporary module workspace");
+ let mut config = Config::default();
+ config.workspace_dir = workspace.path().to_path_buf();
+ config.modules.allow_download = false;
+ config.modules.source = crate::config::schema::ModuleSourceMode::Linked;
+ config.channels_config.telegram = Some(tinychannels_bus::config::TelegramConfig {
+ bot_token: "token".into(),
+ chat_id: None,
+ allowed_users: vec!["42".into()],
+ stream_mode: Default::default(),
+ draft_update_interval_ms: 1,
+ silent_streaming: true,
+ mention_only: false,
+ });
+ let dispatch_ctx = crate::core::runtime::DispatchCtx::host_default();
+ let (dispatch, _receiver) = tokio::sync::mpsc::channel(4);
+ let mut runtime = ModuleChannelRuntime::prepare(
+ &dispatch_ctx,
+ Arc::new(config.clone()),
+ Arc::new(OnceLock::new()),
+ dispatch,
+ )
+ .await
+ .expect("compiled TinyBus module should prepare configured Telegram");
+ runtime.start(&config).await.expect("mock Telegram starts");
+
+ let scope = tinychannels_bus::HostVoiceScope {
+ configured_instance: "telegram".into(),
+ channel: tinychannels_bus::ChannelRef {
+ id: "telegram".into(),
+ account_id: Some("telegram".into()),
+ },
+ conversation: tinychannels_bus::ConversationRef {
+ kind: tinychannels_bus::ConversationKind::Dm,
+ id: "42".into(),
+ ..Default::default()
+ },
+ turn_id: "media-read-lease-test".into(),
+ };
+ let object = runtime.lease.object();
+ let bytes = vec![b'x'; tinychannels_bus::module_media::MEDIA_READ_CHUNK_BYTES + 1];
+ let begin: tinychannels_bus::MediaUploadHandle = object
+ .call_confidential(
+ tinychannels_bus::methods::BEGIN_MEDIA_UPLOAD,
+ (tinychannels_bus::MediaUploadBegin {
+ instance: "telegram".into(),
+ scope: scope.clone(),
+ kind: tinychannels_bus::MediaKind::Document,
+ content_type: "text/plain".into(),
+ file_name: Some("note.txt".into()),
+ duration_ms: None,
+ codec: None,
+ byte_count: bytes.len(),
+ },),
+ )
+ .await
+ .expect("module accepts a scoped upload");
+ let split = tinychannels_bus::module_media::MEDIA_UPLOAD_CHUNK_BYTES;
+ object
+ .call_confidential::<u32>(
+ tinychannels_bus::methods::APPEND_MEDIA_UPLOAD_CHUNK,
+ (tinychannels_bus::MediaUploadChunk {
+ handle: begin.clone(),
+ sequence: 0,
+ bytes: bytes[..split].to_vec(),
+ },),
+ )
+ .await
+ .expect("module accepts the upload bytes");
+ object
+ .call_confidential::<u32>(
+ tinychannels_bus::methods::APPEND_MEDIA_UPLOAD_CHUNK,
+ (tinychannels_bus::MediaUploadChunk {
+ handle: begin.clone(),
+ sequence: 1,
+ bytes: bytes[split..].to_vec(),
+ },),
+ )
+ .await
+ .expect("module accepts the final upload chunk");
+ let media: tinychannels_bus::MediaReference = object
+ .call_confidential(
+ tinychannels_bus::methods::FINISH_MEDIA_UPLOAD,
+ (tinychannels_bus::MediaUploadFinish(begin),),
+ )
+ .await
+ .expect("module persists an opaque scoped media reference");
+
+ let read: tinychannels_bus::MediaReadInfo = object
+ .call_confidential(
+ tinychannels_bus::methods::BEGIN_MEDIA_READ,
+ (tinychannels_bus::MediaReadBegin {
+ instance: "telegram".into(),
+ scope,
+ media,
+ max_bytes: bytes.len() as u64,
+ },),
+ )
+ .await
+ .expect("module opens a scoped read handle");
+ let first_chunk: tinychannels_bus::MediaReadChunkResult = object
+ .call_confidential(
+ tinychannels_bus::methods::READ_MEDIA_CHUNK,
+ (tinychannels_bus::MediaReadChunk {
+ handle: read.handle.clone(),
+ sequence: 0,
+ },),
+ )
+ .await
+ .expect("read handle is usable before lease drop");
+ assert!(!first_chunk.done);
+ let replay: tinychannels_bus::MediaReadChunkResult = object
+ .call_confidential(
+ tinychannels_bus::methods::READ_MEDIA_CHUNK,
+ (tinychannels_bus::MediaReadChunk {
+ handle: read.handle.clone(),
+ sequence: 0,
+ },),
+ )
+ .await
+ .expect("the module retains the last chunk for retry before lease drop");
+ assert_eq!(replay, first_chunk);
+ let handle = read.handle.clone();
+ crate::channels::runtime::dispatch::drop_media_read_lease_for_test(
+ runtime.client.clone(),
+ dispatch_ctx,
+ read.handle,
+ );
+
+ tokio::time::timeout(Duration::from_secs(2), async {
+ loop {
+ let chunk = object
+ .call_confidential::<tinychannels_bus::MediaReadChunkResult>(
+ tinychannels_bus::methods::READ_MEDIA_CHUNK,
+ (tinychannels_bus::MediaReadChunk {
+ handle: handle.clone(),
+ sequence: 1,
+ },),
+ )
+ .await;
+ if let Err(error) = chunk {
+ assert_eq!(error, tinybus::module::ModuleCallError::ModuleFault);
+ break;
+ }
+ tokio::task::yield_now().await;
+ }
+ })
+ .await
+ .expect("dropping the lease eventually aborts the module read handle");
+
+ runtime
+ .close()
+ .await
+ .expect("module runtime closes cleanly");
+}
+
async fn run_hosted_compiled_turn(
object: &tinybus::module::ModuleObjectClient,
channel: tinychannels_bus::ChannelRef,
diff --cc crates/openhuman-core/src/threads/attachments.rs
index ecf7d3ce60,109445c78b..0bb388f8f1
--- a/crates/openhuman-core/src/threads/attachments.rs
+++ b/crates/openhuman-core/src/threads/attachments.rs
@@@ -19,10 -19,8 +19,10 @@@ const STORAGE_KEY: &str = "__openhumanS
mod staged;
use staged::*;
pub(crate) use staged::{
- bind_staged_refs, commit_native_channel_parts, consume_staged_parts, discard_native_channel_parts,
- discard_staged_parts,
- native_parts_in_committed_message, NativeStagedLease, stage_native_channel_part, stage_queued_parts,
- read_authorized_native_attachment, validate_opaque_parts,
- bind_staged_refs, consume_staged_parts, discard_staged_parts, stage_queued_parts,
- validate_opaque_parts,
++ bind_staged_refs, commit_native_channel_parts, consume_staged_parts,
++ discard_native_channel_parts, discard_staged_parts, native_parts_in_committed_message,
++ read_authorized_native_attachment, stage_native_channel_part, stage_queued_parts,
++ validate_opaque_parts, NativeStagedLease,
};
pub(crate) fn has_opaque_ref(part: &ContentPart) -> bool {
diff --cc crates/openhuman-core/src/threads/attachments/staged.rs
index e5ab2d4f05,ee822ac7de..9ac264ddd3
--- a/crates/openhuman-core/src/threads/attachments/staged.rs
+++ b/crates/openhuman-core/src/threads/attachments/staged.rs
@@@ -400,312 -298,6 +400,308 @@@ pub(crate) async fn stage_queued_parts
Ok(public)
}
+/// Store bytes obtained from a trusted, scoped channel-module media read as a
+/// staged attachment. The provider reference is replaced with a fresh opaque
+/// host reference; the private workspace path remains only in this record.
+pub(crate) async fn stage_native_channel_part(
+ ctx: &crate::core::runtime::DispatchCtx,
+ thread_id: &str,
+ config: &crate::config::Config,
+ input: &ContentPart,
+ name: &str,
+ mime: &str,
+ bytes: &[u8],
+) -> Result<ContentPart, String> {
+ if thread_id.trim().is_empty() || bytes.is_empty() {
+ return Err("native attachment scope and bytes must be non-empty".into());
+ }
+ let mut staged_config = config.clone();
+ staged_config.action_dir = crate::agent::attachments::thread_action_dir(
+ ctx,
+ config,
+ &config.workspace_dir,
+ Some(thread_id),
+ )
+ .await
+ .map_err(|error| format!("native attachment workspace unavailable: {error}"))?;
+ let file = crate::agent::attachments::store_scoped_channel_media(
+ &staged_config,
+ thread_id,
+ name,
+ mime,
+ bytes,
+ )
+ .await
+ .map_err(|_| "native attachment could not be staged safely".to_owned())?;
+ let file_path = file.path.clone();
+ let result = async {
+ let mut part = input.clone();
+ let media = opaque_media_mut(&mut part)
+ .ok_or_else(|| "native attachment kind cannot be staged".to_owned())?;
+ let opaque_id = uuid::Uuid::new_v4().simple().to_string();
+ media.opaque_id = Some(opaque_id.clone());
+ media.path = None;
+ media.url = None;
+ media.local_cache_path = None;
+ media.content_type = Some(file.mime.clone());
+ media.size_bytes = Some(file.size_bytes as u64);
+
+ let tenant = tenant_for(ctx)?;
+ let repo = staged_repo(ctx, &config.workspace_dir)?;
+ let id = staged_doc_id(&opaque_id)?;
+ let manifest = StagedAttachment {
+ tenant,
+ thread_id: thread_id.to_owned(),
+ attachment: StoredAttachment {
+ part: part.clone(),
+ path: file.path,
+ },
+ expires_at: chrono::Utc::now().timestamp() + STAGED_TTL_SECS,
+ claim: None,
+ };
+ let body = serde_json::to_value(&manifest)
+ .map_err(|error| format!("serialize native attachment record: {error}"))?;
+ repo.run(|docs| async move {
+ docs.put(STAGED_COLLECTION, &id, body, Precondition::Absent)
+ .await?;
+ Ok(())
+ })
+ .map_err(|error| format!("persist native attachment record: {error}"))?;
+ Ok(part)
+ }
+ .await;
+ if result.is_err() {
+ let path = staged_config.action_dir.join(file_path);
+ let _ = tokio::fs::remove_file(path).await;
+ }
+ result
+}
+
+pub(crate) async fn commit_native_channel_parts(
+ ctx: &crate::core::runtime::DispatchCtx,
+ thread_id: &str,
+ parts: &[ContentPart],
+ committed_message_id: &str,
+) -> Result<(), String> {
+ if parts.is_empty() {
+ return Ok(());
+ }
+ let config = ctx
+ .load_config()
+ .await
+ .map_err(|error| format!("native attachment configuration unavailable: {error}"))?;
+ let entries = read_staged_entries(ctx, thread_id, parts, &config).await?;
+ validate_staged_files(ctx, thread_id, &config, &entries).await?;
+ claim_staged_refs(ctx, thread_id, &config, committed_message_id, &entries).await
+}
+
+pub(crate) async fn discard_native_channel_parts(
+ ctx: &crate::core::runtime::DispatchCtx,
+ thread_id: &str,
+ config: &crate::config::Config,
+ parts: &[ContentPart],
+) -> Result<(), String> {
+ if parts.is_empty() {
+ return Ok(());
+ }
+ let entries = read_staged_entries(ctx, thread_id, parts, config).await?;
+ discard_staged_entries(ctx, thread_id, config, entries).await
+}
+
+pub(crate) async fn read_authorized_native_attachment(
+ ctx: &crate::core::runtime::DispatchCtx,
+ config: &crate::config::Config,
+ thread_id: &str,
+ opaque_id: &str,
+ current_parts: &[ContentPart],
+ allow_committed_history: bool,
+) -> Result<ResolvedNativeAttachment, String> {
+ let id = staged_doc_id(opaque_id)?;
+ let tenant = tenant_for(ctx)?;
+ let repo = staged_repo(ctx, &config.workspace_dir)?;
+ let record = get_staged_record(&repo, &id)?
+ .ok_or_else(|| "native attachment is not registered".to_owned())?;
+ if record.tenant != tenant || record.thread_id != thread_id {
+ return Err("native attachment is outside the active session scope".into());
+ }
+ if record.claim.as_ref().is_some_and(|claim| claim.canceled) {
+ return Err("native attachment was canceled".into());
+ }
+ let current_authority = current_parts.iter().any(|part| {
+ opaque_media(part)
+ .and_then(|media| media.opaque_id.as_deref())
+ .is_some_and(|candidate| candidate == opaque_id)
+ && part == &record.attachment.part
+ });
+ let committed_authority = record.claim.as_ref().is_some_and(|claim| {
+ claim.tenant == tenant
+ && claim.thread_id == thread_id
+ && claim.part == record.attachment.part
+ });
+ if !current_authority && !(allow_committed_history && committed_authority) {
+ return Err("native attachment lacks a current or committed host grant".into());
+ }
+ validate_native_attachment_policy(&record.attachment, config)?;
+ let entries = vec![record.attachment.clone()];
+ validate_staged_files(ctx, thread_id, config, &entries).await?;
+ let entry = &record.attachment;
+ let workspace = crate::agent::attachments::thread_action_dir(
+ ctx,
+ config,
+ &config.workspace_dir,
+ Some(thread_id),
+ )
+ .await
+ .map_err(|_| "native attachment workspace is unavailable".to_owned())?;
+ let scope = crate::agent::attachments::AttachmentAccessScope {
+ external_channel: false,
+ workspace: Some(workspace),
+ thread_id: Some(thread_id.to_owned()),
+ ..Default::default()
+ };
+ let path = crate::agent::attachments::resolve_path(config, &entry.path, &scope)
+ .await
+ .map_err(|_| "native attachment workspace path is invalid".to_owned())?;
+ let media =
+ opaque_media(&entry.part).ok_or_else(|| "native attachment kind is invalid".to_owned())?;
+ let mime = media
+ .content_type
+ .clone()
+ .ok_or_else(|| "native attachment MIME is unavailable".to_owned())?;
+ let root = tokio::fs::canonicalize(&workspace)
+ .await
+ .map_err(|_| "native attachment workspace is unavailable".to_owned())?;
+ let limit = native_attachment_byte_limit(&record.attachment.part, config);
+ let bytes = read_staged_file_bounded(&path, &root, limit).await?;
+ validate_native_attachment_size(&record.attachment.part, bytes.len(), config)?;
+ Ok(ResolvedNativeAttachment {
+ path: entry.path.clone(),
+ mime,
+ bytes,
+ })
+}
+
- fn native_attachment_byte_limit(
- part: &ContentPart,
- config: &crate::config::Config,
- ) -> usize {
++fn native_attachment_byte_limit(part: &ContentPart, config: &crate::config::Config) -> usize {
+ let is_image = matches!(part, ContentPart::Image { .. });
+ let mb = if is_image {
+ config.multimodal.effective_limits().1
+ } else {
+ config.multimodal_files.effective_limits().1
+ };
- mb
- .saturating_mul(1024 * 1024)
++ mb.saturating_mul(1024 * 1024)
+ .min(tinychannels_bus::module_media::MEDIA_UPLOAD_MAX_BYTES)
+}
+
+pub(super) async fn read_staged_file_bounded(
+ path: &std::path::Path,
+ root: &std::path::Path,
+ limit: usize,
+) -> Result<Vec<u8>, String> {
+ let file = crate::agent::attachments::provider::open::secure_open(path, root)
+ .map_err(|_| "native attachment bytes are unavailable".to_owned())?;
+ let metadata = file
+ .metadata()
+ .map_err(|_| "native attachment bytes are unavailable".to_owned())?;
+ if !metadata.is_file() || metadata.len() > limit as u64 {
+ return Err("native attachment exceeds its configured byte limit".into());
+ }
+ use tokio::io::AsyncReadExt;
+ let file = tokio::fs::File::from_std(file);
+ let mut bytes = Vec::new();
+ file.take(limit.saturating_add(1) as u64)
+ .read_to_end(&mut bytes)
+ .await
+ .map_err(|_| "native attachment bytes are unavailable".to_owned())?;
+ if bytes.len() > limit {
+ return Err("native attachment exceeds its configured byte limit".into());
+ }
+ Ok(bytes)
+}
+
+fn validate_native_attachment_policy(
+ entry: &StoredAttachment,
+ config: &crate::config::Config,
+) -> Result<(), String> {
+ let media =
+ opaque_media(&entry.part).ok_or_else(|| "native attachment kind is invalid".to_owned())?;
+ let mime = media
+ .content_type
+ .as_deref()
+ .ok_or_else(|| "native attachment MIME is unavailable".to_owned())?;
+ let is_image = matches!(entry.part, ContentPart::Image { .. });
+ let allowed = if is_image {
+ matches!(
+ mime,
+ "image/png" | "image/jpeg" | "image/webp" | "image/gif"
+ )
+ } else {
+ config
+ .multimodal_files
+ .allowed_mime_types
+ .iter()
+ .any(|value| value == mime)
+ };
+ if !allowed {
+ return Err("native attachment MIME is no longer allowed".into());
+ }
+ Ok(())
+}
+
+fn validate_native_attachment_size(
+ part: &ContentPart,
+ actual_bytes: usize,
+ config: &crate::config::Config,
+) -> Result<(), String> {
+ let media = opaque_media(part).ok_or_else(|| "native attachment kind is invalid".to_owned())?;
+ let is_image = matches!(part, ContentPart::Image { .. });
+ let declared = media
+ .size_bytes
+ .and_then(|size| usize::try_from(size).ok())
+ .ok_or_else(|| "native attachment size is unavailable".to_owned())?;
+ let (image_mb, file_mb) = (
+ config.multimodal.effective_limits().1,
+ config.multimodal_files.effective_limits().1,
+ );
+ let limit = (if is_image { image_mb } else { file_mb })
+ .saturating_mul(1024 * 1024)
+ .min(tinychannels_bus::module_media::MEDIA_UPLOAD_MAX_BYTES);
+ if actual_bytes == 0 || actual_bytes != declared || actual_bytes > limit {
+ return Err("native attachment exceeds its configured byte limit".into());
+ }
+ Ok(())
+}
+
+pub(crate) fn native_parts_in_committed_message(
+ staged: &[ContentPart],
+ message: &tinyinference_llm::message::Message,
+) -> Vec<ContentPart> {
+ let tinyinference_llm::message::Message::User(user) = message else {
+ return Vec::new();
+ };
+ let committed = user
+ .content
+ .iter()
+ .filter_map(|block| match block {
+ tinyinference_llm::message::ContentBlock::Image(image) => {
+ image.url.strip_prefix("openhuman-staged://")
+ }
+ tinyinference_llm::message::ContentBlock::Document(
+ tinyinference_llm::message::MediaRef::Path { path, .. },
+ ) => path.strip_prefix("openhuman-staged://"),
+ _ => None,
+ })
+ .collect::<std::collections::HashSet<_>>();
+ staged
+ .iter()
+ .filter(|part| {
+ opaque_media(part)
+ .and_then(|media| media.opaque_id.as_deref())
+ .is_some_and(|id| committed.contains(id))
+ })
+ .cloned()
+ .collect()
+}
+
pub(super) async fn validate_staged_files(
ctx: &crate::core::runtime::DispatchCtx,
thread_id: &str,
diff --cc crates/openhuman-core/src/threads/attachments_tests.rs
index fd392e1cf2,42f01246fc..f0c163f545
--- a/crates/openhuman-core/src/threads/attachments_tests.rs
+++ b/crates/openhuman-core/src/threads/attachments_tests.rs
@@@ -185,161 -183,6 +183,157 @@@ async fn staged_claim_and_cancellation_
}
}
+#[tokio::test]
+async fn native_attachment_refs_are_bound_to_profile_agent_account_and_turn() {
+ use crate::core::runtime::{ContextOverlay, CoreContext, DispatchCtx, DomainSet};
+
+ let temp = tempfile::tempdir().unwrap();
+ let workspace = temp.path().join("workspace");
+ let config = crate::config::Config {
+ workspace_dir: workspace.clone(),
+ action_dir: workspace.clone(),
+ ..Default::default()
+ };
+ let make_dispatch = |profile: &str, agent: &str| {
+ let root = CoreContext::for_test(DomainSet::none(), Some(workspace.clone()));
+ let scoped = root.derive_with(
+ ContextOverlay::new(config.clone(), DomainSet::full(), Default::default())
+ .profile(profile)
+ .session_agent(agent),
+ );
+ DispatchCtx::new(scoped)
+ };
+ let alice = make_dispatch("profile-a", "agent-a");
+ let other_profile = make_dispatch("profile-b", "agent-a");
+ let other_agent = make_dispatch("profile-a", "agent-b");
+ let part = image(MediaReference {
+ opaque_id: Some(uuid::Uuid::new_v4().simple().to_string()),
+ content_type: Some("image/png".into()),
+ size_bytes: Some(4),
+ ..Default::default()
+ });
+ let thread = "telegram:account-a:conversation-42";
+ let record = StagedAttachment {
+ tenant: crate::core::runtime::Tenant {
+ profile: Some("profile-a".into()),
+ agent: Some("agent-a".into()),
+ },
+ thread_id: thread.into(),
+ attachment: StoredAttachment {
+ part: part.clone(),
+ path: "media.png".into(),
+ },
+ expires_at: chrono::Utc::now().timestamp() + 60,
+ claim: None,
+ };
+ let repo = staged_repo(&alice, &workspace).unwrap();
+ insert_record(&repo, &record);
+ let opaque_id = opaque_media(&part)
+ .unwrap()
+ .opaque_id
+ .as_deref()
+ .unwrap()
+ .to_owned();
+
+ for dispatch in [&other_profile, &other_agent] {
- assert!(
- read_authorized_native_attachment(
- dispatch,
- &config,
- thread,
- &opaque_id,
- std::slice::from_ref(&part),
- false,
- )
- .await
- .is_err()
- );
++ assert!(read_authorized_native_attachment(
++ dispatch,
++ &config,
++ thread,
++ &opaque_id,
++ std::slice::from_ref(&part),
++ false,
++ )
++ .await
++ .is_err());
+ }
+ assert!(
+ read_authorized_native_attachment(
+ &alice,
+ &config,
+ "telegram:account-b:conversation-42",
+ &opaque_id,
+ std::slice::from_ref(&part),
+ false,
+ )
+ .await
+ .is_err(),
+ "the same conversation under another configured account is isolated"
+ );
+
+ let forged_part = match part.clone() {
+ ContentPart::Image {
+ media, alt_text, ..
+ } => ContentPart::Image {
+ media,
+ caption: Some("forged turn".into()),
+ alt_text,
+ },
+ _ => unreachable!(),
+ };
+ assert!(
+ read_authorized_native_attachment(
+ &alice,
+ &config,
+ thread,
+ &opaque_id,
+ &[forged_part],
+ false,
+ )
+ .await
+ .is_err(),
+ "opaque IDs alone do not authorize a modified part"
+ );
+ assert!(
+ read_authorized_native_attachment(&alice, &config, thread, &opaque_id, &[], false)
+ .await
+ .is_err(),
+ "an unclaimed reference from another turn is not authorized"
+ );
+ assert!(
+ read_authorized_native_attachment(&alice, &config, thread, &opaque_id, &[], true)
+ .await
+ .is_err(),
+ "history access still requires a committed host claim"
+ );
+}
+
+#[tokio::test]
+async fn native_staged_file_reader_enforces_limit_and_rejects_symlinks() {
+ let temp = tempfile::tempdir().unwrap();
+ let root = temp.path().join("workspace");
+ let outside = temp.path().join("outside");
+ std::fs::create_dir_all(&root).unwrap();
+ std::fs::write(root.join("small.txt"), b"small").unwrap();
+ std::fs::write(root.join("large.txt"), b"larger than bound").unwrap();
+
+ assert_eq!(
+ super::staged::read_staged_file_bounded(&root.join("small.txt"), &root, 16)
+ .await
+ .unwrap(),
+ b"small"
+ );
+ assert!(
+ super::staged::read_staged_file_bounded(&root.join("large.txt"), &root, 4)
+ .await
+ .unwrap_err()
+ .contains("configured byte limit")
+ );
+
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::symlink;
+
+ std::fs::write(&outside, b"outside workspace").unwrap();
+ symlink(&outside, root.join("swapped.txt")).unwrap();
- assert!(super::staged::read_staged_file_bounded(
- &root.join("swapped.txt"),
- &root,
- 128,
- )
- .await
- .is_err());
++ assert!(
++ super::staged::read_staged_file_bounded(&root.join("swapped.txt"), &root, 128,)
++ .await
++ .is_err()
++ );
+ }
+}
+
#[tokio::test]
async fn multipart_claim_failure_keeps_prior_claim_owned_by_retry() {
let dir = tempfile::tempdir().unwrap();
@@@ -477,161 -310,3 +461,164 @@@ async fn discarding_claimed_attachment_
.unwrap();
assert_eq!(stored.claim, Some(bound));
}
+
+#[tokio::test]
+async fn native_staged_lease_cleans_dropped_queue_and_preserves_committed_claims() {
+ let dir = tempfile::tempdir().unwrap();
+ let mut config = crate::config::Config::default();
+ config.workspace_dir = dir.path().join("workspace");
+ config.action_dir = dir.path().join("actions");
+ let ctx = crate::core::runtime::DispatchCtx::ambient();
+ let thread_id = "telegram:account-a:conversation-42";
+ let workspace = crate::agent::attachments::thread_action_dir(
+ &ctx,
+ &config,
+ &config.workspace_dir,
+ Some(thread_id),
+ )
+ .await
+ .unwrap();
+ tokio::fs::create_dir_all(&workspace).await.unwrap();
+ let repo = test_repo(&config.workspace_dir);
+
+ let pending_id = uuid::Uuid::new_v4().simple().to_string();
+ let pending = image(MediaReference {
+ opaque_id: Some(pending_id.clone()),
+ content_type: Some("image/png".into()),
+ ..Default::default()
+ });
+ let pending_path = "pending.png";
+ let pending_file = workspace.join(pending_path);
- tokio::fs::write(&pending_file, b"pending bytes").await.unwrap();
++ tokio::fs::write(&pending_file, b"pending bytes")
++ .await
++ .unwrap();
+ insert_record(&repo, &staged_record(pending.clone(), pending_path));
+ let lease = NativeStagedLease::new(
+ ctx.clone(),
+ thread_id.into(),
+ config.clone(),
+ vec![pending.clone()],
+ );
+ let queued_clone = lease.clone();
+ drop(lease);
- assert!(get_staged_record(&repo, &staged_doc_id(&pending_id).unwrap())
- .unwrap()
- .unwrap()
- .claim
- .is_none());
++ assert!(
++ get_staged_record(&repo, &staged_doc_id(&pending_id).unwrap())
++ .unwrap()
++ .unwrap()
++ .claim
++ .is_none()
++ );
+ drop(queued_clone);
+ tokio::time::timeout(std::time::Duration::from_secs(2), async {
+ loop {
+ let record = get_staged_record(&repo, &staged_doc_id(&pending_id).unwrap())
+ .unwrap()
+ .unwrap();
+ if record.claim.is_some_and(|claim| claim.canceled) {
+ break;
+ }
+ tokio::task::yield_now().await;
+ }
+ })
+ .await
+ .expect("dropping the final queued payload clone should discard its staged ref");
+ assert!(!pending_file.exists());
+
+ let removed_id = uuid::Uuid::new_v4().simple().to_string();
+ let removed = image(MediaReference {
+ opaque_id: Some(removed_id.clone()),
+ content_type: Some("image/png".into()),
+ ..Default::default()
+ });
+ let removed_path = "removed-from-queue.png";
+ let removed_file = workspace.join(removed_path);
+ tokio::fs::write(&removed_file, b"removed bytes")
+ .await
+ .unwrap();
+ insert_record(&repo, &staged_record(removed.clone(), removed_path));
+ let queue_lease = NativeStagedLease::new(
+ ctx.clone(),
+ thread_id.into(),
+ config.clone(),
+ vec![removed.clone()],
+ );
+ let queue = tinyagents_harness::run_queue::RunQueue::new();
+ let queued = tinyagents_harness::turn_runner::TurnRequest {
+ scope: tinyagents_harness::turn_runner::ScopeKey {
+ agent_id: "orchestrator".into(),
+ profile_id: None,
+ },
+ thread_id: thread_id.into(),
+ request_id: "remove-native-media".into(),
+ text: "queued image".into(),
+ payload: crate::agent::queued_turn::QueuedPayload {
+ native_staged_lease: Some(queue_lease),
+ ..Default::default()
+ },
+ host_authored: false,
+ disposition: tinyagents_harness::turn_runner::QueueDisposition::Followup,
+ deadline: None,
+ idle_timeout: None,
+ };
+ queue
+ .push(tinyagents_harness::run_queue::QueueLane::Followup, queued)
+ .await;
+ assert_eq!(
+ queue
+ .remove_where(|request| request.request_id == "remove-native-media")
+ .await,
+ 1
+ );
+ tokio::time::timeout(std::time::Duration::from_secs(2), async {
+ loop {
+ let record = get_staged_record(&repo, &staged_doc_id(&removed_id).unwrap())
+ .unwrap()
+ .unwrap();
+ if record.claim.is_some_and(|claim| claim.canceled) {
+ break;
+ }
+ tokio::task::yield_now().await;
+ }
+ })
+ .await
+ .expect("removing the queued request should discard its staged media");
+ assert!(!removed_file.exists());
+
+ let committed_id = uuid::Uuid::new_v4().simple().to_string();
+ let committed = image(MediaReference {
+ opaque_id: Some(committed_id.clone()),
+ content_type: Some("image/png".into()),
+ ..Default::default()
+ });
+ let committed_path = "committed.png";
+ let committed_file = workspace.join(committed_path);
+ tokio::fs::write(&committed_file, b"committed bytes")
+ .await
+ .unwrap();
+ insert_record(&repo, &staged_record(committed.clone(), committed_path));
+ let durable_claim = claim("user:request", committed.clone(), false);
+ claim_staged_record(
+ &repo,
+ &durable_claim.tenant,
+ thread_id,
+ &staged_doc_id(&committed_id).unwrap(),
+ &committed,
+ &durable_claim,
+ )
+ .unwrap();
- let committed_lease = NativeStagedLease::new(
- ctx,
- thread_id.into(),
- config,
- vec![committed.clone()],
- );
++ let committed_lease =
++ NativeStagedLease::new(ctx, thread_id.into(), config, vec![committed.clone()]);
+ let queue_clone = committed_lease.clone();
+ committed_lease.mark_committed();
+ drop(committed_lease);
+ drop(queue_clone);
+ tokio::task::yield_now().await;
+ let stored = get_staged_record(&repo, &staged_doc_id(&committed_id).unwrap())
+ .unwrap()
+ .unwrap();
+ assert_eq!(stored.claim, Some(durable_claim));
- assert_eq!(tokio::fs::read(committed_file).await.unwrap(), b"committed bytes");
++ assert_eq!(
++ tokio::fs::read(committed_file).await.unwrap(),
++ b"committed bytes"
++ );
+}
diff --cc crates/openhuman-core/src/threads/store/bus.rs
index ce461a8b56,45044ac3b4..4968a934d6
--- a/crates/openhuman-core/src/threads/store/bus.rs
+++ b/crates/openhuman-core/src/threads/store/bus.rs
@@@ -455,8 -455,8 +455,9 @@@ async fn persist_channel_turn
(
tinychannels_bus::module_inbound::PrepareChannelInboundRequest {
namespace: "main".into(),
+ preparation_mode: Default::default(),
session_policy: Default::default(),
+ preparation_mode: Default::default(),
source: tinychannels_bus::module_inbound::ChannelInboundSource::Legacy {
message: ChannelMessage {
reply_to_id: None,
diff --cc tests/native_channel_session_restart_e2e.rs
index 54229ac9c3,584259b46f..3252d14cd0
--- a/tests/native_channel_session_restart_e2e.rs
+++ b/tests/native_channel_session_restart_e2e.rs
@@@ -21,8 -22,7 +22,8 @@@ use axum::http::StatusCode
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
use axum::{Json, Router};
+use base64::Engine as _;
- use serde_json::{Value, json};
+ use serde_json::{json, Value};
use tokio::task::JoinHandle;
use tokio::time::sleep;
@@@ -49,11 -39,13 +50,16 @@@ fn native_image_png() -> &'static [u8]
struct MockState {
updates: Mutex<VecDeque<Value>>,
update_offsets: Mutex<Vec<i64>>,
+ served_update_ids: Mutex<Vec<i64>>,
+ file_requests: Mutex<Vec<String>>,
+ file_downloads: Mutex<Vec<String>>,
completions: Mutex<Vec<Value>>,
sent_messages: Mutex<Vec<Value>>,
+ transcription_uploads: Mutex<Vec<Vec<u8>>>,
+ voice_file_requests: Mutex<Vec<String>>,
+ voice_download_requests: Mutex<usize>,
+ speech_requests: Mutex<Vec<String>>,
+ sent_voice_uploads: Mutex<Vec<Vec<u8>>>,
}
struct MockServers {
@@@ -899,201 -753,142 +1004,354 @@@ async fn native_telegram_session_resume
second.stop();
}
+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+async fn native_telegram_typed_media_reaches_the_model_without_provider_paths() {
+ use base64::Engine as _;
+
+ let tmp = tempfile::tempdir().expect("temporary core home");
+ let home = std::fs::canonicalize(tmp.path()).expect("canonicalize temporary home");
+ let workspace = home.join("workspace");
+ let logs = home.join("logs");
+ std::fs::create_dir_all(&logs).expect("create logs");
+ let module_source_dir = std::env::var_os("OPENHUMAN_TEST_MODULE_SOURCE_DIR")
+ .map(std::path::PathBuf::from)
+ .expect("build TinyChannels and set OPENHUMAN_TEST_MODULE_SOURCE_DIR");
- assert!(
- module_source_dir
- .join("tinychannels/modules.toml")
- .is_file()
- );
++ assert!(module_source_dir
++ .join("tinychannels/modules.toml")
++ .is_file());
+
+ let mocks = start_mock_servers().await;
+ write_config(&home, &workspace, &mocks.inference);
+ let (mut core, core_rpc) = start_core(
+ &home,
+ &workspace,
+ &mocks.telegram,
+ &mocks.inference,
+ &module_source_dir,
+ &logs.join("typed-media.log"),
++ false,
+ );
+ wait_healthy(&core_rpc, &mut core, &logs.join("typed-media.log")).await;
+
+ push_media_update(&mocks.state, media_update(200, 20, true, false, None));
+ wait_for_media_fetch(&mocks.state, 200, "typed-image-file", "photos/native.png").await;
+ wait_for_completion(&mocks.state, 1).await;
+ push_media_update(&mocks.state, media_update(201, 21, false, true, None));
+ wait_for_media_fetch(
+ &mocks.state,
+ 201,
+ "typed-document-file",
+ "documents/native.txt",
+ )
+ .await;
+ wait_for_completion(&mocks.state, 2).await;
+ push_media_update(
+ &mocks.state,
+ media_update(202, 22, true, false, Some("image caption before media")),
+ );
+ wait_for_media_fetch(&mocks.state, 202, "typed-image-file", "photos/native.png").await;
+ wait_for_completion(&mocks.state, 3).await;
+ push_media_update(
+ &mocks.state,
+ media_update(203, 23, false, true, Some("file caption before media")),
+ );
+ wait_for_media_fetch(
+ &mocks.state,
+ 203,
+ "typed-document-file",
+ "documents/native.txt",
+ )
+ .await;
+ wait_for_completion(&mocks.state, 4).await;
+
+ let requests = mocks
+ .state
+ .completions
+ .lock()
+ .expect("model requests")
+ .clone();
+ assert_eq!(
+ requests.len(),
+ 4,
+ "each media update reaches the model once"
+ );
+ assert_model_received_typed_image(&requests[0]);
+ assert_model_received_file_content(&requests[1]);
+ let image_mixed = serde_json::to_string(
+ last_user_content(&requests[2]).expect("mixed image user message is present"),
+ )
+ .expect("serialize mixed image user content");
+ let image_caption = image_mixed
+ .find("image caption before media")
+ .expect("image caption text is present");
+ let image = image_mixed
+ .find("image_url")
+ .expect("mixed image is still a typed image block");
+ assert!(image_caption < image, "image part order: {image_mixed}");
+
+ assert_model_received_file_content(&requests[3]);
+ let file_content = last_user_content(&requests[3]).expect("mixed file user message is present");
+ let file_mixed = serde_json::to_string(file_content).expect("serialize mixed file content");
+ let file_caption = file_mixed
+ .find("file caption before media")
+ .expect("file caption text is present");
+ let typed_file = ["input_file", "document", "file"]
+ .into_iter()
+ .find_map(|kind| find_block_type(file_content, kind))
+ .and_then(|document| serde_json::to_string(document).ok())
+ .and_then(|document| file_mixed.find(&document));
+ let file_position = typed_file.or_else(|| {
+ file_mixed.find(std::str::from_utf8(NATIVE_FILE_BYTES).expect("fixture is UTF-8"))
+ });
+ let file_position = file_position.expect("file reaches model as a document or extracted text");
+ assert!(
+ file_caption < file_position,
+ "file part order: {file_mixed}"
+ );
+ for request in &requests {
+ let serialized = serde_json::to_string(request).unwrap();
+ assert!(!serialized.contains("typed-image-file"));
+ assert!(!serialized.contains("typed-document-file"));
+ assert!(!serialized.contains("photos/native.png"));
+ assert!(!serialized.contains("documents/native.txt"));
+ }
+ let first_pid = core.child.id();
+ core.stop();
+
+ // A new process must resolve committed native media from the same scoped
+ // claims while preparing the next text-only turn.
+ push_update(
+ &mocks.state,
+ 204,
+ 24,
+ "continue the conversation after native media restart",
+ );
+ let (mut restarted, restarted_rpc) = start_core(
+ &home,
+ &workspace,
+ &mocks.telegram,
+ &mocks.inference,
+ &module_source_dir,
+ &logs.join("typed-media-restarted.log"),
++ false,
+ );
+ assert_ne!(
+ first_pid,
+ restarted.child.id(),
+ "media resume uses a new process"
+ );
+ wait_healthy(
+ &restarted_rpc,
+ &mut restarted,
+ &logs.join("typed-media-restarted.log"),
+ )
+ .await;
+ wait_for_completion(&mocks.state, 5).await;
+ let resumed = mocks
+ .state
+ .completions
+ .lock()
+ .expect("model requests")
+ .get(4)
+ .cloned()
+ .expect("post-restart text reached the model");
+ assert!(model_saw_user_text(
+ &resumed,
+ "continue the conversation after native media restart"
+ ));
+ assert!(find_block_type(&resumed["messages"], "image_url").is_some());
+ assert_request_retains_native_file(&resumed);
+ restarted.stop();
+}
+
+ #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+ #[cfg(feature = "voice")]
+ async fn telegram_voice_note_runs_scoped_stt_agent_and_mirror_tts_through_modules() {
+ let tmp = tempfile::tempdir().expect("temporary core home");
+ let home = std::fs::canonicalize(tmp.path()).expect("canonicalize temporary home");
+ let workspace = home.join("workspace");
+ let logs = home.join("logs");
+ std::fs::create_dir_all(&logs).expect("create logs");
+ let module_source_dir = std::env::var_os("OPENHUMAN_TEST_MODULE_SOURCE_DIR")
+ .map(std::path::PathBuf::from)
+ .expect("build TinyChannels and TinyVoice and set OPENHUMAN_TEST_MODULE_SOURCE_DIR");
+ for module in ["tinychannels", "tinyvoice"] {
+ assert!(
+ module_source_dir
+ .join(module)
+ .join("modules.toml")
+ .is_file(),
+ "compiled {module} descriptor missing from {}",
+ module_source_dir.display()
+ );
+ }
+
+ let mocks = start_mock_servers().await;
+ write_config(&home, &workspace, &mocks.inference);
+ let (mut core, rpc) = start_core(
+ &home,
+ &workspace,
+ &mocks.telegram,
+ &mocks.inference,
+ &module_source_dir,
+ &logs.join("voice.log"),
+ true,
+ );
+ wait_healthy(&rpc, &mut core, &logs.join("voice.log")).await;
+ mocks
+ .state
+ .updates
+ .lock()
+ .expect("Telegram updates")
+ .push_back(voice_update(200, 20));
+
+ wait_until("voice note reaches inference", || {
+ !mocks
+ .state
+ .completions
+ .lock()
+ .expect("model requests")
+ .is_empty()
+ })
+ .await;
+
+ let requests = mocks
+ .state
+ .completions
+ .lock()
+ .expect("model requests")
+ .clone();
+ assert_eq!(
+ requests.len(),
+ 1,
+ "one inbound voice update produces one agent turn"
+ );
+ assert!(
+ model_saw_user_text(&requests[0], "the dictated voice request"),
+ "the model receives the transcript produced by the scoped cloud STT callback; file requests={:?}, downloads={}, transcription uploads={}",
- mocks.state.voice_file_requests.lock().expect("voice file requests"),
- mocks.state.voice_download_requests.lock().expect("voice download requests"),
- mocks.state.transcription_uploads.lock().expect("STT uploads").len()
++ mocks
++ .state
++ .voice_file_requests
++ .lock()
++ .expect("voice file requests"),
++ mocks
++ .state
++ .voice_download_requests
++ .lock()
++ .expect("voice download requests"),
++ mocks
++ .state
++ .transcription_uploads
++ .lock()
++ .expect("STT uploads")
++ .len()
+ );
+ assert_eq!(
+ mocks
+ .state
+ .transcription_uploads
+ .lock()
+ .expect("STT uploads")
+ .len(),
+ 1,
+ "Telegram voice media is downloaded and transcribed once"
+ );
+ assert!(
+ mocks
+ .state
+ .transcription_uploads
+ .lock()
+ .expect("STT uploads")[0]
+ .windows(4)
+ .any(|window| window == b"OggS"),
+ "the cloud STT mock receives the downloaded audio multipart payload"
+ );
+ assert_eq!(
+ mocks
+ .state
+ .voice_file_requests
+ .lock()
+ .expect("voice file requests")
+ .as_slice(),
+ &["fixture-voice-file"],
+ "the real Telegram voice file ID is resolved before scoped transcription"
+ );
+ wait_until("mirror reply reaches Telegram sendVoice", || {
+ !mocks
+ .state
+ .sent_voice_uploads
+ .lock()
+ .expect("voice uploads")
+ .is_empty()
+ })
+ .await;
+ assert_eq!(
+ mocks
+ .state
+ .speech_requests
+ .lock()
+ .expect("TTS requests")
+ .as_slice(),
+ &["mock reply 1"]
+ );
+ assert!(
+ mocks
+ .state
+ .sent_voice_uploads
+ .lock()
+ .expect("voice uploads")[0]
+ .windows(4)
+ .any(|window| window == b"OggS"),
+ "TinyVoice converts the WAV TTS response to Ogg before Telegram sendVoice"
+ );
+ let voice_log = std::fs::read_to_string(logs.join("voice.log"))
+ .expect("read captured core log after voice delivery");
+ assert!(
+ voice_log.contains("[voice message]"),
+ "captured core log should retain the content-free voice preview"
+ );
+ assert!(
+ !voice_log.contains("the dictated voice request"),
+ "voice transcripts must not be written to the core log"
+ );
+ core.stop();
+ }
++
+fn push_media_update(state: &MockState, update: Value) {
+ state
+ .updates
+ .lock()
+ .expect("Telegram updates")
+ .push_back(update);
+}
+
+async fn wait_for_media_fetch(state: &MockState, update_id: i64, file_id: &str, file_path: &str) {
+ let deadline = Instant::now() + Duration::from_secs(15);
+ loop {
+ let served = state
+ .served_update_ids
+ .lock()
+ .expect("served Telegram updates")
+ .contains(&update_id);
+ let metadata = state
+ .file_requests
+ .lock()
+ .expect("Telegram getFile requests")
+ .iter()
+ .any(|request| request == file_id);
+ let downloaded = state
+ .file_downloads
+ .lock()
+ .expect("Telegram file downloads")
+ .iter()
+ .any(|download| download == file_path);
+ assert!(
+ Instant::now() < deadline,
+ "media update {update_id} did not reach file fetch: served={served}, getFile={metadata}, downloaded={downloaded}"
+ );
+ if served && metadata && downloaded {
+ return;
+ }
+ sleep(Duration::from_millis(25)).await;
+ }
+}
diff --cc tests/raw_coverage/channels_round26_raw_coverage_e2e.rs
index 9b8edba49f,89a539126a..00936afb10
--- a/tests/raw_coverage/channels_round26_raw_coverage_e2e.rs
+++ b/tests/raw_coverage/channels_round26_raw_coverage_e2e.rs
@@@ -13,10 -13,11 +13,14 @@@ use axum::
routing::post,
Json, Router,
};
--use tinychannels::providers::telegram::TelegramChannel;
-#[allow(deprecated, reason = "this fixture covers the v21 compatibility send adapter")]
--use tinychannels::{Channel, SendMessage};
use serde_json::{json, Value};
use std::sync::{Arc, Mutex};
++use tinychannels::providers::telegram::TelegramChannel;
++#[allow(
++ deprecated,
++ reason = "this fixture covers the v21 compatibility send adapter"
++)]
++use tinychannels::{Channel, SendMessage};
#[derive(Default)]
struct TelegramMockState {
@@@ -145,7 -146,7 +149,8 @@@ fn env_lock() -> tokio::sync::MutexGuar
async fn env_lock_async() -> tokio::sync::MutexGuard<'static, ()> {
static LOCK: &std::sync::OnceLock<tokio::sync::Mutex<()>> = &crate::SHARED_ENV_LOCK;
LOCK.get_or_init(|| tokio::sync::Mutex::new(()))
-- .lock().await
++ .lock()
++ .await
}
#[tokio::test]