turbovault-core 1.6.0

Core data models and types for TurboVault Server
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
//! Configuration types for the Obsidian server.
//!
//! Follows a builder pattern for complex configuration with validation.

use crate::error::{Error, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::path::Path;
use std::path::PathBuf;

/// Selects which write path serves a vault's mutations (GWS.11).
///
/// **Short-lived**: this flag exists so the git-native substrate
/// (`turbovault-git`) can be wired alongside the legacy `VaultManager` path
/// behind a per-vault switch during the cutover (GWS.15). At cutover the
/// default flips to `Git`, the legacy path is deleted, and the flag is removed
/// from this config entirely.
///
/// Per-vault by design: the substrate's working-tree-equals-HEAD invariant
/// forbids mixing within one vault (a legacy write commits nothing, leaving
/// the working tree out of sync with the git tip), so a vault is one or the
/// other end-to-end.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum WriteBackend {
    /// The legacy `VaultManager` mutators + `BatchExecutor` (default until cutover).
    #[default]
    Legacy,
    /// The git-native write substrate (`turbovault-git`). Requires the vault
    /// path to be a git repository.
    Git,
}

/// How the git substrate merges a fan-out's wip branch back into main
/// (mirrors `turbovault_git::MergeStrategy` as a serializable config type so
/// `turbovault-core` doesn't pick up a git2/libgit2 dependency). The consumer
/// converts at the substrate boundary.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum GitMergeStrategy {
    /// `git merge --no-ff` — preserves the wip branch's per-transaction
    /// commits with a merge commit on main. The default.
    #[default]
    MergeCommit,
    /// Advance main directly to the wip tip — errors if main advanced
    /// concurrently (caller falls back to `MergeCommit`).
    FastForward,
}

/// Commit identity for git-backed writes. Optional in the config — when
/// absent, the substrate falls back to the repo's `user.name`/`user.email`
/// and then to a built-in TurboVault default.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitAuthor {
    pub name: String,
    pub email: String,
}

/// Per-vault git substrate configuration. Only meaningful when
/// [`VaultConfig::write_backend`] is `Git`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VaultGitConfig {
    /// Target branch for commits. `None` = use the repo's current HEAD branch.
    #[serde(default)]
    pub branch: Option<String>,
    /// Commit author identity. `None` = repo's git config -> TurboVault default.
    #[serde(default)]
    pub author: Option<GitAuthor>,
    /// Default merge strategy for fan-out merge-back (`commit_transaction`).
    #[serde(default)]
    pub merge_strategy: GitMergeStrategy,
    /// turbovault-lri: when `false`, every git-backend mutation pre-checks
    /// each touched path against the worktree's `.gitignore` matcher and
    /// refuses the transaction (typed config error) if any path would be
    /// excluded. When `true` (the default), `.gitignore` is ignored and
    /// every requested path is committed — the original always-write
    /// behavior. Useful for vaults that gitignore `.obsidian/`, build
    /// artifacts, or per-user clutter and want a backstop against an MCP
    /// client accidentally committing them.
    #[serde(default = "default_include_ignored")]
    pub include_ignored: bool,
    /// turbovault-5nn: when `true`, every git-backend mutation MUST carry a
    /// caller-supplied commit message — a tool called without one (or with a
    /// blank/whitespace-only one) is refused loudly instead of falling back to
    /// the auto-derived subject (`write_note <path>`, etc.). Default `false`
    /// preserves the auto-derive behavior. Only meaningful on the git backend
    /// (the legacy backend produces no commits, so a message is moot).
    #[serde(default)]
    pub require_commit_message: bool,
}

fn default_include_ignored() -> bool {
    true
}

// Manual `Default` so `VaultGitConfig::default().include_ignored == true`,
// matching the serde-default for that field (derive(Default) on a bool yields
// `false`, which would disagree with the missing-field deserialization).
impl Default for VaultGitConfig {
    fn default() -> Self {
        Self {
            branch: None,
            author: None,
            merge_strategy: GitMergeStrategy::default(),
            include_ignored: default_include_ignored(),
            require_commit_message: false,
        }
    }
}

