use super::ResolvedCitationCompactionBudget;
#[must_use]
pub(crate) fn compaction_window_safety_tokens(available_tokens: u64) -> u64 {
const PERCENT: u64 = 8;
const MIN_TOKENS: u64 = 128;
const MAX_TOKENS: u64 = 1_024;
(available_tokens / PERCENT).clamp(MIN_TOKENS, MAX_TOKENS)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct CompactionReasoningReserve {
percent: u64,
floor_scale: u64,
}
impl CompactionReasoningReserve {
pub(crate) const INITIAL: Self = Self {
percent: 25,
floor_scale: 1,
};
const MAX_PERCENT: u64 = 100;
const MAX_FLOOR_SCALE: u64 = 4;
const FLOOR_WINDOW_PERCENT: u64 = 22;
const MAX_FLOOR_TOKENS: u64 = 65_536;
#[must_use]
pub(crate) fn degraded(self) -> Self {
Self {
percent: (self.percent * 2).min(Self::MAX_PERCENT),
floor_scale: (self.floor_scale * 2).min(Self::MAX_FLOOR_SCALE),
}
}
#[must_use]
pub(crate) const fn percent(self) -> u64 {
self.percent
}
#[must_use]
fn floor(self, compactor_window_tokens: u64, _text_budget_tokens: u64) -> u64 {
let window_share = compactor_window_tokens.saturating_mul(Self::FLOOR_WINDOW_PERCENT) / 100;
window_share
.min(Self::MAX_FLOOR_TOKENS)
.saturating_mul(self.floor_scale)
}
#[must_use]
fn reasoning_allowance(
self,
compactor_window_tokens: u64,
text_budget_tokens: u64,
input_tokens: u64,
) -> u64 {
input_tokens
.saturating_mul(self.percent)
.saturating_div(100)
.max(self.floor(compactor_window_tokens, text_budget_tokens))
}
#[must_use]
pub(crate) fn output_ceiling(
self,
resolved_budget: ResolvedCitationCompactionBudget,
compactor_window_tokens: u64,
input_tokens: u64,
) -> u64 {
let text_budget_tokens = resolved_budget.output_token_limit();
text_budget_tokens.saturating_add(self.reasoning_allowance(
compactor_window_tokens,
text_budget_tokens,
input_tokens,
))
}
#[must_use]
pub(crate) fn allowed_input_tokens(
self,
compactor_window_tokens: u64,
text_budget_tokens: u64,
) -> u64 {
let usable_tokens = compactor_window_tokens.saturating_sub(text_budget_tokens);
let floor = self.floor(compactor_window_tokens, text_budget_tokens);
let by_percent = usable_tokens.saturating_mul(100) / (100 + self.percent);
if by_percent.saturating_mul(self.percent) / 100 >= floor {
by_percent
} else {
usable_tokens.saturating_sub(floor)
}
}
}
const COMPACTION_REFIT_SAFETY_PERCENT: u64 = 5;
const COMPACTION_REFIT_PROGRESS_STEPS: u64 = 8;
#[must_use]
pub(crate) fn tightened_covered_budget(
covered_payload_tokens: u64,
estimated_input_tokens: u64,
allowed_input_tokens: u64,
) -> Option<u64> {
if covered_payload_tokens == 0 {
return None;
}
let excess_input_tokens = estimated_input_tokens.saturating_sub(allowed_input_tokens);
let step = if excess_input_tokens == 0 {
covered_payload_tokens
.div_ceil(COMPACTION_REFIT_PROGRESS_STEPS)
.max(1)
} else {
let safety = excess_input_tokens.saturating_mul(COMPACTION_REFIT_SAFETY_PERCENT) / 100;
excess_input_tokens.saturating_add(safety).max(1)
};
let tightened = covered_payload_tokens.saturating_sub(step);
(tightened < covered_payload_tokens).then_some(tightened)
}