prodigy 0.4.4

Turn ad-hoc Claude sessions into reproducible development pipelines with parallel AI agents
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
// Allow large error types from premortem's API design
#![allow(clippy::result_large_err)]

//! Configuration builder using premortem for layered source loading.
//!
//! This module provides functions to load `ProdigyConfig` from multiple sources
//! with proper precedence and comprehensive error accumulation.
//!
//! # Source Precedence
//!
//! Sources are loaded in order from lowest to highest priority:
//!
//! 1. **Defaults** - Hardcoded default values
//! 2. **Global config** - `~/.prodigy/config.yml` (optional)
//! 3. **Project config** - `.prodigy/config.yml` (optional)
//! 4. **Environment variables** - `PRODIGY_*` prefix (highest priority)
//!
//! # Example
//!
//! ```no_run
//! use prodigy::config::load_prodigy_config;
//!
//! let config = load_prodigy_config().expect("failed to load config");
//! println!("Log level: {}", config.log_level);
//! ```
//!
//! # Testing
//!
//! Use `load_prodigy_config_with` with a `MockEnv` for testing:
//!
//! ```
//! use prodigy::config::load_prodigy_config_with;
//! use premortem::MockEnv;
//!
//! let env = MockEnv::new();
//! let config = load_prodigy_config_with(&env).expect("failed to load config");
//! assert_eq!(config.log_level, "info"); // default value
//! ```

use super::prodigy_config::{global_config_path, project_config_path, ProdigyConfig};
use premortem::config::Config;
use premortem::prelude::*;

/// Load Prodigy configuration from all sources using real I/O.
///
/// This function loads configuration from:
/// 1. Hardcoded defaults
/// 2. Global config file (`~/.prodigy/config.yml`) - optional
/// 3. Project config file (`.prodigy/config.yml`) - optional
/// 4. Environment variables (`PRODIGY_*`)
///
/// Returns a `Config<ProdigyConfig>` wrapper that implements `Deref<Target=ProdigyConfig>`,
/// so you can access fields directly via dot notation.
///
/// # Errors
///
/// Returns `ConfigErrors` if any validation fails. All errors are accumulated
/// and reported together, not just the first one encountered.
///
/// # Example
///
/// ```no_run
/// use prodigy::config::load_prodigy_config;
///
/// let config = load_prodigy_config().expect("failed to load config");
/// println!("Log level: {}", config.log_level);
/// ```
pub fn load_prodigy_config() -> Result<Config<ProdigyConfig>, ConfigErrors> {
    load_prodigy_config_with(&RealEnv)
}

