Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
test(hooks): close assistant capture privacy gaps
AkitaOnRails07c925f2ccfa
diff --git a/crates/ai-memory-hooks/src/assistant_capture.rs b/crates/ai-memory-hooks/src/assistant_capture.rsindex 1f743990..a52ed98a 100644--- a/crates/ai-memory-hooks/src/assistant_capture.rs+++ b/crates/ai-memory-hooks/src/assistant_capture.rs@@ -196,8 +196,8 @@ pub fn apply_assistant_backstop(env: &mut HookEnvelope, server_enabled: bool) {/// This is a defense applied on BOTH sides of the wire (client pre-spool and/// server pre-envelope) and for EVERY agent/event, not just the supported pair:/// a raw assistant-message field must never reach the spool, the wire, tracing,-/// or storage unless the explicit opt-in path (later PR) re-introduces it as a-/// sanitized, capped excerpt. Only top-level keys are inspected — the same scope+/// or storage unless the explicit opt-in path re-introduces it as a sanitized,+/// capped excerpt. Only top-level keys are inspected — the same scope/// as `body_is_subagent`, and where every supported harness places the field.pub fn strip_assistant_message_raw(raw: &mut serde_json::Value) -> bool {let Some(object) = raw.as_object_mut() else {diff --git a/crates/ai-memory-hooks/src/router.rs b/crates/ai-memory-hooks/src/router.rsindex 7192c83d..1539543d 100644--- a/crates/ai-memory-hooks/src/router.rs+++ b/crates/ai-memory-hooks/src/router.rs@@ -2261,7 +2261,7 @@ mod tests {use std::sync::{Arc, Mutex};use ai_memory_consolidate::{AutoImproveReviewConfig, run_auto_improve_review};- use ai_memory_core::Sanitizer;+ use ai_memory_core::{SanitizeConfig, Sanitizer};use ai_memory_llm::{ChatRequest, ChatResponse, LlmProvider, LlmResult};use ai_memory_store::Store;use ai_memory_wiki::Wiki;@@ -6592,10 +6592,17 @@ mod tests {}#[tokio::test]- async fn privacy_protocol_evidence_never_persists_protected_tool_sentinel() {- const SENTINEL: &str = "PHASE3_PROTECTED_PATH_AND_CONTENT_7f6c";+ async fn privacy_protocol_and_assistant_capture_sentinels_never_reach_storage_or_reviewer() {+ const TOOL_SENTINEL: &str = "PHASE3_PROTECTED_PATH_AND_CONTENT_7f6c";+ const ASSISTANT_SENTINEL: &str = "ASSISTANT_PRIVATE_RESULT_9c2e";let tmp = TempDir::new().unwrap();- let state = make_state(&tmp).await;+ let mut state = make_state(&tmp).await;+ state.sanitizer = Sanitizer::new(&SanitizeConfig {+ extra_patterns: vec![ASSISTANT_SENTINEL.into()],+ allowlist: Vec::new(),+ })+ .unwrap();+ state.capture_assistant_enabled = true;let session_id = "privacy-evidence";for (event, body) in [@@ -6634,7 +6641,7 @@ mod tests {},serde_json::json!({"session_id": session_id, "tool_name": "Write",- "tool_input": { "file_path": SENTINEL }, "tool_response": SENTINEL,+ "tool_input": { "file_path": TOOL_SENTINEL }, "tool_response": TOOL_SENTINEL,"_ai_memory_capture": capture_protocol("drop", "inactive", "unknown", 99, "extracted"),}),);@@ -6658,6 +6665,33 @@ mod tests {);process(&state, metadata, None).await.unwrap();+ let mut assistant_body = serde_json::json!({+ "session_id": session_id,+ "cwd": "/repo",+ "last_assistant_message": format!("completed safely: {ASSISTANT_SENTINEL}"),+ });+ let transformed = crate::assistant_capture::transform_for_client(+ &mut assistant_body,+ AgentKind::ClaudeCode,+ HookEvent::Stop,+ );+ assert!(transformed.captured);+ assert!(assistant_body.get("last_assistant_message").is_none());+ let mut assistant = HookEnvelope::from_query_and_body(+ HookQuery {+ event: "stop".into(),+ agent: Some("claude-code".into()),+ capture_assistant: Some("true".into()),+ ..Default::default()+ },+ assistant_body,+ );+ crate::assistant_capture::apply_assistant_backstop(+ &mut assistant,+ state.capture_assistant_enabled,+ );+ process(&state, assistant, None).await.unwrap();+process(&state,HookEnvelope::from_query_and_body(@@ -6688,41 +6722,54 @@ mod tests {.unwrap().unwrap();let observations = state.reader.observations_for_session(sid).await.unwrap();- assert!(observations.iter().all(|observation| {- observation.body.is_empty() || !observation.body.contains(SENTINEL)- }));+ for sentinel in [TOOL_SENTINEL, ASSISTANT_SENTINEL] {+ assert!(observations.iter().all(|observation| {+ observation.body.is_empty() || !observation.body.contains(sentinel)+ }));+ }assert!(observations.iter().any(|observation| {observation.title == "file" && observation.body == "tool_family: file\noutcome: unknown"}));- assert!(- state- .reader- .search_observations_for_project(workspace_id, project_id, SENTINEL.into(), 10)- .await- .unwrap()- .is_empty()- );+ assert!(observations.iter().any(|observation| {+ observation.kind == ObservationKind::Stop+ && observation.body == "completed safely: [REDACTED]"+ }));+ for sentinel in [TOOL_SENTINEL, ASSISTANT_SENTINEL] {+ assert!(+ state+ .reader+ .search_observations_for_project(workspace_id, project_id, sentinel.into(), 10,)+ .await+ .unwrap()+ .is_empty()+ );+ }let page = state.reader.page_body_by_ids(workspace_id, project_id, &format!("sessions/{sid}.md")).await.unwrap().unwrap();- assert!(!page.body.contains(SENTINEL));+ assert!(!page.body.contains(TOOL_SENTINEL));+ assert!(!page.body.contains(ASSISTANT_SENTINEL));let handoff = state.reader.latest_open_handoff(workspace_id, project_id, None).await.unwrap().unwrap();- assert!(!handoff.summary.contains(SENTINEL));+ assert!(!handoff.summary.contains(TOOL_SENTINEL));+ assert!(!handoff.summary.contains(ASSISTANT_SENTINEL));assert!(!state.wiki.recent_checkpoints(20).unwrap().iter()- .any(|entry| entry.summary.contains(SENTINEL))+ .any(|entry| {+ entry.summary.contains(TOOL_SENTINEL)+ || entry.summary.contains(ASSISTANT_SENTINEL)+ }));let llm: &'static RecordingLlm = Box::leak(Box::new(RecordingLlm(Mutex::new(None))));@@ -6748,9 +6795,11 @@ mod tests {.take().expect("review called recording LLM");let request_text = format!("{:?}{:?}", request.system, request.messages);- assert!(!request_text.contains(SENTINEL));+ assert!(!request_text.contains(TOOL_SENTINEL));+ assert!(!request_text.contains(ASSISTANT_SENTINEL));let report_text = serde_json::to_string(&report).unwrap();- assert!(!report_text.contains(SENTINEL));+ assert!(!report_text.contains(TOOL_SENTINEL));+ assert!(!report_text.contains(ASSISTANT_SENTINEL));// Review is read-only: no pending sidecar or approved page is created.assert!(statediff --git a/docs/install.md b/docs/install.mdindex 9d8adab5..9231a0f3 100644--- a/docs/install.md+++ b/docs/install.md@@ -401,8 +401,9 @@ the local spool or the wire, and the server strips it defensively on arrival.excerpt of the assistant's final turn as the Stop body. It is a **doubleopt-in** — enable the server first, then the client:-1. **Server:** set `capture_assistant = true` in `config.default.toml` (or- `AI_MEMORY_CAPTURE_ASSISTANT=true`) and restart `ai-memory serve`.+1. **Server:** set `capture_assistant = true` in the live+ `<data_dir>/config.toml` (or the service's configured TOML file), or set+ `AI_MEMORY_CAPTURE_ASSISTANT=true`, then restart `ai-memory serve`.2. **Client:** re-install the Claude Code hooks with the flag:```bash