mirror of
https://github.com/openai/codex.git
synced 2026-11-25 23:30:49 +00:00
guardian patch and test update
This commit is contained in:
@@ -3438,12 +3438,23 @@ impl Session {
|
||||
});
|
||||
drop(ts);
|
||||
drop(active);
|
||||
self.record_direct_approval_outcome(
|
||||
&pending_approval.call_id,
|
||||
decision,
|
||||
pending_approval.approval_source,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
pub async fn record_direct_approval_outcome(
|
||||
&self,
|
||||
call_id: &str,
|
||||
decision: &ReviewDecision,
|
||||
approval_source: ApprovalSourceMetadata,
|
||||
) {
|
||||
self.record_call_approval_outcome(
|
||||
pending_approval.call_id,
|
||||
ApprovalOutcomeMetadata::reviewed_with_source(
|
||||
decision,
|
||||
pending_approval.approval_source,
|
||||
),
|
||||
call_id.to_string(),
|
||||
ApprovalOutcomeMetadata::reviewed_with_source(decision, approval_source),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
@@ -4584,6 +4584,71 @@ async fn tool_call_metadata_stamps_non_escalated_false_when_feature_enabled() {
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn tool_call_metadata_stamps_guardian_direct_review_when_feature_enabled() {
|
||||
let (mut sess, tc, rx) = make_session_and_context_with_rx().await;
|
||||
let expected_sandbox_policy = sandbox_policy_to_metadata(tc.sandbox_policy.get());
|
||||
Arc::get_mut(&mut sess)
|
||||
.expect("session should be uniquely owned in this test")
|
||||
.features
|
||||
.enable(crate::features::Feature::ItemMetadata)
|
||||
.expect("feature flag should be enabled for this test");
|
||||
|
||||
sess.spawn_task(
|
||||
Arc::clone(&tc),
|
||||
vec![UserInput::Text {
|
||||
text: "start".to_string(),
|
||||
text_elements: Vec::new(),
|
||||
}],
|
||||
NeverEndingTask {
|
||||
kind: TaskKind::Regular,
|
||||
listen_to_cancellation_token: false,
|
||||
},
|
||||
)
|
||||
.await;
|
||||
while rx.try_recv().is_ok() {}
|
||||
|
||||
sess.record_direct_approval_outcome(
|
||||
"call-guardian-runtime-1",
|
||||
&ReviewDecision::Denied,
|
||||
codex_protocol::models::ApprovalSourceMetadata::Guardian,
|
||||
)
|
||||
.await;
|
||||
sess.record_response_item_and_emit_turn_item(
|
||||
tc.as_ref(),
|
||||
ResponseItem::FunctionCall {
|
||||
id: None,
|
||||
name: "shell".to_string(),
|
||||
namespace: None,
|
||||
arguments: "{}".to_string(),
|
||||
call_id: "call-guardian-runtime-1".to_string(),
|
||||
metadata: None,
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
let event = tokio::time::timeout(std::time::Duration::from_secs(2), rx.recv())
|
||||
.await
|
||||
.expect("expected raw response item event")
|
||||
.expect("channel open");
|
||||
assert!(matches!(
|
||||
event.msg,
|
||||
EventMsg::RawResponseItem(ref ev)
|
||||
if matches!(
|
||||
&ev.item,
|
||||
ResponseItem::FunctionCall {
|
||||
metadata: Some(metadata),
|
||||
..
|
||||
} if metadata.is_tool_call_escalated == Some(true)
|
||||
&& metadata.review_decision
|
||||
== Some(codex_protocol::models::ReviewDecisionMetadata::Denied)
|
||||
&& metadata.approval_source
|
||||
== Some(codex_protocol::models::ApprovalSourceMetadata::Guardian)
|
||||
&& metadata.sandbox_policy == Some(expected_sandbox_policy)
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn tool_call_metadata_stamps_policy_source_without_review_decision_when_feature_enabled() {
|
||||
let (sess, tc, rx, expected_sandbox_policy) = setup_tool_call_metadata_runtime_test().await;
|
||||
@@ -4698,9 +4763,10 @@ async fn tool_call_metadata_can_be_restamped_after_approval_outcome() {
|
||||
},
|
||||
)
|
||||
.await;
|
||||
sess.record_call_approval_outcome(
|
||||
"call-restamp-1".to_string(),
|
||||
ApprovalOutcomeMetadata::reviewed(&ReviewDecision::Denied),
|
||||
sess.record_direct_approval_outcome(
|
||||
"call-restamp-1",
|
||||
&ReviewDecision::Denied,
|
||||
codex_protocol::models::ApprovalSourceMetadata::User,
|
||||
)
|
||||
.await;
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ use crate::protocol::McpToolCallBeginEvent;
|
||||
use crate::protocol::McpToolCallEndEvent;
|
||||
use crate::state_db;
|
||||
use codex_protocol::mcp::CallToolResult;
|
||||
use codex_protocol::models::ApprovalSourceMetadata;
|
||||
use codex_protocol::openai_models::InputModality;
|
||||
use codex_protocol::protocol::AskForApproval;
|
||||
use codex_protocol::protocol::ReviewDecision;
|
||||
@@ -532,6 +533,8 @@ async fn maybe_request_mcp_tool_approval(
|
||||
monitor_reason.clone(),
|
||||
)
|
||||
.await;
|
||||
sess.record_direct_approval_outcome(call_id, &decision, ApprovalSourceMetadata::Guardian)
|
||||
.await;
|
||||
let decision = mcp_tool_approval_decision_from_guardian(decision);
|
||||
apply_mcp_tool_approval_decision(
|
||||
sess,
|
||||
|
||||
@@ -23,6 +23,7 @@ use crate::tools::sandboxing::ToolRuntime;
|
||||
use crate::tools::sandboxing::with_cached_approval;
|
||||
use codex_apply_patch::ApplyPatchAction;
|
||||
use codex_apply_patch::CODEX_CORE_APPLY_PATCH_ARG1;
|
||||
use codex_protocol::models::ApprovalSourceMetadata;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_protocol::protocol::AskForApproval;
|
||||
use codex_protocol::protocol::FileChange;
|
||||
@@ -140,7 +141,15 @@ impl Approvable<ApplyPatchRequest> for ApplyPatchRuntime {
|
||||
Box::pin(async move {
|
||||
if routes_approval_to_guardian(turn) {
|
||||
let action = ApplyPatchRuntime::build_guardian_review_request(req, ctx.call_id);
|
||||
return review_approval_request(session, turn, action, retry_reason).await;
|
||||
let decision = review_approval_request(session, turn, action, retry_reason).await;
|
||||
session
|
||||
.record_direct_approval_outcome(
|
||||
&call_id,
|
||||
&decision,
|
||||
ApprovalSourceMetadata::Guardian,
|
||||
)
|
||||
.await;
|
||||
return decision;
|
||||
}
|
||||
if req.permissions_preapproved && retry_reason.is_none() {
|
||||
return ReviewDecision::Approved;
|
||||
|
||||
@@ -35,6 +35,7 @@ use crate::tools::sandboxing::ToolRuntime;
|
||||
use crate::tools::sandboxing::sandbox_override_for_first_attempt;
|
||||
use crate::tools::sandboxing::with_cached_approval;
|
||||
use codex_network_proxy::NetworkProxy;
|
||||
use codex_protocol::models::ApprovalSourceMetadata;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_protocol::protocol::ReviewDecision;
|
||||
use futures::future::BoxFuture;
|
||||
@@ -153,11 +154,11 @@ impl Approvable<ShellRequest> for ShellRuntime {
|
||||
let call_id = ctx.call_id.to_string();
|
||||
Box::pin(async move {
|
||||
if routes_approval_to_guardian(turn) {
|
||||
return review_approval_request(
|
||||
let decision = review_approval_request(
|
||||
session,
|
||||
turn,
|
||||
GuardianApprovalRequest::Shell {
|
||||
id: call_id,
|
||||
id: call_id.clone(),
|
||||
command,
|
||||
cwd,
|
||||
sandbox_permissions: req.sandbox_permissions,
|
||||
@@ -167,6 +168,14 @@ impl Approvable<ShellRequest> for ShellRuntime {
|
||||
retry_reason,
|
||||
)
|
||||
.await;
|
||||
session
|
||||
.record_direct_approval_outcome(
|
||||
&call_id,
|
||||
&decision,
|
||||
ApprovalSourceMetadata::Guardian,
|
||||
)
|
||||
.await;
|
||||
return decision;
|
||||
}
|
||||
with_cached_approval(&session.services, "shell", keys, move || async move {
|
||||
let available_decisions = None;
|
||||
|
||||
@@ -25,6 +25,7 @@ use codex_execpolicy::MatchOptions;
|
||||
use codex_execpolicy::Policy;
|
||||
use codex_execpolicy::RuleMatch;
|
||||
use codex_protocol::config_types::WindowsSandboxLevel;
|
||||
use codex_protocol::models::ApprovalSourceMetadata;
|
||||
use codex_protocol::models::MacOsSeatbeltProfileExtensions;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_protocol::permissions::FileSystemSandboxPolicy;
|
||||
@@ -424,7 +425,7 @@ impl CoreShellActionProvider {
|
||||
Ok(stopwatch
|
||||
.pause_for(async move {
|
||||
if routes_approval_to_guardian(&turn) {
|
||||
return review_approval_request(
|
||||
let decision = review_approval_request(
|
||||
&session,
|
||||
&turn,
|
||||
GuardianApprovalRequest::Execve {
|
||||
@@ -438,6 +439,14 @@ impl CoreShellActionProvider {
|
||||
/*retry_reason*/ None,
|
||||
)
|
||||
.await;
|
||||
session
|
||||
.record_direct_approval_outcome(
|
||||
&call_id,
|
||||
&decision,
|
||||
ApprovalSourceMetadata::Guardian,
|
||||
)
|
||||
.await;
|
||||
return decision;
|
||||
}
|
||||
let available_decisions = vec![
|
||||
Some(ReviewDecision::Approved),
|
||||
|
||||
@@ -38,6 +38,7 @@ use crate::unified_exec::UnifiedExecError;
|
||||
use crate::unified_exec::UnifiedExecProcess;
|
||||
use crate::unified_exec::UnifiedExecProcessManager;
|
||||
use codex_network_proxy::NetworkProxy;
|
||||
use codex_protocol::models::ApprovalSourceMetadata;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_protocol::protocol::ReviewDecision;
|
||||
use futures::future::BoxFuture;
|
||||
@@ -121,11 +122,11 @@ impl Approvable<UnifiedExecRequest> for UnifiedExecRuntime<'_> {
|
||||
let reason = retry_reason.clone().or_else(|| req.justification.clone());
|
||||
Box::pin(async move {
|
||||
if routes_approval_to_guardian(turn) {
|
||||
return review_approval_request(
|
||||
let decision = review_approval_request(
|
||||
session,
|
||||
turn,
|
||||
GuardianApprovalRequest::ExecCommand {
|
||||
id: call_id,
|
||||
id: call_id.clone(),
|
||||
command,
|
||||
cwd,
|
||||
sandbox_permissions: req.sandbox_permissions,
|
||||
@@ -136,6 +137,14 @@ impl Approvable<UnifiedExecRequest> for UnifiedExecRuntime<'_> {
|
||||
retry_reason,
|
||||
)
|
||||
.await;
|
||||
session
|
||||
.record_direct_approval_outcome(
|
||||
&call_id,
|
||||
&decision,
|
||||
ApprovalSourceMetadata::Guardian,
|
||||
)
|
||||
.await;
|
||||
return decision;
|
||||
}
|
||||
with_cached_approval(&session.services, "unified_exec", keys, || async move {
|
||||
let available_decisions = None;
|
||||
|
||||
Reference in New Issue
Block a user