Skip to content

Commit a134af9

Browse files
jhedlundEngineerclaude
committed
feat: shared thread titles on NIP-AR artifacts
A thread's title is a kind-45010 artifact with type buzz.thread-title (the name proposed upstream in #6739): - d = UUIDv5(namespace, "buzz.thread-title:" + root). Folding the type into the name means a future type change gets fresh ids, since NIP-AR never reuses a deleted d. - h = channel, root = thread root, title tag. The title is also copied into content for readers that ignore tags; relay search does not index kind 45010, so titles are not searchable yet. Clearing is op=delete; setting again after a clear is restore. The convention lives in buzz_sdk::thread_title (type, namespace, head selection, write planning, revision builder) so the CLI and the desktop write byte-identical artifacts. CLI: `buzz threads title set|clear|get|list`. Writes plan create/update/delete/restore from the current head and re-read on head conflicts. `set --if-unset` leaves an existing or cleared title alone. buzz-acp: when a human's new top-level channel message mentions agents, the first-mentioned agent is asked to title the thread with --if-unset. Replies, edits, DMs and agent-authored roots never trigger it. Desktop, through Tauri commands (artifact queries are HTTP-only on the relay): - The thread panel header shows the title with an inline editor (pencil, Enter saves, Escape cancels, blank clears). Edits carry the revision they started from, so a concurrent rename is reported, not overwritten. - The title sits on its own line under the author of a thread root in the channel (clickable, opens the thread) and on Inbox rows and the Inbox detail header. - Titles are looked up per root by their derived d, in batches of 100, and lookups made in the same tick share one query, so lookups stay exact however many titles exist. A returned title only counts when its d matches its root. - Artifact events in the open channel refresh titles live. Co-authored-by: Engineer <f79d52bb0d4ab12c7affaf2235c113a7fc797a9da8f9707757123b67cfafa6c6@buzz.matrixsoftwareinc.com> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Jeff Hedlund <[email protected]>
1 parent 8216db1 commit a134af9

32 files changed

Lines changed: 2352 additions & 8 deletions

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11209,6 +11209,22 @@ mod edit_native_steer_tests {
1120911209
assert!(steer.is_none(), "no native steer without a recorded prompt");
1121011210
assert_eq!(control, Some(ControlSignal::Steer));
1121111211
}
11212+
11213+
/// A new top-level channel message opens its own thread, so it is never
11214+
/// steered natively into a running turn. Thread auto-titling relies on
11215+
/// this: the ask lives in `<context>`, which only the cancel+merge
11216+
/// re-prompt carries.
11217+
#[tokio::test]
11218+
async fn new_channel_thread_during_running_turn_cancels_and_merges() {
11219+
let root = "ab".repeat(32);
11220+
for running in [message(None), message(Some(&root))] {
11221+
let (steer, control) =
11222+
steer_into_running_turn(false, Some(false), running, message(None), None);
11223+
11224+
assert!(steer.is_none(), "no native steer for a new thread");
11225+
assert_eq!(control, Some(ControlSignal::Steer));
11226+
}
11227+
}
1121211228
}
1121311229

1121411230
#[cfg(test)]

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3126,6 +3126,7 @@ pub async fn run_prompt_task(
31263126
);
31273127
}
31283128

3129+
let agent_pubkey = ctx.agent_keys.public_key().to_hex();
31293130
crate::queue::format_prompt(
31303131
b,
31313132
&crate::queue::FormatPromptArgs {
@@ -3141,6 +3142,7 @@ pub async fn run_prompt_task(
31413142
team_instructions: standing.team_instructions,
31423143
agent_canvas: standing.agent_canvas,
31433144
standing_context_sent,
3145+
agent_pubkey: Some(&agent_pubkey),
31443146
},
31453147
)
31463148
} else {

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

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,63 @@ fn append_new_thread_reply_instruction(s: &mut String, event_id: &str) {
17191719
));
17201720
}
17211721

