@@ -60,6 +60,7 @@ export function useAgentAddressLockPicker({
6060 mentions,
6161 onAddressAgentMention,
6262 onAutoPinAgentMention,
63+ onImplicitPrefixInserted,
6364 onPulseAddressLock,
6465 profiles,
6566 richText,
@@ -73,6 +74,8 @@ export function useAgentAddressLockPicker({
7374 suggestion : MentionSuggestion ,
7475 options : { reinstateExcluded : boolean } ,
7576 ) => void ;
77+ /** Records the exact automatic prefix at the moment it is inserted. */
78+ onImplicitPrefixInserted ?: ( prefix : string ) => void ;
7679 onPulseAddressLock : ( pubkey : string ) => void ;
7780 profiles ?: UserProfileLookup ;
7881 richText : UseRichTextEditorResult ;
@@ -241,10 +244,12 @@ export function useAgentAddressLockPicker({
241244 } ) ;
242245 const { text } = richText . getPlainTextAndCursor ( ) ;
243246 if ( getMentionOffsets ( text , suggestion . displayName ) . length === 0 ) {
247+ const insertedText = `@${ suggestion . displayName } ` ;
248+ onImplicitPrefixInserted ?.( insertedText ) ;
244249 applyAutocompleteEdit ( {
245250 replaceFromOffset : 0 ,
246251 replaceToOffset : 0 ,
247- insertText : `@ ${ suggestion . displayName } ` ,
252+ insertText : insertedText ,
248253 preserveSelection : text . length > 0 ,
249254 reassertMentionCaret : false ,
250255 } ) ;
@@ -297,6 +302,7 @@ export function useAgentAddressLockPicker({
297302 mentions . openMentionPicker ,
298303 mentions . registerMentionPubkey ,
299304 onAddressAgentMention ,
305+ onImplicitPrefixInserted ,
300306 onPulseAddressLock ,
301307 removeAddressedAgentMentions ,
302308 richText . getPlainTextAndCursor ,
@@ -386,8 +392,21 @@ export function useAgentAddressLockPicker({
386392 return { pubkey, displayName } ;
387393 } ) ;
388394 const { text } = richText . getPlainTextAndCursor ( ) ;
395+ // A profile can rename an agent while this draft is off-screen. Mention
396+ // refs retain the identity of its already-inserted automatic prefix, so
397+ // use that identity as well as the current display name when deciding
398+ // whether restoration is needed.
399+ const presentAgentPubkeys = new Set (
400+ mentions
401+ . getDraftMentionRefs ( text )
402+ . filter ( ( ref ) => ref . isAgent )
403+ . map ( ( ref ) => normalizePubkey ( ref . pubkey ) ) ,
404+ ) ;
389405 for ( const agent of targetAgents ) {
390- if ( getMentionOffsets ( text , agent . displayName ) . length > 0 ) {
406+ if (
407+ presentAgentPubkeys . has ( agent . pubkey ) ||
408+ getMentionOffsets ( text , agent . displayName ) . length > 0
409+ ) {
391410 mentions . registerMentionPubkey ( agent . displayName , agent . pubkey , {
392411 isAgent : true ,
393412 } ) ;
@@ -398,6 +417,7 @@ export function useAgentAddressLockPicker({
398417 ( agent ) =>
399418 ( ! unpinnedAgentPubkeysRef . current . has ( agent . pubkey ) ||
400419 allowedUnpinned . has ( agent . pubkey ) ) &&
420+ ! presentAgentPubkeys . has ( agent . pubkey ) &&
401421 getMentionOffsets ( text , agent . displayName ) . length === 0 ,
402422 ) ;
403423 if ( missingAgents . length === 0 ) return text ;
@@ -410,6 +430,7 @@ export function useAgentAddressLockPicker({
410430 const insertedText = `${ missingAgents
411431 . map ( ( agent ) => `@${ agent . displayName } ` )
412432 . join ( " " ) } `;
433+ onImplicitPrefixInserted ?.( insertedText ) ;
413434 applyAutocompleteEdit ( {
414435 replaceFromOffset : 0 ,
415436 replaceToOffset : 0 ,
@@ -426,8 +447,10 @@ export function useAgentAddressLockPicker({
426447 [
427448 applyAutocompleteEdit ,
428449 audience . pubkeys ,
450+ mentions . getDraftMentionRefs ,
429451 mentions . getMentionDisplayName ,
430452 mentions . registerMentionPubkey ,
453+ onImplicitPrefixInserted ,
431454 profiles ,
432455 richText . focusEnd ,
433456 richText . getPlainTextAndCursor ,
0 commit comments