tatara-core 0.2.77

Shared domain types, config, and cluster types for tatara
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
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

use crate::cluster::roles::RoleConfig;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerConfig {
    #[serde(default = "default_http_addr")]
    pub http_addr: String,
    #[serde(default = "default_grpc_addr")]
    pub grpc_addr: String,
    #[serde(default = "default_log_level")]
    pub log_level: String,
    #[serde(default)]
    pub state: StateConfig,
    #[serde(default)]
    pub scheduler: SchedulerConfig,
    #[serde(default)]
    pub cluster: ClusterConfig,
    #[serde(default)]
    pub p2p: P2pConfig,
    #[serde(default)]
    pub kindling: KindlingConfig,
    #[serde(default)]
    pub reconciler: ReconcilerConfig,
    #[serde(default)]
    pub nats: NatsConfig,
    #[serde(default)]
    pub sui: SuiConfig,
    #[serde(default)]
    pub ports: PortConfig,
    #[serde(default)]
    pub volumes: VolumeConfig,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StateConfig {
    #[serde(default = "default_server_state_dir")]
    pub dir: PathBuf,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SchedulerConfig {
    #[serde(default = "default_eval_interval")]
    pub eval_interval_secs: u64,
    #[serde(default = "default_heartbeat_grace")]
    pub heartbeat_grace_secs: u64,
}

/// Reconciler configuration — controls the reconciliation loop.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReconcilerConfig {
    /// Reconciliation loop interval in seconds.
    #[serde(default = "default_reconcile_interval")]
    pub reconcile_interval_secs: u64,

    /// Nix re-evaluation happens every Nth reconcile tick.
    #[serde(default = "default_reeval_frequency")]
    pub reeval_every_n_ticks: u64,

    /// Max concurrent Nix evaluations.
    #[serde(default = "default_max_concurrent_evals")]
    pub max_concurrent_evals: usize,

    /// Enable spec drift detection via Nix re-evaluation.
    #[serde(default = "default_true")]
    pub drift_detection: bool,

    /// Enable source reconciliation (Pass 5).
    #[serde(default = "default_true")]
    pub source_reconciliation: bool,

    /// Source re-check happens every Nth reconcile tick.
    #[serde(default = "default_source_reeval_frequency")]
    pub source_reeval_every_n_ticks: u64,

    /// Timeout for `nix flake metadata` calls (seconds).
    #[serde(default = "default_flake_metadata_timeout")]
    pub flake_metadata_timeout_secs: u64,
}

/// Cluster configuration — gossip, raft, discovery.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClusterConfig {
    /// Unique cluster identifier (nodes with different IDs ignore each other).
    #[serde(default = "default_cluster_id")]
    pub cluster_id: String,

    /// Gossip listen address (UDP).
    #[serde(default = "default_gossip_addr")]
    pub gossip_addr: String,

    /// Raft listen address (HTTP-based RPCs).
    #[serde(default = "default_raft_addr")]
    pub raft_addr: String,

    /// Static seed peers for gossip bootstrap (host:port).
    /// In addition to mDNS and kindling fleet discovery.
    #[serde(default)]
    pub seed_peers: Vec<String>,

    /// Enable mDNS discovery on local network.
    #[serde(default = "default_true")]
    pub mdns_discovery: bool,

    /// Use kindling fleet peers as gossip seeds.
    #[serde(default = "default_true")]
    pub kindling_fleet_seeds: bool,

    /// Node roles.
    #[serde(default)]
    pub roles: RoleConfig,

    /// Bootstrap as single-node cluster if no peers found.
    #[serde(default = "default_true")]
    pub auto_bootstrap: bool,
}

/// P2P content-addressed cache configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct P2pConfig {
    /// Cache directory.
    #[serde(default = "default_p2p_cache_dir")]
    pub cache_dir: PathBuf,

    /// Maximum cache size in MB. 0 = unlimited.
    #[serde(default)]
    pub max_cache_mb: u64,

    /// Auto-replicate: eagerly fetch data from peers in background.
    #[serde(default = "default_true")]
    pub eager_replication: bool,
}

