Skip to content

Commit abadb87

Browse files
authored
Fix max turns configuration (#7612)
Signed-off-by: alexyao2015 <alexyao2015@users.noreply.github.com> Co-authored-by: alexyao2015 <alexyao2015@users.noreply.github.com>
1 parent 2675a0b commit abadb87

3 files changed

Lines changed: 16 additions & 11 deletions

File tree

crates/goose/src/agents/agent.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,11 @@ impl Agent {
11181118
let reply_stream_span = tracing::info_span!(target: "goose::agents::agent", "reply_stream");
11191119
let _stream_guard = reply_stream_span.enter();
11201120
let mut turns_taken = 0u32;
1121-
let max_turns = session_config.max_turns.unwrap_or(DEFAULT_MAX_TURNS);
1121+
let max_turns = session_config.max_turns.unwrap_or_else(|| {
1122+
Config::global()
1123+
.get_param::<u32>("GOOSE_MAX_TURNS")
1124+
.unwrap_or(DEFAULT_MAX_TURNS)
1125+
});
11221126
let mut compaction_attempts = 0;
11231127
let mut last_assistant_text = String::new();
11241128

crates/goose/src/agents/platform_extensions/summon.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,9 +1455,8 @@ impl SummonClient {
14551455

14561456
let max_turns = self.resolve_max_turns(session);
14571457

1458-
let mut task_config =
1459-
TaskConfig::new(provider, &session.id, &session.working_dir, extensions);
1460-
task_config.max_turns = Some(max_turns);
1458+
let task_config = TaskConfig::new(provider, &session.id, &session.working_dir, extensions)
1459+
.with_max_turns(Some(max_turns));
14611460

14621461
Ok(task_config)
14631462
}
@@ -1512,6 +1511,7 @@ impl SummonClient {
15121511
}
15131512

15141513
fn resolve_max_turns(&self, session: &crate::session::Session) -> usize {
1514+
// Priority: env var > recipe settings > config.yaml > default
15151515
std::env::var("GOOSE_SUBAGENT_MAX_TURNS")
15161516
.ok()
15171517
.and_then(|v| v.parse().ok())
@@ -1522,6 +1522,11 @@ impl SummonClient {
15221522
.and_then(|r| r.settings.as_ref())
15231523
.and_then(|s| s.max_turns)
15241524
})
1525+
.or_else(|| {
1526+
Config::global()
1527+
.get_param::<usize>("GOOSE_SUBAGENT_MAX_TURNS")
1528+
.ok()
1529+
})
15251530
.unwrap_or(DEFAULT_SUBAGENT_MAX_TURNS)
15261531
}
15271532

crates/goose/src/agents/subagent_task_config.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
use crate::agents::ExtensionConfig;
2+
use crate::config::Config;
23
use crate::providers::base::Provider;
3-
use std::env;
44
use std::fmt;
55
use std::path::{Path, PathBuf};
66
use std::sync::Arc;
77

88
/// Default maximum number of turns for task execution
99
pub const DEFAULT_SUBAGENT_MAX_TURNS: usize = 25;
1010

11-
/// Environment variable name for configuring max turns
12-
pub const GOOSE_SUBAGENT_MAX_TURNS_ENV_VAR: &str = "GOOSE_SUBAGENT_MAX_TURNS";
13-
1411
/// Configuration for task execution with all necessary dependencies
1512
#[derive(Clone)]
1613
pub struct TaskConfig {
@@ -46,9 +43,8 @@ impl TaskConfig {
4643
parent_working_dir: parent_working_dir.to_owned(),
4744
extensions,
4845
max_turns: Some(
49-
env::var(GOOSE_SUBAGENT_MAX_TURNS_ENV_VAR)
50-
.ok()
51-
.and_then(|val| val.parse::<usize>().ok())
46+
Config::global()
47+
.get_param::<usize>("GOOSE_SUBAGENT_MAX_TURNS")
5248
.unwrap_or(DEFAULT_SUBAGENT_MAX_TURNS),
5349
),
5450
}

0 commit comments

Comments
 (0)