second-brain-api 0.13.3

HTTP API server for second-brain: REST endpoints for recall, remember, and ingest
Documentation
mod common;

use std::sync::{Arc, RwLock};

use tower::ServiceExt;

use common::{app_with_store, get, json_body, post_json};
use second_brain_api::raw_index::{RawCoverage, RawIndexer};
use second_brain_api::{AppState, routes};
use second_brain_core::index::raw::RawIndex;
use second_brain_core::index::{EMBEDDING_DIM, Indexes};
use second_brain_core::machine::MachineIdentity;
use second_brain_core::retrieval::RetrievalTunables;
use second_brain_core::schema::{Memory, MemoryType};
use second_brain_core::sqlite_store::SqliteStore;
use second_brain_core::store::Store;

async fn status(app: &axum::Router) -> serde_json::Value {
    json_body(app.clone().oneshot(get("/v1/status")).await.unwrap()).await
}

#[tokio::test]
async fn status_counts_live_memories_apart_from_retired_and_superseded() {
    let (app, store) = app_with_store();
    let mut ids = Vec::new();
    for (content, memory_type) in [
        ("a live fact", MemoryType::Semantic),
        ("a live decision", MemoryType::Decision),
        ("a retired fact", MemoryType::Semantic),
        ("a superseded fact", MemoryType::Semantic),
    ] {
        let m = Memory::new(
            content.to_string(),
            memory_type,
            "api".to_string(),
            String::new(),
        );
        store.store_memory(&m).unwrap();
        ids.push(m.id);
    }
    store.retire_memory(ids[2], "wrong").unwrap();
    store.supersede_memory(ids[3], ids[0]).unwrap();

    let body = status(&app).await;
    assert_eq!(body["memory_count"], 2);
    assert_eq!(body["stored_total"], 4);
    assert_eq!(body["archived"], 1);
    assert_eq!(body["superseded"], 1);
    assert_eq!(body["by_type"]["semantic"], 1);
    assert_eq!(body["by_type"]["decision"], 1);
    assert_eq!(body["index"]["verdict"], "unavailable");
}

struct TempIndexDir(std::path::PathBuf);

impl Drop for TempIndexDir {
    fn drop(&mut self) {
        std::fs::remove_dir_all(&self.0).ok();
    }
}

#[tokio::test]
async fn status_index_verdict_follows_the_outbox_and_the_index_counts() {
    let machine = MachineIdentity {
        id: "status-machine".to_string(),
        name: "status".to_string(),
    };
    let sqlite = Arc::new(SqliteStore::in_memory(machine.id.clone()).unwrap());
    let dir =
        TempIndexDir(std::env::temp_dir().join(format!("sb-status-{}", uuid::Uuid::new_v4())));
    let mut writer = Indexes::open(&dir.0).unwrap();
    writer.persist().unwrap();
    let reader = Arc::new(RwLock::new(Indexes::open_readonly(&dir.0).unwrap()));
    let state = Arc::new(AppState {
        data_dir: std::env::temp_dir().join("second-brain-test-no-device-key"),
        pairing: Default::default(),
        bootstrap: Default::default(),
        device_access: Default::default(),
        gate: Default::default(),
        clock: second_brain_api::clock_gate::ClockGate::open_for_tests(),
        store: sqlite.clone(),
        sqlite: sqlite.clone(),
        indexes: Some(reader.clone()),
        raw_index: None,
        raw_coverage: None,
        archive: None,
        reranker: None,
        tunables: RetrievalTunables::default(),
        embedder: None,
        machine,
        maintenance: Arc::new(RwLock::new(None)),
        repair_tx: None,
        near_duplicate_threshold: 0.97,
        near_duplicates: std::sync::Arc::new(std::sync::RwLock::new(None)),
        health: Arc::new(second_brain_api::health::Health::new(
            chrono::Utc::now(),
            None,
        )),
    });
    let app = routes::router().with_state(state);

    assert_eq!(status(&app).await["index"]["verdict"], "consistent");

    app.clone()
        .oneshot(post_json(
            "/v1/remember",
            serde_json::json!({"content": "status probe"}),
        ))
        .await
        .unwrap();
    let body = status(&app).await;
    assert_eq!(body["index"]["verdict"], "draining");
    assert_eq!(body["index"]["outbox_pending"], 1);

    writer.drain(&sqlite).unwrap();
    reader.write().unwrap().reload_view().unwrap();
    let body = status(&app).await;
    assert_eq!(body["index"]["verdict"], "consistent");
    assert_eq!(body["index"]["documents"], 1);
}

