Files
codex/codex-rs/guardian-context/src/retained_instructions.rs
T
felixxia-oai aa12ab45df Recover missing Guardian root instructions in acceptance order (#43472)
## Why

Incomplete retained checkpoints can omit root user instructions that still survive in live history. Queued input can also reach model history after a later-accepted answer, so recording order cannot reliably order grants and restrictions for subagent authorization reviews.

## What changed

- Reconcile retained evidence with surviving local user messages using source identity and persisted acceptance order, including answers present only in the checkpoint.
- Preserve checkpoint gaps and mark evidence incomplete when recovered instructions lack an order or conflict with an existing order.
- Restore the input-order counter from surviving local metadata so new instructions sort after recovered evidence, even without a retained checkpoint.

## Testing

Add unit coverage for source matching, acceptance ordering, persistent gaps, conflicting orders, and counter restoration. Extend subagent authorization tests to cover checkpoint resume, queued approvals, missing sources, and a subsequent revocation.

GitOrigin-RevId: 8bfbfd2c797d725796187e5cecf7f3f11a5f3380
2026-09-07 13:40:29 +00:00

71 lines
2.8 KiB
Rust

//! Selects original user instructions independently of model compaction and transcript selection.
//! The host owns storage and lifecycle. Omitted records remain explicit; restrictions
//! are never truncated into partial permissions, and retained source order is preserved.
//! Section omissions do not change fast-approval eligibility.
use codex_history::RetainedContext;
use codex_history::RetainedContextEntry;
use codex_protocol::protocol::TruncationPolicy;
use crate::ContextSection;
use crate::GuardianRootMessage;
use crate::SectionContributor;
use crate::SectionError;
use crate::SectionInput;
use crate::SectionScope;
const MAX_INSTRUCTION_TOKENS: usize = 900;
/// Renders bounded originals even when transcript selection also includes their source messages.
/// Presence in parent history alone cannot prove complete delivery to a reviewer.
fn render_retained_instructions(context: &RetainedContext) -> Vec<String> {
let mut complete = context.user_messages_complete();
let mut fragments = Vec::new();
for (order, (_, entry)) in context.ordered_entries().enumerate() {
let RetainedContextEntry::UserMessage(message) = entry else {
continue;
};
let text = format!(
"Retained source order: {order}\n{}",
GuardianRootMessage::User(message.text.clone()).render()
);
if message.complete
&& text.len() <= TruncationPolicy::Tokens(MAX_INSTRUCTION_TOKENS).byte_budget()
{
fragments.push(text);
} else {
complete = false;
}
}
if !complete {
fragments.insert(/*index*/ 0, "Host notice: some retained user instructions are unavailable within the evidence budget. Do not treat remaining grants as complete authorization.\n".to_owned());
}
fragments
}
pub(crate) struct RetainedUserInstructionsSection;
impl SectionContributor for RetainedUserInstructionsSection {
fn scope(&self) -> SectionScope {
SectionScope::Shared
}
fn contribute(&self, input: &SectionInput<'_>) -> Result<Option<ContextSection>, SectionError> {
let Some(context) = input.history.retained_context() else {
return Ok(None);
};
let rendered = render_retained_instructions(context);
if rendered.is_empty() {
return Ok(None);
}
let mut items = vec![">>> RETAINED USER INSTRUCTIONS START\nHost: Retained source order labels across instructions and verified answers reflect original acceptance, not section order. Later instructions may revoke earlier grants.\n".to_owned()];
items.extend(rendered);
items.push(">>> RETAINED USER INSTRUCTIONS END\n".to_owned());
Ok(Some(ContextSection::RetainedUserInstructions { items }))
}
}
#[cfg(test)]
#[path = "retained_instructions_tests.rs"]
mod tests;