Skip to content

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

feat(threads): add restage persisted attachments during message edit

Steven Enamakelbb20ad202caa
diff --git a/crates/openhuman-core/src/threads/attachments.rs b/crates/openhuman-core/src/threads/attachments.rs
index c85c1c9a9d..cd7d3280fb 100644
--- a/crates/openhuman-core/src/threads/attachments.rs
+++ b/crates/openhuman-core/src/threads/attachments.rs
@@ -248,6 +248,46 @@ pub(crate) async fn resolve_persisted_parts(
Ok(resolved)
}
+/// Re-stage media already owned by the exact source row for an edit.
+///
+/// The request supplies only opaque refs. Paths are recovered from the
+/// profile/thread-scoped persisted row, validated under the trusted context,
+/// then copied into fresh staging records so the replacement row receives
+/// fresh opaque IDs and does not inherit the deleted row's claim.
+pub(crate) async fn restage_persisted_parts(
+ ctx: &crate::core::runtime::DispatchCtx,
+ thread_id: &str,
+ persisted_message_id: &str,
+ requested: &[ContentPart],
+) -> Result<Vec<ContentPart>, String> {
+ use base64::Engine as _;
+
+ let mut resolved = resolve_persisted_parts(ctx, thread_id, persisted_message_id, requested)
+ .await?;
+ for part in &mut resolved {
+ let media = opaque_media_mut(part)
+ .ok_or_else(|| "stored attachment has an unsupported kind".to_string())?;
+ let path = media
+ .path
+ .take()
+ .ok_or_else(|| "stored attachment path is unavailable".to_string())?;
+ let mime = media
+ .content_type
+ .as_deref()
+ .filter(|mime| !mime.is_empty() && !mime.contains(['\r', '\n', ']']))
+ .ok_or_else(|| "stored attachment content type is unavailable".to_string())?;
+ let bytes = tokio::fs::read(&path)
+ .await
+ .map_err(|error| format!("read validated stored attachment: {error}"))?;
+ media.opaque_id = None;
+ media.url = Some(format!(
+ "data:{mime};base64,{}",
+ base64::engine::general_purpose::STANDARD.encode(bytes)
+ ));
+ }
+ stage_queued_parts(ctx, thread_id, &resolved).await
+}
+
fn opaque_media_mut(part: &mut ContentPart) -> Option<&mut MediaReference> {
match part {
ContentPart::Image { media, .. }
diff --git a/crates/openhuman-core/src/threads/ops/edit.rs b/crates/openhuman-core/src/threads/ops/edit.rs
index 7bc21c72aa..b63aef9ddb 100644
--- a/crates/openhuman-core/src/threads/ops/edit.rs
+++ b/crates/openhuman-core/src/threads/ops/edit.rs
@@ -116,8 +116,25 @@ pub async fn edit_message(
let store = transcript_store(ctx, &thread_id)
.await
.map_err(ThreadsError::Message)?;
+ let has_existing_refs = request.attachments.iter().any(|part| {
+ crate::threads::attachments::opaque_media(part)
+ .is_some_and(|media| media.opaque_id.is_some())
+ });
let staged_parts = if request.attachments.is_empty() {
Vec::new()
+ } else if has_existing_refs {
+ // Opaque refs are accepted only when they match the exact source user
+ // row being edited. Re-staging copies validated bytes into fresh
+ // records before cancellation/truncation; callers cannot nominate
+ // paths or reuse another row's refs.
+ crate::threads::attachments::restage_persisted_parts(
+ ctx,
+ &thread_id,
+ &request.message_id,
+ &request.attachments,
+ )
+ .await
+ .map_err(ThreadsError::Message)?
} else {
// This is the only staging pass. The marker-form intermediate stays
// inside the staging helper and is never persisted or sent to a turn.