Skip to content

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

chore(threads): thread dispatch context through conversation store calls

Steven Enamakel9bc31f867d19
diff --git a/crates/openhuman-core/src/agent/attachments/mod.rs b/crates/openhuman-core/src/agent/attachments/mod.rs
index 526eea9353..08eab0ced1 100644
--- a/crates/openhuman-core/src/agent/attachments/mod.rs
+++ b/crates/openhuman-core/src/agent/attachments/mod.rs
@@ -167,7 +167,7 @@ pub(crate) async fn thread_action_dir(
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)
+ let bound = crate::threads::ops::thread_working_dir(DispatchCtx::ambient(), threads_dir.to_path_buf(), thread_id)
.await
.map_err(anyhow::Error::msg)?;
Ok(bound.unwrap_or_else(|| config.action_dir.clone()))
diff --git a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
index cefaedb174..d24aa38b28 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
@@ -716,6 +716,7 @@ fn persist_delivery_reply(
success: bool,
) -> Result<(), String> {
crate::threads::store::append_message(
+ DispatchCtx::ambient(),
workspace_dir,
thread_id,
crate::threads::store::ConversationMessage {
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent.rs b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent.rs
index 1ebba10de6..3a6eab2421 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent.rs
@@ -582,6 +582,7 @@ fn attach_workflow_proposal(
);
if let Some(thread_id) = parent_thread_id {
let persisted = conversations::append_message(
+ DispatchCtx::ambient(),
workspace_dir.to_path_buf(),
thread_id,
ConversationMessage {
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/spawn_subagent.rs b/crates/openhuman-core/src/agent/orchestration/tools/spawn_subagent.rs
index 663c461cf8..7b84e88608 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/spawn_subagent.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/spawn_subagent.rs
@@ -140,6 +140,7 @@ fn persist_worker_thread(
let now = chrono::Utc::now().to_rfc3339();
conversations::ensure_thread(
+ DispatchCtx::ambient(),
workspace_dir.to_path_buf(),
CreateConversationThread {
id: thread_id.clone(),
@@ -154,6 +155,7 @@ fn persist_worker_thread(
.map_err(|err| format!("ensure_thread: {err}"))?;
conversations::append_message(
+ DispatchCtx::ambient(),
workspace_dir.to_path_buf(),
&thread_id,
ConversationMessage {
@@ -172,6 +174,7 @@ fn persist_worker_thread(
.map_err(|err| format!("append user message: {err}"))?;
conversations::append_message(
+ DispatchCtx::ambient(),
workspace_dir.to_path_buf(),
&thread_id,
ConversationMessage {
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/spawn_worker_thread.rs b/crates/openhuman-core/src/agent/orchestration/tools/spawn_worker_thread.rs
index 653b143439..daf89d8727 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/spawn_worker_thread.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/spawn_worker_thread.rs
@@ -239,7 +239,7 @@ impl SpawnWorkerThreadTool {
"[spawn_worker_thread] invoked"
);
- let threads = conversations::list_threads(parent.workspace_dir.clone())
+ let threads = conversations::list_threads(DispatchCtx::ambient(), parent.workspace_dir.clone())
.map_err(|e| anyhow::anyhow!(e))?;
if let Some(current_thread) = threads.iter().find(|t| t.id == current_thread_id) {
let is_delegated_label = current_thread
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/worker_thread.rs b/crates/openhuman-core/src/agent/orchestration/tools/worker_thread.rs
index b36a39f830..f625d15ae6 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/worker_thread.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/worker_thread.rs
@@ -34,6 +34,7 @@ pub(crate) fn create_worker_thread(
let now = chrono::Utc::now().to_rfc3339();
conversations::ensure_thread(
+ DispatchCtx::ambient(),
workspace_dir.clone(),
CreateConversationThread {
id: worker_thread_id.clone(),
@@ -54,6 +55,7 @@ pub(crate) fn create_worker_thread(
);
if let Err(err) = conversations::append_message(
+ DispatchCtx::ambient(),
workspace_dir,
&worker_thread_id,
ConversationMessage {
@@ -83,6 +85,7 @@ pub(crate) fn append_worker_user_message(
prompt: &str,
) -> Result<(), String> {
conversations::append_message(
+ DispatchCtx::ambient(),
workspace_dir,
worker_thread_id,
ConversationMessage {
diff --git a/crates/openhuman-core/src/agent/subagent_host/ops/graph/worker_mirror.rs b/crates/openhuman-core/src/agent/subagent_host/ops/graph/worker_mirror.rs
index e83dc47755..690b482afc 100644
--- a/crates/openhuman-core/src/agent/subagent_host/ops/graph/worker_mirror.rs
+++ b/crates/openhuman-core/src/agent/subagent_host/ops/graph/worker_mirror.rs
@@ -50,7 +50,7 @@ fn append_worker_message(
sender: sender.to_string(),
created_at: chrono::Utc::now().to_rfc3339(),
};
- if let Err(err) = append_message(workspace_dir.to_path_buf(), thread_id, message) {
+ if let Err(err) = append_message(DispatchCtx::ambient(), workspace_dir.to_path_buf(), thread_id, message) {
tracing::debug!(
agent_id,
thread_id,
diff --git a/crates/openhuman-core/src/channels/host/remote_control.rs b/crates/openhuman-core/src/channels/host/remote_control.rs
index ff5ced51fa..8849756dd9 100644
--- a/crates/openhuman-core/src/channels/host/remote_control.rs
+++ b/crates/openhuman-core/src/channels/host/remote_control.rs
@@ -18,7 +18,7 @@ pub(crate) async fn dispatch_callback(
}
match request {
Request::ListThreads => {
- let threads = conversations::list_threads(ctx.workspace_dir.to_path_buf())
+ let threads = conversations::list_threads(DispatchCtx::ambient(), ctx.workspace_dir.to_path_buf())
.map_err(|_| tinybus::Error::failed("conversation state unavailable"))?;
Ok(Response::Threads(
threads
@@ -39,6 +39,7 @@ pub(crate) async fn dispatch_callback(
return Err(tinybus::Error::failed("channel session ended"));
}
conversations::ensure_thread(
+ DispatchCtx::ambient(),
ctx.workspace_dir.to_path_buf(),
CreateConversationThread {
id: thread.id,
diff --git a/crates/openhuman-core/src/channels/providers/relay/store.rs b/crates/openhuman-core/src/channels/providers/relay/store.rs
index bd1dc36213..f317643f6f 100644
--- a/crates/openhuman-core/src/channels/providers/relay/store.rs
+++ b/crates/openhuman-core/src/channels/providers/relay/store.rs
@@ -61,6 +61,7 @@ pub async fn record_inbound(
) -> Result<Recorded, String> {
let now = Utc::now().to_rfc3339();
conversations::ensure_thread(
+ DispatchCtx::ambient(),
workspace_dir.to_path_buf(),
CreateConversationThread {
id: thread_id.to_string(),
@@ -78,11 +79,12 @@ pub async fn record_inbound(
let lock = thread_lock(workspace_dir, thread_id);
let _guard = lock.lock().await;
let existing =
- conversations::get_messages(workspace_dir.to_path_buf(), thread_id.to_string()).await?;
+ conversations::get_messages(DispatchCtx::ambient(), workspace_dir.to_path_buf(), thread_id.to_string()).await?;
if existing.iter().any(|message| message.id == id) {
return Ok(Recorded::Duplicate);
}
conversations::append_message(
+ DispatchCtx::ambient(),
workspace_dir.to_path_buf(),
thread_id.to_string(),
ConversationMessage {
@@ -117,6 +119,7 @@ pub async fn record_reply(
return Ok(());
}
conversations::append_message(
+ DispatchCtx::ambient(),
workspace_dir.to_path_buf(),
thread_id.to_string(),
ConversationMessage {
@@ -148,7 +151,7 @@ pub async fn prior_turns(
) -> Result<Vec<ChatMessage>, String> {
let current = inbound_message_id(message_id);
let messages =
- conversations::get_messages(workspace_dir.to_path_buf(), thread_id.to_string()).await?;
+ conversations::get_messages(DispatchCtx::ambient(), workspace_dir.to_path_buf(), thread_id.to_string()).await?;
let rows = to_history(&messages, &current);
let skip = rows.len().saturating_sub(limit);
Ok(rows.into_iter().skip(skip).collect())
diff --git a/crates/openhuman-core/src/cron/scheduler/origin_context.rs b/crates/openhuman-core/src/cron/scheduler/origin_context.rs
index 8b801878fd..f896165079 100644
--- a/crates/openhuman-core/src/cron/scheduler/origin_context.rs
+++ b/crates/openhuman-core/src/cron/scheduler/origin_context.rs
@@ -57,6 +57,7 @@ async fn origin_messages(config: &Config, origin: &JobOrigin) -> Vec<(String, St
match origin {
JobOrigin::Web { thread_id, .. } => {
match crate::threads::store::blocking::get_messages(
+ DispatchCtx::ambient(),
config.workspace_dir.clone(),
thread_id.clone(),
)
diff --git a/crates/openhuman-core/src/memory/backfill.rs b/crates/openhuman-core/src/memory/backfill.rs
index cf588adb49..4459c1732c 100644
--- a/crates/openhuman-core/src/memory/backfill.rs
+++ b/crates/openhuman-core/src/memory/backfill.rs
@@ -135,12 +135,12 @@ async fn plan(
bound: Option<&BoundEngine>,
stored: &BTreeMap<String, u32>,
) -> MemoryResult<Vec<ThreadPlan>> {
- let listed = threads::list_threads(workspace_dir.to_path_buf())
+ let listed = threads::list_threads(DispatchCtx::ambient(), workspace_dir.to_path_buf())
.await
.map_err(|error| MemoryError::Engine(format!("reading chat threads failed: {error}")))?;
let mut plans = Vec::new();
for thread in listed {
- let messages = match threads::get_messages(workspace_dir.to_path_buf(), thread.id.clone())
+ let messages = match threads::get_messages(DispatchCtx::ambient(), workspace_dir.to_path_buf(), thread.id.clone())
.await
{
Ok(messages) => messages,
diff --git a/crates/openhuman-core/src/security/credentials/ops/user_scope.rs b/crates/openhuman-core/src/security/credentials/ops/user_scope.rs
index 78466c78e6..1628fcfc7f 100644
--- a/crates/openhuman-core/src/security/credentials/ops/user_scope.rs
+++ b/crates/openhuman-core/src/security/credentials/ops/user_scope.rs
@@ -74,7 +74,7 @@ pub(super) fn activate_user_scope(user_id: &str) -> Result<Vec<String>, String>
// concurrent thread RPC in this process cannot interleave.
let pre_ws = pre_login_user_dir(&root_dir).join("workspace");
let pre_ws_log = pre_ws.display().to_string();
- match conversations::purge_threads(pre_ws) {
+ match conversations::purge_threads(DispatchCtx::ambient(), pre_ws) {
Ok(stats) => {
tracing::info!(
pre_login_workspace = %pre_ws_log,
diff --git a/crates/openhuman-core/src/threads/store/bus.rs b/crates/openhuman-core/src/threads/store/bus.rs
index f3aa27bfa5..385299faf4 100644
--- a/crates/openhuman-core/src/threads/store/bus.rs
+++ b/crates/openhuman-core/src/threads/store/bus.rs
@@ -403,6 +403,7 @@ async fn persist_channel_turn(
let created_at = Utc::now().to_rfc3339();
ensure_thread(
+ DispatchCtx::ambient(),
workspace_dir.to_path_buf(),
CreateConversationThread {
id: thread_id.clone(),
@@ -416,7 +417,7 @@ async fn persist_channel_turn(
)?;
let persisted_message_id = format!("{}:{}", descriptor.role, descriptor.message_id);
- if get_messages(workspace_dir.to_path_buf(), &thread_id)?
+ if get_messages(DispatchCtx::ambient(), workspace_dir.to_path_buf(), &thread_id)?
.iter()
.any(|message| message.id == persisted_message_id)
{
@@ -429,6 +430,7 @@ async fn persist_channel_turn(
}
append_message(
+ DispatchCtx::ambient(),
workspace_dir.to_path_buf(),
&thread_id,
ConversationMessage {
diff --git a/crates/openhuman-core/src/threads/welcome_migration.rs b/crates/openhuman-core/src/threads/welcome_migration.rs
index e6a391103c..c9e157c44a 100644
--- a/crates/openhuman-core/src/threads/welcome_migration.rs
+++ b/crates/openhuman-core/src/threads/welcome_migration.rs
@@ -92,7 +92,7 @@ fn migrate_welcome_thread_labels(
WELCOME_THREAD_LABEL
);
- let threads = conversations::list_threads(workspace_dir.to_path_buf())
+ let threads = conversations::list_threads(DispatchCtx::ambient(), workspace_dir.to_path_buf())
.map_err(|e| format!("[migration::welcome-to-orchestrator] list_threads failed: {e}"))?;
let mut threads_updated = 0usize;
@@ -115,6 +115,7 @@ fn migrate_welcome_thread_labels(
let updated_at = chrono::Utc::now().to_rfc3339();
match conversations::update_thread_labels(
+ DispatchCtx::ambient(),
workspace_dir.to_path_buf(),
&thread.id,
new_labels,
diff --git a/crates/openhuman-core/src/tinymemes/mod.rs b/crates/openhuman-core/src/tinymemes/mod.rs
index 1b3ffbc8d2..2155e56313 100644
--- a/crates/openhuman-core/src/tinymemes/mod.rs
+++ b/crates/openhuman-core/src/tinymemes/mod.rs
@@ -190,7 +190,7 @@ async fn load(
let config = config.clone();
let thread_id = thread_id.to_owned();
crate::core::runtime::spawn_blocking_scoped(move || {
- crate::threads::store::get_messages(config.workspace_dir.clone(), &thread_id)
+ crate::threads::store::get_messages(DispatchCtx::ambient(), config.workspace_dir.clone(), &thread_id)
.map_err(|e| log::warn!("[tinymemes] thread history unavailable: {e}"))
.ok()
})
diff --git a/crates/openhuman-core/src/web_chat/reply_persistence.rs b/crates/openhuman-core/src/web_chat/reply_persistence.rs
index 00cd763264..6f629bc2e6 100644
--- a/crates/openhuman-core/src/web_chat/reply_persistence.rs
+++ b/crates/openhuman-core/src/web_chat/reply_persistence.rs
@@ -70,6 +70,7 @@ pub(crate) fn persist_delivered_reply(
extra_metadata["citations"] = json!(citations);
}
conversations::append_message(
+ DispatchCtx::ambient(),
workspace_dir.to_path_buf(),
thread_id,
ConversationMessage {
diff --git a/crates/openhuman-core/src/web_chat/run_task.rs b/crates/openhuman-core/src/web_chat/run_task.rs
index 9ca67e9a19..0244f86451 100644
--- a/crates/openhuman-core/src/web_chat/run_task.rs
+++ b/crates/openhuman-core/src/web_chat/run_task.rs
@@ -122,7 +122,7 @@ pub(crate) async fn run_chat_task(
// policy (which grants `action_dir` as a trusted root) and its tools are
// all built on the thread's folder.
let thread_dir =
- crate::threads::ops::thread_working_dir(config.workspace_dir.clone(), thread_id).await?;
+ crate::threads::ops::thread_working_dir(DispatchCtx::ambient(), config.workspace_dir.clone(), thread_id).await?;
if let Some(dir) = &thread_dir {
log::debug!("[web-channel] thread working folder applied thread_id={thread_id}");
config.action_dir = dir.clone();