xchecker-config 1.2.0

Configuration model, discovery, and validation for xchecker
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
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::env;
use std::path::{Path, PathBuf};

use crate::error::{ConfigError, XCheckerError};

use super::{
    ClaudeConfig, CliArgs, Config, ConfigSource, Defaults, GeminiConfig, HooksConfig, LlmConfig,
    PhasesConfig, RunnerConfig, SecurityConfig, Selectors,
};

/// TOML configuration file structure
#[derive(Debug, Deserialize, Serialize)]
struct TomlConfig {
    defaults: Option<Defaults>,
    selectors: Option<Selectors>,
    runner: Option<RunnerConfig>,
    llm: Option<LlmConfig>,
    phases: Option<PhasesConfig>,
    hooks: Option<HooksConfig>,
    security: Option<SecurityConfig>,
}

impl Config {
    /// Discover and load configuration with precedence: CLI > file > defaults
    ///
    /// Uses current working directory for config file discovery when no explicit
    /// path is provided in cli_args.
    pub fn discover(cli_args: &CliArgs) -> Result<Self, XCheckerError> {
        let start_dir = std::env::current_dir().map_err(|e| {
            XCheckerError::Config(ConfigError::DiscoveryFailed {
                reason: format!("Failed to get current directory: {e}"),
            })
        })?;
        Self::discover_from(&start_dir, cli_args)
    }

