refactor(buzz-agent): use goose-agent loop APIs directly

Depend on goose-agent at the same pinned Goose revision and import its state
machine, operation traits, effects, and turn-counting helpers directly. This
removes Buzz's copied messages_since_kickoff and assistant_turn_count helpers
without changing loop policy or the broader Goose dependency graph.

Co-authored-by: Michael Neale <michael.neale@gmail.com>
Signed-off-by: Jimmy <1fe240cd1a8cf775f6f3060f115e5a303181f3abf28ad4cb0c2515f4a02b36a8@meshllm.communities.buzz.xyz>
This commit is contained in:
Jimmy
2026-08-27 10:19:11 +10:00
parent 0a4c78e1ef
commit 55326e36d6
4 changed files with 19 additions and 51 deletions

1
Cargo.lock generated
View File

@@ -1203,6 +1203,7 @@ dependencies = [
"futures",
"getrandom 0.4.3",
"goose",
"goose-agent",
"goose-provider-types",
"goose-providers",
"hex",

View File

@@ -30,6 +30,9 @@ path = "tests/bin/fake_mcp.rs"
# the token cache that goose (and the desktop model picker) then read.
buzz-model-catalog = { path = "../buzz-model-catalog" }
goose = { git = "https://github.com/aaif-goose/goose", rev = "92caef37f8c80e0ee31ab9acad117662bc169f78", default-features = false, features = ["rustls-tls"] }
# Consume the GDK loop primitives from their owning crate. Keep this on the
# exact same Goose revision as `goose` so shared conversation types stay unified.
goose-agent = { git = "https://github.com/aaif-goose/goose", rev = "92caef37f8c80e0ee31ab9acad117662bc169f78" }
goose-provider-types = { git = "https://github.com/aaif-goose/goose", rev = "92caef37f8c80e0ee31ab9acad117662bc169f78" }
# ModelConfig: goose re-exports it from goose-providers; goose::model_config
# keeps its own copy private.

View File

@@ -52,7 +52,8 @@ use goose_provider_types::conversation::message::{Message, MessageContent, ToolR
use goose_provider_types::conversation::Conversation;
use crate::types::{AgentError, StopReason};
use goose::agents::state_machine::{ConversationEffect, Emitter};
use goose_agent::machine::StateMachine;
use goose_agent::operation::{ConversationEffect, Emitter};
/// buzz's state machine, spelled once.
///
@@ -62,7 +63,7 @@ use goose::agents::state_machine::{ConversationEffect, Emitter};
/// goose's own `GooseEffect`: the extra variants there (recipes, extension
/// data, recorded usage) exist for operations buzz does not run, so naming the
/// narrower type keeps unreachable cases out of `apply_effects` entirely.
type BuzzMachine<'a> = goose::agents::state_machine::StateMachine<'a, Session, ConversationEffect>;
type BuzzMachine<'a> = StateMachine<'a, Session, ConversationEffect>;
use crate::wire::WireSender;

View File

@@ -22,51 +22,18 @@ use std::sync::{Arc, Mutex};
use anyhow::Result;
use async_trait::async_trait;
use goose::agents::state_machine::{
applied, not_applicable, yielded, ConversationEffect, Emitter, Operation, OperationResult,
};
use goose::agents::Agent;
use goose::conversation::Conversation;
use goose::session::Session;
use goose_agent::machine::{StateMachine, Step};
use goose_agent::operation::{
applied, assistant_turn_count, messages_since_kickoff, not_applicable, yielded,
ConversationEffect, Emitter, Operation, OperationResult,
};
use goose_provider_types::conversation::message::{Message, ToolRequest};
use crate::types::StopReason;
/// Messages belonging to the current turn: everything from the last
/// user-visible, non-tool-response message onward.
///
/// goose has this as `messages_since_kickoff`, but its `operation` module is
/// private (`mod operation;`) so only the names re-exported from
/// `state_machine` escape the crate — the helpers do not. Reimplemented here
/// to match goose's semantics exactly; see `operation.rs:23`.
fn messages_since_kickoff(conversation: &Conversation) -> Option<&[Message]> {
let messages = conversation.messages();
let start = messages.iter().rposition(|message| {
message.role == rmcp::model::Role::User
&& message.is_user_visible()
&& !message.is_tool_response()
})?;
Some(&messages[start..])
}
/// Count assistant turns, treating a run of consecutive assistant messages as
/// one turn. Mirrors goose's `assistant_turn_count` (`operation.rs:47`).
fn assistant_turn_count(messages: &[Message]) -> u32 {
let mut turns = 0;
let mut in_assistant_block = false;
for message in messages.iter().rev() {
if message.role == rmcp::model::Role::Assistant {
if !in_assistant_block {
turns += 1;
in_assistant_block = true;
}
} else {
in_assistant_block = false;
}
}
turns
}
/// Why the turn stopped, as decided by whichever operation applied.
///
/// Shared with the loop because `StepResult` says *that* a step yielded, not
@@ -145,7 +112,7 @@ impl Operation<Session, ConversationEffect> for BuzzMaxRoundsOperation {
// does not leak across prompts in a long-lived session. Counting
// assistant turns rather than loop iterations also means the bound
// survives a restart, which a loop-local counter did not.
let Some(messages) = messages_since_kickoff(conversation) else {
let Ok(messages) = messages_since_kickoff(conversation) else {
// No kickoff message means no turn is in progress, so there is no
// budget to exceed. goose errors here; buzz declines instead --
// an operation that cannot decide must not end the turn.
@@ -247,7 +214,7 @@ impl Operation<Session, ConversationEffect> for BuzzStopVetoOperation {
conversation: &Conversation,
_emit: &Emitter,
) -> Result<OperationResult<ConversationEffect>> {
let Some(messages) = messages_since_kickoff(conversation) else {
let Ok(messages) = messages_since_kickoff(conversation) else {
return not_applicable();
};
// Only a turn that is trying to end can be vetoed.
@@ -362,7 +329,7 @@ impl Operation<Session, ConversationEffect> for BuzzReplyGuardOperation {
conversation: &Conversation,
_emit: &Emitter,
) -> Result<OperationResult<ConversationEffect>> {
let Some(messages) = messages_since_kickoff(conversation) else {
let Ok(messages) = messages_since_kickoff(conversation) else {
return not_applicable();
};
if !ends_turn(messages) {
@@ -554,9 +521,7 @@ pub fn round_gate(
cancel: tokio_util::sync::CancellationToken,
stop_veto: Option<(Arc<Agent>, String)>,
reply_guard_tools: Option<Vec<String>>,
) -> goose::agents::state_machine::StateMachine<'static, Session, ConversationEffect> {
use goose::agents::state_machine::Step;
) -> StateMachine<'static, Session, ConversationEffect> {
// Order is precedence. The round budget is checked first: once it is
// spent the turn ends, and asking `_Stop` to veto a turn we are ending
// anyway would dispatch a tool call whose answer cannot be honoured.
@@ -579,7 +544,7 @@ pub fn round_gate(
available_tools,
))));
}
goose::agents::state_machine::StateMachine::new(steps, cancel)
StateMachine::new(steps, cancel)
}
/// The operations that run at the start of a round, before inference.
@@ -593,13 +558,11 @@ pub fn round_start(
steers: crate::steer::SteerQueue,
compaction: BuzzCompactionOperation,
cancel: tokio_util::sync::CancellationToken,
) -> goose::agents::state_machine::StateMachine<'static, Session, ConversationEffect> {
use goose::agents::state_machine::Step;
) -> StateMachine<'static, Session, ConversationEffect> {
// Steer before compaction: a steer that arrives just as the window fills
// should be part of what gets summarised, not appended to a conversation
// that was compacted a moment earlier without it.
goose::agents::state_machine::StateMachine::new(
StateMachine::new(
vec![
Step::Operation(Arc::new(BuzzSteerOperation::new(steers))),
Step::Operation(Arc::new(compaction)),