meerkat-comms 0.4.0

Inter-agent communication for Meerkat
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
//! CommsMessage types for session-injectable messages.
//!
//! These types bridge the gap between raw `InboxItem` envelopes and
//! the format needed for injection into an agent's session.

use crate::inproc::InprocRegistry;
use crate::{InboxItem, MessageKind, PubKey, TrustedPeers};
use meerkat_core::PlainEventSource;
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use std::borrow::Cow;
use uuid::Uuid;

/// A plain (unauthenticated) event message from an external source.
#[derive(Debug, Clone, PartialEq)]
pub struct PlainMessage {
    /// The event body (plain text or serialized JSON).
    pub body: String,
    /// Where the event originated from.
    pub source: PlainEventSource,
    /// Optional interaction ID for subscription correlation.
    pub interaction_id: Option<uuid::Uuid>,
}

impl PlainMessage {
    /// Format this message for injection into an LLM session.
    pub fn to_user_message_text(&self) -> String {
        format!("[EVENT via {}] {}", self.source, self.body)
    }
}

/// A message drained from the inbox, distinguishing authenticated peer
/// messages from unauthenticated external events.
///
/// The compiler enforces this distinction — authenticated messages have
/// verified sender identity (`CommsMessage`), plain events do not (`PlainMessage`).
#[derive(Debug, Clone)]
pub enum DrainedMessage {
    /// A message from an authenticated peer (Ed25519-verified envelope).
    Authenticated(CommsMessage),
    /// A plain event from an external (unauthenticated) source.
    Plain(PlainMessage),
}

/// Convert an inbox item into a `DrainedMessage`.
///
/// Returns `None` for:
/// - `SubagentResult` items (handled separately)
/// - Ack messages (not injected into session)
/// - Unknown peers (for authenticated messages)
pub fn drain_inbox_item(
    item: &InboxItem,
    trusted_peers: &TrustedPeers,
    require_peer_auth: bool,
) -> Option<DrainedMessage> {
    match item {
        InboxItem::External { .. } => {
            CommsMessage::from_inbox_item(item, trusted_peers, require_peer_auth)
                .map(DrainedMessage::Authenticated)
        }
        InboxItem::PlainEvent {
            body,
            source,
            interaction_id,
        } => Some(DrainedMessage::Plain(PlainMessage {
            body: body.clone(),
            source: *source,
            interaction_id: *interaction_id,
        })),
        InboxItem::SubagentResult { .. } => None,
    }
}

/// Standard message intents for inter-agent communication.
///
/// This enum provides type-safe intent values for common operations,
/// with a `Custom` variant for user-defined extensibility.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MessageIntent {
    /// Request to delegate a task to another agent
    Delegate,
    /// Request status update on a task
    Status,
    /// Request to cancel an operation
    Cancel,
    /// Request acknowledgment/confirmation
    Ack,
    /// Request to review something (code, PR, etc.)
    Review,
    /// Request computation or calculation
    Calculate,
    /// Request information or query
    Query,
    /// Custom intent for user-defined operations
    #[serde(untagged)]
    Custom(String),
}

impl MessageIntent {
    /// Create a custom intent from a string
    pub fn custom(s: impl Into<String>) -> Self {
        Self::Custom(s.into())
    }

    /// Get the intent as a string (for backward compatibility)
    pub fn as_str(&self) -> &str {
        match self {
            Self::Delegate => "delegate",
            Self::Status => "status",
            Self::Cancel => "cancel",
            Self::Ack => "ack",
            Self::Review => "review",
            Self::Calculate => "calculate",
            Self::Query => "query",
            Self::Custom(s) => s.as_str(),
        }
    }
}

impl From<String> for MessageIntent {
    fn from(s: String) -> Self {
        match s.as_str() {
            "delegate" => Self::Delegate,
            "status" => Self::Status,
            "cancel" => Self::Cancel,
            "ack" => Self::Ack,
            "review" => Self::Review,
            "calculate" => Self::Calculate,
            "query" => Self::Query,
            _ => Self::Custom(s),
        }
    }
}

impl From<&str> for MessageIntent {
    fn from(s: &str) -> Self {
        Self::from(s.to_string())
    }
}

