From 433dd0ffc36090fc3e8d989b52a6052fd4bd2bfd Mon Sep 17 00:00:00 2001 From: Kalvin Chau Date: Thu, 19 Nov 2026 08:06:53 -0800 Subject: [PATCH 01/19] feat(messages): pin and unpin channel messages send pin (40010) and unpin (40011) commands from the message menu and the pinned panel when the relay advertises buzz-pins in nip-11. follow the relay-signed pin list (30623) per channel: read once by #d, then kept current by the channel subscription and its replay. show a pinned marker on rows, render message_pinned notices in the timeline, and add a pinned side panel that loads pins with edits applied and refreshes on a jittered 10 minute backstop. allow the command kinds in the broker, native, credential and app agent signers with a one-channel, one-message shape check, and add a pin tool for agents. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Kalvin Chau --- browser-host/relay-authority.test.mjs | 19 ++ browser-host/relay-broker.mjs | 41 ++++ browser-host/relay-broker.test.mjs | 31 +++ crates/agent-controller/src/app_agent.rs | 14 +- .../agent-controller/src/app_agent/tests.rs | 4 +- docs/buzz-help.md | 5 + src-tauri/src/credential_service/policy.rs | 11 +- src-tauri/src/credential_service/tests.rs | 4 +- src-tauri/src/relay.rs | 24 ++ src-tauri/src/relay/tests.rs | 47 ++++ src/bundled/channels/PinsPanel.module.css | 58 +++++ src/bundled/channels/index.test.tsx | 1 + src/bundled/channels/index.tsx | 2 + src/bundled/channels/pins-panel.tsx | 223 ++++++++++++++++++ src/buzz-mcp/tools.test.ts | 29 +++ src/buzz-mcp/tools.ts | 21 ++ .../messages/MessageManagement.test.tsx | 72 +++++- src/features/messages/MessageManagement.tsx | 55 ++++- src/features/messages/MessageRow.test.tsx | 6 + src/features/messages/MessageRow.tsx | 31 ++- src/features/messages/Messages.module.css | 9 + .../messages/membership-rows.test.tsx | 23 ++ src/features/messages/membership-rows.ts | 16 +- src/features/messages/use-pins.ts | 85 +++++++ src/features/relay/channel-kinds.ts | 2 +- src/features/relay/contracts.ts | 17 +- src/features/relay/event-access.ts | 3 +- src/features/relay/live.ts | 2 +- src/features/relay/membership.test.ts | 35 +++ src/features/relay/membership.ts | 9 + src/features/relay/messages.ts | 16 ++ src/features/relay/native.test.ts | 10 + src/features/relay/native.ts | 11 +- src/features/relay/pins.test.ts | 174 ++++++++++++++ src/features/relay/pins.ts | 204 ++++++++++++++++ src/shared/design-system/icons/index.ts | 4 + 36 files changed, 1285 insertions(+), 33 deletions(-) create mode 100644 src/bundled/channels/PinsPanel.module.css create mode 100644 src/bundled/channels/pins-panel.tsx create mode 100644 src/features/messages/use-pins.ts create mode 100644 src/features/relay/pins.test.ts create mode 100644 src/features/relay/pins.ts diff --git a/browser-host/relay-authority.test.mjs b/browser-host/relay-authority.test.mjs index f2a1905fd0..061caf9b71 100644 --- a/browser-host/relay-authority.test.mjs +++ b/browser-host/relay-authority.test.mjs @@ -80,6 +80,25 @@ it.each([ } }, ); +it.each([ + [undefined, false], + [["nip-ar"], false], + [["buzz-pins"], true], +])( + "offers pin commands only with a buzz-pins advertisement: %j", + async (supported_extensions, supported) => { + const h = await harness(() => + Response.json({ self, supported_extensions }), + ); + try { + const transport = await h.connect(); + expect(transport.writer.kinds.includes(40010)).toBe(supported); + expect(transport.writer.kinds.includes(40011)).toBe(supported); + } finally { + await h.close(); + } + }, +); it.each([ ["explicit self", { self, pubkey: contact }, self, self], ["self without contact", { self }, self, self], diff --git a/browser-host/relay-broker.mjs b/browser-host/relay-broker.mjs index d1c72f4cbc..02357b8d10 100644 --- a/browser-host/relay-broker.mjs +++ b/browser-host/relay-broker.mjs @@ -361,6 +361,10 @@ async function relayAuthority(fetch, relay) { searchSort: Array.isArray(nip11.supported_extensions) && nip11.supported_extensions.includes("buzz-search-sort"), + // Pin and unpin commands; the relay keeps the pin list either way. + pins: + Array.isArray(nip11.supported_extensions) && + nip11.supported_extensions.includes("buzz-pins"), ...(readSnapshotCommunity(nip11.read_state_snapshot) ? { readStateCommunity: readSnapshotCommunity(nip11.read_state_snapshot) } : {}), @@ -575,6 +579,36 @@ export function validReport(event) { ].includes(target[2]) ); } +/** A pin (40010) or unpin (40011): one channel and one message, as native + * `valid_pin_command`; the relay decides who may pin what. */ +export function validPinCommand(event) { + if ( + ![40010, 40011].includes(event?.kind) || + event.content !== "" || + !Number.isSafeInteger(event.created_at) || + event.created_at < 0 || + !Array.isArray(event.tags) || + !event.tags.every( + (tag) => + Array.isArray(tag) && + tag.length === 2 && + tag.every((value) => typeof value === "string") && + ["h", "e", "client-id"].includes(tag[0]), + ) + ) + return false; + const named = (name) => event.tags.filter((tag) => tag[0] === name); + const [channel] = named("h"); + const [target] = named("e"); + return ( + named("h").length === 1 && + channel[1].length > 0 && + channel[1].length <= 256 && + named("e").length === 1 && + /^[0-9a-f]{64}$/.test(target[1]) && + named("client-id").length <= 1 + ); +} /** Channel-local NIP-09 removal; the relay enforces authorship of each target. */ export function validMessageDeletion(event) { if ( @@ -1737,6 +1771,7 @@ export function relayBrokerPlugin({ ...((await getAuthority(relay)).artifactSupport ? [45010] : []), ...WORKFLOW_KINDS, ...((await getAuthority(relay)).channelCreation ? [9007] : []), + ...((await getAuthority(relay)).pins ? [40010, 40011] : []), ], channelLifecycle: true, channelDetails: true, @@ -3172,6 +3207,12 @@ export function relayBrokerPlugin({ error: "Report rejected", sent: false, }); + } else if ([40010, 40011].includes(filters?.kind)) { + if (!validPinCommand(filters)) + return json(res, 400, { + error: "Invalid pin command", + sent: false, + }); } else if (filters?.kind === 20002) { if (!validTypingTemplate(filters)) return json(res, 400, { diff --git a/browser-host/relay-broker.test.mjs b/browser-host/relay-broker.test.mjs index ac3cacf050..8e1143f18a 100644 --- a/browser-host/relay-broker.test.mjs +++ b/browser-host/relay-broker.test.mjs @@ -4,6 +4,7 @@ import { createUpstream, validFilters, validMessageTemplate, + validPinCommand, } from "./relay-broker.mjs"; test("read broker accepts non-channel finite reads without relaxing filter budgets", () => { @@ -230,3 +231,33 @@ test("workflow list batches alone may exceed four filters, with 128 unique chann ]) assert.equal(validFilters(invalid), false); }); + +test("pin commands name one channel and one message, with only a client id besides", () => { + const pin = { + kind: 40010, + created_at: 1, + content: "", + tags: [ + ["h", "channel"], + ["e", "a".repeat(64)], + ["client-id", "local"], + ], + }; + assert.equal(validPinCommand(pin), true); + assert.equal(validPinCommand({ ...pin, kind: 40011 }), true); + for (const invalid of [ + { ...pin, kind: 9 }, + { ...pin, content: "x" }, + { ...pin, tags: pin.tags.slice(1) }, + { ...pin, tags: [pin.tags[0], pin.tags[2]] }, + { ...pin, tags: [...pin.tags, ["h", "other"]] }, + { ...pin, tags: [...pin.tags, ["e", "b".repeat(64)]] }, + { ...pin, tags: [...pin.tags, ["client-id", "again"]] }, + { ...pin, tags: [...pin.tags, ["p", "c".repeat(64)]] }, + { ...pin, tags: [["h", ""], pin.tags[1]] }, + { ...pin, tags: [["h", "x".repeat(257)], pin.tags[1]] }, + { ...pin, tags: [pin.tags[0], ["e", "A".repeat(64)]] }, + { ...pin, tags: [pin.tags[0], ["e", "a".repeat(64), "relay"]] }, + ]) + assert.equal(validPinCommand(invalid), false); +}); diff --git a/crates/agent-controller/src/app_agent.rs b/crates/agent-controller/src/app_agent.rs index 632e6c9717..c105dc78ef 100644 --- a/crates/agent-controller/src/app_agent.rs +++ b/crates/agent-controller/src/app_agent.rs @@ -15,13 +15,13 @@ use std::path::{Path, PathBuf}; /// (9), a channel member change (9000, 9001), edit (9002), creation (9007), /// deletion (9008), join (9021) or leave (9022), typing (20002), an edit of its /// own message (40003), a shared note (30023), a channel canvas (40100), a -/// request to open a DM (41010), a repository announcement (30617) or an -/// artifact revision (45010). The relay decides which channel changes and -/// artifact edits it may make. Its profile (0) is the app's, and -/// its memory (30174) is written by `memory`. -const KINDS: [u16; 17] = [ - 5, 7, 9, 9000, 9001, 9002, 9007, 9008, 9021, 9022, 20002, 30023, 30617, 40003, 40100, 41010, - 45010, +/// pin or unpin (40010, 40011), a request to open a DM (41010), a repository +/// announcement (30617) or an artifact revision (45010). The relay decides +/// which channel changes, pins and artifact edits it may make. Its profile (0) +/// is the app's, and its memory (30174) is written by `memory`. +const KINDS: [u16; 19] = [ + 5, 7, 9, 9000, 9001, 9002, 9007, 9008, 9021, 9022, 20002, 30023, 30617, 40003, 40010, 40011, + 40100, 41010, 45010, ]; /// Content of a live channel Canvas (40100), in UTF-8 bytes. Mirrors diff --git a/crates/agent-controller/src/app_agent/tests.rs b/crates/agent-controller/src/app_agent/tests.rs index b9869f7fe5..98e45d74f9 100644 --- a/crates/agent-controller/src/app_agent/tests.rs +++ b/crates/agent-controller/src/app_agent/tests.rs @@ -406,8 +406,8 @@ fn signs_only_bounded_kinds_with_the_saved_attestation() { let key = agent.read_key(&credentials).unwrap(); let saved: Vec = serde_json::from_str(&agent.auth).unwrap(); for kind in [ - 5, 7, 9, 9000, 9001, 9002, 9007, 9008, 9021, 9022, 20002, 30023, 30617, 40003, 40100, - 41010, 45010, + 5, 7, 9, 9000, 9001, 9002, 9007, 9008, 9021, 9022, 20002, 30023, 30617, 40003, 40010, + 40011, 40100, 41010, 45010, ] { let tags = vec![ vec!["auth".to_owned(), "forged".to_owned()], diff --git a/docs/buzz-help.md b/docs/buzz-help.md index f74ba87c2d..ea575357a8 100644 --- a/docs/buzz-help.md +++ b/docs/buzz-help.md @@ -96,6 +96,11 @@ if that desktop app is up and running. - **Messages.** Hover or right-click a message to react, **Reply**, copy a link, forward it, or start a chat about it. On an agent's reply, **View activity** shows what it did. +- **Pins.** **Pin to channel** in a message's More menu adds it to the + channel's pin list, up to 250; the pin button in the header lists them. + Anyone who can post can pin or unpin any message. Forum posts can't be + pinned. A community that doesn't take pins refuses them, and Buzz says so. + Agents pin with the `pin` tool. - **Composer.** **#** links a channel. On desktop, **!** gives your agents a folder and **/** a skill. Attach files by pasting or dragging them in. - **Drafts.** Every message box open on a conversation shares its draft. A box diff --git a/src-tauri/src/credential_service/policy.rs b/src-tauri/src/credential_service/policy.rs index 65257a04b6..c952b9a61b 100644 --- a/src-tauri/src/credential_service/policy.rs +++ b/src-tauri/src/credential_service/policy.rs @@ -14,9 +14,10 @@ pub(super) enum Verdict { } /// Everyday work: messages and their edits, pins, bookmarks, schedules and -/// reminders (9, 40002–40008), reactions (7), shared notes (30023), forum posts, -/// votes and comments (45001–45003), presence and typing (20001–20003), read -/// state (30078) and user status (30315), the identity's own lists (10000, +/// reminders (9, 40002–40008), channel pin commands (40010, 40011), reactions +/// (7), shared notes (30023), forum posts, votes and comments (45001–45003), +/// presence and typing (20001–20003), read state (30078) and user status +/// (30315), the identity's own lists (10000, /// 10001, 10003, 10030, 30000, 30003, 30030), canvases (40100), artifacts /// (45010), git patches, pull requests, issues and statuses (1617–1633), DM /// open and hide (41010, 41012), archive requests (9035, 9036), workflow @@ -25,8 +26,8 @@ pub(super) enum Verdict { const ORDINARY: &[u16] = &[ 7, 9, 1617, 1618, 1619, 1621, 1630, 1631, 1632, 1633, 9035, 9036, 10000, 10001, 10003, 10030, 20001, 20002, 20003, 24200, 30000, 30003, 30023, 30030, 30078, 30174, 30315, 40002, 40003, - 40004, 40005, 40006, 40007, 40008, 40100, 41010, 41012, 43001, 43002, 43003, 43004, 43005, - 43006, 45001, 45002, 45003, 45010, 46020, + 40004, 40005, 40006, 40007, 40008, 40010, 40011, 40100, 41010, 41012, 43001, 43002, 43003, + 43004, 43005, 43006, 45001, 45002, 45003, 45010, 46020, ]; /// Changes to who the identity is, what it controls or what others see about diff --git a/src-tauri/src/credential_service/tests.rs b/src-tauri/src/credential_service/tests.rs index 81c752365f..6155decb84 100644 --- a/src-tauri/src/credential_service/tests.rs +++ b/src-tauri/src/credential_service/tests.rs @@ -950,7 +950,9 @@ fn the_policy_binds_each_proof_to_the_community() { #[test] fn every_kind_has_one_class() { let classes = |kind| classify(kind, &[], NOW, NOW, RELAY); - for kind in [7, 9, 30174, 20001, 24200, 40002, 41010, 45010, 46020, 1621] { + for kind in [ + 7, 9, 30174, 20001, 24200, 40002, 40010, 40011, 41010, 45010, 46020, 1621, + ] { assert_eq!(classes(kind), Verdict::Sign, "{kind}"); } for kind in [ diff --git a/src-tauri/src/relay.rs b/src-tauri/src/relay.rs index 4905aa80d2..aca55a213b 100644 --- a/src-tauri/src/relay.rs +++ b/src-tauri/src/relay.rs @@ -466,6 +466,10 @@ fn validate_event(community: &str, event: &EventTemplate) -> Result<()> { { return Err("Invalid agent presence".into()); } + } else if matches!(event.kind, 40010 | 40011) { + if !valid_pin_command(event) { + return Err("Invalid pin command".into()); + } } else if matches!(event.kind, 9030..=9032) { if !valid_member_command(event) { return Err("Invalid member change".into()); @@ -596,6 +600,26 @@ fn valid_reaction_deletion(event: &EventTemplate) -> bool { .any(|tag| tag[0] == "e" && hex_key(&tag[1])) } +/** A pin (40010) or unpin (40011): one channel and one message, as the + * broker's `validPinCommand`; the relay decides who may pin what. */ +fn valid_pin_command(event: &EventTemplate) -> bool { + let tag = |name: &str| { + event + .tags + .iter() + .filter(|tag| tag.first().map(String::as_str) == Some(name)) + .collect::>() + }; + event.content.is_empty() + && event + .tags + .iter() + .all(|tag| tag.len() == 2 && matches!(tag[0].as_str(), "h" | "e" | "client-id")) + && matches!(tag("h").as_slice(), [h] if !h[1].is_empty() && h[1].len() <= 256) + && matches!(tag("e").as_slice(), [e] if hex_key(&e[1])) + && tag("client-id").len() <= 1 +} + /** Keep the shared kind-5 writer aligned with the broker's channel-local deletion shape. */ fn valid_message_deletion(event: &EventTemplate) -> bool { if event.kind != 5 diff --git a/src-tauri/src/relay/tests.rs b/src-tauri/src/relay/tests.rs index 6d03ca0dbc..201d1f0009 100644 --- a/src-tauri/src/relay/tests.rs +++ b/src-tauri/src/relay/tests.rs @@ -5167,3 +5167,50 @@ fn huddle_end_requires_one_parent_and_a_distinct_room() { assert!(validate_event("https://relay.test", &event).is_err()); } } + +#[test] +fn pin_commands_name_one_channel_and_one_message() { + let pin = |tags: Vec>| EventTemplate { + kind: 40010, + created_at: 1, + content: "".into(), + tags: tags + .into_iter() + .map(|tag| tag.into_iter().map(str::to_owned).collect()) + .collect(), + }; + let target = "a".repeat(64); + let valid = vec![ + vec!["h", "channel"], + vec!["e", &target], + vec!["client-id", "x"], + ]; + assert!(validate_event("https://relay.test", &pin(valid.clone())).is_ok()); + let mut unpin = pin(valid.clone()); + unpin.kind = 40011; + assert!(validate_event("https://relay.test", &unpin).is_ok()); + let mut content = pin(valid.clone()); + content.content = "x".into(); + assert!(validate_event("https://relay.test", &content).is_err()); + let upper = "A".repeat(64); + let long = "x".repeat(257); + for tags in [ + vec![vec!["e", &target]], + vec![vec!["h", "channel"]], + vec![vec!["h", "channel"], vec!["h", "other"], vec!["e", &target]], + vec![vec!["h", "channel"], vec!["e", &target], vec!["e", &target]], + vec![vec!["h", "channel"], vec!["e", &upper]], + vec![vec!["h", ""], vec!["e", &target]], + vec![vec!["h", &long], vec!["e", &target]], + vec![vec!["h", "channel"], vec!["e", &target, "relay"]], + vec![vec!["h", "channel"], vec!["e", &target], vec!["p", &target]], + vec![ + vec!["h", "channel"], + vec!["e", &target], + vec!["client-id", "x"], + vec!["client-id", "y"], + ], + ] { + assert!(validate_event("https://relay.test", &pin(tags)).is_err()); + } +} diff --git a/src/bundled/channels/PinsPanel.module.css b/src/bundled/channels/PinsPanel.module.css new file mode 100644 index 0000000000..2d3845c202 --- /dev/null +++ b/src/bundled/channels/PinsPanel.module.css @@ -0,0 +1,58 @@ +@reference "../../shared/styles/globals.css"; + +.panel { + display: flex; + flex-direction: column; + height: 100%; + min-height: 0; +} +.body { + flex: 1; + min-height: 0; + overflow-y: auto; + padding: var(--space-3); +} +.status { + padding: var(--space-3); + color: var(--text-secondary); +} +.list { + display: flex; + flex-direction: column; + gap: var(--space-1); +} +.row { + display: flex; + align-items: flex-start; + gap: var(--space-2); +} +.open { + display: flex; + flex: 1; + min-width: 0; + flex-direction: column; + gap: var(--space-1); + padding: var(--space-2); + border-radius: var(--radius-md); + text-align: start; +} +.open:hover { + background: var(--affordance-panel-hover); +} +html[data-keyboard-navigation] .open:focus-visible { + outline: 2px solid var(--border-focus); + outline-offset: -2px; +} +.byline { + display: flex; + align-items: baseline; + gap: var(--space-2); +} +.preview { + display: -webkit-box; + -webkit-line-clamp: 3; + -webkit-box-orient: vertical; + overflow: hidden; + overflow-wrap: anywhere; + color: var(--text-secondary); +} diff --git a/src/bundled/channels/index.test.tsx b/src/bundled/channels/index.test.tsx index d255b4d622..ed79a58678 100644 --- a/src/bundled/channels/index.test.tsx +++ b/src/bundled/channels/index.test.tsx @@ -3,6 +3,7 @@ import { afterEach, expect, it, vi } from "vitest"; import { apply } from "./index"; vi.mock("./canvas-panel", () => ({ registerCanvas: () => {} })); +vi.mock("./pins-panel", () => ({ registerPins: () => {} })); afterEach(() => { document.body.innerHTML = ""; diff --git a/src/bundled/channels/index.tsx b/src/bundled/channels/index.tsx index bbd4599245..5d235bce82 100644 --- a/src/bundled/channels/index.tsx +++ b/src/bundled/channels/index.tsx @@ -11,6 +11,7 @@ import type { PluginModule } from "../../plugins/api"; import { ChannelsPage } from "./ChannelsPage"; import { ConversationWorkspace, PageTabs } from "./PageTabs"; import { registerCanvas } from "./canvas-panel"; +import { registerPins } from "./pins-panel"; import { ChannelTemplateSettings } from "./ChannelTemplateSettings"; import { ChatCircleIcon, CopyIcon } from "../../shared/design-system/icons"; import { isApplePlatform } from "../../features/shortcuts/format"; @@ -66,6 +67,7 @@ export const apply: PluginModule["apply"] = (ctx) => { ctx.reflect.get("sidebarSections")?.registerSource?.(soloSidebarSource); ctx.reflect.get("sidebarSections")?.registerSource?.(teamSidebarSource); registerCanvas(ctx); + registerPins(ctx); ctx.conversation.registerCompletion({ id: "channel-typeahead", title: "Channel", diff --git a/src/bundled/channels/pins-panel.tsx b/src/bundled/channels/pins-panel.tsx new file mode 100644 index 0000000000..44f4bdf2db --- /dev/null +++ b/src/bundled/channels/pins-panel.tsx @@ -0,0 +1,223 @@ +import { useState, useSyncExternalStore } from "react"; +import type { Context } from "@deepseek-ai/cordis"; +import type { + ChannelLauncherProps, + PanelProps, +} from "../../features/panels/service"; +import type { ChannelMessage } from "../../features/relay/contracts"; +import { useListedChannel } from "../../features/relay/listed-channel"; +import { diffPreview } from "../../features/relay/membership"; +import { PIN_KIND } from "../../features/relay/pins"; +import { useRelayConnection, useRowProfiles } from "../../features/relay/react"; +import type { RelayData } from "../../features/relay/service"; +import type { RelaySession } from "../../features/relay/session"; +import { useChannelIdentityNames } from "../../features/identity-names/react"; +import { MessageTimestamp } from "../../features/messages/MessageTimestamp"; +import { + usePinnedMessages, + usePinnedRows, +} from "../../features/messages/use-pins"; +import { PinIcon, PinOffIcon, XIcon } from "../../shared/design-system/icons"; +import { Button } from "../../shared/design-system/ui/Button"; +import { EmptyState } from "../../shared/design-system/ui/EmptyState"; +import { IconButton } from "../../shared/design-system/ui/IconButton"; +import { PanelHeader } from "../../shared/design-system/ui/PanelHeader"; +import styles from "./PinsPanel.module.css"; + +function Launcher({ + context, + pressed, + toggle, + relay, +}: ChannelLauncherProps & { relay: RelayData }) { + const connection = useRelayConnection(relay); + // Only where the relay advertises pins (NIP-11 `buzz-pins`). + if ( + connection.scope !== context.scope || + !connection.session.outbox?.supports(PIN_KIND) + ) + return null; + return ( +