use rand::Rng;
use std::time::Duration;
use crate::pacing::BoundedNormalDelay;
use crate::sampling::standard_normal;
pub struct ReadingTimeEstimator {
wpm_mean: f64,
wpm_std: f64,
}
impl Default for ReadingTimeEstimator {
fn default() -> Self {
Self {
wpm_mean: 225.0,
wpm_std: 20.0,
}
}
}
impl ReadingTimeEstimator {
#[must_use]
pub fn new(wpm_mean: f64, wpm_std: f64) -> Self {
Self { wpm_mean, wpm_std }
}
#[must_use]
pub fn estimate(&self, text: &str) -> Duration {
let mut rng = rand::thread_rng();
let word_count = count_words(text).max(1) as f64;
let z = standard_normal(&mut rng);
let wpm = (self.wpm_mean + z * self.wpm_std).max(100.0);
let minutes = word_count / wpm;
let ms = (minutes * 60_000.0) as u64;
Duration::from_millis(ms.clamp(500, 120_000))
}
#[must_use]
pub fn estimate_words(&self, word_count: usize) -> Duration {
let words = "word ".repeat(word_count);
self.estimate(&words)
}
}
pub struct ActionDelay;
impl ActionDelay {
#[must_use]
pub fn before_click() -> Duration {
BoundedNormalDelay::from_unordered_bounds(200, 800).sample_thread()
}
#[must_use]
pub fn form_field_transition() -> Duration {
BoundedNormalDelay::from_unordered_bounds(300, 1_200).sample_thread()
}
#[must_use]
pub fn after_page_load() -> Duration {
BoundedNormalDelay::from_unordered_bounds(800, 3_000).sample_thread()
}
#[must_use]
pub fn micro() -> Duration {
BoundedNormalDelay::from_unordered_bounds(50, 200).sample_thread()
}
#[cfg(feature = "browser")]
pub async fn wait_before_click() {
tokio::time::sleep(Self::before_click()).await;
}
#[cfg(feature = "browser")]
pub async fn wait_form_transition() {
tokio::time::sleep(Self::form_field_transition()).await;
}
#[cfg(feature = "browser")]
pub async fn wait_after_load() {
tokio::time::sleep(Self::after_page_load()).await;
}
#[must_use]
pub fn hover_dwell() -> Duration {
BoundedNormalDelay::from_unordered_bounds(200, 700).sample_thread()
}
#[must_use]
pub fn idle() -> Duration {
BoundedNormalDelay::from_unordered_bounds(500, 2_000).sample_thread()
}
#[must_use]
pub fn between_actions() -> Duration {
BoundedNormalDelay::from_unordered_bounds(100, 400).sample_thread()
}
#[must_use]
pub fn uniform(min_ms: u64, max_ms: u64) -> Duration {
assert!(
min_ms <= max_ms,
"ActionDelay::uniform: min_ms ({min_ms}) > max_ms ({max_ms})"
);
let ms = if min_ms == max_ms {
min_ms
} else {
rand::thread_rng().gen_range(min_ms..=max_ms)
};
Duration::from_millis(ms)
}
#[cfg(feature = "browser")]
pub async fn wait_hover_dwell() {
tokio::time::sleep(Self::hover_dwell()).await;
}
#[cfg(feature = "browser")]
pub async fn wait_idle() {
tokio::time::sleep(Self::idle()).await;
}
#[cfg(feature = "browser")]
pub async fn wait_between_actions() {
tokio::time::sleep(Self::between_actions()).await;
}
#[cfg(feature = "browser")]
pub async fn wait_uniform(min_ms: u64, max_ms: u64) {
tokio::time::sleep(Self::uniform(min_ms, max_ms)).await;
}
}
#[must_use]
pub fn jittered_step<R: Rng + ?Sized>(nominal: Duration, spread: f64, rng: &mut R) -> Duration {
let nominal_ms = nominal.as_millis() as f64;
if nominal_ms <= 0.0 {
return Duration::from_millis(1);
}
let spread = spread.clamp(0.0, 0.95);
if spread == 0.0 {
return Duration::from_millis((nominal_ms.round() as u64).max(1));
}
let low = nominal_ms * (1.0 - spread);
let high = nominal_ms * (1.0 + spread);
let sampled = rng.gen_range(low..=high);
Duration::from_millis((sampled.round() as u64).max(1))
}
#[derive(Debug, Clone)]
pub struct SessionPacing {
actions_taken: u32,
next_idle_at: u32,
fatigue: f64,
burst_remaining: u32,
challenge_mode: bool,
}
impl Default for SessionPacing {
fn default() -> Self {
Self::new()
}
}
impl SessionPacing {
#[must_use]
pub fn new() -> Self {
let mut rng = rand::thread_rng();
Self {
actions_taken: 0,
next_idle_at: rng.gen_range(8..20),
fatigue: 1.0,
burst_remaining: rng.gen_range(3..8),
challenge_mode: false,
}
}
pub fn advance<R: Rng + ?Sized>(&mut self, rng: &mut R) -> Option<Duration> {
self.actions_taken += 1;
self.fatigue = 1.0 + 0.5 * (1.0 - (-(self.actions_taken as f64) / 60.0).exp());
let mut total_ms: u64 = 0;
if self.burst_remaining == 0 {
let base_ms = rng.gen_range(800_u64..3_000);
total_ms += (base_ms as f64 * self.fatigue) as u64;
self.burst_remaining = rng.gen_range(3..8);
} else {
self.burst_remaining -= 1;
}
if self.actions_taken >= self.next_idle_at {
let idle_ms = rng.gen_range(5_000_u64..15_000);
total_ms += (idle_ms as f64 * self.fatigue) as u64;
self.actions_taken = 0;
self.next_idle_at = rng.gen_range(8..20);
self.burst_remaining = rng.gen_range(3..8);
}
if self.challenge_mode {
total_ms = total_ms.saturating_mul(2);
}
(total_ms > 0).then(|| Duration::from_millis(total_ms))
}
#[cfg(feature = "browser")]
pub async fn tick(&mut self) {
use rand::SeedableRng;
let mut rng = rand::rngs::StdRng::from_entropy();
if let Some(delay) = self.advance(&mut rng) {
tokio::time::sleep(delay).await;
}
}
#[must_use]
pub fn fatigue(&self) -> f64 {
self.fatigue
}
#[must_use]
pub fn actions_taken(&self) -> u32 {
self.actions_taken
}
#[must_use]
pub fn scale_duration(&self, base: Duration) -> Duration {
Duration::from_millis((base.as_millis() as f64 * self.fatigue) as u64)
}
pub fn enter_challenge_mode(&mut self) {
self.challenge_mode = true;
}
pub fn exit_challenge_mode(&mut self) {
self.challenge_mode = false;
}
#[must_use]
pub fn is_challenge_mode(&self) -> bool {
self.challenge_mode
}
}
fn count_words(text: &str) -> usize {
text.split_whitespace().count()
}
#[cfg(test)]
#[path = "timing/tests.rs"]
mod tests;
#[cfg(test)]
#[path = "timing/plausibility.rs"]
mod human_plausibility_bounds;