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
use anyhow::{anyhow, Context, Result};
use async_nats::jetstream::consumer::PullConsumer;
use async_nats::jetstream::stream::Stream;
use async_nats::Subscriber;
use async_trait::async_trait;
use futures::StreamExt;
use once_cell::sync::Lazy;
use smith_bus::builders::ResultSubject;
use std::collections::HashSet;
use std::time::Duration;
use tokio::time::timeout;
use tracing::{debug, error, info, warn};

use crate::config::NatsConfig;

/// Trait for NATS publishing operations
///
/// This trait abstracts the publishing functionality of NatsClient
/// to enable mocking in tests and dependency injection.
#[async_trait]
pub trait NatsPublisher: Send + Sync {
    /// Publish a message to a subject
    async fn publish(&self, subject: &str, payload: &[u8]) -> Result<()>;

    /// Publish with reply-to header
    async fn publish_with_reply(&self, subject: &str, reply: &str, payload: &[u8]) -> Result<()>;

    /// Request-reply pattern
    async fn request(&self, subject: &str, payload: &[u8]) -> Result<Vec<u8>>;
}

/// Trait for publishing intent results
///
/// This trait abstracts the result publishing functionality to enable
/// mocking in tests. It handles serialization of IntentResult internally.
#[async_trait]
pub trait IntentResultPublisher: Send + Sync {
    /// Publish an intent result
    async fn publish_result(
        &self,
        intent_id: &str,
        result: &smith_protocol::IntentResult,
    ) -> Result<()>;
}

/// NATS client with JetStream support for pulling intents
#[derive(Clone)]
pub struct NatsClient {
    client: async_nats::Client,
    jetstream: async_nats::jetstream::Context,
    config: NatsConfig,
}

impl NatsClient {
    /// Create new NATS client and connect to server
    pub async fn new(config: &NatsConfig) -> Result<Self> {
        info!("Connecting to NATS servers: {:?}", config.servers);

        let mut connect_opts = async_nats::ConnectOptions::new();

        // Set connection name
        connect_opts = connect_opts.name("smith-executor");

        // Configure TLS if certificates are provided
        if let (Some(cert_path), Some(key_path), Some(ca_path)) =
            (&config.tls_cert, &config.tls_key, &config.tls_ca)
        {
            info!("Configuring TLS connection");

            // Use the public API to configure TLS
            connect_opts = connect_opts
                .require_tls(true)
                .add_client_certificate(cert_path.clone(), key_path.clone())
                .add_root_certificates(ca_path.clone());
        }

        // Set reconnection and timeout options
        connect_opts = connect_opts
            .max_reconnects(None) // Unlimited reconnects
            .read_buffer_capacity(65535) // Max read buffer
            .connection_timeout(Duration::from_secs(10))
            .request_timeout(Some(Duration::from_secs(30)));

        // Connect to NATS server(s)
        let client = if config.servers.len() == 1 {
            async_nats::connect_with_options(&config.servers[0], connect_opts).await
        } else {
            async_nats::connect_with_options(&config.servers, connect_opts).await
        }
        .context("Failed to connect to NATS server")?;

        info!("Connected to NATS server");

        // Get JetStream context
        let jetstream = async_nats::jetstream::new(client.clone());

        Ok(Self {
            client,
            jetstream,
            config: config.clone(),
        })
    }

