Files
optimclaw/src/config/safety.rs
T
3aa36c8f55 fix(tests): eliminate env mutex poison cascade (#1558)
* fix(tests): eliminate env mutex poison cascade and fix test flakiness

The shared ENV_MUTEX used by ~68 config tests would cascade a single
test panic into failures across every module. Replace all .unwrap() /
.expect() lock acquisitions with a poison-recovering lock_env() helper.
Consolidate rogue module-local ENV_LOCK instances (workspace, orchestrator,
bootstrap) onto the shared global mutex to prevent cross-module races.

Also fixes:
- gateway user_id fallback was hardcoded to "default" instead of owner_id
- test_ironclaw_env_path used LazyLock which is order-dependent

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* test(helpers): add regression test for lock_env poison recovery

Satisfies the regression-test-check CI gate by adding a test that
intentionally poisons ENV_MUTEX and verifies lock_env() recovers.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* fix(ci): detect test changes inside #[cfg(test)] regions

The regression test check relied on git diff -W to expand context to
function boundaries, but git doesn't recognize Rust `mod tests {}` as a
function boundary. Changes to imports, helpers, or lock calls inside
test modules were invisible to the check.

Add a line-level fallback: for each changed .rs file, find where
#[cfg(test)] starts and check if any diff hunk targets a line at or
after that boundary. This catches edits anywhere inside test modules
regardless of git's language awareness.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* fix: address PR review feedback

- Clear ENV_MUTEX poison after regression test so it doesn't leave
  global state dirty for subsequent tests.
- Fix CI regression-test-check to match #[cfg(test)] only when followed
  by `mod` (the test module pattern), avoiding false positives from
  standalone #[cfg(test)] items like statics or functions.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
2026-03-22 14:36:24 -07:00

51 lines
1.6 KiB
Rust

use crate::config::helpers::{parse_bool_env, parse_optional_env};
use crate::error::ConfigError;
pub use ironclaw_safety::SafetyConfig;
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", 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::lock_env;
use crate::settings::Settings;
#[test]
fn resolve_falls_back_to_settings() {
let _guard = lock_env();
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 = lock_env();
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);
}
}