Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(audit): address 5 BLOCKING audit findings from today's review
AkitaOnRails65682dc496fd
diff --git a/crates/ai-memory-cli/src/commands/serve.rs b/crates/ai-memory-cli/src/commands/serve.rsindex f232e9ca..7209554e 100644--- a/crates/ai-memory-cli/src/commands/serve.rs+++ b/crates/ai-memory-cli/src/commands/serve.rs@@ -1033,12 +1033,17 @@ async fn inject_web_base_href(return resp;}let (mut parts, body) = resp.into_parts();- let bytes = match axum::body::to_bytes(body, usize::MAX).await {+ // Bound the buffer at MAX_BODY_BYTES — same cap inbound bodies use.+ // A custom-SPA `index.html` over the cap is misconfigured at the+ // operator level; refusing here keeps a runaway template (or a+ // hostile asset masquerading as text/html) from streaming+ // unbounded into memory before injection.+ let bytes = match axum::body::to_bytes(body, MAX_BODY_BYTES).await {Ok(b) => b,Err(_) => {return (StatusCode::INTERNAL_SERVER_ERROR,- "response body read error\n",+ "response body too large or read error\n",).into_response();}diff --git a/crates/ai-memory-llm/src/openai_oauth.rs b/crates/ai-memory-llm/src/openai_oauth.rsindex f730549b..15ef8154 100644--- a/crates/ai-memory-llm/src/openai_oauth.rs+++ b/crates/ai-memory-llm/src/openai_oauth.rs@@ -42,6 +42,20 @@ pub const OAUTH_SCOPES: &str = "openid profile email offline_access";const REFRESH_MARGIN_MS: u64 = 60_000;+/// Status code used when the Codex SSE stream carries a terminal error+/// event (`response.failed`/`response.incomplete`/`response.cancelled`+/// or a top-level `error` payload). 502 ("Bad Gateway") models the+/// situation accurately: the upstream connected, but the response it+/// sent was not a usable completion — the Codex backend, not+/// ai-memory's HTTP layer, produced the failure.+const SSE_TERMINAL_ERROR_STATUS: u16 = 502;++/// Trim length for the body field of an SSE-terminal error so we+/// don't echo a multi-megabyte upstream error into the logs / chain.+/// Matches the 1024 used by `OpenAiProvider::post` for HTTP 4xx/5xx+/// bodies — same defensive cap, same reasoning.+const SSE_ERROR_BODY_TRIM: usize = 1024;+/// Stored OpenAI OAuth token.#[derive(Clone)]pub struct OpenAiOAuthToken {@@ -373,6 +387,18 @@ fn parse_sse_response(body: &str) -> LlmResult<CodexResponsesResponse> {}let value = serde_json::from_str::<serde_json::Value>(trimmed)?;+ // A bare `data: {"error": {...}}` event without a `type` field+ // and without an `event:` header would otherwise fall through+ // the `_ => {}` arm and silently disappear, leaving the caller+ // with "stream closed before response.completed". Promote+ // any top-level `error` payload to a terminal error so the real+ // upstream failure reaches the caller verbatim.+ if value.get("error").is_some() {+ return Err(LlmError::Provider {+ status: SSE_TERMINAL_ERROR_STATUS,+ body: truncate_with_ellipsis(trimmed, SSE_ERROR_BODY_TRIM),+ });+ }let kind = value.get("type").and_then(|v| v.as_str())@@ -390,8 +416,8 @@ fn parse_sse_response(body: &str) -> LlmResult<CodexResponsesResponse> {}"response.failed" | "response.incomplete" | "response.cancelled" | "error" => {return Err(LlmError::Provider {- status: 502,- body: truncate_with_ellipsis(trimmed, 1024),+ status: SSE_TERMINAL_ERROR_STATUS,+ body: truncate_with_ellipsis(trimmed, SSE_ERROR_BODY_TRIM),});}_ => {}@@ -413,7 +439,20 @@ fn parse_sse_response(body: &str) -> LlmResult<CodexResponsesResponse> {data_lines.push(rest.trim_start());}}- flush_event(current_event.as_deref(), &mut data_lines)?;+ // Final flush. A stream that ended mid-`data:` chunk leaves the+ // last partial JSON in `data_lines`; surface that as a+ // truncated-stream error so the caller doesn't see a generic+ // serde parse failure when the true cause is the upstream socket+ // closing before sending the closing `}`.+ if let Err(err) = flush_event(current_event.as_deref(), &mut data_lines) {+ return match (&err, completed.is_some()) {+ (LlmError::Serde(_), false) => Err(LlmError::UnexpectedShape(+ "openai-oauth stream truncated before final event closed (incomplete JSON payload)"+ .into(),+ )),+ _ => Err(err),+ };+ }let mut response = completed.ok_or_else(|| {LlmError::UnexpectedShape("openai-oauth stream closed before response.completed".into())diff --git a/crates/ai-memory-mcp/src/admin.rs b/crates/ai-memory-mcp/src/admin.rsindex 2a2cbbee..c665d32f 100644--- a/crates/ai-memory-mcp/src/admin.rs+++ b/crates/ai-memory-mcp/src/admin.rs@@ -1483,9 +1483,19 @@ async fn handle_purge_project(files_failed.push(proj_root_str);}}- state- .wiki- .dispatch_purge_project(resolved_purge_ctx.as_ref());+ // Mirrors that track filesystem reality (a git-push mirror) want to+ // know the on-disk dir is still present even though the DB rows are+ // gone, so they can refuse to drop their own copy in violation of+ // their source of truth. Mirrors that track DB intent can ignore+ // `partial_failure`. Default-skipped on the wire so existing+ // webhook consumers see no extra key.+ let mut dispatch_ctx = resolved_purge_ctx;+ if !files_failed.is_empty()+ && let Some(ref mut c) = dispatch_ctx+ {+ c.partial_failure = true;+ }+ state.wiki.dispatch_purge_project(dispatch_ctx.as_ref());let report = PurgeProjectReport {label: summary.label,@@ -2213,23 +2223,36 @@ async fn handle_move_project(}// PURGE the source — only reached when every page copied successfully.+ // Admission must run BEFORE the DB destruction so a `failure_policy =+ // reject` webhook can still abort the second leg of the move while the+ // source is intact (mirrors `handle_purge_project`'s ordering). The+ // previous version called `Wiki::purge_project` AFTER `writer.purge_project`,+ // which ran admit AFTER the rows were gone — reject came too late.let label = format!("{}/{}", req.from_workspace, req.project);- let summary = match state.writer.purge_project(src_ws, src_proj, &label).await {- Ok(s) => s,- Err(e) => return internal_err(e.to_string()),- };-- // Remove the source's on-disk dir through Wiki::purge_project so the- // admission chain is notified (op=purge_project) before removal — a- // git-mirror drops its copy of the source project. The DB rows were- // just deleted above, so name-resolution would find nothing; pass the- // workspace/project NAMES explicitly (mirrors handle_purge_project).let purge_ctx = AdmissionContext {workspace: req.from_workspace.clone(),project: req.project.clone(),op: AdmissionOp::PurgeProject,..Default::default()};+ let resolved_purge_ctx = match state+ .wiki+ .admit_purge_project(src_ws, src_proj, Some(purge_ctx))+ .await+ {+ Ok(ctx) => ctx,+ Err(e) => return internal_err(e.to_string()),+ };++ let summary = match state.writer.purge_project(src_ws, src_proj, &label).await {+ Ok(s) => s,+ Err(e) => return internal_err(e.to_string()),+ };++ // Remove the source's on-disk dir, then dispatch the non-blocking+ // purge webhook. Pass the workspace/project NAMES we cached in+ // `resolved_purge_ctx` — the DB rows have just been deleted, so a+ // name-resolution lookup at dispatch time would find nothing.let proj_root_str = state.wiki.project_root(src_ws, src_proj)@@ -2237,17 +2260,21 @@ async fn handle_move_project(.to_string();let mut files_deleted: Vec<String> = Vec::new();let mut files_failed: Vec<String> = Vec::new();- match state- .wiki- .purge_project(src_ws, src_proj, Some(purge_ctx))- .await- {+ match state.wiki.remove_project_dir(src_ws, src_proj) {Ok(()) => files_deleted.push(proj_root_str),Err(e) => {warn!(path = %proj_root_str, error = %e, "move-project: failed to remove source dir");files_failed.push(proj_root_str);}}+ // See `handle_purge_project` for the rationale on `partial_failure`.+ let mut dispatch_ctx = resolved_purge_ctx;+ if !files_failed.is_empty()+ && let Some(ref mut c) = dispatch_ctx+ {+ c.partial_failure = true;+ }+ state.wiki.dispatch_purge_project(dispatch_ctx.as_ref());// The source project_id was just purged; if it was the published active// project, the pointer now dangles — clear it so the next hook re-resolvesdiff --git a/crates/ai-memory-mcp/src/server.rs b/crates/ai-memory-mcp/src/server.rsindex 56e51840..b5bd0330 100644--- a/crates/ai-memory-mcp/src/server.rs+++ b/crates/ai-memory-mcp/src/server.rs@@ -1773,13 +1773,18 @@ mod tests {}}- /// Both prompt surfaces must steer agents toward the H1-in-body+ /// All three prompt surfaces must steer agents toward the H1-in-body/// convention instead of passing the `title` argument. The `title`/// argument is a known source of `JSON parsing` errors when the LLM/// fails to escape quotes (issue #67); routing every "remember this"/// call through the H1 path avoids the footgun entirely.- #[test]- fn prompts_steer_write_page_toward_h1_title_convention() {+ ///+ /// The three surfaces — `MEMORY_INSTRUCTIONS`, the routing snippet+ /// (`SNIPPET_BODY`), and the per-tool `#[tool(description=...)]`+ /// string surfaced via `tools/list` — are independent and must be+ /// kept aligned per the CLAUDE.md "MCP tool surface changes" rule.+ #[tokio::test]+ async fn prompts_steer_write_page_toward_h1_title_convention() {for prompt in [MEMORY_INSTRUCTIONS, ai_memory_core::SNIPPET_BODY] {assert!(prompt.contains("H1"),@@ -1790,6 +1795,31 @@ mod tests {"prompt must tell the agent to omit the `title` argument");}+ // The third surface: the rmcp tool description sent to clients+ // via `tools/list`. Spell-checked against the same keywords so+ // that a future edit cannot silently drop the guidance from the+ // tool the agent actually inspects when deciding how to call.+ let (_tmp, _store, server, _ws, _pj) = setup_server().await;+ let tools = server.tool_router.list_all();+ let write_page = tools+ .iter()+ .find(|t| t.name == "memory_write_page")+ .expect("memory_write_page must be registered");+ let desc = write_page+ .description+ .as_deref()+ .expect("memory_write_page must carry a description");+ assert!(+ desc.contains("H1"),+ "tool description must mention the H1 title convention; got: {desc}"+ );+ assert!(+ desc.contains("Do NOT pass")+ || desc.contains("do NOT pass")+ || desc.contains("omit")+ || desc.contains("Omit"),+ "tool description must explicitly tell the agent to omit `title`; got: {desc}"+ );}/// Read tools resolve the project in the order: explicit `project`diff --git a/crates/ai-memory-wiki/src/admission.rs b/crates/ai-memory-wiki/src/admission.rsindex d0bacfc7..24e191b3 100644--- a/crates/ai-memory-wiki/src/admission.rs+++ b/crates/ai-memory-wiki/src/admission.rs@@ -89,6 +89,13 @@ pub enum FailurePolicy {Reject,}+/// serde `skip_serializing_if` helper for `bool` fields that default+/// to `false` — skip the field entirely when it carries no information+/// so existing webhook consumers don't see an unknown extra key.+fn is_false(b: &bool) -> bool {+ !b+}+fn default_timeout_ms() -> u64 {2_000}@@ -155,6 +162,16 @@ pub struct AdmissionContext {pub actor: ai_memory_core::ActorContext,/// Which lifecycle op fired the chain.pub op: AdmissionOp,+ /// Set to `true` when the underlying op completed its durable DB+ /// work but failed to complete a follow-on filesystem step (e.g.+ /// `remove_project_dir` returned an io::Error during a purge).+ /// The DB-side mutation is irrevocable; this flag lets mirrors that+ /// track filesystem reality (a git-push mirror) refuse to drop their+ /// own copy, while mirrors that track DB intent can ignore it.+ /// Always `false` for write-page chains; only purge/move-source+ /// callers set it.+ #[serde(default, skip_serializing_if = "is_false")]+ pub partial_failure: bool,/// Names from `X-Memory-Skip-Admission-Chain` (CSV at the request layer);/// matched against [`WebhookConfig::name`] to short-circuit re-entrant writes.#[serde(default, skip_serializing)]