Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
refactor: code-audit remediation across the workspace
AkitaOnRailscb37a7f512c1
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 5250259b..3183aca0 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0## [Unreleased]+### Added+- `GET /admin/open-sessions` lists open (not yet ended) sessions for one+ workspace/project/agent, newest first (`all=true` returns every match).+ `ai-memory finalize-session` now uses this endpoint instead of opening+ the local SQLite index directly, so every CLI command is a thin HTTP+ client of the running server; the command now requires a reachable+ server and no longer works against an offline data directory.+## [1.18.0] - 2026-07-23### Addeddiff --git a/Cargo.lock b/Cargo.lockindex 3971dae3..b3eb9c0c 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -51,7 +51,6 @@ dependencies = ["figment","flate2","fs2",- "getrandom 0.3.4","jiff","reqwest","rmcp",@@ -59,7 +58,6 @@ dependencies = ["serde","serde_json","sha2",- "subtle","sysinfo","tar","tempfile",@@ -145,13 +143,11 @@ dependencies = ["axum","git2","jiff",- "regex","serde","serde_json","serde_urlencoded","sha2","tempfile",- "thiserror 2.0.18","tokio","tracing","uuid",@@ -193,13 +189,16 @@ dependencies = ["anyhow","async-trait","axum",+ "base64","flate2",+ "getrandom 0.3.4","http","jiff","rmcp","schemars","serde","serde_json",+ "subtle","tar","tempfile","thiserror 2.0.18",@@ -252,6 +251,7 @@ dependencies = ["thiserror 2.0.18","tokio","tower",+ "tower-http","tracing",]diff --git a/README.md b/README.mdindex 7d8275dd..583aa94c 100644--- a/README.md+++ b/README.md@@ -121,12 +121,13 @@ priors are at the [bottom](#influences-and-prior-art).- **Thin-client CLI.** `ai-memory status`, `bootstrap`, `checkpoints`,`restore-page`, `purge-project`, `rename-project`, `move-project`,`audit-contamination`, `lint`, `curator`, `auto-improve`,- `auto-improve-report`, `pending-writes`, `embed`, `forget-sweep`, `backup` are+ `auto-improve-report`, `pending-writes`, `embed`, `forget-sweep`, `backup`,+ `finalize-session` areall HTTP clients of the running server - never touch SQLite orwiki files directly. `status` also reports passive LLM/embeddingprovider health from the last real provider call. Server is the- single source of truth. `finalize-session` is the exception: it reads the- local SQLite index only to find matching open sessions, then posts synthetic+ single source of truth. `finalize-session` lists matching open+ sessions through `GET /admin/open-sessions`, then posts synthetic`session-end` hooks back to the server.- **LLM is opt-in.** Zero-LLM mode still gives you FTS5 search +rule-based summarisation. Add a provider when you want consolidateddiff --git a/crates/ai-memory-cli/Cargo.toml b/crates/ai-memory-cli/Cargo.tomlindex eff25308..41b1498e 100644--- a/crates/ai-memory-cli/Cargo.toml+++ b/crates/ai-memory-cli/Cargo.toml@@ -25,8 +25,6 @@ ai-memory-wiki.workspace = truereqwest.workspace = truesecrecy.workspace = truesha2.workspace = true-subtle.workspace = true-getrandom.workspace = truebase64.workspace = trueanyhow.workspace = trueaxum.workspace = truediff --git a/crates/ai-memory-cli/src/auth_bearer.rs b/crates/ai-memory-cli/src/auth_bearer.rsindex 7b7370f9..ef47c40b 100644--- a/crates/ai-memory-cli/src/auth_bearer.rs+++ b/crates/ai-memory-cli/src/auth_bearer.rs@@ -45,6 +45,8 @@ pub async fn resolve_oidc(client: &reqwest::Client, auth_path: &Path) -> Option<#[cfg(test)]mod tests {+ use ai_memory_llm::OidcExtras;+use super::*;use secrecy::SecretString;@@ -54,9 +56,11 @@ mod tests {access: SecretString::from(access.to_string()),refresh: SecretString::from("refresh-token".to_string()),expires_at_ms,- issuer: "https://issuer.example.com/realms/team".to_string(),- client_id: "ai-memory-cli".to_string(),- token_endpoint: endpoint.to_string(),+ extra: OidcExtras {+ issuer: "https://issuer.example.com/realms/team".to_string(),+ client_id: "ai-memory-cli".to_string(),+ token_endpoint: endpoint.to_string(),+ },}}diff --git a/crates/ai-memory-cli/src/commands/apply_shared.rs b/crates/ai-memory-cli/src/commands/apply_shared.rsindex b5bb74e0..0989957a 100644--- a/crates/ai-memory-cli/src/commands/apply_shared.rs+++ b/crates/ai-memory-cli/src/commands/apply_shared.rs@@ -10,15 +10,15 @@//! touches the disk on a redundant call.//! 4. Otherwise copies the existing file to `<path>.bak-<unix-ts>`//! so the user has a recovery path.-//! 5. Writes the new content to a sibling tempfile, fsyncs, then-//! renames over the original (POSIX atomic).+//! 5. Writes the new content via the canonical+//! [`ai_memory_wiki::write_atomic`] (sibling tempfile + fsync ++//! rename + parent-dir fsync).//!//! Every `--apply` mode (install-mcp, install-hooks, install-instructions, …)//! routes through this function. The mutator decides the format (JSON ///! TOML / markdown) and the idempotency rule; the I/O atomics live here.use std::fs;-use std::io::Write as _;use std::path::{Path, PathBuf};use anyhow::{Context, Result};@@ -103,30 +103,21 @@ fn backup_path_for(path: &Path) -> PathBuf {PathBuf::from(bak)}-/// Tempfile + rename atomic write. The tempfile MUST land in the-/// same directory as the target so `rename(2)` stays intra-filesystem-/// — otherwise we get EXDEV ("Invalid cross-device link").-///-/// This used to fall back to `tempfile()` (i.e. `$TMPDIR`, typically-/// `/tmp` on tmpfs) when the target had no parent component, but-/// that breaks any relative path like `CLAUDE.md` whose parent is-/// `""` (empty) — the project lives on a different filesystem than-/// `/tmp` in just about every realistic setup. Treat empty parent-/// as `.` (current directory) instead.+/// Atomic write via the canonical [`ai_memory_wiki::write_atomic`]+/// (tempfile + fsync + rename + parent-dir fsync). The one wrinkle kept+/// here: the tempfile MUST land in the same directory as the target so+/// `rename(2)` stays intra-filesystem — otherwise we get EXDEV+/// ("Invalid cross-device link"). A bare relative path like `CLAUDE.md`+/// has an *empty* parent, so treat it as `.` (current directory); a+/// `$TMPDIR` fallback would sit on a different filesystem than the+/// project in just about every realistic setup.fn write_atomic(path: &Path, content: &str) -> Result<()> {- let parent = match path.parent() {- Some(p) if !p.as_os_str().is_empty() => p,- _ => Path::new("."),+ let path = match path.parent() {+ Some(p) if !p.as_os_str().is_empty() => path.to_path_buf(),+ _ => Path::new(".").join(path),};- let mut tmp = tempfile::Builder::new()- .prefix(".ai-memory-apply-tmp.")- .tempfile_in(parent)- .with_context(|| format!("creating tempfile next to {}", path.display()))?;- tmp.write_all(content.as_bytes())- .context("writing tempfile content")?;- tmp.as_file().sync_data().context("fsync tempfile")?;- tmp.persist(path)- .with_context(|| format!("renaming tempfile into place at {}", path.display()))?;+ ai_memory_wiki::write_atomic(&path, content.as_bytes())+ .with_context(|| format!("writing {}", path.display()))?;Ok(())}diff --git a/crates/ai-memory-cli/src/commands/auth.rs b/crates/ai-memory-cli/src/commands/auth.rsindex 70d584ec..337ed2f4 100644--- a/crates/ai-memory-cli/src/commands/auth.rs+++ b/crates/ai-memory-cli/src/commands/auth.rs@@ -174,7 +174,7 @@ async fn login_openai_oauth(config: &Config, timeout_secs: u64) -> Result<()> {token.save(&path).map_err(anyhow::Error::from)?;println!("openai-oauth: logged in");- if let Some(account_id) = token.account_id.as_deref() {+ if let Some(account_id) = token.extra.account_id.as_deref() {println!("account: {account_id}");}println!("token file: {}", path.display());@@ -230,7 +230,7 @@ fn status(config: &Config) -> Result<()> {match OpenAiOAuthToken::load(&openai_path).map_err(anyhow::Error::from)? {Some(token) => {println!("openai-oauth: logged in");- if let Some(account_id) = token.account_id.as_deref() {+ if let Some(account_id) = token.extra.account_id.as_deref() {println!("account: {account_id}");}println!("expires in: {}", format_duration_until(token.expires_at_ms));@@ -283,7 +283,7 @@ fn status(config: &Config) -> Result<()> {match OidcToken::load(&oidc_path).map_err(anyhow::Error::from)? {Some(token) => {println!("oidc-device: logged in");- println!("issuer: {}", token.issuer);+ println!("issuer: {}", token.extra.issuer);println!("expires in: {}", format_duration_until(token.expires_at_ms));println!("token file: {}", oidc_path.display());}diff --git a/crates/ai-memory-cli/src/commands/finalize_session.rs b/crates/ai-memory-cli/src/commands/finalize_session.rsindex 8ce9f675..0e285620 100644--- a/crates/ai-memory-cli/src/commands/finalize_session.rs+++ b/crates/ai-memory-cli/src/commands/finalize_session.rs@@ -1,13 +1,12 @@//! `ai-memory finalize-session` — manually synthesize SessionEnd for Codex.use ai_memory_core::AgentKind;-use ai_memory_store::{DB_FILENAME, OpenSession, ReaderPool};use anyhow::{Context, Result, bail};use serde::{Deserialize, Serialize};use crate::cli::FinalizeSessionArgs;use crate::config::Config;-use crate::http_client::ServerEndpoint;+use crate::http_client::{ServerEndpoint, ServerResponseError, get_json};#[derive(Debug, Serialize)]struct SessionEndPayload<'a> {@@ -26,6 +25,19 @@ struct HookBatchAck {accepted: usize,}+/// Response shape of `GET /admin/open-sessions` on the server.+#[derive(Debug, Deserialize)]+struct OpenSessionsResponse {+ sessions: Vec<OpenSessionEntry>,+}++/// One open session as reported by `GET /admin/open-sessions`.+#[derive(Debug, Deserialize)]+struct OpenSessionEntry {+ session_id: String,+ cwd: Option<String>,+}+#[derive(Debug, Serialize, Deserialize)]struct FinalizeSessionReport {workspace: String,@@ -37,34 +49,18 @@ struct FinalizeSessionReport {/// Run the `finalize-session` subcommand.////// # Errors-/// Returns an error if the local store cannot be read or the configured server-/// rejects a synthetic `session-end` hook.+/// Returns an error if the configured server cannot list the scope's open+/// sessions or rejects a synthetic `session-end` hook.pub async fn run(config: &Config, args: FinalizeSessionArgs) -> Result<()> {let agent = args.agent.kind();let project = super::resolve_project_name(config, args.project.as_deref())?;- let db_path = config.data_dir.join("db").join(DB_FILENAME);- if !db_path.exists() {- return print_report(args, project, agent, Vec::new());- }- let reader = ReaderPool::new(&db_path, 1).context("opening local store reader")?;- let workspace_id = match reader.find_workspace(args.workspace.clone()).await? {- Some(id) => id,- None => return print_report(args, project, agent, Vec::new()),- };- let project_id = match reader.find_project(workspace_id, project.clone()).await? {- Some(id) => id,- None => return print_report(args, project, agent, Vec::new()),- };-- let limit = if args.all { None } else { Some(1) };- let sessions = reader- .open_sessions_for_scope_agent(workspace_id, project_id, agent, limit)- .await?;+ let endpoint = ServerEndpoint::from_config_resolving_auth(config).await;+ let sessions =+ fetch_open_sessions(&endpoint, &args.workspace, &project, agent, args.all).await?;if sessions.is_empty() {return print_report(args, project, agent, Vec::new());}- let endpoint = ServerEndpoint::from_config_resolving_auth(config).await;let client = reqwest::Client::new();let fallback_cwd = effective_cwd(config)?;let mut finalized = Vec::with_capacity(sessions.len());@@ -79,12 +75,48 @@ pub async fn run(config: &Config, args: FinalizeSessionArgs) -> Result<()> {agent,).await?;- finalized.push(session.session_id.to_string());+ finalized.push(session.session_id.clone());}print_report(args, project, agent, finalized)}+/// List open sessions for the scope + agent via the server. An unknown+/// workspace/project fails closed server-side with a 404; that maps to+/// "nothing to finalize" here, matching the previous direct-DB behavior+/// for a missing scope.+async fn fetch_open_sessions(+ endpoint: &ServerEndpoint,+ workspace: &str,+ project: &str,+ agent: AgentKind,+ all: bool,+) -> Result<Vec<OpenSessionEntry>> {+ let all = if all { "true" } else { "false" };+ let result = get_json::<OpenSessionsResponse>(+ endpoint,+ "/admin/open-sessions",+ &[+ ("workspace", workspace),+ ("project", project),+ ("agent", agent.as_str()),+ ("all", all),+ ],+ )+ .await;+ match result {+ Ok(response) => Ok(response.sessions),+ Err(e) => {+ if let Some(server_err) = e.downcast_ref::<ServerResponseError>()+ && server_err.status() == reqwest::StatusCode::NOT_FOUND+ {+ return Ok(Vec::new());+ }+ Err(e)+ }+ }+}+fn print_report(args: FinalizeSessionArgs,project: String,@@ -122,7 +154,7 @@ fn print_report(async fn post_session_end_batch(client: &reqwest::Client,endpoint: &ServerEndpoint,- session: &OpenSession,+ session: &OpenSessionEntry,fallback_cwd: &str,workspace: &str,project: &str,@@ -134,7 +166,7 @@ async fn post_session_end_batch(let items = [HookBatchItem {url: hook_url,body: SessionEndPayload {- session_id: session.session_id.to_string(),+ session_id: session.session_id.clone(),cwd,},}];diff --git a/crates/ai-memory-cli/src/commands/generate_auth_token.rs b/crates/ai-memory-cli/src/commands/generate_auth_token.rsindex 373b9d88..3c8f6b43 100644--- a/crates/ai-memory-cli/src/commands/generate_auth_token.rs+++ b/crates/ai-memory-cli/src/commands/generate_auth_token.rs@@ -10,9 +10,9 @@use anyhow::Result;-use crate::auth::generate_token_hex;use crate::cli::GenerateAuthTokenArgs;use crate::config::Config;+use ai_memory_mcp::auth::generate_token_hex;/// Run the `generate-auth-token` subcommand.///diff --git a/crates/ai-memory-cli/src/commands/init.rs b/crates/ai-memory-cli/src/commands/init.rsindex 92debbea..6c7d926c 100644--- a/crates/ai-memory-cli/src/commands/init.rs+++ b/crates/ai-memory-cli/src/commands/init.rs@@ -6,9 +6,9 @@ use std::path::{Path, PathBuf};use anyhow::{Context, Result};-use crate::auth::generate_token_hex;use crate::cli::InitArgs;use crate::config::Config;+use ai_memory_mcp::auth::generate_token_hex;const DEFAULT_CONFIG_TOML: &str = include_str!("../../templates/config.default.toml");diff --git a/crates/ai-memory-cli/src/commands/install_hooks.rs b/crates/ai-memory-cli/src/commands/install_hooks.rsindex 350da1b1..ea01e4eb 100644--- a/crates/ai-memory-cli/src/commands/install_hooks.rs+++ b/crates/ai-memory-cli/src/commands/install_hooks.rs@@ -433,7 +433,7 @@ fn apply_base_path_to_hook_url(url: &str, base_path: &str) -> String {if !existing_path.is_empty() {return url.to_string();}- let prefix = crate::commands::serve::normalize_prefix(base_path);+ let prefix = ai_memory_web::normalize_prefix(base_path);if prefix.is_empty() {origin} else {diff --git a/crates/ai-memory-cli/src/commands/serve.rs b/crates/ai-memory-cli/src/commands/serve.rsindex 41608c48..b8ff0c52 100644--- a/crates/ai-memory-cli/src/commands/serve.rs+++ b/crates/ai-memory-cli/src/commands/serve.rs@@ -1,33 +1,28 @@//! `ai-memory serve` — MCP server with optional filesystem watcher.-use std::convert::Infallible;use std::future::Future;use std::path::Path;use std::sync::Arc;use std::time::Duration;use ai_memory_consolidate::{- AutoImproveReviewConfig, Consolidator, run_auto_improve_review, run_lint, run_sweep,-};-use ai_memory_core::{- ActiveProject, ActorContext, PagePath, ProjectId, Sanitizer, SessionId, WorkspaceId,+ AutoImproveReviewConfig, Consolidator, EmbedBackfillOptions, ScheduledAutoImproveSettings,+ run_auto_improve_scheduler_tick, run_embedding_backfill, run_lint, run_sweep,};+use ai_memory_core::{ActiveProject, ProjectId, Sanitizer, WorkspaceId};use ai_memory_hooks::{DEFAULT_HOOK_INGEST_MAX_IN_FLIGHT, HookState, ProjectCacheStore, WorkstreamState, hook_router,workstream_router,};use ai_memory_llm::{Embedder, LlmProvider, ProviderHealth, build_embedder, build_provider};use ai_memory_mcp::{AdminState, AiMemoryServer, ScopeInvalidation, admin_router};-use ai_memory_store::{- ApproveAutoImproveProposalResult, AutoImproveProposalOperation, EmbeddingWrite,- NewAutoImproveProposal, ReaderPool, StageAutoImproveRun, Store, WriterHandle, f32_vec_to_bytes,-};-use ai_memory_web;+use ai_memory_store::{ReaderPool, Store, WriterHandle};+use ai_memory_web::{WebMountSpec, mount_web_router, normalize_prefix, web_base_href};use ai_memory_wiki::{WatcherHandle, Wiki, migrations, run_wiki_migrations};use anyhow::{Context, Result};use axum::body::Body;use axum::extract::{DefaultBodyLimit, State};-use axum::http::{Method, Request, StatusCode, header};+use axum::http::{Request, StatusCode, header};use axum::middleware::Next;use axum::response::{IntoResponse, Response};use rmcp::ServiceExt;@@ -36,14 +31,11 @@ use rmcp::transport::streamable_http_server::{StreamableHttpServerConfig, StreamableHttpService, session::local::LocalSessionManager,};use tokio_util::sync::CancellationToken;-use tower::service_fn;-use tower_http::cors::CorsLayer;-use tower_http::services::ServeDir;use tracing::info;-use crate::auth::{AuthState, require_bearer};use crate::cli::{ServeArgs, TransportKind};use crate::config::{AuthSettings, AutoImproveSettings, Config, MaintenanceSettings};+use ai_memory_mcp::auth::{AuthState, require_bearer};/// 10 MB cap on inbound HTTP bodies. The /hook ingress accepts the/// agent's raw payload which can include a tool output excerpt@@ -52,7 +44,6 @@ use crate::config::{AuthSettings, AutoImproveSettings, Config, MaintenanceSettin/// 10 MB is generous headroom; without a cap, axum streams unbounded/// bodies into memory (audit critical #2).const MAX_BODY_BYTES: usize = 10 * 1024 * 1024;-const EMBEDDING_WRITE_BATCH: usize = 100;/// `POST /admin/bootstrap` may carry a large JSON array of sources even/// after client-side prune; keep hooks/MCP at [`MAX_BODY_BYTES`].@@ -790,7 +781,15 @@ async fn start_maintenance_scheduler(let reader = reader.clone();let writer = writer.clone();let wiki = wiki.clone();- match initialize_auto_improve_scheduler_scopes(&reader, &writer).await {+ let scheduler_settings = ScheduledAutoImproveSettings {+ review: auto_improve_review_config_from_settings(&auto_improve),+ require_approval: auto_improve.require_approval,+ min_session_age_secs: scheduler.min_session_age_secs,+ max_sessions_per_tick: scheduler.max_sessions_per_tick,+ };+ match ai_memory_consolidate::initialize_auto_improve_scheduler_scopes(&reader, &writer)+ .await+ {Ok((scopes, errors)) => info!(scopes,errors, "auto-improve scheduler startup scope initialization completed"@@ -809,8 +808,14 @@ async fn start_maintenance_scheduler(loop {tokio::time::sleep(interval).await;let started = std::time::Instant::now();- match run_auto_improve_scheduler_tick(&reader, &writer, &wiki, &llm, &auto_improve)- .await+ match run_auto_improve_scheduler_tick(+ &reader,+ &writer,+ &wiki,+ &llm,+ &scheduler_settings,+ )+ .await{Ok(outcome) => info!(scopes = outcome.scopes,@@ -838,30 +843,6 @@ async fn start_maintenance_scheduler(tasks}-async fn initialize_auto_improve_scheduler_scopes(- reader: &ReaderPool,- writer: &WriterHandle,-) -> Result<(usize, usize)> {- let scopes = reader.list_all_scopes().await?;- let total = scopes.len();- let mut errors = 0usize;- for scope in scopes {- if let Err(e) = writer- .ensure_auto_improve_scheduler_state(scope.workspace_id, scope.project_id)- .await- {- errors += 1;- tracing::warn!(- workspace = %scope.workspace_name,- project = %scope.project_name,- error = %e,- "auto-improve scheduler startup state init failed"- );- }- }- Ok((total, errors))-}-#[derive(Debug, Default)]struct ScheduledSweepTickOutcome {scopes: usize,@@ -987,12 +968,13 @@ async fn run_scheduled_embedding_backfill_tick(embedder,scope.workspace_id,scope.project_id,+ EmbedBackfillOptions::default(),).await{- Ok((embedded, failed)) => {- outcome.embedded += embedded;- outcome.failed += failed;+ Ok(counts) => {+ outcome.embedded += counts.embedded;+ outcome.failed += counts.failed;}Err(e) => {outcome.errors += 1;@@ -1009,347 +991,6 @@ async fn run_scheduled_embedding_backfill_tick(Ok(outcome)}-async fn run_embedding_backfill(- reader: &ReaderPool,- writer: &WriterHandle,- wiki: &Wiki,- embedder: &Arc<dyn Embedder>,- workspace_id: WorkspaceId,- project_id: ProjectId,-) -> Result<(usize, usize)> {- let provider = embedder.provider().to_string();- let model = embedder.model().to_string();- let dim = embedder.dim();- let candidates = reader.decay_candidates(workspace_id, project_id).await?;- let already: std::collections::HashSet<_> = reader- .embedded_page_ids(- workspace_id,- project_id,- provider.clone(),- model.clone(),- dim,- )- .await?- .into_iter()- .collect();-- let mut embedded = 0usize;- let mut failed = 0usize;- let mut pending = Vec::with_capacity(EMBEDDING_WRITE_BATCH);- for cand in candidates {- if already.contains(&cand.id) {- continue;- }- let md = match wiki.read_page(workspace_id, project_id, &cand.path) {- Ok(md) => md,- Err(e) => {- failed += 1;- tracing::warn!(path = %cand.path, error = %e, "scheduled embed: unreadable page");- continue;- }- };- let vec = match embedder.embed_document(&md.body).await {- Ok(vec) => vec,- Err(e) => {- failed += 1;- tracing::warn!(path = %cand.path, error = %e, "scheduled embed: provider failed");- continue;- }- };- pending.push(EmbeddingWrite {- page_id: cand.id,- vector_bytes: f32_vec_to_bytes(&vec),- provider: provider.clone(),- model: model.clone(),- dim,- });- if pending.len() >= EMBEDDING_WRITE_BATCH {- flush_embedding_batch(writer, &mut pending, &mut embedded, &mut failed).await;- }- }- flush_embedding_batch(writer, &mut pending, &mut embedded, &mut failed).await;- Ok((embedded, failed))-}--async fn flush_embedding_batch(- writer: &WriterHandle,- pending: &mut Vec<EmbeddingWrite>,- embedded: &mut usize,- failed: &mut usize,-) {- if pending.is_empty() {- return;- }- let batch = std::mem::replace(pending, Vec::with_capacity(EMBEDDING_WRITE_BATCH));- let count = batch.len();- if let Err(e) = writer.store_embeddings(batch).await {- *failed += count;- tracing::warn!(count, error = %e, "scheduled embed: batch store failed");- } else {- *embedded += count;- }-}--struct ScheduledAutoImproveOutcome {- run_id: ai_memory_core::AutoImproveRunId,- proposals: usize,- approved: usize,- pending: usize,- conflicts: usize,-}--#[derive(Debug, Default)]-struct ScheduledAutoImproveTickOutcome {- scopes: usize,- scopes_with_candidates: usize,- reviewed: usize,- errors: usize,-}--struct ScheduledAutoImproveContext<'a> {- reader: &'a ReaderPool,- writer: &'a WriterHandle,- wiki: &'a Wiki,- llm: &'a Arc<dyn LlmProvider>,- workspace_id: WorkspaceId,- project_id: ProjectId,- settings: &'a AutoImproveSettings,-}--async fn run_auto_improve_scheduler_tick(- reader: &ReaderPool,- writer: &WriterHandle,- wiki: &Wiki,- llm: &Arc<dyn LlmProvider>,- settings: &AutoImproveSettings,-) -> Result<ScheduledAutoImproveTickOutcome> {- let scopes = reader.list_all_scopes().await?;- let mut outcome = ScheduledAutoImproveTickOutcome {- scopes: scopes.len(),- ..ScheduledAutoImproveTickOutcome::default()- };-- for scope in scopes {- if let Err(e) = writer- .ensure_auto_improve_scheduler_state(scope.workspace_id, scope.project_id)- .await- {- outcome.errors += 1;- tracing::warn!(- workspace = %scope.workspace_name,- project = %scope.project_name,- error = %e,- "scheduled auto-improve state init failed"- );- continue;- }-- let candidates = match reader- .auto_improve_candidate_sessions(- scope.workspace_id,- scope.project_id,- settings.scheduler.min_session_age_secs,- settings.scheduler.max_sessions_per_tick,- )- .await- {- Ok(candidates) => candidates,- Err(e) => {- outcome.errors += 1;- tracing::warn!(- workspace = %scope.workspace_name,- project = %scope.project_name,- error = %e,- "scheduled auto-improve candidate query failed"- );- continue;- }- };- if candidates.is_empty() {- continue;- }-- outcome.scopes_with_candidates += 1;- let ctx = ScheduledAutoImproveContext {- reader,- writer,- wiki,- llm,- workspace_id: scope.workspace_id,- project_id: scope.project_id,- settings,- };- for candidate in candidates {- let claimed = match ctx- .writer- .claim_auto_improve_scheduler_session(- ctx.workspace_id,- ctx.project_id,- candidate.session_id,- candidate.ended_at,- )- .await- {- Ok(claimed) => claimed,- Err(e) => {- outcome.errors += 1;- tracing::warn!(- workspace = %scope.workspace_name,- project = %scope.project_name,- session_id = %candidate.session_id,- error = %e,- "scheduled auto-improve claim failed"- );- continue;- }- };- if !claimed {- tracing::debug!(- workspace = %scope.workspace_name,- project = %scope.project_name,- session_id = %candidate.session_id,- "scheduled auto-improve candidate already claimed or reviewed"- );- continue;- }- match run_scheduled_auto_improve(&ctx, candidate.session_id).await {- Ok(run) => {- outcome.reviewed += 1;- info!(- workspace = %scope.workspace_name,- project = %scope.project_name,- session_id = %candidate.session_id,- run_id = %run.run_id,- proposals = run.proposals,- approved = run.approved,- pending = run.pending,- conflicts = run.conflicts,- "scheduled auto-improve completed"- );- }- Err(e) => {- outcome.errors += 1;- tracing::warn!(- workspace = %scope.workspace_name,- project = %scope.project_name,- session_id = %candidate.session_id,- error = %e,- "scheduled auto-improve failed"- );- }- }- }- }-- Ok(outcome)-}--async fn run_scheduled_auto_improve(- ctx: &ScheduledAutoImproveContext<'_>,- session_id: SessionId,-) -> Result<ScheduledAutoImproveOutcome> {- let cfg = auto_improve_review_config_from_settings(ctx.settings);- let report = run_auto_improve_review(- ctx.reader,- &**ctx.llm,- ctx.workspace_id,- ctx.project_id,- session_id,- cfg.clone(),- )- .await?;- let proposals =- scheduled_auto_improve_new_proposals(ctx.reader, ctx.workspace_id, ctx.project_id, &report)- .await?;- let staged = ctx- .writer- .stage_auto_improve_run(StageAutoImproveRun {- workspace_id: ctx.workspace_id,- project_id: ctx.project_id,- session_id: Some(session_id),- provider: Some(report.provider.clone()),- model: Some(report.model.clone()),- summary: Some(report.summary.clone()),- warnings_json: serde_json::to_value(&report.warnings)- .unwrap_or_else(|_| serde_json::json!([])),- rejected_candidates_json: serde_json::to_value(&report.rejected_candidates)- .unwrap_or_else(|_| serde_json::json!([])),- config_json: serde_json::json!({- "trigger": "scheduler",- "min_observations": cfg.min_observations,- "min_session_duration_secs": cfg.min_session_duration_secs,- "min_confidence": cfg.min_confidence,- "max_input_tokens": cfg.max_input_tokens,- "max_proposals_per_run": cfg.max_proposals_per_run,- "include_raw_fallback": cfg.include_raw_fallback,- "max_patchable_pages": cfg.max_patchable_pages,- "max_patchable_body_chars": cfg.max_patchable_body_chars,- "max_edits_per_proposal": cfg.max_edits_per_proposal,- "max_edit_content_chars": cfg.max_edit_content_chars,- "max_changed_chars_per_proposal": cfg.max_changed_chars_per_proposal,- "max_patch_edits_per_run": cfg.max_patch_edits_per_run,- "max_rejection_context": cfg.max_rejection_context,- "rejection_context_days": cfg.rejection_context_days,- "max_final_body_chars": cfg.max_final_body_chars,- "max_rule_page_tokens": cfg.max_rule_page_tokens,- "max_procedure_page_tokens": cfg.max_procedure_page_tokens,- "eval": cfg.eval,- "require_approval": ctx.settings.require_approval,- }),- proposal_actor: ActorContext {- agent: Some(cfg.proposal_actor.clone()),- ..ActorContext::default()- },- proposals,- })- .await?;-- for id in &staged.proposal_ids {- ctx.wiki- .write_auto_improve_sidecar(ctx.workspace_id, ctx.project_id, *id)- .await?;- }-- let mut approved = 0usize;- let mut pending = 0usize;- let mut conflicts = 0usize;- for proposal_id in &staged.proposal_ids {- if ctx.settings.require_approval {- pending += 1;- continue;- }- match ctx- .wiki- .approve_auto_improve_proposal(- ctx.workspace_id,- ctx.project_id,- *proposal_id,- ActorContext {- agent: Some("auto_improve_scheduler_auto_approve".into()),- ..ActorContext::default()- },- None,- Some(ai_memory_wiki::AdmissionContext {- op: ai_memory_wiki::AdmissionOp::WritePage,- ..ai_memory_wiki::AdmissionContext::default()- }),- )- .await?- {- ApproveAutoImproveProposalResult::Approved { .. } => approved += 1,- ApproveAutoImproveProposalResult::Conflict => conflicts += 1,- }- }-- Ok(ScheduledAutoImproveOutcome {- run_id: staged.run_id,- proposals: staged.proposal_ids.len(),- approved,- pending,- conflicts,- })-}-fn auto_improve_review_config_from_settings(settings: &AutoImproveSettings,) -> AutoImproveReviewConfig {@@ -1383,63 +1024,6 @@ fn auto_improve_review_config_from_settings(}}-async fn scheduled_auto_improve_new_proposals(- reader: &ReaderPool,- workspace_id: WorkspaceId,- project_id: ProjectId,- report: &ai_memory_consolidate::AutoImproveReport,-) -> Result<Vec<NewAutoImproveProposal>> {- let mut proposals = Vec::with_capacity(report.proposals.len());- for p in &report.proposals {- let path = PagePath::new(p.path.clone())?;- let target_exists = reader- .page_body_by_ids(workspace_id, project_id, path.as_str())- .await?- .is_some();- let operation = if p.edit_mode == "patch"- || (target_exists && path.as_str() == "_slots/current-focus.md")- {- AutoImproveProposalOperation::Update- } else {- AutoImproveProposalOperation::Create- };- let expected_base_body_sha256 = p- .expected_base_body_sha256- .as_deref()- .map(hex_to_sha256)- .transpose()- .map_err(|e| anyhow::anyhow!("invalid expected_base_body_sha256: {e}"))?;- proposals.push(NewAutoImproveProposal {- operation,- target_path: path,- kind: p.kind.clone(),- title: p.title.clone(),- confidence: f64::from(p.confidence),- rationale: p.rationale.clone(),- evidence_json: serde_json::to_value(&p.evidence)- .unwrap_or_else(|_| serde_json::json!([])),- body_markdown: p.body_markdown.clone(),- artifact_sha256: None,- edit_mode: Some(p.edit_mode.clone()),- patch_json: serde_json::to_value(&p.edits).ok(),- expected_base_body_sha256,- });- }- Ok(proposals)-}--fn hex_to_sha256(hex: &str) -> Result<[u8; 32], String> {- if hex.len() != 64 {- return Err("expected 64 hex chars".into());- }- let mut out = [0_u8; 32];- for (idx, chunk) in hex.as_bytes().chunks_exact(2).enumerate() {- let s = std::str::from_utf8(chunk).map_err(|e| e.to_string())?;- out[idx] = u8::from_str_radix(s, 16).map_err(|e| e.to_string())?;- }- Ok(out)-}-async fn configure_embedder(config: &Config,store: &Store,@@ -1623,591 +1207,93 @@ fn llm_retry_hint(provider: &str, model: &str, base_url: Option<&str>) -> Stringcommand}-/// Normalise an operator-supplied path prefix into either `""` (root) or-/// `/<core>` — exactly one leading slash, no trailing slash, internal-/// empty/`//` segments collapsed.-///-/// Each segment must be a non-trivial member of the RFC 3986 *unreserved*-/// set (`ALPHA / DIGIT / "-" / "." / "_" / "~"`). Dot-segments `.` and-/// `..` are rejected outright even though their characters are unreserved-/// — at the segment level they re-encode "current directory" and "parent-/// directory" and would let a malformed env var hand the operator a-/// traversal vector through every URL the server emits.-///-/// Anything that falls outside the rule collapses to `""` (root) so a-/// bad env var can never inject markup or a protocol-relative `//` into-/// served HTML.-pub(crate) fn normalize_prefix(raw: &str) -> String {- let segs: Vec<&str> = raw.trim().split('/').filter(|s| !s.is_empty()).collect();- if segs.is_empty() {- return String::new();- }- let safe = segs.iter().all(|s| {- // Reject dot-segments (`.` / `..`) — they pass the per-char- // unreserved test but mean "current" / "parent" at the segment- // boundary and turn `/<base>` into `/<base>/..` traversal.- *s != "." && *s != ".." && s.chars().all(is_unreserved_url_char)- });- if !safe {- return String::new();- }- format!("/{}", segs.join("/"))-}--/// RFC 3986 `unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"`. Kept-/// here as a single source of truth for both the path-prefix charset-/// check and any future per-segment validation.-fn is_unreserved_url_char(c: char) -> bool {- c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.' | '~')+fn apply_http_layers(+ router: axum::Router,+ auth_state: Arc<AuthState>,+ allowed_hosts: Vec<String>,+) -> axum::Router {+ router+ .layer(axum::middleware::from_fn_with_state(+ auth_state,+ require_bearer,+ ))+ .layer(axum::middleware::from_fn_with_state(+ Arc::new(allowed_hosts),+ require_allowed_host,+ ))}-/// Build the `<base href>` value (always trailing-slash-terminated, never-/// the protocol-relative `//`) for the web UI mounted at `base_path` +-/// `web_slug`.-pub(crate) fn web_base_href(base_path: &str, web_slug: &str) -> String {- let combined = format!(- "{}{}",- normalize_prefix(base_path),- normalize_prefix(web_slug)- );- if combined.is_empty() {- "/".to_string()- } else {- format!("{combined}/")+async fn require_allowed_host(+ State(allowed_hosts): State<Arc<Vec<String>>>,+ req: Request<Body>,+ next: Next,+) -> Response {+ let Some(host) = req+ .headers()+ .get(header::HOST)+ .and_then(|h| h.to_str().ok())+ else {+ return (StatusCode::BAD_REQUEST, "missing Host header\n").into_response();+ };+ if host_allowed(host, &allowed_hosts) {+ return next.run(req).await;}+ tracing::warn!(host, allowed = ?allowed_hosts, "rejected request with disallowed Host header");+ (StatusCode::FORBIDDEN, "forbidden host\n").into_response()}-/// Escape a string for safe inclusion inside a double-quoted HTML attribute.-fn escape_attr(s: &str) -> String {- s.replace('&', "&")- .replace('"', """)- .replace('<', "<")- .replace('>', ">")+fn host_allowed(host: &str, allowed_hosts: &[String]) -> bool {+ allowed_hosts.iter().any(|allowed| {+ host.eq_ignore_ascii_case(allowed) || host_without_port(host).eq_ignore_ascii_case(allowed)+ })}-/// Insert `snippet` immediately after the first `<head…>` tag (or prepend-/// it when there is no head).-///-/// Matches outside HTML comments only: `<!-- … <head … --> <head>` skips-/// the comment-internal occurrence and injects after the real `<head>`.-/// Anything inside `<textarea>`, `<script>`, or other raw-text elements-/// is NOT specially handled — built-in askama templates never put-/// `<head` in those, and a custom `--web-ui-dir` SPA that does is a-/// misconfiguration the operator can fix at the source. Avoiding a-/// full HTML parser here keeps injection a single pass + alloc.-fn inject_into_head(html: &str, snippet: &str) -> String {- let bytes = html.as_bytes();- let mut cursor = 0usize;- while cursor < bytes.len() {- // Skip past any HTML comment opening here so a `<head` literal- // sitting inside it cannot win the search.- if html[cursor..].starts_with("<!--") {- match html[cursor..].find("-->") {- Some(end) => cursor += end + 3,- None => break, // Unterminated comment — bail out of injection.- }- continue;- }- if html[cursor..].starts_with("<head")- && let Some(gt) = html[cursor..].find('>')- {- let pos = cursor + gt + 1;- let mut out = String::with_capacity(html.len() + snippet.len());- out.push_str(&html[..pos]);- out.push_str(snippet);- out.push_str(&html[pos..]);- return out;+fn host_without_port(host: &str) -> &str {+ if let Some(rest) = host.strip_prefix('[')+ && let Some((inside, _)) = rest.split_once(']')+ {+ return inside;+ }+ match host.rsplit_once(':') {+ Some((name, port)) if !name.contains(':') && port.chars().all(|c| c.is_ascii_digit()) => {+ name}- cursor += 1;+ _ => host,}- format!("{snippet}{html}")-}--/// Inject `<base href="{href}">` so the served SPA's relative asset/router-/// URLs resolve under the configured prefix.-pub(crate) fn inject_base_href(html: &str, href: &str) -> String {- inject_into_head(html, &format!("<base href=\"{}\">", escape_attr(href)))-}--/// Inject `<meta name="ai-memory-base-path" content="{base_path}">` so the-/// SPA can build API URLs as `{base_path}/api/v1`. The `<base href>` alone is-/// ambiguous for this because it also folds in the web slug (e.g. `/web`),-/// whereas `/api/v1` hangs off the base path, not the web mount.-pub(crate) fn inject_base_path_meta(html: &str, base_path: &str) -> String {- inject_into_head(- html,- &format!(- "<meta name=\"ai-memory-base-path\" content=\"{}\">",- escape_attr(base_path)- ),- )}#[cfg(test)]-mod web_base_tests {- use super::{inject_base_href, inject_base_path_meta, normalize_prefix, web_base_href};-- #[test]- fn normalize_prefix_edge_cases() {- assert_eq!(normalize_prefix(""), "");- assert_eq!(normalize_prefix("/"), "");- assert_eq!(normalize_prefix("//"), "");- assert_eq!(normalize_prefix(" / "), "");- assert_eq!(normalize_prefix("wiki"), "/wiki");- assert_eq!(normalize_prefix("/wiki"), "/wiki");- assert_eq!(normalize_prefix("/wiki/"), "/wiki");- assert_eq!(normalize_prefix("//wiki//"), "/wiki");- assert_eq!(normalize_prefix("/wiki/sub"), "/wiki/sub");- // Unsafe chars fall back to root — never inject markup or `//`.- assert_eq!(normalize_prefix("/wi\"ki"), "");- assert_eq!(normalize_prefix("/wiki space"), "");- assert_eq!(normalize_prefix("/<script>"), "");- }-- /// Dot-segments must NOT survive normalisation. Their characters- /// pass the unreserved per-char allowlist (`.` is unreserved), so- /// the segment-level rejection is what stops `/..` and `/.` from- /// turning the base prefix into a traversal vector. Regression- /// guard — without this, `AI_MEMORY_BASE_PATH=/..` would serve- /// `/..` and let an upstream redirect normalise it to `/`.- #[test]- fn normalize_prefix_rejects_dot_segments() {- assert_eq!(normalize_prefix("/.."), "", "/.. must collapse to root");- assert_eq!(normalize_prefix("/."), "", "/. must collapse to root");- assert_eq!(- normalize_prefix("/wiki/.."),- "",- "any embedded /.. fails the whole prefix"- );- assert_eq!(- normalize_prefix("/wiki/./sub"),- "",- "any embedded /. fails the whole prefix"- );- // Segments that merely START with a dot but aren't pure- // dot-segments are still valid (RFC 3986 unreserved chars).- assert_eq!(normalize_prefix("/.wellknown"), "/.wellknown");- }-- /// Nested base paths (`/a/b/c`) are valid; the normaliser keeps the- /// hierarchy intact instead of collapsing to one level.- #[test]- fn normalize_prefix_keeps_nested_paths() {- assert_eq!(normalize_prefix("/a/b/c"), "/a/b/c");- assert_eq!(normalize_prefix("a/b/c"), "/a/b/c");- assert_eq!(normalize_prefix("//a//b//c//"), "/a/b/c");- assert_eq!(normalize_prefix("/a/b/c/d/e"), "/a/b/c/d/e");- }+mod tests {+ use super::*;+ use ai_memory_core::{PagePath, Tier};+ use ai_memory_llm::{ChatRequest, ChatResponse, LlmResult, SyntheticEmbedder};+ use ai_memory_wiki::WritePageRequest;+ use axum::http::Request;+ use std::future::Future;+ use std::pin::Pin;+ use tempfile::TempDir;+ use tower::ServiceExt;- /// `inject_into_head` must skip `<head` literals sitting inside an- /// HTML comment, otherwise a custom SPA whose `index.html` had a- /// `<!-- <head> placeholder -->` comment would have the snippet- /// injected at the wrong place.- #[test]- fn inject_base_href_skips_head_inside_html_comment() {- let html =- "<!-- <head fake --><html><head><meta charset=\"utf-8\"></head><body></body></html>";- let out = inject_base_href(html, "/w/");- // Snippet must follow the REAL <head>, not the commented one —- // the comment must remain unmodified.- assert!(out.contains("<!-- <head fake -->"));- assert!(out.contains("<head><base href=\"/w/\"><meta"));- }-- /// `inject_into_head` falls back to the prepend path on an- /// unterminated comment instead of looping forever (defensive).- #[test]- fn inject_base_href_on_unterminated_comment_falls_back_to_prepend() {- let html = "<!-- never closes <head>";- let out = inject_base_href(html, "/w/");- assert!(out.starts_with("<base href=\"/w/\">"));- }-- #[test]- fn web_base_href_never_protocol_relative() {- assert_eq!(web_base_href("", "/web"), "/web/");- assert_eq!(web_base_href("/wiki", "/web"), "/wiki/web/");- assert_eq!(web_base_href("/wiki", "/"), "/wiki/");- assert_eq!(web_base_href("", "/"), "/");- assert_eq!(web_base_href("/", "/"), "/");- assert_eq!(web_base_href("/wiki/", "web"), "/wiki/web/");- for (b, s) in [("", "/"), ("/", "/"), ("//", "//")] {- assert!(!web_base_href(b, s).starts_with("//"));- }- }-- #[test]- fn inject_base_href_after_head() {- let html = "<!doctype html><html><head><meta charset=\"utf-8\"></head><body></body></html>";- let out = inject_base_href(html, "/wiki/web/");- assert!(out.contains("<head><base href=\"/wiki/web/\"><meta"));- }-- #[test]- fn inject_base_href_no_head_prepends() {- let out = inject_base_href("<html></html>", "/x/");- assert!(out.starts_with("<base href=\"/x/\"><html>"));- }-- #[test]- fn inject_base_href_escapes_attr() {- let out = inject_base_href("<head></head>", "/a\"b/");- assert!(out.contains("<base href=\"/a"b/\">"));- }-- #[test]- fn inject_base_path_meta_emits_meta() {- let out = inject_base_path_meta("<head></head>", "/wiki");- assert!(out.contains("<meta name=\"ai-memory-base-path\" content=\"/wiki\">"));- // Empty base path => empty content (SPA falls back to root).- let empty = inject_base_path_meta("<head></head>", "");- assert!(empty.contains("content=\"\""));- }-}--/// Path / URL config the web mount needs. Bundling these together-/// keeps `mount_web_router` and its helpers under clippy's-/// `too_many_arguments` threshold without `#[allow]` papering over-/// the call shape.-pub(crate) struct WebMountSpec<'a> {- pub web_ui_dir: Option<&'a Path>,- pub cors_origins: &'a [String],- pub web_slug: &'a str,- pub base_href: &'a str,- pub base_path: &'a str,-}--/// Orchestrator: assemble the `/api/v1` + web-UI surfaces on top of-/// `router`. Skips everything when web is disabled. Each concern lives-/// in a dedicated helper below so this function reads as a four-step-/// recipe (CORS-scoped API, slug normalisation, SPA-vs-builtin choice,-/// final mount).-fn mount_web_router(- router: axum::Router,- enable_web: bool,- reader: ReaderPool,- wiki: Wiki,- spec: WebMountSpec<'_>,-) -> Result<axum::Router> {- if !enable_web {- return Ok(router);- }- // Register the web surfaces BEFORE applying the bearer middleware. In- // axum 0.8, `.layer()` only attaches to routes registered before the- // call; nesting after the layer would silently bypass auth for /web/*.- let router = router.nest(- "/api/v1",- build_api_router(&reader, &wiki, spec.cors_origins),- );-- // Where the UI is mounted WITHIN the (already-applied) base path.- // Empty slug => the UI is the root of the base path itself.- let slug = normalize_prefix(spec.web_slug);- let mount = if slug.is_empty() { "/" } else { slug.as_str() };-- // Custom SPA via --web-ui-dir (SPA fallback to index.html), otherwise- // the built-in server-side wiki browser. In both cases the served- // index carries an injected `<base href>` so relative asset/router- // URLs resolve under `{base_path}{web_slug}`.- if let Some(dir) = spec.web_ui_dir {- return mount_custom_spa(router, dir, &slug, spec.base_href, spec.base_path, mount);- }- Ok(mount_builtin_browser(- router,- reader,- wiki,- &slug,- spec.base_href,- mount,- ))-}--/// Build the `/api/v1` router and apply the per-origin CORS layer if-/// the operator configured any. The layer is scoped to this router only-/// (CORS_NOT_APPLIED_TO_OTHER_ROUTES invariant — `/mcp`, `/hook`,-/// `/admin`, and `/web` must remain CORS-free).-fn build_api_router(reader: &ReaderPool, wiki: &Wiki, cors_origins: &[String]) -> axum::Router {- let api = ai_memory_web::api_router(reader.clone(), wiki.clone());- if cors_origins.is_empty() {- return api;- }- // Origins were already validated before binding, so parsing here- // is expected to succeed; `.expect` surfaces a logic bug if it does not.- let parsed: Vec<axum::http::HeaderValue> = cors_origins- .iter()- .map(|o| o.parse().expect("pre-validated origin must parse"))- .collect();- let cors = CorsLayer::new()- .allow_origin(parsed)- .allow_methods([Method::GET, Method::POST, Method::OPTIONS])- .allow_headers([header::AUTHORIZATION, header::CONTENT_TYPE])- .allow_credentials(true)- .max_age(Duration::from_secs(600));- info!(origins = ?cors_origins, "CORS layer attached to /api/v1");- api.layer(cors)-}--/// Mount the operator's custom SPA from `--web-ui-dir`. Reads-/// `index.html`, injects `<base href>` plus the `ai-memory-base-path`-/// meta, and serves the rest as static assets with an SPA fallback to-/// the injected shell. Errors surface as `anyhow` with the dir path.-fn mount_custom_spa(- router: axum::Router,- dir: &Path,- slug: &str,- base_href: &str,- base_path: &str,- mount: &str,-) -> Result<axum::Router> {- let dir = dir.to_path_buf();- let raw = std::fs::read_to_string(dir.join("index.html"))- .with_context(|| format!("reading custom web UI index at {}", dir.display()))?;- let injected = inject_base_path_meta(&inject_base_href(&raw, base_href), base_path);- info!(mount, base_href, base_path, "custom web UI mounted");- let spa = custom_spa_router(dir, injected.clone());- Ok(if slug.is_empty() {- router.merge(spa)- } else {- // `nest(slug, …)` routes `{slug}` (→ inner `/`) and `{slug}/<path>`- // (→ inner `/{*path}`), but NOT the bare trailing-slash root- // `{slug}/` — that empty sub-path matches neither, so it 404s. The- // SPA router normalises its home to exactly that URL, so a refresh on- // the app root returned a hard 404 (`custom_spa_trailing_slash_root_*`).- // Serve the injected shell there too. Unlike the builtin browser —- // which redirects `{slug}/` → `{slug}` — a SPA is happier staying put- // on a 200 than bouncing through a redirect on every root refresh.- let slash_index = Arc::new(injected);- router- .route(- &format!("{slug}/"),- axum::routing::get(move || {- let body = slash_index.clone();- async move { axum::response::Html((*body).clone()) }- }),- )- .nest(slug, spa)- })-}--/// Mount the built-in server-rendered wiki browser at `slug` with-/// `<base href>` injection middleware. When `slug` is non-empty also-/// register the trailing-slash → canonical redirect, preserving any-/// query string the caller passed.-fn mount_builtin_browser(- router: axum::Router,- reader: ReaderPool,- wiki: Wiki,- slug: &str,- base_href: &str,- mount: &str,-) -> axum::Router {- // The built-in browser emits RELATIVE asset/link URLs (`static/…`,- // `w/…`, `search`, `.`). Inject a `<base href>` into every HTML- // response so they resolve under `{base_path}{web_slug}/` — the- // same anchoring the custom SPA gets via its injected index.- let web_router = ai_memory_web::router(reader, wiki).layer(- axum::middleware::from_fn_with_state(Arc::new(base_href.to_string()), inject_web_base_href),- );- info!(mount, base_href, "read-only wiki browser mounted");- if slug.is_empty() {- return router.merge(web_router);- }- // Strip-trailing-slash redirect. The target must carry the full- // external prefix because the surrounding `nest(&base_path, …)`- // does NOT rewrite Location headers. Derive it from base_href- // (which already folds in base_path + slug). The closure takes- // `Uri` so `?q=x` survives — see- // `trailing_slash_redirect_preserves_query_string`.- let canonical = {- let trimmed = base_href.trim_end_matches('/');- if trimmed.is_empty() {- "/".to_string()- } else {- trimmed.to_string()- }- };- router- .route(- &format!("{slug}/"),- axum::routing::get(move |uri: axum::http::Uri| {- let to = match uri.query() {- Some(q) if !q.is_empty() => format!("{canonical}?{q}"),- _ => canonical.clone(),- };- async move { axum::response::Redirect::permanent(&to) }- }),- )- .nest(slug, web_router)-}--fn custom_spa_router(dir: std::path::PathBuf, injected_index: String) -> axum::Router {- let index = Arc::new(injected_index);- let root_index = index.clone();- let direct_index = index.clone();- let fallback_index = index.clone();-- // Assets are served as files; any missing asset path falls back to the- // injected index for SPA client routes. Direct `/index.html` is routed- // explicitly so it cannot bypass injection by being served from disk.- let assets = ServeDir::new(dir)- .append_index_html_on_directories(false)- .fallback(service_fn(move |_req: Request<Body>| {- let body = fallback_index.clone();- async move {- Ok::<_, Infallible>(axum::response::Html((*body).clone()).into_response())- }- }));-- axum::Router::new()- .route(- "/",- axum::routing::get(move || {- let body = root_index.clone();- async move { axum::response::Html((*body).clone()) }- }),- )- .route(- "/index.html",- axum::routing::get(move || {- let body = direct_index.clone();- async move { axum::response::Html((*body).clone()) }- }),- )- .route_service("/{*path}", assets)-}--/// Response middleware: inject `<base href>` into `text/html` responses from-/// the built-in server-rendered web browser, so its relative URLs resolve-/// under the configured `{base_path}{web_slug}` prefix. Non-HTML responses-/// (static assets, redirects) pass through untouched.-async fn inject_web_base_href(- State(base_href): State<Arc<String>>,- req: Request<Body>,- next: Next,-) -> Response {- let resp = next.run(req).await;- let is_html = resp- .headers()- .get(header::CONTENT_TYPE)- .and_then(|v| v.to_str().ok())- .is_some_and(|ct| ct.starts_with("text/html"));- if !is_html {- return resp;- }- let (mut parts, body) = resp.into_parts();- // 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 too large or read error\n",- )- .into_response();- }- };- match std::str::from_utf8(&bytes) {- Ok(html) => {- let injected = inject_base_href(html, &base_href);- // Stale length from the pre-injection body; let hyper recompute.- parts.headers.remove(header::CONTENT_LENGTH);- Response::from_parts(parts, Body::from(injected))- }- Err(_) => Response::from_parts(parts, Body::from(bytes)),- }-}--fn apply_http_layers(- router: axum::Router,- auth_state: Arc<AuthState>,- allowed_hosts: Vec<String>,-) -> axum::Router {- router- .layer(axum::middleware::from_fn_with_state(- auth_state,- require_bearer,- ))- .layer(axum::middleware::from_fn_with_state(- Arc::new(allowed_hosts),- require_allowed_host,- ))-}--async fn require_allowed_host(- State(allowed_hosts): State<Arc<Vec<String>>>,- req: Request<Body>,- next: Next,-) -> Response {- let Some(host) = req- .headers()- .get(header::HOST)- .and_then(|h| h.to_str().ok())- else {- return (StatusCode::BAD_REQUEST, "missing Host header\n").into_response();- };- if host_allowed(host, &allowed_hosts) {- return next.run(req).await;- }- tracing::warn!(host, allowed = ?allowed_hosts, "rejected request with disallowed Host header");- (StatusCode::FORBIDDEN, "forbidden host\n").into_response()-}--fn host_allowed(host: &str, allowed_hosts: &[String]) -> bool {- allowed_hosts.iter().any(|allowed| {- host.eq_ignore_ascii_case(allowed) || host_without_port(host).eq_ignore_ascii_case(allowed)- })-}--fn host_without_port(host: &str) -> &str {- if let Some(rest) = host.strip_prefix('[')- && let Some((inside, _)) = rest.split_once(']')- {- return inside;- }- match host.rsplit_once(':') {- Some((name, port)) if !name.contains(':') && port.chars().all(|c| c.is_ascii_digit()) => {- name- }- _ => host,- }-}--#[cfg(test)]-mod tests {- use super::*;- use ai_memory_core::{AgentKind, NewSession, PagePath, Tier};- use ai_memory_llm::{ChatRequest, ChatResponse, LlmResult, SyntheticEmbedder};- use ai_memory_wiki::WritePageRequest;- use axum::http::Request;- use std::future::Future;- use std::pin::Pin;- use tempfile::TempDir;- use tower::ServiceExt;-- async fn wait_for_maintenance_success(- store: &Store,- job: ai_memory_store::MaintenanceJob,- ) -> i64 {- let deadline = std::time::Instant::now() + Duration::from_secs(5);- loop {- if let Some(last_success) = store- .reader- .maintenance_job_last_success(job)- .await- .unwrap()- {- return last_success;- }- assert!(- std::time::Instant::now() < deadline,- "maintenance writer did not persist successful completion"- );- tokio::task::yield_now().await;- }+ async fn wait_for_maintenance_success(+ store: &Store,+ job: ai_memory_store::MaintenanceJob,+ ) -> i64 {+ let deadline = std::time::Instant::now() + Duration::from_secs(5);+ loop {+ if let Some(last_success) = store+ .reader+ .maintenance_job_last_success(job)+ .await+ .unwrap()+ {+ return last_success;+ }+ assert!(+ std::time::Instant::now() < deadline,+ "maintenance writer did not persist successful completion"+ );+ tokio::task::yield_now().await;+ }}#[test]@@ -2993,676 +2079,72 @@ mod tests {assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);}- /// Assemble the web/API surface under `base_path` + `web_slug` exactly the- /// way the `serve` handler does (mount, then `nest(&base_path, …)`), with- /// no auth/host layers so tests probe routing + injection in isolation.- /// Returns the `TempDir` guard too — the caller must keep it alive for the- /// router's lifetime (the store's SQLite + wiki files live under it).- fn based_web_router(base_path: &str, web_slug: &str) -> (TempDir, axum::Router) {+ #[tokio::test]+ async fn embedder_mismatch_warns_but_keeps_server_startable() {let tmp = TempDir::new().unwrap();let store = Store::open(tmp.path()).unwrap();- let wiki = Wiki::new(tmp.path(), store.writer.clone()).unwrap();- let base = normalize_prefix(base_path);- let base_href = web_base_href(base_path, web_slug);- let router = mount_web_router(- axum::Router::new(),- true,- store.reader.clone(),- wiki,- WebMountSpec {- web_ui_dir: None,- cors_origins: &[],- web_slug,- base_href: &base_href,- base_path: &base,- },- )+ 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 synthetic: Arc<dyn Embedder> = Arc::new(SyntheticEmbedder::new(64));+ let wiki = Wiki::new(tmp.path(), store.writer.clone())+ .unwrap()+ .with_embedder(synthetic);+ wiki.write_page(WritePageRequest {+ workspace_id: ws,+ project_id: proj,+ path: PagePath::new("notes/old-embedding.md").unwrap(),+ frontmatter: serde_json::json!({"title": "old embedding"}),+ body: "existing vector row".into(),+ tier: Tier::Semantic,+ pinned: false,+ title: None,+ admission_ctx: None,+ author_id: None,+ actor: ai_memory_core::ActorContext::anonymous(),+ })+ .await.unwrap();- let router = if base.is_empty() {- router- } else {- axum::Router::new().nest(&base, router)- };- // Mirror the production favicon mount in serve handler: at the- // absolute host root, outside the base-path nest.- let router = router.merge(ai_memory_web::favicon_router());- (tmp, router)- }- #[tokio::test]- async fn base_path_nests_all_surfaces_and_root_404s() {- let (_tmp, router) = based_web_router("/wiki", "/web");+ let cfg = Config {+ data_dir: tmp.path().to_path_buf(),+ embedding_provider: Some("openai".into()),+ runtime_env: crate::config::RuntimeEnv::with_openai_api_key_for_tests("test-key"),+ ..Config::default()+ };+ let wiki = Wiki::new(tmp.path(), store.writer.clone()).unwrap();- // The web UI is reachable UNDER the prefix…- let under = router- .clone()- .oneshot(- Request::builder()- .uri("/wiki/web")- .body(Body::empty())- .unwrap(),- )+ let provider_health = ProviderHealth::default();+ let (_wiki, embedder) = configure_embedder(&cfg, &store, wiki, &provider_health).await.unwrap();- assert_eq!(under.status(), StatusCode::OK);- // …and the API too.- let api = router- .clone()- .oneshot(- Request::builder()- .uri("/wiki/api/v1/projects")- .body(Body::empty())- .unwrap(),- )- .await- .unwrap();- assert_eq!(api.status(), StatusCode::OK);-- // The same paths at the host ROOT must 404 — nothing leaks outside the- // prefix (the whole point of base-path hosting behind a shared proxy).- for uri in ["/web", "/api/v1/projects"] {- let root = router- .clone()- .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())- .await- .unwrap();- assert_eq!(- root.status(),- StatusCode::NOT_FOUND,- "{uri} must 404 at root"- );- }+ let embedder = embedder.expect("configured embedder should be enabled");+ assert_eq!(embedder.provider(), "openai");+ assert_eq!(+ provider_health.snapshot().embedding.status,+ ai_memory_llm::ProviderHealthStatus::Unknown+ );}- #[tokio::test]- async fn inject_web_base_href_targets_html_only() {- let (_tmp, router) = based_web_router("/wiki", "/web");+ // ── Part B: CORS validation tests ──────────────────────────────────────- // HTML response carries the injected <base href> under the prefix.- let html_resp = router- .clone()- .oneshot(- Request::builder()- .uri("/wiki/web")- .body(Body::empty())- .unwrap(),- )- .await- .unwrap();- assert_eq!(html_resp.status(), StatusCode::OK);- let html = axum::body::to_bytes(html_resp.into_body(), usize::MAX)- .await- .unwrap();- let html = std::str::from_utf8(&html).unwrap();+ #[test]+ fn validate_cors_origins_rejects_wildcard() {+ let err = validate_cors_origins(&["*".to_string()]).unwrap_err();assert!(- html.contains(r#"<base href="/wiki/web/">"#),- "expected injected base href, got: {html}"+ err.to_string().contains("wildcard"),+ "error must mention wildcard: {err}");-- // A non-HTML asset passes through untouched (no <base> smuggled in).- let css_resp = router- .clone()- .oneshot(- Request::builder()- .uri("/wiki/web/static/tailwind.css")- .body(Body::empty())- .unwrap(),- )- .await- .unwrap();- assert_eq!(css_resp.status(), StatusCode::OK);- let css = axum::body::to_bytes(css_resp.into_body(), usize::MAX)- .await- .unwrap();- assert!(- !std::str::from_utf8(&css).unwrap().contains("<base href"),- "non-HTML asset must not receive a <base href> injection"- );- }-- #[tokio::test]- async fn trailing_slash_redirect_carries_the_prefix() {- let (_tmp, router) = based_web_router("/wiki", "/web");- let resp = router- .oneshot(- Request::builder()- .uri("/wiki/web/")- .body(Body::empty())- .unwrap(),- )- .await- .unwrap();- assert_eq!(resp.status(), StatusCode::PERMANENT_REDIRECT);- // Location must include the external prefix — the surrounding base nest- // does NOT rewrite Location headers, so a bare `/web` would drop `/wiki`.- assert_eq!(resp.headers().get(header::LOCATION).unwrap(), "/wiki/web",);- }-- /// Trailing-slash redirect must preserve the query string. The- /// original handler took `()` and silently dropped `?q=x`, so a- /// link in the SPA that appended a filter param round-tripped to- /// the canonical URL with the param missing. Fragments are- /// client-only and never reach the server, so we only assert query.- #[tokio::test]- async fn trailing_slash_redirect_preserves_query_string() {- let (_tmp, router) = based_web_router("/wiki", "/web");- let resp = router- .oneshot(- Request::builder()- .uri("/wiki/web/?q=foo&limit=5")- .body(Body::empty())- .unwrap(),- )- .await- .unwrap();- assert_eq!(resp.status(), StatusCode::PERMANENT_REDIRECT);- assert_eq!(- resp.headers().get(header::LOCATION).unwrap(),- "/wiki/web?q=foo&limit=5",- "redirect must carry the original query, not drop it"- );- }-- /// Nested base paths (`/a/b/c`) — exercised by the normaliser- /// unit test but not previously end-to-end. Routes must be- /// reachable under the full prefix and 404 at any shorter prefix.- #[tokio::test]- async fn nested_base_path_nests_web_and_api() {- let (_tmp, router) = based_web_router("/a/b/c", "/web");- // Full nested prefix reaches both surfaces.- for uri in ["/a/b/c/web", "/a/b/c/api/v1/projects"] {- let resp = router- .clone()- .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())- .await- .unwrap();- assert_eq!(- resp.status(),- StatusCode::OK,- "{uri} must reach the surface"- );- }- // A SHORTER prefix (e.g. only /a/b) must NOT leak — the nest is- // exactly `/a/b/c` and any partial mount is unmapped.- for uri in ["/a/b/web", "/a/api/v1/projects"] {- let resp = router- .clone()- .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())- .await- .unwrap();- assert_eq!(- resp.status(),- StatusCode::NOT_FOUND,- "{uri} must 404 — leaks the prefix otherwise"- );- }- }-- /// Custom-SPA index that already carries its own `<base href>` —- /// today we prepend another one; HTML5 says the browser honours- /// the FIRST `<base>` so the injection becomes a silent no-op.- /// Documented behaviour (see `inject_into_head` doc-comment) but- /// also exercised here so a future change to "replace existing"- /// gets noticed by a failing test rather than silently shipping.- #[test]- fn inject_base_href_with_existing_base_tag_does_not_replace() {- let html = "<html><head><base href=\"/old/\"><title>x</title></head></html>";- let out = inject_base_href(html, "/new/");- // Injected snippet appears first, the old <base> remains too.- let new_pos = out.find("<base href=\"/new/\">").expect("new injected");- let old_pos = out.find("<base href=\"/old/\">").expect("old preserved");- assert!(- new_pos < old_pos,- "injected base must appear before the pre-existing one (browser ignores duplicates after the first)"- );- }-- /// Post-merge audit (Phase 7 live test) caught that PR #79's- /// `/favicon.ico` route was nested inside `/web`, so it lived at- /// `/web/favicon.ico` and the browser's automatic root fetch always- /// 404'd. Fix: a separate `favicon_router()` mounted at the absolute- /// HOST root, outside `--base-path` and outside the `/web` nest.- /// This test pins both:- /// * `/favicon.ico` at the host root returns the PNG.- /// * Under `--base-path /wiki`, the favicon STAYS at root —- /// browsers fetch `<host>/favicon.ico` regardless of where the- /// app is mounted; the route must not move with the prefix.- #[tokio::test]- async fn favicon_lives_at_host_root_regardless_of_base_path() {- for base_path in ["", "/wiki"] {- let (_tmp, router) = based_web_router(base_path, "/web");- let resp = router- .oneshot(- Request::builder()- .uri("/favicon.ico")- .body(Body::empty())- .unwrap(),- )- .await- .unwrap();- assert_eq!(- resp.status(),- StatusCode::OK,- "/favicon.ico must be reachable at host root (base_path={base_path:?})"- );- assert_eq!(- resp.headers()- .get(axum::http::header::CONTENT_TYPE)- .and_then(|v| v.to_str().ok()),- Some("image/png"),- );- }- }-- #[tokio::test]- async fn no_base_path_is_byte_equivalent_at_root() {- let (_tmp, router) = based_web_router("", "/web");- let resp = router- .clone()- .oneshot(Request::builder().uri("/web").body(Body::empty()).unwrap())- .await- .unwrap();- assert_eq!(resp.status(), StatusCode::OK);- let body = axum::body::to_bytes(resp.into_body(), usize::MAX)- .await- .unwrap();- assert!(- std::str::from_utf8(&body)- .unwrap()- .contains(r#"<base href="/web/">"#),- "default mount should inject the root-relative base href"- );- }-- #[tokio::test]- async fn custom_spa_index_routes_are_injected_under_base_path() {- let tmp = TempDir::new().unwrap();- let store = Store::open(tmp.path()).unwrap();- let wiki = Wiki::new(tmp.path(), store.writer.clone()).unwrap();- let ui = TempDir::new().unwrap();- std::fs::write(- ui.path().join("index.html"),- "<!doctype html><html><head><title>spa</title></head><body>shell</body></html>",- )- .unwrap();- std::fs::write(ui.path().join("app.js"), "console.log('asset');").unwrap();-- let base = normalize_prefix("/wiki");- let base_href = web_base_href("/wiki", "/web");- let router = mount_web_router(- axum::Router::new(),- true,- store.reader.clone(),- wiki,- WebMountSpec {- web_ui_dir: Some(ui.path()),- cors_origins: &[],- web_slug: "/web",- base_href: &base_href,- base_path: &base,- },- )- .unwrap();- let router = axum::Router::new().nest(&base, router);-- for uri in [- "/wiki/web",- "/wiki/web/", // trailing-slash root: SPA home after router normalises it- "/wiki/web/index.html",- "/wiki/web/client/route",- ] {- let resp = router- .clone()- .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())- .await- .unwrap();- assert_eq!(resp.status(), StatusCode::OK, "{uri}");- let body = axum::body::to_bytes(resp.into_body(), usize::MAX)- .await- .unwrap();- let html = std::str::from_utf8(&body).unwrap();- assert!(- html.contains(r#"<base href="/wiki/web/">"#),- "{uri} must receive injected base href: {html}"- );- assert!(- html.contains(r#"<meta name="ai-memory-base-path" content="/wiki">"#),- "{uri} must receive injected API base-path meta: {html}"- );- assert!(html.contains("shell"), "{uri} returns the SPA shell");- }-- let asset = router- .oneshot(- Request::builder()- .uri("/wiki/web/app.js")- .body(Body::empty())- .unwrap(),- )- .await- .unwrap();- assert_eq!(asset.status(), StatusCode::OK);- let body = axum::body::to_bytes(asset.into_body(), usize::MAX)- .await- .unwrap();- let js = std::str::from_utf8(&body).unwrap();- assert_eq!(js, "console.log('asset');");- }-- /// Regression for the prod 404: with `base_path=""` (host root) the custom- /// SPA mounts at slug `/web`. `nest("/web", …)` served `/web` and- /// `/web/<route>` but left the bare trailing-slash root `/web/` unrouted →- /// hard 404. The SPA normalises its home to exactly `/web/`, so refreshing- /// the app root broke (both a host-root deploy and one mounted under a base- /// path like `/wiki`).- #[tokio::test]- async fn custom_spa_trailing_slash_root_serves_shell() {- let tmp = TempDir::new().unwrap();- let store = Store::open(tmp.path()).unwrap();- let wiki = Wiki::new(tmp.path(), store.writer.clone()).unwrap();- let ui = TempDir::new().unwrap();- std::fs::write(- ui.path().join("index.html"),- "<!doctype html><html><head><title>spa</title></head><body>shell</body></html>",- )- .unwrap();-- let base_href = web_base_href("", "/web");- let router = mount_web_router(- axum::Router::new(),- true,- store.reader.clone(),- wiki,- WebMountSpec {- web_ui_dir: Some(ui.path()),- cors_origins: &[],- web_slug: "/web",- base_href: &base_href,- base_path: "",- },- )- .unwrap();-- // `/web`, the trailing-slash root `/web/`, and a deep client route all- // serve the injected shell — none may 404.- for uri in ["/web", "/web/", "/web/projects/x"] {- let resp = router- .clone()- .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())- .await- .unwrap();- assert_eq!(- resp.status(),- StatusCode::OK,- "{uri} must serve the SPA shell, not 404"- );- let body = axum::body::to_bytes(resp.into_body(), usize::MAX)- .await- .unwrap();- let html = std::str::from_utf8(&body).unwrap();- assert!(- html.contains("shell"),- "{uri} returns the SPA shell: {html}"- );- assert!(- html.contains(r#"<base href="/web/">"#),- "{uri} must receive the injected base href: {html}"- );- }- }-- #[tokio::test]- async fn custom_spa_root_slug_does_not_shadow_api_routes() {- let tmp = TempDir::new().unwrap();- let store = Store::open(tmp.path()).unwrap();- let wiki = Wiki::new(tmp.path(), store.writer.clone()).unwrap();- let ui = TempDir::new().unwrap();- std::fs::write(- ui.path().join("index.html"),- "<!doctype html><html><head><title>spa</title></head><body>root shell</body></html>",- )- .unwrap();- std::fs::write(ui.path().join("app.js"), "console.log('root asset');").unwrap();-- let base = normalize_prefix("/wiki");- let base_href = web_base_href("/wiki", "/");- let router = mount_web_router(- axum::Router::new(),- true,- store.reader.clone(),- wiki,- WebMountSpec {- web_ui_dir: Some(ui.path()),- cors_origins: &[],- web_slug: "/",- base_href: &base_href,- base_path: &base,- },- )- .unwrap();- let router = axum::Router::new().nest(&base, router);-- for uri in ["/wiki", "/wiki/index.html", "/wiki/client/route"] {- let resp = router- .clone()- .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())- .await- .unwrap();- assert_eq!(resp.status(), StatusCode::OK, "{uri}");- let body = axum::body::to_bytes(resp.into_body(), usize::MAX)- .await- .unwrap();- let html = std::str::from_utf8(&body).unwrap();- assert!(- html.contains(r#"<base href="/wiki/">"#),- "{uri} must receive injected base href: {html}"- );- assert!(- html.contains(r#"<meta name="ai-memory-base-path" content="/wiki">"#),- "{uri} must receive injected API base-path meta: {html}"- );- assert!(html.contains("root shell"), "{uri} returns the SPA shell");- }-- let api = router- .clone()- .oneshot(- Request::builder()- .uri("/wiki/api/v1/projects")- .body(Body::empty())- .unwrap(),- )- .await- .unwrap();- assert_eq!(api.status(), StatusCode::OK);- let content_type = api- .headers()- .get(header::CONTENT_TYPE)- .and_then(|h| h.to_str().ok())- .unwrap_or_default();- assert!(- content_type.starts_with("application/json"),- "API route must not be shadowed by the root SPA: {content_type}"- );-- let asset = router- .oneshot(- Request::builder()- .uri("/wiki/app.js")- .body(Body::empty())- .unwrap(),- )- .await- .unwrap();- assert_eq!(asset.status(), StatusCode::OK);- let body = axum::body::to_bytes(asset.into_body(), usize::MAX)- .await- .unwrap();- let js = std::str::from_utf8(&body).unwrap();- assert_eq!(js, "console.log('root asset');");- }-- #[tokio::test]- async fn embedder_mismatch_warns_but_keeps_server_startable() {- let tmp = TempDir::new().unwrap();- let store = Store::open(tmp.path()).unwrap();- 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 synthetic: Arc<dyn Embedder> = Arc::new(SyntheticEmbedder::new(64));- let wiki = Wiki::new(tmp.path(), store.writer.clone())- .unwrap()- .with_embedder(synthetic);- wiki.write_page(WritePageRequest {- workspace_id: ws,- project_id: proj,- path: PagePath::new("notes/old-embedding.md").unwrap(),- frontmatter: serde_json::json!({"title": "old embedding"}),- body: "existing vector row".into(),- tier: Tier::Semantic,- pinned: false,- title: None,- admission_ctx: None,- author_id: None,- actor: ai_memory_core::ActorContext::anonymous(),- })- .await- .unwrap();-- let cfg = Config {- data_dir: tmp.path().to_path_buf(),- embedding_provider: Some("openai".into()),- runtime_env: crate::config::RuntimeEnv::with_openai_api_key_for_tests("test-key"),- ..Config::default()- };- let wiki = Wiki::new(tmp.path(), store.writer.clone()).unwrap();-- let provider_health = ProviderHealth::default();- let (_wiki, embedder) = configure_embedder(&cfg, &store, wiki, &provider_health)- .await- .unwrap();-- let embedder = embedder.expect("configured embedder should be enabled");- assert_eq!(embedder.provider(), "openai");- assert_eq!(- provider_health.snapshot().embedding.status,- ai_memory_llm::ProviderHealthStatus::Unknown- );- }-- #[tokio::test]- async fn auto_improve_scheduler_startup_init_preserves_first_interval_sessions() {- 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 first_project = store- .writer- .get_or_create_project(ws, "first", None)- .await- .unwrap();- let second_project = store- .writer- .get_or_create_project(ws, "second", None)- .await- .unwrap();-- for project_id in [first_project, second_project] {- let before_startup_init = SessionId::new();- store- .writer- .begin_session(NewSession {- id: before_startup_init,- workspace_id: ws,- project_id,- agent_kind: AgentKind::OpenCode,- cwd: None,- })- .await- .unwrap();- store- .writer- .end_session(before_startup_init, None)- .await- .unwrap();- }-- assert_eq!(- initialize_auto_improve_scheduler_scopes(&store.reader, &store.writer)- .await- .unwrap(),- (2, 0)- );-- tokio::time::sleep(std::time::Duration::from_millis(1)).await;- let mut first_interval_sessions = Vec::new();- for project_id in [first_project, second_project] {- let session_id = SessionId::new();- store- .writer- .begin_session(NewSession {- id: session_id,- workspace_id: ws,- project_id,- agent_kind: AgentKind::OpenCode,- cwd: None,- })- .await- .unwrap();- store.writer.end_session(session_id, None).await.unwrap();- first_interval_sessions.push((project_id, session_id));- }-- let mut settings = AutoImproveSettings::default();- settings.scheduler.min_session_age_secs = 0;- settings.scheduler.max_sessions_per_tick = 10;- let llm: Arc<dyn LlmProvider> = Arc::new(PanicLlm);- let outcome =- run_auto_improve_scheduler_tick(&store.reader, &store.writer, &wiki, &llm, &settings)- .await- .unwrap();-- assert_eq!(outcome.scopes, 2);- assert_eq!(outcome.scopes_with_candidates, 2);- assert_eq!(outcome.reviewed, 4);- assert_eq!(outcome.errors, 0);-- for (project_id, session_id) in first_interval_sessions {- let candidates = store- .reader- .auto_improve_candidate_sessions(ws, project_id, 0, 10)- .await- .unwrap();- assert!(- candidates.iter().all(|c| c.session_id != session_id),- "first-interval session should have been reviewed or claimed"- );- }- }-- // ── Part B: CORS validation tests ──────────────────────────────────────-- #[test]- fn validate_cors_origins_rejects_wildcard() {- let err = validate_cors_origins(&["*".to_string()]).unwrap_err();- assert!(- err.to_string().contains("wildcard"),- "error must mention wildcard: {err}"- );- }+ }#[test]fn validate_cors_origins_rejects_missing_scheme() {@@ -3717,148 +2199,4 @@ mod tests {]);}-- #[tokio::test]- async fn cors_layer_on_api_v1_allows_configured_origin() {- let tmp = TempDir::new().unwrap();- let store = Store::open(tmp.path()).unwrap();- let wiki = Wiki::new(tmp.path(), store.writer.clone()).unwrap();- store- .writer- .get_or_create_workspace("default")- .await- .unwrap();-- let cors_origins = ["https://app.example.com".to_string()];- let router = mount_web_router(- axum::Router::new(),- true,- store.reader.clone(),- wiki,- WebMountSpec {- web_ui_dir: None,- cors_origins: &cors_origins,- web_slug: "/web",- base_href: "/web/",- base_path: "",- },- )- .unwrap();- // No auth layer so we can reach /api/v1 directly.- let resp = router- .oneshot(- Request::builder()- .uri("/api/v1/workspaces")- .header("Origin", "https://app.example.com")- .body(Body::empty())- .unwrap(),- )- .await- .unwrap();-- assert_eq!(resp.status(), StatusCode::OK);- let acao = resp- .headers()- .get("access-control-allow-origin")- .expect("ACAO header must be present for allowed origin")- .to_str()- .unwrap();- assert_eq!(acao, "https://app.example.com");- let acac = resp- .headers()- .get("access-control-allow-credentials")- .expect("ACAC header must be present")- .to_str()- .unwrap();- assert_eq!(acac, "true");- }-- #[tokio::test]- async fn cors_layer_on_api_v1_denies_unlisted_origin() {- let tmp = TempDir::new().unwrap();- let store = Store::open(tmp.path()).unwrap();- let wiki = Wiki::new(tmp.path(), store.writer.clone()).unwrap();- store- .writer- .get_or_create_workspace("default")- .await- .unwrap();-- let cors_origins = ["https://app.example.com".to_string()];- let router = mount_web_router(- axum::Router::new(),- true,- store.reader.clone(),- wiki,- WebMountSpec {- web_ui_dir: None,- cors_origins: &cors_origins,- web_slug: "/web",- base_href: "/web/",- base_path: "",- },- )- .unwrap();- let resp = router- .oneshot(- Request::builder()- .uri("/api/v1/workspaces")- .header("Origin", "https://evil.example.com")- .body(Body::empty())- .unwrap(),- )- .await- .unwrap();-- // The request is still served (CORS does not block on the server side),- // but the ACAO header must be absent so the browser enforces the policy.- assert!(- resp.headers().get("access-control-allow-origin").is_none(),- "unlisted origin must not receive ACAO header"- );- }-- #[tokio::test]- async fn cors_not_applied_to_other_routes() {- // /mcp and /admin routes must not carry CORS headers even when- // a CORS origin list is configured (CORS_NOT_APPLIED_TO_OTHER_ROUTES- // invariant). We verify by checking that a request to a non-/api/v1- // path that 404s (no actual handler mounted here) still lacks ACAO.- let tmp = TempDir::new().unwrap();- let store = Store::open(tmp.path()).unwrap();- let wiki = Wiki::new(tmp.path(), store.writer.clone()).unwrap();-- let cors_origins = ["https://app.example.com".to_string()];- let router = mount_web_router(- axum::Router::new(),- true,- store.reader.clone(),- wiki,- WebMountSpec {- web_ui_dir: None,- cors_origins: &cors_origins,- web_slug: "/web",- base_href: "/web/",- base_path: "",- },- )- .unwrap();- // /web is a non-api route; sending an Origin header must not trigger CORS.- let resp = router- .oneshot(- Request::builder()- .uri("/web")- .header("Origin", "https://app.example.com")- .body(Body::empty())- .unwrap(),- )- .await- .unwrap();-- assert!(- resp.headers().get("access-control-allow-origin").is_none(),- "/web must not carry CORS headers: {:?}",- resp.headers()- );- }}diff --git a/crates/ai-memory-cli/src/http_client.rs b/crates/ai-memory-cli/src/http_client.rsindex 2b27275f..ab0d3592 100644--- a/crates/ai-memory-cli/src/http_client.rs+++ b/crates/ai-memory-cli/src/http_client.rs@@ -18,8 +18,8 @@ use anyhow::{Context, Result};use serde::Serialize;use serde::de::DeserializeOwned;-use crate::commands::serve::normalize_prefix;use crate::config::{Config, DEFAULT_SERVER_URL};+use ai_memory_web::normalize_prefix;/// Non-success response returned by the configured ai-memory server.#[derive(Debug)]@@ -656,9 +656,11 @@ mod tests {access: secrecy::SecretString::from("oidc-access".to_string()),refresh: secrecy::SecretString::from("refresh-token".to_string()),expires_at_ms: u64::MAX,- issuer: "https://issuer.example.com/realms/team".to_string(),- client_id: "ai-memory-cli".to_string(),- token_endpoint: "https://issuer.example.com/token".to_string(),+ extra: ai_memory_llm::OidcExtras {+ issuer: "https://issuer.example.com/realms/team".to_string(),+ client_id: "ai-memory-cli".to_string(),+ token_endpoint: "https://issuer.example.com/token".to_string(),+ },}.save(&config.oidc_device_token_path()).expect("save test OIDC token");diff --git a/crates/ai-memory-cli/src/main.rs b/crates/ai-memory-cli/src/main.rsindex 5dab2628..45f3bb7c 100644--- a/crates/ai-memory-cli/src/main.rs+++ b/crates/ai-memory-cli/src/main.rs@@ -13,7 +13,6 @@ use anyhow::Result;use clap::Parser;use tracing::info;-mod auth;mod auth_bearer;mod cli;mod commands;diff --git a/crates/ai-memory-consolidate/src/auto_improve_schedule.rs b/crates/ai-memory-consolidate/src/auto_improve_schedule.rsnew file mode 100644index 00000000..dcf3590c--- /dev/null+++ b/crates/ai-memory-consolidate/src/auto_improve_schedule.rs@@ -0,0 +1,549 @@+//! Scheduled auto-improvement orchestration.+//!+//! The server-side scheduler (started by `ai-memory serve`) drives one+//! non-overlapping tick per configured interval; this module owns what a+//! tick *does*: seed per-scope watermarks at startup, claim newly+//! completed sessions (at-most-once per session), run+//! [`run_auto_improve_review`], stage the validated proposals, write the+//! human-reviewable sidecars, and auto-approve them through the wiki+//! mutation path unless the operator requires manual approval.+//!+//! Approval-gate semantics are deliberately identical to the manual+//! CLI/admin/MCP path: proposals are always staged first, and+//! `require_approval` only decides whether they are applied immediately+//! or left pending — see `docs/auto-improvement-loop.md`.++use std::sync::Arc;++use ai_memory_core::{ActorContext, PagePath, ProjectId, SessionId, WorkspaceId};+use ai_memory_llm::LlmProvider;+use ai_memory_store::{+ ApproveAutoImproveProposalResult, AutoImproveProposalOperation, NewAutoImproveProposal,+ ReaderPool, StageAutoImproveRun, WriterHandle,+};+use ai_memory_wiki::Wiki;+use anyhow::Result;+use tracing::info;++use crate::{AutoImproveReport, AutoImproveReviewConfig, run_auto_improve_review};++/// Settings for the scheduled auto-improvement loop, already mapped from+/// the host's configuration. Bundles the review config with the+/// scheduler-only knobs so the tick driver takes a single value.+#[derive(Debug, Clone)]+pub struct ScheduledAutoImproveSettings {+ /// Full review configuration (`[auto_improve]`).+ pub review: AutoImproveReviewConfig,+ /// When true, validated proposals stay pending for manual review+ /// instead of being auto-approved (`[auto_improve] require_approval`).+ pub require_approval: bool,+ /// Minimum session age before a completed session becomes a+ /// candidate (`[auto_improve.scheduler] min_session_age_secs`).+ pub min_session_age_secs: u64,+ /// Maximum sessions reviewed per scope per tick+ /// (`[auto_improve.scheduler] max_sessions_per_tick`).+ pub max_sessions_per_tick: usize,+}++/// Seed the per-scope scheduler watermark for every known scope at+/// startup, so historical sessions are never auto-reviewed on upgrade.+/// Returns `(scopes, errors)`.+///+/// # Errors+/// Fails only when the scope list itself cannot be read; per-scope+/// state-init failures are logged and counted, not fatal.+pub async fn initialize_auto_improve_scheduler_scopes(+ reader: &ReaderPool,+ writer: &WriterHandle,+) -> Result<(usize, usize)> {+ let scopes = reader.list_all_scopes().await?;+ let total = scopes.len();+ let mut errors = 0usize;+ for scope in scopes {+ if let Err(e) = writer+ .ensure_auto_improve_scheduler_state(scope.workspace_id, scope.project_id)+ .await+ {+ errors += 1;+ tracing::warn!(+ workspace = %scope.workspace_name,+ project = %scope.project_name,+ error = %e,+ "auto-improve scheduler startup state init failed"+ );+ }+ }+ Ok((total, errors))+}++struct ScheduledAutoImproveOutcome {+ run_id: ai_memory_core::AutoImproveRunId,+ proposals: usize,+ approved: usize,+ pending: usize,+ conflicts: usize,+}++/// Aggregate counters for one scheduler tick across every scope.+#[derive(Debug, Default)]+pub struct ScheduledAutoImproveTickOutcome {+ /// Total scopes considered this tick.+ pub scopes: usize,+ /// Scopes with at least one unclaimed candidate session.+ pub scopes_with_candidates: usize,+ /// Sessions whose review completed (staged or empty).+ pub reviewed: usize,+ /// Per-scope/per-session failures, logged and counted, not fatal.+ pub errors: usize,+}++struct ScheduledAutoImproveContext<'a> {+ reader: &'a ReaderPool,+ writer: &'a WriterHandle,+ wiki: &'a Wiki,+ llm: &'a Arc<dyn LlmProvider>,+ workspace_id: WorkspaceId,+ project_id: ProjectId,+ settings: &'a ScheduledAutoImproveSettings,+}++/// One scheduler tick: claim newly completed sessions in every scope+/// (at-most-once via the persisted watermark) and run the auto-improve+/// review + staging pipeline for each. Failures are logged and counted+/// in the outcome; they never abort the tick.+///+/// # Errors+/// Fails only when the scope list itself cannot be read.+pub async fn run_auto_improve_scheduler_tick(+ reader: &ReaderPool,+ writer: &WriterHandle,+ wiki: &Wiki,+ llm: &Arc<dyn LlmProvider>,+ settings: &ScheduledAutoImproveSettings,+) -> Result<ScheduledAutoImproveTickOutcome> {+ let scopes = reader.list_all_scopes().await?;+ let mut outcome = ScheduledAutoImproveTickOutcome {+ scopes: scopes.len(),+ ..ScheduledAutoImproveTickOutcome::default()+ };++ for scope in scopes {+ if let Err(e) = writer+ .ensure_auto_improve_scheduler_state(scope.workspace_id, scope.project_id)+ .await+ {+ outcome.errors += 1;+ tracing::warn!(+ workspace = %scope.workspace_name,+ project = %scope.project_name,+ error = %e,+ "scheduled auto-improve state init failed"+ );+ continue;+ }++ let candidates = match reader+ .auto_improve_candidate_sessions(+ scope.workspace_id,+ scope.project_id,+ settings.min_session_age_secs,+ settings.max_sessions_per_tick,+ )+ .await+ {+ Ok(candidates) => candidates,+ Err(e) => {+ outcome.errors += 1;+ tracing::warn!(+ workspace = %scope.workspace_name,+ project = %scope.project_name,+ error = %e,+ "scheduled auto-improve candidate query failed"+ );+ continue;+ }+ };+ if candidates.is_empty() {+ continue;+ }++ outcome.scopes_with_candidates += 1;+ let ctx = ScheduledAutoImproveContext {+ reader,+ writer,+ wiki,+ llm,+ workspace_id: scope.workspace_id,+ project_id: scope.project_id,+ settings,+ };+ for candidate in candidates {+ let claimed = match ctx+ .writer+ .claim_auto_improve_scheduler_session(+ ctx.workspace_id,+ ctx.project_id,+ candidate.session_id,+ candidate.ended_at,+ )+ .await+ {+ Ok(claimed) => claimed,+ Err(e) => {+ outcome.errors += 1;+ tracing::warn!(+ workspace = %scope.workspace_name,+ project = %scope.project_name,+ session_id = %candidate.session_id,+ error = %e,+ "scheduled auto-improve claim failed"+ );+ continue;+ }+ };+ if !claimed {+ tracing::debug!(+ workspace = %scope.workspace_name,+ project = %scope.project_name,+ session_id = %candidate.session_id,+ "scheduled auto-improve candidate already claimed or reviewed"+ );+ continue;+ }+ match run_scheduled_auto_improve(&ctx, candidate.session_id).await {+ Ok(run) => {+ outcome.reviewed += 1;+ info!(+ workspace = %scope.workspace_name,+ project = %scope.project_name,+ session_id = %candidate.session_id,+ run_id = %run.run_id,+ proposals = run.proposals,+ approved = run.approved,+ pending = run.pending,+ conflicts = run.conflicts,+ "scheduled auto-improve completed"+ );+ }+ Err(e) => {+ outcome.errors += 1;+ tracing::warn!(+ workspace = %scope.workspace_name,+ project = %scope.project_name,+ session_id = %candidate.session_id,+ error = %e,+ "scheduled auto-improve failed"+ );+ }+ }+ }+ }++ Ok(outcome)+}++async fn run_scheduled_auto_improve(+ ctx: &ScheduledAutoImproveContext<'_>,+ session_id: SessionId,+) -> Result<ScheduledAutoImproveOutcome> {+ let cfg = ctx.settings.review.clone();+ let report = run_auto_improve_review(+ ctx.reader,+ &**ctx.llm,+ ctx.workspace_id,+ ctx.project_id,+ session_id,+ cfg.clone(),+ )+ .await?;+ let proposals =+ scheduled_auto_improve_new_proposals(ctx.reader, ctx.workspace_id, ctx.project_id, &report)+ .await?;+ let staged = ctx+ .writer+ .stage_auto_improve_run(StageAutoImproveRun {+ workspace_id: ctx.workspace_id,+ project_id: ctx.project_id,+ session_id: Some(session_id),+ provider: Some(report.provider.clone()),+ model: Some(report.model.clone()),+ summary: Some(report.summary.clone()),+ warnings_json: serde_json::to_value(&report.warnings)+ .unwrap_or_else(|_| serde_json::json!([])),+ rejected_candidates_json: serde_json::to_value(&report.rejected_candidates)+ .unwrap_or_else(|_| serde_json::json!([])),+ config_json: serde_json::json!({+ "trigger": "scheduler",+ "min_observations": cfg.min_observations,+ "min_session_duration_secs": cfg.min_session_duration_secs,+ "min_confidence": cfg.min_confidence,+ "max_input_tokens": cfg.max_input_tokens,+ "max_proposals_per_run": cfg.max_proposals_per_run,+ "include_raw_fallback": cfg.include_raw_fallback,+ "max_patchable_pages": cfg.max_patchable_pages,+ "max_patchable_body_chars": cfg.max_patchable_body_chars,+ "max_edits_per_proposal": cfg.max_edits_per_proposal,+ "max_edit_content_chars": cfg.max_edit_content_chars,+ "max_changed_chars_per_proposal": cfg.max_changed_chars_per_proposal,+ "max_patch_edits_per_run": cfg.max_patch_edits_per_run,+ "max_rejection_context": cfg.max_rejection_context,+ "rejection_context_days": cfg.rejection_context_days,+ "max_final_body_chars": cfg.max_final_body_chars,+ "max_rule_page_tokens": cfg.max_rule_page_tokens,+ "max_procedure_page_tokens": cfg.max_procedure_page_tokens,+ "eval": cfg.eval,+ "require_approval": ctx.settings.require_approval,+ }),+ proposal_actor: ActorContext {+ agent: Some(cfg.proposal_actor.clone()),+ ..ActorContext::default()+ },+ proposals,+ })+ .await?;++ for id in &staged.proposal_ids {+ ctx.wiki+ .write_auto_improve_sidecar(ctx.workspace_id, ctx.project_id, *id)+ .await?;+ }++ let mut approved = 0usize;+ let mut pending = 0usize;+ let mut conflicts = 0usize;+ for proposal_id in &staged.proposal_ids {+ if ctx.settings.require_approval {+ pending += 1;+ continue;+ }+ match ctx+ .wiki+ .approve_auto_improve_proposal(+ ctx.workspace_id,+ ctx.project_id,+ *proposal_id,+ ActorContext {+ agent: Some("auto_improve_scheduler_auto_approve".into()),+ ..ActorContext::default()+ },+ None,+ Some(ai_memory_wiki::AdmissionContext {+ op: ai_memory_wiki::AdmissionOp::WritePage,+ ..ai_memory_wiki::AdmissionContext::default()+ }),+ )+ .await?+ {+ ApproveAutoImproveProposalResult::Approved { .. } => approved += 1,+ ApproveAutoImproveProposalResult::Conflict => conflicts += 1,+ }+ }++ Ok(ScheduledAutoImproveOutcome {+ run_id: staged.run_id,+ proposals: staged.proposal_ids.len(),+ approved,+ pending,+ conflicts,+ })+}++async fn scheduled_auto_improve_new_proposals(+ reader: &ReaderPool,+ workspace_id: WorkspaceId,+ project_id: ProjectId,+ report: &AutoImproveReport,+) -> Result<Vec<NewAutoImproveProposal>> {+ let mut proposals = Vec::with_capacity(report.proposals.len());+ for p in &report.proposals {+ let path = PagePath::new(p.path.clone())?;+ let target_exists = reader+ .page_body_by_ids(workspace_id, project_id, path.as_str())+ .await?+ .is_some();+ let operation = if p.edit_mode == "patch"+ || (target_exists && path.as_str() == "_slots/current-focus.md")+ {+ AutoImproveProposalOperation::Update+ } else {+ AutoImproveProposalOperation::Create+ };+ let expected_base_body_sha256 = p+ .expected_base_body_sha256+ .as_deref()+ .map(hex_to_sha256)+ .transpose()+ .map_err(|e| anyhow::anyhow!("invalid expected_base_body_sha256: {e}"))?;+ proposals.push(NewAutoImproveProposal {+ operation,+ target_path: path,+ kind: p.kind.clone(),+ title: p.title.clone(),+ confidence: f64::from(p.confidence),+ rationale: p.rationale.clone(),+ evidence_json: serde_json::to_value(&p.evidence)+ .unwrap_or_else(|_| serde_json::json!([])),+ body_markdown: p.body_markdown.clone(),+ artifact_sha256: None,+ edit_mode: Some(p.edit_mode.clone()),+ patch_json: serde_json::to_value(&p.edits).ok(),+ expected_base_body_sha256,+ });+ }+ Ok(proposals)+}++fn hex_to_sha256(hex: &str) -> Result<[u8; 32], String> {+ if hex.len() != 64 {+ return Err("expected 64 hex chars".into());+ }+ let mut out = [0_u8; 32];+ for (idx, chunk) in hex.as_bytes().chunks_exact(2).enumerate() {+ let s = std::str::from_utf8(chunk).map_err(|e| e.to_string())?;+ out[idx] = u8::from_str_radix(s, 16).map_err(|e| e.to_string())?;+ }+ Ok(out)+}++#[cfg(test)]+mod tests {+ use super::*;+ use ai_memory_core::{AgentKind, NewSession};+ use ai_memory_llm::{ChatRequest, ChatResponse, LlmResult};+ use ai_memory_store::Store;+ use std::future::Future;+ use std::pin::Pin;+ use tempfile::TempDir;++ struct PanicLlm;++ impl LlmProvider for PanicLlm {+ fn name(&self) -> &'static str {+ "panic"+ }++ fn model(&self) -> &str {+ "panic"+ }++ 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 { panic!("preflight-skipped scheduler test must not call LLM") })+ }++ 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 { panic!("preflight-skipped scheduler test must not call LLM") })+ }+ }++ #[tokio::test]+ async fn auto_improve_scheduler_startup_init_preserves_first_interval_sessions() {+ 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 first_project = store+ .writer+ .get_or_create_project(ws, "first", None)+ .await+ .unwrap();+ let second_project = store+ .writer+ .get_or_create_project(ws, "second", None)+ .await+ .unwrap();++ for project_id in [first_project, second_project] {+ let before_startup_init = SessionId::new();+ store+ .writer+ .begin_session(NewSession {+ id: before_startup_init,+ workspace_id: ws,+ project_id,+ agent_kind: AgentKind::OpenCode,+ cwd: None,+ })+ .await+ .unwrap();+ store+ .writer+ .end_session(before_startup_init, None)+ .await+ .unwrap();+ }++ assert_eq!(+ initialize_auto_improve_scheduler_scopes(&store.reader, &store.writer)+ .await+ .unwrap(),+ (2, 0)+ );++ tokio::time::sleep(std::time::Duration::from_millis(1)).await;+ let mut first_interval_sessions = Vec::new();+ for project_id in [first_project, second_project] {+ let session_id = SessionId::new();+ store+ .writer+ .begin_session(NewSession {+ id: session_id,+ workspace_id: ws,+ project_id,+ agent_kind: AgentKind::OpenCode,+ cwd: None,+ })+ .await+ .unwrap();+ store.writer.end_session(session_id, None).await.unwrap();+ first_interval_sessions.push((project_id, session_id));+ }++ let settings = ScheduledAutoImproveSettings {+ review: AutoImproveReviewConfig::default(),+ require_approval: false,+ min_session_age_secs: 0,+ max_sessions_per_tick: 10,+ };+ let llm: Arc<dyn LlmProvider> = Arc::new(PanicLlm);+ let outcome =+ run_auto_improve_scheduler_tick(&store.reader, &store.writer, &wiki, &llm, &settings)+ .await+ .unwrap();++ assert_eq!(outcome.scopes, 2);+ assert_eq!(outcome.scopes_with_candidates, 2);+ assert_eq!(outcome.reviewed, 4);+ assert_eq!(outcome.errors, 0);++ for (project_id, session_id) in first_interval_sessions {+ let candidates = store+ .reader+ .auto_improve_candidate_sessions(ws, project_id, 0, 10)+ .await+ .unwrap();+ assert!(+ candidates.iter().all(|c| c.session_id != session_id),+ "first-interval session should have been reviewed or claimed"+ );+ }+ }+}diff --git a/crates/ai-memory-consolidate/src/consolidator.rs b/crates/ai-memory-consolidate/src/consolidator.rsindex 72a92e5b..e213a919 100644--- a/crates/ai-memory-consolidate/src/consolidator.rs+++ b/crates/ai-memory-consolidate/src/consolidator.rs@@ -365,12 +365,18 @@ impl Consolidator {let ids = self.wiki.apply_batch(requests).await?;let rationale_short = batch.rationale.chars().take(60).collect::<String>();- let _ = self.wiki.commit_all(&format!(- "consolidate-batch(session {}): {} page(s) — {}",- short_id(&session_id.to_string()),- ids.len(),- rationale_short,- ));+ let _ = self+ .wiki+ .commit_all(&format!(+ "consolidate-batch(session {}): {} page(s) — {}",+ short_id(&session_id.to_string()),+ ids.len(),+ rationale_short,+ ))+ .map_err(|e| {+ tracing::warn!(error = %e, "consolidate-batch auto-commit failed");+ e+ });let outcomes = outcomes_preview.into_iter()diff --git a/crates/ai-memory-consolidate/src/curator.rs b/crates/ai-memory-consolidate/src/curator.rsindex 5ae5b3b7..c31b561d 100644--- a/crates/ai-memory-consolidate/src/curator.rs+++ b/crates/ai-memory-consolidate/src/curator.rs@@ -277,3 +277,674 @@ fn age_days(now_us: i64, then_us: i64) -> f64 {let age = now_us.saturating_sub(then_us) as f64 / US_PER_DAY;age.max(0.0)}++#[cfg(test)]+mod tests {+ use super::*;+ use ai_memory_core::{ActorContext, NewPage, PageId, PagePath};+ use ai_memory_store::Store;+ use ai_memory_wiki::{Wiki, WritePageRequest};+ use rusqlite::params;+ use std::path::{Path, PathBuf};+ use tempfile::TempDir;++ const DAY_US: i64 = 86_400_000_000;++ struct Fixture {+ tmp: TempDir,+ store: Store,+ wiki: Wiki,+ ws: WorkspaceId,+ proj: ProjectId,+ }++ async fn seed_fixture() -> Fixture {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ 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 wiki = Wiki::new(tmp.path(), store.writer.clone()).unwrap();+ Fixture {+ tmp,+ store,+ wiki,+ ws,+ proj,+ }+ }++ /// Write one page through the normal wiki path (disk + store row).+ async fn write(+ fx: &Fixture,+ path: &str,+ title: &str,+ tier: Tier,+ pinned: bool,+ body: &str,+ ) -> PageId {+ fx.wiki+ .write_page(WritePageRequest {+ workspace_id: fx.ws,+ project_id: fx.proj,+ path: PagePath::new(path).unwrap(),+ frontmatter: serde_json::json!({"title": title}),+ body: body.to_string(),+ tier,+ pinned,+ title: Some(title.to_string()),+ admission_ctx: None,+ author_id: None,+ actor: ActorContext::anonymous(),+ })+ .await+ .unwrap()+ }++ /// Time-travel one page's timestamps via a secondary SQLite connection+ /// (same trick as `tests/lifecycle.rs`; WAL mode allows it while the+ /// writer actor is idle).+ fn backdate(fx: &Fixture, id: PageId, days: i64) {+ let db_path = fx.tmp.path().join("db/memory.sqlite");+ let conn = rusqlite::Connection::open(&db_path).unwrap();+ conn.pragma_update(None, "busy_timeout", 5_000).unwrap();+ let updated_at = Timestamp::now().as_microsecond() - days * DAY_US;+ conn.execute(+ "UPDATE pages SET created_at = ?1, updated_at = ?1 WHERE id = ?2",+ params![updated_at, id.as_bytes()],+ )+ .unwrap();+ }++ async fn run_report(fx: &Fixture) -> CuratorReport {+ run_curator_report(+ &fx.store.reader,+ fx.ws,+ fx.proj,+ "default",+ "scratch",+ CuratorParams::default(),+ )+ .await+ .unwrap()+ }++ fn findings_of_kind<'a>(report: &'a CuratorReport, kind: &str) -> Vec<&'a CuratorFinding> {+ report.findings.iter().filter(|f| f.kind == kind).collect()+ }++ #[test]+ fn age_days_handles_zero_exact_and_future_boundaries() {+ // 1.7e15 < 2^53, so these microsecond values are exact in f64.+ let now: i64 = 1_700_000_000_000_000;+ assert_eq!(age_days(now, now), 0.0, "same instant is zero days");+ assert_eq!(age_days(now, now - DAY_US), 1.0, "exactly one day");+ assert_eq!(age_days(now, now - 30 * DAY_US), 30.0, "exact threshold");+ assert_eq!(+ age_days(now, now + DAY_US),+ 0.0,+ "future timestamps clamp to zero"+ );+ assert_eq!(+ age_days(0, i64::MAX),+ 0.0,+ "saturating_sub avoids overflow on extreme values"+ );+ let half = age_days(now, now - DAY_US / 2);+ assert!((half - 0.5).abs() < 1e-9, "half a day: {half}");+ }++ #[test]+ fn normalize_title_collapses_whitespace_and_case() {+ assert_eq!(normalize_title(" Release Notes "), "release notes");+ assert_eq!(normalize_title("Single"), "single");+ assert_eq!(normalize_title(""), "");+ }++ #[test]+ fn frontmatter_pinned_only_true_for_explicit_boolean() {+ assert!(frontmatter_pinned(r#"{"pinned": true}"#));+ assert!(!frontmatter_pinned(r#"{"pinned": false}"#));+ assert!(!frontmatter_pinned(r#"{"pinned": "true"}"#));+ assert!(!frontmatter_pinned("{}"));+ assert!(!frontmatter_pinned("not json"));+ }++ #[tokio::test]+ async fn classifies_cold_episodic_and_skips_warm_and_non_episodic() {+ let fx = seed_fixture().await;+ let cold = write(+ &fx,+ "sessions/old.md",+ "Old Session",+ Tier::Episodic,+ false,+ "old session body",+ )+ .await;+ write(+ &fx,+ "sessions/fresh.md",+ "Fresh Session",+ Tier::Episodic,+ false,+ "fresh body",+ )+ .await;+ let semantic = write(+ &fx,+ "concepts/old-concept.md",+ "Old Concept",+ Tier::Semantic,+ false,+ "old concept body",+ )+ .await;+ backdate(&fx, cold, 200);+ backdate(&fx, semantic, 200);++ let report = run_report(&fx).await;++ assert_eq!(+ report.findings.len(),+ 1,+ "only the old unpinned episodic page is a finding: {:?}",+ report.findings+ );+ let finding = &report.findings[0];+ assert_eq!(finding.kind, "cold_episodic");+ assert_eq!(finding.severity, "info");+ assert_eq!(finding.pages, ["sessions/old.md"]);+ assert!(finding.message.contains("sessions/old.md"));+ let detail = finding.detail.as_ref().unwrap();+ let score = detail["score"].as_f64().unwrap();+ let threshold = detail["threshold"].as_f64().unwrap();+ assert!(+ score < threshold,+ "score {score} below threshold {threshold}"+ );+ assert!(detail["age_days"].as_f64().unwrap() >= 200.0);+ assert_eq!(detail["access_count"].as_i64().unwrap(), 0);+ assert_eq!(report.summary, "1 conservative curator finding(s).");+ }++ #[tokio::test]+ async fn pinned_pages_never_appear_as_cold_findings() {+ let fx = seed_fixture().await;+ // Column-pinned page, written through the normal wiki path.+ let pinned = write(+ &fx,+ "notes/pinned-old.md",+ "Pinned Old",+ Tier::Episodic,+ true,+ "pinned body",+ )+ .await;+ // Frontmatter-pinned only (column stays false) — the curator must+ // honor the frontmatter flag on its own. `Wiki::write_page` would+ // propagate frontmatter `pinned` into the column, so upsert the row+ // directly to isolate the frontmatter path.+ let fm_pinned = fx+ .store+ .writer+ .upsert_page(NewPage {+ workspace_id: fx.ws,+ project_id: fx.proj,+ path: PagePath::new("notes/fm-pinned.md").unwrap(),+ title: "FM Pinned".to_string(),+ body: "frontmatter-pinned body".to_string(),+ tier: Tier::Episodic,+ frontmatter_json: serde_json::json!({"title": "FM Pinned", "pinned": true}),+ pinned: false,+ links: Vec::new(),+ author_id: None,+ })+ .await+ .unwrap();+ backdate(&fx, pinned, 200);+ backdate(&fx, fm_pinned, 200);++ let report = run_report(&fx).await;++ assert!(+ report.findings.is_empty(),+ "pinned pages must never surface as findings: {:?}",+ report.findings+ );+ assert_eq!(report.summary, "No conservative curator findings.");+ }++ #[tokio::test]+ async fn classifies_stale_slots_with_per_slot_thresholds() {+ let fx = seed_fixture().await;+ // 8 days old: past the 7-day current-focus threshold…+ let focus = write(+ &fx,+ "_slots/current-focus.md",+ "Current Focus",+ Tier::Semantic,+ false,+ "focus body",+ )+ .await;+ // …but well under the 30-day threshold for other slots.+ let recent = write(+ &fx,+ "_slots/recent-notes.md",+ "Recent Notes",+ Tier::Semantic,+ false,+ "recent body",+ )+ .await;+ let old = write(+ &fx,+ "_slots/old-context.md",+ "Old Context",+ Tier::Semantic,+ false,+ "old body",+ )+ .await;+ backdate(&fx, focus, 8);+ backdate(&fx, recent, 8);+ backdate(&fx, old, 31);++ let report = run_report(&fx).await;++ let stale = findings_of_kind(&report, "stale_slot");+ assert_eq!(+ report.findings.len(),+ 2,+ "exactly two stale slots, nothing else: {:?}",+ report.findings+ );+ // Stale findings sort by page path.+ assert_eq!(stale[0].pages, ["_slots/current-focus.md"]);+ assert_eq!(stale[0].severity, "warning");+ assert!(stale[0].message.contains("threshold 7"));+ let focus_detail = stale[0].detail.as_ref().unwrap();+ assert_eq!(focus_detail["threshold_days"].as_f64().unwrap(), 7.0);+ assert!(focus_detail["age_days"].as_f64().unwrap() >= 8.0);+ assert_eq!(stale[1].pages, ["_slots/old-context.md"]);+ let old_detail = stale[1].detail.as_ref().unwrap();+ assert_eq!(old_detail["threshold_days"].as_f64().unwrap(), 30.0);+ assert!(old_detail["age_days"].as_f64().unwrap() >= 31.0);+ }++ #[tokio::test]+ async fn under_threshold_slots_and_pending_pages_produce_no_findings() {+ let fx = seed_fixture().await;+ // 6 days is under the 7-day current-focus threshold (comparison is+ // a strict `>`, so under-threshold slots must stay quiet).+ let focus = write(+ &fx,+ "_slots/current-focus.md",+ "Current Focus",+ Tier::Semantic,+ false,+ "focus body",+ )+ .await;+ backdate(&fx, focus, 6);+ // A `_pending/` page sharing a normalized title with a real page+ // must not count toward duplicate_title findings.+ write(+ &fx,+ "notes/release.md",+ "Release Notes",+ Tier::Semantic,+ false,+ "release body",+ )+ .await;+ write(+ &fx,+ "_pending/draft.md",+ "release notes",+ Tier::Episodic,+ false,+ "draft body",+ )+ .await;++ let report = run_report(&fx).await;++ assert!(+ report.findings.is_empty(),+ "under-threshold slots and _pending/ pages are excluded: {:?}",+ report.findings+ );+ }++ #[tokio::test]+ async fn classifies_duplicate_titles_case_and_whitespace_insensitively() {+ let fx = seed_fixture().await;+ write(+ &fx,+ "notes/release-a.md",+ "Release Notes",+ Tier::Semantic,+ false,+ "body a",+ )+ .await;+ write(+ &fx,+ "decisions/release-b.md",+ " release notes ",+ Tier::Semantic,+ false,+ "body b",+ )+ .await;+ write(+ &fx,+ "notes/unique.md",+ "Unique Title",+ Tier::Semantic,+ false,+ "body c",+ )+ .await;++ let report = run_report(&fx).await;++ assert_eq!(+ report.findings.len(),+ 1,+ "one duplicate-title finding only: {:?}",+ report.findings+ );+ let finding = &report.findings[0];+ assert_eq!(finding.kind, "duplicate_title");+ assert_eq!(finding.severity, "info");+ assert_eq!(+ finding.pages,+ ["decisions/release-b.md", "notes/release-a.md"],+ "pages are sorted"+ );+ assert!(finding.message.contains("2 pages share"));+ assert!(finding.message.contains("'release notes'"));+ assert_eq!(+ finding.detail.as_ref().unwrap()["normalized_title"],+ serde_json::json!("release notes")+ );+ }++ #[tokio::test]+ async fn classifies_dangling_cross_project_links() {+ let fx = seed_fixture().await;+ // "infra" exists as a project but lacks the target page; "ghost-project"+ // does not exist at all — the report must distinguish the two.+ fx.store+ .writer+ .get_or_create_project(fx.ws, "infra", None)+ .await+ .unwrap();+ write(+ &fx,+ "notes/links.md",+ "Links",+ Tier::Semantic,+ false,+ "deps: [[infra:runbooks/missing.md]] and [[ghost-project:nope.md]]",+ )+ .await;++ let report = run_report(&fx).await;++ let dangling = findings_of_kind(&report, "dangling_cross_project_link");+ assert_eq!(+ dangling.len(),+ 2,+ "both unresolved cross-project links reported: {:?}",+ report.findings+ );+ for finding in &dangling {+ assert_eq!(finding.severity, "warning");+ assert_eq!(finding.pages, ["notes/links.md"]);+ assert!(finding.message.contains("notes/links.md"));+ }+ let ghost = dangling+ .iter()+ .find(|f| f.message.contains("default/ghost-project/nope.md"))+ .expect("ghost-project finding");+ assert_eq!(+ ghost.detail.as_ref().unwrap()["project_exists"],+ serde_json::json!(false)+ );+ let infra = dangling+ .iter()+ .find(|f| f.message.contains("default/infra/runbooks/missing.md"))+ .expect("infra finding");+ assert_eq!(+ infra.detail.as_ref().unwrap()["project_exists"],+ serde_json::json!(true)+ );+ }++ #[tokio::test]+ async fn empty_project_reports_no_findings() {+ let fx = seed_fixture().await;++ let report = run_report(&fx).await;++ assert!(report.findings.is_empty());+ assert_eq!(report.summary, "No conservative curator findings.");+ assert!(report.dry_run);+ assert_eq!(report.workspace, "default");+ assert_eq!(report.project, "scratch");+ assert!(!report.generated_at.is_empty());+ }++ fn report_with(findings: Vec<CuratorFinding>) -> CuratorReport {+ let summary = if findings.is_empty() {+ "No conservative curator findings.".to_string()+ } else {+ format!("{} conservative curator finding(s).", findings.len())+ };+ CuratorReport {+ workspace: "default".into(),+ project: "scratch".into(),+ generated_at: "2026-07-23T00:00:00Z".into(),+ dry_run: true,+ summary,+ params: CuratorParams::default(),+ findings,+ }+ }++ #[test]+ fn render_markdown_includes_header_summary_and_findings() {+ let report = report_with(vec![+ CuratorFinding {+ kind: "stale_slot".into(),+ severity: "warning".into(),+ message: "Slot _slots/current-focus.md has not changed for 8 days (threshold 7)"+ .into(),+ pages: vec!["_slots/current-focus.md".into()],+ detail: None,+ },+ CuratorFinding {+ kind: "cold_episodic".into(),+ severity: "info".into(),+ message: "Episodic page sessions/old.md is cold (score 0.018, age 200 days)".into(),+ pages: Vec::new(),+ detail: None,+ },+ ]);++ let md = render_curator_report_markdown(&report);++ assert!(md.starts_with("# Curator Report\n"));+ assert!(md.contains("Report-only"));+ assert!(md.contains("- Workspace: `default`\n"));+ assert!(md.contains("- Project: `scratch`\n"));+ assert!(md.contains("- Generated: `2026-07-23T00:00:00Z`\n"));+ assert!(md.contains("- Summary: 2 conservative curator finding(s).\n"));+ assert!(md.contains("## Findings\n"));+ assert!(md.contains(+ "- **stale_slot** (warning) — Slot _slots/current-focus.md has not changed for 8 days (threshold 7)\n"+ ));+ assert!(md.contains(" - Pages: `_slots/current-focus.md`\n"));+ assert!(md.contains(+ "- **cold_episodic** (info) — Episodic page sessions/old.md is cold (score 0.018, age 200 days)\n"+ ));+ assert_eq!(+ md.matches(" - Pages:").count(),+ 1,+ "findings without pages render no Pages line"+ );+ }++ #[test]+ fn render_markdown_empty_report_omits_findings_section() {+ let md = render_curator_report_markdown(&report_with(Vec::new()));++ let expected = "# Curator Report\n\n\+ > Report-only: approving this pending write stores this report page only. \+ It does not edit, delete, merge, rewrite links, or update slots.\n\n\+ - Workspace: `default`\n\+ - Project: `scratch`\n\+ - Generated: `2026-07-23T00:00:00Z`\n\+ - Summary: No conservative curator findings.\n\n\+ No conservative curator findings.\n";+ assert_eq!(md, expected);+ assert!(!md.contains("## Findings"));+ }++ /// Snapshot of every file under the wiki root: relative path, bytes,+ /// mtime. The `.git` dir is included — a read-only report must not+ /// touch it either.+ fn wiki_snapshot(root: &Path) -> Vec<(PathBuf, Vec<u8>, std::time::SystemTime)> {+ let mut out = Vec::new();+ let mut stack = vec![root.to_path_buf()];+ while let Some(dir) = stack.pop() {+ for entry in std::fs::read_dir(&dir).unwrap() {+ let path = entry.unwrap().path();+ if path.is_dir() {+ stack.push(path);+ } else {+ let meta = std::fs::metadata(&path).unwrap();+ out.push((+ path.strip_prefix(root).unwrap().to_path_buf(),+ std::fs::read(&path).unwrap(),+ meta.modified().unwrap(),+ ));+ }+ }+ }+ out.sort_by(|a, b| a.0.cmp(&b.0));+ out+ }++ /// Row-level store snapshot via the same reader calls the curator makes.+ async fn store_snapshot(fx: &Fixture) -> String {+ let candidates = fx+ .store+ .reader+ .decay_candidates(fx.ws, fx.proj)+ .await+ .unwrap();+ let pages = fx+ .store+ .reader+ .list_pages("default", "scratch")+ .await+ .unwrap();+ let dangling = fx+ .store+ .reader+ .dangling_cross_project_links(fx.ws, fx.proj)+ .await+ .unwrap();+ serde_json::to_string(&serde_json::json!({+ "candidates": candidates,+ "pages": pages,+ "dangling": dangling,+ }))+ .unwrap()+ }++ #[tokio::test]+ async fn report_run_leaves_store_and_wiki_untouched() {+ let fx = seed_fixture().await;+ // Seed one finding of every kind so the run exercises all read paths.+ let cold = write(+ &fx,+ "sessions/old.md",+ "Old Session",+ Tier::Episodic,+ false,+ "old body",+ )+ .await;+ let focus = write(+ &fx,+ "_slots/current-focus.md",+ "Current Focus",+ Tier::Semantic,+ false,+ "focus body",+ )+ .await;+ backdate(&fx, cold, 200);+ backdate(&fx, focus, 8);+ write(+ &fx,+ "notes/dup-a.md",+ "Dup Title",+ Tier::Semantic,+ false,+ "a",+ )+ .await;+ write(+ &fx,+ "notes/dup-b.md",+ "dup title",+ Tier::Semantic,+ false,+ "b",+ )+ .await;+ write(+ &fx,+ "notes/links.md",+ "Links",+ Tier::Semantic,+ false,+ "dep: [[ghost-project:nope.md]]",+ )+ .await;++ let wiki_before = wiki_snapshot(fx.wiki.root());+ let store_before = store_snapshot(&fx).await;++ let report = run_report(&fx).await;+ assert!(+ !report.findings.is_empty(),+ "fixture must produce findings so the read paths actually run"+ );++ assert_eq!(+ store_snapshot(&fx).await,+ store_before,+ "report must not mutate store rows"+ );+ assert_eq!(+ wiki_snapshot(fx.wiki.root()),+ wiki_before,+ "report must not touch wiki files"+ );+ }+}diff --git a/crates/ai-memory-consolidate/src/embed.rs b/crates/ai-memory-consolidate/src/embed.rsnew file mode 100644index 00000000..71ad5d40--- /dev/null+++ b/crates/ai-memory-consolidate/src/embed.rs@@ -0,0 +1,174 @@+//! Embedding backfill — embed latest wiki pages that lack a current+//! `(provider, model, dim)` embedding row.+//!+//! Shared by the `POST /admin/embed` endpoint (which adds `reembed` /+//! `dry_run` and stale-row purging on top) and the server's scheduled+//! maintenance tick. Candidates are scanned once, embedded one at a+//! time, and flushed to the store in batches of+//! [`EMBEDDING_WRITE_BATCH`] so writes stay inside the single-writer+//! actor.++use std::collections::HashSet;+use std::sync::Arc;++use ai_memory_core::{ProjectId, WorkspaceId};+use ai_memory_llm::Embedder;+use ai_memory_store::{EmbeddingWrite, ReaderPool, WriterHandle, f32_vec_to_bytes};+use ai_memory_wiki::Wiki;+use serde::Serialize;+use thiserror::Error;+use tracing::warn;++/// Number of embedding rows written per `store_embeddings` call.+pub const EMBEDDING_WRITE_BATCH: usize = 100;++/// Outcome counts for one backfill run.+#[derive(Debug, Default, Clone, Copy, Serialize)]+pub struct EmbedBackfillCounts {+ /// Pages that were actually embedded (zero in dry-run).+ pub embedded: usize,+ /// Pages skipped because a matching embedding already existed or+ /// the body is empty.+ pub skipped: usize,+ /// Pages that failed to embed (read error or provider error).+ pub failed: usize,+ /// Pages that would be embedded in a live run (only meaningful+ /// when `dry_run` was requested).+ pub would_embed: usize,+}++impl EmbedBackfillCounts {+ /// Fold another run's counts into this one (multi-project sweeps).+ pub fn absorb(&mut self, other: Self) {+ self.embedded += other.embedded;+ self.skipped += other.skipped;+ self.failed += other.failed;+ self.would_embed += other.would_embed;+ }+}++/// Options for one backfill run.+#[derive(Debug, Default, Clone, Copy)]+pub struct EmbedBackfillOptions {+ /// When true, regenerates embeddings even for pages that already+ /// have one matching the current `(provider, model, dim)`.+ pub reembed: bool,+ /// When true, counts pages that would be embedded/skipped without+ /// calling the embedder or writing anything.+ pub dry_run: bool,+}++/// Errors raised by the backfill.+#[derive(Debug, Error)]+#[non_exhaustive]+pub enum EmbedBackfillError {+ /// Underlying store error.+ #[error(transparent)]+ Store(#[from] ai_memory_store::StoreError),+}++/// Backfill embeddings for one workspace/project.+///+/// Pages whose body is empty (after trimming) are skipped rather than+/// embedded. Per-page read/provider failures increment+/// [`EmbedBackfillCounts::failed`] and do not abort the run; only store+/// errors on the candidate lookups propagate.+///+/// # Errors+/// Propagates any store error encountered while reading candidates or+/// the set of already-embedded page ids.+pub async fn run_embedding_backfill(+ reader: &ReaderPool,+ writer: &WriterHandle,+ wiki: &Wiki,+ embedder: &Arc<dyn Embedder>,+ workspace_id: WorkspaceId,+ project_id: ProjectId,+ options: EmbedBackfillOptions,+) -> Result<EmbedBackfillCounts, EmbedBackfillError> {+ let provider = embedder.provider().to_string();+ let model = embedder.model().to_string();+ let dim = embedder.dim();++ let candidates = reader.decay_candidates(workspace_id, project_id).await?;+ let already: HashSet<_> = if options.reembed {+ HashSet::new()+ } else {+ reader+ .embedded_page_ids(+ workspace_id,+ project_id,+ provider.clone(),+ model.clone(),+ dim,+ )+ .await?+ .into_iter()+ .collect()+ };++ let mut counts = EmbedBackfillCounts::default();+ let mut pending = Vec::with_capacity(EMBEDDING_WRITE_BATCH);++ for cand in candidates {+ if already.contains(&cand.id) {+ counts.skipped += 1;+ continue;+ }+ if options.dry_run {+ counts.would_embed += 1;+ continue;+ }+ let md = match wiki.read_page(workspace_id, project_id, &cand.path) {+ Ok(md) => md,+ Err(e) => {+ warn!(path = %cand.path, error = %e, "embed: skip unreadable page");+ counts.failed += 1;+ continue;+ }+ };+ if md.body.trim().is_empty() {+ counts.skipped += 1;+ continue;+ }+ let vec = match embedder.embed_document(&md.body).await {+ Ok(vec) => vec,+ Err(e) => {+ warn!(path = %cand.path, error = %e, "embed: provider call failed");+ counts.failed += 1;+ continue;+ }+ };+ pending.push(EmbeddingWrite {+ page_id: cand.id,+ vector_bytes: f32_vec_to_bytes(&vec),+ provider: provider.clone(),+ model: model.clone(),+ dim,+ });+ if pending.len() >= EMBEDDING_WRITE_BATCH {+ flush_embedding_batch(writer, &mut pending, &mut counts).await;+ }+ }+ flush_embedding_batch(writer, &mut pending, &mut counts).await;++ Ok(counts)+}++async fn flush_embedding_batch(+ writer: &WriterHandle,+ pending: &mut Vec<EmbeddingWrite>,+ counts: &mut EmbedBackfillCounts,+) {+ if pending.is_empty() {+ return;+ }+ let batch = std::mem::replace(pending, Vec::with_capacity(EMBEDDING_WRITE_BATCH));+ let count = batch.len();+ if let Err(e) = writer.store_embeddings(batch).await {+ counts.failed += count;+ warn!(count, error = %e, "embed: store_embeddings failed");+ } else {+ counts.embedded += count;+ }+}diff --git a/crates/ai-memory-consolidate/src/lib.rs b/crates/ai-memory-consolidate/src/lib.rsindex 542a33b4..482beef3 100644--- a/crates/ai-memory-consolidate/src/lib.rs+++ b/crates/ai-memory-consolidate/src/lib.rs@@ -9,10 +9,12 @@//! M7b extends this to multi-page atomic fan-out.pub mod auto_improve;+pub mod auto_improve_schedule;pub mod auto_improve_telemetry;pub mod bootstrap;pub mod consolidator;pub mod curator;+pub mod embed;pub mod lint;pub mod projection;pub mod sweep;@@ -33,6 +35,10 @@ pub use auto_improve::{DEFAULT_AUTO_IMPROVE_REJECTION_CONTEXT_DAYS, default_auto_improve_eval_targets,run_auto_improve_review,};+pub use auto_improve_schedule::{+ ScheduledAutoImproveSettings, ScheduledAutoImproveTickOutcome,+ initialize_auto_improve_scheduler_scopes, run_auto_improve_scheduler_tick,+};pub use auto_improve_telemetry::{AutoImproveTelemetryFinding, AutoImproveTelemetryParams, AutoImproveTelemetryReport,AutoImproveTerminalRates, DEFAULT_AUTO_IMPROVE_TELEMETRY_SINCE_DAYS,@@ -52,6 +58,9 @@ pub use curator::{CuratorFinding, CuratorParams, CuratorReport, render_curator_report_markdown,run_curator_report,};+pub use embed::{+ EmbedBackfillCounts, EmbedBackfillError, EmbedBackfillOptions, run_embedding_backfill,+};pub use lint::{LintError, LintFinding, LintReport, run_lint};pub use sweep::{EvictedPage, SweepError, SweepReport, run_sweep};pub use types::{diff --git a/crates/ai-memory-consolidate/tests/embed_backfill.rs b/crates/ai-memory-consolidate/tests/embed_backfill.rsnew file mode 100644index 00000000..3bbda8ea--- /dev/null+++ b/crates/ai-memory-consolidate/tests/embed_backfill.rs@@ -0,0 +1,157 @@+//! Integration tests for [`run_embedding_backfill`]: candidate+//! scanning, skip/dry-run/reembed semantics, and batched writes, using+//! a temp-dir store + wiki and the deterministic `SyntheticEmbedder`.++use std::sync::Arc;++use ai_memory_consolidate::{EmbedBackfillCounts, EmbedBackfillOptions, run_embedding_backfill};+use ai_memory_core::{PagePath, ProjectId, Tier, WorkspaceId};+use ai_memory_llm::{Embedder, SyntheticEmbedder};+use ai_memory_store::Store;+use ai_memory_wiki::{Wiki, WritePageRequest};+use tempfile::TempDir;++struct Fixture {+ _tmp: TempDir,+ store: Store,+ wiki: Wiki,+ ws: WorkspaceId,+ proj: ProjectId,+ embedder: Arc<dyn Embedder>,+}++async fn fixture() -> Fixture {+ let tmp = TempDir::new().expect("tempdir");+ let store = Store::open(tmp.path()).expect("open store");+ let ws = store+ .writer+ .get_or_create_workspace("default")+ .await+ .expect("ws");+ let proj = store+ .writer+ .get_or_create_project(ws, "scratch", None)+ .await+ .expect("proj");+ let embedder: Arc<dyn Embedder> = Arc::new(SyntheticEmbedder::new(64));+ // No embedder attached: pages written here start without embedding+ // rows so the backfill has work to do.+ let wiki = Wiki::new(tmp.path(), store.writer.clone()).expect("wiki");+ Fixture {+ _tmp: tmp,+ store,+ wiki,+ ws,+ proj,+ embedder,+ }+}++impl Fixture {+ async fn write_page(&self, path: &str, body: &str) {+ self.wiki+ .write_page(WritePageRequest {+ workspace_id: self.ws,+ project_id: self.proj,+ path: PagePath::new(path.to_string()).expect("path"),+ frontmatter: serde_json::json!({"title": path}),+ body: body.to_string(),+ tier: Tier::Semantic,+ pinned: false,+ title: None,+ admission_ctx: None,+ author_id: None,+ actor: ai_memory_core::ActorContext::anonymous(),+ })+ .await+ .expect("write page");+ }++ async fn embedded_count(&self) -> usize {+ self.store+ .reader+ .embedded_page_ids(+ self.ws,+ self.proj,+ self.embedder.provider().to_string(),+ self.embedder.model().to_string(),+ self.embedder.dim(),+ )+ .await+ .expect("embedded page ids")+ .len()+ }++ async fn backfill(&self, reembed: bool, dry_run: bool) -> EmbedBackfillCounts {+ run_embedding_backfill(+ &self.store.reader,+ &self.store.writer,+ &self.wiki,+ &self.embedder,+ self.ws,+ self.proj,+ EmbedBackfillOptions { reembed, dry_run },+ )+ .await+ .expect("backfill")+ }+}++#[tokio::test]+async fn backfill_embeds_missing_and_skips_current_and_empty_pages() {+ let f = fixture().await;+ f.write_page("notes/a.md", "writer actor uses an mpsc channel")+ .await;+ f.write_page("notes/b.md", "hybrid retrieval combines fts and vectors")+ .await;+ f.write_page("notes/empty.md", " \n").await;++ let counts = f.backfill(false, false).await;+ assert_eq!(counts.embedded, 2);+ assert_eq!(counts.failed, 0);+ assert_eq!(+ counts.skipped, 1,+ "the whitespace-only page should be skipped"+ );+ assert_eq!(f.embedded_count().await, 2);++ let second = f.backfill(false, false).await;+ assert_eq!(second.embedded, 0, "second pass embeds nothing new");+ assert_eq!(+ second.skipped, 3,+ "already-embedded pages and the empty page are skipped"+ );+ assert_eq!(f.embedded_count().await, 2);+}++#[tokio::test]+async fn backfill_dry_run_counts_without_writing() {+ let f = fixture().await;+ f.write_page("notes/a.md", "dry run should not write embeddings")+ .await;++ let counts = f.backfill(false, true).await;+ assert_eq!(counts.would_embed, 1);+ assert_eq!(counts.embedded, 0);+ assert_eq!(+ f.embedded_count().await,+ 0,+ "dry run must not store embeddings"+ );+}++#[tokio::test]+async fn backfill_reembed_regenerates_existing_rows() {+ let f = fixture().await;+ f.write_page("notes/a.md", "reembed regenerates current rows")+ .await;+ assert_eq!(f.backfill(false, false).await.embedded, 1);++ let counts = f.backfill(true, false).await;+ assert_eq!(+ counts.embedded, 1,+ "reembed should regenerate the existing row"+ );+ assert_eq!(counts.skipped, 0);+ assert_eq!(f.embedded_count().await, 1);+}diff --git a/crates/ai-memory-core/src/actor.rs b/crates/ai-memory-core/src/actor.rsindex 5009715a..5455605a 100644--- a/crates/ai-memory-core/src/actor.rs+++ b/crates/ai-memory-core/src/actor.rs@@ -159,15 +159,6 @@ impl AuthzError {}impl AuthLevel {- /// `true` if this tier is allowed to perform root-only admin- /// operations (currently just `Root`). Centralises the- /// authorization check so handlers don't drift on what counts- /// as "root-only".- #[must_use]- pub fn is_root(self) -> bool {- matches!(self, AuthLevel::Root)- }-/// Check whether this auth tier can use `capability`.////// `multi_user_enabled` is the store-backed presence of any user row:diff --git a/crates/ai-memory-hooks/Cargo.toml b/crates/ai-memory-hooks/Cargo.tomlindex d91bf654..e0b2bfc0 100644--- a/crates/ai-memory-hooks/Cargo.toml+++ b/crates/ai-memory-hooks/Cargo.toml@@ -14,7 +14,6 @@ ai-memory-store.workspace = trueai-memory-wiki.workspace = trueai-memory-consolidate.workspace = trueanyhow.workspace = true-thiserror.workspace = trueserde.workspace = trueserde_json.workspace = true# Parse the per-event `?event=…` query of each spooled URL in `/hook/batch`,@@ -25,7 +24,6 @@ tracing.workspace = truejiff.workspace = trueuuid.workspace = trueaxum.workspace = true-regex.workspace = truesha2.workspace = true[dev-dependencies]diff --git a/crates/ai-memory-hooks/src/router.rs b/crates/ai-memory-hooks/src/router.rsindex 5b07dd24..9f5c6c39 100644--- a/crates/ai-memory-hooks/src/router.rs+++ b/crates/ai-memory-hooks/src/router.rs@@ -2324,11 +2324,17 @@ async fn consolidate_or_synth("{}: LLM consolidation written",checkpoint_label);- let _ = state.wiki.commit_all(&format!(- "{}(session {}): checkpoint",- checkpoint_label,- short_id(&session_id.to_string()),- ));+ let _ = state+ .wiki+ .commit_all(&format!(+ "{}(session {}): checkpoint",+ checkpoint_label,+ short_id(&session_id.to_string()),+ ))+ .map_err(|e| {+ tracing::warn!(error = %e, "{}: checkpoint auto-commit failed", checkpoint_label);+ e+ });return Ok(());}let observations = state.reader.observations_for_session(session_id).await?;@@ -2352,11 +2358,17 @@ async fn consolidate_or_synth(actor: ai_memory_core::ActorContext::anonymous(),}).await?;- let _ = state.wiki.commit_all(&format!(- "{}(session {}): checkpoint",- checkpoint_label,- short_id(&session_id.to_string()),- ));+ let _ = state+ .wiki+ .commit_all(&format!(+ "{}(session {}): checkpoint",+ checkpoint_label,+ short_id(&session_id.to_string()),+ ))+ .map_err(|e| {+ tracing::warn!(error = %e, "{}: checkpoint auto-commit failed", checkpoint_label);+ e+ });debug!(session = %session_id, "{}: rule-based checkpoint written", checkpoint_label);Ok(())}diff --git a/crates/ai-memory-llm/src/auth_file.rs b/crates/ai-memory-llm/src/auth_file.rsindex 71f5faea..ff3fc850 100644--- a/crates/ai-memory-llm/src/auth_file.rs+++ b/crates/ai-memory-llm/src/auth_file.rs@@ -71,6 +71,15 @@ wherewrite_auth_file(path, &root)}+/// Atomic write for credential files, mirroring the canonical+/// `ai_memory_wiki::write_atomic` dance (write tmp → fsync → rename)+/// with two deliberate differences: a predictable `<name>.json.tmp`+/// sibling name, and chmod 0600 on the tempfile *before* any bytes land+/// so auth material is never group/world-readable, even transiently.+///+/// `ai-memory-llm` deliberately does not depend on `ai-memory-wiki`+/// (git2/notify are too heavy to pull in for a write helper), so this+/// stays a copy — keep it in step with the canonical implementation.fn write_auth_file(path: &Path, value: &serde_json::Value) -> LlmResult<()> {use std::io::Write as _;if let Some(parent) = path.parent() {@@ -110,3 +119,162 @@ pub(crate) fn now_ms() -> u64 {.try_into().unwrap_or(u64::MAX)}++#[cfg(test)]+mod tests {+ use serde::{Deserialize, Serialize};++ use super::*;++ /// Mirrors the entry shape stored by `openai_oauth` / `oidc` callers.+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+ struct TestEntry {+ #[serde(rename = "type")]+ kind: String,+ access: String,+ refresh: String,+ expires: u64,+ }++ fn sample_entry() -> TestEntry {+ TestEntry {+ kind: "oauth".into(),+ access: "access-x".into(),+ refresh: "refresh-y".into(),+ expires: 1_700_000_000_000,+ }+ }++ #[test]+ fn round_trip_preserves_entry_fields() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("auth.json");+ save_entry(&path, "test", Some(sample_entry())).unwrap();+ let loaded = load_entry::<TestEntry>(&path, "test").unwrap();+ assert_eq!(loaded, Some(sample_entry()));+ }++ #[test]+ fn load_missing_file_returns_none() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("auth.json");+ let loaded = load_entry::<TestEntry>(&path, "test").unwrap();+ assert!(loaded.is_none());+ }++ #[test]+ fn load_malformed_json_returns_auth_error() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("auth.json");+ std::fs::write(&path, b"not json {{{").unwrap();+ let err = load_entry::<TestEntry>(&path, "test").unwrap_err();+ assert!(+ matches!(err, LlmError::Auth(ref msg) if msg.contains("parse auth file")),+ "unexpected error: {err}"+ );+ }++ #[test]+ fn load_returns_none_when_key_is_absent() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("auth.json");+ save_entry(&path, "other", Some(sample_entry())).unwrap();+ let loaded = load_entry::<TestEntry>(&path, "test").unwrap();+ assert!(loaded.is_none());+ }++ #[test]+ fn load_returns_none_for_non_oauth_entry_type() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("auth.json");+ std::fs::write(+ &path,+ serde_json::to_vec_pretty(&serde_json::json!({+ "test": { "type": "api_key", "access": "a", "refresh": "b", "expires": 1 }+ }))+ .unwrap(),+ )+ .unwrap();+ let loaded = load_entry::<TestEntry>(&path, "test").unwrap();+ assert!(loaded.is_none(), "non-oauth entries must be ignored");+ }++ #[test]+ fn load_errors_when_oauth_entry_has_wrong_shape() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("auth.json");+ std::fs::write(+ &path,+ serde_json::to_vec_pretty(&serde_json::json!({+ "test": { "type": "oauth", "access": "a" }+ }))+ .unwrap(),+ )+ .unwrap();+ let err = load_entry::<TestEntry>(&path, "test").unwrap_err();+ assert!(+ matches!(err, LlmError::Auth(ref msg) if msg.contains("parse test auth entry")),+ "unexpected error: {err}"+ );+ }++ #[test]+ fn save_preserves_sibling_keys() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("auth.json");+ save_entry(&path, "other", Some(sample_entry())).unwrap();+ save_entry(&path, "test", Some(sample_entry())).unwrap();+ let loaded_other = load_entry::<TestEntry>(&path, "other").unwrap();+ let loaded_test = load_entry::<TestEntry>(&path, "test").unwrap();+ assert_eq!(loaded_other, Some(sample_entry()));+ assert_eq!(loaded_test, Some(sample_entry()));+ }++ #[test]+ fn save_rejects_non_object_auth_file() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("auth.json");+ std::fs::write(&path, b"[1, 2, 3]").unwrap();+ let err = save_entry(&path, "test", Some(sample_entry())).unwrap_err();+ assert!(+ matches!(err, LlmError::Auth(ref msg) if msg.contains("must contain a JSON object")),+ "unexpected error: {err}"+ );+ }++ #[test]+ fn removing_last_entry_deletes_file() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("auth.json");+ save_entry(&path, "test", Some(sample_entry())).unwrap();+ assert!(path.exists());+ save_entry::<TestEntry>(&path, "test", None).unwrap();+ assert!(!path.exists(), "last-entry removal deletes the auth file");+ }++ #[cfg(unix)]+ #[test]+ fn saved_file_has_owner_only_permissions() {+ use std::os::unix::fs::PermissionsExt as _;+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("auth.json");+ save_entry(&path, "test", Some(sample_entry())).unwrap();+ let mode = std::fs::metadata(&path).unwrap().permissions().mode() & 0o777;+ assert_eq!(mode, 0o600, "auth file must be owner-read/write only");+ }++ #[test]+ fn successful_save_leaves_no_tmp_file() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("auth.json");+ save_entry(&path, "test", Some(sample_entry())).unwrap();+ let tmp = path.with_extension("json.tmp");+ assert!(!tmp.exists(), "leftover tmp file: {}", tmp.display());+ }++ #[test]+ fn now_ms_returns_plausible_epoch_millis() {+ let ms = now_ms();+ assert!(ms > 1_000_000_000_000, "expected epoch millis, got {ms}");+ }+}diff --git a/crates/ai-memory-llm/src/lib.rs b/crates/ai-memory-llm/src/lib.rsindex b5f20645..22f8b2e8 100644--- a/crates/ai-memory-llm/src/lib.rs+++ b/crates/ai-memory-llm/src/lib.rs@@ -45,6 +45,7 @@ pub mod types;mod auth_file;mod response;+mod stored_token;mod text;pub use anthropic::AnthropicProvider;@@ -66,15 +67,18 @@ pub use health::{ProviderHealth, ProviderHealthSnapshot, ProviderHealthStatus, ProviderRoleHealthSnapshot,};pub use oidc::{- DeviceAuthorizationResponse, OIDC_DEFAULT_SCOPE, OidcDiscovery, OidcToken, OidcTokenResponse,- PollOutcome, discover, poll_token_once, refresh_access_token, request_device_code,+ DeviceAuthorizationResponse, OIDC_DEFAULT_SCOPE, OidcDiscovery, OidcExtras, OidcToken,+ OidcTokenResponse, PollOutcome, discover, poll_token_once, refresh_access_token,+ request_device_code,};pub use openai::OpenAiProvider;pub use openai_compat::OpenAiCompatProvider;pub use openai_oauth::{CODEX_CLIENT_ID, CODEX_RESPONSES_URL, OPENAI_OAUTH_AUTH_URL, OPENAI_OAUTH_ISSUER,- OPENAI_OAUTH_TOKEN_URL, OpenAiOAuthProvider, OpenAiOAuthToken, OpenAiOAuthTokenResponse,+ OPENAI_OAUTH_TOKEN_URL, OpenAiExtras, OpenAiOAuthProvider, OpenAiOAuthToken,+ OpenAiOAuthTokenResponse,};pub use opencode::{OPENCODE_DEFAULT_MODEL, OPENCODE_ZEN_BASE_URL, OpenCodeProvider};pub use provider::{LlmProvider, complete_structured};+pub use stored_token::StoredOAuthToken;pub use types::{ChatMessage, ChatRequest, ChatResponse, Role, Usage};diff --git a/crates/ai-memory-llm/src/oidc.rs b/crates/ai-memory-llm/src/oidc.rsindex 25638ff4..a46543a1 100644--- a/crates/ai-memory-llm/src/oidc.rs+++ b/crates/ai-memory-llm/src/oidc.rs@@ -6,22 +6,21 @@//! **per-developer** JWT instead of a shared static token. The stored token is//! refreshed on demand from the hook fast-path.//!-//! Storage mirrors [`crate::openai_oauth`]: the same shared `auth.json` file,-//! under the `"oidc"` key, with `type: "oauth"` so the shared loader accepts it.-//! The server side is unchanged — its auth layer already validates a Keycloak-//! JWT on the hook routes (requiring the `mcp:read` realm role).+//! Storage uses the shared [`crate::stored_token`] core: the same shared+//! `auth.json` file as [`crate::openai_oauth`], under the `"oidc"` key, with+//! `type: "oauth"` so the shared loader accepts it. The server side is+//! unchanged — its auth layer already validates a Keycloak JWT on the hook+//! routes (requiring the `mcp:read` realm role).use std::path::Path;use secrecy::{ExposeSecret as _, SecretString};use serde::{Deserialize, Serialize};-use crate::auth_file::{load_entry, now_ms, save_entry};+use crate::auth_file::now_ms;use crate::error::{LlmError, LlmResult};use crate::response::{provider_error_body, response_json_limited};--/// Refresh once the access token is within this margin of expiry.-const REFRESH_MARGIN_MS: u64 = 60_000;+use crate::stored_token::{StoredOAuthToken, refresh_grant};/// Default device-flow scopes: `openid` (ID token), `profile` (so the access/// token carries `preferred_username` → `X-Memory-Actor-User` on the server),@@ -29,15 +28,9 @@ const REFRESH_MARGIN_MS: u64 = 60_000;/// server requires is a **realm role** on the user, not a scope.pub const OIDC_DEFAULT_SCOPE: &str = "openid profile offline_access";-/// Stored OIDC device-grant token.-#[derive(Clone)]-pub struct OidcToken {- /// Access token sent as the bearer to the ai-memory server.- pub access: SecretString,- /// Refresh token used to mint a new access token.- pub refresh: SecretString,- /// Expiry in milliseconds since the Unix epoch.- pub expires_at_ms: u64,+/// OIDC-specific fields persisted next to the shared token triple.+#[derive(Clone, Debug, Serialize, Deserialize)]+pub struct OidcExtras {/// OIDC issuer this token was minted by (kept for `status` display).pub issuer: String,/// Public client id used for the device + refresh grants.@@ -46,18 +39,8 @@ pub struct OidcToken {pub token_endpoint: String,}-impl std::fmt::Debug for OidcToken {- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {- f.debug_struct("OidcToken")- .field("access", &"<redacted>")- .field("refresh", &"<redacted>")- .field("expires_at_ms", &self.expires_at_ms)- .field("issuer", &self.issuer)- .field("client_id", &self.client_id)- .field("token_endpoint", &self.token_endpoint)- .finish()- }-}+/// Stored OIDC device-grant token.+pub type OidcToken = StoredOAuthToken<OidcExtras>;impl OidcToken {/// Build a token from a token-endpoint response. `previous_refresh` is used@@ -90,37 +73,20 @@ impl OidcToken {refresh: SecretString::from(refresh),expires_at_ms: now_ms().saturating_add(resp.expires_in.unwrap_or(300).saturating_mul(1000)),- issuer: issuer.into(),- client_id: client_id.into(),- token_endpoint: token_endpoint.into(),+ extra: OidcExtras {+ issuer: issuer.into(),+ client_id: client_id.into(),+ token_endpoint: token_endpoint.into(),+ },})}- /// True when the access token is expired or within the refresh margin.- #[must_use]- pub fn needs_refresh(&self) -> bool {- now_ms().saturating_add(REFRESH_MARGIN_MS) >= self.expires_at_ms- }-/// Load the OIDC token from the shared `auth.json` file.////// # Errors/// Returns [`LlmError::Auth`] when the file exists but cannot be read/parsed.pub fn load(path: &Path) -> LlmResult<Option<Self>> {- let Some(entry) = load_entry::<OidcEntry>(path, "oidc")? else {- return Ok(None);- };- if entry.kind != "oauth" {- return Ok(None);- }- Ok(Some(Self {- access: SecretString::from(entry.access),- refresh: SecretString::from(entry.refresh),- expires_at_ms: entry.expires,- issuer: entry.issuer,- client_id: entry.client_id,- token_endpoint: entry.token_endpoint,- }))+ Self::load_key(path, "oidc")}/// Save the token into the shared `auth.json` file, preserving other keys.@@ -128,16 +94,7 @@ impl OidcToken {/// # Errors/// Returns [`LlmError::Auth`] when the file cannot be written.pub fn save(&self, path: &Path) -> LlmResult<()> {- let entry = OidcEntry {- kind: "oauth".into(),- access: self.access.expose_secret().to_string(),- refresh: self.refresh.expose_secret().to_string(),- expires: self.expires_at_ms,- issuer: self.issuer.clone(),- client_id: self.client_id.clone(),- token_endpoint: self.token_endpoint.clone(),- };- save_entry(path, "oidc", Some(entry))+ self.save_key(path, "oidc")}/// Remove the OIDC entry from the shared `auth.json` file.@@ -145,22 +102,10 @@ impl OidcToken {/// # Errors/// Returns [`LlmError::Auth`] when the file cannot be updated.pub fn remove(path: &Path) -> LlmResult<()> {- save_entry::<OidcEntry>(path, "oidc", None)+ Self::remove_key(path, "oidc")}}-#[derive(Debug, Serialize, Deserialize)]-struct OidcEntry {- #[serde(rename = "type")]- kind: String,- access: String,- refresh: String,- expires: u64,- issuer: String,- client_id: String,- token_endpoint: String,-}-/// The two endpoints we need from `<issuer>/.well-known/openid-configuration`.#[derive(Debug, Deserialize)]pub struct OidcDiscovery {@@ -320,29 +265,20 @@ pub async fn refresh_access_token(client: &reqwest::Client,token: &OidcToken,) -> LlmResult<OidcToken> {- let resp = client- .post(&token.token_endpoint)- .form(&[- ("grant_type", "refresh_token"),- ("refresh_token", token.refresh.expose_secret()),- ("client_id", token.client_id.as_str()),- ])- .send()- .await- .map_err(LlmError::from)?;- let status = resp.status();- if !status.is_success() {- let body = provider_error_body(resp).await;- return Err(LlmError::Auth(format!(- "OIDC refresh failed ({status}): {body}. Run `ai-memory auth login oidc-device` again."- )));- }- let token_response = response_json_limited::<OidcTokenResponse>(resp).await?;+ let token_response = refresh_grant::<OidcTokenResponse>(+ client,+ &token.extra.token_endpoint,+ token.refresh.expose_secret(),+ &token.extra.client_id,+ "OIDC refresh failed",+ "Run `ai-memory auth login oidc-device` again.",+ )+ .await?;OidcToken::from_token_response(&token_response,- token.issuer.clone(),- token.client_id.clone(),- token.token_endpoint.clone(),+ token.extra.issuer.clone(),+ token.extra.client_id.clone(),+ token.extra.token_endpoint.clone(),Some(token.refresh.expose_secret()),)}@@ -356,10 +292,13 @@ mod tests {access: SecretString::from("access-x".to_string()),refresh: SecretString::from("refresh-y".to_string()),expires_at_ms: now_ms() + 3_600_000,- issuer: "https://kc.example.com/realms/ai-memory".to_string(),- client_id: "ai-memory-dev".to_string(),- token_endpoint: "https://kc.example.com/realms/ai-memory/protocol/openid-connect/token"- .to_string(),+ extra: OidcExtras {+ issuer: "https://kc.example.com/realms/ai-memory".to_string(),+ client_id: "ai-memory-dev".to_string(),+ token_endpoint:+ "https://kc.example.com/realms/ai-memory/protocol/openid-connect/token"+ .to_string(),+ },}}@@ -372,9 +311,9 @@ mod tests {let loaded = OidcToken::load(&path).unwrap().unwrap();assert_eq!(loaded.access.expose_secret(), "access-x");assert_eq!(loaded.refresh.expose_secret(), "refresh-y");- assert_eq!(loaded.client_id, "ai-memory-dev");+ assert_eq!(loaded.extra.client_id, "ai-memory-dev");assert_eq!(- loaded.token_endpoint,+ loaded.extra.token_endpoint,"https://kc.example.com/realms/ai-memory/protocol/openid-connect/token");}diff --git a/crates/ai-memory-llm/src/openai_oauth.rs b/crates/ai-memory-llm/src/openai_oauth.rsindex f86fee63..bd9c136c 100644--- a/crates/ai-memory-llm/src/openai_oauth.rs+++ b/crates/ai-memory-llm/src/openai_oauth.rs@@ -15,11 +15,12 @@ use serde::{Deserialize, Serialize};use tokio::sync::Mutex;use tracing::{debug, info};-use crate::auth_file::{load_entry, now_ms, save_entry};+use crate::auth_file::now_ms;use crate::error::{LlmError, LlmResult};use crate::openai::{STRUCTURED_OUTPUT_SCHEMA_NAME, enforce_strict_object_schemas};use crate::provider::LlmProvider;use crate::response::{provider_error_body, response_json_limited, response_text_limited};+use crate::stored_token::{StoredOAuthToken, refresh_grant};use crate::text::truncate_with_ellipsis;use crate::types::{ChatRequest, ChatResponse, Role, Usage};@@ -41,8 +42,6 @@ pub const CODEX_CLIENT_ID: &str = "app_EMoamEEZ73f0CkXaXp7hrann";/// Scopes used by Codex/OpenCode for ChatGPT sign-in.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@@ -57,29 +56,16 @@ const SSE_TERMINAL_ERROR_STATUS: u16 = 502;/// bodies — same defensive cap, same reasoning.const SSE_ERROR_BODY_TRIM: usize = 1024;-/// Stored OpenAI OAuth token.-#[derive(Clone)]-pub struct OpenAiOAuthToken {- /// Access token sent as the bearer token.- pub access: SecretString,- /// Refresh token used to mint a new access token.- pub refresh: SecretString,- /// Expiry in milliseconds since Unix epoch.- pub expires_at_ms: u64,+/// OpenAI-specific claim persisted next to the shared token triple.+#[derive(Clone, Debug, Serialize, Deserialize)]+pub struct OpenAiExtras {/// ChatGPT account/workspace id, when present in the token claims.+ #[serde(rename = "accountId", skip_serializing_if = "Option::is_none")]pub account_id: Option<String>,}-impl std::fmt::Debug for OpenAiOAuthToken {- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {- f.debug_struct("OpenAiOAuthToken")- .field("access", &"<redacted>")- .field("refresh", &"<redacted>")- .field("expires_at_ms", &self.expires_at_ms)- .field("account_id", &self.account_id)- .finish()- }-}+/// Stored OpenAI OAuth token.+pub type OpenAiOAuthToken = StoredOAuthToken<OpenAiExtras>;impl OpenAiOAuthToken {/// Build a token from an OAuth token response.@@ -100,34 +86,17 @@ impl OpenAiOAuthToken {access: SecretString::from(access),refresh: SecretString::from(refresh.into()),expires_at_ms: now_ms().saturating_add(expires_in_secs.saturating_mul(1000)),- account_id,+ extra: OpenAiExtras { account_id },}}- /// True when the access token is expired or within the refresh margin.- #[must_use]- pub fn needs_refresh(&self) -> bool {- now_ms().saturating_add(REFRESH_MARGIN_MS) >= self.expires_at_ms- }-/// Load the OpenAI OAuth token from a shared token file.////// # Errors/// Returns [`LlmError::Auth`] when the file exists but cannot be read or/// parsed.pub fn load(path: &Path) -> LlmResult<Option<Self>> {- let Some(entry) = load_entry::<OAuthEntry>(path, "openai")? else {- return Ok(None);- };- if entry.kind != "oauth" {- return Ok(None);- }- Ok(Some(Self {- access: SecretString::from(entry.access),- refresh: SecretString::from(entry.refresh),- expires_at_ms: entry.expires,- account_id: entry.account_id,- }))+ Self::load_key(path, "openai")}/// Save the token into the shared token file, preserving unknown keys.@@ -135,14 +104,7 @@ impl OpenAiOAuthToken {/// # Errors/// Returns [`LlmError::Auth`] when the file cannot be written.pub fn save(&self, path: &Path) -> LlmResult<()> {- let entry = OAuthEntry {- kind: "oauth".into(),- access: self.access.expose_secret().to_string(),- refresh: self.refresh.expose_secret().to_string(),- expires: self.expires_at_ms,- account_id: self.account_id.clone(),- };- save_entry(path, "openai", Some(entry))+ self.save_key(path, "openai")}/// Remove the OpenAI entry from the shared token file.@@ -150,7 +112,7 @@ impl OpenAiOAuthToken {/// # Errors/// Returns [`LlmError::Auth`] when the file cannot be updated.pub fn remove(path: &Path) -> LlmResult<()> {- save_entry::<OAuthEntry>(path, "openai", None)+ Self::remove_key(path, "openai")}}@@ -236,7 +198,7 @@ impl OpenAiOAuthProvider {.header("originator", "codex_cli_rs").header("session_id", uuid::Uuid::new_v4().to_string()).json(body);- if let Some(account_id) = token.account_id.as_deref() {+ if let Some(account_id) = token.extra.account_id.as_deref() {request = request.header("chatgpt-account-id", account_id);}let resp = request.send().await.map_err(LlmError::from)?;@@ -298,25 +260,15 @@ async fn refresh_access_token(client: &reqwest::Client,current: &OpenAiOAuthToken,) -> LlmResult<OpenAiOAuthToken> {- let resp = client- .post(OPENAI_OAUTH_TOKEN_URL)- .header("content-type", "application/x-www-form-urlencoded")- .form(&[- ("grant_type", "refresh_token"),- ("refresh_token", current.refresh.expose_secret()),- ("client_id", CODEX_CLIENT_ID),- ])- .send()- .await- .map_err(LlmError::from)?;- let status = resp.status();- if !status.is_success() {- let body = provider_error_body(resp).await;- return Err(LlmError::Auth(format!(- "openai-oauth refresh failed ({status}): {body}. Run `ai-memory auth login openai-oauth` again."- )));- }- let token_response = response_json_limited::<OpenAiOAuthTokenResponse>(resp).await?;+ let token_response = refresh_grant::<OpenAiOAuthTokenResponse>(+ client,+ OPENAI_OAUTH_TOKEN_URL,+ current.refresh.expose_secret(),+ CODEX_CLIENT_ID,+ "openai-oauth refresh failed",+ "Run `ai-memory auth login openai-oauth` again.",+ )+ .await?;Ok(OpenAiOAuthToken::from_token_response(token_response.access_token,token_response@@ -324,7 +276,7 @@ async fn refresh_access_token(.unwrap_or_else(|| current.refresh.expose_secret().to_string()),token_response.expires_in.unwrap_or(3600),token_response.id_token.as_deref(),- current.account_id.clone(),+ current.extra.account_id.clone(),))}@@ -578,17 +530,6 @@ fn extract_output_text(response: &CodexResponsesResponse) -> Option<String> {.map(ToOwned::to_owned)}-#[derive(Debug, Serialize, Deserialize)]-struct OAuthEntry {- #[serde(rename = "type")]- kind: String,- access: String,- refresh: String,- expires: u64,- #[serde(rename = "accountId", skip_serializing_if = "Option::is_none")]- account_id: Option<String>,-}-fn extract_account_id_from_jwt(token: &str) -> Option<String> {let payload = token.split('.').nth(1)?;let bytes = base64::engine::general_purpose::URL_SAFE_NO_PAD@@ -638,7 +579,9 @@ mod tests {access: SecretString::from("access"),refresh: SecretString::from("refresh"),expires_at_ms: 1234,- account_id: Some("acct".into()),+ extra: OpenAiExtras {+ account_id: Some("acct".into()),+ },};token.save(&path).unwrap();let value =@@ -656,13 +599,17 @@ mod tests {access: SecretString::from("access"),refresh: SecretString::from("refresh"),expires_at_ms: 1234,- account_id: None,+ extra: OpenAiExtras { account_id: None },};token.save(&path).unwrap();let loaded = OpenAiOAuthToken::load(&path).unwrap().unwrap();assert_eq!(loaded.access.expose_secret(), "access");assert_eq!(loaded.refresh.expose_secret(), "refresh");assert_eq!(loaded.expires_at_ms, 1234);+ // On-disk format: `accountId` is omitted entirely when absent.+ let value =+ serde_json::from_slice::<serde_json::Value>(&std::fs::read(&path).unwrap()).unwrap();+ assert!(value["openai"].get("accountId").is_none());}#[test]@@ -711,7 +658,7 @@ mod tests {access: SecretString::from("access"),refresh: SecretString::from("refresh"),expires_at_ms: 1234,- account_id: None,+ extra: OpenAiExtras { account_id: None },};token.save(&path).unwrap();let mode = std::fs::metadata(&path).unwrap().permissions().mode();diff --git a/crates/ai-memory-llm/src/opencode.rs b/crates/ai-memory-llm/src/opencode.rsindex 4d1cb5c5..301241aa 100644--- a/crates/ai-memory-llm/src/opencode.rs+++ b/crates/ai-memory-llm/src/opencode.rs@@ -60,3 +60,21 @@ impl LlmProvider for OpenCodeProvider {self.inner.complete_structured_raw(request, schema).await}}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn provider_reports_opencode_name_and_configured_model() {+ let provider = OpenCodeProvider::new(SecretString::from("sk-test"), "model-x").unwrap();+ assert_eq!(provider.name(), "opencode");+ assert_eq!(provider.model(), "model-x");+ }++ #[test]+ fn public_constants_point_at_zen_base_url() {+ assert_eq!(OPENCODE_ZEN_BASE_URL, "https://opencode.ai/zen/go/v1");+ assert!(!OPENCODE_DEFAULT_MODEL.is_empty());+ }+}diff --git a/crates/ai-memory-llm/src/stored_token.rs b/crates/ai-memory-llm/src/stored_token.rsnew file mode 100644index 00000000..946cb150--- /dev/null+++ b/crates/ai-memory-llm/src/stored_token.rs@@ -0,0 +1,227 @@+//! Shared core for OAuth tokens persisted in the shared `auth.json` file.+//!+//! Both token-backed providers ([`crate::openai_oauth`] and [`crate::oidc`])+//! store the same access/refresh/expiry triple under their own top-level key,+//! differing only in provider-specific extras. This module holds the shared+//! triple, the storage round-trip, and the refresh-grant POST flow so each+//! provider only defines its extras and its response mapping.++use std::path::Path;++use secrecy::{ExposeSecret as _, SecretString};+use serde::{Deserialize, Serialize, de::DeserializeOwned};++use crate::auth_file::{load_entry, now_ms, save_entry};+use crate::error::{LlmError, LlmResult};+use crate::response::{provider_error_body, response_json_limited};++/// Refresh once the access token is within this margin of expiry.+const REFRESH_MARGIN_MS: u64 = 60_000;++/// Access/refresh/expiry triple shared by every stored OAuth token, with+/// provider-specific claims in `extra` (persisted next to the triple under+/// the same `auth.json` entry).+#[derive(Clone)]+pub struct StoredOAuthToken<E> {+ /// Access token sent as the bearer token.+ pub access: SecretString,+ /// Refresh token used to mint a new access token.+ pub refresh: SecretString,+ /// Expiry in milliseconds since the Unix epoch.+ pub expires_at_ms: u64,+ /// Provider-specific fields persisted next to the core triple.+ pub extra: E,+}++impl<E: std::fmt::Debug> std::fmt::Debug for StoredOAuthToken<E> {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ f.debug_struct("StoredOAuthToken")+ .field("access", &"<redacted>")+ .field("refresh", &"<redacted>")+ .field("expires_at_ms", &self.expires_at_ms)+ .field("extra", &self.extra)+ .finish()+ }+}++/// On-disk entry shape: the core triple plus the provider extras, flattened+/// so the extra keys sit at the top level of the entry object — the persisted+/// format predates this split and must not change.+#[derive(Debug, Serialize, Deserialize)]+struct OAuthEntry<E> {+ #[serde(rename = "type")]+ kind: String,+ access: String,+ refresh: String,+ expires: u64,+ #[serde(flatten)]+ extra: E,+}++impl<E> StoredOAuthToken<E> {+ /// True when the access token is expired or within the refresh margin.+ #[must_use]+ pub fn needs_refresh(&self) -> bool {+ now_ms().saturating_add(REFRESH_MARGIN_MS) >= self.expires_at_ms+ }++ /// Load the token stored under `key` in the shared `auth.json` file.+ pub(crate) fn load_key(path: &Path, key: &str) -> LlmResult<Option<Self>>+ where+ E: DeserializeOwned,+ {+ let Some(entry) = load_entry::<OAuthEntry<E>>(path, key)? else {+ return Ok(None);+ };+ Ok(Some(Self {+ access: SecretString::from(entry.access),+ refresh: SecretString::from(entry.refresh),+ expires_at_ms: entry.expires,+ extra: entry.extra,+ }))+ }++ /// Save the token under `key`, preserving other keys in the file.+ pub(crate) fn save_key(&self, path: &Path, key: &str) -> LlmResult<()>+ where+ E: Serialize,+ {+ let entry = OAuthEntry {+ kind: "oauth".into(),+ access: self.access.expose_secret().to_string(),+ refresh: self.refresh.expose_secret().to_string(),+ expires: self.expires_at_ms,+ extra: &self.extra,+ };+ save_entry(path, key, Some(entry))+ }++ /// Remove the entry stored under `key` from the shared `auth.json` file.+ pub(crate) fn remove_key(path: &Path, key: &str) -> LlmResult<()>+ where+ E: Serialize,+ {+ save_entry::<OAuthEntry<E>>(path, key, None)+ }+}++/// POST a `grant_type=refresh_token` form to `token_endpoint` and parse the+/// success body. Shared by every stored-token provider; each maps the parsed+/// response onto its own token shape afterwards.+///+/// # Errors+/// Returns [`LlmError::Auth`] when the grant is rejected; `failure_label` and+/// `relogin_hint` keep the message provider-specific (e.g. "OIDC refresh+/// failed" and the matching re-login hint).+pub(crate) async fn refresh_grant<T>(+ client: &reqwest::Client,+ token_endpoint: &str,+ refresh_token: &str,+ client_id: &str,+ failure_label: &str,+ relogin_hint: &str,+) -> LlmResult<T>+where+ T: DeserializeOwned,+{+ let resp = client+ .post(token_endpoint)+ .form(&[+ ("grant_type", "refresh_token"),+ ("refresh_token", refresh_token),+ ("client_id", client_id),+ ])+ .send()+ .await+ .map_err(LlmError::from)?;+ let status = resp.status();+ if !status.is_success() {+ let body = provider_error_body(resp).await;+ return Err(LlmError::Auth(format!(+ "{failure_label} ({status}): {body}. {relogin_hint}"+ )));+ }+ response_json_limited::<T>(resp).await+}++#[cfg(test)]+mod tests {+ use serde::Deserialize;++ use super::*;++ #[derive(Debug, Deserialize)]+ struct TestResponse {+ access_token: String,+ }++ /// Serve a single fixed HTTP response on a loopback port, returning the+ /// URL to POST to (mirrors `auth_bearer.rs`'s refresh-flow tests).+ async fn serve_once(status: &'static str, body: &'static str) -> String {+ use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};++ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();+ let addr = listener.local_addr().unwrap();+ tokio::spawn(async move {+ let Ok((mut stream, _)) = listener.accept().await else {+ return;+ };+ let mut buf = [0_u8; 4096];+ let _ = stream.read(&mut buf).await;+ let response = format!(+ "HTTP/1.1 {status}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",+ body.len()+ );+ let _ = stream.write_all(response.as_bytes()).await;+ });+ format!("http://{addr}/token")+ }++ #[tokio::test]+ async fn refresh_grant_parses_success_body() {+ let endpoint = serve_once("200 OK", r#"{"access_token":"fresh-access"}"#).await;+ let client = reqwest::Client::new();++ let parsed = refresh_grant::<TestResponse>(+ &client,+ &endpoint,+ "refresh-x",+ "client-y",+ "test refresh failed",+ "Run `test login` again.",+ )+ .await+ .unwrap();++ assert_eq!(parsed.access_token, "fresh-access");+ }++ #[tokio::test]+ async fn refresh_grant_surfaces_status_body_and_relogin_hint() {+ let endpoint = serve_once("401 Unauthorized", r#"{"error":"invalid_grant"}"#).await;+ let client = reqwest::Client::new();++ let err = refresh_grant::<TestResponse>(+ &client,+ &endpoint,+ "refresh-x",+ "client-y",+ "test refresh failed",+ "Run `test login` again.",+ )+ .await+ .expect_err("rejected grant must fail");++ match err {+ LlmError::Auth(msg) => {+ assert!(+ msg.contains("test refresh failed (401 Unauthorized)"),+ "label+status: {msg}"+ );+ assert!(msg.contains("invalid_grant"), "upstream body: {msg}");+ assert!(msg.contains("Run `test login` again."), "hint: {msg}");+ }+ other => panic!("expected auth error, got {other:?}"),+ }+ }+}diff --git a/crates/ai-memory-llm/src/types.rs b/crates/ai-memory-llm/src/types.rsindex e6576453..58619fdd 100644--- a/crates/ai-memory-llm/src/types.rs+++ b/crates/ai-memory-llm/src/types.rs@@ -66,13 +66,6 @@ impl ChatRequest {}}- /// Attach a system prompt to the request.- #[must_use]- pub fn with_system(mut self, system: impl Into<String>) -> Self {- self.system = Some(system.into());- self- }-/// Override the max-tokens cap.#[must_use]pub const fn with_max_tokens(mut self, n: u32) -> Self {diff --git a/crates/ai-memory-mcp/Cargo.toml b/crates/ai-memory-mcp/Cargo.tomlindex e206f350..1717fde2 100644--- a/crates/ai-memory-mcp/Cargo.toml+++ b/crates/ai-memory-mcp/Cargo.toml@@ -28,6 +28,11 @@ rmcp.workspace = trueschemars.workspace = trueaxum.workspace = truehttp.workspace = true+# HTTP auth middleware (bearer/basic/cookie): constant-time token+# compare, Basic-header decoding, random token generation.+subtle.workspace = true+base64.workspace = true+getrandom.workspace = true# Time gap calculation for `memory_explore` (parse stored ISO-8601# timestamps + delta against now). The rest of the crate doesn't# need a clock dep.@@ -41,6 +46,7 @@ ai-memory-wiki.workspace = trueasync-trait.workspace = trueflate2.workspace = truetar.workspace = true+tempfile.workspace = truetower.workspace = true[lints]diff --git a/crates/ai-memory-mcp/src/admin.rs b/crates/ai-memory-mcp/src/admin.rsindex 9256d87f..bdbc8616 100644--- a/crates/ai-memory-mcp/src/admin.rs+++ b/crates/ai-memory-mcp/src/admin.rs@@ -9,6 +9,7 @@//! - `POST /admin/curator` — dry-run or stage a rule-based curator report.//! - `GET /admin/status` — lifetime counts + server data-dir info.//! - `GET /admin/projects` — authoritative `(workspace, project)` list.+//! - `GET /admin/open-sessions` — open (not yet ended) sessions for one scope + agent.//! - `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.@@ -42,19 +43,20 @@ use std::pin::Pin;use ai_memory_consolidate::{AutoImproveReviewConfig, AutoImproveTelemetryParams, AutoImproveTelemetryReport, Bootstrap,- BootstrapConfig, BootstrapOutcome, BootstrapSource, CuratorParams, CuratorReport, SourceCounts,- prune_sources_to_budget, render_auto_improve_telemetry_report_markdown,- render_curator_report_markdown, run_auto_improve_review, run_auto_improve_telemetry_report,- run_curator_report, run_lint, run_sweep,+ BootstrapConfig, BootstrapOutcome, BootstrapSource, CuratorParams, CuratorReport,+ EmbedBackfillCounts, EmbedBackfillOptions, SourceCounts, prune_sources_to_budget,+ render_auto_improve_telemetry_report_markdown, render_curator_report_markdown,+ run_auto_improve_review, run_auto_improve_telemetry_report, run_curator_report,+ run_embedding_backfill, run_lint, run_sweep,};use ai_memory_core::{- ActiveProject, AutoImproveProposalId, Capability, DEFAULT_PROJECT_NAME, DEFAULT_WORKSPACE_NAME,- PagePath, ProjectId, SessionId, Tier, WorkspaceId,+ ActiveProject, AgentKind, AutoImproveProposalId, Capability, DEFAULT_PROJECT_NAME,+ DEFAULT_WORKSPACE_NAME, PagePath, ProjectId, SessionId, Tier, WorkspaceId,};use ai_memory_llm::{Embedder, LlmProvider, ProviderHealth, ProviderHealthSnapshot};use ai_memory_store::{ApproveAutoImproveProposalResult, AutoImproveProposalOperation, AutoImproveProposalStatus,- DecayParams, EmbeddingWrite, NewAutoImproveProposal, ReaderPool, RejectAutoImproveProposal,+ DecayParams, NewAutoImproveProposal, ReaderPool, RejectAutoImproveProposal,ScopeResolutionError, StageAutoImproveRun, StoreError, WriterHandle, create_explicit_scope,f32_vec_to_bytes, lookup_existing_scope, lookup_existing_workspace,};@@ -73,7 +75,6 @@ use serde::{Deserialize, Serialize};use tokio_util::io::ReaderStream;use tracing::{info, warn};-const EMBEDDING_WRITE_BATCH: usize = 100;const CONTRIBUTORS_WEBHOOK_NAME: &str = "contributors";/// Shared state for the admin router.@@ -528,6 +529,7 @@ pub fn admin_router(state: AdminState) -> Router {).route("/admin/status", get(handle_status)).route("/admin/projects", get(handle_list_projects))+ .route("/admin/open-sessions", get(handle_open_sessions)).route("/admin/audit-contamination",get(handle_audit_contamination),@@ -637,7 +639,7 @@ async fn handle_backup(State(state): State<Arc<AdminState>>) -> Response {Response::builder().status(StatusCode::INTERNAL_SERVER_ERROR).body(Body::empty())- .unwrap()+ .unwrap_or_else(|_| StatusCode::INTERNAL_SERVER_ERROR.into_response())}),Err(e) => {warn!(error = %e, "backup failed");@@ -651,7 +653,7 @@ async fn handle_backup(State(state): State<Arc<AdminState>>) -> Response {Response::builder().status(StatusCode::INTERNAL_SERVER_ERROR).body(Body::empty())- .unwrap()+ .unwrap_or_else(|_| StatusCode::INTERNAL_SERVER_ERROR.into_response())})}}@@ -822,6 +824,87 @@ async fn handle_status(State(state): State<Arc<AdminState>>) -> impl IntoRespons}}+// ---------------------------------------------------------------------+// open-sessions+// ---------------------------------------------------------------------++/// Query string for `GET /admin/open-sessions` — open (not yet ended)+/// sessions for one scope + agent, newest first. Backs the thin+/// `ai-memory finalize-session` command, which posts synthetic+/// session-end hooks for whatever this returns.+#[derive(Debug, Deserialize)]+struct OpenSessionsQuery {+ /// Workspace name (required).+ workspace: String,+ /// Project name (required).+ project: String,+ /// Agent kind filter, kebab-case (`codex`, `claude-code`, …).+ agent: String,+ /// When true, return every open session; otherwise just the newest.+ #[serde(default)]+ all: bool,+}++/// Wire shape for one open session in the `GET /admin/open-sessions`+/// response.+#[derive(Debug, Serialize)]+struct OpenSessionEntry {+ session_id: String,+ cwd: Option<String>,+}++/// Parse a kebab-case agent wire string into an [`AgentKind`].+fn parse_agent_kind(raw: &str) -> Option<AgentKind> {+ AgentKind::ALL.into_iter().find(|kind| kind.as_str() == raw)+}++async fn handle_open_sessions(+ State(state): State<Arc<AdminState>>,+ Query(query): Query<OpenSessionsQuery>,+) -> impl IntoResponse {+ let Some(agent) = parse_agent_kind(&query.agent) else {+ return (+ StatusCode::BAD_REQUEST,+ Json(serde_json::json!({+ "error": format!("unknown agent kind: {}", query.agent)+ })),+ );+ };+ let (ws, proj) = match lookup_ws_proj_no_create(&state, &query.workspace, &query.project).await+ {+ Ok(ids) => ids,+ Err(e) => return e,+ };+ let limit = if query.all { None } else { Some(1) };+ match state+ .reader+ .open_sessions_for_scope_agent(ws, proj, agent, limit)+ .await+ {+ Ok(sessions) => {+ let sessions: Vec<OpenSessionEntry> = sessions+ .into_iter()+ .map(|s| OpenSessionEntry {+ session_id: s.session_id.to_string(),+ cwd: s.cwd,+ })+ .collect();+ (+ StatusCode::OK,+ Json(+ serde_json::to_value(&sessions)+ .map(|list| serde_json::json!({ "sessions": list }))+ .unwrap_or_else(|_| serde_json::json!({ "sessions": [] })),+ ),+ )+ }+ Err(e) => (+ StatusCode::INTERNAL_SERVER_ERROR,+ Json(serde_json::json!({ "error": e.to_string() })),+ ),+ }+}+// ---------------------------------------------------------------------// search// ---------------------------------------------------------------------@@ -2460,23 +2543,6 @@ pub struct EmbedReport {pub dim: u32,}-#[derive(Default)]-struct EmbedCounts {- embedded: usize,- skipped: usize,- failed: usize,- would_embed: usize,-}--impl EmbedCounts {- fn absorb(&mut self, other: Self) {- self.embedded += other.embedded;- self.skipped += other.skipped;- self.failed += other.failed;- self.would_embed += other.would_embed;- }-}-async fn embed_project_pages(state: &AdminState,embedder: &Arc<dyn Embedder>,@@ -2484,87 +2550,18 @@ async fn embed_project_pages(proj: ProjectId,reembed: bool,dry_run: bool,-) -> Result<EmbedCounts, (StatusCode, Json<serde_json::Value>)> {- let provider = embedder.provider().to_string();- let model = embedder.model().to_string();- let dim = embedder.dim();-- let candidates = state- .reader- .decay_candidates(ws, proj)- .await- .map_err(|e| internal_err(e.to_string()))?;-- let already: std::collections::HashSet<_> = if reembed {- std::collections::HashSet::new()- } else {- state- .reader- .embedded_page_ids(ws, proj, provider.clone(), model.clone(), dim)- .await- .map_err(|e| internal_err(e.to_string()))?- .into_iter()- .collect()- };-- let mut counts = EmbedCounts::default();- let mut pending = Vec::with_capacity(EMBEDDING_WRITE_BATCH);-- for cand in candidates {- if !reembed && already.contains(&cand.id) {- counts.skipped += 1;- continue;- }- if dry_run {- counts.would_embed += 1;- continue;- }- let md = match state.wiki.read_page(ws, proj, &cand.path) {- Ok(m) => m,- Err(e) => {- warn!(path = %cand.path, error = %e, "embed: skip unreadable page");- counts.failed += 1;- continue;- }- };- if md.body.trim().is_empty() {- counts.skipped += 1;- continue;- }- let vec = match embedder.embed_document(&md.body).await {- Ok(v) => v,- Err(e) => {- warn!(path = %cand.path, error = %e, "embed: provider call failed");- counts.failed += 1;- continue;- }- };- pending.push(EmbeddingWrite {- page_id: cand.id,- vector_bytes: f32_vec_to_bytes(&vec),- provider: provider.clone(),- model: model.clone(),- dim,- });- if pending.len() >= EMBEDDING_WRITE_BATCH {- flush_embedding_batch(- &state.writer,- &mut pending,- &mut counts.embedded,- &mut counts.failed,- )- .await;- }- }- flush_embedding_batch(+) -> Result<EmbedBackfillCounts, (StatusCode, Json<serde_json::Value>)> {+ run_embedding_backfill(+ &state.reader,&state.writer,- &mut pending,- &mut counts.embedded,- &mut counts.failed,+ &state.wiki,+ embedder,+ ws,+ proj,+ EmbedBackfillOptions { reembed, dry_run },)- .await;-- Ok(counts)+ .await+ .map_err(|e| internal_err(e.to_string()))}async fn handle_embed(@@ -2587,7 +2584,7 @@ async fn handle_embed(let model = embedder.model().to_string();let dim = embedder.dim();- let mut totals = EmbedCounts::default();+ let mut totals = EmbedBackfillCounts::default();if req.all_projects {if let Some(ws) = state@@ -2656,25 +2653,6 @@ async fn handle_embed())}-async fn flush_embedding_batch(- writer: &WriterHandle,- pending: &mut Vec<EmbeddingWrite>,- embedded: &mut usize,- failed: &mut usize,-) {- if pending.is_empty() {- return;- }- let batch = std::mem::replace(pending, Vec::with_capacity(EMBEDDING_WRITE_BATCH));- let count = batch.len();- if let Err(e) = writer.store_embeddings(batch).await {- *failed += count;- warn!(count, error = %e, "embed: store_embeddings failed");- } else {- *embedded += count;- }-}-// ---------------------------------------------------------------------// commit// ---------------------------------------------------------------------@@ -5078,6 +5056,152 @@ mod tests {assert_eq!(json["providers"]["embedding"]["status"], "disabled");}+ #[tokio::test]+ async fn open_sessions_filters_by_scope_and_agent() {+ 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".to_string())+ .await+ .unwrap();+ let target = store+ .writer+ .get_or_create_project(ws, "target".to_string(), None)+ .await+ .unwrap();+ let other_project = store+ .writer+ .get_or_create_project(ws, "other".to_string(), None)+ .await+ .unwrap();+ let older = SessionId::new();+ let latest = SessionId::new();+ let other_agent = SessionId::new();+ let other_scope = SessionId::new();+ let ended = SessionId::new();+ for (id, project_id, agent) in [+ (older, target, AgentKind::Codex),+ (other_agent, target, AgentKind::ClaudeCode),+ (other_scope, other_project, AgentKind::Codex),+ (ended, target, AgentKind::Codex),+ (latest, target, AgentKind::Codex),+ ] {+ store+ .writer+ .begin_session(NewSession {+ id,+ workspace_id: ws,+ project_id,+ agent_kind: agent,+ cwd: Some(std::path::PathBuf::from("/tmp/target")),+ })+ .await+ .unwrap();+ }+ store.writer.end_session(ended, None).await.unwrap();++ let router = admin_router(AdminState {+ writer: store.writer.clone(),+ reader: store.reader.clone(),+ wiki,+ llm: None,+ auto_improve_require_approval: false,+ auto_improve_review_config: Default::default(),+ embedder: None,+ provider_health: ProviderHealth::default(),+ decay_params: DecayParams::default(),+ data_dir: tmp.path().to_path_buf(),+ db_path: store.db_path().to_path_buf(),+ bind: "127.0.0.1:49374".to_string(),+ home_dir: None,+ bootstrap_lock: Arc::new(tokio::sync::Mutex::new(())),+ token_pepper: None,+ active_project: ai_memory_core::ActiveProject::new(),+ scope_invalidator: None,+ });++ // Default: only the newest open session for the scope + agent.+ let resp = router+ .clone()+ .oneshot(+ Request::builder()+ .uri("/admin/open-sessions?workspace=default&project=target&agent=codex")+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap();+ assert_eq!(resp.status(), StatusCode::OK);+ let body = to_bytes(resp.into_body(), usize::MAX).await.unwrap();+ let json: serde_json::Value = serde_json::from_slice(&body).unwrap();+ let sessions = json["sessions"].as_array().unwrap();+ assert_eq!(sessions.len(), 1);+ assert_eq!(sessions[0]["session_id"], latest.to_string());+ assert_eq!(sessions[0]["cwd"], "/tmp/target");++ // `all=true`: every open codex session in the scope, ended and+ // other-scope/other-agent sessions excluded.+ let resp = router+ .clone()+ .oneshot(+ Request::builder()+ .uri("/admin/open-sessions?workspace=default&project=target&agent=codex&all=true")+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap();+ assert_eq!(resp.status(), StatusCode::OK);+ let body = to_bytes(resp.into_body(), usize::MAX).await.unwrap();+ let json: serde_json::Value = serde_json::from_slice(&body).unwrap();+ let sessions = json["sessions"].as_array().unwrap();+ assert_eq!(sessions.len(), 2);+ let ids: Vec<&str> = sessions+ .iter()+ .map(|s| s["session_id"].as_str().unwrap())+ .collect();+ assert!(ids.contains(&latest.to_string().as_str()));+ assert!(ids.contains(&older.to_string().as_str()));++ // Unknown agent kind fails closed with a 400.+ let resp = router+ .clone()+ .oneshot(+ Request::builder()+ .uri("/admin/open-sessions?workspace=default&project=target&agent=nope")+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap();+ assert_eq!(resp.status(), StatusCode::BAD_REQUEST);++ // Unknown scope fails closed with a 404 — never auto-created.+ let resp = router+ .oneshot(+ Request::builder()+ .uri("/admin/open-sessions?workspace=default&project=ghost&agent=codex")+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap();+ assert_eq!(resp.status(), StatusCode::NOT_FOUND);+ assert!(+ store+ .reader+ .find_project(ws, "ghost".to_string())+ .await+ .unwrap()+ .is_none(),+ "read route must not auto-create scopes"+ );+ }+fn read_page_test_router() -> (TempDir, Router) {let tmp = TempDir::new().unwrap();let store = Store::open(tmp.path()).unwrap();diff --git a/crates/ai-memory-cli/src/auth.rs b/crates/ai-memory-mcp/src/auth.rssimilarity index 100%rename from crates/ai-memory-cli/src/auth.rsrename to crates/ai-memory-mcp/src/auth.rsdiff --git a/crates/ai-memory-mcp/src/lib.rs b/crates/ai-memory-mcp/src/lib.rsindex ce27090c..c59a9ba2 100644--- a/crates/ai-memory-mcp/src/lib.rs+++ b/crates/ai-memory-mcp/src/lib.rs@@ -10,6 +10,7 @@pub mod actor;pub mod admin;+pub mod auth;mod server;pub use actor::actor_from_headers;diff --git a/crates/ai-memory-mcp/src/server.rs b/crates/ai-memory-mcp/src/server.rsindex 2f46e56b..ab30474c 100644--- a/crates/ai-memory-mcp/src/server.rs+++ b/crates/ai-memory-mcp/src/server.rs@@ -1075,27 +1075,10 @@ impl AiMemoryServer {/// Attach an LLM-backed consolidator. Without this, the/// `memory_consolidate` tool errors with "not configured". Also/// stores the LLM provider so `memory_lint` can run its- /// contradiction pass.- #[must_use]- pub fn with_consolidator(mut self, wiki: Wiki, llm: Arc<dyn LlmProvider>) -> Self {- let consolidator = Consolidator::new(- self.reader.clone(),- self.writer.clone(),- wiki.clone(),- llm.clone(),- self.workspace_id,- self.project_id,- );- self.consolidator = Some(Arc::new(consolidator));- self.llm = Some(llm);- self.wiki = Some(wiki);- self- }-- /// Variant of [`Self::with_consolidator`] that accepts a pre-built- /// `Arc<Consolidator>`. Used when the same consolidator must be- /// shared with another subsystem (e.g. the hook router's- /// PreCompact branch) so both paths see the same handle.+ /// contradiction pass. Accepts a pre-built `Arc<Consolidator>` so+ /// the same consolidator can be shared with another subsystem+ /// (e.g. the hook router's PreCompact branch) and both paths see+ /// the same handle.#[must_use]pub fn with_consolidator_arc(mut self,diff --git a/crates/ai-memory-store/src/lib.rs b/crates/ai-memory-store/src/lib.rsindex b2f7aef3..0a58c479 100644--- a/crates/ai-memory-store/src/lib.rs+++ b/crates/ai-memory-store/src/lib.rs@@ -122,7 +122,7 @@ impl Store {mod tests {use super::*;use ai_memory_core::{- ActorContext, AgentKind, LinkTarget, NewObservation, NewPage, NewSession,+ ActorContext, AgentKind, LinkTarget, ManagedRunId, NewObservation, NewPage, NewSession,NewWorkstreamEvent, ObservationId, ObservationKind, PageId, PagePath, ProjectId, Sanitized,Sanitizer, SessionId, Tier, UserId, WorkspaceId, WorkstreamEventKind,};@@ -3977,4 +3977,795 @@ mod tests {assert_eq!(second.len(), 1);assert_eq!(second[0].content, "only visible in workspace b");}++ fn managed_prepare_input(+ ws: WorkspaceId,+ proj: ProjectId,+ owner: &str,+ ) -> PrepareWorkstreamRun {+ PrepareWorkstreamRun {+ workspace_id: ws,+ project_id: proj,+ repo_fingerprint: "repo".into(),+ worktree_fingerprint: "worktree".into(),+ cwd: "/repo".into(),+ agent: AgentKind::Codex,+ automatic_harness: false,+ available_agents: Vec::new(),+ selection: WorkstreamSelection::Current,+ lease_owner: owner.into(),+ }+ }++ async fn open_managed_scope(store: &Store, project: &str) -> (WorkspaceId, ProjectId) {+ let ws = store+ .writer+ .get_or_create_workspace("default")+ .await+ .unwrap();+ let proj = store+ .writer+ .get_or_create_project(ws, project, None)+ .await+ .unwrap();+ (ws, proj)+ }++ fn managed_event(+ event_id: &str,+ agent: AgentKind,+ native: &str,+ content: &str,+ ) -> NewWorkstreamEvent {+ NewWorkstreamEvent {+ event_id: event_id.into(),+ agent,+ native_session_id: native.into(),+ source_record_id: None,+ kind: WorkstreamEventKind::Message,+ role: Some("assistant".into()),+ content: content.into(),+ occurred_at: None,+ metadata: serde_json::json!({}),+ }+ }++ fn complete_finish(+ run_id: ManagedRunId,+ events: Vec<NewWorkstreamEvent>,+ ) -> FinishWorkstreamRun {+ FinishWorkstreamRun {+ run_id,+ native_session_id: None,+ source_cursor: None,+ events,+ complete: true,+ segment_path: None,+ exit_code: Some(0),+ }+ }++ fn set_managed_run_lease(db_path: &std::path::Path, run_id: ManagedRunId, lease: i64) {+ let conn = Connection::open(db_path).unwrap();+ conn.execute(+ "UPDATE managed_runs SET lease_expires_at = ?1 WHERE id = ?2",+ params![lease, run_id.as_bytes()],+ )+ .unwrap();+ }++ fn managed_run_lease(db_path: &std::path::Path, run_id: ManagedRunId) -> i64 {+ let conn = Connection::open(db_path).unwrap();+ conn.query_row(+ "SELECT lease_expires_at FROM managed_runs WHERE id = ?1",+ params![run_id.as_bytes()],+ |row| row.get(0),+ )+ .unwrap()+ }++ #[tokio::test]+ async fn managed_run_heartbeat_extends_only_a_live_lease() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let (ws, proj) = open_managed_scope(&store, "managed-heartbeat").await;+ let run = store+ .writer+ .prepare_workstream_run(managed_prepare_input(ws, proj, "test:1"))+ .await+ .unwrap();++ // Pull the lease close to expiry; a heartbeat must push it back out.+ let initial = managed_run_lease(store.db_path(), run.run_id);+ set_managed_run_lease(store.db_path(), run.run_id, initial - 5_000_000);+ assert!(+ store+ .writer+ .heartbeat_managed_run(run.run_id)+ .await+ .unwrap()+ );+ assert!(+ managed_run_lease(store.db_path(), run.run_id) >= initial,+ "heartbeat must extend the lease"+ );++ // A lease that already lapsed cannot be revived by a heartbeat.+ set_managed_run_lease(store.db_path(), run.run_id, 1);+ assert!(+ !store+ .writer+ .heartbeat_managed_run(run.run_id)+ .await+ .unwrap()+ );++ // Unknown runs never heartbeat.+ assert!(+ !store+ .writer+ .heartbeat_managed_run(ManagedRunId::new())+ .await+ .unwrap()+ );++ // Neither do finished runs.+ store+ .writer+ .finish_workstream_run(complete_finish(run.run_id, Vec::new()))+ .await+ .unwrap();+ assert!(+ !store+ .writer+ .heartbeat_managed_run(run.run_id)+ .await+ .unwrap()+ );++ // Nor cancelled runs.+ let cancelled = store+ .writer+ .prepare_workstream_run(managed_prepare_input(ws, proj, "test:2"))+ .await+ .unwrap();+ assert!(+ store+ .writer+ .cancel_managed_run(cancelled.run_id)+ .await+ .unwrap()+ );+ assert!(+ !store+ .writer+ .heartbeat_managed_run(cancelled.run_id)+ .await+ .unwrap()+ );+ }++ #[tokio::test]+ async fn finish_run_after_complete_is_an_idempotent_no_op() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let (ws, proj) = open_managed_scope(&store, "managed-idempotent-finish").await;+ let run = store+ .writer+ .prepare_workstream_run(managed_prepare_input(ws, proj, "test:1"))+ .await+ .unwrap();+ assert!(+ store+ .writer+ .link_managed_run_session(run.run_id, AgentKind::Codex, "native-1")+ .await+ .unwrap()+ );++ let finish = FinishWorkstreamRun {+ run_id: run.run_id,+ native_session_id: Some("native-1".into()),+ source_cursor: Some("cursor-1".into()),+ events: vec![+ managed_event("ev-1", AgentKind::Codex, "native-1", "first"),+ managed_event("ev-2", AgentKind::Codex, "native-1", "second"),+ ],+ complete: true,+ segment_path: Some("segment.jsonl".into()),+ exit_code: Some(0),+ };+ let first = store+ .writer+ .finish_workstream_run(finish.clone())+ .await+ .unwrap();+ assert_eq!(first.imported_events, 2);+ assert_eq!(first.latest_sequence, 2);++ // Re-finishing a completed run imports nothing, even with new input.+ let second = store.writer.finish_workstream_run(finish).await.unwrap();+ assert_eq!(second.imported_events, 0);+ assert_eq!(second.latest_sequence, 2);+ assert_eq!(+ store+ .reader+ .managed_run_status(run.run_id)+ .await+ .unwrap()+ .unwrap()+ .state,+ "finished"+ );+ let events = store+ .reader+ .search_workstream_events(run.workstream_id, String::new(), 10)+ .await+ .unwrap();+ assert_eq!(+ events.len(),+ 2,+ "the no-op finish must not duplicate events"+ );+ }++ #[tokio::test]+ async fn finish_run_rejects_cancelled_and_unknown_runs() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let (ws, proj) = open_managed_scope(&store, "managed-finish-closed").await;+ let run = store+ .writer+ .prepare_workstream_run(managed_prepare_input(ws, proj, "test:1"))+ .await+ .unwrap();+ assert!(store.writer.cancel_managed_run(run.run_id).await.unwrap());++ let cancelled = store+ .writer+ .finish_workstream_run(complete_finish(run.run_id, Vec::new()))+ .await+ .unwrap_err();+ assert!(+ matches!(cancelled, StoreError::InvalidState(ref msg) if msg.contains("expired")),+ "cancelled runs report their state: {cancelled}"+ );++ let unknown = store+ .writer+ .finish_workstream_run(complete_finish(ManagedRunId::new(), Vec::new()))+ .await+ .unwrap_err();+ assert!(matches!(unknown, StoreError::NotFound(_)));+ }++ #[tokio::test]+ async fn finish_run_rejects_foreign_events_and_rolls_back_the_batch() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let (ws, proj) = open_managed_scope(&store, "managed-finish-foreign").await;+ let run = store+ .writer+ .prepare_workstream_run(managed_prepare_input(ws, proj, "test:1"))+ .await+ .unwrap();+ assert!(+ store+ .writer+ .link_managed_run_session(run.run_id, AgentKind::Codex, "native-1")+ .await+ .unwrap()+ );++ // A valid event followed by another agent's event must import nothing.+ let wrong_agent = store+ .writer+ .finish_workstream_run(FinishWorkstreamRun {+ run_id: run.run_id,+ native_session_id: Some("native-1".into()),+ source_cursor: None,+ events: vec![+ managed_event("ev-1", AgentKind::Codex, "native-1", "valid"),+ managed_event("ev-2", AgentKind::ClaudeCode, "native-1", "foreign agent"),+ ],+ complete: false,+ segment_path: None,+ exit_code: None,+ })+ .await+ .unwrap_err();+ assert!(+ matches!(wrong_agent, StoreError::InvalidState(ref msg) if msg.contains("ev-2")),+ "{wrong_agent}"+ );+ assert!(+ store+ .reader+ .search_workstream_events(run.workstream_id, String::new(), 10)+ .await+ .unwrap()+ .is_empty(),+ "a rejected batch must roll back its earlier inserts"+ );+ assert_eq!(+ store+ .reader+ .managed_run_status(run.run_id)+ .await+ .unwrap()+ .unwrap()+ .state,+ "active"+ );++ // Events from a different native session are rejected the same way.+ let wrong_session = store+ .writer+ .finish_workstream_run(FinishWorkstreamRun {+ run_id: run.run_id,+ native_session_id: Some("native-1".into()),+ source_cursor: None,+ events: vec![managed_event(+ "ev-3",+ AgentKind::Codex,+ "native-2",+ "foreign session",+ )],+ complete: false,+ segment_path: None,+ exit_code: None,+ })+ .await+ .unwrap_err();+ assert!(+ matches!(wrong_session, StoreError::InvalidState(ref msg) if msg.contains("native session")),+ "{wrong_session}"+ );++ // The run is still usable after the rejected batches.+ let finished = store+ .writer+ .finish_workstream_run(FinishWorkstreamRun {+ run_id: run.run_id,+ native_session_id: Some("native-1".into()),+ source_cursor: None,+ events: vec![managed_event("ev-1", AgentKind::Codex, "native-1", "valid")],+ complete: true,+ segment_path: None,+ exit_code: Some(0),+ })+ .await+ .unwrap();+ assert_eq!(finished.imported_events, 1);+ assert_eq!(finished.latest_sequence, 1);+ }++ #[tokio::test]+ async fn accept_context_marks_delivery_only_for_active_runs() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let (ws, proj) = open_managed_scope(&store, "managed-accept").await;++ assert!(+ !store+ .writer+ .accept_managed_run_context(ManagedRunId::new())+ .await+ .unwrap(),+ "unknown runs cannot accept context"+ );++ let run = store+ .writer+ .prepare_workstream_run(managed_prepare_input(ws, proj, "test:1"))+ .await+ .unwrap();+ assert!(+ store+ .writer+ .accept_managed_run_context(run.run_id)+ .await+ .unwrap()+ );+ let status = store+ .reader+ .managed_run_status(run.run_id)+ .await+ .unwrap()+ .unwrap();+ assert!(status.context_delivered);+ assert!(+ store+ .writer+ .accept_managed_run_context(run.run_id)+ .await+ .unwrap(),+ "re-acknowledging a live run stays idempotent"+ );++ store+ .writer+ .finish_workstream_run(complete_finish(run.run_id, Vec::new()))+ .await+ .unwrap();+ assert!(+ !store+ .writer+ .accept_managed_run_context(run.run_id)+ .await+ .unwrap(),+ "finished runs cannot accept context"+ );++ let cancelled = store+ .writer+ .prepare_workstream_run(managed_prepare_input(ws, proj, "test:2"))+ .await+ .unwrap();+ assert!(+ store+ .writer+ .cancel_managed_run(cancelled.run_id)+ .await+ .unwrap()+ );+ assert!(+ !store+ .writer+ .accept_managed_run_context(cancelled.run_id)+ .await+ .unwrap(),+ "cancelled runs cannot accept context"+ );+ }++ #[tokio::test]+ async fn run_context_delivers_newest_events_in_sequence_order() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let (ws, proj) = open_managed_scope(&store, "managed-context").await;+ let first = store+ .writer+ .prepare_workstream_run(managed_prepare_input(ws, proj, "test:1"))+ .await+ .unwrap();+ // Finish without a native session so the next run starts with an+ // undelivered context window spanning the whole ledger.+ store+ .writer+ .finish_workstream_run(complete_finish(+ first.run_id,+ vec![+ managed_event("ev-1", AgentKind::Codex, "native-x", "one"),+ managed_event("ev-2", AgentKind::Codex, "native-x", "two"),+ managed_event("ev-3", AgentKind::Codex, "native-x", "three"),+ ],+ ))+ .await+ .unwrap();++ let second = store+ .writer+ .prepare_workstream_run(managed_prepare_input(ws, proj, "test:2"))+ .await+ .unwrap();+ assert_eq!(second.workstream_id, first.workstream_id);+ assert_eq!(second.sync_after, 0);+ assert_eq!(second.sync_through, 3);++ let context = store+ .reader+ .managed_run_context(second.run_id, 256)+ .await+ .unwrap()+ .unwrap();+ assert_eq!(context.workstream_name, "default");+ assert_eq!(context.sync_after, 0);+ assert_eq!(context.sync_through, 3);+ assert!(!context.context_delivered);+ let ids: Vec<&str> = context.events.iter().map(|e| e.event_id.as_str()).collect();+ assert_eq!(ids, ["ev-1", "ev-2", "ev-3"], "ascending sequence order");+ let sequences: Vec<i64> = context.events.iter().map(|e| e.sequence).collect();+ assert_eq!(sequences, [1, 2, 3]);++ // A capped window keeps the newest events, still ascending.+ let capped = store+ .reader+ .managed_run_context(second.run_id, 2)+ .await+ .unwrap()+ .unwrap();+ let capped_ids: Vec<&str> = capped.events.iter().map(|e| e.event_id.as_str()).collect();+ assert_eq!(capped_ids, ["ev-2", "ev-3"]);++ // Accepting marks the delivery on the run's own context only.+ assert!(+ store+ .writer+ .accept_managed_run_context(second.run_id)+ .await+ .unwrap()+ );+ assert!(+ store+ .reader+ .managed_run_context(second.run_id, 256)+ .await+ .unwrap()+ .unwrap()+ .context_delivered+ );++ // Finished and unknown runs have no active context window.+ assert!(+ store+ .reader+ .managed_run_context(first.run_id, 256)+ .await+ .unwrap()+ .is_none()+ );+ assert!(+ store+ .reader+ .managed_run_context(ManagedRunId::new(), 256)+ .await+ .unwrap()+ .is_none()+ );+ assert!(+ store+ .reader+ .managed_run_status(ManagedRunId::new())+ .await+ .unwrap()+ .is_none()+ );+ }++ #[tokio::test]+ async fn search_events_tails_in_descending_order_and_clamps_the_limit() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let (ws, proj) = open_managed_scope(&store, "managed-search").await;+ let run = store+ .writer+ .prepare_workstream_run(managed_prepare_input(ws, proj, "test:1"))+ .await+ .unwrap();+ store+ .writer+ .finish_workstream_run(complete_finish(+ run.run_id,+ vec![+ managed_event("ev-1", AgentKind::Codex, "native-x", "alpha one"),+ managed_event("ev-2", AgentKind::Codex, "native-x", "alpha two"),+ managed_event("ev-3", AgentKind::Codex, "native-x", "alpha zebra three"),+ managed_event("ev-4", AgentKind::Codex, "native-x", "alpha four"),+ managed_event("ev-5", AgentKind::Codex, "native-x", "alpha five"),+ ],+ ))+ .await+ .unwrap();++ let ids = |events: Vec<ai_memory_core::WorkstreamEvent>| -> Vec<String> {+ events.into_iter().map(|e| e.event_id).collect()+ };++ // An empty query tails the ledger, newest first.+ let all = store+ .reader+ .search_workstream_events(run.workstream_id, String::new(), 10)+ .await+ .unwrap();+ assert_eq!(ids(all), ["ev-5", "ev-4", "ev-3", "ev-2", "ev-1"]);++ let top_two = store+ .reader+ .search_workstream_events(run.workstream_id, String::new(), 2)+ .await+ .unwrap();+ assert_eq!(ids(top_two), ["ev-5", "ev-4"]);++ // The limit is clamped into 1..=100.+ let clamped_low = store+ .reader+ .search_workstream_events(run.workstream_id, String::new(), 0)+ .await+ .unwrap();+ assert_eq!(ids(clamped_low), ["ev-5"]);+ let clamped_high = store+ .reader+ .search_workstream_events(run.workstream_id, String::new(), 500)+ .await+ .unwrap();+ assert_eq!(clamped_high.len(), 5);++ // A text query matches through FTS only, and the `field:` prefixes+ // accepted by the search surface are stripped before matching.+ let fts = store+ .reader+ .search_workstream_events(run.workstream_id, "zebra".into(), 10)+ .await+ .unwrap();+ assert_eq!(ids(fts), ["ev-3"]);+ for prefixed in ["title:zebra", "body:zebra", "content:zebra"] {+ let hit = store+ .reader+ .search_workstream_events(run.workstream_id, prefixed.into(), 10)+ .await+ .unwrap();+ assert_eq!(ids(hit), ["ev-3"], "prefix must be stripped: {prefixed}");+ }+ }++ #[tokio::test]+ async fn link_native_session_rejects_blank_wrong_agent_and_inactive_targets() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let (ws, proj) = open_managed_scope(&store, "managed-link").await;+ let run = store+ .writer+ .prepare_workstream_run(managed_prepare_input(ws, proj, "test:1"))+ .await+ .unwrap();++ for blank in ["", " "] {+ assert!(+ !store+ .writer+ .link_managed_run_session(run.run_id, AgentKind::Codex, blank)+ .await+ .unwrap(),+ "blank native session ids are ignored"+ );+ }+ assert!(+ !store+ .writer+ .link_managed_run_session(run.run_id, AgentKind::ClaudeCode, "native-1")+ .await+ .unwrap(),+ "a different harness cannot claim the run"+ );+ assert!(+ !store+ .writer+ .link_managed_run_session(ManagedRunId::new(), AgentKind::Codex, "native-1")+ .await+ .unwrap(),+ "unknown runs cannot be linked"+ );++ assert!(+ store+ .writer+ .link_managed_run_session(run.run_id, AgentKind::Codex, "native-1")+ .await+ .unwrap()+ );+ assert_eq!(+ store+ .reader+ .managed_run_status(run.run_id)+ .await+ .unwrap()+ .unwrap()+ .native_session_id+ .as_deref(),+ Some("native-1")+ );++ assert!(store.writer.cancel_managed_run(run.run_id).await.unwrap());+ assert!(+ !store+ .writer+ .link_managed_run_session(run.run_id, AgentKind::Codex, "native-2")+ .await+ .unwrap(),+ "inactive runs cannot be linked"+ );+ }++ #[tokio::test]+ async fn workstream_selection_validates_names_and_finds_existing() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let (ws, proj) = open_managed_scope(&store, "managed-selection").await;+ let prepare = |selection| PrepareWorkstreamRun {+ selection,+ ..managed_prepare_input(ws, proj, "test:1")+ };++ let created = store+ .writer+ .prepare_workstream_run(prepare(WorkstreamSelection::New("alpha".into())))+ .await+ .unwrap();+ assert_eq!(created.workstream_name, "alpha");+ assert!(+ store+ .writer+ .cancel_managed_run(created.run_id)+ .await+ .unwrap()+ );++ let named = store+ .writer+ .prepare_workstream_run(prepare(WorkstreamSelection::Named("alpha".into())))+ .await+ .unwrap();+ assert_eq!(named.workstream_id, created.workstream_id);+ assert!(store.writer.cancel_managed_run(named.run_id).await.unwrap());++ let duplicate = store+ .writer+ .prepare_workstream_run(prepare(WorkstreamSelection::New("alpha".into())))+ .await+ .unwrap_err();+ assert!(matches!(duplicate, StoreError::Duplicate(_)));++ let missing = store+ .writer+ .prepare_workstream_run(prepare(WorkstreamSelection::Named("missing".into())))+ .await+ .unwrap_err();+ assert!(matches!(missing, StoreError::NotFound(_)));++ for invalid in ["bad/name", "bad\\name", " ", ""] {+ let err = store+ .writer+ .prepare_workstream_run(prepare(WorkstreamSelection::New(invalid.into())))+ .await+ .unwrap_err();+ assert!(+ matches!(err, StoreError::InvalidState(_)),+ "invalid name '{invalid}' must be rejected: {err}"+ );+ }+ }++ #[tokio::test]+ async fn prepare_expires_a_stale_lease_and_reopens_the_workstream() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let (ws, proj) = open_managed_scope(&store, "managed-expiry").await;+ let stale = store+ .writer+ .prepare_workstream_run(managed_prepare_input(ws, proj, "test:1"))+ .await+ .unwrap();++ // Force the lease into the past; the next prepare must expire the run+ // instead of reporting the workstream busy.+ set_managed_run_lease(store.db_path(), stale.run_id, 1);+ let reopened = store+ .writer+ .prepare_workstream_run(managed_prepare_input(ws, proj, "test:2"))+ .await+ .unwrap();+ assert_ne!(reopened.run_id, stale.run_id);+ assert_eq!(reopened.workstream_id, stale.workstream_id);+ assert_eq!(+ store+ .reader+ .managed_run_status(stale.run_id)+ .await+ .unwrap()+ .unwrap()+ .state,+ "expired"+ );+ assert_eq!(+ store+ .reader+ .managed_run_status(reopened.run_id)+ .await+ .unwrap()+ .unwrap()+ .state,+ "active"+ );+ }}diff --git a/crates/ai-memory-store/src/migrations.rs b/crates/ai-memory-store/src/migrations.rsindex be471589..131be7d9 100644--- a/crates/ai-memory-store/src/migrations.rs+++ b/crates/ai-memory-store/src/migrations.rs@@ -112,6 +112,100 @@ mod tests {assert!(rendered.contains("through V30"), "{rendered}");}+ /// A migration that fails partway must surface as a typed+ /// `StoreError::Migration`, must not be recorded in refinery's history+ /// (per-migration transaction rollback), and re-running after the+ /// precondition is fixed must converge to the full embedded schema.+ #[test]+ fn failed_migration_rolls_back_and_recovers_after_fix() {+ let tmp = tempfile::TempDir::new().unwrap();+ let db_path = tmp.path().join("memory.sqlite");+ let mut conn = Connection::open(&db_path).unwrap();++ // Migrate up to just before V31 (managed workstreams), then poison the+ // run by pre-creating a table V31 builds mid-script — after it has+ // already created `workstreams` and its pairing trigger.+ run_to(&mut conn, 30).unwrap();+ conn.execute("CREATE TABLE workstream_native_sessions (id INTEGER)", [])+ .unwrap();++ // (a) The failure surfaces as the typed migration error, not a panic+ // or a misclassified schema-ahead error.+ let err = run(&mut conn).unwrap_err();+ match &err {+ StoreError::Migration(_) => {}+ other => panic!("expected StoreError::Migration, got: {other:?}"),+ }++ // (c) No half-migrated state: refinery's history must stop at V30 —+ // the failed V31 is not recorded — and its earlier statements were+ // rolled back, leaving the poisoned table untouched.+ let applied = applied_versions(&conn);+ assert_eq!(+ applied.last(),+ Some(&30),+ "failed migration must not be recorded: {applied:?}"+ );+ assert!(+ !applied.contains(&31),+ "V31 must be absent from history: {applied:?}"+ );+ assert_eq!(schema_object_count(&conn, "table", "workstreams"), 0);+ assert_eq!(+ schema_object_count(&conn, "trigger", "workstreams_ws_proj_pairing_ai"),+ 0,+ "statements before the failure must roll back with the migration"+ );+ let poisoned_cols: i64 = conn+ .query_row(+ "SELECT COUNT(*) FROM pragma_table_info('workstream_native_sessions')",+ [],+ |row| row.get(0),+ )+ .unwrap();+ assert_eq!(+ poisoned_cols, 1,+ "the pre-existing table must survive untouched"+ );++ // (b) Fix the precondition and re-run: the store converges to the+ // embedded ceiling with the real V31 schema in place.+ conn.execute("DROP TABLE workstream_native_sessions", [])+ .unwrap();+ run(&mut conn).unwrap();+ let applied = applied_versions(&conn);+ assert_eq!(+ applied.last(),+ Some(&i64::from(max_supported_version())),+ "re-run must reach the embedded ceiling: {applied:?}"+ );+ assert!(applied.contains(&31) && applied.contains(&32));+ assert_eq!(schema_object_count(&conn, "table", "workstreams"), 1);+ assert_eq!(+ schema_object_count(&conn, "trigger", "workstreams_ws_proj_pairing_ai"),+ 1+ );+ }++ fn applied_versions(conn: &Connection) -> Vec<i64> {+ let mut stmt = conn+ .prepare("SELECT version FROM refinery_schema_history ORDER BY version")+ .unwrap();+ stmt.query_map([], |row| row.get(0))+ .unwrap()+ .collect::<Result<Vec<i64>, _>>()+ .unwrap()+ }++ fn schema_object_count(conn: &Connection, kind: &str, name: &str) -> i64 {+ conn.query_row(+ "SELECT COUNT(*) FROM sqlite_master WHERE type = ?1 AND name = ?2",+ params![kind, name],+ |row| row.get(0),+ )+ .unwrap()+ }+#[test]fn v28_to_v29_preserves_existing_rows() {let mut conn = Connection::open_in_memory().unwrap();diff --git a/crates/ai-memory-store/src/reader.rs b/crates/ai-memory-store/src/reader.rsindex ad6af2d8..2c41397d 100644--- a/crates/ai-memory-store/src/reader.rs+++ b/crates/ai-memory-store/src/reader.rs@@ -1653,11 +1653,10 @@ impl ReaderPool {let model: String = row.get(1)?;let dim: i64 = row.get(2)?;let count: i64 = row.get(3)?;- #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]Ok((provider,model,- dim as u32,+ u32::try_from(dim).unwrap_or(0),u64::try_from(count).unwrap_or(0),))})?;@@ -3060,11 +3059,10 @@ impl ReaderPool {let last_updated = last_updated_us.and_then(|us| jiff::Timestamp::from_microsecond(us).ok()).map(|ts| ts.to_string());- #[allow(clippy::cast_sign_loss)]out.push(ProjectSummary {workspace_name,project_name,- page_count: page_count.max(0) as u64,+ page_count: u64::try_from(page_count).unwrap_or(0),last_updated,});}@@ -3215,11 +3213,10 @@ impl ReaderPool {let last_updated = last_updated_us.and_then(|us| jiff::Timestamp::from_microsecond(us).ok()).map(|ts| ts.to_string());- #[allow(clippy::cast_sign_loss)]out.push(WorkspaceSummary {workspace_name,- project_count: project_count.max(0) as u64,- page_count: page_count.max(0) as u64,+ project_count: u64::try_from(project_count).unwrap_or(0),+ page_count: u64::try_from(page_count).unwrap_or(0),last_updated,});}@@ -4412,8 +4409,7 @@ fn materialise_observation(source_event,title,body,- #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]- importance: importance.clamp(1, 10) as u8,+ importance: u8::try_from(importance.clamp(1, 10)).unwrap_or(1),created_at: jiff::Timestamp::from_microsecond(created_us).map_err(|e| {StoreError::Memory(ai_memory_core::MemoryError::MalformedRecord(format!("bad timestamp: {e}"diff --git a/crates/ai-memory-store/src/scope.rs b/crates/ai-memory-store/src/scope.rsindex be6b8e95..8359052b 100644--- a/crates/ai-memory-store/src/scope.rs+++ b/crates/ai-memory-store/src/scope.rs@@ -452,19 +452,6 @@ impl<'a> ScopeResolver<'a> {})}- /// Create or fetch an explicit workspace/project pair. Admin write-style- /// operations use this when there is no current-project fallback involved.- pub async fn create_explicit(- &self,- workspace: &str,- project: &str,- ) -> Result<ResolvedScope, ScopeResolutionError> {- let Some(writer) = self.writer else {- return Err(ScopeResolutionError::WriterRequired);- };- create_explicit_scope(writer, workspace, project).await- }-/// Resolve and de-duplicate an explicit multi-scope list.pub async fn resolve_many_existing(&self,@@ -680,4 +667,293 @@ mod tests {let again = create_global_scope(&store.writer).await.unwrap();assert_eq!(again, created);}++ /// Expected outcome for one table-driven read-resolution row.+ #[derive(Debug)]+ enum Expected {+ Resolved(WorkspaceId, ProjectId),+ Failed(ScopeResolutionError),+ }++ struct ReadCase {+ name: &'static str,+ workspace: Option<&'static str>,+ project: Option<&'static str>,+ active: Option<(WorkspaceId, ProjectId)>,+ expected: Expected,+ }++ /// AGENTS.md mandates a table-driven suite over the scope-resolution+ /// policies: partial scope, missing explicit scope, active-project+ /// precedence, and cross-workspace isolation. One fixture with two+ /// workspaces carrying a same-named project feeds every row.+ #[tokio::test]+ async fn read_resolution_table_driven() {+ let tmp = tempfile::TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();++ let default_ws = store+ .writer+ .get_or_create_workspace("default")+ .await+ .unwrap();+ let default_scratch = store+ .writer+ .get_or_create_project(default_ws, "scratch", None)+ .await+ .unwrap();+ let alpha_ws = store.writer.get_or_create_workspace("alpha").await.unwrap();+ let alpha_app = store+ .writer+ .get_or_create_project(alpha_ws, "app", None)+ .await+ .unwrap();+ let alpha_shared = store+ .writer+ .get_or_create_project(alpha_ws, "shared", None)+ .await+ .unwrap();+ let beta_ws = store.writer.get_or_create_workspace("beta").await.unwrap();+ let beta_shared = store+ .writer+ .get_or_create_project(beta_ws, "shared", None)+ .await+ .unwrap();+ let beta_other = store+ .writer+ .get_or_create_project(beta_ws, "other", None)+ .await+ .unwrap();+ assert_ne!(+ alpha_shared, beta_shared,+ "same-named projects must get distinct ids per workspace"+ );++ let cases = vec![+ // --- Partial scope fails closed ---+ ReadCase {+ name: "workspace without project is rejected",+ workspace: Some("alpha"),+ project: None,+ active: Some((alpha_ws, alpha_app)),+ expected: Expected::Failed(ScopeResolutionError::WorkspaceProjectPairRequired),+ },+ ReadCase {+ name: "project-only read does not scan every workspace",+ workspace: None,+ project: Some("app"), // exists only in alpha; no active pointer+ active: None,+ expected: Expected::Failed(+ ScopeResolutionError::ProjectNotFoundInActiveOrDefault {+ project: "app".into(),+ },+ ),+ },+ ReadCase {+ name: "project-only read of a missing project fails closed",+ workspace: None,+ project: Some("ghost"),+ active: Some((alpha_ws, alpha_app)),+ expected: Expected::Failed(+ ScopeResolutionError::ProjectNotFoundInActiveOrDefault {+ project: "ghost".into(),+ },+ ),+ },+ // --- Missing explicit scope errors without creating ---+ ReadCase {+ name: "missing workspace errors",+ workspace: Some("ghost"),+ project: Some("app"),+ active: None,+ expected: Expected::Failed(ScopeResolutionError::WorkspaceNotFound {+ workspace: "ghost".into(),+ }),+ },+ ReadCase {+ name: "missing project in existing workspace errors",+ workspace: Some("alpha"),+ project: Some("ghost"),+ active: Some((alpha_ws, alpha_app)),+ expected: Expected::Failed(ScopeResolutionError::ProjectNotFoundInWorkspace {+ workspace: "alpha".into(),+ project: "ghost".into(),+ }),+ },+ // --- Active-project precedence ---+ ReadCase {+ name: "explicit pair beats active project",+ workspace: Some("beta"),+ project: Some("other"),+ active: Some((alpha_ws, alpha_app)),+ expected: Expected::Resolved(beta_ws, beta_other),+ },+ ReadCase {+ name: "no args resolves the active project",+ workspace: None,+ project: None,+ active: Some((alpha_ws, alpha_app)),+ expected: Expected::Resolved(alpha_ws, alpha_app),+ },+ ReadCase {+ name: "no args and no active resolves the default",+ workspace: None,+ project: None,+ active: None,+ expected: Expected::Resolved(default_ws, default_scratch),+ },+ ReadCase {+ name: "whitespace-only args resolve the default",+ workspace: Some(" "),+ project: Some(" "),+ active: None,+ expected: Expected::Resolved(default_ws, default_scratch),+ },+ ReadCase {+ name: "project-only prefers the active workspace",+ workspace: None,+ project: Some("shared"),+ active: Some((alpha_ws, alpha_app)),+ expected: Expected::Resolved(alpha_ws, alpha_shared),+ },+ ReadCase {+ name: "project-only falls back to the default workspace",+ workspace: None,+ project: Some("scratch"), // exists only in default+ active: Some((alpha_ws, alpha_app)),+ expected: Expected::Resolved(default_ws, default_scratch),+ },+ // --- Cross-workspace isolation ---+ ReadCase {+ name: "shared resolves in alpha when alpha is named",+ workspace: Some("alpha"),+ project: Some("shared"),+ active: None,+ expected: Expected::Resolved(alpha_ws, alpha_shared),+ },+ ReadCase {+ name: "shared resolves in beta when beta is named",+ workspace: Some("beta"),+ project: Some("shared"),+ active: None,+ expected: Expected::Resolved(beta_ws, beta_shared),+ },+ ReadCase {+ name: "project-only with beta active stays in beta",+ workspace: None,+ project: Some("shared"),+ active: Some((beta_ws, beta_other)),+ expected: Expected::Resolved(beta_ws, beta_shared),+ },+ ];++ let actor = ActorKey {+ user: Some("alice".into()),+ session_id: Some("s1".into()),+ };+ for case in &cases {+ let active_project = ActiveProject::new();+ if let Some((ws, proj)) = case.active {+ active_project.set_for(&actor, ws, proj, false);+ }+ let resolver = ScopeResolver::new(&store.reader, default_ws, default_scratch)+ .with_active_project(&active_project);+ let result = resolver+ .resolve_read_args(case.workspace, case.project, &actor)+ .await;+ match (&result, &case.expected) {+ (Ok(scope), Expected::Resolved(ws, proj)) => {+ assert_eq!(+ scope.as_tuple(),+ (*ws, *proj),+ "case '{}' resolved the wrong scope",+ case.name+ );+ }+ (Err(err), Expected::Failed(expected)) => {+ assert_eq!(err, expected, "case '{}' failed the wrong way", case.name);+ }+ (result, expected) => panic!(+ "case '{}': expected {expected:?}, got {result:?}",+ case.name+ ),+ }+ }++ // The failing read rows must not have auto-created anything.+ assert!(+ store+ .reader+ .find_workspace("ghost".into())+ .await+ .unwrap()+ .is_none(),+ "read resolution must never create workspaces"+ );+ assert!(+ store+ .reader+ .find_project(alpha_ws, "ghost".into())+ .await+ .unwrap()+ .is_none(),+ "read resolution must never create projects"+ );++ // The write-style helper is the only path that may create, and once it+ // does, the no-create lookup resolves the same ids.+ let created = create_explicit_scope(&store.writer, "ghost", "app")+ .await+ .unwrap();+ assert_eq!(+ lookup_existing_scope(&store.reader, "ghost", "app")+ .await+ .unwrap(),+ created+ );++ // Multi-scope resolution keeps same-named projects in their own+ // workspaces and fails closed when one entry is missing.+ let resolver = ScopeResolver::new(&store.reader, default_ws, default_scratch);+ let both = resolver+ .resolve_many_existing(+ &[+ ScopeName::new("alpha", "shared"),+ ScopeName::new("beta", "shared"),+ ],+ 25,+ )+ .await+ .unwrap();+ assert_eq!(+ both,+ vec![+ ResolvedScope {+ workspace_id: alpha_ws,+ project_id: alpha_shared+ },+ ResolvedScope {+ workspace_id: beta_ws,+ project_id: beta_shared+ },+ ]+ );+ let err = resolver+ .resolve_many_existing(+ &[+ ScopeName::new("alpha", "shared"),+ ScopeName::new("beta", "ghost"),+ ],+ 25,+ )+ .await+ .unwrap_err();+ assert_eq!(+ err,+ ScopeResolutionError::ProjectNotFoundInWorkspace {+ workspace: "beta".into(),+ project: "ghost".into()+ }+ );+ }}diff --git a/crates/ai-memory-store/src/writer.rs b/crates/ai-memory-store/src/writer.rsindex 34d1676c..1d986633 100644--- a/crates/ai-memory-store/src/writer.rs+++ b/crates/ai-memory-store/src/writer.rs@@ -1595,3 +1595,229 @@ fn worker_loop(mut conn: Connection, mut rx: mpsc::Receiver<WriteCmd>) {}tracing::debug!("writer thread exiting cleanly");}++#[cfg(test)]+mod tests {+ use super::*;+ use crate::Store;+ use ai_memory_core::{NewPage, PagePath, Tier};+ use std::time::Duration;+ use tempfile::TempDir;++ fn sample_page(ws: WorkspaceId, proj: ProjectId, path: &str, body: &str) -> NewPage {+ NewPage {+ workspace_id: ws,+ project_id: proj,+ path: PagePath::new(path).unwrap(),+ title: "test".into(),+ body: body.into(),+ tier: Tier::Semantic,+ frontmatter_json: serde_json::json!({}),+ pinned: false,+ links: Vec::new(),+ author_id: None,+ }+ }++ /// Enqueue a workspace creation without awaiting the reply, returning+ /// the oneshot receiver so the caller decides when to collect it.+ async fn enqueue_workspace(+ writer: &WriterHandle,+ name: &str,+ ) -> oneshot::Receiver<StoreResult<WorkspaceId>> {+ let (tx, rx) = oneshot::channel();+ writer+ .inner+ .tx+ .send(WriteCmd::GetOrCreateWorkspace {+ name: name.to_owned(),+ reply: tx,+ })+ .await+ .expect("writer channel closed while enqueueing");+ rx+ }++ /// Commands enqueued on the bounded channel run on the writer thread in+ /// FIFO order: insertion rowids must match enqueue order exactly.+ #[tokio::test]+ async fn executes_commands_in_fifo_order() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();++ let mut pending = Vec::new();+ for i in 0..8 {+ pending.push(enqueue_workspace(&store.writer, &format!("fifo-{i:02}")).await);+ }+ for rx in pending {+ rx.await.unwrap().unwrap();+ }++ let conn = Connection::open(store.db_path()).unwrap();+ let mut stmt = conn+ .prepare("SELECT name FROM workspaces WHERE name LIKE 'fifo-%' ORDER BY rowid")+ .unwrap();+ let names: Vec<String> = stmt+ .query_map([], |row| row.get(0))+ .unwrap()+ .collect::<Result<_, _>>()+ .unwrap();+ let expected: Vec<String> = (0..8).map(|i| format!("fifo-{i:02}")).collect();+ assert_eq!(+ names, expected,+ "single writer thread must apply commands in enqueue order"+ );+ }++ /// A command whose SQL fails returns the error on *its* oneshot only;+ /// the actor keeps processing — the next command succeeds.+ #[tokio::test]+ async fn sql_error_reaches_caller_without_killing_actor() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let ws = store+ .writer+ .get_or_create_workspace("default")+ .await+ .unwrap();+ let proj = store+ .writer+ .get_or_create_project(ws, "app", None)+ .await+ .unwrap();++ // FK violation: no project row exists for this id.+ let err = store+ .writer+ .upsert_page(sample_page(ws, ProjectId::new(), "notes/bogus.md", "x"))+ .await+ .unwrap_err();+ assert!(+ matches!(err, StoreError::Sqlite(_)),+ "expected the raw SQL failure to reach the caller, got {err:?}"+ );++ // The actor survived: a well-formed write lands afterwards.+ store+ .writer+ .upsert_page(sample_page(ws, proj, "notes/fine.md", "ok"))+ .await+ .unwrap();+ let conn = Connection::open(store.db_path()).unwrap();+ let count: i64 = conn+ .query_row("SELECT COUNT(*) FROM pages", [], |row| row.get(0))+ .unwrap();+ assert_eq!(count, 1, "only the post-error write may be present");+ }++ /// A batch command is one transaction: a failure part-way through rolls+ /// back the pages that already succeeded inside the same batch.+ #[tokio::test]+ async fn batch_upsert_rolls_back_on_mid_batch_failure() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let ws = store+ .writer+ .get_or_create_workspace("default")+ .await+ .unwrap();+ let proj = store+ .writer+ .get_or_create_project(ws, "app", None)+ .await+ .unwrap();++ let pages = vec![+ sample_page(ws, proj, "notes/good.md", "good"),+ // Second page references a project that does not exist.+ sample_page(ws, ProjectId::new(), "notes/bad.md", "bad"),+ ];+ let err = store.writer.upsert_pages_batch(pages).await.unwrap_err();+ assert!(+ matches!(err, StoreError::Sqlite(_)),+ "expected FK failure from the batch, got {err:?}"+ );++ let conn = Connection::open(store.db_path()).unwrap();+ let count: i64 = conn+ .query_row("SELECT COUNT(*) FROM pages", [], |row| row.get(0))+ .unwrap();+ assert_eq!(+ count, 0,+ "mid-batch failure must roll back the earlier page too"+ );++ // Actor is still alive after the rolled-back batch.+ store+ .writer+ .upsert_page(sample_page(ws, proj, "notes/after.md", "after"))+ .await+ .unwrap();+ }++ /// Dropping the last handle queues `Shutdown` behind the pending+ /// commands and joins the thread: every already-enqueued command is+ /// answered, and the join returning proves the thread exited (no hang).+ #[tokio::test]+ async fn drop_completes_pending_commands_and_joins_thread() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let writer = store.writer.clone();+ let db_path = store.db_path().to_owned();+ drop(store);++ let mut pending = Vec::new();+ for i in 0..8 {+ pending.push(enqueue_workspace(&writer, &format!("drain-{i:02}")).await);+ }+ // Joins the writer thread inside `Drop for WriterInner`; returns+ // only after the queue (including Shutdown) was drained.+ drop(writer);++ for rx in pending {+ let id = tokio::time::timeout(Duration::from_secs(10), rx)+ .await+ .expect("pending command was not answered before the writer stopped")+ .expect("reply oneshot dropped")+ .unwrap();+ let conn = Connection::open(&db_path).unwrap();+ let count: i64 = conn+ .query_row(+ "SELECT COUNT(*) FROM workspaces WHERE id = ?1",+ [id.as_bytes()],+ |row| row.get(0),+ )+ .unwrap();+ assert_eq!(count, 1, "pending command must have committed");+ }+ }++ /// Once the actor has stopped, calls fail fast with+ /// [`StoreError::WriterClosed`] instead of hanging on a dead channel.+ #[tokio::test]+ async fn commands_after_shutdown_return_writer_closed() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let writer = store.writer.clone();+ drop(store);++ writer+ .inner+ .tx+ .send(WriteCmd::Shutdown)+ .await+ .expect("writer channel closed before Shutdown");++ // FIFO guarantees Shutdown is processed first; whether the send+ // itself fails or the reply oneshot is dropped, the caller sees+ // WriterClosed either way.+ let err = writer+ .get_or_create_workspace("too-late")+ .await+ .unwrap_err();+ assert!(+ matches!(err, StoreError::WriterClosed),+ "expected WriterClosed, got {err:?}"+ );+ }+}diff --git a/crates/ai-memory-web/Cargo.toml b/crates/ai-memory-web/Cargo.tomlindex 8993bdf4..6d2eab88 100644--- a/crates/ai-memory-web/Cargo.toml+++ b/crates/ai-memory-web/Cargo.toml@@ -21,6 +21,8 @@ tracing.workspace = trueaxum.workspace = truejiff.workspace = truesha2.workspace = true+tower.workspace = true+tower-http.workspace = true# Templating + markdown.askama = "0.12"diff --git a/crates/ai-memory-web/src/lib.rs b/crates/ai-memory-web/src/lib.rsindex f0896893..efb01acf 100644--- a/crates/ai-memory-web/src/lib.rs+++ b/crates/ai-memory-web/src/lib.rs@@ -28,10 +28,15 @@ use ai_memory_wiki::Wiki;use axum::Router;mod markdown;+pub mod mount;mod routes;mod state;mod templates;+pub use mount::{+ WebMountSpec, inject_base_href, inject_base_path_meta, mount_web_router, normalize_prefix,+ web_base_href,+};pub use state::WebState;/// Build the read-only web router. Call once at server startup anddiff --git a/crates/ai-memory-web/src/mount.rs b/crates/ai-memory-web/src/mount.rsnew file mode 100644index 00000000..cd2b4655--- /dev/null+++ b/crates/ai-memory-web/src/mount.rs@@ -0,0 +1,1201 @@+//! Mounting orchestration for the `/api/v1` + web-UI HTTP surfaces.+//!+//! The host binary (`ai-memory serve`) builds its MCP/hook/admin router,+//! then hands it to [`mount_web_router`] to nest the JSON API and mount+//! either the operator's custom SPA (`--web-ui-dir`) or the built-in+//! server-rendered wiki browser. Base-path / web-slug normalisation and+//! `<base href>` injection live here too so the served HTML always+//! resolves relative URLs under the configured prefix.++use std::convert::Infallible;+use std::path::Path;+use std::sync::Arc;+use std::time::Duration;++use ai_memory_store::ReaderPool;+use ai_memory_wiki::Wiki;+use anyhow::{Context, Result};+use axum::body::Body;+use axum::extract::State;+use axum::http::{Method, Request, StatusCode, header};+use axum::middleware::Next;+use axum::response::{IntoResponse, Response};+use tower::service_fn;+use tower_http::cors::CorsLayer;+use tower_http::services::ServeDir;+use tracing::info;++/// 10 MB cap on response bodies buffered for `<base href>` injection.+/// Matches the inbound body cap the HTTP server applies; a custom-SPA+/// `index.html` over the cap is misconfigured at the operator level, and+/// refusing here keeps a runaway template (or a hostile asset+/// masquerading as text/html) from streaming unbounded into memory+/// before injection.+const MAX_INJECT_BODY_BYTES: usize = 10 * 1024 * 1024;++/// Normalise an operator-supplied path prefix into either `""` (root) or+/// `/<core>` — exactly one leading slash, no trailing slash, internal+/// empty/`//` segments collapsed.+///+/// Each segment must be a non-trivial member of the RFC 3986 *unreserved*+/// set (`ALPHA / DIGIT / "-" / "." / "_" / "~"`). Dot-segments `.` and+/// `..` are rejected outright even though their characters are unreserved+/// — at the segment level they re-encode "current directory" and "parent+/// directory" and would let a malformed env var hand the operator a+/// traversal vector through every URL the server emits.+///+/// Anything that falls outside the rule collapses to `""` (root) so a+/// bad env var can never inject markup or a protocol-relative `//` into+/// served HTML.+pub fn normalize_prefix(raw: &str) -> String {+ let segs: Vec<&str> = raw.trim().split('/').filter(|s| !s.is_empty()).collect();+ if segs.is_empty() {+ return String::new();+ }+ let safe = segs.iter().all(|s| {+ // Reject dot-segments (`.` / `..`) — they pass the per-char+ // unreserved test but mean "current" / "parent" at the segment+ // boundary and turn `/<base>` into `/<base>/..` traversal.+ *s != "." && *s != ".." && s.chars().all(is_unreserved_url_char)+ });+ if !safe {+ return String::new();+ }+ format!("/{}", segs.join("/"))+}++/// RFC 3986 `unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"`. Kept+/// here as a single source of truth for both the path-prefix charset+/// check and any future per-segment validation.+fn is_unreserved_url_char(c: char) -> bool {+ c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.' | '~')+}++/// Build the `<base href>` value (always trailing-slash-terminated, never+/// the protocol-relative `//`) for the web UI mounted at `base_path` ++/// `web_slug`.+pub fn web_base_href(base_path: &str, web_slug: &str) -> String {+ let combined = format!(+ "{}{}",+ normalize_prefix(base_path),+ normalize_prefix(web_slug)+ );+ if combined.is_empty() {+ "/".to_string()+ } else {+ format!("{combined}/")+ }+}++/// Escape a string for safe inclusion inside a double-quoted HTML attribute.+fn escape_attr(s: &str) -> String {+ s.replace('&', "&")+ .replace('"', """)+ .replace('<', "<")+ .replace('>', ">")+}++/// Insert `snippet` immediately after the first `<head…>` tag (or prepend+/// it when there is no head).+///+/// Matches outside HTML comments only: `<!-- … <head … --> <head>` skips+/// the comment-internal occurrence and injects after the real `<head>`.+/// Anything inside `<textarea>`, `<script>`, or other raw-text elements+/// is NOT specially handled — built-in askama templates never put+/// `<head` in those, and a custom `--web-ui-dir` SPA that does is a+/// misconfiguration the operator can fix at the source. Avoiding a+/// full HTML parser here keeps injection a single pass + alloc.+fn inject_into_head(html: &str, snippet: &str) -> String {+ let bytes = html.as_bytes();+ let mut cursor = 0usize;+ while cursor < bytes.len() {+ // Skip past any HTML comment opening here so a `<head` literal+ // sitting inside it cannot win the search.+ if html[cursor..].starts_with("<!--") {+ match html[cursor..].find("-->") {+ Some(end) => cursor += end + 3,+ None => break, // Unterminated comment — bail out of injection.+ }+ continue;+ }+ if html[cursor..].starts_with("<head")+ && let Some(gt) = html[cursor..].find('>')+ {+ let pos = cursor + gt + 1;+ let mut out = String::with_capacity(html.len() + snippet.len());+ out.push_str(&html[..pos]);+ out.push_str(snippet);+ out.push_str(&html[pos..]);+ return out;+ }+ cursor += 1;+ }+ format!("{snippet}{html}")+}++/// Inject `<base href="{href}">` so the served SPA's relative asset/router+/// URLs resolve under the configured prefix.+pub fn inject_base_href(html: &str, href: &str) -> String {+ inject_into_head(html, &format!("<base href=\"{}\">", escape_attr(href)))+}++/// Inject `<meta name="ai-memory-base-path" content="{base_path}">` so the+/// SPA can build API URLs as `{base_path}/api/v1`. The `<base href>` alone is+/// ambiguous for this because it also folds in the web slug (e.g. `/web`),+/// whereas `/api/v1` hangs off the base path, not the web mount.+pub fn inject_base_path_meta(html: &str, base_path: &str) -> String {+ inject_into_head(+ html,+ &format!(+ "<meta name=\"ai-memory-base-path\" content=\"{}\">",+ escape_attr(base_path)+ ),+ )+}++#[cfg(test)]+mod web_base_tests {+ use super::{inject_base_href, inject_base_path_meta, normalize_prefix, web_base_href};++ #[test]+ fn normalize_prefix_edge_cases() {+ assert_eq!(normalize_prefix(""), "");+ assert_eq!(normalize_prefix("/"), "");+ assert_eq!(normalize_prefix("//"), "");+ assert_eq!(normalize_prefix(" / "), "");+ assert_eq!(normalize_prefix("wiki"), "/wiki");+ assert_eq!(normalize_prefix("/wiki"), "/wiki");+ assert_eq!(normalize_prefix("/wiki/"), "/wiki");+ assert_eq!(normalize_prefix("//wiki//"), "/wiki");+ assert_eq!(normalize_prefix("/wiki/sub"), "/wiki/sub");+ // Unsafe chars fall back to root — never inject markup or `//`.+ assert_eq!(normalize_prefix("/wi\"ki"), "");+ assert_eq!(normalize_prefix("/wiki space"), "");+ assert_eq!(normalize_prefix("/<script>"), "");+ }++ /// Dot-segments must NOT survive normalisation. Their characters+ /// pass the unreserved per-char allowlist (`.` is unreserved), so+ /// the segment-level rejection is what stops `/..` and `/.` from+ /// turning the base prefix into a traversal vector. Regression+ /// guard — without this, `AI_MEMORY_BASE_PATH=/..` would serve+ /// `/..` and let an upstream redirect normalise it to `/`.+ #[test]+ fn normalize_prefix_rejects_dot_segments() {+ assert_eq!(normalize_prefix("/.."), "", "/.. must collapse to root");+ assert_eq!(normalize_prefix("/."), "", "/. must collapse to root");+ assert_eq!(+ normalize_prefix("/wiki/.."),+ "",+ "any embedded /.. fails the whole prefix"+ );+ assert_eq!(+ normalize_prefix("/wiki/./sub"),+ "",+ "any embedded /. fails the whole prefix"+ );+ // Segments that merely START with a dot but aren't pure+ // dot-segments are still valid (RFC 3986 unreserved chars).+ assert_eq!(normalize_prefix("/.wellknown"), "/.wellknown");+ }++ /// Nested base paths (`/a/b/c`) are valid; the normaliser keeps the+ /// hierarchy intact instead of collapsing to one level.+ #[test]+ fn normalize_prefix_keeps_nested_paths() {+ assert_eq!(normalize_prefix("/a/b/c"), "/a/b/c");+ assert_eq!(normalize_prefix("a/b/c"), "/a/b/c");+ assert_eq!(normalize_prefix("//a//b//c//"), "/a/b/c");+ assert_eq!(normalize_prefix("/a/b/c/d/e"), "/a/b/c/d/e");+ }++ /// `inject_into_head` must skip `<head` literals sitting inside an+ /// HTML comment, otherwise a custom SPA whose `index.html` had a+ /// `<!-- <head> placeholder -->` comment would have the snippet+ /// injected at the wrong place.+ #[test]+ fn inject_base_href_skips_head_inside_html_comment() {+ let html =+ "<!-- <head fake --><html><head><meta charset=\"utf-8\"></head><body></body></html>";+ let out = inject_base_href(html, "/w/");+ // Snippet must follow the REAL <head>, not the commented one —+ // the comment must remain unmodified.+ assert!(out.contains("<!-- <head fake -->"));+ assert!(out.contains("<head><base href=\"/w/\"><meta"));+ }++ /// `inject_into_head` falls back to the prepend path on an+ /// unterminated comment instead of looping forever (defensive).+ #[test]+ fn inject_base_href_on_unterminated_comment_falls_back_to_prepend() {+ let html = "<!-- never closes <head>";+ let out = inject_base_href(html, "/w/");+ assert!(out.starts_with("<base href=\"/w/\">"));+ }++ #[test]+ fn web_base_href_never_protocol_relative() {+ assert_eq!(web_base_href("", "/web"), "/web/");+ assert_eq!(web_base_href("/wiki", "/web"), "/wiki/web/");+ assert_eq!(web_base_href("/wiki", "/"), "/wiki/");+ assert_eq!(web_base_href("", "/"), "/");+ assert_eq!(web_base_href("/", "/"), "/");+ assert_eq!(web_base_href("/wiki/", "web"), "/wiki/web/");+ for (b, s) in [("", "/"), ("/", "/"), ("//", "//")] {+ assert!(!web_base_href(b, s).starts_with("//"));+ }+ }++ #[test]+ fn inject_base_href_after_head() {+ let html = "<!doctype html><html><head><meta charset=\"utf-8\"></head><body></body></html>";+ let out = inject_base_href(html, "/wiki/web/");+ assert!(out.contains("<head><base href=\"/wiki/web/\"><meta"));+ }++ #[test]+ fn inject_base_href_no_head_prepends() {+ let out = inject_base_href("<html></html>", "/x/");+ assert!(out.starts_with("<base href=\"/x/\"><html>"));+ }++ #[test]+ fn inject_base_href_escapes_attr() {+ let out = inject_base_href("<head></head>", "/a\"b/");+ assert!(out.contains("<base href=\"/a"b/\">"));+ }++ #[test]+ fn inject_base_path_meta_emits_meta() {+ let out = inject_base_path_meta("<head></head>", "/wiki");+ assert!(out.contains("<meta name=\"ai-memory-base-path\" content=\"/wiki\">"));+ // Empty base path => empty content (SPA falls back to root).+ let empty = inject_base_path_meta("<head></head>", "");+ assert!(empty.contains("content=\"\""));+ }+}++/// Path / URL config the web mount needs. Bundling these together+/// keeps `mount_web_router` and its helpers under clippy's+/// `too_many_arguments` threshold without `#[allow]` papering over+/// the call shape.+pub struct WebMountSpec<'a> {+ /// Operator-supplied custom SPA directory (`--web-ui-dir`). `None`+ /// mounts the built-in server-rendered wiki browser instead.+ pub web_ui_dir: Option<&'a Path>,+ /// Pre-validated CORS origins scoped to `/api/v1` only.+ pub cors_origins: &'a [String],+ /// Raw `--web-slug` value; normalised internally by the mount.+ pub web_slug: &'a str,+ /// `<base href>` value (base path + slug, trailing slash) injected+ /// into every served HTML page.+ pub base_href: &'a str,+ /// Normalised base path (`""` or `/<core>`) the whole surface is+ /// nested under; stamped into the `ai-memory-base-path` meta tag.+ pub base_path: &'a str,+}++/// Orchestrator: assemble the `/api/v1` + web-UI surfaces on top of+/// `router`. Skips everything when web is disabled. Each concern lives+/// in a dedicated helper below so this function reads as a four-step+/// recipe (CORS-scoped API, slug normalisation, SPA-vs-builtin choice,+/// final mount).+pub fn mount_web_router(+ router: axum::Router,+ enable_web: bool,+ reader: ReaderPool,+ wiki: Wiki,+ spec: WebMountSpec<'_>,+) -> Result<axum::Router> {+ if !enable_web {+ return Ok(router);+ }+ // Register the web surfaces BEFORE applying the bearer middleware. In+ // axum 0.8, `.layer()` only attaches to routes registered before the+ // call; nesting after the layer would silently bypass auth for /web/*.+ let router = router.nest(+ "/api/v1",+ build_api_router(&reader, &wiki, spec.cors_origins),+ );++ // Where the UI is mounted WITHIN the (already-applied) base path.+ // Empty slug => the UI is the root of the base path itself.+ let slug = normalize_prefix(spec.web_slug);+ let mount = if slug.is_empty() { "/" } else { slug.as_str() };++ // Custom SPA via --web-ui-dir (SPA fallback to index.html), otherwise+ // the built-in server-side wiki browser. In both cases the served+ // index carries an injected `<base href>` so relative asset/router+ // URLs resolve under `{base_path}{web_slug}`.+ if let Some(dir) = spec.web_ui_dir {+ return mount_custom_spa(router, dir, &slug, spec.base_href, spec.base_path, mount);+ }+ Ok(mount_builtin_browser(+ router,+ reader,+ wiki,+ &slug,+ spec.base_href,+ mount,+ ))+}++/// Build the `/api/v1` router and apply the per-origin CORS layer if+/// the operator configured any. The layer is scoped to this router only+/// (CORS_NOT_APPLIED_TO_OTHER_ROUTES invariant — `/mcp`, `/hook`,+/// `/admin`, and `/web` must remain CORS-free).+fn build_api_router(reader: &ReaderPool, wiki: &Wiki, cors_origins: &[String]) -> axum::Router {+ let api = crate::api_router(reader.clone(), wiki.clone());+ if cors_origins.is_empty() {+ return api;+ }+ // Origins were already validated before binding, so parsing here+ // is expected to succeed; `.expect` surfaces a logic bug if it does not.+ let parsed: Vec<axum::http::HeaderValue> = cors_origins+ .iter()+ .map(|o| o.parse().expect("pre-validated origin must parse"))+ .collect();+ let cors = CorsLayer::new()+ .allow_origin(parsed)+ .allow_methods([Method::GET, Method::POST, Method::OPTIONS])+ .allow_headers([header::AUTHORIZATION, header::CONTENT_TYPE])+ .allow_credentials(true)+ .max_age(Duration::from_secs(600));+ info!(origins = ?cors_origins, "CORS layer attached to /api/v1");+ api.layer(cors)+}++/// Mount the operator's custom SPA from `--web-ui-dir`. Reads+/// `index.html`, injects `<base href>` plus the `ai-memory-base-path`+/// meta, and serves the rest as static assets with an SPA fallback to+/// the injected shell. Errors surface as `anyhow` with the dir path.+fn mount_custom_spa(+ router: axum::Router,+ dir: &Path,+ slug: &str,+ base_href: &str,+ base_path: &str,+ mount: &str,+) -> Result<axum::Router> {+ let dir = dir.to_path_buf();+ let raw = std::fs::read_to_string(dir.join("index.html"))+ .with_context(|| format!("reading custom web UI index at {}", dir.display()))?;+ let injected = inject_base_path_meta(&inject_base_href(&raw, base_href), base_path);+ info!(mount, base_href, base_path, "custom web UI mounted");+ let spa = custom_spa_router(dir, injected.clone());+ Ok(if slug.is_empty() {+ router.merge(spa)+ } else {+ // `nest(slug, …)` routes `{slug}` (→ inner `/`) and `{slug}/<path>`+ // (→ inner `/{*path}`), but NOT the bare trailing-slash root+ // `{slug}/` — that empty sub-path matches neither, so it 404s. The+ // SPA router normalises its home to exactly that URL, so a refresh on+ // the app root returned a hard 404 (`custom_spa_trailing_slash_root_*`).+ // Serve the injected shell there too. Unlike the builtin browser —+ // which redirects `{slug}/` → `{slug}` — a SPA is happier staying put+ // on a 200 than bouncing through a redirect on every root refresh.+ let slash_index = Arc::new(injected);+ router+ .route(+ &format!("{slug}/"),+ axum::routing::get(move || {+ let body = slash_index.clone();+ async move { axum::response::Html((*body).clone()) }+ }),+ )+ .nest(slug, spa)+ })+}++/// Mount the built-in server-rendered wiki browser at `slug` with+/// `<base href>` injection middleware. When `slug` is non-empty also+/// register the trailing-slash → canonical redirect, preserving any+/// query string the caller passed.+fn mount_builtin_browser(+ router: axum::Router,+ reader: ReaderPool,+ wiki: Wiki,+ slug: &str,+ base_href: &str,+ mount: &str,+) -> axum::Router {+ // The built-in browser emits RELATIVE asset/link URLs (`static/…`,+ // `w/…`, `search`, `.`). Inject a `<base href>` into every HTML+ // response so they resolve under `{base_path}{web_slug}/` — the+ // same anchoring the custom SPA gets via its injected index.+ let web_router = crate::router(reader, wiki).layer(axum::middleware::from_fn_with_state(+ Arc::new(base_href.to_string()),+ inject_web_base_href,+ ));+ info!(mount, base_href, "read-only wiki browser mounted");+ if slug.is_empty() {+ return router.merge(web_router);+ }+ // Strip-trailing-slash redirect. The target must carry the full+ // external prefix because the surrounding `nest(&base_path, …)`+ // does NOT rewrite Location headers. Derive it from base_href+ // (which already folds in base_path + slug). The closure takes+ // `Uri` so `?q=x` survives — see+ // `trailing_slash_redirect_preserves_query_string`.+ let canonical = {+ let trimmed = base_href.trim_end_matches('/');+ if trimmed.is_empty() {+ "/".to_string()+ } else {+ trimmed.to_string()+ }+ };+ router+ .route(+ &format!("{slug}/"),+ axum::routing::get(move |uri: axum::http::Uri| {+ let to = match uri.query() {+ Some(q) if !q.is_empty() => format!("{canonical}?{q}"),+ _ => canonical.clone(),+ };+ async move { axum::response::Redirect::permanent(&to) }+ }),+ )+ .nest(slug, web_router)+}++fn custom_spa_router(dir: std::path::PathBuf, injected_index: String) -> axum::Router {+ let index = Arc::new(injected_index);+ let root_index = index.clone();+ let direct_index = index.clone();+ let fallback_index = index.clone();++ // Assets are served as files; any missing asset path falls back to the+ // injected index for SPA client routes. Direct `/index.html` is routed+ // explicitly so it cannot bypass injection by being served from disk.+ let assets = ServeDir::new(dir)+ .append_index_html_on_directories(false)+ .fallback(service_fn(move |_req: Request<Body>| {+ let body = fallback_index.clone();+ async move {+ Ok::<_, Infallible>(axum::response::Html((*body).clone()).into_response())+ }+ }));++ axum::Router::new()+ .route(+ "/",+ axum::routing::get(move || {+ let body = root_index.clone();+ async move { axum::response::Html((*body).clone()) }+ }),+ )+ .route(+ "/index.html",+ axum::routing::get(move || {+ let body = direct_index.clone();+ async move { axum::response::Html((*body).clone()) }+ }),+ )+ .route_service("/{*path}", assets)+}++/// Response middleware: inject `<base href>` into `text/html` responses from+/// the built-in server-rendered web browser, so its relative URLs resolve+/// under the configured `{base_path}{web_slug}` prefix. Non-HTML responses+/// (static assets, redirects) pass through untouched.+async fn inject_web_base_href(+ State(base_href): State<Arc<String>>,+ req: Request<Body>,+ next: Next,+) -> Response {+ let resp = next.run(req).await;+ let is_html = resp+ .headers()+ .get(header::CONTENT_TYPE)+ .and_then(|v| v.to_str().ok())+ .is_some_and(|ct| ct.starts_with("text/html"));+ if !is_html {+ return resp;+ }+ let (mut parts, body) = resp.into_parts();+ // Bound the buffer at MAX_INJECT_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_INJECT_BODY_BYTES).await {+ Ok(b) => b,+ Err(_) => {+ return (+ StatusCode::INTERNAL_SERVER_ERROR,+ "response body too large or read error\n",+ )+ .into_response();+ }+ };+ match std::str::from_utf8(&bytes) {+ Ok(html) => {+ let injected = inject_base_href(html, &base_href);+ // Stale length from the pre-injection body; let hyper recompute.+ parts.headers.remove(header::CONTENT_LENGTH);+ Response::from_parts(parts, Body::from(injected))+ }+ Err(_) => Response::from_parts(parts, Body::from(bytes)),+ }+}++#[cfg(test)]+mod tests {+ use super::*;+ use ai_memory_store::Store;+ use tempfile::TempDir;+ use tower::ServiceExt;+ /// Assemble the web/API surface under `base_path` + `web_slug` exactly the+ /// way the `serve` handler does (mount, then `nest(&base_path, …)`), with+ /// no auth/host layers so tests probe routing + injection in isolation.+ /// Returns the `TempDir` guard too — the caller must keep it alive for the+ /// router's lifetime (the store's SQLite + wiki files live under it).+ fn based_web_router(base_path: &str, web_slug: &str) -> (TempDir, axum::Router) {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let wiki = Wiki::new(tmp.path(), store.writer.clone()).unwrap();+ let base = normalize_prefix(base_path);+ let base_href = web_base_href(base_path, web_slug);+ let router = mount_web_router(+ axum::Router::new(),+ true,+ store.reader.clone(),+ wiki,+ WebMountSpec {+ web_ui_dir: None,+ cors_origins: &[],+ web_slug,+ base_href: &base_href,+ base_path: &base,+ },+ )+ .unwrap();+ let router = if base.is_empty() {+ router+ } else {+ axum::Router::new().nest(&base, router)+ };+ // Mirror the production favicon mount in serve handler: at the+ // absolute host root, outside the base-path nest.+ let router = router.merge(crate::favicon_router());+ (tmp, router)+ }++ #[tokio::test]+ async fn base_path_nests_all_surfaces_and_root_404s() {+ let (_tmp, router) = based_web_router("/wiki", "/web");++ // The web UI is reachable UNDER the prefix…+ let under = router+ .clone()+ .oneshot(+ Request::builder()+ .uri("/wiki/web")+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap();+ assert_eq!(under.status(), StatusCode::OK);++ // …and the API too.+ let api = router+ .clone()+ .oneshot(+ Request::builder()+ .uri("/wiki/api/v1/projects")+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap();+ assert_eq!(api.status(), StatusCode::OK);++ // The same paths at the host ROOT must 404 — nothing leaks outside the+ // prefix (the whole point of base-path hosting behind a shared proxy).+ for uri in ["/web", "/api/v1/projects"] {+ let root = router+ .clone()+ .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())+ .await+ .unwrap();+ assert_eq!(+ root.status(),+ StatusCode::NOT_FOUND,+ "{uri} must 404 at root"+ );+ }+ }++ #[tokio::test]+ async fn inject_web_base_href_targets_html_only() {+ let (_tmp, router) = based_web_router("/wiki", "/web");++ // HTML response carries the injected <base href> under the prefix.+ let html_resp = router+ .clone()+ .oneshot(+ Request::builder()+ .uri("/wiki/web")+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap();+ assert_eq!(html_resp.status(), StatusCode::OK);+ let html = axum::body::to_bytes(html_resp.into_body(), usize::MAX)+ .await+ .unwrap();+ let html = std::str::from_utf8(&html).unwrap();+ assert!(+ html.contains(r#"<base href="/wiki/web/">"#),+ "expected injected base href, got: {html}"+ );++ // A non-HTML asset passes through untouched (no <base> smuggled in).+ let css_resp = router+ .clone()+ .oneshot(+ Request::builder()+ .uri("/wiki/web/static/tailwind.css")+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap();+ assert_eq!(css_resp.status(), StatusCode::OK);+ let css = axum::body::to_bytes(css_resp.into_body(), usize::MAX)+ .await+ .unwrap();+ assert!(+ !std::str::from_utf8(&css).unwrap().contains("<base href"),+ "non-HTML asset must not receive a <base href> injection"+ );+ }++ #[tokio::test]+ async fn trailing_slash_redirect_carries_the_prefix() {+ let (_tmp, router) = based_web_router("/wiki", "/web");+ let resp = router+ .oneshot(+ Request::builder()+ .uri("/wiki/web/")+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap();+ assert_eq!(resp.status(), StatusCode::PERMANENT_REDIRECT);+ // Location must include the external prefix — the surrounding base nest+ // does NOT rewrite Location headers, so a bare `/web` would drop `/wiki`.+ assert_eq!(resp.headers().get(header::LOCATION).unwrap(), "/wiki/web",);+ }++ /// Trailing-slash redirect must preserve the query string. The+ /// original handler took `()` and silently dropped `?q=x`, so a+ /// link in the SPA that appended a filter param round-tripped to+ /// the canonical URL with the param missing. Fragments are+ /// client-only and never reach the server, so we only assert query.+ #[tokio::test]+ async fn trailing_slash_redirect_preserves_query_string() {+ let (_tmp, router) = based_web_router("/wiki", "/web");+ let resp = router+ .oneshot(+ Request::builder()+ .uri("/wiki/web/?q=foo&limit=5")+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap();+ assert_eq!(resp.status(), StatusCode::PERMANENT_REDIRECT);+ assert_eq!(+ resp.headers().get(header::LOCATION).unwrap(),+ "/wiki/web?q=foo&limit=5",+ "redirect must carry the original query, not drop it"+ );+ }++ /// Nested base paths (`/a/b/c`) — exercised by the normaliser+ /// unit test but not previously end-to-end. Routes must be+ /// reachable under the full prefix and 404 at any shorter prefix.+ #[tokio::test]+ async fn nested_base_path_nests_web_and_api() {+ let (_tmp, router) = based_web_router("/a/b/c", "/web");+ // Full nested prefix reaches both surfaces.+ for uri in ["/a/b/c/web", "/a/b/c/api/v1/projects"] {+ let resp = router+ .clone()+ .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())+ .await+ .unwrap();+ assert_eq!(+ resp.status(),+ StatusCode::OK,+ "{uri} must reach the surface"+ );+ }+ // A SHORTER prefix (e.g. only /a/b) must NOT leak — the nest is+ // exactly `/a/b/c` and any partial mount is unmapped.+ for uri in ["/a/b/web", "/a/api/v1/projects"] {+ let resp = router+ .clone()+ .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())+ .await+ .unwrap();+ assert_eq!(+ resp.status(),+ StatusCode::NOT_FOUND,+ "{uri} must 404 — leaks the prefix otherwise"+ );+ }+ }++ /// Custom-SPA index that already carries its own `<base href>` —+ /// today we prepend another one; HTML5 says the browser honours+ /// the FIRST `<base>` so the injection becomes a silent no-op.+ /// Documented behaviour (see `inject_into_head` doc-comment) but+ /// also exercised here so a future change to "replace existing"+ /// gets noticed by a failing test rather than silently shipping.+ #[test]+ fn inject_base_href_with_existing_base_tag_does_not_replace() {+ let html = "<html><head><base href=\"/old/\"><title>x</title></head></html>";+ let out = inject_base_href(html, "/new/");+ // Injected snippet appears first, the old <base> remains too.+ let new_pos = out.find("<base href=\"/new/\">").expect("new injected");+ let old_pos = out.find("<base href=\"/old/\">").expect("old preserved");+ assert!(+ new_pos < old_pos,+ "injected base must appear before the pre-existing one (browser ignores duplicates after the first)"+ );+ }++ /// Post-merge audit (Phase 7 live test) caught that PR #79's+ /// `/favicon.ico` route was nested inside `/web`, so it lived at+ /// `/web/favicon.ico` and the browser's automatic root fetch always+ /// 404'd. Fix: a separate `favicon_router()` mounted at the absolute+ /// HOST root, outside `--base-path` and outside the `/web` nest.+ /// This test pins both:+ /// * `/favicon.ico` at the host root returns the PNG.+ /// * Under `--base-path /wiki`, the favicon STAYS at root —+ /// browsers fetch `<host>/favicon.ico` regardless of where the+ /// app is mounted; the route must not move with the prefix.+ #[tokio::test]+ async fn favicon_lives_at_host_root_regardless_of_base_path() {+ for base_path in ["", "/wiki"] {+ let (_tmp, router) = based_web_router(base_path, "/web");+ let resp = router+ .oneshot(+ Request::builder()+ .uri("/favicon.ico")+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap();+ assert_eq!(+ resp.status(),+ StatusCode::OK,+ "/favicon.ico must be reachable at host root (base_path={base_path:?})"+ );+ assert_eq!(+ resp.headers()+ .get(axum::http::header::CONTENT_TYPE)+ .and_then(|v| v.to_str().ok()),+ Some("image/png"),+ );+ }+ }++ #[tokio::test]+ async fn no_base_path_is_byte_equivalent_at_root() {+ let (_tmp, router) = based_web_router("", "/web");+ let resp = router+ .clone()+ .oneshot(Request::builder().uri("/web").body(Body::empty()).unwrap())+ .await+ .unwrap();+ assert_eq!(resp.status(), StatusCode::OK);+ let body = axum::body::to_bytes(resp.into_body(), usize::MAX)+ .await+ .unwrap();+ assert!(+ std::str::from_utf8(&body)+ .unwrap()+ .contains(r#"<base href="/web/">"#),+ "default mount should inject the root-relative base href"+ );+ }++ #[tokio::test]+ async fn custom_spa_index_routes_are_injected_under_base_path() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let wiki = Wiki::new(tmp.path(), store.writer.clone()).unwrap();+ let ui = TempDir::new().unwrap();+ std::fs::write(+ ui.path().join("index.html"),+ "<!doctype html><html><head><title>spa</title></head><body>shell</body></html>",+ )+ .unwrap();+ std::fs::write(ui.path().join("app.js"), "console.log('asset');").unwrap();++ let base = normalize_prefix("/wiki");+ let base_href = web_base_href("/wiki", "/web");+ let router = mount_web_router(+ axum::Router::new(),+ true,+ store.reader.clone(),+ wiki,+ WebMountSpec {+ web_ui_dir: Some(ui.path()),+ cors_origins: &[],+ web_slug: "/web",+ base_href: &base_href,+ base_path: &base,+ },+ )+ .unwrap();+ let router = axum::Router::new().nest(&base, router);++ for uri in [+ "/wiki/web",+ "/wiki/web/", // trailing-slash root: SPA home after router normalises it+ "/wiki/web/index.html",+ "/wiki/web/client/route",+ ] {+ let resp = router+ .clone()+ .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())+ .await+ .unwrap();+ assert_eq!(resp.status(), StatusCode::OK, "{uri}");+ let body = axum::body::to_bytes(resp.into_body(), usize::MAX)+ .await+ .unwrap();+ let html = std::str::from_utf8(&body).unwrap();+ assert!(+ html.contains(r#"<base href="/wiki/web/">"#),+ "{uri} must receive injected base href: {html}"+ );+ assert!(+ html.contains(r#"<meta name="ai-memory-base-path" content="/wiki">"#),+ "{uri} must receive injected API base-path meta: {html}"+ );+ assert!(html.contains("shell"), "{uri} returns the SPA shell");+ }++ let asset = router+ .oneshot(+ Request::builder()+ .uri("/wiki/web/app.js")+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap();+ assert_eq!(asset.status(), StatusCode::OK);+ let body = axum::body::to_bytes(asset.into_body(), usize::MAX)+ .await+ .unwrap();+ let js = std::str::from_utf8(&body).unwrap();+ assert_eq!(js, "console.log('asset');");+ }++ /// Regression for the prod 404: with `base_path=""` (host root) the custom+ /// SPA mounts at slug `/web`. `nest("/web", …)` served `/web` and+ /// `/web/<route>` but left the bare trailing-slash root `/web/` unrouted →+ /// hard 404. The SPA normalises its home to exactly `/web/`, so refreshing+ /// the app root broke (both a host-root deploy and one mounted under a base+ /// path like `/wiki`).+ #[tokio::test]+ async fn custom_spa_trailing_slash_root_serves_shell() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let wiki = Wiki::new(tmp.path(), store.writer.clone()).unwrap();+ let ui = TempDir::new().unwrap();+ std::fs::write(+ ui.path().join("index.html"),+ "<!doctype html><html><head><title>spa</title></head><body>shell</body></html>",+ )+ .unwrap();++ let base_href = web_base_href("", "/web");+ let router = mount_web_router(+ axum::Router::new(),+ true,+ store.reader.clone(),+ wiki,+ WebMountSpec {+ web_ui_dir: Some(ui.path()),+ cors_origins: &[],+ web_slug: "/web",+ base_href: &base_href,+ base_path: "",+ },+ )+ .unwrap();++ // `/web`, the trailing-slash root `/web/`, and a deep client route all+ // serve the injected shell — none may 404.+ for uri in ["/web", "/web/", "/web/projects/x"] {+ let resp = router+ .clone()+ .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())+ .await+ .unwrap();+ assert_eq!(+ resp.status(),+ StatusCode::OK,+ "{uri} must serve the SPA shell, not 404"+ );+ let body = axum::body::to_bytes(resp.into_body(), usize::MAX)+ .await+ .unwrap();+ let html = std::str::from_utf8(&body).unwrap();+ assert!(+ html.contains("shell"),+ "{uri} returns the SPA shell: {html}"+ );+ assert!(+ html.contains(r#"<base href="/web/">"#),+ "{uri} must receive the injected base href: {html}"+ );+ }+ }++ #[tokio::test]+ async fn custom_spa_root_slug_does_not_shadow_api_routes() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let wiki = Wiki::new(tmp.path(), store.writer.clone()).unwrap();+ let ui = TempDir::new().unwrap();+ std::fs::write(+ ui.path().join("index.html"),+ "<!doctype html><html><head><title>spa</title></head><body>root shell</body></html>",+ )+ .unwrap();+ std::fs::write(ui.path().join("app.js"), "console.log('root asset');").unwrap();++ let base = normalize_prefix("/wiki");+ let base_href = web_base_href("/wiki", "/");+ let router = mount_web_router(+ axum::Router::new(),+ true,+ store.reader.clone(),+ wiki,+ WebMountSpec {+ web_ui_dir: Some(ui.path()),+ cors_origins: &[],+ web_slug: "/",+ base_href: &base_href,+ base_path: &base,+ },+ )+ .unwrap();+ let router = axum::Router::new().nest(&base, router);++ for uri in ["/wiki", "/wiki/index.html", "/wiki/client/route"] {+ let resp = router+ .clone()+ .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())+ .await+ .unwrap();+ assert_eq!(resp.status(), StatusCode::OK, "{uri}");+ let body = axum::body::to_bytes(resp.into_body(), usize::MAX)+ .await+ .unwrap();+ let html = std::str::from_utf8(&body).unwrap();+ assert!(+ html.contains(r#"<base href="/wiki/">"#),+ "{uri} must receive injected base href: {html}"+ );+ assert!(+ html.contains(r#"<meta name="ai-memory-base-path" content="/wiki">"#),+ "{uri} must receive injected API base-path meta: {html}"+ );+ assert!(html.contains("root shell"), "{uri} returns the SPA shell");+ }++ let api = router+ .clone()+ .oneshot(+ Request::builder()+ .uri("/wiki/api/v1/projects")+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap();+ assert_eq!(api.status(), StatusCode::OK);+ let content_type = api+ .headers()+ .get(header::CONTENT_TYPE)+ .and_then(|h| h.to_str().ok())+ .unwrap_or_default();+ assert!(+ content_type.starts_with("application/json"),+ "API route must not be shadowed by the root SPA: {content_type}"+ );++ let asset = router+ .oneshot(+ Request::builder()+ .uri("/wiki/app.js")+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap();+ assert_eq!(asset.status(), StatusCode::OK);+ let body = axum::body::to_bytes(asset.into_body(), usize::MAX)+ .await+ .unwrap();+ let js = std::str::from_utf8(&body).unwrap();+ assert_eq!(js, "console.log('root asset');");+ }+ #[tokio::test]+ async fn cors_layer_on_api_v1_allows_configured_origin() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let wiki = Wiki::new(tmp.path(), store.writer.clone()).unwrap();+ store+ .writer+ .get_or_create_workspace("default")+ .await+ .unwrap();++ let cors_origins = ["https://app.example.com".to_string()];+ let router = mount_web_router(+ axum::Router::new(),+ true,+ store.reader.clone(),+ wiki,+ WebMountSpec {+ web_ui_dir: None,+ cors_origins: &cors_origins,+ web_slug: "/web",+ base_href: "/web/",+ base_path: "",+ },+ )+ .unwrap();+ // No auth layer so we can reach /api/v1 directly.+ let resp = router+ .oneshot(+ Request::builder()+ .uri("/api/v1/workspaces")+ .header("Origin", "https://app.example.com")+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap();++ assert_eq!(resp.status(), StatusCode::OK);+ let acao = resp+ .headers()+ .get("access-control-allow-origin")+ .expect("ACAO header must be present for allowed origin")+ .to_str()+ .unwrap();+ assert_eq!(acao, "https://app.example.com");+ let acac = resp+ .headers()+ .get("access-control-allow-credentials")+ .expect("ACAC header must be present")+ .to_str()+ .unwrap();+ assert_eq!(acac, "true");+ }++ #[tokio::test]+ async fn cors_layer_on_api_v1_denies_unlisted_origin() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let wiki = Wiki::new(tmp.path(), store.writer.clone()).unwrap();+ store+ .writer+ .get_or_create_workspace("default")+ .await+ .unwrap();++ let cors_origins = ["https://app.example.com".to_string()];+ let router = mount_web_router(+ axum::Router::new(),+ true,+ store.reader.clone(),+ wiki,+ WebMountSpec {+ web_ui_dir: None,+ cors_origins: &cors_origins,+ web_slug: "/web",+ base_href: "/web/",+ base_path: "",+ },+ )+ .unwrap();+ let resp = router+ .oneshot(+ Request::builder()+ .uri("/api/v1/workspaces")+ .header("Origin", "https://evil.example.com")+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap();++ // The request is still served (CORS does not block on the server side),+ // but the ACAO header must be absent so the browser enforces the policy.+ assert!(+ resp.headers().get("access-control-allow-origin").is_none(),+ "unlisted origin must not receive ACAO header"+ );+ }++ #[tokio::test]+ async fn cors_not_applied_to_other_routes() {+ // /mcp and /admin routes must not carry CORS headers even when+ // a CORS origin list is configured (CORS_NOT_APPLIED_TO_OTHER_ROUTES+ // invariant). We verify by checking that a request to a non-/api/v1+ // path that 404s (no actual handler mounted here) still lacks ACAO.+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let wiki = Wiki::new(tmp.path(), store.writer.clone()).unwrap();++ let cors_origins = ["https://app.example.com".to_string()];+ let router = mount_web_router(+ axum::Router::new(),+ true,+ store.reader.clone(),+ wiki,+ WebMountSpec {+ web_ui_dir: None,+ cors_origins: &cors_origins,+ web_slug: "/web",+ base_href: "/web/",+ base_path: "",+ },+ )+ .unwrap();+ // /web is a non-api route; sending an Origin header must not trigger CORS.+ let resp = router+ .oneshot(+ Request::builder()+ .uri("/web")+ .header("Origin", "https://app.example.com")+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap();++ assert!(+ resp.headers().get("access-control-allow-origin").is_none(),+ "/web must not carry CORS headers: {:?}",+ resp.headers()+ );+ }+}diff --git a/crates/ai-memory-wiki/src/lib.rs b/crates/ai-memory-wiki/src/lib.rsindex bdd92dcf..69a9d8bd 100644--- a/crates/ai-memory-wiki/src/lib.rs+++ b/crates/ai-memory-wiki/src/lib.rs@@ -18,6 +18,7 @@ pub use admission::{AdmissionChain, AdmissionContext, AdmissionOp, FailurePolicy, MAX_ADMISSION_WEBHOOKS,MAX_RESPONSE_BYTES, WebhookConfig,};+pub use atomic::write_atomic;pub use error::{WikiError, WikiResult};pub use git::{COMMIT_AUTHOR_EMAIL, COMMIT_AUTHOR_NAME, GitAdapter};pub use markdown::{Markdown, derive_title, emit, parse};