    /// Discover and load configuration starting from a specific directory
    ///
    /// This is the path-driven variant used by tests to avoid process-global state.
    /// Uses the given directory for config file discovery when no explicit path
    /// is provided in cli_args.
    pub fn discover_from(start_dir: &Path, cli_args: &CliArgs) -> Result<Self, XCheckerError> {
        let mut source_attribution = HashMap::new();

        // Start with built-in defaults
        let mut defaults = Defaults::default();
        let mut selectors = Selectors::default();
        let mut runner = RunnerConfig::default();
        let mut llm = LlmConfig {
            provider: None,
            fallback_provider: None,
            claude: None,
            gemini: None,
            openrouter: None,
            anthropic: None,
            execution_strategy: None,
            prompt_template: None,
        };
        let mut hooks = HooksConfig::default();
        let mut phases = PhasesConfig::default();
        let mut security = SecurityConfig::default();

        // Track default sources
        source_attribution.insert("max_turns".to_string(), ConfigSource::Default);
        source_attribution.insert("packet_max_bytes".to_string(), ConfigSource::Default);
        source_attribution.insert("packet_max_lines".to_string(), ConfigSource::Default);
        source_attribution.insert("output_format".to_string(), ConfigSource::Default);
        source_attribution.insert("verbose".to_string(), ConfigSource::Default);
        source_attribution.insert("runner_mode".to_string(), ConfigSource::Default);
        source_attribution.insert("phase_timeout".to_string(), ConfigSource::Default);
        source_attribution.insert("stdout_cap_bytes".to_string(), ConfigSource::Default);
        source_attribution.insert("stderr_cap_bytes".to_string(), ConfigSource::Default);
        source_attribution.insert("lock_ttl_seconds".to_string(), ConfigSource::Default);
        source_attribution.insert("debug_packet".to_string(), ConfigSource::Default);
        source_attribution.insert("allow_links".to_string(), ConfigSource::Default);

        // Discover and load config file (if not explicitly provided)
        let config_path = if let Some(explicit_path) = &cli_args.config_path {
            Some(explicit_path.clone())
        } else {
            Self::discover_config_file_from(start_dir)?
        };

        if let Some(path) = &config_path {
            let file_config = Self::load_config_file(path)?;

            let config_source = ConfigSource::Config;

            // Apply config file values (override defaults)
            if let Some(file_defaults) = file_config.defaults {
                if file_defaults.model.is_some() {
                    defaults.model = file_defaults.model;
                    source_attribution.insert("model".to_string(), config_source.clone());
                }
                if file_defaults.max_turns.is_some() {
                    defaults.max_turns = file_defaults.max_turns;
                    source_attribution.insert("max_turns".to_string(), config_source.clone());
                }
                if file_defaults.packet_max_bytes.is_some() {
                    defaults.packet_max_bytes = file_defaults.packet_max_bytes;
                    source_attribution
                        .insert("packet_max_bytes".to_string(), config_source.clone());
                }
                if file_defaults.packet_max_lines.is_some() {
                    defaults.packet_max_lines = file_defaults.packet_max_lines;
                    source_attribution
                        .insert("packet_max_lines".to_string(), config_source.clone());
                }
                if file_defaults.output_format.is_some() {
                    defaults.output_format = file_defaults.output_format;
                    source_attribution.insert("output_format".to_string(), config_source.clone());
                }
                if file_defaults.verbose.is_some() {
                    defaults.verbose = file_defaults.verbose;
                    source_attribution.insert("verbose".to_string(), config_source.clone());
                }
                if file_defaults.phase_timeout.is_some() {
                    defaults.phase_timeout = file_defaults.phase_timeout;
                    source_attribution.insert("phase_timeout".to_string(), config_source.clone());
                }
                if file_defaults.stdout_cap_bytes.is_some() {
                    defaults.stdout_cap_bytes = file_defaults.stdout_cap_bytes;
                    source_attribution
                        .insert("stdout_cap_bytes".to_string(), config_source.clone());
                }
                if file_defaults.stderr_cap_bytes.is_some() {
                    defaults.stderr_cap_bytes = file_defaults.stderr_cap_bytes;
                    source_attribution
                        .insert("stderr_cap_bytes".to_string(), config_source.clone());
                }
                if file_defaults.lock_ttl_seconds.is_some() {
                    defaults.lock_ttl_seconds = file_defaults.lock_ttl_seconds;
                    source_attribution
                        .insert("lock_ttl_seconds".to_string(), config_source.clone());
                }
                if file_defaults.debug_packet.is_some() {
                    defaults.debug_packet = file_defaults.debug_packet;
                    source_attribution.insert("debug_packet".to_string(), config_source.clone());
                }
                if file_defaults.allow_links.is_some() {
                    defaults.allow_links = file_defaults.allow_links;
                    source_attribution.insert("allow_links".to_string(), config_source.clone());
                }
                if file_defaults.strict_validation.is_some() {
                    defaults.strict_validation = file_defaults.strict_validation;
                    source_attribution
                        .insert("strict_validation".to_string(), config_source.clone());
                }
            }

            if let Some(file_selectors) = file_config.selectors {
                if !file_selectors.include.is_empty() {
                    selectors.include = file_selectors.include;
                    source_attribution
                        .insert("selectors_include".to_string(), config_source.clone());
                }
                if !file_selectors.exclude.is_empty() {
                    selectors.exclude = file_selectors.exclude;
                    source_attribution
                        .insert("selectors_exclude".to_string(), config_source.clone());
                }
            }

            if let Some(file_runner) = file_config.runner {
                if file_runner.mode.is_some() {
                    runner.mode = file_runner.mode;
                    source_attribution.insert("runner_mode".to_string(), config_source.clone());
                }
                if file_runner.distro.is_some() {
                    runner.distro = file_runner.distro;
                    source_attribution.insert("runner_distro".to_string(), config_source.clone());
                }
                if file_runner.claude_path.is_some() {
                    runner.claude_path = file_runner.claude_path;
                    source_attribution.insert("claude_path".to_string(), config_source.clone());
                }
            }

            if let Some(file_llm) = file_config.llm {
                if file_llm.provider.is_some() {
                    llm.provider = file_llm.provider;
                    source_attribution.insert("llm_provider".to_string(), config_source.clone());
                }
                if file_llm.fallback_provider.is_some() {
                    llm.fallback_provider = file_llm.fallback_provider;
                    source_attribution
                        .insert("llm_fallback_provider".to_string(), config_source.clone());
                }
                if let Some(file_claude) = file_llm.claude
                    && file_claude.binary.is_some()
                {
                    llm.claude = Some(file_claude);
                    source_attribution
                        .insert("llm_claude_binary".to_string(), config_source.clone());
                }
                if let Some(file_gemini) = file_llm.gemini {
                    llm.gemini = Some(file_gemini);
                    source_attribution
                        .insert("llm_gemini_config".to_string(), config_source.clone());
                }
                if let Some(file_openrouter) = file_llm.openrouter {
                    llm.openrouter = Some(file_openrouter);
                    source_attribution
                        .insert("llm_openrouter_config".to_string(), config_source.clone());
                }
                if let Some(file_anthropic) = file_llm.anthropic {
                    llm.anthropic = Some(file_anthropic);
                    source_attribution
                        .insert("llm_anthropic_config".to_string(), config_source.clone());
                }
                if file_llm.execution_strategy.is_some() {
                    llm.execution_strategy = file_llm.execution_strategy;
                    source_attribution
                        .insert("execution_strategy".to_string(), config_source.clone());
                }
                if file_llm.prompt_template.is_some() {
                    llm.prompt_template = file_llm.prompt_template;
                    source_attribution.insert("prompt_template".to_string(), config_source.clone());
                }
            }

            // Load phases configuration from file
            if let Some(file_phases) = file_config.phases {
                phases = file_phases;
                source_attribution.insert("phases".to_string(), config_source.clone());
            }

            // Load hooks configuration from file
            if let Some(file_hooks) = file_config.hooks {
                hooks = file_hooks;
                source_attribution.insert("hooks".to_string(), config_source.clone());
            }

            // Load security configuration from file
            if let Some(file_security) = file_config.security {
                security = file_security;
                source_attribution.insert("security".to_string(), config_source);
            }
        }

        // Apply CLI overrides (highest priority)
        if let Some(model) = &cli_args.model {
            defaults.model = Some(model.clone());
            source_attribution.insert("model".to_string(), ConfigSource::Cli);
        }
        if let Some(max_turns) = cli_args.max_turns {
            defaults.max_turns = Some(max_turns);
            source_attribution.insert("max_turns".to_string(), ConfigSource::Cli);
        }
        if let Some(packet_max_bytes) = cli_args.packet_max_bytes {
            defaults.packet_max_bytes = Some(packet_max_bytes);
            source_attribution.insert("packet_max_bytes".to_string(), ConfigSource::Cli);
        }
        if let Some(packet_max_lines) = cli_args.packet_max_lines {
            defaults.packet_max_lines = Some(packet_max_lines);
            source_attribution.insert("packet_max_lines".to_string(), ConfigSource::Cli);
        }
        if let Some(output_format) = &cli_args.output_format {
            defaults.output_format = Some(output_format.clone());
            source_attribution.insert("output_format".to_string(), ConfigSource::Cli);
        }
        if let Some(verbose) = cli_args.verbose {
            defaults.verbose = Some(verbose);
            source_attribution.insert("verbose".to_string(), ConfigSource::Cli);
        }
        if let Some(runner_mode) = &cli_args.runner_mode {
            runner.mode = Some(runner_mode.clone());
            source_attribution.insert("runner_mode".to_string(), ConfigSource::Cli);
        }
        if let Some(runner_distro) = &cli_args.runner_distro {
            runner.distro = Some(runner_distro.clone());
            source_attribution.insert("runner_distro".to_string(), ConfigSource::Cli);
        }
        if let Some(claude_path) = &cli_args.claude_path {
            runner.claude_path = Some(claude_path.clone());
            source_attribution.insert("claude_path".to_string(), ConfigSource::Cli);
        }
        if let Some(phase_timeout) = cli_args.phase_timeout {
            defaults.phase_timeout = Some(phase_timeout);
            source_attribution.insert("phase_timeout".to_string(), ConfigSource::Cli);
        }
        if let Some(stdout_cap_bytes) = cli_args.stdout_cap_bytes {
            defaults.stdout_cap_bytes = Some(stdout_cap_bytes);
            source_attribution.insert("stdout_cap_bytes".to_string(), ConfigSource::Cli);
        }
        if let Some(stderr_cap_bytes) = cli_args.stderr_cap_bytes {
            defaults.stderr_cap_bytes = Some(stderr_cap_bytes);
            source_attribution.insert("stderr_cap_bytes".to_string(), ConfigSource::Cli);
        }
        if let Some(lock_ttl_seconds) = cli_args.lock_ttl_seconds {
            defaults.lock_ttl_seconds = Some(lock_ttl_seconds);
            source_attribution.insert("lock_ttl_seconds".to_string(), ConfigSource::Cli);
        }
        if cli_args.debug_packet {
            defaults.debug_packet = Some(true);
            source_attribution.insert("debug_packet".to_string(), ConfigSource::Cli);
        }
        if cli_args.allow_links {
            defaults.allow_links = Some(true);
            source_attribution.insert("allow_links".to_string(), ConfigSource::Cli);
        }
        if let Some(strict_validation) = cli_args.strict_validation {
            defaults.strict_validation = Some(strict_validation);
            source_attribution.insert("strict_validation".to_string(), ConfigSource::Cli);
        }

        // Apply security pattern overrides (CLI > file > defaults)
        if !cli_args.extra_secret_pattern.is_empty() {
            security
                .extra_secret_patterns
                .extend(cli_args.extra_secret_pattern.clone());
            source_attribution.insert("security".to_string(), ConfigSource::Cli);
        }
        if !cli_args.ignore_secret_pattern.is_empty() {
            security
                .ignore_secret_patterns
                .extend(cli_args.ignore_secret_pattern.clone());
            source_attribution.insert("security".to_string(), ConfigSource::Cli);
        }

        // Apply LLM configuration with precedence: CLI > env > config > defaults
        // Check environment variable first
        if let Ok(env_provider) = env::var("XCHECKER_LLM_PROVIDER")
            && !env_provider.is_empty()
        {
            llm.provider = Some(env_provider);
            source_attribution.insert("llm_provider".to_string(), ConfigSource::Env);
        }

        // CLI flag overrides environment variable
        if let Some(provider) = &cli_args.llm_provider {
            llm.provider = Some(provider.clone());
            source_attribution.insert("llm_provider".to_string(), ConfigSource::Cli);
        }

        // Default to "claude-cli" if no provider is set
        if llm.provider.is_none() {
            llm.provider = Some("claude-cli".to_string());
            source_attribution.insert("llm_provider".to_string(), ConfigSource::Default);
        }

        // Apply fallback provider configuration with precedence: CLI > env > config
        if let Ok(env_fallback) = env::var("XCHECKER_LLM_FALLBACK_PROVIDER")
            && !env_fallback.is_empty()
        {
            llm.fallback_provider = Some(env_fallback);
            source_attribution.insert("llm_fallback_provider".to_string(), ConfigSource::Env);
        }

        if let Some(fallback_provider) = &cli_args.llm_fallback_provider {
            llm.fallback_provider = Some(fallback_provider.clone());
            source_attribution.insert("llm_fallback_provider".to_string(), ConfigSource::Cli);
        }

        // Apply prompt template configuration with precedence: CLI > env > config
        if let Ok(env_template) = env::var("XCHECKER_LLM_PROMPT_TEMPLATE")
            && !env_template.is_empty()
        {
            llm.prompt_template = Some(env_template);
            source_attribution.insert("prompt_template".to_string(), ConfigSource::Env);
        }

        if let Some(prompt_template) = &cli_args.prompt_template {
            llm.prompt_template = Some(prompt_template.clone());
            source_attribution.insert("prompt_template".to_string(), ConfigSource::Cli);
        }

        // Apply Claude binary configuration
        if let Some(binary) = &cli_args.llm_claude_binary {
            if llm.claude.is_none() {
                llm.claude = Some(ClaudeConfig { binary: None });
            }
            if let Some(claude_config) = &mut llm.claude {
                claude_config.binary = Some(binary.clone());
                source_attribution.insert("llm_claude_binary".to_string(), ConfigSource::Cli);
            }
        }

        // Apply Gemini binary configuration
        if let Some(binary) = &cli_args.llm_gemini_binary {
            if llm.gemini.is_none() {
                llm.gemini = Some(GeminiConfig {
                    binary: None,
                    default_model: None,
                    profiles: None,
                });
            }
            if let Some(gemini_config) = &mut llm.gemini {
                gemini_config.binary = Some(binary.clone());
                source_attribution.insert("llm_gemini_binary".to_string(), ConfigSource::Cli);
            }
        }

        // Apply Gemini default model configuration with precedence: CLI > env > config
        if let Ok(env_default_model) = env::var("XCHECKER_LLM_GEMINI_DEFAULT_MODEL")
            && !env_default_model.is_empty()
        {
            if llm.gemini.is_none() {
                llm.gemini = Some(GeminiConfig {
                    binary: None,
                    default_model: None,
                    profiles: None,
                });
            }
            if let Some(gemini_config) = &mut llm.gemini {
                gemini_config.default_model = Some(env_default_model);
                source_attribution
                    .insert("llm_gemini_default_model".to_string(), ConfigSource::Env);
            }
        }

        if let Some(default_model) = &cli_args.llm_gemini_default_model {
            if llm.gemini.is_none() {
                llm.gemini = Some(GeminiConfig {
                    binary: None,
                    default_model: None,
                    profiles: None,
                });
            }
            if let Some(gemini_config) = &mut llm.gemini {
                gemini_config.default_model = Some(default_model.clone());
                source_attribution
                    .insert("llm_gemini_default_model".to_string(), ConfigSource::Cli);
            }
        }

        // Apply execution strategy configuration with precedence: CLI > env > config > default
        // Check environment variable (overrides config file)
        if let Ok(env_strategy) = env::var("XCHECKER_EXECUTION_STRATEGY")
            && !env_strategy.is_empty()
        {
            llm.execution_strategy = Some(env_strategy);
            source_attribution.insert("execution_strategy".to_string(), ConfigSource::Env);
        }

        // CLI flag overrides everything
        if let Some(strategy) = &cli_args.execution_strategy {
            llm.execution_strategy = Some(strategy.clone());
            source_attribution.insert("execution_strategy".to_string(), ConfigSource::Cli);
        }

        // Default to "controlled" if not specified
        if llm.execution_strategy.is_none() {
            llm.execution_strategy = Some("controlled".to_string());
            source_attribution.insert("execution_strategy".to_string(), ConfigSource::Default);
        }

        let config = Self {
            defaults,
            selectors,
            runner,
            llm,
            phases,
            hooks,
            security,
            source_attribution,
        };

        // Validate the final configuration
        config.validate()?;

        Ok(config)
    }

