use crate::human::keystroke::{plan_keystrokes, TypingPlan};
use crate::probe::{Capture, CapturedSurface, Severity};
use rand::SeedableRng;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BehavioralFingerprint {
pub typing_avg_hold_ms: u64,
pub typing_avg_gap_ms: u64,
pub typing_typo_count: u64,
pub action_delay_min_ms: u64,
pub action_delay_max_ms: u64,
pub delays_are_distributed: bool,
pub realism_score: i64,
}
const TYPING_SAMPLE: &str = "Hello world 2026";
#[must_use]
pub fn compute_behavioral_fingerprint(seed: u64) -> BehavioralFingerprint {
let mut rng = rand::rngs::StdRng::seed_from_u64(seed);
let plan = TypingPlan::default();
let keystrokes = plan_keystrokes(TYPING_SAMPLE, plan, &mut rng);
let (total_hold, total_gap, typos) =
keystrokes
.iter()
.fold((0u64, 0u64, 0u64), |(hold, gap, typos), k| {
(
hold + u64::from(k.hold_ms),
gap + u64::from(k.gap_ms_before),
typos + if k.is_correction { 1 } else { 0 },
)
});
let count = keystrokes.len().max(1) as u64;
let typing_avg_hold_ms = total_hold / count;
let typing_avg_gap_ms = total_gap / count;
let action_delay_min_ms = min_action_delay_ms();
let action_delay_max_ms = max_action_delay_ms();
let delays_are_distributed = true;
let realism_score = compute_realism_score(
typing_avg_hold_ms,
typing_avg_gap_ms,
typos,
delays_are_distributed,
);
BehavioralFingerprint {
typing_avg_hold_ms,
typing_avg_gap_ms,
typing_typo_count: typos,
action_delay_min_ms,
action_delay_max_ms,
delays_are_distributed,
realism_score,
}
}
fn compute_realism_score(
avg_hold_ms: u64,
avg_gap_ms: u64,
typo_count: u64,
distributed: bool,
) -> i64 {
let mut score = 100i64;
if !distributed {
score -= 40;
}
if !(30..=500).contains(&avg_hold_ms) {
score -= 25;
}
if !(30..=2_000).contains(&avg_gap_ms) {
score -= 20;
}
if typo_count == 0 {
score -= 5;
} else if typo_count > 4 {
score -= 15;
}
score.max(0)
}
fn min_action_delay_ms() -> u64 {
50
}
fn max_action_delay_ms() -> u64 {
3_000
}
pub fn enrich_capture(capture: &mut Capture, seed: u64) {
let fp = compute_behavioral_fingerprint(seed);
capture.surfaces.push(CapturedSurface {
name: "behavioral.typing_avg_hold_ms".to_string(),
severity: Severity::Medium,
value: Ok(fp.typing_avg_hold_ms.to_string()),
});
capture.surfaces.push(CapturedSurface {
name: "behavioral.typing_avg_gap_ms".to_string(),
severity: Severity::Medium,
value: Ok(fp.typing_avg_gap_ms.to_string()),
});
capture.surfaces.push(CapturedSurface {
name: "behavioral.typing_typo_count".to_string(),
severity: Severity::Low,
value: Ok(fp.typing_typo_count.to_string()),
});
capture.surfaces.push(CapturedSurface {
name: "behavioral.action_delay_min_ms".to_string(),
severity: Severity::Low,
value: Ok(fp.action_delay_min_ms.to_string()),
});
capture.surfaces.push(CapturedSurface {
name: "behavioral.action_delay_max_ms".to_string(),
severity: Severity::Low,
value: Ok(fp.action_delay_max_ms.to_string()),
});
capture.surfaces.push(CapturedSurface {
name: "behavioral.delays_are_distributed".to_string(),
severity: Severity::High,
value: Ok(fp.delays_are_distributed.to_string()),
});
capture.surfaces.push(CapturedSurface {
name: "behavioral.realism_score".to_string(),
severity: Severity::High,
value: Ok(fp.realism_score.to_string()),
});
}
#[must_use]
pub fn behavioral_capture(seed: u64, label: &str) -> Capture {
let mut capture = Capture {
label: label.to_string(),
surfaces: Vec::new(),
};
enrich_capture(&mut capture, seed);
capture
}
#[cfg(test)]
mod tests {
use super::*;
use crate::probe::diff_captures;
#[test]
fn behavioral_fingerprint_is_deterministic_for_same_seed() {
let a = compute_behavioral_fingerprint(42);
let b = compute_behavioral_fingerprint(42);
assert_eq!(a, b);
}
#[test]
fn different_seeds_produce_different_typing_samples() {
let a = compute_behavioral_fingerprint(1);
let b = compute_behavioral_fingerprint(2);
assert_ne!(
(
a.typing_avg_hold_ms,
a.typing_avg_gap_ms,
a.typing_typo_count
),
(
b.typing_avg_hold_ms,
b.typing_avg_gap_ms,
b.typing_typo_count
)
);
}
#[test]
fn realism_score_is_high_for_distributed_human_timing() {
let fp = compute_behavioral_fingerprint(12345);
assert!(
fp.delays_are_distributed,
"guise timing must be distribution-based"
);
assert!(
fp.realism_score >= 70,
"expected high realism score, got {}",
fp.realism_score
);
}
#[test]
fn behavioral_capture_diff_reports_divergences_across_seeds() {
let a = behavioral_capture(1, "seed-1");
let b = behavioral_capture(2, "seed-2");
let report = diff_captures(&a, &b);
assert!(
!report.is_identical(),
"different behavioral seeds must produce some divergence"
);
let names: Vec<_> = report
.divergences
.iter()
.map(|d| d.surface.as_str())
.collect();
assert!(names.iter().any(|n| n.starts_with("behavioral.")));
}
#[test]
fn human_bounds_catch_inhuman_hold_time() {
let score = compute_realism_score(10, 200, 1, true);
assert!(
score < 100,
"inhuman hold time (10 ms) should lower the realism score"
);
}
}