agent loop: make tool operations own tool call checks (#12865)
This commit is contained in:
@@ -17,7 +17,11 @@ is a function of the persisted conversation, not of in-memory loop state.
|
||||
`applied()`, `yielded()`, `yielded_with()`.
|
||||
- **`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`.
|
||||
into an `InferenceInput`. It runs whenever the conversation ends on a user or
|
||||
tool message, even if other tool calls from the same reply are still
|
||||
unanswered. A pipeline that keeps calls open, for example while waiting for
|
||||
the user to approve one, needs an operation before inference that yields
|
||||
while any call is open.
|
||||
- **`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
|
||||
|
||||
@@ -7,9 +7,7 @@ use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use futures::StreamExt;
|
||||
use goose_provider_types::base::Provider;
|
||||
use goose_provider_types::conversation::message::{
|
||||
ActionRequiredData, InferenceMetadata, Message, MessageContent,
|
||||
};
|
||||
use goose_provider_types::conversation::message::{InferenceMetadata, Message, MessageContent};
|
||||
use goose_provider_types::conversation::token_usage::ProviderUsage;
|
||||
use goose_provider_types::conversation::{
|
||||
effective_role, fix_conversation, merge_consecutive_messages_for_request, Conversation,
|
||||
@@ -276,35 +274,7 @@ fn ends_with_provider_turn(messages: &[Message]) -> bool {
|
||||
})
|
||||
}
|
||||
|
||||
fn has_open_tool_approval(turn: &[Message]) -> bool {
|
||||
let mut goose_calls = HashSet::new();
|
||||
let mut answered = HashSet::new();
|
||||
let mut prompted = Vec::new();
|
||||
for content in turn.iter().flat_map(|message| &message.content) {
|
||||
match content {
|
||||
MessageContent::ToolRequest(request) if !request.was_executed_externally() => {
|
||||
goose_calls.insert(request.id.as_str());
|
||||
}
|
||||
MessageContent::ToolResponse(response) => {
|
||||
answered.insert(response.id.as_str());
|
||||
}
|
||||
MessageContent::ActionRequired(action) => {
|
||||
if let ActionRequiredData::ToolConfirmation { id, .. } = &action.data {
|
||||
prompted.push(id.as_str());
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
prompted
|
||||
.into_iter()
|
||||
.any(|id| goose_calls.contains(id) && !answered.contains(id))
|
||||
}
|
||||
|
||||
fn should_infer(conversation: &Conversation, turn: &[Message]) -> bool {
|
||||
if has_open_tool_approval(turn) {
|
||||
return false;
|
||||
}
|
||||
let projected = messages_for_provider(conversation, turn, true);
|
||||
if projected
|
||||
.last()
|
||||
@@ -590,9 +560,6 @@ impl<S: MaybeSync, E: InferenceEffect> Inference<S, E> for InferenceRunner<'_, S
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use goose_provider_types::conversation::message::TOOL_META_EXTERNAL_DISPATCH_KEY;
|
||||
use goose_provider_types::permission::Permission;
|
||||
use rmcp::model::{CallToolRequestParams, CallToolResult};
|
||||
|
||||
#[test]
|
||||
fn provider_session_id_comes_only_from_latest_inference() {
|
||||
@@ -631,70 +598,6 @@ mod tests {
|
||||
));
|
||||
}
|
||||
|
||||
fn call(id: &str) -> Message {
|
||||
Message::assistant().with_tool_request(id, Ok(CallToolRequestParams::new("write")))
|
||||
}
|
||||
|
||||
fn prompt(id: &str) -> Message {
|
||||
Message::assistant()
|
||||
.with_action_required(id, "write".to_string(), Default::default(), None)
|
||||
.user_only()
|
||||
}
|
||||
|
||||
fn answer(id: &str) -> Message {
|
||||
Message::user().with_tool_response(id, Ok(CallToolResult::success(Vec::new())))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_tool_approval_stays_open_until_the_call_is_answered() {
|
||||
assert!(has_open_tool_approval(&[call("write"), prompt("write")]));
|
||||
|
||||
let decided = Message::user().with_content(
|
||||
MessageContent::action_required_tool_confirmation_response(
|
||||
"write",
|
||||
Permission::AllowOnce,
|
||||
),
|
||||
);
|
||||
assert!(has_open_tool_approval(&[
|
||||
call("write"),
|
||||
prompt("write"),
|
||||
decided
|
||||
]));
|
||||
|
||||
assert!(!has_open_tool_approval(&[
|
||||
call("write"),
|
||||
prompt("write"),
|
||||
answer("write")
|
||||
]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prompts_without_a_goose_call_are_not_open() {
|
||||
assert!(!has_open_tool_approval(&[prompt("cli-request")]));
|
||||
|
||||
let external = Message::assistant().with_tool_request_with_metadata(
|
||||
"external",
|
||||
Ok(CallToolRequestParams::new("write")),
|
||||
None,
|
||||
Some(serde_json::json!({ TOOL_META_EXTERNAL_DISPATCH_KEY: true })),
|
||||
);
|
||||
assert!(!has_open_tool_approval(&[external, prompt("external")]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_inference_while_a_tool_approval_is_open() {
|
||||
let conversation = Conversation::new_unvalidated([
|
||||
Message::user().with_text("add and write"),
|
||||
Message::assistant()
|
||||
.with_tool_request("free", Ok(CallToolRequestParams::new("add")))
|
||||
.with_tool_request("gated", Ok(CallToolRequestParams::new("write"))),
|
||||
answer("free"),
|
||||
prompt("gated"),
|
||||
]);
|
||||
let turn = messages_since_kickoff(&conversation).unwrap();
|
||||
assert!(!should_infer(&conversation, turn));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn whitespace_only_text_is_an_empty_response() {
|
||||
assert!(is_empty_response(&Message::assistant().with_text(" \n\t ")));
|
||||
|
||||
@@ -19,14 +19,9 @@ use crate::agents::provider_manager::ProviderManager;
|
||||
use crate::agents::state_machine::ops_recipe;
|
||||
use crate::agents::state_machine::{
|
||||
has_unapplied_tool_confirmation_response, pending_tool_confirmations,
|
||||
persist_tool_confirmation_decision, run_goose, BangShellOperation, CompactionOperation,
|
||||
DoctorOperation, Emitter, EmptyResponseOperation, ExitOnErrorOperation, GooseEffect,
|
||||
GooseInferenceProvider, GooseInferenceRequestPreparer, InferenceRunner, MaxTurnsOperation,
|
||||
Operation, ProjectOperation, RecipeOperation, RetryOperation, SkillOperation,
|
||||
SlashCommandOperation, StateMachine, StatusOperation, SteerOperation, SteerQueue, Step,
|
||||
StopHookOperation, SubagentOperation, ToolApprovalOperation, ToolExecutionOperation,
|
||||
ToolPairCompactionOperation, UnknownToolOperation,
|
||||
persist_tool_confirmation_decision, run_goose, Emitter, GooseEffect, StateMachine, SteerQueue,
|
||||
};
|
||||
use crate::agents::state_machine_builder::{build_state_machine, StateMachineConfig};
|
||||
use crate::agents::subagent_handler::SubagentRunner;
|
||||
use crate::agents::types::{
|
||||
SessionConfig, DEFAULT_ON_FAILURE_TIMEOUT_SECONDS, DEFAULT_RETRY_TIMEOUT_SECONDS,
|
||||
@@ -598,90 +593,27 @@ impl Agent {
|
||||
.unwrap_or_else(|_| {
|
||||
crate::context_mgmt::compute_tool_call_cutoff(context_limit, compaction_threshold)
|
||||
});
|
||||
let manages_own_context = provider.manages_own_context();
|
||||
let tool_pair_compaction_enabled =
|
||||
crate::context_mgmt::tool_pair_summarization_enabled() && !manages_own_context;
|
||||
|
||||
let mut operations: Vec<Arc<dyn Operation<Session, GooseEffect> + '_>> = vec![
|
||||
Arc::new(SteerOperation::new(steer_queue, self.hook_manager.clone())),
|
||||
Arc::new(BangShellOperation::new()),
|
||||
];
|
||||
if !manages_own_context {
|
||||
operations.push(Arc::new(CompactionOperation::new(
|
||||
provider.clone(),
|
||||
model_config.clone(),
|
||||
context_limit,
|
||||
compaction_threshold,
|
||||
)));
|
||||
}
|
||||
let remaining_operations: Vec<Arc<dyn Operation<Session, GooseEffect> + '_>> = vec![
|
||||
Arc::new(ToolPairCompactionOperation::new(
|
||||
provider.clone(),
|
||||
model_config.clone(),
|
||||
tool_call_cutoff,
|
||||
tool_pair_compaction_enabled,
|
||||
)),
|
||||
Arc::new(ToolApprovalOperation::new(&self.tool_inspection_manager)),
|
||||
Arc::new(DoctorOperation::new(self.config.session_manager.clone())),
|
||||
Arc::new(ProjectOperation),
|
||||
Arc::new(SkillOperation::new(
|
||||
self.hook_manager.clone(),
|
||||
Arc::clone(&extension_lease),
|
||||
)),
|
||||
// Before RecipeOperation: a `delegate` response only means the subagent
|
||||
// started, so a final output from the same batch must not be shown until
|
||||
// the subagents have run.
|
||||
Arc::new(SubagentOperation::new(
|
||||
SubagentRunner::new(Arc::clone(self)),
|
||||
cancel.clone(),
|
||||
)),
|
||||
Arc::new(RecipeOperation::new(
|
||||
provider.clone(),
|
||||
self.hook_manager.clone(),
|
||||
)),
|
||||
Arc::new(ToolExecutionOperation::new(
|
||||
self.extension_manager.clone(),
|
||||
self.hook_manager.clone(),
|
||||
Arc::clone(&extension_lease),
|
||||
)),
|
||||
Arc::new(UnknownToolOperation::new(self.hook_manager.clone())),
|
||||
Arc::new(RetryOperation::new(
|
||||
std::time::Duration::from_secs(retry_timeout),
|
||||
std::time::Duration::from_secs(on_failure_timeout),
|
||||
)),
|
||||
Arc::new(EmptyResponseOperation),
|
||||
Arc::new(StopHookOperation::new(
|
||||
self.hook_manager.clone(),
|
||||
stop_hook_block_cap,
|
||||
)),
|
||||
Arc::new(ExitOnErrorOperation),
|
||||
Arc::new(MaxTurnsOperation::new(max_turns)),
|
||||
];
|
||||
operations.extend(remaining_operations);
|
||||
let request_preparer = GooseInferenceRequestPreparer {
|
||||
build_state_machine(StateMachineConfig {
|
||||
provider,
|
||||
model_config,
|
||||
context_limit,
|
||||
compaction_threshold,
|
||||
tool_call_cutoff,
|
||||
tool_pair_summarization: crate::context_mgmt::tool_pair_summarization_enabled(),
|
||||
session_manager: self.config.session_manager.clone(),
|
||||
extension_manager: Arc::clone(&self.extension_manager),
|
||||
extension_lease,
|
||||
tool_inspection_manager: &self.tool_inspection_manager,
|
||||
context_limit,
|
||||
};
|
||||
let status_operation =
|
||||
Arc::new(StatusOperation::new(provider.clone(), model_config.clone()));
|
||||
let inference_provider = Arc::new(GooseInferenceProvider::new(provider));
|
||||
let inference = Arc::new(
|
||||
InferenceRunner::new(inference_provider, model_config)
|
||||
.with_request_preparer(Arc::new(request_preparer)),
|
||||
);
|
||||
let mut command_handlers = operations.clone();
|
||||
command_handlers.push(status_operation);
|
||||
let command_operation: Arc<dyn Operation<Session, GooseEffect> + '_> =
|
||||
Arc::new(SlashCommandOperation::new(command_handlers));
|
||||
let steps = std::iter::once(command_operation)
|
||||
.chain(operations)
|
||||
.map(Step::Operation)
|
||||
.chain(std::iter::once(Step::Inference(inference)))
|
||||
.collect();
|
||||
|
||||
StateMachine::new(steps, cancel)
|
||||
hook_manager: self.hook_manager.clone(),
|
||||
steer_queue,
|
||||
subagent_runner: SubagentRunner::new(Arc::clone(self)),
|
||||
retry_timeout: std::time::Duration::from_secs(retry_timeout),
|
||||
on_failure_timeout: std::time::Duration::from_secs(on_failure_timeout),
|
||||
stop_hook_block_cap,
|
||||
max_turns,
|
||||
cancel,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn reply_with_state_machine(
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
use std::path::Path;
|
||||
|
||||
use crate::slash_commands::{recipe_slash_command, skill_slash_command};
|
||||
|
||||
use super::Agent;
|
||||
|
||||
pub fn slash_commands_enabled() -> bool {
|
||||
@@ -96,20 +92,6 @@ pub fn context_management_unsupported_message(command: &str, provider: &str) ->
|
||||
)
|
||||
}
|
||||
|
||||
pub fn is_known_slash_command(message_text: &str, working_dir: Option<&Path>) -> bool {
|
||||
let Some(parsed) = parse_slash_command(message_text) else {
|
||||
return false;
|
||||
};
|
||||
|
||||
COMMANDS
|
||||
.iter()
|
||||
.any(|command| command.name == parsed.command)
|
||||
|| recipe_slash_command::get_recipe_for_command(parsed.command).is_some()
|
||||
|| skill_slash_command::list_commands(working_dir)
|
||||
.into_iter()
|
||||
.any(|command| command.name.eq_ignore_ascii_case(parsed.command))
|
||||
}
|
||||
|
||||
fn is_clear_goal_param(params_str: &str) -> bool {
|
||||
matches!(params_str, "off" | "clear" | "none")
|
||||
}
|
||||
|
||||
@@ -13,6 +13,9 @@ pub const FINAL_OUTPUT_TOOL_NAME: &str = "recipe__final_output";
|
||||
pub const FINAL_OUTPUT_SUCCESS_MESSAGE: &str = "Final output successfully collected.";
|
||||
pub const FINAL_OUTPUT_CONTINUATION_MESSAGE: &str =
|
||||
"You MUST call the `final_output` tool NOW with the final output for the user.";
|
||||
pub const FINAL_OUTPUT_NOT_ALONE_MESSAGE: &str =
|
||||
"final_output was not accepted because it was called together with other tools. \
|
||||
Read the results of those tools, then call final_output on its own.";
|
||||
|
||||
pub(crate) fn structured_output_unsupported_message(provider_name: &str) -> String {
|
||||
format!(
|
||||
@@ -49,69 +52,7 @@ impl FinalOutputTool {
|
||||
})
|
||||
}
|
||||
|
||||
fn assistant_block_bounds(messages: &[Message], message_index: usize) -> (usize, usize) {
|
||||
let start = (0..message_index)
|
||||
.rev()
|
||||
.take_while(|index| messages[*index].role == rmcp::model::Role::Assistant)
|
||||
.last()
|
||||
.unwrap_or(message_index);
|
||||
let end = (message_index + 1..messages.len())
|
||||
.take_while(|index| messages[*index].role == rmcp::model::Role::Assistant)
|
||||
.last()
|
||||
.map_or(message_index + 1, |index| index + 1);
|
||||
(start, end)
|
||||
}
|
||||
|
||||
pub(crate) fn has_unanswered_siblings(messages: &[Message], request_id: &str) -> bool {
|
||||
let answered: HashSet<&str> = messages
|
||||
.iter()
|
||||
.flat_map(|message| &message.content)
|
||||
.filter_map(|content| match content {
|
||||
MessageContent::ToolResponse(response) => Some(response.id.as_str()),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
let Some(message_index) = messages.iter().position(|message| {
|
||||
message.content.iter().any(|content| {
|
||||
matches!(
|
||||
content,
|
||||
MessageContent::ToolRequest(request) if request.id == request_id
|
||||
)
|
||||
})
|
||||
}) else {
|
||||
return false;
|
||||
};
|
||||
let (start, end) = Self::assistant_block_bounds(messages, message_index);
|
||||
messages[start..end]
|
||||
.iter()
|
||||
.flat_map(|message| &message.content)
|
||||
.any(|content| match content {
|
||||
// Another unanswered final-output call is not a reason to wait.
|
||||
// This operation drains them one per pass, so treating a sibling
|
||||
// final-output call as unfinished work would deadlock the pair:
|
||||
// each would wait for the other and neither would be answered.
|
||||
// Ordinary tool calls still have to finish first.
|
||||
MessageContent::ToolRequest(request) => {
|
||||
request.id != request_id
|
||||
&& !answered.contains(request.id.as_str())
|
||||
&& !request
|
||||
.tool_call
|
||||
.as_ref()
|
||||
.is_ok_and(|tool_call| tool_call.name == FINAL_OUTPUT_TOOL_NAME)
|
||||
}
|
||||
_ => false,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn successful_output(messages: &[Message]) -> Option<String> {
|
||||
let answered_responses: HashSet<&str> = messages
|
||||
.iter()
|
||||
.flat_map(|message| &message.content)
|
||||
.filter_map(|content| match content {
|
||||
MessageContent::ToolResponse(response) => Some(response.id.as_str()),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
let successful_responses: HashSet<&str> = messages
|
||||
.iter()
|
||||
.flat_map(|message| &message.content)
|
||||
@@ -132,42 +73,25 @@ impl FinalOutputTool {
|
||||
})
|
||||
.collect();
|
||||
|
||||
for (message_index, message) in messages.iter().enumerate().rev() {
|
||||
let output = message
|
||||
.content
|
||||
.iter()
|
||||
.rev()
|
||||
.find_map(|content| match content {
|
||||
MessageContent::ToolRequest(request)
|
||||
if successful_responses.contains(request.id.as_str()) =>
|
||||
{
|
||||
request.tool_call.as_ref().ok().and_then(|tool_call| {
|
||||
(tool_call.name == FINAL_OUTPUT_TOOL_NAME).then(|| {
|
||||
serde_json::Value::Object(
|
||||
tool_call.arguments.clone().unwrap_or_default(),
|
||||
)
|
||||
.to_string()
|
||||
})
|
||||
messages
|
||||
.iter()
|
||||
.rev()
|
||||
.flat_map(|message| message.content.iter().rev())
|
||||
.find_map(|content| match content {
|
||||
MessageContent::ToolRequest(request)
|
||||
if successful_responses.contains(request.id.as_str()) =>
|
||||
{
|
||||
request.tool_call.as_ref().ok().and_then(|tool_call| {
|
||||
(tool_call.name == FINAL_OUTPUT_TOOL_NAME).then(|| {
|
||||
serde_json::Value::Object(
|
||||
tool_call.arguments.clone().unwrap_or_default(),
|
||||
)
|
||||
.to_string()
|
||||
})
|
||||
}
|
||||
_ => None,
|
||||
});
|
||||
if output.is_some() {
|
||||
let (block_start, block_end) =
|
||||
Self::assistant_block_bounds(messages, message_index);
|
||||
let siblings_answered = messages[block_start..block_end]
|
||||
.iter()
|
||||
.flat_map(|message| &message.content)
|
||||
.all(|content| match content {
|
||||
MessageContent::ToolRequest(request) => {
|
||||
answered_responses.contains(request.id.as_str())
|
||||
}
|
||||
_ => true,
|
||||
});
|
||||
return siblings_answered.then_some(output).flatten();
|
||||
}
|
||||
}
|
||||
None
|
||||
})
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn tool(&self) -> Tool {
|
||||
@@ -183,6 +107,7 @@ impl FinalOutputTool {
|
||||
|
||||
Usage:
|
||||
- Call the `final_output` tool with your JSON final output passed as the argument.
|
||||
- Call it on its own, in a reply with no other tool calls, after you have read the results of your other tool calls.
|
||||
|
||||
The expected JSON schema format is:
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ pub mod retry;
|
||||
#[cfg(feature = "scheduler")]
|
||||
mod schedule_tool;
|
||||
pub mod state_machine;
|
||||
mod state_machine_builder;
|
||||
pub mod subagent_execution_tool;
|
||||
pub(crate) mod subagent_handler;
|
||||
pub(crate) mod subagent_task_config;
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
//! Goose-specific inference request preparation.
|
||||
|
||||
use crate::agents::extension_manager::{ExtensionLease, ExtensionManager};
|
||||
use crate::agents::state_machine::awaits_tool_responses;
|
||||
use crate::agents::PromptManager;
|
||||
use crate::config::GooseMode;
|
||||
use crate::session::Session;
|
||||
@@ -28,18 +27,10 @@ impl InferenceRequestPreparer<Session> for GooseInferenceRequestPreparer<'_> {
|
||||
.extension_manager
|
||||
.current_session_snapshot(session)
|
||||
.await?;
|
||||
// Tool requests run under the lease of the inference that made them, so an
|
||||
// approval answered after that lease is gone expires instead of picking up a new one.
|
||||
let awaiting = match &session.conversation {
|
||||
Some(conversation) => awaits_tool_responses(messages_since_kickoff(conversation)?),
|
||||
None => false,
|
||||
};
|
||||
if !awaiting {
|
||||
*self
|
||||
.extension_lease
|
||||
.lock()
|
||||
.expect("extension lease unavailable") = Some(Arc::new(lease));
|
||||
}
|
||||
*self
|
||||
.extension_lease
|
||||
.lock()
|
||||
.expect("extension lease unavailable") = Some(Arc::new(lease));
|
||||
Ok(Some(session))
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ mod ops_llm;
|
||||
mod ops_maxturns;
|
||||
mod ops_project;
|
||||
pub(crate) mod ops_recipe;
|
||||
mod ops_remaining_tool_calls;
|
||||
mod ops_retry;
|
||||
mod ops_skills;
|
||||
mod ops_slash_command;
|
||||
@@ -26,33 +27,12 @@ mod ops_subagent;
|
||||
mod ops_tool_approval;
|
||||
mod ops_tool_pair_compaction;
|
||||
mod ops_toolcalling;
|
||||
mod ops_unknown_tool;
|
||||
mod session;
|
||||
pub(crate) use session::run as run_goose;
|
||||
pub(crate) use session::session_start_message;
|
||||
mod tool_confirmation;
|
||||
mod usage;
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
use crate::conversation::message::{Message, MessageContent};
|
||||
|
||||
/// Several operations answer parts of one tool batch in separate messages, so a
|
||||
/// tool tail alone does not mean the batch is complete.
|
||||
pub(super) fn awaits_tool_responses(messages: &[Message]) -> bool {
|
||||
let answered: HashSet<&str> = messages
|
||||
.iter()
|
||||
.flat_map(Message::get_tool_response_ids)
|
||||
.collect();
|
||||
messages
|
||||
.iter()
|
||||
.flat_map(|message| &message.content)
|
||||
.filter_map(MessageContent::as_tool_request)
|
||||
.any(|request| {
|
||||
!request.was_executed_externally() && !answered.contains(request.id.as_str())
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
@@ -80,6 +60,7 @@ pub(super) use ops_llm::{GooseInferenceProvider, InferenceRunner};
|
||||
pub(super) use ops_maxturns::{MaxTurnsOperation, MAX_TURNS_MESSAGE};
|
||||
pub(super) use ops_project::ProjectOperation;
|
||||
pub(super) use ops_recipe::RecipeOperation;
|
||||
pub(super) use ops_remaining_tool_calls::RemainingToolCallsOperation;
|
||||
pub(super) use ops_retry::RetryOperation;
|
||||
pub(super) use ops_skills::SkillOperation;
|
||||
pub(super) use ops_slash_command::SlashCommandOperation;
|
||||
@@ -90,4 +71,3 @@ pub(super) use ops_subagent::{SubagentOperation, DELEGATED_META_KEY};
|
||||
pub(super) use ops_tool_approval::ToolApprovalOperation;
|
||||
pub(super) use ops_tool_pair_compaction::ToolPairCompactionOperation;
|
||||
pub(super) use ops_toolcalling::ToolExecutionOperation;
|
||||
pub(super) use ops_unknown_tool::UnknownToolOperation;
|
||||
|
||||
@@ -6,12 +6,11 @@ use anyhow::{anyhow, Result};
|
||||
use async_trait::async_trait;
|
||||
use tracing_futures::Instrument;
|
||||
|
||||
use crate::agents::final_output_tool::FinalOutputTool;
|
||||
use crate::agents::state_machine::ops_llm::{chat_span, record_chat_usage};
|
||||
use crate::agents::state_machine::{
|
||||
applied, awaits_tool_responses, last_effective_role, messages_since_kickoff, not_applicable,
|
||||
trailing_error, yielded, yielded_with, ConversationEffect, Emitter, GooseEffect, Operation,
|
||||
OperationResult, SlashCommand,
|
||||
applied, last_effective_role, messages_since_kickoff, not_applicable, trailing_error, yielded,
|
||||
yielded_with, ConversationEffect, Emitter, GooseEffect, Operation, OperationResult,
|
||||
SlashCommand,
|
||||
};
|
||||
use crate::context_mgmt::{compact_messages, count_context_tokens};
|
||||
use crate::conversation::message::{Message, MessageErrorKind, SystemNotificationType};
|
||||
@@ -256,15 +255,8 @@ impl Operation<Session, GooseEffect> for CompactionOperation {
|
||||
return not_applicable();
|
||||
}
|
||||
} else {
|
||||
// Compact only ahead of an inference. An assistant tail ends the turn or
|
||||
// awaits tool responses, hiding an unanswered request orphans its result,
|
||||
// and RecipeOperation delivers a successful final output from a tool tail.
|
||||
let tail = last_effective_role(messages)?;
|
||||
if tail == EffectiveRole::Assistant
|
||||
|| awaits_tool_responses(messages)
|
||||
|| (tail == EffectiveRole::Tool
|
||||
&& FinalOutputTool::successful_output(messages).is_some())
|
||||
{
|
||||
// Compact only ahead of an inference; an assistant tail ends the turn.
|
||||
if last_effective_role(messages)? == EffectiveRole::Assistant {
|
||||
return not_applicable();
|
||||
}
|
||||
let tokens = self.context_tokens(session, conversation).await?;
|
||||
|
||||
@@ -56,7 +56,7 @@ fn enrich_unclaimed_tool_errors(messages: &[Message], tools: &[rmcp::model::Tool
|
||||
continue;
|
||||
};
|
||||
if metadata
|
||||
.remove(super::ops_unknown_tool::UNCLAIMED_TOOL_ERROR)
|
||||
.remove(super::ops_remaining_tool_calls::UNCLAIMED_TOOL_ERROR)
|
||||
.is_none()
|
||||
{
|
||||
continue;
|
||||
|
||||
@@ -4,25 +4,25 @@ use std::sync::Arc;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use async_trait::async_trait;
|
||||
use rmcp::model::{CallToolResult, ContentBlock, Tool};
|
||||
use rmcp::model::{CallToolResult, ContentBlock, ErrorCode, ErrorData, Tool};
|
||||
use tracing_futures::Instrument;
|
||||
|
||||
use crate::agents::final_output_tool::{
|
||||
structured_output_unsupported_message, FinalOutputTool, FINAL_OUTPUT_CONTINUATION_MESSAGE,
|
||||
FINAL_OUTPUT_TOOL_NAME,
|
||||
FINAL_OUTPUT_NOT_ALONE_MESSAGE, FINAL_OUTPUT_TOOL_NAME,
|
||||
};
|
||||
use crate::agents::state_machine::ops_toolcalling::{
|
||||
emit_post_tool_use, pending_advertised_tool_requests, run_pre_tool_hooks, tool_span,
|
||||
ToolDisposition,
|
||||
};
|
||||
use crate::agents::state_machine::{
|
||||
applied, ends_turn, last_effective_role, messages_since_kickoff, not_applicable, yielded_with,
|
||||
ConversationEffect, Emitter, GooseEffect, Operation, OperationResult, SlashCommand,
|
||||
applied, ends_turn, messages_since_kickoff, not_applicable, yielded_with, ConversationEffect,
|
||||
Emitter, GooseEffect, Operation, OperationResult, SlashCommand,
|
||||
};
|
||||
use crate::agents::tool_execution::CHAT_MODE_TOOL_SKIPPED_RESPONSE;
|
||||
use crate::config::GooseMode;
|
||||
use crate::conversation::message::Message;
|
||||
use crate::conversation::{Conversation, EffectiveRole};
|
||||
use crate::conversation::message::{Message, MessageContent};
|
||||
use crate::conversation::Conversation;
|
||||
use crate::hooks::HookManager;
|
||||
use crate::providers::base::Provider;
|
||||
use crate::session::Session;
|
||||
@@ -37,6 +37,37 @@ pub(crate) fn final_output_tool(session: &Session) -> Result<Option<FinalOutputT
|
||||
.map_err(|error| anyhow!(error))
|
||||
}
|
||||
|
||||
fn assistant_block_bounds(messages: &[Message], message_index: usize) -> (usize, usize) {
|
||||
let start = (0..message_index)
|
||||
.rev()
|
||||
.take_while(|index| messages[*index].role == rmcp::model::Role::Assistant)
|
||||
.last()
|
||||
.unwrap_or(message_index);
|
||||
let end = (message_index + 1..messages.len())
|
||||
.take_while(|index| messages[*index].role == rmcp::model::Role::Assistant)
|
||||
.last()
|
||||
.map_or(message_index + 1, |index| index + 1);
|
||||
(start, end)
|
||||
}
|
||||
|
||||
fn has_sibling_tool_calls(messages: &[Message], request_id: &str) -> bool {
|
||||
let Some(message_index) = messages.iter().position(|message| {
|
||||
message
|
||||
.content
|
||||
.iter()
|
||||
.filter_map(MessageContent::as_tool_request)
|
||||
.any(|request| request.id == request_id)
|
||||
}) else {
|
||||
return false;
|
||||
};
|
||||
let (start, end) = assistant_block_bounds(messages, message_index);
|
||||
messages[start..end]
|
||||
.iter()
|
||||
.flat_map(|message| &message.content)
|
||||
.filter_map(MessageContent::as_tool_request)
|
||||
.any(|request| request.id != request_id && !request.was_executed_externally())
|
||||
}
|
||||
|
||||
pub(crate) fn recipe_prompt_parts(session: &Session) -> Result<Vec<(String, String)>> {
|
||||
let instructions = session
|
||||
.recipe
|
||||
@@ -225,8 +256,19 @@ impl Operation<Session, GooseEffect> for RecipeOperation {
|
||||
let response = emit.message(response);
|
||||
return applied([response.into()]);
|
||||
}
|
||||
if FinalOutputTool::has_unanswered_siblings(messages, &request.id) {
|
||||
return not_applicable();
|
||||
if has_sibling_tool_calls(messages, &request.id) {
|
||||
let mut response = Message::user();
|
||||
response.add_tool_response_with_metadata(
|
||||
request.id,
|
||||
Err(ErrorData::new(
|
||||
ErrorCode::INVALID_REQUEST,
|
||||
FINAL_OUTPUT_NOT_ALONE_MESSAGE,
|
||||
None,
|
||||
)),
|
||||
request.metadata.as_ref(),
|
||||
);
|
||||
let response = emit.message(response);
|
||||
return applied([response.into()]);
|
||||
}
|
||||
|
||||
let tool_call = request
|
||||
@@ -298,15 +340,14 @@ impl Operation<Session, GooseEffect> for RecipeOperation {
|
||||
}
|
||||
let response = self.take_response().expect("recipe response held");
|
||||
let response = emit.message(response);
|
||||
return applied([response.into()]);
|
||||
let Some(accepted) = final_output.final_output.take() else {
|
||||
return applied([response.into()]);
|
||||
};
|
||||
let accepted = emit.message(Message::assistant().with_text(accepted));
|
||||
return applied([response.into(), accepted.into()]);
|
||||
}
|
||||
|
||||
if let Some(output) = FinalOutputTool::successful_output(messages) {
|
||||
if last_effective_role(messages)? == EffectiveRole::Tool {
|
||||
let message = Message::assistant().with_text(output);
|
||||
let message = emit.message(message);
|
||||
return applied([message.into()]);
|
||||
}
|
||||
if FinalOutputTool::successful_output(messages).is_some() {
|
||||
return not_applicable();
|
||||
}
|
||||
|
||||
|
||||
+195
-7
@@ -1,4 +1,6 @@
|
||||
//! Turns tool requests that no operation can handle into tool errors.
|
||||
//! Answers tool requests that no other operation handled, and waits while any await approval.
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
@@ -12,31 +14,68 @@ use crate::agents::state_machine::ops_toolcalling::{
|
||||
run_pre_tool_hooks, tool_span, ToolDisposition,
|
||||
};
|
||||
use crate::agents::state_machine::{
|
||||
applied, messages_since_kickoff, not_applicable, Emitter, Operation, OperationResult,
|
||||
applied, messages_since_kickoff, not_applicable, pending_tool_confirmations, yielded, Emitter,
|
||||
Operation, OperationResult,
|
||||
};
|
||||
use crate::agents::tool_execution::{CHAT_MODE_TOOL_SKIPPED_RESPONSE, DECLINED_RESPONSE};
|
||||
use crate::config::GooseMode;
|
||||
use crate::conversation::message::Message;
|
||||
use crate::conversation::message::{Message, MessageContent, ToolRequest};
|
||||
use crate::conversation::Conversation;
|
||||
use crate::hooks::HookManager;
|
||||
use crate::session::Session;
|
||||
|
||||
pub(super) const UNCLAIMED_TOOL_ERROR: &str = "goose.unclaimed_tool";
|
||||
const NOT_HANDLED_RESPONSE: &str = "This tool call was not handled.";
|
||||
|
||||
pub struct UnknownToolOperation {
|
||||
fn unanswered_tool_requests<'a>(
|
||||
messages: &'a [Message],
|
||||
awaiting_approval: &HashSet<String>,
|
||||
) -> Vec<&'a ToolRequest> {
|
||||
let answered: HashSet<&str> = messages
|
||||
.iter()
|
||||
.flat_map(Message::get_tool_response_ids)
|
||||
.collect();
|
||||
messages
|
||||
.iter()
|
||||
.flat_map(|message| &message.content)
|
||||
.filter_map(MessageContent::as_tool_request)
|
||||
.filter(|request| {
|
||||
!request.was_executed_externally()
|
||||
&& !answered.contains(request.id.as_str())
|
||||
&& !awaiting_approval.contains(&request.id)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn not_handled_response(unhandled: &[&ToolRequest]) -> Message {
|
||||
let mut response = Message::user();
|
||||
for request in unhandled {
|
||||
tracing::error!(tool_call_id = %request.id, "tool call was not answered by any operation");
|
||||
response.add_tool_response_with_metadata(
|
||||
request.id.clone(),
|
||||
Ok(CallToolResult::error(vec![ContentBlock::text(
|
||||
NOT_HANDLED_RESPONSE,
|
||||
)])),
|
||||
request.metadata.as_ref(),
|
||||
);
|
||||
}
|
||||
response
|
||||
}
|
||||
|
||||
pub struct RemainingToolCallsOperation {
|
||||
hook_manager: HookManager,
|
||||
}
|
||||
|
||||
impl UnknownToolOperation {
|
||||
impl RemainingToolCallsOperation {
|
||||
pub fn new(hook_manager: HookManager) -> Self {
|
||||
Self { hook_manager }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Operation<Session, GooseEffect> for UnknownToolOperation {
|
||||
impl Operation<Session, GooseEffect> for RemainingToolCallsOperation {
|
||||
fn name(&self) -> &'static str {
|
||||
"unknown_tool"
|
||||
"remaining_tool_calls"
|
||||
}
|
||||
|
||||
async fn run(
|
||||
@@ -68,6 +107,17 @@ impl Operation<Session, GooseEffect> for UnknownToolOperation {
|
||||
})
|
||||
.collect();
|
||||
if pending.is_empty() {
|
||||
let awaiting_approval: HashSet<String> = pending_tool_confirmations(conversation)
|
||||
.into_iter()
|
||||
.map(|confirmation| confirmation.id)
|
||||
.collect();
|
||||
let unhandled = unanswered_tool_requests(messages, &awaiting_approval);
|
||||
if !unhandled.is_empty() {
|
||||
return applied([emit.message(not_handled_response(&unhandled)).into()]);
|
||||
}
|
||||
if !awaiting_approval.is_empty() {
|
||||
return yielded();
|
||||
}
|
||||
return not_applicable();
|
||||
}
|
||||
|
||||
@@ -196,3 +246,141 @@ impl Operation<Session, GooseEffect> for UnknownToolOperation {
|
||||
applied([response.into()])
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::agents::state_machine::ops_tool_approval::TOOL_EXECUTABLE_KEY;
|
||||
use crate::agents::state_machine::{ConversationEffect, RunStatus};
|
||||
use crate::conversation::message::TOOL_META_EXTERNAL_DISPATCH_KEY;
|
||||
use crate::recipe::{Recipe, Response};
|
||||
use rmcp::model::CallToolRequestParams;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
fn answer(id: &str) -> Message {
|
||||
Message::user().with_tool_response(
|
||||
id,
|
||||
Ok(CallToolResult::success(vec![ContentBlock::text("done")])),
|
||||
)
|
||||
}
|
||||
|
||||
fn unanswered_ids(messages: &[Message]) -> Vec<&str> {
|
||||
unanswered_tool_requests(messages, &HashSet::new())
|
||||
.into_iter()
|
||||
.map(|request| request.id.as_str())
|
||||
.collect()
|
||||
}
|
||||
|
||||
async fn run(
|
||||
session: &Session,
|
||||
messages: Vec<Message>,
|
||||
) -> Result<OperationResult<GooseEffect>> {
|
||||
let (tx, _rx) = mpsc::unbounded_channel();
|
||||
let emit = Emitter::new(tx, CancellationToken::new());
|
||||
RemainingToolCallsOperation::new(HookManager::default())
|
||||
.run(session, &Conversation::new_unvalidated(messages), &emit)
|
||||
.await
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_reply_with_every_call_answered_is_not_open() {
|
||||
let messages = [
|
||||
Message::assistant().with_tool_request("call", Ok(CallToolRequestParams::new("tool"))),
|
||||
answer("call"),
|
||||
];
|
||||
assert!(unanswered_ids(&messages).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn calls_answered_in_separate_messages_stay_open_until_the_last_answer() {
|
||||
let mut messages = vec![
|
||||
Message::assistant()
|
||||
.with_tool_request("first", Ok(CallToolRequestParams::new("tool")))
|
||||
.with_tool_request("second", Ok(CallToolRequestParams::new("tool"))),
|
||||
answer("first"),
|
||||
];
|
||||
assert_eq!(unanswered_ids(&messages), ["second"]);
|
||||
|
||||
messages.push(answer("second"));
|
||||
assert!(unanswered_ids(&messages).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_externally_executed_call_is_never_open() {
|
||||
let messages = [Message::assistant().with_tool_request_with_metadata(
|
||||
"external",
|
||||
Ok(CallToolRequestParams::new("tool")),
|
||||
None,
|
||||
Some(serde_json::json!({ TOOL_META_EXTERNAL_DISPATCH_KEY: true })),
|
||||
)];
|
||||
assert!(unanswered_ids(&messages).is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_call_awaiting_approval_yields() -> Result<()> {
|
||||
let messages = vec![
|
||||
Message::user().with_text("run it"),
|
||||
Message::assistant().with_tool_request_with_metadata(
|
||||
"call",
|
||||
Ok(CallToolRequestParams::new("shell")),
|
||||
None,
|
||||
Some(serde_json::json!({ TOOL_EXECUTABLE_KEY: false })),
|
||||
),
|
||||
Message::assistant()
|
||||
.with_action_required("call", "shell".to_string(), Default::default(), None)
|
||||
.user_only(),
|
||||
];
|
||||
|
||||
let OperationResult::Applied(step) = run(&Session::default(), messages).await? else {
|
||||
panic!("expected the operation to yield");
|
||||
};
|
||||
assert_eq!(step.status, RunStatus::Yielded);
|
||||
assert!(step.effects.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_call_no_operation_answered_gets_an_error_and_the_turn_continues() -> Result<()> {
|
||||
let session = Session {
|
||||
recipe: Some(
|
||||
Recipe::builder()
|
||||
.title("recipe")
|
||||
.description("recipe")
|
||||
.instructions("answer")
|
||||
.response(Response {
|
||||
json_schema: Some(serde_json::json!({ "type": "object" })),
|
||||
})
|
||||
.build()
|
||||
.unwrap(),
|
||||
),
|
||||
..Session::default()
|
||||
};
|
||||
let messages = vec![
|
||||
Message::user().with_text("go"),
|
||||
Message::assistant().with_tool_request(
|
||||
"final",
|
||||
Ok(CallToolRequestParams::new(FINAL_OUTPUT_TOOL_NAME)),
|
||||
),
|
||||
];
|
||||
|
||||
let OperationResult::Applied(step) = run(&session, messages).await? else {
|
||||
panic!("expected the operation to answer the call");
|
||||
};
|
||||
assert_eq!(step.status, RunStatus::Continuing);
|
||||
let [GooseEffect::Conversation(ConversationEffect::AppendMessage(message))] =
|
||||
step.effects.as_slice()
|
||||
else {
|
||||
panic!("expected one answer message");
|
||||
};
|
||||
let response = message.content[0].as_tool_response().unwrap();
|
||||
assert_eq!(response.id, "final");
|
||||
let result = response.tool_result.as_ref().unwrap();
|
||||
assert_eq!(result.is_error, Some(true));
|
||||
assert_eq!(
|
||||
result.content[0].as_text().unwrap().text,
|
||||
NOT_HANDLED_RESPONSE
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,8 @@ use tokio::sync::Mutex;
|
||||
|
||||
use crate::agents::state_machine::effects::GooseEffect;
|
||||
use crate::agents::state_machine::{
|
||||
applied, awaits_tool_responses, ends_turn, last_effective_role, messages_since_kickoff,
|
||||
not_applicable, Emitter, Operation, OperationResult,
|
||||
applied, ends_turn, last_effective_role, messages_since_kickoff, not_applicable, Emitter,
|
||||
Operation, OperationResult,
|
||||
};
|
||||
use crate::conversation::message::Message;
|
||||
use crate::conversation::{Conversation, EffectiveRole};
|
||||
@@ -66,7 +66,7 @@ impl Operation<Session, GooseEffect> for SteerOperation {
|
||||
let messages = messages_since_kickoff(conversation)?;
|
||||
let between_turns =
|
||||
ends_turn(messages) || last_effective_role(messages)? == EffectiveRole::Tool;
|
||||
if !between_turns || awaits_tool_responses(messages) {
|
||||
if !between_turns {
|
||||
return not_applicable();
|
||||
}
|
||||
|
||||
|
||||
@@ -8,8 +8,7 @@ use tokio_util::sync::CancellationToken;
|
||||
use tracing::Instrument;
|
||||
|
||||
use crate::agents::state_machine::{
|
||||
applied, awaits_tool_responses, messages_since_kickoff, not_applicable, Emitter, GooseEffect,
|
||||
Operation, OperationResult,
|
||||
applied, not_applicable, Emitter, GooseEffect, Operation, OperationResult,
|
||||
};
|
||||
use crate::agents::subagent_handler::{SubagentOutcome, SubagentRunner, SubagentStart};
|
||||
use crate::conversation::message::{Message, MessageContent, SystemNotificationType};
|
||||
@@ -240,9 +239,6 @@ impl Operation<Session, GooseEffect> for SubagentOperation {
|
||||
if session.session_type == SessionType::SubAgent {
|
||||
return not_applicable();
|
||||
}
|
||||
if awaits_tool_responses(messages_since_kickoff(conversation)?) {
|
||||
return not_applicable();
|
||||
}
|
||||
let pending = pending_subagents(conversation.messages());
|
||||
if pending.is_empty() {
|
||||
return not_applicable();
|
||||
@@ -494,27 +490,6 @@ mod tests {
|
||||
assert_eq!(pending_subagents(&messages), ["waiting"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn waits_for_all_tool_responses_before_running_subagents() {
|
||||
let requests = Message::assistant()
|
||||
.with_tool_request("delegate-call", Ok(CallToolRequestParams::new("delegate")))
|
||||
.with_tool_request("other-call", Ok(CallToolRequestParams::new("other")));
|
||||
let response = Message::user().with_tool_response(
|
||||
"delegate-call",
|
||||
Ok(CallToolResult::success(vec![ContentBlock::text(
|
||||
"delegated",
|
||||
)])),
|
||||
);
|
||||
let mut messages = vec![requests, response];
|
||||
assert!(awaits_tool_responses(&messages));
|
||||
|
||||
messages.push(Message::user().with_tool_response(
|
||||
"other-call",
|
||||
Ok(CallToolResult::success(vec![ContentBlock::text("done")])),
|
||||
));
|
||||
assert!(!awaits_tool_responses(&messages));
|
||||
}
|
||||
|
||||
async fn save_final_output(manager: &SessionManager, subagent_id: &str) -> Result<()> {
|
||||
let arguments = serde_json::json!({"summary": "done"})
|
||||
.as_object()
|
||||
|
||||
@@ -4,15 +4,13 @@ use serde_json::Value;
|
||||
|
||||
use super::calculator_extension::{value, ADD};
|
||||
use super::pipeline::{test_pipeline, MessageKind::Agent, MessageKind::ToolResponse, MAX_TURNS};
|
||||
use crate::agents::final_output_tool::FINAL_OUTPUT_TOOL_NAME;
|
||||
use crate::agents::platform_extensions::MANAGE_EXTENSIONS_TOOL_NAME_COMPLETE;
|
||||
use crate::agents::final_output_tool::{FINAL_OUTPUT_NOT_ALONE_MESSAGE, FINAL_OUTPUT_TOOL_NAME};
|
||||
use crate::agents::state_machine::ops_remaining_tool_calls::UNCLAIMED_TOOL_ERROR;
|
||||
use crate::agents::state_machine::ops_stop_hook::DENIED;
|
||||
use crate::agents::state_machine::ops_unknown_tool::UNCLAIMED_TOOL_ERROR;
|
||||
use crate::agents::tool_execution::{CHAT_MODE_TOOL_SKIPPED_RESPONSE, DECLINED_RESPONSE};
|
||||
use crate::config::permission::PermissionLevel;
|
||||
use crate::config::GooseMode;
|
||||
use crate::conversation::message::{Message, MessageContent, SystemNotificationType};
|
||||
use crate::permission::Permission;
|
||||
|
||||
struct HookTestEnv {
|
||||
_temp_dir: tempfile::TempDir,
|
||||
@@ -618,14 +616,9 @@ fn final_output_recipe() -> crate::recipe::Recipe {
|
||||
.expect("valid recipe")
|
||||
}
|
||||
|
||||
/// recipe final-output parity: the call `RecipeOperation` executes directly still
|
||||
/// emits `PreToolUse` and `PreToolUseResult`, correlated by one `tool_call_id`.
|
||||
#[tokio::test]
|
||||
async fn recipe_final_output_emits_pre_tool_use_and_result_with_matching_id() -> Result<()> {
|
||||
let env = RecordingHookEnv::new(&[
|
||||
("PreToolUse", "", "pre.sh", RECORD_PRE_SCRIPT),
|
||||
("PreToolUseResult", "", "result.sh", RECORD_RESULT_SCRIPT),
|
||||
]);
|
||||
async fn recipe_final_output_emits_tool_hooks_with_one_tool_call_id() -> Result<()> {
|
||||
let env = recording_lifecycle_env();
|
||||
let (pipeline, api) = test_pipeline().await?;
|
||||
let pipeline = pipeline.with_hook_manager(env.hook_manager());
|
||||
pipeline.set_recipe(final_output_recipe()).await?;
|
||||
@@ -638,6 +631,7 @@ async fn recipe_final_output_emits_pre_tool_use_and_result_with_matching_id() ->
|
||||
|
||||
let pres = env.payloads("pre.log");
|
||||
let results = env.payloads("result.log");
|
||||
let posts = env.payloads("post.log");
|
||||
assert_eq!(
|
||||
pres.len(),
|
||||
1,
|
||||
@@ -648,54 +642,25 @@ async fn recipe_final_output_emits_pre_tool_use_and_result_with_matching_id() ->
|
||||
1,
|
||||
"PreToolUseResult must fire for recipe final output"
|
||||
);
|
||||
assert_eq!(pres[0]["tool_name"], FINAL_OUTPUT_TOOL_NAME);
|
||||
assert_eq!(results[0]["tool_name"], FINAL_OUTPUT_TOOL_NAME);
|
||||
assert_eq!(results[0]["event"], "PreToolUseResult");
|
||||
assert_eq!(results[0]["decision"], "allow");
|
||||
assert_eq!(
|
||||
pres[0]["tool_call_id"], results[0]["tool_call_id"],
|
||||
"PreToolUse and PreToolUseResult must carry the same tool_call_id"
|
||||
);
|
||||
assert!(pres[0]["tool_call_id"]
|
||||
.as_str()
|
||||
.is_some_and(|id| !id.is_empty()));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// recipe final-output parity: the post-tool event fires once the call completes,
|
||||
/// carrying the id the pre events carried.
|
||||
#[tokio::test]
|
||||
async fn recipe_final_output_emits_post_tool_event() -> Result<()> {
|
||||
let env = RecordingHookEnv::new(&[
|
||||
("PreToolUse", "", "pre.sh", RECORD_PRE_SCRIPT),
|
||||
("PostToolUse", "", "post.sh", RECORD_POST_SCRIPT),
|
||||
(
|
||||
"PostToolUseFailure",
|
||||
"",
|
||||
"postfail.sh",
|
||||
RECORD_POST_FAILURE_SCRIPT,
|
||||
),
|
||||
]);
|
||||
let (pipeline, api) = test_pipeline().await?;
|
||||
let pipeline = pipeline.with_hook_manager(env.hook_manager());
|
||||
pipeline.set_recipe(final_output_recipe()).await?;
|
||||
api.on("produce the answer").call(
|
||||
FINAL_OUTPUT_TOOL_NAME,
|
||||
serde_json::json!({ "answer": "done" }),
|
||||
);
|
||||
|
||||
pipeline.run(["produce the answer"]).await?;
|
||||
|
||||
let pres = env.payloads("pre.log");
|
||||
let posts = env.payloads("post.log");
|
||||
assert_eq!(pres.len(), 1);
|
||||
assert_eq!(
|
||||
posts.len(),
|
||||
1,
|
||||
"PostToolUse must fire for a successful recipe final output"
|
||||
);
|
||||
assert!(env.payloads("postfail.log").is_empty());
|
||||
assert_eq!(pres[0]["tool_name"], FINAL_OUTPUT_TOOL_NAME);
|
||||
assert_eq!(results[0]["tool_name"], FINAL_OUTPUT_TOOL_NAME);
|
||||
assert_eq!(results[0]["event"], "PreToolUseResult");
|
||||
assert_eq!(results[0]["decision"], "allow");
|
||||
assert_eq!(posts[0]["event"], "PostToolUse");
|
||||
assert_eq!(posts[0]["tool_name"], FINAL_OUTPUT_TOOL_NAME);
|
||||
assert!(pres[0]["tool_call_id"]
|
||||
.as_str()
|
||||
.is_some_and(|id| !id.is_empty()));
|
||||
assert_eq!(
|
||||
pres[0]["tool_call_id"], results[0]["tool_call_id"],
|
||||
"PreToolUse and PreToolUseResult must carry the same tool_call_id"
|
||||
);
|
||||
assert_eq!(
|
||||
posts[0]["tool_call_id"], pres[0]["tool_call_id"],
|
||||
"the post event must carry the same tool_call_id as the pre events"
|
||||
@@ -704,111 +669,79 @@ async fn recipe_final_output_emits_post_tool_event() -> Result<()> {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn recipe_final_output_waits_for_sibling_tools_and_is_emitted_once() -> Result<()> {
|
||||
async fn final_output_beside_another_call_is_rejected_until_called_alone() -> Result<()> {
|
||||
let (pipeline, api) = test_pipeline().await?;
|
||||
pipeline.set_recipe(final_output_recipe()).await?;
|
||||
api.on("finish and add").calls([
|
||||
(
|
||||
"final-output",
|
||||
"early-output",
|
||||
FINAL_OUTPUT_TOOL_NAME,
|
||||
serde_json::json!({ "answer": "done" }),
|
||||
serde_json::json!({ "answer": "guess" }),
|
||||
),
|
||||
("side-effect", ADD, value(1)),
|
||||
]);
|
||||
api.on(FINAL_OUTPUT_NOT_ALONE_MESSAGE).call(
|
||||
FINAL_OUTPUT_TOOL_NAME,
|
||||
serde_json::json!({ "answer": "done" }),
|
||||
);
|
||||
|
||||
let result = pipeline.run(["finish and add"]).await?;
|
||||
|
||||
assert_eq!(pipeline.calculator_total(), 1);
|
||||
assert_eq!(api.call_count(), 2);
|
||||
assert!(api.calls()[1].input_contains("result: 1"));
|
||||
let messages = result.conversation().messages();
|
||||
let side_effect_response = messages
|
||||
assert_tool_transcript_bijection(messages);
|
||||
assert!(messages
|
||||
.iter()
|
||||
.position(|message| {
|
||||
message.content.iter().any(|content| {
|
||||
matches!(
|
||||
content,
|
||||
MessageContent::ToolResponse(response) if response.id == "side-effect"
|
||||
)
|
||||
})
|
||||
})
|
||||
.expect("sibling tool response");
|
||||
let final_answers: Vec<_> = messages
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, message)| message.as_concat_text() == r#"{"answer":"done"}"#)
|
||||
.collect();
|
||||
assert_eq!(final_answers.len(), 1, "final output must be emitted once");
|
||||
assert!(
|
||||
side_effect_response < final_answers[0].0,
|
||||
"final output must wait until sibling tools finish"
|
||||
);
|
||||
.all(|message| message.as_concat_text() != r#"{"answer":"guess"}"#));
|
||||
result.assert_message(-1, Agent, r#"{"answer":"done"}"#);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn recipe_final_output_waits_for_approval_pending_sibling() -> Result<()> {
|
||||
async fn rejected_final_output_after_a_stop_hook_block_does_not_reshow_the_blocked_output(
|
||||
) -> Result<()> {
|
||||
let blocked = HookTestEnv::new("Stop", RECORD_AND_BLOCK_MARKER_SCRIPT);
|
||||
let (pipeline, api) = test_pipeline().await?;
|
||||
let pipeline = pipeline.with_goose_mode(GooseMode::Approve).await;
|
||||
pipeline.set_permission(FINAL_OUTPUT_TOOL_NAME, PermissionLevel::AlwaysAllow);
|
||||
let pipeline = pipeline.with_hook_manager(blocked.hook_manager());
|
||||
pipeline.set_recipe(final_output_recipe()).await?;
|
||||
api.on("finish after approval").calls([
|
||||
api.on("finish").call(
|
||||
FINAL_OUTPUT_TOOL_NAME,
|
||||
serde_json::json!({ "answer": "policy marker" }),
|
||||
);
|
||||
api.on("blocked ending this turn").calls([
|
||||
("side-effect", ADD, value(1)),
|
||||
(
|
||||
"final-output",
|
||||
"early-output",
|
||||
FINAL_OUTPUT_TOOL_NAME,
|
||||
serde_json::json!({ "answer": "approved" }),
|
||||
),
|
||||
(
|
||||
"side-effect",
|
||||
MANAGE_EXTENSIONS_TOOL_NAME_COMPLETE,
|
||||
serde_json::json!({
|
||||
"action": "enable",
|
||||
"extension_name": "analyze"
|
||||
}),
|
||||
serde_json::json!({ "answer": "guess" }),
|
||||
),
|
||||
]);
|
||||
|
||||
let awaiting_approval = pipeline.run(["finish after approval"]).await?;
|
||||
assert!(
|
||||
awaiting_approval
|
||||
.conversation()
|
||||
.messages()
|
||||
.iter()
|
||||
.all(|message| message.as_concat_text() != r#"{"answer":"approved"}"#),
|
||||
"final output must wait while a sibling needs approval"
|
||||
api.on(FINAL_OUTPUT_NOT_ALONE_MESSAGE).call(
|
||||
FINAL_OUTPUT_TOOL_NAME,
|
||||
serde_json::json!({ "answer": "fixed" }),
|
||||
);
|
||||
|
||||
pipeline
|
||||
.confirm("side-effect", Permission::AllowOnce)
|
||||
.await?;
|
||||
let result = pipeline.resume().await?;
|
||||
let result = pipeline.run(["finish"]).await?;
|
||||
|
||||
let messages = result.conversation().messages();
|
||||
let sibling_response = messages
|
||||
.iter()
|
||||
.position(|message| {
|
||||
message.content.iter().any(|content| {
|
||||
matches!(
|
||||
content,
|
||||
MessageContent::ToolResponse(response) if response.id == "side-effect"
|
||||
)
|
||||
})
|
||||
})
|
||||
.expect("approved sibling response");
|
||||
let final_answers = result
|
||||
assert_eq!(api.call_count(), 3);
|
||||
let shown: Vec<String> = result
|
||||
.conversation()
|
||||
.messages()
|
||||
.iter()
|
||||
.filter(|message| message.as_concat_text() == r#"{"answer":"approved"}"#)
|
||||
.count();
|
||||
assert_eq!(final_answers, 1, "approved final output must emit once");
|
||||
let final_answer = messages
|
||||
.iter()
|
||||
.position(|message| message.as_concat_text() == r#"{"answer":"approved"}"#)
|
||||
.expect("approved final output");
|
||||
assert!(
|
||||
sibling_response < final_answer,
|
||||
"the approved sibling must execute before finalization"
|
||||
.filter(|message| message.role == Role::Assistant)
|
||||
.map(Message::as_concat_text)
|
||||
.filter(|text| text.contains("answer"))
|
||||
.collect();
|
||||
assert_eq!(
|
||||
shown,
|
||||
vec![
|
||||
r#"{"answer":"policy marker"}"#.to_string(),
|
||||
r#"{"answer":"fixed"}"#.to_string()
|
||||
]
|
||||
);
|
||||
result.assert_message(-1, Agent, r#"{"answer":"approved"}"#);
|
||||
result.assert_message(-1, Agent, r#"{"answer":"fixed"}"#);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1324,13 +1257,6 @@ fn assert_tool_transcript_bijection(messages: &[Message]) {
|
||||
}
|
||||
}
|
||||
|
||||
fn lifecycle_ids(env: &RecordingHookEnv, log: &str) -> Vec<String> {
|
||||
env.payloads(log)
|
||||
.iter()
|
||||
.filter_map(|payload| payload["tool_call_id"].as_str().map(str::to_string))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn recording_lifecycle_env() -> RecordingHookEnv {
|
||||
RecordingHookEnv::new(&[
|
||||
("PreToolUse", "", "pre.sh", RECORD_PRE_SCRIPT),
|
||||
@@ -1408,64 +1334,6 @@ async fn recipe_final_output_denied_by_permission_receives_declined_response() -
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Two final-output calls in one assistant block both get answered, each with its
|
||||
/// own lifecycle, and the last valid one is published. Before the fix the pair
|
||||
/// deadlocked: each waited for the other and neither was answered.
|
||||
#[tokio::test]
|
||||
async fn duplicate_final_output_calls_are_each_answered_and_last_valid_wins() -> Result<()> {
|
||||
let env = recording_lifecycle_env();
|
||||
let (pipeline, api) = test_pipeline().await?;
|
||||
let pipeline = pipeline.with_hook_manager(env.hook_manager());
|
||||
pipeline.set_recipe(final_output_recipe()).await?;
|
||||
api.on("finish twice").calls([
|
||||
(
|
||||
"final-a",
|
||||
FINAL_OUTPUT_TOOL_NAME,
|
||||
serde_json::json!({ "answer": "first" }),
|
||||
),
|
||||
(
|
||||
"final-b",
|
||||
FINAL_OUTPUT_TOOL_NAME,
|
||||
serde_json::json!({ "answer": "second" }),
|
||||
),
|
||||
]);
|
||||
|
||||
let result = pipeline.run(["finish twice"]).await?;
|
||||
let messages = result.conversation().messages();
|
||||
assert_tool_transcript_bijection(messages);
|
||||
|
||||
// Assert the recorded order, not just membership: execution order is what
|
||||
// decides which call wins, so sorting here would erase the property.
|
||||
let pre = lifecycle_ids(&env, "pre.log");
|
||||
let results = lifecycle_ids(&env, "result.log");
|
||||
let posts = lifecycle_ids(&env, "post.log");
|
||||
assert!(
|
||||
env.payloads("postfail.log").is_empty(),
|
||||
"both calls succeed, so no post-failure event may be recorded"
|
||||
);
|
||||
let expected = vec!["final-a".to_string(), "final-b".to_string()];
|
||||
assert_eq!(pre, expected, "both calls need a PreToolUse");
|
||||
assert_eq!(results, expected, "both calls need a PreToolUseResult");
|
||||
assert_eq!(posts, expected, "both calls need a post event");
|
||||
|
||||
let published: Vec<_> = messages
|
||||
.iter()
|
||||
.filter(|message| message.as_concat_text() == r#"{"answer":"second"}"#)
|
||||
.collect();
|
||||
assert_eq!(
|
||||
published.len(),
|
||||
1,
|
||||
"the last valid final output is published exactly once"
|
||||
);
|
||||
assert!(
|
||||
messages
|
||||
.iter()
|
||||
.all(|message| message.as_concat_text() != r#"{"answer":"first"}"#),
|
||||
"the superseded final output must not be published"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A malformed final-output call gets one parse-error response and runs no
|
||||
/// lifecycle, because nothing executes.
|
||||
#[tokio::test]
|
||||
@@ -1511,45 +1379,47 @@ async fn malformed_final_output_call_receives_parse_error_and_no_lifecycle() ->
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// An unfinished ordinary sibling still delays publication, and every request in
|
||||
/// the block is answered exactly once.
|
||||
#[tokio::test]
|
||||
async fn final_output_waits_for_ordinary_sibling_and_answers_every_request() -> Result<()> {
|
||||
async fn two_final_outputs_in_one_reply_are_both_rejected() -> Result<()> {
|
||||
let (pipeline, api) = test_pipeline().await?;
|
||||
pipeline.set_recipe(final_output_recipe()).await?;
|
||||
api.on("finish and add").calls([
|
||||
api.on("finish twice").calls([
|
||||
(
|
||||
"final-output",
|
||||
"first-output",
|
||||
FINAL_OUTPUT_TOOL_NAME,
|
||||
serde_json::json!({ "answer": "after sibling" }),
|
||||
serde_json::json!({ "answer": "first" }),
|
||||
),
|
||||
(
|
||||
"second-output",
|
||||
FINAL_OUTPUT_TOOL_NAME,
|
||||
serde_json::json!({ "answer": "second" }),
|
||||
),
|
||||
("side-effect", ADD, value(1)),
|
||||
]);
|
||||
api.on(FINAL_OUTPUT_NOT_ALONE_MESSAGE).call(
|
||||
FINAL_OUTPUT_TOOL_NAME,
|
||||
serde_json::json!({ "answer": "alone" }),
|
||||
);
|
||||
|
||||
let result = pipeline.run(["finish and add"]).await?;
|
||||
let result = pipeline.run(["finish twice"]).await?;
|
||||
|
||||
assert_eq!(api.call_count(), 2);
|
||||
let messages = result.conversation().messages();
|
||||
assert_tool_transcript_bijection(messages);
|
||||
assert_eq!(pipeline.calculator_total(), 1);
|
||||
|
||||
let sibling_answer = messages
|
||||
let rejected = messages
|
||||
.iter()
|
||||
.position(|message| {
|
||||
message.content.iter().any(|content| {
|
||||
matches!(
|
||||
content,
|
||||
MessageContent::ToolResponse(response) if response.id == "side-effect"
|
||||
)
|
||||
})
|
||||
.flat_map(|message| &message.content)
|
||||
.filter(|content| {
|
||||
matches!(
|
||||
content,
|
||||
MessageContent::ToolResponse(response)
|
||||
if response.tool_result.as_ref().is_err_and(|error| {
|
||||
error.message.ends_with(FINAL_OUTPUT_NOT_ALONE_MESSAGE)
|
||||
})
|
||||
)
|
||||
})
|
||||
.expect("sibling tool response");
|
||||
let published = messages
|
||||
.iter()
|
||||
.position(|message| message.as_concat_text() == r#"{"answer":"after sibling"}"#)
|
||||
.expect("published final output");
|
||||
assert!(
|
||||
sibling_answer < published,
|
||||
"final output must wait until the ordinary sibling finishes"
|
||||
);
|
||||
.count();
|
||||
assert_eq!(rejected, 2);
|
||||
result.assert_message(-1, Agent, r#"{"answer":"alone"}"#);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use std::collections::VecDeque;
|
||||
use std::collections::{HashSet, VecDeque};
|
||||
use std::sync::{Arc, Mutex as StdMutex};
|
||||
|
||||
use anyhow::Result;
|
||||
@@ -15,13 +15,9 @@ use crate::agents::extension_manager::{
|
||||
};
|
||||
use crate::agents::mcp_client::McpClientTrait;
|
||||
use crate::agents::state_machine::{
|
||||
BangShellOperation, CompactionOperation, DoctorOperation, Emitter, ExitOnErrorOperation,
|
||||
GooseEffect, GooseInferenceProvider, GooseInferenceRequestPreparer, InferenceRunner,
|
||||
MaxTurnsOperation, Operation, ProjectOperation, RecipeOperation, RetryOperation, RunStatus,
|
||||
SkillOperation, SlashCommandOperation, StateMachine, StatusOperation, SteerOperation,
|
||||
SteerQueue, Step, StopHookOperation, SubagentOperation, ToolApprovalOperation,
|
||||
ToolExecutionOperation, ToolPairCompactionOperation, UnknownToolOperation,
|
||||
messages_since_kickoff, Emitter, GooseEffect, RunStatus, StateMachine, SteerQueue,
|
||||
};
|
||||
use crate::agents::state_machine_builder::{build_state_machine, StateMachineConfig};
|
||||
use crate::agents::subagent_handler::SubagentRunner;
|
||||
use crate::agents::AgentEvent;
|
||||
use crate::config::permission::{PermissionLevel, PermissionManager};
|
||||
@@ -111,93 +107,30 @@ impl TestPipeline {
|
||||
&self,
|
||||
cancel: CancellationToken,
|
||||
) -> StateMachine<'_, Session, GooseEffect> {
|
||||
let provider = self.provider.clone();
|
||||
let tool_call_cutoff = crate::context_mgmt::compute_tool_call_cutoff(
|
||||
self.model_config.context_limit(),
|
||||
COMPACTION_THRESHOLD,
|
||||
);
|
||||
let mut operations: Vec<Arc<dyn Operation<Session, GooseEffect> + '_>> = vec![
|
||||
Arc::new(SteerOperation::new(
|
||||
self.steer_queue.clone(),
|
||||
self.hook_manager.clone(),
|
||||
)),
|
||||
Arc::new(BangShellOperation::new()),
|
||||
];
|
||||
if !self.provider_features.manages_own_context {
|
||||
operations.push(Arc::new(CompactionOperation::new(
|
||||
provider.clone(),
|
||||
self.model_config.clone(),
|
||||
self.model_config.context_limit(),
|
||||
let context_limit = self.model_config.context_limit();
|
||||
build_state_machine(StateMachineConfig {
|
||||
provider: self.provider.clone(),
|
||||
model_config: self.model_config.clone(),
|
||||
context_limit,
|
||||
compaction_threshold: COMPACTION_THRESHOLD,
|
||||
tool_call_cutoff: crate::context_mgmt::compute_tool_call_cutoff(
|
||||
context_limit,
|
||||
COMPACTION_THRESHOLD,
|
||||
)));
|
||||
}
|
||||
let extension_lease = Arc::clone(&self.extension_lease);
|
||||
let remaining_operations: Vec<Arc<dyn Operation<Session, GooseEffect> + '_>> = vec![
|
||||
Arc::new(ToolPairCompactionOperation::new(
|
||||
provider.clone(),
|
||||
self.model_config.clone(),
|
||||
tool_call_cutoff,
|
||||
!self.provider_features.manages_own_context,
|
||||
)),
|
||||
Arc::new(ToolApprovalOperation::new(&self.tool_inspection_manager)),
|
||||
Arc::new(DoctorOperation::new(self.session_manager.clone())),
|
||||
Arc::new(ProjectOperation),
|
||||
Arc::new(SkillOperation::new(
|
||||
self.hook_manager.clone(),
|
||||
Arc::clone(&extension_lease),
|
||||
)),
|
||||
Arc::new(SubagentOperation::new(
|
||||
SubagentRunner::new(Arc::clone(&self.subagent_host)),
|
||||
cancel.clone(),
|
||||
)),
|
||||
Arc::new(RecipeOperation::new(
|
||||
provider.clone(),
|
||||
self.hook_manager.clone(),
|
||||
)),
|
||||
Arc::new(ToolExecutionOperation::new(
|
||||
self.extension_manager.clone(),
|
||||
self.hook_manager.clone(),
|
||||
Arc::clone(&extension_lease),
|
||||
)),
|
||||
Arc::new(UnknownToolOperation::new(self.hook_manager.clone())),
|
||||
Arc::new(RetryOperation::new(
|
||||
std::time::Duration::from_secs(1),
|
||||
std::time::Duration::from_secs(1),
|
||||
)),
|
||||
Arc::new(StopHookOperation::new(
|
||||
self.hook_manager.clone(),
|
||||
self.stop_hook_block_cap,
|
||||
)),
|
||||
Arc::new(ExitOnErrorOperation),
|
||||
Arc::new(MaxTurnsOperation::new(self.max_turns)),
|
||||
];
|
||||
operations.extend(remaining_operations);
|
||||
let request_preparer = GooseInferenceRequestPreparer {
|
||||
extension_manager: Arc::clone(&self.extension_manager),
|
||||
extension_lease,
|
||||
),
|
||||
tool_pair_summarization: true,
|
||||
session_manager: self.session_manager.clone(),
|
||||
extension_manager: self.extension_manager.clone(),
|
||||
extension_lease: Arc::clone(&self.extension_lease),
|
||||
tool_inspection_manager: &self.tool_inspection_manager,
|
||||
context_limit: self.model_config.context_limit(),
|
||||
};
|
||||
let status_operation = Arc::new(StatusOperation::new(
|
||||
provider.clone(),
|
||||
self.model_config.clone(),
|
||||
));
|
||||
let inference_provider = Arc::new(GooseInferenceProvider::new(provider));
|
||||
let inference = Arc::new(
|
||||
InferenceRunner::new(inference_provider, self.model_config.clone())
|
||||
.with_request_preparer(Arc::new(request_preparer)),
|
||||
);
|
||||
let mut command_handlers = operations.clone();
|
||||
command_handlers.push(status_operation);
|
||||
let command_operation: Arc<dyn Operation<Session, GooseEffect> + '_> =
|
||||
Arc::new(SlashCommandOperation::new(command_handlers));
|
||||
let steps = std::iter::once(command_operation)
|
||||
.chain(operations)
|
||||
.map(Step::Operation)
|
||||
.chain(std::iter::once(Step::Inference(inference)))
|
||||
.collect();
|
||||
|
||||
StateMachine::new(steps, cancel)
|
||||
hook_manager: self.hook_manager.clone(),
|
||||
steer_queue: self.steer_queue.clone(),
|
||||
subagent_runner: SubagentRunner::new(Arc::clone(&self.subagent_host)),
|
||||
retry_timeout: std::time::Duration::from_secs(1),
|
||||
on_failure_timeout: std::time::Duration::from_secs(1),
|
||||
stop_hook_block_cap: self.stop_hook_block_cap,
|
||||
max_turns: self.max_turns,
|
||||
cancel,
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) async fn with_goose_mode(self, mode: GooseMode) -> Self {
|
||||
@@ -442,6 +375,7 @@ impl TestPipeline {
|
||||
let session = self.session().await?;
|
||||
let machine = self.machine(cancel.clone());
|
||||
let mut result = machine.step(&session, &emit).await?;
|
||||
assert_only_tool_operations_act_on_open_calls(&session, result.applied_step);
|
||||
machine
|
||||
.apply(
|
||||
self.session_manager.as_ref(),
|
||||
@@ -653,6 +587,7 @@ impl TestPipeline {
|
||||
}
|
||||
}
|
||||
};
|
||||
assert_only_tool_operations_act_on_open_calls(&session, result.applied_step);
|
||||
machine
|
||||
.apply(
|
||||
self.session_manager.as_ref(),
|
||||
@@ -1134,6 +1069,45 @@ impl TestRun {
|
||||
}
|
||||
}
|
||||
|
||||
const TOOL_OPERATIONS: [&str; 5] = [
|
||||
"tool_approval",
|
||||
"skills",
|
||||
"recipe",
|
||||
"tool_execution",
|
||||
"remaining_tool_calls",
|
||||
];
|
||||
|
||||
fn assert_only_tool_operations_act_on_open_calls(session: &Session, applied_step: Option<&str>) {
|
||||
let Some(step) = applied_step else {
|
||||
return;
|
||||
};
|
||||
let conversation = session
|
||||
.conversation
|
||||
.as_ref()
|
||||
.expect("session has a conversation");
|
||||
let turn = messages_since_kickoff(conversation).expect("conversation has a turn");
|
||||
assert!(
|
||||
TOOL_OPERATIONS.contains(&step) || !has_open_tool_call(turn),
|
||||
"{step} acted while a tool call was open"
|
||||
);
|
||||
}
|
||||
|
||||
fn has_open_tool_call(turn: &[Message]) -> bool {
|
||||
let mut open = HashSet::new();
|
||||
for content in turn.iter().flat_map(|message| &message.content) {
|
||||
match content {
|
||||
MessageContent::ToolRequest(request) if !request.was_executed_externally() => {
|
||||
open.insert(request.id.as_str());
|
||||
}
|
||||
MessageContent::ToolResponse(response) => {
|
||||
open.remove(response.id.as_str());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
!open.is_empty()
|
||||
}
|
||||
|
||||
pub(super) async fn run_machine(pipeline: &TestPipeline) -> Result<Vec<AgentEvent>> {
|
||||
let cancel = CancellationToken::new();
|
||||
let machine = pipeline.machine(cancel.clone());
|
||||
@@ -1144,6 +1118,7 @@ pub(super) async fn run_machine(pipeline: &TestPipeline) -> Result<Vec<AgentEven
|
||||
loop {
|
||||
let session = pipeline.session().await?;
|
||||
let mut result = machine.step(&session, &emit).await?;
|
||||
assert_only_tool_operations_act_on_open_calls(&session, result.applied_step);
|
||||
machine
|
||||
.apply(
|
||||
pipeline.session_manager.as_ref(),
|
||||
|
||||
@@ -98,6 +98,47 @@ async fn prompt_and_skill_lifecycle() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn builtin_commands_win_over_skills_and_recipes() -> Result<()> {
|
||||
let (pipeline, api) = test_pipeline().await?;
|
||||
for name in ["compact", "clear"] {
|
||||
let skill_dir = pipeline.working_dir().join(".agents/skills").join(name);
|
||||
std::fs::create_dir_all(&skill_dir)?;
|
||||
std::fs::write(
|
||||
skill_dir.join("SKILL.md"),
|
||||
format!("---\nname: {name}\ndescription: Shadowing skill\n---\nSHADOWING_SKILL_BODY"),
|
||||
)?;
|
||||
}
|
||||
let recipe_path = pipeline.working_dir().join("status.yaml");
|
||||
std::fs::write(
|
||||
&recipe_path,
|
||||
"version: 1.0.0\ntitle: Status\ndescription: Shadowing recipe\nprompt: SHADOWING_RECIPE_PROMPT\n",
|
||||
)?;
|
||||
let mappings = json!([{ "command": "status", "recipe_path": recipe_path }]).to_string();
|
||||
let _guard = env_lock::lock_env([("SLASH_COMMANDS", Some(mappings.as_str()))]);
|
||||
|
||||
let status = pipeline.run(["/status"]).await?;
|
||||
status.assert_message(-1, Agent, "Session status");
|
||||
assert_eq!(api.call_count(), 0);
|
||||
|
||||
api.on("first turn").reply("first reply");
|
||||
pipeline.run(["first turn"]).await?;
|
||||
api.on("Please summarize the conversation history")
|
||||
.reply("summary");
|
||||
let compacted = pipeline.run(["/compact"]).await?;
|
||||
compacted.assert_emitted("Compaction complete");
|
||||
|
||||
let cleared = pipeline.run(["/clear"]).await?;
|
||||
cleared.assert_message(-1, Agent, "Conversation cleared");
|
||||
|
||||
assert!(api.calls().iter().all(|call| {
|
||||
!call.input_contains("SHADOWING_SKILL_BODY")
|
||||
&& !call.input_contains("SHADOWING_RECIPE_PROMPT")
|
||||
}));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "code-mode")]
|
||||
#[tokio::test]
|
||||
async fn unadvertised_load_skill_is_neither_approved_nor_loaded() -> Result<()> {
|
||||
|
||||
@@ -16,6 +16,7 @@ use crate::agents::types::{RetryConfig, SuccessCheck};
|
||||
#[cfg(feature = "code-mode")]
|
||||
use crate::config::permission::PermissionLevel;
|
||||
use crate::config::GooseMode;
|
||||
use crate::conversation::message::Message;
|
||||
#[cfg(feature = "code-mode")]
|
||||
use crate::conversation::message::MessageContent;
|
||||
use crate::recipe::build_recipe::build_recipe_from_template;
|
||||
@@ -248,6 +249,47 @@ async fn final_output_beside_delegate_is_not_shown_before_the_subagent_runs() ->
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn final_output_nudge_goes_in_before_a_queued_steer() -> Result<()> {
|
||||
let (pipeline, api) = test_pipeline().await?;
|
||||
let recipe = Recipe::builder()
|
||||
.title("Structured answer")
|
||||
.description("Returns structured output")
|
||||
.prompt("Research the topic")
|
||||
.response(Response {
|
||||
json_schema: Some(json!({
|
||||
"type": "object",
|
||||
"properties": { "result": { "type": "string" } },
|
||||
"required": ["result"]
|
||||
})),
|
||||
})
|
||||
.build()
|
||||
.expect("valid recipe");
|
||||
pipeline.set_recipe(recipe).await?;
|
||||
pipeline
|
||||
.steer(Message::user().with_text("then be brief"))
|
||||
.await;
|
||||
api.on("Research the topic").reply("draft");
|
||||
api.on(FINAL_OUTPUT_CONTINUATION_MESSAGE)
|
||||
.call(FINAL_OUTPUT_TOOL_NAME, json!({ "result": "done" }));
|
||||
api.on("then be brief")
|
||||
.call(FINAL_OUTPUT_TOOL_NAME, json!({ "result": "brief" }));
|
||||
|
||||
let result = pipeline.run(["Research the topic"]).await?;
|
||||
|
||||
assert_eq!(api.call_count(), 3);
|
||||
let messages = result.conversation().messages();
|
||||
let position = |text: &str| {
|
||||
messages
|
||||
.iter()
|
||||
.position(|message| message.as_concat_text().contains(text))
|
||||
.unwrap_or_else(|| panic!("{text} was persisted"))
|
||||
};
|
||||
assert!(position(FINAL_OUTPUT_CONTINUATION_MESSAGE) < position("then be brief"));
|
||||
result.assert_message(-1, Agent, r#"{"result":"brief"}"#);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn recipe_retry_and_final_output_run_to_completion() -> Result<()> {
|
||||
let (pipeline, api) = test_pipeline().await?;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Result;
|
||||
use rmcp::model::ElicitationAction;
|
||||
use rmcp::model::{CallToolRequestParams, ElicitationAction, ErrorData};
|
||||
use serde_json::json;
|
||||
|
||||
use super::calculator_extension::{
|
||||
@@ -12,6 +12,7 @@ use super::pipeline::MessageKind::{Agent, Confirmation, ToolCall, ToolResponse};
|
||||
use super::pipeline::TestPipeline;
|
||||
use super::pipeline::MAX_TURNS;
|
||||
use super::test_pipeline;
|
||||
use crate::agents::state_machine::MAX_TURNS_MESSAGE;
|
||||
use crate::agents::tool_execution::{CHAT_MODE_TOOL_SKIPPED_RESPONSE, DECLINED_RESPONSE};
|
||||
use crate::agents::AgentEvent;
|
||||
use crate::config::permission::PermissionLevel;
|
||||
@@ -83,6 +84,65 @@ async fn steer_waits_for_an_open_tool_confirmation() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn max_turns_waits_for_an_open_tool_confirmation() -> Result<()> {
|
||||
let (pipeline, api) = pipeline_with_gated_divide().await?;
|
||||
let pipeline = pipeline.with_max_turns(1);
|
||||
|
||||
let awaiting_confirmation = pipeline.run(["add four and halve it"]).await?;
|
||||
awaiting_confirmation.assert_message(-1, ToolResponse, "result: 4");
|
||||
|
||||
pipeline.confirm("gated", Permission::AllowOnce).await?;
|
||||
let result = pipeline.resume().await?;
|
||||
result.assert_message(-1, Agent, MAX_TURNS_MESSAGE);
|
||||
assert_eq!(pipeline.calculator_total(), 2);
|
||||
assert_eq!(api.call_count(), 1);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn steer_waits_for_calls_answered_by_different_operations() -> Result<()> {
|
||||
let (pipeline, api) = test_pipeline().await?;
|
||||
let arguments = value(1).as_object().unwrap().clone();
|
||||
pipeline
|
||||
.seed([
|
||||
Message::user().with_text("do two things"),
|
||||
Message::assistant()
|
||||
.with_tool_request(
|
||||
"valid",
|
||||
Ok(CallToolRequestParams::new(ADD).with_arguments(arguments)),
|
||||
)
|
||||
.with_tool_request(
|
||||
"malformed",
|
||||
Err(ErrorData::invalid_params("bad json", None)),
|
||||
),
|
||||
])
|
||||
.await?;
|
||||
pipeline
|
||||
.steer(Message::user().with_text("then say done"))
|
||||
.await;
|
||||
api.on("then say done").reply("done");
|
||||
|
||||
let result = pipeline.resume().await?;
|
||||
|
||||
result.assert_message(-1, Agent, "done");
|
||||
let messages = result.conversation().messages();
|
||||
let answer = |id: &str| {
|
||||
messages
|
||||
.iter()
|
||||
.position(|message| message.get_tool_response_ids().contains(id))
|
||||
.unwrap_or_else(|| panic!("{id} was answered"))
|
||||
};
|
||||
let steer = messages
|
||||
.iter()
|
||||
.position(|message| message.as_concat_text() == "then say done")
|
||||
.expect("steer was persisted");
|
||||
assert!(answer("valid") < steer);
|
||||
assert!(answer("malformed") < steer);
|
||||
assert!(api.calls().last().unwrap().answers_every_tool_call_next());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn basic_tool_calling() -> Result<()> {
|
||||
let (pipeline, api) = test_pipeline().await?;
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
use std::sync::{Arc, Mutex as StdMutex};
|
||||
use std::time::Duration;
|
||||
|
||||
use goose_providers::model::ModelConfig;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use crate::agents::extension_manager::{ExtensionLease, ExtensionManager};
|
||||
use crate::agents::state_machine::{
|
||||
BangShellOperation, CompactionOperation, DoctorOperation, EmptyResponseOperation,
|
||||
ExitOnErrorOperation, GooseEffect, GooseInferenceProvider, GooseInferenceRequestPreparer,
|
||||
InferenceRunner, MaxTurnsOperation, Operation, ProjectOperation, RecipeOperation,
|
||||
RemainingToolCallsOperation, RetryOperation, SkillOperation, SlashCommandOperation,
|
||||
StateMachine, StatusOperation, SteerOperation, SteerQueue, Step, StopHookOperation,
|
||||
SubagentOperation, ToolApprovalOperation, ToolExecutionOperation, ToolPairCompactionOperation,
|
||||
};
|
||||
use crate::agents::subagent_handler::SubagentRunner;
|
||||
use crate::hooks::HookManager;
|
||||
use crate::providers::base::Provider;
|
||||
use crate::session::{Session, SessionManager};
|
||||
use crate::tool_inspection::ToolInspectionManager;
|
||||
|
||||
pub(crate) struct StateMachineConfig<'a> {
|
||||
pub(crate) provider: Arc<dyn Provider>,
|
||||
pub(crate) model_config: ModelConfig,
|
||||
pub(crate) context_limit: usize,
|
||||
pub(crate) compaction_threshold: f64,
|
||||
pub(crate) tool_call_cutoff: usize,
|
||||
pub(crate) tool_pair_summarization: bool,
|
||||
pub(crate) session_manager: Arc<SessionManager>,
|
||||
pub(crate) extension_manager: Arc<ExtensionManager>,
|
||||
pub(crate) extension_lease: Arc<StdMutex<Option<Arc<ExtensionLease>>>>,
|
||||
pub(crate) tool_inspection_manager: &'a ToolInspectionManager,
|
||||
pub(crate) hook_manager: HookManager,
|
||||
pub(crate) steer_queue: SteerQueue,
|
||||
pub(crate) subagent_runner: SubagentRunner,
|
||||
pub(crate) retry_timeout: Duration,
|
||||
pub(crate) on_failure_timeout: Duration,
|
||||
pub(crate) stop_hook_block_cap: u32,
|
||||
pub(crate) max_turns: u32,
|
||||
pub(crate) cancel: CancellationToken,
|
||||
}
|
||||
|
||||
pub(crate) fn build_state_machine(
|
||||
config: StateMachineConfig<'_>,
|
||||
) -> StateMachine<'_, Session, GooseEffect> {
|
||||
let StateMachineConfig {
|
||||
provider,
|
||||
model_config,
|
||||
context_limit,
|
||||
compaction_threshold,
|
||||
tool_call_cutoff,
|
||||
tool_pair_summarization,
|
||||
session_manager,
|
||||
extension_manager,
|
||||
extension_lease,
|
||||
tool_inspection_manager,
|
||||
hook_manager,
|
||||
steer_queue,
|
||||
subagent_runner,
|
||||
retry_timeout,
|
||||
on_failure_timeout,
|
||||
stop_hook_block_cap,
|
||||
max_turns,
|
||||
cancel,
|
||||
} = config;
|
||||
let manages_own_context = provider.manages_own_context();
|
||||
|
||||
let mut operations: Vec<Arc<dyn Operation<Session, GooseEffect> + '_>> = vec![
|
||||
Arc::new(ToolApprovalOperation::new(tool_inspection_manager)),
|
||||
Arc::new(SkillOperation::new(
|
||||
hook_manager.clone(),
|
||||
Arc::clone(&extension_lease),
|
||||
)),
|
||||
Arc::new(RecipeOperation::new(provider.clone(), hook_manager.clone())),
|
||||
Arc::new(ToolExecutionOperation::new(
|
||||
Arc::clone(&extension_manager),
|
||||
hook_manager.clone(),
|
||||
Arc::clone(&extension_lease),
|
||||
)),
|
||||
Arc::new(RemainingToolCallsOperation::new(hook_manager.clone())),
|
||||
Arc::new(SubagentOperation::new(subagent_runner, cancel.clone())),
|
||||
Arc::new(SteerOperation::new(steer_queue, hook_manager.clone())),
|
||||
Arc::new(BangShellOperation::new()),
|
||||
];
|
||||
if !manages_own_context {
|
||||
operations.push(Arc::new(CompactionOperation::new(
|
||||
provider.clone(),
|
||||
model_config.clone(),
|
||||
context_limit,
|
||||
compaction_threshold,
|
||||
)));
|
||||
}
|
||||
let remaining_operations: Vec<Arc<dyn Operation<Session, GooseEffect> + '_>> = vec![
|
||||
Arc::new(ToolPairCompactionOperation::new(
|
||||
provider.clone(),
|
||||
model_config.clone(),
|
||||
tool_call_cutoff,
|
||||
tool_pair_summarization && !manages_own_context,
|
||||
)),
|
||||
Arc::new(DoctorOperation::new(session_manager)),
|
||||
Arc::new(ProjectOperation),
|
||||
Arc::new(RetryOperation::new(retry_timeout, on_failure_timeout)),
|
||||
Arc::new(EmptyResponseOperation),
|
||||
Arc::new(StopHookOperation::new(hook_manager, stop_hook_block_cap)),
|
||||
Arc::new(ExitOnErrorOperation),
|
||||
Arc::new(MaxTurnsOperation::new(max_turns)),
|
||||
];
|
||||
operations.extend(remaining_operations);
|
||||
let request_preparer = GooseInferenceRequestPreparer {
|
||||
extension_manager,
|
||||
extension_lease,
|
||||
tool_inspection_manager,
|
||||
context_limit,
|
||||
};
|
||||
let status_operation = Arc::new(StatusOperation::new(provider.clone(), model_config.clone()));
|
||||
let inference_provider = Arc::new(GooseInferenceProvider::new(provider));
|
||||
let inference = Arc::new(
|
||||
InferenceRunner::new(inference_provider, model_config)
|
||||
.with_request_preparer(Arc::new(request_preparer)),
|
||||
);
|
||||
let mut command_handlers = operations.clone();
|
||||
command_handlers.push(status_operation);
|
||||
let command_operation: Arc<dyn Operation<Session, GooseEffect> + '_> =
|
||||
Arc::new(SlashCommandOperation::new(command_handlers));
|
||||
let steps = std::iter::once(command_operation)
|
||||
.chain(operations)
|
||||
.map(Step::Operation)
|
||||
.chain(std::iter::once(Step::Inference(inference)))
|
||||
.collect();
|
||||
|
||||
StateMachine::new(steps, cancel)
|
||||
}
|
||||
@@ -15,8 +15,6 @@ use indoc::indoc;
|
||||
use rmcp::model::Role;
|
||||
#[cfg(test)]
|
||||
use rmcp::model::{Annotations, ContentBlock, TextContent};
|
||||
use std::sync::Arc;
|
||||
use tokio::task::JoinHandle;
|
||||
use tracing::info;
|
||||
use tracing::log::warn;
|
||||
|
||||
@@ -508,105 +506,6 @@ pub async fn summarize_tool_call(
|
||||
Ok(response.with_generated_id())
|
||||
}
|
||||
|
||||
pub fn maybe_summarize_tool_pairs(
|
||||
provider: Arc<dyn Provider>,
|
||||
model_config: ModelConfig,
|
||||
session_id: String,
|
||||
conversation: Conversation,
|
||||
cutoff: usize,
|
||||
protect_last_n: usize,
|
||||
) -> Option<JoinHandle<Vec<(Message, String)>>> {
|
||||
if !tool_pair_summarization_enabled() || provider.manages_own_context() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let tool_ids = tool_ids_to_summarize(&conversation, cutoff, protect_last_n);
|
||||
if tool_ids.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// A request/response message can contain multiple parallel tool calls.
|
||||
// Summarization formats whole messages, so sibling IDs in the same pair
|
||||
// must be compacted as one group or we'd issue duplicate summary calls.
|
||||
let mut seen_message_pairs = std::collections::HashSet::new();
|
||||
let mut grouped_tool_ids = Vec::new();
|
||||
for tool_id in tool_ids {
|
||||
let pair = match agent_visible_tool_pair(&conversation, &tool_id) {
|
||||
Ok(pair) => pair,
|
||||
Err(error) => {
|
||||
warn!("Failed to identify tool pair for summarization: {}", error);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
if pair.len() != 2 {
|
||||
warn!(
|
||||
"Expected a tool request/response pair for '{}', found {} messages",
|
||||
tool_id,
|
||||
pair.len()
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
let request_ids: std::collections::HashSet<&str> = pair
|
||||
.iter()
|
||||
.flat_map(|message| message.get_tool_request_ids())
|
||||
.collect();
|
||||
let response_ids: std::collections::HashSet<&str> = pair
|
||||
.iter()
|
||||
.flat_map(|message| message.get_tool_response_ids())
|
||||
.collect();
|
||||
if request_ids != response_ids {
|
||||
warn!(
|
||||
"Tool pair for '{}' has siblings answered elsewhere; skipping",
|
||||
tool_id
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut message_ids = pair
|
||||
.iter()
|
||||
.filter_map(|message| message.id.clone())
|
||||
.collect::<Vec<_>>();
|
||||
if message_ids.len() != 2 {
|
||||
warn!(
|
||||
"Expected two persisted messages for tool pair '{}', found {}",
|
||||
tool_id,
|
||||
message_ids.len()
|
||||
);
|
||||
continue;
|
||||
}
|
||||
message_ids.sort_unstable();
|
||||
if seen_message_pairs.insert(message_ids) {
|
||||
grouped_tool_ids.push(tool_id);
|
||||
}
|
||||
}
|
||||
|
||||
if grouped_tool_ids.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(tokio::spawn(async move {
|
||||
let mut results = Vec::new();
|
||||
for tool_id in grouped_tool_ids {
|
||||
match summarize_tool_call(
|
||||
provider.as_ref(),
|
||||
&model_config,
|
||||
&session_id,
|
||||
&conversation,
|
||||
&tool_id,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(summary) => results.push((summary, tool_id)),
|
||||
Err(e) => {
|
||||
warn!("Failed to summarize tool pair: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
results
|
||||
}))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -643,7 +542,6 @@ mod tests {
|
||||
config: ModelConfig,
|
||||
max_tool_responses: Option<usize>,
|
||||
captured_system: std::sync::Mutex<Option<String>>,
|
||||
calls: std::sync::atomic::AtomicUsize,
|
||||
}
|
||||
|
||||
impl MockProvider {
|
||||
@@ -664,7 +562,6 @@ mod tests {
|
||||
},
|
||||
max_tool_responses: None,
|
||||
captured_system: std::sync::Mutex::new(None),
|
||||
calls: std::sync::atomic::AtomicUsize::new(0),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -672,10 +569,6 @@ mod tests {
|
||||
self.max_tool_responses = Some(max);
|
||||
self
|
||||
}
|
||||
|
||||
fn call_count(&self) -> usize {
|
||||
self.calls.load(std::sync::atomic::Ordering::Relaxed)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
@@ -691,8 +584,6 @@ mod tests {
|
||||
messages: &[Message],
|
||||
_tools: &[Tool],
|
||||
) -> Result<MessageStream, ProviderError> {
|
||||
self.calls
|
||||
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
*self.captured_system.lock().unwrap() = Some(system.to_string());
|
||||
// If max_tool_responses is set, fail if we have too many
|
||||
if let Some(max) = self.max_tool_responses {
|
||||
@@ -1191,58 +1082,6 @@ mod tests {
|
||||
assert!(error.to_string().contains("No agent-visible tool pair"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn parallel_tool_calls_share_one_summary_request() {
|
||||
let _guard = env_lock::lock_env([("GOOSE_TOOL_PAIR_SUMMARIZATION", Some("true"))]);
|
||||
let provider = Arc::new(MockProvider::new(
|
||||
Message::assistant().with_text("summary"),
|
||||
1000,
|
||||
));
|
||||
let mut messages = vec![Message::user().with_text("start")];
|
||||
for index in 0..7 {
|
||||
let first_id = format!("tool_{index}a");
|
||||
let second_id = format!("tool_{index}b");
|
||||
messages.push(
|
||||
Message::assistant()
|
||||
.with_tool_request(&first_id, Ok(CallToolRequestParams::new("read_file")))
|
||||
.with_tool_request(&second_id, Ok(CallToolRequestParams::new("read_file")))
|
||||
.with_id(format!("request_{index}")),
|
||||
);
|
||||
messages.push(
|
||||
Message::user()
|
||||
.with_tool_response(
|
||||
&first_id,
|
||||
Ok(rmcp::model::CallToolResult::success(vec![
|
||||
ContentBlock::text("first result"),
|
||||
])),
|
||||
)
|
||||
.with_tool_response(
|
||||
&second_id,
|
||||
Ok(rmcp::model::CallToolResult::success(vec![
|
||||
ContentBlock::text("second result"),
|
||||
])),
|
||||
)
|
||||
.with_id(format!("response_{index}")),
|
||||
);
|
||||
}
|
||||
|
||||
let conversation = Conversation::new_unvalidated(messages);
|
||||
let summaries = maybe_summarize_tool_pairs(
|
||||
provider.clone(),
|
||||
provider.config.clone(),
|
||||
"test-session-id".to_string(),
|
||||
conversation,
|
||||
2,
|
||||
0,
|
||||
)
|
||||
.unwrap()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(summaries.len(), 5);
|
||||
assert_eq!(provider.call_count(), 5);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_progressive_removal_on_context_exceeded() {
|
||||
let response_message = Message::assistant().with_text("<mock summary>");
|
||||
|
||||
@@ -5,6 +5,7 @@ use anyhow::{anyhow, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tracing::warn;
|
||||
|
||||
use super::slash_command::is_builtin_command;
|
||||
use super::types::{SlashCommandEntry, SlashCommandSource};
|
||||
use super::util::normalize_command_name;
|
||||
use crate::config::Config;
|
||||
@@ -65,6 +66,10 @@ pub fn get_recipe_for_command(command: &str) -> Option<PathBuf> {
|
||||
.map(|mapping| PathBuf::from(mapping.recipe_path))
|
||||
}
|
||||
|
||||
pub(super) fn get_existing_recipe_for_command(command: &str) -> Option<PathBuf> {
|
||||
get_recipe_for_command(command).filter(|recipe_path| recipe_path.exists())
|
||||
}
|
||||
|
||||
pub(super) fn commands_from_mappings(mappings: Vec<SlashCommandMapping>) -> Vec<SlashCommandEntry> {
|
||||
mappings
|
||||
.into_iter()
|
||||
@@ -150,14 +155,12 @@ pub fn resolve_command(
|
||||
command: &str,
|
||||
params_str: &str,
|
||||
) -> Result<Option<(Recipe, String)>, String> {
|
||||
let full_command = format!("/{}", command);
|
||||
let Some(recipe_path) = get_recipe_for_command(&full_command) else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
if !recipe_path.exists() {
|
||||
if is_builtin_command(command) {
|
||||
return Ok(None);
|
||||
}
|
||||
let Some(recipe_path) = get_existing_recipe_for_command(command) else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
let recipe_content =
|
||||
std::fs::read_to_string(&recipe_path).map_err(|e| invalid_recipe_msg(command, e))?;
|
||||
|
||||
@@ -2,6 +2,8 @@ use std::path::Path;
|
||||
|
||||
use goose_sdk_types::custom_requests::{SourceEntry, SourceType};
|
||||
|
||||
use super::recipe_slash_command::get_existing_recipe_for_command;
|
||||
use super::slash_command::is_builtin_command;
|
||||
use super::types::{SlashCommandEntry, SlashCommandSource};
|
||||
use super::util::normalize_command_name;
|
||||
|
||||
@@ -45,9 +47,13 @@ pub fn resolve_command(
|
||||
params_str: &str,
|
||||
working_dir: Option<&Path>,
|
||||
) -> Result<Option<String>, String> {
|
||||
if is_builtin_command(command) || get_existing_recipe_for_command(command).is_some() {
|
||||
return Ok(None);
|
||||
}
|
||||
let command_name = normalize_command_name(command);
|
||||
let Some(skill) = crate::skills::list_installed_skills(working_dir)
|
||||
.into_iter()
|
||||
.find(|skill| skill.name.eq_ignore_ascii_case(command))
|
||||
.find(|skill| normalize_command_name(&skill.name) == command_name)
|
||||
else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
@@ -3,6 +3,7 @@ use std::path::Path;
|
||||
|
||||
use super::types::{SlashCommandEntry, SlashCommandSource};
|
||||
use super::util::normalize_command_name;
|
||||
use crate::agents::execute_commands;
|
||||
|
||||
pub fn list_builtin_commands() -> Vec<SlashCommandEntry> {
|
||||
crate::agents::execute_commands::list_commands()
|
||||
@@ -27,6 +28,13 @@ pub fn list_acp_commands(working_dir: Option<&Path>) -> Vec<SlashCommandEntry> {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn is_builtin_command(command: &str) -> bool {
|
||||
let command = normalize_command_name(command);
|
||||
execute_commands::list_commands()
|
||||
.iter()
|
||||
.any(|builtin| builtin.name == command)
|
||||
}
|
||||
|
||||
pub(super) fn merge_command_sources(
|
||||
builtins: Vec<SlashCommandEntry>,
|
||||
recipes: Vec<SlashCommandEntry>,
|
||||
@@ -55,6 +63,9 @@ pub(super) fn merge_command_sources(
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::slash_commands::recipe_slash_command::{self, SlashCommandMapping};
|
||||
use crate::slash_commands::skill_slash_command;
|
||||
use tempfile::TempDir;
|
||||
|
||||
#[test]
|
||||
fn lists_acp_safe_builtin_commands() {
|
||||
@@ -112,6 +123,111 @@ mod tests {
|
||||
assert_eq!(compact[0].source, SlashCommandSource::Builtin);
|
||||
}
|
||||
|
||||
fn write_skill(dir: &Path, name: &str) {
|
||||
let skill_dir = dir.join(".agents").join("skills").join(name);
|
||||
std::fs::create_dir_all(&skill_dir).unwrap();
|
||||
std::fs::write(
|
||||
skill_dir.join("SKILL.md"),
|
||||
format!("---\nname: {name}\ndescription: {name} skill\n---\n{name} body"),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn recipe_mappings(dir: &Path, commands: &[&str]) -> String {
|
||||
let mappings: Vec<_> = commands
|
||||
.iter()
|
||||
.map(|command| {
|
||||
let recipe_path = dir.join(format!("{command}.yaml"));
|
||||
std::fs::write(
|
||||
&recipe_path,
|
||||
format!(
|
||||
"version: 1.0.0\ntitle: {command}\ndescription: {command} recipe\nprompt: {command} prompt\n"
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
SlashCommandMapping {
|
||||
command: command.to_string(),
|
||||
recipe_path: recipe_path.display().to_string(),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
serde_json::to_string(&mappings).unwrap()
|
||||
}
|
||||
|
||||
fn runs_as_skill(command: &str, working_dir: &Path) -> bool {
|
||||
skill_slash_command::resolve_command(command, "", Some(working_dir))
|
||||
.unwrap()
|
||||
.is_some()
|
||||
}
|
||||
|
||||
fn runs_as_recipe(command: &str) -> bool {
|
||||
recipe_slash_command::resolve_command(command, "")
|
||||
.unwrap()
|
||||
.is_some()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn builtin_names_never_run_as_recipes_or_skills() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
write_skill(tmp.path(), "compact");
|
||||
let mappings = recipe_mappings(tmp.path(), &["compact"]);
|
||||
let _guard = env_lock::lock_env([("SLASH_COMMANDS", Some(mappings.as_str()))]);
|
||||
|
||||
for command in ["compact", "Compact", "/COMPACT"] {
|
||||
assert!(is_builtin_command(command), "{command}");
|
||||
assert!(!runs_as_recipe(command), "{command}");
|
||||
assert!(!runs_as_skill(command, tmp.path()), "{command}");
|
||||
}
|
||||
assert!(!is_builtin_command("review"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recipe_wins_over_skill_with_the_same_name() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
write_skill(tmp.path(), "precedence-shared");
|
||||
let mappings = recipe_mappings(tmp.path(), &["precedence-shared"]);
|
||||
let _guard = env_lock::lock_env([("SLASH_COMMANDS", Some(mappings.as_str()))]);
|
||||
|
||||
for command in ["precedence-shared", "Precedence-Shared"] {
|
||||
assert!(runs_as_recipe(command), "{command}");
|
||||
assert!(!runs_as_skill(command, tmp.path()), "{command}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skill_runs_regardless_of_case() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
write_skill(tmp.path(), "precedence-skill");
|
||||
write_skill(tmp.path(), "Résumé-Écrire");
|
||||
let _guard = env_lock::lock_env([("SLASH_COMMANDS", Some("[]"))]);
|
||||
|
||||
for command in [
|
||||
"precedence-skill",
|
||||
"PRECEDENCE-SKILL",
|
||||
"Résumé-Écrire",
|
||||
"résumé-écrire",
|
||||
"RÉSUMÉ-ÉCRIRE",
|
||||
] {
|
||||
assert!(runs_as_skill(command, tmp.path()), "{command}");
|
||||
}
|
||||
assert!(!runs_as_skill("precedence-missing", tmp.path()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recipe_without_file_does_not_block_skill() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
write_skill(tmp.path(), "precedence-stale");
|
||||
let mappings = serde_json::to_string(&[SlashCommandMapping {
|
||||
command: "precedence-stale".to_string(),
|
||||
recipe_path: tmp.path().join("missing.yaml").display().to_string(),
|
||||
}])
|
||||
.unwrap();
|
||||
let _guard = env_lock::lock_env([("SLASH_COMMANDS", Some(mappings.as_str()))]);
|
||||
|
||||
assert!(!runs_as_recipe("precedence-stale"));
|
||||
assert!(runs_as_skill("precedence-stale", tmp.path()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn merge_dedupes_by_normalized_name() {
|
||||
let merged = merge_command_sources(
|
||||
|
||||
Reference in New Issue
Block a user