Handle pending network reviews after process completion (#42746)

## Why

A remote process can finish while a network policy review is still pending. Normal process cleanup should withdraw that review without turning the completed command into a review failure or losing its output.

## What changed

- Record whether a network policy request was withdrawn because the process finished, was cancelled, lost its executor connection, or timed out.
- Treat normal process completion as cleanup while retaining fail-closed behavior for other cancellation causes.
- Preserve explicit network denials before policy persistence so cleanup cannot replace the reported call outcome.

## Testing

Add an integration test that completes a remote process during a pending network review and verifies that the command reports its successful exit and output without approving the withdrawn request.

GitOrigin-RevId: 7f42d75631ee29eba43bf04cc953eea490f553fc
This commit is contained in:
jif authored and copyberry committed 2026-09-04 12:08:02 +00:00
1 parent 4e48cd02da
commit 8e85265c39
9 files changed
+430 -11

No files matched your search

+40 -5
View File
@@ -17,6 +17,8 @@ use codex_network_proxy::NetworkPolicyDecider;
use codex_network_proxy::NetworkPolicyRequest;
use codex_network_proxy::NetworkProtocol;
use codex_network_proxy::NetworkProxy;
use codex_network_proxy::NetworkRequestCancellation;
use codex_network_proxy::NetworkRequestCancellationReason;
use codex_network_proxy::NetworkRequestDisconnect;
use codex_protocol::approvals::NetworkApprovalContext;
use codex_protocol::approvals::NetworkApprovalProtocol;
@@ -280,6 +282,7 @@ struct PendingHostApprovalOwner<'a> {
pending: Arc<PendingHostApproval>,
execution_cancellation: Option<CancellationToken>,
disconnect: Option<NetworkRequestDisconnect>,
cancellation: Option<NetworkRequestCancellation>,
decision_on_drop: PendingApprovalDecision,
completed: bool,
}
@@ -297,6 +300,7 @@ impl<'a> PendingHostApprovalOwner<'a> {
pending,
execution_cancellation,
disconnect: None,
cancellation: None,
decision_on_drop: PendingApprovalDecision::Deny,
completed: false,
}
@@ -342,6 +346,17 @@ impl<'a> PendingHostApprovalOwner<'a> {
impl Drop for PendingHostApprovalOwner<'_> {
fn drop(&mut self) {
if !self.completed {
if self
.cancellation
.as_ref()
.and_then(NetworkRequestCancellation::reason)
== Some(NetworkRequestCancellationReason::ProcessFinished)
{
// Normal process cleanup is not a new review failure. Any earlier
// denial is already recorded in the call outcome.
self.publish_and_remove(self.decision_on_drop);
return;
}
if matches!(self.decision_on_drop, PendingApprovalDecision::Deny)
&& let Some(registration_id) = self.key.execution_id.as_deref()
&& let Some(elapsed) = self
@@ -517,20 +532,27 @@ impl NetworkApprovalService {
}
fn record_call_outcome(&self, registration_id: &str, outcome: String) {
if let Some(cancellation_token) = self.store_call_outcome(registration_id, outcome) {
cancellation_token.cancel();
}
}
fn store_call_outcome(
&self,
registration_id: &str,
outcome: String,
) -> Option<CancellationToken> {
let mut calls = self
.calls
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let Some(call) = calls.active_calls.get(registration_id).cloned() else {
return;
};
let call = calls.active_calls.get(registration_id)?.clone();
// Explicit network-review outcomes replace generic blocked-request fallbacks.
calls
.call_outcomes
.insert(registration_id.to_string(), outcome);
drop(calls);
call.cancellation_token.cancel();
Some(call.cancellation_token.clone())
}
fn record_call_outcome_if_absent(&self, registration_id: &str, outcome: String) {
@@ -676,6 +698,7 @@ impl NetworkApprovalService {
.map(|call| call.cancellation_token.clone()),
);
pending_owner.disconnect = request.disconnect.clone();
pending_owner.cancellation = request.cancellation.clone();
let permission_profile = owner_call
.as_ref()
@@ -814,6 +837,18 @@ impl NetworkApprovalService {
}
};
if matches!(
&approval_decision,
ReviewDecision::NetworkPolicyAmendment { network_policy_amendment }
if network_policy_amendment.action == NetworkPolicyRuleAction::Deny
) && let Some(owner_call) = owner_call.as_ref()
{
// Preserve the denial before waiting for the policy commit. Defer
// cancellation until after saving so it cannot interrupt persistence.
let _ = self
.store_call_outcome(&owner_call.registration_id, "rejected by user".to_string());
}
let _session_policy_commit_guard = if matches!(
&approval_decision,
ReviewDecision::Approved