Include sender user messages in Guardian delegation reviews (#46179)

## Why

A delegated task can omit restrictions from the sender's user messages. Guardian needs that context when reviewing actions in the receiving thread.

## What changed

- When thread-owned Guardian context is enabled, capture up to three recent local user messages when a `send_message_to_thread` delivery from `codex_app` or `codex_tui` is accepted.
- Provide a bounded snapshot to synchronous and asynchronous reviewers, with explicit notices for unavailable evidence and a reminder that historical context does not transfer permission. Keep the snapshot out of the worker prompt.
- Retain snapshots with their deliveries across replay, compaction, and rollback, and strip them from forked agent history.

## Testing

Add coverage for reviewer-only visibility, missing sender provenance, context-budget preservation, checkpoint replay and rollback, fork isolation, and input serialization that rejects harness metadata.

GitOrigin-RevId: 3870ba762aa81689713b56d4cb3b98566bffed88
This commit is contained in:
jif authored and copyberry committed 2026-09-17 10:02:38 +00:00
1 parent b0659c5386
commit e269f2164c
22 files changed
+666 -19

No files matched your search

+7
View File
@@ -77,6 +77,10 @@ pub struct CodexHarnessMetadata {
/// Copied parent context stays model-visible but must not become child-local authorization.
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub inherited_user_message: bool,
/// Sender context captured by the host when this task message was accepted.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub sender_user_messages: Option<Box<SenderUserMessages>>,
}
impl ResponseItemEnvelope {
@@ -175,6 +179,9 @@ impl JsonSchema for RolloutItem {
mod guardian_history;
mod reconciled_retained_context;
mod retained_context;
mod sender_user_messages;
pub use sender_user_messages::SenderUserMessages;
pub use reconciled_retained_context::ReconciledRetainedContext;
pub use retained_context::RetainedContext;
+50
View File
@@ -11,6 +11,7 @@ use serde::Serialize;
use crate::CodexHarnessMetadata;
use crate::ResponseItemEnvelope;
use crate::SenderUserMessages;
const MAX_FAMILY_RECORDS: usize = 8;
const MAX_RECORD_BYTES: usize = 16_384;
@@ -149,6 +150,9 @@ pub struct RetainedContext {
user_messages_incomplete: bool,
#[serde(default)]
next_order: u64,
/// Delivery snapshots share local input order, so later steers can be undone independently.
#[serde(default, skip_serializing_if = "VecDeque::is_empty")]
sender_deliveries: VecDeque<Ordered<SenderUserMessages>>,
}
fn legacy_user_messages_incomplete() -> bool {
@@ -202,6 +206,35 @@ impl RetainedContextEvent {
}
impl RetainedContext {
/// Context for the latest delivery still present in this task's retained history.
pub fn sender_user_messages(&self) -> Option<&SenderUserMessages> {
self.sender_deliveries.back().map(|entry| &entry.value)
}
/// Retains host metadata with the delivery's acceptance order, including during replay.
pub fn record_sender_user_messages(&mut self, metadata: &CodexHarnessMetadata) -> bool {
let Some(messages) = metadata.sender_user_messages.as_deref() else {
return false;
};
if self
.sender_deliveries
.iter()
.any(|entry| entry.value.receiver_message_id == messages.receiver_message_id)
{
return false;
}
let mut messages = messages.clone();
messages.bound();
let order = self.record_order(metadata.user_input_order);
self.sender_deliveries.push_back(Ordered {
inherited: false,
order,
value: messages,
});
bound_family(&mut self.sender_deliveries, /*incomplete*/ &mut false);
true
}
/// Reserves order without retaining pending input that hooks may reject or cancel.
pub fn reserve_order(&mut self) -> u64 {
let order = self.next_order;
@@ -396,6 +429,17 @@ impl RetainedContext {
self.next_order = self.next_order.max(entry.order.saturating_add(1));
}
}
for entry in &mut self.sender_deliveries {
entry.value.bound();
self.next_order = self.next_order.max(entry.order.saturating_add(1));
}
bound_family(&mut self.sender_deliveries, /*incomplete*/ &mut false);
for metadata in surviving_items
.iter()
.filter_map(|item| item.metadata.as_ref())
{
self.record_sender_user_messages(metadata);
}
for order in surviving_items
.iter()
.filter_map(|item| item.metadata.as_ref())
@@ -435,6 +479,8 @@ impl RetainedContext {
.map(Ordered::key)
})
}) {
self.sender_deliveries
.retain(|entry| entry.key() < boundary);
self.verified_answers.retain(|entry| entry.key() < boundary);
self.user_messages.retain(|entry| entry.key() < boundary);
return;
@@ -448,6 +494,10 @@ impl RetainedContext {
source != RetainedInputSource::Inherited
&& !turn_ids.contains(&answer.value.turn_id.as_str())
});
self.sender_deliveries.retain(|entry| {
source != RetainedInputSource::Inherited
&& !turn_ids.contains(&entry.value.receiver_turn_id.as_str())
});
self.user_messages.retain(|message| {
(source != RetainedInputSource::Inherited || message.inherited)
&& !turn_ids.contains(&message.value.turn_id.as_str())
@@ -0,0 +1,36 @@
//! Bounded, host-rendered sender evidence attached to a delivered task message.
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
const MAX_CONTEXT_BYTES: usize = 3_600;
const MAX_ID_BYTES: usize = 128;
/// Rendered evidence and the receiver identities needed for replay and rollback.
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct SenderUserMessages {
pub receiver_turn_id: String,
pub receiver_message_id: String,
pub text: String,
}
impl SenderUserMessages {
/// Restored metadata follows the same hard bound as live, reviewer-only fragments.
pub fn bound(&mut self) {
if self.text.len() > MAX_CONTEXT_BYTES {
self.text = "Host: Sender context exceeds the evidence budget. Do not infer permission from missing evidence.\n".to_owned();
}
for id in [&mut self.receiver_turn_id, &mut self.receiver_message_id] {
id.truncate(id.floor_char_boundary(MAX_ID_BYTES));
}
}
}
impl std::fmt::Debug for SenderUserMessages {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("SenderUserMessages")
.finish_non_exhaustive()
}
}