Skip to content

Commit e34d19e

Browse files
Revert "feat(acp): report control command outcomes as system rows"
The harness cannot publish a kind:40099. Every client submission — WebSocket and `POST /events` alike — goes through `ingest_event_inner`, which calls `required_scope_for_kind` (`ingest.rs:1544`); 40099 has no arm there and falls through to `_ => Err("restricted: unknown event kind")`. The existing system rows never hit that path: `emit_system_message` writes straight to `state.db.insert_event` (`side_effects.rs:775`). So the system-row version was rejected at ingest and the channel stayed silent — the exact failure this PR exists to fix. Caught by @chessing234 in review. My earlier check was necessary but not sufficient: I confirmed 40099 was absent from `is_relay_only_kind` and had no `handle_side_effects` arm, and concluded it was publishable, having missed the gate that actually decides. Reverts the switch and restores the kind:9 notice, which is accepted today (`KIND_STREAM_MESSAGE` → `Scope::MessagesWrite`). The thread-placement regression test from 3c05927 is unaffected and still guards the behaviour. Note for anyone tempted by the one-line fix: adding 40099 to `required_scope_for_kind` would be a security regression, not a fix. Every system row today is relay-authored and authoritative — membership changes, `channel_archived`, and the `message_deleted` moderation tombstone carrying "Removed by community moderators". Making the kind client-writable would let any member forge those. A legitimate system row needs its own agent-writable kind, which is a protocol decision, not a scope-table edit. Refs #3711 Co-Authored-By: Claude Opus 5 <[email protected]> Signed-off-by: Bérenger Ouadi <[email protected]>
1 parent abba357 commit e34d19e

8 files changed

Lines changed: 37 additions & 288 deletions

File tree

‎crates/buzz-acp/README.md‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,7 @@ Use `!cancel` to stop only the current turn; it is a no-op when the channel is i
159159

160160
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.
161161

162-
Because the command never reaches the agent, the harness publishes the outcome back into the channel as a kind:40099 system message — the grey row clients render beside "X joined the channel", since a cancelled turn or a rotated session is a lifecycle event rather than the agent speaking. It lands in the thread the command was typed in, and it is published for the no-op outcomes too (`!cancel` with no turn in flight, `!rotate` with no cached session), so a command that changed nothing stays distinguishable from one that was never received.
163-
164-
The payload is `{"type": …, "actor": <owner>, "target": <agent>}`. Clients switch on `type` and silently drop an unknown one, so a client older than this contract shows no row at all.
162+
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.
165163

166164
> **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.
167165

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

Lines changed: 25 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,15 +2047,11 @@ async fn tokio_main() -> Result<()> {
20472047
// Awaited, not spawned: the harness is
20482048
// about to exit, and a spawned task
20492049
// would race the shutdown.
2050-
pool::post_system_notice(
2050+
pool::post_notice(
20512051
&ctx.rest_client,
20522052
buzz_event.channel_id,
20532053
&queue::parse_thread_tags(&buzz_event.event),
2054-
&control_notice_payload(
2055-
NOTICE_SHUTDOWN,
2056-
owner,
2057-
&pubkey_hex,
2058-
),
2054+
"Shutting down.",
20592055
)
20602056
.await;
20612057
let _ = shutdown_tx.send(());
@@ -2099,12 +2095,10 @@ async fn tokio_main() -> Result<()> {
20992095
&buzz_event.event,
21002096
buzz_event.channel_id,
21012097
if fired {
2102-
NOTICE_TURN_CANCELLED
2098+
"Cancelled the current turn."
21032099
} else {
2104-
NOTICE_TURN_CANCEL_NOOP
2100+
"Nothing to cancel — no turn in flight."
21052101
},
2106-
owner,
2107-
&pubkey_hex,
21082102
);
21092103
continue; // consume event — do NOT push to queue
21102104
}
@@ -2143,23 +2137,21 @@ async fn tokio_main() -> Result<()> {
21432137
channel_id = %buzz_event.channel_id,
21442138
"!rotate received — cancelling in-flight turn and rotating session"
21452139
);
2146-
NOTICE_SESSION_ROTATED_IN_FLIGHT
2140+
"Cancelled the current turn — the next one starts from a fresh session."
21472141
} else {
21482142
let invalidated = pool.invalidate_channel_sessions(buzz_event.channel_id);
21492143
tracing::info!(
21502144
channel_id = %buzz_event.channel_id,
21512145
invalidated,
21522146
"!rotate received — invalidated idle channel session(s)"
21532147
);
2154-
rotate_idle_notice_type(invalidated)
2148+
rotate_idle_notice(invalidated)
21552149
};
21562150
spawn_control_notice(
21572151
&ctx.rest_client,
21582152
&buzz_event.event,
21592153
buzz_event.channel_id,
21602154
notice,
2161-
owner,
2162-
&pubkey_hex,
21632155
);
21642156
continue; // consume event — do NOT push to queue
21652157
}
@@ -3082,44 +3074,13 @@ fn spawn_failure_notice(
30823074
let rest = rest.clone();
30833075
let channel_id = batch.channel_id;
30843076
tokio::spawn(async move {
3085-
pool::post_failure_notice(&rest, channel_id, &thread_tags, &content).await;
3077+
pool::post_notice(&rest, channel_id, &thread_tags, &content).await;
30863078
});
30873079
}
30883080
}
30893081

