Skip to content

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

refactor(tinyagents): delegate use_skill dispatch to the pack owner

Steven Enamakel17c4d50a139f
diff --git a/crates/openhuman-core/src/agent/tinyagents/mod.rs b/crates/openhuman-core/src/agent/tinyagents/mod.rs
index 8f2c17396c..11cc1b9959 100644
--- a/crates/openhuman-core/src/agent/tinyagents/mod.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/mod.rs
@@ -59,7 +59,6 @@ mod turn_plan;
pub(crate) mod turn_policy;
mod turn_run_error;
mod turn_run_finalize;
-mod use_skill_dispatch;
mod verify_before_finish;
pub(crate) use crate::agent::message_convert::chat_message_to_message;
diff --git a/crates/openhuman-core/src/agent/tinyagents/policy_denial.rs b/crates/openhuman-core/src/agent/tinyagents/policy_denial.rs
index edf999314e..207c6a3b27 100644
--- a/crates/openhuman-core/src/agent/tinyagents/policy_denial.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/policy_denial.rs
@@ -19,11 +19,11 @@
//! It is a mitigation, not a guarantee — detecting fabrication needs the
//! tool-call record carried out to the caller.
-use tinytools::status::POLICY_BLOCKED_MARKER;
-use tinytools::PermissionLevel;
use tinyagents_harness::middleware::library::{
StructuredDenialParts, StructuredDenialRenderOptions,
};
+use tinytools::status::POLICY_BLOCKED_MARKER;
+use tinytools::PermissionLevel;
/// Generic workaround for a raw security-policy / autonomy block. These denials
/// originate deep in the tools / `SecurityPolicy` layer (autonomy tier, command
@@ -172,12 +172,11 @@ impl PolicyDenial<'_> {
),
};
- StructuredDenialParts::new(blocked, reason, workaround, RELAY_INSTRUCTION).render_with_options(
- &StructuredDenialRenderOptions {
+ StructuredDenialParts::new(blocked, reason, workaround, RELAY_INSTRUCTION)
+ .render_with_options(&StructuredDenialRenderOptions {
reason_label: "Reason",
relay_separator: " ",
- },
- )
+ })
}
}
diff --git a/crates/openhuman-core/src/agent/tinyagents/tool_registration.rs b/crates/openhuman-core/src/agent/tinyagents/tool_registration.rs
index b6b622cd1d..f2346db5de 100644
--- a/crates/openhuman-core/src/agent/tinyagents/tool_registration.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/tool_registration.rs
@@ -8,7 +8,7 @@ use std::sync::Arc;
use async_trait::async_trait;
use tinyagents_graph::goals::GoalToolKind;
-use tinyagents_harness::tool::packs::USE_SKILL;
+use tinyagents_harness::tool::packs::{PackSelection, UseSkillDispatch, USE_SKILL};
use tinyagents_harness::tool::{
CanonicalSharedToolAdapter, ToolDispatch, ToolExecScope, ToolExecution, ToolExecutionContext,
};
@@ -25,7 +25,6 @@ use crate::agent::orchestration::tools::{
use crate::agent::tinyagents::host::run_context::HostOperationContext;
use crate::agent::tinyagents::host::OpenHumanRunContext;
use crate::agent::tinyagents::turn_policy::is_subagent_spawn_or_delegate_tool;
-use crate::agent::tinyagents::use_skill_dispatch::UseSkillDispatch;
use crate::agent::tools::TodoToolDispatch;
use crate::core::runtime::CoreContext;
use crate::tools::delegate::DelegateToolDispatch;
@@ -353,18 +352,28 @@ fn dispatch_selector(tool_sets: Vec<SharedToolSet>) -> DispatchSelector {
.map(str::to_string);
return typed_dispatch_for(name, adapter, delegation_target);
}
- // The pack-registry handle comes off the raw registered tool, not the
- // adapter.
- let handle = tool_sets
- .iter()
- .flat_map(|set| set.iter())
- .find(|tool| tool.name() == name)
- .and_then(|tool| crate::tools::host_extensions::pack_registry_handle(tool.as_ref()))
- .cloned()?;
- Some(Arc::new(UseSkillDispatch::new(adapter, handle)))
+ let selector: Arc<
+ tinyagents_harness::tool::packs::PackDispatchSelector<(), OpenHumanRunContext>,
+ > = Arc::new(select_packed_tool_dispatch);
+ Some(Arc::new(UseSkillDispatch::new(adapter, selector)))
})
}
+/// Build the host's usual typed dispatch for the exact pack selection pinned
+/// by TinyAgents before authorization. Only tool-specific dispatch selection
+/// remains host-owned; the owner binds and executes the packed target.
+fn select_packed_tool_dispatch(selection: PackSelection) -> Option<RegisteredToolDispatch> {
+ let name = selection.tool_name().to_owned();
+ let registry = selection.registry();
+ let delegation_target = registry
+ .iter()
+ .find(|tool| tool.name() == name)
+ .and_then(|tool| crate::tools::host_extensions::delegation_target(tool.as_ref()))
+ .map(str::to_string);
+ let adapter = shared_tool_adapter(vec![registry], &name)?;
+ typed_dispatch_for(&name, Arc::new(adapter), delegation_target)
+}
+
fn subagent_control_dispatches(
tool_sets: &[SharedToolSet],
run_context: &OpenHumanRunContext,
@@ -662,3 +671,7 @@ pub(crate) fn capability_diagnostics(
#[cfg(test)]
#[path = "tool_registration_tests.rs"]
mod tests;
+
+#[cfg(test)]
+#[path = "use_skill_dispatch_tests.rs"]
+mod use_skill_dispatch_tests;
diff --git a/crates/openhuman-core/src/agent/tinyagents/use_skill_dispatch.rs b/crates/openhuman-core/src/agent/tinyagents/use_skill_dispatch.rs
deleted file mode 100644
index 2c6948d968..0000000000
--- a/crates/openhuman-core/src/agent/tinyagents/use_skill_dispatch.rs
+++ /dev/null
@@ -1,374 +0,0 @@
-//! Typed dispatch for `use_skill` (regression R3).
-//!
-//! `use_skill` is registered as a plain canonical tool, so a call reaching a
-//! packed archetype delegation (`create_image`, `do_crypto`,
-//! `make_presentation`, …) through it used to run through
-//! `tinytools::Tool::execute_with_context`, which has no live parent
-//! `RunContext`. `dispatch_subagent_with_live_parent` refuses to run without
-//! one — "delegation requires a live harness run context." — so every packed
-//! delegate was reachable only when a model happened to call it under its
-//! bare name, and `PackedToolRouteMiddleware` actively rewrites bare packed
-//! calls INTO `use_skill`, making the bug unconditional for any tool this
-//! build packs.
-//!
-//! [`UseSkillDispatch`] closes the gap: it resolves the inner tool exactly as
-//! [`crate::tools::toolpacks::tools::UseSkillTool::execute_with_context`]
-//! does, then re-selects the same
-//! [`super::tool_registration::typed_dispatch_for`] the harness would
-//! have picked had the inner tool been natively advertised, and hands it the
-//! REAL parent this dispatch itself received. The disclosure half (no `tool`
-//! named), a missing `skill`, and a not-found tool are all delegated verbatim
-//! to the wrapped `use_skill` adapter — those paths render `UseSkillTool`'s
-//! existing schema listing / error text and need no live parent, so
-//! duplicating that logic here would only create a second place for it to
-//! drift.
-
-use std::sync::Arc;
-
-use async_trait::async_trait;
-use serde_json::Value;
-use tinyagents_harness::context::RunContext;
-use tinyagents_harness::tool::{ToolDispatch, ToolExecutionContext};
-use tinytools::{Tool, ToolCallOptions, ToolResult};
-
-use super::host::OpenHumanRunContext;
-use super::tool_registration::typed_dispatch_for;
-use crate::tools::toolpacks::registry::CATALOG;
-use tinyagents_harness::tool::packs::{named_tool, PackRegistryHandle};
-
-/// Live-parent dispatch for the `use_skill` proxy tool.
-///
-/// `tool` is the `CanonicalSharedToolAdapter` the harness registered for
-/// `use_skill` itself (used for the disclosure/error fallback paths and for
-/// `tool()`); `handle` is the same pack-registry handle `use_skill`'s own
-/// tool object carries, read off its erased host extension at registration
-/// time.
-pub(crate) struct UseSkillDispatch {
- tool: Arc<dyn Tool>,
- handle: PackRegistryHandle,
-}
-
-impl UseSkillDispatch {
- pub(crate) fn new(tool: Arc<dyn Tool>, handle: PackRegistryHandle) -> Self {
- Self { tool, handle }
- }
-}
-
-#[async_trait]
-impl ToolDispatch<(), OpenHumanRunContext> for UseSkillDispatch {
- fn tool(&self) -> Arc<dyn Tool> {
- self.tool.clone()
- }
-
- fn bind_call(
- &self,
- arguments: &Value,
- ) -> anyhow::Result<Option<Arc<dyn ToolDispatch<(), OpenHumanRunContext>>>> {
- let requested = arguments
- .get("skill")
- .and_then(Value::as_str)
- .zip(named_tool(arguments));
- let Some((skill, name)) = requested else {
- // Disclosure and malformed calls stay on the outer adapter. They
- // cannot execute a packed target.
- return Ok(None);
- };
-
- let Some(tools) = self.handle.resolve_registry_for(&CATALOG, skill, name) else {
- let result = ToolResult::error(format!(
- "{} No tool `{name}` in skill `{skill}`. Call `use_skill {{ \"skill\": \"{skill}\" }}` to see what it contains.\n\nSkills:\n{}",
- CATALOG.not_found_marker(),
- CATALOG.pack_index_markdown()
- ));
- return Ok(Some(Arc::new(BoundUseSkillDispatch {
- outer: self.tool.clone(),
- inner: None,
- inner_arguments: arguments
- .get("args")
- .cloned()
- .unwrap_or_else(|| serde_json::json!({})),
- typed: None,
- answer: Some(result),
- })));
- };
-
- let inner_arguments = arguments
- .get("args")
- .cloned()
- .unwrap_or_else(|| serde_json::json!({}));
- let delegation_target = tools
- .iter()
- .find(|tool| tool.name() == name)
- .and_then(|tool| crate::tools::host_extensions::delegation_target(tool.as_ref()))
- .map(str::to_string);
- let Some(inner_adapter) = super::tool_registration::shared_tool_adapter(vec![tools], name)
- .map(|adapter| Arc::new(adapter) as Arc<dyn Tool>)
- else {
- // Freeze the not-found result too: a target appearing after
- // authorization must not turn a rejected call into an execution.
- let marker = CATALOG.not_found_marker();
- let result =
- ToolResult::error(format!("{marker} No tool `{name}` in the requested skill."));
- return Ok(Some(Arc::new(BoundUseSkillDispatch {
- outer: self.tool.clone(),
- inner: None,
- inner_arguments,
- typed: None,
- answer: Some(result),
- })));
- };
- let typed = typed_dispatch_for(name, inner_adapter.clone(), delegation_target);
- Ok(Some(Arc::new(BoundUseSkillDispatch {
- outer: self.tool.clone(),
- inner: Some(inner_adapter),
- inner_arguments,
- typed,
- answer: None,
- })))
- }
-
- async fn execute(
- &self,
- _state: &(),
- call_id: tinyagents_harness::CallId,
- arguments: Value,
- options: ToolCallOptions,
- parent: &RunContext<OpenHumanRunContext>,
- ) -> anyhow::Result<ToolResult> {
- // Resolve `skill` + `tool` against the pack registry exactly as
- // `UseSkillTool::execute_with_context` does. Anything that does not
- // resolve here — no `skill`, the disclosure half (no `tool` named),
- // or a name the pack does not own — is a path that tool already
- // renders correctly and that touches no live parent, so fall through
- // to it verbatim rather than re-deriving the same schema listing or
- // not-found message.
- let resolved = arguments
- .get("skill")
- .and_then(Value::as_str)
- .zip(named_tool(&arguments))
- .and_then(|(skill, name)| {
- self.handle
- .resolve_registry_for(&CATALOG, skill, name)
- .map(|tools| (name.to_string(), tools))
- });
-
- let Some((name, tools)) = resolved else {
- return self
- .tool
- .execute_with_context(arguments, options, None)
- .await;
- };
-
- let inner_args = arguments
- .get("args")
- .cloned()
- .unwrap_or_else(|| serde_json::json!({}));
- let delegation_target = tools
- .iter()
- .find(|tool| tool.name() == name)
- .and_then(|tool| crate::tools::host_extensions::delegation_target(tool.as_ref()))
- .map(str::to_string);
-
- // Re-wrap the resolved tool in the same `CanonicalSharedToolAdapter`
- // seam the harness itself builds at registration. The OpenHuman
- // delegation target is read from the raw packed tool before wrapping,
- // since it is a host extension rather than portable adapter metadata.
- let Some(inner_adapter) = super::tool_registration::shared_tool_adapter(vec![tools], &name)
- .map(|adapter| Arc::new(adapter) as Arc<dyn Tool>)
- else {
- return self
- .tool
- .execute_with_context(arguments, options, None)
- .await;
- };
-
- if let Some(dispatch) = typed_dispatch_for(&name, inner_adapter.clone(), delegation_target)
- {
- return dispatch
- .execute(&(), call_id, inner_args, options, parent)
- .await;
- }
-
- // Not a typed-dispatch tool: run it the way `use_skill` always has,
- // through `Tool::execute_with_context`, but still hand it a real
- // `ToolExecutionContext` built from the live parent rather than
- // `None` — a non-recursive packed tool that reads call id, thread id
- // or workspace off the erased host extension gets the same facts a
- // native registration would have given it.
- let tool_context = ToolExecutionContext::from_run_context(parent, call_id);
- inner_adapter
- .execute_with_context(inner_args, options, Some(&tool_context))
- .await
- }
-}
-
-struct BoundUseSkillDispatch {
- outer: Arc<dyn Tool>,
- inner: Option<Arc<dyn Tool>>,
- inner_arguments: Value,
- typed: Option<Arc<dyn ToolDispatch<(), OpenHumanRunContext>>>,
- answer: Option<ToolResult>,
-}
-
-struct BoundUseSkillTool {
- outer: Arc<dyn Tool>,
- inner: Option<Arc<dyn Tool>>,
- inner_arguments: Value,
- answer: Option<ToolResult>,
-}
-
-#[async_trait]
-impl Tool for BoundUseSkillTool {
- fn name(&self) -> &str {
- self.outer.name()
- }
-
- fn description(&self) -> &str {
- self.outer.description()
- }
-
- fn parameters_schema(&self) -> Value {
- self.outer.parameters_schema()
- }
-
- fn policy(&self) -> tinytools::ToolPolicy {
- self.inner
- .as_ref()
- .map_or_else(|| self.outer.policy(), |tool| tool.policy())
- }
-
- fn supports_markdown(&self) -> bool {
- self.inner
- .as_ref()
- .is_some_and(|tool| tool.supports_markdown())
- }
-
- fn permission_level(&self) -> tinytools::PermissionLevel {
- self.inner.as_ref().map_or_else(
- || self.outer.permission_level(),
- |tool| tool.permission_level(),
- )
- }
-
- fn permission_level_with_args(&self, _arguments: &Value) -> tinytools::PermissionLevel {
- self.inner.as_ref().map_or_else(
- || self.outer.permission_level(),
- |tool| tool.permission_level_with_args(&self.inner_arguments),
- )
- }
-
- fn external_effect_with_args(&self, _arguments: &Value) -> bool {
- self.inner
- .as_ref()
- .is_some_and(|tool| tool.external_effect_with_args(&self.inner_arguments))
- }
-
- fn timeout_policy(&self, _arguments: &Value) -> tinytools::ToolTimeout {
- self.inner
- .as_ref()
- .map_or(tinytools::ToolTimeout::Inherit, |tool| {
- tool.timeout_policy(&self.inner_arguments)
- })
- }
-
- async fn prepare_authorization(&self, _arguments: &Value) -> anyhow::Result<Option<Value>> {
- match &self.inner {
- Some(tool) => tool.prepare_authorization(&self.inner_arguments).await,
- None => Ok(None),
- }
- }
-
- async fn execute(&self, _arguments: Value) -> anyhow::Result<ToolResult> {
- if let Some(answer) = &self.answer {
- return Ok(answer.clone());
- }
- match &self.inner {
- Some(tool) => tool.execute(self.inner_arguments.clone()).await,
- None => self.outer.execute(Value::Null).await,
- }
- }
-
- async fn execute_with_context(
- &self,
- _arguments: Value,
- options: ToolCallOptions,
- context: Option<&dyn tinytools::ToolRunContext>,
- ) -> anyhow::Result<ToolResult> {
- if let Some(answer) = &self.answer {
- return Ok(answer.clone());
- }
- match &self.inner {
- Some(tool) => {
- tool.execute_with_context(self.inner_arguments.clone(), options, context)
- .await
- }
- None => {
- self.outer
- .execute_with_context(Value::Null, options, context)
- .await
- }
- }
- }
-}
-
-#[async_trait]
-impl ToolDispatch<(), OpenHumanRunContext> for BoundUseSkillDispatch {
- fn tool(&self) -> Arc<dyn Tool> {
- Arc::new(BoundUseSkillTool {
- outer: self.outer.clone(),
- inner: self.inner.clone(),
- inner_arguments: self.inner_arguments.clone(),
- answer: self.answer.clone(),
- })
- }
-
- fn output_origin(&self) -> tinyagents_harness::host::ContentOrigin {
- self.typed
- .as_ref()
- .map_or(tinyagents_harness::host::ContentOrigin::Tool, |dispatch| {
- dispatch.output_origin()
- })
- }
-
- fn admission_target(&self) -> Option<(&dyn Tool, &Value)> {
- Some((self.inner.as_deref()?, &self.inner_arguments))
- }
-
- async fn execute(
- &self,
- state: &(),
- call_id: tinyagents_harness::CallId,
- _arguments: Value,
- options: ToolCallOptions,
- parent: &RunContext<OpenHumanRunContext>,
- ) -> anyhow::Result<ToolResult> {
- if let Some(answer) = &self.answer {
- return Ok(answer.clone());
- }
- let Some(inner) = &self.inner else {
- return self
- .outer
- .execute_with_context(Value::Null, options, None)
- .await;
- };
- if let Some(dispatch) = &self.typed {
- return dispatch
- .execute(
- state,
- call_id,
- self.inner_arguments.clone(),
- options,
- parent,
- )
- .await;
- }
- let context = ToolExecutionContext::from_run_context(parent, call_id);
- inner
- .execute_with_context(self.inner_arguments.clone(), options, Some(&context))
- .await
- }
-}
-
-#[cfg(test)]
-#[path = "use_skill_dispatch_tests.rs"]
-mod tests;
diff --git a/crates/openhuman-core/src/agent/tinyagents/use_skill_dispatch_tests.rs b/crates/openhuman-core/src/agent/tinyagents/use_skill_dispatch_tests.rs
index 0055f93a71..24f2c9d144 100644
--- a/crates/openhuman-core/src/agent/tinyagents/use_skill_dispatch_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/use_skill_dispatch_tests.rs
@@ -22,7 +22,7 @@ use serde_json::json;
use std::path::Path;
use std::sync::Arc;
use tinyagents_harness::context::RunConfig;
-use tinyagents_harness::tool::packs::{PackRegistryHandle, UseSkillTool};
+use tinyagents_harness::tool::packs::{PackRegistryHandle, UseSkillTool, USE_SKILL};
use tinyagents_harness::CallId;
use tinyinference_llm::message::Message;
use tinyinference_llm::model::{ChatModel, ModelProfile, ModelRequest, ModelResponse};
@@ -133,13 +133,13 @@ fn parent_execution_context(workspace_dir: &Path) -> ParentExecutionContext {
/// real `UseSkillTool` plus one packed archetype-delegate stub, a
/// `PackRegistryHandle` bound to it, and the `CanonicalSharedToolAdapter`
/// wrapping `use_skill` that the harness would have registered.
-fn build_use_skill_dispatch_with_handle() -> (UseSkillDispatch, PackRegistryHandle) {
+fn build_use_skill_dispatch_with_handle() -> (RegisteredToolDispatch, PackRegistryHandle) {
build_use_skill_dispatch_with_target(None)
}
fn build_use_skill_dispatch_with_target(
target_id: Option<&str>,
-) -> (UseSkillDispatch, PackRegistryHandle) {
+) -> (RegisteredToolDispatch, PackRegistryHandle) {
let handle = PackRegistryHandle::default();
let use_skill_tool: Box<dyn Tool> = Box::new(UseSkillTool::new(
handle.clone(),
@@ -154,13 +154,12 @@ fn build_use_skill_dispatch_with_target(
let adapter = shared_tool_adapter(vec![durable], tinyagents_harness::tool::packs::USE_SKILL)
.expect("use_skill resolves in the durable registry it was just placed in");
- (
- UseSkillDispatch::new(Arc::new(adapter), handle.clone()),
- handle,
- )
+ let dispatch = dispatch_selector(vec![durable])(USE_SKILL, Arc::new(adapter) as Arc<dyn Tool>)
+ .expect("use_skill receives TinyAgents' bound owner dispatch");
+ (dispatch, handle)
}
-fn build_use_skill_dispatch() -> UseSkillDispatch {
+fn build_use_skill_dispatch() -> RegisteredToolDispatch {
build_use_skill_dispatch_with_handle().0
}