smith-config 0.1.1

Unified configuration management for agent services
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
//! NATS and JetStream configuration

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use std::time::Duration;

/// NATS connection and JetStream configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NatsConfig {
    /// NATS server URL (nats://, tls://, or ws://)
    pub url: String,

    /// Additional NATS server URLs for clustering
    pub cluster_urls: Vec<String>,

    /// JetStream domain
    pub jetstream_domain: String,

    /// Connection timeout
    #[serde(with = "duration_serde")]
    pub connection_timeout: Duration,

    /// Request timeout for pub/sub operations
    #[serde(with = "duration_serde")]
    pub request_timeout: Duration,

    /// TLS configuration
    pub tls: Option<TlsConfig>,

    /// Authentication configuration
    pub auth: Option<AuthConfig>,

    /// Performance tuning
    pub performance: NatsPerformanceConfig,

    /// Stream and consumer configuration templates
    pub streams: HashMap<String, StreamConfig>,
}

/// TLS configuration for NATS connection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TlsConfig {
    /// Client certificate file path
    pub cert_file: Option<PathBuf>,

    /// Client private key file path
    pub key_file: Option<PathBuf>,

    /// CA certificate file path
    pub ca_file: Option<PathBuf>,

    /// Skip certificate verification (dangerous, only for development)
    pub insecure: bool,
}

/// Authentication configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthConfig {
    /// Username for basic auth
    pub username: Option<String>,

    /// Password for basic auth
    pub password: Option<String>,

    /// JWT token for JWT auth
    pub jwt: Option<String>,

    /// Seed file path for NKey auth
    pub nkey_seed: Option<PathBuf>,

    /// Credentials file path
    pub credentials_file: Option<PathBuf>,
}

/// NATS performance configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NatsPerformanceConfig {
    /// Maximum messages per second rate limit
    pub max_messages_per_second: u64,

    /// Target round-trip latency in milliseconds
    pub target_latency_ms: u64,

    /// Maximum message size in bytes
    pub max_message_size: usize,

    /// Connection pool size
    pub connection_pool_size: usize,

    /// Enable message compression
    pub enable_compression: bool,

    /// Batch size for bulk operations
    pub batch_size: usize,

    /// Flush interval for batched messages
    #[serde(with = "duration_serde")]
    pub flush_interval: Duration,

    /// Reconnection configuration
    pub reconnect: ReconnectConfig,
}

/// Reconnection configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReconnectConfig {
    /// Maximum reconnection attempts (0 = unlimited)
    pub max_attempts: u32,

    /// Initial reconnection delay
    #[serde(with = "duration_serde")]
    pub initial_delay: Duration,

    /// Maximum reconnection delay
    #[serde(with = "duration_serde")]
    pub max_delay: Duration,

    /// Backoff multiplier (exponential backoff)
    pub backoff_multiplier: f64,
}

/// JetStream stream configuration template
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StreamConfig {
    /// Stream name
    pub name: String,

    /// Stream subjects pattern
    pub subjects: Vec<String>,

    /// Maximum age for messages
    pub max_age: String,

    /// Maximum bytes for stream
    pub max_bytes: String,

    /// Maximum messages in stream
    pub max_messages: Option<i64>,

    /// Storage type (file or memory)
    pub storage: String,

    /// Retention policy
    pub retention: String,

    /// Number of replicas
    pub replicas: u32,

    /// Consumer configuration
    pub consumers: HashMap<String, ConsumerConfig>,
}

/// JetStream consumer configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsumerConfig {
    /// Consumer name
    pub name: String,

    /// Delivery subject (for push consumers)
    pub deliver_subject: Option<String>,

    /// Delivery policy (all, last, new, by_start_sequence, by_start_time)
    pub deliver_policy: String,

    /// Ack policy (none, all, explicit)
    pub ack_policy: String,

    /// Ack wait timeout
    pub ack_wait: String,

    /// Maximum delivery attempts
    pub max_deliver: u32,

    /// Filter subject for consuming subset of stream
    pub filter_subject: Option<String>,

    /// Replay policy (instant, original)
    pub replay_policy: String,
}

