use std::time::Instant;
use axiom_engine::config::AxiomConfig;
use axiom_engine::context_compressor::{adapt_session_blocking, extract_memory_vector_blocking};
use axiom_engine::inference::InferencePipeline;
use axiom_engine::meta_train::MetaTrainer;
use candle_core::Device;
const CONVERGED_CHECKPOINT: &str = "./checkpoints/axiom_converged.bin";
const NEEDLE: &str =
"const AXIOM_SECRET_VALIDATION_TOKEN: &str = \"TTT_ALIVE_AND_CONVERGING\";";
const COLLAPSE_CEILING: f32 = 0.05;
const HEALTHY_LO: f32 = 0.250;
const HEALTHY_HI: f32 = 1.500;
fn default_config() -> AxiomConfig {
AxiomConfig {
d_model: 64,
n_layers: 2,
vocab_size: 256,
lr_inner: 1e-3,
norm_eps: 1e-6,
}
}
fn passkey_haystack() -> String {
let pre: String = (0..70)
.map(|i| format!("fn module_{i}_handler(ctx: &Ctx, arg_{i}: usize) -> usize {{ ctx.scale(arg_{i}) + {i} }}"))
.collect::<Vec<_>>()
.join("\n");
let post: String = (0..70)
.map(|i| format!("let route_{i} = router.bind(\"/api/v{i}\", module_{i}_handler);"))
.collect::<Vec<_>>()
.join("\n");
format!("{pre}\n{NEEDLE}\n{post}\n")
}
fn load_or_train_pipeline(config: &AxiomConfig, device: &Device) -> (InferencePipeline, String) {
if !std::path::Path::new(CONVERGED_CHECKPOINT).exists() {
println!("[passkey] no converged checkpoint at {CONVERGED_CHECKPOINT}; meta-training one");
let repo_root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.map(|p| p.to_path_buf())
.unwrap_or_else(|| std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")));
let mut trainer = MetaTrainer::build(
config.clone(),
device.clone(),
repo_root,
CONVERGED_CHECKPOINT.to_string(),
8,
32,
256,
1024,
42,
)
.expect("meta-trainer must build from repo files");
assert!(trainer.dataset_len() > 0, "meta-train dataset must be non-empty");
let final_loss = trainer
.run( 1, 60, 1e-3)
.expect("meta-training run must succeed");
println!("[passkey] meta-train final loss = {final_loss:.4}");
}
let pipeline = InferencePipeline::with_checkpoint(
config.clone(),
device.clone(),
CONVERGED_CHECKPOINT.to_string(),
)
.expect("pipeline must load the converged checkpoint");
(pipeline, CONVERGED_CHECKPOINT.to_string())
}
#[test]
fn passkey_recall_breaks_past_zero_after_convergence() {
let config = default_config();
let device = Device::Cpu;
let (pipeline, checkpoint) = load_or_train_pipeline(&config, &device);
println!("[passkey] loaded checkpoint: {checkpoint}");
let mut states = pipeline
.init_session_states()
.expect("session states must initialise");
let context = passkey_haystack();
let ctx_tokens = pipeline.encode_text(&context);
assert!(
ctx_tokens.len() > 200,
"context must exceed 200 tokens of bloat (got {})",
ctx_tokens.len()
);
let pre = extract_memory_vector_blocking(
&pipeline,
&mut states.clone(),
&pipeline.encode_text("probe"),
"passkey-pre",
0,
Instant::now(),
8,
)
.expect("pre-adaptation probe must run");
adapt_session_blocking(&pipeline, &mut states, &ctx_tokens)
.expect("context ingestion (TTT adaptation) must succeed");
let query = "What is the value of AXIOM_SECRET_VALIDATION_TOKEN?";
let q_tokens = pipeline.encode_text(query);
let fingerprint = extract_memory_vector_blocking(
&pipeline,
&mut states,
&q_tokens,
"passkey-verify",
ctx_tokens.len(),
Instant::now(),
32,
)
.expect("associative recall pass must succeed");
println!("\n================ PASSKEY FINGERPRINT ================");
println!("{}", fingerprint.to_prompt_block());
println!("====================================================");
println!("[telemetry] needle : {NEEDLE}");
println!("[telemetry] context_tokens : {}", ctx_tokens.len());
println!(
"[telemetry] pre-adapt W̃ norms : {:?}",
pre.layer_frobenius_norms
);
println!(
"[telemetry] post-adapt W̃ norms : {:?}",
fingerprint.layer_frobenius_norms
);
println!("[telemetry] pre-adapt recall_norm = {:.6}", pre.recall_norm);
println!(
"[telemetry] post-adapt recall_norm = {:.6} (healthy {HEALTHY_LO}..{HEALTHY_HI})",
fingerprint.recall_norm
);
println!("[telemetry] recall_l1 = {:.6}", fingerprint.recall_l1);
assert!(
fingerprint.recall_norm.is_finite(),
"recall_norm must be finite, got {}",
fingerprint.recall_norm
);
assert!(
fingerprint.recall_norm > COLLAPSE_CEILING,
"recall_norm={} is in the collapsed regime (<= {}) — trained projections \
are NOT lifting the secret out of the hidden state",
fingerprint.recall_norm,
COLLAPSE_CEILING
);
assert!(
fingerprint.recall_norm >= HEALTHY_LO && fingerprint.recall_norm <= HEALTHY_HI,
"recall_norm={} is outside the healthy convergence range {}..{}",
fingerprint.recall_norm,
HEALTHY_LO,
HEALTHY_HI
);
assert_eq!(
fingerprint.context_tokens_processed,
ctx_tokens.len(),
"fingerprint must report the full ingested-token count"
);
assert!(
fingerprint
.layer_frobenius_norms
.iter()
.all(|n| n.is_finite() && *n > 0.0),
"every adapted layer W̃ must have a finite, non-zero Frobenius norm"
);
}