pub struct ToolRegistry { /* private fields */ }Expand description
Registry of tools available to an LLM session.
Why: Centralizes tool lookup and schema emission so the PM loop and
multi-turn sub-agent loop stay short and test-friendly.
What: HashMap<String, Arc<dyn ToolExecutor>> with register, dispatch,
dispatch_gated, dispatch_for_user, schemas, and filter_tools_for_user.
Test: See unit tests below.
Implementations§
Source§impl ToolRegistry
impl ToolRegistry
Sourcepub fn new() -> Self
pub fn new() -> Self
Construct an empty registry.
Why: Callers build up the registry incrementally via register.
What: Returns a ToolRegistry with no tools registered.
Test: new_registry_is_empty.
Sourcepub fn register(&mut self, tool: Arc<dyn ToolExecutor>)
pub fn register(&mut self, tool: Arc<dyn ToolExecutor>)
Register a tool by its name().
Why: Using the tool’s own name prevents mismatches between registered
key and schema-declared function name.
What: Inserts into the map. A debug_assert! panics in debug builds on
duplicate names so collisions surface during development.
Test: register_then_dispatch_succeeds, register_panics_on_duplicate_in_debug.
Sourcepub fn merge_missing(&mut self, other: &ToolRegistry)
pub fn merge_missing(&mut self, other: &ToolRegistry)
Copy in every tool from other this registry does not already hold.
Why (#8031): a --no-delegate run merges the NAMED agent’s own tool set
(built by the same RegistryFactory a delegation of that agent would
use) into a top-level registry that already carries the harness tools —
finish_task, use_skill — the factory also builds. Self::register
asserts on a duplicate name, so the overlap needs a first-wins merge
rather than a loop of register calls.
What: for each of other’s entries, inserts a cloned Arc only when
this registry has no tool of that name; an existing registration always
wins. Order-independent.
Test: merge_missing_adds_new_tools, merge_missing_keeps_the_existing_tool.
Sourcepub fn contains(&self, name: &str) -> bool
pub fn contains(&self, name: &str) -> bool
Whether a tool with the given name is registered.
Why: Lets callers skip registration when a tool already exists.
What: HashMap membership check.
Test: contains_after_register.
Sourcepub async fn dispatch(&self, name: &str, args: Value) -> ToolResult
pub async fn dispatch(&self, name: &str, args: Value) -> ToolResult
Dispatch a named tool call.
Why: Single place where missing-tool errors are surfaced as a structured
ToolResult::Error so the LLM loop can continue.
What: Looks up name, calls execute(args), returns the ToolResult.
Test: Dispatch to an unregistered name returns ToolResult::Error;
dispatch to a registered one returns ToolResult::Success.
Sourcepub async fn dispatch_gated(
&self,
name: &str,
args: Value,
allowed: Option<&[String]>,
) -> ToolResult
pub async fn dispatch_gated( &self, name: &str, args: Value, allowed: Option<&[String]>, ) -> ToolResult
Dispatch with an optional per-agent allowlist.
Why: Per-agent tool gating prevents, e.g., the plan-agent from shelling
out or the research-agent from delegating. Allowlist is checked
centrally so every call site inherits the same policy.
What: If allowed is Some and does not contain name, returns a
recoverable ToolResult::Error naming what is permitted. Otherwise
delegates to dispatch.
Test: dispatch_gated_rejects_disallowed.
Sourcepub async fn dispatch_for_user(
&self,
name: &str,
args: Value,
allowed: Option<&[String]>,
user: &UserIdentity,
) -> ToolResult
pub async fn dispatch_for_user( &self, name: &str, args: Value, allowed: Option<&[String]>, user: &UserIdentity, ) -> ToolResult
Dispatch honoring a UserIdentity’s ServiceTier.
Why: Some tools must be denied to less-trusted transports (Slack guest
users, unauthenticated HTTP) even if the LLM tries to call them.
Enforcing the tier check at the dispatch boundary means transport authors
can’t accidentally bypass RBAC by forgetting to pre-filter the tool list.
What: If the tool is registered AND user.can_access_tier is false,
returns ToolResult::Error. Otherwise delegates to dispatch_gated.
Test: dispatch_for_user_blocks_restricted_tier.
Sourcepub fn gated(&self, allowed: &[String]) -> ToolRegistry
pub fn gated(&self, allowed: &[String]) -> ToolRegistry
Build a new registry containing only the tools named in allowed.
Why: The agent loop calls dispatch_gated(name, args, None) — it does
not thread a per-agent allowlist through every dispatch. The robust place
to enforce an agent’s tools.allowed (issue #1029 acceptance criterion)
is therefore before the loop runs: hand the loop a registry that simply
does not contain the denied tools, so a denied call surfaces as the same
structured “no tool registered” error and the model can self-correct.
Unknown names in allowed are ignored (they register nothing); this keeps
a typo in an agent config from being a hard error.
What: Returns a fresh ToolRegistry whose map holds cloned Arcs for
exactly the registered tools whose name() appears in allowed,
preserving the shared executors (no re-construction). Order-independent.
Test: gated_keeps_only_allowed, gated_ignores_unknown_names.
Sourcepub fn schemas(&self) -> Vec<Value>
pub fn schemas(&self) -> Vec<Value>
Emit all registered tool schemas as raw JSON values.
Why: The sub-agent tool-calling loop attaches raw JSON schemas to LLM
requests; the OpenAI-compatible schema is produced by each tool.
What: Collects tool.schema() for every registered tool.
Test: Registering two tools returns a vector of length 2.
Sourcepub fn schema_for(&self, name: &str) -> Option<Value>
pub fn schema_for(&self, name: &str) -> Option<Value>
Return the raw schema for a single registered tool by name.
Why: llm::ToolCallExtractor (#1023) validates one tool call’s
arguments at a time; looking up exactly one schema is cheaper and
simpler than enumerating and filtering the full schemas() vector on
every dispatch.
What: Returns Some(tool.schema()) for a registered name, else None.
Test: schema_for_returns_registered_tool_schema, schema_for_none_when_missing.
Sourcepub fn filter_tools_for_user(
&self,
user: &UserIdentity,
) -> Vec<Arc<dyn ToolExecutor>>
pub fn filter_tools_for_user( &self, user: &UserIdentity, ) -> Vec<Arc<dyn ToolExecutor>>
Return the subset of registered tools that user may invoke.
Why: Schema emission for the LLM request must reflect what the user can
actually call, otherwise the model will hallucinate denied calls.
What: Returns cloned Arcs for tools where user.can_access_tier is true.
Test: filter_tools_for_user_drops_restricted.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for ToolRegistry
impl !UnwindSafe for ToolRegistry
impl Freeze for ToolRegistry
impl Send for ToolRegistry
impl Sync for ToolRegistry
impl Unpin for ToolRegistry
impl UnsafeUnpin for ToolRegistry
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more