Skip to content

Commit 1fe074b

Browse files
committed
fix(acp): deliver plain-text replies when the model skips the send tool
buzz-agent's output is its tool calls; streamed assistant content is observability-only and normally never posted. Capable models reliably call `buzz messages send`, but weaker local models (e.g. via Buzz shared compute) often answer a conversational prompt in plain content and never call the send tool, silently dropping the reply. Add a content-delivery fallback in buzz-acp: track per-turn whether the model published its own message and buffer streamed content; on a normal turn end with content but no publish tool call, post that content as a threaded kind-9 reply (reusing buzz_sdk::build_message, signed with the agent keys, best-effort, mirroring post_failure_notice). Skips bare acknowledgements the base prompt forbids publishing. Live-validated against a small Gemma on a real community: the fallback fires when the model answers in prose and stays dormant when it calls the send tool. Unit tests cover send-tool detection (incl. read-vs-send discrimination) and bare-ack filtering.
1 parent 2a72d94 commit 1fe074b

2 files changed

Lines changed: 307 additions & 0 deletions

File tree

‎crates/buzz-acp/src/acp.rs‎

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,17 @@ pub struct AcpClient {
200200
/// deltas. Both goose and buzz-agent emit this notification; goose gates
201201
/// on client capability advertisement, buzz-agent emits unconditionally.
202202
goose_usage: UsageTracker,
203+
/// Accumulated `agent_message_chunk` text for the current turn. Used by the
204+
/// content-delivery fallback: weak local models (e.g. via Buzz shared
205+
/// compute) often answer a conversational prompt in plain assistant
206+
/// `content` instead of calling `buzz messages send`, which would otherwise
207+
/// be silently dropped (buzz-agent's output is its tool calls; streamed
208+
/// text is observability-only). Reset at the start of every turn.
209+
turn_message_text: String,
210+
/// Whether a `buzz messages send` (or forum-post/comment) publish tool call
211+
/// was observed this turn. When true the fallback does NOT fire — the agent
212+
/// delivered its own reply. Reset at the start of every turn.
213+
turn_sent_message: bool,
203214
}
204215

205216
/// Recursively merge `overlay` into `base`, with `overlay` winning on scalar/shape
@@ -492,6 +503,8 @@ impl AcpClient {
492503
active_run_id: None,
493504
steer_rx: None,
494505
goose_usage: UsageTracker::default(),
506+
turn_message_text: String::new(),
507+
turn_sent_message: false,
495508
})
496509
}
497510