1722+
/// Ask the agent to name the thread a human just started by mentioning it.
1723+
///
1724+
/// `--if-unset` makes the write create-only, so a title the human (or another
1725+
/// agent) already set is never replaced, and concurrent agents cannot clobber
1726+
/// each other.
1727+
fn append_auto_title_instruction(s: &mut String, channel_id: Uuid, event_id: &str) {
1728+
s.push_str(&format!(
1729+
"\nTHREAD TITLE: You are the first agent mentioned in this new thread, \
1730+
so name it. Once you understand the request, run \
1731+
`buzz threads title set --channel {channel_id} --event {event_id} \
1732+
--if-unset --title \"<title>\"` with a short, specific title \
1733+
(about 3–8 words, no trailing punctuation) describing what the thread \
1734+
is about. Do this once, without mentioning it in your reply. If the \
1735+
command reports \"already titled\", leave the existing title alone."
1736+
));
1737+
}
1738+
1739+
/// Whether this agent should auto-title the thread the triggering event starts.
1740+
///
1741+
/// Only a human's new top-level message qualifies (never a reply, an edit, or
1742+
/// a DM), and only for the first agent it mentions, so a later mention inside
1743+
/// an existing thread never renames it. Pubkeys whose profile is unknown are
1744+
/// skipped rather than treated as agents; if one was an agent too, the
1745+
/// create-only write in [`append_auto_title_instruction`] settles the race.
1746+
fn should_auto_title(
1747+
be: &BatchEvent,
1748+
thread_tags: &ThreadTags,
1749+
is_dm: bool,
1750+
agent_pubkey: Option<&str>,
1751+
profile_lookup: Option<&PromptProfileLookup>,
1752+
) -> bool {
1753+
let Some(agent_pubkey) = agent_pubkey else {
1754+
return false;
1755+
};
1756+
if is_dm || thread_tags.root_event_id.is_some() || edit_target_id(&be.event).is_some() {
1757+
return false;
1758+
}
1759+
let is_agent = |pubkey: &str| {
1760+
profile_lookup
1761+
.and_then(|m| m.get(&normalize_lookup_key(pubkey)))
1762+
.is_some_and(|p| p.is_agent)
1763+
};
1764+
if is_agent(&be.event.pubkey.to_hex()) {
1765+
return false;
1766+
}
1767+
let me = normalize_lookup_key(agent_pubkey);
1768+
for pubkey in &thread_tags.mentioned_pubkeys {
1769+
if normalize_lookup_key(pubkey) == me {
1770+
return true;
1771+
}
1772+
if is_agent(pubkey) {
1773+
return false;
1774+
}
1775+
}
1776+
false
1777+
}
1778+
17221779
/// Decide whether a turn is human-facing for reply-anchor purposes.
17231780
///
17241781
/// A turn is human-facing when the triggering sender is a human, OR a human
@@ -1955,6 +2012,7 @@ fn format_context_hints(
19552012
is_dm: bool,
19562013
conversation_context_status: ConversationContextStatus,
19572014
reply_anchor: Option<&str>,
2015+
auto_title_event: Option<&str>,
19582016
) -> String {
19592017
let channel_id = scope.channel_id();
19602018
let channel_display = match channel_info {
@@ -2051,6 +2109,9 @@ fn format_context_hints(
20512109
append_new_thread_reply_instruction(&mut s, event_id);
20522110
}
20532111
}
2112+
if let Some(event_id) = auto_title_event {
2113+
append_auto_title_instruction(&mut s, channel_id, event_id);
2114+
}
20542115
crate::prompt_framing::semantic_section("context", &s)
20552116
} else {
20562117
let mut s = format!(
@@ -2066,6 +2127,9 @@ fn format_context_hints(
20662127
if let Some(event_id) = reply_anchor {
20672128
append_new_thread_reply_instruction(&mut s, event_id);
20682129
}
2130+
if let Some(event_id) = auto_title_event {
2131+
append_auto_title_instruction(&mut s, channel_id, event_id);
2132+
}
20692133
crate::prompt_framing::semantic_section("context", &s)
20702134
}
20712135
}
@@ -2231,6 +2295,9 @@ pub struct FormatPromptArgs<'a> {
22312295
/// Defaults to `false` so a caller that never sets it behaves as if this
22322296
/// were the session's first message.
22332297
pub standing_context_sent: bool,
2298+
/// This agent's own pubkey (hex). Required to recognize when it is the
2299+
/// first agent mentioned in a new thread; `None` disables auto-titling.
2300+
pub agent_pubkey: Option<&'a str>,
22342301
}
22352302

