agentd 0.1.2

Agent daemon for secure capability execution with pluggable isolation backends
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
//! Output sink and multiplexer traits
//!
//! This module defines the core traits for routing execution results
//! to multiple destinations. This enables:
//! - Replying to the source adapter
//! - Broadcasting to NATS for downstream consumers
//! - Writing to audit logs
//! - Streaming to observability systems

use anyhow::Result;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

use super::intent::{IntentResponse, IntentStatus};

/// Context for emitting results
#[derive(Debug, Clone)]
pub struct EmitContext {
    /// Request ID
    pub request_id: String,

    /// Trace ID for distributed tracing
    pub trace_id: String,

    /// Source adapter that received the request
    pub source_adapter: String,

    /// Reply-to channel (adapter-specific)
    pub reply_to: Option<String>,

    /// Client identifier
    pub client_id: String,

    /// Capability that was executed
    pub capability: String,

    /// Whether the execution succeeded
    pub success: bool,

    /// Custom metadata
    pub metadata: Vec<(String, String)>,
}

impl Default for EmitContext {
    fn default() -> Self {
        Self {
            request_id: String::new(),
            trace_id: String::new(),
            source_adapter: String::new(),
            reply_to: None,
            client_id: String::new(),
            capability: String::new(),
            success: true,
            metadata: vec![],
        }
    }
}

/// Output chunk for streaming
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum OutputChunk {
    /// Stdout data
    Stdout(Vec<u8>),
    /// Stderr data
    Stderr(Vec<u8>),
    /// Progress update
    Progress {
        /// Completion percentage (0.0 - 100.0)
        percent: f32,
        /// Progress message
        message: String,
    },
    /// Log message
    Log {
        /// Log level
        level: String,
        /// Log message
        message: String,
        /// Timestamp
        timestamp: chrono::DateTime<chrono::Utc>,
    },
    /// Execution metrics update
    Metrics {
        /// Metric name
        name: String,
        /// Metric value
        value: f64,
        /// Metric labels
        labels: Vec<(String, String)>,
    },
    /// Execution completed
    Complete {
        /// Exit code
        exit_code: i32,
        /// Duration in milliseconds
        duration_ms: u64,
    },
    /// Error occurred
    Error {
        /// Error code
        code: String,
        /// Error message
        message: String,
    },
}

/// Trait for output sinks that receive execution results
#[async_trait]
pub trait OutputSink: Send + Sync {
    /// Get the name of this sink
    fn name(&self) -> &str;

    /// Emit a complete result
    async fn emit(&self, result: &IntentResponse, ctx: &EmitContext) -> Result<()>;

    /// Stream an output chunk (for real-time streaming)
    async fn stream(&self, chunk: &OutputChunk, ctx: &EmitContext) -> Result<()>;

    /// Check if this sink is available and working
    async fn is_available(&self) -> bool;

    /// Flush any buffered data
    async fn flush(&self) -> Result<()>;
}

/// Rule for routing output to sinks
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoutingRule {
    /// Rule name
    pub name: String,

    /// Sink names to route to
    pub sinks: Vec<String>,

    /// Condition for when this rule applies
    pub condition: RoutingCondition,

    /// Whether to continue checking rules after this one matches
    pub continue_matching: bool,

    /// Priority (higher = checked first)
    pub priority: i32,
}

/// Condition for routing rules
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RoutingCondition {
    /// Always route
    Always,

    /// Never route (disabled rule)
    Never,

    /// Route if the sink is available
    IfAvailable,

    /// Route based on success/failure
    OnStatus(IntentStatus),

    /// Route based on capability pattern
    CapabilityMatch(String),

    /// Route based on source adapter
    SourceAdapter(String),

    /// Route based on metadata key presence
    HasMetadata(String),

    /// Route based on metadata key-value match
    MetadataMatch { key: String, value: String },

    /// Combine conditions with AND
    And(Vec<RoutingCondition>),

    /// Combine conditions with OR
    Or(Vec<RoutingCondition>),

    /// Negate a condition
    Not(Box<RoutingCondition>),
}

