Skip to content

Commit b58de7c

Browse files
tellahoCarl
andauthored
fix(desktop-messages): preserve inline agent mentions with persistent addressing (#6793)
**Category:** fix **User Impact:** Users can place `@Agent` mentions inline anywhere in a message—even when that agent is also persistently addressed—so separate agents can receive separate instructions in the same message. **Problem:** Selecting an agent from the composer mention picker could turn the selection into persistent addressing instead of leaving an inline `@Agent` at the cursor. That made persistent addressing and inline composition mutually exclusive: once the persistent behavior took over, users lost the clear `@Agent A do X, @agent B do Y` message structure they previously had. **Solution:** Make inline mentioning and persistent addressing independent behaviors: - Ordinary mention selections always insert `@Agent` inline at the current cursor position. - This remains true when the agent is already persistently addressed; the persistent audience never blocks or replaces an inline mention. - If **Automatically mention agents** is enabled, a successfully sent inline mention may additionally make that agent persistent for later messages. Existing saved preferences remain respected. - The dedicated automatic-mention control and primary+Shift+Enter shortcut continue to add or remove persistent addressing directly. - For users without a saved preference, **Automatically mention agents** now defaults off. <details> <summary>File changes</summary> **desktop/src/features/messages/lib/autoPinMentionedAgentsPreference.test.mjs** Updates preference coverage for the default-off behavior while preserving explicit saved choices. **desktop/src/features/messages/lib/autoPinMentionedAgentsPreference.ts** Defaults automatic post-send persistence off when no valid preference exists. **desktop/src/features/messages/ui/useAgentAddressLockPicker.test.mjs** Verifies ordinary picker selections insert inline mentions without changing or pulsing the persistent audience, including when the selected agent is already persistently addressed. **desktop/src/features/messages/ui/useAgentAddressLockPicker.ts** Keeps ordinary mention selection on the existing inline insertion path while preserving the separate persistent-address controls and shortcut. **desktop/tests/e2e/persistent-agent-audience.spec.ts** Covers separately targeted inline instructions to two agents, signed outgoing recipients, default-off persistence, and explicit opt-in persistence scenarios. </details> ## Reproduction steps ### Inline mentions without persistence 1. Open a channel with at least two available agents and leave **Automatically mention agents** disabled. 2. Use the composer mention picker to insert the first agent, type an instruction, then insert a second agent and type a different instruction. 3. Confirm the draft reads like `@Agent A review this, @agent B test that` and neither agent appears in the persistent addressed-agent controls. 4. Send the message and confirm both agents are recipients while neither remains persistently addressed for the next draft. ### Inline mentions with persistence 1. Enable **Automatically mention agents**, then mention an agent inline and send successfully. 2. Confirm that agent becomes persistently addressed for later messages. 3. In a new draft, select the same agent from the mention picker again. 4. Confirm a new inline `@Agent` is inserted at the cursor while the agent remains persistently addressed. 5. Confirm the dedicated automatic-mention control and primary+Shift+Enter shortcut can still add or remove persistent addressing directly. ## Demo - **Before:** Persistent addressing could consume an ordinary picker selection, preventing users from placing that agent inline in the message. - **After:** Ordinary selection always produces an inline `@Agent`; persistence is a separate optional behavior that can coexist with inline mentions. ### Related issue N/A — reported through the Buzz feature room. ### Testing At `a4579c0a666f3644abe44271614186d7b9356bf5`: - Post-push hooks passed desktop check, desktop typecheck, and the full desktop unit suite. - The two persistence-focused Playwright smoke scenarios passed after explicitly opting into **Automatically mention agents**. - The one-time multi-agent targeting and persistent-address shortcut Playwright scenarios passed on the feature changes before the final rebase; CI is validating the rebased branch. --------- Signed-off-by: Taylor Ho <[email protected]> Co-authored-by: Carl <acda9e433d19dcd0e6b6840f7f4b98f3a56f1fab98049d444c087019e6d36560@buzz.block.builderlab.xyz>
1 parent f249710 commit b58de7c

8 files changed

Lines changed: 372 additions & 55 deletions

‎desktop/src/features/messages/lib/autoPinMentionedAgentsPreference.test.mjs‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ globalThis.localStorage = {
99

1010
const preference = await import("./autoPinMentionedAgentsPreference.ts");
1111

12-
test("defaults missing and invalid values to keeping mentioned agents pinned", () => {
13-
assert.equal(preference.parseKeepMentionedAgentsPinned(null), true);
14-
assert.equal(preference.parseKeepMentionedAgentsPinned("invalid"), true);
12+
test("defaults missing and invalid values to one-time agent mentions", () => {
13+
assert.equal(preference.parseKeepMentionedAgentsPinned(null), false);
14+
assert.equal(preference.parseKeepMentionedAgentsPinned("invalid"), false);
1515
assert.equal(preference.parseKeepMentionedAgentsPinned("true"), true);
1616
assert.equal(preference.parseKeepMentionedAgentsPinned("false"), false);
1717
});
1818

1919
test("persists changes to the post-mention pinning preference", () => {
20-
preference.setKeepMentionedAgentsPinned(false);
21-
assert.equal(preference.getKeepMentionedAgentsPinned(), false);
20+
preference.setKeepMentionedAgentsPinned(true);
21+
assert.equal(preference.getKeepMentionedAgentsPinned(), true);
2222
assert.equal(
2323
values.get(preference.KEEP_MENTIONED_AGENTS_PINNED_STORAGE_KEY),
24-
"false",
24+
"true",
2525
);
2626

27-
preference.setKeepMentionedAgentsPinned(true);
28-
assert.equal(preference.getKeepMentionedAgentsPinned(), true);
27+
preference.setKeepMentionedAgentsPinned(false);
28+
assert.equal(preference.getKeepMentionedAgentsPinned(), false);
2929
assert.equal(
3030
values.get(preference.KEEP_MENTIONED_AGENTS_PINNED_STORAGE_KEY),
31-
"true",
31+
"false",
3232
);
3333
});

‎desktop/src/features/messages/lib/autoPinMentionedAgentsPreference.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from "react";
22

33
export const KEEP_MENTIONED_AGENTS_PINNED_STORAGE_KEY =
44
"buzz.messages.keepMentionedAgentsPinned";
5-
export const DEFAULT_KEEP_MENTIONED_AGENTS_PINNED = true;
5+
export const DEFAULT_KEEP_MENTIONED_AGENTS_PINNED = false;
66

77
const listeners = new Set<() => void>();
88
let keepMentionedAgentsPinned = readStoredPreference();

‎desktop/src/features/messages/lib/useDrafts.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ function storageKey(): string {
114114
: legacyStorageKey();
115115
}
116116

117+
export function getDraftStoreScope(): string {
118+
return storageKey();
119+
}
120+
117121
function legacyStorageKey(): string {
118122
return `${LEGACY_DRAFT_STORE_KEY_PREFIX}:${currentPubkey}`;
119123
}

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

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -170,32 +170,33 @@ function MessageComposerImpl({
170170
media.queuedAttachmentsRef.current.length === 0;
171171
const ownsDropZone = mediaController === undefined;
172172
const backgroundUpload = useBackgroundMediaUpload();
173-
useDraftPersistLifecycle({
174-
effectiveDraftKey,
175-
channelId,
176-
loadDraft: drafts.loadDraft,
177-
persistDraft: drafts.persistDraft,
178-
getMentionRefs: mentions.getDraftMentionRefs,
179-
restoreMentionRefs: mentions.restoreDraftMentionRefs,
180-
livePendingImeta: media.pendingImeta,
181-
setPendingImeta: media.setPendingImeta,
182-
getQueuedAttachments: () => media.queuedAttachmentsRef.current,
183-
saveQueuedAttachmentsForDraft,
184-
clearQueuedAttachments: media.clearQueuedAttachments,
185-
restoreQueuedAttachments: media.restoreQueuedAttachments,
186-
takeQueuedAttachmentsForDraft,
187-
setContent: (content) => {
188-
setComposerContent(content);
189-
richText.setContent(content);
190-
},
191-
clearContent: () => {
192-
setComposerContent("");
193-
richText.clearContent();
194-
},
195-
setSpoileredAttachmentUrls,
196-
spoileredAttachmentUrlsRef,
197-
syncComposerContentFromEditor,
198-
});
173+
const { trackAuthoredContent: trackDraftAuthoredContent } =
174+
useDraftPersistLifecycle({
175+
effectiveDraftKey,
176+
channelId,
177+
loadDraft: drafts.loadDraft,
178+
persistDraft: drafts.persistDraft,
179+
getMentionRefs: mentions.getDraftMentionRefs,
180+
restoreMentionRefs: mentions.restoreDraftMentionRefs,
181+
livePendingImeta: media.pendingImeta,
182+
setPendingImeta: media.setPendingImeta,
183+
getQueuedAttachments: () => media.queuedAttachmentsRef.current,
184+
saveQueuedAttachmentsForDraft,
185+
clearQueuedAttachments: media.clearQueuedAttachments,
186+
restoreQueuedAttachments: media.restoreQueuedAttachments,
187+
takeQueuedAttachmentsForDraft,
188+
setContent: (content) => {
189+
setComposerContent(content);
190+
richText.setContent(content);
191+
},
192+
clearContent: () => {
193+
setComposerContent("");
194+
richText.clearContent();
195+
},
196+
setSpoileredAttachmentUrls,
197+
spoileredAttachmentUrlsRef,
198+
syncComposerContentFromEditor,
199+
});
199200
// biome-ignore lint/correctness/useExhaustiveDependencies: effectiveDraftKey is the sole trigger
200201
React.useEffect(() => {
201202
media.setUploadState({ status: "idle" });
@@ -272,6 +273,8 @@ function MessageComposerImpl({
272273
onLinkSelectionChange: (info) => onLinkSelectionChangeRef.current?.(info),
273274
onLinkShortcut: () => onLinkShortcutRef.current?.() ?? false,
274275
onUpdate: ({ cursor, linkPreviewContent, text }) => {
276+
trackDraftAuthoredContent(text);
277+
contentRef.current = text;
275278
setComposerContentFromText(text);
276279
setPreviewContent(linkPreviewContent);
277280
if (!isSubmitLockedRef.current && !editTargetRef.current) {

‎desktop/src/features/messages/ui/MessageComposerDraftImagePersist.test.mjs‎

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,225 @@ test("draft_lifecycle_empty_target_clears_stale_mention_refs", async () => {
585585
await handle.unmount();
586586
});
587587

588+
test("draft_lifecycle_persists_an_explicit_clear_before_async_rerender", async () => {
589+
const DRAFT_KEY = "chan-clear-race";
590+
setupStore("pubkey-clear-race");
591+
persistDraftEntry(DRAFT_KEY, "draft text", DRAFT_KEY, [], []);
592+
593+
let editorContent = "";
594+
let trackAuthoredContent;
595+
const spoileredRef = { current: new Set() };
596+
597+
function HarnessComposer() {
598+
({ trackAuthoredContent } = useDraftPersistLifecycle({
599+
effectiveDraftKey: DRAFT_KEY,
600+
channelId: DRAFT_KEY,
601+
loadDraft: loadDraftEntry,
602+
persistDraft: persistDraftEntry,
603+
getMentionRefs: () => [],
604+
restoreMentionRefs: () => {},
605+
livePendingImeta: [],
606+
setPendingImeta: () => {},
607+
setContent: (content) => {
608+
editorContent = content;
609+
},
610+
clearContent: () => {
611+
editorContent = "";
612+
},
613+
setSpoileredAttachmentUrls: () => {},
614+
spoileredAttachmentUrlsRef: spoileredRef,
615+
syncComposerContentFromEditor: () => editorContent,
616+
}));
617+
return null;
618+
}
619+
620+
const handle = await mountStrictMode(HarnessComposer);
621+
assert.equal(editorContent, "draft text");
622+
623+
trackAuthoredContent("");
624+
assert.equal(
625+
loadDraftEntry(DRAFT_KEY),
626+
undefined,
627+
"the authoritative update removes the stale body even while editor reads lag",
628+
);
629+
630+
await handle.unmount();
631+
assert.equal(
632+
loadDraftEntry(DRAFT_KEY),
633+
undefined,
634+
"async settlement must not repersist the deleted body",
635+
);
636+
637+
const remounted = await mountStrictMode(HarnessComposer);
638+
assert.equal(editorContent, "", "the deleted body must not be restored");
639+
await remounted.unmount();
640+
});
641+
642+
test("draft_lifecycle_clear_caption_preserves_image_and_spoiler_on_remount", async () => {
643+
const DRAFT_KEY = "chan-clear-caption-image";
644+
setupStore("pubkey-clear-caption-image");
645+
persistDraftEntry(DRAFT_KEY, "caption", DRAFT_KEY, [IMG_A], [IMG_A.url]);
646+
647+
let editorContent = "";
648+
let pendingImeta = [];
649+
let spoileredUrls = new Set();
650+
let trackAuthoredContent;
651+
652+
function HarnessComposer() {
653+
({ trackAuthoredContent } = useDraftPersistLifecycle({
654+
effectiveDraftKey: DRAFT_KEY,
655+
channelId: DRAFT_KEY,
656+
loadDraft: loadDraftEntry,
657+
persistDraft: persistDraftEntry,
658+
getMentionRefs: () => [],
659+
restoreMentionRefs: () => {},
660+
livePendingImeta: pendingImeta,
661+
setPendingImeta: (imeta) => {
662+
pendingImeta = imeta;
663+
},
664+
setContent: (content) => {
665+
editorContent = content;
666+
},
667+
clearContent: () => {
668+
editorContent = "";
669+
},
670+
setSpoileredAttachmentUrls: (urls) => {
671+
spoileredUrls = urls;
672+
},
673+
spoileredAttachmentUrlsRef: {
674+
get current() {
675+
return spoileredUrls;
676+
},
677+
},
678+
syncComposerContentFromEditor: () => editorContent,
679+
}));
680+
return null;
681+
}
682+
683+
const handle = await mountStrictMode(HarnessComposer);
684+
trackAuthoredContent("");
685+
editorContent = "";
686+
await handle.unmount();
687+
688+
const remounted = await mountStrictMode(HarnessComposer);
689+
assert.equal(editorContent, "");
690+
assert.equal(pendingImeta[0]?.url, IMG_A.url);
691+
assert.deepEqual([...spoileredUrls], [IMG_A.url]);
692+
assert.equal(loadDraftEntry(DRAFT_KEY)?.content, "");
693+
await remounted.unmount();
694+
});
695+
696+
test("draft_lifecycle_clear_caption_preserves_queued_file_on_remount", async () => {
697+
const DRAFT_KEY = "chan-clear-caption-file";
698+
setupStore("pubkey-clear-caption-file");
699+
persistDraftEntry(DRAFT_KEY, "caption", DRAFT_KEY, [], []);
700+
const FILE_A = {
701+
file: new File(["report"], "report.pdf", { type: "application/pdf" }),
702+
id: 9,
703+
spoilered: true,
704+
};
705+
706+
let editorContent = "";
707+
let queuedAttachments = [FILE_A];
708+
let trackAuthoredContent;
709+
const spoileredRef = { current: new Set() };
710+
711+
function HarnessComposer() {
712+
({ trackAuthoredContent } = useDraftPersistLifecycle({
713+
effectiveDraftKey: DRAFT_KEY,
714+
channelId: DRAFT_KEY,
715+
loadDraft: loadDraftEntry,
716+
persistDraft: persistDraftEntry,
717+
getMentionRefs: () => [],
718+
restoreMentionRefs: () => {},
719+
livePendingImeta: [],
720+
setPendingImeta: () => {},
721+
getQueuedAttachments: () => queuedAttachments,
722+
saveQueuedAttachmentsForDraft,
723+
clearQueuedAttachments: () => {
724+
queuedAttachments = [];
725+
},
726+
restoreQueuedAttachments: (attachments) => {
727+
queuedAttachments = attachments;
728+
},
729+
takeQueuedAttachmentsForDraft,
730+
setContent: (content) => {
731+
editorContent = content;
732+
},
733+
clearContent: () => {
734+
editorContent = "";
735+
},
736+
setSpoileredAttachmentUrls: () => {},
737+
spoileredAttachmentUrlsRef: spoileredRef,
738+
syncComposerContentFromEditor: () => editorContent,
739+
}));
740+
return null;
741+
}
742+
743+
saveQueuedAttachmentsForDraft(DRAFT_KEY, [FILE_A]);
744+
const handle = await mountStrictMode(HarnessComposer);
745+
trackAuthoredContent("");
746+
editorContent = "";
747+
await handle.unmount();
748+
749+
const remounted = await mountStrictMode(HarnessComposer);
750+
assert.equal(editorContent, "");
751+
assert.equal(queuedAttachments[0]?.file.name, "report.pdf");
752+
assert.equal(queuedAttachments[0]?.spoilered, true);
753+
await remounted.unmount();
754+
});
755+
756+
test("draft_lifecycle_clear_authority_is_scoped_to_relay_and_identity", async () => {
757+
const DRAFT_KEY = "shared-key";
758+
installFreshLocalStorage();
759+
clearAllDrafts();
760+
initDraftStore("pubkey-a", "wss://relay-a.example");
761+
persistDraftEntry(DRAFT_KEY, "workspace A", DRAFT_KEY, [], []);
762+
763+
let editorContent = "";
764+
let trackAuthoredContent;
765+
const spoileredRef = { current: new Set() };
766+
function HarnessComposer() {
767+
({ trackAuthoredContent } = useDraftPersistLifecycle({
768+
effectiveDraftKey: DRAFT_KEY,
769+
channelId: DRAFT_KEY,
770+
loadDraft: loadDraftEntry,
771+
persistDraft: persistDraftEntry,
772+
getMentionRefs: () => [],
773+
restoreMentionRefs: () => {},
774+
livePendingImeta: [],
775+
setPendingImeta: () => {},
776+
setContent: (content) => {
777+
editorContent = content;
778+
},
779+
clearContent: () => {
780+
editorContent = "";
781+
},
782+
setSpoileredAttachmentUrls: () => {},
783+
spoileredAttachmentUrlsRef: spoileredRef,
784+
syncComposerContentFromEditor: () => editorContent,
785+
}));
786+
return null;
787+
}
788+
789+
const workspaceA = await mountStrictMode(HarnessComposer);
790+
trackAuthoredContent("");
791+
editorContent = "";
792+
await workspaceA.unmount();
793+
794+
initDraftStore("pubkey-b", "wss://relay-b.example");
795+
persistDraftEntry(DRAFT_KEY, "workspace B", DRAFT_KEY, [], []);
796+
const workspaceB = await mountStrictMode(HarnessComposer);
797+
assert.equal(editorContent, "workspace B");
798+
await workspaceB.unmount();
799+
assert.equal(loadDraftEntry(DRAFT_KEY)?.content, "workspace B");
800+
801+
initDraftStore("pubkey-a", "wss://relay-a.example");
802+
const workspaceARemount = await mountStrictMode(HarnessComposer);
803+
assert.equal(editorContent, "", "workspace A stale text must stay cleared");
804+
await workspaceARemount.unmount();
805+
});
806+
588807
test("draft_lifecycle_preserves_local_files_across_a_b_a_switch", async () => {
589808
setupStore("pubkey-switch-files");
590809
const FILE_A = {

0 commit comments

Comments
 (0)