22362303
/// The prompt sections that do not change for the life of a session: base
@@ -2401,6 +2468,14 @@ pub fn format_prompt(batch: &FlushBatch, args: &FormatPromptArgs<'_>) -> Vec<Str
24012468
args.profile_lookup,
24022469
)
24032470
};
2471+
let auto_title_event = should_auto_title(
2472+
last_event,
2473+
&thread_tags,
2474+
is_dm,
2475+
args.agent_pubkey,
2476+
args.profile_lookup,
2477+
)
2478+
.then_some(routing_event_id.as_str());
24042479
sections.push(format_context_hints(
24052480
&batch.scope,
24062481
args.channel_info,
@@ -2412,6 +2487,7 @@ pub fn format_prompt(batch: &FlushBatch, args: &FormatPromptArgs<'_>) -> Vec<Str
24122487
args.conversation_context_had_session_events,
24132488
),
24142489
reply_anchor.as_deref(),
2490+
auto_title_event,
24152491
));
24162492

24172493
// 3. Conversation context (thread or DM).
@@ -7408,4 +7484,143 @@ mod tests {
74087484
"unresolved metadata must not render a Description field; got: {prompt}"
74097485
);
74107486
}
7487+
7488+
fn auto_title_batch(channel_id: Uuid, tags: Vec<nostr::Tag>) -> FlushBatch {
7489+
let event = EventBuilder::new(Kind::Custom(9), "@Agent please help")
7490+
.tags(tags)
7491+
.sign_with_keys(&Keys::generate())
7492+
.unwrap();
7493+
FlushBatch {
7494+
channel_id,
7495+
scope: conv(channel_id),
7496+
events: vec![BatchEvent {
7497+
edit: None,
7498+
event,
7499+
prompt_tag: "@mention".into(),
7500+
received_at: Instant::now(),
7501+
}],
7502+
cancelled_events: vec![],
7503+
cancel_reason: None,
7504+
}
7505+
}
7506+
7507+
fn p_tag(pubkey: &str) -> nostr::Tag {
7508+
nostr::Tag::parse(["p", pubkey]).unwrap()
7509+
}
7510+
7511+
fn agent_profiles(agents: &[&str], humans: &[&str]) -> PromptProfileLookup {
7512+
agents
7513+
.iter()
7514+
.map(|pk| (pk, true))
7515+
.chain(humans.iter().map(|pk| (pk, false)))
7516+
.map(|(pk, is_agent)| {
7517+
(
7518+
pk.to_string(),
7519+
PromptProfile {
7520+
is_agent,
7521+
..Default::default()
7522+
},
7523+
)
7524+
})
7525+
.collect()
7526+
}
7527+
7528+
fn auto_title_prompt(batch: &FlushBatch, me: &str, profiles: &PromptProfileLookup) -> String {
7529+
format_prompt(
7530+
batch,
7531+
&FormatPromptArgs {
7532+
profile_lookup: Some(profiles),
7533+
agent_pubkey: Some(me),
7534+
..Default::default()
7535+
},
7536+
)
7537+
.join("\n\n")
7538+
}
7539+
7540+
#[test]
7541+
fn first_mentioned_agent_is_asked_to_title_a_new_thread() {
7542+
let ch = Uuid::new_v4();
7543+
let (me, other, human) = ("a".repeat(64), "b".repeat(64), "c".repeat(64));
7544+
// A human mention ahead of the agents doesn't count; agent order does.
7545+
let batch = auto_title_batch(ch, vec![p_tag(&human), p_tag(&me), p_tag(&other)]);
7546+
let profiles = agent_profiles(&[&me, &other], &[&human]);
7547+
let event_id = batch.events[0].event.id.to_hex();
7548+
7549+
let mine = auto_title_prompt(&batch, &me, &profiles);
7550+
assert!(mine.contains("THREAD TITLE:"), "got: {mine}");
7551+
assert!(mine.contains(&format!(
7552+
"buzz threads title set --channel {ch} --event {event_id} --if-unset"
7553+
)));
7554+
let theirs = auto_title_prompt(&batch, &other, &profiles);
7555+
assert!(
7556+
!theirs.contains("THREAD TITLE:"),
7557+
"only the first agent titles"
7558+
);
7559+
}
7560+
7561+
#[test]
7562+
fn unknown_profile_ahead_of_me_does_not_block_auto_title() {
7563+
let ch = Uuid::new_v4();
7564+
let (me, unknown) = ("a".repeat(64), "d".repeat(64));
7565+
let batch = auto_title_batch(ch, vec![p_tag(&unknown), p_tag(&me)]);
7566+
let prompt = auto_title_prompt(&batch, &me, &agent_profiles(&[&me], &[]));
7567+
assert!(prompt.contains("THREAD TITLE:"));
7568+
}
7569+
7570+
#[test]
7571+
fn replies_agent_authors_dms_and_unmentioned_agents_never_auto_title() {
7572+
let ch = Uuid::new_v4();
7573+
let me = "a".repeat(64);
7574+
let profiles = agent_profiles(&[&me], &[]);
7575+
7576+
// A mention inside an existing thread never (re)names it.
7577+
let root = "f".repeat(64);
7578+
let reply = auto_title_batch(
7579+
ch,
7580+
vec![
7581+
nostr::Tag::parse(["e", root.as_str(), "", "reply"]).unwrap(),
7582+
p_tag(&me),
7583+
],
7584+
);
7585+
assert!(!auto_title_prompt(&reply, &me, &profiles).contains("THREAD TITLE:"));
7586+
7587+
// Not mentioned at all.
7588+
let unmentioned = auto_title_batch(ch, vec![]);
7589+
assert!(!auto_title_prompt(&unmentioned, &me, &profiles).contains("THREAD TITLE:"));
7590+
7591+
// Started by another agent rather than a human.
7592+
let agent_batch = auto_title_batch(ch, vec![p_tag(&me)]);
7593+
let author = agent_batch.events[0].event.pubkey.to_hex();
7594+
let with_agent_author = agent_profiles(&[&me, &author], &[]);
7595+
assert!(!auto_title_prompt(&agent_batch, &me, &with_agent_author).contains("THREAD TITLE:"));
7596+
7597+
// DMs have no thread list to name.
7598+
let dm_info = PromptChannelInfo {
7599+
name: "dm".into(),
7600+
channel_type: "dm".into(),
7601+
..Default::default()
7602+
};
7603+
let dm = format_prompt(
7604+
&agent_batch,
7605+
&FormatPromptArgs {
7606+
channel_info: Some(&dm_info),
7607+
profile_lookup: Some(&profiles),
7608+
agent_pubkey: Some(&me),
7609+
..Default::default()
7610+
},
7611+
)
7612+
.join("\n\n");
7613+
assert!(!dm.contains("THREAD TITLE:"));
7614+
7615+
// No identity wired in → feature off.
7616+
let off = format_prompt(
7617+
&agent_batch,
7618+
&FormatPromptArgs {
7619+
profile_lookup: Some(&profiles),
7620+
..Default::default()
7621+
},
7622+
)
7623+
.join("\n\n");
7624+
assert!(!off.contains("THREAD TITLE:"));
7625+
}
74117626
}

‎crates/buzz-cli/src/commands/messages.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ fn thread_ref_from_parent_tags(
5151
/// - Nested reply: `root` is the parent's own root marker; `parent` is unchanged.
5252
///
5353
/// Ensures CLI-sent replies thread correctly using the same NIP-10 logic.
54-
async fn fetch_event(client: &BuzzClient, event_id: &str) -> Result<serde_json::Value, CliError> {
54+
pub(crate) async fn fetch_event(
55+
client: &BuzzClient,
56+
event_id: &str,
57+
) -> Result<serde_json::Value, CliError> {
5558
let filter = serde_json::json!({ "ids": [event_id], "limit": 1 });
5659
let raw = client.query(&filter).await?;
5760
let events: serde_json::Value = serde_json::from_str(&raw)

‎crates/buzz-cli/src/commands/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub mod reactions;
2020
mod repo_default_branch;
2121
pub mod repos;
2222
pub mod social;
23+
pub mod threads;
2324
pub mod upload;
2425
pub mod users;
2526
pub mod workflows;

0 commit comments

Comments
 (0)