mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Treat user input answers as Guardian authorization changes (#40623)
## Why Answers collected by `request_user_input` can change what the user authorizes, but post-tool hooks may replace or reject the tool output that Guardian normally sees. ## What changed - Record bounded, host-observed answers before post-tool hooks run and provide them as trusted evidence to synchronous and asynchronous Guardian reviews. - Include root-thread answers in worker review context. - Count successful answers as authorization changes so earlier review evidence becomes stale, while ignoring empty or unrelated answers. ## Testing Add coverage for allowed and denied stale reviews, empty and oversized answers, hook-modified output, and root-to-worker propagation. GitOrigin-RevId: 970c52e40338ecd057c7cd2a69d04b06e97cd068
This commit is contained in:
1 parent
c395364915
commit
4b81410a80
9 files changed
+563
-72
No files matched your search
@@ -1,4 +1,7 @@
|
||||
use crate::codex_thread::GuardianRootMessage;
|
||||
use crate::context::GuardianReviewEvidence;
|
||||
use crate::function_tool::FunctionCallError;
|
||||
use crate::guardian::guardian_truncate_text;
|
||||
use crate::tools::context::FunctionToolOutput;
|
||||
use crate::tools::context::ToolInvocation;
|
||||
use crate::tools::context::ToolPayload;
|
||||
@@ -12,6 +15,7 @@ use crate::tools::handlers::request_user_input_spec::request_user_input_tool_des
|
||||
use crate::tools::handlers::request_user_input_spec::request_user_input_unavailable_message;
|
||||
use crate::tools::registry::CoreToolRuntime;
|
||||
use crate::tools::registry::ToolExecutor;
|
||||
use codex_features::Feature;
|
||||
use codex_protocol::config_types::ModeKind;
|
||||
use codex_protocol::request_user_input::RequestUserInputArgs;
|
||||
use codex_tools::ToolName;
|
||||
@@ -21,6 +25,9 @@ pub struct RequestUserInputHandler {
|
||||
pub available_modes: Vec<ModeKind>,
|
||||
}
|
||||
|
||||
const MAX_GUARDIAN_USER_INPUT_ANSWERS: usize = 8;
|
||||
const MAX_GUARDIAN_USER_INPUT_TOKENS: usize = 900;
|
||||
|
||||
impl ToolExecutor<ToolInvocation> for RequestUserInputHandler {
|
||||
fn tool_name(&self) -> ToolName {
|
||||
ToolName::plain(REQUEST_USER_INPUT_TOOL_NAME)
|
||||
@@ -76,8 +83,9 @@ impl RequestUserInputHandler {
|
||||
is_blocking: mode == ModeKind::Plan,
|
||||
auto_resolution_ms: None,
|
||||
};
|
||||
let questions = args.questions.clone();
|
||||
let response = session
|
||||
.request_user_input(turn.as_ref(), call_id, args)
|
||||
.request_user_input(turn.as_ref(), call_id.clone(), args)
|
||||
.await
|
||||
.ok_or_else(|| {
|
||||
FunctionCallError::RespondToModel(format!(
|
||||
@@ -90,10 +98,50 @@ impl RequestUserInputHandler {
|
||||
"failed to serialize {REQUEST_USER_INPUT_TOOL_NAME} response: {err}"
|
||||
))
|
||||
})?;
|
||||
let user_input = questions
|
||||
.iter()
|
||||
.filter_map(|question| {
|
||||
let response = response.answers.get(&question.id)?;
|
||||
let answers = response
|
||||
.answers
|
||||
.iter()
|
||||
.filter(|answer| !answer.trim().is_empty())
|
||||
.take(MAX_GUARDIAN_USER_INPUT_ANSWERS)
|
||||
.cloned()
|
||||
.collect::<Vec<_>>();
|
||||
if answers.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut question_text = question.question.clone();
|
||||
for option in question
|
||||
.options
|
||||
.iter()
|
||||
.flatten()
|
||||
.filter(|option| response.answers.contains(&option.label))
|
||||
.take(MAX_GUARDIAN_USER_INPUT_ANSWERS)
|
||||
{
|
||||
question_text.push_str(&format!("\n{}: {}", option.label, option.description));
|
||||
}
|
||||
Some(format!(
|
||||
"{}{}",
|
||||
GuardianRootMessage::Assistant(question_text).render(),
|
||||
GuardianRootMessage::User(answers.join("\n")).render()
|
||||
))
|
||||
})
|
||||
.take(MAX_GUARDIAN_USER_INPUT_ANSWERS)
|
||||
.collect::<String>();
|
||||
if !user_input.is_empty() && turn.config.features.enabled(Feature::GuardianApproval) {
|
||||
let fragment = guardian_truncate_text(&user_input, MAX_GUARDIAN_USER_INPUT_TOKENS).0;
|
||||
session
|
||||
.services
|
||||
.thread_extension_data
|
||||
.get_or_init(GuardianReviewEvidence::default)
|
||||
.record_user_input(&call_id, fragment);
|
||||
}
|
||||
|
||||
Ok(boxed_tool_output(FunctionToolOutput::from_text(
|
||||
content,
|
||||
Some(true),
|
||||
/*success*/ Some(true),
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,14 +7,18 @@ use crate::tools::context::ToolInvocation;
|
||||
use crate::tools::context::ToolPayload;
|
||||
use crate::turn_diff_tracker::TurnDiffTracker;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use codex_protocol::protocol::EventMsg;
|
||||
use codex_protocol::protocol::SessionSource;
|
||||
use codex_protocol::protocol::SubAgentSource;
|
||||
use codex_protocol::request_user_input::RequestUserInputAnswer;
|
||||
use codex_protocol::request_user_input::RequestUserInputResponse;
|
||||
use codex_utils_output_truncation::approx_token_count;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::json;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use test_case::test_case;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
#[tokio::test]
|
||||
@@ -75,8 +79,13 @@ async fn multi_agent_v2_request_user_input_rejects_subagent_threads() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test_case(None; "empty")]
|
||||
#[test_case(Some(("other_question", "A".to_owned())); "unrequested question")]
|
||||
#[test_case(Some(("pick_one", " ".to_owned())); "blank answer")]
|
||||
#[test_case(Some(("pick_one", "A".to_owned())); "genuine answer")]
|
||||
#[test_case(Some(("pick_one", "x\n".repeat(900))); "multiline answer remains bounded")]
|
||||
#[tokio::test]
|
||||
async fn request_user_input_sets_non_blocking_outside_plan_mode() {
|
||||
async fn request_user_input_sets_non_blocking_outside_plan_mode(answer: Option<(&str, String)>) {
|
||||
let (session, turn, events) = make_session_and_context_with_rx().await;
|
||||
*session.active_turn.lock().await = Some(ActiveTurn::default());
|
||||
|
||||
@@ -132,15 +141,84 @@ async fn request_user_input_sets_non_blocking_outside_plan_mode() {
|
||||
.notify_user_input_response(
|
||||
&request_event.turn_id,
|
||||
RequestUserInputResponse {
|
||||
answers: HashMap::new(),
|
||||
answers: answer
|
||||
.iter()
|
||||
.map(|(question_id, answer)| {
|
||||
(
|
||||
(*question_id).to_owned(),
|
||||
RequestUserInputAnswer {
|
||||
answers: vec![answer.clone()],
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
request
|
||||
let output = request
|
||||
.await
|
||||
.expect("request_user_input handler task should finish")
|
||||
.expect("request_user_input handler should succeed");
|
||||
assert!(output.success_for_logging());
|
||||
let recorded_answer = session
|
||||
.services
|
||||
.thread_extension_data
|
||||
.get::<GuardianReviewEvidence>()
|
||||
.and_then(|evidence| evidence.user_input_for_call("call-1"));
|
||||
if let Some(("pick_one", answer)) = answer {
|
||||
if answer.contains('\n') {
|
||||
let fragment = recorded_answer.expect("genuine multiline answer should be recorded");
|
||||
assert!(approx_token_count(&fragment) <= MAX_GUARDIAN_USER_INPUT_TOKENS);
|
||||
assert!(fragment.contains("<truncated omitted_approx_tokens="));
|
||||
} else if !answer.trim().is_empty() {
|
||||
assert_eq!(
|
||||
recorded_answer,
|
||||
Some("assistant: Pick one\nassistant: A: A\nuser: A\n".to_owned())
|
||||
);
|
||||
} else {
|
||||
assert_eq!(recorded_answer, None);
|
||||
}
|
||||
} else {
|
||||
assert_eq!(recorded_answer, None);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn guardian_user_input_evidence_is_bounded() {
|
||||
let (session, turn, _) = make_session_and_context_with_rx().await;
|
||||
let evidence = GuardianReviewEvidence::default();
|
||||
for index in 0..10 {
|
||||
let call_id = format!("call-{index}");
|
||||
session
|
||||
.record_conversation_items(
|
||||
turn.as_ref(),
|
||||
&[ResponseItem::FunctionCall {
|
||||
id: None,
|
||||
name: REQUEST_USER_INPUT_TOOL_NAME.to_owned(),
|
||||
namespace: None,
|
||||
arguments: "{}".to_owned(),
|
||||
call_id: call_id.clone(),
|
||||
encrypted_function_args: None,
|
||||
internal_chat_message_metadata_passthrough: None,
|
||||
}],
|
||||
)
|
||||
.await;
|
||||
evidence.record_user_input(&call_id, format!("user: answer-{index}\n"));
|
||||
}
|
||||
let history = session.conversation_history_snapshot().await;
|
||||
assert_eq!(
|
||||
evidence.user_input_fragments(history.as_ref()),
|
||||
(2..10)
|
||||
.map(|index| format!("user: answer-{index}\n"))
|
||||
.collect::<Vec<_>>()
|
||||
);
|
||||
assert_eq!(
|
||||
evidence
|
||||
.authorization_version(history.as_ref())
|
||||
.user_input_response_count,
|
||||
10
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
Reference in new issue
Block a user