From abadb87892889d62f48aca8d780d4d1728a9d281 Mon Sep 17 00:00:00 2001 From: Alex Yao <33379584+alexyao2015@users.noreply.github.com> Date: Thu, 5 Mar 2026 15:42:12 -0500 Subject: [PATCH] Fix max turns configuration (#7612) Signed-off-by: alexyao2015 Co-authored-by: alexyao2015 --- crates/goose/src/agents/agent.rs | 6 +++++- crates/goose/src/agents/platform_extensions/summon.rs | 11 ++++++++--- crates/goose/src/agents/subagent_task_config.rs | 10 +++------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/crates/goose/src/agents/agent.rs b/crates/goose/src/agents/agent.rs index ff398f95..4a498e4d 100644 --- a/crates/goose/src/agents/agent.rs +++ b/crates/goose/src/agents/agent.rs @@ -1118,7 +1118,11 @@ impl Agent { let reply_stream_span = tracing::info_span!(target: "goose::agents::agent", "reply_stream"); let _stream_guard = reply_stream_span.enter(); let mut turns_taken = 0u32; - let max_turns = session_config.max_turns.unwrap_or(DEFAULT_MAX_TURNS); + let max_turns = session_config.max_turns.unwrap_or_else(|| { + Config::global() + .get_param::("GOOSE_MAX_TURNS") + .unwrap_or(DEFAULT_MAX_TURNS) + }); let mut compaction_attempts = 0; let mut last_assistant_text = String::new(); diff --git a/crates/goose/src/agents/platform_extensions/summon.rs b/crates/goose/src/agents/platform_extensions/summon.rs index cfb5fd6d..52a9482b 100644 --- a/crates/goose/src/agents/platform_extensions/summon.rs +++ b/crates/goose/src/agents/platform_extensions/summon.rs @@ -1455,9 +1455,8 @@ impl SummonClient { let max_turns = self.resolve_max_turns(session); - let mut task_config = - TaskConfig::new(provider, &session.id, &session.working_dir, extensions); - task_config.max_turns = Some(max_turns); + let task_config = TaskConfig::new(provider, &session.id, &session.working_dir, extensions) + .with_max_turns(Some(max_turns)); Ok(task_config) } @@ -1512,6 +1511,7 @@ impl SummonClient { } fn resolve_max_turns(&self, session: &crate::session::Session) -> usize { + // Priority: env var > recipe settings > config.yaml > default std::env::var("GOOSE_SUBAGENT_MAX_TURNS") .ok() .and_then(|v| v.parse().ok()) @@ -1522,6 +1522,11 @@ impl SummonClient { .and_then(|r| r.settings.as_ref()) .and_then(|s| s.max_turns) }) + .or_else(|| { + Config::global() + .get_param::("GOOSE_SUBAGENT_MAX_TURNS") + .ok() + }) .unwrap_or(DEFAULT_SUBAGENT_MAX_TURNS) } diff --git a/crates/goose/src/agents/subagent_task_config.rs b/crates/goose/src/agents/subagent_task_config.rs index f25c0ef1..1cca9c08 100644 --- a/crates/goose/src/agents/subagent_task_config.rs +++ b/crates/goose/src/agents/subagent_task_config.rs @@ -1,6 +1,6 @@ use crate::agents::ExtensionConfig; +use crate::config::Config; use crate::providers::base::Provider; -use std::env; use std::fmt; use std::path::{Path, PathBuf}; use std::sync::Arc; @@ -8,9 +8,6 @@ use std::sync::Arc; /// Default maximum number of turns for task execution pub const DEFAULT_SUBAGENT_MAX_TURNS: usize = 25; -/// Environment variable name for configuring max turns -pub const GOOSE_SUBAGENT_MAX_TURNS_ENV_VAR: &str = "GOOSE_SUBAGENT_MAX_TURNS"; - /// Configuration for task execution with all necessary dependencies #[derive(Clone)] pub struct TaskConfig { @@ -46,9 +43,8 @@ impl TaskConfig { parent_working_dir: parent_working_dir.to_owned(), extensions, max_turns: Some( - env::var(GOOSE_SUBAGENT_MAX_TURNS_ENV_VAR) - .ok() - .and_then(|val| val.parse::().ok()) + Config::global() + .get_param::("GOOSE_SUBAGENT_MAX_TURNS") .unwrap_or(DEFAULT_SUBAGENT_MAX_TURNS), ), }