mirror of
https://github.com/openai/codex.git
synced 2026-11-26 23:40:43 +00:00
Use measured durations to budget uploaded audio (#59268)
## Why Uploaded audio always reserved 12,000 tokens per file, even for short clips. This could discard recordings during compaction that would fit within the retained-message budget. ## What changed - Add optional audio duration metadata to attachments and cache measured durations per session, resolving metadata for new input and replayed history. - Use measured durations for history estimates and compaction budgets at 10 tokens per second, rounded up and capped at 12,000 tokens. Keep the full budget for missing or unusable metadata. - Bound metadata lookups to 250 milliseconds per batch with up to eight concurrent requests, and record accepted input before lookup so interruption preserves the batch. ## Testing Add coverage for duration rounding and fallback estimates, mixed measured and unknown audio, retention through compaction and cold resume, and interruption during metadata lookup. GitOrigin-RevId: 26fe320c7024944d9d4c371d5fdcda84442b5cc5
This commit is contained in:
@@ -194,3 +194,19 @@ fn preparation_errors_map_to_expected_placeholders() {
|
||||
assert_eq!(error.placeholder(), expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_audio_uses_the_measured_length_or_the_full_file_budget() {
|
||||
use std::time::Duration;
|
||||
for (duration, expected) in [
|
||||
(None, 12_000),
|
||||
(Some(Duration::ZERO), 12_000),
|
||||
(Some(Duration::from_nanos(1)), 1),
|
||||
(Some(Duration::from_secs(2)), 20),
|
||||
(Some(Duration::from_millis(2_001)), 21),
|
||||
(Some(Duration::from_secs(20 * 60)), 12_000),
|
||||
(Some(Duration::MAX), 12_000),
|
||||
] {
|
||||
assert_eq!(super::estimate_file_audio_tokens(duration), expected);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ const UNSUPPORTED_AUDIO_FORMAT_PLACEHOLDER: &str =
|
||||
|
||||
const MAX_PROMPT_AUDIO_BASE64_BYTES: usize = MAX_PROMPT_AUDIO_INPUT_BYTES.div_ceil(3) * 4;
|
||||
const AUDIO_TOKEN_ESTIMATE_CACHE_SIZE: usize = 32;
|
||||
const AUDIO_TOKENS_PER_SECOND: f64 = 10.0;
|
||||
const AUDIO_TOKENS_PER_SECOND: u32 = 10;
|
||||
|
||||
static AUDIO_TOKEN_ESTIMATE_CACHE: LazyLock<BlockingLruCache<[u8; 20], Arc<OnceLock<usize>>>> =
|
||||
LazyLock::new(|| {
|
||||
@@ -147,6 +147,19 @@ fn canonical_audio_mime(mime: &str) -> Option<&'static str> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Budget one file reference at the inline audio rate. Missing or unusable metadata
|
||||
/// reserves the full 20 minutes accepted by Responses. Admission belongs to Responses.
|
||||
pub fn estimate_file_audio_tokens(duration: Option<std::time::Duration>) -> usize {
|
||||
const MAX_DURATION: std::time::Duration = std::time::Duration::from_secs(20 * 60);
|
||||
let duration = duration
|
||||
.filter(|value| !value.is_zero())
|
||||
.unwrap_or(MAX_DURATION)
|
||||
.min(MAX_DURATION);
|
||||
let tokens =
|
||||
(duration.as_nanos() * u128::from(AUDIO_TOKENS_PER_SECOND)).div_ceil(1_000_000_000);
|
||||
usize::try_from(tokens).unwrap_or(usize::MAX)
|
||||
}
|
||||
|
||||
/// Estimates audio tokens from decoded duration, falling back to the data URL size.
|
||||
pub fn estimate_audio_token_count(audio_url: &str) -> usize {
|
||||
let key = sha1_digest(audio_url.as_bytes());
|
||||
@@ -160,7 +173,7 @@ pub fn estimate_audio_token_count_without_cache(audio_url: &str) -> usize {
|
||||
let Some(duration_seconds) = audio_duration_seconds(audio_url) else {
|
||||
return approx_token_count(audio_url);
|
||||
};
|
||||
let token_count = (duration_seconds * AUDIO_TOKENS_PER_SECOND).ceil();
|
||||
let token_count = (duration_seconds * f64::from(AUDIO_TOKENS_PER_SECOND)).ceil();
|
||||
if token_count >= usize::MAX as f64 {
|
||||
usize::MAX
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user