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 {