Skip to content

Commit 29e5af0

Browse files
fix(acp): require control commands at command position
An owner-authored message beginning with `@` armed a scan across all remaining prose for the first whitespace-prefixed bang token. That made asking an agent *about* a command indistinguishable from issuing it: `@Agent explain what happens when I use !shutdown` parsed as `!shutdown` and terminated the harness, and the same shape could cancel or rotate an active turn mid-sentence. Commands are now recognized only at command position — first in the message, or immediately after the run of leading `@mention` tokens. This deliberately reverses an earlier trade-off in this branch, which accepted a bang anywhere after a leading mention so that `@Agent please run !model haiku` would fire. That was too wide: the same permissiveness is exactly what lets a question execute. `@Agent !model haiku` still works; the prose form no longer does. It also drops mentions holding spaces (`@Will Pfleger !rotate`), which the previous scan tolerated by accident. The parser has no display-name list, so where such a mention ends is genuinely ambiguous — and any rule loose enough to end it at `Pfleger` also ends it at the last word of a sentence. Not firing is the safe side of that ambiguity. Signed-off-by: Troy Hoffman <[email protected]>
1 parent 5f9a08b commit 29e5af0

2 files changed

Lines changed: 44 additions & 21 deletions

File tree

‎crates/buzz-acp/README.md‎

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

161161
`!model` matches model IDs exactly — adapters ship near-identical pairs (`opus[1m]` vs `claude-opus-5`, `gpt-5.3-codex` vs `gpt-5.3-codex/low`) where a prefix match would silently pick a different context lane and price point. An unknown ID is rejected with the list and leaves the current model untouched.
162162

163-
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. The command may be typed on its own (`!rotate`) or after a leading mention (`@Agent !rotate`).
163+
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. The command may be typed on its own (`!rotate`) or immediately after the leading `@mention` tokens (`@Agent !rotate`, `@Sol @Eva !rotate`).
164+
165+
The bang must be at that command position. A bang appearing later in the message is ordinary text, so asking an agent *about* a command — `@Agent what happens if I use !shutdown` — is delivered as a normal message rather than executed. Since the harness has no display-name list, a mention holding spaces (`@Will Pfleger !rotate`) does not reach command position either; address such an agent with a single-token handle to use control commands.
164166

165167
> **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.
166168

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

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2838,6 +2838,19 @@ fn event_mentions_agent(event: &nostr::Event, agent_pubkey_hex: &str) -> bool {
28382838
/// starts with a `!token`, or with an `@mention` followed by one. Ownership is
28392839
/// NOT checked here — callers re-check the author so a non-owner's `!cancel`
28402840
/// still reaches the agent as a prompt.
2841+
///
2842+
/// The bang must sit at **command position**: either first, or immediately
2843+
/// after the run of leading `@mention` tokens that addresses the agent. Prose
2844+
/// never arms it. Anything looser makes an ordinary question — `@Agent what
2845+
/// happens if I run !shutdown` — indistinguishable from the command itself.
2846+
///
2847+
/// A mention is one whitespace-delimited `@token`, so a display name holding
2848+
/// spaces (`@Will Pfleger !rotate`) does not reach command position and is
2849+
/// delivered to the agent as an ordinary message instead. Without the name
2850+
/// list — which the main loop does not have — where such a mention ends is
2851+
/// genuinely ambiguous, and that ambiguity is the whole vulnerability: any
2852+
/// rule permissive enough to end the mention at `Pfleger` also ends it at the
2853+
/// last word of a sentence. Not firing is the safe side of that trade.
28412854
fn owner_control_command<'a>(
28422855
event: &'a nostr::Event,
28432856
kind_u32: u32,
@@ -2846,19 +2859,20 @@ fn owner_control_command<'a>(
28462859
if kind_u32 != KIND_STREAM_MESSAGE || !event_mentions_agent(event, agent_pubkey_hex) {
28472860
return None;
28482861
}
2849-
let content = event.content.trim();
2862+
let mut content = event.content.trim();
28502863
// Mentioning is how a client addresses an agent, so `@Name !rotate` is the
2851-
// natural gesture, and a display name may hold spaces (`@Codex (Sol)`).
2852-
// Skip past the mention to the first `!`-initiated token rather than
2853-
// matching the name: the `p` tag already proved the mention is ours.
2854-
let content = if content.starts_with('@') {
2855-
content
2856-
.char_indices()
2857-
.find(|&(i, c)| c == '!' && content[..i].ends_with(char::is_whitespace))
2858-
.map_or("", |(i, _)| &content[i..])
2859-
} else {
2860-
content
2861-
};
2864+
// natural gesture, and a channel may address several agents at once
2865+
// (`@Sol @Eva !rotate`). Skip the leading mention tokens without matching a
2866+
// name — the `p` tag already proved the mention is ours. Every skipped
2867+
// token must itself start with `@`, which is what keeps the scan from
2868+
// running into prose.
2869+
while let Some(after_at) = content.strip_prefix('@') {
2870+
let token_len = after_at.find(char::is_whitespace).unwrap_or(after_at.len());
2871+
if token_len == 0 {
2872+
return None; // bare `@` — not a mention
2873+
}
2874+
content = after_at[token_len..].trim_start();
2875+
}
28622876
if !content.starts_with('!') {
28632877
return None;
28642878
}
@@ -4402,20 +4416,27 @@ mod owner_control_command_tests {
44024416
// `("!shutdown", "please")` matches no arm and falls through.
44034417
("!shutdown please", Some(("!shutdown", "please"))),
44044418
("!bogus", Some(("!bogus", ""))),
4405-
// Clients address an agent by mention, and a display name may hold
4406-
// spaces, so a leading mention is skipped without matching a name.
4419+
// Clients address an agent by mention, so leading `@token`s are
4420+
// skipped without matching a name — including several at once.
44074421
("@Claude !rotate", Some(("!rotate", ""))),
44084422
("@Claude !rotate now", Some(("!rotate", "now"))),
4409-
("@Will Pfleger !rotate", Some(("!rotate", ""))),
4410-
("@Codex (Sol) !cancel", Some(("!cancel", ""))),
4423+
("@Sol @Eva !cancel", Some(("!cancel", ""))),
44114424
("@Claude", None),
4412-
// A leading `@` arms the scan for the rest of the content, so a
4413-
// bang anywhere after one is a command.
4414-
("@Claude please run !rotate", Some(("!rotate", ""))),
4415-
// Without that leading `@`, a bang never fires.
4425+
// The bang must be at command position. Prose after the mention
4426+
// never arms it, or asking the agent *about* a command runs it.
4427+
("@Claude please run !rotate", None),
4428+
("@Claude what happens if I use !shutdown", None),
4429+
// A display name holding spaces does not reach command position:
4430+
// the parser has no name list, and any rule loose enough to end
4431+
// the mention at `Pfleger` also ends it mid-sentence.
4432+
("@Will Pfleger !rotate", None),
4433+
("@Codex (Sol) !cancel", None),
4434+
// A bang that is not first and not behind a leading mention is
4435+
// just text.
44164436
("hello @Claude !rotate", None),
44174437
("hello !rotate", None),
44184438
("", None),
4439+
("@", None),
44194440
] {
44204441
let event = make_event(KIND_STREAM_MESSAGE, content, Some(&agent));
44214442
assert_eq!(

0 commit comments

Comments
 (0)