Include parent evidence in recorded Guardian reviews (#63770)

## Why

An archived worker's transcript can omit the original user instructions and
assistant context that Guardian needs to assess a recorded tool call.

## What changed

Let clients supply optional `rootEvidence` to experimental
`thread/guardianReview` on an idle, ephemeral non-subagent thread. Guardian
reviews the ordered parent messages and omission flags alongside the worker
history loaded through `thread/inject_items`. The parent evidence stays out
of the worker history, and `usedRootEvidence: true` confirms that the review
used it.

Guardian can reuse reviewer history when the evidence is unchanged. Changing
or omitting the evidence resets that history.

## Testing

Add an app-server integration test covering rejection on persisted threads,
reviewer history reuse and reset, and review without executing the tool call.

GitOrigin-RevId: a4e78d770af5ad29864cb745b2e31f0fc921f213
This commit is contained in:
RJ
2026-11-24 23:56:17 +00:00
committed by copyberry
parent ff2ee6212c
commit e1492bd86d
12 changed files with 294 additions and 4 deletions
+31 -1
View File
@@ -1149,12 +1149,42 @@ impl CodexThread {
&self,
tool_call: &ResponseItem,
cancellation: &CancellationToken,
) -> CodexResult<crate::guardian_review::GuardianReviewResult> {
self.guardian_review_with_root_evidence(
tool_call,
/*root_evidence*/ None,
cancellation,
)
.await
}
/// Use the supplied parent evidence only for this review of the injected history.
pub async fn guardian_review_with_root_evidence(
&self,
tool_call: &ResponseItem,
root_evidence: Option<codex_app_server_protocol::GuardianRootEvidence>,
cancellation: &CancellationToken,
) -> CodexResult<crate::guardian_review::GuardianReviewResult> {
if self.session.active_turn.lock().await.is_some() {
return Err(CodexErr::InvalidRequest("thread has an active turn".into()));
}
if root_evidence.is_some()
&& (!self.config().await.ephemeral
|| matches!(self.session_source(), SessionSource::SubAgent(_)))
{
return Err(CodexErr::InvalidRequest(
"recorded worker review requires an ephemeral non-subagent thread".into(),
));
}
let history = self.session.clone_history().await;
crate::guardian::review_tool_call(&self.session, history, tool_call, cancellation).await
crate::guardian::review_tool_call(
&self.session,
history,
tool_call,
root_evidence,
cancellation,
)
.await
}
/// Inject raw Responses items. Plaintext agent-mail batches in a native MAv2
+5
View File
@@ -97,6 +97,8 @@ pub(crate) use codex_guardian_reviewer::GUARDIAN_MAX_NODE_REPL_TOOL_RESULT_TOKEN
/// MCP elicitations use their live invocation's issuing step when available, otherwise turn inputs.
#[derive(Clone)]
pub(crate) struct GuardianReviewContext {
/// Parent evidence supplied for a direct review of recorded worker history.
root_evidence: Option<Arc<codex_app_server_protocol::GuardianRootEvidence>>,
pub(crate) request_country: RequestCountry,
pub(crate) serving_assessment: Option<Arc<ServingAssessment>>,
/// Observational serving probe; never an approval decision.
@@ -151,6 +153,7 @@ impl GuardianReviewContext {
serving_assessment: None,
guardian_probe: None,
review_execution: None,
root_evidence: None,
enclosing_invocation_id: None,
parent_response: turn.extension_data.get::<ParentResponse>(),
environments: environments.clone(),
@@ -181,6 +184,7 @@ impl From<&Arc<StepContext>> for GuardianReviewContext {
serving_assessment: step.serving_assessment.clone(),
guardian_probe: step.guardian_probe,
review_execution: None,
root_evidence: None,
enclosing_invocation_id: None,
parent_response: step.turn.extension_data.get::<ParentResponse>(),
turn: Arc::clone(&step.turn),
@@ -203,6 +207,7 @@ impl From<Arc<TurnContext>> for GuardianReviewContext {
serving_assessment: None,
guardian_probe: None,
review_execution: None,
root_evidence: None,
enclosing_invocation_id: None,
parent_response: turn.extension_data.get::<ParentResponse>(),
environments: turn.initial_environments.clone(),
+5 -1
View File
@@ -179,6 +179,10 @@ pub(super) async fn collect_guardian_prompt_items_with_parent_turn(
Some(context) => context.turn().config.guardian_transcript_mode,
None => config.guardian_transcript_mode,
};
let recorded_root = parent_context
.and_then(|context| context.root_evidence.as_deref())
.map(super::recorded_tool_call::root_messages)
.unwrap_or_default();
let sections = collect_guardian_context(SectionInput {
target: profile.target,
history: &GuardianReviewHistory {
@@ -188,7 +192,7 @@ pub(super) async fn collect_guardian_prompt_items_with_parent_turn(
},
transcript: &profile.transcript,
is_selected_exec_output,
root_conversation: &[],
root_conversation: &recorded_root,
root_conversation_candidates: root_authorization.as_ref(),
trusted_user_answers: &trusted_user_inputs,
planned_action: Some(&planned_action),
@@ -3,7 +3,10 @@
use std::sync::Arc;
use codex_app_server_protocol::GuardianRootEvidence;
use codex_app_server_protocol::GuardianRootEvidenceMessage;
use codex_features::Feature;
use codex_guardian_context::GuardianRootMessage;
use codex_guardian_reviewer::GuardianReviewError;
use codex_guardian_reviewer::ReviewDeadline;
use codex_protocol::error::CodexErr;
@@ -40,6 +43,7 @@ pub(crate) async fn review_tool_call(
session: &Arc<Session>,
history: ContextManager,
tool_call: &ResponseItem,
root_evidence: Option<GuardianRootEvidence>,
cancellation: &CancellationToken,
) -> Result<GuardianReviewResult> {
let is_supported_builtin = match tool_call {
@@ -85,6 +89,13 @@ pub(crate) async fn review_tool_call(
"direct review does not support replaying request_permissions".into(),
));
}
if let Some(evidence) = &root_evidence
&& !evidence.messages.iter().any(
|entry| matches!(entry, GuardianRootEvidenceMessage::User(text) if !text.trim().is_empty()),
)
{
return Err(CodexErr::InvalidRequest("missing root user evidence".into()));
}
let deadline = ReviewDeadline::from_timeout(session.get_config().await.guardian_review_timeout);
let review = async {
@@ -143,7 +154,10 @@ pub(crate) async fn review_tool_call(
PreToolUseError::Cancelled(_) => CodexErr::TurnAborted,
})?;
let step = &invocation.step_context;
let context = GuardianReviewContext::from(step);
let context = GuardianReviewContext {
root_evidence: root_evidence.map(Arc::new),
..GuardianReviewContext::from(step)
};
let (approval, request) = if let Some(tool_info) = advertised_mcp_tool {
let request = mcp_tool_call::recorded::prepare_review_request(&invocation, tool_info)
.await
@@ -259,6 +273,23 @@ pub(crate) async fn review_tool_call(
}
}
pub(super) fn root_messages(evidence: &GuardianRootEvidence) -> Vec<GuardianRootMessage> {
let mut messages = vec![GuardianRootMessage::RetainedContextScope];
if evidence.missing_user_instructions {
messages.push(GuardianRootMessage::IncompleteRootInstructions);
}
if evidence.missing_assistant_context {
messages.push(GuardianRootMessage::IncompleteAssistantContext);
}
messages.extend(evidence.messages.iter().map(|entry| match entry {
GuardianRootEvidenceMessage::User(text) => GuardianRootMessage::User(text.clone()),
GuardianRootEvidenceMessage::Assistant(text) => {
GuardianRootMessage::Assistant(text.clone())
}
}));
messages
}
fn tool_error(error: FunctionCallError) -> CodexErr {
match error {
FunctionCallError::RespondToModel(message) => CodexErr::InvalidRequest(message),
@@ -145,6 +145,8 @@ pub struct GuardianReviewSessionReuseKey {
// history rewrites that invalidate existing reviewer context.
parent_history_version: u64,
parent_reset_version: u64,
// Never carry a recorded decision into a review with different or absent root evidence.
root_evidence: Option<Arc<codex_app_server_protocol::GuardianRootEvidence>>,
root_review_version: Option<(crate::codex_thread::GuardianAuthorizationVersion, u64)>,
node_repl_auto_review_required: bool,
node_repl_policy: String,
@@ -179,6 +181,7 @@ impl GuardianReviewSessionReuseKey {
context_mode: GuardianContextMode,
) -> Self {
Self {
root_evidence: None,
root_review_version: None,
parent_reset_version: 0,
parent_history_version: match ReviewContextPolicy::for_context(
@@ -61,6 +61,7 @@ impl PreparedGuardianContext {
.with_environments(context.environments())
.with_node_repl_policy_eligibility(context.model_info.computer_use_review_required())
.with_node_repl_policy(node_repl_policy);
key.root_evidence = context.root_evidence.clone();
key.root_review_version = root_review_version;
key.parent_reset_version = history.reset_version;
let inference = crate::guardian::GuardianInferenceContext::for_config(