Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix: harden backup streaming and web links
AkitaOnRailsaf26c8f42e6a
diff --git a/crates/ai-memory-cli/src/commands/serve.rs b/crates/ai-memory-cli/src/commands/serve.rsindex 5b16db4d..2b83d96e 100644--- a/crates/ai-memory-cli/src/commands/serve.rs+++ b/crates/ai-memory-cli/src/commands/serve.rs@@ -171,7 +171,7 @@ pub async fn run(config: &Config, args: ServeArgs) -> Result<()> {let bind = args.bind.unwrap_or_else(|| config.bind.clone());let cancel = CancellationToken::new();let server_clone = server.clone();- // `Host`-header allowlist for rmcp's DNS-rebinding guard.+ // `Host`-header allowlist for the HTTP DNS-rebinding guard.// Sourced from Config (which already handles the// `AI_MEMORY_ALLOWED_HOSTS=a,b,c` env-string vs.// config.toml sequence forms via the string-or-vec@@ -179,7 +179,7 @@ pub async fn run(config: &Config, args: ServeArgs) -> Result<()> {// effective list against what they intended.info!(allowed_hosts = ?config.allowed_hosts,- "MCP Host-header allowlist"+ "HTTP Host-header allowlist");// Default to stateless Streamable HTTP: each POST is serviced// independently and answered as plain `application/json`, sodiff --git a/crates/ai-memory-cli/src/config.rs b/crates/ai-memory-cli/src/config.rsindex e073559f..7934389e 100644--- a/crates/ai-memory-cli/src/config.rs+++ b/crates/ai-memory-cli/src/config.rs@@ -49,9 +49,9 @@ pub struct Config {/// `AI_MEMORY_AUTH_TOKEN` env var or `[auth].bearer_token` in/// config.toml.pub auth: AuthSettings,- /// `Host`-header allowlist for the MCP transport. rmcp's- /// `StreamableHttpService` rejects any inbound request whose- /// `Host` header doesn't match this list (DNS-rebinding defence).+ /// `Host`-header allowlist for the HTTP server. Requests whose+ /// `Host` header doesn't match this list are rejected before they+ /// reach MCP, hook, admin, or web routes (DNS-rebinding defence)./// Default is loopback only; to expose ai-memory on a LAN/// IP / `home.lan` / etc., add that authority here or pass it via/// `AI_MEMORY_ALLOWED_HOSTS=host1,host2,…` at startup.diff --git a/crates/ai-memory-mcp/src/admin.rs b/crates/ai-memory-mcp/src/admin.rsindex 5ad3ca15..14dc4292 100644--- a/crates/ai-memory-mcp/src/admin.rs+++ b/crates/ai-memory-mcp/src/admin.rs@@ -21,6 +21,7 @@use std::sync::Arc;+use std::io::Seek;use std::path::PathBuf;use ai_memory_consolidate::{@@ -184,10 +185,9 @@ async fn build_backup_tarball_file(state: &AdminState) -> anyhow::Result<tokio::.await.map_err(|e| anyhow::anyhow!("sqlite snapshot: {e}"))?;- let mut tar_file = tempfile::NamedTempFile::new()?;- let tar_path = tar_file.path().to_path_buf();+ let mut tar_file = tempfile::tempfile()?;{- let encoder = GzEncoder::new(tar_file.as_file_mut(), Compression::default());+ let encoder = GzEncoder::new(&mut tar_file, Compression::default());let mut tar = tar::Builder::new(encoder);tar.mode(tar::HeaderMode::Deterministic);@@ -209,11 +209,9 @@ async fn build_backup_tarball_file(state: &AdminState) -> anyhow::Result<tokio::let encoder = tar.into_inner()?;encoder.finish()?;}- tar_file.as_file_mut().sync_data()?;- let file = tokio::fs::File::open(&tar_path).await?;- drop(tar_file);- let _ = std::fs::remove_file(&tar_path);- Ok(file)+ tar_file.sync_data()?;+ tar_file.rewind()?;+ Ok(tokio::fs::File::from_std(tar_file))}// ---------------------------------------------------------------------diff --git a/crates/ai-memory-web/src/routes/index.rs b/crates/ai-memory-web/src/routes/index.rsindex b3b48eeb..59c0699f 100644--- a/crates/ai-memory-web/src/routes/index.rs+++ b/crates/ai-memory-web/src/routes/index.rs@@ -8,7 +8,7 @@ use axum::http::StatusCode;use axum::response::Html;use crate::state::WebState;-use crate::templates::{ProjectCard, ProjectsView, humanize};+use crate::templates::{ProjectCard, ProjectsView, humanize, project_href};/// Handler for `GET /`.pub(crate) async fn handler(@@ -24,7 +24,7 @@ pub(crate) async fn handler(.into_iter().map(|s| {let last_updated_relative = s.last_updated.as_deref().map(humanize).unwrap_or_default();- let href = format!("/web/w/{}/{}", s.workspace_name, s.project_name);+ let href = project_href(&s.workspace_name, &s.project_name);ProjectCard {workspace: s.workspace_name,project: s.project_name,diff --git a/crates/ai-memory-web/src/routes/page.rs b/crates/ai-memory-web/src/routes/page.rsindex 155111e3..b25cf2ac 100644--- a/crates/ai-memory-web/src/routes/page.rs+++ b/crates/ai-memory-web/src/routes/page.rs@@ -10,7 +10,7 @@ use axum::response::{Html, IntoResponse, Response};use crate::markdown;use crate::state::WebState;-use crate::templates::{NotFoundView, PageView, humanize};+use crate::templates::{NotFoundView, PageView, humanize, page_href, project_href};/// Handler for `GET /w/:workspace/:project/p/*path`.pub(crate) async fn handler(@@ -40,9 +40,18 @@ pub(crate) async fn handler(// in its header, so leaving it in the body duplicates it.let body_html = markdown::render(markdown::strip_leading_h1(&markdown_doc.body));+ let project_href = project_href(&workspace, &project);+ let supersedes_path = meta.supersedes.unwrap_or_default();+ let supersedes_href = if supersedes_path.is_empty() {+ String::new()+ } else {+ page_href(&workspace, &project, &supersedes_path)+ };+match (PageView {workspace,project,+ project_href,path: meta.path,title: meta.title,kind: meta.kind,@@ -50,7 +59,8 @@ pub(crate) async fn handler(pinned: meta.pinned,updated_relative: humanize(&meta.updated_at),created_relative: humanize(&meta.created_at),- supersedes_path: meta.supersedes.unwrap_or_default(),+ supersedes_path,+ supersedes_href,body_html,}.render())diff --git a/crates/ai-memory-web/src/routes/project.rs b/crates/ai-memory-web/src/routes/project.rsindex 32fc35bb..4486b7b1 100644--- a/crates/ai-memory-web/src/routes/project.rs+++ b/crates/ai-memory-web/src/routes/project.rs@@ -9,7 +9,7 @@ use axum::http::StatusCode;use axum::response::Html;use crate::state::WebState;-use crate::templates::{Folder, PageRow, ProjectView, humanize};+use crate::templates::{Folder, PageRow, ProjectView, humanize, page_href};/// Handler for `GET /w/:workspace/:project`.pub(crate) async fn handler(@@ -40,6 +40,7 @@ pub(crate) async fn handler(.unwrap_or_else(|| "(root)".to_owned());folder_map.entry(folder).or_default().push(PageRow {path: p.path.clone(),+ href: page_href(&workspace, &project, &p.path),title: p.title.clone(),kind: p.kind.clone(),updated_relative: humanize(&p.updated_at),@@ -58,6 +59,7 @@ pub(crate) async fn handler(.into_iter().map(|p| PageRow {path: p.path.clone(),+ href: page_href(&workspace, &project, &p.path),title: p.title.clone(),kind: p.kind.clone(),updated_relative: humanize(&p.updated_at),diff --git a/crates/ai-memory-web/src/routes/search.rs b/crates/ai-memory-web/src/routes/search.rsindex 2132ec2a..5eb5fd25 100644--- a/crates/ai-memory-web/src/routes/search.rs+++ b/crates/ai-memory-web/src/routes/search.rs@@ -10,7 +10,7 @@ use serde::Deserialize;use crate::markdown;use crate::state::WebState;-use crate::templates::{SearchHit, SearchView};+use crate::templates::{SearchHit, SearchView, page_href};/// Query-string parameters for the search endpoint.#[derive(Debug, Deserialize)]@@ -36,22 +36,19 @@ pub(crate) async fn handler(.await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;- // For each hit we need (workspace, project) — look up via a second- // query per hit is too expensive; instead query the page_meta inline.- // Since search_pages returns PageHit with workspace/project not yet- // separated, we query page_meta by path using the "default" workspace- // for now. The proper approach joins workspaces/projects in the FTS- // query; for v1 we accept the extra lookup.+ // Search hits carry only the page id/path/title/snippet. Resolve+ // workspace + project by page id so duplicate paths in sibling+ // projects still link to the right page.let mut results = Vec::with_capacity(raw.len());for h in raw {- // Get workspace + project by looking up full meta.- // We try across all project/workspace combos by using the raw- // search hit's path and calling a lightweight query.if let Ok(Some(m)) = state.reader.page_meta_by_id(h.id).await {+ let path = h.path.as_str().to_owned();+ let href = page_href(&m.workspace_name, &m.project_name, &path);results.push(SearchHit {workspace: m.workspace_name,project: m.project_name,- path: h.path.as_str().to_owned(),+ path,+ href,title: h.title,snippet: markdown::escape_snippet(&h.snippet),});diff --git a/crates/ai-memory-web/src/templates.rs b/crates/ai-memory-web/src/templates.rsindex 7d04016f..4db4e846 100644--- a/crates/ai-memory-web/src/templates.rs+++ b/crates/ai-memory-web/src/templates.rs@@ -2,6 +2,53 @@use askama::Template;+// ---------------------------------------------------------------------------+// URL helpers+// ---------------------------------------------------------------------------++/// Build a `/web` project URL with path segments percent-encoded.+#[must_use]+pub(crate) fn project_href(workspace: &str, project: &str) -> String {+ format!(+ "/web/w/{}/{}",+ encode_segment(workspace),+ encode_segment(project)+ )+}++/// Build a `/web` page URL with workspace/project/path percent-encoded.+#[must_use]+pub(crate) fn page_href(workspace: &str, project: &str, path: &str) -> String {+ format!(+ "{}/p/{}",+ project_href(workspace, project),+ encode_path(path)+ )+}++fn encode_path(path: &str) -> String {+ path.split('/')+ .map(encode_segment)+ .collect::<Vec<_>>()+ .join("/")+}++fn encode_segment(segment: &str) -> String {+ let mut out = String::with_capacity(segment.len());+ for byte in segment.bytes() {+ match byte {+ b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => {+ out.push(byte as char);+ }+ _ => {+ use std::fmt::Write as _;+ let _ = write!(&mut out, "%{byte:02X}");+ }+ }+ }+ out+}+// ---------------------------------------------------------------------------// Humanise helper// ---------------------------------------------------------------------------@@ -75,6 +122,8 @@ pub(crate) struct ProjectsView {pub(crate) struct PageRow {/// Relative wiki path.pub path: String,+ /// Link target for this page.+ pub href: String,/// Page title.pub title: String,/// Semantic kind badge text.@@ -117,6 +166,8 @@ pub(crate) struct PageView {pub workspace: String,/// Project name.pub project: String,+ /// Link target for the containing project.+ pub project_href: String,/// Relative wiki path.pub path: String,/// Page title.@@ -133,6 +184,8 @@ pub(crate) struct PageView {pub created_relative: String,/// Path of the page this supersedes, or empty string.pub supersedes_path: String,+ /// Link target for the superseded page, or empty string.+ pub supersedes_href: String,/// Rendered markdown body (HTML, trusted).pub body_html: String,}@@ -149,6 +202,8 @@ pub(crate) struct SearchHit {pub project: String,/// Relative wiki path.pub path: String,+ /// Link target for this hit.+ pub href: String,/// Page title.pub title: String,/// FTS5 snippet (HTML-marked with `<mark>` tags).@@ -175,3 +230,20 @@ pub(crate) struct SearchView {#[derive(Template)]#[template(path = "not_found.html")]pub(crate) struct NotFoundView {}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn href_helpers_percent_encode_segments() {+ assert_eq!(+ project_href("default space", "proj#one"),+ "/web/w/default%20space/proj%23one"+ );+ assert_eq!(+ page_href("default", "scratch", "notes/a b%25.md"),+ "/web/w/default/scratch/p/notes/a%20b%2525.md"+ );+ }+}diff --git a/crates/ai-memory-web/templates/page.html b/crates/ai-memory-web/templates/page.htmlindex 3a06dbda..ee1bbdcf 100644--- a/crates/ai-memory-web/templates/page.html+++ b/crates/ai-memory-web/templates/page.html@@ -6,7 +6,7 @@<nav class="mb-6 flex items-center gap-1 text-sm text-slate-500 dark:text-slate-400"><a href="/web/" class="hover:text-blue-600 dark:hover:text-blue-400">home</a><span>/</span>- <a href="/web/w/{{ workspace }}/{{ project }}" class="hover:text-blue-600 dark:hover:text-blue-400">{{ workspace }}/{{ project }}</a>+ <a href="{{ project_href }}" class="hover:text-blue-600 dark:hover:text-blue-400">{{ workspace }}/{{ project }}</a><span>/</span><span class="text-slate-700 dark:text-slate-200 truncate max-w-xs">{{ path }}</span></nav>@@ -32,7 +32,7 @@<span>Created {{ created_relative }}</span>{% if supersedes_path != "" %}<span>·</span>- <span>Supersedes <a href="/web/w/{{ workspace }}/{{ project }}/p/{{ supersedes_path }}" class="text-blue-600 dark:text-blue-400 underline">{{ supersedes_path }}</a></span>+ <span>Supersedes <a href="{{ supersedes_href }}" class="text-blue-600 dark:text-blue-400 underline">{{ supersedes_path }}</a></span>{% endif %}</div>diff --git a/crates/ai-memory-web/templates/project.html b/crates/ai-memory-web/templates/project.htmlindex f91f1150..6a670c8f 100644--- a/crates/ai-memory-web/templates/project.html+++ b/crates/ai-memory-web/templates/project.html@@ -27,7 +27,7 @@<ul class="space-y-0.5 pl-2">{% for page in folder.pages %}<li>- <a href="/web/w/{{ workspace }}/{{ project }}/p/{{ page.path }}"+ <a href="{{ page.href }}"class="block truncate text-sm text-slate-700 dark:text-slate-300 hover:text-blue-600 dark:hover:text-blue-400 py-0.5">{{ page.title }}</a>@@ -50,7 +50,7 @@{% for page in recent %}<li class="py-3 flex items-start justify-between gap-4"><div class="min-w-0">- <a href="/web/w/{{ workspace }}/{{ project }}/p/{{ page.path }}"+ <a href="{{ page.href }}"class="text-sm font-medium text-slate-800 dark:text-slate-100 hover:text-blue-600 dark:hover:text-blue-400 truncate block">{{ page.title }}</a>diff --git a/crates/ai-memory-web/templates/search.html b/crates/ai-memory-web/templates/search.htmlindex 64ac4745..31afddfa 100644--- a/crates/ai-memory-web/templates/search.html+++ b/crates/ai-memory-web/templates/search.html@@ -35,7 +35,7 @@<ul class="divide-y divide-slate-100 dark:divide-slate-800">{% for hit in hits %}<li class="py-4">- <a href="/web/w/{{ hit.workspace }}/{{ hit.project }}/p/{{ hit.path }}"+ <a href="{{ hit.href }}"class="text-base font-medium text-blue-700 dark:text-blue-400 hover:underline block mb-1">{{ hit.title }}</a>diff --git a/crates/ai-memory-web/tests/routes.rs b/crates/ai-memory-web/tests/routes.rsindex f364cf3f..f302dc78 100644--- a/crates/ai-memory-web/tests/routes.rs+++ b/crates/ai-memory-web/tests/routes.rs@@ -211,6 +211,49 @@ async fn smoke_search_returns_200() {);}+#[tokio::test]+async fn web_links_percent_encode_route_segments() {+ let (_tmp, store, wiki) = setup().await;+ let ws = store+ .writer+ .get_or_create_workspace("default")+ .await+ .unwrap();+ let proj = store+ .writer+ .get_or_create_project(ws, "scratch #1", None)+ .await+ .unwrap();+ store+ .writer+ .upsert_page(new_page(+ ws,+ proj,+ "notes/a b%25.md",+ "Encoded Link",+ "route encoding check",+ ))+ .await+ .unwrap();++ let app = router(store.reader.clone(), wiki.clone());+ let req = Request::builder()+ .uri("/w/default/scratch%20%231")+ .body(Body::empty())+ .unwrap();+ let resp = app.oneshot(req).await.unwrap();+ assert_eq!(resp.status(), StatusCode::OK);++ let body = axum::body::to_bytes(resp.into_body(), usize::MAX)+ .await+ .unwrap();+ let text = std::str::from_utf8(&body).unwrap();+ assert!(+ text.contains("/web/w/default/scratch%20%231/p/notes/a%20b%2525.md"),+ "expected encoded href in project response: {text}"+ );+}+#[tokio::test]async fn smoke_page_not_found_returns_404() {let (_tmp, store, wiki) = setup().await;