mirror of
https://github.com/openai/codex.git
synced 2026-11-18 22:20:48 +00:00
Represent terminal input in approval reviews (#40528)
## What changed - Add a `writeStdin` Guardian action carrying the approval, process, input, and working-directory details. - Distinguish `command` and `writeStdin` execution approval requests while defaulting missing kinds to `command` for compatibility with older events. - Render terminal-input review progress, denials, timeouts, and retry actions in the TUI, and record the action separately in analytics. - Keep terminal-input reviews as child approvals so they do not change the parent command item's lifecycle or reconstructed history. ## Testing - Cover Guardian serialization, app-server protocol conversion, command history reconstruction, analytics classification, and TUI review flows for terminal input. GitOrigin-RevId: b083d5e2963bdd23b928644ec03c7eb68ebf2950
This commit is contained in:
@@ -4468,6 +4468,7 @@ mod tests {
|
||||
#[test]
|
||||
fn command_execution_request_approval_additional_permissions_is_marked_experimental() {
|
||||
let params = v2::CommandExecutionRequestApprovalParams {
|
||||
kind: Default::default(),
|
||||
thread_id: "thr_123".to_string(),
|
||||
turn_id: "turn_123".to_string(),
|
||||
item_id: "call_123".to_string(),
|
||||
|
||||
@@ -185,6 +185,7 @@ fn command_actions_for_path_uri(parsed_cmd: &[ParsedCommand], cwd: &PathUri) ->
|
||||
///
|
||||
/// Currently this only synthesizes [`ThreadItem::CommandExecution`] for
|
||||
/// [`GuardianAssessmentAction::Command`] and [`GuardianAssessmentAction::Execve`].
|
||||
/// Stdin reviews are child approvals and must not change the parent item lifecycle.
|
||||
pub fn build_item_from_guardian_event(
|
||||
assessment: &GuardianAssessmentEvent,
|
||||
status: CommandExecutionStatus,
|
||||
@@ -249,7 +250,8 @@ pub fn build_item_from_guardian_event(
|
||||
duration_ms: None,
|
||||
})
|
||||
}
|
||||
GuardianAssessmentAction::ApplyPatch { .. }
|
||||
GuardianAssessmentAction::WriteStdin { .. }
|
||||
| GuardianAssessmentAction::ApplyPatch { .. }
|
||||
| GuardianAssessmentAction::NetworkAccess { .. }
|
||||
| GuardianAssessmentAction::McpToolCall { .. }
|
||||
| GuardianAssessmentAction::RequestPermissions { .. } => None,
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
use super::*;
|
||||
use crate::protocol::thread_history::build_turns_from_rollout_items;
|
||||
use codex_protocol::protocol::EventMsg;
|
||||
use codex_protocol::protocol::ExecCommandSource;
|
||||
use codex_protocol::protocol::GuardianAssessmentStatus;
|
||||
use codex_protocol::protocol::TurnStartedEvent;
|
||||
use codex_rollout::RolloutItem;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::json;
|
||||
|
||||
@@ -83,3 +89,87 @@ fn read_command_actions_preserve_native_and_foreign_paths() {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn guardian_stdin_reviews_preserve_parent_command_history() {
|
||||
let cwd = PathUri::parse("file:///home/alice/repo").expect("valid cwd URI");
|
||||
let begin = ExecCommandBeginEvent {
|
||||
call_id: "terminal-command".into(),
|
||||
plugin_id: None,
|
||||
script_path: None,
|
||||
process_id: Some("42".into()),
|
||||
turn_id: "turn-1".into(),
|
||||
started_at_ms: 1_000,
|
||||
command: vec!["cat".into()],
|
||||
cwd: cwd.clone(),
|
||||
parsed_cmd: vec![ParsedCommand::Unknown { cmd: "cat".into() }],
|
||||
source: ExecCommandSource::UnifiedExecStartup,
|
||||
interaction_input: None,
|
||||
};
|
||||
let start_turn = |turn_id: &str| {
|
||||
RolloutItem::EventMsg(EventMsg::TurnStarted(TurnStartedEvent {
|
||||
turn_id: turn_id.into(),
|
||||
trace_id: None,
|
||||
started_at: None,
|
||||
model_context_window: None,
|
||||
collaboration_mode_kind: Default::default(),
|
||||
}))
|
||||
};
|
||||
let mut items = vec![
|
||||
start_turn("turn-1"),
|
||||
RolloutItem::EventMsg(EventMsg::ExecCommandBegin(begin.clone())),
|
||||
start_turn("turn-2"),
|
||||
];
|
||||
let expected = build_turns_from_rollout_items(&items);
|
||||
assert_eq!(expected.len(), 2);
|
||||
assert_eq!(
|
||||
expected[0].items,
|
||||
vec![build_command_execution_begin_item(&begin)],
|
||||
);
|
||||
assert!(expected[1].items.is_empty());
|
||||
|
||||
let assessment = GuardianAssessmentEvent {
|
||||
id: "review-stdin".into(),
|
||||
target_item_id: Some("terminal-command".into()),
|
||||
plugin_id: None,
|
||||
script_path: None,
|
||||
turn_id: "turn-2".into(),
|
||||
started_at_ms: 2_000,
|
||||
completed_at_ms: None,
|
||||
status: GuardianAssessmentStatus::InProgress,
|
||||
risk_level: None,
|
||||
user_authorization: None,
|
||||
rationale: None,
|
||||
decision_source: None,
|
||||
action: GuardianAssessmentAction::WriteStdin {
|
||||
approval_id: "stdin-approval".into(),
|
||||
process_id: "42".into(),
|
||||
stdin: "yes\n".into(),
|
||||
cwd,
|
||||
},
|
||||
};
|
||||
|
||||
for turn_id in ["turn-1", "turn-2"] {
|
||||
for status in [
|
||||
GuardianAssessmentStatus::InProgress,
|
||||
GuardianAssessmentStatus::Approved,
|
||||
GuardianAssessmentStatus::Denied,
|
||||
GuardianAssessmentStatus::TimedOut,
|
||||
GuardianAssessmentStatus::Aborted,
|
||||
] {
|
||||
items.push(RolloutItem::EventMsg(EventMsg::GuardianAssessment(
|
||||
GuardianAssessmentEvent {
|
||||
turn_id: turn_id.into(),
|
||||
status,
|
||||
..assessment.clone()
|
||||
},
|
||||
)));
|
||||
assert_eq!(
|
||||
build_turns_from_rollout_items(&items),
|
||||
expected,
|
||||
"stdin review {status:?} in {turn_id} must preserve command history",
|
||||
);
|
||||
items.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ pub use codex_extension_items::image_generation::ImageGenerationItem;
|
||||
pub use codex_extension_items::sleep::SleepItem;
|
||||
pub use codex_extension_items::web_search::WebSearchAction;
|
||||
pub use codex_extension_items::web_search::WebSearchItem;
|
||||
use codex_protocol::approvals::ExecApprovalKind as CoreExecApprovalKind;
|
||||
use codex_protocol::approvals::GuardianAssessmentAction as CoreGuardianAssessmentAction;
|
||||
use codex_protocol::approvals::GuardianAssessmentDecisionSource as CoreGuardianAssessmentDecisionSource;
|
||||
use codex_protocol::approvals::GuardianCommandSource as CoreGuardianCommandSource;
|
||||
@@ -635,6 +636,15 @@ pub enum GuardianApprovalReviewAction {
|
||||
argv: Vec<String>,
|
||||
cwd: AbsolutePathBuf,
|
||||
},
|
||||
/// A child approval for input to an existing command execution item.
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[ts(rename_all = "camelCase")]
|
||||
WriteStdin {
|
||||
approval_id: String,
|
||||
process_id: String,
|
||||
stdin: String,
|
||||
cwd: LegacyAppPathString,
|
||||
},
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[ts(rename_all = "camelCase")]
|
||||
ApplyPatch {
|
||||
@@ -689,6 +699,17 @@ impl From<CoreGuardianAssessmentAction> for GuardianApprovalReviewAction {
|
||||
argv,
|
||||
cwd,
|
||||
},
|
||||
CoreGuardianAssessmentAction::WriteStdin {
|
||||
approval_id,
|
||||
process_id,
|
||||
stdin,
|
||||
cwd,
|
||||
} => Self::WriteStdin {
|
||||
approval_id,
|
||||
process_id,
|
||||
stdin,
|
||||
cwd: cwd.into(),
|
||||
},
|
||||
CoreGuardianAssessmentAction::ApplyPatch { cwd, files } => {
|
||||
Self::ApplyPatch { cwd, files }
|
||||
}
|
||||
@@ -752,6 +773,19 @@ impl TryFrom<GuardianApprovalReviewAction> for CoreGuardianAssessmentAction {
|
||||
argv,
|
||||
cwd,
|
||||
},
|
||||
GuardianApprovalReviewAction::WriteStdin {
|
||||
approval_id,
|
||||
process_id,
|
||||
stdin,
|
||||
cwd,
|
||||
} => Self::WriteStdin {
|
||||
approval_id,
|
||||
process_id,
|
||||
stdin,
|
||||
cwd: cwd
|
||||
.try_into()
|
||||
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?,
|
||||
},
|
||||
GuardianApprovalReviewAction::ApplyPatch { cwd, files } => {
|
||||
Self::ApplyPatch { cwd, files }
|
||||
}
|
||||
@@ -1280,6 +1314,8 @@ pub struct ItemGuardianApprovalReviewStartedNotification {
|
||||
/// In most cases, one review maps to one target item. The exceptions are
|
||||
/// - execve reviews, where a single command may contain multiple execve
|
||||
/// calls to review (only possible when using the shell_zsh_fork feature)
|
||||
/// - stdin reviews, which refer to the existing parent command item and
|
||||
/// have a separate approval ID in the action payload
|
||||
/// - network policy reviews, where there is no target item
|
||||
///
|
||||
/// A network call is triggered by a CommandExecution item, so having a
|
||||
@@ -1312,6 +1348,8 @@ pub struct ItemGuardianApprovalReviewCompletedNotification {
|
||||
/// In most cases, one review maps to one target item. The exceptions are
|
||||
/// - execve reviews, where a single command may contain multiple execve
|
||||
/// calls to review (only possible when using the shell_zsh_fork feature)
|
||||
/// - stdin reviews, which refer to the existing parent command item and
|
||||
/// have a separate approval ID in the action payload
|
||||
/// - network policy reviews, where there is no target item
|
||||
///
|
||||
/// A network call is triggered by a CommandExecution item, so having a
|
||||
@@ -1447,10 +1485,24 @@ pub struct FileChangePatchUpdatedNotification {
|
||||
pub changes: Vec<FileUpdateChange>,
|
||||
}
|
||||
|
||||
v2_enum_from_core! {
|
||||
/// Distinguishes a command approval from input sent to an existing terminal.
|
||||
#[derive(Default)]
|
||||
#[ts(rename_all = "camelCase")]
|
||||
pub enum CommandExecutionApprovalKind from CoreExecApprovalKind {
|
||||
#[default]
|
||||
Command,
|
||||
WriteStdin,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS, ExperimentalApi)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[ts(export_to = "v2/")]
|
||||
pub struct CommandExecutionRequestApprovalParams {
|
||||
/// Kind of action under review. Defaults to `command` for older servers.
|
||||
#[serde(default)]
|
||||
pub kind: CommandExecutionApprovalKind,
|
||||
pub thread_id: String,
|
||||
pub turn_id: String,
|
||||
pub item_id: String,
|
||||
@@ -1464,6 +1516,7 @@ pub struct CommandExecutionRequestApprovalParams {
|
||||
/// For zsh-exec-bridge subcommand approvals, multiple callbacks can belong to
|
||||
/// one parent `itemId`, so `approvalId` is a distinct opaque callback id
|
||||
/// (a UUID) used to disambiguate routing.
|
||||
/// Stdin approvals also use a distinct callback id; inspect `kind` to distinguish them.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
#[ts(optional = nullable)]
|
||||
pub approval_id: Option<String>,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use super::*;
|
||||
use crate::ServerNotification;
|
||||
use codex_protocol::approvals::ElicitationRequest as CoreElicitationRequest;
|
||||
use codex_protocol::approvals::GuardianAssessmentAction as CoreGuardianAssessmentAction;
|
||||
use codex_protocol::config_types::MultiAgentMode;
|
||||
use codex_protocol::items::AgentMessageContent;
|
||||
use codex_protocol::items::AgentMessageItem;
|
||||
@@ -2834,6 +2835,46 @@ fn guardian_approval_review_action_round_trips_command_shape() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn guardian_stdin_review_action_round_trips_native_and_foreign_paths() {
|
||||
for (cwd_uri, cwd_native) in [
|
||||
("file:///home/alice/repo", "/home/alice/repo"),
|
||||
(
|
||||
"file:///C:/Users/Alice%20Smith/repo",
|
||||
r"C:\Users\Alice Smith\repo",
|
||||
),
|
||||
("file://server/share/repo", r"\\server\share\repo"),
|
||||
] {
|
||||
let core_action = CoreGuardianAssessmentAction::WriteStdin {
|
||||
approval_id: "stdin-approval".into(),
|
||||
process_id: "42".into(),
|
||||
stdin: "yes\n".into(),
|
||||
cwd: PathUri::parse(cwd_uri).expect("valid cwd URI"),
|
||||
};
|
||||
let action = GuardianApprovalReviewAction::from(core_action.clone());
|
||||
let value = json!({
|
||||
"type": "writeStdin",
|
||||
"approvalId": "stdin-approval",
|
||||
"processId": "42",
|
||||
"stdin": "yes\n",
|
||||
"cwd": cwd_native,
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
serde_json::to_value(&action).expect("serialize stdin review action"),
|
||||
value,
|
||||
);
|
||||
let deserialized: GuardianApprovalReviewAction =
|
||||
serde_json::from_value(value).expect("deserialize stdin review action");
|
||||
assert_eq!(deserialized, action);
|
||||
assert_eq!(
|
||||
CoreGuardianAssessmentAction::try_from(deserialized)
|
||||
.expect("convert stdin review action"),
|
||||
core_action,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn network_requirements_deserializes_legacy_fields() {
|
||||
let requirements: NetworkRequirements = serde_json::from_value(json!({
|
||||
|
||||
Reference in New Issue
Block a user