/// Kindling integration configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KindlingConfig {
    /// Path to kindling's node.yaml. Empty = auto-detect.
    #[serde(default)]
    pub identity_path: Option<String>,

    /// Path to kindling's report.json. Empty = auto-detect.
    #[serde(default)]
    pub report_path: Option<String>,

    /// Kindling daemon HTTP address (for API client).
    /// Empty = auto-detect from default kindling port.
    #[serde(default = "default_kindling_addr")]
    pub daemon_addr: String,

    /// Publish kindling reports to the p2p cache.
    #[serde(default = "default_true")]
    pub publish_reports: bool,

    /// Report refresh interval (seconds). 0 = use kindling's own interval.
    #[serde(default)]
    pub report_refresh_secs: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClientConfig {
    #[serde(default = "default_client_server_addr")]
    pub server_addr: String,
    #[serde(default = "default_log_level")]
    pub log_level: String,
    #[serde(default = "default_alloc_dir")]
    pub alloc_dir: PathBuf,
    #[serde(default)]
    pub resources: ResourceConfig,
    #[serde(default)]
    pub drivers: DriverConfig,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceConfig {
    #[serde(default)]
    pub cpu_mhz: u64,
    #[serde(default)]
    pub memory_mb: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DriverConfig {
    #[serde(default = "default_true")]
    pub exec: bool,
    #[serde(default = "default_true")]
    pub oci: bool,
    #[serde(default = "default_true")]
    pub nix: bool,
}

impl ServerConfig {
    pub fn load(path: Option<&str>) -> anyhow::Result<Self> {
        let config_path = match path {
            Some(p) => PathBuf::from(p),
            None => default_config_dir().join("server.toml"),
        };

        if config_path.exists() {
            let contents = std::fs::read_to_string(&config_path)?;
            Ok(toml::from_str(&contents)?)
        } else {
            Ok(Self::default())
        }
    }
}

impl ClientConfig {
    pub fn load(path: Option<&str>) -> anyhow::Result<Self> {
        let config_path = match path {
            Some(p) => PathBuf::from(p),
            None => default_config_dir().join("client.toml"),
        };

        if config_path.exists() {
            let contents = std::fs::read_to_string(&config_path)?;
            Ok(toml::from_str(&contents)?)
        } else {
            Ok(Self::default())
        }
    }
}

impl Default for ServerConfig {
    fn default() -> Self {
        Self {
            http_addr: default_http_addr(),
            grpc_addr: default_grpc_addr(),
            log_level: default_log_level(),
            state: StateConfig::default(),
            scheduler: SchedulerConfig::default(),
            cluster: ClusterConfig::default(),
            p2p: P2pConfig::default(),
            kindling: KindlingConfig::default(),
            reconciler: ReconcilerConfig::default(),
            nats: NatsConfig::default(),
            sui: SuiConfig::default(),
            ports: PortConfig::default(),
            volumes: VolumeConfig::default(),
        }
    }
}

impl Default for ReconcilerConfig {
    fn default() -> Self {
        Self {
            reconcile_interval_secs: default_reconcile_interval(),
            reeval_every_n_ticks: default_reeval_frequency(),
            max_concurrent_evals: default_max_concurrent_evals(),
            drift_detection: true,
            source_reconciliation: true,
            source_reeval_every_n_ticks: default_source_reeval_frequency(),
            flake_metadata_timeout_secs: default_flake_metadata_timeout(),
        }
    }
}

impl Default for ClientConfig {
    fn default() -> Self {
        Self {
            server_addr: default_client_server_addr(),
            log_level: default_log_level(),
            alloc_dir: default_alloc_dir(),
            resources: ResourceConfig::default(),
            drivers: DriverConfig::default(),
        }
    }
}

impl Default for StateConfig {
    fn default() -> Self {
        Self {
            dir: default_server_state_dir(),
        }
    }
}

impl Default for SchedulerConfig {
    fn default() -> Self {
        Self {
            eval_interval_secs: default_eval_interval(),
            heartbeat_grace_secs: default_heartbeat_grace(),
        }
    }
}

impl Default for ClusterConfig {
    fn default() -> Self {
        Self {
            cluster_id: default_cluster_id(),
            gossip_addr: default_gossip_addr(),
            raft_addr: default_raft_addr(),
            seed_peers: Vec::new(),
            mdns_discovery: true,
            kindling_fleet_seeds: true,
            roles: RoleConfig::default(),
            auto_bootstrap: true,
        }
    }
}

impl Default for P2pConfig {
    fn default() -> Self {
        Self {
            cache_dir: default_p2p_cache_dir(),
            max_cache_mb: 0,
            eager_replication: true,
        }
    }
}

impl Default for KindlingConfig {
    fn default() -> Self {
        Self {
            identity_path: None,
            report_path: None,
            daemon_addr: default_kindling_addr(),
            publish_reports: true,
            report_refresh_secs: 0,
        }
    }
}

impl Default for ResourceConfig {
    fn default() -> Self {
        Self {
            cpu_mhz: 0,
            memory_mb: 0,
        }
    }
}

impl Default for DriverConfig {
    fn default() -> Self {
        Self {
            exec: true,
            oci: true,
            nix: true,
        }
    }
}

fn default_config_dir() -> PathBuf {
    dirs::config_dir()
        .unwrap_or_else(|| PathBuf::from("~/.config"))
        .join("tatara")
}

fn default_data_dir() -> PathBuf {
    dirs::data_local_dir()
        .unwrap_or_else(|| PathBuf::from("~/.local/share"))
        .join("tatara")
}

fn default_http_addr() -> String {
    "0.0.0.0:4646".to_string()
}

fn default_grpc_addr() -> String {
    "0.0.0.0:4647".to_string()
}

fn default_gossip_addr() -> String {
    "0.0.0.0:4648".to_string()
}

fn default_raft_addr() -> String {
    "0.0.0.0:4649".to_string()
}

fn default_log_level() -> String {
    "info".to_string()
}

fn default_cluster_id() -> String {
    "tatara-default".to_string()
}

fn default_server_state_dir() -> PathBuf {
    default_data_dir().join("server")
}

fn default_alloc_dir() -> PathBuf {
    default_data_dir().join("alloc")
}

fn default_p2p_cache_dir() -> PathBuf {
    default_data_dir().join("p2p")
}

fn default_client_server_addr() -> String {
    "127.0.0.1:4647".to_string()
}

fn default_eval_interval() -> u64 {
    1
}

fn default_reconcile_interval() -> u64 {
    10
}

fn default_reeval_frequency() -> u64 {
    6
}

fn default_max_concurrent_evals() -> usize {
    2
}

fn default_heartbeat_grace() -> u64 {
    30
}

fn default_source_reeval_frequency() -> u64 {
    3
}

fn default_flake_metadata_timeout() -> u64 {
    30
}

fn default_kindling_addr() -> String {
    "http://127.0.0.1:3000".to_string()
}

fn default_true() -> bool {
    true
}

// ── New subsystem configs ───────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NatsConfig {
    #[serde(default)]
    pub enabled: bool,
    #[serde(default = "default_nats_url")]
    pub url: String,
}

impl Default for NatsConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            url: default_nats_url(),
        }
    }
}

