Record root turn IDs in Guardian review analytics (#63837)

## Why

The review's `turn_id` alone can't identify the root turn of the action
being reviewed. For example, a background terminal can request network
access during a later turn.

## What changed

Add an optional `root_turn_id` to `codex_guardian_review` events. When
a verified execution tag ties a network request to a registered tool
call, use the root turn ID captured when the call started. Leave the
field unset when the reviewed action's root turn is unknown.

## Testing

Extend the MCP approval test to check the reported root turn ID. Cover
untagged network requests and verify that a cross-turn network review
reports the active turn's `turn_id` alongside the issuing turn's
`root_turn_id`.

GitOrigin-RevId: f0ae40ec4fc441891b4d843912f823a2385fa36f
This commit is contained in:
Eddie Chen
2026-11-25 06:19:21 +00:00
committed by copyberry
parent c265acf3b1
commit 1ed2a3f22d
13 changed files with 197 additions and 37 deletions
@@ -289,6 +289,7 @@ impl PendingHostApproval {
struct ActiveNetworkApprovalCall {
registration_id: String,
turn_id: String,
root_turn_id: Option<String>,
trigger: GuardianNetworkAccessTrigger,
tool_name: ToolName,
command: String,
@@ -307,6 +308,8 @@ enum ActiveNetworkApprovalAttribution {
struct NetworkRequestAttribution {
owner_call: Option<Arc<ActiveNetworkApprovalCall>>,
environment_id: Option<String>,
/// Only a verified execution tag establishes the action's root.
reviewed_action_root_turn_id: Option<String>,
}
#[derive(Default)]
@@ -600,6 +603,7 @@ impl NetworkApprovalService {
.clone()
.unwrap_or_else(|| call.environment_id.clone());
return (call.environment_id == environment_id).then_some(NetworkRequestAttribution {
reviewed_action_root_turn_id: call.root_turn_id.clone(),
owner_call: Some(call),
environment_id: Some(environment_id),
});
@@ -616,6 +620,7 @@ impl NetworkApprovalService {
return Some(NetworkRequestAttribution {
owner_call,
environment_id: Some(environment_id),
reviewed_action_root_turn_id: None,
});
}
@@ -623,12 +628,14 @@ impl NetworkApprovalService {
ActiveNetworkApprovalAttribution::None => Some(NetworkRequestAttribution {
owner_call: None,
environment_id: None,
reviewed_action_root_turn_id: None,
}),
ActiveNetworkApprovalAttribution::Single(call) => {
let environment_id = call.environment_id.clone();
Some(NetworkRequestAttribution {
owner_call: Some(call),
environment_id: Some(environment_id),
reviewed_action_root_turn_id: None,
})
}
ActiveNetworkApprovalAttribution::Ambiguous => None,
@@ -762,6 +769,7 @@ impl NetworkApprovalService {
let Some(NetworkRequestAttribution {
owner_call,
environment_id: active_environment_id,
reviewed_action_root_turn_id,
}) = self.resolve_request_attribution(&request).await
else {
return NetworkDecision::deny(REASON_NOT_ALLOWED);
@@ -847,6 +855,7 @@ impl NetworkApprovalService {
Arc::clone(&turn_context),
&settings,
review_environments,
reviewed_action_root_turn_id,
);
if !allows_network_approval_flow(review_context.approval_policy) {
if let Some(owner_call) = owner_call.as_ref() {
@@ -1277,6 +1286,7 @@ pub(crate) async fn begin_network_approval(
.register_call(ActiveNetworkApprovalCall {
registration_id: registration_id.clone(),
turn_id: turn.sub_id.clone(),
root_turn_id: turn.turn_metadata_state.root_turn_id(),
trigger,
tool_name,
command,
@@ -800,6 +800,7 @@ async fn register_call_with_default_shell_trigger(
let registration = service.register_call(ActiveNetworkApprovalCall {
registration_id: registration_id.to_string(),
turn_id: "turn-1".to_string(),
root_turn_id: None,
tool_name: ToolName::plain("exec_command"),
trigger: GuardianNetworkAccessTrigger {
call_id: registration_id.to_string(),
@@ -838,6 +839,7 @@ async fn active_call_preserves_triggering_command_context() {
let _registration = service.register_call(ActiveNetworkApprovalCall {
registration_id: "registration-1".to_string(),
turn_id: "turn-1".to_string(),
root_turn_id: Some("issuing-root".to_string()),
trigger: expected.clone(),
tool_name: tool_name.clone(),
command: "curl https://example.com".to_string(),
@@ -847,15 +849,48 @@ async fn active_call_preserves_triggering_command_context() {
cancellation_token: CancellationToken::new(),
});
let call = service
.resolve_single_active_call()
let request = NetworkPolicyRequest {
protocol: NetworkProtocol::Http,
host: "example.com".to_string(),
port: 80,
execution_id: Some("registration-1".to_string()),
environment_id: Some("remote".to_string()),
client_addr: None,
method: None,
command: None,
exec_policy_hint: None,
disconnect: None,
cancellation: None,
};
let attributed = service
.resolve_request_attribution(&request)
.await
.expect("single active call should resolve");
.expect("tagged request should resolve");
assert_eq!(
attributed.reviewed_action_root_turn_id.as_deref(),
Some("issuing-root")
);
let call = attributed.owner_call.expect("registered execution");
assert_eq!(&call.trigger, &expected);
assert_eq!(call.tool_name, tool_name);
assert_eq!(call.command, "curl https://example.com");
assert_eq!(call.environment_id, "remote");
let untagged = service
.resolve_request_attribution(&NetworkPolicyRequest {
execution_id: None,
..request
})
.await
.expect("untagged request can use current settings");
assert_eq!(
(
untagged.owner_call.is_some(),
untagged.reviewed_action_root_turn_id
),
(true, None)
);
}
#[tokio::test]