From 46ea0e5b61dd36ff56ac2a71e9a4ece26d59142d Mon Sep 17 00:00:00 2001 From: M3gA-Mind Date: Fri, 6 Nov 2026 01:26:10 +0530 Subject: [PATCH 1/3] fix(security): give the session's tools the answers the host gate got The host gate answers a tool call against the live or agent policy (effective_policy), but the session's tools hold the constructor policy, a separate from_config instance with its own snapshot cells. With the autonomy policy enabled, the tools' budget snapshot was never filled, so can_act was false and every file write was refused as "autonomy is read-only", even after an approval. When the evaluated policy is a different instance, authorize now copies its cached budget and class-gate answers onto the constructor policy: after the budget check, and as allow-all when the evaluated policy is unchecked. --- .../agent/tinyagents/host/security_gate.rs | 9 ++++ .../host/security_gate/authorize.rs | 2 + .../security_gate_screen_and_policy_tests.rs | 37 ++++++++++++++ .../src/security/policy/enforcement.rs | 49 +++++++++++++++++++ .../src/security/policy/policy_tests.rs | 32 ++++++++++++ 5 files changed, 129 insertions(+) diff --git a/crates/openhuman-core/src/agent/tinyagents/host/security_gate.rs b/crates/openhuman-core/src/agent/tinyagents/host/security_gate.rs index da96b824120..61668c3812b 100644 --- a/crates/openhuman-core/src/agent/tinyagents/host/security_gate.rs +++ b/crates/openhuman-core/src/agent/tinyagents/host/security_gate.rs @@ -241,6 +241,15 @@ impl OpenHumanSecurityGate { .unwrap_or_else(|| self.policy.clone()) } + /// Hand the answers TinySecurity gave for `evaluated` to the constructor + /// policy, which the session's tools hold, when the two are different + /// instances. The tools' sync gates read only their own cached answers. + fn share_answers(&self, evaluated: &Arc) { + if !Arc::ptr_eq(evaluated, &self.policy) { + self.policy.adopt_answers_from(evaluated); + } + } + /// The origin of the turn this gate authorizes for: the run's own context /// when it carries one, else the calling task's turn scope. fn turn_origin(&self) -> Option { diff --git a/crates/openhuman-core/src/agent/tinyagents/host/security_gate/authorize.rs b/crates/openhuman-core/src/agent/tinyagents/host/security_gate/authorize.rs index 14374cafe88..2f4b331b309 100644 --- a/crates/openhuman-core/src/agent/tinyagents/host/security_gate/authorize.rs +++ b/crates/openhuman-core/src/agent/tinyagents/host/security_gate/authorize.rs @@ -203,6 +203,7 @@ impl OpenHumanSecurityGate { // (`crate::security::switch`) skips the same stages: there is no // module to classify the action or keep the budget. if policy.unchecked() { + self.share_answers(&policy); return Ok(self.settled(channel_approved)); } @@ -268,6 +269,7 @@ impl OpenHumanSecurityGate { if let Err(reason) = budget { return Ok(GateDecision::deny(reason)); } + self.share_answers(&policy); channel_approved = approved; tracing::debug!( diff --git a/crates/openhuman-core/src/agent/tinyagents/host/security_gate_screen_and_policy_tests.rs b/crates/openhuman-core/src/agent/tinyagents/host/security_gate_screen_and_policy_tests.rs index d6988a0dded..aa88154fe51 100644 --- a/crates/openhuman-core/src/agent/tinyagents/host/security_gate_screen_and_policy_tests.rs +++ b/crates/openhuman-core/src/agent/tinyagents/host/security_gate_screen_and_policy_tests.rs @@ -391,3 +391,40 @@ async fn prompt_screening_uses_the_bound_dispatch_configuration() { }) .await; } + +/// TS-E2E-025: the session's tools hold the constructor policy, but the gate +/// answers against the live one. Whatever the gate allowed, the tools' own +/// gates must read too, or an enabled tool policy with no cached answer +/// refuses every write as "autonomy is read-only". +#[cfg(feature = "security-module")] +#[tokio::test] +async fn the_session_tools_can_act_on_what_the_live_policy_allowed() { + let session = Arc::new(SecurityPolicy { + enabled: true, + ..SecurityPolicy::default() + }); + let live = Arc::new(SecurityPolicy { + enabled: false, + ..SecurityPolicy::default() + }); + let _live = crate::security::live_policy::install_scoped( + live, + std::path::PathBuf::new(), + std::path::PathBuf::new(), + ); + assert!( + !session.can_act(), + "no answer is cached before the gate runs" + ); + + let decision = OpenHumanSecurityGate::new(session.clone(), registry()) + .authorize_tool(&req("write_file", json!({ "path": "notes.md" }))) + .await + .unwrap(); + + assert_eq!(decision, GateDecision::Allow); + assert!( + session.can_act(), + "the tools' policy must take the live policy's answer" + ); +} diff --git a/crates/openhuman-core/src/security/policy/enforcement.rs b/crates/openhuman-core/src/security/policy/enforcement.rs index a68309ecea6..f455bc493e9 100644 --- a/crates/openhuman-core/src/security/policy/enforcement.rs +++ b/crates/openhuman-core/src/security/policy/enforcement.rs @@ -120,6 +120,55 @@ impl SecurityPolicy { ) } + /// Take `governing`'s cached TinySecurity answers (budget and class gates), + /// so the sync tool gates holding this instance read the decision the host + /// gate made on `governing`. When `governing` is unchecked it allows + /// everything, which is what this instance's tools are told too. + /// + /// Each [`from_config`](Self::from_config) owns its cells, so the session + /// policy the tools hold and the live or agent policy the host gate answers + /// against are separate. Without this, an enabled tool policy never gets an + /// answer and refuses as read-only. + pub(crate) fn adopt_answers_from(&self, governing: &SecurityPolicy) { + use super::types::{CommandClass, GateDecision}; + // Read before writing, so cells shared by a clone never lock twice. + let (budget, gates) = if governing.unchecked() { + let all = [ + CommandClass::Read, + CommandClass::Write, + CommandClass::Network, + CommandClass::Install, + CommandClass::Destructive, + ]; + ( + Some(tinysecurity_bus::BudgetDecision2::Allow), + all.map(|class| (class, GateDecision::Allow)).to_vec(), + ) + } else { + let budget = governing + .budget_snapshot + .lock() + .map(|snapshot| snapshot.clone()) + .unwrap_or(None); + let gates = governing + .class_gate_snapshot + .lock() + .map(|snapshot| snapshot.clone()) + .unwrap_or_default(); + (budget, gates) + }; + log::debug!( + "[openhuman:policy] tool policy adopted the host gate's answers: budget={budget:?} classes={}", + gates.len() + ); + if let Ok(mut snapshot) = self.budget_snapshot.lock() { + *snapshot = budget; + } + if let Ok(mut snapshot) = self.class_gate_snapshot.lock() { + *snapshot = gates; + } + } + /// Cached class decision from TinySecurity for synchronous tool adapters. /// The async host gate refreshes it before the tool is dispatched; an /// enabled policy with no answer fails closed. diff --git a/crates/openhuman-core/src/security/policy/policy_tests.rs b/crates/openhuman-core/src/security/policy/policy_tests.rs index 74c50bd5eba..84eff29b001 100644 --- a/crates/openhuman-core/src/security/policy/policy_tests.rs +++ b/crates/openhuman-core/src/security/policy/policy_tests.rs @@ -272,3 +272,35 @@ fn an_unchecked_host_action_is_classed_from_its_permission_level() { Class::Destructive ); } + +/// TS-E2E-025: a policy takes another instance's cached TinySecurity answers, +/// so the tools read what the host gate was told. Adopting from a clone, which +/// shares the cells, must not deadlock. +#[cfg(feature = "security-module")] +#[test] +fn a_policy_adopts_the_answers_another_instance_was_given() { + let governing = default_policy(); + *governing.budget_snapshot.lock().unwrap() = Some(tinysecurity_bus::BudgetDecision2::Allow); + governing + .class_gate_snapshot + .lock() + .unwrap() + .push((CommandClass::Write, GateDecision::Prompt)); + let tools = default_policy(); + assert!(!tools.can_act()); + + tools.adopt_answers_from(&governing); + assert!(tools.can_act()); + assert_eq!( + tools.gate_decision(CommandClass::Write), + GateDecision::Prompt + ); + assert_eq!( + tools.gate_decision(CommandClass::Install), + GateDecision::Block + ); + + let shared = tools.clone(); + tools.adopt_answers_from(&shared); + assert!(tools.can_act()); +} From 5d174a9d98056b9cc15012a98c8372d34d46d89c Mon Sep 17 00:00:00 2001 From: M3gA-Mind Date: Fri, 6 Nov 2026 11:47:12 +0530 Subject: [PATCH 2/3] fix(security): refuse a call when the session's tool settings went stale Review (#8567): handing the live policy's cached answers to the session's own policy let its tools act, but they still validated paths against the settings the session was built with. After config.update_autonomy_settings added a forbidden path or switched the policy, a write could be checked against settings no longer in force. The gate now shares the answers only when the governing policy sets what the tools act on (tier, enabled, privacy, workspace_only, forbidden paths) the same way as the session's policy; otherwise it refuses the call and asks for a new conversation, which builds its tools from the current settings. --- .../agent/tinyagents/host/security_gate.rs | 19 ++++++++++-- .../host/security_gate/authorize.rs | 12 ++++++-- .../security_gate_screen_and_policy_tests.rs | 22 +++++++------- .../src/security/policy/enforcement.rs | 15 ++++++++++ .../src/security/policy/policy_tests.rs | 30 +++++++++++++++++++ 5 files changed, 82 insertions(+), 16 deletions(-) diff --git a/crates/openhuman-core/src/agent/tinyagents/host/security_gate.rs b/crates/openhuman-core/src/agent/tinyagents/host/security_gate.rs index 61668c3812b..30d4f80ef52 100644 --- a/crates/openhuman-core/src/agent/tinyagents/host/security_gate.rs +++ b/crates/openhuman-core/src/agent/tinyagents/host/security_gate.rs @@ -244,10 +244,23 @@ impl OpenHumanSecurityGate { /// Hand the answers TinySecurity gave for `evaluated` to the constructor /// policy, which the session's tools hold, when the two are different /// instances. The tools' sync gates read only their own cached answers. - fn share_answers(&self, evaluated: &Arc) { - if !Arc::ptr_eq(evaluated, &self.policy) { - self.policy.adopt_answers_from(evaluated); + /// + /// `false` when `evaluated` sets the tools' questions differently (the + /// autonomy settings changed during the session): the tools would check + /// paths against settings no longer in force, so the caller refuses. + fn share_answers(&self, evaluated: &Arc) -> bool { + if Arc::ptr_eq(evaluated, &self.policy) { + return true; + } + if !self.policy.decides_tools_like(evaluated) { + tracing::warn!( + target: "tinyagents", + "[tinyagents::host::security] the autonomy settings changed during this session; refusing the call" + ); + return false; } + self.policy.adopt_answers_from(evaluated); + true } /// The origin of the turn this gate authorizes for: the run's own context diff --git a/crates/openhuman-core/src/agent/tinyagents/host/security_gate/authorize.rs b/crates/openhuman-core/src/agent/tinyagents/host/security_gate/authorize.rs index 2f4b331b309..fff0d499513 100644 --- a/crates/openhuman-core/src/agent/tinyagents/host/security_gate/authorize.rs +++ b/crates/openhuman-core/src/agent/tinyagents/host/security_gate/authorize.rs @@ -1,5 +1,9 @@ use super::*; +/// Why a call is refused when the session's tools hold settings no longer in +/// force. A new conversation builds its tools from the current settings. +const STALE_SESSION_SETTINGS: &str = "[policy-blocked] The autonomy settings changed during this conversation. Start a new conversation to use the new settings."; + impl OpenHumanSecurityGate { pub(super) async fn authorize_tool_with_facts( &self, @@ -203,7 +207,9 @@ impl OpenHumanSecurityGate { // (`crate::security::switch`) skips the same stages: there is no // module to classify the action or keep the budget. if policy.unchecked() { - self.share_answers(&policy); + if !self.share_answers(&policy) { + return Ok(GateDecision::deny(STALE_SESSION_SETTINGS)); + } return Ok(self.settled(channel_approved)); } @@ -269,7 +275,9 @@ impl OpenHumanSecurityGate { if let Err(reason) = budget { return Ok(GateDecision::deny(reason)); } - self.share_answers(&policy); + if !self.share_answers(&policy) { + return Ok(GateDecision::deny(STALE_SESSION_SETTINGS)); + } channel_approved = approved; tracing::debug!( diff --git a/crates/openhuman-core/src/agent/tinyagents/host/security_gate_screen_and_policy_tests.rs b/crates/openhuman-core/src/agent/tinyagents/host/security_gate_screen_and_policy_tests.rs index aa88154fe51..2c7c2473417 100644 --- a/crates/openhuman-core/src/agent/tinyagents/host/security_gate_screen_and_policy_tests.rs +++ b/crates/openhuman-core/src/agent/tinyagents/host/security_gate_screen_and_policy_tests.rs @@ -393,12 +393,13 @@ async fn prompt_screening_uses_the_bound_dispatch_configuration() { } /// TS-E2E-025: the session's tools hold the constructor policy, but the gate -/// answers against the live one. Whatever the gate allowed, the tools' own -/// gates must read too, or an enabled tool policy with no cached answer -/// refuses every write as "autonomy is read-only". +/// answers against the live one. When the live settings no longer match the +/// session's (here the policy was switched off after the session was built), +/// the tools would validate paths against settings no longer in force, so the +/// call is refused and nothing is handed to the tools. #[cfg(feature = "security-module")] #[tokio::test] -async fn the_session_tools_can_act_on_what_the_live_policy_allowed() { +async fn a_call_is_refused_when_the_autonomy_settings_changed_during_the_session() { let session = Arc::new(SecurityPolicy { enabled: true, ..SecurityPolicy::default() @@ -412,19 +413,18 @@ async fn the_session_tools_can_act_on_what_the_live_policy_allowed() { std::path::PathBuf::new(), std::path::PathBuf::new(), ); - assert!( - !session.can_act(), - "no answer is cached before the gate runs" - ); let decision = OpenHumanSecurityGate::new(session.clone(), registry()) .authorize_tool(&req("write_file", json!({ "path": "notes.md" }))) .await .unwrap(); - assert_eq!(decision, GateDecision::Allow); assert!( - session.can_act(), - "the tools' policy must take the live policy's answer" + matches!(&decision, GateDecision::Deny { reason } if reason.contains("settings changed")), + "stale session settings must be refused: {decision:?}" + ); + assert!( + !session.can_act(), + "a refused call hands the tools nothing" ); } diff --git a/crates/openhuman-core/src/security/policy/enforcement.rs b/crates/openhuman-core/src/security/policy/enforcement.rs index f455bc493e9..d27c09d17b3 100644 --- a/crates/openhuman-core/src/security/policy/enforcement.rs +++ b/crates/openhuman-core/src/security/policy/enforcement.rs @@ -120,6 +120,21 @@ impl SecurityPolicy { ) } + /// Whether `other` sets what the tools' own gates act on (the tier, path + /// scope, forbidden paths and privacy) the same way. When it does not, the + /// autonomy settings changed after this instance was built, and tools + /// holding it would validate against settings no longer in force. + /// + /// Trusted roots are left out: the session's own policy adds the working + /// folder and the projects home as roots, so they differ legitimately. + pub(crate) fn decides_tools_like(&self, other: &SecurityPolicy) -> bool { + self.enabled == other.enabled + && self.autonomy == other.autonomy + && self.privacy_mode == other.privacy_mode + && self.workspace_only == other.workspace_only + && self.forbidden_paths == other.forbidden_paths + } + /// Take `governing`'s cached TinySecurity answers (budget and class gates), /// so the sync tool gates holding this instance read the decision the host /// gate made on `governing`. When `governing` is unchecked it allows diff --git a/crates/openhuman-core/src/security/policy/policy_tests.rs b/crates/openhuman-core/src/security/policy/policy_tests.rs index 84eff29b001..a057ac5a2e1 100644 --- a/crates/openhuman-core/src/security/policy/policy_tests.rs +++ b/crates/openhuman-core/src/security/policy/policy_tests.rs @@ -304,3 +304,33 @@ fn a_policy_adopts_the_answers_another_instance_was_given() { tools.adopt_answers_from(&shared); assert!(tools.can_act()); } + +/// TS-E2E-025: the settings the tools act on decide whether a live policy's +/// answers may be handed to a session's own instance. +#[test] +fn tool_settings_match_only_when_tier_scope_forbidden_paths_and_privacy_agree() { + let session = default_policy(); + assert!(session.decides_tools_like(&default_policy())); + + let forbidden = SecurityPolicy { + forbidden_paths: vec!["/srv/secret".into()], + ..default_policy() + }; + assert!(!session.decides_tools_like(&forbidden), "a newly forbidden path"); + let disabled = SecurityPolicy { + enabled: false, + ..default_policy() + }; + assert!(!session.decides_tools_like(&disabled), "the policy switched off"); + let rooted = SecurityPolicy { + trusted_roots: vec![TrustedRoot { + path: "/work".into(), + access: crate::security::policy::TrustedAccess::ReadWrite, + }], + ..default_policy() + }; + assert!( + session.decides_tools_like(&rooted), + "a session's own working-folder root is not a settings change" + ); +} From 1011fc99e21fbbccdcca894a5d89500f8c08b9e2 Mon Sep 17 00:00:00 2001 From: M3gA-Mind Date: Fri, 6 Nov 2026 13:15:30 +0530 Subject: [PATCH 3/3] style: rustfmt the stale-settings tests --- .../host/security_gate_screen_and_policy_tests.rs | 5 +---- .../openhuman-core/src/security/policy/policy_tests.rs | 10 ++++++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/crates/openhuman-core/src/agent/tinyagents/host/security_gate_screen_and_policy_tests.rs b/crates/openhuman-core/src/agent/tinyagents/host/security_gate_screen_and_policy_tests.rs index 2c7c2473417..01dd4dc7011 100644 --- a/crates/openhuman-core/src/agent/tinyagents/host/security_gate_screen_and_policy_tests.rs +++ b/crates/openhuman-core/src/agent/tinyagents/host/security_gate_screen_and_policy_tests.rs @@ -423,8 +423,5 @@ async fn a_call_is_refused_when_the_autonomy_settings_changed_during_the_session matches!(&decision, GateDecision::Deny { reason } if reason.contains("settings changed")), "stale session settings must be refused: {decision:?}" ); - assert!( - !session.can_act(), - "a refused call hands the tools nothing" - ); + assert!(!session.can_act(), "a refused call hands the tools nothing"); } diff --git a/crates/openhuman-core/src/security/policy/policy_tests.rs b/crates/openhuman-core/src/security/policy/policy_tests.rs index a057ac5a2e1..b5e7e536690 100644 --- a/crates/openhuman-core/src/security/policy/policy_tests.rs +++ b/crates/openhuman-core/src/security/policy/policy_tests.rs @@ -316,12 +316,18 @@ fn tool_settings_match_only_when_tier_scope_forbidden_paths_and_privacy_agree() forbidden_paths: vec!["/srv/secret".into()], ..default_policy() }; - assert!(!session.decides_tools_like(&forbidden), "a newly forbidden path"); + assert!( + !session.decides_tools_like(&forbidden), + "a newly forbidden path" + ); let disabled = SecurityPolicy { enabled: false, ..default_policy() }; - assert!(!session.decides_tools_like(&disabled), "the policy switched off"); + assert!( + !session.decides_tools_like(&disabled), + "the policy switched off" + ); let rooted = SecurityPolicy { trusted_roots: vec![TrustedRoot { path: "/work".into(),