impl Default for NatsConfig {
    fn default() -> Self {
        let mut streams = HashMap::new();

        // Default intent stream configuration
        streams.insert(
            "intents".to_string(),
            StreamConfig {
                name: "INTENTS".to_string(),
                subjects: vec!["smith.intents.>".to_string()],
                max_age: "10m".to_string(),
                max_bytes: "1GB".to_string(),
                max_messages: None,
                storage: "file".to_string(),
                retention: "limits".to_string(),
                replicas: 1,
                consumers: {
                    let mut consumers = HashMap::new();
                    consumers.insert(
                        "executor".to_string(),
                        ConsumerConfig {
                            name: "executor".to_string(),
                            deliver_subject: None, // Pull consumer
                            deliver_policy: "new".to_string(),
                            ack_policy: "explicit".to_string(),
                            ack_wait: "30s".to_string(),
                            max_deliver: 3,
                            filter_subject: None,
                            replay_policy: "instant".to_string(),
                        },
                    );
                    consumers
                },
            },
        );

        // Default results stream configuration
        streams.insert(
            "results".to_string(),
            StreamConfig {
                name: "RESULTS".to_string(),
                subjects: vec!["smith.results.>".to_string()],
                max_age: "5m".to_string(),
                max_bytes: "512MB".to_string(),
                max_messages: None,
                storage: "file".to_string(),
                retention: "limits".to_string(),
                replicas: 1,
                consumers: {
                    let mut consumers = HashMap::new();
                    consumers.insert(
                        "http".to_string(),
                        ConsumerConfig {
                            name: "http".to_string(),
                            deliver_subject: None,
                            deliver_policy: "new".to_string(),
                            ack_policy: "explicit".to_string(),
                            ack_wait: "10s".to_string(),
                            max_deliver: 2,
                            filter_subject: None,
                            replay_policy: "instant".to_string(),
                        },
                    );
                    consumers
                },
            },
        );

        Self {
            url: "nats://127.0.0.1:4222".to_string(),
            cluster_urls: vec![],
            jetstream_domain: "JS".to_string(),
            connection_timeout: Duration::from_secs(5),
            request_timeout: Duration::from_millis(100),
            tls: None,
            auth: None,
            performance: NatsPerformanceConfig::default(),
            streams,
        }
    }
}

impl Default for NatsPerformanceConfig {
    fn default() -> Self {
        Self {
            max_messages_per_second: 1000,
            target_latency_ms: 20,
            max_message_size: 1024 * 1024, // 1MB
            connection_pool_size: 4,
            enable_compression: false, // Latency over bandwidth
            batch_size: 10,
            flush_interval: Duration::from_millis(10),
            reconnect: ReconnectConfig::default(),
        }
    }
}

impl Default for ReconnectConfig {
    fn default() -> Self {
        Self {
            max_attempts: 10,
            initial_delay: Duration::from_millis(100),
            max_delay: Duration::from_secs(10),
            backoff_multiplier: 2.0,
        }
    }
}

impl NatsConfig {
    /// Validate the top-level NATS connection, TLS, and stream settings.
    pub fn validate(&self) -> Result<()> {
        // Validate URL
        if self.url.is_empty() {
            return Err(anyhow::anyhow!("NATS URL cannot be empty"));
        }

        url::Url::parse(&self.url)
            .map_err(|e| anyhow::anyhow!("Invalid NATS URL '{}': {}", self.url, e))?;

        // Validate cluster URLs
        for url in &self.cluster_urls {
            url::Url::parse(url)
                .map_err(|e| anyhow::anyhow!("Invalid cluster URL '{}': {}", url, e))?;
        }

        // Validate JetStream domain
        if self.jetstream_domain.is_empty() {
            return Err(anyhow::anyhow!("JetStream domain cannot be empty"));
        }

        // Validate timeouts
        if self.connection_timeout.as_millis() == 0 {
            return Err(anyhow::anyhow!("Connection timeout must be > 0"));
        }

        if self.request_timeout.as_millis() == 0 {
            return Err(anyhow::anyhow!("Request timeout must be > 0"));
        }

        // Validate TLS config if present
        if let Some(ref tls) = self.tls {
            tls.validate()?;
        }

        // Validate auth config if present
        if let Some(ref auth) = self.auth {
            auth.validate()?;
        }

        // Validate performance config
        self.performance.validate()?;

        // Validate stream configurations
        for (name, stream) in &self.streams {
            stream
                .validate()
                .map_err(|e| anyhow::anyhow!("Stream '{}' validation failed: {}", name, e))?;
        }

        Ok(())
    }

