Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
Redact provider response bodies from consolidation error surfaces
Djalma Júnior31fb60969ab4
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 20b2a554..d399fe77 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0## [Unreleased]### Changed+- Redacted provider response bodies from the error surfaces this change+ touches: the `memory_consolidate` and `memory_auto_improve` MCP errors, the+ reranker degradation warning, the consolidator's transient-retry warning,+ the SessionEnd consolidation worker's queue `last_error` and failure logs,+ the auto-improve scheduler's claim `last_error` and tick warnings, and the+ hook router's checkpoint-fallback warning now carry a stable `class`/`status`+ summary (for example `class=provider status=400`) instead of the error's+ `Display`, which for a provider failure includes the upstream response+ body. Other error surfaces are unchanged. (#PRNUM)- Documented FutureInfra as an endpoint for the existing `openai-compat`provider. (#1026)diff --git a/Cargo.lock b/Cargo.lockindex 0015f62e..f6961c5a 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -170,6 +170,7 @@ dependencies = ["tempfile","tokio","tracing",+ "tracing-subscriber","uuid",]diff --git a/crates/ai-memory-cli/src/commands/serve.rs b/crates/ai-memory-cli/src/commands/serve.rsindex cf69c5c0..8bea5c15 100644--- a/crates/ai-memory-cli/src/commands/serve.rs+++ b/crates/ai-memory-cli/src/commands/serve.rs@@ -845,7 +845,11 @@ async fn run_session_consolidation_worker(};let terminal = retry_at.is_none();if let Err(store_error) = writer- .fail_session_consolidation(job, error.to_string(), retry_at)+ .fail_session_consolidation(+ job,+ ai_memory_consolidate::redacted_error_summary(&error),+ retry_at,+ ).await{tracing::warn!(@@ -856,7 +860,7 @@ async fn run_session_consolidation_worker();} else if terminal {tracing::error!(- %error,+ error_summary = %ai_memory_consolidate::redacted_error_summary(&error),session = %session_id,generation,attempts,@@ -864,7 +868,7 @@ async fn run_session_consolidation_worker();} else {tracing::warn!(- %error,+ error_summary = %ai_memory_consolidate::redacted_error_summary(&error),session = %session_id,generation,attempts,@@ -3777,6 +3781,53 @@ mod tests {}}+ /// A provider that answers HTTP 400 with a private body; the persisted+ /// `last_error` must stay a redacted class/status summary.+ struct Provider400ConsolidationLlm;++ impl LlmProvider for Provider400ConsolidationLlm {+ fn name(&self) -> &'static str {+ "provider-400-consolidation"+ }++ fn model(&self) -> &str {+ "test"+ }++ fn complete<'life0, 'async_trait>(+ &'life0 self,+ _request: ChatRequest,+ ) -> Pin<Box<dyn Future<Output = LlmResult<ChatResponse>> + Send + 'async_trait>>+ where+ 'life0: 'async_trait,+ Self: 'async_trait,+ {+ Box::pin(async move {+ Err(ai_memory_llm::LlmError::Provider {+ status: 400,+ body: "SENTINEL_PRIVATE_BODY".into(),+ })+ })+ }++ fn complete_structured_raw<'life0, 'async_trait>(+ &'life0 self,+ _request: ChatRequest,+ _schema: serde_json::Value,+ ) -> Pin<Box<dyn Future<Output = LlmResult<serde_json::Value>> + Send + 'async_trait>>+ where+ 'life0: 'async_trait,+ Self: 'async_trait,+ {+ Box::pin(async move {+ Err(ai_memory_llm::LlmError::Provider {+ status: 400,+ body: "SENTINEL_PRIVATE_BODY".into(),+ })+ })+ }+ }+/// #678: the pointer is process memory, so `systemctl restart` mid-session/// drops it. An unscoped read then resolved through the baked default scope/// and reported zero counts for a project holding thousands of observations,@@ -4059,6 +4110,140 @@ mod tests {);}+ // Mutation captured: persisting the error's `Display` writes the private+ // provider body into the queue row; the worker must persist only the+ // redacted class/status summary, and a failed first attempt keeps the+ // queue's scheduled retry instead of flipping terminal.+ #[tokio::test]+ async fn session_end_worker_persists_redacted_summary_not_provider_body() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let workspace_id = store+ .writer+ .get_or_create_workspace("default")+ .await+ .unwrap();+ let project_id = store+ .writer+ .get_or_create_project(workspace_id, "project", None)+ .await+ .unwrap();+ let session_id = SessionId::new();+ store+ .writer+ .begin_session(NewSession {+ occurred_at: None,+ id: session_id,+ workspace_id,+ project_id,+ agent_kind: AgentKind::Codex,+ cwd: None,+ actor_user: None,+ })+ .await+ .unwrap();+ store+ .writer+ .insert_observation(Sanitized::new(+ NewObservation {+ occurred_at: None,+ session_id,+ workspace_id,+ project_id,+ kind: ObservationKind::UserPrompt,+ extension: None,+ source_event: None,+ title: "finish".into(),+ body: "end the session".into(),+ importance: 8,+ },+ &Sanitizer::default(),+ ))+ .await+ .unwrap();+ store.writer.end_session(session_id, None).await.unwrap();+ store+ .writer+ .enqueue_session_consolidation(workspace_id, project_id, session_id)+ .await+ .unwrap();++ let consolidator = Arc::new(Consolidator::new(+ store.reader.clone(),+ store.writer.clone(),+ Wiki::new(tmp.path(), store.writer.clone()).unwrap(),+ Arc::new(Provider400ConsolidationLlm),+ workspace_id,+ project_id,+ ));+ let notify = Arc::new(tokio::sync::Notify::new());+ let completed = Arc::new(tokio::sync::Notify::new());+ let cancel = CancellationToken::new();+ let task = tokio::spawn(run_session_consolidation_worker(+ store.writer.clone(),+ consolidator,+ notify.clone(),+ cancel.child_token(),+ completed.clone(),+ ));+ notify.notify_one();++ // Read the queue row through a read-only connection (WAL readers do+ // not block the writer): the row exists from enqueue, is `running`+ // while claimed, and settles with a `last_error` once the failure+ // lands.+ let db = rusqlite::Connection::open(store.db_path()).unwrap();+ let deadline = std::time::Instant::now() + Duration::from_secs(5);+ let (state, last_error, next_attempt_at) = loop {+ let row = db+ .query_row(+ "SELECT state, last_error, next_attempt_at \+ FROM session_consolidation_jobs WHERE session_id = ?1",+ rusqlite::params![session_id.as_bytes()],+ |r| {+ Ok::<(String, Option<String>, Option<i64>), _>((+ r.get(0)?,+ r.get(1)?,+ r.get(2)?,+ ))+ },+ )+ .ok();+ if let Some(row) = &row+ && &row.0 != "running"+ && row.1.is_some()+ {+ break row.clone();+ }+ assert!(+ std::time::Instant::now() < deadline,+ "failure row never settled"+ );+ tokio::time::sleep(Duration::from_millis(50)).await;+ };+ cancel.cancel();+ task.await.unwrap();++ assert_eq!(+ last_error.as_deref(),+ Some("consolidation failed: class=provider status=400"),+ "only the redacted class/status summary may be persisted"+ );+ let last_error = last_error.expect("checked above");+ assert!(+ !last_error.contains("SENTINEL_PRIVATE_BODY"),+ "provider body leaked into the queue row: {last_error}"+ );+ assert_eq!(+ state, "pending",+ "a failed first attempt must keep the queue's retry"+ );+ assert!(+ next_attempt_at.is_some(),+ "the retryable failure must keep a scheduled retry"+ );+ }+async fn two_project_wiki() -> (TempDir, Store, Wiki, WorkspaceId, ProjectId, ProjectId) {let tmp = TempDir::new().unwrap();let store = Store::open(tmp.path()).unwrap();diff --git a/crates/ai-memory-consolidate/src/auto_improve.rs b/crates/ai-memory-consolidate/src/auto_improve.rsindex 1b3a5793..3eba48d9 100644--- a/crates/ai-memory-consolidate/src/auto_improve.rs+++ b/crates/ai-memory-consolidate/src/auto_improve.rs@@ -238,6 +238,34 @@ pub enum AutoImproveError {Eval(String),}+/// Redacted one-line summary of an auto-improve review failure:+/// `auto-improve failed: class=<class> status=<status-or-none>`.+///+/// For the MCP boundary where the full `Display` of [`AutoImproveError`]+/// would leak a provider response body to the tool caller+/// (`AutoImproveError::Llm` is transparent to [`LlmError`]). `class` is a+/// fixed label per variant (or [`LlmError::class`] for LLM failures) and+/// `status` is the HTTP status captured by the failure+/// ([`LlmError::http_status`]), or `none`. It never carries the cause's+/// `Display`, a response body, URL, prompt, token, or headers.+#[must_use]+pub fn redacted_auto_improve_summary(error: &AutoImproveError) -> String {+ let (class, status) = match error {+ AutoImproveError::Store(_) => ("store", None),+ AutoImproveError::Llm(llm) => (llm.class(), llm.http_status()),+ AutoImproveError::Memory(_) => ("memory", None),+ AutoImproveError::SessionNotFound(_) => ("session-not-found", None),+ AutoImproveError::SessionOutOfScope { .. } => ("session-out-of-scope", None),+ AutoImproveError::Eval(_) => ("eval", None),+ };+ format!(+ "auto-improve failed: class={class} status={}",+ status+ .map(|status| status.to_string())+ .unwrap_or_else(|| "none".into())+ )+}+/// Result alias for auto-improvement review.pub type AutoImproveResult<T> = Result<T, AutoImproveError>;@@ -1964,11 +1992,64 @@ mod tests {use ai_memory_core::{AgentKind, NewObservation, NewSession, ObservationId, ObservationKind, Sanitized, Sanitizer,};- use ai_memory_llm::{ChatResponse, LlmResult};+ use ai_memory_llm::{ChatResponse, LlmError, LlmResult};use ai_memory_store::Store;use jiff::Timestamp;use tempfile::TempDir;+ /// Body a fake provider returns in the redaction tests; the summary must+ /// never contain it.+ const AUTO_IMPROVE_REDACTION_SENTINEL: &str = "SENTINEL_PRIVATE_BODY";++ /// The summary is the only failure text the `memory_auto_improve` MCP+ /// call returns, so it must expose class/status and drop the provider+ /// body.+ #[test]+ fn auto_improve_redacted_summary_exposes_class_and_status_without_body() {+ let error = AutoImproveError::Llm(LlmError::Provider {+ status: 400,+ body: AUTO_IMPROVE_REDACTION_SENTINEL.into(),+ });+ let summary = redacted_auto_improve_summary(&error);+ assert_eq!(summary, "auto-improve failed: class=provider status=400");+ assert!(!summary.contains(AUTO_IMPROVE_REDACTION_SENTINEL));+ }++ /// Non-LLM variants take a static label with `status=none`; the shape+ /// stays stable and the cause's `Display` never enters the summary.+ #[test]+ fn auto_improve_redacted_summary_non_llm_variants_report_none_status() {+ for (error, class) in [+ (+ AutoImproveError::SessionNotFound(ai_memory_core::SessionId::new()),+ "session-not-found",+ ),+ (+ AutoImproveError::SessionOutOfScope {+ session_id: ai_memory_core::SessionId::new(),+ },+ "session-out-of-scope",+ ),+ (+ AutoImproveError::Eval(AUTO_IMPROVE_REDACTION_SENTINEL.into()),+ "eval",+ ),+ (+ AutoImproveError::Llm(LlmError::NotConfigured(+ AUTO_IMPROVE_REDACTION_SENTINEL.into(),+ )),+ "not-configured",+ ),+ ] {+ let summary = redacted_auto_improve_summary(&error);+ assert_eq!(+ summary,+ format!("auto-improve failed: class={class} status=none")+ );+ assert!(!summary.contains(AUTO_IMPROVE_REDACTION_SENTINEL));+ }+ }+struct FakeLlm;#[test]diff --git a/crates/ai-memory-consolidate/src/auto_improve_schedule.rs b/crates/ai-memory-consolidate/src/auto_improve_schedule.rsindex 9ee8cbcc..978684fb 100644--- a/crates/ai-memory-consolidate/src/auto_improve_schedule.rs+++ b/crates/ai-memory-consolidate/src/auto_improve_schedule.rs@@ -25,7 +25,10 @@ use ai_memory_wiki::Wiki;use anyhow::Result;use tracing::info;-use crate::{AutoImproveReport, AutoImproveReviewConfig, run_auto_improve_review};+use crate::{+ AutoImproveError, AutoImproveReport, AutoImproveReviewConfig, redacted_auto_improve_summary,+ run_auto_improve_review,+};/// Settings for the scheduled auto-improvement loop, already mapped from/// the host's configuration. Bundles the review config with the@@ -236,10 +239,14 @@ pub async fn run_auto_improve_scheduler_tick(}Err(e) => {outcome.errors += 1;+ // Redacted summary only: the `Display` of this+ // anyhow chain is transparent to the provider+ // error and would print the response body.+ let error_summary = redacted_scheduler_error_summary(&e);tracing::warn!(workspace = %scope.workspace_name,project = %scope.project_name,- error = %e,+ error_summary = %error_summary,"experience pass failed");}@@ -335,13 +342,18 @@ pub async fn run_auto_improve_scheduler_tick(// reports `errors=1` once and clean runs forever after. Release it// so the next tick retries, and let the attempt counter park it// once a deterministic failure has proved it will not recover.+ // Redacted summary only: this exact string is persisted+ // to the claim's `last_error` and logged — the `Display`+ // of the anyhow chain would leak the provider body to+ // both.+ let error_summary = redacted_scheduler_error_summary(&e);let attempts = match ctx.writer.record_auto_improve_claim_failure(ctx.workspace_id,ctx.project_id,candidate.session_id,- &e.to_string(),+ &error_summary,).await{@@ -368,7 +380,7 @@ pub async fn run_auto_improve_scheduler_tick(workspace = %scope.workspace_name,project = %scope.project_name,session_id = %candidate.session_id,- error = %e,+ error_summary = %error_summary,attempts = attempts.unwrap_or(0),parked,"scheduled auto-improve failed"@@ -381,6 +393,38 @@ pub async fn run_auto_improve_scheduler_tick(Ok(outcome)}+/// Redacted one-line summary of a scheduler failure, for the persisted claim+/// `last_error` and the tick/experience log events.+///+/// `run_scheduled_auto_improve` and `run_scheduled_experience` convert the+/// typed [`AutoImproveError`] into `anyhow::Error` on the way out, and+/// `AutoImproveError::Llm` is transparent to `LlmError` — a provider 400+/// would otherwise persist its response body to the claim and print it in+/// the tick warnings. This is the scheduler's single redaction boundary:+/// classify by the typed chain, never by its `Display` or `Debug`. A typed+/// review error reuses [`redacted_auto_improve_summary`]; a bare+/// [`LlmError`] is summarized by its class and status; anything else gets a+/// generic class so an unrecognized cause cannot smuggle free text through.+#[must_use]+fn redacted_scheduler_error_summary(error: &anyhow::Error) -> String {+ for cause in error.chain() {+ if let Some(review_error) = cause.downcast_ref::<AutoImproveError>() {+ return redacted_auto_improve_summary(review_error);+ }+ if let Some(llm_error) = cause.downcast_ref::<ai_memory_llm::LlmError>() {+ return format!(+ "auto-improve failed: class={} status={}",+ llm_error.class(),+ llm_error+ .http_status()+ .map(|status| status.to_string())+ .unwrap_or_else(|| "none".into())+ );+ }+ }+ "auto-improve failed: class=unrecognized status=none".to_string()+}+async fn run_scheduled_auto_improve(ctx: &ScheduledAutoImproveContext<'_>,session_id: SessionId,@@ -594,11 +638,12 @@ mod tests {use ai_memory_core::{AgentKind, NewObservation, NewSession, ObservationKind, Sanitized, Sanitizer,};- use ai_memory_llm::{ChatRequest, ChatResponse, LlmResult};+ use ai_memory_llm::{ChatRequest, ChatResponse, LlmError, LlmResult};use ai_memory_store::Store;use std::future::Future;use std::pin::Pin;use std::sync::Mutex;+ use std::sync::atomic::{AtomicUsize, Ordering};use tempfile::TempDir;struct PanicLlm;@@ -636,6 +681,416 @@ mod tests {}}+ /// A provider that rejects the scheduled review with a private body:+ /// the claim's persisted `last_error` and the tick warnings must carry+ /// class/status only, never the body.+ struct SchedulerSentinelLlm {+ calls: Arc<AtomicUsize>,+ }++ impl LlmProvider for SchedulerSentinelLlm {+ fn name(&self) -> &'static str {+ "scheduler-sentinel"+ }++ fn model(&self) -> &str {+ "sentinel-model"+ }++ fn complete<'life0, 'async_trait>(+ &'life0 self,+ _request: ChatRequest,+ ) -> Pin<Box<dyn Future<Output = LlmResult<ChatResponse>> + Send + 'async_trait>>+ where+ 'life0: 'async_trait,+ Self: 'async_trait,+ {+ Box::pin(async move {+ Err(LlmError::Provider {+ status: 400,+ body: "SENTINEL_PRIVATE_BODY".into(),+ })+ })+ }++ fn complete_structured_raw<'life0, 'async_trait>(+ &'life0 self,+ _request: ChatRequest,+ _schema: serde_json::Value,+ ) -> Pin<Box<dyn Future<Output = LlmResult<serde_json::Value>> + Send + 'async_trait>>+ where+ 'life0: 'async_trait,+ Self: 'async_trait,+ {+ let calls = Arc::clone(&self.calls);+ Box::pin(async move {+ calls.fetch_add(1, Ordering::SeqCst);+ Err(LlmError::Provider {+ status: 400,+ body: "SENTINEL_PRIVATE_BODY".into(),+ })+ })+ }+ }++ /// Cross-session fake that fails the experience review with a private+ /// body: the experience warnings must carry class/status only.+ struct ExperienceSentinelLlm;++ impl LlmProvider for ExperienceSentinelLlm {+ fn name(&self) -> &'static str {+ "experience-sentinel"+ }++ fn model(&self) -> &str {+ "sentinel-model"+ }++ fn complete<'life0, 'async_trait>(+ &'life0 self,+ _request: ChatRequest,+ ) -> Pin<Box<dyn Future<Output = LlmResult<ChatResponse>> + Send + 'async_trait>>+ where+ 'life0: 'async_trait,+ Self: 'async_trait,+ {+ Box::pin(async move {+ Ok(ChatResponse {+ text: "unused".into(),+ usage: None,+ model: "sentinel-model".into(),+ })+ })+ }++ fn complete_structured_raw<'life0, 'async_trait>(+ &'life0 self,+ _request: ChatRequest,+ _schema: serde_json::Value,+ ) -> Pin<Box<dyn Future<Output = LlmResult<serde_json::Value>> + Send + 'async_trait>>+ where+ 'life0: 'async_trait,+ Self: 'async_trait,+ {+ Box::pin(async move {+ Err(LlmError::Provider {+ status: 400,+ body: "SENTINEL_PRIVATE_BODY".into(),+ })+ })+ }+ }++ /// Read the persisted claim row directly. A retryable claim is not+ /// parked, so the typed parked-claims listing never sees it — the only+ /// way to assert what a single retryable failure persisted.+ fn claim_row(+ db_path: &std::path::Path,+ ws: WorkspaceId,+ proj: ProjectId,+ session: SessionId,+ ) -> (String, u32) {+ let conn = rusqlite::Connection::open(db_path).unwrap();+ conn.query_row(+ "SELECT last_error, attempts FROM auto_improve_scheduler_claims \+ WHERE workspace_id = ?1 AND project_id = ?2 AND session_id = ?3",+ rusqlite::params![ws.as_bytes(), proj.as_bytes(), session.as_bytes()],+ |row| {+ let last_error: Option<String> = row.get(0)?;+ Ok((last_error.unwrap(), row.get(1)?))+ },+ )+ .expect("the claim row must exist after a failed review")+ }++ /// A retryable provider failure must persist only the redacted+ /// class/status summary to the claim's `last_error` on every attempt,+ /// keep the existing attempts/park policy, and leave a fresh store+ /// handle (process restart) unable to find the body in the DB.+ #[tokio::test]+ async fn scheduled_retryable_failure_persists_only_class_status_summary() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let wiki = Wiki::new(tmp.path(), store.writer.clone()).unwrap();+ let ws = store+ .writer+ .get_or_create_workspace("default")+ .await+ .unwrap();+ let project = store+ .writer+ .get_or_create_project(ws, "retry-sentinel", None)+ .await+ .unwrap();+ initialize_auto_improve_scheduler_scopes(&store.reader, &store.writer)+ .await+ .unwrap();+ let session_id = seed_reviewable_session(&store, ws, project).await;+ let calls = Arc::new(AtomicUsize::new(0));+ let llm: Arc<dyn LlmProvider> = Arc::new(SchedulerSentinelLlm {+ calls: Arc::clone(&calls),+ });+ let settings = ScheduledAutoImproveSettings {+ review: AutoImproveReviewConfig {+ min_observations: 3,+ min_session_duration_secs: 0,+ ..AutoImproveReviewConfig::default()+ },+ require_approval: true,+ min_session_age_secs: 0,+ max_sessions_per_tick: 10,+ experience: None,+ };++ let expected = "auto-improve failed: class=provider status=400";+ let logs = CapturedLogs::default();+ let subscriber = tracing_subscriber::fmt()+ .with_max_level(tracing::Level::INFO)+ .with_writer(logs.clone())+ .without_time()+ .with_ansi(false)+ .finish();+ let guard = tracing::subscriber::set_default(subscriber);++ // Attempt 1: retryable provider 400 → the claim records the attempt+ // (not parked). The persisted column carries only class/status.+ let first =+ run_auto_improve_scheduler_tick(&store.reader, &store.writer, &wiki, &llm, &settings)+ .await+ .unwrap();+ assert_eq!(first.errors, 1, "the retryable failure must be reported");+ assert_eq!(+ first.parked, 0,+ "a retryable failure must not park the claim"+ );+ let (last_error, attempts) = claim_row(store.db_path(), ws, project, session_id);+ assert_eq!(attempts, 1);+ assert_eq!(+ last_error, expected,+ "the persisted last_error must be the redacted summary"+ );+ assert!(!last_error.contains("SENTINEL_PRIVATE_BODY"));++ // Attempt 2: the claim re-arms and each attempt writes only the safe+ // summary.+ let second =+ run_auto_improve_scheduler_tick(&store.reader, &store.writer, &wiki, &llm, &settings)+ .await+ .unwrap();+ assert_eq!(second.errors, 1);+ assert_eq!(second.parked, 0);+ let (last_error, attempts) = claim_row(store.db_path(), ws, project, session_id);+ assert_eq!(attempts, 2);+ assert_eq!(last_error, expected);++ // Attempt 3 exhausts the budget: the claim parks and stops being a+ // candidate — the policy is unchanged by the redaction.+ let third =+ run_auto_improve_scheduler_tick(&store.reader, &store.writer, &wiki, &llm, &settings)+ .await+ .unwrap();+ assert_eq!(third.errors, 1);+ assert_eq!(third.parked, 1, "the third attempt must park the claim");+ assert_eq!(calls.load(Ordering::SeqCst), 3);++ let fourth =+ run_auto_improve_scheduler_tick(&store.reader, &store.writer, &wiki, &llm, &settings)+ .await+ .unwrap();+ assert_eq!(fourth.errors, 0, "a parked claim must not be retried");+ assert_eq!(+ calls.load(Ordering::SeqCst),+ 3,+ "the parked claim must not call the LLM"+ );+ drop(guard);++ // No warning may carry the body; the class/status label must stay+ // diagnosable.+ let logged = String::from_utf8(logs.0.lock().unwrap().clone()).unwrap();+ assert!(+ !logged.contains("SENTINEL_PRIVATE_BODY"),+ "no provider body may reach the scheduler log: {logged}"+ );+ assert!(+ logged.contains("scheduled auto-improve failed"),+ "the failure event must fire: {logged}"+ );+ assert!(+ logged.contains("class=provider status=400"),+ "the redacted class/status must stay diagnosable: {logged}"+ );++ // Crash contract: a process death right after the write (before the+ // warning) leaves only the redacted value. A fresh handle on the+ // same DB file finds class/status, never the body.+ drop(wiki);+ drop(store);+ let restarted = Store::open(tmp.path()).unwrap();+ let (last_error, attempts) = claim_row(restarted.db_path(), ws, project, session_id);+ assert_eq!((attempts, last_error.as_str()), (3, expected));+ let parked = restarted+ .reader+ .auto_improve_parked_claims(ws, project)+ .await+ .unwrap();+ assert_eq!(parked.len(), 1);+ assert_eq!(parked[0].last_error.as_deref(), Some(expected));+ }++ /// An experience pass failure must warn with the redacted class/status+ /// and never the provider body.+ #[tokio::test]+ async fn experience_pass_failure_warns_class_status_not_body() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let wiki = Wiki::new(tmp.path(), store.writer.clone())+ .unwrap()+ .with_store_reader(store.reader.clone());+ let ws = store+ .writer+ .get_or_create_workspace("default")+ .await+ .unwrap();+ let project = store+ .writer+ .get_or_create_project(ws, "experience-sentinel", None)+ .await+ .unwrap();+ assert_eq!(+ initialize_auto_improve_scheduler_scopes(&store.reader, &store.writer)+ .await+ .unwrap(),+ (1, 0)+ );++ // Three completed sessions AFTER init, each with a summary page —+ // the cadence gate is due (same fixture as the cadence-gating test).+ tokio::time::sleep(std::time::Duration::from_millis(2)).await;+ for _ in 0..3 {+ let session_id = SessionId::new();+ store+ .writer+ .begin_session(ai_memory_core::NewSession {+ occurred_at: None,+ id: session_id,+ workspace_id: ws,+ project_id: project,+ agent_kind: ai_memory_core::AgentKind::OpenCode,+ cwd: None,+ actor_user: None,+ })+ .await+ .unwrap();+ store.writer.end_session(session_id, None).await.unwrap();+ wiki.write_page(ai_memory_wiki::WritePageRequest {+ workspace_id: ws,+ project_id: project,+ path: PagePath::new(format!("sessions/{session_id}.md")).unwrap(),+ frontmatter: serde_json::json!({"title": "session"}),+ body: "tag main then deploy; restart stack".into(),+ tier: ai_memory_core::Tier::Episodic,+ pinned: false,+ title: None,+ admission_ctx: None,+ author_id: None,+ actor: ActorContext::anonymous(),+ evidence: Vec::new(),+ })+ .await+ .unwrap();+ }++ let settings = ScheduledAutoImproveSettings {+ review: AutoImproveReviewConfig::default(),+ require_approval: true,+ min_session_age_secs: 0,+ max_sessions_per_tick: 10,+ experience: Some(crate::ExperienceConfig {+ sessions: 10,+ min_new_sessions: 3,+ ..crate::ExperienceConfig::default()+ }),+ };+ let llm: Arc<dyn LlmProvider> = Arc::new(ExperienceSentinelLlm);++ let logs = CapturedLogs::default();+ let subscriber = tracing_subscriber::fmt()+ .with_max_level(tracing::Level::INFO)+ .with_writer(logs.clone())+ .without_time()+ .with_ansi(false)+ .finish();+ let guard = tracing::subscriber::set_default(subscriber);+ let outcome =+ run_auto_improve_scheduler_tick(&store.reader, &store.writer, &wiki, &llm, &settings)+ .await+ .unwrap();+ drop(guard);++ assert_eq!(outcome.experience_runs, 0, "{outcome:?}");+ assert_eq!(+ outcome.errors, 1,+ "the experience failure must be reported once: {outcome:?}"+ );++ let logged = String::from_utf8(logs.0.lock().unwrap().clone()).unwrap();+ assert!(+ !logged.contains("SENTINEL_PRIVATE_BODY"),+ "no provider body may reach the experience log: {logged}"+ );+ assert!(+ logged.contains("experience pass failed"),+ "the failure warning must fire: {logged}"+ );+ assert!(+ logged.contains("class=provider status=400"),+ "the redacted class/status must stay diagnosable: {logged}"+ );+ }++ /// The scheduler redaction boundary classifies by the typed chain: a+ /// typed review error reuses the redacted summary, a bare `LlmError`+ /// keeps the same shape, and an unrecognized cause (including a non-LLM+ /// one whose `Display` carries the sentinel) gets a generic safe class+ /// with no free text.+ #[test]+ fn redacted_scheduler_error_summary_classifies_the_typed_chain() {+ let provider_400 = anyhow::Error::new(AutoImproveError::Llm(LlmError::Provider {+ status: 400,+ body: "SENTINEL_PRIVATE_BODY".into(),+ }));+ assert_eq!(+ redacted_scheduler_error_summary(&provider_400),+ "auto-improve failed: class=provider status=400"+ );++ // The same failure without the typed wrapper: identical shape.+ let bare = anyhow::Error::new(LlmError::Provider {+ status: 400,+ body: "SENTINEL_PRIVATE_BODY".into(),+ });+ assert_eq!(+ redacted_scheduler_error_summary(&bare),+ redacted_scheduler_error_summary(&provider_400)+ );++ // Non-LLM control: a string-only chain whose `Display` carries the+ // sentinel must not leak it.+ let opaque = anyhow::anyhow!("store blip with SENTINEL_PRIVATE_BODY details");+ assert_eq!(+ redacted_scheduler_error_summary(&opaque),+ "auto-improve failed: class=unrecognized status=none"+ );++ // A non-LLM typed variant keeps its fixed label and `none` status.+ let missing = anyhow::Error::new(AutoImproveError::SessionNotFound(SessionId::new()));+ assert_eq!(+ redacted_scheduler_error_summary(&missing),+ "auto-improve failed: class=session-not-found status=none"+ );+ }+#[tokio::test]async fn auto_improve_scheduler_startup_init_preserves_first_interval_sessions() {let tmp = TempDir::new().unwrap();diff --git a/crates/ai-memory-consolidate/src/consolidator.rs b/crates/ai-memory-consolidate/src/consolidator.rsindex 44231cb6..4bfb63c8 100644--- a/crates/ai-memory-consolidate/src/consolidator.rs+++ b/crates/ai-memory-consolidate/src/consolidator.rs@@ -63,6 +63,36 @@ impl From<serde_json::Error> for ConsolidatorError {}}+/// Redacted one-line summary of a consolidation failure:+/// `consolidation failed: class=<class> status=<status-or-none>`.+///+/// For typed boundaries where the full error text would leak a provider+/// response body — the SessionEnd queue's persisted `last_error` and the+/// `McpError` returned by `memory_consolidate`. It keeps what an operator+/// needs to diagnose: `class` is a fixed label per `ConsolidatorError`+/// variant (or [`LlmError::class`] for LLM failures) and `status` is the+/// HTTP status captured by the failure ([`LlmError::http_status`]), or+/// `none`. It never carries the cause's `Display`, a response body, URL,+/// prompt, token, or headers.+#[must_use]+pub fn redacted_error_summary(error: &ConsolidatorError) -> String {+ let (class, status) = match error {+ ConsolidatorError::Memory(_) => ("memory", None),+ ConsolidatorError::Store(_) => ("store", None),+ ConsolidatorError::Wiki(_) => ("wiki", None),+ ConsolidatorError::Llm(llm) => (llm.class(), llm.http_status()),+ ConsolidatorError::Serde(_) => ("serde", None),+ ConsolidatorError::SessionNotFound(_) => ("session-not-found", None),+ ConsolidatorError::EmptySession(_) => ("empty-session", None),+ };+ format!(+ "consolidation failed: class={class} status={}",+ status+ .map(|status| status.to_string())+ .unwrap_or_else(|| "none".into())+ )+}+/// Result alias used by the consolidator.pub type ConsolidatorResult<T> = Result<T, ConsolidatorError>;@@ -96,10 +126,14 @@ wherematch complete_structured_with_operation_id::<T>(llm, request.clone(), operation_id).await {Ok(value) => return Ok(value),Err(e) if attempt < CONSOLIDATION_LLM_MAX_ATTEMPTS && e.is_transient() => {+ // Redacted fields only: this log line is on the consolidation+ // path, and the `Display` of a provider failure carries the+ // response body while an HTTP error's can carry the URL.warn!(attempt,max = CONSOLIDATION_LLM_MAX_ATTEMPTS,- error = %e,+ error_class = %e.class(),+ error_status = ?e.http_status(),"consolidation hit a transient LLM error; retrying shortly",);tokio::time::sleep(retry_delay).await;@@ -1845,6 +1879,64 @@ mod tests {use jiff::Timestamp;use std::sync::atomic::{AtomicUsize, Ordering};+ /// Body a fake provider returns in the redaction tests; the summary must+ /// never contain it.+ const REDACTION_SENTINEL: &str = "SENTINEL_PRIVATE_BODY";++ /// The summary is the only failure text the SessionEnd queue persists and+ /// the `memory_consolidate` MCP call returns, so it must expose the+ /// class/status and drop the provider body.+ #[test]+ fn redacted_summary_exposes_class_and_status_without_body() {+ let error = ConsolidatorError::Llm(LlmError::Provider {+ status: 400,+ body: REDACTION_SENTINEL.into(),+ });+ let summary = redacted_error_summary(&error);+ assert_eq!(summary, "consolidation failed: class=provider status=400");+ assert!(!summary.contains(REDACTION_SENTINEL));+ }++ /// Switching the variant to `Serde` changes only the allowed fields:+ /// the stable shape stays, `class`/`status` take the new values, and the+ /// body still never enters the summary.+ #[test]+ fn redacted_summary_variant_switch_changes_only_class_and_status() {+ let provider = ConsolidatorError::Llm(LlmError::Provider {+ status: 400,+ body: REDACTION_SENTINEL.into(),+ });+ let serde_error = ConsolidatorError::Serde(REDACTION_SENTINEL.into());+ let provider_summary = redacted_error_summary(&provider);+ let serde_summary = redacted_error_summary(&serde_error);+ assert_eq!(+ provider_summary,+ "consolidation failed: class=provider status=400"+ );+ assert_eq!(+ serde_summary,+ "consolidation failed: class=serde status=none"+ );+ for summary in [&provider_summary, &serde_summary] {+ assert!(summary.starts_with("consolidation failed: class="));+ assert!(!summary.contains(REDACTION_SENTINEL));+ }+ }++ /// Dropping the HTTP status changes only the status field to `none`;+ /// the class still comes from the LLM error's fixed label.+ #[test]+ fn redacted_summary_without_http_status_reports_none() {+ let without_status =+ ConsolidatorError::Llm(LlmError::NotConfigured(REDACTION_SENTINEL.into()));+ let summary = redacted_error_summary(&without_status);+ assert_eq!(+ summary,+ "consolidation failed: class=not-configured status=none"+ );+ assert!(!summary.contains(REDACTION_SENTINEL));+ }+/// Helper for prompt construction tests.fn obs_of_size(body_len: usize) -> Observation {Observation {diff --git a/crates/ai-memory-consolidate/src/lib.rs b/crates/ai-memory-consolidate/src/lib.rsindex 1156c977..f5ebf4a7 100644--- a/crates/ai-memory-consolidate/src/lib.rs+++ b/crates/ai-memory-consolidate/src/lib.rs@@ -40,7 +40,7 @@ pub use auto_improve::{DEFAULT_AUTO_IMPROVE_MIN_OBSERVATIONS, DEFAULT_AUTO_IMPROVE_MIN_SESSION_DURATION_SECS,DEFAULT_AUTO_IMPROVE_PATCHABLE_PAGE_PREFIXES, DEFAULT_AUTO_IMPROVE_PENDING_PATH,DEFAULT_AUTO_IMPROVE_PROPOSAL_ACTOR, DEFAULT_AUTO_IMPROVE_REJECTION_CONTEXT_DAYS,- default_auto_improve_eval_targets, run_auto_improve_review,+ default_auto_improve_eval_targets, redacted_auto_improve_summary, run_auto_improve_review,};pub use auto_improve_schedule::{ScheduledAutoImproveSettings, ScheduledAutoImproveTickOutcome,@@ -64,7 +64,7 @@ pub use consolidator::{BATCH_SYSTEM_PROMPT, Consolidator, ConsolidatorError, ConsolidatorResult,DEFAULT_CONSOLIDATION_INPUT_TOKEN_SAFETY_MARGIN, DEFAULT_CONSOLIDATION_MAX_INPUT_TOKENS,DEFAULT_CONSOLIDATION_MAX_OUTPUT_TOKENS, MIN_CONSOLIDATION_MAX_INPUT_TOKENS,- MIN_CONSOLIDATION_MAX_OUTPUT_TOKENS, build_batch_request,+ MIN_CONSOLIDATION_MAX_OUTPUT_TOKENS, build_batch_request, redacted_error_summary,};pub use curator::{CuratorFinding, CuratorParams, CuratorReport, render_curator_report_markdown,diff --git a/crates/ai-memory-hooks/Cargo.toml b/crates/ai-memory-hooks/Cargo.tomlindex a460025f..ca66b097 100644--- a/crates/ai-memory-hooks/Cargo.toml+++ b/crates/ai-memory-hooks/Cargo.toml@@ -35,6 +35,9 @@ tempfile.workspace = truegit2.workspace = trueai-memory-llm.workspace = trueasync-trait.workspace = true+# Per-test log capture in the checkpoint redaction tests (thread-local+# `set_default`); the same workspace dep the MCP crate's dev-deps use.+tracing-subscriber.workspace = true[lints]workspace = truediff --git a/crates/ai-memory-hooks/src/router.rs b/crates/ai-memory-hooks/src/router.rsindex a6739003..f5c7b691 100644--- a/crates/ai-memory-hooks/src/router.rs+++ b/crates/ai-memory-hooks/src/router.rs@@ -12,7 +12,7 @@ use std::collections::{HashMap, HashSet, VecDeque};use std::str::FromStr;use std::sync::{Arc, Weak};-use ai_memory_consolidate::{Consolidator, ConsolidatorError};+use ai_memory_consolidate::{Consolidator, ConsolidatorError, redacted_error_summary};use ai_memory_core::{ActiveProject, ActorKey, AgentKind, DEFAULT_WORKSPACE_NAME, Handoff, IdentityKey,MANAGED_WORKSTREAM_PACKET_MARKER, ManagedRunId, MidSessionRouting, NewHandoff, NewObservation,@@ -3885,7 +3885,7 @@ async fn consolidate_or_synth(Err(ConsolidatorError::EmptySession(_)) => return Ok(()),Err(e) if checkpoint_degrades_to_synth(&e) => {warn!(- error = %e,+ error_summary = %redacted_error_summary(&e),session = %session_id,"{}: LLM consolidation unavailable; falling back to rule-based checkpoint",checkpoint_label@@ -4852,6 +4852,143 @@ mod tests {);}+ /// A provider that fails with a private body: the checkpoint fallback+ /// warning must carry class/status only, never the body.+ struct CheckpointSentinelLlm;++ #[async_trait::async_trait]+ impl LlmProvider for CheckpointSentinelLlm {+ fn name(&self) -> &'static str {+ "checkpoint-sentinel"+ }++ fn model(&self) -> &str {+ "sentinel-model"+ }++ async fn complete(&self, _request: ChatRequest) -> LlmResult<ChatResponse> {+ Err(self.fail())+ }++ async fn complete_structured_raw(+ &self,+ _request: ChatRequest,+ _schema: serde_json::Value,+ ) -> LlmResult<serde_json::Value> {+ Err(self.fail())+ }+ }++ impl CheckpointSentinelLlm {+ fn fail(&self) -> ai_memory_llm::LlmError {+ ai_memory_llm::LlmError::Provider {+ status: 400,+ body: "SENTINEL_PRIVATE_BODY".into(),+ }+ }+ }++ /// Captures everything a subscriber writes so a test can assert on it.+ /// `set_default` is thread-local and each `#[tokio::test]` runs on its+ /// own thread, so parallel tests do not share (or fight over) a capture.+ #[derive(Clone, Default)]+ struct CapturedLog(Arc<Mutex<Vec<u8>>>);++ impl CapturedLog {+ fn text(&self) -> String {+ String::from_utf8_lossy(&self.0.lock().unwrap()).into_owned()+ }+ }++ impl std::io::Write for CapturedLog {+ fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {+ self.0.lock().unwrap().extend_from_slice(buf);+ Ok(buf.len())+ }++ fn flush(&mut self) -> std::io::Result<()> {+ Ok(())+ }+ }++ impl<'a> tracing_subscriber::fmt::MakeWriter<'a> for CapturedLog {+ type Writer = Self;++ fn make_writer(&'a self) -> Self::Writer {+ self.clone()+ }+ }++ /// Mutation captured: logging the `ConsolidatorError`'s `Display` on the+ /// fallback path copies the provider body into the hook log. Both+ /// checkpoint flavors (PreCompact and PostCompaction) run the same+ /// function, so both labels are driven against the sentinel provider.+ #[tokio::test]+ async fn checkpoint_fallback_log_carries_class_status_not_provider_body() {+ let captured = CapturedLog::default();+ let subscriber = tracing_subscriber::fmt()+ .with_writer(captured.clone())+ .with_max_level(tracing::Level::WARN)+ .with_ansi(false)+ .finish();+ let _guard = tracing::subscriber::set_default(subscriber);++ let tmp = TempDir::new().unwrap();+ let mut state = make_state(&tmp).await;+ state.consolidator = Some(Arc::new(Consolidator::new(+ state.reader.clone(),+ state.writer.clone(),+ state.wiki.clone(),+ Arc::new(CheckpointSentinelLlm),+ state.workspace_id,+ state.project_id,+ )));++ for checkpoint_label in ["pre-compact", "post-compaction"] {+ let session_id = seed_checkpoint_observation(&state, "keep-this-working-state").await;+ consolidate_or_synth(+ &state,+ session_id,+ state.workspace_id,+ state.project_id,+ AgentKind::ClaudeCode,+ checkpoint_label,+ ai_memory_core::ActorContext::anonymous(),+ )+ .await+ .expect("a provider 400 must not lose the checkpoint");++ let path = ai_memory_core::PagePath::new(format!("sessions/{session_id}.md")).unwrap();+ let page = state+ .wiki+ .read_page(state.workspace_id, state.project_id, &path)+ .unwrap_or_else(|e| panic!("rule-based checkpoint page missing: {e}"));+ assert!(+ page.body.contains("keep-this-working-state"),+ "fallback page must carry the session's observations, got: {}",+ page.body+ );+ }++ let logged = captured.text();+ for checkpoint_label in ["pre-compact", "post-compaction"] {+ assert!(+ logged.contains(&format!(+ "{checkpoint_label}: LLM consolidation unavailable; falling back to rule-based checkpoint"+ )),+ "the fallback warning must fire for {checkpoint_label}; captured log was: {logged:?}"+ );+ }+ assert!(+ logged.contains("class=provider status=400"),+ "the redacted class/status must stay diagnosable: {logged}"+ );+ assert!(+ !logged.contains("SENTINEL_PRIVATE_BODY"),+ "provider body leaked into the checkpoint log: {logged}"+ );+ }+/// A provider failure happens after the consolidate preflight. The/// fallback write must still use the same operation so consolidate-only/// admission policy and mutations run on the actual page body too.diff --git a/crates/ai-memory-mcp/src/server.rs b/crates/ai-memory-mcp/src/server.rsindex b0bf0873..8c026ffe 100644--- a/crates/ai-memory-mcp/src/server.rs+++ b/crates/ai-memory-mcp/src/server.rs@@ -2316,10 +2316,13 @@ impl AiMemoryServer {{Ok(Ok(scores)) => scores,Ok(Err(e)) => {+ // Redacted fields only: the `Display` of a provider failure+ // carries the upstream response body.tracing::warn!(reranker = reranker.name(),model = reranker.model(),- error = %e,+ error_class = %e.class(),+ error_status = ?e.http_status(),"reranker failed; keeping pre-rerank order");hits.truncate(limit);@@ -3502,7 +3505,12 @@ impl AiMemoryServer {let outcomes = consolidator.consolidate_session_multi(session_id, dry, actor, author_id, instructions).await- .map_err(|e| McpError::internal_error(e.to_string(), None))?;+ .map_err(|e| {+ McpError::internal_error(+ ai_memory_consolidate::redacted_error_summary(&e),+ None,+ )+ })?;if !dry {self.reconcile_consolidation_job(session_id).await;}@@ -3511,7 +3519,12 @@ impl AiMemoryServer {let outcome = consolidator.consolidate_session(session_id, dry, actor, author_id, instructions).await- .map_err(|e| McpError::internal_error(e.to_string(), None))?;+ .map_err(|e| {+ McpError::internal_error(+ ai_memory_consolidate::redacted_error_summary(&e),+ None,+ )+ })?;if !dry {self.reconcile_consolidation_job(session_id).await;}@@ -3627,7 +3640,12 @@ impl AiMemoryServer {let report =run_auto_improve_review(&self.reader, &**llm, ws, proj, session_id, cfg.clone()).await- .map_err(|e| McpError::internal_error(e.to_string(), None))?;+ .map_err(|e| {+ McpError::internal_error(+ ai_memory_consolidate::redacted_auto_improve_summary(&e),+ None,+ )+ })?;// Whose suggestion this is; it also scopes the one-pending-per-target// rule (V42). Only meaningful where operators are actually told apart:// on a single-operator server the caller would otherwise stage into@@ -6171,8 +6189,8 @@ mod tests {}use ai_memory_core::{- ActorContext, AuthLevel, NewObservation, NewPage, NewSession, NewUser, ObservationKind,- PagePath, Tier,+ ActorContext, AgentKind, AuthLevel, NewObservation, NewPage, NewSession, NewUser,+ ObservationKind, PagePath, SessionId, Tier,};use ai_memory_store::Store;use ai_memory_wiki::{Wiki, WritePageRequest};@@ -6319,6 +6337,9 @@ mod tests {Scores(Vec<ai_memory_llm::RerankScore>),Reverse,Fail,+ /// A provider failure with a private body: the degradation warning+ /// must carry class/status only.+ ProviderFail,}struct StubReranker {@@ -6364,6 +6385,10 @@ mod tests {StubRerankOutcome::Fail => Err(ai_memory_llm::LlmError::UnexpectedShape("stub failure".into(),)),+ StubRerankOutcome::ProviderFail => Err(ai_memory_llm::LlmError::Provider {+ status: 400,+ body: "SENTINEL_PRIVATE_BODY".into(),+ }),}}}@@ -6547,6 +6572,171 @@ mod tests {assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 0);}+ /// Captures everything a subscriber writes so a test can assert on it.+ /// `set_default` is thread-local and each `#[tokio::test]` runs on its+ /// own thread, so parallel tests do not share (or fight over) a capture.+ #[derive(Clone, Default)]+ struct CapturedLog(Arc<Mutex<Vec<u8>>>);++ impl CapturedLog {+ fn text(&self) -> String {+ String::from_utf8_lossy(&self.0.lock().unwrap()).into_owned()+ }+ }++ impl std::io::Write for CapturedLog {+ fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {+ self.0.lock().unwrap().extend_from_slice(buf);+ Ok(buf.len())+ }++ fn flush(&mut self) -> std::io::Result<()> {+ Ok(())+ }+ }++ impl<'a> tracing_subscriber::fmt::MakeWriter<'a> for CapturedLog {+ type Writer = Self;++ fn make_writer(&'a self) -> Self::Writer {+ self.clone()+ }+ }++ /// Installs the per-thread capture subscriber and returns its guard,+ /// a companion `Dispatch`, and the companion's own capture buffer.+ /// Keep all three alive until the assertions read the capture.+ ///+ /// The companion exists because tracing-core caches each callsite's+ /// interest process-wide: while exactly one dispatcher is registered,+ /// the cache is computed from the *registering* thread's default+ /// dispatcher, so a first registration by a subscriber-less thread —+ /// a parallel test hitting the same production `warn!` — pins+ /// `Interest::never` and later emissions are dropped before reaching+ /// any subscriber. With a second live `Dispatch` the rebuilder reads+ /// the registered dispatchers instead of the calling thread, so a+ /// subscriber-less registration can no longer pin `never`; the+ /// `rebuild_interest_cache()` call heals callsites that were+ /// registered (and possibly poisoned) before the companion came+ /// alive. The companion must be a distinct registered `Dispatch` — a+ /// clone of the capture dispatch is the same entry and does not count+ /// — and it must outlive the capture: dropping it early reopens the+ /// single-dispatcher window. Both dispatchers drop with the test, so+ /// no state outlives it.+ fn install_capture(+ captured: CapturedLog,+ ) -> (+ tracing::subscriber::DefaultGuard,+ tracing::Dispatch,+ CapturedLog,+ ) {+ let companion_capture = CapturedLog::default();+ let companion = tracing::Dispatch::new(+ tracing_subscriber::fmt()+ .with_writer(companion_capture.clone())+ .with_max_level(tracing::Level::WARN)+ .with_ansi(false)+ .finish(),+ );+ let subscriber = tracing_subscriber::fmt()+ .with_writer(captured)+ .with_max_level(tracing::Level::WARN)+ .with_ansi(false)+ .finish();+ let guard = tracing::subscriber::set_default(subscriber);+ tracing::callsite::rebuild_interest_cache();+ (guard, companion, companion_capture)+ }++ /// Mutation captured: formatting the `LlmError`'s `Display` into the+ /// degradation warning copies the provider body into the server log. The+ /// warning must carry class/status only, the hits keep the pre-rerank+ /// order and the limit, and the failure must not read as a successful+ /// rerank (no `rerank_score` stamped).+ #[tokio::test]+ async fn reranker_provider_failure_warning_carries_class_status_not_body() {+ let captured = CapturedLog::default();+ let (_guard, _companion, companion_log) = install_capture(captured.clone());++ let (_tmp, _store, server, _ws, _proj) = setup_server().await;+ let hits = rerank_test_hits(4);+ let original_ids: Vec<PageId> = hits.iter().map(|(hit, _)| hit.id).collect();+ let (reranker, calls, _) = stub_reranker(StubRerankOutcome::ProviderFail, Duration::ZERO);+ let server = server.with_reranker(reranker);++ // Adversarial schedule: the window this mechanism produces — a+ // subscriber-less thread registering the production failure+ // callsite first (the scheduling class behind the Windows CI+ // failure; the exact runner order was not instrumented). This+ // thread performs that first use before the emission below.+ // `join` fixes the order without any timing dependency, and the+ // thread runs its own server and reranker so the counters and+ // assertions below stay exact.+ let adversarial_hits = hits.clone();+ std::thread::spawn(move || {+ let runtime = tokio::runtime::Builder::new_current_thread()+ .enable_all()+ .build()+ .expect("adversarial first-registration runtime");+ runtime.block_on(async move {+ let (_tmp, _store, poison_server, _ws, _proj) = setup_server().await;+ let (poison_reranker, poison_calls, _) =+ stub_reranker(StubRerankOutcome::ProviderFail, Duration::ZERO);+ let result = poison_server+ .with_reranker(poison_reranker)+ .rerank_hits("query", adversarial_hits, 2)+ .await;+ assert_eq!(result.len(), 2);+ assert_eq!(poison_calls.load(std::sync::atomic::Ordering::SeqCst), 1);+ });+ })+ .join()+ .expect("adversarial first-registration thread");++ let result = server.rerank_hits("query", hits, 2).await;++ // Degradation, not success: pre-rerank order, truncated to the limit,+ // and no hit may carry a rerank score.+ assert_eq!(result.len(), 2);+ assert_eq!(+ result.iter().map(|(hit, _)| hit.id).collect::<Vec<_>>(),+ original_ids[..2].to_vec()+ );+ assert!(+ result.iter().all(|(_, explain)| explain+ .as_ref()+ .and_then(|explain| explain.rerank_score)+ .is_none()),+ "a failed rerank must not stamp rerank scores"+ );+ assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 1);++ let logged = captured.text();+ assert!(+ logged.contains("reranker failed; keeping pre-rerank order"),+ "the degradation warning must still fire; captured log was: {logged:?}"+ );+ assert!(+ logged.contains("error_class=provider"),+ "the failure class must stay diagnosable: {logged}"+ );+ assert!(+ logged.contains("error_status=Some(400)"),+ "the status must stay: {logged}"+ );+ assert!(+ !logged.contains("SENTINEL_PRIVATE_BODY"),+ "provider body leaked into the reranker log: {logged}"+ );+ // The companion dispatch only guards the process-wide interest+ // cache: nothing may flow through it.+ assert!(+ companion_log.text().is_empty(),+ "the companion dispatch must stay scoped out of the capture: {}",+ companion_log.text()+ );+ }+#[tokio::test]async fn multi_scope_query_invokes_the_reranker_once_after_fusion() {let (_tmp, store, server, ws, proj) = setup_server().await;@@ -11440,6 +11630,214 @@ mod tests {);}+ /// Provider failure for the `memory_consolidate` redaction tests: a 400+ /// whose private body must never reach the MCP error message.+ struct ConsolidationBodyFailure;++ #[async_trait::async_trait]+ impl LlmProvider for ConsolidationBodyFailure {+ fn name(&self) -> &'static str {+ "consolidation-body-failure"+ }++ fn model(&self) -> &str {+ "test"+ }++ async fn complete(+ &self,+ _request: ai_memory_llm::ChatRequest,+ ) -> ai_memory_llm::LlmResult<ai_memory_llm::ChatResponse> {+ Err(ai_memory_llm::LlmError::Provider {+ status: 400,+ body: "SENTINEL_PRIVATE_BODY".into(),+ })+ }++ async fn complete_structured_raw(+ &self,+ _request: ai_memory_llm::ChatRequest,+ _schema: serde_json::Value,+ ) -> ai_memory_llm::LlmResult<serde_json::Value> {+ Err(ai_memory_llm::LlmError::Provider {+ status: 400,+ body: "SENTINEL_PRIVATE_BODY".into(),+ })+ }+ }++ /// A completed session with one observation: the minimum a real+ /// consolidation can reach the LLM with.+ async fn seeded_completed_session(+ store: &Store,+ ws: WorkspaceId,+ proj: ProjectId,+ ) -> SessionId {+ let session_id = SessionId::new();+ store+ .writer+ .begin_session(NewSession {+ occurred_at: None,+ id: session_id,+ workspace_id: ws,+ project_id: proj,+ agent_kind: AgentKind::Codex,+ cwd: None,+ actor_user: None,+ })+ .await+ .unwrap();+ store+ .writer+ .insert_observation(Sanitized::new(+ NewObservation {+ occurred_at: None,+ session_id,+ workspace_id: ws,+ project_id: proj,+ kind: ObservationKind::UserPrompt,+ extension: None,+ source_event: None,+ title: "finish".into(),+ body: "end the session".into(),+ importance: 8,+ },+ &Sanitizer::default(),+ ))+ .await+ .unwrap();+ store.writer.end_session(session_id, None).await.unwrap();+ session_id+ }++ /// Build a server whose consolidator fails with the private-body 400.+ async fn consolidating_server_failing_with_private_body(+ tmp: &TempDir,+ store: &Store,+ ws: WorkspaceId,+ proj: ProjectId,+ ) -> AiMemoryServer {+ let wiki = Wiki::new(tmp.path(), store.writer.clone())+ .unwrap()+ .with_store_reader(store.reader.clone());+ let llm: Arc<dyn LlmProvider> = Arc::new(ConsolidationBodyFailure);+ let consolidator = Arc::new(Consolidator::new(+ store.reader.clone(),+ store.writer.clone(),+ wiki.clone(),+ llm.clone(),+ ws,+ proj,+ ));+ AiMemoryServer::new(store.reader.clone(), store.writer.clone(), ws, proj)+ .with_consolidator_arc(wiki, llm, consolidator)+ }++ // Mutation captured: mapping the consolidator error's `Display` into the+ // `McpError` copies the bounded provider body to the tool caller in both+ // consolidation modes; only the redacted class/status summary may go out.+ #[tokio::test]+ async fn memory_consolidate_single_redacts_provider_body_from_mcp_error() {+ let (tmp, store, _server, ws, proj) = setup_server().await;+ let session_id = seeded_completed_session(&store, ws, proj).await;+ let server = consolidating_server_failing_with_private_body(&tmp, &store, ws, proj).await;++ let err = server+ .memory_consolidate(+ Parameters(+ serde_json::from_value(serde_json::json!({+ "session_id": session_id.to_string()+ }))+ .unwrap(),+ ),+ OptionalParts(test_parts_default()),+ )+ .await+ .expect_err("the provider failure must surface as an MCP error");+ assert_eq!(+ err.message, "consolidation failed: class=provider status=400",+ "the MCP error must carry only the redacted class/status summary"+ );+ assert!(+ !err.message.contains("SENTINEL_PRIVATE_BODY"),+ "provider body leaked into the MCP error: {}",+ err.message+ );+ }++ // Same leak, multi-page mode: the fan-out shares the single helper, and+ // the failure must land before any page write.+ #[tokio::test]+ async fn memory_consolidate_multi_redacts_provider_body_from_mcp_error() {+ let (tmp, store, _server, ws, proj) = setup_server().await;+ let session_id = seeded_completed_session(&store, ws, proj).await;+ let server = consolidating_server_failing_with_private_body(&tmp, &store, ws, proj).await;++ let err = server+ .memory_consolidate(+ Parameters(+ serde_json::from_value(serde_json::json!({+ "session_id": session_id.to_string(),+ "multi_page": true+ }))+ .unwrap(),+ ),+ OptionalParts(test_parts_default()),+ )+ .await+ .expect_err("the provider failure must surface as an MCP error");+ assert_eq!(+ err.message, "consolidation failed: class=provider status=400",+ "the MCP error must carry only the redacted class/status summary"+ );+ assert!(+ !err.message.contains("SENTINEL_PRIVATE_BODY"),+ "provider body leaked into the MCP error: {}",+ err.message+ );+ let path = format!("sessions/{session_id}.md");+ assert!(+ store+ .reader+ .page_body_by_ids(ws, proj, &path)+ .await+ .unwrap()+ .is_none(),+ "a failed consolidation must not write the session page"+ );+ }++ // Control: a non-LLM failure (no observations) must report its own class+ // with `status=none` — the caller can still tell configuration, provider,+ // and parse failures apart — and carry no body at all.+ #[tokio::test]+ async fn memory_consolidate_non_llm_failure_reports_class_without_body() {+ let (tmp, store, _server, ws, proj) = setup_server().await;+ let server = consolidating_server_failing_with_private_body(&tmp, &store, ws, proj).await;++ // A valid UUID that never had a session row: the consolidator fails+ // before the LLM with `EmptySession`, so the provider body cannot be+ // involved at all.+ let missing = SessionId::new();+ let err = server+ .memory_consolidate(+ Parameters(+ serde_json::from_value(serde_json::json!({+ "session_id": missing.to_string()+ }))+ .unwrap(),+ ),+ OptionalParts(test_parts_default()),+ )+ .await+ .expect_err("the missing session must surface as an MCP error");+ assert_eq!(+ err.message,+ "consolidation failed: class=empty-session status=none"+ );+ assert!(!err.message.contains("SENTINEL_PRIVATE_BODY"));+ }+#[test]fn explore_gap_bucket_picks_right_label() {use ai_memory_store::BriefingSnapshot;@@ -15031,6 +15429,219 @@ mod tests {}}+ /// A provider that fails the auto-improve review with a private body:+ /// the `McpError` must carry only the redacted class/status summary.+ struct AutoImproveSentinelLlm;++ fn auto_improve_sentinel_error() -> ai_memory_llm::LlmError {+ ai_memory_llm::LlmError::Provider {+ status: 400,+ body: "SENTINEL_PRIVATE_BODY".into(),+ }+ }++ #[async_trait::async_trait]+ impl ai_memory_llm::LlmProvider for AutoImproveSentinelLlm {+ fn name(&self) -> &'static str {+ "auto-improve-sentinel"+ }++ fn model(&self) -> &str {+ "sentinel-model"+ }++ async fn complete(+ &self,+ _request: ai_memory_llm::ChatRequest,+ ) -> ai_memory_llm::LlmResult<ai_memory_llm::ChatResponse> {+ Err(auto_improve_sentinel_error())+ }++ async fn complete_structured_raw(+ &self,+ _request: ai_memory_llm::ChatRequest,+ _schema: serde_json::Value,+ ) -> ai_memory_llm::LlmResult<serde_json::Value> {+ Err(auto_improve_sentinel_error())+ }+ }++ /// A completed-scope session with one observation, enough for a review+ /// whose minimums are lowered to pass the preflight and reach the LLM.+ async fn seeded_auto_improve_session(+ store: &Store,+ ws: WorkspaceId,+ proj: ProjectId,+ ) -> SessionId {+ let session_id = SessionId::new();+ store+ .writer+ .begin_session(NewSession {+ occurred_at: None,+ id: session_id,+ workspace_id: ws,+ project_id: proj,+ agent_kind: AgentKind::Other,+ cwd: None,+ actor_user: None,+ })+ .await+ .unwrap();+ store+ .writer+ .insert_observation(Sanitized::new(+ NewObservation {+ occurred_at: None,+ session_id,+ workspace_id: ws,+ project_id: proj,+ kind: ObservationKind::UserPrompt,+ extension: None,+ source_event: None,+ title: "prompt".into(),+ body: "durable lesson worth capturing".into(),+ importance: 5,+ },+ &Sanitizer::builtin(),+ ))+ .await+ .unwrap();+ session_id+ }++ /// Build the server fixture the auto-improve tests share: real store and+ /// wiki, the sentinel provider, and a consolidator so `self.llm`/`self.wiki`+ /// are set.+ async fn auto_improve_server_with_sentinel_llm() -> (+ TempDir,+ Store,+ AiMemoryServer,+ WorkspaceId,+ ProjectId,+ SessionId,+ ) {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let wiki = Wiki::new(tmp.path(), store.writer.clone())+ .unwrap()+ .with_store_reader(store.reader.clone());+ let ws = store+ .writer+ .get_or_create_workspace("default")+ .await+ .unwrap();+ let proj = store+ .writer+ .get_or_create_project(ws, "scratch", None)+ .await+ .unwrap();+ let session_id = seeded_auto_improve_session(&store, ws, proj).await;+ let llm: Arc<dyn LlmProvider> = Arc::new(AutoImproveSentinelLlm);+ let consolidator = Arc::new(Consolidator::new(+ store.reader.clone(),+ store.writer.clone(),+ wiki.clone(),+ llm.clone(),+ ws,+ proj,+ ));+ let server = AiMemoryServer::new(store.reader.clone(), store.writer.clone(), ws, proj)+ .with_consolidator_arc(wiki, llm, consolidator);+ (tmp, store, server, ws, proj, session_id)+ }++ /// Mutation captured: mapping the `AutoImproveError`'s `Display` into the+ /// `McpError` copies the bounded provider body to the tool caller. Only+ /// the redacted class/status summary may go out, and a failed review must+ /// stage nothing.+ #[tokio::test]+ async fn memory_auto_improve_llm_failure_exposes_class_status_not_body() {+ let (_tmp, store, server, ws, proj, session_id) =+ auto_improve_server_with_sentinel_llm().await;++ let err = server+ .memory_auto_improve(+ Parameters(AutoImproveArgs {+ session_id: Some(session_id.to_string()),+ dry_run: None,+ stage: None,+ mode: None,+ project: None,+ workspace: None,+ min_observations: Some(1),+ min_session_duration_secs: Some(0),+ min_confidence: Some(0.75),+ max_input_tokens: None,+ max_proposals: Some(5),+ include_raw_fallback: None,+ }),+ OptionalParts(test_parts_default()),+ )+ .await+ .expect_err("the provider failure must surface as an MCP error");+ assert_eq!(+ err.message, "auto-improve failed: class=provider status=400",+ "the MCP error must carry only the redacted class/status summary"+ );+ assert!(+ !err.message.contains("SENTINEL_PRIVATE_BODY"),+ "provider body leaked into the MCP error: {}",+ err.message+ );+ // The review failed before staging: no proposal row may exist.+ let proposals = store+ .reader+ .list_auto_improve_proposals(ws, proj, None, 100)+ .await+ .unwrap();+ assert!(+ proposals.is_empty(),+ "a failed review must not stage proposals: {proposals:?}"+ );+ }++ /// Control: a non-LLM failure (a session that does not exist) keeps its+ /// own class with `status=none`, so configuration, provider, and parse+ /// failures stay distinguishable, and still stages nothing.+ #[tokio::test]+ async fn memory_auto_improve_missing_session_error_keeps_class_without_body() {+ let (_tmp, store, server, ws, proj, _seeded) =+ auto_improve_server_with_sentinel_llm().await;++ let missing = SessionId::new();+ let err = server+ .memory_auto_improve(+ Parameters(AutoImproveArgs {+ session_id: Some(missing.to_string()),+ dry_run: None,+ stage: None,+ mode: None,+ project: None,+ workspace: None,+ min_observations: Some(1),+ min_session_duration_secs: Some(0),+ min_confidence: Some(0.75),+ max_input_tokens: None,+ max_proposals: Some(5),+ include_raw_fallback: None,+ }),+ OptionalParts(test_parts_default()),+ )+ .await+ .expect_err("the missing session must surface as an MCP error");+ assert_eq!(+ err.message,+ "auto-improve failed: class=session-not-found status=none"+ );+ assert!(!err.message.contains("SENTINEL_PRIVATE_BODY"));+ let proposals = store+ .reader+ .list_auto_improve_proposals(ws, proj, None, 100)+ .await+ .unwrap();+ assert!(proposals.is_empty());+ }+/// Stage one pending proposal for `notes/collides.md` into/// `pending_bucket`, then run `memory_auto_improve` as `caller` against the/// same page. Returns the tool's JSON so the caller can see whether the