use sha2::{Digest, Sha256};
use std::fs;
use std::io::Write;
use std::path::Path;
struct Fact {
id: &'static str,
text: &'static str,
supersedes: Option<&'static str>,
}
struct Probe {
id: &'static str,
query: &'static str,
expected: &'static [&'static str],
stale: &'static [&'static str],
}
static FACTS: &[Fact] = &[
Fact {
id: "pref_rust",
text: "I prefer Rust over Python for systems programming",
supersedes: None,
},
Fact {
id: "pref_dark",
text: "I use dark mode in all editors and terminals",
supersedes: None,
},
Fact {
id: "editor_old",
text: "I use vim as my primary editor",
supersedes: None,
},
Fact {
id: "editor_new",
text: "I switched from vim to neovim",
supersedes: Some("editor_old"),
},
Fact {
id: "distractor_1",
text: "The capital of France is Paris",
supersedes: None,
},
Fact {
id: "distractor_2",
text: "Water boils at one hundred degrees Celsius",
supersedes: None,
},
];
static PROBES: &[Probe] = &[
Probe {
id: "q_rust",
query: "Rust Python systems programming",
expected: &["pref_rust"],
stale: &[],
},
Probe {
id: "q_editor",
query: "editor vim neovim",
expected: &["editor_new"],
stale: &["editor_old"],
},
Probe {
id: "q_dark",
query: "dark mode editors terminals",
expected: &["pref_dark"],
stale: &[],
},
];
const DIM: usize = 128;
fn word_bag(text: &str) -> [f32; DIM] {
let mut v = [0.0f32; DIM];
for word in text.split_whitespace() {
let hash = Sha256::digest(word.to_lowercase().as_bytes());
let idx =
u64::from_le_bytes(hash[..8].try_into().expect("sha256 ≥ 8 bytes")) as usize % DIM;
v[idx] += 1.0;
}
let norm: f32 = v.iter().map(|x| x * x).sum::<f32>().sqrt();
if norm > 1e-9 {
v.iter_mut().for_each(|x| *x /= norm);
}
v
}
fn cosine(a: &[f32], b: &[f32]) -> f32 {
debug_assert_eq!(a.len(), b.len());
a.iter().zip(b.iter()).map(|(x, y)| x * y).sum()
}
fn estimate_tokens(text: &str, chars_per_token: f64) -> usize {
((text.len() as f64) / chars_per_token).ceil() as usize
}
struct ProbeResult {
probe_id: &'static str,
retrieved_ids: Vec<&'static str>,
context_tokens: usize,
}
#[derive(serde::Serialize)]
struct ArmReport {
recall_accuracy: f64,
staleness_rate: f64,
mean_context_tokens: f64,
}
#[derive(serde::Serialize)]
struct BenchmarkReport {
version: &'static str,
seed: u64,
load_all: ArmReport,
selective: ArmReport,
}
fn recall_accuracy(probes: &[Probe], results: &[ProbeResult]) -> f64 {
if results.is_empty() {
return 0.0;
}
let hits = results
.iter()
.filter(|r| {
let Some(p) = probes.iter().find(|p| p.id == r.probe_id) else {
return false;
};
p.expected.iter().all(|e| r.retrieved_ids.contains(e))
})
.count();
hits as f64 / results.len() as f64
}
fn staleness_rate(probes: &[Probe], results: &[ProbeResult]) -> f64 {
if results.is_empty() {
return 0.0;
}
let stale = results
.iter()
.filter(|r| {
let Some(p) = probes.iter().find(|p| p.id == r.probe_id) else {
return false;
};
p.stale.iter().any(|s| r.retrieved_ids.contains(s))
})
.count();
stale as f64 / results.len() as f64
}
fn mean_tokens(results: &[ProbeResult]) -> f64 {
if results.is_empty() {
return 0.0;
}
let total: usize = results.iter().map(|r| r.context_tokens).sum();
total as f64 / results.len() as f64
}
fn run_load_all(chars_per_token: f64) -> Vec<ProbeResult> {
let all_text: String = FACTS.iter().map(|f| f.text).collect::<Vec<_>>().join(" ");
let total_tokens = estimate_tokens(&all_text, chars_per_token);
PROBES
.iter()
.map(|p| ProbeResult {
probe_id: p.id,
retrieved_ids: FACTS.iter().map(|f| f.id).collect(),
context_tokens: total_tokens,
})
.collect()
}
fn run_selective(top_k: usize, chars_per_token: f64) -> Vec<ProbeResult> {
let superseded: std::collections::HashSet<&str> =
FACTS.iter().filter_map(|f| f.supersedes).collect();
let active: Vec<(&Fact, [f32; DIM])> = FACTS
.iter()
.filter(|f| !superseded.contains(f.id))
.map(|f| (f, word_bag(f.text)))
.collect();
PROBES
.iter()
.map(|p| {
let q_vec = word_bag(p.query);
let mut scored: Vec<(f32, &Fact)> = active
.iter()
.map(|(f, v)| (cosine(&q_vec, v), *f))
.collect();
scored.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(std::cmp::Ordering::Equal));
let top: Vec<&Fact> = scored.into_iter().take(top_k).map(|(_, f)| f).collect();
let context_text: String = top.iter().map(|f| f.text).collect::<Vec<_>>().join(" ");
let context_tokens = estimate_tokens(&context_text, chars_per_token);
let retrieved_ids = top.iter().map(|f| f.id).collect();
ProbeResult {
probe_id: p.id,
retrieved_ids,
context_tokens,
}
})
.collect()
}
fn main() {
const TOP_K: usize = 2;
const CHARS_PER_TOKEN: f64 = 4.0;
const SEED: u64 = 42;
println!("bench_memory: tiered memory benchmark harness (T14, D-08)");
println!(
"Dataset : {} facts, {} probes (seed={})",
FACTS.len(),
PROBES.len(),
SEED
);
println!("Config : top_k={TOP_K}, chars_per_token={CHARS_PER_TOKEN}");
println!();
let la_results = run_load_all(CHARS_PER_TOKEN);
let sel_results = run_selective(TOP_K, CHARS_PER_TOKEN);
let la = ArmReport {
recall_accuracy: recall_accuracy(PROBES, &la_results),
staleness_rate: staleness_rate(PROBES, &la_results),
mean_context_tokens: mean_tokens(&la_results),
};
let sel = ArmReport {
recall_accuracy: recall_accuracy(PROBES, &sel_results),
staleness_rate: staleness_rate(PROBES, &sel_results),
mean_context_tokens: mean_tokens(&sel_results),
};
println!("╔═══════════════════════════════════════════════════════════════╗");
println!("║ SC-29 HEADLINE — bench_memory results ║");
println!("╠══════════════════════════╦════════════╦══════════════════════╣");
println!("║ Metric ║ load_all ║ selective ║");
println!("╠══════════════════════════╬════════════╬══════════════════════╣");
println!(
"║ recall_accuracy ║ {:.3} ║ {:.3} ║",
la.recall_accuracy, sel.recall_accuracy
);
println!(
"║ staleness_rate ║ {:.3} ║ {:.3} ║",
la.staleness_rate, sel.staleness_rate
);
println!(
"║ mean_context_tokens ║ {:<10.1} ║ {:<20.1} ║",
la.mean_context_tokens, sel.mean_context_tokens
);
println!("╠══════════════════════════╩════════════╩══════════════════════╣");
let token_fraction = if la.mean_context_tokens > 0.0 {
sel.mean_context_tokens / la.mean_context_tokens
} else {
1.0
};
let selective_ge = sel.recall_accuracy >= la.recall_accuracy;
let bounded_window = sel.mean_context_tokens < la.mean_context_tokens;
let lower_staleness = sel.staleness_rate <= la.staleness_rate;
println!(
"║ selective recall ≥ load_all: {} (SC-29) ║",
if selective_ge { "PASS ✓" } else { "FAIL ✗" }
);
println!(
"║ token fraction: {:.1}% of load_all ({}) ║",
token_fraction * 100.0,
if bounded_window {
"bounded ✓"
} else {
"not bounded ✗"
}
);
println!(
"║ staleness delta: {:+.3} ({}) ║",
sel.staleness_rate - la.staleness_rate,
if lower_staleness {
"lower ✓"
} else {
"not lower ✗"
}
);
println!("╚═══════════════════════════════════════════════════════════════╝");
println!();
let report = BenchmarkReport {
version: "1.0",
seed: SEED,
load_all: la,
selective: sel,
};
let json = serde_json::to_string_pretty(&report).expect("serialization must not fail");
let report_path = Path::new("target/bench_memory_report.json");
if let Some(parent) = report_path.parent() {
fs::create_dir_all(parent).expect("cannot create target/ dir");
}
let mut f = fs::File::create(report_path).expect("cannot create report file");
f.write_all(json.as_bytes()).expect("cannot write report");
println!("report.json written to: {}", report_path.display());
if !selective_ge || !bounded_window || !lower_staleness {
eprintln!("ERROR: SC-29 headline assertions failed — check the report.");
std::process::exit(1);
}
}