3090-
/// `type` values of the kind:40099 system message reporting the outcome of a
3091-
/// consumed owner control command.
3092-
///
3093-
/// The renderers switch on these strings and drop an unknown one silently
3094-
/// (`describeSystemEvent` in `SystemMessageRow.tsx`, `SystemEvent.fromContent`
3095-
/// in `timeline_message.dart`), so adding a variant here means adding a case in
3096-
/// both.
3097-
const NOTICE_SHUTDOWN: &str = "agent_shutdown";
3098-
const NOTICE_TURN_CANCELLED: &str = "agent_turn_cancelled";
3099-
const NOTICE_TURN_CANCEL_NOOP: &str = "agent_turn_cancel_noop";
3100-
const NOTICE_SESSION_ROTATED_IN_FLIGHT: &str = "agent_session_rotated_in_flight";
3101-
const NOTICE_SESSION_ROTATED: &str = "agent_session_rotated";
3102-
const NOTICE_SESSION_ROTATE_NOOP: &str = "agent_session_rotate_noop";
3103-
3104-
/// Build the system-message payload for a consumed owner control command.
3105-
///
3106-
/// `actor` is the owner who issued the command, `target` the agent it acted on
3107-
/// — the same two fields every other system message uses, so the clients
3108-
/// resolve both to profile names and prefetch their profiles.
3109-
fn control_notice_payload(
3110-
notice_type: &str,
3111-
owner_hex: &str,
3112-
agent_hex: &str,
3113-
) -> serde_json::Value {
3114-
serde_json::json!({
3115-
"type": notice_type,
3116-
"actor": owner_hex,
3117-
"target": agent_hex,
3118-
})
3119-
}
3120-
3121-
/// Spawn a task that publishes the outcome of a consumed owner control command
3122-
/// into the channel it came from.
3082+
/// Spawn a task that posts the outcome of a consumed owner control command
3083+
/// back into the channel it came from.
31233084
///
31243085
/// Control commands are consumed by the harness and never reach the agent, so
31253086
/// this notice is the only signal the owner gets. Without it a successful
@@ -3129,28 +3090,25 @@ fn spawn_control_notice(
31293090
rest_client: &relay::RestClient,
31303091
event: &nostr::Event,
31313092
channel_id: Uuid,
3132-
notice_type: &'static str,
3133-
owner_hex: &str,
3134-
agent_hex: &str,
3093+
content: &'static str,
31353094
) {
31363095
let thread_tags = queue::parse_thread_tags(event);
3137-
let payload = control_notice_payload(notice_type, owner_hex, agent_hex);
31383096
let rest = rest_client.clone();
31393097
tokio::spawn(async move {
3140-
pool::post_system_notice(&rest, channel_id, &thread_tags, &payload).await;
3098+
pool::post_notice(&rest, channel_id, &thread_tags, content).await;
31413099
});
31423100
}
31433101

