use axum::{
Json,
extract::{FromRef, Path, Query, Request, State},
http::{HeaderMap, StatusCode},
middleware::Next,
response::IntoResponse,
};
use chrono::{Duration, Utc};
use serde::Deserialize;
use serde_json::json;
use std::sync::Arc;
use tokio::sync::Mutex;
use uuid::Uuid;
use crate::config::ResolvedTtl;
use crate::db;
use crate::embeddings::Embedder;
use crate::hnsw::VectorIndex;
use crate::models::{
CreateMemory, ForgetQuery, LinkBody, ListQuery, Memory, MemoryLink, RecallBody, RecallQuery,
RegisterAgentBody, SearchQuery, Tier, UpdateMemory,
};
use crate::validate;
pub type Db = Arc<Mutex<(rusqlite::Connection, std::path::PathBuf, ResolvedTtl, bool)>>;
#[derive(Clone)]
pub struct AppState {
pub db: Db,
pub embedder: Arc<Option<Embedder>>,
pub vector_index: Arc<Mutex<Option<VectorIndex>>>,
pub federation: Arc<Option<crate::federation::FederationConfig>>,
}
impl FromRef<AppState> for Db {
fn from_ref(app: &AppState) -> Self {
app.db.clone()
}
}
const MAX_BULK_SIZE: usize = 1000;
#[derive(Clone)]
pub struct ApiKeyState {
pub key: Option<String>,
}
#[inline]
fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
if a.len() != b.len() {
return false;
}
let mut diff: u8 = 0;
for (x, y) in a.iter().zip(b.iter()) {
diff |= x ^ y;
}
diff == 0
}
pub async fn api_key_auth(
State(auth): State<ApiKeyState>,
req: Request,
next: Next,
) -> impl IntoResponse {
let Some(ref expected) = auth.key else {
return next.run(req).await.into_response();
};
if req.uri().path() == "/api/v1/health" {
return next.run(req).await.into_response();
}
if let Some(header_val) = req.headers().get("x-api-key")
&& let Ok(val) = header_val.to_str()
&& constant_time_eq(val.as_bytes(), expected.as_bytes())
{
return next.run(req).await.into_response();
}
if let Some(query) = req.uri().query() {
for pair in query.split('&') {
if let Some(val) = pair.strip_prefix("api_key=")
&& constant_time_eq(val.as_bytes(), expected.as_bytes())
{
return next.run(req).await.into_response();
}
}
}
(
StatusCode::UNAUTHORIZED,
Json(json!({"error": "missing or invalid API key"})),
)
.into_response()
}
pub async fn health(State(state): State<Db>) -> impl IntoResponse {
let lock = state.lock().await;
let ok = db::health_check(&lock.0).unwrap_or(false);
let code = if ok {
StatusCode::OK
} else {
StatusCode::SERVICE_UNAVAILABLE
};
(
code,
Json(json!({"status": if ok { "ok" } else { "error" }, "service": "ai-memory"})),
)
.into_response()
}
pub async fn prometheus_metrics(State(state): State<Db>) -> impl IntoResponse {
{
let lock = state.lock().await;
if let Ok(stats) = db::stats(&lock.0, &lock.1) {
crate::metrics::registry()
.memories_gauge
.set(stats.total.try_into().unwrap_or(i64::MAX));
}
}
let body = crate::metrics::render();
(
StatusCode::OK,
[(
axum::http::header::CONTENT_TYPE,
"text/plain; version=0.0.4; charset=utf-8",
)],
body,
)
.into_response()
}
#[allow(clippy::too_many_lines)]
pub async fn create_memory(
State(app): State<AppState>,
headers: HeaderMap,
Json(body): Json<CreateMemory>,
) -> impl IntoResponse {
let state = app.db.clone();
if let Err(e) = validate::validate_create(&body) {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": e.to_string()})),
)
.into_response();
}
let header_agent_id = headers.get("x-agent-id").and_then(|v| v.to_str().ok());
let agent_id =
match crate::identity::resolve_http_agent_id(body.agent_id.as_deref(), header_agent_id) {
Ok(id) => id,
Err(e) => {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": format!("invalid agent_id: {e}")})),
)
.into_response();
}
};
let mut metadata = body.metadata;
if let Some(obj) = metadata.as_object_mut() {
obj.insert("agent_id".to_string(), serde_json::Value::String(agent_id));
}
if let Some(ref s) = body.scope {
if let Err(e) = validate::validate_scope(s) {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": e.to_string()})),
)
.into_response();
}
if let Some(obj) = metadata.as_object_mut() {
obj.insert("scope".to_string(), serde_json::Value::String(s.clone()));
}
}
let embedding_text = format!("{} {}", body.title, body.content);
let embedding: Option<Vec<f32>> =
app.embedder
.as_ref()
.as_ref()
.and_then(|emb| match emb.embed(&embedding_text) {
Ok(v) => Some(v),
Err(e) => {
tracing::warn!("embedding generation failed: {e}");
None
}
});
let now = Utc::now();
let lock = state.lock().await;
let expires_at = body.expires_at.or_else(|| {
body.ttl_secs
.or(lock.2.ttl_for_tier(&body.tier))
.map(|s| (now + Duration::seconds(s)).to_rfc3339())
});
let mem = Memory {
id: Uuid::new_v4().to_string(),
tier: body.tier,
namespace: body.namespace,
title: body.title,
content: body.content,
tags: body.tags,
priority: body.priority.clamp(1, 10),
confidence: body.confidence.clamp(0.0, 1.0),
source: body.source,
access_count: 0,
created_at: now.to_rfc3339(),
updated_at: now.to_rfc3339(),
last_accessed_at: None,
expires_at,
metadata,
};
{
use crate::models::{GovernanceDecision, GovernedAction};
let agent_for_gov = mem
.metadata
.get("agent_id")
.and_then(|v| v.as_str())
.unwrap_or_default()
.to_string();
let payload = serde_json::to_value(&mem).unwrap_or_default();
match db::enforce_governance(
&lock.0,
GovernedAction::Store,
&mem.namespace,
&agent_for_gov,
None,
None,
&payload,
) {
Ok(GovernanceDecision::Allow) => {}
Ok(GovernanceDecision::Deny(reason)) => {
return (
StatusCode::FORBIDDEN,
Json(json!({"error": format!("store denied by governance: {reason}")})),
)
.into_response();
}
Ok(GovernanceDecision::Pending(pending_id)) => {
return (
StatusCode::ACCEPTED,
Json(json!({
"status": "pending",
"pending_id": pending_id,
"reason": "governance requires approval",
"action": "store",
"namespace": mem.namespace,
})),
)
.into_response();
}
Err(e) => {
tracing::error!("governance error: {e}");
return (
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "governance check failed"})),
)
.into_response();
}
}
}
let contradictions =
db::find_contradictions(&lock.0, &mem.title, &mem.namespace).unwrap_or_default();
let contradiction_ids: Vec<String> = contradictions
.iter()
.filter(|c| c.id != mem.id)
.map(|c| c.id.clone())
.collect();
match db::insert(&lock.0, &mem) {
Ok(actual_id) => {
if let Some(ref vec) = embedding
&& let Err(e) = db::set_embedding(&lock.0, &actual_id, vec)
{
tracing::warn!("failed to store embedding for {actual_id}: {e}");
}
drop(lock);
if let Some(vec) = embedding {
let mut idx_lock = app.vector_index.lock().await;
if let Some(idx) = idx_lock.as_mut() {
idx.insert(actual_id.clone(), vec);
}
}
let resolved_agent_id = mem
.metadata
.get("agent_id")
.and_then(|v| v.as_str())
.map(str::to_string);
let mut response = json!({
"id": actual_id,
"tier": mem.tier,
"namespace": mem.namespace,
"title": mem.title,
"agent_id": resolved_agent_id,
});
if !contradiction_ids.is_empty() {
response["potential_contradictions"] = json!(contradiction_ids);
}
if let Some(fed) = app.federation.as_ref() {
let mut mem_echo = mem.clone();
mem_echo.id = actual_id.clone();
match crate::federation::broadcast_store_quorum(fed, &mem_echo).await {
Ok(tracker) => match crate::federation::finalise_quorum(&tracker) {
Ok(got) => {
response["quorum_acks"] = json!(got);
return (StatusCode::CREATED, Json(response)).into_response();
}
Err(err) => {
let payload = crate::federation::QuorumNotMetPayload::from_err(&err);
return (
StatusCode::SERVICE_UNAVAILABLE,
[("Retry-After", "2")],
Json(serde_json::to_value(&payload).unwrap_or_default()),
)
.into_response();
}
},
Err(err) => {
let payload = crate::federation::QuorumNotMetPayload::from_err(&err);
return (
StatusCode::SERVICE_UNAVAILABLE,
[("Retry-After", "2")],
Json(serde_json::to_value(&payload).unwrap_or_default()),
)
.into_response();
}
}
}
(StatusCode::CREATED, Json(response)).into_response()
}
Err(e) => {
tracing::error!("handler error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
pub async fn register_agent(
State(state): State<Db>,
Json(body): Json<RegisterAgentBody>,
) -> impl IntoResponse {
if let Err(e) = validate::validate_agent_id(&body.agent_id) {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": e.to_string()})),
)
.into_response();
}
if let Err(e) = validate::validate_agent_type(&body.agent_type) {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": e.to_string()})),
)
.into_response();
}
let capabilities = body.capabilities.unwrap_or_default();
if let Err(e) = validate::validate_capabilities(&capabilities) {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": e.to_string()})),
)
.into_response();
}
let lock = state.lock().await;
match db::register_agent(&lock.0, &body.agent_id, &body.agent_type, &capabilities) {
Ok(id) => (
StatusCode::CREATED,
Json(json!({
"registered": true,
"id": id,
"agent_id": body.agent_id,
"agent_type": body.agent_type,
"capabilities": capabilities,
})),
)
.into_response(),
Err(e) => {
tracing::error!("handler error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
#[derive(Deserialize)]
pub struct PendingListQuery {
#[serde(default)]
pub status: Option<String>,
#[serde(default = "default_pending_limit")]
pub limit: Option<usize>,
}
#[allow(clippy::unnecessary_wraps)]
fn default_pending_limit() -> Option<usize> {
Some(100)
}
pub async fn list_pending(
State(state): State<Db>,
Query(p): Query<PendingListQuery>,
) -> impl IntoResponse {
let limit = p.limit.unwrap_or(100).min(1000);
let lock = state.lock().await;
match db::list_pending_actions(&lock.0, p.status.as_deref(), limit) {
Ok(items) => Json(json!({"count": items.len(), "pending": items})).into_response(),
Err(e) => {
tracing::error!("handler error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
pub async fn approve_pending(
State(state): State<Db>,
headers: HeaderMap,
Path(id): Path<String>,
) -> impl IntoResponse {
use crate::db::ApproveOutcome;
if let Err(e) = validate::validate_id(&id) {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": e.to_string()})),
)
.into_response();
}
let header_agent_id = headers.get("x-agent-id").and_then(|v| v.to_str().ok());
let agent_id = match crate::identity::resolve_http_agent_id(None, header_agent_id) {
Ok(a) => a,
Err(e) => {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": format!("invalid agent_id: {e}")})),
)
.into_response();
}
};
let lock = state.lock().await;
match db::approve_with_approver_type(&lock.0, &id, &agent_id) {
Ok(ApproveOutcome::Approved) => match db::execute_pending_action(&lock.0, &id) {
Ok(memory_id) => Json(json!({
"approved": true,
"id": id,
"decided_by": agent_id,
"executed": true,
"memory_id": memory_id,
}))
.into_response(),
Err(e) => {
tracing::error!("execute pending error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "approved but execution failed"})),
)
.into_response()
}
},
Ok(ApproveOutcome::Pending { votes, quorum }) => (
StatusCode::ACCEPTED,
Json(json!({
"approved": false,
"status": "pending",
"id": id,
"votes": votes,
"quorum": quorum,
"reason": "consensus threshold not yet reached",
})),
)
.into_response(),
Ok(ApproveOutcome::Rejected(reason)) => (
StatusCode::FORBIDDEN,
Json(json!({"error": format!("approve rejected: {reason}")})),
)
.into_response(),
Err(e) => {
tracing::error!("handler error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
pub async fn reject_pending(
State(state): State<Db>,
headers: HeaderMap,
Path(id): Path<String>,
) -> impl IntoResponse {
if let Err(e) = validate::validate_id(&id) {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": e.to_string()})),
)
.into_response();
}
let header_agent_id = headers.get("x-agent-id").and_then(|v| v.to_str().ok());
let agent_id = match crate::identity::resolve_http_agent_id(None, header_agent_id) {
Ok(a) => a,
Err(e) => {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": format!("invalid agent_id: {e}")})),
)
.into_response();
}
};
let lock = state.lock().await;
match db::decide_pending_action(&lock.0, &id, false, &agent_id) {
Ok(true) => {
Json(json!({"rejected": true, "id": id, "decided_by": agent_id})).into_response()
}
Ok(false) => (
StatusCode::NOT_FOUND,
Json(json!({"error": "pending action not found or already decided"})),
)
.into_response(),
Err(e) => {
tracing::error!("handler error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
pub async fn list_agents(State(state): State<Db>) -> impl IntoResponse {
let lock = state.lock().await;
match db::list_agents(&lock.0) {
Ok(agents) => (
StatusCode::OK,
Json(json!({"count": agents.len(), "agents": agents})),
)
.into_response(),
Err(e) => {
tracing::error!("handler error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
pub async fn get_memory(State(state): State<Db>, Path(id): Path<String>) -> impl IntoResponse {
if let Err(e) = validate::validate_id(&id) {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": e.to_string()})),
)
.into_response();
}
let lock = state.lock().await;
match db::resolve_id(&lock.0, &id) {
Ok(Some(mem)) => {
let links = db::get_links(&lock.0, &mem.id).unwrap_or_default();
Json(json!({"memory": mem, "links": links})).into_response()
}
Ok(None) => (StatusCode::NOT_FOUND, Json(json!({"error": "not found"}))).into_response(),
Err(e) => {
let msg = e.to_string();
if msg.contains("ambiguous ID prefix") {
return (StatusCode::BAD_REQUEST, Json(json!({"error": msg}))).into_response();
}
tracing::error!("handler error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
pub async fn update_memory(
State(app): State<AppState>,
Path(id): Path<String>,
Json(body): Json<UpdateMemory>,
) -> impl IntoResponse {
let state = app.db.clone();
if let Err(e) = validate::validate_id(&id) {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": e.to_string()})),
)
.into_response();
}
if let Err(e) = validate::validate_update(&body) {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": e.to_string()})),
)
.into_response();
}
let lock = state.lock().await;
let resolved_id = match db::resolve_id(&lock.0, &id) {
Ok(Some(mem)) => mem.id,
Ok(None) => {
return (StatusCode::NOT_FOUND, Json(json!({"error": "not found"}))).into_response();
}
Err(e) => {
let msg = e.to_string();
if msg.contains("ambiguous ID prefix") {
return (StatusCode::BAD_REQUEST, Json(json!({"error": msg}))).into_response();
}
tracing::error!("handler error: {e}");
return (
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response();
}
};
let preserved_metadata = body.metadata.as_ref().map(|new_meta| {
let existing_meta = db::get(&lock.0, &resolved_id).ok().flatten().map_or_else(
|| serde_json::Value::Object(serde_json::Map::new()),
|m| m.metadata,
);
crate::identity::preserve_agent_id(&existing_meta, new_meta)
});
match db::update(
&lock.0,
&resolved_id,
body.title.as_deref(),
body.content.as_deref(),
body.tier.as_ref(),
body.namespace.as_deref(),
body.tags.as_ref(),
body.priority,
body.confidence,
body.expires_at.as_deref(),
preserved_metadata.as_ref(),
) {
Ok((true, _)) => {
let mem = db::get(&lock.0, &resolved_id).ok().flatten();
let content_changed = body.title.is_some() || body.content.is_some();
if content_changed && let Some(ref m) = mem {
let text = format!("{} {}", m.title, m.content);
if let Some(emb) = app.embedder.as_ref().as_ref() {
match emb.embed(&text) {
Ok(vec) => {
if let Err(e) = db::set_embedding(&lock.0, &resolved_id, &vec) {
tracing::warn!(
"failed to refresh embedding for {resolved_id}: {e}"
);
}
drop(lock);
let mut idx_lock = app.vector_index.lock().await;
if let Some(idx) = idx_lock.as_mut() {
idx.remove(&resolved_id);
idx.insert(resolved_id.clone(), vec);
}
}
Err(e) => tracing::warn!("embedding regeneration failed: {e}"),
}
}
}
Json(json!(mem)).into_response()
}
Ok((false, _)) => {
(StatusCode::NOT_FOUND, Json(json!({"error": "not found"}))).into_response()
}
Err(e) => {
let msg = e.to_string();
if msg.contains("already exists in namespace") {
return (StatusCode::CONFLICT, Json(json!({"error": msg}))).into_response();
}
tracing::error!("handler error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
pub async fn delete_memory(
State(state): State<Db>,
headers: HeaderMap,
Path(id): Path<String>,
) -> impl IntoResponse {
if let Err(e) = validate::validate_id(&id) {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": e.to_string()})),
)
.into_response();
}
let lock = state.lock().await;
let target = match db::resolve_id(&lock.0, &id) {
Ok(Some(m)) => m,
Ok(None) => {
return (StatusCode::NOT_FOUND, Json(json!({"error": "not found"}))).into_response();
}
Err(e) => {
let msg = e.to_string();
if msg.contains("ambiguous ID prefix") {
return (StatusCode::BAD_REQUEST, Json(json!({"error": msg}))).into_response();
}
tracing::error!("handler error: {e}");
return (
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response();
}
};
{
use crate::models::{GovernanceDecision, GovernedAction};
let header_agent_id = headers.get("x-agent-id").and_then(|v| v.to_str().ok());
let agent_id = match crate::identity::resolve_http_agent_id(None, header_agent_id) {
Ok(a) => a,
Err(e) => {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": format!("invalid agent_id: {e}")})),
)
.into_response();
}
};
let mem_owner = target
.metadata
.get("agent_id")
.and_then(|v| v.as_str())
.map(str::to_string);
let payload = json!({"id": target.id, "title": target.title});
match db::enforce_governance(
&lock.0,
GovernedAction::Delete,
&target.namespace,
&agent_id,
Some(&target.id),
mem_owner.as_deref(),
&payload,
) {
Ok(GovernanceDecision::Allow) => {}
Ok(GovernanceDecision::Deny(reason)) => {
return (
StatusCode::FORBIDDEN,
Json(json!({"error": format!("delete denied by governance: {reason}")})),
)
.into_response();
}
Ok(GovernanceDecision::Pending(pending_id)) => {
return (
StatusCode::ACCEPTED,
Json(json!({
"status": "pending",
"pending_id": pending_id,
"reason": "governance requires approval",
"action": "delete",
"memory_id": target.id,
})),
)
.into_response();
}
Err(e) => {
tracing::error!("governance error: {e}");
return (
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "governance check failed"})),
)
.into_response();
}
}
}
match db::delete(&lock.0, &target.id) {
Ok(true) => Json(json!({"deleted": true})).into_response(),
_ => (StatusCode::NOT_FOUND, Json(json!({"error": "not found"}))).into_response(),
}
}
#[allow(clippy::too_many_lines)]
pub async fn promote_memory(
State(state): State<Db>,
headers: HeaderMap,
Path(id): Path<String>,
) -> impl IntoResponse {
if let Err(e) = validate::validate_id(&id) {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": e.to_string()})),
)
.into_response();
}
let lock = state.lock().await;
let target = match db::resolve_id(&lock.0, &id) {
Ok(Some(mem)) => mem,
Ok(None) => {
return (StatusCode::NOT_FOUND, Json(json!({"error": "not found"}))).into_response();
}
Err(e) => {
let msg = e.to_string();
if msg.contains("ambiguous ID prefix") {
return (StatusCode::BAD_REQUEST, Json(json!({"error": msg}))).into_response();
}
tracing::error!("handler error: {e}");
return (
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response();
}
};
{
use crate::models::{GovernanceDecision, GovernedAction};
let header_agent_id = headers.get("x-agent-id").and_then(|v| v.to_str().ok());
let agent_id = match crate::identity::resolve_http_agent_id(None, header_agent_id) {
Ok(a) => a,
Err(e) => {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": format!("invalid agent_id: {e}")})),
)
.into_response();
}
};
let mem_owner = target
.metadata
.get("agent_id")
.and_then(|v| v.as_str())
.map(str::to_string);
let payload = json!({"id": target.id});
match db::enforce_governance(
&lock.0,
GovernedAction::Promote,
&target.namespace,
&agent_id,
Some(&target.id),
mem_owner.as_deref(),
&payload,
) {
Ok(GovernanceDecision::Allow) => {}
Ok(GovernanceDecision::Deny(reason)) => {
return (
StatusCode::FORBIDDEN,
Json(json!({"error": format!("promote denied by governance: {reason}")})),
)
.into_response();
}
Ok(GovernanceDecision::Pending(pending_id)) => {
return (
StatusCode::ACCEPTED,
Json(json!({
"status": "pending",
"pending_id": pending_id,
"reason": "governance requires approval",
"action": "promote",
"memory_id": target.id,
})),
)
.into_response();
}
Err(e) => {
tracing::error!("governance error: {e}");
return (
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "governance check failed"})),
)
.into_response();
}
}
}
let resolved_id = target.id.clone();
match db::update(
&lock.0,
&resolved_id,
None,
None,
Some(&Tier::Long),
None,
None,
None,
None,
None,
None,
) {
Ok((true, _)) => {
if let Err(e) = lock.0.execute(
"UPDATE memories SET expires_at = NULL WHERE id = ?1",
rusqlite::params![resolved_id],
) {
tracing::error!("promote clear expiry failed: {e}");
return (
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response();
}
Json(json!({"promoted": true, "id": resolved_id, "tier": "long"})).into_response()
}
Ok((false, _)) => {
(StatusCode::NOT_FOUND, Json(json!({"error": "not found"}))).into_response()
}
Err(e) => {
tracing::error!("handler error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
pub async fn list_memories(
State(state): State<Db>,
Query(p): Query<ListQuery>,
) -> impl IntoResponse {
if let Some(ref aid) = p.agent_id
&& let Err(e) = validate::validate_agent_id(aid)
{
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": format!("invalid agent_id filter: {e}")})),
)
.into_response();
}
let lock = state.lock().await;
let limit = p.limit.unwrap_or(20).min(200);
match db::list(
&lock.0,
p.namespace.as_deref(),
p.tier.as_ref(),
limit,
p.offset.unwrap_or(0),
p.min_priority,
p.since.as_deref(),
p.until.as_deref(),
p.tags.as_deref(),
p.agent_id.as_deref(),
) {
Ok(mems) => Json(json!({"memories": mems, "count": mems.len()})).into_response(),
Err(e) => {
tracing::error!("handler error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
pub async fn search_memories(
State(state): State<Db>,
Query(p): Query<SearchQuery>,
) -> impl IntoResponse {
if p.q.trim().is_empty() {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": "query is required"})),
)
.into_response();
}
if let Some(ref aid) = p.agent_id
&& let Err(e) = validate::validate_agent_id(aid)
{
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": format!("invalid agent_id filter: {e}")})),
)
.into_response();
}
if let Some(ref a) = p.as_agent
&& let Err(e) = validate::validate_namespace(a)
{
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": format!("invalid as_agent: {e}")})),
)
.into_response();
}
let lock = state.lock().await;
let limit = p.limit.unwrap_or(20).min(200);
match db::search(
&lock.0,
&p.q,
p.namespace.as_deref(),
p.tier.as_ref(),
limit,
p.min_priority,
p.since.as_deref(),
p.until.as_deref(),
p.tags.as_deref(),
p.agent_id.as_deref(),
p.as_agent.as_deref(),
) {
Ok(r) => Json(json!({"results": r, "count": r.len(), "query": p.q})).into_response(),
Err(e) => {
tracing::error!("handler error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
pub async fn recall_memories_get(
State(state): State<Db>,
Query(p): Query<RecallQuery>,
) -> impl IntoResponse {
let ctx = p.context.unwrap_or_default();
if ctx.trim().is_empty() {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": "context is required"})),
)
.into_response();
}
if let Some(ref a) = p.as_agent
&& let Err(e) = validate::validate_namespace(a)
{
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": format!("invalid as_agent: {e}")})),
)
.into_response();
}
let lock = state.lock().await;
let limit = p.limit.unwrap_or(10).min(50);
match db::recall(
&lock.0,
&ctx,
p.namespace.as_deref(),
limit,
p.tags.as_deref(),
p.since.as_deref(),
p.until.as_deref(),
lock.2.short_extend_secs,
lock.2.mid_extend_secs,
p.as_agent.as_deref(),
p.budget_tokens,
) {
Ok((r, tokens_used)) => {
let scored: Vec<serde_json::Value> = r
.iter()
.map(|(m, s)| {
let mut v = serde_json::to_value(m).unwrap_or_default();
if let Some(obj) = v.as_object_mut() {
obj.insert("score".to_string(), json!((*s * 1000.0).round() / 1000.0));
}
v
})
.collect();
let mut resp = json!({
"memories": scored,
"count": scored.len(),
"tokens_used": tokens_used,
});
if let Some(b) = p.budget_tokens {
resp["budget_tokens"] = json!(b);
}
Json(resp).into_response()
}
Err(e) => {
tracing::error!("handler error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
pub async fn recall_memories_post(
State(state): State<Db>,
Json(body): Json<RecallBody>,
) -> impl IntoResponse {
if body.context.trim().is_empty() {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": "context is required"})),
)
.into_response();
}
if let Some(ref a) = body.as_agent
&& let Err(e) = validate::validate_namespace(a)
{
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": format!("invalid as_agent: {e}")})),
)
.into_response();
}
let lock = state.lock().await;
let limit = body.limit.unwrap_or(10).min(50);
match db::recall(
&lock.0,
&body.context,
body.namespace.as_deref(),
limit,
body.tags.as_deref(),
body.since.as_deref(),
body.until.as_deref(),
lock.2.short_extend_secs,
lock.2.mid_extend_secs,
body.as_agent.as_deref(),
body.budget_tokens,
) {
Ok((r, tokens_used)) => {
let scored: Vec<serde_json::Value> = r
.iter()
.map(|(m, s)| {
let mut v = serde_json::to_value(m).unwrap_or_default();
if let Some(obj) = v.as_object_mut() {
obj.insert("score".to_string(), json!((*s * 1000.0).round() / 1000.0));
}
v
})
.collect();
let mut resp = json!({
"memories": scored,
"count": scored.len(),
"tokens_used": tokens_used,
});
if let Some(b) = body.budget_tokens {
resp["budget_tokens"] = json!(b);
}
Json(resp).into_response()
}
Err(e) => {
tracing::error!("handler error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
pub async fn forget_memories(
State(state): State<Db>,
Json(body): Json<ForgetQuery>,
) -> impl IntoResponse {
let lock = state.lock().await;
match db::forget(
&lock.0,
body.namespace.as_deref(),
body.pattern.as_deref(),
body.tier.as_ref(),
lock.3, ) {
Ok(n) => Json(json!({"deleted": n})).into_response(),
Err(e) => (
StatusCode::BAD_REQUEST,
Json(json!({"error": e.to_string()})),
)
.into_response(),
}
}
pub async fn list_namespaces(State(state): State<Db>) -> impl IntoResponse {
let lock = state.lock().await;
match db::list_namespaces(&lock.0) {
Ok(ns) => Json(json!({"namespaces": ns})).into_response(),
Err(e) => {
tracing::error!("handler error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
pub async fn create_link(State(state): State<Db>, Json(body): Json<LinkBody>) -> impl IntoResponse {
if let Err(e) = validate::validate_link(&body.source_id, &body.target_id, &body.relation) {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": e.to_string()})),
)
.into_response();
}
let lock = state.lock().await;
match db::create_link(&lock.0, &body.source_id, &body.target_id, &body.relation) {
Ok(()) => (StatusCode::CREATED, Json(json!({"linked": true}))).into_response(),
Err(e) => {
tracing::error!("handler error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
pub async fn get_links(State(state): State<Db>, Path(id): Path<String>) -> impl IntoResponse {
if let Err(e) = validate::validate_id(&id) {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": e.to_string()})),
)
.into_response();
}
let lock = state.lock().await;
match db::get_links(&lock.0, &id) {
Ok(links) => Json(json!({"links": links})).into_response(),
Err(e) => {
tracing::error!("handler error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
pub async fn get_stats(State(state): State<Db>) -> impl IntoResponse {
let lock = state.lock().await;
match db::stats(&lock.0, &lock.1) {
Ok(s) => Json(json!(s)).into_response(),
Err(e) => {
tracing::error!("handler error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
pub async fn run_gc(State(state): State<Db>) -> impl IntoResponse {
let lock = state.lock().await;
match db::gc(&lock.0, lock.3) {
Ok(n) => Json(json!({"expired_deleted": n})).into_response(),
Err(e) => {
tracing::error!("handler error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
pub async fn export_memories(State(state): State<Db>) -> impl IntoResponse {
let lock = state.lock().await;
match (db::export_all(&lock.0), db::export_links(&lock.0)) {
(Ok(memories), Ok(links)) => {
let count = memories.len();
Json(json!({"memories": memories, "links": links, "count": count, "exported_at": Utc::now().to_rfc3339()})).into_response()
}
(Err(e), _) | (_, Err(e)) => {
tracing::error!("export error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
pub async fn import_memories(
State(state): State<Db>,
Json(body): Json<ImportBody>,
) -> impl IntoResponse {
if body.memories.len() > MAX_BULK_SIZE {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": format!("import limited to {} memories", MAX_BULK_SIZE)})),
)
.into_response();
}
let lock = state.lock().await;
let mut imported = 0usize;
let mut errors = Vec::new();
for mem in body.memories {
if let Err(e) = validate::validate_memory(&mem) {
errors.push(format!("{}: {}", mem.id, e));
continue;
}
match db::insert(&lock.0, &mem) {
Ok(_) => imported += 1,
Err(e) => errors.push(format!("{}: {}", mem.id, e)),
}
}
for link in body.links.unwrap_or_default() {
if validate::validate_link(&link.source_id, &link.target_id, &link.relation).is_err() {
continue;
}
let _ = db::create_link(&lock.0, &link.source_id, &link.target_id, &link.relation);
}
Json(json!({"imported": imported, "errors": errors})).into_response()
}
#[derive(serde::Deserialize)]
pub struct ImportBody {
pub memories: Vec<Memory>,
#[serde(default)]
pub links: Option<Vec<MemoryLink>>,
}
#[derive(serde::Deserialize)]
pub struct ConsolidateBody {
pub ids: Vec<String>,
pub title: String,
pub summary: String,
#[serde(default = "default_ns")]
pub namespace: String,
#[serde(default)]
pub tier: Option<Tier>,
#[serde(default)]
pub agent_id: Option<String>,
}
fn default_ns() -> String {
"global".to_string()
}
pub async fn consolidate_memories(
State(state): State<Db>,
headers: HeaderMap,
Json(body): Json<ConsolidateBody>,
) -> impl IntoResponse {
if let Err(e) =
validate::validate_consolidate(&body.ids, &body.title, &body.summary, &body.namespace)
{
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": e.to_string()})),
)
.into_response();
}
let header_agent_id = headers.get("x-agent-id").and_then(|v| v.to_str().ok());
let consolidator_agent_id =
match crate::identity::resolve_http_agent_id(body.agent_id.as_deref(), header_agent_id) {
Ok(id) => id,
Err(e) => {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": format!("invalid agent_id: {e}")})),
)
.into_response();
}
};
let lock = state.lock().await;
let tier = body.tier.unwrap_or(Tier::Long);
match db::consolidate(
&lock.0,
&body.ids,
&body.title,
&body.summary,
&body.namespace,
&tier,
"consolidation",
&consolidator_agent_id,
) {
Ok(new_id) => (
StatusCode::CREATED,
Json(json!({"id": new_id, "consolidated": body.ids.len()})),
)
.into_response(),
Err(e) => {
tracing::error!("handler error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
pub async fn bulk_create(
State(state): State<Db>,
Json(bodies): Json<Vec<CreateMemory>>,
) -> impl IntoResponse {
if bodies.len() > MAX_BULK_SIZE {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": format!("bulk operations limited to {} items", MAX_BULK_SIZE)})),
)
.into_response();
}
let now = Utc::now();
let lock = state.lock().await;
let mut created = 0usize;
let mut errors = Vec::new();
for body in bodies {
if let Err(e) = validate::validate_create(&body) {
errors.push(format!("{}: {}", body.title, e));
continue;
}
let expires_at = body.expires_at.or_else(|| {
body.ttl_secs
.or(lock.2.ttl_for_tier(&body.tier))
.map(|s| (now + Duration::seconds(s)).to_rfc3339())
});
let mem = Memory {
id: Uuid::new_v4().to_string(),
tier: body.tier,
namespace: body.namespace,
title: body.title,
content: body.content,
tags: body.tags,
priority: body.priority.clamp(1, 10),
confidence: body.confidence.clamp(0.0, 1.0),
source: body.source,
access_count: 0,
created_at: now.to_rfc3339(),
updated_at: now.to_rfc3339(),
last_accessed_at: None,
expires_at,
metadata: body.metadata,
};
match db::insert(&lock.0, &mem) {
Ok(_) => created += 1,
Err(e) => errors.push(e.to_string()),
}
}
Json(json!({"created": created, "errors": errors})).into_response()
}
#[derive(Debug, Deserialize)]
pub struct ArchiveListQuery {
pub namespace: Option<String>,
#[serde(default = "default_archive_limit")]
pub limit: Option<usize>,
#[serde(default)]
pub offset: Option<usize>,
}
#[allow(clippy::unnecessary_wraps)]
fn default_archive_limit() -> Option<usize> {
Some(50)
}
pub async fn list_archive(
State(state): State<Db>,
Query(q): Query<ArchiveListQuery>,
) -> impl IntoResponse {
let lock = state.lock().await;
let limit = q.limit.unwrap_or(50).min(1000);
let offset = q.offset.unwrap_or(0);
match db::list_archived(&lock.0, q.namespace.as_deref(), limit, offset) {
Ok(items) => Json(json!({"archived": items, "count": items.len()})).into_response(),
Err(e) => {
tracing::error!("handler error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
pub async fn restore_archive(State(state): State<Db>, Path(id): Path<String>) -> impl IntoResponse {
if let Err(e) = validate::validate_id(&id) {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": e.to_string()})),
)
.into_response();
}
let lock = state.lock().await;
match db::restore_archived(&lock.0, &id) {
Ok(true) => Json(json!({"restored": true, "id": id})).into_response(),
Ok(false) => (
StatusCode::NOT_FOUND,
Json(json!({"error": "not found in archive"})),
)
.into_response(),
Err(e) => {
tracing::error!("handler error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
#[derive(Debug, Deserialize)]
pub struct PurgeQuery {
pub older_than_days: Option<i64>,
}
pub async fn purge_archive(
State(state): State<Db>,
Query(q): Query<PurgeQuery>,
) -> impl IntoResponse {
let lock = state.lock().await;
match db::purge_archive(&lock.0, q.older_than_days) {
Ok(n) => Json(json!({"purged": n})).into_response(),
Err(e) => {
tracing::error!("handler error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
pub async fn archive_stats(State(state): State<Db>) -> impl IntoResponse {
let lock = state.lock().await;
match db::archive_stats(&lock.0) {
Ok(archive_stats) => Json(archive_stats).into_response(),
Err(e) => {
tracing::error!("handler error: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response()
}
}
}
#[derive(Deserialize)]
pub struct SyncPushBody {
pub sender_agent_id: String,
#[serde(default)]
#[allow(dead_code)] pub sender_clock: crate::models::VectorClock,
pub memories: Vec<Memory>,
#[serde(default)]
pub dry_run: bool,
}
#[derive(Deserialize)]
pub struct SyncSinceQuery {
pub since: Option<String>,
pub limit: Option<usize>,
pub peer: Option<String>,
}
pub async fn sync_push(
State(state): State<Db>,
headers: HeaderMap,
Json(body): Json<SyncPushBody>,
) -> impl IntoResponse {
if let Err(e) = validate::validate_agent_id(&body.sender_agent_id) {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": format!("invalid sender_agent_id: {e}")})),
)
.into_response();
}
if body.memories.len() > MAX_BULK_SIZE {
return (
StatusCode::BAD_REQUEST,
Json(json!({
"error": format!("sync_push limited to {} memories per request", MAX_BULK_SIZE)
})),
)
.into_response();
}
let header_agent_id = headers.get("x-agent-id").and_then(|v| v.to_str().ok());
let local_agent_id = match crate::identity::resolve_http_agent_id(None, header_agent_id) {
Ok(id) => id,
Err(e) => {
return (
StatusCode::BAD_REQUEST,
Json(json!({"error": format!("invalid x-agent-id: {e}")})),
)
.into_response();
}
};
let lock = state.lock().await;
let mut applied = 0usize;
let mut noop = 0usize;
let mut skipped = 0usize;
let mut latest_seen: Option<String> = None;
for mem in &body.memories {
if validate::validate_memory(mem).is_err() {
skipped += 1;
continue;
}
if latest_seen
.as_deref()
.is_none_or(|current| mem.updated_at.as_str() > current)
{
latest_seen = Some(mem.updated_at.clone());
}
if body.dry_run {
noop += 1;
continue;
}
match db::insert_if_newer(&lock.0, mem) {
Ok(_id) => applied += 1,
Err(e) => {
tracing::warn!("sync_push: insert_if_newer failed for {}: {e}", mem.id);
skipped += 1;
}
}
}
if !body.dry_run
&& let Some(at) = latest_seen.as_deref()
&& let Err(e) = db::sync_state_observe(&lock.0, &local_agent_id, &body.sender_agent_id, at)
{
tracing::warn!("sync_push: sync_state_observe failed: {e}");
}
let receiver_clock = db::sync_state_load(&lock.0, &local_agent_id)
.unwrap_or_else(|_| crate::models::VectorClock::default());
(
StatusCode::OK,
Json(json!({
"applied": applied,
"noop": noop,
"skipped": skipped,
"dry_run": body.dry_run,
"receiver_agent_id": local_agent_id,
"receiver_clock": receiver_clock,
})),
)
.into_response()
}
pub async fn sync_since(
State(state): State<Db>,
headers: HeaderMap,
Query(q): Query<SyncSinceQuery>,
) -> impl IntoResponse {
if let Some(ref s) = q.since
&& !s.is_empty()
&& chrono::DateTime::parse_from_rfc3339(s).is_err()
{
return (
StatusCode::BAD_REQUEST,
Json(json!({
"error": "invalid `since` parameter — expected RFC 3339 timestamp"
})),
)
.into_response();
}
let limit = q.limit.unwrap_or(500).min(10_000);
let lock = state.lock().await;
let mems = match db::memories_updated_since(&lock.0, q.since.as_deref(), limit) {
Ok(v) => v,
Err(e) => {
tracing::error!("sync_since: {e}");
return (
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "internal server error"})),
)
.into_response();
}
};
let header_agent_id = headers.get("x-agent-id").and_then(|v| v.to_str().ok());
if let (Some(peer), Ok(local_agent_id)) = (
q.peer.as_deref(),
crate::identity::resolve_http_agent_id(None, header_agent_id),
) && validate::validate_agent_id(peer).is_ok()
&& let Some(last) = mems.last()
&& let Err(e) = db::sync_state_observe(&lock.0, &local_agent_id, peer, &last.updated_at)
{
tracing::debug!("sync_since: sync_state_observe failed: {e}");
}
(
StatusCode::OK,
Json(json!({
"count": mems.len(),
"limit": limit,
"memories": mems,
})),
)
.into_response()
}
#[cfg(test)]
mod tests {
use super::*;
fn test_state() -> Db {
let conn = db::open(std::path::Path::new(":memory:")).unwrap();
let path = std::path::PathBuf::from(":memory:");
Arc::new(Mutex::new((conn, path, ResolvedTtl::default(), true)))
}
#[tokio::test]
async fn health_returns_ok() {
let state = test_state();
let lock = state.lock().await;
let ok = db::health_check(&lock.0).unwrap_or(false);
assert!(ok);
}
#[tokio::test]
async fn store_and_retrieve_via_state() {
let state = test_state();
let lock = state.lock().await;
let now = Utc::now();
let mem = Memory {
id: Uuid::new_v4().to_string(),
tier: Tier::Long,
namespace: "test".into(),
title: "Handler test".into(),
content: "Testing handlers.".into(),
tags: vec!["test".into()],
priority: 7,
confidence: 1.0,
source: "test".into(),
access_count: 0,
created_at: now.to_rfc3339(),
updated_at: now.to_rfc3339(),
last_accessed_at: None,
expires_at: None,
metadata: serde_json::json!({}),
};
let id = db::insert(&lock.0, &mem).unwrap();
let got = db::get(&lock.0, &id).unwrap().unwrap();
assert_eq!(got.title, "Handler test");
}
#[tokio::test]
async fn recall_via_state() {
let state = test_state();
let lock = state.lock().await;
let now = Utc::now();
let mem = Memory {
id: Uuid::new_v4().to_string(),
tier: Tier::Long,
namespace: "test".into(),
title: "Recall handler test".into(),
content: "Content for recall.".into(),
tags: vec![],
priority: 8,
confidence: 1.0,
source: "test".into(),
access_count: 0,
created_at: now.to_rfc3339(),
updated_at: now.to_rfc3339(),
last_accessed_at: None,
expires_at: None,
metadata: serde_json::json!({}),
};
db::insert(&lock.0, &mem).unwrap();
let (results, _tokens) = db::recall(
&lock.0,
"recall handler",
Some("test"),
10,
None,
None,
None,
crate::models::SHORT_TTL_EXTEND_SECS,
crate::models::MID_TTL_EXTEND_SECS,
None,
None,
)
.unwrap();
assert!(!results.is_empty());
assert!(results[0].1 > 0.0); }
#[tokio::test]
async fn stats_via_state() {
let state = test_state();
let lock = state.lock().await;
let path = std::path::Path::new(":memory:");
let s = db::stats(&lock.0, path).unwrap();
assert_eq!(s.total, 0);
}
#[tokio::test]
async fn bulk_size_limit() {
assert_eq!(MAX_BULK_SIZE, 1000);
}
#[tokio::test]
async fn list_empty_namespace() {
let state = test_state();
let lock = state.lock().await;
let results = db::list(
&lock.0,
Some("nonexistent"),
None,
10,
0,
None,
None,
None,
None,
None,
)
.unwrap();
assert!(results.is_empty());
}
#[tokio::test]
async fn create_and_update_with_metadata() {
let state = test_state();
let lock = state.lock().await;
let now = Utc::now();
let mem = Memory {
id: Uuid::new_v4().to_string(),
tier: Tier::Long,
namespace: "test".into(),
title: "HTTP metadata test".into(),
content: "Testing metadata through handler layer.".into(),
tags: vec![],
priority: 5,
confidence: 1.0,
source: "api".into(),
access_count: 0,
created_at: now.to_rfc3339(),
updated_at: now.to_rfc3339(),
last_accessed_at: None,
expires_at: None,
metadata: serde_json::json!({"http_test": true, "version": 1}),
};
let id = db::insert(&lock.0, &mem).unwrap();
let got = db::get(&lock.0, &id).unwrap().unwrap();
assert_eq!(got.metadata["http_test"], true);
assert_eq!(got.metadata["version"], 1);
let new_meta =
serde_json::json!({"http_test": true, "version": 2, "updated_by": "handler"});
let (found, _) = db::update(
&lock.0,
&id,
None,
None,
None,
None,
None,
None,
None,
None,
Some(&new_meta),
)
.unwrap();
assert!(found);
let got = db::get(&lock.0, &id).unwrap().unwrap();
assert_eq!(got.metadata["version"], 2);
assert_eq!(got.metadata["updated_by"], "handler");
}
use axum::{Router, body::Body, routing::get as axum_get, routing::post as axum_post};
use tower::ServiceExt as _;
fn test_app_state(db: Db) -> AppState {
AppState {
db,
embedder: Arc::new(None),
vector_index: Arc::new(Mutex::new(None)),
federation: Arc::new(None),
}
}
#[tokio::test]
async fn http_create_memory_uses_appstate_and_persists() {
let state = test_state();
let app = Router::new()
.route("/api/v1/memories", axum_post(create_memory))
.with_state(test_app_state(state.clone()));
let body = serde_json::json!({
"tier": "long",
"namespace": "http-embed-test",
"title": "Semantic-ready via HTTP",
"content": "HTTP-authored memories must now participate in semantic recall.",
"tags": ["issue-219"],
"priority": 7,
"confidence": 1.0,
"source": "api",
"metadata": {}
});
let resp = app
.oneshot(
axum::http::Request::builder()
.uri("/api/v1/memories")
.method("POST")
.header("content-type", "application/json")
.header("x-agent-id", "alice")
.body(Body::from(serde_json::to_vec(&body).unwrap()))
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::CREATED);
let lock = state.lock().await;
let rows = db::list(
&lock.0,
Some("http-embed-test"),
None,
10,
0,
None,
None,
None,
None,
None,
)
.unwrap();
assert!(!rows.is_empty(), "HTTP-authored memory must be persisted");
assert_eq!(rows[0].title, "Semantic-ready via HTTP");
}
#[tokio::test]
async fn http_update_memory_uses_appstate() {
let state = test_state();
let now = Utc::now();
let id = {
let lock = state.lock().await;
let mem = Memory {
id: Uuid::new_v4().to_string(),
tier: Tier::Long,
namespace: "http-embed-test".into(),
title: "Before update".into(),
content: "Original content.".into(),
tags: vec![],
priority: 5,
confidence: 1.0,
source: "test".into(),
access_count: 0,
created_at: now.to_rfc3339(),
updated_at: now.to_rfc3339(),
last_accessed_at: None,
expires_at: None,
metadata: serde_json::json!({}),
};
db::insert(&lock.0, &mem).unwrap()
};
let app = Router::new()
.route("/api/v1/memories/{id}", axum::routing::put(update_memory))
.with_state(test_app_state(state.clone()));
let patch = serde_json::json!({"content": "Updated content for semantic refresh."});
let resp = app
.oneshot(
axum::http::Request::builder()
.uri(format!("/api/v1/memories/{id}"))
.method("PUT")
.header("content-type", "application/json")
.body(Body::from(serde_json::to_vec(&patch).unwrap()))
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
}
#[tokio::test]
async fn http_sync_push_applies_and_advances_clock() {
let state = test_state();
let app = Router::new()
.route("/api/v1/sync/push", axum_post(sync_push))
.with_state(state.clone());
let now = Utc::now().to_rfc3339();
let body = serde_json::json!({
"sender_agent_id": "peer-alice",
"sender_clock": {"entries": {}},
"memories": [{
"id": Uuid::new_v4().to_string(),
"tier": "long",
"namespace": "sync-smoke",
"title": "From peer",
"content": "Pushed via HTTP sync endpoint.",
"tags": [],
"priority": 5,
"confidence": 1.0,
"source": "api",
"access_count": 0,
"created_at": now,
"updated_at": now,
"last_accessed_at": null,
"expires_at": null,
"metadata": {"agent_id": "peer-alice"}
}],
"dry_run": false
});
let resp = app
.oneshot(
axum::http::Request::builder()
.uri("/api/v1/sync/push")
.method("POST")
.header("content-type", "application/json")
.header("x-agent-id", "local-receiver")
.body(Body::from(serde_json::to_vec(&body).unwrap()))
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let lock = state.lock().await;
let rows = db::list(
&lock.0,
Some("sync-smoke"),
None,
10,
0,
None,
None,
None,
None,
None,
)
.unwrap();
assert_eq!(rows.len(), 1);
let clock = db::sync_state_load(&lock.0, "local-receiver").unwrap();
assert!(
clock.latest_from("peer-alice").is_some(),
"push must record sender in sync_state; got: {:?}",
clock.entries
);
}
#[tokio::test]
async fn http_sync_push_rejects_oversized_batch_redteam_242() {
let state = test_state();
let app = Router::new()
.route("/api/v1/sync/push", axum_post(sync_push))
.with_state(state);
let now = Utc::now().to_rfc3339();
let mems: Vec<serde_json::Value> = (0..=MAX_BULK_SIZE)
.map(|i| {
serde_json::json!({
"id": Uuid::new_v4().to_string(),
"tier": "long",
"namespace": "oversize",
"title": format!("m{i}"),
"content": "x",
"tags": [],
"priority": 5,
"confidence": 1.0,
"source": "api",
"access_count": 0,
"created_at": now,
"updated_at": now,
"last_accessed_at": null,
"expires_at": null,
"metadata": {}
})
})
.collect();
let body = serde_json::json!({
"sender_agent_id": "peer-flood",
"sender_clock": {"entries": {}},
"memories": mems,
"dry_run": false,
});
let resp = app
.oneshot(
axum::http::Request::builder()
.uri("/api/v1/sync/push")
.method("POST")
.header("content-type", "application/json")
.body(Body::from(serde_json::to_vec(&body).unwrap()))
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}
#[tokio::test]
async fn http_sync_push_dry_run_applies_nothing() {
let state = test_state();
let app = Router::new()
.route("/api/v1/sync/push", axum_post(sync_push))
.with_state(state.clone());
let now = Utc::now().to_rfc3339();
let body = serde_json::json!({
"sender_agent_id": "peer-bob",
"sender_clock": {"entries": {}},
"memories": [{
"id": Uuid::new_v4().to_string(),
"tier": "long",
"namespace": "sync-dryrun",
"title": "Must not land",
"content": "Preview only.",
"tags": [],
"priority": 5,
"confidence": 1.0,
"source": "api",
"access_count": 0,
"created_at": now,
"updated_at": now,
"last_accessed_at": null,
"expires_at": null,
"metadata": {}
}],
"dry_run": true
});
let resp = app
.oneshot(
axum::http::Request::builder()
.uri("/api/v1/sync/push")
.method("POST")
.header("content-type", "application/json")
.body(Body::from(serde_json::to_vec(&body).unwrap()))
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let lock = state.lock().await;
let rows = db::list(
&lock.0,
Some("sync-dryrun"),
None,
10,
0,
None,
None,
None,
None,
None,
)
.unwrap();
assert!(rows.is_empty(), "dry_run must not write rows");
}
#[tokio::test]
async fn http_sync_since_streams_new_memories_only() {
let state = test_state();
let old_ts = "2020-01-01T00:00:00+00:00";
let new_ts = Utc::now().to_rfc3339();
{
let lock = state.lock().await;
for (title, ts) in [("old-mem", old_ts), ("new-mem", new_ts.as_str())] {
let mem = Memory {
id: Uuid::new_v4().to_string(),
tier: Tier::Long,
namespace: "since-test".into(),
title: title.into(),
content: "body".into(),
tags: vec![],
priority: 5,
confidence: 1.0,
source: "api".into(),
access_count: 0,
created_at: ts.to_string(),
updated_at: ts.to_string(),
last_accessed_at: None,
expires_at: None,
metadata: serde_json::json!({}),
};
db::insert(&lock.0, &mem).unwrap();
}
}
let app = Router::new()
.route("/api/v1/sync/since", axum_get(sync_since))
.with_state(state);
let resp = app
.oneshot(
axum::http::Request::builder()
.uri("/api/v1/sync/since?since=2020-06-01T00:00:00%2B00:00")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let bytes = axum::body::to_bytes(resp.into_body(), 1024 * 1024)
.await
.unwrap();
let v: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
let titles: Vec<String> = v["memories"]
.as_array()
.unwrap()
.iter()
.filter_map(|m| m["title"].as_str().map(str::to_string))
.collect();
assert_eq!(titles, vec!["new-mem".to_string()]);
}
#[tokio::test]
async fn sync_since_rejects_garbage_timestamp_with_400() {
let state = test_state();
let app = Router::new()
.route("/api/v1/sync/since", axum_get(sync_since))
.with_state(state);
let resp = app
.oneshot(
axum::http::Request::builder()
.uri("/api/v1/sync/since?since=not-a-date")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
let bytes = axum::body::to_bytes(resp.into_body(), 1024 * 1024)
.await
.unwrap();
let v: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
assert!(v["error"].as_str().unwrap().contains("RFC 3339"));
}
#[tokio::test]
async fn sync_state_observe_is_monotonic() {
let conn = db::open(std::path::Path::new(":memory:")).unwrap();
let older = "2020-01-01T00:00:00+00:00";
let newer = "2026-04-17T00:00:00+00:00";
db::sync_state_observe(&conn, "local", "peer-a", newer).unwrap();
db::sync_state_observe(&conn, "local", "peer-a", older).unwrap();
let clock = db::sync_state_load(&conn, "local").unwrap();
assert_eq!(clock.latest_from("peer-a"), Some(newer));
}
async fn dummy_handler() -> impl IntoResponse {
(StatusCode::OK, "ok")
}
fn auth_app(api_key: Option<&str>) -> Router {
let auth_state = ApiKeyState {
key: api_key.map(String::from),
};
Router::new()
.route("/api/v1/health", axum_get(dummy_handler))
.route("/api/v1/memories", axum_get(dummy_handler))
.layer(axum::middleware::from_fn_with_state(
auth_state,
api_key_auth,
))
}
#[tokio::test]
async fn api_key_no_key_configured_allows_all() {
let app = auth_app(None);
let resp = app
.oneshot(
axum::http::Request::builder()
.uri("/api/v1/memories")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
}
#[tokio::test]
async fn api_key_valid_header_allows() {
let app = auth_app(Some("secret123"));
let resp = app
.oneshot(
axum::http::Request::builder()
.uri("/api/v1/memories")
.header("x-api-key", "secret123")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
}
#[tokio::test]
async fn api_key_invalid_header_rejected() {
let app = auth_app(Some("secret123"));
let resp = app
.oneshot(
axum::http::Request::builder()
.uri("/api/v1/memories")
.header("x-api-key", "wrong")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
}
#[tokio::test]
async fn api_key_missing_header_rejected() {
let app = auth_app(Some("secret123"));
let resp = app
.oneshot(
axum::http::Request::builder()
.uri("/api/v1/memories")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
}
#[tokio::test]
async fn api_key_valid_query_param_allows() {
let app = auth_app(Some("secret123"));
let resp = app
.oneshot(
axum::http::Request::builder()
.uri("/api/v1/memories?api_key=secret123")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
}
#[tokio::test]
async fn api_key_health_exempt() {
let app = auth_app(Some("secret123"));
let resp = app
.oneshot(
axum::http::Request::builder()
.uri("/api/v1/health")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
}
}