    /// Development profile optimized for localhost experimentation.
    pub fn development() -> Self {
        Self {
            url: "nats://127.0.0.1:4222".to_string(),
            performance: NatsPerformanceConfig {
                target_latency_ms: 50, // Relaxed for development
                ..Default::default()
            },
            ..Default::default()
        }
    }

    /// Production profile with clustering and higher throughput defaults.
    pub fn production() -> Self {
        Self {
            url: "nats://nats-cluster:4222".to_string(),
            cluster_urls: vec![
                "nats://nats-1:4222".to_string(),
                "nats://nats-2:4222".to_string(),
                "nats://nats-3:4222".to_string(),
            ],
            connection_timeout: Duration::from_secs(10),
            request_timeout: Duration::from_millis(50),
            performance: NatsPerformanceConfig {
                max_messages_per_second: 2000,
                target_latency_ms: 10,
                connection_pool_size: 8,
                ..Default::default()
            },
            streams: {
                let mut streams = HashMap::new();

                // Production intent stream with replication
                streams.insert(
                    "intents".to_string(),
                    StreamConfig {
                        name: "INTENTS".to_string(),
                        subjects: vec!["smith.intents.>".to_string()],
                        max_age: "10m".to_string(),
                        max_bytes: "5GB".to_string(),
                        max_messages: None,
                        storage: "file".to_string(),
                        retention: "limits".to_string(),
                        replicas: 3,
                        consumers: HashMap::new(),
                    },
                );

                streams.insert(
                    "results".to_string(),
                    StreamConfig {
                        name: "RESULTS".to_string(),
                        subjects: vec!["smith.results.>".to_string()],
                        max_age: "5m".to_string(),
                        max_bytes: "2GB".to_string(),
                        max_messages: None,
                        storage: "file".to_string(),
                        retention: "limits".to_string(),
                        replicas: 3,
                        consumers: HashMap::new(),
                    },
                );

                streams
            },
            ..Default::default()
        }
    }

    /// Lightweight profile used in CI where infrastructure is mocked.
    pub fn testing() -> Self {
        Self {
            url: "nats://127.0.0.1:4222".to_string(),
            request_timeout: Duration::from_millis(500), // Generous for CI
            performance: NatsPerformanceConfig {
                max_messages_per_second: 100, // Limited for tests
                batch_size: 5,                // Smaller batches
                ..Default::default()
            },
            streams: HashMap::new(), // No default streams for tests
            ..Default::default()
        }
    }
}

impl TlsConfig {
    /// Ensure referenced TLS artifacts exist on disk.
    pub fn validate(&self) -> Result<()> {
        if let Some(ref cert_file) = self.cert_file {
            if !cert_file.exists() {
                return Err(anyhow::anyhow!(
                    "TLS cert file does not exist: {}",
                    cert_file.display()
                ));
            }
        }

        if let Some(ref key_file) = self.key_file {
            if !key_file.exists() {
                return Err(anyhow::anyhow!(
                    "TLS key file does not exist: {}",
                    key_file.display()
                ));
            }
        }

        if let Some(ref ca_file) = self.ca_file {
            if !ca_file.exists() {
                return Err(anyhow::anyhow!(
                    "TLS CA file does not exist: {}",
                    ca_file.display()
                ));
            }
        }

        Ok(())
    }
}

impl AuthConfig {
    /// Ensure exactly one authentication mechanism is configured correctly.
    pub fn validate(&self) -> Result<()> {
        // At most one auth method should be configured
        let auth_methods = [
            self.username.is_some() && self.password.is_some(),
            self.jwt.is_some(),
            self.nkey_seed.is_some(),
            self.credentials_file.is_some(),
        ];

        let auth_count = auth_methods.iter().filter(|&&x| x).count();
        if auth_count > 1 {
            return Err(anyhow::anyhow!(
                "Multiple authentication methods configured. Use only one."
            ));
        }

        if let Some(ref nkey_file) = self.nkey_seed {
            if !nkey_file.exists() {
                return Err(anyhow::anyhow!(
                    "NKey seed file does not exist: {}",
                    nkey_file.display()
                ));
            }
        }

        if let Some(ref creds_file) = self.credentials_file {
            if !creds_file.exists() {
                return Err(anyhow::anyhow!(
                    "Credentials file does not exist: {}",
                    creds_file.display()
                ));
            }
        }

        Ok(())
    }
}

