cognisagent 0.2.1

Batteries-included agent framework built on cognis and cognisgraph
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
//! Inter-agent communication system for message passing, shared state, and coordination.
//!
//! This module provides the building blocks for agents to communicate with each
//! other through direct messages, pub/sub channels, and shared key-value state.
//!
//! # Components
//!
//! - [`AgentMessage`] -- a structured message with priority, metadata, and reply tracking
//! - [`MessageFilter`] -- declarative filter for selecting messages by criteria
//! - [`Mailbox`] -- per-agent inbox for receiving messages
//! - [`SharedState`] -- shared key-value store with ownership tracking
//! - [`Channel`] -- named pub/sub channel for broadcasting to subscribers
//! - [`CommunicationHub`] -- central router that ties everything together
//!
//! # Example
//!
//! ```rust
//! use cognisagent::communication::*;
//! use serde_json::json;
//!
//! let mut hub = CommunicationHub::new();
//! hub.register_agent("planner");
//! hub.register_agent("executor");
//!
//! let msg = AgentMessage::new("planner", "executor", "task", json!({"action": "search"}));
//! hub.send(msg).unwrap();
//!
//! let mailbox = hub.get_mailbox("executor").unwrap();
//! let messages = mailbox.read();
//! assert_eq!(messages.len(), 1);
//! ```

use std::collections::HashMap;
use std::fmt;
use std::time::Instant;

use serde_json::{json, Value};
use uuid::Uuid;

use crate::agent::DeepAgentError;

/// Result type alias for communication operations.
pub type Result<T> = std::result::Result<T, DeepAgentError>;

// ---------------------------------------------------------------------------
// MessagePriority
// ---------------------------------------------------------------------------

/// Priority level for inter-agent messages.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MessagePriority {
    /// Time-sensitive, should be processed immediately.
    Urgent,
    /// Default priority.
    Normal,
    /// Background or informational, can be deferred.
    Low,
}

impl MessagePriority {
    /// Returns a numeric rank for ordering (higher = more urgent).
    fn rank(self) -> u8 {
        match self {
            Self::Urgent => 2,
            Self::Normal => 1,
            Self::Low => 0,
        }
    }
}

impl PartialOrd for MessagePriority {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for MessagePriority {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.rank().cmp(&other.rank())
    }
}

impl fmt::Display for MessagePriority {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Urgent => write!(f, "Urgent"),
            Self::Normal => write!(f, "Normal"),
            Self::Low => write!(f, "Low"),
        }
    }
}

// ---------------------------------------------------------------------------
// AgentMessage
// ---------------------------------------------------------------------------

/// A message sent between agents.
#[derive(Debug, Clone)]
pub struct AgentMessage {
    /// Unique message identifier.
    pub id: String,
    /// Name of the sending agent.
    pub from: String,
    /// Name of the intended recipient agent.
    pub to: String,
    /// Short description of the message purpose.
    pub subject: String,
    /// Message payload.
    pub body: Value,
    /// Delivery priority.
    pub priority: MessagePriority,
    /// When the message was created.
    pub timestamp: Instant,
    /// If this is a reply, the id of the original message.
    pub reply_to: Option<String>,
    /// Arbitrary key-value metadata.
    pub metadata: HashMap<String, Value>,
}

impl AgentMessage {
    /// Create a new message with default priority and auto-generated id.
    pub fn new(
        from: impl Into<String>,
        to: impl Into<String>,
        subject: impl Into<String>,
        body: Value,
    ) -> Self {
        Self {
            id: Uuid::new_v4().to_string(),
            from: from.into(),
            to: to.into(),
            subject: subject.into(),
            body,
            priority: MessagePriority::Normal,
            timestamp: Instant::now(),
            reply_to: None,
            metadata: HashMap::new(),
        }
    }

    /// Set the message priority (builder pattern).
    pub fn with_priority(mut self, priority: MessagePriority) -> Self {
        self.priority = priority;
        self
    }

    /// Mark this message as a reply to another message (builder pattern).
    pub fn with_reply_to(mut self, id: impl Into<String>) -> Self {
        self.reply_to = Some(id.into());
        self
    }