/// Configuration for a single vault
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VaultConfig {
    /// Unique identifier for this vault
    pub name: String,
    /// Path to the vault directory
    pub path: PathBuf,
    /// Whether this is the default vault
    pub is_default: bool,

    // Optional overrides
    pub watch_for_changes: Option<bool>,
    pub max_file_size: Option<u64>,
    pub allowed_extensions: Option<HashSet<String>>,
    pub excluded_paths: Option<HashSet<String>>,
    pub enable_caching: Option<bool>,
    pub cache_ttl: Option<u64>,
    pub template_dirs: Option<Vec<PathBuf>>,
    pub allowed_operations: Option<HashSet<String>>,

    /// Write backend selection (GWS.11). Default `Legacy` until cutover.
    #[serde(default)]
    pub write_backend: WriteBackend,
    /// Git substrate settings. Only used when `write_backend == Git`.
    #[serde(default)]
    pub git: Option<VaultGitConfig>,
}

impl VaultConfig {
    /// Create a new vault config with builder
    pub fn builder(name: impl Into<String>, path: impl Into<PathBuf>) -> VaultConfigBuilder {
        VaultConfigBuilder::new(name, path)
    }

    /// Validate the vault configuration
    pub fn validate(&self) -> Result<()> {
        if self.name.is_empty() {
            return Err(Error::config_error("Vault name cannot be empty"));
        }

        if !self.path.exists() {
            std::fs::create_dir_all(&self.path).map_err(|e| {
                Error::config_error(format!(
                    "Vault path does not exist and could not be created: {} ({})",
                    self.path.display(),
                    e
                ))
            })?;
        }

        if !self.path.is_dir() {
            return Err(Error::config_error(format!(
                "Vault path is not a directory: {}",
                self.path.display()
            )));
        }

        Ok(())
    }
}

/// Builder for VaultConfig
pub struct VaultConfigBuilder {
    name: String,
    path: PathBuf,
    is_default: bool,
    watch_for_changes: Option<bool>,
    max_file_size: Option<u64>,
    allowed_extensions: Option<HashSet<String>>,
    excluded_paths: Option<HashSet<String>>,
    enable_caching: Option<bool>,
    cache_ttl: Option<u64>,
    template_dirs: Option<Vec<PathBuf>>,
    allowed_operations: Option<HashSet<String>>,
    write_backend: WriteBackend,
    git: Option<VaultGitConfig>,
}

impl VaultConfigBuilder {
    /// Create a new builder
    pub fn new(name: impl Into<String>, path: impl Into<PathBuf>) -> Self {
        Self {
            name: name.into(),
            path: path.into(),
            is_default: false,
            watch_for_changes: None,
            max_file_size: None,
            allowed_extensions: None,
            excluded_paths: None,
            enable_caching: None,
            cache_ttl: None,
            template_dirs: None,
            allowed_operations: None,
            write_backend: WriteBackend::default(),
            git: None,
        }
    }

    /// Mark as default vault
    pub fn as_default(mut self) -> Self {
        self.is_default = true;
        self
    }

    /// Set watch_for_changes
    pub fn watch_for_changes(mut self, watch: bool) -> Self {
        self.watch_for_changes = Some(watch);
        self
    }

    /// Select the write backend (GWS.11).
    pub fn write_backend(mut self, backend: WriteBackend) -> Self {
        self.write_backend = backend;
        self
    }

    /// Set the per-vault git substrate config (typically combined with
    /// `write_backend(WriteBackend::Git)`).
    pub fn git(mut self, git: VaultGitConfig) -> Self {
        self.git = Some(git);
        self
    }

    /// Build and validate
    pub fn build(self) -> Result<VaultConfig> {
        // Expand tilde and environment variables in the path
        let expanded_path = shellexpand::full(&self.path.to_string_lossy())
            .map(|p| PathBuf::from(p.into_owned()))
            .unwrap_or(self.path);

        let config = VaultConfig {
            name: self.name,
            path: expanded_path,
            is_default: self.is_default,
            watch_for_changes: self.watch_for_changes,
            max_file_size: self.max_file_size,
            allowed_extensions: self.allowed_extensions,
            excluded_paths: self.excluded_paths,
            enable_caching: self.enable_caching,
            cache_ttl: self.cache_ttl,
            template_dirs: self.template_dirs,
            allowed_operations: self.allowed_operations,
            write_backend: self.write_backend,
            git: self.git,
        };
        config.validate()?;
        Ok(config)
    }
}