    /// Create or get existing JetStream stream for a capability
    pub async fn ensure_stream(
        &self,
        capability: &str,
        stream_config: &crate::config::IntentStreamConfig,
    ) -> Result<Stream> {
        let stream_name = format!("JS_INTENTS_{}", capability.replace(".", "_").to_uppercase());

        debug!("Ensuring stream exists: {}", stream_name);

        // Try to get existing stream first
        match self.jetstream.get_stream(&stream_name).await {
            Ok(stream) => {
                debug!("Using existing stream: {}", stream_name);
                return Ok(stream);
            }
            Err(_) => {
                debug!("Stream {} does not exist, creating it", stream_name);
            }
        }

        // Parse configuration values
        let max_age = parse_duration(&stream_config.max_age)?;
        let max_bytes = crate::config::parse_byte_size(&stream_config.max_bytes)? as i64;
        let stream_subject = stream_config.subject.clone();

        // Create stream configuration
        let js_stream_config = async_nats::jetstream::stream::Config {
            name: stream_name.clone(),
            subjects: vec![stream_subject.clone()],
            retention: async_nats::jetstream::stream::RetentionPolicy::WorkQueue,
            max_age,
            max_bytes,
            storage: async_nats::jetstream::stream::StorageType::File,
            num_replicas: 1,
            discard: async_nats::jetstream::stream::DiscardPolicy::Old,
            ..Default::default()
        };

        // Create the stream
        match self.jetstream.create_stream(js_stream_config).await {
            Ok(stream) => {
                info!("Created JetStream stream: {}", stream_name);
                Ok(stream)
            }
            Err(err) => {
                warn!(
                    stream = %stream_name,
                    error = %err,
                    "Failed to create stream; assuming it already exists via bootstrap"
                );

                if stream_subject.starts_with("smith.intents.") {
                    match self.jetstream.get_stream("INTENTS").await {
                        Ok(stream) => {
                            info!(
                                stream = %stream_name,
                                fallback_stream = "INTENTS",
                                "Using INTENTS stream provided by bootstrap for capability"
                            );
                            return Ok(stream);
                        }
                        Err(fallback_err) => {
                            warn!(
                                stream = %stream_name,
                                fallback_stream = "INTENTS",
                                error = %fallback_err,
                                "Failed to fetch fallback INTENTS stream after creation conflict"
                            );
                        }
                    }
                }

                self.jetstream
                    .get_stream(&stream_name)
                    .await
                    .with_context(|| {
                        format!(
                            "Stream creation failed and fetching existing stream {} also failed",
                            stream_name
                        )
                    })
            }
        }
    }

    /// Create pull consumer for a capability  
    pub async fn create_consumer(
        &self,
        capability: &str,
        stream_config: &crate::config::IntentStreamConfig,
    ) -> Result<IntentConsumer> {
        // Ensure stream exists
        let stream = self.ensure_stream(capability, stream_config).await?;

        let consumer_name = format!("executor-{}-workqueue", capability.replace(".", "-"));

        // Create pull consumer configuration
        let consumer_config = async_nats::jetstream::consumer::pull::Config {
            durable_name: Some(consumer_name.clone()),
            description: Some(format!(
                "executor worker consumer for capability {capability}"
            )),
            filter_subject: stream_config.subject.clone(),
            ack_policy: async_nats::jetstream::consumer::AckPolicy::Explicit,
            ack_wait: Duration::from_secs(30),
            max_ack_pending: (stream_config.workers * 2) as i64, // 2x worker concurrency
            deliver_policy: async_nats::jetstream::consumer::DeliverPolicy::All,
            replay_policy: async_nats::jetstream::consumer::ReplayPolicy::Instant,
            inactive_threshold: Duration::from_secs(600),
            ..Default::default()
        };

        // Create or re-use the consumer
        let mut consumer = stream
            .get_or_create_consumer(&consumer_name, consumer_config)
            .await
            .with_context(|| format!("Failed to create consumer: {}", consumer_name))?;

        let info = consumer
            .info()
            .await
            .with_context(|| format!("Failed to fetch consumer info: {}", consumer_name))?;
        info!(
            consumer = %consumer_name,
            capability = capability,
            created_at = %info.created,
            "JetStream consumer ready for capability"
        );

        Ok(IntentConsumer {
            consumer,
            capability: capability.to_string(),
        })
    }