    /// Add a metadata entry (builder pattern).
    pub fn with_metadata(mut self, key: impl Into<String>, value: Value) -> Self {
        self.metadata.insert(key.into(), value);
        self
    }

    /// Returns `true` if this message is a reply to another.
    pub fn is_reply(&self) -> bool {
        self.reply_to.is_some()
    }

    /// Serialize the message to a JSON [`Value`].
    pub fn to_json(&self) -> Value {
        json!({
            "id": self.id,
            "from": self.from,
            "to": self.to,
            "subject": self.subject,
            "body": self.body,
            "priority": self.priority.to_string(),
            "reply_to": self.reply_to,
            "metadata": self.metadata,
        })
    }
}

// ---------------------------------------------------------------------------
// MessageFilter
// ---------------------------------------------------------------------------

/// Declarative filter for selecting messages by various criteria.
///
/// All configured conditions must match (logical AND). An empty filter matches
/// everything.
#[derive(Debug, Clone, Default)]
pub struct MessageFilter {
    from: Option<String>,
    to: Option<String>,
    subject_contains: Option<String>,
    priority: Option<MessagePriority>,
}

impl MessageFilter {
    /// Create an empty filter that matches all messages.
    pub fn new() -> Self {
        Self::default()
    }

    /// Only match messages from the given agent.
    pub fn from_agent(mut self, name: impl Into<String>) -> Self {
        self.from = Some(name.into());
        self
    }

    /// Only match messages addressed to the given agent.
    pub fn to_agent(mut self, name: impl Into<String>) -> Self {
        self.to = Some(name.into());
        self
    }

    /// Only match messages whose subject contains the given text.
    pub fn subject_contains(mut self, text: impl Into<String>) -> Self {
        self.subject_contains = Some(text.into());
        self
    }

    /// Only match messages with the given priority.
    pub fn priority(mut self, p: MessagePriority) -> Self {
        self.priority = Some(p);
        self
    }

    /// Returns `true` if the message satisfies all configured conditions.
    pub fn matches(&self, msg: &AgentMessage) -> bool {
        if let Some(ref from) = self.from {
            if msg.from != *from {
                return false;
            }
        }
        if let Some(ref to) = self.to {
            if msg.to != *to {
                return false;
            }
        }
        if let Some(ref text) = self.subject_contains {
            if !msg.subject.contains(text.as_str()) {
                return false;
            }
        }
        if let Some(p) = self.priority {
            if msg.priority != p {
                return false;
            }
        }
        true
    }
}

// ---------------------------------------------------------------------------
// Mailbox
// ---------------------------------------------------------------------------

/// Per-agent message inbox.
///
/// Messages are stored in delivery order and can be consumed (drained) or
/// peeked without removal.
#[derive(Debug)]
pub struct Mailbox {
    /// The agent that owns this mailbox.
    owner: String,
    /// Messages waiting to be read.
    inbox: Vec<AgentMessage>,
}

impl Mailbox {
    /// Create a new empty mailbox for the given agent.
    pub fn new(owner: &str) -> Self {
        Self {
            owner: owner.to_string(),
            inbox: Vec::new(),
        }
    }

    /// Deliver a message to this mailbox.
    pub fn deliver(&mut self, msg: AgentMessage) {
        self.inbox.push(msg);
    }

    /// Read and consume all messages, draining the inbox.
    pub fn read(&mut self) -> Vec<AgentMessage> {
        std::mem::take(&mut self.inbox)
    }

    /// View all messages without consuming them.
    pub fn peek(&self) -> Vec<&AgentMessage> {
        self.inbox.iter().collect()
    }

    /// Read and consume only messages that match the given filter.
    ///
    /// Non-matching messages remain in the inbox.
    pub fn read_filtered(&mut self, filter: &MessageFilter) -> Vec<AgentMessage> {
        let mut matched = Vec::new();
        let mut remaining = Vec::new();
        for msg in self.inbox.drain(..) {
            if filter.matches(&msg) {
                matched.push(msg);
            } else {
                remaining.push(msg);
            }
        }
        self.inbox = remaining;
        matched
    }

    /// Number of unread messages in the inbox.
    pub fn unread_count(&self) -> usize {
        self.inbox.len()
    }

    /// Returns `true` if the inbox has no messages.
    pub fn is_empty(&self) -> bool {
        self.inbox.is_empty()
    }

