Files
codex/codex-rs/guardian-context/src/budget_tests.rs
T
Krish Chainani b974893c90 Account for file images in context budgets and Guardian reviews (#46072)
## Why

File images contributed zero to history and compaction token estimates and were dropped from Guardian context. This undercounted context usage and omitted image evidence from reviews.

## What changed

- Estimate file images using the fixed resized-image cost, or 10,000 patch tokens for `original` detail because file references do not expose dimensions.
- Charge file images against remote compaction and Guardian request budgets.
- Preserve file images from user messages and tool outputs through Guardian selection and delivery, and deduplicate review images by file ID.
- Apply transcript image count and byte limits to both reference types, counting file ID bytes rather than referenced file contents.

## Testing

Add coverage for file-image estimates, selection limits, delivery, and compaction retention. Extend Guardian tests to verify file-image evidence reaches reviews and uploaded original-detail images trigger reviewer-history compaction.

GitOrigin-RevId: ee024204dcbe16e277fea9347c6a4d7fc120e18d
2026-09-17 01:38:13 +00:00

115 lines
3.8 KiB
Rust

//! Costs distinguish evidence payloads from complete model request estimates.
use super::*;
use crate::Budgeted;
use crate::composition::SectionOutput;
use crate::composition::user_message as message;
use codex_protocol::models::ImageReference;
use pretty_assertions::assert_eq;
#[test]
fn section_costs_keep_multimodal_payloads_separate() {
let context = ComposedContext {
sections: vec![
SectionOutput {
id: "transcript",
delivery: SectionDelivery::UserContent(vec![
Budgeted::required(ContentItem::InputText {
text: "évidence".to_owned(),
}),
Budgeted::required(ContentItem::InputImage {
image: ImageReference::Inline {
image_url: "data:image/png;base64,AAAA".to_owned(),
},
detail: None,
}),
Budgeted::required(ContentItem::InputImage {
image: ImageReference::File {
file_id: "file_123".to_owned(),
},
detail: None,
}),
]),
},
SectionOutput {
id: "trusted",
delivery: SectionDelivery::Message(Box::new(message(vec![
ContentItem::InputText {
text: "verified".to_owned(),
},
]))),
},
],
truncations: Vec::new(),
};
assert_eq!(
context.section_costs().collect::<Vec<_>>(),
vec![
(
"transcript",
SectionCost {
text_bytes: "évidence".len(),
image_bytes: "data:image/png;base64,AAAA".len(),
image_count: 2
}
),
(
"trusted",
SectionCost {
text_bytes: "verified".len(),
..SectionCost::default()
}
),
]
);
}
#[test]
fn request_estimate_reserves_images_independently_of_encoded_size() {
let image = |payload: &str| {
message(vec![ContentItem::InputImage {
image: ImageReference::Inline {
image_url: format!("data:image/png;base64,{payload}"),
},
detail: None,
}])
};
let short = estimate_input_tokens(&image("AAAA"));
assert_eq!(short, estimate_input_tokens(&image(&"A".repeat(200_000))));
assert!(short >= IMAGE_TOKEN_RESERVATION);
let file = message(vec![ContentItem::InputImage {
image: ImageReference::File {
file_id: "file_123".to_owned(),
},
detail: Some(codex_protocol::models::ImageDetail::Original),
}]);
assert!(estimate_input_tokens(&file) >= IMAGE_TOKEN_RESERVATION);
}
#[test]
fn section_estimate_bounds_the_delivered_message() {
// Individually rounded costs do not always leave room for JSON separators.
for text in ["x", "quoted \"text\"", "évidence"] {
for count in 1..=40 {
let context = ComposedContext {
sections: vec![SectionOutput {
id: "transcript",
delivery: SectionDelivery::UserContent(vec![
Budgeted::required(
ContentItem::InputText {
text: text.to_owned(),
}
);
count
]),
}],
truncations: Vec::new(),
};
let estimate = context.estimated_tokens();
let delivered = context.into_messages();
assert!(estimate >= estimate_input_tokens(&delivered[0]));
}
}
}