Skip to main content

ralph/config/
mod.rs

1//! Configuration resolution for Ralph, including global and project layers.
2//!
3//! Responsibilities:
4//! - Resolve configuration from multiple layers: global config, project config, and defaults.
5//! - Load and parse config files (JSON with JSONC comment support via `load_layer`).
6//! - Merge configuration layers via `ConfigLayer` and `apply_layer`.
7//! - Validate configuration values (version, paths, numeric ranges, runner binaries).
8//! - Resolve queue/done file paths and ID generation settings (prefix, width).
9//! - Discover repository root via `.ralph/` directory or `.git/`.
10//!
11//! Not handled here:
12//! - CLI argument parsing (see `crate::cli`).
13//! - Queue operations like task CRUD (see `crate::queue`).
14//! - Runner execution or agent invocation (see `crate::runner`).
15//! - Prompt rendering or template processing (see `crate::prompts_internal`).
16//! - Lock management (see `crate::lock`).
17//!
18//! Invariants/assumptions:
19//! - Config version must be 1; unsupported versions are rejected.
20//! - Paths are resolved relative to repo root unless absolute.
21//! - Global config resolves from `~/.config/ralph/config.jsonc` with `.json` fallback.
22//! - Project config resolves from `.ralph/config.jsonc` with `.json` fallback.
23//! - Config layers are applied in this order: defaults, then global, then project (later layers override earlier ones).
24//! - `save_layer` creates parent directories automatically if needed.
25
26use std::path::PathBuf;
27
28mod layer;
29mod resolution;
30mod validation;
31
32#[cfg(test)]
33mod tests;
34
35// Re-export main types and functions for backward compatibility
36pub use layer::{ConfigLayer, apply_layer, load_layer, save_layer};
37pub use resolution::{
38    find_repo_root, global_config_path, prefer_jsonc_then_json, project_config_path,
39    resolve_done_path, resolve_from_cwd, resolve_from_cwd_for_doctor,
40    resolve_from_cwd_with_profile, resolve_id_prefix, resolve_id_width, resolve_queue_path,
41};
42pub use validation::{
43    git_ref_invalid_reason, validate_agent_binary_paths, validate_agent_patch, validate_config,
44    validate_queue_done_file_override, validate_queue_file_override,
45    validate_queue_id_prefix_override, validate_queue_id_width_override, validate_queue_overrides,
46};
47
48/// Resolved configuration including computed paths.
49#[derive(Debug, Clone)]
50pub struct Resolved {
51    pub config: crate::contracts::Config,
52    pub repo_root: PathBuf,
53    pub queue_path: PathBuf,
54    pub done_path: PathBuf,
55    pub id_prefix: String,
56    pub id_width: usize,
57    pub global_config_path: Option<PathBuf>,
58    pub project_config_path: Option<PathBuf>,
59}