Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
38 changes: 31 additions & 7 deletions crates/goose/src/agents/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,26 +640,50 @@ impl Agent {
Ok(())
}

pub async fn subagents_enabled(&self) -> bool {
let config = crate::config::Config::global();
Copy link
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(false)
{
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") {
prefixed_tools.extend([platform_tools::manage_schedule_tool()]);
prefixed_tools.push(platform_tools::manage_schedule_tool());
}

if extension_name.is_none() {
if let Some(final_output_tool) = self.final_output_tool.lock().await.as_ref() {
prefixed_tools.push(final_output_tool.tool());
}

// Add the unified subagent tool
let sub_recipes = self.sub_recipes.lock().await;
let sub_recipes_vec: Vec<_> = sub_recipes.values().cloned().collect();
prefixed_tools.push(create_subagent_tool(&sub_recipes_vec));
if subagents_enabled {
let sub_recipes = self.sub_recipes.lock().await;
let sub_recipes_vec: Vec<_> = sub_recipes.values().cloned().collect();
prefixed_tools.push(create_subagent_tool(&sub_recipes_vec));
}
}

prefixed_tools
Expand Down Expand Up @@ -1374,7 +1398,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 @@ -1594,7 +1618,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 @@ -7,7 +7,6 @@ use std::collections::HashMap;

use crate::agents::extension::ExtensionInfo;
use crate::agents::router_tools::llm_search_tool_prompt;
use crate::agents::subagent_tool::should_enable_subagents;
use crate::hints::load_hints::{load_hint_files, AGENTS_MD_FILENAME, GOOSE_HINTS_FILENAME};
use crate::{
config::{Config, GooseMode},
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_enable_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
11 changes: 2 additions & 9 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::subagent_tool::should_enable_subagents;
use crate::session::SessionManager;
#[cfg(test)]
use crate::session::SessionType;
Expand Down Expand Up @@ -122,12 +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_enable_subagents(&model_name) {
tools.retain(|tool| tool.name != crate::agents::subagent_tool::SUBAGENT_TOOL_NAME);
}
}

// Add frontend tools
Expand All @@ -149,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
13 changes: 0 additions & 13 deletions crates/goose/src/agents/subagent_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use tokio_util::sync::CancellationToken;
use crate::agents::subagent_handler::run_complete_subagent_task;
use crate::agents::subagent_task_config::TaskConfig;
use crate::agents::tool_execution::ToolCallResult;
use crate::config::GooseMode;
use crate::providers;
use crate::recipe::build_recipe::build_recipe_from_template;
use crate::recipe::local_recipes::load_local_recipe_file;
Expand Down Expand Up @@ -440,18 +439,6 @@ async fn apply_settings_overrides(
Ok(task_config)
}

pub fn should_enable_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
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
8 changes: 4 additions & 4 deletions scripts/test_compaction.sh
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,16 @@ echo ""
# TEST 2: Auto Compaction
# ==================================================
echo "---------------------------------------------------"
echo "TEST 2: Auto Compaction via threshold (0.01)"
echo "TEST 2: Auto Compaction via threshold (0.005)"
echo "---------------------------------------------------"

TESTDIR=$(mktemp -d)
echo "test content" > "$TESTDIR/test.txt"
echo "Test directory: $TESTDIR"
echo ""

# Set auto-compact threshold very low (1%) to trigger it quickly
export GOOSE_AUTO_COMPACT_THRESHOLD=0.01
# Set auto-compact threshold very low (.5%) to trigger it quickly
export GOOSE_AUTO_COMPACT_THRESHOLD=0.005

OUTPUT=$(mktemp)

Expand Down Expand Up @@ -197,7 +197,7 @@ else
fi
else
echo "✗ FAILED: Auto compaction was not triggered"
echo " Expected to see auto-compact messages with threshold of 0.01"
echo " Expected to see auto-compact messages with threshold of 0.005"
RESULTS+=("✗ Auto Compaction")
fi
fi
Expand Down
Loading