Skip to content

Commit 0972cd9

Browse files
authored
chore: refactor to move Arc<RwLock> concern outside exec_policy_for (#7615)
The caller should decide whether wrapping the policy in `Arc<RwLock>` is necessary. This should make #7609 a bit smoother. - `exec_policy_for()` -> `load_exec_policy_for_features()` - introduce `load_exec_policy()` that does not take `Features` as an arg - both return `Result<Policy, ExecPolicyError>` instead of Result<Arc<RwLock<Policy>>, ExecPolicyError>` This simplifies the tests as they have no need for `Arc<RwLock>`.
1 parent 28dcdb5 commit 0972cd9

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

codex-rs/core/src/codex.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::compact;
1111
use crate::compact::run_inline_auto_compact_task;
1212
use crate::compact::should_use_remote_compact_task;
1313
use crate::compact_remote::run_inline_remote_auto_compact_task;
14+
use crate::exec_policy::load_exec_policy_for_features;
1415
use crate::features::Feature;
1516
use crate::features::Features;
1617
use crate::openai_models::models_manager::ModelsManager;
@@ -174,9 +175,10 @@ impl Codex {
174175

175176
let user_instructions = get_user_instructions(&config).await;
176177

177-
let exec_policy = crate::exec_policy::exec_policy_for(&config.features, &config.codex_home)
178+
let exec_policy = load_exec_policy_for_features(&config.features, &config.codex_home)
178179
.await
179180
.map_err(|err| CodexErr::Fatal(format!("failed to load execpolicy: {err}")))?;
181+
let exec_policy = Arc::new(RwLock::new(exec_policy));
180182

181183
let config = Arc::new(config);
182184

codex-rs/core/src/exec_policy.rs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,18 @@ pub enum ExecPolicyUpdateError {
7373
FeatureDisabled,
7474
}
7575

76-
pub(crate) async fn exec_policy_for(
76+
pub(crate) async fn load_exec_policy_for_features(
7777
features: &Features,
7878
codex_home: &Path,
79-
) -> Result<Arc<RwLock<Policy>>, ExecPolicyError> {
79+
) -> Result<Policy, ExecPolicyError> {
8080
if !features.enabled(Feature::ExecPolicy) {
81-
return Ok(Arc::new(RwLock::new(Policy::empty())));
81+
Ok(Policy::empty())
82+
} else {
83+
load_exec_policy(codex_home).await
8284
}
85+
}
8386

87+
pub async fn load_exec_policy(codex_home: &Path) -> Result<Policy, ExecPolicyError> {
8488
let policy_dir = codex_home.join(POLICY_DIR_NAME);
8589
let policy_paths = collect_policy_files(&policy_dir).await?;
8690

@@ -102,7 +106,7 @@ pub(crate) async fn exec_policy_for(
102106
})?;
103107
}
104108

105-
let policy = Arc::new(RwLock::new(parser.build()));
109+
let policy = parser.build();
106110
tracing::debug!(
107111
"loaded execpolicy from {} files in {}",
108112
policy_paths.len(),
@@ -306,7 +310,7 @@ mod tests {
306310
features.disable(Feature::ExecPolicy);
307311
let temp_dir = tempdir().expect("create temp dir");
308312

309-
let policy = exec_policy_for(&features, temp_dir.path())
313+
let policy = load_exec_policy_for_features(&features, temp_dir.path())
310314
.await
311315
.expect("policy result");
312316

@@ -319,10 +323,7 @@ mod tests {
319323
decision: Decision::Allow
320324
}],
321325
},
322-
policy
323-
.read()
324-
.await
325-
.check_multiple(commands.iter(), &|_| Decision::Allow)
326+
policy.check_multiple(commands.iter(), &|_| Decision::Allow)
326327
);
327328
assert!(!temp_dir.path().join(POLICY_DIR_NAME).exists());
328329
}
@@ -350,7 +351,7 @@ mod tests {
350351
)
351352
.expect("write policy file");
352353

353-
let policy = exec_policy_for(&Features::with_defaults(), temp_dir.path())
354+
let policy = load_exec_policy(temp_dir.path())
354355
.await
355356
.expect("policy result");
356357
let command = [vec!["rm".to_string()]];
@@ -362,10 +363,7 @@ mod tests {
362363
decision: Decision::Forbidden
363364
}],
364365
},
365-
policy
366-
.read()
367-
.await
368-
.check_multiple(command.iter(), &|_| Decision::Allow)
366+
policy.check_multiple(command.iter(), &|_| Decision::Allow)
369367
);
370368
}
371369

@@ -378,7 +376,7 @@ mod tests {
378376
)
379377
.expect("write policy file");
380378

381-
let policy = exec_policy_for(&Features::with_defaults(), temp_dir.path())
379+
let policy = load_exec_policy(temp_dir.path())
382380
.await
383381
.expect("policy result");
384382
let command = [vec!["ls".to_string()]];
@@ -390,10 +388,7 @@ mod tests {
390388
decision: Decision::Allow
391389
}],
392390
},
393-
policy
394-
.read()
395-
.await
396-
.check_multiple(command.iter(), &|_| Decision::Allow)
391+
policy.check_multiple(command.iter(), &|_| Decision::Allow)
397392
);
398393
}
399394

0 commit comments

Comments
 (0)