mirror of
https://github.com/openai/codex.git
synced 2026-11-19 23:05:23 +00:00
Add keybindings for cycling TUI permission modes (#39873)
## What changed - Add unbound `previous_permission_mode` and `next_permission_mode` actions to `tui.keymap.chat` so users can configure shortcuts that cycle through the available built-in permission modes. - Apply shortcut selections only to the active session, without writing them to `config.toml`, and report update failures in the transcript. - Exclude modes disallowed by managed requirements or platform safety checks, reject printable-key bindings, and ignore these bindings in project config. ## Testing - Cover mode cycling, unavailable modes, session-only updates, server errors, stale threads, keymap validation, and project-config sanitization. GitOrigin-RevId: c3cfdf1e732b7b7bfe8f53fcac19a37e593bbfab
This commit is contained in:
1 parent
44e95c857f
commit
2aaefa32b0
32 files changed
+708
-47
No files matched your search
@@ -211,6 +211,7 @@ impl ChatWidget {
|
||||
input_queue: InputQueueState::default(),
|
||||
safety_buffering_prompt: None,
|
||||
chat_keymap,
|
||||
permission_shortcut_pending: false,
|
||||
queued_message_edit_hint_binding,
|
||||
show_welcome_banner: is_first_run,
|
||||
startup_tooltip_override,
|
||||
|
||||
@@ -36,7 +36,7 @@ impl ChatWidget {
|
||||
return;
|
||||
}
|
||||
|
||||
if self.handle_reasoning_shortcut(key_event) {
|
||||
if self.handle_reasoning_shortcut(key_event) || self.handle_permission_shortcut(key_event) {
|
||||
self.bottom_pane.clear_quit_shortcut_hint();
|
||||
self.quit_shortcut_expires_at = None;
|
||||
self.quit_shortcut_key = None;
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
//! Session-scoped shortcuts for the ordinary built-in permission modes.
|
||||
|
||||
use super::*;
|
||||
|
||||
impl ChatWidget {
|
||||
pub(super) fn handle_permission_shortcut(&mut self, key_event: KeyEvent) -> bool {
|
||||
let forward = if self.chat_keymap.next_permission_mode.is_pressed(key_event) {
|
||||
true
|
||||
} else if self
|
||||
.chat_keymap
|
||||
.previous_permission_mode
|
||||
.is_pressed(key_event)
|
||||
{
|
||||
false
|
||||
} else {
|
||||
return false;
|
||||
};
|
||||
if !self.bottom_pane.no_modal_or_popup_active() {
|
||||
return false;
|
||||
}
|
||||
if self.permission_shortcut_pending {
|
||||
return true;
|
||||
}
|
||||
if self.blocks_direct_input {
|
||||
self.add_error_message(PARENT_OWNED_INPUT_MESSAGE.to_string());
|
||||
return true;
|
||||
}
|
||||
let Some(thread_id) = self.thread_id else {
|
||||
return true;
|
||||
};
|
||||
|
||||
let current_approval =
|
||||
AskForApproval::from(self.config.permissions.approval_policy.value());
|
||||
let active_profile = self.config.permissions.active_permission_profile();
|
||||
let mut choices = Vec::new();
|
||||
for preset in builtin_approval_presets() {
|
||||
if !matches!(preset.id, "read-only" | "auto") {
|
||||
continue;
|
||||
}
|
||||
for reviewer in [ApprovalsReviewer::User, ApprovalsReviewer::AutoReview] {
|
||||
if reviewer == ApprovalsReviewer::AutoReview
|
||||
&& (preset.id != "auto"
|
||||
|| !self.config.features.enabled(Feature::GuardianApproval))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let approval = AskForApproval::from(preset.approval);
|
||||
let requirements = self.config.config_layer_stack.requirements();
|
||||
if self
|
||||
.permission_mode_disabled_reason(&preset, approval)
|
||||
.is_some()
|
||||
|| requirements.approvals_reviewer.can_set(&reviewer).is_err()
|
||||
|| (requirements.auto_review_required_for_model(self.current_model())
|
||||
&& reviewer != ApprovalsReviewer::AutoReview)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
// These modes still need the explicit Windows setup/warning flow.
|
||||
#[cfg(target_os = "windows")]
|
||||
if preset.id == "auto"
|
||||
&& reviewer == ApprovalsReviewer::User
|
||||
&& (crate::windows_sandbox::level_from_config(&self.config)
|
||||
== WindowsSandboxLevel::Disabled
|
||||
|| self.world_writable_warning_details().is_some())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let is_current = current_approval == approval
|
||||
&& self.config.approvals_reviewer == reviewer
|
||||
&& active_profile.as_ref().map_or_else(
|
||||
|| {
|
||||
Self::preset_matches_current(
|
||||
current_approval,
|
||||
self.config.permissions.permission_profile(),
|
||||
self.config.cwd.as_path(),
|
||||
&preset,
|
||||
)
|
||||
},
|
||||
|active| active.id == preset.active_permission_profile.id,
|
||||
);
|
||||
let label = match (preset.id, reviewer) {
|
||||
("auto", ApprovalsReviewer::User) => ASK_FOR_APPROVAL_LABEL,
|
||||
("auto", ApprovalsReviewer::AutoReview) => APPROVE_FOR_ME_LABEL,
|
||||
_ => preset.label,
|
||||
};
|
||||
choices.push((
|
||||
is_current,
|
||||
PermissionProfileSelection {
|
||||
profile_id: preset.active_permission_profile.id.clone(),
|
||||
approval_policy: Some(approval),
|
||||
approvals_reviewer: Some(reviewer),
|
||||
display_label: label.to_string(),
|
||||
},
|
||||
));
|
||||
}
|
||||
}
|
||||
if !forward {
|
||||
choices.reverse();
|
||||
}
|
||||
let start = choices
|
||||
.iter()
|
||||
.position(|(current, _)| *current)
|
||||
.map_or(0, |index| index + 1);
|
||||
if let Some((_, selection)) = choices
|
||||
.iter()
|
||||
.cycle()
|
||||
.skip(start)
|
||||
.take(choices.len())
|
||||
.find(|(current, _)| !current)
|
||||
{
|
||||
self.permission_shortcut_pending = true;
|
||||
self.app_event_tx.send(AppEvent::ApplyPermissionShortcut {
|
||||
thread_id,
|
||||
selection: selection.clone(),
|
||||
});
|
||||
} else {
|
||||
self.add_info_message(
|
||||
"No other permission modes are available.".to_string(),
|
||||
/*hint*/ None,
|
||||
);
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
pub(crate) fn complete_permission_shortcut(&mut self, thread_id: ThreadId) {
|
||||
if self.thread_id == Some(thread_id) {
|
||||
self.permission_shortcut_pending = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,31 @@ pub(crate) fn cyber_model_approval_reviewer(config: &Config) -> Option<Approvals
|
||||
}
|
||||
|
||||
impl ChatWidget {
|
||||
pub(super) fn permission_mode_disabled_reason(
|
||||
&self,
|
||||
preset: &ApprovalPreset,
|
||||
approval_policy: AskForApproval,
|
||||
) -> Option<String> {
|
||||
self.config
|
||||
.permissions
|
||||
.approval_policy
|
||||
.can_set(&approval_policy.to_core())
|
||||
.and_then(|()| {
|
||||
self.config
|
||||
.permissions
|
||||
.can_set_permission_profile(&preset.permission_profile)
|
||||
})
|
||||
.err()
|
||||
.map(|err| err.to_string())
|
||||
.or_else(|| {
|
||||
(!self.config.is_permission_profile_allowed(
|
||||
&preset.active_permission_profile.id,
|
||||
&preset.permission_profile,
|
||||
))
|
||||
.then(|| "Disabled by requirements.".to_string())
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn open_permission_profiles_popup(&mut self) {
|
||||
let active_profile_id = self
|
||||
.config
|
||||
@@ -161,26 +186,7 @@ impl ChatWidget {
|
||||
/*return_to_permissions*/ true,
|
||||
),
|
||||
dismiss_on_select: true,
|
||||
disabled_reason: self
|
||||
.config
|
||||
.permissions
|
||||
.approval_policy
|
||||
.can_set(&approval_policy.to_core())
|
||||
.err()
|
||||
.map(|err| err.to_string())
|
||||
.or_else(|| {
|
||||
self.config
|
||||
.permissions
|
||||
.can_set_permission_profile(&preset.permission_profile)
|
||||
.err()
|
||||
.map(|err| err.to_string())
|
||||
})
|
||||
.or_else(|| {
|
||||
(!self
|
||||
.config
|
||||
.is_permission_profile_allowed(id, &preset.permission_profile))
|
||||
.then(|| "Disabled by requirements.".to_string())
|
||||
}),
|
||||
disabled_reason: self.permission_mode_disabled_reason(preset, approval_policy),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -245,6 +245,8 @@ mod history_replay;
|
||||
mod mcp_startup;
|
||||
#[path = "tests/misalignment_policy_tests.rs"]
|
||||
mod misalignment_policy;
|
||||
#[path = "tests/permission_shortcuts_tests.rs"]
|
||||
mod permission_shortcuts_tests;
|
||||
mod permissions;
|
||||
mod plan_mode;
|
||||
#[path = "tests/plugin_catalog_tests.rs"]
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
use super::permissions::requirements_stack;
|
||||
use super::*;
|
||||
use ApprovalsReviewer::AutoReview;
|
||||
use ApprovalsReviewer::User;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[tokio::test]
|
||||
async fn permission_shortcuts_cycle_builtin_modes() {
|
||||
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
|
||||
let thread_id = ThreadId::new();
|
||||
chat.thread_id = Some(thread_id);
|
||||
chat.set_feature_enabled(Feature::GuardianApproval, /*enabled*/ true);
|
||||
chat.chat_keymap.next_permission_mode = vec![crate::key_hint::plain(KeyCode::F(8))];
|
||||
chat.chat_keymap.previous_permission_mode = vec![crate::key_hint::plain(KeyCode::F(7))];
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
chat.config.notices.hide_world_writable_warning = Some(true);
|
||||
chat.set_windows_sandbox_mode(Some(WindowsSandboxModeToml::Unelevated));
|
||||
}
|
||||
for (current, reviewer, key, expected, next_reviewer) in [
|
||||
(":workspace", User, KeyCode::F(8), ":workspace", AutoReview),
|
||||
(":workspace", AutoReview, KeyCode::F(8), ":read-only", User),
|
||||
(":read-only", User, KeyCode::F(8), ":workspace", User),
|
||||
(":read-only", User, KeyCode::F(7), ":workspace", AutoReview),
|
||||
] {
|
||||
let profile = if current == ":read-only" {
|
||||
PermissionProfile::read_only()
|
||||
} else {
|
||||
PermissionProfile::workspace_write()
|
||||
};
|
||||
chat.config
|
||||
.permissions
|
||||
.set_permission_profile_from_session_snapshot(PermissionProfileSnapshot::active(
|
||||
profile,
|
||||
ActivePermissionProfile::new(current),
|
||||
))
|
||||
.expect("set current profile");
|
||||
chat.config.approvals_reviewer = reviewer;
|
||||
chat.handle_key_event(KeyEvent::from(key));
|
||||
chat.handle_key_event(KeyEvent::from(key));
|
||||
let AppEvent::ApplyPermissionShortcut {
|
||||
thread_id: target,
|
||||
selection,
|
||||
} = rx.try_recv().expect("permission selection")
|
||||
else {
|
||||
panic!("expected one typed permission selection");
|
||||
};
|
||||
assert_eq!(
|
||||
(
|
||||
target,
|
||||
selection.profile_id.as_str(),
|
||||
selection.approval_policy,
|
||||
selection.approvals_reviewer
|
||||
),
|
||||
(
|
||||
thread_id,
|
||||
expected,
|
||||
Some(AskForApproval::OnRequest),
|
||||
Some(next_reviewer)
|
||||
)
|
||||
);
|
||||
assert!(
|
||||
rx.try_recv().is_err(),
|
||||
"pending shortcut must not be duplicated"
|
||||
);
|
||||
chat.complete_permission_shortcut(thread_id);
|
||||
}
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
chat.set_windows_sandbox_mode(/*mode*/ None);
|
||||
chat.set_feature_enabled(Feature::WindowsSandbox, /*enabled*/ false);
|
||||
chat.set_feature_enabled(Feature::WindowsSandboxElevated, /*enabled*/ false);
|
||||
chat.config
|
||||
.permissions
|
||||
.set_permission_profile(PermissionProfile::read_only())
|
||||
.unwrap();
|
||||
chat.config.approvals_reviewer = User;
|
||||
chat.handle_key_event(KeyEvent::from(KeyCode::F(8)));
|
||||
assert!(matches!(
|
||||
rx.try_recv(),
|
||||
Ok(AppEvent::ApplyPermissionShortcut {
|
||||
selection: PermissionProfileSelection {
|
||||
approvals_reviewer: Some(AutoReview),
|
||||
..
|
||||
},
|
||||
..
|
||||
})
|
||||
));
|
||||
assert!(rx.try_recv().is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn permission_shortcuts_respect_managed_mode_requirements() {
|
||||
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
|
||||
chat.thread_id = Some(ThreadId::new());
|
||||
chat.set_feature_enabled(Feature::GuardianApproval, /*enabled*/ true);
|
||||
chat.config.approvals_reviewer = AutoReview;
|
||||
chat.chat_keymap.next_permission_mode = vec![crate::key_hint::plain(KeyCode::F(8))];
|
||||
chat.config
|
||||
.permissions
|
||||
.set_permission_profile_from_session_snapshot(PermissionProfileSnapshot::active(
|
||||
PermissionProfile::workspace_write(),
|
||||
ActivePermissionProfile::new(":workspace"),
|
||||
))
|
||||
.expect("set active profile");
|
||||
|
||||
for requirements in [
|
||||
codex_config::ConfigRequirementsToml {
|
||||
allowed_approvals_reviewers: Some(vec![AutoReview]),
|
||||
..Default::default()
|
||||
},
|
||||
codex_config::ConfigRequirementsToml {
|
||||
auto_review: Some(codex_config::AutoReviewRequirementsToml {
|
||||
required_on_models: Some(vec![chat.current_model().to_string()]),
|
||||
..Default::default()
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
] {
|
||||
chat.config.config_layer_stack = requirements_stack(requirements);
|
||||
chat.handle_key_event(KeyEvent::from(KeyCode::F(8)));
|
||||
let AppEvent::InsertHistoryCell(cell) = rx.try_recv().expect("unavailable-mode notice")
|
||||
else {
|
||||
panic!("must not submit a forbidden mode");
|
||||
};
|
||||
insta::assert_snapshot!(
|
||||
"permission_shortcut_no_alternative",
|
||||
lines_to_single_string(&cell.display_lines(/*width*/ 80))
|
||||
);
|
||||
assert!(rx.try_recv().is_err(), "must not submit a forbidden mode");
|
||||
}
|
||||
}
|
||||
@@ -67,7 +67,9 @@ fn windows_sandbox_requirements_stack(
|
||||
requirements_stack(requirements_toml)
|
||||
}
|
||||
|
||||
fn requirements_stack(requirements_toml: codex_config::ConfigRequirementsToml) -> ConfigLayerStack {
|
||||
pub(super) fn requirements_stack(
|
||||
requirements_toml: codex_config::ConfigRequirementsToml,
|
||||
) -> ConfigLayerStack {
|
||||
let mut requirements_with_sources = codex_config::ConfigRequirementsWithSources::default();
|
||||
requirements_with_sources
|
||||
.merge_unset_fields(RequirementSource::Unknown, requirements_toml.clone());
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
---
|
||||
source: tui/src/chatwidget/tests/permission_shortcuts_tests.rs
|
||||
expression: lines_to_single_string(&cell.display_lines(80))
|
||||
---
|
||||
• No other permission modes are available.
|
||||
Reference in new issue
Block a user