Skip to content

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

fix(attachments): stage uploads in the thread's working folder (#7714)

M3gA-Mind68531d325209
diff --git a/crates/openhuman-core/src/agent/attachments/mod.rs b/crates/openhuman-core/src/agent/attachments/mod.rs
index 752f0d0926..6e6e1f3739 100644
--- a/crates/openhuman-core/src/agent/attachments/mod.rs
+++ b/crates/openhuman-core/src/agent/attachments/mod.rs
@@ -156,6 +156,23 @@ pub(crate) fn action_root(config: &Config, scope: &AttachmentAccessScope) -> Pat
.unwrap_or_else(|| config.action_dir.clone())
}
+/// The folder a thread acts in: its bound working folder, else `action_dir`.
+/// Uploads must be written, validated and read under this one root; the turn
+/// runs with `action_dir` replaced by the thread's folder (#7714).
+pub(crate) async fn thread_action_dir(
+ config: &Config,
+ threads_dir: &Path,
+ thread_id: Option<&str>,
+) -> anyhow::Result<PathBuf> {
+ let Some(thread_id) = thread_id.filter(|id| !id.is_empty()) else {
+ return Ok(config.action_dir.clone());
+ };
+ let bound = crate::threads::ops::thread_working_dir(threads_dir.to_path_buf(), thread_id)
+ .await
+ .map_err(anyhow::Error::msg)?;
+ Ok(bound.unwrap_or_else(|| config.action_dir.clone()))
+}
+
fn policy(config: &Config, root: &Path) -> SecurityPolicy {
SecurityPolicy::from_config(&config.autonomy, &config.workspace_dir, root)
}
@@ -414,6 +431,7 @@ pub(crate) async fn delegation_prompt(
args: &serde_json::Value,
workspace: Option<&tinytools::WorkspaceDescriptor>,
origin: Option<&crate::agent::turn_origin::AgentTurnOrigin>,
+ thread_id: Option<&str>,
) -> anyhow::Result<String> {
let mut prompt = prompt.to_string();
if let Some(value) = args.get("image_paths") {
@@ -442,6 +460,11 @@ pub(crate) async fn delegation_prompt(
.map_err(anyhow::Error::msg)?;
if let Some(workspace) = workspace {
config.action_dir = workspace.root.clone();
+ } else {
+ // No explicit workspace: the turn acts in its thread's folder, so the
+ // copy must land (and be checked) there too (#7714).
+ let dir = thread_action_dir(&config, &config.workspace_dir, thread_id).await?;
+ config.action_dir = dir;
}
let scope = AttachmentAccessScope {
external_channel: false,
@@ -455,6 +478,7 @@ pub(crate) async fn has_resolvable_image(
prompt: &str,
workspace: Option<&tinytools::WorkspaceDescriptor>,
origin: Option<&crate::agent::turn_origin::AgentTurnOrigin>,
+ thread_id: Option<&str>,
) -> anyhow::Result<bool> {
let rehydrated = crate::agent::multimodal::rehydrate_image_placeholders(&[
tinyagents_session::transcript::TranscriptMessage::user(prompt),
@@ -480,6 +504,11 @@ pub(crate) async fn has_resolvable_image(
}
if let Some(workspace) = workspace {
config.action_dir = workspace.root.clone();
+ } else {
+ // No explicit workspace: the turn acts in its thread's folder, so its
+ // uploads are resolved there too (#7714).
+ let dir = thread_action_dir(&config, &config.workspace_dir, thread_id).await?;
+ config.action_dir = dir;
}
let scope = AttachmentAccessScope {
external_channel: false,
diff --git a/crates/openhuman-core/src/agent/attachments/mod_access_tests.rs b/crates/openhuman-core/src/agent/attachments/mod_access_tests.rs
index 04006f34b9..aa65bb10b3 100644
--- a/crates/openhuman-core/src/agent/attachments/mod_access_tests.rs
+++ b/crates/openhuman-core/src/agent/attachments/mod_access_tests.rs
@@ -80,6 +80,7 @@ async fn delegation_rejects_malformed_explicit_paths_and_external_reads_before_c
"mention picture.png in prose",
&serde_json::json!({}),
None,
+ None,
None
)
.await
@@ -97,6 +98,7 @@ async fn delegation_rejects_malformed_explicit_paths_and_external_reads_before_c
"task",
&serde_json::json!({"image_paths": image_paths}),
None,
+ None,
None
)
.await
@@ -107,8 +109,85 @@ async fn delegation_rejects_malformed_explicit_paths_and_external_reads_before_c
&serde_json::json!({"image_paths": ["picture.png"]}),
None,
Some(&external_origin()),
+ None,
)
.await
.unwrap_err();
assert!(error.to_string().contains("external channel"));
}
+
+async fn thread_in(config: &Config, id: &str, working_dir: Option<&Path>) {
+ crate::threads::store::blocking::ensure_thread(
+ config.workspace_dir.clone(),
+ crate::threads::store::CreateConversationThread {
+ id: id.into(),
+ title: id.into(),
+ created_at: "2026-10-13T00:00:00Z".into(),
+ parent_thread_id: None,
+ labels: None,
+ personality_id: None,
+ working_dir: working_dir.map(|dir| dir.display().to_string()),
+ },
+ )
+ .await
+ .unwrap();
+}
+
+// #7714: an upload in a thread with a selected folder was written under the
+// global action_dir and then read against the thread folder, so the model
+// never saw it.
+#[tokio::test]
+async fn thread_folder_upload_is_staged_where_the_turn_reads_it() {
+ let temp = tempfile::tempdir().unwrap();
+ let folder = temp.path().join("project");
+ std::fs::create_dir_all(&folder).unwrap();
+ let folder = folder.canonicalize().unwrap();
+ let config = Config {
+ action_dir: temp.path().join("global-acting"),
+ workspace_dir: temp.path().join("internal"),
+ ..Config::default()
+ };
+ thread_in(&config, "bound", Some(&folder)).await;
+ thread_in(&config, "unbound", None).await;
+
+ let root = thread_action_dir(&config, &config.workspace_dir, Some("bound"))
+ .await
+ .unwrap();
+ assert_eq!(root, folder);
+ for unbound in [Some("unbound"), Some("missing"), Some(""), None] {
+ assert_eq!(
+ thread_action_dir(&config, &config.workspace_dir, unbound)
+ .await
+ .unwrap(),
+ config.action_dir
+ );
+ }
+
+ // Stage the way `start_chat` does, then read the way the turn does: its
+ // config acts in the thread folder and the request scope names no
+ // workspace of its own.
+ let staged = stage(
+ UPLOAD,
+ "bound",
+ &config,
+ &AttachmentAccessScope {
+ external_channel: false,
+ workspace: Some(root),
+ },
+ )
+ .await
+ .unwrap();
+ let (_, files) = parse(&staged);
+ assert_eq!(files.len(), 1);
+ let turn_config = Config {
+ action_dir: folder.clone(),
+ ..config.clone()
+ };
+ let turn_scope = AttachmentAccessScope::default();
+ assert!(!is_missing_local_reference(&turn_config, &files[0].path, &turn_scope).await);
+ let path = resolve_path(&turn_config, &files[0].path, &turn_scope)
+ .await
+ .unwrap();
+ assert_eq!(tokio::fs::read(path).await.unwrap(), b"a");
+ assert!(!config.action_dir.join("uploads").exists());
+}
diff --git a/crates/openhuman-core/src/agent/attachments/mod_tests.rs b/crates/openhuman-core/src/agent/attachments/mod_tests.rs
index 498362977d..11de97ad9c 100644
--- a/crates/openhuman-core/src/agent/attachments/mod_tests.rs
+++ b/crates/openhuman-core/src/agent/attachments/mod_tests.rs
@@ -320,6 +320,7 @@ async fn vision_without_explicit_references_fails_before_configuration_or_infere
assert!(!has_resolvable_image(
"Describe a filename mentioned only in prose: picture.png",
None,
+ None,
None
)
.await
@@ -328,6 +329,7 @@ async fn vision_without_explicit_references_fails_before_configuration_or_infere
"task",
&serde_json::json!({ "image_paths": [42] }),
None,
+ None,
None
)
.await
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/archetype_delegation.rs b/crates/openhuman-core/src/agent/orchestration/tools/archetype_delegation.rs
index b699dd5961..eeae608434 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/archetype_delegation.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/archetype_delegation.rs
@@ -194,6 +194,7 @@ pub(crate) async fn execute_archetype_delegation_with_live_parent(
.and_then(|ctx| ctx.workspace())
.or(run_context.workspace.as_ref()),
run_context.origin.as_ref(),
+ run_context.thread_id.as_deref(),
)
.await
{
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/collapsed_delegation.rs b/crates/openhuman-core/src/agent/orchestration/tools/collapsed_delegation.rs
index 20aee7b8e8..9ae3b82be8 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/collapsed_delegation.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/collapsed_delegation.rs
@@ -370,6 +370,7 @@ pub(crate) async fn execute_collapsed_delegation_with_live_parent(
.and_then(|ctx| ctx.workspace())
.or(run_context.workspace.as_ref()),
run_context.origin.as_ref(),
+ run_context.thread_id.as_deref(),
)
.await
{
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/dispatch.rs b/crates/openhuman-core/src/agent/orchestration/tools/dispatch.rs
index 4105c24c42..3485d5c066 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/dispatch.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/dispatch.rs
@@ -255,6 +255,9 @@ pub(crate) async fn dispatch_subagent_with_live_parent(
prompt,
parent_workspace_descriptor.as_ref(),
run_context.origin.as_ref(),
+ tool_context
+ .and_then(ToolRunContext::thread_id)
+ .or(run_context.thread_id.as_deref()),
)
.await
{
diff --git a/crates/openhuman-core/src/threads/ops/crud.rs b/crates/openhuman-core/src/threads/ops/crud.rs
index cfe2541eab..4b98e09c1b 100644
--- a/crates/openhuman-core/src/threads/ops/crud.rs
+++ b/crates/openhuman-core/src/threads/ops/crud.rs
@@ -200,13 +200,21 @@ async fn message_append_with_config(
"attachment count exceeds configured limit".into(),
));
}
+ // Uploads were staged under the thread's working folder, if any.
+ let workspace = crate::agent::attachments::thread_action_dir(
+ config,
+ persistence_dir,
+ Some(&request.thread_id),
+ )
+ .await
+ .map_err(|error| ThreadsError::Message(format!("validate user attachment: {error}")))?;
for file in &durable_files {
let scope = crate::agent::attachments::AttachmentAccessScope {
external_channel: matches!(
origin,
Some(crate::agent::turn_origin::AgentTurnOrigin::ExternalChannel { .. })
),
- workspace: Some(config.action_dir.clone()),
+ workspace: Some(workspace.clone()),
};
let path = crate::agent::attachments::resolve_path(config, &file.path, &scope)
.await
diff --git a/crates/openhuman-core/src/web_chat/ops/start_chat.rs b/crates/openhuman-core/src/web_chat/ops/start_chat.rs
index 9d6d8c37e1..d7c26a812c 100644
--- a/crates/openhuman-core/src/web_chat/ops/start_chat.rs
+++ b/crates/openhuman-core/src/web_chat/ops/start_chat.rs
@@ -195,13 +195,22 @@ async fn start_chat_inner(
.map_err(|error| {
StartChatError::Other(format!("upload configuration unavailable: {error}"))
})?;
+ // Stage where the turn will read: the thread's working folder when
+ // one is bound (`run_task` applies the same folder), else action_dir.
+ let workspace = crate::agent::attachments::thread_action_dir(
+ &config,
+ &config.workspace_dir,
+ Some(&thread_id),
+ )
+ .await
+ .map_err(|error| StartChatError::Other(format!("upload could not be saved: {error}")))?;
crate::agent::attachments::stage(
&message,
&thread_id,
&config,
&crate::agent::attachments::AttachmentAccessScope {
external_channel: false,
- workspace: Some(config.action_dir.clone()),
+ workspace: Some(workspace),
},
)
.await