diff --git a/crates/openhuman-core/src/security/live_policy.rs b/crates/openhuman-core/src/security/live_policy.rs index 3597a13c7f1..9018a7e55ff 100644 --- a/crates/openhuman-core/src/security/live_policy.rs +++ b/crates/openhuman-core/src/security/live_policy.rs @@ -201,7 +201,14 @@ pub fn generation() -> u64 { /// Rebuild the policy from `autonomy_config` against the stored workspace dir /// and swap it in, bumping the generation counter. No-op if nothing has been /// installed yet (e.g. a CLI invocation that never started a session runtime). +/// A test thread holding an `install_scoped` policy owns its policy, so its +/// reloads never reach the process-global one that sibling tests read. pub fn reload_from(autonomy_config: &crate::config::AutonomyConfig) { + #[cfg(test)] + if TEST_POLICY_OVERRIDE.with(|current| current.borrow().is_some()) { + tracing::trace!("[security:live_policy] reload skipped for scoped test policy"); + return; + } let Some(state) = STATE.get() else { return; }; diff --git a/crates/openhuman-core/src/security/live_policy_tests.rs b/crates/openhuman-core/src/security/live_policy_tests.rs index 830f3a65cb8..6e92ae54c94 100644 --- a/crates/openhuman-core/src/security/live_policy_tests.rs +++ b/crates/openhuman-core/src/security/live_policy_tests.rs @@ -39,6 +39,40 @@ fn scoped_policy_is_thread_local_and_restored() { ); } +#[test] +fn a_reload_on_a_scoped_thread_leaves_the_process_policy_alone() { + let _env = crate::config::TEST_ENV_LOCK.blocking_lock(); + let workspace = std::env::temp_dir().join("openhuman_scoped_reload_test"); + install( + Arc::new(SecurityPolicy::default()), + workspace.clone(), + workspace.clone(), + ); + let _guard = install_scoped( + Arc::new(SecurityPolicy::default()), + workspace.clone(), + workspace, + ); + + reload_from(&AutonomyConfig { + auto_approve: vec!["create_workflow".into()], + ..AutonomyConfig::default() + }); + + let sibling_list = std::thread::spawn(|| { + current() + .expect("global policy remains installed") + .auto_approve + .clone() + }) + .join() + .expect("sibling thread joined"); + assert!( + !sibling_list.iter().any(|tool| tool == "create_workflow"), + "a scoped test's reload must not reach the process-global policy" + ); +} + #[test] fn install_then_reload_swaps_policy_and_bumps_generation() { // Serialize against other tests that install/reload this process-global