diff --git a/codex-rs/core/src/config/mod.rs b/codex-rs/core/src/config/mod.rs index e906b456eb..6ec6b41d7c 100644 --- a/codex-rs/core/src/config/mod.rs +++ b/codex-rs/core/src/config/mod.rs @@ -1934,57 +1934,6 @@ impl Config { }; } - pub fn configured_sandbox_policy_subject_to_requirements( - &self, - ) -> ConstraintResult> { - let Some(user_layer) = self.config_layer_stack.get_user_layer() else { - return Ok(None); - }; - let user_config: ConfigToml = match user_layer.config.clone().try_into() { - Ok(config) => config, - Err(err) => { - tracing::warn!( - "failed to parse user config layer while resolving configured sandbox policy: {err}" - ); - return Ok(None); - } - }; - - let configured_sandbox_mode = self - .active_profile - .as_ref() - .and_then(|profile_name| user_config.profiles.get(profile_name)) - .and_then(|profile| profile.sandbox_mode) - .or(user_config.sandbox_mode); - - let Some(configured_sandbox_mode) = configured_sandbox_mode else { - return Ok(None); - }; - - let sandbox_policy = match configured_sandbox_mode { - SandboxMode::ReadOnly => SandboxPolicy::new_read_only_policy(), - SandboxMode::WorkspaceWrite => match user_config.sandbox_workspace_write.as_ref() { - Some(SandboxWorkspaceWrite { - writable_roots, - network_access, - exclude_tmpdir_env_var, - exclude_slash_tmp, - }) => SandboxPolicy::WorkspaceWrite { - writable_roots: writable_roots.clone(), - read_only_access: ReadOnlyAccess::FullAccess, - network_access: *network_access, - exclude_tmpdir_env_var: *exclude_tmpdir_env_var, - exclude_slash_tmp: *exclude_slash_tmp, - }, - None => SandboxPolicy::new_workspace_write_policy(), - }, - SandboxMode::DangerFullAccess => SandboxPolicy::DangerFullAccess, - }; - - self.permissions.sandbox_policy.can_set(&sandbox_policy)?; - Ok(Some(sandbox_policy)) - } - pub fn permissions_satisfy_requirements( &self, candidate: &Permissions, @@ -5132,138 +5081,6 @@ mcp_oauth_callback_port = 5678 Ok(()) } - #[tokio::test] - async fn configured_sandbox_policy_subject_to_requirements_returns_none_when_unset() - -> std::io::Result<()> { - let codex_home = TempDir::new()?; - let config = ConfigBuilder::default() - .codex_home(codex_home.path().to_path_buf()) - .fallback_cwd(Some(codex_home.path().to_path_buf())) - .build() - .await?; - - assert_eq!( - config.configured_sandbox_policy_subject_to_requirements()?, - None - ); - Ok(()) - } - - #[tokio::test] - async fn configured_sandbox_policy_subject_to_requirements_uses_active_profile_first() - -> std::io::Result<()> { - let codex_home = TempDir::new()?; - let mut profiles = HashMap::new(); - profiles.insert( - "work".to_string(), - ConfigProfile { - sandbox_mode: Some(SandboxMode::ReadOnly), - ..Default::default() - }, - ); - let config_toml = ConfigToml { - profile: Some("work".to_string()), - sandbox_mode: Some(SandboxMode::DangerFullAccess), - profiles, - ..Default::default() - }; - std::fs::write( - codex_home.path().join(CONFIG_TOML_FILE), - toml::to_string(&config_toml).expect("serialize config"), - )?; - - let config = ConfigBuilder::default() - .codex_home(codex_home.path().to_path_buf()) - .fallback_cwd(Some(codex_home.path().to_path_buf())) - .build() - .await?; - - assert_eq!(config.active_profile.as_deref(), Some("work")); - assert_eq!( - config.configured_sandbox_policy_subject_to_requirements()?, - Some(SandboxPolicy::new_read_only_policy()) - ); - Ok(()) - } - - #[tokio::test] - async fn configured_sandbox_policy_subject_to_requirements_falls_back_to_top_level() - -> std::io::Result<()> { - let codex_home = TempDir::new()?; - let writable_root_path = codex_home.path().join("writable-root"); - let writable_root = AbsolutePathBuf::try_from(writable_root_path.as_path()) - .expect("writable root should be absolute"); - let config_toml = ConfigToml { - sandbox_mode: Some(SandboxMode::WorkspaceWrite), - sandbox_workspace_write: Some(SandboxWorkspaceWrite { - writable_roots: vec![writable_root.clone()], - network_access: true, - exclude_tmpdir_env_var: true, - exclude_slash_tmp: true, - }), - ..Default::default() - }; - std::fs::write( - codex_home.path().join(CONFIG_TOML_FILE), - toml::to_string(&config_toml).expect("serialize config"), - )?; - - let config = ConfigBuilder::default() - .codex_home(codex_home.path().to_path_buf()) - .fallback_cwd(Some(codex_home.path().to_path_buf())) - .build() - .await?; - - assert_eq!( - config.configured_sandbox_policy_subject_to_requirements()?, - Some(SandboxPolicy::WorkspaceWrite { - writable_roots: vec![writable_root], - read_only_access: ReadOnlyAccess::FullAccess, - network_access: true, - exclude_tmpdir_env_var: true, - exclude_slash_tmp: true, - }) - ); - Ok(()) - } - - #[tokio::test] - async fn configured_sandbox_policy_subject_to_requirements_returns_err_when_disallowed() - -> std::io::Result<()> { - let codex_home = TempDir::new()?; - std::fs::write( - codex_home.path().join(CONFIG_TOML_FILE), - r#"sandbox_mode = "danger-full-access" -"#, - )?; - - let config = ConfigBuilder::default() - .codex_home(codex_home.path().to_path_buf()) - .fallback_cwd(Some(codex_home.path().to_path_buf())) - .cloud_requirements(CloudRequirementsLoader::new(async { - Some(crate::config_loader::ConfigRequirementsToml { - allowed_sandbox_modes: Some(vec![ - crate::config_loader::SandboxModeRequirement::ReadOnly, - ]), - ..Default::default() - }) - })) - .build() - .await?; - - let err = config - .configured_sandbox_policy_subject_to_requirements() - .expect_err("danger-full-access should be rejected"); - assert!(matches!( - err, - ConstraintError::InvalidValue { - field_name: "sandbox_mode", - .. - } - )); - Ok(()) - } - #[tokio::test] async fn permissions_satisfy_requirements_accepts_valid_candidate() -> std::io::Result<()> { let codex_home = TempDir::new()?; diff --git a/codex-rs/tui/src/chatwidget/tests.rs b/codex-rs/tui/src/chatwidget/tests.rs index cc8a38c9b8..d9d0c1c9e8 100644 --- a/codex-rs/tui/src/chatwidget/tests.rs +++ b/codex-rs/tui/src/chatwidget/tests.rs @@ -5342,16 +5342,13 @@ async fn disabled_slash_command_while_task_running_snapshot() { } #[tokio::test] -async fn approvals_popup_shows_disabled_presets() { +async fn approvals_popup_constrains_disallowed_presets() { let (mut chat, _rx, _op_rx) = make_chatwidget_manual(None).await; chat.config.permissions.approval_policy = Constrained::new(AskForApproval::OnRequest, |candidate| match candidate { AskForApproval::OnRequest => Ok(()), - _ => Err(invalid_value( - candidate.to_string(), - "this message should be printed in the description", - )), + _ => Err(invalid_value(candidate.to_string(), "[on-request]")), }) .expect("construct constrained approval policy"); chat.open_approvals_popup(); @@ -5368,17 +5365,17 @@ async fn approvals_popup_shows_disabled_presets() { let screen = terminal.backend().vt100().screen().contents(); let collapsed = screen.split_whitespace().collect::>().join(" "); assert!( - collapsed.contains("(disabled)"), - "disabled preset label should be shown" + !collapsed.contains("(disabled)"), + "disallowed presets should be omitted, not rendered disabled" ); assert!( - collapsed.contains("this message should be printed in the description"), - "disabled preset reason should be shown" + !collapsed.contains("Full Access"), + "full-access preset should be omitted when disallowed" ); } #[tokio::test] -async fn approvals_popup_navigation_skips_disabled() { +async fn approvals_popup_selection_ignores_missing_numeric_shortcuts() { let (mut chat, mut rx, mut op_rx) = make_chatwidget_manual(None).await; chat.config.permissions.approval_policy = @@ -5389,13 +5386,7 @@ async fn approvals_popup_navigation_skips_disabled() { .expect("construct constrained approval policy"); chat.open_approvals_popup(); - // The approvals popup is the active bottom-pane view; drive navigation via chat handle_key_event. - // Start selected at idx 0 (enabled), move down twice; the disabled option should be skipped - // and selection should wrap back to idx 0 (also enabled). - chat.handle_key_event(KeyEvent::from(KeyCode::Down)); - chat.handle_key_event(KeyEvent::from(KeyCode::Down)); - - // Press numeric shortcut for the disabled row (3 => idx 2); should not close or accept. + // Press numeric shortcut for an item that does not exist after filtering. chat.handle_key_event(KeyEvent::from(KeyCode::Char('3'))); // Ensure the popup remains open and no selection actions were sent. @@ -5406,11 +5397,11 @@ async fn approvals_popup_navigation_skips_disabled() { terminal.set_viewport_area(Rect::new(0, 0, width, height)); terminal .draw(|f| chat.render(f.area(), f.buffer_mut())) - .expect("render approvals popup after disabled selection"); + .expect("render approvals popup after invalid numeric shortcut"); let screen = terminal.backend().vt100().screen().contents(); assert!( screen.contains("Update Model Permissions"), - "popup should remain open after selecting a disabled entry" + "popup should remain open after selecting a non-existent entry" ); assert!( op_rx.try_recv().is_err(), @@ -5444,7 +5435,7 @@ async fn approvals_popup_navigation_skips_disabled() { .. }) )), - "disabled preset should not be selected" + "disallowed preset should not be selectable" ); } diff --git a/codex-rs/tui/src/permissions/mod.rs b/codex-rs/tui/src/permissions/mod.rs index 2682d7603c..1d08e7a6e7 100644 --- a/codex-rs/tui/src/permissions/mod.rs +++ b/codex-rs/tui/src/permissions/mod.rs @@ -6,6 +6,8 @@ use crate::bottom_pane::SelectionAction; use crate::bottom_pane::SelectionItem; use crate::history_cell; use codex_core::config::Config; +use codex_core::config::Constrained; +use codex_core::config::Permissions; use codex_core::protocol::AskForApproval; use codex_core::protocol::Op; use codex_core::protocol::SandboxPolicy; @@ -54,9 +56,10 @@ pub fn builtin_permissions_presets() -> Vec { } pub(crate) fn visible_permissions_options(config: &Config) -> Vec { - let mut presets = builtin_permissions_presets() + let presets = builtin_permissions_presets() .into_iter() - .filter(|preset| preset.is_visible(config)); + .filter(|preset| preset.is_visible(config)) + .filter(|preset| preset.satisfies_requirements(config)); presets .map(|preset| preset.to_selection_item(config)) @@ -90,7 +93,6 @@ impl PermissionsPreset { is_current: self.is_current(config), actions: self.actions(config), dismiss_on_select: true, - disabled_reason: self.disabled_reason(config), ..Default::default() } } @@ -100,31 +102,26 @@ impl PermissionsPreset { && self.sandbox == *config.permissions.sandbox_policy.get() } - fn disabled_reason(&self, config: &Config) -> Option { - let disabled_sandbox_reason = match config.permissions.sandbox_policy.can_set(&self.sandbox) - { - Ok(()) => None, - Err(err) => Some(err.to_string()), - }; - if disabled_sandbox_reason.is_some() { - return disabled_sandbox_reason; - } - - let disabled_approval_reason = - match config.permissions.approval_policy.can_set(&self.approval) { - Ok(()) => None, - Err(err) => Some(err.to_string()), - }; - if disabled_approval_reason.is_some() { - return disabled_approval_reason; - } - - None + fn satisfies_requirements(&self, config: &Config) -> bool { + config + .permissions_satisfy_requirements(&Permissions { + approval_policy: Constrained::allow_any(self.approval), + sandbox_policy: Constrained::allow_any(self.sandbox.clone()), + network: config.permissions.network.clone(), + shell_environment_policy: config.permissions.shell_environment_policy.clone(), + windows_sandbox_mode: config.permissions.windows_sandbox_mode, + macos_seatbelt_profile_extensions: config + .permissions + .macos_seatbelt_profile_extensions + .clone(), + }) + .is_ok() } pub(crate) fn actions(&self, config: &Config) -> Vec { let requires_full_access_confirmation = - self.id == "full-access" && !config.notices.hide_full_access_warning.unwrap_or(false); + matches!(self.sandbox, SandboxPolicy::DangerFullAccess) + && !config.notices.hide_full_access_warning.unwrap_or(false); if requires_full_access_confirmation { let preset = self.clone(); return vec![Box::new(move |tx: &AppEventSender| {