feat(goose-agent): add tool calling (#11627)

This commit is contained in:
Jack Amadeo
2026-09-02 20:03:40 +00:00
committed by GitHub
parent d16a3ef91b
commit 0f7d763b3f
8 changed files with 1162 additions and 6 deletions
Generated
+2
View File
@@ -5337,6 +5337,8 @@ dependencies = [
"futures",
"goose-provider-types",
"rmcp 3.1.4",
"schemars 1.2.2",
"serde",
"serde_json",
"tokio",
"tokio-util",
+5 -1
View File
@@ -17,9 +17,13 @@ tokio = { workspace = true, features = ["rt-multi-thread", "macros", "sync", "ti
tokio-util = { workspace = true, features = ["compat"] }
async-trait = { workspace = true }
goose-provider-types = { version = "0.1.0-alpha.7", path = "../goose-provider-types", default-features = false }
rmcp = { workspace = true }
rmcp = { workspace = true, features = ["server"] }
uuid = { workspace = true }
serde_json = { workspace = true }
futures = { workspace = true }
tracing = { workspace = true }
tracing-futures = { workspace = true }
[dev-dependencies]
schemars.workspace = true
serde = { workspace = true, features = ["derive"] }
+7
View File
@@ -18,6 +18,13 @@ is a function of the persisted conversation, not of in-memory loop state.
- **`Inference<S, E>`** — the step that reaches the provider. Before calling it,
the machine collects tools and prompt parts from *every* operation in the list
into an `InferenceInput`.
- **`ToolOperation<S>`** — adapts caller-defined `rmcp` tools into one operation.
Register `SyncTool<S>` and `AsyncTool<S>` implementations with the typed builder
methods, or implement `ToolProvider<S>` for tools discovered dynamically per
session. Dynamic providers are queried before inference and again before
execution, so their tool names and handlers must remain stable between those
boundaries. Their schemas are sent to inference and matching calls are
dispatched against the current session.
- **`StateMachine<'a, S, E>`** — holds `Vec<Step<..>>` and a `CancellationToken`.
`step()` runs one pass, `apply()` writes effects back, `run()` loops until a
step yields to the client or no step applies.
+1
View File
@@ -2,3 +2,4 @@ pub mod events;
pub mod inference;
pub mod machine;
pub mod operation;
pub mod tool;
+55 -4
View File
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{collections::HashSet, sync::Arc};
use anyhow::{anyhow, Result};
use async_trait::async_trait;
@@ -50,6 +50,20 @@ pub struct StateMachine<'a, S, E = ConversationEffect> {
cancel: CancellationToken,
}
fn add_tools_to_inference_input(
input: &mut InferenceInput,
tool_names: &mut HashSet<String>,
tools: Vec<rmcp::model::Tool>,
) -> Result<()> {
for tool in tools {
if !tool_names.insert(tool.name.to_string()) {
anyhow::bail!("multiple operations registered tool '{}'", tool.name);
}
input.tools.push(tool);
}
Ok(())
}
impl<'a, S, E> StateMachine<'a, S, E>
where
S: MachineSession,
@@ -72,11 +86,18 @@ where
let step_fut: OperationFuture<'_, Result<OperationResult<E>>> = match step {
Step::Operation(operation) => operation.run(session, conversation, emit),
Step::Inference(inference) => {
if !inference.applies(conversation) {
continue;
}
let mut input = InferenceInput::default();
let mut tool_names = HashSet::new();
for operation in self.steps.iter().map(|step| step.operation()) {
input
.tools
.extend(operation.inference_tools(session).await?);
let tools = tokio::select! {
biased;
_ = self.cancel.cancelled() => return Ok(None),
tools = operation.inference_tools(session) => tools?,
};
add_tools_to_inference_input(&mut input, &mut tool_names, tools)?;
input
.prompt_parts
.extend(operation.prompt_parts(session, conversation).await?);
@@ -151,3 +172,33 @@ where
runtime.load(session_id).await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rejects_duplicate_tools_across_operations() {
let schema = Arc::new(serde_json::Map::new());
let mut input = InferenceInput::default();
let mut names = HashSet::new();
add_tools_to_inference_input(
&mut input,
&mut names,
vec![rmcp::model::Tool::new("duplicate", "first", schema.clone())],
)
.unwrap();
let error = add_tools_to_inference_input(
&mut input,
&mut names,
vec![rmcp::model::Tool::new("duplicate", "second", schema)],
)
.unwrap_err();
assert_eq!(
error.to_string(),
"multiple operations registered tool 'duplicate'"
);
}
}
+371
View File
@@ -0,0 +1,371 @@
use std::{collections::HashSet, sync::Arc};
use anyhow::Result;
use async_trait::async_trait;
use goose_provider_types::conversation::{
message::{Message, MessageContent, ToolRequest},
Conversation,
};
use rmcp::{
handler::server::router::tool::{AsyncTool, SyncTool, ToolBase},
model::{CallToolRequestParams, CallToolResult, ErrorData, JsonObject, Tool},
};
use serde_json::{json, Value};
use crate::operation::{
applied, messages_since_kickoff, not_applicable, Emitter, Operation, OperationFuture,
OperationResult,
};
fn empty_input_schema() -> Arc<JsonObject> {
Arc::new(
serde_json::from_value(json!({
"type": "object",
"properties": {},
"additionalProperties": false
}))
.expect("empty tool input schema is an object"),
)
}
fn definition<T: ToolBase>() -> Tool {
let mut tool = Tool::new_with_raw(
T::name(),
T::description(),
T::input_schema().unwrap_or_else(empty_input_schema),
);
if let Some(title) = T::title() {
tool = tool.with_title(title);
}
if let Some(output_schema) = T::output_schema() {
tool = tool.with_raw_output_schema(output_schema);
}
if let Some(annotations) = T::annotations() {
tool = tool.with_annotations(annotations);
}
if let Some(icons) = T::icons() {
tool = tool.with_icons(icons);
}
if let Some(meta) = T::meta() {
tool = tool.with_meta(meta);
}
tool
}
fn pending_requests(requests: Vec<ToolRequest>, tool_names: &HashSet<&str>) -> Vec<ToolRequest> {
requests
.into_iter()
.filter(|request| {
request
.tool_call
.as_ref()
.map_or(true, |call| tool_names.contains(call.name.as_ref()))
})
.collect()
}
fn interrupted_result() -> Result<CallToolResult, ErrorData> {
Ok(CallToolResult::error(vec![
rmcp::model::ContentBlock::text("Tool call was interrupted before completing"),
]))
}
fn parameters<T: ToolBase>(arguments: Option<JsonObject>) -> Result<T::Parameter, ErrorData> {
if T::input_schema().is_none() {
return Ok(T::Parameter::default());
}
serde_json::from_value(Value::Object(arguments.unwrap_or_default())).map_err(|error| {
ErrorData::invalid_params(format!("failed to deserialize parameters: {error}"), None)
})
}
fn result<T: ToolBase>(output: Result<T::Output, T::Error>) -> Result<CallToolResult, ErrorData> {
let output = output.map_err(Into::into)?;
let value = serde_json::to_value(output).map_err(|error| {
ErrorData::internal_error(format!("failed to serialize tool output: {error}"), None)
})?;
Ok(CallToolResult::structured(value))
}
/// Supplies tools whose definitions and implementations may vary by session.
///
/// A provider's tool names and their handlers must remain stable from an inference
/// advertisement until every tool call produced by that inference has been handled.
#[async_trait]
pub trait ToolProvider<S>: Send + Sync {
async fn tools(&self, session: &S) -> Result<Vec<Tool>>;
async fn call(
&self,
session: &S,
request_id: &str,
call: CallToolRequestParams,
emit: &Emitter,
) -> Result<CallToolResult, ErrorData>;
}
type ToolHandler<S> = dyn for<'a> Fn(&'a S, Option<JsonObject>) -> OperationFuture<'a, Result<CallToolResult, ErrorData>>
+ Send
+ Sync;
struct RegisteredTool<S> {
definition: Tool,
handler: Arc<ToolHandler<S>>,
}
struct RegisteredToolProvider<S> {
tools: Vec<RegisteredTool<S>>,
}
#[async_trait]
impl<S> ToolProvider<S> for RegisteredToolProvider<S>
where
S: Send + Sync + 'static,
{
async fn tools(&self, _session: &S) -> Result<Vec<Tool>> {
Ok(self
.tools
.iter()
.map(|tool| tool.definition.clone())
.collect())
}
async fn call(
&self,
session: &S,
_request_id: &str,
call: CallToolRequestParams,
_emit: &Emitter,
) -> Result<CallToolResult, ErrorData> {
let tool = self
.tools
.iter()
.find(|tool| tool.definition.name == call.name)
.ok_or_else(|| {
ErrorData::invalid_params(format!("unknown tool {}", call.name), None)
})?;
(tool.handler)(session, call.arguments).await
}
}
/// An agent operation that advertises and dispatches tools.
///
/// Tools can be registered from rmcp's typed tool traits, or supplied at runtime
/// by a [`ToolProvider`] whose definitions may vary by session.
pub struct ToolOperation<S> {
registered: RegisteredToolProvider<S>,
providers: Vec<Arc<dyn ToolProvider<S>>>,
}
impl<S> ToolOperation<S>
where
S: Send + Sync + 'static,
{
pub fn new() -> Self {
Self {
registered: RegisteredToolProvider { tools: Vec::new() },
providers: Vec::new(),
}
}
fn register(&mut self, tool: RegisteredTool<S>) {
if let Some(existing) = self
.registered
.tools
.iter_mut()
.find(|existing| existing.definition.name == tool.definition.name)
{
*existing = tool;
} else {
self.registered.tools.push(tool);
}
}
pub fn with_provider(mut self, provider: Arc<dyn ToolProvider<S>>) -> Self {
self.providers.push(provider);
self
}
pub fn with_sync_tool<T>(mut self) -> Self
where
S: Clone,
T: SyncTool<S> + Send + Sync + 'static,
{
self.register(RegisteredTool {
definition: definition::<T>(),
handler: Arc::new(|session, arguments| {
let session = session.clone();
Box::pin(async move {
let parameters = parameters::<T>(arguments)?;
tokio::task::spawn_blocking(move || {
result::<T>(T::invoke(&session, parameters))
})
.await
.map_err(|error| {
ErrorData::internal_error(
format!("synchronous tool task failed: {error}"),
None,
)
})?
})
}),
});
self
}
pub fn with_async_tool<T>(mut self) -> Self
where
T: AsyncTool<S> + Send + Sync + 'static,
{
self.register(RegisteredTool {
definition: definition::<T>(),
handler: Arc::new(|session, arguments| {
Box::pin(async move {
let parameters = parameters::<T>(arguments)?;
result::<T>(T::invoke(session, parameters).await)
})
}),
});
self
}
async fn available_tools(&self, session: &S) -> Result<Vec<(Tool, &dyn ToolProvider<S>)>> {
let mut available = self
.registered
.tools(session)
.await?
.into_iter()
.map(|tool| (tool, &self.registered as &dyn ToolProvider<S>))
.collect::<Vec<_>>();
for provider in &self.providers {
for tool in provider.tools(session).await? {
if available
.iter()
.any(|(available, _)| available.name == tool.name)
{
anyhow::bail!("multiple tool providers registered '{}'", tool.name);
}
available.push((tool, provider.as_ref()));
}
}
Ok(available)
}
}
impl<S> Default for ToolOperation<S>
where
S: Send + Sync + 'static,
{
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl<S, E> Operation<S, E> for ToolOperation<S>
where
S: Send + Sync + 'static,
E: From<Message> + Send + 'static,
{
fn name(&self) -> &'static str {
"tools"
}
async fn inference_tools(&self, session: &S) -> Result<Vec<Tool>> {
Ok(self
.available_tools(session)
.await?
.into_iter()
.map(|(tool, _)| tool)
.collect())
}
async fn run(
&self,
session: &S,
conversation: &Conversation,
emit: &Emitter,
) -> Result<OperationResult<E>> {
let turn = messages_since_kickoff(conversation)?;
let answered: HashSet<&str> = turn
.iter()
.flat_map(Message::get_tool_response_ids)
.collect();
let pending = turn
.iter()
.flat_map(|message| message.content.iter())
.filter_map(MessageContent::as_tool_request)
.filter(|request| {
!request.was_executed_externally() && !answered.contains(request.id.as_str())
})
.cloned()
.collect::<Vec<_>>();
if pending.is_empty() {
return not_applicable();
}
let available = tokio::select! {
biased;
_ = emit.cancelled() => {
let mut message = Message::user();
for request in pending {
message.add_tool_response_with_metadata(
request.id,
interrupted_result(),
request.metadata.as_ref(),
);
}
let message = emit.message(message).await;
return applied([E::from(message)]);
},
available = self.available_tools(session) => available?,
};
let available_names = available
.iter()
.map(|(tool, _)| tool.name.as_ref())
.collect();
let pending = pending_requests(pending, &available_names);
if pending.is_empty() {
return not_applicable();
}
let mut message = Message::user();
let mut cancelled = false;
for request in pending {
let provider = match request.tool_call.as_ref() {
Ok(call) => available
.iter()
.find(|(tool, _)| tool.name == call.name)
.map(|(_, provider)| *provider)
.expect("pending requests were filtered by available tools"),
Err(_) => &self.registered as &dyn ToolProvider<S>,
};
let tool_result = if cancelled || emit.cancel_token().is_cancelled() {
cancelled = true;
interrupted_result()
} else {
match request.tool_call.as_ref() {
Err(error) => Err(error.clone()),
Ok(call) => {
tokio::select! {
biased;
_ = emit.cancelled() => {
cancelled = true;
interrupted_result()
},
result = provider.call(session, &request.id, call.clone(), emit) => result,
}
}
}
};
message.add_tool_response_with_metadata(
request.id,
tool_result,
request.metadata.as_ref(),
);
}
let message = emit.message(message).await;
applied([E::from(message)])
}
}
+692
View File
@@ -0,0 +1,692 @@
use std::{
borrow::Cow,
sync::{
atomic::{AtomicBool, AtomicUsize, Ordering},
Arc,
},
};
use anyhow::Result;
use async_trait::async_trait;
use goose_agent::{
machine::{MachineSession, StateMachine, Step},
operation::{
ConversationEffect, Emitter, Inference, InferenceInput, Operation, OperationResult,
},
tool::{ToolOperation, ToolProvider},
};
use goose_provider_types::conversation::{
message::{Message, MessageContent},
Conversation,
};
use rmcp::{
handler::server::router::tool::{AsyncTool, SyncTool, ToolBase},
model::{CallToolRequestParams, CallToolResult, ErrorData, Tool},
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::json;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;
#[derive(Default, Deserialize, JsonSchema)]
struct AddInput {
left: u64,
right: u64,
}
#[derive(Serialize, JsonSchema)]
struct AddOutput {
sum: u64,
}
struct Add;
impl ToolBase for Add {
type Parameter = AddInput;
type Output = AddOutput;
type Error = ErrorData;
fn name() -> Cow<'static, str> {
"add".into()
}
fn description() -> Option<Cow<'static, str>> {
Some("Add two integers".into())
}
}
impl SyncTool<()> for Add {
fn invoke(_session: &(), input: AddInput) -> Result<AddOutput, ErrorData> {
Ok(AddOutput {
sum: input.left + input.right,
})
}
}
#[derive(Default, Deserialize, JsonSchema)]
struct GreetInput {
name: String,
}
#[derive(Serialize, JsonSchema)]
struct GreetOutput {
greeting: String,
}
struct Greet;
impl ToolBase for Greet {
type Parameter = GreetInput;
type Output = GreetOutput;
type Error = ErrorData;
fn name() -> Cow<'static, str> {
"greet".into()
}
}
impl AsyncTool<()> for Greet {
async fn invoke(_session: &(), input: GreetInput) -> Result<GreetOutput, ErrorData> {
Ok(GreetOutput {
greeting: format!("Hello, {}!", input.name),
})
}
}
fn emitter() -> Emitter {
emitter_with_token(CancellationToken::new())
}
fn emitter_with_token(cancel: CancellationToken) -> Emitter {
let (tx, _rx) = mpsc::channel(1);
Emitter::new(tx, cancel)
}
fn operation() -> ToolOperation<()> {
ToolOperation::new()
.with_sync_tool::<Add>()
.with_async_tool::<Greet>()
}
fn appended_message(result: OperationResult<ConversationEffect>) -> Message {
let OperationResult::Applied(result) = result else {
panic!("operation should apply");
};
let ConversationEffect::AppendMessage(message) = result.effects.into_iter().next().unwrap()
else {
panic!("operation should append a message");
};
message
}
#[tokio::test]
async fn advertises_user_defined_tools_directly() {
let tools = <ToolOperation<()> as Operation<(), ConversationEffect>>::inference_tools(
&operation(),
&(),
)
.await
.unwrap();
assert_eq!(tools.len(), 2);
assert_eq!(tools[0].name, "add");
assert_eq!(tools[0].description.as_deref(), Some("Add two integers"));
assert!(tools[0].input_schema["properties"]["left"].is_object());
assert_eq!(tools[1].name, "greet");
}
#[tokio::test]
async fn dispatches_calls_to_user_defined_tools() {
let conversation = Conversation::new_unvalidated([
Message::user().with_text("do both"),
Message::assistant()
.with_tool_request(
"call-1",
Ok(CallToolRequestParams::new("add").with_arguments(
serde_json::from_value(json!({"left": 2, "right": 3})).unwrap(),
)),
)
.with_tool_request(
"call-2",
Ok(CallToolRequestParams::new("greet")
.with_arguments(serde_json::from_value(json!({"name": "Goose"})).unwrap())),
),
]);
let message = appended_message(
<ToolOperation<()> as Operation<(), ConversationEffect>>::run(
&operation(),
&(),
&conversation,
&emitter(),
)
.await
.unwrap(),
);
let responses = message
.content
.iter()
.map(|content| match content {
MessageContent::ToolResponse(response) => response,
_ => panic!("expected tool responses"),
})
.collect::<Vec<_>>();
assert_eq!(responses[0].id, "call-1");
assert_eq!(
responses[0]
.tool_result
.as_ref()
.unwrap()
.structured_content,
Some(json!({"sum": 5}))
);
assert_eq!(responses[1].id, "call-2");
assert_eq!(
responses[1]
.tool_result
.as_ref()
.unwrap()
.structured_content,
Some(json!({"greeting": "Hello, Goose!"}))
);
}
struct DynamicTools {
discoveries: Arc<AtomicUsize>,
}
#[async_trait]
impl ToolProvider<bool> for DynamicTools {
async fn tools(&self, enabled: &bool) -> Result<Vec<Tool>> {
self.discoveries.fetch_add(1, Ordering::SeqCst);
Ok(if *enabled {
vec![Tool::new(
"dynamic",
"A session-dependent tool",
Arc::new(serde_json::from_value(json!({"type": "object"}))?),
)]
} else {
Vec::new()
})
}
async fn call(
&self,
_session: &bool,
request_id: &str,
call: CallToolRequestParams,
_emit: &Emitter,
) -> Result<CallToolResult, ErrorData> {
Ok(CallToolResult::structured(json!({
"request_id": request_id,
"name": call.name
})))
}
}
#[tokio::test]
async fn rediscovers_dynamic_tools_for_inference_and_execution() {
let discoveries = Arc::new(AtomicUsize::new(0));
let operation = ToolOperation::new().with_provider(Arc::new(DynamicTools {
discoveries: discoveries.clone(),
}));
let tools = <ToolOperation<bool> as Operation<bool, ConversationEffect>>::inference_tools(
&operation, &true,
)
.await
.unwrap();
assert_eq!(tools[0].name, "dynamic");
let conversation = Conversation::new_unvalidated([
Message::user().with_text("call the dynamic tool"),
Message::assistant()
.with_tool_request("dynamic-call", Ok(CallToolRequestParams::new("dynamic"))),
]);
let message = appended_message(
<ToolOperation<bool> as Operation<bool, ConversationEffect>>::run(
&operation,
&true,
&conversation,
&emitter(),
)
.await
.unwrap(),
);
assert_eq!(discoveries.load(Ordering::SeqCst), 2);
assert_eq!(
message.content[0]
.as_tool_response()
.unwrap()
.tool_result
.as_ref()
.unwrap()
.structured_content,
Some(json!({"request_id": "dynamic-call", "name": "dynamic"}))
);
}
#[tokio::test]
async fn dynamic_tools_vary_by_session() {
let operation = ToolOperation::new().with_provider(Arc::new(DynamicTools {
discoveries: Arc::new(AtomicUsize::new(0)),
}));
let enabled = <ToolOperation<bool> as Operation<bool, ConversationEffect>>::inference_tools(
&operation, &true,
)
.await
.unwrap();
let disabled = <ToolOperation<bool> as Operation<bool, ConversationEffect>>::inference_tools(
&operation, &false,
)
.await
.unwrap();
assert_eq!(enabled[0].name, "dynamic");
assert!(disabled.is_empty());
}
#[tokio::test]
async fn rejects_duplicate_dynamic_tool_names_at_both_boundaries() {
let operation = ToolOperation::new()
.with_provider(Arc::new(DynamicTools {
discoveries: Arc::new(AtomicUsize::new(0)),
}))
.with_provider(Arc::new(DynamicTools {
discoveries: Arc::new(AtomicUsize::new(0)),
}));
let kickoff = Message::user().with_text("use tools");
let inference_error =
<ToolOperation<bool> as Operation<bool, ConversationEffect>>::inference_tools(
&operation, &true,
)
.await
.unwrap_err();
assert_eq!(
inference_error.to_string(),
"multiple tool providers registered 'dynamic'"
);
let conversation = Conversation::new_unvalidated([
kickoff,
Message::assistant()
.with_tool_request("dynamic-call", Ok(CallToolRequestParams::new("dynamic"))),
]);
let execution_error = match <ToolOperation<bool> as Operation<bool, ConversationEffect>>::run(
&operation,
&true,
&conversation,
&emitter(),
)
.await
{
Ok(_) => panic!("duplicate tools should fail during execution discovery"),
Err(error) => error,
};
assert_eq!(
execution_error.to_string(),
"multiple tool providers registered 'dynamic'"
);
}
#[tokio::test]
async fn ignores_requests_outside_the_current_turn() {
let conversation = Conversation::new_unvalidated([
Message::user().with_text("old turn"),
Message::assistant().with_tool_request(
"stale-call",
Ok(CallToolRequestParams::new("add")
.with_arguments(serde_json::from_value(json!({"left": 2, "right": 3})).unwrap())),
),
Message::user().with_text("new turn"),
]);
let result = <ToolOperation<()> as Operation<(), ConversationEffect>>::run(
&operation(),
&(),
&conversation,
&emitter(),
)
.await
.unwrap();
assert!(matches!(result, OperationResult::NotApplicable));
}
#[tokio::test]
async fn responds_to_unparseable_tool_requests() {
let conversation = Conversation::new_unvalidated([
Message::user().with_text("call a tool"),
Message::assistant().with_tool_request(
"invalid-call",
Err(ErrorData::invalid_params("malformed arguments", None)),
),
]);
let message = appended_message(
<ToolOperation<()> as Operation<(), ConversationEffect>>::run(
&operation(),
&(),
&conversation,
&emitter(),
)
.await
.unwrap(),
);
let response = message.content[0].as_tool_response().unwrap();
assert_eq!(response.id, "invalid-call");
assert_eq!(
response.tool_result.as_ref().unwrap_err().message,
"malformed arguments"
);
}
#[derive(Default, Clone)]
struct BlockingSession {
started: Arc<AtomicBool>,
}
struct BlockingSyncTool;
impl ToolBase for BlockingSyncTool {
type Parameter = ();
type Output = ();
type Error = ErrorData;
fn name() -> Cow<'static, str> {
"blocking_sync".into()
}
fn input_schema() -> Option<Arc<serde_json::Map<String, serde_json::Value>>> {
None
}
}
impl SyncTool<BlockingSession> for BlockingSyncTool {
fn invoke(session: &BlockingSession, _input: ()) -> Result<(), ErrorData> {
session.started.store(true, Ordering::SeqCst);
std::thread::sleep(std::time::Duration::from_millis(100));
Ok(())
}
}
#[tokio::test]
async fn cancellation_interrupts_blocking_sync_tools() {
let session = BlockingSession::default();
let started = session.started.clone();
let operation = ToolOperation::new().with_sync_tool::<BlockingSyncTool>();
let conversation = Conversation::new_unvalidated([
Message::user().with_text("call it"),
Message::assistant()
.with_tool_request("call-1", Ok(CallToolRequestParams::new("blocking_sync"))),
]);
let cancel = CancellationToken::new();
let run_cancel = cancel.clone();
let run = tokio::spawn(async move {
<ToolOperation<BlockingSession> as Operation<BlockingSession, ConversationEffect>>::run(
&operation,
&session,
&conversation,
&emitter_with_token(run_cancel),
)
.await
});
while !started.load(Ordering::SeqCst) {
tokio::task::yield_now().await;
}
cancel.cancel();
let result = tokio::time::timeout(std::time::Duration::from_millis(50), run)
.await
.expect("operation should not wait for the blocking tool")
.unwrap()
.unwrap();
let message = appended_message(result);
assert!(message.content[0]
.as_tool_response()
.unwrap()
.tool_result
.as_ref()
.unwrap()
.is_error
.is_some_and(|is_error| is_error));
}
struct BlockingDiscovery {
started: Arc<AtomicBool>,
}
#[async_trait]
impl<S: Send + Sync> ToolProvider<S> for BlockingDiscovery {
async fn tools(&self, _session: &S) -> Result<Vec<Tool>> {
self.started.store(true, Ordering::SeqCst);
std::future::pending().await
}
async fn call(
&self,
_session: &S,
_request_id: &str,
_call: CallToolRequestParams,
_emit: &Emitter,
) -> Result<CallToolResult, ErrorData> {
unreachable!()
}
}
struct TestSession {
conversation: Conversation,
}
impl MachineSession for TestSession {
fn id(&self) -> &str {
"test"
}
fn conversation(&self) -> Option<&Conversation> {
Some(&self.conversation)
}
}
struct TestInference;
#[async_trait]
impl Operation<TestSession> for TestInference {
fn name(&self) -> &'static str {
"inference"
}
}
#[async_trait]
impl Inference<TestSession> for TestInference {
fn applies(&self, _conversation: &Conversation) -> bool {
true
}
async fn infer(
&self,
_session: &TestSession,
_conversation: &Conversation,
_input: InferenceInput,
_emit: &Emitter,
) -> Result<OperationResult<ConversationEffect>> {
unreachable!()
}
}
#[tokio::test]
async fn cancellation_interrupts_inference_discovery() {
let started = Arc::new(AtomicBool::new(false));
let operation = ToolOperation::new().with_provider(Arc::new(BlockingDiscovery {
started: started.clone(),
}));
let cancel = CancellationToken::new();
let machine = StateMachine::new(
vec![
Step::Operation(Arc::new(operation)),
Step::Inference(Arc::new(TestInference)),
],
cancel.clone(),
);
let session = TestSession {
conversation: Conversation::new_unvalidated([Message::user().with_text("use tools")]),
};
let emit = emitter_with_token(cancel.clone());
let step = tokio::spawn(async move { machine.step(&session, &emit).await });
while !started.load(Ordering::SeqCst) {
tokio::task::yield_now().await;
}
cancel.cancel();
assert!(step.await.unwrap().unwrap().is_none());
}
#[tokio::test]
async fn cancellation_interrupts_execution_discovery() {
let started = Arc::new(AtomicBool::new(false));
let operation = ToolOperation::new().with_provider(Arc::new(BlockingDiscovery {
started: started.clone(),
}));
let conversation = Conversation::new_unvalidated([
Message::user().with_text("call it"),
Message::assistant()
.with_tool_request("call-1", Ok(CallToolRequestParams::new("blocking"))),
]);
let cancel = CancellationToken::new();
let run_cancel = cancel.clone();
let run = tokio::spawn(async move {
<ToolOperation<()> as Operation<(), ConversationEffect>>::run(
&operation,
&(),
&conversation,
&emitter_with_token(run_cancel),
)
.await
});
while !started.load(Ordering::SeqCst) {
tokio::task::yield_now().await;
}
cancel.cancel();
let message = appended_message(run.await.unwrap().unwrap());
let response = message.content[0].as_tool_response().unwrap();
assert_eq!(response.id, "call-1");
assert!(response
.tool_result
.as_ref()
.unwrap()
.is_error
.is_some_and(|is_error| is_error));
}
struct BlockingTools {
calls: Arc<AtomicUsize>,
}
#[async_trait]
impl ToolProvider<()> for BlockingTools {
async fn tools(&self, _session: &()) -> Result<Vec<Tool>> {
Ok(vec![Tool::new(
"blocking",
"A tool that never finishes",
Arc::new(serde_json::from_value(json!({"type": "object"}))?),
)])
}
async fn call(
&self,
_session: &(),
_request_id: &str,
_call: CallToolRequestParams,
_emit: &Emitter,
) -> Result<CallToolResult, ErrorData> {
self.calls.fetch_add(1, Ordering::SeqCst);
std::future::pending().await
}
}
#[tokio::test]
async fn cancellation_interrupts_current_and_remaining_calls() {
let calls = Arc::new(AtomicUsize::new(0));
let operation = ToolOperation::new().with_provider(Arc::new(BlockingTools {
calls: calls.clone(),
}));
let conversation = Conversation::new_unvalidated([
Message::user().with_text("call twice"),
Message::assistant()
.with_tool_request("call-1", Ok(CallToolRequestParams::new("blocking")))
.with_tool_request("call-2", Ok(CallToolRequestParams::new("blocking"))),
]);
let cancel = CancellationToken::new();
let run_cancel = cancel.clone();
let run = tokio::spawn(async move {
<ToolOperation<()> as Operation<(), ConversationEffect>>::run(
&operation,
&(),
&conversation,
&emitter_with_token(run_cancel),
)
.await
});
while calls.load(Ordering::SeqCst) == 0 {
tokio::task::yield_now().await;
}
cancel.cancel();
let message = appended_message(run.await.unwrap().unwrap());
assert_eq!(calls.load(Ordering::SeqCst), 1);
assert_eq!(
message.get_tool_response_ids(),
["call-1", "call-2"].into_iter().collect()
);
for content in &message.content {
let result = content
.as_tool_response()
.unwrap()
.tool_result
.as_ref()
.unwrap();
assert!(result.is_error.is_some_and(|is_error| is_error));
assert_eq!(
result.content[0].as_text().unwrap().text,
"Tool call was interrupted before completing"
);
}
}
#[tokio::test]
async fn ignores_unavailable_answered_and_externally_dispatched_requests() {
let conversation = Conversation::new_unvalidated([
Message::user().with_text("call tools"),
Message::assistant()
.with_tool_request("answered", Ok(CallToolRequestParams::new("add")))
.with_tool_request(
"unavailable",
Ok(CallToolRequestParams::new("not_registered")),
)
.with_tool_request_with_metadata(
"external",
Ok(CallToolRequestParams::new("add")),
None,
Some(json!({"goose.external_dispatch": true})),
),
Message::user().with_tool_response(
"answered",
Ok(CallToolResult::structured(json!({"sum": 5}))),
),
]);
let result = <ToolOperation<()> as Operation<(), ConversationEffect>>::run(
&operation(),
&(),
&conversation,
&emitter(),
)
.await
.unwrap();
assert!(matches!(result, OperationResult::NotApplicable));
}
+29 -1
View File
@@ -1,7 +1,7 @@
use anyhow::Result;
use goose_providers::errors::ProviderError;
use regex::Regex;
use std::sync::Arc;
use std::{collections::HashSet, sync::Arc};
use async_stream::try_stream;
use futures::stream::StreamExt;
@@ -183,6 +183,16 @@ fn is_mergeable_assistant_chunk(message: &Message) -> bool {
})
}
fn ensure_unique_tool_names(tools: &[Tool]) -> Result<()> {
let mut names = HashSet::new();
for tool in tools {
if !names.insert(tool.name.as_ref()) {
anyhow::bail!("multiple tools registered '{}'", tool.name);
}
}
Ok(())
}
impl Agent {
pub async fn prepare_tools_and_prompt(
&self,
@@ -190,6 +200,7 @@ impl Agent {
working_dir: &std::path::Path,
) -> Result<(Vec<Tool>, Vec<Tool>, String, ModelConfig)> {
let tools = self.list_tools(session_id, None).await;
ensure_unique_tool_names(&tools)?;
#[cfg(feature = "code-mode")]
let code_execution_active = self
@@ -2109,3 +2120,20 @@ mod tests {
assert!(result.is_ok());
}
}
#[cfg(test)]
mod duplicate_tool_tests {
use super::*;
#[test]
fn rejects_duplicate_tool_names_for_legacy_inference() {
let schema = Arc::new(serde_json::Map::new());
let tools = vec![
Tool::new("duplicate", "first", schema.clone()),
Tool::new("duplicate", "second", schema),
];
let error = ensure_unique_tool_names(&tools).unwrap_err();
assert_eq!(error.to_string(), "multiple tools registered 'duplicate'");
}
}