/// Global server configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerConfig {
    /// List of configured vaults
    pub vaults: Vec<VaultConfig>,
    /// Configuration profile name
    pub profile: String,

    // Core settings
    pub watch_for_changes: bool,
    pub max_file_size: u64,
    pub allowed_extensions: HashSet<String>,
    pub excluded_paths: HashSet<String>,
    pub enable_caching: bool,
    pub cache_ttl: u64,
    pub log_level: String,

    // Advanced settings
    pub template_dirs: Vec<PathBuf>,
    pub default_template_variables: serde_json::Value,
    pub editor_backup_enabled: bool,
    pub editor_atomic_writes: bool,
    pub max_backup_files: usize,
    pub max_edit_history: usize,
    pub backup_retention_days: u32,

    // Link graph settings
    pub link_graph_enabled: bool,
    pub link_suggestions_enabled: bool,
    pub max_link_suggestions: usize,
    pub link_similarity_threshold: f32,

    // Search settings
    pub full_text_search_enabled: bool,
    pub index_rebuild_interval: u64,

    // Multi-vault
    pub multi_vault_enabled: bool,

    // Admin
    pub metrics_enabled: bool,
    pub debug_mode: bool,
}

impl Default for ServerConfig {
    fn default() -> Self {
        Self {
            vaults: vec![],
            profile: "default".to_string(),
            watch_for_changes: true,
            max_file_size: 10 * 1024 * 1024, // 10MB
            allowed_extensions: [".md", ".txt", ".canvas"]
                .iter()
                .map(|s| s.to_string())
                .collect(),
            excluded_paths: [".obsidian", ".git", ".DS_Store", "node_modules"]
                .iter()
                .map(|s| s.to_string())
                .collect(),
            enable_caching: true,
            cache_ttl: 3600,
            log_level: "INFO".to_string(),
            template_dirs: vec![],
            default_template_variables: serde_json::json!({}),
            editor_backup_enabled: true,
            editor_atomic_writes: true,
            max_backup_files: 100,
            max_edit_history: 100,
            backup_retention_days: 7,
            link_graph_enabled: true,
            link_suggestions_enabled: true,
            max_link_suggestions: 10,
            link_similarity_threshold: 0.3,
            full_text_search_enabled: true,
            index_rebuild_interval: 3600,
            multi_vault_enabled: false,
            metrics_enabled: false,
            debug_mode: false,
        }
    }
}

impl ServerConfig {
    /// Create new configuration
    pub fn new() -> Self {
        Self::default()
    }

    /// Validate configuration
    pub fn validate(&self) -> Result<()> {
        if self.vaults.is_empty() {
            return Err(Error::config_error("At least one vault must be configured"));
        }

        // Check unique vault names
        let names: HashSet<_> = self.vaults.iter().map(|v| &v.name).collect();
        if names.len() != self.vaults.len() {
            return Err(Error::config_error("Vault names must be unique"));
        }

        // Check unique default vaults
        let defaults: Vec<_> = self.vaults.iter().filter(|v| v.is_default).collect();
        if defaults.len() > 1 {
            return Err(Error::config_error("Only one vault can be default"));
        }

        // Validate each vault
        for vault in &self.vaults {
            vault.validate()?;
        }

        Ok(())
    }

    /// Get default vault config
    pub fn default_vault(&self) -> Result<&VaultConfig> {
        self.vaults
            .iter()
            .find(|v| v.is_default)
            .or_else(|| self.vaults.first())
            .ok_or_else(|| Error::config_error("No default vault configured"))
    }

    /// Save vault configuration to file (for persistence)
    pub async fn save_vaults(&self, path: &Path) -> Result<()> {
        let yaml = yaml_serde::to_string(&self.vaults)
            .map_err(|e| Error::config_error(format!("Failed to serialize vaults: {}", e)))?;

        tokio::fs::write(path, yaml).await.map_err(|e| {
            Error::config_error(format!(
                "Failed to save vaults to {}: {}",
                path.display(),
                e
            ))
        })
    }