impl NatsPerformanceConfig {
    /// Validate batching, pool sizing, and reconnect tuning before use.
    pub fn validate(&self) -> Result<()> {
        if self.max_messages_per_second == 0 {
            return Err(anyhow::anyhow!("Max messages per second must be > 0"));
        }

        if self.target_latency_ms > 1000 {
            tracing::warn!("Target latency > 1000ms may impact system performance");
        }

        if self.max_message_size < 1024 {
            return Err(anyhow::anyhow!("Max message size must be >= 1KB"));
        }

        if self.connection_pool_size == 0 {
            return Err(anyhow::anyhow!("Connection pool size must be > 0"));
        }

        if self.batch_size == 0 {
            return Err(anyhow::anyhow!("Batch size must be > 0"));
        }

        self.reconnect.validate()?;

        Ok(())
    }
}

impl ReconnectConfig {
    /// Ensure reconnect backoff parameters are positive and ordered correctly.
    pub fn validate(&self) -> Result<()> {
        if self.initial_delay.as_millis() == 0 {
            return Err(anyhow::anyhow!("Initial delay must be > 0"));
        }

        if self.max_delay < self.initial_delay {
            return Err(anyhow::anyhow!("Max delay must be >= initial delay"));
        }

        if self.backoff_multiplier <= 1.0 {
            return Err(anyhow::anyhow!("Backoff multiplier must be > 1.0"));
        }

        Ok(())
    }
}

impl StreamConfig {
    /// Validate stream metadata, retention, and nested consumer templates.
    pub fn validate(&self) -> Result<()> {
        if self.name.is_empty() {
            return Err(anyhow::anyhow!("Stream name cannot be empty"));
        }

        if self.subjects.is_empty() {
            return Err(anyhow::anyhow!("Stream must have at least one subject"));
        }

        // Validate storage type
        if !["file", "memory"].contains(&self.storage.as_str()) {
            return Err(anyhow::anyhow!(
                "Invalid storage type: {}. Must be 'file' or 'memory'",
                self.storage
            ));
        }

        // Validate retention policy
        if !["limits", "interest", "workqueue"].contains(&self.retention.as_str()) {
            return Err(anyhow::anyhow!(
                "Invalid retention policy: {}. Must be 'limits', 'interest', or 'workqueue'",
                self.retention
            ));
        }

        if self.replicas == 0 {
            return Err(anyhow::anyhow!("Stream replicas must be > 0"));
        }

        // Validate consumers
        for (name, consumer) in &self.consumers {
            consumer
                .validate()
                .map_err(|e| anyhow::anyhow!("Consumer '{}' validation failed: {}", name, e))?;
        }

        Ok(())
    }
}

impl ConsumerConfig {
    /// Validate deliver/ack policies and replay configuration for a consumer.
    pub fn validate(&self) -> Result<()> {
        if self.name.is_empty() {
            return Err(anyhow::anyhow!("Consumer name cannot be empty"));
        }

        // Validate deliver policy
        let valid_policies = ["all", "last", "new", "by_start_sequence", "by_start_time"];
        if !valid_policies.contains(&self.deliver_policy.as_str()) {
            return Err(anyhow::anyhow!(
                "Invalid deliver policy: {}. Must be one of: {}",
                self.deliver_policy,
                valid_policies.join(", ")
            ));
        }

        // Validate ack policy
        if !["none", "all", "explicit"].contains(&self.ack_policy.as_str()) {
            return Err(anyhow::anyhow!(
                "Invalid ack policy: {}. Must be 'none', 'all', or 'explicit'",
                self.ack_policy
            ));
        }

        // Validate replay policy
        if !["instant", "original"].contains(&self.replay_policy.as_str()) {
            return Err(anyhow::anyhow!(
                "Invalid replay policy: {}. Must be 'instant' or 'original'",
                self.replay_policy
            ));
        }

        if self.max_deliver == 0 {
            return Err(anyhow::anyhow!("Max deliver must be > 0"));
        }

        Ok(())
    }
}

/// Helper module for serializing durations as millisecond integers.
pub(crate) mod duration_serde {
    use serde::{Deserialize, Deserializer, Serializer};
    use std::time::Duration;

    /// Serialize a `Duration` using millisecond precision for config files.
    pub fn serialize<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_u64(duration.as_millis() as u64)
    }

    /// Deserialize a millisecond count into a `Duration`.
    pub fn deserialize<'de, D>(deserializer: D) -> Result<Duration, D::Error>
    where
        D: Deserializer<'de>,
    {
        let millis = u64::deserialize(deserializer)?;
        Ok(Duration::from_millis(millis))
    }
}