Skip to content

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

feat(core): add event bus for memory tool writes

Steven Enamakel957faee8f30c
diff --git a/crates/openhuman-core/src/core/bus.rs b/crates/openhuman-core/src/core/bus.rs
index 3cb5370064..3d7eb6db5e 100644
--- a/crates/openhuman-core/src/core/bus.rs
+++ b/crates/openhuman-core/src/core/bus.rs
@@ -89,7 +89,9 @@ pub const EVENTS_INTERFACE: &str = "ai.tinyhumans.openhuman.Events";
/// `1.10.0` removes the first-run harness initialization progress events.
/// `1.11.0` removes `FlowScheduleTick`: the cron scheduler fires a flow's
/// schedule trigger through the schedule runtime's `TriggerSink` instead.
-pub const EVENTS_VERSION: Version = Version::new(1, 11, 0);
+/// `1.12.0` adds optional `thread_id` and `client_id` to `MemoryStored` and
+/// `MemoryRecalled` (additive): the chat routing was an ambient task-local.
+pub const EVENTS_VERSION: Version = Version::new(1, 12, 0);
/// The bus. Initialised once by [`init`]; safe to touch before that.
pub static BUS: OnceBus<DomainEvent> = OnceBus::new();
diff --git a/crates/openhuman-core/src/core/events.rs b/crates/openhuman-core/src/core/events.rs
index 530de68b57..e2ba82ef4d 100644
--- a/crates/openhuman-core/src/core/events.rs
+++ b/crates/openhuman-core/src/core/events.rs
@@ -280,9 +280,22 @@ pub enum DomainEvent {
key: String,
category: String,
namespace: String,
+ /// Chat thread of the turn whose tool call stored the entry; `None`
+ /// outside a chat turn (CLI, cron, queued background writes).
+ thread_id: Option<String>,
+ /// Client of that turn, for the web channel to fan the activity to.
+ client_id: Option<String>,
},
/// A memory recall query completed.
- MemoryRecalled { query: String, hit_count: usize },
+ MemoryRecalled {
+ query: String,
+ hit_count: usize,
+ /// Chat thread of the turn whose tool call recalled; `None` outside a
+ /// chat turn.
+ thread_id: Option<String>,
+ /// Client of that turn.
+ client_id: Option<String>,
+ },
// ── Channels ────────────────────────────────────────────────────────
/// An inbound channel message from the transport layer, ready for processing.
diff --git a/crates/openhuman-core/src/memory/tool_writes.rs b/crates/openhuman-core/src/memory/tool_writes.rs
index 3a8aeff774..95970d0d3d 100644
--- a/crates/openhuman-core/src/memory/tool_writes.rs
+++ b/crates/openhuman-core/src/memory/tool_writes.rs
@@ -542,6 +542,9 @@ impl WriteExecutor for Executor<'_> {
key: receipt.id.0,
category: category.to_string(),
namespace: "learnings".to_string(),
+ // A queued write runs off the turn: no chat to show it in.
+ thread_id: None,
+ client_id: None,
});
Ok(())
}
diff --git a/crates/openhuman-core/src/memory/tools.rs b/crates/openhuman-core/src/memory/tools.rs
index 6c7b726686..2ad3c31772 100644
--- a/crates/openhuman-core/src/memory/tools.rs
+++ b/crates/openhuman-core/src/memory/tools.rs
@@ -129,6 +129,9 @@ pub struct CallFacts {
pub agent_id: Option<String>,
/// The provider-assigned tool-call id.
pub tool_call_id: Option<String>,
+ /// The chat the call's turn is surfaced in, so the memory activity it
+ /// raises reaches that chat. `None` outside a chat turn.
+ pub chat: Option<crate::security::approval::ApprovalChatContext>,
/// Where the agent's learnings go: its layout's learnings node.
pub namespace: Namespace,
/// What the agent may read and forget: its whole layout.
@@ -158,6 +161,7 @@ impl CallFacts {
.and_then(ToolRunContext::thread_id)
.map(str::to_string),
tool_call_id: crate::tools::host_extensions::tool_call_id(context),
+ chat: crate::agent::tinyagents::host::run_context::approval_for_tool(context).chat,
..Self::of(config, &identity)
}
}
@@ -231,6 +235,8 @@ pub async fn run_action(config: &Config, args: &Value, facts: &CallFacts) -> Too
BUS.publish(DomainEvent::MemoryRecalled {
query: question,
hit_count: view.citations.len(),
+ thread_id: facts.chat.as_ref().map(|chat| chat.thread_id.clone()),
+ client_id: facts.chat.as_ref().map(|chat| chat.client_id.clone()),
});
json!(view)
})
@@ -266,6 +272,8 @@ pub async fn run_action(config: &Config, args: &Value, facts: &CallFacts) -> Too
key: view.id.clone(),
category: kind.as_str().to_string(),
namespace: "learnings".to_string(),
+ thread_id: facts.chat.as_ref().map(|chat| chat.thread_id.clone()),
+ client_id: facts.chat.as_ref().map(|chat| chat.client_id.clone()),
});
json!({"id": view.id, "status": LEARN_STATUS})
})
diff --git a/crates/openhuman-core/src/web_chat/event_bus.rs b/crates/openhuman-core/src/web_chat/event_bus.rs
index a44c5258dc..757f259386 100644
--- a/crates/openhuman-core/src/web_chat/event_bus.rs
+++ b/crates/openhuman-core/src/web_chat/event_bus.rs
@@ -111,22 +111,14 @@ const MEMORY_ACTIVITY_QUERY_PREVIEW_CHARS: usize = 40;
/// event so the chat surface can show a brief "remembered"/"recalled N"
/// indicator.
///
-/// Routing: these domain events carry no `thread_id`/`client_id` of their
-/// own (unlike the artifact events), so this subscriber reads the current
-/// turn's chat context off the same
-/// [`crate::security::approval::APPROVAL_CHAT_CONTEXT`] task-local the
-/// artifact producers use, and drops the event when it is absent (CLI /
-/// cron / sub-agent paths — no client to fan out to). Only ever carries
+/// Routing: the events carry the `thread_id`/`client_id` of the turn whose tool
+/// call raised them (taken from that turn's explicit approval context), and the
+/// subscriber drops an event that has none (CLI / cron / sub-agent paths — no
+/// client to fan out to). Only ever carries
/// `key`/`category`/`namespace` (never stored content) and a short, clipped
/// preview of the recall query (never the full query text).
struct MemoryActivitySurfaceSubscriber;
-fn current_chat_context() -> Option<(String, String)> {
- crate::security::approval::APPROVAL_CHAT_CONTEXT
- .try_with(|ctx| (ctx.thread_id.clone(), ctx.client_id.clone()))
- .ok()
-}
-
#[async_trait]
impl EventHandler<DomainEvent> for MemoryActivitySurfaceSubscriber {
fn name(&self) -> &str {
@@ -138,13 +130,17 @@ impl EventHandler<DomainEvent> for MemoryActivitySurfaceSubscriber {
}
async fn handle(&self, event: &DomainEvent) {
- let (event_name, args) = match event {
+ let (event_name, thread_id, client_id, args) = match event {
DomainEvent::MemoryStored {
key,
category,
namespace,
+ thread_id,
+ client_id,
} => (
"stored",
+ thread_id,
+ client_id,
serde_json::json!({
"kind": "stored",
"key": key,
@@ -152,7 +148,12 @@ impl EventHandler<DomainEvent> for MemoryActivitySurfaceSubscriber {
"namespace": namespace,
}),
),
- DomainEvent::MemoryRecalled { query, hit_count } => {
+ DomainEvent::MemoryRecalled {
+ query,
+ hit_count,
+ thread_id,
+ client_id,
+ } => {
let preview: String = query
.chars()
.take(MEMORY_ACTIVITY_QUERY_PREVIEW_CHARS)
@@ -160,6 +161,8 @@ impl EventHandler<DomainEvent> for MemoryActivitySurfaceSubscriber {
let truncated = query.chars().count() > MEMORY_ACTIVITY_QUERY_PREVIEW_CHARS;
(
"recalled",
+ thread_id,
+ client_id,
serde_json::json!({
"kind": "recalled",
"query_preview": if truncated { format!("{preview}…") } else { preview },
@@ -169,7 +172,7 @@ impl EventHandler<DomainEvent> for MemoryActivitySurfaceSubscriber {
}
_ => return,
};
- let Some((thread_id, client_id)) = current_chat_context() else {
+ let (Some(thread_id), Some(client_id)) = (thread_id.clone(), client_id.clone()) else {
log::debug!("[web-channel] memory-activity-surface skip {event_name}: no chat context");
return;
};