use std::time::Duration;
use num_traits::ToPrimitive as _;
const FORK_HEADROOM: f64 = 0.9;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ContextPolicy {
pub handoff_reserve: usize,
pub compact_ceiling: usize,
pub start_distance: 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 count_distance: usize,
pub user_message_budget: usize,
}
impl Default for ContextPolicy {
fn default() -> Self {
Self {
handoff_reserve: 20_000,
compact_ceiling: 400_000,
start_distance: 50_000,
warn_distance: 80_000,
restore_budget: 0.15,
idle_compact_before_expiry: Duration::from_secs(6 * 60),
compaction_floor: 20_000,
count_distance: 30_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 start_point(&self, window: u32) -> usize {
self.pause_point(window).saturating_sub(self.start_distance)
}
#[must_use]
pub fn warn_point(&self, window: u32) -> usize {
self.pause_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 should_count_endpoint(&self, tokens: usize, window: u32) -> bool {
window > 0 && tokens >= self.warn_point(window).saturating_sub(self.count_distance)
}
#[must_use]
pub fn pause_point(&self, window: u32) -> usize {
usize::try_from(window)
.unwrap_or(usize::MAX)
.saturating_sub(self.handoff_reserve)
.min(self.compact_ceiling)
}
#[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.pause_point(window)) * FORK_HEADROOM;
to_f64(tokens) < limit
}
}
fn to_f64(tokens: usize) -> f64 {
tokens.to_f64().unwrap_or(f64::MAX)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_endpoint_count_is_gated_on_the_warn_point() {
let policy = ContextPolicy {
count_distance: 30_000,
..ContextPolicy::default()
};
assert!(!policy.should_count_endpoint(100_000, 1_000_000));
assert!(policy.should_count_endpoint(290_000, 1_000_000));
assert!(policy.should_count_endpoint(320_000, 1_000_000));
assert!(!policy.should_count_endpoint(1_000_000, 0));
}
#[test]
fn the_pause_point_is_the_lesser_of_window_minus_reserve_and_the_ceiling() {
let policy = ContextPolicy::default();
assert_eq!(policy.pause_point(1_000_000), 400_000);
assert_eq!(policy.pause_point(200_000), 180_000);
assert_eq!(policy.pause_point(500_000), 400_000);
assert_eq!(policy.pause_point(0), 0);
}
#[test]
fn the_reserve_and_the_ceiling_are_configurable() {
let policy = ContextPolicy {
handoff_reserve: 50_000,
compact_ceiling: 100_000,
..ContextPolicy::default()
};
assert_eq!(policy.pause_point(1_000_000), 100_000);
assert_eq!(policy.pause_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) < window as usize);
}
}
#[test]
fn the_start_and_warn_points_sit_their_distances_before_the_pause_point() {
let policy = ContextPolicy::default();
assert_eq!(policy.start_point(1_000_000), 350_000);
assert_eq!(policy.warn_point(1_000_000), 320_000);
assert_eq!(policy.start_point(200_000), 130_000);
assert_eq!(policy.warn_point(200_000), 100_000);
}
#[test]
fn thresholds_are_inclusive() {
let policy = ContextPolicy::default();
assert!(policy.should_compact(130_000, 200_000));
assert!(!policy.should_compact(129_999, 200_000));
assert!(policy.should_pause(180_000, 200_000));
assert!(!policy.should_pause(179_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(161_999, 200_000));
assert!(!policy.allows_fork(162_000, 200_000));
assert!(!policy.allows_fork(175_000, 200_000));
}
#[test]
fn a_window_no_larger_than_the_reserve_is_not_admitted() {
let policy = ContextPolicy::default();
let smallest = u32::try_from(policy.handoff_reserve).expect("fits u32");
assert!(!policy.admits(smallest));
assert!(policy.admits(smallest + 1));
}
}