feat(config): unify config resolution with Settings fallback (Phase 2, #1119) (#1203)

Unify config resolution with Settings fallback (Phase 2)
This commit is contained in:
Reid
2026-03-16 08:01:51 +00:00
committed by GitHub
parent 0c31da46e7
commit a357972908
6 changed files with 284 additions and 32 deletions
+42 -6
View File
@@ -32,13 +32,16 @@ impl Default for BuilderModeConfig {
}
impl BuilderModeConfig {
pub(crate) fn resolve() -> Result<Self, ConfigError> {
pub(crate) fn resolve(settings: &crate::settings::Settings) -> Result<Self, ConfigError> {
let bs = &settings.builder;
Ok(Self {
enabled: parse_bool_env("BUILDER_ENABLED", true)?,
build_dir: optional_env("BUILDER_DIR")?.map(PathBuf::from),
max_iterations: parse_optional_env("BUILDER_MAX_ITERATIONS", 20)?,
timeout_secs: parse_optional_env("BUILDER_TIMEOUT_SECS", 600)?,
auto_register: parse_bool_env("BUILDER_AUTO_REGISTER", true)?,
enabled: parse_bool_env("BUILDER_ENABLED", bs.enabled)?,
build_dir: optional_env("BUILDER_DIR")?
.map(PathBuf::from)
.or_else(|| bs.build_dir.clone()),
max_iterations: parse_optional_env("BUILDER_MAX_ITERATIONS", bs.max_iterations)?,
timeout_secs: parse_optional_env("BUILDER_TIMEOUT_SECS", bs.timeout_secs)?,
auto_register: parse_bool_env("BUILDER_AUTO_REGISTER", bs.auto_register)?,
})
}
@@ -56,3 +59,36 @@ impl BuilderModeConfig {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::helpers::ENV_MUTEX;
use crate::settings::Settings;
#[test]
fn resolve_falls_back_to_settings() {
let _guard = ENV_MUTEX.lock().expect("env mutex poisoned");
let mut settings = Settings::default();
settings.builder.max_iterations = 99;
settings.builder.auto_register = false;
let cfg = BuilderModeConfig::resolve(&settings).expect("resolve");
assert_eq!(cfg.max_iterations, 99);
assert!(!cfg.auto_register);
}
#[test]
fn env_overrides_settings() {
let _guard = ENV_MUTEX.lock().expect("env mutex poisoned");
let mut settings = Settings::default();
settings.builder.timeout_secs = 123;
// SAFETY: Under ENV_MUTEX, no concurrent env access.
unsafe { std::env::set_var("BUILDER_TIMEOUT_SECS", "3") };
let cfg = BuilderModeConfig::resolve(&settings).expect("resolve");
unsafe { std::env::remove_var("BUILDER_TIMEOUT_SECS") };
assert_eq!(cfg.timeout_secs, 3);
}
}
+5 -5
View File
@@ -317,15 +317,15 @@ impl Config {
channels: ChannelsConfig::resolve(settings, tunnel.is_enabled())?,
tunnel,
agent: AgentConfig::resolve(settings)?,
safety: resolve_safety_config()?,
wasm: WasmConfig::resolve()?,
safety: resolve_safety_config(settings)?,
wasm: WasmConfig::resolve(settings)?,
secrets: SecretsConfig::resolve().await?,
builder: BuilderModeConfig::resolve()?,
builder: BuilderModeConfig::resolve(settings)?,
heartbeat: HeartbeatConfig::resolve(settings)?,
hygiene: HygieneConfig::resolve()?,
routines: RoutineConfig::resolve()?,
sandbox: SandboxModeConfig::resolve()?,
claude_code: ClaudeCodeConfig::resolve()?,
sandbox: SandboxModeConfig::resolve(settings)?,
claude_code: ClaudeCodeConfig::resolve(settings)?,
skills: SkillsConfig::resolve()?,
transcription: TranscriptionConfig::resolve(settings)?,
search: WorkspaceSearchConfig::resolve()?,
+42 -3
View File
@@ -3,9 +3,48 @@ use crate::error::ConfigError;
pub use ironclaw_safety::SafetyConfig;
pub(crate) fn resolve_safety_config() -> Result<SafetyConfig, ConfigError> {
pub(crate) fn resolve_safety_config(
settings: &crate::settings::Settings,
) -> Result<SafetyConfig, ConfigError> {
let ss = &settings.safety;
Ok(SafetyConfig {
max_output_length: parse_optional_env("SAFETY_MAX_OUTPUT_LENGTH", 100_000)?,
injection_check_enabled: parse_bool_env("SAFETY_INJECTION_CHECK_ENABLED", true)?,
max_output_length: parse_optional_env("SAFETY_MAX_OUTPUT_LENGTH", ss.max_output_length)?,
injection_check_enabled: parse_bool_env(
"SAFETY_INJECTION_CHECK_ENABLED",
ss.injection_check_enabled,
)?,
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::helpers::ENV_MUTEX;
use crate::settings::Settings;
#[test]
fn resolve_falls_back_to_settings() {
let _guard = ENV_MUTEX.lock().expect("env mutex poisoned");
let mut settings = Settings::default();
settings.safety.max_output_length = 42;
settings.safety.injection_check_enabled = false;
let cfg = resolve_safety_config(&settings).expect("resolve");
assert_eq!(cfg.max_output_length, 42);
assert!(!cfg.injection_check_enabled);
}
#[test]
fn env_overrides_settings() {
let _guard = ENV_MUTEX.lock().expect("env mutex poisoned");
let mut settings = Settings::default();
settings.safety.max_output_length = 42;
// SAFETY: Under ENV_MUTEX, no concurrent env access.
unsafe { std::env::set_var("SAFETY_MAX_OUTPUT_LENGTH", "7") };
let cfg = resolve_safety_config(&settings).expect("resolve");
unsafe { std::env::remove_var("SAFETY_MAX_OUTPUT_LENGTH") };
assert_eq!(cfg.max_output_length, 7);
}
}
+121 -11
View File
@@ -52,11 +52,20 @@ impl Default for SandboxModeConfig {
}
impl SandboxModeConfig {
pub(crate) fn resolve() -> Result<Self, ConfigError> {
pub(crate) fn resolve(settings: &crate::settings::Settings) -> Result<Self, ConfigError> {
let ss = &settings.sandbox;
let extra_domains = optional_env("SANDBOX_EXTRA_DOMAINS")?
.map(|s| s.split(',').map(|d| d.trim().to_string()).collect())
.unwrap_or_default();
.unwrap_or_else(|| {
if ss.extra_allowed_domains.is_empty() {
Vec::new()
} else {
ss.extra_allowed_domains.clone()
}
});
// reaper/orphan fields have no Settings counterpart — env > default only.
let reaper_interval_secs: u64 = parse_optional_env("SANDBOX_REAPER_INTERVAL_SECS", 300)?;
let orphan_threshold_secs: u64 = parse_optional_env("SANDBOX_ORPHAN_THRESHOLD_SECS", 600)?;
@@ -76,14 +85,15 @@ impl SandboxModeConfig {
}
Ok(Self {
enabled: parse_bool_env("SANDBOX_ENABLED", true)?,
policy: parse_string_env("SANDBOX_POLICY", "readonly")?,
enabled: parse_bool_env("SANDBOX_ENABLED", ss.enabled)?,
policy: parse_string_env("SANDBOX_POLICY", ss.policy.clone())?,
// allow_full_access has no Settings counterpart — env > default only.
allow_full_access: parse_bool_env("SANDBOX_ALLOW_FULL_ACCESS", false)?,
timeout_secs: parse_optional_env("SANDBOX_TIMEOUT_SECS", 120)?,
memory_limit_mb: parse_optional_env("SANDBOX_MEMORY_LIMIT_MB", 2048)?,
cpu_shares: parse_optional_env("SANDBOX_CPU_SHARES", 1024)?,
image: parse_string_env("SANDBOX_IMAGE", "ironclaw-worker:latest")?,
auto_pull_image: parse_bool_env("SANDBOX_AUTO_PULL", true)?,
timeout_secs: parse_optional_env("SANDBOX_TIMEOUT_SECS", ss.timeout_secs)?,
memory_limit_mb: parse_optional_env("SANDBOX_MEMORY_LIMIT_MB", ss.memory_limit_mb)?,
cpu_shares: parse_optional_env("SANDBOX_CPU_SHARES", ss.cpu_shares)?,
image: parse_string_env("SANDBOX_IMAGE", ss.image.clone())?,
auto_pull_image: parse_bool_env("SANDBOX_AUTO_PULL", ss.auto_pull_image)?,
extra_allowed_domains: extra_domains,
reaper_interval_secs,
orphan_threshold_secs,
@@ -200,7 +210,7 @@ impl ClaudeCodeConfig {
/// Load from environment variables only (used inside containers where
/// there is no database or full config).
pub fn from_env() -> Self {
match Self::resolve() {
match Self::resolve_env_only() {
Ok(c) => c,
Err(e) => {
tracing::warn!("Failed to resolve ClaudeCodeConfig: {e}, using defaults");
@@ -253,7 +263,33 @@ impl ClaudeCodeConfig {
None
}
pub(crate) fn resolve() -> Result<Self, ConfigError> {
pub(crate) fn resolve(settings: &crate::settings::Settings) -> Result<Self, ConfigError> {
let defaults = Self::default();
Ok(Self {
// Use settings.sandbox.claude_code_enabled as fallback (written by setup wizard).
enabled: parse_bool_env("CLAUDE_CODE_ENABLED", settings.sandbox.claude_code_enabled)?,
config_dir: optional_env("CLAUDE_CONFIG_DIR")?
.map(std::path::PathBuf::from)
.unwrap_or(defaults.config_dir),
model: parse_string_env("CLAUDE_CODE_MODEL", defaults.model)?,
max_turns: parse_optional_env("CLAUDE_CODE_MAX_TURNS", defaults.max_turns)?,
memory_limit_mb: parse_optional_env(
"CLAUDE_CODE_MEMORY_LIMIT_MB",
defaults.memory_limit_mb,
)?,
allowed_tools: optional_env("CLAUDE_CODE_ALLOWED_TOOLS")?
.map(|s| {
s.split(',')
.map(|t| t.trim().to_string())
.filter(|t| !t.is_empty())
.collect()
})
.unwrap_or(defaults.allowed_tools),
})
}
/// Resolve from env vars only, no Settings. Used inside containers.
fn resolve_env_only() -> Result<Self, ConfigError> {
let defaults = Self::default();
Ok(Self {
enabled: parse_bool_env("CLAUDE_CODE_ENABLED", defaults.enabled)?,
@@ -554,6 +590,80 @@ mod tests {
);
}
// ── Settings fallback tests ──────────────────────────────────────
#[test]
fn sandbox_resolve_falls_back_to_settings() {
let _guard = crate::config::helpers::ENV_MUTEX
.lock()
.expect("env mutex poisoned");
let mut settings = crate::settings::Settings::default();
settings.sandbox.cpu_shares = 99;
settings.sandbox.auto_pull_image = false;
settings.sandbox.enabled = false;
let cfg = SandboxModeConfig::resolve(&settings).expect("resolve");
assert!(!cfg.enabled);
assert_eq!(cfg.cpu_shares, 99);
assert!(!cfg.auto_pull_image);
}
#[test]
fn sandbox_env_overrides_settings() {
let _guard = crate::config::helpers::ENV_MUTEX
.lock()
.expect("env mutex poisoned");
let mut settings = crate::settings::Settings::default();
settings.sandbox.timeout_secs = 999;
// SAFETY: Under ENV_MUTEX, no concurrent env access.
unsafe { std::env::set_var("SANDBOX_TIMEOUT_SECS", "5") };
let cfg = SandboxModeConfig::resolve(&settings).expect("resolve");
unsafe { std::env::remove_var("SANDBOX_TIMEOUT_SECS") };
assert_eq!(cfg.timeout_secs, 5);
}
// ── ClaudeCodeConfig settings fallback tests ────────────────────
#[test]
fn claude_code_resolve_uses_settings_enabled() {
let _guard = crate::config::helpers::ENV_MUTEX
.lock()
.expect("env mutex poisoned");
let mut settings = crate::settings::Settings::default();
settings.sandbox.claude_code_enabled = true;
let cfg = ClaudeCodeConfig::resolve(&settings).expect("resolve");
assert!(cfg.enabled);
}
#[test]
fn claude_code_resolve_defaults_disabled() {
let _guard = crate::config::helpers::ENV_MUTEX
.lock()
.expect("env mutex poisoned");
let settings = crate::settings::Settings::default();
let cfg = ClaudeCodeConfig::resolve(&settings).expect("resolve");
assert!(!cfg.enabled);
}
#[test]
fn claude_code_env_overrides_settings() {
let _guard = crate::config::helpers::ENV_MUTEX
.lock()
.expect("env mutex poisoned");
let mut settings = crate::settings::Settings::default();
settings.sandbox.claude_code_enabled = true;
// SAFETY: Under ENV_MUTEX, no concurrent env access.
unsafe { std::env::set_var("CLAUDE_CODE_ENABLED", "false") };
let cfg = ClaudeCodeConfig::resolve(&settings).expect("resolve");
unsafe { std::env::remove_var("CLAUDE_CODE_ENABLED") };
assert!(!cfg.enabled);
}
#[test]
fn test_readonly_policy_unaffected() {
let config = SandboxModeConfig {
+50 -7
View File
@@ -44,20 +44,30 @@ fn default_tools_dir() -> PathBuf {
}
impl WasmConfig {
pub(crate) fn resolve() -> Result<Self, ConfigError> {
pub(crate) fn resolve(settings: &crate::settings::Settings) -> Result<Self, ConfigError> {
let ws = &settings.wasm;
Ok(Self {
enabled: parse_bool_env("WASM_ENABLED", true)?,
enabled: parse_bool_env("WASM_ENABLED", ws.enabled)?,
tools_dir: optional_env("WASM_TOOLS_DIR")?
.map(PathBuf::from)
.or_else(|| ws.tools_dir.clone())
.unwrap_or_else(default_tools_dir),
default_memory_limit: parse_optional_env(
"WASM_DEFAULT_MEMORY_LIMIT",
10 * 1024 * 1024,
ws.default_memory_limit,
)?,
default_timeout_secs: parse_optional_env("WASM_DEFAULT_TIMEOUT_SECS", 60)?,
default_fuel_limit: parse_optional_env("WASM_DEFAULT_FUEL_LIMIT", 10_000_000)?,
cache_compiled: parse_bool_env("WASM_CACHE_COMPILED", true)?,
cache_dir: optional_env("WASM_CACHE_DIR")?.map(PathBuf::from),
default_timeout_secs: parse_optional_env(
"WASM_DEFAULT_TIMEOUT_SECS",
ws.default_timeout_secs,
)?,
default_fuel_limit: parse_optional_env(
"WASM_DEFAULT_FUEL_LIMIT",
ws.default_fuel_limit,
)?,
cache_compiled: parse_bool_env("WASM_CACHE_COMPILED", ws.cache_compiled)?,
cache_dir: optional_env("WASM_CACHE_DIR")?
.map(PathBuf::from)
.or_else(|| ws.cache_dir.clone()),
})
}
@@ -81,3 +91,36 @@ impl WasmConfig {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::helpers::ENV_MUTEX;
use crate::settings::Settings;
#[test]
fn resolve_falls_back_to_settings() {
let _guard = ENV_MUTEX.lock().expect("env mutex poisoned");
let mut settings = Settings::default();
settings.wasm.default_memory_limit = 42;
settings.wasm.cache_compiled = false;
let cfg = WasmConfig::resolve(&settings).expect("resolve");
assert_eq!(cfg.default_memory_limit, 42);
assert!(!cfg.cache_compiled);
}
#[test]
fn env_overrides_settings() {
let _guard = ENV_MUTEX.lock().expect("env mutex poisoned");
let mut settings = Settings::default();
settings.wasm.default_fuel_limit = 42;
// SAFETY: Under ENV_MUTEX, no concurrent env access.
unsafe { std::env::set_var("WASM_DEFAULT_FUEL_LIMIT", "7") };
let cfg = WasmConfig::resolve(&settings).expect("resolve");
unsafe { std::env::remove_var("WASM_DEFAULT_FUEL_LIMIT") };
assert_eq!(cfg.default_fuel_limit, 7);
}
}
+24
View File
@@ -523,6 +523,30 @@ async fn async_main() -> anyhow::Result<()> {
}
}
// Persist auto-generated auth token so it survives restarts.
// Write to the "default" settings namespace, which is the namespace
// Config::from_db() reads from — NOT the gateway channel's user_id.
if gw_config.auth_token.is_none() {
let token_to_persist = gw.auth_token().to_string();
if let Some(ref db) = components.db {
let db = db.clone();
tokio::spawn(async move {
if let Err(e) = db
.set_setting(
"default",
"channels.gateway_auth_token",
&serde_json::Value::String(token_to_persist),
)
.await
{
tracing::warn!("Failed to persist auto-generated gateway auth token: {e}");
} else {
tracing::debug!("Persisted auto-generated gateway auth token to settings");
}
});
}
}
gateway_url = Some(format!(
"http://{}:{}/?token={}",
gw_config.host,