mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Scope TUI approval requests to their threads (#39372)
## Why Approval and item IDs can collide across concurrent threads. Tracking pending app-server approvals by ID alone can therefore resolve, submit, or dismiss a request from the wrong thread. ## What changed - Key command execution, file change, and permissions approvals by both thread ID and approval ID. - Route approval responses through the originating thread and require the thread to match when processing resolution notifications or dismissing approval prompts. - Canonicalize parseable thread IDs so equivalent UUID representations still match. ## Testing - Cover colliding approval IDs across threads, mismatched resolution notifications, prompt dismissal, and response routing for primary and background threads. GitOrigin-RevId: c2d899a1e4b6a6413c78a0595a5311f5fabb9815
This commit is contained in:
1 parent
fde2156057
commit
8843960ba0
9 files changed
+358
-125
No files matched your search
@@ -50,12 +50,15 @@ pub(super) struct UnsupportedAppServerRequest {
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub(crate) enum ResolvedAppServerRequest {
|
||||
ExecApproval {
|
||||
thread_id: String,
|
||||
id: String,
|
||||
},
|
||||
FileChangeApproval {
|
||||
thread_id: String,
|
||||
id: String,
|
||||
},
|
||||
PermissionsApproval {
|
||||
thread_id: String,
|
||||
id: String,
|
||||
},
|
||||
UserInput {
|
||||
@@ -69,14 +72,20 @@ pub(crate) enum ResolvedAppServerRequest {
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub(super) struct PendingAppServerRequests {
|
||||
exec_approvals: HashMap<String, AppServerRequestId>,
|
||||
file_change_approvals: HashMap<String, AppServerRequestId>,
|
||||
permissions_approvals: HashMap<String, AppServerRequestId>,
|
||||
exec_approvals: HashMap<(String, String), AppServerRequestId>,
|
||||
file_change_approvals: HashMap<(String, String), AppServerRequestId>,
|
||||
permissions_approvals: HashMap<(String, String), AppServerRequestId>,
|
||||
user_inputs: HashMap<String, VecDeque<PendingUserInputRequest>>,
|
||||
mcp_requests: HashMap<McpRequestKey, AppServerRequestId>,
|
||||
}
|
||||
|
||||
impl PendingAppServerRequests {
|
||||
fn canonical_thread_id(thread_id: &str) -> String {
|
||||
codex_protocol::ThreadId::from_string(thread_id)
|
||||
.map(|thread_id| thread_id.to_string())
|
||||
.unwrap_or_else(|_| thread_id.to_string())
|
||||
}
|
||||
|
||||
pub(super) fn clear(&mut self) {
|
||||
self.exec_approvals.clear();
|
||||
self.file_change_approvals.clear();
|
||||
@@ -95,12 +104,20 @@ impl PendingAppServerRequests {
|
||||
.approval_id
|
||||
.clone()
|
||||
.unwrap_or_else(|| params.item_id.clone());
|
||||
self.exec_approvals.insert(approval_id, request_id.clone());
|
||||
self.exec_approvals.insert(
|
||||
(Self::canonical_thread_id(¶ms.thread_id), approval_id),
|
||||
request_id.clone(),
|
||||
);
|
||||
None
|
||||
}
|
||||
ServerRequest::FileChangeRequestApproval { request_id, params } => {
|
||||
self.file_change_approvals
|
||||
.insert(params.item_id.clone(), request_id.clone());
|
||||
self.file_change_approvals.insert(
|
||||
(
|
||||
Self::canonical_thread_id(¶ms.thread_id),
|
||||
params.item_id.clone(),
|
||||
),
|
||||
request_id.clone(),
|
||||
);
|
||||
None
|
||||
}
|
||||
ServerRequest::PermissionsRequestApproval { request_id, params } => {
|
||||
@@ -116,8 +133,13 @@ impl PendingAppServerRequests {
|
||||
message: format!("failed to localize requested filesystem paths: {err}"),
|
||||
});
|
||||
}
|
||||
self.permissions_approvals
|
||||
.insert(params.item_id.clone(), request_id.clone());
|
||||
self.permissions_approvals.insert(
|
||||
(
|
||||
Self::canonical_thread_id(¶ms.thread_id),
|
||||
params.item_id.clone(),
|
||||
),
|
||||
request_id.clone(),
|
||||
);
|
||||
None
|
||||
}
|
||||
ServerRequest::ToolRequestUserInput { request_id, params } => {
|
||||
@@ -178,16 +200,18 @@ impl PendingAppServerRequests {
|
||||
|
||||
pub(super) fn take_resolution<T>(
|
||||
&mut self,
|
||||
thread_id: &str,
|
||||
op: T,
|
||||
) -> Result<Option<AppServerRequestResolution>, String>
|
||||
where
|
||||
T: Into<AppCommand>,
|
||||
{
|
||||
let thread_id = Self::canonical_thread_id(thread_id);
|
||||
let op: AppCommand = op.into();
|
||||
let resolution = match &op {
|
||||
AppCommand::ExecApproval { id, decision, .. } => self
|
||||
.exec_approvals
|
||||
.remove(id)
|
||||
.remove(&(thread_id, id.clone()))
|
||||
.map(|request_id| {
|
||||
Ok::<AppServerRequestResolution, String>(AppServerRequestResolution {
|
||||
request_id,
|
||||
@@ -204,7 +228,7 @@ impl PendingAppServerRequests {
|
||||
.transpose()?,
|
||||
AppCommand::PatchApproval { id, decision } => self
|
||||
.file_change_approvals
|
||||
.remove(id)
|
||||
.remove(&(thread_id, id.clone()))
|
||||
.map(|request_id| {
|
||||
Ok::<AppServerRequestResolution, String>(AppServerRequestResolution {
|
||||
request_id,
|
||||
@@ -219,7 +243,7 @@ impl PendingAppServerRequests {
|
||||
.transpose()?,
|
||||
AppCommand::RequestPermissionsResponse { id, response } => self
|
||||
.permissions_approvals
|
||||
.remove(id)
|
||||
.remove(&(thread_id, id.clone()))
|
||||
.map(|request_id| {
|
||||
Ok::<AppServerRequestResolution, String>(AppServerRequestResolution {
|
||||
request_id,
|
||||
@@ -280,33 +304,38 @@ impl PendingAppServerRequests {
|
||||
|
||||
pub(super) fn resolve_notification(
|
||||
&mut self,
|
||||
thread_id: &str,
|
||||
request_id: &AppServerRequestId,
|
||||
) -> Option<ResolvedAppServerRequest> {
|
||||
if let Some(id) = self
|
||||
.exec_approvals
|
||||
.iter()
|
||||
.find_map(|(id, value)| (value == request_id).then(|| id.clone()))
|
||||
{
|
||||
self.exec_approvals.remove(&id);
|
||||
return Some(ResolvedAppServerRequest::ExecApproval { id });
|
||||
let thread_id = Self::canonical_thread_id(thread_id);
|
||||
if let Some(key) = self.exec_approvals.iter().find_map(|(key, value)| {
|
||||
(key.0 == thread_id && value == request_id).then(|| key.clone())
|
||||
}) {
|
||||
self.exec_approvals.remove(&key);
|
||||
return Some(ResolvedAppServerRequest::ExecApproval {
|
||||
thread_id: key.0,
|
||||
id: key.1,
|
||||
});
|
||||
}
|
||||
|
||||
if let Some(id) = self
|
||||
.file_change_approvals
|
||||
.iter()
|
||||
.find_map(|(id, value)| (value == request_id).then(|| id.clone()))
|
||||
{
|
||||
self.file_change_approvals.remove(&id);
|
||||
return Some(ResolvedAppServerRequest::FileChangeApproval { id });
|
||||
if let Some(key) = self.file_change_approvals.iter().find_map(|(key, value)| {
|
||||
(key.0 == thread_id && value == request_id).then(|| key.clone())
|
||||
}) {
|
||||
self.file_change_approvals.remove(&key);
|
||||
return Some(ResolvedAppServerRequest::FileChangeApproval {
|
||||
thread_id: key.0,
|
||||
id: key.1,
|
||||
});
|
||||
}
|
||||
|
||||
if let Some(id) = self
|
||||
.permissions_approvals
|
||||
.iter()
|
||||
.find_map(|(id, value)| (value == request_id).then(|| id.clone()))
|
||||
{
|
||||
self.permissions_approvals.remove(&id);
|
||||
return Some(ResolvedAppServerRequest::PermissionsApproval { id });
|
||||
if let Some(key) = self.permissions_approvals.iter().find_map(|(key, value)| {
|
||||
(key.0 == thread_id && value == request_id).then(|| key.clone())
|
||||
}) {
|
||||
self.permissions_approvals.remove(&key);
|
||||
return Some(ResolvedAppServerRequest::PermissionsApproval {
|
||||
thread_id: key.0,
|
||||
id: key.1,
|
||||
});
|
||||
}
|
||||
|
||||
if let Some(pending) = self.remove_user_input_request(request_id) {
|
||||
@@ -475,11 +504,14 @@ mod tests {
|
||||
assert_eq!(pending.note_server_request(&request), None);
|
||||
|
||||
let resolution = pending
|
||||
.take_resolution(&Op::ExecApproval {
|
||||
id: "approval-1".to_string(),
|
||||
turn_id: None,
|
||||
decision: CommandExecutionApprovalDecision::Accept,
|
||||
})
|
||||
.take_resolution(
|
||||
"thread-1",
|
||||
&Op::ExecApproval {
|
||||
id: "approval-1".to_string(),
|
||||
turn_id: None,
|
||||
decision: CommandExecutionApprovalDecision::Accept,
|
||||
},
|
||||
)
|
||||
.expect("resolution should serialize")
|
||||
.expect("request should be pending");
|
||||
|
||||
@@ -487,6 +519,83 @@ mod tests {
|
||||
assert_eq!(resolution.result, json!({ "decision": "accept" }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn colliding_approvals_resolve_only_on_their_own_thread() {
|
||||
let approvals = [
|
||||
(
|
||||
"item/commandExecution/requestApproval",
|
||||
Op::ExecApproval {
|
||||
id: "shared-id".to_string(),
|
||||
turn_id: None,
|
||||
decision: CommandExecutionApprovalDecision::Accept,
|
||||
},
|
||||
),
|
||||
(
|
||||
"item/fileChange/requestApproval",
|
||||
Op::PatchApproval {
|
||||
id: "shared-id".to_string(),
|
||||
decision: FileChangeApprovalDecision::Accept,
|
||||
},
|
||||
),
|
||||
(
|
||||
"item/permissions/requestApproval",
|
||||
Op::RequestPermissionsResponse {
|
||||
id: "shared-id".to_string(),
|
||||
response: codex_protocol::request_permissions::RequestPermissionsResponse {
|
||||
permissions: RequestPermissionProfile::default(),
|
||||
scope: codex_protocol::request_permissions::PermissionGrantScope::Turn,
|
||||
strict_auto_review: false,
|
||||
},
|
||||
},
|
||||
),
|
||||
];
|
||||
|
||||
for (method, op) in approvals {
|
||||
let mut pending = PendingAppServerRequests::default();
|
||||
let thread_ids = [
|
||||
codex_protocol::ThreadId::new().to_string(),
|
||||
codex_protocol::ThreadId::new().to_string(),
|
||||
];
|
||||
for (index, thread_id) in thread_ids.iter().enumerate() {
|
||||
let wire_thread_id = if index == 0 {
|
||||
thread_id.to_ascii_uppercase()
|
||||
} else {
|
||||
thread_id.replace('-', "")
|
||||
};
|
||||
let request: ServerRequest = serde_json::from_value(json!({
|
||||
"method": method,
|
||||
"id": index + 1,
|
||||
"params": {
|
||||
"threadId": wire_thread_id,
|
||||
"turnId": "turn-1",
|
||||
"itemId": "shared-id",
|
||||
"startedAtMs": 0,
|
||||
"cwd": if cfg!(windows) { r"C:\tmp" } else { "/tmp" },
|
||||
"permissions": {},
|
||||
},
|
||||
}))
|
||||
.expect("approval request should deserialize");
|
||||
assert_eq!(pending.note_server_request(&request), None);
|
||||
assert!(pending.contains_server_request(&request));
|
||||
}
|
||||
assert_eq!(
|
||||
pending.resolve_notification(&thread_ids[1], &AppServerRequestId::Integer(1)),
|
||||
None
|
||||
);
|
||||
|
||||
for (thread_id, request_id) in thread_ids.iter().zip(1..=2) {
|
||||
let resolution = pending
|
||||
.take_resolution(thread_id, &op)
|
||||
.expect("approval resolution should serialize")
|
||||
.expect("approval should remain pending on its own thread");
|
||||
assert_eq!(
|
||||
resolution.request_id,
|
||||
AppServerRequestId::Integer(request_id)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_permissions_with_paths_that_cannot_be_localized() {
|
||||
let mut pending = PendingAppServerRequests::default();
|
||||
@@ -587,22 +696,25 @@ mod tests {
|
||||
);
|
||||
|
||||
let permissions = pending
|
||||
.take_resolution(&Op::RequestPermissionsResponse {
|
||||
id: "perm-1".to_string(),
|
||||
response: codex_protocol::request_permissions::RequestPermissionsResponse {
|
||||
permissions: RequestPermissionProfile {
|
||||
network: Some(NetworkPermissions {
|
||||
enabled: Some(true),
|
||||
}),
|
||||
file_system: Some(FileSystemPermissions::from_read_write_roots(
|
||||
Some(vec![absolute_path(read_path)]),
|
||||
Some(vec![absolute_path(write_path)]),
|
||||
)),
|
||||
.take_resolution(
|
||||
"thread-1",
|
||||
&Op::RequestPermissionsResponse {
|
||||
id: "perm-1".to_string(),
|
||||
response: codex_protocol::request_permissions::RequestPermissionsResponse {
|
||||
permissions: RequestPermissionProfile {
|
||||
network: Some(NetworkPermissions {
|
||||
enabled: Some(true),
|
||||
}),
|
||||
file_system: Some(FileSystemPermissions::from_read_write_roots(
|
||||
Some(vec![absolute_path(read_path)]),
|
||||
Some(vec![absolute_path(write_path)]),
|
||||
)),
|
||||
},
|
||||
scope: codex_protocol::request_permissions::PermissionGrantScope::Session,
|
||||
strict_auto_review: false,
|
||||
},
|
||||
scope: codex_protocol::request_permissions::PermissionGrantScope::Session,
|
||||
strict_auto_review: false,
|
||||
},
|
||||
})
|
||||
)
|
||||
.expect("permissions response should serialize")
|
||||
.expect("permissions request should be pending");
|
||||
assert_eq!(permissions.request_id, AppServerRequestId::Integer(7));
|
||||
@@ -640,18 +752,21 @@ mod tests {
|
||||
);
|
||||
|
||||
let user_input = pending
|
||||
.take_resolution(&Op::UserInputAnswer {
|
||||
id: "turn-2".to_string(),
|
||||
response: ToolRequestUserInputResponse {
|
||||
answers: std::iter::once((
|
||||
"question".to_string(),
|
||||
ToolRequestUserInputAnswer {
|
||||
answers: vec!["yes".to_string()],
|
||||
},
|
||||
))
|
||||
.collect(),
|
||||
.take_resolution(
|
||||
"thread-1",
|
||||
&Op::UserInputAnswer {
|
||||
id: "turn-2".to_string(),
|
||||
response: ToolRequestUserInputResponse {
|
||||
answers: std::iter::once((
|
||||
"question".to_string(),
|
||||
ToolRequestUserInputAnswer {
|
||||
answers: vec!["yes".to_string()],
|
||||
},
|
||||
))
|
||||
.collect(),
|
||||
},
|
||||
},
|
||||
})
|
||||
)
|
||||
.expect("user input response should serialize")
|
||||
.expect("user input request should be pending");
|
||||
assert_eq!(user_input.request_id, AppServerRequestId::Integer(8));
|
||||
@@ -697,13 +812,16 @@ mod tests {
|
||||
);
|
||||
|
||||
let resolution = pending
|
||||
.take_resolution(&Op::ResolveElicitation {
|
||||
server_name: "example".to_string(),
|
||||
request_id: AppServerRequestId::Integer(12),
|
||||
decision: McpServerElicitationAction::Accept,
|
||||
content: Some(json!({ "answer": "yes" })),
|
||||
meta: Some(json!({ "source": "tui" })),
|
||||
})
|
||||
.take_resolution(
|
||||
"thread-1",
|
||||
&Op::ResolveElicitation {
|
||||
server_name: "example".to_string(),
|
||||
request_id: AppServerRequestId::Integer(12),
|
||||
decision: McpServerElicitationAction::Accept,
|
||||
content: Some(json!({ "answer": "yes" })),
|
||||
meta: Some(json!({ "source": "tui" })),
|
||||
},
|
||||
)
|
||||
.expect("elicitation response should serialize")
|
||||
.expect("elicitation request should be pending");
|
||||
|
||||
@@ -779,10 +897,13 @@ mod tests {
|
||||
);
|
||||
|
||||
let resolution = pending
|
||||
.take_resolution(&Op::PatchApproval {
|
||||
id: "patch-1".to_string(),
|
||||
decision: FileChangeApprovalDecision::Cancel,
|
||||
})
|
||||
.take_resolution(
|
||||
"thread-1",
|
||||
&Op::PatchApproval {
|
||||
id: "patch-1".to_string(),
|
||||
decision: FileChangeApprovalDecision::Cancel,
|
||||
},
|
||||
)
|
||||
.expect("resolution should serialize")
|
||||
.expect("request should be pending");
|
||||
|
||||
@@ -793,11 +914,12 @@ mod tests {
|
||||
#[test]
|
||||
fn resolve_notification_returns_resolved_exec_request() {
|
||||
let mut pending = PendingAppServerRequests::default();
|
||||
let thread_id = codex_protocol::ThreadId::new().to_string();
|
||||
assert_eq!(
|
||||
pending.note_server_request(&ServerRequest::CommandExecutionRequestApproval {
|
||||
request_id: AppServerRequestId::Integer(41),
|
||||
params: CommandExecutionRequestApprovalParams {
|
||||
thread_id: "thread-1".to_string(),
|
||||
thread_id: thread_id.to_ascii_uppercase(),
|
||||
turn_id: "turn-1".to_string(),
|
||||
item_id: "call-1".to_string(),
|
||||
started_at_ms: 0,
|
||||
@@ -818,13 +940,17 @@ mod tests {
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
pending.resolve_notification(&AppServerRequestId::Integer(41)),
|
||||
pending.resolve_notification(
|
||||
&thread_id.replace('-', ""),
|
||||
&AppServerRequestId::Integer(41)
|
||||
),
|
||||
Some(ResolvedAppServerRequest::ExecApproval {
|
||||
thread_id: thread_id.clone(),
|
||||
id: "approval-1".to_string(),
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
pending.resolve_notification(&AppServerRequestId::Integer(41)),
|
||||
pending.resolve_notification(&thread_id, &AppServerRequestId::Integer(41)),
|
||||
None
|
||||
);
|
||||
}
|
||||
@@ -855,7 +981,7 @@ mod tests {
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
pending.resolve_notification(&AppServerRequestId::Integer(12)),
|
||||
pending.resolve_notification("thread-1", &AppServerRequestId::Integer(12)),
|
||||
Some(ResolvedAppServerRequest::McpElicitation {
|
||||
server_name: "example".to_string(),
|
||||
request_id: AppServerRequestId::Integer(12),
|
||||
@@ -879,7 +1005,7 @@ mod tests {
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
pending.resolve_notification(&AppServerRequestId::Integer(8)),
|
||||
pending.resolve_notification("thread-1", &AppServerRequestId::Integer(8)),
|
||||
Some(ResolvedAppServerRequest::UserInput {
|
||||
call_id: "tool-1".to_string(),
|
||||
})
|
||||
@@ -907,17 +1033,23 @@ mod tests {
|
||||
answers: HashMap::new(),
|
||||
};
|
||||
let first_response = pending
|
||||
.take_resolution(&Op::UserInputAnswer {
|
||||
id: "turn-1".to_string(),
|
||||
response: response.clone(),
|
||||
})
|
||||
.take_resolution(
|
||||
"thread-1",
|
||||
&Op::UserInputAnswer {
|
||||
id: "turn-1".to_string(),
|
||||
response: response.clone(),
|
||||
},
|
||||
)
|
||||
.expect("user input response should serialize")
|
||||
.expect("first user input request should be pending");
|
||||
let second_response = pending
|
||||
.take_resolution(&Op::UserInputAnswer {
|
||||
id: "turn-1".to_string(),
|
||||
response,
|
||||
})
|
||||
.take_resolution(
|
||||
"thread-1",
|
||||
&Op::UserInputAnswer {
|
||||
id: "turn-1".to_string(),
|
||||
response,
|
||||
},
|
||||
)
|
||||
.expect("user input response should serialize")
|
||||
.expect("second user input request should be pending");
|
||||
|
||||
|
||||
Reference in new issue
Block a user