#[tokio::test]
async fn status_reports_chunked_memories_and_the_last_backfill_pending_count() {
    let (app, store) = app_with_store();
    let body = status(&app).await;
    assert_eq!(body["chunks"]["memories_chunked"], 0);
    assert!(body["chunks"]["memories_pending"].is_null());

    for i in 0..2 {
        let m = Memory::new(
            format!(
                "The Pi runs the daemon under a MemoryMax of 1800M at all times. \
                 Its tax helper lives in the taxer project, copy {i}."
            ),
            MemoryType::Semantic,
            "api".to_string(),
            String::new(),
        );
        store.store_memory(&m).unwrap();
    }
    let embed = |texts: &[&str]| -> anyhow::Result<Vec<Vec<f32>>> {
        Ok(texts.iter().map(|_| vec![0.05; 384]).collect())
    };
    store
        .backfill_chunk_embeddings(&embed, 1, std::time::Duration::from_secs(60))
        .unwrap();

    let body = status(&app).await;
    assert_eq!(body["chunks"]["memories_chunked"], 1);
    assert_eq!(body["chunks"]["memories_pending"], 1);
}

#[tokio::test]
async fn status_heap_attributes_resident_memory_to_components() {
    let (app, _) = app_with_store();
    let heap = status(&app).await["heap"].clone();
    let components = heap["components"].as_object().unwrap();
    for key in [
        "embedder",
        "reranker",
        "vector_writer",
        "vector_reader",
        "lexical",
        "sqlite",
        "allocator_free",
    ] {
        assert!(components[key].is_u64(), "components lacks {key}: {heap}");
    }
    assert_eq!(components.len(), 7, "{heap}");
    assert!(heap["unattributed_bytes"].is_i64(), "{heap}");
    assert!(heap["rss_anon_bytes"].is_u64(), "{heap}");
}

#[tokio::test]
async fn status_reports_the_raw_index_age_bound_and_the_passages_it_skipped() {
    let machine = MachineIdentity {
        id: "status-machine".to_string(),
        name: "status".to_string(),
    };
    let sqlite = Arc::new(SqliteStore::in_memory(machine.id.clone()).unwrap());
    let mut stale = Memory::new(
        "a chunk from january".to_string(),
        MemoryType::Semantic,
        "claude-code".to_string(),
        String::new(),
    );
    stale.embedding = vec![0.0; EMBEDDING_DIM];
    stale.session_at = Some(chrono::Utc::now() - chrono::Duration::days(300));
    sqlite.store_memory(&stale).unwrap();
    sqlite.archive_memory(stale.id).unwrap();
    let dir = tempfile::tempdir().unwrap();
    let raw_index = Arc::new(RwLock::new(
        RawIndex::open(&dir.path().join("raw")).unwrap(),
    ));
    let coverage = Arc::new(RawCoverage::new(30));
    RawIndexer {
        index: raw_index.clone(),
        archive: None,
        store: sqlite.clone(),
        embed: Arc::new(|texts: &[&str]| Ok(vec![vec![0.0; EMBEDDING_DIM]; texts.len()])),
        pause: std::time::Duration::ZERO,
        coverage: coverage.clone(),
    }
    .pass()
    .unwrap();
    let state = Arc::new(AppState {
        data_dir: std::env::temp_dir().join("second-brain-test-no-device-key"),
        pairing: Default::default(),
        bootstrap: Default::default(),
        device_access: Default::default(),
        gate: Default::default(),
        clock: second_brain_api::clock_gate::ClockGate::open_for_tests(),
        store: sqlite.clone(),
        sqlite: sqlite.clone(),
        indexes: None,
        raw_index: Some(raw_index),
        raw_coverage: Some(coverage),
        archive: None,
        reranker: None,
        tunables: RetrievalTunables::default(),
        embedder: None,
        machine,
        maintenance: Arc::new(RwLock::new(None)),
        repair_tx: None,
        near_duplicate_threshold: 0.97,
        near_duplicates: std::sync::Arc::new(std::sync::RwLock::new(None)),
        health: Arc::new(second_brain_api::health::Health::new(
            chrono::Utc::now(),
            None,
        )),
    });
    let app = routes::router().with_state(state);

    let raw = &status(&app).await["raw_index"];

    assert_eq!(raw["passages"], 0);
    assert_eq!(raw["max_age_days"], 30);
    assert_eq!(raw["skipped_by_age"], 1);
}