From 84b853b217c6d43cef0b79391bd7d80f26680de3 Mon Sep 17 00:00:00 2001 From: felixxia-oai Date: Tue, 1 Dec 2026 19:02:05 +0000 Subject: [PATCH] Allow larger text entries in Guardian context budgets (#65071) ## Why Guardian context delivery can split a text entry across multiple bounded parts. Capping each entry at the size of one part prevented configurations from allocating more room to a single entry, even when the section budget could accommodate it. ## What changed Allow `max_section_entry_tokens` to exceed the per-part delivery limit when it fits within `min_total_section_tokens`. ## Testing Exercise elastic budgeting with a large transcript tool entry and a file image. Verify that the delivered text parts respect the part limit and reassemble into the original text, that the image is preserved, and that the budget estimate covers the delivered message. GitOrigin-RevId: a303180bc90dce1a21fc91cd12e2eded90eaad8e --- codex-rs/guardian-context/src/budget.rs | 13 ++------ codex-rs/guardian-context/src/budget_tests.rs | 2 +- .../guardian-context/src/composition_tests.rs | 30 ++++++++++++++++--- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/codex-rs/guardian-context/src/budget.rs b/codex-rs/guardian-context/src/budget.rs index d8ca78d6d8..5c8dabe843 100644 --- a/codex-rs/guardian-context/src/budget.rs +++ b/codex-rs/guardian-context/src/budget.rs @@ -228,7 +228,7 @@ impl ElasticBudgetConfig { /// capacity is shared between sections up to each maximum, with both priorities eligible. /// Preparation must cap text entries once, including framing and omission markers, /// before measuring demand. Entries that fit keep their original representation. -/// Entry caps cannot exceed the existing text-part delivery allowance. +/// A logical entry can span multiple bounded text parts at delivery. /// Exact actions use protected global space. Opaque native payloads and images stay /// whole and use section/global space rather than the text entry cap. /// Effective section minimums stay fixed and must sum to DEFAULT_MAX_INPUT_TOKENS. @@ -246,7 +246,7 @@ pub struct ElasticSectionLimits { } impl ElasticSectionLimits { - /// Reject empty quotas, oversized entries and minimums that cannot hold one capped entry. + /// A section must reserve room for at least one capped entry, which can span text parts. pub fn new( min_total_section_tokens: usize, max_section_entry_tokens: usize, @@ -257,9 +257,6 @@ impl ElasticSectionLimits { if max_section_entry_tokens > min_total_section_tokens { return Err(ElasticSectionLimitsError::EntryExceedsMinimum); } - if max_section_entry_tokens > crate::composition::MAX_TEXT_PART_ESTIMATED_TOKENS { - return Err(ElasticSectionLimitsError::EntryExceedsDeliveryLimit); - } Ok(Self { min_total_section_tokens, max_section_entry_tokens, @@ -326,7 +323,6 @@ impl<'de> serde::Deserialize<'de> for ElasticSectionLimits { pub enum ElasticSectionLimitsError { ZeroTokens, EntryExceedsMinimum, - EntryExceedsDeliveryLimit, MaximumBelowMinimum, } @@ -340,11 +336,6 @@ impl std::fmt::Display for ElasticSectionLimitsError { .write_str("min_total_section_tokens must be at least max_section_entry_tokens"), Self::MaximumBelowMinimum => formatter .write_str("max_total_section_tokens must be at least min_total_section_tokens"), - Self::EntryExceedsDeliveryLimit => write!( - formatter, - "max_section_entry_tokens must not exceed {}", - crate::composition::MAX_TEXT_PART_ESTIMATED_TOKENS - ), } } } diff --git a/codex-rs/guardian-context/src/budget_tests.rs b/codex-rs/guardian-context/src/budget_tests.rs index cdcb76250c..cb84dcca34 100644 --- a/codex-rs/guardian-context/src/budget_tests.rs +++ b/codex-rs/guardian-context/src/budget_tests.rs @@ -249,7 +249,7 @@ fn elastic_section_config_validates_entry_limits() { }); let parsed = serde_json::from_value::(config.clone()).unwrap(); assert_eq!(serde_json::to_value(parsed).unwrap(), config); - for (minimum, entry_cap) in [(0, cap), (cap, 0), (cap - 1, cap), (cap + 1, cap + 1)] { + for (minimum, entry_cap) in [(0, cap), (cap, 0), (cap - 1, cap)] { let mut invalid = config.clone(); invalid["sections"]["transcript_tools"]["min_total_section_tokens"] = minimum.into(); invalid["sections"]["transcript_tools"]["max_section_entry_tokens"] = entry_cap.into(); diff --git a/codex-rs/guardian-context/src/composition_tests.rs b/codex-rs/guardian-context/src/composition_tests.rs index 7d6ac2ca7d..207dd175c2 100644 --- a/codex-rs/guardian-context/src/composition_tests.rs +++ b/codex-rs/guardian-context/src/composition_tests.rs @@ -187,16 +187,19 @@ fn delivery_preserves_arbitrary_message_boundaries_and_rejects_them_for_sync() { } #[test] -fn long_text_and_file_image_delivery_is_lossless_bounded_and_fully_budgeted() { - let text = "é🙂\"\n".repeat(/*n*/ 20_000); +fn elastic_tool_text_and_file_image_delivery_is_lossless_bounded_and_fully_budgeted() { + let text = "é🙂\"\n".repeat(60 * 1024 / 8); let file_image = ImageReference::File { file_id: "file_123".to_owned(), }; let context = ComposedContext { sections: vec![SectionOutput { - id: "planned_action", + id: "conversation_transcript", delivery: SectionDelivery::user_content(vec![ - Budgeted::required(ContentItem::InputText { text: text.clone() }), + Budgeted::optional( + ContentItem::InputText { text: text.clone() }, + BudgetPriority::Tool, + ), Budgeted::required(ContentItem::InputImage { image: file_image.clone(), detail: Some(ImageDetail::High), @@ -205,6 +208,25 @@ fn long_text_and_file_image_delivery_is_lossless_bounded_and_fully_budgeted() { }], ..Default::default() }; + let limits = crate::ElasticSectionLimits::new( + /*min_total_section_tokens*/ 24_000, /*max_section_entry_tokens*/ 24_000, + ) + .unwrap(); + let config = crate::budget::tests::complete_minimum_budget(crate::ElasticBudgetConfig { + sections: [(crate::ElasticSection::TranscriptTools, limits)].into(), + ..Default::default() + }); + let context = context + .prepare_elastic_entries(&config) + .unwrap() + .enforce_budget( + crate::RequestBudget { + max_input_tokens: crate::DEFAULT_MAX_INPUT_TOKENS, + existing_context_tokens: 0, + }, + "Evidence omitted.".into(), + ) + .unwrap(); let estimated = context.estimated_tokens(); let messages = context.clone().into_messages(); let ResponseItem::Message { content, .. } = &messages[0] else {