use std::time::Duration;
use num_traits::ToPrimitive as _;
const FORK_HEADROOM: f64 = 0.9;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ContextPolicy {
pub compact_at: f32,
pub compact_ceiling: usize,
pub start_distance: usize,
pub safe_buffer: usize,
pub warn_distance: usize,
pub restore_budget: f32,
pub idle_compact_before_expiry: Duration,
pub compaction_floor: usize,
pub spill_threshold: usize,
pub spill_head: usize,
pub user_message_budget: usize,
}
impl Default for ContextPolicy {
fn default() -> Self {
Self {
compact_at: 0.8,
compact_ceiling: 400_000,
start_distance: 50_000,
safe_buffer: 5_000,
warn_distance: 80_000,
restore_budget: 0.15,
idle_compact_before_expiry: Duration::from_secs(6 * 60),
compaction_floor: 20_000,
spill_threshold: 4_000,
spill_head: 200,
user_message_budget: 20_000,
}
}
}
impl ContextPolicy {
#[must_use]
pub const fn below_floor(&self, tokens: usize) -> bool {
tokens < self.compaction_floor
}
#[must_use]
pub fn compact_point(&self, window: u32) -> usize {
let fraction = f64::from(window) * f64::from(self.compact_at.clamp(0.0, 1.0));
to_tokens(fraction).min(self.compact_ceiling)
}
#[must_use]
pub fn start_point(&self, window: u32) -> usize {
self.compact_point(window)
.saturating_sub(self.start_distance)
}
#[must_use]
pub fn warn_point(&self, window: u32) -> usize {
self.compact_point(window)
.saturating_sub(self.warn_distance)
}
#[must_use]
pub fn should_warn(&self, tokens: usize, window: u32) -> bool {
window > 0 && tokens >= self.warn_point(window)
}
#[must_use]
pub fn pause_point(&self, window: u32) -> usize {
self.compact_point(window).saturating_sub(self.safe_buffer)
}
#[must_use]
pub fn admits(&self, window: u32) -> bool {
self.pause_point(window) > 0
}
#[must_use]
pub fn should_compact(&self, tokens: usize, window: u32) -> bool {
window > 0 && tokens >= self.start_point(window)
}
#[must_use]
pub fn should_pause(&self, tokens: usize, window: u32) -> bool {
window > 0 && tokens >= self.pause_point(window)
}
#[must_use]
pub fn allows_fork(&self, tokens: usize, window: u32) -> bool {
let limit = to_f64(self.compact_point(window)) * FORK_HEADROOM;
to_f64(tokens) < limit
}
}
fn to_f64(tokens: usize) -> f64 {
tokens.to_f64().unwrap_or(f64::MAX)
}
fn to_tokens(value: f64) -> usize {
value.floor().to_usize().unwrap_or(usize::MAX)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_auto_compact_point_is_the_lesser_of_the_fraction_and_the_ceiling() {
let policy = ContextPolicy::default();
assert_eq!(policy.compact_point(1_000_000), 400_000);
assert_eq!(policy.compact_point(200_000), 160_000);
assert_eq!(policy.compact_point(500_000), 400_000);
assert_eq!(policy.compact_point(0), 0);
}
#[test]
fn the_fraction_and_the_ceiling_are_configurable() {
let policy = ContextPolicy {
compact_at: 0.5,
compact_ceiling: 100_000,
..ContextPolicy::default()
};
assert_eq!(policy.compact_point(1_000_000), 100_000);
assert_eq!(policy.compact_point(100_000), 50_000);
}
#[test]
fn default_points_are_ordered() {
let policy = ContextPolicy::default();
for window in [100_000, 200_000, 1_000_000] {
assert!(policy.warn_point(window) < policy.start_point(window));
assert!(policy.start_point(window) < policy.pause_point(window));
assert!(policy.pause_point(window) < policy.compact_point(window));
}
assert!(policy.restore_budget < policy.compact_at);
}
#[test]
fn the_start_and_pause_points_sit_a_fixed_distance_before_the_compact_point() {
let policy = ContextPolicy::default();
assert_eq!(policy.start_point(1_000_000), 350_000);
assert_eq!(policy.pause_point(1_000_000), 395_000);
assert_eq!(policy.start_point(200_000), 110_000);
assert_eq!(policy.pause_point(200_000), 155_000);
}
#[test]
fn thresholds_are_inclusive() {
let policy = ContextPolicy::default();
assert!(policy.should_compact(110_000, 200_000));
assert!(!policy.should_compact(109_999, 200_000));
assert!(policy.should_pause(155_000, 200_000));
assert!(!policy.should_pause(154_999, 200_000));
}
#[test]
fn a_window_of_unknown_size_never_crosses_a_threshold() {
let policy = ContextPolicy::default();
assert!(!policy.should_compact(1_000_000, 0));
assert!(!policy.should_pause(1_000_000, 0));
}
#[test]
fn the_floor_defaults_to_twenty_thousand_tokens_and_is_exclusive_below() {
let policy = ContextPolicy::default();
assert!(policy.below_floor(19_999));
assert!(!policy.below_floor(20_000));
}
#[test]
fn a_fork_is_refused_within_a_tenth_of_the_compaction_point() {
let policy = ContextPolicy::default();
assert!(policy.allows_fork(143_000, 200_000));
assert!(!policy.allows_fork(144_000, 200_000));
assert!(!policy.allows_fork(155_000, 200_000));
}
}