    /// Publish result to results subject
    pub async fn publish_result(
        &self,
        intent_id: &str,
        result: &smith_protocol::IntentResult,
    ) -> Result<()> {
        let subject = ResultSubject::for_intent(intent_id);
        let payload = serde_json::to_vec(result).context("Failed to serialize intent result")?;

        let payload_len = payload.len();
        let subject_len = subject.len();
        let subject_hex: String = subject
            .as_bytes()
            .iter()
            .map(|b| format!("{:02X}", b))
            .collect();
        debug!(
            subject = %subject,
            subject_len,
            subject_hex = %subject_hex,
            payload_len = payload_len,
            "Publishing executor result to core NATS"
        );

        self.client
            .publish(subject.clone(), payload.into())
            .await
            .context("Failed to publish result to NATS")?;

        let flush_disabled = std::env::var("SMITH_EXECUTOR_DISABLE_RESULT_FLUSH")
            .map(|val| val == "1")
            .unwrap_or(false);

        if !flush_disabled {
            debug!(subject = %subject, subject_len, "Awaiting executor result flush");
            let flush_client = self.client.clone();
            let flush_subject = subject.clone();
            tokio::spawn(async move {
                let subject_len = flush_subject.len();
                debug!(subject = %flush_subject, subject_len, "Executor flush task started");
                match timeout(Duration::from_secs(2), flush_client.flush()).await {
                    Ok(Ok(())) => {
                        info!(subject = %flush_subject, subject_len, "Flushed executor result publish");
                    }
                    Ok(Err(err)) => {
                        warn!(
                            subject = %flush_subject,
                            subject_len,
                            error = %err,
                            "Failed to flush executor result publish"
                        );
                    }
                    Err(_) => {
                        warn!(
                            subject = %flush_subject,
                            subject_len,
                            "Executor result flush timed out; continuing without confirmation"
                        );
                    }
                }
            });
        } else {
            debug!(subject = %subject, subject_len, "Skipping executor result flush (disabled)");
        }

        info!(
            subject = %subject,
            subject_len,
            subject_hex = %subject_hex,
            intent_id = intent_id,
            payload_len = payload_len,
            "Executor result publish completed"
        );

        Ok(())
    }

    pub async fn maybe_spawn_debug_result_tap(&self) -> Result<()> {
        let debug_enabled = std::env::var("SMITH_EXECUTOR_DEBUG_TAP")
            .map(|val| val == "1")
            .unwrap_or(false);

        if !debug_enabled {
            return Ok(());
        }

        let mut subscriber = self
            .client
            .subscribe(ResultSubject::all())
            .await
            .context("Failed to subscribe to smith.results.* for debug tap")?;

        tokio::spawn(async move {
            while let Some(message) = subscriber.next().await {
                let subject = message.subject.clone();
                let payload_len = message.payload.len();
                let preview: String = String::from_utf8_lossy(&message.payload)
                    .chars()
                    .take(256)
                    .collect();
                info!(
                    subject = %subject,
                    payload_len,
                    payload_preview = %preview,
                    "Executor debug tap observed smith.results message"
                );
            }
        });

        Ok(())
    }

    pub async fn subscribe(&self, subject: &str, queue: Option<&str>) -> Result<Subscriber> {
        if let Some(queue) = queue {
            self.client
                .queue_subscribe(subject.to_string(), queue.to_string())
                .await
                .context("Failed to create queued subscription")
        } else {
            self.client
                .subscribe(subject.to_string())
                .await
                .context("Failed to create subscription")
        }
    }

    /// Get stream configuration for capability
    fn get_stream_config(&self, _capability: &str) -> Result<&crate::config::IntentStreamConfig> {
        // This would need access to the full config - for now return error
        Err(anyhow::anyhow!(
            "Stream config not available in this context"
        ))
    }

    /// Get NATS server info
    pub async fn server_info(&self) -> Result<async_nats::ServerInfo> {
        Ok(self.client.server_info())
    }

    /// Check connection status
    pub fn connection_status(&self) -> async_nats::connection::State {
        self.client.connection_state()
    }
}

#[async_trait]
impl NatsPublisher for NatsClient {
    async fn publish(&self, subject: &str, payload: &[u8]) -> Result<()> {
        self.client
            .publish(subject.to_string(), payload.to_vec().into())
            .await
            .context("Failed to publish message to NATS")?;
        Ok(())
    }

    async fn publish_with_reply(&self, subject: &str, reply: &str, payload: &[u8]) -> Result<()> {
        self.client
            .publish_with_reply(
                subject.to_string(),
                reply.to_string(),
                payload.to_vec().into(),
            )
            .await
            .context("Failed to publish message with reply to NATS")?;
        Ok(())
    }

    async fn request(&self, subject: &str, payload: &[u8]) -> Result<Vec<u8>> {
        let response = self
            .client
            .request(subject.to_string(), payload.to_vec().into())
            .await
            .context("Failed to make NATS request")?;
        Ok(response.payload.to_vec())
    }
}

#[async_trait]
impl IntentResultPublisher for NatsClient {
    async fn publish_result(
        &self,
        intent_id: &str,
        result: &smith_protocol::IntentResult,
    ) -> Result<()> {
        // Delegate to the existing publish_result method
        NatsClient::publish_result(self, intent_id, result).await
    }
}

/// Consumer wrapper for pulling intents from JetStream
pub struct IntentConsumer {
    consumer: PullConsumer,
    capability: String,
}

