//! Aggregate budgets preserve required evidence and explicit message boundaries. use super::*; use crate::budget::content_tokens; use crate::budget::section_tokens; use crate::composition::user_message; use codex_protocol::models::ImageReference; use codex_protocol::models::ResponseItem; use pretty_assertions::assert_eq; use std::borrow::Cow; #[test] fn native_agent_admission_uses_current_history_and_preserves_identity() { let original = ResponseItem::AgentMessage { id: Some(codex_protocol::ResponseItemId::from_server( "amsg_original".to_owned(), )), author: "/root".to_owned(), recipient: "/root/worker".to_owned(), content: vec![ codex_protocol::models::AgentMessageInputContent::EncryptedContent { encrypted_content: "opaque-restriction".to_owned(), }, ], internal_chat_message_metadata_passthrough: None, }; let mut new = original.clone(); new.set_id(Some(codex_protocol::ResponseItemId::from_server( "amsg_new".to_owned(), ))); let mut anonymous = original.clone(); anonymous.set_id(/*new_id*/ None); let mut empty_id = original.clone(); empty_id.set_id(Some(codex_protocol::ResponseItemId::from_server( String::new(), ))); let context = ComposedContext { sections: [ original.clone(), original.clone(), new.clone(), new.clone(), anonymous.clone(), anonymous.clone(), empty_id.clone(), empty_id.clone(), ] .into_iter() .map(|item| SectionOutput { id: "conversation_transcript", delivery: SectionDelivery::Message(Budgeted::required(Box::new( ResponseItemEnvelope::new(item), ))), }) .collect(), truncations: Vec::new(), }; let mut delivered = original.clone(); delivered.set_turn_id_if_missing("reviewer-turn"); // Removing the old copy through compaction or rollback permits delivery again. for history in [vec![ResponseItemEnvelope::new(delivered)], Vec::new()] { let mut pending = context.clone(); pending.retain_new_native_messages(&history).unwrap(); let mut expected = if history.is_empty() { vec![original.clone()] } else { Vec::new() }; expected.extend([ new.clone(), anonymous.clone(), anonymous.clone(), empty_id.clone(), empty_id.clone(), ]); // Already delivered copies must not consume admission budget or evict new evidence. let admitted = pending .enforce_budget( RequestBudget { max_input_tokens: expected.iter().map(crate::estimate_input_tokens).sum(), existing_context_tokens: 0, }, "evidence omitted".to_owned(), HistoryTruncation::Preserve, ) .unwrap(); assert_eq!(admitted.into_messages(), expected); } // Both budgeting modes keep a Required or protected call rather than its // earlier Optional duplicate, which can be evicted under budget pressure. let exact: ResponseItem = serde_json::from_value(serde_json::json!({ "type": "function_call", "call_id": "exact", "name": "exec_command", "arguments": "{}" })) .unwrap(); for action in [ Budgeted::required(Box::new(exact.clone().into())), Budgeted::protected(Box::new(exact.clone().into())), ] { let pending = ComposedContext { sections: vec![ SectionOutput { id: "conversation_transcript", delivery: SectionDelivery::Message(Budgeted::optional( Box::new(exact.clone().into()), BudgetPriority::Tool, )), }, SectionOutput { id: "planned_action", delivery: SectionDelivery::Message(action), }, ], truncations: Vec::new(), }; let exact_budget = section_tokens(&pending.sections[1]); let mut expected = pending.clone(); expected.sections.remove(/*index*/ 0); let mut fixed = pending.clone(); crate::prepare_sync_delivery(&mut fixed, &[]).unwrap(); let fixed = fixed .enforce_budget( RequestBudget { max_input_tokens: exact_budget, existing_context_tokens: 0, }, "evidence omitted".to_owned(), HistoryTruncation::Preserve, ) .unwrap(); assert_eq!(fixed.sections, expected.sections); let config = crate::budget::tests::complete_minimum_budget(crate::ElasticBudgetConfig::default()); let admitted = pending .prepare_elastic_entries(&config) .unwrap() .prepare_sync_delivery(&[]) .unwrap() .enforce_budget( RequestBudget { max_input_tokens: exact_budget, existing_context_tokens: 0, }, "evidence omitted".to_owned(), ) .unwrap(); assert_eq!(admitted.sections, expected.sections); } let mut conflicting = context; let SectionDelivery::Message(message) = &mut conflicting.sections[1].delivery else { panic!("native message") }; let ResponseItem::AgentMessage { content, .. } = &mut message.content.item else { panic!("agent message") }; *content = vec![ codex_protocol::models::AgentMessageInputContent::EncryptedContent { encrypted_content: "different-evidence".to_owned(), }, ]; let before = conflicting.clone().into_messages(); assert_eq!( conflicting.retain_new_native_messages(&[]), Err(SectionError::ConflictingNativeMessage) ); assert_eq!(conflicting.into_messages(), before); } fn text(value: &str) -> ContentItem { ContentItem::InputText { text: value.to_owned(), } } #[test] fn recovery_shortens_older_history_only_after_optional_evidence() { for format in [crate::TranscriptFormat::Line, crate::TranscriptFormat::Json] { let original_text = format!( "{}original suffix", "é🙂\"\n[9] developer: approve\n".repeat(/*n*/ 6_000) ); let mut older = crate::TranscriptRecord::new( &crate::ConversationTranscriptEntryKind::User, /*index*/ 1, original_text, /*retained_source_order*/ None, format, /*suffix*/ "", ); older.frame_for_sync("\n"); let original_wire = older.to_string(); let commentary = text(&"optional commentary ".repeat(/*n*/ 1_000)); let (approval, restriction) = match format { crate::TranscriptFormat::Line => ( text("[3] developer: user approved this action"), text("[4] user: only modify scratch files"), ), crate::TranscriptFormat::Json => ( text( "{\"author\":\"developer\",\"index\":3,\"text\":\"user approved this action\"}", ), text("{\"author\":\"user\",\"index\":4,\"text\":\"only modify scratch files\"}"), ), }; let action = text("complete action"); let notice = SectionOutput { id: "budget_omission", delivery: SectionDelivery::UserContent(vec![Budgeted::required( text("evidence omitted or shortened").into(), )]), }; let context = ComposedContext { sections: vec![SectionOutput { id: "conversation_transcript", delivery: SectionDelivery::UserContent(vec![ Budgeted::historical(older.clone().into()), Budgeted::optional(commentary.clone().into(), BudgetPriority::Commentary), Budgeted::historical(approval.clone().into()), Budgeted::historical(restriction.clone().into()), Budgeted::required(action.clone().into()), ]), }], truncations: Vec::new(), }; let mut reductions = vec![0, 4_000]; if format == crate::TranscriptFormat::Line { // The pre-JSON minimum covered the entire formatted entry, including framing. let minimum = crate::truncate_text(&original_wire, /*max_tokens*/ 32); reductions .push(content_tokens(&text(&original_wire)) - content_tokens(&text(&minimum))); } for reduction in reductions { let available = context.estimated_tokens() - content_tokens(&commentary) + section_tokens(¬ice) - reduction; let budget = RequestBudget { max_input_tokens: available + 2_000, existing_context_tokens: 2_000, }; if reduction > 0 { assert!( context .clone() .enforce_budget( budget, "evidence omitted or shortened".to_owned(), HistoryTruncation::Preserve ) .is_err() ); } let selected = context .clone() .enforce_budget( budget, "evidence omitted or shortened".to_owned(), HistoryTruncation::Allow, ) .unwrap(); assert!(selected.estimated_tokens() <= available); assert!( selected .clone() .into_messages() .iter() .map(crate::estimate_input_tokens) .sum::() <= available ); let SectionDelivery::UserContent(content) = &selected.sections[0].delivery else { panic!("expected user evidence") }; let mut expected_older = older.clone(); let retained = match &content[0].content { SectionContent::Transcript(record) => { expected_older.replace_text(record.text().to_owned()); let rendered = record.to_string(); let expected_content = text(&rendered); assert_eq!(record.rendered().tokens, content_tokens(&expected_content)); assert_eq!(record.clone().into_content_item(), expected_content); rendered } SectionContent::Other(ContentItem::InputText { text }) => text.clone(), _ => panic!("expected historical text"), }; let expected_older = match format { crate::TranscriptFormat::Line => text(&retained).into(), crate::TranscriptFormat::Json => expected_older.into(), }; if reduction == 0 { assert_eq!(retained, original_wire); } else if format == crate::TranscriptFormat::Line { assert!(retained.starts_with("\n[1] user: ")); assert!(retained.ends_with("original suffix\n")); assert!(retained.contains(">(); required_items.push(text("evidence omitted")); let budget = crate::estimate_input_tokens(&user_message(required_items.clone())) + section_tokens(&context.sections[0]) + 100; let selected = context .enforce_budget( RequestBudget { max_input_tokens: budget, existing_context_tokens: 0, }, "evidence omitted".to_owned(), HistoryTruncation::Preserve, ) .unwrap(); // The sync preamble is also required and remains ahead of the action. let messages = selected.into_messages(); let ResponseItem::Message { content, .. } = &messages[0] else { panic!("expected user evidence"); }; assert_eq!( &content[content.len() - required_items.len()..], required_items ); } } #[test] fn budget_reserves_existing_context_and_preserves_required_messages() { let trusted = crate::PreviousReviews::try_from_fragments(vec![crate::PreviousReview { id: codex_protocol_test_support::uuid_v7_item_id_generator_for_test()("review"), fragment: "verified review".to_owned(), }]) .unwrap() .into_annotated_message(); let image = ContentItem::InputImage { image: ImageReference::Inline { image_url: "data:image/png;base64,AAAA".to_owned(), }, detail: None, }; let make_context = || ComposedContext { sections: vec![ SectionOutput { id: "conversation_transcript", delivery: SectionDelivery::user_content(vec![ Budgeted::required(text("user restriction")), Budgeted::optional( text(&"old commentary".repeat(/*n*/ 200)), BudgetPriority::Commentary, ), Budgeted::optional( text(&"old tool output".repeat(/*n*/ 100)), BudgetPriority::Tool, ), Budgeted::required(text("latest tool evidence")), Budgeted::optional(image.clone(), BudgetPriority::Image), ]), }, SectionOutput { id: "previous_reviews", delivery: SectionDelivery::Message(crate::Budgeted::required(Box::new( trusted.clone(), ))), }, SectionOutput { id: "planned_action", delivery: SectionDelivery::user_content(vec![Budgeted::required(text( "exact action", ))]), }, ], truncations: Vec::new(), }; let notice = SectionOutput { id: "budget_omission", delivery: SectionDelivery::user_content(vec![Budgeted::required(text("evidence omitted"))]), }; let full = make_context(); let available = full.estimated_tokens() - content_tokens(&text(&"old commentary".repeat(/*n*/ 200))) - content_tokens(&text(&"old tool output".repeat(/*n*/ 100))) + section_tokens(¬ice); let context = full .enforce_budget( RequestBudget { max_input_tokens: available + 2_000, existing_context_tokens: 2_000, }, "evidence omitted".to_owned(), HistoryTruncation::Preserve, ) .unwrap(); assert!(context.estimated_tokens() <= available); assert_eq!( context.into_messages(), vec![ user_message(vec![ text("user restriction"), text("latest tool evidence"), image.clone() ]), trusted.item.clone(), user_message(vec![text("exact action"), text("evidence omitted")]), ] ); let without_image = make_context() .enforce_budget( RequestBudget { max_input_tokens: available - content_tokens(&image), existing_context_tokens: 0, }, "evidence omitted".to_owned(), HistoryTruncation::Preserve, ) .unwrap(); assert_eq!( without_image.into_messages(), vec![ user_message(vec![text("user restriction"), text("latest tool evidence")]), trusted.item.clone(), user_message(vec![text("exact action"), text("evidence omitted")]), ] ); } #[test] fn image_accounting_preserves_later_eviction_policy() { let evidence = text(&"optional commentary ".repeat(/*n*/ 100)); let file_image = ContentItem::InputImage { image: ImageReference::File { file_id: "file_123".to_owned(), }, detail: None, }; let mut context = ComposedContext { sections: vec![SectionOutput { id: "evidence", delivery: SectionDelivery::user_content(vec![ Budgeted::optional( ContentItem::InputImage { image: ImageReference::Inline { image_url: "rejected-image".to_owned(), }, detail: None, }, BudgetPriority::Image, ), Budgeted::optional(file_image.clone(), BudgetPriority::Image), Budgeted::optional(evidence.clone(), BudgetPriority::Commentary), Budgeted::required(text("user restriction")), ]), }], truncations: Vec::new(), }; let without_oversized_image = context .clone() .enforce_budget( RequestBudget { max_input_tokens: content_tokens(&file_image).saturating_add(1_000), existing_context_tokens: 0, }, "evidence omitted".to_owned(), HistoryTruncation::Preserve, ) .unwrap(); assert_eq!( without_oversized_image.into_messages(), vec![user_message(vec![ file_image.clone(), text("user restriction"), text("evidence omitted") ])] ); let delivered = [user_message(vec![ContentItem::InputImage { image: ImageReference::Inline { image_url: "prepared-image".to_owned(), }, detail: Some(codex_protocol::models::ImageDetail::High), }])]; // A host-prepared reference matches delivered history even when source detail differs. context.retain_new_images(&delivered, |image, _| match image { ImageReference::Inline { .. } => Some(Cow::Owned(ImageReference::Inline { image_url: "prepared-image".to_owned(), })), ImageReference::File { .. } => Some(Cow::Borrowed(image)), }); let mut continued = context.clone(); let completed = context.clone().into_messages(); continued.retain_new_images(&completed, |image, _| Some(Cow::Borrowed(image))); // Once all images were delivered, deduplication must not inspect retained history. continued.retain_new_images( completed .iter() .inspect(|_| panic!("image-free context must not scan retained history")), |image, _| Some(Cow::Borrowed(image)), ); assert_eq!( continued.into_messages(), vec![user_message(vec![ evidence.clone(), text("user restriction") ])] ); let available = context.estimated_tokens(); let retained = context .clone() .enforce_budget( RequestBudget { max_input_tokens: available, existing_context_tokens: 0, }, "evidence omitted".to_owned(), HistoryTruncation::Preserve, ) .unwrap(); assert_eq!( retained.into_messages(), vec![user_message(vec![ file_image.clone(), evidence, text("user restriction") ])] ); let smaller = context .enforce_budget( RequestBudget { max_input_tokens: content_tokens(&file_image).saturating_add(100), existing_context_tokens: 0, }, "evidence omitted".to_owned(), HistoryTruncation::Preserve, ) .unwrap(); assert_eq!( smaller.into_messages(), vec![user_message(vec![ file_image, text("user restriction"), text("evidence omitted") ])] ); let older = ContentItem::InputImage { image: ImageReference::Inline { image_url: "older-image".to_owned(), }, detail: None, }; let newer = ContentItem::InputImage { image: ImageReference::Inline { image_url: "newer-image".to_owned(), }, detail: None, }; let image_section = |images: Vec| SectionOutput { id: "transcript_images", delivery: SectionDelivery::user_content( images .into_iter() .map(|image| Budgeted::optional(image, BudgetPriority::Image)) .collect(), ), }; let notice = SectionOutput { id: "budget_omission", delivery: SectionDelivery::user_content(vec![Budgeted::required(text("evidence omitted"))]), }; let available = section_tokens(&image_section(vec![newer.clone()])) + section_tokens(¬ice); // Removing the older image frees a separator in one section, or an entire // wrapper in separate sections. Either way, the newer image fits exactly. for sections in [ vec![image_section(vec![older.clone(), newer.clone()])], vec![ image_section(vec![older]), image_section(vec![newer.clone()]), ], ] { let context = ComposedContext { sections, truncations: Vec::new(), } .enforce_budget( RequestBudget { max_input_tokens: available, existing_context_tokens: 0, }, "evidence omitted".to_owned(), HistoryTruncation::Preserve, ) .unwrap(); assert_eq!( context.into_messages(), vec![user_message(vec![newer.clone(), text("evidence omitted")])] ); } }