3144-
/// Notice type for a `!rotate` that found no turn in flight.
3102+
/// Notice for a `!rotate` that found no turn in flight.
31453103
///
31463104
/// `invalidated` is how many cached sessions were dropped. Zero means there was
31473105
/// nothing to rotate — the next turn was already going to start fresh. Saying
31483106
/// so explicitly is the point: a silent no-op reads as a broken command.
3149-
fn rotate_idle_notice_type(invalidated: usize) -> &'static str {
3107+
fn rotate_idle_notice(invalidated: usize) -> &'static str {
31503108
if invalidated > 0 {
3151-
NOTICE_SESSION_ROTATED
3109+
"Session rotated — the next turn starts fresh."
31523110
} else {
3153-
NOTICE_SESSION_ROTATE_NOOP
3111+
"No session to rotate — the next turn already starts fresh."
31543112
}
31553113
}
31563114

@@ -4448,43 +4406,17 @@ mod owner_control_command_tests {
44484406

44494407
#[test]
44504408
fn rotate_idle_notice_distinguishes_a_rotation_from_a_no_op() {
4451-
// The no-op is the case worth distinguishing: nothing was cached, so
4452-
// the command changed nothing, and the row has to say that rather than
4453-
// claim a rotation that did not happen.
4454-
assert_eq!(rotate_idle_notice_type(0), NOTICE_SESSION_ROTATE_NOOP);
4455-
4456-
let rotated = rotate_idle_notice_type(1);
4457-
assert_eq!(rotated, NOTICE_SESSION_ROTATED);
4458-
assert_eq!(rotate_idle_notice_type(3), rotated);
4459-
}
4460-
4461-
#[test]
4462-
fn control_notice_payload_names_the_owner_and_the_agent() {
4463-
// `actor` and `target` are the fields every other system message uses,
4464-
// and both renderers resolve them to profile names — swapping them
4465-
// would credit the wrong party for the command in the channel history.
4466-
let owner = "ab".repeat(32);
4467-
let agent = "cd".repeat(32);
4468-
let payload = control_notice_payload(NOTICE_TURN_CANCELLED, &owner, &agent);
4469-
4470-
assert_eq!(payload["type"], NOTICE_TURN_CANCELLED);
4471-
assert_eq!(payload["actor"], owner);
4472-
assert_eq!(payload["target"], agent);
4473-
}
4409+
// The no-op is the case worth wording carefully: nothing was cached, so
4410+
// the command changed nothing, and the notice has to say that rather
4411+
// than claim a rotation that did not happen.
4412+
assert_eq!(
4413+
rotate_idle_notice(0),
4414+
"No session to rotate — the next turn already starts fresh."
4415+
);
44744416

4475-
#[test]
4476-
fn control_notice_types_are_distinct() {
4477-
// A duplicate would make two different outcomes render the same row.
4478-
let all = [
4479-
NOTICE_SHUTDOWN,
4480-
NOTICE_TURN_CANCELLED,
4481-
NOTICE_TURN_CANCEL_NOOP,
4482-
NOTICE_SESSION_ROTATED_IN_FLIGHT,
4483-
NOTICE_SESSION_ROTATED,
4484-
NOTICE_SESSION_ROTATE_NOOP,
4485-
];
4486-
let unique: std::collections::HashSet<&str> = all.iter().copied().collect();
4487-
assert_eq!(unique.len(), all.len());
4417+
let rotated = rotate_idle_notice(1);
4418+
assert_eq!(rotated, "Session rotated — the next turn starts fresh.");
4419+
assert_eq!(rotate_idle_notice(3), rotated);
44884420
}
44894421

44904422
#[test]

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

Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3635,51 +3635,11 @@ pub(crate) fn thread_ref_from_tags(thread_tags: &ThreadTags) -> Option<buzz_sdk:
36353635
})
36363636
}
36373637

