mirror of
https://github.com/openai/codex.git
synced 2026-11-18 23:02: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
@@ -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