okc 0.1.0

A local-first tool for AI agents to browse, parse, search, and reason over Open Knowledge Format (OKF) repositories
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
//! Configuration types for the Open Knowledge Catalog indexer.
//!
//! This module defines the [`OkcConfig`] struct which controls all aspects of
//! the indexing process, including root directories, file filtering, size limits,
//! database location, and watcher behavior.
//!
//! Configuration can be loaded from:
//! 1. Default values (lowest priority)
//! 2. TOML config file at `./okc.toml` or `~/.config/okc/config.toml`
//! 3. Environment variables with `OKC_` prefix (e.g., `OKC_ROOTS`, `OKC_DB_PATH`)
//! 4. CLI flags (highest priority)

use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};
use thiserror::Error;

pub mod bm25;

#[cfg(test)]
pub mod tests;

pub use bm25::Bm25Config;

/// Configuration for the OKC indexer and service.
///
/// This struct controls all aspects of the indexing process:
/// - Which directories to scan (`roots`)
/// - Which files to exclude (`exclude_patterns`)
/// - Size limits for files and front-matter
/// - Graph traversal limits
/// - Database location and connection settings
/// - File watcher debouncing and reconciliation intervals
/// - BM25 search relevance ranking parameters
///
/// # Example
///
/// ```rust
/// use okc::config::OkcConfig;
/// use std::path::PathBuf;
///
/// let config = OkcConfig {
///     roots: vec![PathBuf::from("/path/to/knowledge-base")],
///     ..Default::default()
/// };
/// ```
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub struct OkcConfig {
    /// Root directories to scan for markdown files.
    /// Each root is walked recursively (respecting exclude patterns).
    #[serde(default)]
    pub roots: Vec<PathBuf>,

    /// Glob patterns for files and directories to exclude from scanning.
    /// Defaults include common VCS, dependency, and build directories.
    #[serde(default = "default_exclude_patterns")]
    pub exclude_patterns: Vec<String>,

    /// Maximum file size in bytes to process. Larger files are skipped.
    /// Default: 2 MiB.
    #[serde(default = "default_max_file_size")]
    pub max_file_size: u64,

    /// Maximum front-matter size in bytes. Larger front-matter causes parse failure.
    /// Default: 64 KiB.
    #[serde(default = "default_max_front_matter_size")]
    pub max_front_matter_size: usize,

    /// Maximum YAML input size in bytes. Larger input is rejected before parsing
    /// to prevent pathological inputs from causing OOM in the YAML library.
    /// Default: 8 MiB.
    #[serde(default = "default_max_yaml_input_size")]
    pub max_yaml_input_size: usize,

    /// Maximum depth for graph traversal operations.
    /// Default: 5.
    #[serde(default = "default_max_graph_depth")]
    pub max_graph_depth: usize,

    /// Maximum number of nodes to return in graph traversal.
    /// Default: 100.
    #[serde(default = "default_max_graph_nodes")]
    pub max_graph_nodes: usize,

    /// Maximum serialized character count for a document response.
    /// Default: 500,000 characters.
    #[serde(default = "default_max_response_chars")]
    pub max_response_chars: usize,

    /// Whether to follow symbolic links during scanning.
    /// Default: false (for safety).
    #[serde(default)]
    pub follow_symlinks: bool,

    /// Whether to require index files (e.g., `index.md`) for directory browsing.
    /// Default: false.
    #[serde(default)]
    pub require_index_files: bool,

    /// Path to the SQLite database file.
    /// Default: `okc_index.db` in the current directory.
    #[serde(default = "default_db_path")]
    pub db_path: PathBuf,

    /// Debounce window in milliseconds for file watcher events.
    /// Multiple events within this window are batched.
    /// Default: 500ms.
    #[serde(default = "default_watcher_debounce_ms")]
    pub watcher_debounce_ms: u64,

    /// Full reconciliation interval in seconds for the file watcher.
    /// Periodically re-scans all roots to catch missed changes.
    /// Default: 600s (10 minutes).
    #[serde(default = "default_watcher_reconcile_secs")]
    pub watcher_reconcile_secs: u64,

    /// BM25 search relevance ranking configuration.
    /// Controls field weights and algorithm parameters for FTS5 search.
    #[serde(default)]
    pub bm25: Bm25Config,
}