fn default_nats_url() -> String {
    "nats://127.0.0.1:4222".to_string()
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SuiConfig {
    #[serde(default)]
    pub daemon_addr: Option<String>,
}

impl Default for SuiConfig {
    fn default() -> Self {
        Self { daemon_addr: None }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PortConfig {
    #[serde(default = "default_port_start")]
    pub range_start: u16,
    #[serde(default = "default_port_end")]
    pub range_end: u16,
}

impl Default for PortConfig {
    fn default() -> Self {
        Self {
            range_start: default_port_start(),
            range_end: default_port_end(),
        }
    }
}

fn default_port_start() -> u16 {
    20000
}
fn default_port_end() -> u16 {
    32000
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VolumeConfig {
    #[serde(default = "default_volume_dir")]
    pub dir: PathBuf,
}

impl Default for VolumeConfig {
    fn default() -> Self {
        Self {
            dir: default_volume_dir(),
        }
    }
}

fn default_volume_dir() -> PathBuf {
    dirs::data_dir()
        .unwrap_or_else(|| PathBuf::from("/tmp"))
        .join("tatara")
        .join("volumes")
}

// ── shikumi::TieredConfig — prime directive ────────────────
//
// Operators reach via:
//   tatara config-show bare           # zero-opinion floor
//   tatara config-show default        # prescribed defaults
//   TATARA_TIER=bare tatara server    # explicit tier override
//
// bare() = empty addrs, zero intervals, false flags. The documented
// minimum that doesn't bind any port, gossip, raft, or NATS endpoint.
// prescribed_default() = current Default impl (curated tatara server
// + client defaults).

impl shikumi::TieredConfig for ServerConfig {
    fn bare() -> Self {
        Self {
            http_addr: String::new(),
            grpc_addr: String::new(),
            log_level: String::new(),
            state: <StateConfig as shikumi::TieredConfig>::bare(),
            scheduler: <SchedulerConfig as shikumi::TieredConfig>::bare(),
            cluster: <ClusterConfig as shikumi::TieredConfig>::bare(),
            p2p: <P2pConfig as shikumi::TieredConfig>::bare(),
            kindling: <KindlingConfig as shikumi::TieredConfig>::bare(),
            reconciler: <ReconcilerConfig as shikumi::TieredConfig>::bare(),
            nats: <NatsConfig as shikumi::TieredConfig>::bare(),
            sui: <SuiConfig as shikumi::TieredConfig>::bare(),
            ports: <PortConfig as shikumi::TieredConfig>::bare(),
            volumes: <VolumeConfig as shikumi::TieredConfig>::bare(),
        }
    }
    fn prescribed_default() -> Self { Self::default() }
}

impl shikumi::TieredConfig for ClientConfig {
    fn bare() -> Self {
        Self {
            server_addr: String::new(),
            log_level: String::new(),
            alloc_dir: PathBuf::new(),
            resources: ResourceConfig::default(),
            drivers: <DriverConfig as shikumi::TieredConfig>::bare(),
        }
    }
    fn prescribed_default() -> Self { Self::default() }
}

impl shikumi::TieredConfig for StateConfig {
    fn bare() -> Self { Self { dir: PathBuf::new() } }
    fn prescribed_default() -> Self { Self::default() }
}

impl shikumi::TieredConfig for SchedulerConfig {
    fn bare() -> Self { Self { eval_interval_secs: 0, heartbeat_grace_secs: 0 } }
    fn prescribed_default() -> Self { Self::default() }
}

impl shikumi::TieredConfig for ReconcilerConfig {
    fn bare() -> Self {
        Self {
            reconcile_interval_secs: 0,
            reeval_every_n_ticks: 0,
            max_concurrent_evals: 0,
            drift_detection: false,
            source_reconciliation: false,
            source_reeval_every_n_ticks: 0,
            flake_metadata_timeout_secs: 0,
        }
    }
    fn prescribed_default() -> Self { Self::default() }
}

impl shikumi::TieredConfig for ClusterConfig {
    fn bare() -> Self {
        Self {
            cluster_id: String::new(),
            gossip_addr: String::new(),
            raft_addr: String::new(),
            seed_peers: Vec::new(),
            mdns_discovery: false,
            kindling_fleet_seeds: false,
            roles: RoleConfig::default(),
            auto_bootstrap: false,
        }
    }
    fn prescribed_default() -> Self { Self::default() }
}

impl shikumi::TieredConfig for P2pConfig {
    fn bare() -> Self {
        Self {
            cache_dir: PathBuf::new(),
            max_cache_mb: 0,
            eager_replication: false,
        }
    }
    fn prescribed_default() -> Self { Self::default() }
}

impl shikumi::TieredConfig for KindlingConfig {
    fn bare() -> Self { <Self as shikumi::TieredConfig>::prescribed_default() }
    fn prescribed_default() -> Self { Self::default() }
}

impl shikumi::TieredConfig for DriverConfig {
    fn bare() -> Self { <Self as shikumi::TieredConfig>::prescribed_default() }
    fn prescribed_default() -> Self { Self::default() }
}

impl shikumi::TieredConfig for NatsConfig {
    fn bare() -> Self { <Self as shikumi::TieredConfig>::prescribed_default() }
    fn prescribed_default() -> Self { Self::default() }
}

impl shikumi::TieredConfig for SuiConfig {
    fn bare() -> Self { <Self as shikumi::TieredConfig>::prescribed_default() }
    fn prescribed_default() -> Self { Self::default() }
}

impl shikumi::TieredConfig for PortConfig {
    fn bare() -> Self { <Self as shikumi::TieredConfig>::prescribed_default() }
    fn prescribed_default() -> Self { Self::default() }
}

impl shikumi::TieredConfig for VolumeConfig {
    fn bare() -> Self { Self { dir: PathBuf::new() } }
    fn prescribed_default() -> Self { Self::default() }
}

#[cfg(test)]
mod tiered_tests {
    use super::*;
    use shikumi::{ConfigTier, TieredConfig};

    #[test]
    fn server_config_bare_is_zero_opinion() {
        let b = <ServerConfig as TieredConfig>::bare();
        assert_eq!(b.http_addr, "");
        assert_eq!(b.grpc_addr, "");
        assert_eq!(b.log_level, "");
        assert!(b.state.dir.as_os_str().is_empty());
        assert_eq!(b.scheduler.eval_interval_secs, 0);
        assert!(!b.cluster.mdns_discovery);
        assert!(!b.reconciler.drift_detection);
    }

    #[test]
    fn server_config_prescribed_matches_default() {
        let p = <ServerConfig as TieredConfig>::prescribed_default();
        let d = ServerConfig::default();
        assert_eq!(p.http_addr, d.http_addr);
        assert_eq!(p.log_level, d.log_level);
    }

    #[test]
    fn server_config_diff_bare_vs_default_is_non_empty() {
        let b = <ServerConfig as TieredConfig>::bare();
        let d = <ServerConfig as TieredConfig>::prescribed_default();
        let diff = d.diff_against(&b);
        assert!(!diff.is_empty_diff(), "bare and prescribed_default must differ");
    }

    #[test]
    fn server_config_resolve_tier_dispatches_correctly() {
        assert_eq!(
            <ServerConfig as TieredConfig>::resolve_tier(ConfigTier::Bare).http_addr,
            ""
        );
        assert!(!<ServerConfig as TieredConfig>::resolve_tier(ConfigTier::Default)
            .http_addr
            .is_empty());
    }

    #[test]
    fn client_config_bare_is_zero_opinion() {
        let b = <ClientConfig as TieredConfig>::bare();
        assert_eq!(b.server_addr, "");
        assert_eq!(b.log_level, "");
        assert!(b.alloc_dir.as_os_str().is_empty());
    }
}