    /// Load vault configuration from file
    pub async fn load_vaults(path: &Path) -> Result<Vec<VaultConfig>> {
        if !path.exists() {
            return Ok(Vec::new()); // Return empty if file doesn't exist
        }

        let content = tokio::fs::read_to_string(path).await.map_err(|e| {
            Error::config_error(format!(
                "Failed to load vaults from {}: {}",
                path.display(),
                e
            ))
        })?;

        let vaults = yaml_serde::from_str(&content)
            .map_err(|e| Error::config_error(format!("Invalid vault configuration: {}", e)))?;

        Ok(vaults)
    }
}

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

    #[test]
    fn test_vault_config_builder() {
        let temp = TempDir::new().unwrap();
        let vault = VaultConfig::builder("main", temp.path())
            .as_default()
            .watch_for_changes(true)
            .build();

        assert!(vault.is_ok());
        let v = vault.unwrap();
        assert_eq!(v.name, "main");
        assert!(v.is_default);
    }

    #[test]
    fn test_server_config_validation() {
        let mut config = ServerConfig::new();
        config.vaults.clear();
        assert!(config.validate().is_err());
    }

    // -------- GWS.11 write-backend + git config --------

    #[test]
    fn vault_config_defaults_to_legacy_backend_and_no_git() {
        let temp = TempDir::new().unwrap();
        let v = VaultConfig::builder("main", temp.path()).build().unwrap();
        assert_eq!(v.write_backend, WriteBackend::Legacy);
        assert!(v.git.is_none());
    }

    #[test]
    fn vault_config_builder_sets_git_backend() {
        let temp = TempDir::new().unwrap();
        let v = VaultConfig::builder("g", temp.path())
            .write_backend(WriteBackend::Git)
            .git(VaultGitConfig {
                branch: Some("main".to_string()),
                author: Some(GitAuthor {
                    name: "TurboVault".to_string(),
                    email: "tv@localhost".to_string(),
                }),
                merge_strategy: GitMergeStrategy::FastForward,
                include_ignored: false,
                require_commit_message: false,
            })
            .build()
            .unwrap();
        assert_eq!(v.write_backend, WriteBackend::Git);
        let g = v.git.unwrap();
        assert_eq!(g.branch.as_deref(), Some("main"));
        assert_eq!(g.merge_strategy, GitMergeStrategy::FastForward);
        assert!(!g.include_ignored);
        assert_eq!(g.author.unwrap().email, "tv@localhost");
    }

    #[test]
    fn vault_config_yaml_roundtrip_with_git_section() {
        let temp = TempDir::new().unwrap();
        let v = VaultConfig::builder("g", temp.path())
            .write_backend(WriteBackend::Git)
            .git(VaultGitConfig::default())
            .build()
            .unwrap();
        let yaml = yaml_serde::to_string(&v).unwrap();
        let back: VaultConfig = yaml_serde::from_str(&yaml).unwrap();
        assert_eq!(back.write_backend, WriteBackend::Git);
        assert!(back.git.is_some());
        // VaultGitConfig defaults survive a roundtrip.
        let g = back.git.unwrap();
        assert_eq!(g.merge_strategy, GitMergeStrategy::MergeCommit);
        assert!(g.include_ignored, "include_ignored defaults to true");
    }

    #[test]
    fn vault_config_yaml_legacy_omits_git_section() {
        let temp = TempDir::new().unwrap();
        let v = VaultConfig::builder("l", temp.path()).build().unwrap();
        let yaml = yaml_serde::to_string(&v).unwrap();
        // The roundtrip preserves the legacy default + None git.
        let back: VaultConfig = yaml_serde::from_str(&yaml).unwrap();
        assert_eq!(back.write_backend, WriteBackend::Legacy);
        assert!(back.git.is_none());
    }

    #[test]
    fn write_backend_serializes_lowercase() {
        let yaml = yaml_serde::to_string(&WriteBackend::Git).unwrap();
        assert!(yaml.contains("git"), "got: {yaml}");
        let back: WriteBackend = yaml_serde::from_str("legacy\n").unwrap();
        assert_eq!(back, WriteBackend::Legacy);
    }

    #[test]
    fn merge_strategy_serializes_kebab_case() {
        let yaml = yaml_serde::to_string(&GitMergeStrategy::MergeCommit).unwrap();
        assert!(yaml.contains("merge-commit"), "got: {yaml}");
        let back: GitMergeStrategy = yaml_serde::from_str("fast-forward\n").unwrap();
        assert_eq!(back, GitMergeStrategy::FastForward);
    }
}