impl IntentConsumer {
    fn debug_pull_enabled(&self) -> bool {
        static DEBUG_PULL_CAPS: Lazy<HashSet<String>> = Lazy::new(|| {
            std::env::var("SMITH_EXECUTOR_DEBUG_PULL")
                .ok()
                .map(|raw| {
                    raw.split(',')
                        .map(|cap| cap.trim().to_string())
                        .filter(|cap| !cap.is_empty())
                        .collect::<HashSet<_>>()
                })
                .unwrap_or_default()
        });

        if DEBUG_PULL_CAPS.is_empty() {
            return false;
        }

        DEBUG_PULL_CAPS.contains("*") || DEBUG_PULL_CAPS.contains(&self.capability)
    }

    /// Pull next message from the stream
    pub async fn next(&mut self) -> Result<Option<IntentMessage>> {
        let mut batch = match self
            .consumer
            .stream()
            .max_messages_per_batch(1)
            .expires(Duration::from_secs(30))
            .messages()
            .await
        {
            Ok(batch) => batch,
            Err(err) => {
                error!(
                    capability = %self.capability,
                    error = %err,
                    "Failed to create JetStream pull stream"
                );
                return Err(anyhow::anyhow!(
                    "Failed to create JetStream pull stream: {}",
                    err
                ));
            }
        };

        debug!(
            capability = %self.capability,
            "Awaiting next message from JetStream stream"
        );

        match batch.next().await {
            Some(Ok(message)) => {
                if self.debug_pull_enabled() {
                    info!(
                        capability = %self.capability,
                        subject = %message.subject,
                        "IntentConsumer received message"
                    );
                } else {
                    debug!("Received message for capability: {}", self.capability);
                }
                Ok(Some(IntentMessage { message }))
            }
            Some(Err(err)) => {
                error!(
                    capability = %self.capability,
                    error = %err,
                    "Error receiving message from JetStream stream"
                );
                Err(anyhow::anyhow!("Error receiving message: {}", err))
            }
            None => {
                debug!(
                    capability = %self.capability,
                    "JetStream stream returned no messages"
                );
                if self.debug_pull_enabled() {
                    match self.consumer.info().await {
                        Ok(info) => {
                            info!(
                                capability = %self.capability,
                                num_pending = info.num_pending,
                                num_waiting = info.num_waiting,
                                num_ack_pending = info.num_ack_pending,
                                "IntentConsumer stream yielded no data"
                            );
                        }
                        Err(err) => {
                            warn!(
                                capability = %self.capability,
                                error = %err,
                                "IntentConsumer failed to fetch consumer info after empty batch"
                            );
                        }
                    }
                }
                Ok(None)
            }
        }
    }

    /// Pull multiple messages in batch
    pub async fn batch(&mut self, max_messages: usize) -> Result<Vec<IntentMessage>> {
        let mut messages = self
            .consumer
            .fetch()
            .max_messages(max_messages)
            .messages()
            .await?;

        let mut batch = Vec::new();

        while let Some(message_result) = messages.next().await {
            match message_result {
                Ok(message) => {
                    batch.push(IntentMessage { message });
                }
                Err(e) => {
                    error!("Error in batch message: {}", e);
                    return Err(anyhow::anyhow!("Error in batch message: {}", e));
                }
            }
        }

        Ok(batch)
    }

    /// Get consumer info
    pub async fn info(&mut self) -> Result<async_nats::jetstream::consumer::Info> {
        Ok(self.consumer.info().await?.clone())
    }
}

/// Wrapper around JetStream message for intent processing
pub struct IntentMessage {
    pub message: async_nats::jetstream::Message,
}

impl IntentMessage {
    /// Get message payload as bytes
    pub fn payload(&self) -> &[u8] {
        &self.message.payload
    }

    /// Get message subject
    pub fn subject(&self) -> &str {
        &self.message.subject
    }

    /// Get message metadata
    pub fn context(&self) -> &async_nats::jetstream::context::Context {
        &self.message.context
    }

    /// Acknowledge message processing
    pub async fn ack(&self) -> Result<()> {
        self.message
            .ack()
            .await
            .map_err(|e| anyhow::anyhow!("Failed to acknowledge message: {}", e))
    }

