use chrono::{Duration, TimeZone, Utc};
use std::path::PathBuf;
use std::sync::Mutex;
use crate::core::cognitive_gate::{basic_science_enabled, full_science_enabled};
use crate::core::config::{CognitiveMode, CompressionLevel};
use crate::core::context_prefetch::{FileTrajectory, build_prefetch_plan};
use crate::core::echo_ratio::compute_echo_ratio;
use crate::core::ib::{TaskIntent, classify_intent, compute_relevance, intent_query_terms};
use crate::core::memory_scheduler::{initial_state, retrievability};
use crate::core::session::{SessionState, TaskInfo};
use crate::core::stigmergy::{
PheromoneSignal, PressureMap, SignalKind, deposit_signal, read_signals, reset_signals,
};
use crate::core::tokens::count_tokens;
use crate::core::verbosity::{
BehaviorSignal, TranscriptEntry, analyze_transcript, extract_signals, recommend_level,
};
static STIGMERGY_TEST_LOCK: Mutex<()> = Mutex::new(());
fn session_with_task(description: &str) -> SessionState {
let mut session = SessionState::new();
session.task = Some(TaskInfo {
description: description.to_owned(),
intent: None,
progress_pct: None,
});
session
}
fn transcript_read(target: &str, level: &str, tokens: usize, seconds: i64) -> TranscriptEntry {
TranscriptEntry {
tool: "ctx_read".to_owned(),
target: target.to_owned(),
compression_level: level.to_owned(),
response_tokens: tokens,
timestamp: Utc.timestamp_opt(seconds, 0).single().expect("valid time"),
}
}
fn pheromone(agent_id: &str, path: &str, kind: SignalKind) -> PheromoneSignal {
PheromoneSignal {
agent_id: agent_id.to_owned(),
kind,
path: path.to_owned(),
symbol: None,
strength: 0.8,
deposited_at: Utc.with_ymd_and_hms(2026, 1, 1, 0, 0, 0).unwrap(),
note: None,
}
}
fn fsrs_boost(retrievability: f64) -> f64 {
(1.5 - retrievability).max(0.1)
}
fn science_enabled_for(mode: CognitiveMode) -> (bool, bool) {
match mode {
CognitiveMode::Off => (false, false),
CognitiveMode::Basic => (true, false),
CognitiveMode::Full => (true, true),
}
}
fn estimate_pipeline_tokens(
mode: CognitiveMode,
task: &str,
sources: &[(&str, &str)],
) -> (usize, usize, f64) {
let (basic_on, full_on) = science_enabled_for(mode);
let mut parts = vec![task.to_owned()];
parts.extend(
sources
.iter()
.map(|(path, content)| format!("{path}\n{content}")),
);
let mut total = parts.iter().map(|part| count_tokens(part)).sum::<usize>();
let session = session_with_task(task);
let intent = classify_intent(&session);
let mut extra_terms = 0_usize;
let mut top_relevance = 0.0_f64;
if basic_on {
let terms = intent_query_terms(&intent);
extra_terms = terms.len();
total += count_tokens(&terms.join(" "));
let chunk_refs: Vec<&str> = sources.iter().map(|(_, content)| *content).collect();
let relevance = compute_relevance(&chunk_refs, &intent, Some(task));
if let Some(top) = relevance.first() {
top_relevance = top.score;
}
total += relevance.len() * 4;
}
if full_on {
let now = Utc.with_ymd_and_hms(2026, 6, 1, 12, 0, 0).unwrap();
for days in [1_u64, 7, 30] {
let mut state = initial_state(format!("fact-{days}"), 3);
state.last_review = now - Duration::days(days as i64);
let r = retrievability(&state, now);
total += count_tokens(&format!("fsrs:{days}d r={r:.3} boost={:.3}", fsrs_boost(r)));
}
let mut trajectory = FileTrajectory::new(20);
for path in sources.iter().map(|(path, _)| *path) {
trajectory.record(path);
}
let plan = build_prefetch_plan(&trajectory, &[], 3, 0.1);
total += plan
.files
.iter()
.map(|entry| count_tokens(&entry.path))
.sum::<usize>();
total += count_tokens("stigmergy:3-agent-coordination");
}
(total, extra_terms, top_relevance)
}
#[test]
fn benchmark_ib_intent_keyword_enrichment() {
let cases: [(&str, TaskIntent, &[&str]); 6] = [
(
"fix the null pointer bug in user authentication",
TaskIntent::Debug,
&["error", "panic", "unwrap"],
),
(
"refactor the database connection pool",
TaskIntent::Refactor,
&["struct", "trait", "impl"],
),
(
"implement a new REST API endpoint for billing",
TaskIntent::Implement,
&["test", "spec", "api"],
),
(
"review the security of the encryption module",
TaskIntent::Review,
&["unsafe", "security"],
),
(
"understand how the caching layer works",
TaskIntent::Explore,
&["mod", "struct", "fn"],
),
(
"update the README with installation instructions",
TaskIntent::Unknown,
&[],
),
];
eprintln!("\n=== IB Intent Keyword Enrichment ===");
eprintln!(
"{:<55} {:>10} {:>6} {:>8}",
"Task", "Intent", "Terms", "Relevant"
);
eprintln!("{}", "-".repeat(85));
for (description, expected_intent, expected_terms) in cases {
let session = session_with_task(description);
let intent = classify_intent(&session);
assert_eq!(
intent, expected_intent,
"wrong intent for task: {description}"
);
let terms = intent_query_terms(&intent);
let extra = terms.len();
let relevant = expected_terms
.iter()
.filter(|term| terms.iter().any(|t| t.eq_ignore_ascii_case(term)))
.count();
if expected_intent == TaskIntent::Unknown {
assert!(terms.is_empty(), "Unknown intent should add no terms");
} else {
assert!(
!terms.is_empty(),
"{expected_intent} intent should supply query terms"
);
assert_eq!(
relevant,
expected_terms.len(),
"expected all sample terms for {description}, got {terms:?}"
);
}
eprintln!(
"{:<55} {:>10} {:>6} {:>8}",
description.chars().take(54).collect::<String>(),
intent,
extra,
relevant
);
}
}
#[test]
fn benchmark_fsrs_memory_reranking() {
let now = Utc.with_ymd_and_hms(2026, 6, 1, 12, 0, 0).unwrap();
let intervals = [1_i64, 3, 7, 14, 30];
eprintln!("\n=== FSRS Memory Re-ranking ===");
eprintln!(
"{:<12} {:>14} {:>10} {:>10}",
"Last seen", "Retrievability", "Boost", "Rank key"
);
eprintln!("{}", "-".repeat(50));
let mut scored: Vec<(i64, f64, f64)> = Vec::new();
for days in intervals {
let mut state = initial_state(format!("fact-{days}d"), 3);
state.last_review = now - Duration::days(days);
let r = retrievability(&state, now);
let boost = fsrs_boost(r);
scored.push((days, r, boost));
eprintln!(
"{:<12} {:>14.4} {:>10.4} {:>10.4}",
format!("{days} days"),
r,
boost,
boost
);
}
for window in scored.windows(2) {
let (days_old, r_old, boost_old) = window[0];
let (days_new, r_new, boost_new) = window[1];
assert!(
r_old > r_new,
"retrievability should decrease with age ({days_old}d r={r_old} vs {days_new}d r={r_new})"
);
assert!(
boost_old < boost_new,
"boost should increase with age ({days_old}d boost={boost_old} vs {days_new}d boost={boost_new})"
);
}
let base_relevance = 1.0_f64;
let mut ranked: Vec<(i64, f64)> = intervals
.iter()
.map(|&days| {
let mut state = initial_state(format!("fact-{days}d"), 3);
state.last_review = now - Duration::days(days);
let r = retrievability(&state, now);
(days, base_relevance * fsrs_boost(r))
})
.collect();
let original_order: Vec<i64> = ranked.iter().map(|(days, _)| *days).collect();
ranked.sort_by(|left, right| {
right
.1
.partial_cmp(&left.1)
.unwrap_or(std::cmp::Ordering::Equal)
});
let reranked_order: Vec<i64> = ranked.iter().map(|(days, _)| *days).collect();
assert_ne!(
original_order, reranked_order,
"FSRS boost should change fact ordering"
);
assert_eq!(
reranked_order[0], 30,
"oldest fact (30 days) should rank first after FSRS boost"
);
assert_eq!(
reranked_order.last().copied(),
Some(1),
"most recent fact (1 day) should rank last after FSRS boost"
);
}
#[test]
fn benchmark_echo_ratio_detection() {
let input = "The database connection handler acquires a pooled connection \
and validates credentials before returning the session token.";
#[allow(clippy::type_complexity)]
let cases: [(&str, &str, fn(f64) -> bool); 4] = [
(
"high echo",
"The database connection handler acquires a pooled connection \
and validates credentials before returning the session token.",
|ratio| ratio > 0.7,
),
(
"low echo",
"Implemented retry backoff with jitter and structured error codes.",
|ratio| ratio < 0.3,
),
(
"medium echo",
"The database connection handler now uses exponential backoff \
when the pool is exhausted during peak traffic.",
|ratio| ratio > 0.3 && ratio < 0.7,
),
("empty output", "", |ratio| {
(ratio - 0.0).abs() < f64::EPSILON
}),
];
eprintln!("\n=== Echo Ratio Detection ===");
eprintln!(
"{:<14} {:>8} {:>10} {:>10}",
"Scenario", "Ratio", "Verdict", "Echo words"
);
eprintln!("{}", "-".repeat(46));
for (label, output, predicate) in cases {
let report = compute_echo_ratio(input, output);
assert!(
predicate(report.ratio),
"{label}: ratio {} failed predicate",
report.ratio
);
eprintln!(
"{:<14} {:>8.2} {:>10} {:>4}/{}",
label, report.ratio, report.verdict, report.echo_words, report.output_words
);
}
}
#[test]
fn benchmark_verbosity_recommendation() {
let high_detail_entries = vec![
transcript_read("src/auth.rs", "max", 800, 0),
transcript_read("src/auth.rs", "max", 820, 15),
transcript_read("src/auth.rs", "full", 1200, 30),
transcript_read("src/auth.rs", "max", 850, 45),
TranscriptEntry {
tool: "ctx_expand".to_owned(),
target: "src/auth.rs".to_owned(),
compression_level: "max".to_owned(),
response_tokens: 600,
timestamp: Utc.timestamp_opt(50, 0).single().expect("valid time"),
},
TranscriptEntry {
tool: "ctx_expand".to_owned(),
target: "src/session.rs".to_owned(),
compression_level: "standard".to_owned(),
response_tokens: 500,
timestamp: Utc.timestamp_opt(55, 0).single().expect("valid time"),
},
];
let efficient_entries: Vec<TranscriptEntry> = (0..6)
.map(|idx| {
transcript_read(
&format!("src/module_{idx}.rs"),
"max",
120,
i64::from(idx) * 30,
)
})
.collect();
let efficient_signals: Vec<BehaviorSignal> = (0..5)
.flat_map(|_| {
[
BehaviorSignal::TaskComplete { reads_count: 1 },
BehaviorSignal::TaskComplete { reads_count: 2 },
]
})
.collect();
let high_detail_signals = extract_signals(&high_detail_entries);
let high_detail_analysis = analyze_transcript(&high_detail_entries, 20);
let high_detail = recommend_level(&high_detail_signals);
let efficient_analysis = analyze_transcript(&efficient_entries, 20);
let efficient = recommend_level(&efficient_signals);
let default_profile = recommend_level(&[]);
eprintln!("\n=== Verbosity Recommendation ===");
eprintln!(
"High-detail: level={:?} confidence={:.2} re_reads={} corrections={}",
high_detail.level,
high_detail.confidence,
high_detail_analysis.re_read_count,
high_detail_analysis.correction_count
);
eprintln!(
"Efficient: level={:?} confidence={:.2} window={} dominant={}",
efficient.level,
efficient.confidence,
efficient_analysis.window_size,
efficient_analysis.dominant_level
);
eprintln!(
"Default: level={:?} confidence={:.2}",
default_profile.level, default_profile.confidence
);
assert_eq!(
high_detail.level,
CompressionLevel::Off,
"high-detail user should get less compression"
);
assert!(
matches!(
efficient.level,
CompressionLevel::Max | CompressionLevel::Standard
),
"efficient user should recommend equal or more compression than standard, got {:?}",
efficient.level
);
assert_eq!(
default_profile.level,
CompressionLevel::Lite,
"default should recommend standard lite level"
);
assert!(
compression_rank(efficient.level) > compression_rank(high_detail.level),
"efficient profile should compress more aggressively than high-detail"
);
}
fn compression_rank(level: CompressionLevel) -> u8 {
match level {
CompressionLevel::Raw => 5,
CompressionLevel::Max => 4,
CompressionLevel::Standard => 3,
CompressionLevel::Lite => 2,
CompressionLevel::Off => 1,
}
}
#[test]
fn benchmark_stigmergy_coordination() {
let _guard = STIGMERGY_TEST_LOCK
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
reset_signals();
let shared = "src/auth/handler.rs";
for agent in ["cursor-1", "cursor-2", "cursor-3"] {
deposit_signal(pheromone(agent, shared, SignalKind::Exploration));
}
let shared_signals = read_signals(shared, None);
let shared_pressure = PressureMap::from_signals(&shared_signals);
let shared_field = shared_pressure.pressure_at(shared);
reset_signals();
let split_paths = [
"src/auth/handler.rs",
"src/db/pool.rs",
"src/api/billing.rs",
];
for (idx, path) in split_paths.iter().enumerate() {
deposit_signal(pheromone(
&format!("cursor-{}", idx + 1),
path,
SignalKind::Exploration,
));
}
let split_signals: Vec<PheromoneSignal> = split_paths
.iter()
.flat_map(|path| read_signals(path, None))
.collect();
let split_pressure = PressureMap::from_signals(&split_signals);
let split_field = split_pressure.pressure_at("src/auth/handler.rs");
eprintln!("\n=== Stigmergy Signal Coordination ===");
eprintln!(
"Same file (3 agents): strength={:.2} agents={}",
shared_field.total_strength, shared_field.agent_count
);
eprintln!(
"Split files (1 each): strength={:.2} agents={}",
split_field.total_strength, split_field.agent_count
);
assert_eq!(shared_field.agent_count, 3);
assert!(
shared_field.total_strength > split_field.total_strength,
"three agents on one file should produce higher per-file pressure"
);
assert_eq!(split_field.agent_count, 1);
reset_signals();
assert!(
read_signals(shared, None).is_empty(),
"reset_signals should clear state"
);
}
#[test]
fn benchmark_context_prefetch_accuracy() {
let paths = ["src/a.rs", "src/b.rs", "src/c.rs"];
let mut full = FileTrajectory::new(20);
for path in [paths[0], paths[1], paths[2], paths[0], paths[1], paths[2]] {
full.record(path);
}
let from_c = full.predict(1);
assert_eq!(
from_c.first().map(|(path, _)| path.as_str()),
Some(paths[0]),
"from C should predict A"
);
let mut ending_at_a = FileTrajectory::new(20);
for path in [paths[0], paths[1], paths[2], paths[0]] {
ending_at_a.record(path);
}
let from_a = ending_at_a.predict(1);
assert_eq!(
from_a.first().map(|(path, _)| path.as_str()),
Some(paths[1]),
"from A should predict B"
);
eprintln!("\n=== Context Prefetch Accuracy ===");
eprintln!("Pattern: A → B → C → A → B → C");
eprintln!(
"From C: {:?} (expected A)",
from_c
.first()
.map(|(p, prob)| format!("{p} ({:.0}%)", prob * 100.0))
);
eprintln!(
"From A: {:?} (expected B)",
from_a
.first()
.map(|(p, prob)| format!("{p} ({:.0}%)", prob * 100.0))
);
}
#[test]
fn benchmark_full_pipeline_token_comparison() {
let task = "fix null pointer bug in user authentication session handler";
let sources = [
(
"src/auth/session.rs",
"pub fn authenticate(creds: &Credentials) -> Result<Session, AuthError> {
creds.validate().map_err(AuthError::Invalid)?;
let session = Session::new(creds.user_id);
if session.token.is_null() {
panic!(\"null session token after authenticate\");
}
Ok(session)
}",
),
(
"src/auth/handler.rs",
"pub struct AuthHandler { pool: ConnectionPool }
impl AuthHandler {
pub fn login(&self, user: &str, pass: &str) -> Result<(), Error> {
self.pool.acquire()?.authenticate(user, pass)
}
}",
),
(
"src/db/pool.rs",
"pub struct ConnectionPool { max: u32 }
impl ConnectionPool {
pub fn acquire(&self) -> Result<Connection, Error> { todo!() }
}",
),
(
"src/api/routes.rs",
"pub fn mount_auth_routes(router: &mut Router) {
router.post(\"/login\", auth_handler);
}",
),
(
"src/util/error.rs",
"pub enum AuthError { Invalid, Expired, NullPointer }
impl fmt::Display for AuthError { /* ... */ }",
),
];
eprintln!("\n=== Full Pipeline Token Comparison ===");
eprintln!(
"Runtime gates: basic={} full={}",
basic_science_enabled(),
full_science_enabled()
);
eprintln!(
"{:<8} {:>10} {:>12} {:>14} {:>12}",
"Mode", "Tokens", "IB terms", "Top relevance", "Δ vs off"
);
eprintln!("{}", "-".repeat(62));
let (off_tokens, _, _) = estimate_pipeline_tokens(CognitiveMode::Off, task, &sources);
let mut rows = Vec::new();
for mode in [
CognitiveMode::Off,
CognitiveMode::Basic,
CognitiveMode::Full,
] {
let (tokens, ib_terms, top_rel) = estimate_pipeline_tokens(mode, task, &sources);
rows.push((mode, tokens, ib_terms, top_rel));
}
for (mode, tokens, ib_terms, top_rel) in &rows {
let delta = tokens.saturating_sub(off_tokens);
eprintln!(
"{:<8} {:>10} {:>12} {:>14.3} {:>12}",
mode.to_string(),
tokens,
ib_terms,
top_rel,
format!("+{delta}")
);
}
let off = rows[0].1;
let basic = rows[1].1;
let full = rows[2].1;
assert!(
basic >= off,
"basic mode should process at least as many tokens as off ({basic} vs {off})"
);
assert!(
full >= basic,
"full mode should process at least as many tokens as basic ({full} vs {basic})"
);
assert!(
rows[1].3 > 0.0,
"basic mode should produce non-zero top relevance for bug-fix task"
);
}
use crate::tools::CrpMode;
use crate::tools::ctx_read::render::{ReadTuning, process_mode_tuned};
const PROOF_MODES: &[&str] = &[
"raw",
"full",
"map",
"signatures",
"cognitive",
"mdl",
"entropy",
"aggressive",
];
const PROOF_FILES: &[(&str, &str)] = &[
("tiny", "src/core/cognitive_gate.rs"), ("small", "src/core/echo_ratio.rs"), ("medium", "src/core/tokens.rs"), ("large", "src/tools/ctx_read/render.rs"), ("huge", "src/tools/registered/ctx_read.rs"), ];
fn manifest_path(relative: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(relative)
}
fn read_proof_file(relative: &str) -> String {
std::fs::read_to_string(manifest_path(relative))
.unwrap_or_else(|e| panic!("failed to read {relative}: {e}"))
}
fn file_ext(path: &str) -> &str {
path.rsplit('.').next().unwrap_or("rs")
}
fn file_short(path: &str) -> &str {
path.rsplit('/').next().unwrap_or(path)
}
fn render_mode(content: &str, path: &str, mode: &str) -> (String, usize) {
let ext = file_ext(path);
let short = file_short(path);
let original_tokens = count_tokens(content);
let tuning = if mode == "entropy" {
ReadTuning {
aggressiveness: Some(0.8),
protect: &[],
}
} else {
ReadTuning::default()
};
process_mode_tuned(
content,
mode,
short,
short,
ext,
original_tokens,
CrpMode::Off,
path,
None,
tuning,
)
}
fn savings_pct(raw_tokens: usize, output_tokens: usize) -> f64 {
if raw_tokens == 0 {
0.0
} else {
(1.0 - output_tokens as f64 / raw_tokens as f64) * 100.0
}
}
fn assert_science_enabled() {
assert!(
basic_science_enabled(),
"proof benchmarks require cognitive_mode != off (default is basic); \
set cognitive_mode = \"basic\" or \"full\" in config.toml"
);
}
#[test]
fn proof_cognitive_mode_saves_tokens() {
assert_science_enabled();
let path = "src/tools/ctx_read/render.rs";
let content = read_proof_file(path);
let raw_tokens = count_tokens(&content);
let (cognitive_output, cognitive_tokens) = render_mode(&content, path, "cognitive");
let saving = savings_pct(raw_tokens, cognitive_tokens);
println!("=== PROOF: cognitive mode ===");
println!(" File: {}", file_short(path));
println!(" Raw tokens: {raw_tokens}");
println!(" Cognitive tokens: {cognitive_tokens}");
println!(" Savings: {saving:.1}%");
println!(
" Output preview: {}…",
&cognitive_output[..cognitive_output.len().min(120)]
);
assert!(
cognitive_tokens < raw_tokens,
"cognitive mode must save tokens on {path} ({cognitive_tokens} >= {raw_tokens})"
);
assert!(
saving > 40.0,
"cognitive should achieve >40% savings on render.rs, got {saving:.1}%"
);
}
#[test]
fn proof_mdl_mode_saves_more_than_map() {
assert_science_enabled();
let path = "src/tools/ctx_read/render.rs";
let content = read_proof_file(path);
let raw_tokens = count_tokens(&content);
let (_, map_tokens) = render_mode(&content, path, "map");
let (_, mdl_tokens) = render_mode(&content, path, "mdl");
println!("=== PROOF: mdl vs map ===");
println!(" Raw tokens: {raw_tokens}");
println!(
" Map tokens: {map_tokens} ({:.1}% saved)",
savings_pct(raw_tokens, map_tokens)
);
println!(
" MDL tokens: {mdl_tokens} ({:.1}% saved)",
savings_pct(raw_tokens, mdl_tokens)
);
assert!(map_tokens < raw_tokens, "map must compress");
assert!(mdl_tokens < raw_tokens, "mdl must compress");
assert!(
mdl_tokens <= map_tokens,
"mdl ({mdl_tokens}) should be <= map ({map_tokens}) on structural file"
);
}
#[test]
fn proof_compression_modes_ordered() {
assert_science_enabled();
let path = "src/core/tokens.rs";
let content = read_proof_file(path);
let raw_tokens = count_tokens(&content);
let modes = [
"raw",
"full",
"signatures",
"map",
"cognitive",
"mdl",
"entropy",
"aggressive",
];
let tokens_by_mode: Vec<(&str, usize)> = modes
.iter()
.map(|&mode| {
let (_, tok) = render_mode(&content, path, mode);
(mode, tok)
})
.collect();
println!("=== PROOF: mode ordering on tokens.rs ===");
for (mode, tok) in &tokens_by_mode {
println!(
" {mode:<12} {tok:>6} tok ({:>6.1}% vs raw)",
savings_pct(raw_tokens, *tok)
);
}
assert_eq!(tokens_by_mode[0].1, raw_tokens);
for (mode, tok) in tokens_by_mode.iter().skip(2) {
if matches!(*mode, "full" | "entropy") {
continue;
}
assert!(
*tok < raw_tokens,
"{mode} ({tok}) must beat raw ({raw_tokens}) on tokens.rs"
);
}
}
#[test]
fn proof_entropy_mode_saves_on_huge_file() {
assert_science_enabled();
let path = "src/tools/registered/ctx_read.rs";
let content = read_proof_file(path);
let raw_tokens = count_tokens(&content);
let (_, entropy_tokens) = render_mode(&content, path, "entropy");
let saving = savings_pct(raw_tokens, entropy_tokens);
println!("=== PROOF: entropy mode (huge file) ===");
println!(" Raw tokens: {raw_tokens}");
println!(" Entropy tokens: {entropy_tokens}");
println!(" Savings: {saving:.1}%");
assert!(
entropy_tokens < raw_tokens,
"entropy must save tokens on {path} with aggressiveness tuning"
);
}
#[test]
fn proof_comprehensive_mode_comparison() {
assert_science_enabled();
println!("\n=== PROOF: Comprehensive Token Savings ===");
println!(
"Science gates: basic={} full={}",
basic_science_enabled(),
full_science_enabled()
);
println!(
"{:<8} {:<22} {:>12} {:>8} {:>8} {:>8}",
"Size", "File", "Mode", "Raw", "Output", "Saving%"
);
println!("{}", "-".repeat(72));
let mut total_checks = 0_usize;
let mut passed_checks = 0_usize;
for (size, file) in PROOF_FILES {
let content = read_proof_file(file);
let raw_tokens = count_tokens(&content);
let short = file_short(file);
for mode in PROOF_MODES {
let (_, output_tokens) = render_mode(&content, file, mode);
let saving = savings_pct(raw_tokens, output_tokens);
println!(
"{size:<8} {short:<22} {mode:>12} {raw_tokens:>8} {output_tokens:>8} {saving:>7.1}%",
);
let must_compress = match *mode {
"map" | "signatures" | "mdl" | "aggressive" => true,
"cognitive" => raw_tokens > 500,
"entropy" => raw_tokens > 12_000,
_ => false,
};
if must_compress {
total_checks += 1;
if output_tokens < raw_tokens {
passed_checks += 1;
} else {
eprintln!(
" FAIL: {mode} on {file} did not compress ({output_tokens} >= {raw_tokens})"
);
}
}
}
println!();
}
assert_eq!(
passed_checks, total_checks,
"{passed_checks}/{total_checks} compression modes saved tokens — see table above"
);
}