Skip to content

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

fix(agent): prevent duplicate turn dispatch on the bus

Steven Enamakelc3fd3558026b
diff --git a/crates/openhuman-core/src/agent/bus.rs b/crates/openhuman-core/src/agent/bus.rs
index f11377d62f..89a971a40d 100644
--- a/crates/openhuman-core/src/agent/bus.rs
+++ b/crates/openhuman-core/src/agent/bus.rs
@@ -130,19 +130,24 @@ pub struct AgentTurnRequest {
/// status updates for this turn.
pub on_progress: Option<mpsc::Sender<AgentProgress>>,
- /// Trust/routing label for this turn. The bus handler scopes
- /// [`AGENT_TURN_ORIGIN`](crate::agent::turn_origin::AGENT_TURN_ORIGIN)
+ /// Trust/routing label for this turn. The bus handler scopes the origin
/// around the tool loop so the approval gate (and any other
/// origin-aware policy) sees the same value the caller intended.
///
- /// Native-bus dispatch crosses a `tokio::spawn` boundary inside the
- /// registry, so callers cannot rely on `AGENT_TURN_ORIGIN` propagating
- /// implicitly — the origin must travel as an owned field.
+ /// The origin travels as an owned field, so the handler does not depend on
+ /// the caller's ambient scope.
///
/// Defaults to [`AgentTurnOrigin::Unknown`] so any caller that fails
/// to populate this fails closed at the gate rather than silently
/// inheriting trusted-automation semantics.
pub origin: AgentTurnOrigin,
+
+ /// Where this turn's parked approvals surface (the channel's chat thread
+ /// and client), handed to the turn's run context so the gate routes a
+ /// `Prompt`-class tool call back to the channel instead of letting it
+ /// through unprompted. Default (no chat) for channels without an approval
+ /// surface and for non-channel callers such as triage.
+ pub approval: crate::security::approval::ApprovalTurnContext,
}
/// Final response from an agentic turn.
@@ -201,6 +206,7 @@ async fn handle_agent_run_turn(req: AgentTurnRequest) -> Result<AgentTurnRespons
extra_tools,
on_progress,
origin,
+ approval,
} = req;
tracing::debug!(
@@ -322,6 +328,7 @@ async fn handle_agent_run_turn(req: AgentTurnRequest) -> Result<AgentTurnRespons
on_progress,
Some(graph_origin),
sandbox_mode,
+ approval,
)
.await
}),
diff --git a/crates/openhuman-core/src/agent/harness/graph.rs b/crates/openhuman-core/src/agent/harness/graph.rs
index 0099c06f12..600981a9be 100644
--- a/crates/openhuman-core/src/agent/harness/graph.rs
+++ b/crates/openhuman-core/src/agent/harness/graph.rs
@@ -58,6 +58,7 @@ pub(crate) async fn run_channel_turn_via_graph(
on_progress: Option<Sender<AgentProgress>>,
origin: Option<crate::agent::turn_origin::AgentTurnOrigin>,
sandbox_mode: tinyagents_definition::SandboxMode,
+ approval: crate::security::approval::ApprovalTurnContext,
) -> Result<crate::agent::tinyagents::TinyagentsTurnOutcome> {
let extra_arc = Arc::new(extra_tools);
@@ -140,6 +141,7 @@ pub(crate) async fn run_channel_turn_via_graph(
let turn_origin = origin.clone();
let mut run_context = crate::agent::tinyagents::host::OpenHumanRunContext::new();
run_context.origin = origin;
+ run_context.approval = approval;
run_context.sandbox_mode = Some(sandbox_mode);
run_context.workspace = attachment_workspace.map(tinytools::WorkspaceDescriptor::new);
seed_channel_attachments(&mut run_context, &prepared);
diff --git a/crates/openhuman-core/src/agent/triage/evaluator/arm.rs b/crates/openhuman-core/src/agent/triage/evaluator/arm.rs
index 61aa4477e4..121d7e7062 100644
--- a/crates/openhuman-core/src/agent/triage/evaluator/arm.rs
+++ b/crates/openhuman-core/src/agent/triage/evaluator/arm.rs
@@ -134,6 +134,7 @@ pub(super) async fn try_arm(
// tool call originating from this turn as remote-attacker input
// (the triage agent doesn't usually invoke such tools — it
// classifies and routes — but label correctly for defense in depth).
+ approval: Default::default(),
origin: crate::agent::turn_origin::AgentTurnOrigin::ExternalChannel {
sender_name: None,
channel: envelope.source.slug().to_string(),
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/turn.rs b/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/turn.rs
index 8fad6521ab..8a942d5227 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/turn.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/turn.rs
@@ -28,6 +28,9 @@ pub(crate) struct HostChannelTurn {
pub(crate) origin: AgentTurnOrigin,
/// Live progress for the channel's draft updates.
pub(crate) progress: Option<mpsc::Sender<AgentProgress>>,
+ /// Where this turn's parked approvals surface; default for a channel
+ /// without an approval surface.
+ pub(crate) approval: crate::security::approval::ApprovalTurnContext,
}
/// Run `turn` as `host`: its definition and host tools, inside its context,
@@ -44,6 +47,7 @@ pub(crate) async fn run_host_agent_turn(
message,
origin,
progress,
+ approval,
} = turn;
let mut config = host.config.clone();
let profile_ceiling = host
@@ -93,7 +97,7 @@ pub(crate) async fn run_host_agent_turn(
session.set_on_progress(progress);
let text = host
- .scope(session.run_single_with_origin(&message, Some(origin)))
+ .scope(session.run_single_with_approval(&message, Some(origin), approval))
.await?;
Ok(AgentTurnResponse::with_resolved_route(
text,
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/processor/bus_turn.rs b/crates/openhuman-core/src/channels/runtime/dispatch/processor/bus_turn.rs
index 251acd5081..643802b4d6 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/processor/bus_turn.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/processor/bus_turn.rs
@@ -22,6 +22,7 @@ pub(super) async fn dispatch_bus_turn(
active_turn_model_source: Option<crate::agent::tinyagents::TurnModelSource>,
progress_tx: Option<tokio::sync::mpsc::Sender<AgentProgress>>,
turn_origin: AgentTurnOrigin,
+ approval: crate::security::approval::ApprovalTurnContext,
) -> anyhow::Result<AgentTurnResponse> {
// Dispatch the agentic turn through the native event bus instead of
// calling `run_tool_call_loop` directly. The agent domain registers
@@ -95,6 +96,7 @@ pub(super) async fn dispatch_bus_turn(
extra_tools: scoping.extra_tools,
on_progress: progress_tx,
origin: turn_origin,
+ approval,
};
tracing::debug!(
channel = %msg.channel,
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs b/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
index a96f64ebe9..16fe49d7cf 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
@@ -414,6 +414,24 @@ pub(crate) async fn process_channel_runtime_message(
history_key: Some(history_key.clone()),
};
+ // Sub-issue 2 of #3098: give the agent turn an `ApprovalTurnContext` for
+ // every channel with the `chat_approvals` capability (served by
+ // `ChannelApprovalSurfaceSubscriber`). Without a chat context the gate's
+ // "no chat context -> allow straight through" branch silently bypasses
+ // every `Prompt`-class tool call, voiding the `supervised` autonomy tier
+ // on the channel.
+ let approval = if channel_has_approval_surface(&msg.channel) {
+ crate::security::approval::ApprovalTurnContext::chat(
+ crate::security::approval::ApprovalChatContext {
+ thread_id: history_key.clone(),
+ client_id: msg.channel.clone(),
+ request_id: Some(msg.id.clone()),
+ },
+ )
+ } else {
+ crate::security::approval::ApprovalTurnContext::default()
+ };
+
// A channel bound to a host agent (`agent.channel_agents`) runs as that
// agent; every other channel goes over the bus to the orchestrator.
let agent_call: std::pin::Pin<
@@ -429,6 +447,7 @@ pub(crate) async fn process_channel_runtime_message(
message: enriched_message.clone(),
origin: turn_origin,
progress: progress_tx,
+ approval,
},
)),
None => Box::pin(dispatch_bus_turn(
@@ -439,28 +458,11 @@ pub(crate) async fn process_channel_runtime_message(
active_turn_model_source,
progress_tx,
turn_origin,
+ approval,
)),
};
- // Sub-issue 2 of #3098: scope the agent turn in an `ApprovalChatContext`
- // for every channel with the `chat_approvals` capability (served by
- // `ChannelApprovalSurfaceSubscriber`). Without this scope the gate's
- // "no chat context → allow straight through" branch
- // (`approval/gate.rs:219-231`) silently bypasses every `Prompt`-class
- // tool call, voiding the `supervised` autonomy tier on the channel.
- let llm_result = tokio::time::timeout(Duration::from_secs(ctx.message_timeout_secs), async {
- if channel_has_approval_surface(&msg.channel) {
- let approval_ctx = crate::security::approval::ApprovalChatContext {
- thread_id: history_key.clone(),
- client_id: msg.channel.clone(),
- request_id: Some(msg.id.clone()),
- };
- crate::security::approval::APPROVAL_CHAT_CONTEXT
- .scope(approval_ctx, agent_call)
- .await
- } else {
- agent_call.await
- }
- })
+ let llm_result =
+ tokio::time::timeout(Duration::from_secs(ctx.message_timeout_secs), agent_call)
.await;
// Wait for draft updater to finish