impl RoutingCondition {
    /// Evaluate the condition against a context
    pub fn evaluate(&self, ctx: &EmitContext, status: &IntentStatus) -> bool {
        match self {
            RoutingCondition::Always => true,
            RoutingCondition::Never => false,
            RoutingCondition::IfAvailable => true, // Checked separately
            RoutingCondition::OnStatus(s) => s == status,
            RoutingCondition::CapabilityMatch(pattern) => {
                // Simple glob-style matching
                if pattern.ends_with('*') {
                    ctx.capability.starts_with(&pattern[..pattern.len() - 1])
                } else {
                    ctx.capability == *pattern
                }
            }
            RoutingCondition::SourceAdapter(adapter) => ctx.source_adapter == *adapter,
            RoutingCondition::HasMetadata(key) => ctx.metadata.iter().any(|(k, _)| k == key),
            RoutingCondition::MetadataMatch { key, value } => {
                ctx.metadata.iter().any(|(k, v)| k == key && v == value)
            }
            RoutingCondition::And(conditions) => conditions.iter().all(|c| c.evaluate(ctx, status)),
            RoutingCondition::Or(conditions) => conditions.iter().any(|c| c.evaluate(ctx, status)),
            RoutingCondition::Not(condition) => !condition.evaluate(ctx, status),
        }
    }
}

/// Multiplexer that routes output to multiple sinks
pub struct OutputMultiplexer {
    sinks: Vec<Arc<dyn OutputSink>>,
    rules: Vec<RoutingRule>,
    default_sinks: Vec<String>,
}

impl OutputMultiplexer {
    /// Create a new multiplexer
    pub fn new() -> Self {
        Self {
            sinks: Vec::new(),
            rules: Vec::new(),
            default_sinks: Vec::new(),
        }
    }

    /// Add a sink to the multiplexer
    pub fn add_sink(&mut self, sink: Arc<dyn OutputSink>) {
        self.sinks.push(sink);
    }

    /// Add a routing rule
    pub fn add_rule(&mut self, rule: RoutingRule) {
        self.rules.push(rule);
        // Keep rules sorted by priority (descending)
        self.rules.sort_by(|a, b| b.priority.cmp(&a.priority));
    }

    /// Set default sinks (used when no rules match)
    pub fn set_defaults(&mut self, sinks: Vec<String>) {
        self.default_sinks = sinks;
    }

    /// Emit a result to all matching sinks
    pub async fn emit(&self, result: &IntentResponse, ctx: &EmitContext) -> Result<()> {
        let target_sinks = self.resolve_sinks(ctx, &result.status);

        for sink_name in &target_sinks {
            if let Some(sink) = self.sinks.iter().find(|s| s.name() == *sink_name) {
                if matches!(self.should_emit(sink_name, ctx), true) {
                    if let Err(e) = sink.emit(result, ctx).await {
                        tracing::warn!(
                            sink = sink_name,
                            error = %e,
                            "Failed to emit to sink"
                        );
                    }
                }
            }
        }

        Ok(())
    }

    /// Stream a chunk to all matching sinks
    pub async fn stream(&self, chunk: &OutputChunk, ctx: &EmitContext) -> Result<()> {
        // For streaming, we use a simpler resolution that doesn't depend on final status
        let target_sinks = self.resolve_streaming_sinks(ctx);

        for sink_name in &target_sinks {
            if let Some(sink) = self.sinks.iter().find(|s| s.name() == *sink_name) {
                if let Err(e) = sink.stream(chunk, ctx).await {
                    tracing::warn!(
                        sink = sink_name,
                        error = %e,
                        "Failed to stream to sink"
                    );
                }
            }
        }

        Ok(())
    }

    /// Flush all sinks
    pub async fn flush(&self) -> Result<()> {
        for sink in &self.sinks {
            if let Err(e) = sink.flush().await {
                tracing::warn!(
                    sink = sink.name(),
                    error = %e,
                    "Failed to flush sink"
                );
            }
        }
        Ok(())
    }

    /// Resolve which sinks should receive the output
    fn resolve_sinks(&self, ctx: &EmitContext, status: &IntentStatus) -> Vec<String> {
        let mut matched_sinks = Vec::new();
        let mut matched = false;

        for rule in &self.rules {
            if rule.condition.evaluate(ctx, status) {
                matched_sinks.extend(rule.sinks.clone());
                matched = true;

                if !rule.continue_matching {
                    break;
                }
            }
        }

        if !matched {
            matched_sinks.extend(self.default_sinks.clone());
        }

        // Deduplicate while preserving order
        let mut seen = std::collections::HashSet::new();
        matched_sinks.retain(|s| seen.insert(s.clone()));

        matched_sinks
    }

