#[derive(Debug, Clone, Copy)]
pub struct PragmaticConfig {
pub threshold: f32,
pub urgency_override_level: f32,
pub emit_on_first: bool,
}
impl Default for PragmaticConfig {
fn default() -> Self {
Self { threshold: 0.05, urgency_override_level: 0.8, emit_on_first: true }
}
}
impl PragmaticConfig {
pub const fn conservative() -> Self {
Self { threshold: 0.10, urgency_override_level: 0.9, emit_on_first: true }
}
pub const fn sensitive() -> Self {
Self { threshold: 0.01, urgency_override_level: 0.5, emit_on_first: true }
}
}
pub struct PragmaticGate<const S: usize> {
history: [f32; S],
head: usize,
count: usize,
config: PragmaticConfig,
total_samples: u64,
total_emitted: u64,
last_emitted: f32,
}
impl<const S: usize> PragmaticGate<S> {
pub const fn new(config: PragmaticConfig) -> Self {
Self {
history: [0.0; S],
head: 0,
count: 0,
config,
total_samples: 0,
total_emitted: 0,
last_emitted: -1.0, }
}
pub const fn default_gate() -> Self {
Self::new(PragmaticConfig { threshold: 0.05, urgency_override_level: 0.8, emit_on_first: true })
}
pub fn should_emit(&mut self, grammar_entropy: f32, urgency: f32) -> bool {
self.total_samples += 1;
self.history[self.head] = grammar_entropy;
self.head = (self.head + 1) % S;
if self.count < S { self.count += 1; }
if urgency >= self.config.urgency_override_level {
self.total_emitted += 1;
self.last_emitted = grammar_entropy;
return true;
}
if self.config.emit_on_first && self.last_emitted < 0.0 {
self.total_emitted += 1;
self.last_emitted = grammar_entropy;
return true;
}
let delta = (grammar_entropy - self.last_emitted).abs();
if delta >= self.config.threshold {
self.total_emitted += 1;
self.last_emitted = grammar_entropy;
return true;
}
false
}
pub fn backplane_efficiency(&self) -> f32 {
if self.total_samples == 0 { return 1.0; }
1.0 - (self.total_emitted as f32 / self.total_samples as f32)
}
pub fn total_samples(&self) -> u64 { self.total_samples }
pub fn total_emitted(&self) -> u64 { self.total_emitted }
pub fn mean_entropy(&self) -> f32 {
if self.count == 0 { return 0.0; }
let sum: f32 = self.history[..self.count.min(S)].iter().sum();
sum / self.count.min(S) as f32
}
pub fn reset(&mut self) {
self.history = [0.0; S];
self.head = 0;
self.count = 0;
self.total_samples = 0;
self.total_emitted = 0;
self.last_emitted = -1.0;
}
}
impl<const S: usize> Default for PragmaticGate<S> {
fn default() -> Self { Self::default_gate() }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn first_observation_always_emitted() {
let mut gate = PragmaticGate::<8>::default_gate();
assert!(gate.should_emit(0.2, 0.0), "first sample must always emit");
}
#[test]
fn constant_stream_suppressed_after_first() {
let mut gate = PragmaticGate::<8>::default_gate();
let mut emitted = 0u32;
for _ in 0..100 {
if gate.should_emit(0.1, 0.0) { emitted += 1; }
}
assert_eq!(emitted, 1, "constant entropy should suppress all after first: {}", emitted);
}
#[test]
fn state_change_triggers_emission() {
let mut gate = PragmaticGate::<8>::default_gate();
gate.should_emit(0.1, 0.0); for _ in 0..10 { gate.should_emit(0.1, 0.0); }
let emitted = gate.should_emit(0.9, 0.0);
assert!(emitted, "large entropy change must trigger emission");
}
#[test]
fn urgency_override_bypasses_gate() {
let mut gate = PragmaticGate::<8>::default_gate();
gate.should_emit(0.1, 0.0); for _ in 0..5 {
let e = gate.should_emit(0.1, 0.95); assert!(e, "high urgency must bypass gate");
}
}
#[test]
fn backplane_efficiency_high_for_constant() {
let mut gate = PragmaticGate::<8>::default_gate();
for _ in 0..200 { gate.should_emit(0.1, 0.0); }
let eff = gate.backplane_efficiency();
assert!(eff > 0.98, "constant stream efficiency > 98%: {:.4}", eff);
}
#[test]
fn reset_clears_state() {
let mut gate = PragmaticGate::<8>::default_gate();
for _ in 0..50 { gate.should_emit(0.1, 0.0); }
gate.reset();
assert_eq!(gate.total_samples(), 0);
assert_eq!(gate.total_emitted(), 0);
assert!(gate.should_emit(0.1, 0.0), "first after reset must emit");
}
#[test]
fn efficiency_reflects_emission_ratio() {
let mut gate = PragmaticGate::<8>::default_gate();
for _ in 0..100 { gate.should_emit(0.2, 0.0); }
let eff = gate.backplane_efficiency();
assert!((eff - 0.99).abs() < 0.02, "efficiency: {:.4}", eff);
}
}