fn default_exclude_patterns() -> Vec<String> {
    vec![
        ".git/".into(),
        "node_modules/".into(),
        "vendor/".into(),
        "target/".into(),
        ".env".into(),
        "secrets/".into(),
        "credentials/".into(),
    ]
}

fn default_max_file_size() -> u64 {
    2 * 1024 * 1024
}

fn default_max_front_matter_size() -> usize {
    64 * 1024
}

fn default_max_yaml_input_size() -> usize {
    8 * 1024 * 1024
}

fn default_max_graph_depth() -> usize {
    5
}

fn default_max_graph_nodes() -> usize {
    100
}

fn default_max_response_chars() -> usize {
    500_000
}

fn default_db_path() -> PathBuf {
    PathBuf::from("okc_index.db")
}

fn default_watcher_debounce_ms() -> u64 {
    500
}

fn default_watcher_reconcile_secs() -> u64 {
    600
}

impl Default for OkcConfig {
    fn default() -> Self {
        Self {
            roots: vec![],
            exclude_patterns: default_exclude_patterns(),
            max_file_size: default_max_file_size(),
            max_front_matter_size: default_max_front_matter_size(),
            max_yaml_input_size: default_max_yaml_input_size(),
            max_graph_depth: default_max_graph_depth(),
            max_graph_nodes: default_max_graph_nodes(),
            max_response_chars: default_max_response_chars(),
            follow_symlinks: false,
            require_index_files: false,
            db_path: default_db_path(),
            watcher_debounce_ms: default_watcher_debounce_ms(),
            watcher_reconcile_secs: default_watcher_reconcile_secs(),
            bm25: Bm25Config::default(),
        }
    }
}

/// Errors that can occur during configuration loading and validation.
#[derive(Debug, Error)]
pub enum ConfigError {
    #[error("Config file not found: {0}")]
    NotFound(PathBuf),