    /// Resolve sinks for streaming (simpler rules)
    fn resolve_streaming_sinks(&self, ctx: &EmitContext) -> Vec<String> {
        // For streaming, just use sinks that are marked for streaming
        // This is a simplified version - could be enhanced
        let mut sinks: Vec<String> = self
            .sinks
            .iter()
            .filter(|s| s.name().contains("stream") || s.name() == "reply")
            .map(|s| s.name().to_string())
            .collect();

        if sinks.is_empty() {
            sinks = self.default_sinks.clone();
        }

        sinks
    }

    /// Check if we should emit to a sink (availability check)
    fn should_emit(&self, sink_name: &str, _ctx: &EmitContext) -> bool {
        // Check if any rule has IfAvailable condition for this sink
        for rule in &self.rules {
            if rule.sinks.contains(&sink_name.to_string()) {
                if matches!(rule.condition, RoutingCondition::IfAvailable) {
                    // Would check sink.is_available() here
                    return true;
                }
            }
        }
        true
    }
}

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

/// A simple reply sink that sends results back to the source
pub struct ReplySink {
    name: String,
}

impl ReplySink {
    pub fn new() -> Self {
        Self {
            name: "reply".to_string(),
        }
    }
}

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

#[async_trait]
impl OutputSink for ReplySink {
    fn name(&self) -> &str {
        &self.name
    }

    async fn emit(&self, result: &IntentResponse, ctx: &EmitContext) -> Result<()> {
        // The actual reply mechanism depends on the adapter
        // This sink just marks that a reply should be sent
        tracing::debug!(
            request_id = %ctx.request_id,
            reply_to = ?ctx.reply_to,
            status = ?result.status,
            "Reply sink: result ready for reply"
        );
        Ok(())
    }

    async fn stream(&self, chunk: &OutputChunk, ctx: &EmitContext) -> Result<()> {
        tracing::trace!(
            request_id = %ctx.request_id,
            "Reply sink: streaming chunk"
        );
        Ok(())
    }

    async fn is_available(&self) -> bool {
        true
    }

    async fn flush(&self) -> Result<()> {
        Ok(())
    }
}

/// An audit sink that logs all results for compliance
pub struct AuditSink {
    name: String,
}

impl AuditSink {
    pub fn new() -> Self {
        Self {
            name: "audit".to_string(),
        }
    }
}

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

#[async_trait]
impl OutputSink for AuditSink {
    fn name(&self) -> &str {
        &self.name
    }

    async fn emit(&self, result: &IntentResponse, ctx: &EmitContext) -> Result<()> {
        // Log to audit trail
        tracing::info!(
            target: "audit",
            request_id = %ctx.request_id,
            trace_id = %ctx.trace_id,
            capability = %ctx.capability,
            client_id = %ctx.client_id,
            source_adapter = %ctx.source_adapter,
            status = ?result.status,
            code = %result.code,
            "Audit: intent execution completed"
        );
        Ok(())
    }

    async fn stream(&self, _chunk: &OutputChunk, _ctx: &EmitContext) -> Result<()> {
        // Audit sink typically doesn't stream intermediate output
        Ok(())
    }

    async fn is_available(&self) -> bool {
        true
    }

