mirror of
https://github.com/openai/codex.git
synced 2026-11-16 22:00:36 +00:00
## 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
96 lines
3.1 KiB
Rust
96 lines
3.1 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 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_url: "data:image/png;base64,AAAA".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: 1
|
|
}
|
|
),
|
|
(
|
|
"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_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);
|
|
}
|
|
|
|
#[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]));
|
|
}
|
|
}
|
|
}
|