fix (agent loop): Finish pending tools before enforcing max turns (#12702)

This commit is contained in:
Lifei Zhou
2026-10-07 02:24:12 +00:00
committed by GitHub
parent 8f72d2d305
commit 968dc4bdb2
8 changed files with 113 additions and 14 deletions
+2 -2
View File
@@ -1740,7 +1740,6 @@ impl Agent {
let mut operations: Vec<Arc<dyn Operation<Session, GooseEffect> + '_>> = vec![
Arc::new(SteerOperation::new(steer_queue, self.hook_manager.clone())),
Arc::new(MaxTurnsOperation::new(max_turns)),
Arc::new(BangShellOperation::new()),
];
if !manages_own_context {
@@ -1801,6 +1800,7 @@ impl Agent {
stop_hook_block_cap,
)),
Arc::new(ExitOnErrorOperation),
Arc::new(MaxTurnsOperation::new(max_turns)),
];
operations.extend(remaining_operations);
let request_preparer = GooseInferenceRequestPreparer {
@@ -3474,7 +3474,7 @@ impl Agent {
warn!("Final output tool has not been called yet. Continuing agent loop.");
let message = push_message_with_id(
&mut messages_to_add,
Message::user().with_text(FINAL_OUTPUT_CONTINUATION_MESSAGE),
Message::user().with_text(FINAL_OUTPUT_CONTINUATION_MESSAGE).agent_only(),
);
yield AgentEvent::Message(message);
}
@@ -279,7 +279,9 @@ impl Operation<Session, GooseEffect> for RecipeOperation {
}
if ends_turn(messages) {
let message = Message::user().with_text(FINAL_OUTPUT_CONTINUATION_MESSAGE);
let message = Message::user()
.with_text(FINAL_OUTPUT_CONTINUATION_MESSAGE)
.agent_only();
let message = emit.message(message).await;
return applied([message.into()]);
}
@@ -18,6 +18,7 @@ use super::calculator_extension::{delayed_value, value, CalculatorExtension, ADD
use super::dummy_api::{DummyApi, ProviderFeatures};
use crate::acp::server::GooseAcpAgent;
use crate::agents::extension::ExtensionConfig;
use crate::agents::final_output_tool::{FINAL_OUTPUT_CONTINUATION_MESSAGE, FINAL_OUTPUT_TOOL_NAME};
use crate::agents::mcp_client::McpClientTrait;
use crate::agents::state_machine::ops_toolcalling::EXPIRED_APPROVAL_RESPONSE;
use crate::agents::{Agent, AgentConfig, AgentEvent, GoosePlatform, SessionConfig};
@@ -160,6 +161,102 @@ async fn stream_messages(
Ok(messages)
}
#[tokio::test]
async fn both_loops_execute_every_tool_from_the_last_allowed_reply() -> Result<()> {
for use_state_machine in [false, true] {
let (agent, api, session_id, calculator, _temp_dir) = agent_with_calculator().await?;
agent
.update_goose_mode(GooseMode::Auto, &session_id)
.await?;
api.on("add twice")
.calls([("first_add", ADD, value(1)), ("second_add", ADD, value(2))]);
let messages = stream_messages(
agent
.reply(
Message::user().with_text("add twice"),
SessionConfig {
id: session_id,
schedule_id: None,
max_turns: Some(1),
retry_config: None,
},
use_state_machine,
None,
)
.await?,
)
.await?;
assert_eq!(api.call_count(), 1);
assert_eq!(calculator.total(), 3);
assert_eq!(
messages.last().unwrap().as_concat_text(),
crate::agents::state_machine::MAX_TURNS_MESSAGE
);
}
Ok(())
}
#[tokio::test]
async fn both_loops_keep_recipe_continuations_within_the_turn_budget() -> Result<()> {
for use_state_machine in [false, true] {
let (agent, api, session_id, _temp_dir) = agent_with_dummy_api().await?;
let recipe = crate::recipe::Recipe::builder()
.title("Structured output")
.description("Return structured output")
.instructions("Use the final output tool")
.response(crate::recipe::Response {
json_schema: Some(json!({ "type": "object" })),
})
.build()
.expect("valid recipe");
agent
.apply_recipe_components(recipe.response.clone(), true)
.await?;
agent
.config
.session_manager
.update(&session_id)
.recipe(Some(recipe))
.apply()
.await?;
api.on("compute the answer").reply("thinking about it");
api.on(FINAL_OUTPUT_CONTINUATION_MESSAGE)
.call(FINAL_OUTPUT_TOOL_NAME, json!({ "result": "42" }));
let messages = stream_messages(
agent
.reply(
Message::user().with_text("compute the answer"),
SessionConfig {
id: session_id,
schedule_id: None,
max_turns: Some(1),
retry_config: None,
},
use_state_machine,
None,
)
.await?,
)
.await?;
assert_eq!(api.call_count(), 1);
assert_eq!(
messages.last().unwrap().as_concat_text(),
crate::agents::state_machine::MAX_TURNS_MESSAGE
);
let continuation = messages
.iter()
.find(|message| message.as_concat_text() == FINAL_OUTPUT_CONTINUATION_MESSAGE)
.expect("recipe continuation");
assert!(!continuation.is_user_visible());
assert!(continuation.is_agent_visible());
}
Ok(())
}
#[tokio::test]
async fn state_machine_confirmation_through_agent_resumes_tool_call() -> Result<()> {
let _guard = env_lock::lock_env([("GOOSE_STATE_MACHINE", Some("1"))]);
@@ -667,7 +764,7 @@ async fn reply_streams_the_turn_and_ends() -> Result<()> {
let session_config = SessionConfig {
id: session_id.clone(),
schedule_id: None,
max_turns: Some(2),
max_turns: Some(1),
retry_config: None,
};
let stream = agent
@@ -690,10 +787,7 @@ async fn reply_streams_the_turn_and_ends() -> Result<()> {
})
.await??;
assert!(
replies.iter().any(|reply| reply == "still here"),
"expected the scripted reply, got {replies:?}"
);
assert_eq!(replies.last().map(String::as_str), Some("still here"));
assert_eq!(api.call_count(), 1);
Ok(())
@@ -151,7 +151,7 @@ async fn stop_hooks_allow_block_and_skip_non_stop_exits() -> Result<()> {
pipeline.run(["keep going"]).await?;
assert_eq!(api.call_count(), MAX_TURNS as usize);
assert_eq!(pipeline.calculator_total(), MAX_TURNS as i64 - 1);
assert_eq!(pipeline.calculator_total(), MAX_TURNS as i64);
assert_eq!(maxed.invocations(), 0);
Ok(())
@@ -143,7 +143,7 @@ async fn max_turns_counts_inference_calls_and_injects_budget() -> Result<()> {
let result = pipeline.run(["keep going"]).await?;
let calls = api.calls();
assert_eq!(calls.len(), MAX_TURNS as usize);
assert_eq!(pipeline.calculator_total(), MAX_TURNS as i64 - 1);
assert_eq!(pipeline.calculator_total(), MAX_TURNS as i64);
let first_budgeted_call = MAX_TURNS.div_ceil(2) as usize;
assert!(!calls[first_budgeted_call - 1].input_contains("<turn-budget>"));
@@ -126,7 +126,6 @@ impl TestPipeline {
self.steer_queue.clone(),
self.hook_manager.clone(),
)),
Arc::new(MaxTurnsOperation::new(self.max_turns)),
Arc::new(BangShellOperation::new()),
];
if !self.provider_features.manages_own_context {
@@ -182,6 +181,7 @@ impl TestPipeline {
self.stop_hook_block_cap,
)),
Arc::new(ExitOnErrorOperation),
Arc::new(MaxTurnsOperation::new(self.max_turns)),
];
operations.extend(remaining_operations);
let request_preparer = GooseInferenceRequestPreparer {
@@ -366,6 +366,7 @@ impl TestPipeline {
)
.await?
.with_hook_manager(self.hook_manager.clone())
.with_max_turns(self.max_turns)
.with_stop_hook_block_cap(self.stop_hook_block_cap);
pipeline.extension_lease = Arc::clone(&self.extension_lease);
*pipeline.goal.lock().await = goal;
@@ -186,6 +186,7 @@ settings:
#[tokio::test]
async fn final_output_beside_delegate_is_not_shown_before_the_subagent_runs() -> Result<()> {
let (pipeline, api) = test_pipeline().await?;
let pipeline = pipeline.with_max_turns(2);
let host = api.uri();
let _guard = env_lock::lock_env([
("OPENAI_API_KEY", Some("fake-openai-no-keyring")),
@@ -218,7 +219,7 @@ async fn final_output_beside_delegate_is_not_shown_before_the_subagent_runs() ->
(
"call_delegate",
"delegate",
json!({ "instructions": "Find the answer" }),
json!({ "instructions": "Find the answer", "max_turns": 1 }),
),
(
"call_early_output",
@@ -276,6 +277,7 @@ async fn recipe_retry_and_final_output_run_to_completion() -> Result<()> {
exhausted.assert_message(-1, Error, "Maximum retry attempts (1) exceeded");
let (pipeline, api) = test_pipeline().await?;
let pipeline = pipeline.with_max_turns(2);
api.on("compute the answer").reply("thinking about it");
api.on(FINAL_OUTPUT_CONTINUATION_MESSAGE)
.call(FINAL_OUTPUT_TOOL_NAME, json!({ "result": "42" }));
@@ -167,7 +167,7 @@ async fn reconstruction_and_session_isolation() -> Result<()> {
let calls_before = api.call_count();
let bounded = pipeline.run(["keep adding"]).await?;
assert_eq!(api.call_count() - calls_before, 2);
assert_eq!(pipeline.calculator_total(), 1);
assert_eq!(pipeline.calculator_total(), 2);
bounded.assert_message(-1, Agent, crate::agents::state_machine::MAX_TURNS_MESSAGE);
let restored = pipeline.session().await?;
@@ -206,7 +206,7 @@ async fn reconstruction_and_session_isolation() -> Result<()> {
.await?;
elicited.assert_message(-2, ToolResponse, "result: 9");
assert_eq!(other.calculator_total(), 9);
assert_eq!(pipeline.calculator_total(), 1);
assert_eq!(pipeline.calculator_total(), 2);
Ok(())
}