Add aggregate budget enforcement for Guardian context (#44166)

## What changed

Add `ComposedContext::enforce_budget` to fit evidence within the input token allowance after reserving existing context. Preserve required content and message boundaries, reserve an omission notice, and return an error if required evidence cannot fit.

Carry retention policies through transcript rendering and composition. Protect user messages, protected messages, and the newest five tool entries. Remove oversized optional items first, then evict commentary, older tool evidence, and images in priority order. Add image admission support and record omitted content in truncation observations.

## Testing

Add tests for existing-context reservations, required-message preservation, image omission, framing costs, and protection of the newest five tool entries in both context profiles.

GitOrigin-RevId: 8a0f245bad82f562323b4533442385ddbcb6e541
This commit is contained in:
felixxia-oai authored and copyberry committed 2026-09-09 14:55:36 +00:00
1 parent d3ffbbed5a
commit 17e64839eb
13 files changed
+666 -76

No files matched your search

+30 -2
View File
@@ -4,12 +4,15 @@
use codex_protocol::protocol::TruncationPolicy;
use crate::BudgetPriority;
use crate::Budgeted;
use crate::ContextTarget;
use crate::ConversationTranscriptConfig;
use crate::ConversationTranscriptEntry;
use crate::ConversationTranscriptEntryKind;
use crate::ConversationTranscriptOptions;
use crate::RenderedTranscript;
use crate::Retention;
use crate::TranscriptEntryLimits;
use crate::TranscriptRetentionConfig;
use crate::TruncationObservation;
@@ -19,6 +22,8 @@ use crate::select_user_messages;
use self::window::TranscriptWindow;
mod window;
const MIN_RECENT_TOOL_ENTRIES: usize = 5;
/// Request-local policy resolved from the consumer's model and configuration.
/// Registry scope follows `target`; source flags and caps apply before retention.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
@@ -198,7 +203,7 @@ impl ContextProfile {
&& included.iter().any(|included| !included))
.then(|| "Some conversation entries were omitted.".to_owned());
let mut truncations = Vec::new();
let items = entries
let mut items: Vec<_> = entries
.into_iter()
.enumerate()
.filter_map(|(index, entry)| {
@@ -221,9 +226,32 @@ impl ContextProfile {
retained_bytes,
});
}
included[index].then_some(entry.text)
if included[index] {
Some(match entry.kind {
TranscriptEntryKind::User | TranscriptEntryKind::ProtectedMessage => {
Budgeted::required(entry.text)
}
TranscriptEntryKind::Message => {
Budgeted::optional(entry.text, BudgetPriority::Commentary)
}
TranscriptEntryKind::Tool => {
Budgeted::optional(entry.text, BudgetPriority::Tool)
}
})
} else {
None
}
})
.collect();
// Keep the newest tool evidence even when the aggregate allowance is tight.
for item in items
.iter_mut()
.rev()
.filter(|item| item.retention == Retention::Optional(BudgetPriority::Tool))
.take(MIN_RECENT_TOOL_ENTRIES)
{
item.retention = Retention::Required;
}
RenderedTranscript {
items,
omission_note,