@@ -685,6 +698,10 @@ impl AcpClient {
685698
// misattributed to this turn.
686699
self.goose_usage.begin_turn(session_id);
687700

701+
// Reset the content-delivery fallback trackers for this turn.
702+
self.turn_message_text.clear();
703+
self.turn_sent_message = false;
704+
688705
self.last_prompt_id = Some(self.next_id);
689706
let id = self.next_id;
690707
self.next_id += 1;
@@ -780,6 +797,29 @@ impl AcpClient {
780797
self.goose_usage.take()
781798
}
782799

800+
/// Take the accumulated assistant `content` text for the completed turn,
801+
/// if and only if the agent did NOT publish a message itself this turn.
802+
///
803+
/// Returns `Some(trimmed_text)` when the turn produced streamed assistant
804+
/// content but no `buzz messages send` tool call fired — the caller then
805+
/// delivers it as the channel reply (content-delivery fallback). Returns
806+
/// `None` when the agent sent its own message, when there was no content,
807+
/// or when the content is only a bare acknowledgement (which the base
808+
/// prompt forbids publishing). Clears the buffer either way.
809+
pub fn take_undelivered_turn_message(&mut self) -> Option<String> {
810+
let text = std::mem::take(&mut self.turn_message_text);
811+
let sent = self.turn_sent_message;
812+
self.turn_sent_message = false;
813+
if sent {
814+
return None;
815+
}
816+
let trimmed = text.trim();
817+
if trimmed.is_empty() || is_bare_acknowledgement(trimmed) {
818+
return None;
819+
}
820+
Some(trimmed.to_string())
821+
}
822+
783823
/// Install a per-turn steer request channel for goose-native
784824
/// non-cancelling mid-turn delivery.
785825
///
@@ -1531,6 +1571,10 @@ impl AcpClient {
15311571
"agent_message_chunk" => {
15321572
if let Some(text) = update["content"]["text"].as_str() {
15331573
tracing::info!(target: "acp::stream", "{text}");
1574+
// Accumulate for the content-delivery fallback (see
1575+
// `turn_message_text`). Streamed assistant text is otherwise
1576+
// observability-only and never posted to the channel.
1577+
self.turn_message_text.push_str(text);
15341578
}
15351579
false
15361580
}
@@ -1544,6 +1588,17 @@ impl AcpClient {
15441588
.and_then(|v| v.as_str())
15451589
.unwrap_or("unknown");
15461590
tracing::info!(target: "acp::tool", "tool_call: {title} ({kind})");
1591+
// Detect a message-publish tool call so the fallback knows the
1592+
// agent delivered its own reply. The publish path is the
1593+
// dev-mcp `shell` tool running `buzz messages send` (title is
1594+
// the tool name, rawInput carries the command/args), so scan
1595+
// both title and rawInput for the CLI publish signature.
1596+
if tool_call_is_message_publish(update) {
1597+
self.turn_sent_message = true;
1598+
// Debug-level: the model published its own reply, so the
1599+
// content-delivery fallback will stay dormant this turn.
1600+
tracing::debug!("agent published its own message via send tool ({title})");
1601+
}
15471602
true
15481603
}
15491604
"tool_call_update" => {
@@ -1944,6 +1999,73 @@ pub fn model_in_catalog(
19441999
})
19452000
}
19462001

2002+
/// Return true if a `tool_call` session update represents a Buzz message
2003+
/// publish (kind 9 / forum post / comment). The publish path is the dev-mcp
2004+
/// `shell` tool running `buzz messages send` (or `buzz social publish`), so the
2005+
/// tool name alone is not enough — inspect `rawInput` (the command/args) for the
2006+
/// CLI publish signature. Conservative: only matches an actual send subcommand,
2007+
/// not reads like `buzz messages get`.
2008+
fn tool_call_is_message_publish(update: &serde_json::Value) -> bool {
2009+
// Flatten title + rawInput into one lowercase haystack. rawInput is
2010+
// arbitrary JSON (shell command string, or structured args), so serialize
2011+
// whatever is there.
2012+
let mut haystack = String::new();
2013+
if let Some(title) = update.get("title").and_then(|v| v.as_str()) {
2014+
haystack.push_str(title);
2015+
haystack.push(' ');
2016+
}
2017+
if let Some(raw) = update.get("rawInput") {
2018+
haystack.push_str(&raw.to_string());
2019+
}
2020+
let h = haystack.to_ascii_lowercase();
2021+
// Match the publish subcommands that actually post to a channel. Guard
2022+
// against read subcommands (get/thread/search/list) sharing the "messages"
2023+
// prefix by requiring the send/publish verb.
2024+
h.contains("messages send") || h.contains("messages send-diff") || h.contains("social publish")
2025+
}
2026+
2027+
/// Return true if `text` is a bare acknowledgement the base prompt forbids
2028+
/// publishing ("Got it", "Confirmed", "Standing by", …). Used to keep the
2029+
/// content-delivery fallback from posting filler that a capable agent would
2030+
/// have suppressed. Deliberately conservative — only short, whole-message
2031+
/// acks match, so a substantive reply that merely opens with "Got it, …"
2032+
/// still gets delivered.
2033+
fn is_bare_acknowledgement(text: &str) -> bool {
2034+
// Only consider short messages — a real reply with content is never a bare
2035+
// ack even if it starts with one.
2036+
if text.chars().count() > 40 {
2037+
return false;
2038+
}
2039+
let normalized: String = text
2040+
.to_ascii_lowercase()
2041+
.chars()
2042+
.filter(|c| c.is_alphanumeric() || c.is_whitespace())
2043+
.collect();
2044+
let normalized = normalized.trim();
2045+
const BARE_ACKS: &[&str] = &[
2046+
"got it",
2047+
"confirmed",
2048+
"acknowledged",
2049+
"ack",
2050+
"clear and noted",
2051+
"noted",
2052+
"aligned",
2053+
"standing by",
2054+
"parked",
2055+
"ok",
2056+
"okay",
2057+
"will do",
2058+
"understood",
2059+
"sounds good",
2060+
"on it",
2061+
"roger",
2062+
"roger that",
2063+
"i wont reply again",
2064+
"i will not reply again",
2065+
];
2066+
BARE_ACKS.contains(&normalized)
2067+
}
2068+
19472069
// ─── Drop: kill child process ─────────────────────────────────────────────────
19482070

19492071
impl Drop for AcpClient {
@@ -1991,6 +2113,70 @@ fn kill_process_group(_pid: u32) -> bool {
19912113
mod tests {
19922114
use super::*;
19932115

2116+
#[test]
2117+
fn tool_call_publish_detection() {
2118+
// A `buzz messages send` shell tool call → detected as a publish.
2119+
let send = serde_json::json!({
2120+
"title": "shell",
2121+
"rawInput": { "command": "buzz messages send --channel abc --content 'hi'" }
2122+
});
2123+
assert!(tool_call_is_message_publish(&send));
2124+
2125+
// send-diff variant → detected.
2126+
let diff = serde_json::json!({
2127+
"title": "shell",
2128+
"rawInput": { "command": "buzz messages send-diff --channel abc" }
2129+
});
2130+
assert!(tool_call_is_message_publish(&diff));
2131+
2132+
// social publish → detected.
2133+
let social = serde_json::json!({
2134+
"title": "shell",
2135+
"rawInput": { "command": "buzz social publish --content x" }
2136+
});
2137+
assert!(tool_call_is_message_publish(&social));
2138+
2139+
// A READ subcommand sharing the "messages" prefix → NOT a publish.
2140+
let read = serde_json::json!({
2141+
"title": "shell",
2142+
"rawInput": { "command": "buzz messages get --channel abc" }
2143+
});
2144+
assert!(!tool_call_is_message_publish(&read));
2145+
2146+
// Unrelated tool → not a publish.
2147+
let other = serde_json::json!({
2148+
"title": "read_file",
2149+
"rawInput": { "path": "/tmp/foo" }
2150+
});
2151+
assert!(!tool_call_is_message_publish(&other));
2152+
}
2153+
2154+
#[test]
2155+
fn bare_acknowledgement_detection() {
2156+
// Bare acks the base prompt forbids publishing.
2157+
for ack in [
2158+
"Got it",
2159+
"confirmed",
2160+
"Standing by",
2161+
"OK",
2162+
" Noted. ",
2163+
"will do",
2164+
] {
2165+
assert!(is_bare_acknowledgement(ack), "should be bare ack: {ack:?}");
2166+
}
2167+
// Substantive replies are NOT bare acks, even if they open with one.
2168+
for real in [
2169+
"Got it — I'll start on the migration and report back when the tests pass.",
2170+
"I'm doing well, thank you for asking! How are you today?",
2171+
"The build failed: missing dependency in Cargo.toml.",
2172+
] {
2173+
assert!(
2174+
!is_bare_acknowledgement(real),
2175+
"should NOT be bare ack: {real:?}"
2176+
);
2177+
}
2178+
}
2179+
19942180
#[test]
19952181
fn stop_reason_parses_all_known_values() {
19962182
assert_eq!(StopReason::from_str("end_turn"), Some(StopReason::EndTurn));

‎crates/buzz-acp/src/pool.rs‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,6 +1775,28 @@ pub async fn run_prompt_task(
17751775
None => prompt_sections.iter().map(String::as_str).collect(),
17761776
};
17771777

1778+
// Capture the reply destination for the content-delivery fallback BEFORE
1779+
// the prompt runs, so it survives any move of `batch` in the outcome arms.
1780+
// Only channel turns with a triggering event can receive a fallback post;
1781+
// heartbeats and DMs without a triggering message are skipped (None).
1782+
let fallback_reply: Option<FallbackReplyTarget> = batch.as_ref().and_then(|b| {
1783+
b.events.last().map(|last| {
1784+
let tags = crate::queue::parse_thread_tags(&last.event);
1785+
// Thread the reply to the triggering event: if the trigger is
1786+
// itself a reply, anchor to its root; otherwise the trigger IS
1787+
// the root. Mirrors the CLI `resolve_thread_ref` semantics.
1788+
let root_hex = tags
1789+
.root_event_id
1790+
.clone()
1791+
.unwrap_or_else(|| last.event.id.to_hex());
1792+
FallbackReplyTarget {
1793+
channel_id: b.channel_id,
1794+
root_event_hex: root_hex,
1795+
parent_event_hex: last.event.id.to_hex(),
1796+
}
1797+
})
1798+
});
1799+
17781800
// When control_rx is Some (channel tasks), wrap the prompt in select! so
17791801
// the main loop can cancel, interrupt, or rotate it. Heartbeats
17801802
// (control_rx=None) take the simple await path — they are not controllable.
@@ -1927,6 +1949,15 @@ pub async fn run_prompt_task(
19271949
Some(buzz_core::agent_turn_metric::StopReason::EndTurn),
19281950
)
19291951
.await;
1952+
// Content-delivery fallback (see the main EndTurn arm):
1953+
// this rare branch is also a successful turn end, so an
1954+
// undelivered plain-text reply still needs posting.
1955+
if let (Some(target), Some(content)) =
1956+
(&fallback_reply, agent.acp.take_undelivered_turn_message())
1957+
{
1958+
post_agent_content_fallback(&ctx.rest_client, target, &content)
1959+
.await;
1960+
}
19301961
send_prompt_result(
19311962
&result_tx,
19321963
&turn_id,
@@ -1990,6 +2021,21 @@ pub async fn run_prompt_task(
19902021
)
19912022
.await;
19922023

2024+
// Content-delivery fallback: on a normal turn end, if the agent
2025+
// produced assistant text but never called a publish tool, post
2026+
// that text as the channel reply. Only fires for `EndTurn` (not
2027+
// MaxTokens/MaxTurnRequests, which are truncated/aborted turns
2028+
// whose partial text shouldn't be treated as a deliberate reply)
2029+
// and only when a `fallback_reply` destination was captured
2030+
// (channel turns with a triggering event; not heartbeats).
2031+
if matches!(stop_reason, StopReason::EndTurn) {
2032+
if let (Some(target), Some(content)) =
2033+
(&fallback_reply, agent.acp.take_undelivered_turn_message())
2034+
{
2035+
post_agent_content_fallback(&ctx.rest_client, target, &content).await;
2036+
}
2037+
}
2038+
19932039
send_prompt_result(
19942040
&result_tx,
19952041
&turn_id,
@@ -3495,6 +3541,81 @@ pub(crate) async fn post_failure_notice(
34953541
}
34963542
}
34973543

3544+
/// Captured reply destination for the content-delivery fallback, taken before
3545+
/// the prompt runs so it survives any move of the triggering `batch`.
3546+
#[derive(Clone)]
3547+
struct FallbackReplyTarget {
3548+
channel_id: Uuid,
3549+
/// Thread root the reply anchors to (hex). Equals `parent_event_hex` when
3550+
/// the trigger was a top-level message.
3551+
root_event_hex: String,
3552+
/// Immediate parent being replied to (hex) — the triggering event.
3553+
parent_event_hex: String,
3554+
}
3555+
3556+
/// Content-delivery fallback: post an agent's plain-text reply (kind:9) that it
3557+
/// generated but never published itself.
3558+
///
3559+
/// buzz-agent's output is its tool calls; streamed assistant `content` is
3560+
/// observability-only and is normally not posted. Capable models reliably call
3561+
/// `buzz messages send`, but weaker local models (e.g. via Buzz shared compute)
3562+
/// often answer a conversational prompt in plain content and never call the
3563+
/// send tool — silently dropping the reply. When [`AcpClient`] reports such an
3564+
/// undelivered turn message, this posts it as a threaded reply, mirroring
3565+
/// [`post_failure_notice`]'s build/sign/submit path. Best-effort: any error is
3566+
/// logged and swallowed.
3567+
async fn post_agent_content_fallback(
3568+
rest: &crate::relay::RestClient,
3569+
target: &FallbackReplyTarget,
3570+
content: &str,
3571+
) {
3572+
let thread_ref = match (
3573+
nostr::EventId::from_hex(&target.root_event_hex),
3574+
nostr::EventId::from_hex(&target.parent_event_hex),
3575+
) {
3576+
(Ok(root_id), Ok(parent_id)) => Some(buzz_sdk::ThreadRef {
3577+
root_event_id: root_id,
3578+
parent_event_id: parent_id,
3579+
}),
3580+
_ => None,
3581+
};
3582+
let builder = match buzz_sdk::build_message(
3583+
target.channel_id,
3584+
content,
3585+
thread_ref.as_ref(),
3586+
&[],
3587+
false,
3588+
&[],
3589+
) {
3590+
Ok(b) => b,
3591+
Err(e) => {
3592+
tracing::warn!(channel = %target.channel_id, "content fallback: build failed: {e}");
3593+
return;
3594+
}
3595+
};
3596+
let event = match builder.sign_with_keys(&rest.keys) {
3597+
Ok(e) => e,
3598+
Err(e) => {
3599+
tracing::warn!(channel = %target.channel_id, "content fallback: sign failed: {e}");
3600+
return;
3601+
}
3602+
};
3603+
match tokio::time::timeout(Duration::from_secs(5), rest.submit_event(&event)).await {
3604+
Ok(Ok(_)) => {
3605+
// WARN (not INFO) and default target (buzz_acp::pool) so it is
3606+
// always visible under the harness's `buzz_acp=info` filter — this
3607+
// fallback firing is a signal worth surfacing (a model failed to
3608+
// call the send tool and we delivered its reply for it).
3609+
tracing::warn!(
3610+
channel = %target.channel_id,
3611+
"content-delivery fallback: posted undelivered agent content as channel reply"
3612+
);
3613+
}
3614+
Ok(Err(e)) => tracing::warn!(channel = %target.channel_id, "content fallback failed: {e}"),
3615+
Err(_) => tracing::warn!(channel = %target.channel_id, "content fallback timed out"),
3616+
}
3617+
}
3618+
34983619
/// Best-effort: remove a reaction via a signed kind:5 (NIP-09) deletion event.
34993620
///
35003621
/// Queries kind:7 reactions by our pubkey targeting the event, finds the matching

0 commit comments

Comments
 (0)