Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

cli: phase 3 — reorg/lint/forget-sweep/embed/commit become HTTP clients

AkitaOnRailsc4c696d19c66
diff --git a/crates/ai-memory-cli/src/commands/commit.rs b/crates/ai-memory-cli/src/commands/commit.rs
index cf106e07..f5ae68a3 100644
--- a/crates/ai-memory-cli/src/commands/commit.rs
+++ b/crates/ai-memory-cli/src/commands/commit.rs
@@ -1,23 +1,53 @@
-//! `ai-memory commit` — manual wiki git commit.
+//! `ai-memory commit` — thin HTTP client for the manual wiki git commit.
-use ai_memory_store::Store;
-use ai_memory_wiki::Wiki;
-use anyhow::{Context, Result};
+use anyhow::Result;
+use serde::{Deserialize, Serialize};
use crate::cli::CommitArgs;
use crate::config::Config;
+use crate::http_client::{ServerEndpoint, post_json};
+
+/// Request sent to `POST /admin/commit`.
+#[derive(Serialize)]
+struct CommitRequest {
+ message: String,
+}
+
+/// Response from `POST /admin/commit`.
+#[derive(Deserialize)]
+struct CommitResponse {
+ committed: bool,
+ #[serde(default)]
+ oid: Option<String>,
+ #[serde(default)]
+ reason: Option<String>,
+}
/// Run the `commit` subcommand.
///
/// # Errors
-/// Returns an error if the wiki/git layer fails.
-pub fn run(config: &Config, args: CommitArgs) -> Result<()> {
- let store = Store::open(&config.data_dir)
- .with_context(|| format!("opening store at {}", config.data_dir.display()))?;
- let wiki = Wiki::new(&config.data_dir, store.writer.clone())?;
- match wiki.commit_all(&args.message)? {
- Some(oid) => println!("committed: {oid}"),
- None => println!("nothing to commit (working tree clean)"),
+/// Returns an error if the server is unreachable or returns a non-2xx
+/// response.
+pub async fn run(_config: &Config, args: CommitArgs) -> Result<()> {
+ let endpoint = ServerEndpoint::from_env();
+ let resp: CommitResponse = post_json(
+ &endpoint,
+ "/admin/commit",
+ &CommitRequest {
+ message: args.message,
+ },
+ )
+ .await?;
+ if resp.committed {
+ println!("committed: {}", resp.oid.as_deref().unwrap_or("(unknown)"));
+ } else {
+ println!(
+ "nothing to commit{}",
+ resp.reason
+ .as_deref()
+ .map(|r| format!(" ({r})"))
+ .unwrap_or_default()
+ );
}
Ok(())
}
diff --git a/crates/ai-memory-cli/src/commands/embed.rs b/crates/ai-memory-cli/src/commands/embed.rs
index 3ae7be6d..5a9847e5 100644
--- a/crates/ai-memory-cli/src/commands/embed.rs
+++ b/crates/ai-memory-cli/src/commands/embed.rs
@@ -1,120 +1,39 @@
-//! `ai-memory embed` — compute + store embeddings for every latest page.
-//!
-//! Used to backfill embeddings after attaching an embedder for the
-//! first time, or after switching `(provider, model, dim)`.
+//! `ai-memory embed` — thin HTTP client for the M9 embedding backfill.
-use ai_memory_llm::{build_embedder, embedder_from_env};
-use ai_memory_store::{Store, f32_vec_to_bytes};
-use ai_memory_wiki::Wiki;
-use anyhow::{Context, Result, bail};
-use tracing::{info, warn};
+use anyhow::Result;
+use serde::Serialize;
use crate::cli::EmbedArgs;
use crate::config::Config;
+use crate::http_client::{ServerEndpoint, post_json};
+
+/// Request sent to `POST /admin/embed`.
+#[derive(Serialize)]
+struct EmbedRequest {
+ workspace: String,
+ project: String,
+ reembed: bool,
+}
/// Run the `embed` subcommand.
///
/// # Errors
-/// Returns an error if no embedder is configured, the store cannot
-/// be opened, or any embed/store call fails.
-pub async fn run(config: &Config, args: EmbedArgs) -> Result<()> {
- let Some(cfg) = embedder_from_env()? else {
- bail!(
- "AI_MEMORY_EMBEDDING_PROVIDER is unset. Configure an embedder \
- (see CLAUDE.md / docs/design-decisions.md §4) and re-run."
- );
- };
- let provider = cfg.provider.name().to_string();
- let model = cfg.model.clone();
- let dim = cfg.dim;
- let embedder = build_embedder(cfg).context("building embedder")?;
-
- let store = Store::open(&config.data_dir)
- .with_context(|| format!("opening store at {}", config.data_dir.display()))?;
- let ws = store
- .writer
- .get_or_create_workspace(args.workspace.clone())
- .await?;
- let proj = store
- .writer
- .get_or_create_project(ws, args.project.clone(), None)
- .await?;
- let wiki = Wiki::new(&config.data_dir, store.writer.clone())?;
-
- // Snapshot all current pages.
- let candidates = store.reader.decay_candidates(ws, proj).await?;
- info!(
- provider = embedder.provider(),
- model = embedder.model(),
- dim = embedder.dim(),
- candidates = candidates.len(),
- "starting embed backfill",
- );
-
- // Optimisation: when --force is off, skip pages that already have
- // a matching embedding row.
- let already: std::collections::HashSet<_> = if args.force {
- std::collections::HashSet::new()
- } else {
- store
- .reader
- .load_embeddings(ws, proj, provider.clone(), model.clone(), dim)
- .await?
- .into_iter()
- .map(|s| s.id)
- .collect()
- };
-
- let mut embedded = 0_usize;
- let mut skipped = 0_usize;
- let mut failed = 0_usize;
- for cand in candidates {
- if !args.force && already.contains(&cand.id) {
- skipped += 1;
- continue;
- }
- let md = match wiki.read_page(&cand.path) {
- Ok(m) => m,
- Err(e) => {
- warn!(path = %cand.path, error = %e, "skip: unable to read page");
- failed += 1;
- continue;
- }
- };
- if args.dry_run {
- embedded += 1;
- continue;
- }
- let vec = match embedder.embed(&md.body).await {
- Ok(v) => v,
- Err(e) => {
- warn!(path = %cand.path, error = %e, "embed call failed");
- failed += 1;
- continue;
- }
- };
- let bytes = f32_vec_to_bytes(&vec);
- if let Err(e) = store
- .writer
- .store_embedding(cand.id, bytes, provider.clone(), model.clone(), dim)
- .await
- {
- warn!(path = %cand.path, error = %e, "store_embedding failed");
- failed += 1;
- continue;
- }
- embedded += 1;
- }
-
- let report = serde_json::json!({
- "dry_run": args.dry_run,
- "provider": provider,
- "model": model,
- "dim": dim,
- "embedded": embedded,
- "skipped": skipped,
- "failed": failed,
- });
+/// Returns an error if the server is unreachable or returns a non-2xx
+/// response.
+pub async fn run(_config: &Config, args: EmbedArgs) -> Result<()> {
+ let endpoint = ServerEndpoint::from_env();
+ let report: serde_json::Value = post_json(
+ &endpoint,
+ "/admin/embed",
+ &EmbedRequest {
+ workspace: args.workspace,
+ project: args.project,
+ // The CLI flag was historically named `force`; the server
+ // field is `reembed` — map them here.
+ reembed: args.force,
+ },
+ )
+ .await?;
println!("{}", serde_json::to_string_pretty(&report)?);
Ok(())
}
diff --git a/crates/ai-memory-cli/src/commands/forget_sweep.rs b/crates/ai-memory-cli/src/commands/forget_sweep.rs
index 36704bee..b08c2404 100644
--- a/crates/ai-memory-cli/src/commands/forget_sweep.rs
+++ b/crates/ai-memory-cli/src/commands/forget_sweep.rs
@@ -1,31 +1,35 @@
-//! `ai-memory forget-sweep` — run the M8 retention sweep manually.
+//! `ai-memory forget-sweep` — thin HTTP client for the M8 retention sweep.
-use ai_memory_consolidate::run_sweep;
-use ai_memory_store::Store;
-use anyhow::{Context, Result};
+use anyhow::Result;
+use serde::Serialize;
use crate::cli::ForgetSweepArgs;
use crate::config::Config;
+use crate::http_client::{ServerEndpoint, post_json};
+
+/// Request sent to `POST /admin/forget-sweep`.
+#[derive(Serialize)]
+struct ForgetSweepRequest {
+ workspace: String,
+ project: String,
+ dry_run: bool,
+}
/// Run the `forget-sweep` subcommand.
///
/// # Errors
-/// Returns an error if the store cannot be opened or the sweep fails.
-pub async fn run(config: &Config, args: ForgetSweepArgs) -> Result<()> {
- let store = Store::open(&config.data_dir)
- .with_context(|| format!("opening store at {}", config.data_dir.display()))?;
- let ws = store.writer.get_or_create_workspace("default").await?;
- let proj = store
- .writer
- .get_or_create_project(ws, "scratch", None)
- .await?;
- let report = run_sweep(
- &store.reader,
- &store.writer,
- ws,
- proj,
- &config.decay,
- args.dry_run,
+/// Returns an error if the server is unreachable or returns a non-2xx
+/// response.
+pub async fn run(_config: &Config, args: ForgetSweepArgs) -> Result<()> {
+ let endpoint = ServerEndpoint::from_env();
+ let report: serde_json::Value = post_json(
+ &endpoint,
+ "/admin/forget-sweep",
+ &ForgetSweepRequest {
+ workspace: "default".to_string(),
+ project: "scratch".to_string(),
+ dry_run: args.dry_run,
+ },
)
.await?;
println!("{}", serde_json::to_string_pretty(&report)?);
diff --git a/crates/ai-memory-cli/src/commands/lint.rs b/crates/ai-memory-cli/src/commands/lint.rs
index 297d042f..fd09d329 100644
--- a/crates/ai-memory-cli/src/commands/lint.rs
+++ b/crates/ai-memory-cli/src/commands/lint.rs
@@ -1,40 +1,37 @@
-//! `ai-memory lint` — run the M8 lint pass manually.
+//! `ai-memory lint` — thin HTTP client for the M8 lint pass.
-use ai_memory_consolidate::run_lint;
-use ai_memory_llm::{build_provider, provider_from_env};
-use ai_memory_store::Store;
-use ai_memory_wiki::Wiki;
-use anyhow::{Context, Result};
+use anyhow::Result;
+use serde::Serialize;
use crate::cli::LintArgs;
use crate::config::Config;
+use crate::http_client::{ServerEndpoint, post_json};
+
+/// Request sent to `POST /admin/lint`.
+#[derive(Serialize)]
+struct LintRequest {
+ workspace: String,
+ project: String,
+ dry_run: bool,
+}
/// Run the `lint` subcommand.
///
/// # Errors
-/// Returns an error if the store cannot be opened or the lint pass
-/// fails.
-pub async fn run(config: &Config, args: LintArgs) -> Result<()> {
- let store = Store::open(&config.data_dir)
- .with_context(|| format!("opening store at {}", config.data_dir.display()))?;
- let ws = store
- .writer
- .get_or_create_workspace(args.workspace.clone())
- .await?;
- let proj = store
- .writer
- .get_or_create_project(ws, args.project.clone(), None)
- .await?;
- let wiki = Wiki::new(&config.data_dir, store.writer.clone())?;
-
- // Build provider if env configured; otherwise rule-based only.
- let llm = if let Some(cfg) = provider_from_env()? {
- Some(build_provider(cfg)?)
- } else {
- None
- };
-
- let report = run_lint(&store.reader, &wiki, llm.as_ref(), ws, proj, args.dry_run).await?;
+/// Returns an error if the server is unreachable or returns a non-2xx
+/// response.
+pub async fn run(_config: &Config, args: LintArgs) -> Result<()> {
+ let endpoint = ServerEndpoint::from_env();
+ let report: serde_json::Value = post_json(
+ &endpoint,
+ "/admin/lint",
+ &LintRequest {
+ workspace: args.workspace,
+ project: args.project,
+ dry_run: args.dry_run,
+ },
+ )
+ .await?;
println!("{}", serde_json::to_string_pretty(&report)?);
Ok(())
}
diff --git a/crates/ai-memory-cli/src/commands/reorg.rs b/crates/ai-memory-cli/src/commands/reorg.rs
index bd939c61..34122b9d 100644
--- a/crates/ai-memory-cli/src/commands/reorg.rs
+++ b/crates/ai-memory-cli/src/commands/reorg.rs
@@ -1,393 +1,89 @@
-//! `ai-memory reorg --by-cwd [--dry-run]`
+//! `ai-memory reorg --dry-run`
//!
-//! Retro-fits existing sessions + observations to per-cwd projects based
-//! on the `cwd` captured at session-start. Pages are graveyarded
-//! (`is_latest=0`) because they were synthesised as a mash-up across all
-//! sessions that used the old single-project bucket; a fresh consolidation
-//! will regenerate them cleanly per project. The operation is idempotent:
-//! sessions already in the right project are skipped.
+//! Thin HTTP client — delegates all store reads/writes to the server.
-use ai_memory_core::{ProjectId, SessionId, WorkspaceId};
-use ai_memory_store::{Store, StoreError};
use anyhow::Result;
-use tracing::info;
+use serde::{Deserialize, Serialize};
use crate::cli::ReorgArgs;
use crate::config::Config;
+use crate::http_client::{ServerEndpoint, post_json};
-/// A single entry in the reorg plan.
-#[derive(Debug)]
-struct PlanEntry {
- session_id: SessionId,
- new_project_id: ProjectId,
+/// Request sent to `POST /admin/reorg`.
+#[derive(Serialize)]
+struct ReorgRequest {
+ dry_run: bool,
+}
+
+/// One entry in the server's reorg plan.
+#[derive(Deserialize)]
+struct ReorgPlanEntry {
+ session_id: String,
cwd: String,
- project_name: String,
+ new_project: String,
+}
+
+/// Summary counts from the server response.
+#[derive(Deserialize)]
+struct ReorgSummaryJson {
+ sessions_moved: usize,
+ observations_updated: usize,
+ pages_graveyarded: usize,
+ distinct_new_projects: usize,
+}
+
+/// Full response from `POST /admin/reorg`.
+#[derive(Deserialize)]
+struct ReorgReport {
+ dry_run: bool,
+ plan: Vec<ReorgPlanEntry>,
+ summary: ReorgSummaryJson,
}
/// Run the `reorg` subcommand.
///
/// # Errors
-/// Returns an error if the store cannot be opened or if any SQL operation
-/// fails.
-pub async fn run(config: &Config, args: ReorgArgs) -> Result<()> {
- let store = Store::open(&config.data_dir)?;
-
- // Step 1: Ensure the `default` workspace exists (idempotent).
- let ws = store.writer.get_or_create_workspace("default").await?;
-
- // Step 2: Read all sessions with a non-NULL, non-empty cwd.
- let sessions_with_cwd: Vec<(SessionId, ProjectId, String)> = store
- .reader
- .with_conn(move |conn| {
- let mut stmt = conn.prepare(
- "SELECT id, project_id, cwd \
- FROM sessions \
- WHERE cwd IS NOT NULL AND cwd != '' \
- ORDER BY started_at",
- )?;
- let rows = stmt.query_map([], |row| {
- let id_bytes: Vec<u8> = row.get(0)?;
- let proj_bytes: Vec<u8> = row.get(1)?;
- let cwd: String = row.get(2)?;
- Ok((id_bytes, proj_bytes, cwd))
- })?;
- let mut out = Vec::new();
- for r in rows {
- let (id_bytes, proj_bytes, cwd) = r?;
- // from_slice returns MemoryError; lift through StoreError.
- let session_id = SessionId::from_slice(&id_bytes).map_err(StoreError::Memory)?;
- let project_id = ProjectId::from_slice(&proj_bytes).map_err(StoreError::Memory)?;
- out.push((session_id, project_id, cwd));
- }
- Ok(out)
- })
- .await?;
-
- if sessions_with_cwd.is_empty() {
- println!("No sessions with a cwd found; nothing to reorg.");
- return Ok(());
- }
-
- // Step 3: Derive project_name = basename(cwd) for each session and
- // resolve the target (workspace_id, project_id). Cache by cwd so we
- // call get_or_create_project once per distinct path.
- let mut cwd_to_proj: std::collections::HashMap<String, (WorkspaceId, ProjectId)> =
- std::collections::HashMap::new();
- for (_, _, cwd) in &sessions_with_cwd {
- if cwd_to_proj.contains_key(cwd.as_str()) {
- continue;
- }
- let project_name = std::path::Path::new(cwd.as_str())
- .file_name()
- .and_then(|s| s.to_str())
- .filter(|s| !s.is_empty())
- .map(str::to_string)
- .unwrap_or_else(|| "unknown".to_string());
- let proj = store
- .writer
- .get_or_create_project(ws, project_name, Some(cwd.clone()))
- .await?;
- cwd_to_proj.insert(cwd.clone(), (ws, proj));
- }
-
- // Step 4: Build the plan — only include sessions whose project_id
- // differs from what it should be (idempotency).
- let mut plan: Vec<PlanEntry> = Vec::new();
- for (session_id, old_project_id, cwd) in &sessions_with_cwd {
- let (_, new_project_id) = cwd_to_proj[cwd.as_str()];
- if new_project_id == *old_project_id {
- // Already in the right bucket.
- continue;
- }
- let project_name = std::path::Path::new(cwd.as_str())
- .file_name()
- .and_then(|s| s.to_str())
- .unwrap_or("unknown")
- .to_string();
- plan.push(PlanEntry {
- session_id: *session_id,
- new_project_id,
- cwd: cwd.clone(),
- project_name,
- });
- }
-
- // Step 5: Dry-run: print the plan as a table and exit.
- if args.dry_run || plan.is_empty() {
- if plan.is_empty() {
- println!("All sessions are already in the correct per-cwd project; nothing to do.");
- } else {
- println!("{} session(s) would be moved:\n", plan.len());
- println!("{:<38} {:<20} cwd", "session_id", "new_project");
- println!("{}", "-".repeat(80));
- for e in &plan {
- println!(
- "{:<38} {:<20} {}",
- e.session_id.to_string(),
- e.project_name,
- e.cwd,
- );
- }
- println!("\n(dry-run; omit --dry-run to apply)");
- }
+/// Returns an error if the server is unreachable or returns a non-2xx
+/// response.
+pub async fn run(_config: &Config, args: ReorgArgs) -> Result<()> {
+ let endpoint = ServerEndpoint::from_env();
+ let report: ReorgReport = post_json(
+ &endpoint,
+ "/admin/reorg",
+ &ReorgRequest {
+ dry_run: args.dry_run,
+ },
+ )
+ .await?;
+
+ if report.plan.is_empty() {
+ println!("All sessions are already in the correct per-cwd project; nothing to do.");
return Ok(());
}
- // Step 6: Execute in one transaction via the writer actor.
- let writer_plan: Vec<(SessionId, ProjectId)> = plan
- .iter()
- .map(|e| (e.session_id, e.new_project_id))
- .collect();
- let summary = store.writer.reorg_sessions(writer_plan).await?;
-
- // Count distinct new projects created (those with project_id not seen
- // in the old bucket, i.e. all entries in cwd_to_proj whose project_id
- // appears in the plan's new_project_id set).
- let distinct_projects: std::collections::HashSet<ProjectId> =
- plan.iter().map(|e| e.new_project_id).collect();
-
- info!(
- sessions_moved = summary.sessions_moved,
- observations_updated = summary.observations_updated,
- pages_graveyarded = summary.pages_graveyarded,
- distinct_projects = distinct_projects.len(),
- "reorg complete",
- );
- println!("Reorg complete:");
- println!(" sessions moved: {}", summary.sessions_moved);
- println!(" observations updated: {}", summary.observations_updated);
- println!(" pages graveyarded: {}", summary.pages_graveyarded);
- println!(" distinct new projects: {}", distinct_projects.len());
- Ok(())
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use ai_memory_core::{
- AgentKind, NewObservation, NewPage, NewSession, ObservationKind, PagePath, Tier,
- };
- use ai_memory_store::Store;
- use tempfile::TempDir;
-
- /// Seed a store with two sessions in two different cwds plus some
- /// observations and a page. After reorg the sessions + observations
- /// must land in per-cwd projects and the page must be graveyarded.
- #[tokio::test]
- async fn reorg_moves_sessions_and_graveyards_pages() {
- let tmp = TempDir::new().unwrap();
- let store = Store::open(tmp.path()).unwrap();
-
- // Create the initial "scratch" project (server default).
- let ws = store
- .writer
- .get_or_create_workspace("default")
- .await
- .unwrap();
- let scratch = store
- .writer
- .get_or_create_project(ws, "scratch", None)
- .await
- .unwrap();
-
- // Session A — cwd=/home/user/project-alpha
- let sid_a = SessionId::new();
- store
- .writer
- .begin_session(NewSession {
- id: sid_a,
- workspace_id: ws,
- project_id: scratch,
- agent_kind: AgentKind::ClaudeCode,
- cwd: Some(std::path::PathBuf::from("/home/user/project-alpha")),
- })
- .await
- .unwrap();
- store
- .writer
- .insert_observation(NewObservation {
- session_id: sid_a,
- workspace_id: ws,
- project_id: scratch,
- kind: ObservationKind::UserPrompt,
- title: "hello alpha".into(),
- body: "".into(),
- importance: 5,
- })
- .await
- .unwrap();
-
- // Session B — cwd=/home/user/project-beta
- let sid_b = SessionId::new();
- store
- .writer
- .begin_session(NewSession {
- id: sid_b,
- workspace_id: ws,
- project_id: scratch,
- agent_kind: AgentKind::ClaudeCode,
- cwd: Some(std::path::PathBuf::from("/home/user/project-beta")),
- })
- .await
- .unwrap();
- store
- .writer
- .insert_observation(NewObservation {
- session_id: sid_b,
- workspace_id: ws,
- project_id: scratch,
- kind: ObservationKind::UserPrompt,
- title: "hello beta".into(),
- body: "".into(),
- importance: 5,
- })
- .await
- .unwrap();
-
- // Insert a mash-up page in the scratch project.
- store
- .writer
- .upsert_page(NewPage {
- workspace_id: ws,
- project_id: scratch,
- path: PagePath::new("sessions/mash.md").unwrap(),
- title: "Mash-up page".into(),
- body: "multi-project content".into(),
- tier: Tier::Episodic,
- frontmatter_json: serde_json::json!({}),
- pinned: false,
- })
- .await
- .unwrap();
-
- // Verify baseline: one is_latest page.
- let counts_before = store.reader.status_counts().await.unwrap();
- assert_eq!(counts_before.pages_latest, 1, "one page before reorg");
-
- // Run reorg logic (not via CLI; call helpers directly).
- let ws_id = store
- .writer
- .get_or_create_workspace("default")
- .await
- .unwrap();
-
- // Simulate what `run` does: read sessions, derive project names.
- let sessions_with_cwd: Vec<(SessionId, ProjectId, String)> = store
- .reader
- .with_conn(move |conn| {
- let mut stmt = conn.prepare(
- "SELECT id, project_id, cwd FROM sessions \
- WHERE cwd IS NOT NULL AND cwd != '' ORDER BY started_at",
- )?;
- let rows = stmt.query_map([], |row| {
- let id_bytes: Vec<u8> = row.get(0)?;
- let proj_bytes: Vec<u8> = row.get(1)?;
- let cwd: String = row.get(2)?;
- Ok((id_bytes, proj_bytes, cwd))
- })?;
- let mut out = Vec::new();
- for r in rows {
- let (id_bytes, proj_bytes, cwd) = r.unwrap();
- let sid = SessionId::from_slice(&id_bytes).unwrap();
- let pid = ProjectId::from_slice(&proj_bytes).unwrap();
- out.push((sid, pid, cwd));
- }
- Ok(out)
- })
- .await
- .unwrap();
-
- assert_eq!(sessions_with_cwd.len(), 2);
-
- let mut cwd_to_proj: std::collections::HashMap<String, ProjectId> =
- std::collections::HashMap::new();
- for (_, _, cwd) in &sessions_with_cwd {
- if cwd_to_proj.contains_key(cwd.as_str()) {
- continue;
- }
- let name = std::path::Path::new(cwd.as_str())
- .file_name()
- .unwrap()
- .to_str()
- .unwrap()
- .to_string();
- let proj = store
- .writer
- .get_or_create_project(ws_id, name, Some(cwd.clone()))
- .await
- .unwrap();
- cwd_to_proj.insert(cwd.clone(), proj);
+ if report.dry_run {
+ println!("{} session(s) would be moved:\n", report.plan.len());
+ println!("{:<38} {:<20} cwd", "session_id", "new_project");
+ println!("{}", "-".repeat(80));
+ for e in &report.plan {
+ println!("{:<38} {:<20} {}", e.session_id, e.new_project, e.cwd);
}
-
- let plan: Vec<(SessionId, ProjectId)> = sessions_with_cwd
- .iter()
- .filter_map(|(sid, old_pid, cwd)| {
- let new_pid = cwd_to_proj[cwd.as_str()];
- if new_pid != *old_pid {
- Some((*sid, new_pid))
- } else {
- None
- }
- })
- .collect();
-
- assert_eq!(plan.len(), 2, "both sessions need moving");
-
- let summary = store.writer.reorg_sessions(plan).await.unwrap();
- assert_eq!(summary.sessions_moved, 2);
- assert_eq!(summary.observations_updated, 2);
- assert_eq!(summary.pages_graveyarded, 1);
-
- // Pages should all be graveyarded (is_latest = 0).
- let counts_after = store.reader.status_counts().await.unwrap();
- assert_eq!(counts_after.pages_latest, 0, "page must be graveyarded");
-
- // Sessions should now be in distinct projects.
- let alpha_proj = cwd_to_proj["/home/user/project-alpha"];
- let beta_proj = cwd_to_proj["/home/user/project-beta"];
- assert_ne!(alpha_proj, beta_proj);
-
- // Re-read sessions from DB and verify they carry the correct project.
- // We piggy-back on the same read-sessions logic used by `run`.
- let sessions_after: Vec<(SessionId, ProjectId, String)> = store
- .reader
- .with_conn(move |conn| {
- let mut stmt = conn.prepare(
- "SELECT id, project_id, cwd FROM sessions \
- WHERE cwd IS NOT NULL AND cwd != '' ORDER BY started_at",
- )?;
- let rows = stmt.query_map([], |row| {
- let id_bytes: Vec<u8> = row.get(0)?;
- let proj_bytes: Vec<u8> = row.get(1)?;
- let cwd: String = row.get(2)?;
- Ok((id_bytes, proj_bytes, cwd))
- })?;
- let mut out = Vec::new();
- for r in rows {
- let (id_bytes, proj_bytes, cwd) = r?;
- let sid = SessionId::from_slice(&id_bytes).map_err(StoreError::Memory)?;
- let pid = ProjectId::from_slice(&proj_bytes).map_err(StoreError::Memory)?;
- out.push((sid, pid, cwd));
- }
- Ok(out)
- })
- .await
- .unwrap();
-
- for (sid, pid, cwd) in &sessions_after {
- let expected = cwd_to_proj[cwd.as_str()];
- assert_eq!(
- *pid, expected,
- "session {sid} in cwd {cwd} should have project {expected:?}"
- );
- }
- assert!(
- sessions_after.iter().any(|(sid, _, _)| *sid == sid_a),
- "session A present"
+ println!("\n(dry-run; omit --dry-run to apply)");
+ } else {
+ println!("Reorg complete:");
+ println!(" sessions moved: {}", report.summary.sessions_moved);
+ println!(
+ " observations updated: {}",
+ report.summary.observations_updated
);
- assert!(
- sessions_after.iter().any(|(sid, _, _)| *sid == sid_b),
- "session B present"
+ println!(
+ " pages graveyarded: {}",
+ report.summary.pages_graveyarded
+ );
+ println!(
+ " distinct new projects: {}",
+ report.summary.distinct_new_projects
);
}
+ Ok(())
}
diff --git a/crates/ai-memory-cli/src/commands/serve.rs b/crates/ai-memory-cli/src/commands/serve.rs
index cce69df2..831e1a2b 100644
--- a/crates/ai-memory-cli/src/commands/serve.rs
+++ b/crates/ai-memory-cli/src/commands/serve.rs
@@ -180,6 +180,8 @@ pub async fn run(config: &Config, args: ServeArgs) -> Result<()> {
reader: store.reader.clone(),
wiki: wiki.clone(),
llm: admin_llm,
+ embedder: embedder.clone(),
+ decay_params: config.decay,
data_dir: config.data_dir.clone(),
db_path: store.db_path().to_path_buf(),
bind: bind.clone(),
diff --git a/crates/ai-memory-cli/src/main.rs b/crates/ai-memory-cli/src/main.rs
index 36811f0b..1faa6a0e 100644
--- a/crates/ai-memory-cli/src/main.rs
+++ b/crates/ai-memory-cli/src/main.rs
@@ -50,7 +50,7 @@ async fn main() -> Result<()> {
Command::Restore(args) => commands::restore::run(&config, args),
Command::InstallHooks(args) => commands::install_hooks::run(&config, args),
Command::InstallMcp(args) => commands::install_mcp::run(&config, args),
- Command::Commit(args) => commands::commit::run(&config, args),
+ Command::Commit(args) => commands::commit::run(&config, args).await,
Command::LlmTest(args) => commands::llm_test::run(&config, args).await,
Command::ForgetSweep(args) => commands::forget_sweep::run(&config, args).await,
Command::Lint(args) => commands::lint::run(&config, args).await,
diff --git a/crates/ai-memory-mcp/src/admin.rs b/crates/ai-memory-mcp/src/admin.rs
index 3e6127b8..5b01a833 100644
--- a/crates/ai-memory-mcp/src/admin.rs
+++ b/crates/ai-memory-mcp/src/admin.rs
@@ -1,10 +1,15 @@
//! Admin HTTP routes — state-touching operations invoked by the CLI
//! over plain HTTP (not MCP). Currently exposes:
//!
-//! - `POST /admin/bootstrap` — ingest a pre-collected source bundle
+//! - `POST /admin/bootstrap` — ingest a pre-collected source bundle
//! into seed wiki pages via the configured LLM provider.
-//! - `GET /admin/status` — lifetime counts + server data-dir info.
-//! - `GET /admin/search?q=` — FTS5 hits against the wiki index.
+//! - `GET /admin/status` — lifetime counts + server data-dir info.
+//! - `GET /admin/search?q=` — FTS5 hits against the wiki index.
+//! - `POST /admin/reorg` — retro-fit sessions to per-cwd projects.
+//! - `POST /admin/lint` — run the M8 lint pass.
+//! - `POST /admin/forget-sweep` — run the M8 retention sweep.
+//! - `POST /admin/embed` — backfill embeddings for latest pages.
+//! - `POST /admin/commit` — stage + commit the wiki tree via git.
//!
//! The CLI is responsible for filesystem access (collecting sources from
//! the project repo, rendering output for humans); the server is
@@ -15,10 +20,12 @@ use std::sync::Arc;
use std::path::PathBuf;
use ai_memory_consolidate::{
- Bootstrap, BootstrapConfig, BootstrapOutcome, BootstrapSource, SourceCounts,
+ Bootstrap, BootstrapConfig, BootstrapOutcome, BootstrapSource, SourceCounts, run_lint,
+ run_sweep,
};
-use ai_memory_llm::LlmProvider;
-use ai_memory_store::{ReaderPool, WriterHandle};
+use ai_memory_core::{ProjectId, SessionId, WorkspaceId};
+use ai_memory_llm::{Embedder, LlmProvider};
+use ai_memory_store::{DecayParams, ReaderPool, StoreError, WriterHandle, f32_vec_to_bytes};
use ai_memory_wiki::Wiki;
use axum::Json;
use axum::Router;
@@ -27,6 +34,7 @@ use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::routing::{get, post};
use serde::{Deserialize, Serialize};
+use tracing::warn;
/// Shared state for the admin router.
#[derive(Clone)]
@@ -39,6 +47,10 @@ pub struct AdminState {
pub wiki: Wiki,
/// Optional LLM provider. When `None`, bootstrap returns 503.
pub llm: Option<Arc<dyn LlmProvider>>,
+ /// Optional embedder. When `None`, `/admin/embed` returns 503.
+ pub embedder: Option<Arc<dyn Embedder>>,
+ /// Retention-decay parameters forwarded from server config.
+ pub decay_params: DecayParams,
/// Server's resolved data directory (e.g. `/data` in the docker
/// image). Surfaced via `/admin/status` so the CLI can report
/// "where the wiki + db actually live".
@@ -77,11 +89,21 @@ fn default_max_input_tokens() -> usize {
/// - `POST /admin/bootstrap`
/// - `GET /admin/status`
/// - `GET /admin/search`
+/// - `POST /admin/reorg`
+/// - `POST /admin/lint`
+/// - `POST /admin/forget-sweep`
+/// - `POST /admin/embed`
+/// - `POST /admin/commit`
pub fn admin_router(state: AdminState) -> Router {
Router::new()
.route("/admin/bootstrap", post(handle_bootstrap))
.route("/admin/status", get(handle_status))
.route("/admin/search", get(handle_search))
+ .route("/admin/reorg", post(handle_reorg))
+ .route("/admin/lint", post(handle_lint))
+ .route("/admin/forget-sweep", post(handle_forget_sweep))
+ .route("/admin/embed", post(handle_embed))
+ .route("/admin/commit", post(handle_commit))
.with_state(Arc::new(state))
}
@@ -307,3 +329,548 @@ fn dry_run_outcome(
Json(serde_json::to_value(&outcome).unwrap_or_else(|_| serde_json::json!({}))),
)
}
+
+// ---------------------------------------------------------------------
+// reorg
+// ---------------------------------------------------------------------
+
+/// JSON request body for `POST /admin/reorg`.
+#[derive(Deserialize)]
+struct ReorgRequest {
+ /// Show what would change without writing.
+ #[serde(default)]
+ dry_run: bool,
+}
+
+/// One entry in the reorg plan (serialised in the response).
+#[derive(Debug, Serialize)]
+pub struct ReorgPlanEntry {
+ /// Session UUID string.
+ pub session_id: String,
+ /// Working directory the session was started in.
+ pub cwd: String,
+ /// Basename-derived project name the session will move to.
+ pub new_project: String,
+}
+
+/// Summary counts returned alongside the plan.
+#[derive(Debug, Serialize)]
+pub struct ReorgSummaryJson {
+ /// Sessions whose `project_id` was changed.
+ pub sessions_moved: usize,
+ /// Observations updated to match their session's new project.
+ pub observations_updated: usize,
+ /// `is_latest=1` pages marked `is_latest=0` (mash-up graveyard).
+ pub pages_graveyarded: usize,
+ /// Number of distinct per-cwd projects referenced in the plan.
+ pub distinct_new_projects: usize,
+}
+
+/// Full response for `POST /admin/reorg`.
+#[derive(Debug, Serialize)]
+pub struct ReorgReport {
+ /// `true` when `dry_run` was requested.
+ pub dry_run: bool,
+ /// All sessions that need (or needed) moving, with their target project.
+ pub plan: Vec<ReorgPlanEntry>,
+ /// Counts after execution (zeros when `dry_run=true`).
+ pub summary: ReorgSummaryJson,
+}
+
+async fn handle_reorg(
+ State(state): State<Arc<AdminState>>,
+ Json(req): Json<ReorgRequest>,
+) -> impl IntoResponse {
+ // Step 1: ensure the default workspace exists.
+ let ws = match state.writer.get_or_create_workspace("default").await {
+ Ok(id) => id,
+ Err(e) => {
+ return (
+ StatusCode::INTERNAL_SERVER_ERROR,
+ Json(serde_json::json!({ "error": format!("workspace: {e}") })),
+ );
+ }
+ };
+
+ // Step 2: read all sessions with a non-NULL, non-empty cwd.
+ let sessions_with_cwd: Vec<(SessionId, ProjectId, String)> = match state
+ .reader
+ .with_conn(move |conn| {
+ let mut stmt = conn.prepare(
+ "SELECT id, project_id, cwd \
+ FROM sessions \
+ WHERE cwd IS NOT NULL AND cwd != '' \
+ ORDER BY started_at",
+ )?;
+ let rows = stmt.query_map([], |row| {
+ let id_bytes: Vec<u8> = row.get(0)?;
+ let proj_bytes: Vec<u8> = row.get(1)?;
+ let cwd: String = row.get(2)?;
+ Ok((id_bytes, proj_bytes, cwd))
+ })?;
+ let mut out = Vec::new();
+ for r in rows {
+ let (id_bytes, proj_bytes, cwd) = r?;
+ let sid = SessionId::from_slice(&id_bytes).map_err(StoreError::Memory)?;
+ let pid = ProjectId::from_slice(&proj_bytes).map_err(StoreError::Memory)?;
+ out.push((sid, pid, cwd));
+ }
+ Ok(out)
+ })
+ .await
+ {
+ Ok(v) => v,
+ Err(e) => {
+ return (
+ StatusCode::INTERNAL_SERVER_ERROR,
+ Json(serde_json::json!({ "error": e.to_string() })),
+ );
+ }
+ };
+
+ if sessions_with_cwd.is_empty() {
+ let report = ReorgReport {
+ dry_run: req.dry_run,
+ plan: Vec::new(),
+ summary: ReorgSummaryJson {
+ sessions_moved: 0,
+ observations_updated: 0,
+ pages_graveyarded: 0,
+ distinct_new_projects: 0,
+ },
+ };
+ return (
+ StatusCode::OK,
+ Json(serde_json::to_value(&report).unwrap_or_else(|_| serde_json::json!({}))),
+ );
+ }
+
+ // Step 3: resolve target project per distinct cwd (basename-derived).
+ let mut cwd_to_proj: std::collections::HashMap<String, (WorkspaceId, ProjectId, String)> =
+ std::collections::HashMap::new();
+ for (_, _, cwd) in &sessions_with_cwd {
+ if cwd_to_proj.contains_key(cwd.as_str()) {
+ continue;
+ }
+ let project_name = std::path::Path::new(cwd.as_str())
+ .file_name()
+ .and_then(|s| s.to_str())
+ .filter(|s| !s.is_empty())
+ .map(str::to_string)
+ .unwrap_or_else(|| "unknown".to_string());
+ let proj = match state
+ .writer
+ .get_or_create_project(ws, project_name.clone(), Some(cwd.clone()))
+ .await
+ {
+ Ok(id) => id,
+ Err(e) => {
+ return (
+ StatusCode::INTERNAL_SERVER_ERROR,
+ Json(serde_json::json!({ "error": format!("project: {e}") })),
+ );
+ }
+ };
+ cwd_to_proj.insert(cwd.clone(), (ws, proj, project_name));
+ }
+
+ // Step 4: build plan — sessions whose project_id already matches are skipped.
+ let mut plan_entries: Vec<ReorgPlanEntry> = Vec::new();
+ let mut writer_plan: Vec<(SessionId, ProjectId)> = Vec::new();
+ for (session_id, old_project_id, cwd) in &sessions_with_cwd {
+ let (_, new_project_id, project_name) = &cwd_to_proj[cwd.as_str()];
+ if *new_project_id == *old_project_id {
+ continue;
+ }
+ plan_entries.push(ReorgPlanEntry {
+ session_id: session_id.to_string(),
+ cwd: cwd.clone(),
+ new_project: project_name.clone(),
+ });
+ writer_plan.push((*session_id, *new_project_id));
+ }
+
+ let distinct_new_projects: std::collections::HashSet<ProjectId> =
+ writer_plan.iter().map(|(_, pid)| *pid).collect();
+
+ // Step 5: dry-run → return the plan without writing.
+ if req.dry_run || writer_plan.is_empty() {
+ let report = ReorgReport {
+ dry_run: req.dry_run,
+ plan: plan_entries,
+ summary: ReorgSummaryJson {
+ sessions_moved: 0,
+ observations_updated: 0,
+ pages_graveyarded: 0,
+ distinct_new_projects: distinct_new_projects.len(),
+ },
+ };
+ return (
+ StatusCode::OK,
+ Json(serde_json::to_value(&report).unwrap_or_else(|_| serde_json::json!({}))),
+ );
+ }
+
+ // Step 6: execute.
+ let summary = match state.writer.reorg_sessions(writer_plan).await {
+ Ok(s) => s,
+ Err(e) => {
+ return (
+ StatusCode::INTERNAL_SERVER_ERROR,
+ Json(serde_json::json!({ "error": e.to_string() })),
+ );
+ }
+ };
+
+ let report = ReorgReport {
+ dry_run: false,
+ plan: plan_entries,
+ summary: ReorgSummaryJson {
+ sessions_moved: summary.sessions_moved,
+ observations_updated: summary.observations_updated,
+ pages_graveyarded: summary.pages_graveyarded,
+ distinct_new_projects: distinct_new_projects.len(),
+ },
+ };
+ (
+ StatusCode::OK,
+ Json(serde_json::to_value(&report).unwrap_or_else(|_| serde_json::json!({}))),
+ )
+}
+
+// ---------------------------------------------------------------------
+// lint
+// ---------------------------------------------------------------------
+
+/// JSON request body for `POST /admin/lint`.
+#[derive(Deserialize)]
+struct LintRequest {
+ /// Workspace name (auto-created if absent).
+ #[serde(default = "default_workspace")]
+ workspace: String,
+ /// Project name (auto-created if absent).
+ #[serde(default = "default_project")]
+ project: String,
+ /// Don't write the lint report page.
+ #[serde(default)]
+ dry_run: bool,
+}
+
+fn default_workspace() -> String {
+ "default".to_string()
+}
+
+fn default_project() -> String {
+ "scratch".to_string()
+}
+
+async fn handle_lint(
+ State(state): State<Arc<AdminState>>,
+ Json(req): Json<LintRequest>,
+) -> impl IntoResponse {
+ let ws = match state.writer.get_or_create_workspace(req.workspace).await {
+ Ok(id) => id,
+ Err(e) => {
+ return (
+ StatusCode::INTERNAL_SERVER_ERROR,
+ Json(serde_json::json!({ "error": format!("workspace: {e}") })),
+ );
+ }
+ };
+ let proj = match state
+ .writer
+ .get_or_create_project(ws, req.project, None)
+ .await
+ {
+ Ok(id) => id,
+ Err(e) => {
+ return (
+ StatusCode::INTERNAL_SERVER_ERROR,
+ Json(serde_json::json!({ "error": format!("project: {e}") })),
+ );
+ }
+ };
+
+ match run_lint(
+ &state.reader,
+ &state.wiki,
+ state.llm.as_ref(),
+ ws,
+ proj,
+ req.dry_run,
+ )
+ .await
+ {
+ Ok(report) => (
+ StatusCode::OK,
+ Json(serde_json::to_value(&report).unwrap_or_else(|_| serde_json::json!({}))),
+ ),
+ Err(e) => (
+ StatusCode::INTERNAL_SERVER_ERROR,
+ Json(serde_json::json!({ "error": e.to_string() })),
+ ),
+ }
+}
+
+// ---------------------------------------------------------------------
+// forget-sweep
+// ---------------------------------------------------------------------
+
+/// JSON request body for `POST /admin/forget-sweep`.
+#[derive(Deserialize)]
+struct ForgetSweepRequest {
+ /// Workspace name (auto-created if absent).
+ #[serde(default = "default_workspace")]
+ workspace: String,
+ /// Project name (auto-created if absent).
+ #[serde(default = "default_project")]
+ project: String,
+ /// Report what would be evicted without actually mutating.
+ #[serde(default)]
+ dry_run: bool,
+}
+
+async fn handle_forget_sweep(
+ State(state): State<Arc<AdminState>>,
+ Json(req): Json<ForgetSweepRequest>,
+) -> impl IntoResponse {
+ let ws = match state.writer.get_or_create_workspace(req.workspace).await {
+ Ok(id) => id,
+ Err(e) => {
+ return (
+ StatusCode::INTERNAL_SERVER_ERROR,
+ Json(serde_json::json!({ "error": format!("workspace: {e}") })),
+ );
+ }
+ };
+ let proj = match state
+ .writer
+ .get_or_create_project(ws, req.project, None)
+ .await
+ {
+ Ok(id) => id,
+ Err(e) => {
+ return (
+ StatusCode::INTERNAL_SERVER_ERROR,
+ Json(serde_json::json!({ "error": format!("project: {e}") })),
+ );
+ }
+ };
+
+ match run_sweep(
+ &state.reader,
+ &state.writer,
+ ws,
+ proj,
+ &state.decay_params,
+ req.dry_run,
+ )
+ .await
+ {
+ Ok(report) => (
+ StatusCode::OK,
+ Json(serde_json::to_value(&report).unwrap_or_else(|_| serde_json::json!({}))),
+ ),
+ Err(e) => (
+ StatusCode::INTERNAL_SERVER_ERROR,
+ Json(serde_json::json!({ "error": e.to_string() })),
+ ),
+ }
+}
+
+// ---------------------------------------------------------------------
+// embed
+// ---------------------------------------------------------------------
+
+/// JSON request body for `POST /admin/embed`.
+#[derive(Deserialize)]
+struct EmbedRequest {
+ /// Workspace name (auto-created if absent).
+ #[serde(default = "default_workspace")]
+ workspace: String,
+ /// Project name (auto-created if absent).
+ #[serde(default = "default_project")]
+ project: String,
+ /// When true, regenerates embeddings even for pages that already
+ /// have one matching the current (provider, model, dim).
+ #[serde(default)]
+ reembed: bool,
+}
+
+/// Summary response from `POST /admin/embed`.
+#[derive(Debug, Serialize)]
+pub struct EmbedReport {
+ /// Total pages that were embedded (or would be, in dry-run mode).
+ pub embedded: usize,
+ /// Pages skipped because a matching embedding already existed.
+ pub skipped: usize,
+ /// Pages that failed to embed (read error or provider error).
+ pub failed: usize,
+ /// Provider name.
+ pub provider: String,
+ /// Model identifier.
+ pub model: String,
+ /// Embedding dimensionality.
+ pub dim: u32,
+}
+
+async fn handle_embed(
+ State(state): State<Arc<AdminState>>,
+ Json(req): Json<EmbedRequest>,
+) -> impl IntoResponse {
+ let embedder = match state.embedder.clone() {
+ Some(e) => e,
+ None => {
+ return (
+ StatusCode::SERVICE_UNAVAILABLE,
+ Json(serde_json::json!({
+ "error": "embedder not configured on server"
+ })),
+ );
+ }
+ };
+
+ let provider = embedder.provider().to_string();
+ let model = embedder.model().to_string();
+ let dim = embedder.dim();
+
+ let ws = match state.writer.get_or_create_workspace(req.workspace).await {
+ Ok(id) => id,
+ Err(e) => {
+ return (
+ StatusCode::INTERNAL_SERVER_ERROR,
+ Json(serde_json::json!({ "error": format!("workspace: {e}") })),
+ );
+ }
+ };
+ let proj = match state
+ .writer
+ .get_or_create_project(ws, req.project, None)
+ .await
+ {
+ Ok(id) => id,
+ Err(e) => {
+ return (
+ StatusCode::INTERNAL_SERVER_ERROR,
+ Json(serde_json::json!({ "error": format!("project: {e}") })),
+ );
+ }
+ };
+
+ let candidates = match state.reader.decay_candidates(ws, proj).await {
+ Ok(c) => c,
+ Err(e) => {
+ return (
+ StatusCode::INTERNAL_SERVER_ERROR,
+ Json(serde_json::json!({ "error": e.to_string() })),
+ );
+ }
+ };
+
+ // Build the set of page ids that already have a matching embedding.
+ let already: std::collections::HashSet<_> = if req.reembed {
+ std::collections::HashSet::new()
+ } else {
+ match state
+ .reader
+ .load_embeddings(ws, proj, provider.clone(), model.clone(), dim)
+ .await
+ {
+ Ok(embeddings) => embeddings.into_iter().map(|s| s.id).collect(),
+ Err(e) => {
+ return (
+ StatusCode::INTERNAL_SERVER_ERROR,
+ Json(serde_json::json!({ "error": e.to_string() })),
+ );
+ }
+ }
+ };
+
+ let mut embedded = 0_usize;
+ let mut skipped = 0_usize;
+ let mut failed = 0_usize;
+
+ for cand in candidates {
+ if !req.reembed && already.contains(&cand.id) {
+ skipped += 1;
+ continue;
+ }
+ let md = match state.wiki.read_page(&cand.path) {
+ Ok(m) => m,
+ Err(e) => {
+ warn!(path = %cand.path, error = %e, "embed: skip unreadable page");
+ failed += 1;
+ continue;
+ }
+ };
+ let vec = match embedder.embed(&md.body).await {
+ Ok(v) => v,
+ Err(e) => {
+ warn!(path = %cand.path, error = %e, "embed: provider call failed");
+ failed += 1;
+ continue;
+ }
+ };
+ let bytes = f32_vec_to_bytes(&vec);
+ if let Err(e) = state
+ .writer
+ .store_embedding(cand.id, bytes, provider.clone(), model.clone(), dim)
+ .await
+ {
+ warn!(path = %cand.path, error = %e, "embed: store_embedding failed");
+ failed += 1;
+ continue;
+ }
+ embedded += 1;
+ }
+
+ let report = EmbedReport {
+ embedded,
+ skipped,
+ failed,
+ provider,
+ model,
+ dim,
+ };
+ (
+ StatusCode::OK,
+ Json(serde_json::to_value(&report).unwrap_or_else(|_| serde_json::json!({}))),
+ )
+}
+
+// ---------------------------------------------------------------------
+// commit
+// ---------------------------------------------------------------------
+
+/// JSON request body for `POST /admin/commit`.
+#[derive(Deserialize)]
+struct CommitRequest {
+ /// Commit message.
+ message: String,
+}
+
+async fn handle_commit(
+ State(state): State<Arc<AdminState>>,
+ Json(req): Json<CommitRequest>,
+) -> impl IntoResponse {
+ match state.wiki.commit_all(&req.message) {
+ Ok(Some(oid)) => (
+ StatusCode::OK,
+ Json(serde_json::json!({
+ "committed": true,
+ "oid": oid.to_string(),
+ })),
+ ),
+ Ok(None) => (
+ StatusCode::OK,
+ Json(serde_json::json!({
+ "committed": false,
+ "reason": "nothing to commit",
+ })),
+ ),
+ Err(e) => (
+ StatusCode::INTERNAL_SERVER_ERROR,
+ Json(serde_json::json!({ "error": e.to_string() })),
+ ),
+ }
+}
diff --git a/crates/ai-memory-mcp/tests/admin_bootstrap.rs b/crates/ai-memory-mcp/tests/admin_bootstrap.rs
index a7515aae..1562cf62 100644
--- a/crates/ai-memory-mcp/tests/admin_bootstrap.rs
+++ b/crates/ai-memory-mcp/tests/admin_bootstrap.rs
@@ -7,7 +7,7 @@
use ai_memory_consolidate::{BootstrapOutcome, BootstrapSource, SourceKind};
use ai_memory_mcp::{AdminState, admin_router};
-use ai_memory_store::Store;
+use ai_memory_store::{DecayParams, Store};
use ai_memory_wiki::Wiki;
use axum::body::Body;
use axum::http::{Request, StatusCode};
@@ -25,6 +25,8 @@ async fn make_admin_state(tmp: &TempDir) -> AdminState {
reader: store.reader.clone(),
wiki,
llm: None, // no LLM — dry-run path only
+ embedder: None,
+ decay_params: DecayParams::default(),
data_dir: tmp.path().to_path_buf(),
db_path,
bind: "127.0.0.1:0".to_string(),
diff --git a/crates/ai-memory-mcp/tests/admin_phase3.rs b/crates/ai-memory-mcp/tests/admin_phase3.rs
new file mode 100644
index 00000000..48b33407
--- /dev/null
+++ b/crates/ai-memory-mcp/tests/admin_phase3.rs
@@ -0,0 +1,343 @@
+//! Integration tests for Phase 3 admin routes:
+//! `POST /admin/reorg`, `POST /admin/lint`, `POST /admin/forget-sweep`,
+//! `POST /admin/embed`, `POST /admin/commit`.
+//!
+//! Follows the same pattern as `admin_bootstrap.rs` and
+//! `admin_status_search.rs`: build a real [`AdminState`] over a
+//! tmpdir-backed store + wiki, drive the router with
+//! `tower::ServiceExt::oneshot`.
+
+use ai_memory_core::{
+ AgentKind, NewObservation, NewPage, NewSession, ObservationKind, PagePath, SessionId, Tier,
+};
+use ai_memory_mcp::{AdminState, admin_router};
+use ai_memory_store::{DecayParams, Store};
+use ai_memory_wiki::Wiki;
+use axum::body::Body;
+use axum::http::{Request, StatusCode};
+use serde_json::json;
+use tempfile::TempDir;
+use tower::ServiceExt;
+
+// ---------------------------------------------------------------------------
+// Helpers
+// ---------------------------------------------------------------------------
+
+/// Build a minimal `AdminState` with no LLM and no embedder.
+async fn make_state(tmp: &TempDir) -> (AdminState, Store) {
+ let store = Store::open(tmp.path()).unwrap();
+ let wiki = Wiki::new(tmp.path(), store.writer.clone()).unwrap();
+ let db_path = store.db_path().to_path_buf();
+ let state = AdminState {
+ writer: store.writer.clone(),
+ reader: store.reader.clone(),
+ wiki,
+ llm: None,
+ embedder: None,
+ decay_params: DecayParams::default(),
+ data_dir: tmp.path().to_path_buf(),
+ db_path,
+ bind: "127.0.0.1:0".to_string(),
+ };
+ (state, store)
+}
+
+async fn body_json(resp: axum::response::Response) -> serde_json::Value {
+ let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
+ .await
+ .unwrap();
+ serde_json::from_slice(&bytes).unwrap_or(serde_json::Value::Null)
+}
+
+async fn post(state: AdminState, uri: &str, body: serde_json::Value) -> axum::response::Response {
+ let router = admin_router(state);
+ let req = Request::builder()
+ .method("POST")
+ .uri(uri)
+ .header("content-type", "application/json")
+ .body(Body::from(serde_json::to_vec(&body).unwrap()))
+ .unwrap();
+ router.oneshot(req).await.unwrap()
+}
+
+// ---------------------------------------------------------------------------
+// reorg
+// ---------------------------------------------------------------------------
+
+/// Seed two sessions in two distinct cwds inside the "scratch" project.
+async fn seed_sessions_for_reorg(store: &Store) -> (SessionId, SessionId) {
+ let ws = store
+ .writer
+ .get_or_create_workspace("default")
+ .await
+ .unwrap();
+ let scratch = store
+ .writer
+ .get_or_create_project(ws, "scratch", None)
+ .await
+ .unwrap();
+
+ let sid_a = SessionId::new();
+ store
+ .writer
+ .begin_session(NewSession {
+ id: sid_a,
+ workspace_id: ws,
+ project_id: scratch,
+ agent_kind: AgentKind::ClaudeCode,
+ cwd: Some(std::path::PathBuf::from("/home/user/alpha-repo")),
+ })
+ .await
+ .unwrap();
+ store
+ .writer
+ .insert_observation(NewObservation {
+ session_id: sid_a,
+ workspace_id: ws,
+ project_id: scratch,
+ kind: ObservationKind::UserPrompt,
+ title: "alpha prompt".into(),
+ body: "".into(),
+ importance: 5,
+ })
+ .await
+ .unwrap();
+
+ let sid_b = SessionId::new();
+ store
+ .writer
+ .begin_session(NewSession {
+ id: sid_b,
+ workspace_id: ws,
+ project_id: scratch,
+ agent_kind: AgentKind::ClaudeCode,
+ cwd: Some(std::path::PathBuf::from("/home/user/beta-repo")),
+ })
+ .await
+ .unwrap();
+ store
+ .writer
+ .insert_observation(NewObservation {
+ session_id: sid_b,
+ workspace_id: ws,
+ project_id: scratch,
+ kind: ObservationKind::UserPrompt,
+ title: "beta prompt".into(),
+ body: "".into(),
+ importance: 5,
+ })
+ .await
+ .unwrap();
+
+ (sid_a, sid_b)
+}
+
+#[tokio::test]
+async fn reorg_dry_run_returns_plan_entries() {
+ let tmp = TempDir::new().unwrap();
+ let (state, store) = make_state(&tmp).await;
+ let (sid_a, sid_b) = seed_sessions_for_reorg(&store).await;
+
+ let resp = post(state, "/admin/reorg", json!({ "dry_run": true })).await;
+ assert_eq!(resp.status(), StatusCode::OK);
+
+ let body = body_json(resp).await;
+ let plan = body["plan"].as_array().unwrap();
+ // Both sessions are in the wrong (scratch) project; both must appear.
+ assert_eq!(plan.len(), 2, "two sessions need moving: {body}");
+
+ let session_ids: Vec<&str> = plan
+ .iter()
+ .map(|e| e["session_id"].as_str().unwrap())
+ .collect();
+ assert!(
+ session_ids.contains(&sid_a.to_string().as_str())
+ || session_ids.iter().any(|s| *s == sid_a.to_string()),
+ "sid_a must be in plan"
+ );
+ assert!(
+ session_ids.iter().any(|s| *s == sid_b.to_string()),
+ "sid_b must be in plan"
+ );
+
+ // dry-run → summary counters must be zero.
+ assert_eq!(body["summary"]["sessions_moved"].as_u64().unwrap(), 0);
+ assert!(body["dry_run"].as_bool().unwrap());
+}
+
+#[tokio::test]
+async fn reorg_live_moves_sessions() {
+ let tmp = TempDir::new().unwrap();
+ let (state, store) = make_state(&tmp).await;
+ seed_sessions_for_reorg(&store).await;
+
+ let resp = post(state, "/admin/reorg", json!({ "dry_run": false })).await;
+ assert_eq!(resp.status(), StatusCode::OK);
+
+ let body = body_json(resp).await;
+ assert_eq!(body["summary"]["sessions_moved"].as_u64().unwrap(), 2);
+ assert_eq!(body["summary"]["observations_updated"].as_u64().unwrap(), 2);
+ assert!(!body["dry_run"].as_bool().unwrap());
+}
+
+#[tokio::test]
+async fn reorg_empty_store_returns_empty_plan() {
+ let tmp = TempDir::new().unwrap();
+ let (state, _store) = make_state(&tmp).await;
+
+ let resp = post(state, "/admin/reorg", json!({})).await;
+ assert_eq!(resp.status(), StatusCode::OK);
+
+ let body = body_json(resp).await;
+ assert_eq!(body["plan"].as_array().unwrap().len(), 0);
+}
+
+// ---------------------------------------------------------------------------
+// lint
+// ---------------------------------------------------------------------------
+
+#[tokio::test]
+async fn lint_dry_run_returns_lint_report_shape() {
+ let tmp = TempDir::new().unwrap();
+ let (state, store) = make_state(&tmp).await;
+
+ // Seed a page so the lint pass has something to evaluate.
+ let ws = store
+ .writer
+ .get_or_create_workspace("default")
+ .await
+ .unwrap();
+ let proj = store
+ .writer
+ .get_or_create_project(ws, "scratch", None)
+ .await
+ .unwrap();
+ store
+ .writer
+ .upsert_page(NewPage {
+ workspace_id: ws,
+ project_id: proj,
+ path: PagePath::new("notes/test.md").unwrap(),
+ title: "Test page".into(),
+ body: "Some content for lint testing.".into(),
+ tier: Tier::Semantic,
+ frontmatter_json: serde_json::json!({}),
+ pinned: false,
+ })
+ .await
+ .unwrap();
+
+ let resp = post(
+ state,
+ "/admin/lint",
+ json!({
+ "workspace": "default",
+ "project": "scratch",
+ "dry_run": true,
+ }),
+ )
+ .await;
+ assert_eq!(resp.status(), StatusCode::OK);
+
+ let body = body_json(resp).await;
+ // LintReport must have a `findings` array (possibly empty when
+ // the rule-based pass finds nothing to flag).
+ assert!(
+ body["findings"].is_array(),
+ "response must have a findings array: {body}"
+ );
+}
+
+// ---------------------------------------------------------------------------
+// forget-sweep
+// ---------------------------------------------------------------------------
+
+#[tokio::test]
+async fn forget_sweep_dry_run_returns_sweep_report_shape() {
+ let tmp = TempDir::new().unwrap();
+ let (state, _store) = make_state(&tmp).await;
+
+ let resp = post(
+ state,
+ "/admin/forget-sweep",
+ json!({
+ "workspace": "default",
+ "project": "scratch",
+ "dry_run": true,
+ }),
+ )
+ .await;
+ assert_eq!(resp.status(), StatusCode::OK);
+
+ let body = body_json(resp).await;
+ // SweepReport must have `dry_run`, `candidates_evaluated`, `evicted`,
+ // and `hard_deleted` fields.
+ assert!(
+ body["dry_run"].is_boolean(),
+ "response must have dry_run field: {body}"
+ );
+ assert!(
+ body["candidates_evaluated"].is_number(),
+ "response must have candidates_evaluated: {body}"
+ );
+ assert!(
+ body["evicted"].is_array(),
+ "response must have evicted array: {body}"
+ );
+}
+
+// ---------------------------------------------------------------------------
+// embed
+// ---------------------------------------------------------------------------
+
+#[tokio::test]
+async fn embed_without_embedder_returns_503() {
+ let tmp = TempDir::new().unwrap();
+ // AdminState is built with `embedder: None` by `make_state`.
+ let (state, _store) = make_state(&tmp).await;
+
+ let resp = post(
+ state,
+ "/admin/embed",
+ json!({
+ "workspace": "default",
+ "project": "scratch",
+ "reembed": false,
+ }),
+ )
+ .await;
+ assert_eq!(
+ resp.status(),
+ StatusCode::SERVICE_UNAVAILABLE,
+ "embed without embedder must return 503"
+ );
+
+ let body = body_json(resp).await;
+ assert!(
+ body["error"].as_str().unwrap_or("").contains("embedder"),
+ "error must mention embedder: {body}"
+ );
+}
+
+// ---------------------------------------------------------------------------
+// commit
+// ---------------------------------------------------------------------------
+
+#[tokio::test]
+async fn commit_clean_wiki_returns_not_committed() {
+ let tmp = TempDir::new().unwrap();
+ let (state, _store) = make_state(&tmp).await;
+
+ let resp = post(state, "/admin/commit", json!({ "message": "test commit" })).await;
+ // Route must not error; clean tree → committed: false.
+ assert_eq!(resp.status(), StatusCode::OK);
+
+ let body = body_json(resp).await;
+ assert!(
+ body["committed"].is_boolean(),
+ "response must have committed field: {body}"
+ );
+ // An empty wiki may return either false (nothing to stage) or true
+ // (first commit of an empty tree). Both are acceptable; we just
+ // verify the route doesn't panic.
+}
diff --git a/crates/ai-memory-mcp/tests/admin_status_search.rs b/crates/ai-memory-mcp/tests/admin_status_search.rs
index 668d0410..5405ce87 100644
--- a/crates/ai-memory-mcp/tests/admin_status_search.rs
+++ b/crates/ai-memory-mcp/tests/admin_status_search.rs
@@ -6,7 +6,7 @@
use ai_memory_core::{NewPage, PagePath, Tier};
use ai_memory_mcp::{AdminState, admin_router};
-use ai_memory_store::Store;
+use ai_memory_store::{DecayParams, Store};
use ai_memory_wiki::Wiki;
use axum::body::Body;
use axum::http::{Request, StatusCode};
@@ -22,6 +22,8 @@ async fn make_admin_state(tmp: &TempDir) -> (AdminState, Store) {
reader: store.reader.clone(),
wiki,
llm: None,
+ embedder: None,
+ decay_params: DecayParams::default(),
data_dir: tmp.path().to_path_buf(),
db_path,
bind: "127.0.0.1:49374".to_string(),