#![cfg(target_os = "macos")]
use std::path::PathBuf;
use std::sync::atomic::{AtomicU64, Ordering};
use half::f16;
use model_io::ExpertCacheSlots;
use turbospark_repack::{
build_synthetic_gemma4_real_install, build_synthetic_qwen4_exp_decode_install,
build_synthetic_qwen_gdn_dense_install,
};
use turbospark_runtime::{
ChunkedPrefillRunner, DraftPolicies, LogitProducer, RealForwardRunner, SteeringPolicy,
};
use turbospark_vision_io::MropePositions;
static COUNTER: AtomicU64 = AtomicU64::new(0);
fn temp_dir() -> PathBuf {
let n = COUNTER.fetch_add(1, Ordering::SeqCst);
let dir = std::env::temp_dir().join(format!(
"turbospark-session-pool-{}-{n}",
std::process::id()
));
std::fs::create_dir_all(&dir).unwrap();
dir
}
const VOCAB: i64 = 128;
fn build_install(dir: &std::path::Path) -> model_io::ArchConfig {
build_synthetic_gemma4_real_install(dir, VOCAB, 2, 16, 4, 8, "tiny-gemma4-session-pool")
.expect("real-naming install builds")
}
fn open(
dir: &std::path::Path,
arch: model_io::ArchConfig,
session_slots: usize,
) -> RealForwardRunner {
let mut runner = RealForwardRunner::open_with_slot_policy_speculation_steering_and_sessions(
dir,
arch,
4096,
ExpertCacheSlots::Fixed(16),
DraftPolicies::off(),
SteeringPolicy::off(),
session_slots,
)
.expect("real-naming install opens");
runner.set_prefix_reuse(true);
runner
}
fn feed(runner: &mut RealForwardRunner, tokens: &[i32]) {
let mut logits = vec![f16::from_f32(0.0); VOCAB as usize];
let last = tokens.len() - 1;
for (position, &token) in tokens.iter().enumerate() {
if position == last {
runner.produce(token, position, &mut logits)
} else {
runner.produce_prefill(token, position, &mut logits)
}
.expect("produce succeeds");
}
}
fn text_positions(len: usize) -> MropePositions {
MropePositions {
triples: (0..len).map(|i| (i as i32, i as i32, i as i32)).collect(),
rope_delta: 0,
spans: Vec::new(),
}
}
#[test]
fn an_active_image_prompt_cannot_select_a_parked_session() {
let dir = temp_dir();
let arch = build_install(&dir);
let mut runner = open(&dir, arch, 2);
let victim = [1, 2, 3, 4, 5, 6];
let other = [7, 8, 9, 10, 11, 12];
runner.reset();
feed(&mut runner, &victim);
runner.reset();
feed(&mut runner, &other);
runner
.set_prompt_vision(&[], &text_positions(victim.len()), victim.len())
.expect("the image prompt map validates");
assert_eq!(
runner.try_reuse_prefix(&victim),
0,
"an image request must not swap in token-matching state parked by another request"
);
}
#[test]
fn state_produced_with_an_image_map_remains_tainted_when_parked() {
let dir = temp_dir();
let arch = build_install(&dir);
let mut runner = open(&dir, arch, 2);
let image_prompt = [1, 2, 3, 4, 5, 6];
let other = [7, 8, 9, 10, 11, 12];
runner
.set_prompt_vision(&[], &text_positions(image_prompt.len()), image_prompt.len())
.expect("the image prompt map validates");
runner.reset();
feed(&mut runner, &image_prompt);
runner.clear_prompt_vision();
assert_eq!(runner.try_reuse_prefix(&other), 0);
runner.reset();
feed(&mut runner, &other);
assert_eq!(
runner.try_reuse_prefix(&image_prompt),
0,
"state built while an image map was active must remain tainted after it is parked"
);
}
#[test]
fn a_pool_of_one_is_a_complete_no_op() {
let dir = temp_dir();
let arch = build_install(&dir);
let mut runner = open(&dir, arch, 1);
let session_a: [i32; 13] = [100, 101, 5, 9, 2, 7, 1, 3, 8, 4, 6, 0, 11];
runner.reset();
feed(&mut runner, &session_a);
let session_b_prompt: [i32; 13] = [100, 101, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60];
let reused = runner.try_reuse_prefix(&session_b_prompt);
assert_eq!(
reused, 2,
"without a pool, a shallow match is reused in place unchanged, matching Part 1's \
shipped single-session behavior"
);
}
#[test]
fn a_shallow_match_against_the_live_session_does_not_destroy_it() {
let dir = temp_dir();
let arch = build_install(&dir);
let mut runner = open(&dir, arch, 2);
let session_a: [i32; 13] = [100, 101, 5, 9, 2, 7, 1, 3, 8, 4, 6, 0, 11];
runner.reset();
feed(&mut runner, &session_a);
let session_b_prompt: [i32; 13] = [100, 101, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60];
let reused_b = runner.try_reuse_prefix(&session_b_prompt);
assert_eq!(
reused_b, 0,
"a shallow match (2 of 13 tokens) that would discard more of the live session than \
it keeps must be refused once a pool is active, not partially reused in place -- \
refusing is what lets the caller's reset() park the live session instead of \
overwriting it"
);
runner.reset();
feed(&mut runner, &session_b_prompt);
let session_a_continued: [i32; 15] = [100, 101, 5, 9, 2, 7, 1, 3, 8, 4, 6, 0, 11, 99, 98];
let reused_a2 = runner.try_reuse_prefix(&session_a_continued);
assert!(
reused_a2 >= 12,
"session A's history should have been parked (not destroyed) when B's turn ran, and \
found again through the pool when A's conversation continued; expected close to \
A's full 13-token history reused, got {reused_a2}"
);
}
fn feed_chunked(runner: &mut RealForwardRunner, prompt: &[i32], reply: &[i32]) {
let mut logits = vec![f16::from_f32(0.0); VOCAB as usize];
runner
.prefill_chunk(prompt, 0, &mut logits)
.expect("prefill_chunk succeeds");
for (i, &token) in reply.iter().enumerate() {
runner
.produce(token, prompt.len() + i, &mut logits)
.expect("produce succeeds");
}
}
#[test]
fn a_shallow_match_does_not_destroy_a_chunked_prefill_session() {
let dir = temp_dir();
let arch = build_install(&dir);
let mut runner = open(&dir, arch, 2);
let session_a_prompt: [i32; 10] = [100, 101, 5, 9, 2, 7, 1, 3, 8, 4];
let session_a_reply: [i32; 3] = [6, 0, 11];
runner.reset();
feed_chunked(&mut runner, &session_a_prompt, &session_a_reply);
let session_b_prompt: [i32; 13] = [100, 101, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60];
let reused_b = runner.try_reuse_prefix(&session_b_prompt);
assert_eq!(
reused_b, 0,
"the shallow match must be refused on the chunked-prefill path too"
);
runner.reset();
let session_b_reply: [i32; 2] = [61, 62];
feed_chunked(&mut runner, &session_b_prompt, &session_b_reply);
let session_a_continued: [i32; 15] = [100, 101, 5, 9, 2, 7, 1, 3, 8, 4, 6, 0, 11, 99, 98];
let reused_a2 = runner.try_reuse_prefix(&session_a_continued);
assert!(
reused_a2 >= 12,
"session A's chunked-prefill session should have been parked (not destroyed) when \
B's turn ran; expected close to A's full 13-token history reused, got {reused_a2}"
);
}
#[test]
fn eviction_is_reported_only_when_something_real_was_discarded() {
let dir = temp_dir();
let arch = build_install(&dir);
let mut runner = open(&dir, arch, 2);
let session_a: [i32; 6] = [1, 2, 3, 4, 5, 6];
runner.reset();
feed(&mut runner, &session_a);
let session_b: [i32; 6] = [7, 8, 9, 10, 11, 12];
assert_eq!(runner.try_reuse_prefix(&session_b), 0);
runner.reset();
assert!(
!runner.session_slot_evicted(),
"promoting a never-used parked slot must not report an eviction"
);
feed(&mut runner, &session_b);
let session_c: [i32; 6] = [13, 14, 15, 16, 17, 18];
assert_eq!(runner.try_reuse_prefix(&session_c), 0);
runner.reset();
assert!(
runner.session_slot_evicted(),
"the LRU slot at this point holds session A's real content (parked when B's turn \
ran); promoting it to make room for C must report the eviction"
);
}
fn open_qwen_gdn(dir: &std::path::Path, session_slots: usize) -> RealForwardRunner {
build_synthetic_qwen_gdn_dense_install(dir, VOCAB, 4, "tiny-bonsai-session-pool")
.expect("dense qwen3_5 install builds");
let arch = turbospark_repack::peek_manifest_arch(dir)
.expect("a dense manifest peeks against the qwen3_5 baseline");
let mut runner = RealForwardRunner::open_with_slot_policy_speculation_steering_and_sessions(
dir,
arch,
4096,
ExpertCacheSlots::Fixed(16),
DraftPolicies::off(),
SteeringPolicy::off(),
session_slots,
)
.expect("dense qwen3_5 install opens with a session pool");
runner.set_prefix_reuse(true);
runner
}
#[test]
fn a_shallow_match_does_not_destroy_a_gdn_familys_recurrent_state() {
let dir = temp_dir();
let mut runner = open_qwen_gdn(&dir, 2);
let session_a: [i32; 13] = [100, 101, 5, 9, 2, 7, 1, 3, 8, 4, 6, 0, 11];
runner.reset();
feed(&mut runner, &session_a);
let session_b_prompt: [i32; 13] = [100, 101, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60];
let reused_b = runner.try_reuse_prefix(&session_b_prompt);
assert_eq!(reused_b, 0, "a rewind is always refused on a GDN family");
runner.reset();
feed(&mut runner, &session_b_prompt);
let session_a_continued: [i32; 15] = [100, 101, 5, 9, 2, 7, 1, 3, 8, 4, 6, 0, 11, 99, 98];
let reused_a2 = runner.try_reuse_prefix(&session_a_continued);
assert!(
reused_a2 >= 12,
"session A's GDN-carrying session should have been parked (not destroyed) when B's \
turn ran; expected close to A's full 13-token history reused, got {reused_a2}"
);
}
#[test]
fn a_session_pool_is_refused_by_name_on_qwen4_exp() {
let dir = temp_dir();
let arch = build_synthetic_qwen4_exp_decode_install(&dir, VOCAB, "qwen4-pool")
.expect("qwen4 install builds");
let runner = RealForwardRunner::open_with_slot_policy_speculation_steering_and_sessions(
&dir,
arch.clone(),
4096,
ExpertCacheSlots::Fixed(16),
DraftPolicies::off(),
SteeringPolicy::off(),
1,
);
assert!(runner.is_ok(), "1 session slot should succeed on qwen4_exp");
let err = RealForwardRunner::open_with_slot_policy_speculation_steering_and_sessions(
&dir,
arch,
4096,
ExpertCacheSlots::Fixed(16),
DraftPolicies::off(),
SteeringPolicy::off(),
2,
)
.err()
.expect("2 session slots must be refused on qwen4_exp");
let text = err.to_string();
assert!(text.contains("--session-slots"), "{text}");
assert!(text.contains("gdn"), "{text}");
assert!(text.contains("qsa"), "{text}");
assert!(text.contains("ngram_context"), "{text}");
assert!(text.contains("ple_conv_tail"), "{text}");
assert!(text.contains("families/qwen4/state.rs"), "{text}");
}