-
Notifications
You must be signed in to change notification settings - Fork 6.5k
whitelist command prefix integration in core and tui #7033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zhao-oai
wants to merge
23
commits into
pr7032
Choose a base branch
from
pr7033
base: pr7032
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+675
−109
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
548fcfc
Add approval allow-prefix flow in core and tui
zhao-oai aa4a09d
Add explicit prefix-approval decision and wire it through execpolicy/…
zhao-oai 92fbfaf
update doc
zhao-oai 9370f3b
mutating in memory policy instead of reloading
zhao-oai 3202f62
using RW locks
zhao-oai 8f139be
clippy
zhao-oai 3ba2ef3
refactor: adding allow_prefix into ApprovedAllowPrefix
zhao-oai 79fb80e
fmt
zhao-oai 62dea7c
do not send allow_prefix if execpolicy is disabled
zhao-oai d0ad931
moving args around
zhao-oai 79183f8
cleanup exec_policy getters
zhao-oai 4ad58b9
undo diff
zhao-oai 040bc89
fixing rw lock bug causing tui to hang
zhao-oai 82e3fbb
updating phrasing
zhao-oai 73f586b
integration test
zhao-oai 6a1afea
.
zhao-oai 2790b64
fix compile
zhao-oai 82778f6
fix flaky test
zhao-oai 4f8ebeb
fix compile error
zhao-oai 54fee01
running test with single thread
zhao-oai e6d3d2b
fixup allow_prefix_if_applicable
zhao-oai 62b8f73
fix formatting
zhao-oai ec037e6
fix approvals test
zhao-oai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,6 +50,7 @@ use mcp_types::RequestId; | |
| use serde_json; | ||
| use serde_json::Value; | ||
| use tokio::sync::Mutex; | ||
| use tokio::sync::OwnedRwLockReadGuard; | ||
| use tokio::sync::RwLock; | ||
| use tokio::sync::oneshot; | ||
| use tokio_util::sync::CancellationToken; | ||
|
|
@@ -71,6 +72,7 @@ use crate::error::CodexErr; | |
| use crate::error::Result as CodexResult; | ||
| #[cfg(test)] | ||
| use crate::exec::StreamOutput; | ||
| use crate::exec_policy::ExecPolicyUpdateError; | ||
| use crate::mcp::auth::compute_auth_statuses; | ||
| use crate::mcp_connection_manager::McpConnectionManager; | ||
| use crate::model_family::find_family_for_model; | ||
|
|
@@ -288,7 +290,7 @@ pub(crate) struct TurnContext { | |
| pub(crate) final_output_json_schema: Option<Value>, | ||
| pub(crate) codex_linux_sandbox_exe: Option<PathBuf>, | ||
| pub(crate) tool_call_gate: Arc<ReadinessFlag>, | ||
| pub(crate) exec_policy: Arc<ExecPolicy>, | ||
| pub(crate) exec_policy: Arc<RwLock<ExecPolicy>>, | ||
| pub(crate) truncation_policy: TruncationPolicy, | ||
| } | ||
|
|
||
|
|
@@ -346,7 +348,7 @@ pub(crate) struct SessionConfiguration { | |
| /// Set of feature flags for this session | ||
| features: Features, | ||
| /// Execpolicy policy, applied only when enabled by feature flag. | ||
| exec_policy: Arc<ExecPolicy>, | ||
| exec_policy: Arc<RwLock<ExecPolicy>>, | ||
|
|
||
| // TODO(pakrym): Remove config from here | ||
| original_config_do_not_use: Arc<Config>, | ||
|
|
@@ -861,11 +863,52 @@ impl Session { | |
| .await | ||
| } | ||
|
|
||
| pub(crate) async fn persist_command_allow_prefix( | ||
| &self, | ||
| prefix: &[String], | ||
| ) -> Result<(), ExecPolicyUpdateError> { | ||
| let (features, codex_home, current_policy) = { | ||
| let state = self.state.lock().await; | ||
| ( | ||
| state.session_configuration.features.clone(), | ||
| state | ||
| .session_configuration | ||
| .original_config_do_not_use | ||
| .codex_home | ||
| .clone(), | ||
| state.session_configuration.exec_policy.clone(), | ||
| ) | ||
| }; | ||
|
|
||
| if !features.enabled(Feature::ExecPolicy) { | ||
| error!("attempted to append execpolicy rule while execpolicy feature is disabled"); | ||
| return Err(ExecPolicyUpdateError::FeatureDisabled); | ||
| } | ||
|
|
||
| crate::exec_policy::append_allow_prefix_rule_and_update( | ||
| &codex_home, | ||
| ¤t_policy, | ||
| prefix, | ||
| ) | ||
| .await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| pub(crate) async fn current_exec_policy(&self) -> OwnedRwLockReadGuard<ExecPolicy> { | ||
| let exec_policy = { | ||
| let state = self.state.lock().await; | ||
| state.session_configuration.exec_policy.clone() | ||
| }; | ||
| exec_policy.read_owned().await | ||
| } | ||
|
|
||
| /// Emit an exec approval request event and await the user's decision. | ||
| /// | ||
| /// The request is keyed by `sub_id`/`call_id` so matching responses are delivered | ||
| /// to the correct in-flight turn. If the task is aborted, this returns the | ||
| /// default `ReviewDecision` (`Denied`). | ||
| #[allow(clippy::too_many_arguments)] | ||
| pub async fn request_command_approval( | ||
| &self, | ||
| turn_context: &TurnContext, | ||
|
|
@@ -874,6 +917,7 @@ impl Session { | |
| cwd: PathBuf, | ||
| reason: Option<String>, | ||
| risk: Option<SandboxCommandAssessment>, | ||
| allow_prefix: Option<Vec<String>>, | ||
| ) -> ReviewDecision { | ||
| let sub_id = turn_context.sub_id.clone(); | ||
| // Add the tx_approve callback to the map before sending the request. | ||
|
|
@@ -901,6 +945,7 @@ impl Session { | |
| cwd, | ||
| reason, | ||
| risk, | ||
| allow_prefix, | ||
| parsed_cmd, | ||
| }); | ||
| self.send_event(turn_context, event).await; | ||
|
|
@@ -1058,6 +1103,15 @@ impl Session { | |
| .enabled(feature) | ||
| } | ||
|
|
||
| pub(crate) async fn features(&self) -> Features { | ||
| self.state | ||
| .lock() | ||
| .await | ||
| .session_configuration | ||
| .features | ||
| .clone() | ||
| } | ||
|
|
||
| async fn send_raw_response_items(&self, turn_context: &TurnContext, items: &[ResponseItem]) { | ||
| for item in items { | ||
| self.send_event( | ||
|
|
@@ -1490,6 +1544,7 @@ mod handlers { | |
| use codex_protocol::protocol::ReviewDecision; | ||
| use codex_protocol::protocol::ReviewRequest; | ||
| use codex_protocol::protocol::TurnAbortReason; | ||
| use codex_protocol::protocol::WarningEvent; | ||
|
|
||
| use codex_protocol::user_input::UserInput; | ||
| use codex_rmcp_client::ElicitationAction; | ||
|
|
@@ -1605,6 +1660,18 @@ mod handlers { | |
| } | ||
|
|
||
| pub async fn exec_approval(sess: &Arc<Session>, id: String, decision: ReviewDecision) { | ||
| if let ReviewDecision::ApprovedAllowPrefix { allow_prefix } = &decision | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a docstring to this function because I think it's contract is not obvious now. |
||
| && let Err(err) = sess.persist_command_allow_prefix(allow_prefix).await | ||
| { | ||
| let message = format!("Failed to update execpolicy allow list: {err}"); | ||
| tracing::warn!("{message}"); | ||
| let warning = EventMsg::Warning(WarningEvent { message }); | ||
| sess.send_event_raw(Event { | ||
| id: id.clone(), | ||
| msg: warning, | ||
| }) | ||
| .await; | ||
| } | ||
| match decision { | ||
| ReviewDecision::Abort => { | ||
| sess.interrupt_task().await; | ||
|
|
@@ -2669,7 +2736,7 @@ mod tests { | |
| cwd: config.cwd.clone(), | ||
| original_config_do_not_use: Arc::clone(&config), | ||
| features: Features::default(), | ||
| exec_policy: Arc::new(ExecPolicy::empty()), | ||
| exec_policy: Arc::new(RwLock::new(ExecPolicy::empty())), | ||
| session_source: SessionSource::Exec, | ||
| }; | ||
|
|
||
|
|
@@ -2747,7 +2814,7 @@ mod tests { | |
| cwd: config.cwd.clone(), | ||
| original_config_do_not_use: Arc::clone(&config), | ||
| features: Features::default(), | ||
| exec_policy: Arc::new(ExecPolicy::empty()), | ||
| exec_policy: Arc::new(RwLock::new(ExecPolicy::empty())), | ||
| session_source: SessionSource::Exec, | ||
| }; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this feels slightly undesirable since
SessionConfigurationis already generally wrapped in a mutex, but maybe this is OK, I'm not sure yet...