mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Make user input blocking behavior explicit (#36410)
## Why Clients need to know whether a `request_user_input` request must wait for an explicit response or may auto-resolve. Using `autoResolutionMs` as that signal conflated the blocking decision with timeout policy. ## What changed - Add required `isBlocking` fields to user input protocol and app-server request payloads. Plan-mode requests are blocking, while requests from other enabled modes are non-blocking. - Drive the TUI's auto-resolution behavior from `isBlocking` and remove `autoResolutionMs` from the model-facing tool schema. - Deprecate `autoResolutionMs` while retaining it for compatibility, and treat legacy payloads without `isBlocking` as blocking. ## Testing - Cover mode-derived blocking behavior, legacy deserialization, app-server forwarding, delegated requests, and TUI auto-resolution. GitOrigin-RevId: 29aade657ef743065ec264376ba567a9b353d7d7
This commit is contained in:
1 parent
a850875a8e
commit
e2c0837923
36 files changed
+578
-133
No files matched your search
@@ -5,8 +5,9 @@ use crate::tools::context::ToolPayload;
|
||||
use crate::tools::context::boxed_tool_output;
|
||||
use crate::tools::handlers::parse_arguments;
|
||||
use crate::tools::handlers::request_user_input_spec::REQUEST_USER_INPUT_TOOL_NAME;
|
||||
use crate::tools::handlers::request_user_input_spec::RequestUserInputToolArgs;
|
||||
use crate::tools::handlers::request_user_input_spec::create_request_user_input_tool;
|
||||
use crate::tools::handlers::request_user_input_spec::normalize_request_user_input_args;
|
||||
use crate::tools::handlers::request_user_input_spec::normalize_request_user_input_tool_args;
|
||||
use crate::tools::handlers::request_user_input_spec::request_user_input_tool_description;
|
||||
use crate::tools::handlers::request_user_input_spec::request_user_input_unavailable_message;
|
||||
use crate::tools::registry::CoreToolRuntime;
|
||||
@@ -62,14 +63,19 @@ impl RequestUserInputHandler {
|
||||
));
|
||||
}
|
||||
|
||||
let mode = session.collaboration_mode().await.mode;
|
||||
let mode = turn.collaboration_mode().mode;
|
||||
if let Some(message) = request_user_input_unavailable_message(mode, &self.available_modes) {
|
||||
return Err(FunctionCallError::RespondToModel(message));
|
||||
}
|
||||
|
||||
let args: RequestUserInputArgs = parse_arguments(&arguments)?;
|
||||
let args =
|
||||
normalize_request_user_input_args(args).map_err(FunctionCallError::RespondToModel)?;
|
||||
let args: RequestUserInputToolArgs = parse_arguments(&arguments)?;
|
||||
let args = normalize_request_user_input_tool_args(args)
|
||||
.map_err(FunctionCallError::RespondToModel)?;
|
||||
let args = RequestUserInputArgs {
|
||||
questions: args.questions,
|
||||
is_blocking: mode == ModeKind::Plan,
|
||||
auto_resolution_ms: None,
|
||||
};
|
||||
let response = session
|
||||
.request_user_input(turn.as_ref(), call_id, args)
|
||||
.await
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
use codex_protocol::config_types::ModeKind;
|
||||
use codex_protocol::request_user_input::RequestUserInputArgs;
|
||||
use codex_protocol::request_user_input::RequestUserInputQuestion;
|
||||
use codex_tools::JsonSchema;
|
||||
use codex_tools::ResponsesApiTool;
|
||||
use codex_tools::ToolSpec;
|
||||
use serde::Deserialize;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
pub const REQUEST_USER_INPUT_TOOL_NAME: &str = "request_user_input";
|
||||
pub const MIN_AUTO_RESOLUTION_MS: u64 = 60_000;
|
||||
pub const MAX_AUTO_RESOLUTION_MS: u64 = 240_000;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
|
||||
pub(crate) struct RequestUserInputToolArgs {
|
||||
pub questions: Vec<RequestUserInputQuestion>,
|
||||
}
|
||||
|
||||
pub fn create_request_user_input_tool(description: String) -> ToolSpec {
|
||||
let option_props = BTreeMap::from([
|
||||
@@ -68,14 +72,7 @@ pub fn create_request_user_input_tool(description: String) -> ToolSpec {
|
||||
Some("Questions to show the user. Prefer 1 and do not exceed 3".to_string()),
|
||||
);
|
||||
|
||||
let auto_resolution_ms_schema = JsonSchema::number(Some(format!(
|
||||
"Optional auto-resolution window in milliseconds, from {MIN_AUTO_RESOLUTION_MS} to {MAX_AUTO_RESOLUTION_MS}. Include this only when the question is useful but non-blocking and continuing with best judgment is acceptable if the user does not answer; omit it when explicit user input is required before continuing. Use {MIN_AUTO_RESOLUTION_MS} for lightly helpful context and up to {MAX_AUTO_RESOLUTION_MS} when the answer would materially unblock better work."
|
||||
)));
|
||||
|
||||
let properties = BTreeMap::from([
|
||||
("questions".to_string(), questions_schema),
|
||||
("autoResolutionMs".to_string(), auto_resolution_ms_schema),
|
||||
]);
|
||||
let properties = BTreeMap::from([("questions".to_string(), questions_schema)]);
|
||||
|
||||
ToolSpec::Function(ResponsesApiTool {
|
||||
name: REQUEST_USER_INPUT_TOOL_NAME.to_string(),
|
||||
@@ -105,9 +102,9 @@ pub fn request_user_input_unavailable_message(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn normalize_request_user_input_args(
|
||||
mut args: RequestUserInputArgs,
|
||||
) -> Result<RequestUserInputArgs, String> {
|
||||
pub(crate) fn normalize_request_user_input_tool_args(
|
||||
mut args: RequestUserInputToolArgs,
|
||||
) -> Result<RequestUserInputToolArgs, String> {
|
||||
let missing_options = args
|
||||
.questions
|
||||
.iter()
|
||||
@@ -120,26 +117,13 @@ pub fn normalize_request_user_input_args(
|
||||
question.is_other = true;
|
||||
}
|
||||
|
||||
if let Some(auto_resolution_ms) = args.auto_resolution_ms {
|
||||
let clamped_auto_resolution_ms =
|
||||
auto_resolution_ms.clamp(MIN_AUTO_RESOLUTION_MS, MAX_AUTO_RESOLUTION_MS);
|
||||
if clamped_auto_resolution_ms != auto_resolution_ms {
|
||||
tracing::warn!(
|
||||
auto_resolution_ms,
|
||||
clamped_auto_resolution_ms,
|
||||
"clamped request_user_input autoResolutionMs to supported range"
|
||||
);
|
||||
args.auto_resolution_ms = Some(clamped_auto_resolution_ms);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(args)
|
||||
}
|
||||
|
||||
pub fn request_user_input_tool_description(available_modes: &[ModeKind]) -> String {
|
||||
let allowed_modes = format_allowed_modes(available_modes);
|
||||
format!(
|
||||
"Request user input for one to three short questions and wait for the response. Set autoResolutionMs, from {MIN_AUTO_RESOLUTION_MS} to {MAX_AUTO_RESOLUTION_MS} milliseconds, only when the question is useful but non-blocking and continuing with best judgment is acceptable if the user does not answer; omit it when explicit user input is required. This tool is only available in {allowed_modes}."
|
||||
"Request user input for one to three short questions and wait for the response. This tool is only available in {allowed_modes}."
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -29,13 +29,6 @@ fn request_user_input_tool_includes_questions_schema() {
|
||||
strict: false,
|
||||
defer_loading: None,
|
||||
parameters: JsonSchema::object(BTreeMap::from([
|
||||
(
|
||||
"autoResolutionMs".to_string(),
|
||||
JsonSchema::number(Some(
|
||||
"Optional auto-resolution window in milliseconds, from 60000 to 240000. Include this only when the question is useful but non-blocking and continuing with best judgment is acceptable if the user does not answer; omit it when explicit user input is required before continuing. Use 60000 for lightly helpful context and up to 240000 when the answer would materially unblock better work."
|
||||
.to_string(),
|
||||
)),
|
||||
),
|
||||
(
|
||||
"questions".to_string(),
|
||||
JsonSchema::array(
|
||||
@@ -107,15 +100,17 @@ fn request_user_input_tool_includes_questions_schema() {
|
||||
),
|
||||
),
|
||||
),
|
||||
]), Some(vec!["questions".to_string()]), Some(false.into())),
|
||||
]),
|
||||
Some(vec!["questions".to_string()]),
|
||||
Some(false.into())),
|
||||
output_schema: None,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalize_request_user_input_args_clamps_out_of_range_auto_resolution_ms() {
|
||||
let args = RequestUserInputArgs {
|
||||
fn normalize_request_user_input_tool_args_sets_other_on_every_question() {
|
||||
let args = RequestUserInputToolArgs {
|
||||
questions: vec![RequestUserInputQuestion {
|
||||
id: "confirm".to_string(),
|
||||
header: "Confirm".to_string(),
|
||||
@@ -127,73 +122,35 @@ fn normalize_request_user_input_args_clamps_out_of_range_auto_resolution_ms() {
|
||||
description: "Continue.".to_string(),
|
||||
}]),
|
||||
}],
|
||||
auto_resolution_ms: Some(MIN_AUTO_RESOLUTION_MS - 1),
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
normalize_request_user_input_args(args.clone()),
|
||||
Ok(RequestUserInputArgs {
|
||||
normalize_request_user_input_tool_args(args.clone()),
|
||||
Ok(RequestUserInputToolArgs {
|
||||
questions: vec![RequestUserInputQuestion {
|
||||
is_other: true,
|
||||
..args.questions[0].clone()
|
||||
}],
|
||||
auto_resolution_ms: Some(MIN_AUTO_RESOLUTION_MS),
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
normalize_request_user_input_args(RequestUserInputArgs {
|
||||
auto_resolution_ms: Some(MAX_AUTO_RESOLUTION_MS + 1),
|
||||
..args.clone()
|
||||
}),
|
||||
Ok(RequestUserInputArgs {
|
||||
questions: vec![RequestUserInputQuestion {
|
||||
is_other: true,
|
||||
..args.questions[0].clone()
|
||||
}],
|
||||
auto_resolution_ms: Some(MAX_AUTO_RESOLUTION_MS),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalize_request_user_input_args_accepts_auto_resolution_boundaries() {
|
||||
let args = RequestUserInputArgs {
|
||||
fn normalize_request_user_input_tool_args_rejects_missing_options() {
|
||||
let args = RequestUserInputToolArgs {
|
||||
questions: vec![RequestUserInputQuestion {
|
||||
id: "confirm".to_string(),
|
||||
header: "Confirm".to_string(),
|
||||
question: "Proceed?".to_string(),
|
||||
is_other: false,
|
||||
is_secret: false,
|
||||
options: Some(vec![RequestUserInputQuestionOption {
|
||||
label: "Yes (Recommended)".to_string(),
|
||||
description: "Continue.".to_string(),
|
||||
}]),
|
||||
options: None,
|
||||
}],
|
||||
auto_resolution_ms: Some(MIN_AUTO_RESOLUTION_MS),
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
normalize_request_user_input_args(args.clone()),
|
||||
Ok(RequestUserInputArgs {
|
||||
questions: vec![RequestUserInputQuestion {
|
||||
is_other: true,
|
||||
..args.questions[0].clone()
|
||||
}],
|
||||
auto_resolution_ms: Some(MIN_AUTO_RESOLUTION_MS),
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
normalize_request_user_input_args(RequestUserInputArgs {
|
||||
auto_resolution_ms: Some(MAX_AUTO_RESOLUTION_MS),
|
||||
..args.clone()
|
||||
}),
|
||||
Ok(RequestUserInputArgs {
|
||||
questions: vec![RequestUserInputQuestion {
|
||||
is_other: true,
|
||||
..args.questions[0].clone()
|
||||
}],
|
||||
auto_resolution_ms: Some(MAX_AUTO_RESOLUTION_MS),
|
||||
})
|
||||
normalize_request_user_input_tool_args(args),
|
||||
Err("request_user_input requires non-empty options for every question".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
@@ -231,10 +188,14 @@ fn request_user_input_unavailable_messages_respect_default_mode_feature_flag() {
|
||||
fn request_user_input_tool_description_mentions_available_modes() {
|
||||
assert_eq!(
|
||||
request_user_input_tool_description(&default_available_modes()),
|
||||
"Request user input for one to three short questions and wait for the response. Set autoResolutionMs, from 60000 to 240000 milliseconds, only when the question is useful but non-blocking and continuing with best judgment is acceptable if the user does not answer; omit it when explicit user input is required. This tool is only available in Plan mode.".to_string()
|
||||
"Request user input for one to three short questions and wait for the response. This tool is only available in Plan mode.".to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
request_user_input_tool_description(&default_mode_enabled_available_modes()),
|
||||
"Request user input for one to three short questions and wait for the response. Set autoResolutionMs, from 60000 to 240000 milliseconds, only when the question is useful but non-blocking and continuing with best judgment is acceptable if the user does not answer; omit it when explicit user input is required. This tool is only available in Default or Plan mode.".to_string()
|
||||
"Request user input for one to three short questions and wait for the response. This tool is only available in Default or Plan mode.".to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
request_user_input_tool_description(&[ModeKind::Default]),
|
||||
"Request user input for one to three short questions and wait for the response. This tool is only available in Default mode.".to_string()
|
||||
);
|
||||
}
|
||||
@@ -1,14 +1,19 @@
|
||||
use super::*;
|
||||
use crate::session::step_context::StepContext;
|
||||
use crate::session::tests::make_session_and_context;
|
||||
use crate::session::tests::make_session_and_context_with_rx;
|
||||
use crate::state::ActiveTurn;
|
||||
use crate::tools::context::ToolInvocation;
|
||||
use crate::tools::context::ToolPayload;
|
||||
use crate::turn_diff_tracker::TurnDiffTracker;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::protocol::EventMsg;
|
||||
use codex_protocol::protocol::SessionSource;
|
||||
use codex_protocol::protocol::SubAgentSource;
|
||||
use codex_protocol::request_user_input::RequestUserInputResponse;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::json;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
@@ -69,3 +74,142 @@ async fn multi_agent_v2_request_user_input_rejects_subagent_threads() {
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn request_user_input_sets_non_blocking_outside_plan_mode() {
|
||||
let (session, turn, events) = make_session_and_context_with_rx().await;
|
||||
*session.active_turn.lock().await = Some(ActiveTurn::default());
|
||||
|
||||
let request = tokio::spawn({
|
||||
let session = Arc::clone(&session);
|
||||
let turn = Arc::clone(&turn);
|
||||
async move {
|
||||
RequestUserInputHandler {
|
||||
available_modes: vec![ModeKind::Default],
|
||||
}
|
||||
.handle(ToolInvocation {
|
||||
session,
|
||||
step_context: StepContext::for_test(Arc::clone(&turn)),
|
||||
turn,
|
||||
cancellation_token: tokio_util::sync::CancellationToken::new(),
|
||||
tracker: Arc::new(Mutex::new(TurnDiffTracker::default())),
|
||||
call_id: "call-1".to_string(),
|
||||
tool_name: codex_tools::ToolName::plain(REQUEST_USER_INPUT_TOOL_NAME),
|
||||
source: crate::tools::context::ToolCallSource::Direct,
|
||||
payload: ToolPayload::Function {
|
||||
arguments: json!({
|
||||
"questions": [{
|
||||
"header": "Hdr",
|
||||
"question": "Pick one",
|
||||
"id": "pick_one",
|
||||
"options": [
|
||||
{
|
||||
"label": "A",
|
||||
"description": "A"
|
||||
},
|
||||
{
|
||||
"label": "B",
|
||||
"description": "B"
|
||||
}
|
||||
]
|
||||
}]
|
||||
})
|
||||
.to_string(),
|
||||
},
|
||||
})
|
||||
.await
|
||||
}
|
||||
});
|
||||
|
||||
let event = events.recv().await.expect("request_user_input event");
|
||||
let EventMsg::RequestUserInput(request_event) = event.msg else {
|
||||
panic!("expected request_user_input event");
|
||||
};
|
||||
assert_eq!(request_event.call_id, "call-1");
|
||||
assert!(!request_event.is_blocking);
|
||||
|
||||
session
|
||||
.notify_user_input_response(
|
||||
&request_event.turn_id,
|
||||
RequestUserInputResponse {
|
||||
answers: HashMap::new(),
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
request
|
||||
.await
|
||||
.expect("request_user_input handler task should finish")
|
||||
.expect("request_user_input handler should succeed");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn request_user_input_sets_blocking_from_turn_mode() {
|
||||
let (session, mut turn, events) = make_session_and_context_with_rx().await;
|
||||
Arc::get_mut(&mut turn)
|
||||
.expect("turn context should be uniquely owned")
|
||||
.mode = ModeKind::Plan;
|
||||
*session.active_turn.lock().await = Some(ActiveTurn::default());
|
||||
|
||||
let request = tokio::spawn({
|
||||
let session = Arc::clone(&session);
|
||||
let turn = Arc::clone(&turn);
|
||||
async move {
|
||||
RequestUserInputHandler {
|
||||
available_modes: vec![ModeKind::Plan],
|
||||
}
|
||||
.handle(ToolInvocation {
|
||||
session,
|
||||
step_context: StepContext::for_test(Arc::clone(&turn)),
|
||||
turn,
|
||||
cancellation_token: tokio_util::sync::CancellationToken::new(),
|
||||
tracker: Arc::new(Mutex::new(TurnDiffTracker::default())),
|
||||
call_id: "call-1".to_string(),
|
||||
tool_name: codex_tools::ToolName::plain(REQUEST_USER_INPUT_TOOL_NAME),
|
||||
source: crate::tools::context::ToolCallSource::Direct,
|
||||
payload: ToolPayload::Function {
|
||||
arguments: json!({
|
||||
"questions": [{
|
||||
"header": "Hdr",
|
||||
"question": "Pick one",
|
||||
"id": "pick_one",
|
||||
"options": [
|
||||
{
|
||||
"label": "A",
|
||||
"description": "A"
|
||||
},
|
||||
{
|
||||
"label": "B",
|
||||
"description": "B"
|
||||
}
|
||||
]
|
||||
}]
|
||||
})
|
||||
.to_string(),
|
||||
},
|
||||
})
|
||||
.await
|
||||
}
|
||||
});
|
||||
|
||||
let event = events.recv().await.expect("request_user_input event");
|
||||
let EventMsg::RequestUserInput(request_event) = event.msg else {
|
||||
panic!("expected request_user_input event");
|
||||
};
|
||||
assert_eq!(request_event.call_id, "call-1");
|
||||
assert!(request_event.is_blocking);
|
||||
|
||||
session
|
||||
.notify_user_input_response(
|
||||
&request_event.turn_id,
|
||||
RequestUserInputResponse {
|
||||
answers: HashMap::new(),
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
request
|
||||
.await
|
||||
.expect("request_user_input handler task should finish")
|
||||
.expect("request_user_input handler should succeed");
|
||||
}
|
||||
Reference in new issue
Block a user