    async fn flush(&self) -> Result<()> {
        Ok(())
    }
}

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

    // ===== EmitContext Tests =====

    #[test]
    fn test_emit_context_default() {
        let ctx = EmitContext::default();
        assert!(ctx.request_id.is_empty());
        assert!(ctx.trace_id.is_empty());
        assert!(ctx.source_adapter.is_empty());
        assert!(ctx.reply_to.is_none());
        assert!(ctx.client_id.is_empty());
        assert!(ctx.capability.is_empty());
        assert!(ctx.success);
        assert!(ctx.metadata.is_empty());
    }

    #[test]
    fn test_emit_context_creation() {
        let ctx = EmitContext {
            request_id: "req-123".to_string(),
            trace_id: "trace-456".to_string(),
            source_adapter: "grpc".to_string(),
            reply_to: Some("reply-channel".to_string()),
            client_id: "client-789".to_string(),
            capability: "fs.read.v1".to_string(),
            success: true,
            metadata: vec![("key".to_string(), "value".to_string())],
        };

        assert_eq!(ctx.request_id, "req-123");
        assert_eq!(ctx.trace_id, "trace-456");
        assert_eq!(ctx.source_adapter, "grpc");
        assert_eq!(ctx.reply_to, Some("reply-channel".to_string()));
        assert_eq!(ctx.capability, "fs.read.v1");
    }

    // ===== OutputChunk Tests =====

    #[test]
    fn test_output_chunk_stdout() {
        let chunk = OutputChunk::Stdout(b"hello world".to_vec());
        if let OutputChunk::Stdout(data) = chunk {
            assert_eq!(data, b"hello world");
        } else {
            panic!("Expected Stdout variant");
        }
    }

    #[test]
    fn test_output_chunk_stderr() {
        let chunk = OutputChunk::Stderr(b"error message".to_vec());
        if let OutputChunk::Stderr(data) = chunk {
            assert_eq!(data, b"error message");
        } else {
            panic!("Expected Stderr variant");
        }
    }

    #[test]
    fn test_output_chunk_progress() {
        let chunk = OutputChunk::Progress {
            percent: 50.0,
            message: "Halfway done".to_string(),
        };
        if let OutputChunk::Progress { percent, message } = chunk {
            assert_eq!(percent, 50.0);
            assert_eq!(message, "Halfway done");
        } else {
            panic!("Expected Progress variant");
        }
    }

    #[test]
    fn test_output_chunk_log() {
        let chunk = OutputChunk::Log {
            level: "info".to_string(),
            message: "Processing started".to_string(),
            timestamp: chrono::Utc::now(),
        };
        if let OutputChunk::Log { level, message, .. } = chunk {
            assert_eq!(level, "info");
            assert_eq!(message, "Processing started");
        } else {
            panic!("Expected Log variant");
        }
    }

    #[test]
    fn test_output_chunk_metrics() {
        let chunk = OutputChunk::Metrics {
            name: "cpu_usage".to_string(),
            value: 75.5,
            labels: vec![("host".to_string(), "localhost".to_string())],
        };
        if let OutputChunk::Metrics {
            name,
            value,
            labels,
        } = chunk
        {
            assert_eq!(name, "cpu_usage");
            assert_eq!(value, 75.5);
            assert_eq!(labels.len(), 1);
        } else {
            panic!("Expected Metrics variant");
        }
    }

    #[test]
    fn test_output_chunk_complete() {
        let chunk = OutputChunk::Complete {
            exit_code: 0,
            duration_ms: 1234,
        };
        if let OutputChunk::Complete {
            exit_code,
            duration_ms,
        } = chunk
        {
            assert_eq!(exit_code, 0);
            assert_eq!(duration_ms, 1234);
        } else {
            panic!("Expected Complete variant");
        }
    }

    #[test]
    fn test_output_chunk_error() {
        let chunk = OutputChunk::Error {
            code: "E001".to_string(),
            message: "Something went wrong".to_string(),
        };
        if let OutputChunk::Error { code, message } = chunk {
            assert_eq!(code, "E001");
            assert_eq!(message, "Something went wrong");
        } else {
            panic!("Expected Error variant");
        }
    }

    // ===== RoutingRule Tests =====

    #[test]
    fn test_routing_rule_creation() {
        let rule = RoutingRule {
            name: "error-to-audit".to_string(),
            sinks: vec!["audit".to_string()],
            condition: RoutingCondition::OnStatus(IntentStatus::Error),
            continue_matching: false,
            priority: 10,
        };

        assert_eq!(rule.name, "error-to-audit");
        assert_eq!(rule.sinks.len(), 1);
        assert!(!rule.continue_matching);
        assert_eq!(rule.priority, 10);
    }

    // ===== RoutingCondition Tests =====

    #[test]
    fn test_routing_condition_always() {
        let ctx = EmitContext::default();
        let condition = RoutingCondition::Always;
        assert!(condition.evaluate(&ctx, &IntentStatus::Ok));
        assert!(condition.evaluate(&ctx, &IntentStatus::Error));
    }

    #[test]
    fn test_routing_condition_never() {
        let ctx = EmitContext::default();
        let condition = RoutingCondition::Never;
        assert!(!condition.evaluate(&ctx, &IntentStatus::Ok));
        assert!(!condition.evaluate(&ctx, &IntentStatus::Error));
    }

    #[test]
    fn test_routing_condition_if_available() {
        let ctx = EmitContext::default();
        let condition = RoutingCondition::IfAvailable;
        // IfAvailable always evaluates to true (availability is checked separately)
        assert!(condition.evaluate(&ctx, &IntentStatus::Ok));
    }

    #[test]
    fn test_routing_condition_on_status() {
        let ctx = EmitContext::default();
        let condition = RoutingCondition::OnStatus(IntentStatus::Ok);
        assert!(condition.evaluate(&ctx, &IntentStatus::Ok));
        assert!(!condition.evaluate(&ctx, &IntentStatus::Error));
    }

    #[test]
    fn test_routing_condition_capability_match_exact() {
        let mut ctx = EmitContext::default();
        ctx.capability = "fs.read.v1".to_string();

        let condition = RoutingCondition::CapabilityMatch("fs.read.v1".to_string());
        assert!(condition.evaluate(&ctx, &IntentStatus::Ok));

        let condition2 = RoutingCondition::CapabilityMatch("fs.write.v1".to_string());
        assert!(!condition2.evaluate(&ctx, &IntentStatus::Ok));
    }

    #[test]
    fn test_routing_condition_capability_match_wildcard() {
        let mut ctx = EmitContext::default();
        ctx.capability = "fs.read.v1".to_string();

        let condition = RoutingCondition::CapabilityMatch("fs.*".to_string());
        assert!(condition.evaluate(&ctx, &IntentStatus::Ok));

        let condition2 = RoutingCondition::CapabilityMatch("http.*".to_string());
        assert!(!condition2.evaluate(&ctx, &IntentStatus::Ok));
    }

    #[test]
    fn test_routing_condition_source_adapter() {
        let mut ctx = EmitContext::default();
        ctx.source_adapter = "grpc".to_string();

        let condition = RoutingCondition::SourceAdapter("grpc".to_string());
        assert!(condition.evaluate(&ctx, &IntentStatus::Ok));

        let condition2 = RoutingCondition::SourceAdapter("nats".to_string());
        assert!(!condition2.evaluate(&ctx, &IntentStatus::Ok));
    }

    #[test]
    fn test_routing_condition_has_metadata() {
        let mut ctx = EmitContext::default();
        ctx.metadata = vec![("tenant".to_string(), "acme".to_string())];

        let condition = RoutingCondition::HasMetadata("tenant".to_string());
        assert!(condition.evaluate(&ctx, &IntentStatus::Ok));

        let condition2 = RoutingCondition::HasMetadata("region".to_string());
        assert!(!condition2.evaluate(&ctx, &IntentStatus::Ok));
    }

    #[test]
    fn test_routing_condition_metadata_match() {
        let mut ctx = EmitContext::default();
        ctx.metadata = vec![("tenant".to_string(), "acme".to_string())];

        let condition = RoutingCondition::MetadataMatch {
            key: "tenant".to_string(),
            value: "acme".to_string(),
        };
        assert!(condition.evaluate(&ctx, &IntentStatus::Ok));

        let condition2 = RoutingCondition::MetadataMatch {
            key: "tenant".to_string(),
            value: "other".to_string(),
        };
        assert!(!condition2.evaluate(&ctx, &IntentStatus::Ok));
    }

    #[test]
    fn test_routing_condition_and() {
        let mut ctx = EmitContext::default();
        ctx.capability = "fs.read.v1".to_string();
        ctx.source_adapter = "grpc".to_string();

        let condition = RoutingCondition::And(vec![
            RoutingCondition::CapabilityMatch("fs.*".to_string()),
            RoutingCondition::SourceAdapter("grpc".to_string()),
        ]);
        assert!(condition.evaluate(&ctx, &IntentStatus::Ok));

        let condition2 = RoutingCondition::And(vec![
            RoutingCondition::CapabilityMatch("fs.*".to_string()),
            RoutingCondition::SourceAdapter("nats".to_string()),
        ]);
        assert!(!condition2.evaluate(&ctx, &IntentStatus::Ok));
    }

    #[test]
    fn test_routing_condition_or() {
        let mut ctx = EmitContext::default();
        ctx.capability = "fs.read.v1".to_string();
        ctx.source_adapter = "grpc".to_string();

        let condition = RoutingCondition::Or(vec![
            RoutingCondition::CapabilityMatch("http.*".to_string()),
            RoutingCondition::SourceAdapter("grpc".to_string()),
        ]);
        assert!(condition.evaluate(&ctx, &IntentStatus::Ok));

        let condition2 = RoutingCondition::Or(vec![
            RoutingCondition::CapabilityMatch("http.*".to_string()),
            RoutingCondition::SourceAdapter("nats".to_string()),
        ]);
        assert!(!condition2.evaluate(&ctx, &IntentStatus::Ok));
    }

    #[test]
    fn test_routing_condition_not() {
        let mut ctx = EmitContext::default();
        ctx.capability = "fs.read.v1".to_string();

        let condition = RoutingCondition::Not(Box::new(RoutingCondition::CapabilityMatch(
            "http.*".to_string(),
        )));
        assert!(condition.evaluate(&ctx, &IntentStatus::Ok));

        let condition2 = RoutingCondition::Not(Box::new(RoutingCondition::CapabilityMatch(
            "fs.*".to_string(),
        )));
        assert!(!condition2.evaluate(&ctx, &IntentStatus::Ok));
    }

    // ===== OutputMultiplexer Tests =====

    #[test]
    fn test_output_multiplexer_new() {
        let mux = OutputMultiplexer::new();
        assert!(mux.sinks.is_empty());
        assert!(mux.rules.is_empty());
        assert!(mux.default_sinks.is_empty());
    }

    #[test]
    fn test_output_multiplexer_default() {
        let mux = OutputMultiplexer::default();
        assert!(mux.sinks.is_empty());
    }

    #[test]
    fn test_output_multiplexer_add_rule_priority_sorting() {
        let mut mux = OutputMultiplexer::new();

        mux.add_rule(RoutingRule {
            name: "low".to_string(),
            sinks: vec![],
            condition: RoutingCondition::Always,
            continue_matching: false,
            priority: 1,
        });

        mux.add_rule(RoutingRule {
            name: "high".to_string(),
            sinks: vec![],
            condition: RoutingCondition::Always,
            continue_matching: false,
            priority: 10,
        });

        mux.add_rule(RoutingRule {
            name: "medium".to_string(),
            sinks: vec![],
            condition: RoutingCondition::Always,
            continue_matching: false,
            priority: 5,
        });

        // Rules should be sorted by priority descending
        assert_eq!(mux.rules[0].name, "high");
        assert_eq!(mux.rules[1].name, "medium");
        assert_eq!(mux.rules[2].name, "low");
    }

    #[test]
    fn test_output_multiplexer_set_defaults() {
        let mut mux = OutputMultiplexer::new();
        mux.set_defaults(vec!["audit".to_string(), "reply".to_string()]);

        assert_eq!(mux.default_sinks.len(), 2);
        assert!(mux.default_sinks.contains(&"audit".to_string()));
        assert!(mux.default_sinks.contains(&"reply".to_string()));
    }

    #[test]
    fn test_output_multiplexer_resolve_sinks_no_rules() {
        let mut mux = OutputMultiplexer::new();
        mux.set_defaults(vec!["default-sink".to_string()]);

        let ctx = EmitContext::default();
        let sinks = mux.resolve_sinks(&ctx, &IntentStatus::Ok);

        assert_eq!(sinks, vec!["default-sink"]);
    }

    #[test]
    fn test_output_multiplexer_resolve_sinks_with_matching_rule() {
        let mut mux = OutputMultiplexer::new();
        mux.set_defaults(vec!["default".to_string()]);

        mux.add_rule(RoutingRule {
            name: "errors-to-audit".to_string(),
            sinks: vec!["audit".to_string()],
            condition: RoutingCondition::OnStatus(IntentStatus::Error),
            continue_matching: false,
            priority: 10,
        });

        let ctx = EmitContext::default();

        // On error, should route to audit
        let sinks = mux.resolve_sinks(&ctx, &IntentStatus::Error);
        assert_eq!(sinks, vec!["audit"]);

        // On success, should use defaults
        let sinks = mux.resolve_sinks(&ctx, &IntentStatus::Ok);
        assert_eq!(sinks, vec!["default"]);
    }

    #[test]
    fn test_output_multiplexer_resolve_sinks_continue_matching() {
        let mut mux = OutputMultiplexer::new();

        mux.add_rule(RoutingRule {
            name: "all-to-audit".to_string(),
            sinks: vec!["audit".to_string()],
            condition: RoutingCondition::Always,
            continue_matching: true, // Continue to next rule
            priority: 10,
        });

        mux.add_rule(RoutingRule {
            name: "all-to-metrics".to_string(),
            sinks: vec!["metrics".to_string()],
            condition: RoutingCondition::Always,
            continue_matching: false,
            priority: 5,
        });

        let ctx = EmitContext::default();
        let sinks = mux.resolve_sinks(&ctx, &IntentStatus::Ok);

        // Should include both since first rule has continue_matching = true
        assert!(sinks.contains(&"audit".to_string()));
        assert!(sinks.contains(&"metrics".to_string()));
    }

    #[test]
    fn test_output_multiplexer_resolve_sinks_deduplication() {
        let mut mux = OutputMultiplexer::new();

        mux.add_rule(RoutingRule {
            name: "rule1".to_string(),
            sinks: vec!["audit".to_string()],
            condition: RoutingCondition::Always,
            continue_matching: true,
            priority: 10,
        });

        mux.add_rule(RoutingRule {
            name: "rule2".to_string(),
            sinks: vec!["audit".to_string(), "metrics".to_string()],
            condition: RoutingCondition::Always,
            continue_matching: false,
            priority: 5,
        });

        let ctx = EmitContext::default();
        let sinks = mux.resolve_sinks(&ctx, &IntentStatus::Ok);

        // audit should only appear once due to deduplication
        assert_eq!(sinks.iter().filter(|s| *s == "audit").count(), 1);
    }

    // ===== ReplySink Tests =====

    #[test]
    fn test_reply_sink_new() {
        let sink = ReplySink::new();
        assert_eq!(sink.name, "reply");
    }

    #[test]
    fn test_reply_sink_default() {
        let sink = ReplySink::default();
        assert_eq!(sink.name, "reply");
    }

    // ===== AuditSink Tests =====

    #[test]
    fn test_audit_sink_new() {
        let sink = AuditSink::new();
        assert_eq!(sink.name, "audit");
    }

    #[test]
    fn test_audit_sink_default() {
        let sink = AuditSink::default();
        assert_eq!(sink.name, "audit");
    }

    // ===== Serialization Tests =====

    #[test]
    fn test_output_chunk_serialization() {
        let chunk = OutputChunk::Progress {
            percent: 75.5,
            message: "Almost done".to_string(),
        };
        let json = serde_json::to_string(&chunk).unwrap();
        let deserialized: OutputChunk = serde_json::from_str(&json).unwrap();

        if let OutputChunk::Progress { percent, message } = deserialized {
            assert_eq!(percent, 75.5);
            assert_eq!(message, "Almost done");
        } else {
            panic!("Expected Progress variant");
        }
    }

    #[test]
    fn test_routing_rule_serialization() {
        let rule = RoutingRule {
            name: "test-rule".to_string(),
            sinks: vec!["audit".to_string()],
            condition: RoutingCondition::Always,
            continue_matching: true,
            priority: 5,
        };

        let json = serde_json::to_string(&rule).unwrap();
        let deserialized: RoutingRule = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.name, "test-rule");
        assert_eq!(deserialized.priority, 5);
    }

    #[test]
    fn test_routing_condition_serialization() {
        let condition = RoutingCondition::And(vec![
            RoutingCondition::Always,
            RoutingCondition::OnStatus(IntentStatus::Ok),
        ]);

        let json = serde_json::to_string(&condition).unwrap();
        let deserialized: RoutingCondition = serde_json::from_str(&json).unwrap();

        let ctx = EmitContext::default();
        // Both conditions match, so AND should be true
        assert!(deserialized.evaluate(&ctx, &IntentStatus::Ok));
    }
}