    /// Discover config file by searching upward from a given directory
    ///
    /// This is the path-driven variant used by tests to avoid process-global state.
    /// Walks up the directory tree looking for `.xchecker/config.toml`, stopping
    /// at repository root markers (.git, .hg, .svn) or filesystem root.
    pub fn discover_config_file_from(start_dir: &Path) -> Result<Option<PathBuf>, XCheckerError> {
        let mut current_dir = start_dir.to_path_buf();

        loop {
            let config_path = current_dir.join(".xchecker").join("config.toml");
            if config_path.exists() {
                return Ok(Some(config_path));
            }

            // Check if we've reached the filesystem root or repository root
            if current_dir.parent().is_none() {
                break;
            }

            // Check for repository root markers
            if current_dir.join(".git").exists()
                || current_dir.join(".hg").exists()
                || current_dir.join(".svn").exists()
            {
                // Stop at repository root if no config found
                break;
            }

            current_dir = current_dir.parent().unwrap().to_path_buf();
        }

        Ok(None)
    }

    /// Load configuration from TOML file
    fn load_config_file(path: &Path) -> Result<TomlConfig, XCheckerError> {
        match std::fs::read_to_string(path) {
            Ok(content) => toml::from_str(&content).map_err(|e| {
                XCheckerError::Config(ConfigError::InvalidFile(format!(
                    "Failed to parse TOML config file {}: {e}",
                    path.display()
                )))
            }),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                // Missing config file is OK - return empty config (will use defaults)
                Ok(TomlConfig {
                    defaults: None,
                    selectors: None,
                    runner: None,
                    llm: None,
                    phases: None,
                    hooks: None,
                    security: None,
                })
            }
            Err(e) => Err(XCheckerError::Config(ConfigError::DiscoveryFailed {
                reason: format!("Failed to read config file {}: {}", path.display(), e),
            })),
        }
    }

    /// Discover configuration from environment and filesystem.
    ///
    /// This method uses the same discovery logic as the CLI:
    /// - `XCHECKER_HOME` environment variable (if set)
    /// - Upward search for `.xchecker/config.toml` from current directory
    /// - Built-in defaults
    ///
    /// Precedence: config file > defaults
    ///
    /// This is the recommended method for library consumers who want CLI-like
    /// behavior without needing to construct `CliArgs`.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use xchecker_config::Config;
    ///
    /// let config = Config::discover_from_env_and_fs()
    ///     .expect("Failed to discover config");
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The current directory cannot be determined
    /// - A config file exists but cannot be parsed
    /// - Configuration validation fails
    pub fn discover_from_env_and_fs() -> Result<Self, XCheckerError> {
        // Use empty CliArgs to get config file + defaults behavior
        // This matches CLI semantics without any CLI overrides
        let cli_args = CliArgs::default();
        Self::discover(&cli_args)
    }
}