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

@@ -136,6 +136,9 @@ impl CollectedContext {
ContextSection::RootConversation { items } => {
(4, "root_conversation", text_content(items))
}
ContextSection::SenderUserMessages { items } => {
(4, "sender_user_messages", text_content(items))
}
ContextSection::RetainedUserInstructions { items } => {
(5, "retained_user_instructions", text_content(items))
}
@@ -116,3 +116,78 @@ fn long_text_and_file_image_delivery_is_lossless_bounded_and_fully_budgeted() {
});
assert_eq!(context.into_user_inputs().unwrap(), expected_inputs);
}
#[test]
fn sender_restrictions_survive_budget_trimming_for_both_reviewers() {
for presentation in [
ContextPresentation::SyncFull {
session_id: "receiver",
},
ContextPresentation::SyncDelta {
session_id: "receiver",
},
ContextPresentation::Async,
] {
let sender = vec!["user: Only use the staging pool.\n".to_owned()];
let context = CollectedContext {
sections: vec![
ContextSection::SenderUserMessages {
items: sender.clone(),
},
ContextSection::ConversationTranscript { items: Vec::new() },
],
}
.compose(
presentation,
RenderedTranscript {
items: vec![Budgeted::optional(
"old tool output ".repeat(/*n*/ 2_000),
BudgetPriority::Tool,
)],
omission_note: None,
truncations: Vec::new(),
},
)
.unwrap();
let selected = context
.clone()
.enforce_budget(
crate::RequestBudget {
max_input_tokens: 1_000,
existing_context_tokens: 0,
},
"evidence omitted".to_owned(),
crate::HistoryTruncation::Allow,
)
.unwrap();
assert!(selected.estimated_tokens() <= 1_000);
assert_eq!(selected.truncations.len(), 1);
let sender_section = selected
.sections
.into_iter()
.find(|section| section.id == "sender_user_messages")
.unwrap();
let SectionDelivery::UserContent(items) = sender_section.delivery else {
panic!("sender context must remain user evidence");
};
assert_eq!(
items,
vec![Budgeted::required(ContentItem::InputText {
text: sender[0].clone()
})]
);
assert!(matches!(
context.enforce_budget(
crate::RequestBudget {
max_input_tokens: 1,
existing_context_tokens: 0
},
"evidence omitted".to_owned(),
crate::HistoryTruncation::Allow,
),
Err(SectionError::EvidenceLimitExceeded {
section: "request_budget"
})
));
}
}
+3
View File
@@ -18,6 +18,7 @@ use codex_protocol::models::ResponseItem;
use authorization::RootConversationSection;
use authorization::TrustedUserAnswersSection;
use retained_instructions::RetainedUserInstructionsSection;
use sender_user_messages::SenderUserMessagesSection;
use transcript::ConversationTranscriptSection;
pub use action::ActionPresentation;
@@ -44,6 +45,7 @@ pub use verified_answers::render_verified_answer;
pub use verified_answers::render_verified_answers;
mod retained_instructions;
mod sender_user_messages;
mod action;
mod enforcement;
@@ -254,6 +256,7 @@ pub fn default_registry() -> &'static SectionRegistry {
registry.register(trusted_tool::TrustedToolSection);
registry.register(trusted_skills::TrustedSkillsSection);
registry.register(RootConversationSection);
registry.register(SenderUserMessagesSection);
registry.register(RetainedUserInstructionsSection);
registry.register(TrustedUserAnswersSection);
registry.register(ConversationTranscriptSection);
+1
View File
@@ -21,6 +21,7 @@ pub enum ContextSection<T = ConversationTranscriptEntry> {
RootConversation { items: Vec<String> },
TrustedUserAnswers { items: Vec<String> },
RetainedUserInstructions { items: Vec<String> },
SenderUserMessages { items: Vec<String> },
PlannedAction(PlannedAction),
PreviousReviews(PreviousReviews),
TrustedTool(TrustedTool),
@@ -0,0 +1,27 @@
//! Both reviewers consume the same host-rendered, delivery-bound sender evidence.
use codex_history::RetainedContext;
use crate::ContextSection;
use crate::SectionContributor;
use crate::SectionError;
use crate::SectionInput;
use crate::SectionScope;
pub(crate) struct SenderUserMessagesSection;
impl SectionContributor for SenderUserMessagesSection {
fn scope(&self) -> SectionScope {
SectionScope::Shared
}
fn contribute(&self, input: &SectionInput<'_>) -> Result<Option<ContextSection>, SectionError> {
Ok(input
.history
.retained_context()
.and_then(RetainedContext::sender_user_messages)
.map(|snapshot| ContextSection::SenderUserMessages {
items: vec![snapshot.text.clone()],
}))
}
}