    /// Negative acknowledge (requeue for retry)
    pub async fn nak(&self) -> Result<()> {
        self.message
            .ack_with(async_nats::jetstream::AckKind::Nak(None))
            .await
            .map_err(|e| anyhow::anyhow!("Failed to negative acknowledge message: {}", e))
    }

    /// Negative acknowledge with delay
    pub async fn nak_with_delay(&self, delay: Duration) -> Result<()> {
        self.message
            .ack_with(async_nats::jetstream::AckKind::Nak(Some(delay)))
            .await
            .map_err(|e| {
                anyhow::anyhow!("Failed to negative acknowledge message with delay: {}", e)
            })
    }

    /// Terminate message (don't redeliver)
    pub async fn term(&self) -> Result<()> {
        self.message
            .ack_with(async_nats::jetstream::AckKind::Term)
            .await
            .map_err(|e| anyhow::anyhow!("Failed to terminate message: {}", e))
    }
}

/// Parse duration string to Duration (e.g., "10m", "1h", "30s")
fn parse_duration(duration_str: &str) -> Result<Duration> {
    let seconds = crate::config::parse_duration_seconds(duration_str)?;
    Ok(Duration::from_secs(seconds))
}

/// Mock implementation of IntentResultPublisher for testing
#[cfg(any(test, feature = "test-support"))]
pub mod mock {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};

    /// A mock result publisher that tracks calls for testing
    #[derive(Default)]
    pub struct MockResultPublisher {
        pub publish_calls: std::sync::Mutex<Vec<(String, smith_protocol::IntentResult)>>,
        pub should_fail: std::sync::atomic::AtomicBool,
        pub fail_count: AtomicUsize,
    }

    impl MockResultPublisher {
        pub fn new() -> Self {
            Self::default()
        }

        /// Configure the mock to fail on the next N calls
        pub fn fail_next(&self, count: usize) {
            self.fail_count.store(count, Ordering::SeqCst);
            self.should_fail.store(true, Ordering::SeqCst);
        }

        /// Get all published results
        pub fn published_results(&self) -> Vec<(String, smith_protocol::IntentResult)> {
            self.publish_calls.lock().unwrap().clone()
        }

        /// Get the count of publish calls
        pub fn call_count(&self) -> usize {
            self.publish_calls.lock().unwrap().len()
        }

        /// Clear recorded calls
        pub fn clear(&self) {
            self.publish_calls.lock().unwrap().clear();
        }
    }

    #[async_trait]
    impl IntentResultPublisher for MockResultPublisher {
        async fn publish_result(
            &self,
            intent_id: &str,
            result: &smith_protocol::IntentResult,
        ) -> Result<()> {
            if self.should_fail.load(Ordering::SeqCst) {
                let remaining = self.fail_count.fetch_sub(1, Ordering::SeqCst);
                if remaining > 0 {
                    if remaining == 1 {
                        self.should_fail.store(false, Ordering::SeqCst);
                    }
                    return Err(anyhow!("Mock publish failure"));
                }
            }
            self.publish_calls
                .lock()
                .unwrap()
                .push((intent_id.to_string(), result.clone()));
            Ok(())
        }
    }

    #[async_trait]
    impl NatsPublisher for MockResultPublisher {
        async fn publish(&self, _subject: &str, _payload: &[u8]) -> Result<()> {
            Ok(())
        }

        async fn publish_with_reply(
            &self,
            _subject: &str,
            _reply: &str,
            _payload: &[u8],
        ) -> Result<()> {
            Ok(())
        }

        async fn request(&self, _subject: &str, _payload: &[u8]) -> Result<Vec<u8>> {
            Ok(vec![])
        }
    }
}

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

    #[test]
    fn test_parse_duration() {
        assert_eq!(parse_duration("30s").unwrap(), Duration::from_secs(30));
        assert_eq!(parse_duration("10m").unwrap(), Duration::from_secs(600));
        assert_eq!(parse_duration("1h").unwrap(), Duration::from_secs(3600));

        assert!(parse_duration("invalid").is_err());
    }

    #[test]
    fn test_parse_duration_days() {
        assert_eq!(parse_duration("1d").unwrap(), Duration::from_secs(86400));
        assert_eq!(parse_duration("7d").unwrap(), Duration::from_secs(604800));
    }

    #[test]
    fn test_parse_duration_minutes() {
        assert_eq!(parse_duration("5m").unwrap(), Duration::from_secs(300));
        assert_eq!(parse_duration("60m").unwrap(), Duration::from_secs(3600));
    }

    #[test]
    fn test_parse_duration_hours() {
        assert_eq!(parse_duration("2h").unwrap(), Duration::from_secs(7200));
        assert_eq!(parse_duration("24h").unwrap(), Duration::from_secs(86400));
    }

    #[test]
    fn test_truncate_payload_short() {
        let payload = b"Hello, World!";
        let truncated = truncate_payload(payload, 100);
        assert_eq!(truncated, "Hello, World!");
    }

    #[test]
    fn test_truncate_payload_long() {
        let payload = b"This is a very long payload that should be truncated";
        let truncated = truncate_payload(payload, 10);
        assert_eq!(truncated, "This is a ");
    }

    #[test]
    fn test_truncate_payload_empty() {
        let payload: &[u8] = b"";
        let truncated = truncate_payload(payload, 100);
        assert_eq!(truncated, "");
    }

    #[test]
    fn test_truncate_payload_exact_length() {
        let payload = b"12345";
        let truncated = truncate_payload(payload, 5);
        assert_eq!(truncated, "12345");
    }

    #[test]
    fn test_extract_intent_id_valid() {
        let payload = br#"{"intent_id": "test-123", "data": "hello"}"#;
        let id = extract_intent_id(payload);
        assert_eq!(id, Some("test-123".to_string()));
    }

    #[test]
    fn test_extract_intent_id_missing() {
        let payload = br#"{"data": "hello"}"#;
        let id = extract_intent_id(payload);
        assert_eq!(id, None);
    }

    #[test]
    fn test_extract_intent_id_invalid_json() {
        let payload = b"not valid json";
        let id = extract_intent_id(payload);
        assert_eq!(id, None);
    }

    #[test]
    fn test_extract_intent_id_non_string() {
        let payload = br#"{"intent_id": 12345}"#;
        let id = extract_intent_id(payload);
        assert_eq!(id, None);
    }

    #[test]
    fn test_extract_intent_id_null() {
        let payload = br#"{"intent_id": null}"#;
        let id = extract_intent_id(payload);
        assert_eq!(id, None);
    }

    #[test]
    fn test_extract_intent_id_empty_string() {
        let payload = br#"{"intent_id": ""}"#;
        let id = extract_intent_id(payload);
        assert_eq!(id, Some("".to_string()));
    }

    #[test]
    fn test_extract_intent_id_uuid_format() {
        let payload = br#"{"intent_id": "550e8400-e29b-41d4-a716-446655440000"}"#;
        let id = extract_intent_id(payload);
        assert_eq!(id, Some("550e8400-e29b-41d4-a716-446655440000".to_string()));
    }

    #[tokio::test]
    async fn test_nats_client_creation() {
        // This test requires a running NATS server, so it's disabled by default
        // Enable by setting TEST_NATS=1 environment variable

        if std::env::var("TEST_NATS").is_ok() {
            let config = NatsConfig {
                servers: vec!["nats://127.0.0.1:4222".to_string()],
                jetstream_domain: "JS".to_string(),
                tls_cert: None,
                tls_key: None,
                tls_ca: None,
            };

            let client = NatsClient::new(&config).await;
            assert!(client.is_ok(), "Should connect to NATS server");
        }
    }

    #[test]
    fn test_nats_config_clone() {
        let config = NatsConfig {
            servers: vec!["nats://localhost:4222".to_string()],
            jetstream_domain: "JS".to_string(),
            tls_cert: None,
            tls_key: None,
            tls_ca: None,
        };

        let cloned = config.clone();
        assert_eq!(cloned.servers, config.servers);
        assert_eq!(cloned.jetstream_domain, config.jetstream_domain);
    }

    #[test]
    fn test_nats_config_with_tls() {
        use std::path::PathBuf;

        let config = NatsConfig {
            servers: vec!["nats://localhost:4222".to_string()],
            jetstream_domain: "JS".to_string(),
            tls_cert: Some(PathBuf::from("/path/to/cert.pem")),
            tls_key: Some(PathBuf::from("/path/to/key.pem")),
            tls_ca: Some(PathBuf::from("/path/to/ca.pem")),
        };

        assert!(config.tls_cert.is_some());
        assert!(config.tls_key.is_some());
        assert!(config.tls_ca.is_some());
    }

    #[test]
    fn test_nats_config_multiple_servers() {
        let config = NatsConfig {
            servers: vec![
                "nats://server1:4222".to_string(),
                "nats://server2:4222".to_string(),
                "nats://server3:4222".to_string(),
            ],
            jetstream_domain: "JS".to_string(),
            tls_cert: None,
            tls_key: None,
            tls_ca: None,
        };

        assert_eq!(config.servers.len(), 3);
    }

    // ==================== Mock Publisher Tests ====================

    mod mock_tests {
        use super::super::mock::*;
        use super::*;
        use smith_protocol::{AuditRef, ExecutionStatus, IntentResult, RunnerMetadata};

        fn create_test_result(intent_id: &str) -> IntentResult {
            IntentResult {
                intent_id: intent_id.to_string(),
                status: ExecutionStatus::Ok,
                output: Some(serde_json::json!({"test": "output"})),
                error: None,
                started_at_ns: 1000,
                finished_at_ns: 2000,
                runner_meta: RunnerMetadata::empty(),
                audit_ref: AuditRef {
                    id: "test-audit".to_string(),
                    timestamp: 1000,
                    hash: "abc".to_string(),
                },
            }
        }

        #[tokio::test]
        async fn test_mock_publisher_records_calls() {
            let mock = MockResultPublisher::new();
            let result = create_test_result("intent-1");

            mock.publish_result("intent-1", &result).await.unwrap();

            assert_eq!(mock.call_count(), 1);
            let published = mock.published_results();
            assert_eq!(published[0].0, "intent-1");
            assert_eq!(published[0].1.status, ExecutionStatus::Ok);
        }

        #[tokio::test]
        async fn test_mock_publisher_multiple_calls() {
            let mock = MockResultPublisher::new();
            let result1 = create_test_result("intent-1");
            let result2 = create_test_result("intent-2");

            mock.publish_result("intent-1", &result1).await.unwrap();
            mock.publish_result("intent-2", &result2).await.unwrap();

            assert_eq!(mock.call_count(), 2);
        }

        #[tokio::test]
        async fn test_mock_publisher_fail_next() {
            let mock = MockResultPublisher::new();
            let result = create_test_result("intent-1");

            mock.fail_next(1);
            let publish_result = mock.publish_result("intent-1", &result).await;
            assert!(publish_result.is_err());

            // Next call should succeed
            let publish_result = mock.publish_result("intent-1", &result).await;
            assert!(publish_result.is_ok());
        }

        #[tokio::test]
        async fn test_mock_publisher_fail_multiple() {
            let mock = MockResultPublisher::new();
            let result = create_test_result("intent-1");

            mock.fail_next(3);
            assert!(mock.publish_result("intent-1", &result).await.is_err());
            assert!(mock.publish_result("intent-1", &result).await.is_err());
            assert!(mock.publish_result("intent-1", &result).await.is_err());
            assert!(mock.publish_result("intent-1", &result).await.is_ok());
        }

        #[tokio::test]
        async fn test_mock_publisher_clear() {
            let mock = MockResultPublisher::new();
            let result = create_test_result("intent-1");

            mock.publish_result("intent-1", &result).await.unwrap();
            assert_eq!(mock.call_count(), 1);

            mock.clear();
            assert_eq!(mock.call_count(), 0);
        }

        #[tokio::test]
        async fn test_mock_nats_publisher_methods() {
            let mock = MockResultPublisher::new();

            // Test basic publish
            assert!(mock.publish("test.subject", b"payload").await.is_ok());

            // Test publish with reply
            assert!(mock
                .publish_with_reply("test.subject", "reply.to", b"payload")
                .await
                .is_ok());

            // Test request
            let response = mock.request("test.subject", b"request").await.unwrap();
            assert!(response.is_empty());
        }

        #[test]
        fn test_mock_publisher_default() {
            let mock = MockResultPublisher::default();
            assert_eq!(mock.call_count(), 0);
        }
    }
}

fn truncate_payload(payload: &[u8], max_len: usize) -> String {
    let preview = String::from_utf8_lossy(payload);
    preview.chars().take(max_len).collect()
}

fn extract_intent_id(payload: &[u8]) -> Option<String> {
    serde_json::from_slice::<serde_json::Value>(payload)
        .ok()
        .and_then(|value| {
            value
                .get("intent_id")
                .and_then(|v| v.as_str().map(|s| s.to_string()))
        })
}