    /// The name of the agent that owns this mailbox.
    pub fn owner(&self) -> &str {
        &self.owner
    }
}

// ---------------------------------------------------------------------------
// SharedState
// ---------------------------------------------------------------------------

/// Shared key-value state store with ownership tracking.
///
/// Any agent can read all entries, but writes record which agent set the value.
#[derive(Debug, Default)]
pub struct SharedState {
    data: HashMap<String, Value>,
    owners: HashMap<String, String>,
}

impl SharedState {
    /// Create a new empty shared state.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set a key to a value, recording the owning agent.
    pub fn set(&mut self, key: &str, value: Value, owner: &str) {
        self.data.insert(key.to_string(), value);
        self.owners.insert(key.to_string(), owner.to_string());
    }

    /// Get the value for a key, if it exists.
    pub fn get(&self, key: &str) -> Option<&Value> {
        self.data.get(key)
    }

    /// Get the agent that last set the given key.
    pub fn get_owner(&self, key: &str) -> Option<&str> {
        self.owners.get(key).map(|s| s.as_str())
    }

    /// Remove a key, returning its value if it existed.
    pub fn remove(&mut self, key: &str) -> Option<Value> {
        self.owners.remove(key);
        self.data.remove(key)
    }

    /// All keys currently in the store.
    pub fn keys(&self) -> Vec<&str> {
        self.data.keys().map(|s| s.as_str()).collect()
    }

    /// Number of entries.
    pub fn len(&self) -> usize {
        self.data.len()
    }

    /// Returns `true` if the store is empty.
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    /// Return all entries set by a particular agent.
    pub fn entries_by_owner(&self, owner: &str) -> Vec<(&str, &Value)> {
        self.owners
            .iter()
            .filter(|(_, o)| o.as_str() == owner)
            .filter_map(|(k, _)| self.data.get(k).map(|v| (k.as_str(), v)))
            .collect()
    }
}

// ---------------------------------------------------------------------------
// Channel
// ---------------------------------------------------------------------------

/// A named pub/sub channel for broadcasting messages to subscribed agents.
#[derive(Debug)]
pub struct Channel {
    /// Channel name.
    name: String,
    /// Set of subscribed agent names.
    subscribers: Vec<String>,
}

impl Channel {
    /// Create a new channel with no subscribers.
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            subscribers: Vec::new(),
        }
    }

    /// Subscribe an agent to this channel. No-op if already subscribed.
    pub fn subscribe(&mut self, agent: &str) {
        if !self.subscribers.iter().any(|s| s == agent) {
            self.subscribers.push(agent.to_string());
        }
    }

    /// Unsubscribe an agent from this channel. No-op if not subscribed.
    pub fn unsubscribe(&mut self, agent: &str) {
        self.subscribers.retain(|s| s != agent);
    }

    /// List current subscribers.
    pub fn subscribers(&self) -> Vec<&str> {
        self.subscribers.iter().map(|s| s.as_str()).collect()
    }

    /// Number of subscribers.
    pub fn subscriber_count(&self) -> usize {
        self.subscribers.len()
    }

    /// The name of this channel.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Create a message for each subscriber (excluding the sender).
    ///
    /// Returns the list of generated messages ready for delivery.
    pub fn broadcast(&self, from: &str, subject: &str, body: Value) -> Vec<AgentMessage> {
        self.subscribers
            .iter()
            .filter(|s| s.as_str() != from)
            .map(|s| AgentMessage::new(from, s.as_str(), subject, body.clone()))
            .collect()
    }
}

// ---------------------------------------------------------------------------
// CommunicationHub
// ---------------------------------------------------------------------------

/// Central message router that manages mailboxes, channels, and shared state.
///
/// Agents must be registered before they can send or receive messages.
#[derive(Debug)]
pub struct CommunicationHub {
    mailboxes: HashMap<String, Mailbox>,
    channels: HashMap<String, Channel>,
    shared_state: SharedState,
}

impl CommunicationHub {
    /// Create a new hub with no agents or channels.
    pub fn new() -> Self {
        Self {
            mailboxes: HashMap::new(),
            channels: HashMap::new(),
            shared_state: SharedState::new(),
        }
    }