    #[error("Failed to read config file: {0}")]
    ReadError(#[from] std::io::Error),

    #[error("Failed to parse TOML config: {0}")]
    ParseError(#[from] toml::de::Error),

    #[error("Failed to serialize TOML config: {0}")]
    SerializeError(#[from] toml::ser::Error),

    #[error("Config validation failed: {0}")]
    ValidationError(String),

    #[error("Environment variable parse error: {0}")]
    EnvParseError(String),
}

impl OkcConfig {
    /// Load configuration from file, environment variables, and defaults.
    ///
    /// Priority order (highest to lowest):
    /// 1. CLI overrides (applied by caller after this function)
    /// 2. Environment variables (OKC_*)
    /// 3. Config file (./okc.toml or ~/.config/okc/config.toml)
    /// 4. Default values
    ///
    /// If `config_path` is Some, only that file is tried.
    /// If `config_path` is None, the following locations are checked in order:
    /// - ./okc.toml (current directory)
    /// - ~/.config/okc/config.toml (XDG config directory)
    pub fn load(config_path: Option<&Path>) -> Result<Self, ConfigError> {
        let mut config = Self::default();

        // Load from config file if present
        let paths = Self::config_file_paths(config_path);
        let mut loaded = false;
        for path in paths {
            if path.exists() {
                let content = std::fs::read_to_string(&path)?;
                config = toml::from_str(&content)?;
                tracing::info!("Loaded config from: {}", path.display());
                loaded = true;
                break;
            }
        }

        // If explicit path was provided but not found, return error
        if let Some(path) = config_path {
            if !loaded {
                return Err(ConfigError::NotFound(path.to_path_buf()));
            }
        }

        // Apply environment variable overrides
        config.apply_env_overrides()?;

        Ok(config)
    }

    /// Get the list of config file paths to check, in priority order.
    fn config_file_paths(explicit_path: Option<&Path>) -> Vec<PathBuf> {
        if let Some(path) = explicit_path {
            return vec![path.to_path_buf()];
        }

        let mut paths = vec![PathBuf::from("okc.toml")];

        if let Some(config_dir) = dirs::config_dir() {
            paths.push(config_dir.join("okc").join("config.toml"));
        }

        paths
    }

    /// Apply environment variable overrides to the configuration.
    ///
    /// Environment variables use the `OKC_` prefix with uppercase snake_case names:
    /// - OKC_ROOTS (comma-separated paths)
    /// - OKC_DB_PATH
    /// - OKC_MAX_FILE_SIZE
    /// - OKC_MAX_FRONT_MATTER_SIZE
    /// - OKC_MAX_YAML_INPUT_SIZE
    /// - OKC_MAX_GRAPH_DEPTH
    /// - OKC_MAX_GRAPH_NODES
    /// - OKC_MAX_RESPONSE_CHARS
    /// - OKC_FOLLOW_SYMLINKS (true/false)
    /// - OKC_REQUIRE_INDEX_FILES (true/false)
    /// - OKC_WATCHER_DEBOUNCE_MS
    /// - OKC_WATCHER_RECONCILE_SECS
    /// - OKC_EXCLUDE_PATTERNS (comma-separated)
    /// - OKC_BM25_TITLE_WEIGHT
    /// - OKC_BM25_DESCRIPTION_WEIGHT
    /// - OKC_BM25_HEADINGS_WEIGHT
    /// - OKC_BM25_BODY_WEIGHT
    /// - OKC_BM25_CONCEPT_TYPE_WEIGHT
    /// - OKC_BM25_K1
    /// - OKC_BM25_B
    fn apply_env_overrides(&mut self) -> Result<(), ConfigError> {
        // Roots (comma-separated)
        if let Ok(roots) = std::env::var("OKC_ROOTS") {
            self.roots = roots
                .split(',')
                .map(|s| PathBuf::from(s.trim()))
                .filter(|p| !p.as_os_str().is_empty())
                .collect();
        }

        // Database path
        if let Ok(db_path) = std::env::var("OKC_DB_PATH") {
            self.db_path = PathBuf::from(db_path);
        }

        // Max file size
        if let Ok(val) = std::env::var("OKC_MAX_FILE_SIZE") {
            self.max_file_size = val.parse().map_err(|_| {
                ConfigError::EnvParseError(format!("OKC_MAX_FILE_SIZE: invalid u64: {val}"))
            })?;
        }

        // Max front matter size
        if let Ok(val) = std::env::var("OKC_MAX_FRONT_MATTER_SIZE") {
            self.max_front_matter_size = val.parse().map_err(|_| {
                ConfigError::EnvParseError(format!(
                    "OKC_MAX_FRONT_MATTER_SIZE: invalid usize: {val}"
                ))
            })?;
        }

        // Max YAML input size
        if let Ok(val) = std::env::var("OKC_MAX_YAML_INPUT_SIZE") {
            self.max_yaml_input_size = val.parse().map_err(|_| {
                ConfigError::EnvParseError(format!("OKC_MAX_YAML_INPUT_SIZE: invalid usize: {val}"))
            })?;
        }

        // Max graph depth
        if let Ok(val) = std::env::var("OKC_MAX_GRAPH_DEPTH") {
            self.max_graph_depth = val.parse().map_err(|_| {
                ConfigError::EnvParseError(format!("OKC_MAX_GRAPH_DEPTH: invalid usize: {val}"))
            })?;
        }

        // Max graph nodes
        if let Ok(val) = std::env::var("OKC_MAX_GRAPH_NODES") {
            self.max_graph_nodes = val.parse().map_err(|_| {
                ConfigError::EnvParseError(format!("OKC_MAX_GRAPH_NODES: invalid usize: {val}"))
            })?;
        }

        if let Ok(val) = std::env::var("OKC_MAX_RESPONSE_CHARS") {
            self.max_response_chars = val.parse().map_err(|_| {
                ConfigError::EnvParseError(format!("OKC_MAX_RESPONSE_CHARS: invalid usize: {val}"))
            })?;
        }

        // Follow symlinks
        if let Ok(val) = std::env::var("OKC_FOLLOW_SYMLINKS") {
            self.follow_symlinks = val.parse().map_err(|_| {
                ConfigError::EnvParseError(format!("OKC_FOLLOW_SYMLINKS: invalid bool: {val}"))
            })?;
        }

        // Require index files
        if let Ok(val) = std::env::var("OKC_REQUIRE_INDEX_FILES") {
            self.require_index_files = val.parse().map_err(|_| {
                ConfigError::EnvParseError(format!("OKC_REQUIRE_INDEX_FILES: invalid bool: {val}"))
            })?;
        }

        // Watcher debounce ms
        if let Ok(val) = std::env::var("OKC_WATCHER_DEBOUNCE_MS") {
            self.watcher_debounce_ms = val.parse().map_err(|_| {
                ConfigError::EnvParseError(format!("OKC_WATCHER_DEBOUNCE_MS: invalid u64: {val}"))
            })?;
        }

        // Watcher reconcile secs
        if let Ok(val) = std::env::var("OKC_WATCHER_RECONCILE_SECS") {
            self.watcher_reconcile_secs = val.parse().map_err(|_| {
                ConfigError::EnvParseError(format!(
                    "OKC_WATCHER_RECONCILE_SECS: invalid u64: {val}"
                ))
            })?;
        }

        // Exclude patterns (comma-separated)
        if let Ok(patterns) = std::env::var("OKC_EXCLUDE_PATTERNS") {
            self.exclude_patterns = patterns
                .split(',')
                .map(|s| s.trim().to_string())
                .filter(|s| !s.is_empty())
                .collect();
        }

        // BM25 title weight
        if let Ok(val) = std::env::var("OKC_BM25_TITLE_WEIGHT") {
            self.bm25.title_weight = val.parse().map_err(|_| {
                ConfigError::EnvParseError(format!("OKC_BM25_TITLE_WEIGHT: invalid f64: {val}"))
            })?;
        }

        // BM25 description weight
        if let Ok(val) = std::env::var("OKC_BM25_DESCRIPTION_WEIGHT") {
            self.bm25.description_weight = val.parse().map_err(|_| {
                ConfigError::EnvParseError(format!(
                    "OKC_BM25_DESCRIPTION_WEIGHT: invalid f64: {val}"
                ))
            })?;
        }

        // BM25 headings weight
        if let Ok(val) = std::env::var("OKC_BM25_HEADINGS_WEIGHT") {
            self.bm25.headings_weight = val.parse().map_err(|_| {
                ConfigError::EnvParseError(format!("OKC_BM25_HEADINGS_WEIGHT: invalid f64: {val}"))
            })?;
        }

        // BM25 body weight
        if let Ok(val) = std::env::var("OKC_BM25_BODY_WEIGHT") {
            self.bm25.body_weight = val.parse().map_err(|_| {
                ConfigError::EnvParseError(format!("OKC_BM25_BODY_WEIGHT: invalid f64: {val}"))
            })?;
        }

        // BM25 concept type weight
        if let Ok(val) = std::env::var("OKC_BM25_CONCEPT_TYPE_WEIGHT") {
            self.bm25.concept_type_weight = val.parse().map_err(|_| {
                ConfigError::EnvParseError(format!(
                    "OKC_BM25_CONCEPT_TYPE_WEIGHT: invalid f64: {val}"
                ))
            })?;
        }

        // BM25 k1
        if let Ok(val) = std::env::var("OKC_BM25_K1") {
            self.bm25.k1 = val.parse().map_err(|_| {
                ConfigError::EnvParseError(format!("OKC_BM25_K1: invalid f64: {val}"))
            })?;
        }

        // BM25 b
        if let Ok(val) = std::env::var("OKC_BM25_B") {
            self.bm25.b = val.parse().map_err(|_| {
                ConfigError::EnvParseError(format!("OKC_BM25_B: invalid f64: {val}"))
            })?;
        }

        Ok(())
    }

    /// Validate the configuration.
    pub fn validate(&self) -> Result<(), ConfigError> {
        if self.roots.is_empty() {
            return Err(ConfigError::ValidationError(
                "At least one root directory must be specified".into(),
            ));
        }

        for root in &self.roots {
            if !root.exists() {
                return Err(ConfigError::ValidationError(format!(
                    "Root directory does not exist: {}",
                    root.display()
                )));
            }
            if !root.is_dir() {
                return Err(ConfigError::ValidationError(format!(
                    "Root path is not a directory: {}",
                    root.display()
                )));
            }
        }

        if self.max_file_size == 0 {
            return Err(ConfigError::ValidationError(
                "max_file_size must be greater than 0".into(),
            ));
        }

        if self.max_front_matter_size == 0 {
            return Err(ConfigError::ValidationError(
                "max_front_matter_size must be greater than 0".into(),
            ));
        }

        if self.max_yaml_input_size == 0 {
            return Err(ConfigError::ValidationError(
                "max_yaml_input_size must be greater than 0".into(),
            ));
        }

        if self.max_graph_depth == 0 {
            return Err(ConfigError::ValidationError(
                "max_graph_depth must be greater than 0".into(),
            ));
        }

        if self.max_graph_nodes == 0 {
            return Err(ConfigError::ValidationError(
                "max_graph_nodes must be greater than 0".into(),
            ));
        }
        if self.max_response_chars == 0 {
            return Err(ConfigError::ValidationError(
                "max_response_chars must be greater than 0".into(),
            ));
        }

        if self.watcher_debounce_ms == 0 {
            return Err(ConfigError::ValidationError(
                "watcher_debounce_ms must be greater than 0".into(),
            ));
        }

        if self.watcher_reconcile_secs == 0 {
            return Err(ConfigError::ValidationError(
                "watcher_reconcile_secs must be greater than 0".into(),
            ));
        }

        // Validate BM25 config
        if self.bm25.title_weight < 0.0
            || self.bm25.description_weight < 0.0
            || self.bm25.headings_weight < 0.0
            || self.bm25.body_weight < 0.0
            || self.bm25.concept_type_weight < 0.0
        {
            return Err(ConfigError::ValidationError(
                "BM25 weights must be non-negative".into(),
            ));
        }

        if self.bm25.k1 <= 0.0 {
            return Err(ConfigError::ValidationError(
                "BM25 k1 must be positive".into(),
            ));
        }

        if !(0.0..=1.0).contains(&self.bm25.b) {
            return Err(ConfigError::ValidationError(
                "BM25 b must be in range [0.0, 1.0]".into(),
            ));
        }

        // Validate db_path parent directory exists or can be created
        if let Some(parent) = self.db_path.parent() {
            if !parent.as_os_str().is_empty() && !parent.exists() {
                return Err(ConfigError::ValidationError(format!(
                    "Database parent directory does not exist: {}",
                    parent.display()
                )));
            }
        }

        Ok(())
    }

    /// Create a default config file at the XDG config directory.
    pub fn create_default_config_file() -> Result<PathBuf, ConfigError> {
        let config_dir = dirs::config_dir()
            .ok_or_else(|| {
                ConfigError::ValidationError("Could not determine config directory".into())
            })?
            .join("okc");

        std::fs::create_dir_all(&config_dir)?;

        let config_path = config_dir.join("config.toml");
        let default_config = Self::default();
        let toml_content = toml::to_string_pretty(&default_config)?;
        std::fs::write(&config_path, toml_content)?;

        Ok(config_path)
    }
}