impl std::fmt::Display for MessageIntent {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Content variants for comms messages.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum CommsContent {
    /// A simple text message from a peer.
    Message {
        /// The message body.
        body: String,
    },
    /// A request from a peer asking this agent to perform an action.
    Request {
        /// The request ID (for send_response).
        request_id: Uuid,
        /// The intent/action being requested (now type-safe).
        intent: MessageIntent,
        /// Parameters for the request.
        params: JsonValue,
    },
    /// A response from a peer to a previous request.
    Response {
        /// The ID of the request this is responding to.
        in_reply_to: Uuid,
        /// The status of the response.
        status: CommsStatus,
        /// The result data.
        result: JsonValue,
    },
}

/// Response status (mirrors meerkat_comms::Status).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CommsStatus {
    Accepted,
    Completed,
    Failed,
}

impl From<crate::Status> for CommsStatus {
    fn from(s: crate::Status) -> Self {
        match s {
            crate::Status::Accepted => CommsStatus::Accepted,
            crate::Status::Completed => CommsStatus::Completed,
            crate::Status::Failed => CommsStatus::Failed,
        }
    }
}

impl From<CommsStatus> for crate::Status {
    fn from(s: CommsStatus) -> Self {
        match s {
            CommsStatus::Accepted => crate::Status::Accepted,
            CommsStatus::Completed => crate::Status::Completed,
            CommsStatus::Failed => crate::Status::Failed,
        }
    }
}

/// A message from the comms inbox ready for session injection.
///
/// This is the processed form of an `InboxItem::External` envelope,
/// containing the peer name (resolved from pubkey) and parsed content.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CommsMessage {
    /// The envelope ID (for reference in responses).
    pub envelope_id: Uuid,
    /// The peer name (resolved from pubkey via TrustedPeers).
    pub from_peer: String,
    /// The peer's public key (for explicit reference).
    pub from_pubkey: PubKey,
    /// The message content.
    pub content: CommsContent,
}

impl CommsMessage {
    /// Create a CommsMessage from an InboxItem and trusted peers list.
    /// Resolves sender only from trusted peers.
    ///
    /// Returns `None` if:
    /// - The item is not an External envelope
    /// - The sender cannot be resolved and `require_peer_auth` is enabled
    /// - The message kind is Ack (acks are not injected into session)
    pub fn from_inbox_item(
        item: &InboxItem,
        trusted_peers: &TrustedPeers,
        require_peer_auth: bool,
    ) -> Option<Self> {
        let envelope = match item {
            InboxItem::External { envelope } => envelope,
            InboxItem::SubagentResult { .. } | InboxItem::PlainEvent { .. } => return None,
        };

        // Resolve peer name from pubkey
        let from_peer = match trusted_peers
            .get_peer(&envelope.from)
            .map(|peer| peer.name.clone())
        {
            Some(name) => name,
            None if require_peer_auth => return None,
            None => InprocRegistry::global()
                .get_name_by_pubkey(&envelope.from)
                .unwrap_or_else(|| envelope.from.to_peer_id()),
        };

        // Convert MessageKind to CommsContent
        let content = match &envelope.kind {
            MessageKind::Message { body } => CommsContent::Message { body: body.clone() },
            MessageKind::Request { intent, params } => CommsContent::Request {
                request_id: envelope.id,
                intent: MessageIntent::from(intent.as_str()),
                params: params.clone(),
            },
            MessageKind::Response {
                in_reply_to,
                status,
                result,
            } => CommsContent::Response {
                in_reply_to: *in_reply_to,
                status: (*status).into(),
                result: result.clone(),
            },
            MessageKind::Ack { .. } => return None, // Don't inject acks
        };

        Some(CommsMessage {
            envelope_id: envelope.id,
            from_peer,
            from_pubkey: envelope.from,
            content,
        })
    }