    /// Register an agent, creating its mailbox.
    ///
    /// If the agent is already registered this is a no-op.
    pub fn register_agent(&mut self, name: &str) {
        self.mailboxes
            .entry(name.to_string())
            .or_insert_with(|| Mailbox::new(name));
    }

    /// Route a message to the recipient's mailbox.
    ///
    /// Returns an error if the recipient is not registered.
    pub fn send(&mut self, msg: AgentMessage) -> Result<()> {
        let to = msg.to.clone();
        match self.mailboxes.get_mut(&to) {
            Some(mailbox) => {
                mailbox.deliver(msg);
                Ok(())
            }
            None => Err(DeepAgentError::Other(format!("unknown recipient: {}", to))),
        }
    }

    /// Broadcast a message to all subscribers of a channel.
    ///
    /// Returns the number of messages delivered, or an error if the channel
    /// does not exist.
    pub fn broadcast(
        &mut self,
        channel: &str,
        from: &str,
        subject: &str,
        body: Value,
    ) -> Result<usize> {
        let messages = match self.channels.get(channel) {
            Some(ch) => ch.broadcast(from, subject, body),
            None => {
                return Err(DeepAgentError::Other(format!(
                    "unknown channel: {}",
                    channel
                )));
            }
        };

        let count = messages.len();
        for msg in messages {
            // Silently skip unregistered recipients (they may have been removed
            // after subscribing to the channel).
            if let Some(mailbox) = self.mailboxes.get_mut(&msg.to) {
                mailbox.deliver(msg);
            }
        }
        Ok(count)
    }

    /// Create a new named channel. No-op if it already exists.
    pub fn create_channel(&mut self, name: &str) {
        self.channels
            .entry(name.to_string())
            .or_insert_with(|| Channel::new(name));
    }

    /// Subscribe an agent to a channel, creating the channel if needed.
    pub fn subscribe(&mut self, channel: &str, agent: &str) {
        self.channels
            .entry(channel.to_string())
            .or_insert_with(|| Channel::new(channel))
            .subscribe(agent);
    }

    /// Get a mutable reference to an agent's mailbox.
    pub fn get_mailbox(&mut self, agent: &str) -> Option<&mut Mailbox> {
        self.mailboxes.get_mut(agent)
    }

    /// Read-only access to shared state.
    pub fn shared_state(&self) -> &SharedState {
        &self.shared_state
    }

    /// Mutable access to shared state.
    pub fn shared_state_mut(&mut self) -> &mut SharedState {
        &mut self.shared_state
    }

    /// Number of registered agents.
    pub fn agent_count(&self) -> usize {
        self.mailboxes.len()
    }

    /// Number of channels.
    pub fn channel_count(&self) -> usize {
        self.channels.len()
    }
}

