use anyhow::{anyhow, Result};
use sha2::{Digest, Sha256};
use crate::debug::investigation_env::gemma_full_input as activation;
use crate::inference::models::gemma4::kv_cache::{anchor_budget, MultiSeqHybridKvBuffers};
#[derive(Clone, Debug)]
pub(crate) struct Checkpoint {
len: usize,
tokens_sha256: [u8; 32],
}
fn digest(tokens: &[u32]) -> [u8; 32] {
let mut hash = Sha256::new();
for token in tokens {
hash.update(token.to_le_bytes());
}
hash.finalize().into()
}
pub(crate) fn request_tag(
enabled: bool,
last_is_tool: bool,
prompt: &[u32],
stable: &[u32],
) -> Option<Checkpoint> {
(enabled && last_is_tool && !prompt.is_empty() && prompt == stable).then(|| Checkpoint {
len: prompt.len(),
tokens_sha256: digest(prompt),
})
}
impl Checkpoint {
pub(crate) fn validate(&self, prompt: &[u32]) -> Result<()> {
anyhow::ensure!(
self.len == prompt.len() && self.tokens_sha256 == digest(prompt),
"Gemma full-input checkpoint token identity changed"
);
Ok(())
}
}
fn substrate_supported(gemma: bool, slots: Option<u32>, hybrid: bool, bits: u32) -> bool {
gemma && matches!(slots, Some(1..=4)) && hybrid && bits == 8
}
fn env_substrate_supported(gemma: bool, slots: Option<u32>) -> bool {
let env = &crate::debug::INVESTIGATION_ENV;
substrate_supported(gemma, slots, env.hybrid_kv, env.tq_codebook_bits)
}
fn slot_count(mode: super::EngineMode) -> Option<u32> {
match mode {
super::EngineMode::SlotAware { max_slots } => Some(max_slots),
_ => None,
}
}
pub(super) fn validate_spawn(gemma: bool, mode: super::EngineMode) -> Result<(), String> {
let intent = activation::mode();
intent.validate()?;
intent.decide(env_substrate_supported(gemma, slot_count(mode)))?;
Ok(())
}
pub(crate) fn enabled_for_engine(gemma: bool, mode: super::EngineMode) -> bool {
activation::mode()
.decide(env_substrate_supported(gemma, slot_count(mode)))
.unwrap_or(false)
}
pub(super) fn decide_for_scaffold(
slots: u32,
scaffold: Option<&[MultiSeqHybridKvBuffers]>,
max_prompt_tokens: usize,
) -> Result<(bool, Option<anchor_budget::QuotaPlan>)> {
let intent = activation::mode();
if intent == activation::Mode::Off {
return Ok((false, None));
}
let plan = match scaffold
.map(|scaffold| anchor_budget::quota_plan(scaffold, max_prompt_tokens))
.transpose()
{
Ok(plan) => plan,
Err(error) if intent == activation::Mode::Default => {
tracing::warn!(error = %error, "Gemma full-input checkpoint quota plan failed; staying off");
None
}
Err(error) => return Err(error),
};
let supported =
env_substrate_supported(true, Some(slots)) && plan.is_some_and(|plan| plan.fits());
let active = intent.decide(supported).map_err(|e| anyhow!(e))?;
Ok((active, plan))
}
pub(super) fn activate_worker(
slots: u32,
scaffold: Option<&[MultiSeqHybridKvBuffers]>,
max_prompt_tokens: usize,
) -> Result<bool> {
let (active, plan) = decide_for_scaffold(slots, scaffold, max_prompt_tokens)?;
activation::set_worker_active(active);
tracing::info!(
active,
mode = ?activation::mode(),
slots,
per_slot_requested_bytes = plan.map(|plan| plan.per_slot_bytes),
worst_case_requested_bytes = plan.and_then(|plan| plan.total_bytes),
quota_bytes = anchor_budget::LIMIT,
"Gemma full-input checkpoint worker decision"
);
Ok(active)
}
pub(super) fn effective(params: &super::SamplingParams) -> Option<&Checkpoint> {
params
.gemma_full_input_checkpoint
.as_ref()
.filter(|_| activation::worker_active())
}
pub(super) fn is_quota_refusal(error: &anyhow::Error) -> bool {
error
.downcast_ref::<anchor_budget::ReservationError>()
.is_some()
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) enum AnchorRole {
FullInput,
ExactImage,
StableBoundary,
StableBatchBoundary,
RequestStartRollback,
}
pub(super) fn capture_or_skip<T>(
slot: crate::serve::multi_seq_kv::SlotId,
role: AnchorRole,
capture: Result<T>,
) -> Result<Option<T>> {
match capture {
Ok(anchor) => Ok(Some(anchor)),
Err(error) if is_quota_refusal(&error) => {
tracing::warn!(
slot = slot.0,
?role,
error = %error,
"Gemma anchor capture skipped: CPU anchor quota refused; previous anchor kept"
);
Ok(None)
}
Err(error) => Err(error),
}
}
#[cfg(test)]
pub(super) static ADMISSION_ANCHOR_RESTORES: std::sync::atomic::AtomicUsize =
std::sync::atomic::AtomicUsize::new(0);
#[cfg(test)]
pub(super) static CANCELLATION_ANCHOR_RESTORES: std::sync::atomic::AtomicUsize =
std::sync::atomic::AtomicUsize::new(0);
#[cfg(test)]
thread_local! {
static CAPTURE_COUNTS: std::cell::Cell<(usize, usize)> = const { std::cell::Cell::new((0, 0)) };
}
#[cfg(test)]
pub(super) fn capture_counts() -> (usize, usize) {
CAPTURE_COUNTS.with(|counts| counts.get())
}
pub(super) fn capture(
guard: &super::Gemma4KvGuard<'_>,
slot: crate::serve::multi_seq_kv::SlotId,
prompt: &[u32],
tag: &Checkpoint,
) -> Result<crate::inference::models::gemma4::kv_cache::GemmaHybridSlotAnchor> {
#[cfg(test)]
CAPTURE_COUNTS.with(|counts| {
let (attempts, successes) = counts.get();
counts.set((attempts + 1, successes));
});
tag.validate(prompt)?;
super::validate_gemma4_slot_cursor(guard, slot, prompt.len())?;
let hybrid = guard
.hybrid
.as_ref()
.ok_or_else(|| anyhow!("Gemma full input requires actual Hybrid KV"))?;
let anchor = crate::inference::models::gemma4::kv_cache::snapshot_gemma_hybrid_slot_anchor(
hybrid,
slot,
guard.model.weights.kv_graft_len() + prompt.len(),
)?;
tracing::info!(slot = slot.0, prompt_tokens = prompt.len(),
input_tokens_sha256 = %hex::encode(tag.tokens_sha256), cpu_payload_bytes = anchor.total_bytes(),
"Gemma full-input checkpoint captured; publication pending");
#[cfg(test)]
CAPTURE_COUNTS.with(|counts| {
let (attempts, successes) = counts.get();
counts.set((attempts, successes + 1));
});
Ok(anchor)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn gemma_full_input_identity_requires_exact_family_tool_and_full_render() {
let p = [1, 2, 3];
let tag = request_tag(true, true, &p, &p).unwrap();
assert!(tag.validate(&p).is_ok());
assert!(tag.validate(&[1, 9, 3]).is_err());
assert!(tag.validate(&[1, 2]).is_err());
assert!(request_tag(false, true, &p, &p).is_none());
assert!(request_tag(true, false, &p, &p).is_none());
assert!(request_tag(true, true, &p, &[1, 2]).is_none());
assert!(request_tag(true, true, &[], &[]).is_none());
}
#[test]
fn gemma_full_input_substrate_is_gemma_hybrid_tq8_slot_aware_one_to_four() {
for n in 1..=4 {
assert!(substrate_supported(true, Some(n), true, 8));
}
for (gemma, slots, hybrid, bits) in [
(false, Some(4), true, 8),
(true, None, true, 8),
(true, Some(0), true, 8),
(true, Some(5), true, 8),
(true, Some(4), false, 8),
(true, Some(4), true, 6),
] {
assert!(!substrate_supported(gemma, slots, hybrid, bits));
}
assert_eq!(slot_count(super::super::EngineMode::SerialFifo), None);
assert_eq!(
slot_count(super::super::EngineMode::SlotAware { max_slots: 4 }),
Some(4)
);
}
#[test]
fn gemma_full_input_tag_is_inert_on_an_inactive_worker_thread() {
let p = [7, 8, 9];
let params = super::super::SamplingParams {
gemma_full_input_checkpoint: request_tag(true, true, &p, &p),
..super::super::SamplingParams::default()
};
activation::set_worker_active(false);
assert!(effective(¶ms).is_none(), "inactive worker ignores tags");
activation::set_worker_active(true);
assert!(effective(¶ms).is_some());
activation::set_worker_active(false);
}
#[test]
fn gemma_anchor_capture_quota_refusal_skips_every_role_and_keeps_other_errors() {
use crate::serve::multi_seq_kv::SlotId;
for role in [
AnchorRole::FullInput,
AnchorRole::ExactImage,
AnchorRole::StableBoundary,
AnchorRole::StableBatchBoundary,
AnchorRole::RequestStartRollback,
] {
assert_eq!(capture_or_skip(SlotId(1), role, Ok(7)).unwrap(), Some(7));
let refused: Result<u8> = Err(anchor_budget::refusal_for_test());
assert_eq!(capture_or_skip(SlotId(1), role, refused).unwrap(), None);
let wrapped: Result<u8> =
Err(anchor_budget::refusal_for_test().context("capture Gemma anchor"));
assert_eq!(capture_or_skip(SlotId(1), role, wrapped).unwrap(), None);
let identity: Result<u8> = Err(anyhow!(
"Gemma full-input checkpoint token identity changed"
));
assert!(
capture_or_skip(SlotId(1), role, identity).is_err(),
"{role:?}"
);
}
}
}
#[cfg(test)]
mod routing_controls {
use super::super::{gemma4_cross_batch_candidate, Request, SamplingParams};
use super::*;
#[test]
fn gemma_full_input_tag_leaves_cold_batch_only_on_an_active_worker() {
let p = [1, 2, 3];
let (reply, _) = tokio::sync::oneshot::channel();
let cold = Request::Generate {
prompt_tokens: p.to_vec(),
params: SamplingParams::default(),
reply,
};
assert!(gemma4_cross_batch_candidate(&cold).is_some());
let (reply, _) = tokio::sync::oneshot::channel();
let full = Request::Generate {
prompt_tokens: p.to_vec(),
params: SamplingParams {
gemma_full_input_checkpoint: request_tag(true, true, &p, &p),
..SamplingParams::default()
},
reply,
};
activation::set_worker_active(true);
assert!(gemma4_cross_batch_candidate(&full).is_none());
activation::set_worker_active(false);
assert!(gemma4_cross_batch_candidate(&full).is_some());
}
}