@@ -19,31 +19,76 @@ use uuid::Uuid;
1919
2020use crate :: filter:: SubscriptionRule ;
2121
22- /// Immutable conversation binding for a remote workspace.
22+ /// Immutable channel-thread or direct-message binding for a remote workspace.
2323#[ derive( Debug , Clone , serde:: Deserialize ) ]
24- #[ serde( deny_unknown_fields) ]
25- pub struct BoundSession {
26- pub channel_id : Uuid ,
27- pub thread_root : String ,
24+ #[ serde( untagged, deny_unknown_fields) ]
25+ pub enum BoundSession {
26+ /// One published thread in a channel.
27+ Thread {
28+ channel_id : Uuid ,
29+ thread_root : String ,
30+ } ,
31+ /// One complete direct-message conversation.
32+ DirectMessage { dm_channel_id : Uuid } ,
2833}
2934
3035impl BoundSession {
36+ /// The only channel admitted by this workspace.
37+ pub fn channel_id ( & self ) -> Uuid {
38+ match self {
39+ Self :: Thread { channel_id, .. } => * channel_id,
40+ Self :: DirectMessage { dm_channel_id } => * dm_channel_id,
41+ }
42+ }
43+
44+ /// The published root for a channel thread; DMs have no root.
45+ pub fn thread_root ( & self ) -> Option < & str > {
46+ match self {
47+ Self :: Thread { thread_root, .. } => Some ( thread_root) ,
48+ Self :: DirectMessage { .. } => None ,
49+ }
50+ }
51+
52+ pub ( crate ) fn validate_channel_metadata (
53+ & self ,
54+ info : Option < & crate :: relay:: ChannelInfo > ,
55+ ) -> Result < ( ) , ConfigError > {
56+ if matches ! ( self , Self :: DirectMessage { .. } )
57+ && info. is_none_or ( |info| info. channel_type != "dm" )
58+ {
59+ return Err ( ConfigError :: ConfigFile (
60+ "bound direct-message workspace requires verified DM channel metadata" . into ( ) ,
61+ ) ) ;
62+ }
63+ Ok ( ( ) )
64+ }
65+
66+ /// Whether a routed event belongs to this immutable scope.
3167 pub fn accepts ( & self , scope : & crate :: scope:: SessionScope ) -> bool {
32- scope. channel_id ( ) == self . channel_id && scope. root_event_id ( ) == Some ( & self . thread_root )
68+ match self {
69+ Self :: Thread {
70+ channel_id,
71+ thread_root,
72+ } => scope. channel_id ( ) == * channel_id && scope. root_event_id ( ) == Some ( thread_root) ,
73+ Self :: DirectMessage { dm_channel_id } => {
74+ scope. channel_id ( ) == * dm_channel_id && !scope. is_thread ( )
75+ }
76+ }
3377 }
3478}
3579
3680fn parse_bound_session ( value : & str ) -> Result < BoundSession , String > {
3781 let binding: BoundSession = serde_json:: from_str ( value) . map_err ( |_| {
38- "bound session requires a channel UUID and canonical thread root " . to_string ( )
82+ "bound session requires a channel thread or a direct-message channel UUID " . to_string ( )
3983 } ) ?;
40- if binding. thread_root . len ( ) != 64
41- || !binding
42- . thread_root
43- . bytes ( )
44- . all ( |b| b. is_ascii_hexdigit ( ) && !b. is_ascii_uppercase ( ) )
45- {
46- return Err ( "bound session thread_root must be a lowercase event ID" . into ( ) ) ;
84+ if let Some ( thread_root) = binding. thread_root ( ) {
85+ if thread_root. len ( ) != 64
86+ || !thread_root
87+ . bytes ( )
88+ . all ( |b| b. is_ascii_hexdigit ( ) && !b. is_ascii_uppercase ( ) )
89+ {
90+ return Err ( "bound session thread_root must be a lowercase event ID" . into ( ) ) ;
91+ }
4792 }
4893 Ok ( binding)
4994}
@@ -997,11 +1042,21 @@ impl Config {
9971042 /// `propagate_legacy_env_vars()` before the tokio runtime starts (in the
9981043 /// sync `fn main()` wrapper) — see Rust 2024 edition safety.
9991044 pub fn from_args ( mut args : CliArgs ) -> Result < Self , ConfigError > {
1000- if args. bound_session . is_some ( ) {
1045+ if let Some ( binding) = & args. bound_session {
1046+ let direct_message = matches ! ( binding, BoundSession :: DirectMessage { .. } ) ;
1047+ let subscription = if direct_message {
1048+ SubscribeMode :: All
1049+ } else {
1050+ SubscribeMode :: Mentions
1051+ } ;
10011052 let refusal = if args. respond_to != RespondTo :: Owner {
10021053 Some ( "--respond-to owner" )
1003- } else if args. subscribe != SubscribeMode :: Mentions {
1004- Some ( "--subscribe mentions" )
1054+ } else if args. subscribe != subscription {
1055+ Some ( if direct_message {
1056+ "--subscribe all"
1057+ } else {
1058+ "--subscribe mentions"
1059+ } )
10051060 } else if args. no_mention_filter {
10061061 Some ( "no --no-mention-filter" )
10071062 } else if args. session_policy != crate :: scope:: SessionPolicy :: Thread {
@@ -1019,6 +1074,12 @@ impl Config {
10191074 ) ) ) ;
10201075 }
10211076 }
1077+ if matches ! ( args. bound_session, Some ( BoundSession :: DirectMessage { .. } ) )
1078+ && args. kinds . is_none ( )
1079+ {
1080+ // Every owner message may wake a bound DM, but auxiliary events may not.
1081+ args. kinds = Some ( default_mention_kinds ( ) ) ;
1082+ }
10221083 let keys = Keys :: parse ( & args. private_key ) ?;
10231084 // Best-effort zeroize: overwrite the raw private key string to reduce
10241085 // exposure via core dumps or heap inspection (#41). Without the `zeroize`
@@ -1693,6 +1754,126 @@ mod tests {
16931754 }
16941755 }
16951756
1757+ #[ test]
1758+ fn a_bound_dm_requires_verified_metadata_and_owner_only_subscription ( ) {
1759+ let channel = Uuid :: new_v4 ( ) ;
1760+ let binding = serde_json:: json!( { "dm_channel_id" : channel} ) . to_string ( ) ;
1761+ let parsed = parse_bound_session ( & binding) . unwrap ( ) ;
1762+ assert ! ( parsed. validate_channel_metadata( None ) . is_err( ) ) ;
1763+ for ( kind, accepted) in [ ( "dm" , true ) , ( "stream" , false ) , ( "" , false ) ] {
1764+ let info = crate :: relay:: ChannelInfo {
1765+ name : "test" . into ( ) ,
1766+ channel_type : kind. into ( ) ,
1767+ description : None ,
1768+ } ;
1769+ assert_eq ! (
1770+ parsed. validate_channel_metadata( Some ( & info) ) . is_ok( ) ,
1771+ accepted
1772+ ) ;
1773+ }
1774+ for invalid in [
1775+ serde_json:: json!( { "dm_channel_id" : channel, "thread_root" : "a" . repeat( 64 ) } ) ,
1776+ serde_json:: json!( { "dm_channel_id" : channel, "channel_id" : channel} ) ,
1777+ serde_json:: json!( { "dm_channel_id" : "invalid" } ) ,
1778+ ] {
1779+ assert ! ( parse_bound_session( & invalid. to_string( ) ) . is_err( ) ) ;
1780+ }
1781+ let key = Keys :: generate ( ) . secret_key ( ) . to_secret_hex ( ) ;
1782+ let owner = Keys :: generate ( ) . public_key ( ) . to_hex ( ) ;
1783+ let args = || {
1784+ CliArgs :: try_parse_from ( [
1785+ "buzz-acp" ,
1786+ "--private-key" ,
1787+ & key,
1788+ "--agent-owner" ,
1789+ & owner,
1790+ "--bound-session" ,
1791+ & binding,
1792+ "--respond-to" ,
1793+ "owner" ,
1794+ "--subscribe" ,
1795+ "all" ,
1796+ "--session-policy" ,
1797+ "thread" ,
1798+ "--heartbeat-interval" ,
1799+ "0" ,
1800+ ] )
1801+ . unwrap ( )
1802+ } ;
1803+ assert ! ( Config :: from_args( args( ) ) . is_ok( ) ) ;
1804+ let mut mentions = args ( ) ;
1805+ mentions. subscribe = SubscribeMode :: Mentions ;
1806+ assert ! ( Config :: from_args( mentions) . is_err( ) ) ;
1807+ let mut siblings = args ( ) ;
1808+ siblings. respond_to = RespondTo :: OwnerOnly ;
1809+ assert ! ( Config :: from_args( siblings) . is_err( ) ) ;
1810+ }
1811+
1812+ #[ tokio:: test]
1813+ async fn bound_dm_kind_defaults_reach_relay_and_local_filters ( ) {
1814+ let keys = Keys :: generate ( ) ;
1815+ let key = keys. secret_key ( ) . to_secret_hex ( ) ;
1816+ let owner = keys. public_key ( ) . to_hex ( ) ;
1817+ let channel = Uuid :: new_v4 ( ) ;
1818+ let binding = serde_json:: json!( { "dm_channel_id" : channel} ) . to_string ( ) ;
1819+ for ( bound, override_kinds, expected_kinds, message, reaction, deletion) in [
1820+ (
1821+ true ,
1822+ None ,
1823+ Some ( default_mention_kinds ( ) ) ,
1824+ true ,
1825+ false ,
1826+ false ,
1827+ ) ,
1828+ ( true , Some ( vec ! [ 7 ] ) , Some ( vec ! [ 7 ] ) , false , true , false ) ,
1829+ ( false , None , None , true , true , true ) ,
1830+ ] {
1831+ let mut args = CliArgs :: try_parse_from ( [
1832+ "buzz-acp" ,
1833+ "--private-key" ,
1834+ & key,
1835+ "--agent-owner" ,
1836+ & owner,
1837+ "--bound-session" ,
1838+ & binding,
1839+ "--respond-to" ,
1840+ "owner" ,
1841+ "--subscribe" ,
1842+ "all" ,
1843+ "--session-policy" ,
1844+ "thread" ,
1845+ "--heartbeat-interval" ,
1846+ "0" ,
1847+ ] )
1848+ . unwrap ( ) ;
1849+ if !bound {
1850+ args. bound_session = None ;
1851+ }
1852+ args. kinds = override_kinds;
1853+ let config = Config :: from_args ( args) . unwrap ( ) ;
1854+ let rules = crate :: startup_subscription_rules ( & config) . unwrap ( ) ;
1855+ let relay = resolve_channel_filters ( & config, & [ channel] , & rules) ;
1856+ assert_eq ! ( relay[ & channel] . kinds, expected_kinds) ;
1857+ assert ! ( !relay[ & channel] . require_mention) ;
1858+ for ( kind, admitted) in [
1859+ ( KIND_STREAM_MESSAGE as u16 , message) ,
1860+ ( 7 , reaction) ,
1861+ ( 5 , deletion) ,
1862+ ] {
1863+ let event = nostr:: EventBuilder :: new ( nostr:: Kind :: Custom ( kind) , "plain DM event" )
1864+ . sign_with_keys ( & keys)
1865+ . unwrap ( ) ;
1866+ assert_eq ! (
1867+ crate :: filter:: match_event( & event, channel, & rules, & "b" . repeat( 64 ) )
1868+ . await
1869+ . is_some( ) ,
1870+ admitted,
1871+ "bound={bound}, kind={kind}"
1872+ ) ;
1873+ }
1874+ }
1875+ }
1876+
16961877 #[ test]
16971878 fn bound_session_requires_explicit_owner_mentions_without_autonomous_prompts ( ) {
16981879 let key = Keys :: generate ( ) . secret_key ( ) . to_secret_hex ( ) ;
0 commit comments