impl Default for CommunicationHub {
    fn default() -> Self {
        Self::new()
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    // -- AgentMessage tests --

    #[test]
    fn test_message_new_has_id() {
        let msg = AgentMessage::new("a", "b", "hello", json!({}));
        assert!(!msg.id.is_empty());
        assert_eq!(msg.from, "a");
        assert_eq!(msg.to, "b");
        assert_eq!(msg.subject, "hello");
        assert_eq!(msg.priority, MessagePriority::Normal);
        assert!(!msg.is_reply());
    }

    #[test]
    fn test_message_unique_ids() {
        let m1 = AgentMessage::new("a", "b", "s", json!({}));
        let m2 = AgentMessage::new("a", "b", "s", json!({}));
        assert_ne!(m1.id, m2.id);
    }

    #[test]
    fn test_message_with_priority() {
        let msg =
            AgentMessage::new("a", "b", "s", json!({})).with_priority(MessagePriority::Urgent);
        assert_eq!(msg.priority, MessagePriority::Urgent);
    }

    #[test]
    fn test_message_with_reply_to() {
        let original = AgentMessage::new("a", "b", "s", json!({}));
        let reply = AgentMessage::new("b", "a", "re: s", json!({})).with_reply_to(&original.id);
        assert!(reply.is_reply());
        assert_eq!(reply.reply_to.as_deref(), Some(original.id.as_str()));
    }

    #[test]
    fn test_message_with_metadata() {
        let msg = AgentMessage::new("a", "b", "s", json!({}))
            .with_metadata("key", json!("value"))
            .with_metadata("num", json!(42));
        assert_eq!(msg.metadata.get("key"), Some(&json!("value")));
        assert_eq!(msg.metadata.get("num"), Some(&json!(42)));
    }

    #[test]
    fn test_message_is_reply_false() {
        let msg = AgentMessage::new("a", "b", "s", json!({}));
        assert!(!msg.is_reply());
    }

    #[test]
    fn test_message_to_json() {
        let msg = AgentMessage::new("alice", "bob", "greet", json!({"text": "hi"}))
            .with_priority(MessagePriority::Urgent);
        let j = msg.to_json();
        assert_eq!(j["from"], "alice");
        assert_eq!(j["to"], "bob");
        assert_eq!(j["subject"], "greet");
        assert_eq!(j["body"]["text"], "hi");
        assert_eq!(j["priority"], "Urgent");
        assert!(j["reply_to"].is_null());
    }

    #[test]
    fn test_message_to_json_with_reply() {
        let msg = AgentMessage::new("a", "b", "s", json!({})).with_reply_to("orig-123");
        let j = msg.to_json();
        assert_eq!(j["reply_to"], "orig-123");
    }

    // -- MessagePriority tests --

    #[test]
    fn test_priority_ordering() {
        assert!(MessagePriority::Urgent > MessagePriority::Normal);
        assert!(MessagePriority::Normal > MessagePriority::Low);
        assert!(MessagePriority::Urgent > MessagePriority::Low);
    }

    #[test]
    fn test_priority_display() {
        assert_eq!(format!("{}", MessagePriority::Urgent), "Urgent");
        assert_eq!(format!("{}", MessagePriority::Normal), "Normal");
        assert_eq!(format!("{}", MessagePriority::Low), "Low");
    }

    #[test]
    fn test_priority_sort() {
        let mut priorities = vec![
            MessagePriority::Low,
            MessagePriority::Urgent,
            MessagePriority::Normal,
        ];
        priorities.sort();
        assert_eq!(
            priorities,
            vec![
                MessagePriority::Low,
                MessagePriority::Normal,
                MessagePriority::Urgent,
            ]
        );
    }

    // -- MessageFilter tests --

    #[test]
    fn test_filter_empty_matches_all() {
        let filter = MessageFilter::new();
        let msg = AgentMessage::new("a", "b", "s", json!({}));
        assert!(filter.matches(&msg));
    }

    #[test]
    fn test_filter_from_agent() {
        let filter = MessageFilter::new().from_agent("alice");
        let yes = AgentMessage::new("alice", "bob", "s", json!({}));
        let no = AgentMessage::new("carol", "bob", "s", json!({}));
        assert!(filter.matches(&yes));
        assert!(!filter.matches(&no));
    }

    #[test]
    fn test_filter_to_agent() {
        let filter = MessageFilter::new().to_agent("bob");
        let yes = AgentMessage::new("alice", "bob", "s", json!({}));
        let no = AgentMessage::new("alice", "carol", "s", json!({}));
        assert!(filter.matches(&yes));
        assert!(!filter.matches(&no));
    }

    #[test]
    fn test_filter_subject_contains() {
        let filter = MessageFilter::new().subject_contains("urgent");
        let yes = AgentMessage::new("a", "b", "urgent task", json!({}));
        let no = AgentMessage::new("a", "b", "normal task", json!({}));
        assert!(filter.matches(&yes));
        assert!(!filter.matches(&no));
    }

    #[test]
    fn test_filter_priority() {
        let filter = MessageFilter::new().priority(MessagePriority::Urgent);
        let yes =
            AgentMessage::new("a", "b", "s", json!({})).with_priority(MessagePriority::Urgent);
        let no = AgentMessage::new("a", "b", "s", json!({}));
        assert!(filter.matches(&yes));
        assert!(!filter.matches(&no));
    }

    #[test]
    fn test_filter_combined() {
        let filter = MessageFilter::new()
            .from_agent("alice")
            .priority(MessagePriority::Urgent);
        let yes =
            AgentMessage::new("alice", "b", "s", json!({})).with_priority(MessagePriority::Urgent);
        let no_wrong_sender =
            AgentMessage::new("bob", "b", "s", json!({})).with_priority(MessagePriority::Urgent);
        let no_wrong_priority = AgentMessage::new("alice", "b", "s", json!({}));
        assert!(filter.matches(&yes));
        assert!(!filter.matches(&no_wrong_sender));
        assert!(!filter.matches(&no_wrong_priority));
    }

    // -- Mailbox tests --

    #[test]
    fn test_mailbox_new_is_empty() {
        let mb = Mailbox::new("agent1");
        assert!(mb.is_empty());
        assert_eq!(mb.unread_count(), 0);
        assert_eq!(mb.owner(), "agent1");
    }

    #[test]
    fn test_mailbox_deliver_and_read() {
        let mut mb = Mailbox::new("agent1");
        mb.deliver(AgentMessage::new("a", "agent1", "s", json!({})));
        mb.deliver(AgentMessage::new("b", "agent1", "s2", json!({})));
        assert_eq!(mb.unread_count(), 2);

        let messages = mb.read();
        assert_eq!(messages.len(), 2);
        assert!(mb.is_empty());
    }

    #[test]
    fn test_mailbox_peek_does_not_consume() {
        let mut mb = Mailbox::new("agent1");
        mb.deliver(AgentMessage::new("a", "agent1", "s", json!({})));
        let peeked = mb.peek();
        assert_eq!(peeked.len(), 1);
        assert_eq!(mb.unread_count(), 1); // still there
    }

    #[test]
    fn test_mailbox_read_filtered() {
        let mut mb = Mailbox::new("agent1");
        mb.deliver(
            AgentMessage::new("alice", "agent1", "task", json!({}))
                .with_priority(MessagePriority::Urgent),
        );
        mb.deliver(AgentMessage::new("bob", "agent1", "info", json!({})));
        mb.deliver(
            AgentMessage::new("alice", "agent1", "task2", json!({}))
                .with_priority(MessagePriority::Urgent),
        );

        let filter = MessageFilter::new().priority(MessagePriority::Urgent);
        let urgent = mb.read_filtered(&filter);
        assert_eq!(urgent.len(), 2);
        assert_eq!(mb.unread_count(), 1); // the Normal one remains
    }

    #[test]
    fn test_mailbox_read_empty() {
        let mut mb = Mailbox::new("agent1");
        let messages = mb.read();
        assert!(messages.is_empty());
    }

    // -- SharedState tests --

    #[test]
    fn test_shared_state_new_is_empty() {
        let state = SharedState::new();
        assert!(state.is_empty());
        assert_eq!(state.len(), 0);
    }

    #[test]
    fn test_shared_state_set_and_get() {
        let mut state = SharedState::new();
        state.set("key1", json!("value1"), "agent_a");
        assert_eq!(state.get("key1"), Some(&json!("value1")));
        assert_eq!(state.get_owner("key1"), Some("agent_a"));
    }

    #[test]
    fn test_shared_state_overwrite() {
        let mut state = SharedState::new();
        state.set("key1", json!("v1"), "a");
        state.set("key1", json!("v2"), "b");
        assert_eq!(state.get("key1"), Some(&json!("v2")));
        assert_eq!(state.get_owner("key1"), Some("b"));
    }

    #[test]
    fn test_shared_state_remove() {
        let mut state = SharedState::new();
        state.set("key1", json!("v"), "a");
        let removed = state.remove("key1");
        assert_eq!(removed, Some(json!("v")));
        assert!(state.get("key1").is_none());
        assert!(state.get_owner("key1").is_none());
    }

    #[test]
    fn test_shared_state_remove_nonexistent() {
        let mut state = SharedState::new();
        assert!(state.remove("nope").is_none());
    }

    #[test]
    fn test_shared_state_keys_and_len() {
        let mut state = SharedState::new();
        state.set("a", json!(1), "x");
        state.set("b", json!(2), "x");
        assert_eq!(state.len(), 2);
        let mut keys = state.keys();
        keys.sort();
        assert_eq!(keys, vec!["a", "b"]);
    }

    #[test]
    fn test_shared_state_entries_by_owner() {
        let mut state = SharedState::new();
        state.set("k1", json!(1), "alice");
        state.set("k2", json!(2), "bob");
        state.set("k3", json!(3), "alice");

        let alice_entries = state.entries_by_owner("alice");
        assert_eq!(alice_entries.len(), 2);

        let bob_entries = state.entries_by_owner("bob");
        assert_eq!(bob_entries.len(), 1);

        let nobody = state.entries_by_owner("nobody");
        assert!(nobody.is_empty());
    }

    // -- Channel tests --

    #[test]
    fn test_channel_new() {
        let ch = Channel::new("updates");
        assert_eq!(ch.name(), "updates");
        assert_eq!(ch.subscriber_count(), 0);
    }

    #[test]
    fn test_channel_subscribe() {
        let mut ch = Channel::new("ch");
        ch.subscribe("a");
        ch.subscribe("b");
        assert_eq!(ch.subscriber_count(), 2);
        assert_eq!(ch.subscribers(), vec!["a", "b"]);
    }

    #[test]
    fn test_channel_subscribe_idempotent() {
        let mut ch = Channel::new("ch");
        ch.subscribe("a");
        ch.subscribe("a");
        assert_eq!(ch.subscriber_count(), 1);
    }

    #[test]
    fn test_channel_unsubscribe() {
        let mut ch = Channel::new("ch");
        ch.subscribe("a");
        ch.subscribe("b");
        ch.unsubscribe("a");
        assert_eq!(ch.subscriber_count(), 1);
        assert_eq!(ch.subscribers(), vec!["b"]);
    }

    #[test]
    fn test_channel_unsubscribe_nonexistent() {
        let mut ch = Channel::new("ch");
        ch.unsubscribe("x"); // should not panic
        assert_eq!(ch.subscriber_count(), 0);
    }

    #[test]
    fn test_channel_broadcast() {
        let mut ch = Channel::new("ch");
        ch.subscribe("a");
        ch.subscribe("b");
        ch.subscribe("c");

        let msgs = ch.broadcast("a", "news", json!({"data": 1}));
        // sender "a" should be excluded
        assert_eq!(msgs.len(), 2);
        let recipients: Vec<&str> = msgs.iter().map(|m| m.to.as_str()).collect();
        assert!(recipients.contains(&"b"));
        assert!(recipients.contains(&"c"));
        assert_eq!(msgs[0].from, "a");
        assert_eq!(msgs[0].subject, "news");
    }

    #[test]
    fn test_channel_broadcast_no_subscribers() {
        let ch = Channel::new("ch");
        let msgs = ch.broadcast("sender", "s", json!({}));
        assert!(msgs.is_empty());
    }

    // -- CommunicationHub tests --

    #[test]
    fn test_hub_new() {
        let hub = CommunicationHub::new();
        assert_eq!(hub.agent_count(), 0);
        assert_eq!(hub.channel_count(), 0);
    }

    #[test]
    fn test_hub_register_agent() {
        let mut hub = CommunicationHub::new();
        hub.register_agent("a");
        hub.register_agent("b");
        assert_eq!(hub.agent_count(), 2);
    }

    #[test]
    fn test_hub_register_agent_idempotent() {
        let mut hub = CommunicationHub::new();
        hub.register_agent("a");
        hub.register_agent("a");
        assert_eq!(hub.agent_count(), 1);
    }

    #[test]
    fn test_hub_send_message() {
        let mut hub = CommunicationHub::new();
        hub.register_agent("alice");
        hub.register_agent("bob");

        let msg = AgentMessage::new("alice", "bob", "hello", json!({"text": "hi"}));
        hub.send(msg).unwrap();

        let mailbox = hub.get_mailbox("bob").unwrap();
        assert_eq!(mailbox.unread_count(), 1);
        let messages = mailbox.read();
        assert_eq!(messages[0].from, "alice");
        assert_eq!(messages[0].subject, "hello");
    }

    #[test]
    fn test_hub_send_unknown_recipient() {
        let mut hub = CommunicationHub::new();
        hub.register_agent("alice");

        let msg = AgentMessage::new("alice", "nobody", "s", json!({}));
        let result = hub.send(msg);
        assert!(result.is_err());
    }

    #[test]
    fn test_hub_broadcast_channel() {
        let mut hub = CommunicationHub::new();
        hub.register_agent("a");
        hub.register_agent("b");
        hub.register_agent("c");

        hub.create_channel("updates");
        hub.subscribe("updates", "a");
        hub.subscribe("updates", "b");
        hub.subscribe("updates", "c");

        let count = hub
            .broadcast("updates", "a", "news", json!({"v": 1}))
            .unwrap();
        assert_eq!(count, 2); // b and c (not a)

        assert_eq!(hub.get_mailbox("a").unwrap().unread_count(), 0);
        assert_eq!(hub.get_mailbox("b").unwrap().unread_count(), 1);
        assert_eq!(hub.get_mailbox("c").unwrap().unread_count(), 1);
    }

    #[test]
    fn test_hub_broadcast_unknown_channel() {
        let mut hub = CommunicationHub::new();
        let result = hub.broadcast("nonexistent", "a", "s", json!({}));
        assert!(result.is_err());
    }

    #[test]
    fn test_hub_shared_state() {
        let mut hub = CommunicationHub::new();
        hub.shared_state_mut()
            .set("plan", json!({"step": 1}), "planner");
        assert_eq!(hub.shared_state().get("plan"), Some(&json!({"step": 1})));
        assert_eq!(hub.shared_state().get_owner("plan"), Some("planner"));
    }

    #[test]
    fn test_hub_get_mailbox_nonexistent() {
        let mut hub = CommunicationHub::new();
        assert!(hub.get_mailbox("ghost").is_none());
    }

    #[test]
    fn test_hub_create_channel() {
        let mut hub = CommunicationHub::new();
        hub.create_channel("ch1");
        hub.create_channel("ch2");
        assert_eq!(hub.channel_count(), 2);
    }

    #[test]
    fn test_hub_subscribe_creates_channel() {
        let mut hub = CommunicationHub::new();
        hub.subscribe("auto-ch", "agent1");
        assert_eq!(hub.channel_count(), 1);
    }

    #[test]
    fn test_hub_end_to_end_conversation() {
        let mut hub = CommunicationHub::new();
        hub.register_agent("planner");
        hub.register_agent("executor");
        hub.register_agent("reviewer");

        // Planner sends task to executor
        let task = AgentMessage::new(
            "planner",
            "executor",
            "execute task",
            json!({"task": "build"}),
        )
        .with_priority(MessagePriority::Urgent);
        let task_id = task.id.clone();
        hub.send(task).unwrap();

        // Executor reads and replies
        let mailbox = hub.get_mailbox("executor").unwrap();
        let msgs = mailbox.read();
        assert_eq!(msgs.len(), 1);
        assert_eq!(msgs[0].priority, MessagePriority::Urgent);

        let reply = AgentMessage::new("executor", "planner", "task done", json!({"status": "ok"}))
            .with_reply_to(&task_id);
        hub.send(reply).unwrap();

        // Planner reads the reply
        let planner_msgs = hub.get_mailbox("planner").unwrap().read();
        assert_eq!(planner_msgs.len(), 1);
        assert!(planner_msgs[0].is_reply());

        // Broadcast result to all via channel
        hub.create_channel("results");
        hub.subscribe("results", "planner");
        hub.subscribe("results", "executor");
        hub.subscribe("results", "reviewer");

        let count = hub
            .broadcast("results", "planner", "result ready", json!({"done": true}))
            .unwrap();
        assert_eq!(count, 2); // executor and reviewer

        assert_eq!(hub.get_mailbox("reviewer").unwrap().unread_count(), 1);
    }

    #[test]
    fn test_hub_shared_state_multiple_agents() {
        let mut hub = CommunicationHub::new();
        hub.shared_state_mut().set("x", json!(1), "a");
        hub.shared_state_mut().set("y", json!(2), "b");
        hub.shared_state_mut().set("z", json!(3), "a");

        let a_entries = hub.shared_state().entries_by_owner("a");
        assert_eq!(a_entries.len(), 2);
        assert_eq!(hub.shared_state().len(), 3);
    }

    #[test]
    fn test_priority_equality() {
        assert_eq!(MessagePriority::Normal, MessagePriority::Normal);
        assert_ne!(MessagePriority::Urgent, MessagePriority::Low);
    }
}