/// Load Prodigy configuration with a custom environment.
///
/// This function is designed for testing with `MockEnv` to avoid actual I/O
/// and enable isolated, deterministic tests.
///
/// # Arguments
///
/// * `env` - A `ConfigEnv` implementation (`RealEnv` for production, `MockEnv` for tests)
///
/// # Environment Variable Support
///
/// The following environment variables are supported:
/// - `PRODIGY_CLAUDE_API_KEY` → `claude_api_key`
/// - `PRODIGY_LOG_LEVEL` → `log_level`
/// - `PRODIGY_AUTO_COMMIT` → `auto_commit`
/// - `PRODIGY_EDITOR` → `default_editor`
/// - `PRODIGY_MAX_CONCURRENT` → `max_concurrent_specs`
///
/// For nested fields, use double underscore:
/// - `PRODIGY__PROJECT__NAME` → `project.name`
/// - `PRODIGY__STORAGE__BACKEND` → `storage.backend`
///
/// # Example
///
/// ```
/// use prodigy::config::load_prodigy_config_with;
/// use premortem::MockEnv;
///
/// let env = MockEnv::new()
///     .with_env("PRODIGY__LOG_LEVEL", "debug")
///     .with_env("PRODIGY__MAX_CONCURRENT_SPECS", "10");
///
/// let config = load_prodigy_config_with(&env).expect("failed to load");
/// assert_eq!(config.log_level, "debug");
/// assert_eq!(config.max_concurrent_specs, 10);
/// ```
pub fn load_prodigy_config_with<E: ConfigEnv>(
    env: &E,
) -> Result<Config<ProdigyConfig>, ConfigErrors> {
    let global_path = global_config_path();
    let project_path = project_config_path();

    // First, build with file sources and structured env vars
    let mut builder = Config::<ProdigyConfig>::builder()
        // Layer 1: Hardcoded defaults (lowest priority)
        .source(Defaults::from(ProdigyConfig::default()))
        // Layer 2: Global config file (optional - missing file is OK)
        .source(
            Yaml::file(global_path.to_string_lossy().to_string())
                .optional()
                .named("global config"),
        )
        // Layer 3: Project config file (optional - missing file is OK)
        .source(
            Yaml::file(project_path.to_string_lossy().to_string())
                .optional()
                .named("project config"),
        )
        // Layer 4: Environment variables (highest priority)
        // Use "__" as separator so single underscores in field names are preserved
        // E.g., PRODIGY__LOG_LEVEL -> log_level, PRODIGY__STORAGE__COMPRESSION_LEVEL -> storage.compression_level
        .source(Env::prefix("PRODIGY__").separator("__"));

    // Layer 5: Legacy environment variable mappings for backward compatibility
    // These use single underscore and have explicit field mappings
    builder = builder.source(
        Env::prefix("PRODIGY_")
            .map("CLAUDE_API_KEY", "claude_api_key")
            .map("LOG_LEVEL", "log_level")
            .map("AUTO_COMMIT", "auto_commit")
            .map("EDITOR", "default_editor")
            .map("MAX_CONCURRENT", "max_concurrent_specs"),
    );

    builder.build_with_env(env)
}

/// Load Prodigy configuration with tracing for debugging.
///
/// Returns a `TracedConfig` that tracks where each value originated,
/// useful for debugging configuration issues.
///
/// # Example
///
/// ```no_run
/// use prodigy::config::load_prodigy_config_traced;
///
/// let traced = load_prodigy_config_traced().expect("failed to load");
///
/// // See where a value came from
/// if let Some(trace) = traced.trace("max_concurrent_specs") {
///     println!("Value: {:?}", trace.final_value.value);
///     println!("Source: {:?}", trace.final_value.source);
/// }
///
/// // Get the actual config
/// let config = traced.into_inner();
/// ```
pub fn load_prodigy_config_traced() -> Result<TracedConfig<ProdigyConfig>, ConfigErrors> {
    load_prodigy_config_traced_with(&RealEnv)
}

/// Load Prodigy configuration with tracing using a custom environment.
pub fn load_prodigy_config_traced_with<E: ConfigEnv>(
    env: &E,
) -> Result<TracedConfig<ProdigyConfig>, ConfigErrors> {
    let global_path = global_config_path();
    let project_path = project_config_path();

    Config::<ProdigyConfig>::builder()
        .source(Defaults::from(ProdigyConfig::default()))
        .source(
            Yaml::file(global_path.to_string_lossy().to_string())
                .optional()
                .named("global config"),
        )
        .source(
            Yaml::file(project_path.to_string_lossy().to_string())
                .optional()
                .named("project config"),
        )
        .source(Env::prefix("PRODIGY__").separator("__"))
        // Legacy environment variable mappings for backward compatibility
        .source(
            Env::prefix("PRODIGY_")
                .map("CLAUDE_API_KEY", "claude_api_key")
                .map("LOG_LEVEL", "log_level")
                .map("AUTO_COMMIT", "auto_commit")
                .map("EDITOR", "default_editor")
                .map("MAX_CONCURRENT", "max_concurrent_specs"),
        )
        .build_traced_with_env(env)
}

