second-brain-api 0.11.0

HTTP API server for second-brain: REST endpoints for recall, remember, and ingest
Documentation
use std::sync::{Arc, Mutex};
use std::time::Duration;

use axum::Json;
use axum::Router;
use axum::extract::State;
use axum::routing::post;
use chrono::Utc;
use second_brain_api::alert;
use second_brain_api::health::{Health, State as HealthState};
use second_brain_api::progress::ProgressCheck;
use second_brain_core::config::AlertConfig;
use second_brain_core::schema::{Memory, MemoryType, SyncState};
use second_brain_core::sqlite_store::SqliteStore;
use second_brain_core::store::{Store, StoreAdmin};
use second_brain_sync::peers;

type Received = Arc<Mutex<Vec<serde_json::Value>>>;

async fn receiver() -> (String, Received) {
    let received: Received = Arc::default();
    let app = Router::new()
        .route(
            "/hook",
            post(
                |State(r): State<Received>, Json(body): Json<serde_json::Value>| async move {
                    r.lock().unwrap().push(body);
                },
            ),
        )
        .with_state(received.clone());
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
    (format!("http://{addr}/hook"), received)
}

async fn wait_for(received: &Received, n: usize) {
    for _ in 0..100 {
        if received.lock().unwrap().len() >= n {
            return;
        }
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
}

struct Device {
    _dir: tempfile::TempDir,
    data_dir: std::path::PathBuf,
    store: Arc<SqliteStore>,
}

fn device() -> Device {
    let dir = tempfile::tempdir().unwrap();
    let data_dir = dir.path().join(".second-brain");
    std::fs::create_dir_all(&data_dir).unwrap();
    let store = SqliteStore::open(&data_dir.join("brain.db"), "machine-a".to_string()).unwrap();
    Device {
        _dir: dir,
        data_dir,
        store: Arc::new(store),
    }
}

fn check(device: &Device, health: Arc<Health>) -> ProgressCheck {
    ProgressCheck {
        store: device.store.clone(),
        data_dir: device.data_dir.clone(),
        health,
    }
}

fn synced_with_a_peer(device: &Device) {
    device
        .store
        .set_sync_state(&SyncState {
            peer_id: "6f1c1d1e-0000-4000-8000-000000000001".to_string(),
            last_seq: 42,
            last_sync_at: Utc::now() - chrono::Duration::days(2),
        })
        .unwrap();
}

async fn announced_on_startup(device: &Device) -> Vec<serde_json::Value> {
    let (url, received) = receiver().await;
    let config = AlertConfig {
        webhook_url: Some(url),
        ..AlertConfig::default()
    };
    let tx = alert::start(&config, "laptop".to_string()).expect("a webhook is set");
    let health = Arc::new(Health::new(Utc::now(), Some(tx)));
    let task = check(device, health.clone()).start().await;
    health.announce(Utc::now());
    wait_for(&received, 1).await;
    tokio::time::sleep(Duration::from_millis(300)).await;
    task.abort();
    received.lock().unwrap().clone()
}

#[tokio::test]
async fn startup_sends_the_ok_state_of_a_solo_device_once() {
    let device = device();
    let bodies = announced_on_startup(&device).await;
    assert_eq!(bodies.len(), 1, "{bodies:?}");
    assert_eq!(bodies[0]["state"], "ok");
    assert!(bodies[0]["reason"].is_null());
    assert_eq!(bodies[0]["machine"], "laptop");
}

#[tokio::test]
async fn startup_sends_a_degraded_state_for_a_device_that_lost_its_peers() {
    let device = device();
    synced_with_a_peer(&device);
    std::fs::write(device.data_dir.join(peers::PEERS_FILE), "").unwrap();
    let bodies = announced_on_startup(&device).await;
    assert_eq!(bodies.len(), 1, "{bodies:?}");
    assert_eq!(bodies[0]["state"], "degraded");
    let reason = bodies[0]["reason"].as_str().unwrap();
    assert!(reason.contains("sync-peers lists no peers"), "{reason}");
}

#[test]
fn a_recorded_sync_success_clears_an_overdue_sync() {
    let device = device();
    synced_with_a_peer(&device);
    std::fs::write(device.data_dir.join(peers::PEERS_FILE), "user@peer\n").unwrap();
    let now = Utc::now();
    let health = Arc::new(Health::new(now, None));
    let check = check(&device, health.clone());

    peers::record_success(&device.data_dir, now - chrono::Duration::hours(30)).unwrap();
    check.run_once(now);
    let report = health.report();
    assert_eq!(report.state, HealthState::Degraded);
    assert!(
        report
            .reason
            .as_deref()
            .unwrap()
            .contains("no sync has succeeded in 30h"),
        "{report:?}"
    );
    assert_eq!(report.sync.peers, Some(1));
    assert!(report.sync.synced_before);

    peers::record_success(&device.data_dir, now).unwrap();
    check.run_once(now);
    let report = health.report();
    assert_eq!(report.state, HealthState::Ok, "{:?}", report.reason);
    assert_eq!(report.sync.last_success_at, Some(now));
}

#[test]
fn raws_waiting_to_be_distilled_are_counted_from_the_store() {
    let device = device();
    let mut raw = Memory::new(
        "a transcript chunk".to_string(),
        MemoryType::Episodic,
        "claude-code".to_string(),
        String::new(),
    );
    raw.created_at = Utc::now() - chrono::Duration::hours(30);
    device.store.store_memory(&raw).unwrap();
    let now = Utc::now();
    let health = Arc::new(Health::new(now, None));
    check(&device, health.clone()).run_once(now);

    let report = health.report();
    assert_eq!(report.state, HealthState::Degraded);
    assert_eq!(report.distill.pending, 1);
    assert!(
        report
            .reason
            .as_deref()
            .unwrap()
            .contains("1 raw memory has waited 30h"),
        "{report:?}"
    );
}

#[test]
fn an_unreadable_peers_list_degrades_once_it_persists() {
    let device = device();
    std::fs::create_dir(device.data_dir.join(peers::PEERS_FILE)).unwrap();
    let now = Utc::now();
    let health = Arc::new(Health::new(now, None));
    let check = check(&device, health.clone());
    check.run_once(now);
    assert_eq!(health.report().state, HealthState::Ok);
    check.run_once(now + chrono::Duration::minutes(10));
    let report = health.report();
    assert_eq!(report.state, HealthState::Degraded);
    assert!(
        report
            .reason
            .as_deref()
            .unwrap()
            .contains("cannot check sync and distillation"),
        "{report:?}"
    );
}