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

+14 -4
View File
@@ -1,4 +1,5 @@
use codex_extension_api::ConversationHistorySnapshot;
use codex_guardian_context::Budgeted;
use codex_guardian_context::CollectedContext;
use codex_guardian_context::ContextPresentation;
use codex_guardian_context::ContextProfile;
@@ -208,7 +209,9 @@ pub(crate) async fn build_guardian_prompt_items_with_parent_turn(
let profile = ContextProfile::synchronous();
let mut transcript = profile.render_transcript(transcript_entries, offset);
if transcript_entries.is_empty() {
transcript.items.push(placeholder.to_owned());
transcript
.items
.push(Budgeted::required(placeholder.to_owned()));
}
let context = sections.compose(presentation, transcript)?;
for (section, cost) in context.section_costs() {
@@ -267,11 +270,18 @@ pub(crate) fn render_guardian_transcript_entries(
let mut transcript =
ContextProfile::synchronous().render_transcript(entries, /*entry_number_offset*/ 0);
if entries.is_empty() {
transcript.items.push(Budgeted::required(
"<no retained transcript entries>".to_owned(),
));
}
(
transcript
.items
.push("<no retained transcript entries>".to_owned());
}
(transcript.items, transcript.omission_note)
.into_iter()
.map(|item| item.content)
.collect(),
transcript.omission_note,
)
}
/// Retains the human-readable conversation plus recent tool call / result