/// Options for customizing config loading.
#[derive(Debug, Clone, Default)]
pub struct LoadOptions {
    /// Path to a specific config file to load (instead of searching).
    pub config_path: Option<std::path::PathBuf>,
    /// Skip loading the global config file.
    pub skip_global: bool,
    /// Skip loading the project config file.
    pub skip_project: bool,
    /// Skip environment variables.
    pub skip_env: bool,
}

/// Load Prodigy configuration with custom options.
///
/// This function allows fine-grained control over which sources are loaded.
pub fn load_prodigy_config_with_options(
    options: &LoadOptions,
) -> Result<Config<ProdigyConfig>, ConfigErrors> {
    load_prodigy_config_with_options_and_env(options, &RealEnv)
}

/// Load Prodigy configuration with custom options and environment.
pub fn load_prodigy_config_with_options_and_env<E: ConfigEnv>(
    options: &LoadOptions,
    env: &E,
) -> Result<Config<ProdigyConfig>, ConfigErrors> {
    let mut builder = Config::<ProdigyConfig>::builder();

    // Always start with defaults
    builder = builder.source(Defaults::from(ProdigyConfig::default()));

    // Add global config if not skipped
    if !options.skip_global {
        let global_path = global_config_path();
        builder = builder.source(
            Yaml::file(global_path.to_string_lossy().to_string())
                .optional()
                .named("global config"),
        );
    }

    // Add specific config path if provided
    if let Some(ref path) = options.config_path {
        builder = builder.source(
            Yaml::file(path.to_string_lossy().to_string())
                .required()
                .named("specified config"),
        );
    } else if !options.skip_project {
        // Add project config if not skipped and no specific path provided
        let project_path = project_config_path();
        builder = builder.source(
            Yaml::file(project_path.to_string_lossy().to_string())
                .optional()
                .named("project config"),
        );
    }

    // Add environment variables if not skipped
    if !options.skip_env {
        builder = builder
            .source(Env::prefix("PRODIGY__").separator("__"))
            // Legacy environment variable mappings for backward compatibility
            .source(
                Env::prefix("PRODIGY_")
                    .map("CLAUDE_API_KEY", "claude_api_key")
                    .map("LOG_LEVEL", "log_level")
                    .map("AUTO_COMMIT", "auto_commit")
                    .map("EDITOR", "default_editor")
                    .map("MAX_CONCURRENT", "max_concurrent_specs"),
            );
    }

    builder.build_with_env(env)
}

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

    #[test]
    fn test_load_with_defaults_only() {
        let env = MockEnv::new();
        let config = load_prodigy_config_with(&env).unwrap();

        assert_eq!(config.log_level, "info");
        assert_eq!(config.max_concurrent_specs, 4);
        assert!(config.auto_commit);
    }

    #[test]
    fn test_load_with_env_vars_only() {
        // Test env override with TOML + snake_case field names
        // Use double underscore separator for snake_case fields
        #[derive(Debug, serde::Deserialize, DeriveValidate)]
        struct SnakeCaseConfig {
            #[validate(non_empty)]
            log_level: String,
            #[validate(range(1..=100))]
            max_items: usize,
        }

        let env = MockEnv::new()
            .with_file(
                "config.toml",
                r#"
log_level = "info"
max_items = 10
"#,
            )
            // Use double underscore separator - APP__LOG_LEVEL -> log_level
            .with_env("APP__LOG_LEVEL", "debug")
            .with_env("APP__MAX_ITEMS", "50");

        let config = Config::<SnakeCaseConfig>::builder()
            .source(Toml::file("config.toml"))
            .source(Env::prefix("APP__").separator("__"))
            .build_with_env(&env)
            .expect("should load successfully");

        assert_eq!(config.log_level, "debug"); // From env
        assert_eq!(config.max_items, 50); // From env
    }

    #[test]
    fn test_load_with_global_config() {
        let global_path = global_config_path();
        let env = MockEnv::new().with_file(
            global_path.to_string_lossy().to_string(),
            r#"
log_level: debug
max_concurrent_specs: 8
"#,
        );

        let config = load_prodigy_config_with(&env).unwrap();

        assert_eq!(config.log_level, "debug");
        assert_eq!(config.max_concurrent_specs, 8);
    }

    #[test]
    fn test_load_with_env_override() {
        let global_path = global_config_path();
        let env = MockEnv::new()
            .with_file(global_path.to_string_lossy().to_string(), "log_level: info")
            // Use double underscore prefix for ProdigyConfig
            .with_env("PRODIGY__LOG_LEVEL", "debug");

        let config = load_prodigy_config_with(&env).unwrap();

        // Environment variable takes precedence
        assert_eq!(config.log_level, "debug");
    }

    #[test]
    fn test_load_with_project_config() {
        let project_path = project_config_path();
        let env = MockEnv::new().with_file(
            project_path.to_string_lossy().to_string(),
            r#"
project:
  name: test-project
  spec_dir: custom-specs
"#,
        );

        let config = load_prodigy_config_with(&env).unwrap();

        assert!(config.project.is_some());
        let project = config.project.clone().unwrap();
        assert_eq!(project.name, Some("test-project".to_string()));
        assert_eq!(
            project.spec_dir,
            Some(std::path::PathBuf::from("custom-specs"))
        );
    }

    #[test]
    fn test_load_with_options_skip_global() {
        let global_path = global_config_path();
        let env = MockEnv::new().with_file(
            global_path.to_string_lossy().to_string(),
            "log_level: debug",
        );

        let options = LoadOptions {
            skip_global: true,
            ..Default::default()
        };

        let config = load_prodigy_config_with_options_and_env(&options, &env).unwrap();

        // Should use default because global was skipped
        assert_eq!(config.log_level, "info");
    }

    #[test]
    fn test_validation_error_accumulation() {
        let project_path = project_config_path();
        let env = MockEnv::new().with_file(
            project_path.to_string_lossy().to_string(),
            r#"
log_level: ""
max_concurrent_specs: 0
storage:
  compression_level: 15
"#,
        );

        let result = load_prodigy_config_with(&env);

        // Should fail with multiple validation errors
        assert!(result.is_err());

        let errors = result.unwrap_err();
        // Should have multiple errors (empty log_level, out-of-range max_concurrent_specs, out-of-range compression_level)
        assert!(
            errors.len() >= 2,
            "Expected multiple errors, got {}",
            errors.len()
        );
    }

    #[test]
    fn test_missing_config_files_ok() {
        // No files configured - should still work with defaults
        let env = MockEnv::new();
        let config = load_prodigy_config_with(&env).unwrap();

        assert_eq!(config.log_level, "info");
    }

    #[test]
    fn test_layered_precedence() {
        let global_path = global_config_path();
        let project_path = project_config_path();

        let env = MockEnv::new()
            .with_file(
                global_path.to_string_lossy().to_string(),
                r#"
log_level: debug
max_concurrent_specs: 8
auto_commit: true
"#,
            )
            .with_file(
                project_path.to_string_lossy().to_string(),
                r#"
log_level: warn
"#,
            )
            // Use double underscore prefix
            .with_env("PRODIGY__LOG_LEVEL", "error");

        let config = load_prodigy_config_with(&env).unwrap();

        // env > project > global > defaults
        assert_eq!(config.log_level, "error"); // from env
        assert_eq!(config.max_concurrent_specs, 8); // from global (project didn't override)
        assert!(config.auto_commit); // from global
    }

    #[test]
    fn test_traced_config() {
        let global_path = global_config_path();
        let env = MockEnv::new()
            .with_file(
                global_path.to_string_lossy().to_string(),
                "log_level: debug",
            )
            // Use double underscore prefix
            .with_env("PRODIGY__LOG_LEVEL", "warn");

        let traced = load_prodigy_config_traced_with(&env).unwrap();

        // The final value should be from env
        assert_eq!(traced.log_level, "warn");

        // Check the trace shows the override
        if let Some(trace) = traced.trace("log_level") {
            assert!(trace.was_overridden());
        }

        // Can extract the config
        let config = traced.into_inner();
        assert_eq!(config.log_level, "warn");
    }

    #[test]
    fn test_legacy_env_vars_claude_api_key() {
        // Test legacy single-underscore env var PRODIGY_CLAUDE_API_KEY
        let env = MockEnv::new().with_env("PRODIGY_CLAUDE_API_KEY", "test-api-key-123");

        let config = load_prodigy_config_with(&env).unwrap();

        assert_eq!(config.claude_api_key, Some("test-api-key-123".to_string()));
        assert_eq!(config.effective_api_key(), Some("test-api-key-123"));
    }

    #[test]
    fn test_legacy_env_vars_log_level() {
        // Test legacy single-underscore env var PRODIGY_LOG_LEVEL
        let env = MockEnv::new().with_env("PRODIGY_LOG_LEVEL", "debug");

        let config = load_prodigy_config_with(&env).unwrap();

        assert_eq!(config.log_level, "debug");
        assert_eq!(config.effective_log_level(), "debug");
    }

    #[test]
    fn test_legacy_env_vars_auto_commit() {
        // Test legacy single-underscore env var PRODIGY_AUTO_COMMIT
        let env = MockEnv::new().with_env("PRODIGY_AUTO_COMMIT", "false");

        let config = load_prodigy_config_with(&env).unwrap();

        assert!(!config.auto_commit);
        assert!(!config.effective_auto_commit());
    }

    #[test]
    fn test_legacy_env_vars_editor() {
        // Test legacy single-underscore env var PRODIGY_EDITOR
        let env = MockEnv::new().with_env("PRODIGY_EDITOR", "vim");

        let config = load_prodigy_config_with(&env).unwrap();

        assert_eq!(config.default_editor, Some("vim".to_string()));
        assert_eq!(config.effective_editor(), Some("vim"));
    }

    #[test]
    fn test_legacy_env_vars_max_concurrent() {
        // Test legacy single-underscore env var PRODIGY_MAX_CONCURRENT
        let env = MockEnv::new().with_env("PRODIGY_MAX_CONCURRENT", "16");

        let config = load_prodigy_config_with(&env).unwrap();

        assert_eq!(config.max_concurrent_specs, 16);
        assert_eq!(config.effective_max_concurrent(), 16);
    }

    #[test]
    fn test_legacy_env_vars_override_file() {
        // Legacy env vars should override file config
        let global_path = global_config_path();
        let env = MockEnv::new()
            .with_file(
                global_path.to_string_lossy().to_string(),
                r#"
log_level: info
auto_commit: true
"#,
            )
            .with_env("PRODIGY_LOG_LEVEL", "trace")
            .with_env("PRODIGY_AUTO_COMMIT", "false");

        let config = load_prodigy_config_with(&env).unwrap();

        assert_eq!(config.log_level, "trace"); // env override
        assert!(!config.auto_commit); // env override
    }

    #[test]
    fn test_all_legacy_env_vars_together() {
        // Test all legacy env vars together
        let env = MockEnv::new()
            .with_env("PRODIGY_CLAUDE_API_KEY", "my-key")
            .with_env("PRODIGY_LOG_LEVEL", "warn")
            .with_env("PRODIGY_AUTO_COMMIT", "true")
            .with_env("PRODIGY_EDITOR", "nano")
            .with_env("PRODIGY_MAX_CONCURRENT", "8");

        let config = load_prodigy_config_with(&env).unwrap();

        assert_eq!(config.claude_api_key, Some("my-key".to_string()));
        assert_eq!(config.log_level, "warn");
        assert!(config.auto_commit);
        assert_eq!(config.default_editor, Some("nano".to_string()));
        assert_eq!(config.max_concurrent_specs, 8);
    }
}