Skip to content

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

fix(security): correct approval gate state transition on media generation

Steven Enamakelb3635516a2be
diff --git a/crates/openhuman-core/src/media/generation/mod.rs b/crates/openhuman-core/src/media/generation/mod.rs
index d771ce074c..ffb85bc99f 100644
--- a/crates/openhuman-core/src/media/generation/mod.rs
+++ b/crates/openhuman-core/src/media/generation/mod.rs
@@ -14,9 +14,11 @@
//! privacy and budget gates ([`provider`]), plus tool names, descriptions and
//! the local-reference policy ([`tools`]).
+mod artifact_tool;
pub mod provider;
pub mod tools;
+pub use artifact_tool::MediaArtifactTool;
pub use provider::{managed_generators, MediaGenerators, OPENROUTER_PROXY_PATH};
pub use tools::{
build_media_tools, media_tools_from, MediaListModelsTool, IMAGE_TOOL_NAME,
diff --git a/crates/openhuman-core/src/security/approval/gate_state.rs b/crates/openhuman-core/src/security/approval/gate_state.rs
index c278806c1f..938a793e99 100644
--- a/crates/openhuman-core/src/security/approval/gate_state.rs
+++ b/crates/openhuman-core/src/security/approval/gate_state.rs
@@ -185,6 +185,29 @@ impl ApprovalGate {
waiters.remove(request_id)
}
+ /// Record the routing correlation for a newly-parked request. Called at
+ /// park time in `intercept_audited_inner`, alongside the `thread_to_request`
+ /// insert.
+ pub(super) fn insert_request_route(&self, request_id: &str, route: super::gate::RequestRoute) {
+ self.request_routes.lock().insert(request_id.to_string(), route);
+ }
+
+ /// Remove and return the routing correlation for `request_id`, if any.
+ /// Consumed exactly once per request — by whichever path resolves the
+ /// decision first (`decide`, a TTL timeout, or a dropped channel).
+ pub(super) fn take_request_route(&self, request_id: &str) -> Option<super::gate::RequestRoute> {
+ self.request_routes.lock().remove(request_id)
+ }
+
+ /// Drop the routing correlation for `request_id` without reading it —
+ /// used on the caller-bound-abandon path, which leaves the row pending
+ /// and must not consume the route a later real decision still needs...
+ /// except the abandon path removes the routing precisely because no
+ /// later decision from THIS process will use it (see `WaiterGuard`).
+ pub(super) fn clear_request_route(&self, request_id: &str) {
+ self.request_routes.lock().remove(request_id);
+ }
+
fn evict_waiter(&self, request_id: &str) {
let mut waiters = self.waiters.lock();
waiters.remove(request_id);
diff --git a/crates/openhuman-core/src/web_chat/event_bus.rs b/crates/openhuman-core/src/web_chat/event_bus.rs
index 7f598ecfd8..44ef0c736f 100644
--- a/crates/openhuman-core/src/web_chat/event_bus.rs
+++ b/crates/openhuman-core/src/web_chat/event_bus.rs
@@ -74,6 +74,165 @@ pub fn register_artifact_surface_subscriber() {
}
}
+static AGENT_SURFACE_HANDLE: OnceLock<SubscriptionHandle> = OnceLock::new();
+
+/// Register the agent-surface bridge that turns thread-goal, thread-todo, and
+/// run-queue lifecycle events (domain `"agent"`) into `thread_goal_updated` /
+/// `thread_goal_cleared` / `thread_todos_changed` / `queue_item_queued` /
+/// `queue_item_delivered` web-channel events (C3: goals/todos/queue UI).
+/// Idempotent via a process-level [`OnceLock`].
+pub fn register_agent_surface_subscriber() {
+ if AGENT_SURFACE_HANDLE.get().is_some() {
+ return;
+ }
+ match crate::core::bus::BUS.subscribe(Arc::new(AgentSurfaceSubscriber)) {
+ Some(handle) => {
+ let _ = AGENT_SURFACE_HANDLE.set(handle);
+ log::info!(
+ "[web-channel] agent-surface subscriber registered (domain=agent) — bridges ThreadGoalUpdated/ThreadGoalCleared/ThreadTodosChanged/RunQueue* → thread_goal_updated/thread_goal_cleared/thread_todos_changed/queue_item_queued/queue_item_delivered socket events"
+ );
+ }
+ None => {
+ log::warn!(
+ "[web-channel] failed to register agent-surface subscriber — bus not initialized"
+ );
+ }
+ }
+}
+
+/// Bridge thread-goal / thread-todo / run-queue [`DomainEvent`]s onto the web
+/// channel. These events carry only a `thread_id` (no `client_id` — a goal,
+/// todo list, or queue is thread-scoped, not client-scoped), so every emitted
+/// [`WebChannelEvent`] uses an empty `client_id`; `emit_web_channel_event`
+/// still routes it to the `thread:<id>` room because room selection only
+/// requires a non-empty `thread_id` and a `client_id` that isn't `"system"`.
+struct AgentSurfaceSubscriber;
+
+#[async_trait]
+impl EventHandler<DomainEvent> for AgentSurfaceSubscriber {
+ fn name(&self) -> &str {
+ "web_chat::agent_surface"
+ }
+
+ fn domains(&self) -> Option<&[&str]> {
+ Some(&["agent"])
+ }
+
+ async fn handle(&self, event: &DomainEvent) {
+ match event {
+ DomainEvent::ThreadGoalUpdated {
+ thread_id, goal, ..
+ } => {
+ log::debug!(
+ "[web-channel] agent-surface emitting thread_goal_updated thread_id={thread_id}"
+ );
+ publish_web_channel_event(WebChannelEvent {
+ event: "thread_goal_updated".to_string(),
+ client_id: String::new(),
+ thread_id: thread_id.clone(),
+ goal: goal.clone(),
+ ..Default::default()
+ });
+ }
+ DomainEvent::ThreadGoalCleared { thread_id } => {
+ log::debug!(
+ "[web-channel] agent-surface emitting thread_goal_cleared thread_id={thread_id}"
+ );
+ publish_web_channel_event(WebChannelEvent {
+ event: "thread_goal_cleared".to_string(),
+ client_id: String::new(),
+ thread_id: thread_id.clone(),
+ ..Default::default()
+ });
+ }
+ DomainEvent::ThreadTodosChanged { thread_id, todos } => {
+ log::debug!(
+ "[web-channel] agent-surface emitting thread_todos_changed thread_id={thread_id}"
+ );
+ publish_web_channel_event(WebChannelEvent {
+ event: "thread_todos_changed".to_string(),
+ client_id: String::new(),
+ thread_id: thread_id.clone(),
+ todos: Some(todos.clone()),
+ ..Default::default()
+ });
+ }
+ DomainEvent::RunQueueMessageQueued {
+ thread_id,
+ item_id,
+ text_preview,
+ ..
+ }
+ | DomainEvent::RunQueueSteerRequeued {
+ thread_id,
+ item_id,
+ text_preview,
+ ..
+ } => {
+ let Some(item_id) = item_id.clone() else {
+ return;
+ };
+ log::debug!(
+ "[web-channel] agent-surface emitting queue_item_queued thread_id={thread_id} item_id={item_id}"
+ );
+ publish_web_channel_event(WebChannelEvent {
+ event: "queue_item_queued".to_string(),
+ client_id: String::new(),
+ thread_id: thread_id.clone(),
+ queue_item: Some(crate::core::socketio::QueueItemPayload {
+ id: item_id,
+ lane: None,
+ text_preview: text_preview.clone(),
+ }),
+ ..Default::default()
+ });
+ }
+ DomainEvent::RunQueueMessageDelivered {
+ thread_id,
+ mode,
+ item_id,
+ text_preview,
+ ..
+ }
+ | DomainEvent::RunQueueFollowupDispatched {
+ thread_id,
+ item_id,
+ text_preview,
+ ..
+ }
+ | DomainEvent::RunQueueInterrupted {
+ thread_id,
+ item_id,
+ text_preview,
+ ..
+ } => {
+ let Some(item_id) = item_id.clone() else {
+ return;
+ };
+ let lane = match event {
+ DomainEvent::RunQueueMessageDelivered { .. } => Some(mode.clone()),
+ _ => None,
+ };
+ log::debug!(
+ "[web-channel] agent-surface emitting queue_item_delivered thread_id={thread_id} item_id={item_id}"
+ );
+ publish_web_channel_event(WebChannelEvent {
+ event: "queue_item_delivered".to_string(),
+ client_id: String::new(),
+ thread_id: thread_id.clone(),
+ queue_item: Some(crate::core::socketio::QueueItemPayload {
+ id: item_id,
+ lane,
+ text_preview: text_preview.clone(),
+ }),
+ ..Default::default()
+ });
+ }
+ _ => {}
+ }
+ }
+}
+
static EGRESS_SURFACE_HANDLE: OnceLock<SubscriptionHandle> = OnceLock::new();
/// Register the egress-surface bridge that turns