@@ -360,19 +360,26 @@ fn event_has_client_marker(event: &Event, marker: &str) -> bool {
360360
361361async fn find_managed_agent_channel_message_by_marker (
362362 state : & AppState ,
363- agent_pubkey : & str ,
363+ agent_pubkey : Option < & str > ,
364364 channel_id : & str ,
365365 marker : & str ,
366366) -> Result < Option < Event > , String > {
367+ let author = agent_pubkey
368+ . map ( str:: trim)
369+ . filter ( |pubkey| !pubkey. is_empty ( ) )
370+ . map ( str:: to_ascii_lowercase) ;
371+
367372 let mut until: Option < u64 > = None ;
368373
369374 for _ in 0 ..10 {
370375 let mut filter = serde_json:: json!( {
371- "authors" : [ agent_pubkey] ,
372376 "kinds" : [ buzz_core_pkg:: kind:: KIND_STREAM_MESSAGE ] ,
373377 "#h" : [ channel_id] ,
374378 "limit" : 500 ,
375379 } ) ;
380+ if let Some ( author) = author. as_deref ( ) {
381+ filter[ "authors" ] = serde_json:: json!( [ author] ) ;
382+ }
376383 if let Some ( until) = until {
377384 filter[ "until" ] = serde_json:: json!( until) ;
378385 }
@@ -401,6 +408,16 @@ async fn find_managed_agent_channel_message_by_marker(
401408 Ok ( None )
402409}
403410
411+ fn marker_author_for_scope < ' a > (
412+ marker_scope : Option < & str > ,
413+ agent_pubkey : & ' a str ,
414+ ) -> Option < & ' a str > {
415+ match marker_scope {
416+ Some ( "channel" ) => None ,
417+ _ => Some ( agent_pubkey) ,
418+ }
419+ }
420+
404421fn stored_managed_agent_auth_tag ( auth_tag : Option < & str > ) -> Option < String > {
405422 auth_tag
406423 . map ( str:: trim)
@@ -440,6 +457,7 @@ pub async fn send_managed_agent_channel_message(
440457 channel_id : String ,
441458 content : String ,
442459 marker : Option < String > ,
460+ marker_scope : Option < String > ,
443461 app : AppHandle ,
444462 state : State < ' _ , AppState > ,
445463) -> Result < SendChannelMessageResponse , String > {
@@ -480,7 +498,7 @@ pub async fn send_managed_agent_channel_message(
480498 if let Some ( marker) = marker. as_deref ( ) {
481499 if let Some ( existing) = find_managed_agent_channel_message_by_marker (
482500 & state,
483- & record. pubkey ,
501+ marker_author_for_scope ( marker_scope . as_deref ( ) , & record. pubkey ) ,
484502 & channel_id,
485503 marker,
486504 )
@@ -526,6 +544,30 @@ pub async fn send_managed_agent_channel_message(
526544mod tests {
527545 use super :: * ;
528546
547+ #[ test]
548+ fn marker_author_scope_defaults_to_agent ( ) {
549+ assert_eq ! (
550+ marker_author_for_scope( None , "agent-pubkey" ) ,
551+ Some ( "agent-pubkey" )
552+ ) ;
553+ assert_eq ! (
554+ marker_author_for_scope( Some ( "agent" ) , "agent-pubkey" ) ,
555+ Some ( "agent-pubkey" )
556+ ) ;
557+ assert_eq ! (
558+ marker_author_for_scope( Some ( "unknown" ) , "agent-pubkey" ) ,
559+ Some ( "agent-pubkey" )
560+ ) ;
561+ }
562+
563+ #[ test]
564+ fn marker_author_scope_can_dedupe_across_channel ( ) {
565+ assert_eq ! (
566+ marker_author_for_scope( Some ( "channel" ) , "agent-pubkey" ) ,
567+ None
568+ ) ;
569+ }
570+
529571 #[ test]
530572 fn stored_managed_agent_auth_tag_trims_blank_values ( ) {
531573 assert_eq ! (
0 commit comments