Preserve Guardian review evidence when callers are cancelled (#63693)

## Why

Cancelling a turn or tool call could drop a pending history write or
lose an answer the session had already accepted. A missing write leaves
Guardian unable to confirm that its review history is complete.

## What changed

Let the session finish tracked evidence writes in queue order after the
caller is cancelled, and drain those writes on shutdown. Record verified
`request_user_input` answers at acceptance before waking the tool.
Continue persisting evidence after an earlier write goes unconfirmed,
while still reporting the history gap to Guardian.

## Testing

Regression tests cover initial and retried reviews waiting for a
revocation after caller cancellation, and an accepted answer reaching
history before the turn interruption even with an earlier unconfirmed
write.

GitOrigin-RevId: b8e212d34686bbc793002a34f5a97834a900a342
This commit is contained in:
felixxia-oai
2026-11-24 17:33:28 +00:00
committed by copyberry
parent 9a3303253b
commit 4ec69d3c5f
13 changed files with 429 additions and 209 deletions
@@ -13,9 +13,6 @@ use crate::tools::handlers::request_user_input_spec::request_user_input_unavaila
use crate::tools::registry::CoreToolRuntime;
use crate::tools::registry::ToolExecutor;
use codex_features::Feature;
use codex_history::RetainedContextEvent;
use codex_history::VerifiedAnswer;
use codex_history::VerifiedQuestionAnswer;
use codex_protocol::config_types::ModeKind;
use codex_protocol::request_user_input::RequestUserInputArgs;
use codex_tools::ToolName;
@@ -83,9 +80,10 @@ impl RequestUserInputHandler {
is_blocking: mode == ModeKind::Plan,
auto_resolution_ms: None,
};
let questions = args.questions.clone();
let accepted = session
.request_user_input(turn.as_ref(), call_id.to_string(), args)
// Retain answers even while an older checkpoint needs compatibility review.
let verify = turn.config.features.enabled(Feature::GuardianApproval);
let response = session
.prompt_user(turn.as_ref(), call_id.into(), args, verify)
.await
.ok_or_else(|| {
FunctionCallError::RespondToModel(format!(
@@ -93,58 +91,11 @@ impl RequestUserInputHandler {
))
})?;
let response = accepted.response;
let content = serde_json::to_string(&response).map_err(|err| {
FunctionCallError::Fatal(format!(
"failed to serialize {REQUEST_USER_INPUT_TOOL_NAME} response: {err}"
))
})?;
// Persist answers even while an older checkpoint needs compatibility review.
if turn.config.features.enabled(Feature::GuardianApproval) {
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())
.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))
{
question_text
.push_str(&format!("\n{}: {}", option.label, option.description));
}
Some(VerifiedQuestionAnswer {
question: question_text,
answer: answers.join("\n"),
})
})
.collect::<Vec<_>>();
if !user_input.is_empty() {
session
.record_retained_context(RetainedContextEvent::VerifiedAnswer {
answer: VerifiedAnswer {
turn_id: turn.sub_id.clone(),
call_id: call_id.into(),
questions: user_input,
},
acceptance_order: Some(accepted.acceptance_order),
})
.await;
}
}
Ok(boxed_tool_output(FunctionToolOutput::from_text(
content,
/*success*/ Some(true),