use std::sync::Arc;
use lunaris_core::LunarisError;
use lunaris_retrieve::{LedgerBoostProvider, RetrievalBuilder};
use tracing::Instrument;
use crate::handle::Lunaris;
const ENV_VERIFY_WARN_THRESHOLD: &str = "LUNARIS_VERIFY_QUEUE_WARN_THRESHOLD";
const DEFAULT_VERIFY_WARN_THRESHOLD: u64 = 1000;
const VERIFY_TOPIC: &str = "__lunaris_verify__";
const DEFAULT_RECALL_K: usize = 30;
pub const ENV_ACTIVATION_BOOST: &str = "LUNARIS_ACTIVATION_BOOST";
pub(crate) fn activation_boost_enabled() -> bool {
std::env::var(ENV_ACTIVATION_BOOST).map(|v| v != "0").unwrap_or(true)
}
impl Lunaris {
pub fn recall(&self) -> RetrievalBuilder {
tracing::warn!(
"Lunaris::recall() uses Scope::dev() — migrate to engine.scoped(scope).recall() for scope-isolated retrieval"
);
let mut b = RetrievalBuilder::from_handle(self.storage(), self.keyword(), self.embedder());
if let Some(moon) = self.moon_storage() {
b = b.with_moon_storage(moon);
}
b = b.with_boost_cache(self.boost_cache.clone());
if activation_boost_enabled() {
b = b.with_boost_provider(Arc::new(LedgerBoostProvider::new(self.storage())));
}
let graph_on = self.graph_pipeline().is_enabled();
let rerank = self.recall_rerank();
if rerank.enabled {
b = b.with_root(lunaris_retrieve::production_root_reranked(
DEFAULT_RECALL_K,
graph_on,
self.reranker(),
rerank.top_in,
));
} else {
b = b.with_root(lunaris_retrieve::production_root(DEFAULT_RECALL_K, graph_on));
}
b
}
pub async fn recall_with_degraded_check(&self) -> Result<RetrievalBuilder, LunarisError> {
let span = tracing::info_span!("lunaris.recall", correlation_id = tracing::field::Empty);
async move {
let threshold = std::env::var(ENV_VERIFY_WARN_THRESHOLD)
.ok()
.and_then(|s| s.parse::<u64>().ok())
.unwrap_or(DEFAULT_VERIFY_WARN_THRESHOLD);
let degraded_signal = match self
.storage
.queue_depth(&lunaris_core::Scope::dev(), VERIFY_TOPIC, 0)
.await
{
Ok(depth) => {
tracing::debug!(
verify_queue_depth = depth,
threshold,
"recall_queue_depth_check"
);
depth > threshold
}
Err(e) => {
tracing::debug!(err = %e, "recall_queue_depth_unavailable; degraded=false");
false
}
};
let mut b = self.recall();
if degraded_signal {
b = b.with_initial_degraded(true);
}
Ok(b)
}
.instrument(span)
.await
}
}