agent-loop: changed yield_to_customer to next enum (#12789)
This commit is contained in:
@@ -26,8 +26,10 @@ is a function of the persisted conversation, not of in-memory loop state.
|
||||
boundaries. Their schemas are sent to inference and matching calls are
|
||||
dispatched against the current session.
|
||||
- **`StateMachine<'a, S, E>`** — holds `Vec<Step<..>>` and a `CancellationToken`.
|
||||
`step()` runs one pass, `apply()` writes effects back, `run()` loops until a
|
||||
step yields to the client or no step applies.
|
||||
`step()` runs one pass and returns a `StepResult` whose `applied_step` names
|
||||
the step that applied (`None` if none did) and whose `status` says whether the
|
||||
run is `Continuing`, `Yielded` to the client, or `Cancelled`; `apply()` writes
|
||||
effects back; `run()` loops while `status` is `Continuing`.
|
||||
- **`ConversationEffect`** — the default effect type: `AppendMessage`,
|
||||
`ReplaceConversation`, `PatchToolRequestMeta`, `SetMessageVisibility`. Bring
|
||||
your own by implementing `MachineEffect`.
|
||||
|
||||
@@ -9,7 +9,7 @@ use tokio_util::sync::CancellationToken;
|
||||
|
||||
use crate::operation::{
|
||||
messages_since_kickoff, ConversationEffect, Emitter, Inference, InferenceInput, MachineEffect,
|
||||
Operation, OperationResult, StepResult,
|
||||
Operation, OperationResult, RunStatus, StepResult,
|
||||
};
|
||||
use goose_provider_types::conversation::message::{Message, MessageContent};
|
||||
use goose_provider_types::conversation::Conversation;
|
||||
@@ -58,6 +58,14 @@ pub struct StateMachine<'a, S, E = ConversationEffect> {
|
||||
interrupted_step: Mutex<Option<usize>>,
|
||||
}
|
||||
|
||||
fn empty_result<E>(status: RunStatus) -> StepResult<E> {
|
||||
StepResult {
|
||||
effects: Vec::new(),
|
||||
applied_step: None,
|
||||
status,
|
||||
}
|
||||
}
|
||||
|
||||
fn interrupted_response(messages: &[Message]) -> Option<Message> {
|
||||
let answered = messages
|
||||
.iter()
|
||||
@@ -110,7 +118,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn step(&self, session: &S, emit: &Emitter) -> Result<Option<StepResult<E>>> {
|
||||
pub async fn step(&self, session: &S, emit: &Emitter) -> Result<StepResult<E>> {
|
||||
let conversation = session
|
||||
.conversation()
|
||||
.ok_or_else(|| anyhow!("state-machine session loaded without conversation"))?;
|
||||
@@ -118,7 +126,7 @@ where
|
||||
for (index, step) in self.steps.iter().enumerate() {
|
||||
let name = step.operation().name();
|
||||
if self.cancel.is_cancelled() {
|
||||
return Ok(None);
|
||||
return Ok(empty_result(RunStatus::Cancelled));
|
||||
}
|
||||
let execution = async {
|
||||
match step {
|
||||
@@ -158,44 +166,46 @@ where
|
||||
Some(Err(error)) if !self.cancel.is_cancelled() => return Err(error),
|
||||
_ => {
|
||||
*self.interrupted_step.lock().unwrap() = Some(index);
|
||||
return Ok(None);
|
||||
return Ok(empty_result(RunStatus::Cancelled));
|
||||
}
|
||||
};
|
||||
|
||||
match result {
|
||||
OperationResult::NotApplicable => {}
|
||||
OperationResult::Applied(mut result) => {
|
||||
tracing::debug!(step = name, "applied step");
|
||||
result.applied_step = Some(name);
|
||||
for effect in &mut result.effects {
|
||||
effect.ensure_message_ids();
|
||||
}
|
||||
if self.cancel.is_cancelled() {
|
||||
result.yield_to_client = true;
|
||||
result.status = RunStatus::Cancelled;
|
||||
}
|
||||
return Ok(Some(result));
|
||||
return Ok(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
Ok(empty_result(RunStatus::Yielded))
|
||||
}
|
||||
|
||||
pub async fn apply<R>(
|
||||
&self,
|
||||
runtime: &R,
|
||||
session: &S,
|
||||
result: &mut StepResult<E>,
|
||||
effects: &mut [E],
|
||||
emit: &Emitter,
|
||||
) -> Result<()>
|
||||
where
|
||||
R: EffectHandler<S, E>,
|
||||
{
|
||||
for effect in &mut result.effects {
|
||||
if effects.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
for effect in effects.iter_mut() {
|
||||
effect.ensure_message_ids();
|
||||
}
|
||||
runtime
|
||||
.apply_effects(session, &mut result.effects, emit)
|
||||
.await
|
||||
runtime.apply_effects(session, effects, emit).await
|
||||
}
|
||||
|
||||
pub async fn run<R>(&self, runtime: &R, session_id: &str, emit: &Emitter) -> Result<S>
|
||||
@@ -204,12 +214,12 @@ where
|
||||
{
|
||||
loop {
|
||||
let session = runtime.load(session_id).await?;
|
||||
let Some(mut result) = self.step(&session, emit).await? else {
|
||||
break;
|
||||
};
|
||||
self.apply(runtime, &session, &mut result, emit).await?;
|
||||
if result.yield_to_client {
|
||||
break;
|
||||
let mut result = self.step(&session, emit).await?;
|
||||
self.apply(runtime, &session, &mut result.effects, emit)
|
||||
.await?;
|
||||
match result.status {
|
||||
RunStatus::Continuing => {}
|
||||
RunStatus::Yielded | RunStatus::Cancelled => break,
|
||||
}
|
||||
}
|
||||
self.finalize(runtime, session_id, emit).await
|
||||
@@ -278,7 +288,7 @@ where
|
||||
runtime: &R,
|
||||
session_id: &str,
|
||||
session: S,
|
||||
effects: Vec<E>,
|
||||
mut effects: Vec<E>,
|
||||
emit: &Emitter,
|
||||
) -> Result<S>
|
||||
where
|
||||
@@ -287,12 +297,7 @@ where
|
||||
if effects.is_empty() {
|
||||
return Ok(session);
|
||||
}
|
||||
let mut result = StepResult {
|
||||
effects,
|
||||
applied_step: None,
|
||||
yield_to_client: true,
|
||||
};
|
||||
self.apply(runtime, &session, &mut result, emit).await?;
|
||||
self.apply(runtime, &session, &mut effects, emit).await?;
|
||||
runtime.load(session_id).await
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,10 +164,17 @@ pub trait Inference<S, E: MaybeSend + 'static = ConversationEffect>: Operation<S
|
||||
) -> Result<OperationResult<E>>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum RunStatus {
|
||||
Continuing,
|
||||
Yielded,
|
||||
Cancelled,
|
||||
}
|
||||
|
||||
pub struct StepResult<E = ConversationEffect> {
|
||||
pub effects: Vec<E>,
|
||||
pub applied_step: Option<&'static str>,
|
||||
pub yield_to_client: bool,
|
||||
pub status: RunStatus,
|
||||
}
|
||||
|
||||
pub enum OperationResult<E = ConversationEffect> {
|
||||
@@ -183,7 +190,7 @@ pub fn applied<E>(effects: impl IntoIterator<Item = E>) -> Result<OperationResul
|
||||
Ok(OperationResult::Applied(StepResult {
|
||||
effects: effects.into_iter().collect(),
|
||||
applied_step: None,
|
||||
yield_to_client: false,
|
||||
status: RunStatus::Continuing,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -191,7 +198,7 @@ pub fn yielded<E>() -> Result<OperationResult<E>> {
|
||||
Ok(OperationResult::Applied(StepResult {
|
||||
effects: Vec::new(),
|
||||
applied_step: None,
|
||||
yield_to_client: true,
|
||||
status: RunStatus::Yielded,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -199,7 +206,7 @@ pub fn yielded_with<E>(effects: impl IntoIterator<Item = E>) -> Result<Operation
|
||||
Ok(OperationResult::Applied(StepResult {
|
||||
effects: effects.into_iter().collect(),
|
||||
applied_step: None,
|
||||
yield_to_client: true,
|
||||
status: RunStatus::Yielded,
|
||||
}))
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ use goose_agent::{
|
||||
machine::{MachineSession, StateMachine, Step},
|
||||
operation::{
|
||||
ConversationEffect, Emitter, Inference, InferenceInput, Operation, OperationResult,
|
||||
RunStatus,
|
||||
},
|
||||
tool::{ToolOperation, ToolProvider},
|
||||
};
|
||||
@@ -470,7 +471,7 @@ async fn cancellation_interrupts_inference_discovery() {
|
||||
}
|
||||
cancel.cancel();
|
||||
|
||||
assert!(step.await.unwrap().unwrap().is_none());
|
||||
assert_eq!(step.await.unwrap().unwrap().status, RunStatus::Cancelled);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
@@ -63,7 +63,7 @@ pub use goose_agent::machine::{
|
||||
pub use goose_agent::operation::{
|
||||
applied, assistant_turn_count, ends_turn, last_effective_role, messages_since_kickoff,
|
||||
not_applicable, trailing_error, yielded, yielded_with, ConversationEffect, Emitter, Inference,
|
||||
InferenceInput, MachineEffect, Operation, OperationResult, SlashCommand, StepResult,
|
||||
InferenceInput, MachineEffect, Operation, OperationResult, RunStatus, SlashCommand, StepResult,
|
||||
};
|
||||
pub(crate) use tool_confirmation::{
|
||||
has_unapplied_tool_confirmation_response, pending_tool_confirmations,
|
||||
|
||||
@@ -298,7 +298,7 @@ mod tests {
|
||||
|
||||
use super::*;
|
||||
use crate::agents::final_output_tool::{FINAL_OUTPUT_SUCCESS_MESSAGE, FINAL_OUTPUT_TOOL_NAME};
|
||||
use crate::agents::state_machine::ConversationEffect;
|
||||
use crate::agents::state_machine::{ConversationEffect, RunStatus};
|
||||
use crate::config::GooseMode;
|
||||
use crate::session::SessionManager;
|
||||
use goose_agent::machine::EffectHandler;
|
||||
@@ -417,7 +417,7 @@ mod tests {
|
||||
let OperationResult::Applied(step) = result else {
|
||||
panic!("expected the operation to apply");
|
||||
};
|
||||
assert!(!step.yield_to_client);
|
||||
assert_eq!(step.status, RunStatus::Continuing);
|
||||
step.effects
|
||||
}
|
||||
|
||||
|
||||
@@ -334,19 +334,7 @@ pub(crate) async fn run(
|
||||
session_manager: runtime,
|
||||
usage: Default::default(),
|
||||
};
|
||||
loop {
|
||||
let session = runtime.load(session_id).await?;
|
||||
let Some(mut result) = machine.step(&session, emit).await? else {
|
||||
break;
|
||||
};
|
||||
tracing::debug!(target: "goose::state_machine", step = result.applied_step, "applied step");
|
||||
machine.apply(&runtime, &session, &mut result, emit).await?;
|
||||
if result.yield_to_client {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let session = machine.finalize(&runtime, session_id, emit).await?;
|
||||
let session = machine.run(&runtime, session_id, emit).await?;
|
||||
let turn_usage = runtime.usage.into_inner().unwrap();
|
||||
let last_assistant_text = session
|
||||
.conversation()
|
||||
|
||||
@@ -86,18 +86,13 @@ async fn proactive_and_manual_compaction_continue_with_replaced_usage() -> Resul
|
||||
state_machine::StateMachine::new(Vec::new(), tokio_util::sync::CancellationToken::new());
|
||||
let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
|
||||
let emit = state_machine::Emitter::new(tx, tokio_util::sync::CancellationToken::new());
|
||||
let apply = async |effects: Vec<state_machine::GooseEffect>| -> Result<()> {
|
||||
let apply = async |mut effects: Vec<state_machine::GooseEffect>| -> Result<()> {
|
||||
let session = pipeline.session().await?;
|
||||
let mut result = state_machine::StepResult {
|
||||
effects,
|
||||
applied_step: None,
|
||||
yield_to_client: false,
|
||||
};
|
||||
machine
|
||||
.apply(
|
||||
pipeline.session_manager.as_ref(),
|
||||
&session,
|
||||
&mut result,
|
||||
&mut effects,
|
||||
&emit,
|
||||
)
|
||||
.await
|
||||
|
||||
@@ -18,9 +18,10 @@ use crate::agents::state_machine::{
|
||||
BangShellOperation, CompactionOperation, DoctorOperation, Emitter, ExitOnErrorOperation,
|
||||
ForegroundSubagentOperation, GooseEffect, GooseInferenceProvider,
|
||||
GooseInferenceRequestPreparer, InferenceRunner, MaxTurnsOperation, Operation, ProjectOperation,
|
||||
RecipeOperation, RetryOperation, SkillOperation, SlashCommandOperation, StateMachine,
|
||||
StatusOperation, SteerOperation, SteerQueue, Step, StopHookOperation, ToolApprovalOperation,
|
||||
ToolExecutionOperation, ToolPairCompactionOperation, UnknownToolOperation,
|
||||
RecipeOperation, RetryOperation, RunStatus, SkillOperation, SlashCommandOperation,
|
||||
StateMachine, StatusOperation, SteerOperation, SteerQueue, Step, StopHookOperation,
|
||||
ToolApprovalOperation, ToolExecutionOperation, ToolPairCompactionOperation,
|
||||
UnknownToolOperation,
|
||||
};
|
||||
use crate::agents::subagent_handler::ForegroundSubagentRunner;
|
||||
use crate::agents::AgentEvent;
|
||||
@@ -440,21 +441,25 @@ impl TestPipeline {
|
||||
loop {
|
||||
let session = self.session().await?;
|
||||
let machine = self.machine(cancel.clone());
|
||||
let Some(mut result) = machine.step(&session, &emit).await? else {
|
||||
break;
|
||||
};
|
||||
let mut result = machine.step(&session, &emit).await?;
|
||||
machine
|
||||
.apply(self.session_manager.as_ref(), &session, &mut result, &emit)
|
||||
.apply(
|
||||
self.session_manager.as_ref(),
|
||||
&session,
|
||||
&mut result.effects,
|
||||
&emit,
|
||||
)
|
||||
.await?;
|
||||
applied_steps += 1;
|
||||
if result.applied_step.is_some() {
|
||||
applied_steps += 1;
|
||||
}
|
||||
while let Ok(event) = rx.try_recv() {
|
||||
events.push(event);
|
||||
}
|
||||
|
||||
let yield_to_client = result.yield_to_client;
|
||||
drop(machine);
|
||||
self = self.reconstruct().await?;
|
||||
if yield_to_client {
|
||||
if result.status != RunStatus::Continuing {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -648,16 +653,18 @@ impl TestPipeline {
|
||||
}
|
||||
}
|
||||
};
|
||||
let Some(ref mut result) = result else {
|
||||
break;
|
||||
};
|
||||
machine
|
||||
.apply(self.session_manager.as_ref(), &session, result, &emit)
|
||||
.apply(
|
||||
self.session_manager.as_ref(),
|
||||
&session,
|
||||
&mut result.effects,
|
||||
&emit,
|
||||
)
|
||||
.await?;
|
||||
while let Ok(event) = rx.try_recv() {
|
||||
events.push(event);
|
||||
}
|
||||
if result.yield_to_client {
|
||||
if result.status != RunStatus::Continuing {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1128,21 +1135,19 @@ pub(super) async fn run_machine(pipeline: &TestPipeline) -> Result<Vec<AgentEven
|
||||
pipeline.start_turn().await?;
|
||||
loop {
|
||||
let session = pipeline.session().await?;
|
||||
let Some(mut result) = machine.step(&session, &emit).await? else {
|
||||
break;
|
||||
};
|
||||
let mut result = machine.step(&session, &emit).await?;
|
||||
machine
|
||||
.apply(
|
||||
pipeline.session_manager.as_ref(),
|
||||
&session,
|
||||
&mut result,
|
||||
&mut result.effects,
|
||||
&emit,
|
||||
)
|
||||
.await?;
|
||||
while let Ok(event) = rx.try_recv() {
|
||||
events.push(event);
|
||||
}
|
||||
if result.yield_to_client {
|
||||
if result.status != RunStatus::Continuing {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use goose::agents::state_machine::{
|
||||
yielded_with, Emitter, GooseEffect, Inference, InferenceInput, Operation, OperationResult,
|
||||
StateMachine, Step,
|
||||
RunStatus, StateMachine, Step,
|
||||
};
|
||||
use goose::agents::AgentEvent;
|
||||
use goose::config::GooseMode;
|
||||
@@ -116,7 +116,7 @@ async fn custom_pipeline_supports_step_apply_run_and_usage() -> Result<()> {
|
||||
);
|
||||
|
||||
let session = session_manager.get_session(&session.id, true).await?;
|
||||
let mut result = machine.step(&session, &emit).await?.unwrap();
|
||||
let mut result = machine.step(&session, &emit).await?;
|
||||
assert!(matches!(
|
||||
result.effects.first(),
|
||||
Some(GooseEffect::Conversation(
|
||||
@@ -124,9 +124,9 @@ async fn custom_pipeline_supports_step_apply_run_and_usage() -> Result<()> {
|
||||
)) if message.id.is_some()
|
||||
));
|
||||
machine
|
||||
.apply(&session_manager, &session, &mut result, &emit)
|
||||
.apply(&session_manager, &session, &mut result.effects, &emit)
|
||||
.await?;
|
||||
assert!(result.yield_to_client);
|
||||
assert_eq!(result.status, RunStatus::Yielded);
|
||||
let session = session_manager.get_session(&session.id, true).await?;
|
||||
let persisted = session
|
||||
.conversation
|
||||
|
||||
Reference in New Issue
Block a user