    /// Format this message as text suitable for injection into an LLM session.
    ///
    /// The format is designed to be clear to the LLM about:
    /// - Who sent the message
    /// - What type of message it is
    /// - How to respond (for requests)
    pub fn to_user_message_text(&self) -> String {
        match &self.content {
            CommsContent::Message { body } => {
                format!("[COMMS MESSAGE from {}]\n{}", self.from_peer, body)
            }
            CommsContent::Request {
                request_id,
                intent,
                params,
            } => {
                let params_str =
                    if params.is_null() || params == &JsonValue::Object(Default::default()) {
                        String::new()
                    } else {
                        format!(
                            "\nParams: {}",
                            serde_json::to_string_pretty(params).unwrap_or_default()
                        )
                    };
                format!(
                    "[COMMS REQUEST from {} (id: {})]\n\
                     Intent: {}{}\n\
                     \n\
                     To respond, use send_response with peer=\"{}\", request_id=\"{}\"",
                    self.from_peer, request_id, intent, params_str, self.from_peer, request_id
                )
            }
            CommsContent::Response {
                in_reply_to,
                status,
                result,
            } => {
                let status_str = match status {
                    CommsStatus::Accepted => "accepted",
                    CommsStatus::Completed => "completed",
                    CommsStatus::Failed => "failed",
                };
                let result_str =
                    if result.is_null() || result == &JsonValue::Object(Default::default()) {
                        String::new()
                    } else {
                        format!(
                            "\nResult: {}",
                            serde_json::to_string_pretty(result).unwrap_or_default()
                        )
                    };
                format!(
                    "[COMMS RESPONSE from {} (to request: {})]\n\
                     Status: {}{}",
                    self.from_peer, in_reply_to, status_str, result_str
                )
            }
        }
    }
}

impl meerkat_core::TurnBoundaryMessage for CommsMessage {
    fn render_for_prompt(&self) -> Cow<'_, str> {
        Cow::Owned(self.to_user_message_text())
    }
}

