Files
buzz/desktop/src-tauri/src/persona_catalog_tests.rs
Logan Johnson dd1c2afc2a Discover alternate Buzz ACP commands (#6948)
🤖 I’m Larry.

## Summary
Managed-agent definitions can now choose which installed ACP transport
will launch the agent. The choice sits beside **Agent harness** while
creating or editing an agent, so it is made before first deployment and
remains visible afterward.

Buzz discovers executable commands named `buzz-*-acp`, keeps **Buzz ACP
(default)** as the safe default, and preserves existing custom command
values. Discovery and launch share the same resolver, so a command
offered in the picker is the command Buzz will execute.

## UX walkthrough

### 1. The ACP command picker appears while creating the agent
The default is explicit, and the helper text explains that this
selection controls deployment.

![Create-agent form with the ACP command picker beside Agent
harness](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6948/01-persona-create-acp-selector.png)

### 2. Installed wrapper commands are offered by name
Here Buzz has discovered `buzz-janet-acp` alongside the stock transport.

![ACP command menu offering Buzz ACP and
buzz-janet-acp](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6948/02-discovered-wrapper-dropdown.png)

### 3. The wrapper is selected before deployment

![Create-agent form with buzz-janet-acp
selected](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6948/03-wrapper-selected-before-deploy.png)

### 4. The deployed profile reports the effective command

![Deployed agent Runtime profile showing
buzz-janet-acp](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6948/04-deployed-profile-effective-acp.png)

### 5. Editing the deployed agent reopens its definition with the choice
preserved

![Edit-agent form retaining
buzz-janet-acp](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6948/05-edit-shows-persisted-wrapper.png)

## Details
- Definitions carry the selected ACP command through relay publication,
snapshots, imports, team adoption, deployment, and later edits.
- Existing definitions without this field continue to use `buzz-acp`.
- Unknown persisted commands remain visible but unavailable rather than
being silently replaced.
- Discovery is read-only and never executes candidates. It filters
non-files and non-executables, supports Windows `.exe`, `.cmd`, and
`.bat` shims, deduplicates aliases, and returns a stable sorted list.
- Command names remain portable; Buzz does not persist machine-specific
absolute paths.

---------

Signed-off-by: Logan Johnson <loganj@squareup.com>
Signed-off-by: Larry <8cf5a83f590ec0955b11647d1c88f796a98e088c30a492c58e0e46c3026ae7a4@buzz.block.builderlab.xyz>
Signed-off-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz>
Co-authored-by: Larry <8cf5a83f590ec0955b11647d1c88f796a98e088c30a492c58e0e46c3026ae7a4@nostr>
Co-authored-by: Larry <8cf5a83f590ec0955b11647d1c88f796a98e088c30a492c58e0e46c3026ae7a4@buzz.block.builderlab.xyz>
Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz>
2026-09-24 11:33:24 -04:00

347 lines
12 KiB
Rust

use super::*;
use nostr::{EventBuilder, Keys, Kind, Tag, Timestamp};
use serde_json::json;
fn event(keys: &Keys, created_at: u64, source: &str, shared: bool, content: Value) -> Event {
let mut tags = vec![Tag::parse(["d", source]).unwrap()];
if shared {
tags.push(Tag::parse(["shared", "true"]).unwrap());
}
EventBuilder::new(Kind::Custom(KIND_PERSONA as u16), content.to_string())
.tags(tags)
.custom_created_at(Timestamp::from(created_at))
.sign_with_keys(keys)
.unwrap()
}
fn valid_content(name: &str) -> Value {
json!({
"display_name": name,
"system_prompt": "Review changes.",
"avatar_url": "https://relay.example/avatar.png",
"runtime": " goose ",
"model": "claude",
"provider": null,
"name_pool": ["Reviewer", 7],
"respond_to": "allowlist",
"parallelism": 4,
"session_policy": "thread"
})
}
#[test]
fn paging_uses_oldest_verified_cursor_and_stops_on_ties_or_short_pages() {
let keys = Keys::generate();
let newest = event(&keys, 9, "newest", true, valid_content("Newest"));
let oldest = event(&keys, 4, "oldest", true, valid_content("Oldest"));
let mut by_id = HashMap::new();
assert_eq!(
merge_verified_page(
&mut by_id,
CATALOG_PAGE_SIZE,
vec![newest.clone(), oldest.clone()]
),
PageProgress::Next(4)
);
assert_eq!(
merge_verified_page(&mut by_id, CATALOG_PAGE_SIZE, vec![newest, oldest]),
PageProgress::Done
);
let short = event(&keys, 1, "short", true, valid_content("Short"));
assert_eq!(
merge_verified_page(&mut by_id, CATALOG_PAGE_SIZE - 1, vec![short]),
PageProgress::Done
);
assert_eq!(
merge_verified_page(&mut HashMap::new(), CATALOG_PAGE_SIZE, Vec::new()),
PageProgress::Done
);
}
#[test]
fn forged_newest_head_is_dropped_before_it_can_claim_the_coordinate() {
let keys = Keys::generate();
let older = event(&keys, 1, "reviewer", true, valid_content("Older"));
let mut forged = event(&keys, 2, "reviewer", true, valid_content("Forged"));
forged.content = valid_content("Tampered").to_string();
let verified = [older.clone(), forged]
.into_iter()
.filter(|candidate| candidate.verify().is_ok())
.collect();
let publications = publications_from_verified_events(verified);
assert_eq!(publications.len(), 1);
assert_eq!(publications[0].event_id, older.id.to_hex());
}
#[test]
fn valid_newest_head_claims_before_visibility_and_content_parsing() {
let keys = Keys::generate();
for newest in [
event(&keys, 2, "reviewer", false, valid_content("Unshared")),
event(&keys, 2, "reviewer", true, json!({})),
] {
let older = event(&keys, 1, "reviewer", true, valid_content("Older"));
assert!(publications_from_verified_events(vec![older, newest]).is_empty());
}
}
#[test]
fn equal_second_heads_use_lowest_event_id_and_authors_are_independent() {
let alice = Keys::generate();
let bob = Keys::generate();
let shared = event(&alice, 1, "reviewer", true, valid_content("Shared"));
let unshared = event(&alice, 1, "reviewer", false, valid_content("Hidden"));
let bob_head = event(&bob, 1, "reviewer", true, valid_content("Bob"));
let expected_alice = if shared.id < unshared.id { 1 } else { 0 };
let publications = publications_from_verified_events(vec![shared, unshared, bob_head]);
assert_eq!(publications.len(), expected_alice + 1);
}
#[test]
fn parser_projects_types_and_foreign_allowlists_exactly() {
let projection = parse_agent(&valid_content("Reviewer").to_string()).unwrap();
assert_eq!(projection.display_name, "Reviewer");
assert_eq!(projection.runtime.as_deref(), Some(" goose "));
assert_eq!(projection.provider, None);
assert_eq!(projection.name_pool, vec!["Reviewer"]);
assert_eq!(projection.respond_to.as_deref(), Some("owner-only"));
assert_eq!(projection.parallelism, Some(4));
assert_eq!(projection.session_policy, AcpSessionPolicy::Thread);
for bad in [0, 33] {
let mut content = valid_content("Reviewer");
content["parallelism"] = json!(bad);
assert_eq!(parse_agent(&content.to_string()).unwrap().parallelism, None);
}
let mut legacy = valid_content("Legacy");
legacy.as_object_mut().unwrap().remove("session_policy");
assert_eq!(
parse_agent(&legacy.to_string()).unwrap().session_policy,
AcpSessionPolicy::Channel
);
let mut malformed = valid_content("Malformed");
malformed["session_policy"] = json!("conversation");
assert_eq!(
parse_agent(&malformed.to_string()).unwrap().session_policy,
AcpSessionPolicy::Channel
);
malformed["session_policy"] = json!(null);
assert_eq!(
parse_agent(&malformed.to_string()).unwrap().session_policy,
AcpSessionPolicy::Channel
);
}
#[test]
fn parser_rejects_malformed_and_invisible_definition_text() {
for content in [
"not-json".to_string(),
"[]".to_string(),
json!({"display_name": 7}).to_string(),
valid_content("Review\u{202e}er").to_string(),
] {
assert!(parse_agent(&content).is_none());
}
// A description that violates the shared visible-text policy or the
// 280-char cap rejects the whole entry — never silently stripped.
for bad_description in [
"hidden\u{200b}text".to_string(),
"description\n".to_string(),
"a".repeat(281),
] {
let mut content = valid_content("Reviewer");
content["description"] = json!(bad_description);
assert!(parse_agent(&content.to_string()).is_none());
}
for malformed_description in [json!(7), json!([]), json!({})] {
let mut content = valid_content("Reviewer");
content["description"] = malformed_description;
assert!(parse_agent(&content.to_string()).is_none());
}
let mut content = valid_content("Reviewer");
content["description"] = json!("A careful reviewer.");
assert_eq!(
parse_agent(&content.to_string())
.unwrap()
.description
.as_deref(),
Some("A careful reviewer.")
);
let visible = parse_agent(
&json!({
"display_name": "Reviewer 🐝",
"system_prompt": "Review.\n\t||literal markdown||"
})
.to_string(),
)
.unwrap();
assert_eq!(visible.display_name, "Reviewer 🐝");
}
#[test]
fn avatar_allowlist_and_bounds_match_the_renderer_contract() {
assert!(safe_avatar("https://relay.example/avatar.png"));
assert!(!safe_avatar("javascript:alert(1)"));
assert!(safe_avatar("data:image/svg+xml,<svg/>"));
assert!(!safe_avatar(&format!(
"data:image/svg+xml,{}",
"a".repeat(MAX_INLINE_SVG_AVATAR_LENGTH)
)));
for mime in ["png", "jpeg", "gif", "webp"] {
assert!(safe_avatar(&format!(
"data:image/{mime};base64,iVBORw0KGgo="
)));
}
assert!(!safe_avatar("data:image/bmp;base64,aA=="));
assert!(!safe_avatar("data:image/png;base64,not base64"));
}
#[test]
fn exact_tags_reject_duplicates_and_extra_fields() {
let keys = Keys::generate();
let base = event(&keys, 1, "reviewer", true, valid_content("Reviewer"));
assert_eq!(exact_tag(&base, "shared").as_deref(), Some("true"));
let duplicate = EventBuilder::new(
Kind::Custom(KIND_PERSONA as u16),
valid_content("x").to_string(),
)
.tags([
Tag::parse(["d", "reviewer"]).unwrap(),
Tag::parse(["shared", "true"]).unwrap(),
Tag::parse(["shared", "true"]).unwrap(),
])
.sign_with_keys(&keys)
.unwrap();
assert_eq!(exact_tag(&duplicate, "shared"), None);
// The old renderer accepts an extended d tag (it reads tag[1]) but shared
// is opt-in only for the exact two-field shape.
let extended = EventBuilder::new(
Kind::Custom(KIND_PERSONA as u16),
valid_content("x").to_string(),
)
.tags([
Tag::parse(["d", "reviewer", "relay hint"]).unwrap(),
Tag::parse(["shared", "true", "extra"]).unwrap(),
])
.sign_with_keys(&keys)
.unwrap();
assert_eq!(coordinate_tag(&extended, "d").as_deref(), Some("reviewer"));
assert_eq!(exact_tag(&extended, "shared"), None);
}
/// Pins the serialized DTO output against the renderer's catalog contract.
/// The Tauri generic is only a TypeScript assertion; serde's bytes are the
/// actual boundary, so populate every optional field and compare the value.
#[test]
fn serialized_catalog_matches_the_typescript_contract() {
let publication = PersonaCatalogPublication {
event_id: "ev1".into(),
owner_pubkey: "owner".into(),
source_persona_id: "persona-1".into(),
created_at: 42,
agent: CatalogAgentProjection {
acp_command: Some("buzz-janet-acp".into()),
display_name: "Ada".into(),
avatar_url: Some("https://example.com/a.png".into()),
description: Some("A kind agent.".into()),
system_prompt: "be kind".into(),
runtime: Some("acp".into()),
model: Some("m1".into()),
provider: Some("p1".into()),
name_pool: vec!["Ada".into(), "Lin".into()],
respond_to: Some("mentions".into()),
parallelism: Some(2),
session_policy: AcpSessionPolicy::Thread,
},
};
let actual = serde_json::to_value(vec![publication]).unwrap();
let expected = serde_json::json!([{
"eventId": "ev1",
"ownerPubkey": "owner",
"sourcePersonaId": "persona-1",
"createdAt": 42,
"agent": {
"displayName": "Ada",
"avatarUrl": "https://example.com/a.png",
"description": "A kind agent.",
"systemPrompt": "be kind",
"runtime": "acp",
"acpCommand": "buzz-janet-acp",
"model": "m1",
"provider": "p1",
"namePool": ["Ada", "Lin"],
"respondTo": "mentions",
"parallelism": 2,
"sessionPolicy": "thread",
},
}]);
assert_eq!(actual, expected);
}
#[test]
fn catalog_preserves_portable_acp_alias_and_rejects_nonportable_values() {
let mut content = valid_content("Reviewer");
assert_eq!(parse_agent(&content.to_string()).unwrap().acp_command, None);
for command in ["buzz-acp", "buzz-janet-acp"] {
content["acp_command"] = json!(command);
let projected = parse_agent(&content.to_string()).unwrap();
assert_eq!(projected.acp_command.as_deref(), Some(command));
assert_eq!(
serde_json::to_value(projected).unwrap()["acpCommand"],
command
);
}
for command in [
json!("/tmp/buzz-janet-acp"),
json!(r"C:\buzz-janet-acp.cmd"),
json!("sh"),
json!("buzz-a&b-acp"),
json!(7),
] {
content["acp_command"] = command;
assert!(parse_agent(&content.to_string()).is_none());
}
}
#[test]
fn shared_persona_publication_is_portable_and_catalog_readable() {
use crate::managed_agents::persona_events::{build_persona_event, persona_from_event};
let keys = Keys::generate();
let mut content = valid_content("Reviewer");
content["name_pool"] = json!(["Reviewer"]);
let seed = event(&keys, 1, "reviewer", false, content);
let mut persona = persona_from_event(&seed).unwrap();
persona.shared = true;
for (command, projected) in [
(Some("/opt/custom-acp"), None),
(Some("buzz-janet-acp"), Some("buzz-janet-acp")),
(Some("buzz-acp"), Some("buzz-acp")),
(None, Some("buzz-acp")),
] {
persona.acp_command = command.map(str::to_string);
let published = build_persona_event(&persona)
.unwrap()
.sign_with_keys(&keys)
.unwrap();
published.verify().unwrap();
assert!(!published.content.contains("/opt/custom-acp"));
let publications = publications_from_verified_events(vec![published]);
assert_eq!(publications.len(), 1);
assert_eq!(publications[0].agent.acp_command.as_deref(), projected);
assert_eq!(
persona.acp_command.as_deref(),
command,
"publication must not mutate local state"
);
}
}