use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant};
use chrono::Utc;
use tokio::sync::{Notify, RwLock};
use tracing::debug;
use uuid::Uuid;
use memvault_core::models::InjectChannel;
use memvault_core::router::MemoryRouter;
use crate::context::SessionContext;
pub const PREFETCH_WAIT_WINDOW: Duration = Duration::from_millis(250);
pub fn ngram_window_from_env() -> usize {
std::env::var("MEMVAULT_CONTEXT_NGRAM_WINDOW")
.ok()
.and_then(|v| v.parse::<usize>().ok())
.filter(|w| *w > 0)
.unwrap_or(5)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InjectionPhase {
Deterministic,
Full,
}
pub struct InjectionState {
pub session_id: String,
pub formatted_text: String,
#[allow(dead_code)]
pub injected_memory_ids: Vec<String>,
#[allow(dead_code)]
pub updated_at: chrono::DateTime<Utc>,
#[allow(dead_code)]
pub skipped: Vec<memvault_core::models::SkippedMemory>,
pub phase: InjectionPhase,
}
pub struct InjectionEngine {
router: Arc<MemoryRouter>,
context: Arc<SessionContext>,
state: RwLock<Option<InjectionState>>,
agent_id: RwLock<String>,
generation: AtomicU64,
prefetch_notify: Notify,
}
impl InjectionEngine {
pub fn new(router: Arc<MemoryRouter>, context: Arc<SessionContext>) -> Arc<Self> {
Arc::new(Self {
router,
context,
state: RwLock::new(None),
agent_id: RwLock::new("proxy-client".to_string()),
generation: AtomicU64::new(0),
prefetch_notify: Notify::new(),
})
}
#[allow(dead_code)]
pub async fn set_agent_id(&self, agent_id: &str) {
*self.agent_id.write().await = agent_id.to_string();
}
pub async fn refresh_if_needed(&self) -> bool {
if !self.context.take_if_changed().await {
return false;
}
self.refresh().await
}
pub async fn refresh(&self) -> bool {
let agent_id = self.agent_id.read().await.clone();
if !self.router.channel_allows(&agent_id, InjectChannel::Proxy) {
debug!(agent_id = %agent_id, "proxy auto-injection skipped: not the canonical channel");
return false;
}
let context_hint = self
.context
.conversation_ngram(ngram_window_from_env())
.await;
let project = self.context.get_project().await;
let output = match self
.router
.session_start_layered(&agent_id, context_hint.as_deref(), project.as_deref())
.await
{
Ok(r) => r,
Err(e) => {
tracing::warn!(error = %e, "injection refresh failed");
return false;
}
};
let formatted = self.router.format_layered_instructions(&output);
let session_id = format!("inj_{}", Uuid::new_v4().simple());
let memory_ids: Vec<String> = output
.injected
.iter()
.map(|r| r.memory.id.clone())
.collect();
let formatted_with_session =
format!("[MEMORY CONTEXT - session: {}]\n{}", session_id, formatted);
debug!(
session_id = %session_id,
memories = memory_ids.len(),
overflow = output.overflow_count,
"injection refreshed (layered)"
);
*self.state.write().await = Some(InjectionState {
session_id,
formatted_text: formatted_with_session,
injected_memory_ids: memory_ids,
updated_at: Utc::now(),
skipped: output.skipped.clone(),
phase: InjectionPhase::Full,
});
self.prefetch_notify.notify_waiters();
true
}
pub async fn refresh_if_needed_two_phase(self: &Arc<Self>) -> bool {
if !self.context.take_if_changed().await {
return false;
}
self.refresh_two_phase().await
}
pub async fn refresh_two_phase(self: &Arc<Self>) -> bool {
let generation = self.generation.fetch_add(1, Ordering::SeqCst) + 1;
let session_id = format!("inj_{}", Uuid::new_v4().simple());
let agent_id = self.agent_id.read().await.clone();
if !self.router.channel_allows(&agent_id, InjectChannel::Proxy) {
debug!(agent_id = %agent_id, "proxy auto-injection skipped: not the canonical channel");
return false;
}
let context_hint = self
.context
.conversation_ngram(ngram_window_from_env())
.await;
let project = self.context.get_project().await;
match self
.router
.deterministic_injection(&agent_id, project.as_deref())
.await
{
Ok(results) if !results.is_empty() => {
let formatted = self.router.format_as_instructions(&results);
let formatted_with_session =
format!("[MEMORY CONTEXT - session: {}]\n{}", session_id, formatted);
let memory_ids: Vec<String> = results.iter().map(|r| r.memory.id.clone()).collect();
debug!(
session_id = %session_id,
memories = memory_ids.len(),
"deterministic fast-path injection ready"
);
*self.state.write().await = Some(InjectionState {
session_id: session_id.clone(),
formatted_text: formatted_with_session,
injected_memory_ids: memory_ids,
updated_at: Utc::now(),
skipped: Vec::new(),
phase: InjectionPhase::Deterministic,
});
self.prefetch_notify.notify_waiters();
}
Ok(_) => {
debug!("no MUST memories for deterministic fast path");
}
Err(e) => {
tracing::warn!(error = %e, "deterministic fast path failed; waiting for prefetch only");
}
}
let engine = Arc::clone(self);
let hint = context_hint.clone();
let proj = project.clone();
tokio::spawn(async move {
let output = engine
.router
.session_start_layered(&agent_id, hint.as_deref(), proj.as_deref())
.await;
if engine.generation.load(Ordering::SeqCst) != generation {
debug!(generation, "prefetch superseded by a newer refresh");
return;
}
let output = match output {
Ok(r) => r,
Err(e) => {
tracing::warn!(error = %e, "injection prefetch failed; keeping deterministic baseline");
return;
}
};
let formatted = engine.router.format_layered_instructions(&output);
let formatted_with_session =
format!("[MEMORY CONTEXT - session: {}]\n{}", session_id, formatted);
let memory_ids: Vec<String> = output
.injected
.iter()
.map(|r| r.memory.id.clone())
.collect();
debug!(
session_id = %session_id,
memories = memory_ids.len(),
overflow = output.overflow_count,
"injection prefetch complete (layered)"
);
*engine.state.write().await = Some(InjectionState {
session_id,
formatted_text: formatted_with_session,
injected_memory_ids: memory_ids,
updated_at: Utc::now(),
skipped: output.skipped.clone(),
phase: InjectionPhase::Full,
});
engine.prefetch_notify.notify_waiters();
});
true
}
pub async fn wait_full(&self, timeout: Duration) -> bool {
let deadline = Instant::now() + timeout;
loop {
let notified = self.prefetch_notify.notified();
{
let state = self.state.read().await;
if let Some(s) = state.as_ref()
&& s.phase == InjectionPhase::Full
{
return true;
}
}
let remaining = deadline.saturating_duration_since(Instant::now());
if remaining.is_zero() {
return false;
}
if tokio::time::timeout(remaining, notified).await.is_err() {
return false;
}
}
}
pub async fn get_phase(&self) -> Option<InjectionPhase> {
self.state.read().await.as_ref().map(|s| s.phase)
}
pub async fn get_current_injection(&self) -> Option<String> {
let state = self.state.read().await;
state.as_ref().map(|s| s.formatted_text.clone())
}
#[allow(dead_code)]
pub async fn get_session_id(&self) -> Option<String> {
let state = self.state.read().await;
state.as_ref().map(|s| s.session_id.clone())
}
#[allow(dead_code)]
pub async fn get_injected_memory_ids(&self) -> Vec<String> {
let state = self.state.read().await;
state
.as_ref()
.map(|s| s.injected_memory_ids.clone())
.unwrap_or_default()
}
}
#[cfg(test)]
mod tests {
use super::*;
use memvault_core::models::{Memory, MemoryType, Priority, SourceAgent};
use memvault_core::storage::MemoryStore;
use memvault_core::storage::sqlite::SqliteStore;
async fn make_engine() -> (Arc<InjectionEngine>, Arc<SessionContext>, Arc<SqliteStore>) {
let store = Arc::new(SqliteStore::in_memory().unwrap());
let router = Arc::new(MemoryRouter::new(store.clone()));
let context = SessionContext::new();
let engine = InjectionEngine::new(router, context.clone());
(engine, context, store)
}
#[tokio::test]
async fn test_new_engine_has_no_state() {
let (engine, _ctx, _store) = make_engine().await;
assert!(engine.get_current_injection().await.is_none());
assert!(engine.get_session_id().await.is_none());
assert!(engine.get_injected_memory_ids().await.is_empty());
}
#[tokio::test]
async fn test_refresh_without_changes_does_not_run() {
let (engine, _ctx, _store) = make_engine().await;
let ran = engine.refresh_if_needed().await;
assert!(!ran);
assert!(engine.get_current_injection().await.is_none());
}
#[tokio::test]
async fn test_refresh_if_needed_after_context_change() {
let (engine, ctx, _store) = make_engine().await;
ctx.observe_tool_call(
"read",
&serde_json::json!({ "path": "/repo/myapp/src/main.rs" }),
)
.await;
let ran = engine.refresh_if_needed().await;
assert!(ran, "context changed -> refresh runs");
let injection = engine.get_current_injection().await.expect("state present");
assert!(injection.contains("MEMORY CONTEXT"));
assert!(engine.get_session_id().await.unwrap().starts_with("inj_"));
}
#[tokio::test]
async fn test_refresh_after_change_only_once() {
let (engine, ctx, _store) = make_engine().await;
ctx.observe_tool_call(
"read",
&serde_json::json!({ "path": "/repo/myapp/src/main.rs" }),
)
.await;
assert!(engine.refresh_if_needed().await);
assert!(
!engine.refresh_if_needed().await,
"second refresh is a no-op until the next change"
);
}
#[tokio::test]
async fn test_refresh_saves_injection_with_memories() {
let (engine, ctx, store) = make_engine().await;
store
.save(Memory::new(
MemoryType::Preference,
"prefers vim".to_string(),
Priority::Must,
SourceAgent {
id: "proxy-client".to_string(),
agent_type: "general".to_string(),
session_id: None,
},
))
.await
.unwrap();
ctx.observe_tool_call(
"edit",
&serde_json::json!({ "path": "/repo/myapp/src/main.rs" }),
)
.await;
assert!(engine.refresh_if_needed().await);
let ids = engine.get_injected_memory_ids().await;
assert!(!ids.is_empty());
let text = engine.get_current_injection().await.unwrap();
assert!(text.contains("prefers vim"));
}
#[tokio::test]
async fn test_set_agent_id_changes_scope() {
let (engine, _ctx, _store) = make_engine().await;
engine.set_agent_id("other-agent").await;
let refreshed = engine.refresh().await;
assert!(refreshed, "refresh should report success");
let _ = engine.get_current_injection().await;
assert!(engine.get_session_id().await.is_some());
}
struct GatedEmbedder {
rx: tokio::sync::watch::Receiver<bool>,
}
#[async_trait::async_trait]
impl memvault_core::embedding::EmbeddingProvider for GatedEmbedder {
async fn embed(&self, texts: &[String]) -> memvault_core::error::Result<Vec<Vec<f32>>> {
let mut rx = self.rx.clone();
while !*rx.borrow_and_update() {
if rx.changed().await.is_err() {
break;
}
}
Ok(texts.iter().map(|_| vec![0.1_f32, 0.2]).collect())
}
fn dimension(&self) -> usize {
2
}
}
async fn make_gated_engine() -> (
Arc<InjectionEngine>,
Arc<SessionContext>,
Arc<SqliteStore>,
tokio::sync::watch::Sender<bool>,
) {
let store = Arc::new(SqliteStore::in_memory().unwrap());
let (tx, rx) = tokio::sync::watch::channel(false);
let router = Arc::new(
MemoryRouter::new(store.clone()).with_embedder(Arc::new(GatedEmbedder { rx })),
);
let context = SessionContext::new();
let engine = InjectionEngine::new(router, context.clone());
(engine, context, store, tx)
}
fn agent() -> SourceAgent {
SourceAgent {
id: "proxy-client".to_string(),
agent_type: "general".to_string(),
session_id: None,
}
}
#[tokio::test]
async fn test_two_phase_serves_deterministic_then_full() {
let (engine, ctx, store, gate) = make_gated_engine().await;
store
.save(Memory::new(
MemoryType::Preference,
"always use Rust".to_string(),
Priority::Must,
agent(),
))
.await
.unwrap();
store
.save(Memory::new(
MemoryType::Fact,
"the checkout service uses PostgreSQL".to_string(),
Priority::Reference,
agent(),
))
.await
.unwrap();
ctx.observe_tool_call(
"edit",
&serde_json::json!({ "path": "/repo/myapp/src/main.rs" }),
)
.await;
assert!(engine.refresh_if_needed_two_phase().await);
let text = engine
.get_current_injection()
.await
.expect("deterministic baseline must be available immediately");
assert!(text.contains("always use Rust"), "baseline: {}", text);
assert_eq!(
engine.get_phase().await,
Some(InjectionPhase::Deterministic)
);
assert!(
!engine.wait_full(Duration::from_millis(50)).await,
"prefetch is still blocked; wait_full must time out, not hang"
);
gate.send(true).unwrap();
assert!(
engine.wait_full(Duration::from_secs(5)).await,
"prefetch should land once the embedder unblocks"
);
assert_eq!(engine.get_phase().await, Some(InjectionPhase::Full));
let full = engine.get_current_injection().await.unwrap();
assert!(full.contains("always use Rust"), "full: {}", full);
}
#[tokio::test]
async fn test_two_phase_without_must_memories_still_reaches_full() {
let (engine, ctx, store, gate) = make_gated_engine().await;
store
.save(Memory::new(
MemoryType::Fact,
"the checkout service uses PostgreSQL".to_string(),
Priority::Reference,
agent(),
))
.await
.unwrap();
ctx.observe_tool_call(
"edit",
&serde_json::json!({ "path": "/repo/myapp/src/main.rs" }),
)
.await;
assert!(engine.refresh_if_needed_two_phase().await);
assert!(engine.get_phase().await.is_none());
gate.send(true).unwrap();
assert!(engine.wait_full(Duration::from_secs(5)).await);
assert_eq!(engine.get_phase().await, Some(InjectionPhase::Full));
}
#[tokio::test]
async fn test_two_phase_no_change_is_noop() {
let (engine, _ctx, _store, _gate) = make_gated_engine().await;
assert!(!engine.refresh_if_needed_two_phase().await);
assert!(engine.get_phase().await.is_none());
}
#[tokio::test]
async fn test_refresh_skipped_when_proxy_not_canonical() {
let store = Arc::new(SqliteStore::in_memory().unwrap());
let registry = vec![memvault_core::models::AgentProfile {
id: "proxy-client".to_string(),
agent_type: "general".to_string(),
description: String::new(),
inject_rules: memvault_core::models::InjectRules::default(),
api_key: None,
inject_channel: Some(InjectChannel::Mcp),
}];
let router = Arc::new(MemoryRouter::with_registry(store.clone(), registry));
let context = SessionContext::new();
let engine = InjectionEngine::new(router, context.clone());
context
.observe_tool_call(
"edit",
&serde_json::json!({ "path": "/repo/myapp/src/main.rs" }),
)
.await;
assert!(
!engine.refresh_if_needed().await,
"proxy auto-injection must be skipped when another channel is canonical"
);
assert!(engine.get_current_injection().await.is_none());
assert!(engine.get_phase().await.is_none());
context
.observe_tool_call(
"read",
&serde_json::json!({ "path": "/repo/myapp/src/x.rs" }),
)
.await;
assert!(!engine.refresh_if_needed_two_phase().await);
assert!(engine.get_phase().await.is_none());
}
#[tokio::test]
async fn test_refresh_allowed_when_proxy_canonical_or_unset() {
let (engine, ctx, _store) = make_engine().await;
ctx.observe_tool_call(
"edit",
&serde_json::json!({ "path": "/repo/myapp/src/main.rs" }),
)
.await;
assert!(engine.refresh_if_needed().await);
assert!(engine.get_current_injection().await.is_some());
}
}