Skip to content

Commit ec71b1a

Browse files
authored
fix(sandbox): apply initial OCSF JSON setting (#1921)
1 parent ed65bfd commit ec71b1a

1 file changed

Lines changed: 49 additions & 5 deletions

File tree

  • crates/openshell-sandbox/src

crates/openshell-sandbox/src/lib.rs

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,7 @@ async fn run_policy_poll_loop(ctx: PolicyPollLoopContext) -> Result<()> {
14531453
// Initialize revision from the first poll.
14541454
match client.poll_settings(&ctx.sandbox_id).await {
14551455
Ok(result) => {
1456+
apply_ocsf_json_setting(&ctx.ocsf_enabled, &result.settings);
14561457
current_config_revision = result.config_revision;
14571458
current_policy_hash = result.policy_hash.clone();
14581459
current_settings = result.settings;
@@ -1629,11 +1630,7 @@ async fn run_policy_poll_loop(ctx: PolicyPollLoopContext) -> Result<()> {
16291630
}
16301631

16311632
// Apply OCSF JSON toggle from the `ocsf_json_enabled` setting.
1632-
let new_ocsf = extract_bool_setting(&result.settings, "ocsf_json_enabled").unwrap_or(false);
1633-
let prev_ocsf = ctx.ocsf_enabled.swap(new_ocsf, Ordering::Relaxed);
1634-
if new_ocsf != prev_ocsf {
1635-
info!(ocsf_json_enabled = new_ocsf, "OCSF JSONL logging toggled");
1636-
}
1633+
apply_ocsf_json_setting(&ctx.ocsf_enabled, &result.settings);
16371634

16381635
// Apply the agent-proposals feature toggle. On a false→true transition
16391636
// we lazily install the skill so a sandbox that started with the flag
@@ -1675,6 +1672,19 @@ async fn run_policy_poll_loop(ctx: PolicyPollLoopContext) -> Result<()> {
16751672
}
16761673
}
16771674

1675+
fn apply_ocsf_json_setting(
1676+
enabled: &std::sync::atomic::AtomicBool,
1677+
settings: &std::collections::HashMap<String, openshell_core::proto::EffectiveSetting>,
1678+
) {
1679+
use std::sync::atomic::Ordering;
1680+
1681+
let new_ocsf = extract_bool_setting(settings, "ocsf_json_enabled").unwrap_or(false);
1682+
let prev_ocsf = enabled.swap(new_ocsf, Ordering::Relaxed);
1683+
if new_ocsf != prev_ocsf {
1684+
info!(ocsf_json_enabled = new_ocsf, "OCSF JSONL logging toggled");
1685+
}
1686+
}
1687+
16781688
/// Extract a bool value from an effective setting, if present.
16791689
fn extract_bool_setting(
16801690
settings: &std::collections::HashMap<String, openshell_core::proto::EffectiveSetting>,
@@ -1769,6 +1779,40 @@ fn format_setting_value(es: &openshell_core::proto::EffectiveSetting) -> String
17691779
)]
17701780
mod tests {
17711781
use super::*;
1782+
use std::sync::atomic::{AtomicBool, Ordering};
1783+
1784+
fn effective_bool(value: bool) -> openshell_core::proto::EffectiveSetting {
1785+
openshell_core::proto::EffectiveSetting {
1786+
value: Some(openshell_core::proto::SettingValue {
1787+
value: Some(openshell_core::proto::setting_value::Value::BoolValue(
1788+
value,
1789+
)),
1790+
}),
1791+
scope: openshell_core::proto::SettingScope::Global.into(),
1792+
}
1793+
}
1794+
1795+
#[test]
1796+
fn apply_ocsf_json_setting_enables_from_initial_settings_snapshot() {
1797+
let enabled = AtomicBool::new(false);
1798+
let mut settings = std::collections::HashMap::new();
1799+
settings.insert("ocsf_json_enabled".to_string(), effective_bool(true));
1800+
1801+
apply_ocsf_json_setting(&enabled, &settings);
1802+
1803+
assert!(enabled.load(Ordering::Relaxed));
1804+
}
1805+
1806+
#[test]
1807+
fn apply_ocsf_json_setting_disables_when_setting_is_unset() {
1808+
let enabled = AtomicBool::new(true);
1809+
let settings = std::collections::HashMap::new();
1810+
1811+
apply_ocsf_json_setting(&enabled, &settings);
1812+
1813+
assert!(!enabled.load(Ordering::Relaxed));
1814+
}
1815+
17721816
// ---- Policy disk discovery tests ----
17731817

17741818
#[test]

0 commit comments

Comments
 (0)