Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/buzz-acp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ Use `!cancel` to stop only the current turn; it is a no-op when the channel is i

Owner control commands must be kind:9 stream messages from the owner, must mention this agent with a `p` tag, and are consumed by the harness instead of being forwarded to the agent.

Because the command never reaches the agent, the harness posts the outcome back into the channel as a kind:9 notice — including when the command was a no-op (`!cancel` with no turn in flight, `!rotate` with no cached session), so a command that changed nothing is distinguishable from one that was never received.

> **Note:** The default mode is `owner-only`. Agents without a registered `agent_owner_pubkey` will not respond to any events until the owner is resolved. Set `--respond-to anyone` to disable the gate entirely.

**Examples:**
Expand Down
82 changes: 79 additions & 3 deletions crates/buzz-acp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,16 @@ async fn tokio_main() -> Result<()> {
sender = %buzz_event.event.pubkey.to_hex(),
"shutdown command from owner — exiting gracefully"
);
// Awaited, not spawned: the harness is
// about to exit, and a spawned task
// would race the shutdown.
pool::post_notice(
&ctx.rest_client,
buzz_event.channel_id,
&queue::parse_thread_tags(&buzz_event.event),
"Shutting down.",
)
.await;
let _ = shutdown_tx.send(());
continue;
}
Expand Down Expand Up @@ -2080,6 +2090,16 @@ async fn tokio_main() -> Result<()> {
"!cancel received but no in-flight task — no-op"
);
}
spawn_control_notice(
&ctx.rest_client,
&buzz_event.event,
buzz_event.channel_id,
if fired {
"Cancelled the current turn."
} else {
"Nothing to cancel — no turn in flight."
},
);
continue; // consume event — do NOT push to queue
}
}
Expand Down Expand Up @@ -2112,19 +2132,27 @@ async fn tokio_main() -> Result<()> {
buzz_event.channel_id,
ControlSignal::Rotate,
);
if fired {
let notice = if fired {
tracing::info!(
channel_id = %buzz_event.channel_id,
"!rotate received — cancelling in-flight turn and rotating session"
);
"Cancelled the current turn — the next one starts from a fresh session."
} else {
let invalidated = pool.invalidate_channel_sessions(buzz_event.channel_id);
tracing::info!(
channel_id = %buzz_event.channel_id,
invalidated,
"!rotate received — invalidated idle channel session(s)"
);
}
rotate_idle_notice(invalidated)
};
spawn_control_notice(
&ctx.rest_client,
&buzz_event.event,
buzz_event.channel_id,
notice,
);
continue; // consume event — do NOT push to queue
}
}
Expand Down Expand Up @@ -3046,11 +3074,44 @@ fn spawn_failure_notice(
let rest = rest.clone();
let channel_id = batch.channel_id;
tokio::spawn(async move {
pool::post_failure_notice(&rest, channel_id, &thread_tags, &content).await;
pool::post_notice(&rest, channel_id, &thread_tags, &content).await;
});
}
}

/// Spawn a task that posts the outcome of a consumed owner control command
/// back into the channel it came from.
///
/// Control commands are consumed by the harness and never reach the agent, so
/// this notice is the only signal the owner gets. Without it a successful
/// command, a no-op, and an event the harness never saw all look the same from
/// the chat: your own message, then silence.
fn spawn_control_notice(
rest_client: &relay::RestClient,
event: &nostr::Event,
channel_id: Uuid,
content: &'static str,
) {
let thread_tags = queue::parse_thread_tags(event);
let rest = rest_client.clone();
tokio::spawn(async move {
pool::post_notice(&rest, channel_id, &thread_tags, content).await;
});
}

/// Notice for a `!rotate` that found no turn in flight.
///
/// `invalidated` is how many cached sessions were dropped. Zero means there was
/// nothing to rotate — the next turn was already going to start fresh. Saying
/// so explicitly is the point: a silent no-op reads as a broken command.
fn rotate_idle_notice(invalidated: usize) -> &'static str {
if invalidated > 0 {
"Session rotated — the next turn starts fresh."
} else {
"No session to rotate — the next turn already starts fresh."
}
}

#[allow(clippy::too_many_arguments)]
fn handle_prompt_result(
pool: &mut AgentPool,
Expand Down Expand Up @@ -4314,6 +4375,21 @@ mod owner_control_command_tests {
));
}

#[test]
fn rotate_idle_notice_distinguishes_a_rotation_from_a_no_op() {
// The no-op is the case worth wording carefully: nothing was cached, so
// the command changed nothing, and the notice has to say that rather
// than claim a rotation that did not happen.
assert_eq!(
rotate_idle_notice(0),
"No session to rotate — the next turn already starts fresh."
);

let rotated = rotate_idle_notice(1);
assert_eq!(rotated, "Session rotated — the next turn starts fresh.");
assert_eq!(rotate_idle_notice(3), rotated);
}

#[test]
fn mode_gate_signal_maps_handling_to_control_signal() {
let owner = "a".repeat(64);
Expand Down
18 changes: 9 additions & 9 deletions crates/buzz-acp/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3615,11 +3615,11 @@ pub(crate) async fn reaction_add(rest: &crate::relay::RestClient, event_id: &str
}
}

/// Best-effort: post a visible failure notice (kind:9) to a channel after a
/// batch is dead-lettered. Replies into the thread of `thread_tags` when the
/// triggering event was threaded. Errors are logged and swallowed — the
/// notice must never take down the main loop.
pub(crate) async fn post_failure_notice(
/// Best-effort: post a visible notice (kind:9) to a channel — a dead-letter
/// warning, or the outcome of a consumed owner control command. Replies into
/// the thread of `thread_tags` when the triggering event was threaded. Errors
/// are logged and swallowed — the notice must never take down the main loop.
pub(crate) async fn post_notice(
rest: &crate::relay::RestClient,
channel_id: Uuid,
thread_tags: &ThreadTags,
Expand All @@ -3641,21 +3641,21 @@ pub(crate) async fn post_failure_notice(
match buzz_sdk::build_message(channel_id, content, thread_ref.as_ref(), &[], false, &[]) {
Ok(b) => b,
Err(e) => {
tracing::warn!(channel = %channel_id, "failure notice: build failed: {e}");
tracing::warn!(channel = %channel_id, "notice: build failed: {e}");
return;
}
};
let event = match builder.sign_with_keys(&rest.keys) {
Ok(e) => e,
Err(e) => {
tracing::warn!(channel = %channel_id, "failure notice: sign failed: {e}");
tracing::warn!(channel = %channel_id, "notice: sign failed: {e}");
return;
}
};
match tokio::time::timeout(Duration::from_secs(5), rest.submit_event(&event)).await {
Ok(Ok(_)) => {}
Ok(Err(e)) => tracing::warn!(channel = %channel_id, "failure notice failed: {e}"),
Err(_) => tracing::warn!(channel = %channel_id, "failure notice timed out"),
Ok(Err(e)) => tracing::warn!(channel = %channel_id, "notice failed: {e}"),
Err(_) => tracing::warn!(channel = %channel_id, "notice timed out"),
}
}

Expand Down