/// Create a CommsMessage from an Envelope directly (for testing).
#[cfg(test)]
impl CommsMessage {
    pub fn from_envelope(
        envelope: &crate::Envelope,
        trusted_peers: &TrustedPeers,
        require_peer_auth: bool,
    ) -> Option<Self> {
        Self::from_inbox_item(
            &InboxItem::External {
                envelope: envelope.clone(),
            },
            trusted_peers,
            require_peer_auth,
        )
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use crate::{Envelope, Keypair, Signature, TrustedPeer};

    fn make_keypair() -> Keypair {
        Keypair::generate()
    }

    fn make_trusted_peers(name: &str, pubkey: &PubKey) -> TrustedPeers {
        TrustedPeers {
            peers: vec![TrustedPeer {
                name: name.to_string(),
                pubkey: *pubkey,
                addr: "tcp://127.0.0.1:4200".to_string(),
                meta: crate::PeerMeta::default(),
            }],
        }
    }

    fn make_envelope(from: &Keypair, to: PubKey, kind: MessageKind) -> Envelope {
        let mut envelope = Envelope {
            id: Uuid::new_v4(),
            from: from.public_key(),
            to,
            kind,
            sig: Signature::new([0u8; 64]),
        };
        envelope.sign(from);
        envelope
    }

    #[test]
    fn test_comms_message_struct() {
        let msg = CommsMessage {
            envelope_id: Uuid::new_v4(),
            from_peer: "test-peer".to_string(),
            from_pubkey: PubKey::new([1u8; 32]),
            content: CommsContent::Message {
                body: "hello".to_string(),
            },
        };
        assert_eq!(msg.from_peer, "test-peer");
    }

    #[test]
    fn test_comms_content_variants() {
        // Message
        let _ = CommsContent::Message {
            body: "hello".to_string(),
        };
        // Request
        let _ = CommsContent::Request {
            request_id: Uuid::new_v4(),
            intent: MessageIntent::Review,
            params: serde_json::json!({"pr": 42}),
        };
        // Response
        let _ = CommsContent::Response {
            in_reply_to: Uuid::new_v4(),
            status: CommsStatus::Completed,
            result: serde_json::json!({"approved": true}),
        };
    }

    #[test]
    fn test_comms_message_from_inbox_item() {
        let sender = make_keypair();
        let receiver = make_keypair();
        let trusted = make_trusted_peers("sender-agent", &sender.public_key());

        let envelope = make_envelope(
            &sender,
            receiver.public_key(),
            MessageKind::Message {
                body: "hello world".to_string(),
            },
        );

        let item = InboxItem::External { envelope };
        let msg = CommsMessage::from_inbox_item(&item, &trusted, true);

        assert!(msg.is_some());
        let msg = msg.unwrap();
        assert_eq!(msg.from_peer, "sender-agent");
        match msg.content {
            CommsContent::Message { body } => assert_eq!(body, "hello world"),
            _ => unreachable!("expected Message"),
        }
    }

    #[test]
    fn test_comms_message_from_inbox_item_request() {
        let sender = make_keypair();
        let receiver = make_keypair();
        let trusted = make_trusted_peers("requester", &sender.public_key());

        // Use standard "review" intent to test enum mapping
        let envelope = make_envelope(
            &sender,
            receiver.public_key(),
            MessageKind::Request {
                intent: "review".to_string(),
                params: serde_json::json!({"pr": 123}),
            },
        );
        let request_id = envelope.id;

        let item = InboxItem::External { envelope };
        let msg = CommsMessage::from_inbox_item(&item, &trusted, true).unwrap();

        match msg.content {
            CommsContent::Request {
                request_id: rid,
                intent,
                params,
            } => {
                assert_eq!(rid, request_id);
                assert_eq!(intent, MessageIntent::Review);
                assert_eq!(params["pr"], 123);
            }
            _ => unreachable!("expected Request"),
        }
    }

    #[test]
    fn test_comms_message_from_inbox_item_request_custom_intent() {
        let sender = make_keypair();
        let receiver = make_keypair();
        let trusted = make_trusted_peers("requester", &sender.public_key());

        // Custom intent should be preserved as Custom variant
        let envelope = make_envelope(
            &sender,
            receiver.public_key(),
            MessageKind::Request {
                intent: "review-pr".to_string(),
                params: serde_json::json!({"pr": 456}),
            },
        );

        let item = InboxItem::External { envelope };
        let msg = CommsMessage::from_inbox_item(&item, &trusted, true).unwrap();

        match msg.content {
            CommsContent::Request { intent, params, .. } => {
                assert_eq!(intent, MessageIntent::Custom("review-pr".to_string()));
                assert_eq!(params["pr"], 456);
            }
            _ => unreachable!("expected Request"),
        }
    }

    #[test]
    fn test_comms_message_from_inbox_item_response() {
        let sender = make_keypair();
        let receiver = make_keypair();
        let trusted = make_trusted_peers("responder", &sender.public_key());

        let orig_request_id = Uuid::new_v4();
        let envelope = make_envelope(
            &sender,
            receiver.public_key(),
            MessageKind::Response {
                in_reply_to: orig_request_id,
                status: crate::Status::Completed,
                result: serde_json::json!({"approved": true}),
            },
        );

        let item = InboxItem::External { envelope };
        let msg = CommsMessage::from_inbox_item(&item, &trusted, true).unwrap();

        match msg.content {
            CommsContent::Response {
                in_reply_to,
                status,
                result,
            } => {
                assert_eq!(in_reply_to, orig_request_id);
                assert_eq!(status, CommsStatus::Completed);
                assert_eq!(result["approved"], true);
            }
            _ => unreachable!("expected Response"),
        }
    }

    #[test]
    fn test_comms_message_from_inbox_item_ignores_ack() {
        let sender = make_keypair();
        let receiver = make_keypair();
        let trusted = make_trusted_peers("sender", &sender.public_key());

        let envelope = make_envelope(
            &sender,
            receiver.public_key(),
            MessageKind::Ack {
                in_reply_to: Uuid::new_v4(),
            },
        );

        let item = InboxItem::External { envelope };
        let msg = CommsMessage::from_inbox_item(&item, &trusted, true);

        assert!(
            msg.is_none(),
            "Acks should not be converted to CommsMessage"
        );
    }

    #[test]
    fn test_comms_message_from_inbox_item_ignores_unknown_peer() {
        let sender = make_keypair();
        let other = make_keypair();
        let receiver = make_keypair();
        let trusted = make_trusted_peers("other", &other.public_key()); // sender NOT trusted

        let envelope = make_envelope(
            &sender,
            receiver.public_key(),
            MessageKind::Message {
                body: "hello".to_string(),
            },
        );

        let item = InboxItem::External { envelope };
        let msg = CommsMessage::from_inbox_item(&item, &trusted, true);

        assert!(msg.is_none(), "Unknown peers should return None");
    }

    #[test]
    fn test_comms_message_from_inbox_item_uses_inproc_name_without_auth() {
        let sender = make_keypair();
        let receiver = make_keypair();
        let trusted = TrustedPeers::new();
        let sender_name = format!("sender-no-auth-{name}", name = Uuid::new_v4().simple());

        let (_, sender_inbox_sender) = crate::Inbox::new();
        InprocRegistry::global().register(
            sender_name.clone(),
            sender.public_key(),
            sender_inbox_sender,
        );

        let envelope = make_envelope(
            &sender,
            receiver.public_key(),
            MessageKind::Message {
                body: "hello".to_string(),
            },
        );
        let item = InboxItem::External { envelope };
        let msg = CommsMessage::from_inbox_item(&item, &trusted, false).unwrap();

        assert_eq!(msg.from_peer, sender_name);
        match msg.content {
            CommsContent::Message { body } => assert_eq!(body, "hello"),
            _ => unreachable!("expected Message"),
        }

        assert!(InprocRegistry::global().unregister(&sender.public_key()));
    }

    #[test]
    fn test_comms_message_from_inbox_item_ignores_subagent_result() {
        let trusted = TrustedPeers::new();

        let item = InboxItem::SubagentResult {
            subagent_id: Uuid::new_v4(),
            result: serde_json::json!({"done": true}),
            summary: "Task completed".to_string(),
        };

        let msg = CommsMessage::from_inbox_item(&item, &trusted, true);
        assert!(msg.is_none(), "SubagentResult should return None");
    }

    #[test]
    fn test_comms_message_formatting() {
        let msg = CommsMessage {
            envelope_id: Uuid::new_v4(),
            from_peer: "review-agent".to_string(),
            from_pubkey: PubKey::new([1u8; 32]),
            content: CommsContent::Message {
                body: "Please review PR #42".to_string(),
            },
        };

        let text = msg.to_user_message_text();
        assert!(text.contains("COMMS MESSAGE"));
        assert!(text.contains("review-agent"));
        assert!(text.contains("Please review PR #42"));
    }

    #[test]
    fn test_comms_message_formatting_request() {
        let request_id = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap();
        let msg = CommsMessage {
            envelope_id: request_id,
            from_peer: "coding-agent".to_string(),
            from_pubkey: PubKey::new([1u8; 32]),
            content: CommsContent::Request {
                request_id,
                intent: MessageIntent::Review,
                params: serde_json::json!({"pr": 42}),
            },
        };

        let text = msg.to_user_message_text();
        assert!(text.contains("COMMS REQUEST"));
        assert!(text.contains("coding-agent"));
        assert!(text.contains("review"));
        assert!(text.contains("550e8400-e29b-41d4-a716-446655440000"));
        assert!(text.contains("send_response"));
    }

    #[test]
    fn test_comms_message_formatting_response() {
        let in_reply_to = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap();
        let msg = CommsMessage {
            envelope_id: Uuid::new_v4(),
            from_peer: "review-agent".to_string(),
            from_pubkey: PubKey::new([1u8; 32]),
            content: CommsContent::Response {
                in_reply_to,
                status: CommsStatus::Completed,
                result: serde_json::json!({"approved": true}),
            },
        };

        let text = msg.to_user_message_text();
        assert!(text.contains("COMMS RESPONSE"));
        assert!(text.contains("review-agent"));
        assert!(text.contains("completed"));
        assert!(text.contains("550e8400-e29b-41d4-a716-446655440000"));
    }

    // ========================================================================
    // MessageIntent type-safety tests
    // ========================================================================

    #[test]
    fn test_message_intent_standard_variants() {
        // All standard variants should serialize as snake_case strings
        assert_eq!(MessageIntent::Delegate.as_str(), "delegate");
        assert_eq!(MessageIntent::Status.as_str(), "status");
        assert_eq!(MessageIntent::Cancel.as_str(), "cancel");
        assert_eq!(MessageIntent::Ack.as_str(), "ack");
        assert_eq!(MessageIntent::Review.as_str(), "review");
        assert_eq!(MessageIntent::Calculate.as_str(), "calculate");
        assert_eq!(MessageIntent::Query.as_str(), "query");
    }

    #[test]
    fn test_message_intent_custom_variant() {
        let custom = MessageIntent::custom("my-custom-intent");
        assert_eq!(custom.as_str(), "my-custom-intent");
        assert_eq!(
            custom,
            MessageIntent::Custom("my-custom-intent".to_string())
        );
    }

    #[test]
    fn test_message_intent_from_string() {
        // Standard intents should map to enum variants
        assert_eq!(MessageIntent::from("delegate"), MessageIntent::Delegate);
        assert_eq!(MessageIntent::from("status"), MessageIntent::Status);
        assert_eq!(MessageIntent::from("cancel"), MessageIntent::Cancel);
        assert_eq!(MessageIntent::from("ack"), MessageIntent::Ack);
        assert_eq!(MessageIntent::from("review"), MessageIntent::Review);
        assert_eq!(MessageIntent::from("calculate"), MessageIntent::Calculate);
        assert_eq!(MessageIntent::from("query"), MessageIntent::Query);

        // Unknown strings should become Custom
        assert_eq!(
            MessageIntent::from("unknown-intent"),
            MessageIntent::Custom("unknown-intent".to_string())
        );
    }

    #[test]
    fn test_message_intent_display() {
        assert_eq!(format!("{}", MessageIntent::Review), "review");
        assert_eq!(
            format!("{}", MessageIntent::Custom("foo".to_string())),
            "foo"
        );
    }

    #[test]
    fn test_message_intent_serialization() {
        // Standard variants serialize to their string representations
        let json = serde_json::to_string(&MessageIntent::Delegate).unwrap();
        assert_eq!(json, "\"delegate\"");

        let json = serde_json::to_string(&MessageIntent::Review).unwrap();
        assert_eq!(json, "\"review\"");

        // Custom variant serializes to the inner string
        let json = serde_json::to_string(&MessageIntent::Custom("custom-op".to_string())).unwrap();
        assert_eq!(json, "\"custom-op\"");
    }

    #[test]
    fn test_message_intent_deserialization() {
        // Standard strings deserialize to enum variants
        let intent: MessageIntent = serde_json::from_str("\"delegate\"").unwrap();
        assert_eq!(intent, MessageIntent::Delegate);

        let intent: MessageIntent = serde_json::from_str("\"review\"").unwrap();
        assert_eq!(intent, MessageIntent::Review);

        // Unknown strings deserialize to Custom
        let intent: MessageIntent = serde_json::from_str("\"my-custom\"").unwrap();
        assert_eq!(intent, MessageIntent::Custom("my-custom".to_string()));
    }

    // === DrainedMessage / PlainMessage tests ===

    #[test]
    fn test_drained_message_from_external() {
        let sender = make_keypair();
        let receiver = make_keypair();
        let trusted = make_trusted_peers("sender-agent", &sender.public_key());

        let envelope = make_envelope(
            &sender,
            receiver.public_key(),
            MessageKind::Message {
                body: "hello".to_string(),
            },
        );
        let item = InboxItem::External { envelope };
        let drained = drain_inbox_item(&item, &trusted, true);

        assert!(drained.is_some());
        match drained.unwrap() {
            DrainedMessage::Authenticated(msg) => {
                assert_eq!(msg.from_peer, "sender-agent");
            }
            DrainedMessage::Plain(_) => panic!("Expected Authenticated"),
        }
    }

    #[test]
    fn test_drained_message_from_plain_event() {
        use meerkat_core::PlainEventSource;

        let trusted = TrustedPeers::new();
        let item = InboxItem::PlainEvent {
            body: "New email arrived".to_string(),
            source: PlainEventSource::Tcp,
            interaction_id: None,
        };
        let drained = drain_inbox_item(&item, &trusted, true);

        assert!(drained.is_some());
        match drained.unwrap() {
            DrainedMessage::Plain(msg) => {
                assert_eq!(msg.body, "New email arrived");
                assert_eq!(msg.source, PlainEventSource::Tcp);
                assert_eq!(msg.interaction_id, None);
            }
            DrainedMessage::Authenticated(_) => panic!("Expected Plain"),
        }
    }

    #[test]
    fn test_plain_message_to_user_message_text() {
        use meerkat_core::PlainEventSource;

        let msg = PlainMessage {
            body: "CPU > 95% on prod-3".to_string(),
            source: PlainEventSource::Webhook,
            interaction_id: None,
        };
        let text = msg.to_user_message_text();
        assert_eq!(text, "[EVENT via webhook] CPU > 95% on prod-3");
    }

    #[test]
    fn test_plain_message_formatting_all_sources() {
        use meerkat_core::PlainEventSource;

        for (source, label) in [
            (PlainEventSource::Tcp, "tcp"),
            (PlainEventSource::Uds, "uds"),
            (PlainEventSource::Stdin, "stdin"),
            (PlainEventSource::Webhook, "webhook"),
            (PlainEventSource::Rpc, "rpc"),
        ] {
            let msg = PlainMessage {
                body: "test".to_string(),
                source,
                interaction_id: None,
            };
            assert!(
                msg.to_user_message_text().contains(label),
                "PlainMessage should contain source label '{}'",
                label
            );
        }
    }
}