3638-
/// Best-effort: publish a kind:40099 system message into `channel_id`, signed
3639-
/// with the agent's own key.
3640-
///
3641-
/// Carries the outcome of an owner control command the harness consumed. A
3642-
/// cancelled turn or a rotated session is a lifecycle event, not the agent
3643-
/// speaking, so it renders as a system row beside "X joined the channel"
3644-
/// instead of as a message in the conversation. It still lands in the thread
3645-
/// the command was typed in — thread placement is derived from NIP-10 `e` tags
3646-
/// and is kind-agnostic on the client.
3647-
///
3648-
/// Errors are logged and swallowed: the notice must never take down the main
3649-
/// loop, and must never block the command it reports on.
3650-
pub(crate) async fn post_system_notice(
3651-
rest: &crate::relay::RestClient,
3652-
channel_id: Uuid,
3653-
thread_tags: &ThreadTags,
3654-
payload: &serde_json::Value,
3655-
) {
3656-
let thread_ref = thread_ref_from_tags(thread_tags);
3657-
let builder = match buzz_sdk::build_system_message(channel_id, payload, thread_ref.as_ref()) {
3658-
Ok(b) => b,
3659-
Err(e) => {
3660-
tracing::warn!(channel = %channel_id, "system notice: build failed: {e}");
3661-
return;
3662-
}
3663-
};
3664-
let event = match builder.sign_with_keys(&rest.keys) {
3665-
Ok(e) => e,
3666-
Err(e) => {
3667-
tracing::warn!(channel = %channel_id, "system notice: sign failed: {e}");
3668-
return;
3669-
}
3670-
};
3671-
match tokio::time::timeout(Duration::from_secs(5), rest.submit_event(&event)).await {
3672-
Ok(Ok(_)) => {}
3673-
Ok(Err(e)) => tracing::warn!(channel = %channel_id, "system notice failed: {e}"),
3674-
Err(_) => tracing::warn!(channel = %channel_id, "system notice timed out"),
3675-
}
3676-
}
3677-
3678-
/// Best-effort: post a visible failure notice (kind:9) to a channel after a
3679-
/// batch is dead-lettered. Replies into the thread of `thread_tags` when the
3680-
/// triggering event was threaded. Errors are logged and swallowed — the
3681-
/// notice must never take down the main loop.
3682-
pub(crate) async fn post_failure_notice(
3638+
/// Best-effort: post a visible notice (kind:9) to a channel — a dead-letter
3639+
/// warning, or the outcome of a consumed owner control command. Replies into
3640+
/// the thread of `thread_tags` when the triggering event was threaded. Errors
3641+
/// are logged and swallowed — the notice must never take down the main loop.
3642+
pub(crate) async fn post_notice(
36833643
rest: &crate::relay::RestClient,
36843644
channel_id: Uuid,
36853645
thread_tags: &ThreadTags,
@@ -3690,21 +3650,21 @@ pub(crate) async fn post_failure_notice(
36903650
match buzz_sdk::build_message(channel_id, content, thread_ref.as_ref(), &[], false, &[]) {
36913651
Ok(b) => b,
36923652
Err(e) => {
3693-
tracing::warn!(channel = %channel_id, "failure notice: build failed: {e}");
3653+
tracing::warn!(channel = %channel_id, "notice: build failed: {e}");
36943654
return;
36953655
}
36963656
};
36973657
let event = match builder.sign_with_keys(&rest.keys) {
36983658
Ok(e) => e,
36993659
Err(e) => {
3700-
tracing::warn!(channel = %channel_id, "failure notice: sign failed: {e}");
3660+
tracing::warn!(channel = %channel_id, "notice: sign failed: {e}");
37013661
return;
37023662
}
37033663
};
37043664
match tokio::time::timeout(Duration::from_secs(5), rest.submit_event(&event)).await {
37053665
Ok(Ok(_)) => {}
3706-
Ok(Err(e)) => tracing::warn!(channel = %channel_id, "failure notice failed: {e}"),
3707-
Err(_) => tracing::warn!(channel = %channel_id, "failure notice timed out"),
3666+
Ok(Err(e)) => tracing::warn!(channel = %channel_id, "notice failed: {e}"),
3667+
Err(_) => tracing::warn!(channel = %channel_id, "notice timed out"),
37083668
}
37093669
}
37103670

‎crates/buzz-core/src/kind.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,7 @@ pub const KIND_STREAM_REMINDER: u32 = 40007;
433433
pub const KIND_STREAM_MESSAGE_DIFF: u32 = 40008;
434434
/// Canvas (shared document) for a channel.
435435
pub const KIND_CANVAS: u32 = 40100;
436-
/// System message for channel state changes (join, leave, rename, etc.),
437-
/// and for the outcome of an owner control command consumed by the ACP
438-
/// harness. Relay-signed for channel state; agent-signed for the latter.
436+
/// System message for channel state changes (join, leave, rename, etc.).
439437
pub const KIND_SYSTEM_MESSAGE: u32 = 40099;
440438

441439
// Relay-only sidecar kinds (never client-submitted)

‎crates/buzz-sdk/src/builders.rs‎

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Typed event builder functions (39 builders).
1+
//! Typed event builder functions (38 builders).
22
//!
33
//! All functions return `Result<nostr::EventBuilder, SdkError>`.
44
//! The caller signs: `builder.sign_with_keys(&keys)?`.
@@ -237,35 +237,6 @@ pub fn build_message(
237237
Ok(EventBuilder::new(Kind::Custom(9), content).tags(tags))
238238
}
239239

240-
/// Build a system message (kind 40099) for a channel.
241-
///
242-
/// System messages carry a JSON payload whose `type` field clients switch on to
243-
/// render a system row — a channel lifecycle event such as a membership change,
244-
/// or the outcome of an owner control command consumed by the harness — instead
245-
/// of a message in the conversation. Unknown `type` values are dropped by the
246-
/// clients, so a new one needs matching cases in the renderers.
247-
///
248-
/// `thread_ref` places the row in the thread that triggered it; `None` posts it
249-
/// at channel level. Thread placement is derived from NIP-10 `e` tags and is
250-
/// kind-agnostic on the client, so this threads exactly like [`build_message`].
251-
pub fn build_system_message(
252-
channel_id: Uuid,
253-
payload: &serde_json::Value,
254-
thread_ref: Option<&ThreadRef>,
255-
) -> Result<EventBuilder, SdkError> {
256-
let content = payload.to_string();
257-
check_content(&content, 64 * 1024)?;
258-
let mut tags = vec![tag(&["h", &channel_id.to_string()])?];
259-
if let Some(tr) = thread_ref {
260-
thread_tags(tr, &mut tags)?;
261-
}
262-
Ok(EventBuilder::new(
263-
Kind::Custom(buzz_core::kind::KIND_SYSTEM_MESSAGE as u16),
264-
content,
265-
)
266-
.tags(tags))
267-
}
268-
269240
/// Build an encrypted agent observer frame (kind 24200).
270241
///
271242
/// `recipient_pubkey` is the cleartext `p` tag used by the relay for owner-only

‎desktop/src/features/messages/ui/SystemMessageRow.tsx‎

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -574,53 +574,6 @@ function describeSystemEvent(
574574
),
575575
};
576576
}
577-
// Owner control commands consumed by the ACP harness (`!shutdown`,
578-
// `!cancel`, `!rotate`). The command never reaches the agent, so this row
579-
// is the only signal it took effect — including when it changed nothing,
580-
// which is why the no-op outcomes get their own wording instead of being
581-
// dropped. Published by the harness, not the relay; see `buzz-acp`.
582-
case "agent_shutdown":
583-
if (!payload.actor || !payload.target) return null;
584-
return {
585-
title: actorName,
586-
action: <>shut down {targetName}</>,
587-
};
588-
case "agent_turn_cancelled":
589-
if (!payload.actor || !payload.target) return null;
590-
return {
591-
title: actorName,
592-
action: <>cancelled {targetName}&rsquo;s current turn</>,
593-
};
594-
case "agent_turn_cancel_noop":
595-
if (!payload.actor || !payload.target) return null;
596-
return {
597-
title: actorName,
598-
action: (
599-
<>tried to cancel {targetName}&rsquo;s turn — nothing was running</>
600-
),
601-
};
602-
case "agent_session_rotated_in_flight":
603-
if (!payload.actor || !payload.target) return null;
604-
return {
605-
title: actorName,
606-
action: (
607-
<>cancelled {targetName}&rsquo;s turn and rotated its session</>
608-
),
609-
};
610-
case "agent_session_rotated":
611-
if (!payload.actor || !payload.target) return null;
612-
return {
613-
title: actorName,
614-
action: <>rotated {targetName}&rsquo;s session</>,
615-
};
616-
case "agent_session_rotate_noop":
617-
if (!payload.actor || !payload.target) return null;
618-
return {
619-
title: actorName,
620-
action: (
621-
<>rotated {targetName}&rsquo;s session — it was already fresh</>
622-
),
623-
};
624577
case "member_left":
625578
return {
626579
title: actorName,

0 commit comments

Comments
 (0)