Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions crates/goose/src/agents/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,19 +714,41 @@ impl Agent {
Ok(())
}

pub async fn subagents_enabled(&self) -> bool {
let config = crate::config::Config::global();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now that this is in the agent context (yeah!) we should be able to add the session_type to this too and not enable subagents if the session type is subagent. that way instead of failing the creation of a subagent when a subagent is trying to create one, it will not even know about it

let is_autonomous = config.get_goose_mode().unwrap_or(GooseMode::Auto) == GooseMode::Auto;
if !is_autonomous {
return false;
}
if self
.provider()
.await
.map(|provider| provider.get_active_model_name().starts_with("gemini"))
.unwrap_or(true)

Copilot AI Nov 20, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If provider() returns an error, .unwrap_or(true) will disable subagents. This seems overly defensive - if we can't determine the model, we should probably allow subagents rather than assume it's gemini. Consider using .unwrap_or(false) instead.

Suggested change
.unwrap_or(true)
.unwrap_or(false)

Copilot uses AI. Check for mistakes.
{
return false;
}
!self
.extension_manager
.list_extensions()
.await
.map(|ext| ext.is_empty())
.unwrap_or(true)
}

pub async fn list_tools(&self, extension_name: Option<String>) -> Vec<Tool> {
let mut prefixed_tools = self
.extension_manager
.get_prefixed_tools(extension_name.clone())
.await
.unwrap_or_default();

let subagents_enabled = self.subagents_enabled().await;
if extension_name.is_none() || extension_name.as_deref() == Some("platform") {
// Add platform tools
// TODO: migrate the manage schedule tool as well
prefixed_tools.extend([platform_tools::manage_schedule_tool()]);
// Dynamic task tool
prefixed_tools.push(create_dynamic_task_tool());
prefixed_tools.push(platform_tools::manage_schedule_tool());
if subagents_enabled {
prefixed_tools.push(create_dynamic_task_tool());
}
}

if extension_name.is_none() {
Expand All @@ -736,7 +758,10 @@ impl Agent {
if let Some(final_output_tool) = self.final_output_tool.lock().await.as_ref() {
prefixed_tools.push(final_output_tool.tool());
}
prefixed_tools.push(subagent_execute_task_tool::create_subagent_execute_task_tool());
if subagents_enabled {
prefixed_tools
.push(subagent_execute_task_tool::create_subagent_execute_task_tool());
}
}

prefixed_tools
Expand Down Expand Up @@ -1444,7 +1469,7 @@ impl Agent {

let prompt_manager = self.prompt_manager.lock().await;
let system_prompt = prompt_manager
.builder(model_name)
.builder()
.with_extensions(extensions_info.into_iter())
.with_frontend_instructions(self.frontend_instructions.lock().await.clone())
.with_extension_and_tool_counts(extension_count, tool_count)
Expand Down Expand Up @@ -1664,7 +1689,7 @@ mod tests {
);

let prompt_manager = agent.prompt_manager.lock().await;
let system_prompt = prompt_manager.builder("gpt-4o").build();
let system_prompt = prompt_manager.builder().build();

let final_output_tool_ref = agent.final_output_tool.lock().await;
let final_output_tool_system_prompt =
Expand Down
30 changes: 17 additions & 13 deletions crates/goose/src/agents/prompt_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use serde_json::Value;
use std::collections::HashMap;

use crate::agents::extension::ExtensionInfo;
use crate::agents::recipe_tools::dynamic_task_tools::should_enabled_subagents;
use crate::agents::router_tools::llm_search_tool_prompt;
use crate::hints::load_hints::{load_hint_files, AGENTS_MD_FILENAME, GOOSE_HINTS_FILENAME};
use crate::{
Expand Down Expand Up @@ -47,13 +46,13 @@ struct SystemPromptContext {
}

pub struct SystemPromptBuilder<'a, M> {
model_name: String,
manager: &'a M,

extensions_info: Vec<ExtensionInfo>,
frontend_instructions: Option<String>,
extension_tool_count: Option<(usize, usize)>,
router_enabled: bool,
subagents_enabled: bool,
hints: Option<String>,
}

Expand Down Expand Up @@ -116,6 +115,11 @@ impl<'a> SystemPromptBuilder<'a, PromptManager> {
self
}

pub fn with_enable_subagents(mut self, subagents_enabled: bool) -> Self {
self.subagents_enabled = subagents_enabled;
self
}

pub fn build(self) -> String {
let mut extensions_info = self.extensions_info;

Expand Down Expand Up @@ -152,7 +156,7 @@ impl<'a> SystemPromptBuilder<'a, PromptManager> {
extension_tool_limits,
goose_mode,
is_autonomous: goose_mode == GooseMode::Auto,
enable_subagents: should_enabled_subagents(self.model_name.as_str()),
enable_subagents: self.subagents_enabled,
max_extensions: MAX_EXTENSIONS,
max_tools: MAX_TOOLS,
};
Expand Down Expand Up @@ -228,15 +232,15 @@ impl PromptManager {
self.system_prompt_override = Some(template);
}

pub fn builder<'a>(&'a self, model_name: &str) -> SystemPromptBuilder<'a, Self> {
pub fn builder<'a>(&'a self) -> SystemPromptBuilder<'a, Self> {
SystemPromptBuilder {
model_name: model_name.to_string(),
manager: self,

extensions_info: vec![],
frontend_instructions: None,
extension_tool_count: None,
router_enabled: false,
subagents_enabled: false,
hints: None,
}
}
Expand All @@ -260,7 +264,7 @@ mod tests {
let malicious_override = "System prompt\u{E0041}\u{E0042}\u{E0043}with hidden text";
manager.set_system_prompt_override(malicious_override.to_string());

let result = manager.builder("gpt-4o").build();
let result = manager.builder().build();

assert!(!result.contains('\u{E0041}'));
assert!(!result.contains('\u{E0042}'));
Expand All @@ -275,7 +279,7 @@ mod tests {
let malicious_extra = "Extra instruction\u{E0041}\u{E0042}\u{E0043}hidden";
manager.add_system_prompt_extra(malicious_extra.to_string());

let result = manager.builder("gpt-4o").build();
let result = manager.builder().build();

assert!(!result.contains('\u{E0041}'));
assert!(!result.contains('\u{E0042}'));
Expand All @@ -291,7 +295,7 @@ mod tests {
manager.add_system_prompt_extra("Second\u{E0042}instruction".to_string());
manager.add_system_prompt_extra("Third\u{E0043}instruction".to_string());

let result = manager.builder("gpt-4o").build();
let result = manager.builder().build();

assert!(!result.contains('\u{E0041}'));
assert!(!result.contains('\u{E0042}'));
Expand All @@ -307,7 +311,7 @@ mod tests {
let legitimate_unicode = "Instruction with 世界 and 🌍 emojis";
manager.add_system_prompt_extra(legitimate_unicode.to_string());

let result = manager.builder("gpt-4o").build();
let result = manager.builder().build();

assert!(result.contains("世界"));
assert!(result.contains("🌍"));
Expand All @@ -325,7 +329,7 @@ mod tests {
);

let result = manager
.builder("gpt-4o")
.builder()
.with_extension(malicious_extension_info)
.build();

Expand All @@ -340,7 +344,7 @@ mod tests {
fn test_basic() {
let manager = PromptManager::with_timestamp(DateTime::<Utc>::from_timestamp(0, 0).unwrap());

let system_prompt = manager.builder("gpt-4o").build();
let system_prompt = manager.builder().build();

assert_snapshot!(system_prompt)
}
Expand All @@ -350,7 +354,7 @@ mod tests {
let manager = PromptManager::with_timestamp(DateTime::<Utc>::from_timestamp(0, 0).unwrap());

let system_prompt = manager
.builder("gpt-4o")
.builder()
.with_extension(ExtensionInfo::new(
"test",
"how to use this extension",
Expand All @@ -367,7 +371,7 @@ mod tests {
let manager = PromptManager::with_timestamp(DateTime::<Utc>::from_timestamp(0, 0).unwrap());

let system_prompt = manager
.builder("gpt-4o")
.builder()
.with_extension(ExtensionInfo::new(
"extension_A",
"<instructions on how to use extension A>",
Expand Down
13 changes: 0 additions & 13 deletions crates/goose/src/agents/recipe_tools/dynamic_task_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::agents::subagent_execution_tool::{
task_types::{Task, TaskPayload},
};
use crate::agents::tool_execution::ToolCallResult;
use crate::config::GooseMode;
use crate::recipe::{Recipe, RecipeBuilder};
use crate::session::SessionManager;
use anyhow::{anyhow, Result};
Expand Down Expand Up @@ -90,18 +89,6 @@ pub struct TaskParameter {
pub return_last_only: Option<bool>,
}

pub fn should_enabled_subagents(model_name: &str) -> bool {
let config = crate::config::Config::global();
let is_autonomous = config.get_goose_mode().unwrap_or(GooseMode::Auto) == GooseMode::Auto;
if !is_autonomous {
return false;
}
if model_name.starts_with("gemini") {
return false;
}
true
}

pub fn create_dynamic_task_tool() -> Tool {
let schema = schema_for!(CreateDynamicTaskParams);
let schema_value =
Expand Down
14 changes: 2 additions & 12 deletions crates/goose/src/agents/reply_parts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::providers::toolshim::{
modify_system_prompt_for_tool_json, OllamaInterpreter,
};

use crate::agents::recipe_tools::dynamic_task_tools::should_enabled_subagents;
use crate::session::SessionManager;
#[cfg(test)]
use crate::session::SessionType;
Expand Down Expand Up @@ -122,15 +121,6 @@ impl Agent {
// If router is disabled and no tools were returned, fall back to regular tools
if !router_enabled && tools.is_empty() {
tools = self.list_tools(None).await;
let provider = self.provider().await?;
let model_name = provider.get_model_config().model_name;

if !should_enabled_subagents(&model_name) {
tools.retain(|tool| {
tool.name != crate::agents::subagent_execution_tool::subagent_execute_task_tool::SUBAGENT_EXECUTE_TASK_TOOL_NAME
&& tool.name != crate::agents::recipe_tools::dynamic_task_tools::DYNAMIC_TASK_TOOL_NAME_PREFIX
});
}
}

// Add frontend tools
Expand All @@ -152,16 +142,16 @@ impl Agent {
// Get model name from provider
let provider = self.provider().await?;
let model_config = provider.get_model_config();
let model_name = &model_config.model_name;

let prompt_manager = self.prompt_manager.lock().await;
let mut system_prompt = prompt_manager
.builder(model_name)
.builder()
.with_extensions(extensions_info.into_iter())
.with_frontend_instructions(self.frontend_instructions.lock().await.clone())
.with_extension_and_tool_counts(extension_count, tool_count)
.with_router_enabled(router_enabled)
.with_hints(working_dir)
.with_enable_subagents(self.subagents_enabled().await)
.build();

// Handle toolshim if enabled
Expand Down
Loading