use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};
use tokio::net::TcpListener;
use chrono::Utc;
use second_brain_api::at_rest;
use second_brain_api::backfill::{Backfill, LiveIngest, PAUSE_BETWEEN_FILES};
use second_brain_api::bootstrap::Bootstrap;
use second_brain_api::clock_gate::{self, ClockGate};
use second_brain_api::cold_pass::{self, ColdPass};
use second_brain_api::device_access::DeviceAccess;
use second_brain_api::footprint::{FootprintCheck, StorePaths};
use second_brain_api::health::{Health, Windows};
use second_brain_api::identity_report;
use second_brain_api::maintenance::{DrainLoop, WriterSlot};
use second_brain_api::models::{self, IndexHandles};
use second_brain_api::note_cover::NoteCover;
use second_brain_api::progress::ProgressCheck;
use second_brain_api::raw_index::{
PAUSE_AFTER_EMBED, RawCoverage, RawIndexer, open_or_rebuild_keyed,
};
use second_brain_api::sources::{GitHub, PullRequests};
use second_brain_api::startup::Startup;
use second_brain_api::store_compaction::Compaction;
use second_brain_api::store_encryption::StoreEncryptor;
use second_brain_api::sync_loop::SyncLoop;
use second_brain_api::watcher::Watcher;
use second_brain_api::write_gate::WriteGate;
use second_brain_api::write_queue::{self, WriteQueue};
use second_brain_api::{AppState, alert, file_modes, sources, stored_redaction, vaults, watcher};
use second_brain_core::api_token;
use second_brain_core::cold::{self, ColdTier, EnvelopeArchiveKeys};
use second_brain_core::config::Config;
use second_brain_core::embedding::DocumentEmbedder;
use second_brain_core::index::raw;
use second_brain_core::keys;
use second_brain_core::machine::{self, MachineIdentity};
use second_brain_core::sealed::SealKeys;
use second_brain_core::sqlite_store::encrypt_migration::remove_plaintext_copies;
use second_brain_core::sqlite_store::{AtRestPurpose, SqliteStore};
use second_brain_core::store::StoreAdmin;
use second_brain_core::transcript_archive::TranscriptArchive;
use second_brain_sync::keyring::{self, Continuity};
use second_brain_sync::mdns::Net;
fn main() {
second_brain_api::heap::cap_arenas();
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.expect("cannot start the async runtime")
.block_on(run());
}
async fn run() {
unsafe { libc::umask(0o077) };
second_brain_api::logging::init();
match second_brain_api::open_files::raise_open_file_limit() {
Ok(limit) => tracing::info!("open-file limit {} (started at {})", limit.to, limit.from),
Err(e) => tracing::warn!("cannot raise the open-file limit: {e}"),
}
let bind = std::env::var("SECOND_BRAIN_BIND").unwrap_or_else(|_| "127.0.0.1:7200".to_string());
let listener = match TcpListener::bind(&bind).await {
Ok(l) => l,
Err(e) => {
tracing::error!(
"cannot listen on {bind}: {e}; another second-brain daemon probably holds it"
);
std::process::exit(1);
}
};
let startup = Startup::new();
let server = tokio::spawn(startup.clone().serve(listener));
tracing::info!("listening on {bind}");
let db_path = std::env::var("SECOND_BRAIN_DB")
.map(PathBuf::from)
.unwrap_or_else(|_| {
let home = std::env::var("HOME").unwrap_or_else(|_| "/tmp".to_string());
PathBuf::from(home).join(".second-brain").join("brain.db")
});
if let Some(parent) = db_path.parent() {
std::fs::create_dir_all(parent).ok();
}
let data_dir = machine::data_dir().expect("cannot determine home directory");
let api_token = match api_token::ensure(&data_dir) {
Ok(token) => token,
Err(e) => {
tracing::error!(
"cannot create the API token in {}: {e:#}",
data_dir.display()
);
std::process::exit(1);
}
};
let config_dir = MachineIdentity::config_dir().expect("cannot determine home directory");
let clock = ClockGate::system(&data_dir);
clock.spawn_ticker();
startup.phase("opening the database");
let key_storage = keys::KeyStorage::detect_for(&data_dir);
tracing::info!("device key storage: {}", key_storage.name());
let hardware = check_hardware(&data_dir, &config_dir, &key_storage);
let machine = MachineIdentity::load_or_create(&config_dir)
.expect("failed to load or create machine identity");
tracing::info!("machine identity: {} ({})", machine.name, machine.id);
match remove_plaintext_copies(&data_dir, None) {
Ok(0) => {}
Ok(n) => tracing::info!("removed {n} plaintext store copies left from before the start"),
Err(e) => tracing::warn!("cannot remove plaintext store copies: {e:#}"),
}
let (sqlite, encryption) =
match keys::open_device_store(&db_path, &data_dir, machine.id.clone(), &key_storage) {
Ok(opened) => (
Arc::new(opened.store.with_wall_clock(clock.clone())),
opened.encryption,
),
Err(e) => {
tracing::error!("failed to open database at {}: {e:#}", db_path.display());
std::process::exit(1);
}
};
tracing::info!("store encryption: {encryption:?}");
let store: Arc<dyn StoreAdmin> = sqlite.clone();
store
.register_machine(&machine)
.expect("failed to register machine identity");
if let Err(e) = keys::ensure_device_keys_in(&sqlite, &data_dir, &key_storage) {
tracing::error!("device keys unavailable: {e:#}");
}
let hardware = match hardware {
Some(keys::HardwareCheck::NoKeys) => match keys::binding_after_creation(&data_dir) {
Ok(check) => {
log_hardware(&check);
Some(check)
}
Err(e) => {
tracing::error!(
"cannot read the hardware the new device keys were bound to: {e:#}"
);
None
}
},
checked => checked,
};
let queue_identity = write_queue::publish_device_identity(&data_dir, &key_storage);
match keyring::migrate_sync_key(&sqlite, &data_dir, &machine.name, clock.wall_micros()) {
Ok(Continuity::Migrated { identity }) => {
tracing::info!(
"sync.key became epoch 0 of sync identity {}",
identity.as_str()
)
}
Ok(Continuity::Ignored { reason }) => tracing::warn!("{reason}"),
Ok(Continuity::NoKeyFile) => {}
Err(e) => tracing::error!("cannot carry sync.key over to the device key: {e:#}"),
}
let cold_tier = attach_cold_tier(&sqlite, &db_path, &data_dir);
tracing::info!("database opened at {}", db_path.display());
let (config, config_loaded) = match Config::load_default() {
Ok(c) => (c, true),
Err(e) => {
tracing::warn!("config.toml unusable, running with defaults: {e:#}");
(Config::default(), false)
}
};
let llm_configured = !config_loaded || config.llm.is_some();
let health = Arc::new(
Health::new(
Utc::now(),
alert::start(&config.alert, machine.name.clone()),
)
.with_windows(Windows::from_config(&config.alert)),
);
clock_gate::report_to(&clock, health.clone(), sqlite.clone());
health.key_storage_detected(key_storage.name(), Utc::now());
health.hardware_checked(
hardware
.as_ref()
.map_or("failed", keys::HardwareCheck::name),
Utc::now(),
);
identity_report::report(&sqlite, &data_dir, &health);
startup.phase("loading the embedding model");
let embedder = models::load_embedder();
startup.phase("opening the search indexes");
let index_dir = db_path.with_extension("indexes");
let index_keys = at_rest_keys(&sqlite, AtRestPurpose::Indexes);
let index_handles =
match models::open_index_handles(&index_dir, &sqlite, index_keys.as_ref(), |n| {
startup.phase(format!("rebuilding search indexes for {n} memories"))
}) {
Ok(h) => Some(h),
Err(e) => {
tracing::warn!(
"search indexes unavailable at {}: {e}; recall falls back to the legacy engine",
index_dir.display()
);
health.indexes_unavailable(&format!("{e:#}"), Utc::now());
None
}
};
let indexes = index_handles.as_ref().map(|h| h.reader.clone());
let archive_dir = data_dir.join("transcripts");
let archive_keys = at_rest_keys(&sqlite, AtRestPurpose::TranscriptArchive);
let opened_archive = match archive_keys {
Some(keys) => TranscriptArchive::open_sealed(&archive_dir, keys),
None => TranscriptArchive::open(&archive_dir),
};
let archive = match opened_archive {
Ok(a) => Some(Arc::new(a)),
Err(e) => {
tracing::error!(
"cannot open the transcript archive at {}: {e:#}",
archive_dir.display()
);
None
}
};
startup.phase("opening the raw transcript index");
let raw_dir = raw::dir_for_db(&db_path);
let raw_keys = at_rest_keys(&sqlite, AtRestPurpose::RawIndex);
let raw_index = match open_or_rebuild_keyed(&raw_dir, raw_keys.as_ref()) {
Ok(index) => Some(Arc::new(RwLock::new(index))),
Err(e) => {
tracing::warn!(
"raw transcript index unavailable at {}: {e:#}",
raw_dir.display()
);
None
}
};
for (path, e) in file_modes::make_private(&data_dir, &db_path, &index_dir) {
tracing::warn!("could not make {} private: {e}", path.display());
}
startup.phase("loading the reranker");
let reranker = models::load_reranker(indexes.is_some());
startup.phase("starting background tasks");
let maintenance = Arc::new(RwLock::new(None));
let near_duplicates = Arc::new(RwLock::new(None));
let mut repair_tx = None;
if let Some(IndexHandles {
reader,
writer,
claim,
}) = index_handles
{
let (tx, requests) = std::sync::mpsc::channel();
repair_tx = Some(tx);
DrainLoop {
store: sqlite.clone(),
reader,
writer: WriterSlot::new(writer, claim),
embedder: embedder.clone(),
summary: maintenance.clone(),
near_duplicates: near_duplicates.clone(),
near_duplicate_threshold: config.distill.near_duplicate_threshold,
config: config.maintenance.clone(),
requests,
health: health.clone(),
}
.spawn();
}
let raw_coverage = raw_index
.as_ref()
.map(|_| Arc::new(RawCoverage::new(config.raw_index.max_age_days)));
if let Some(index) = &raw_index {
at_rest::Sealer::new(sqlite.clone(), AtRestPurpose::RawIndex, index.clone()).spawn();
}
if let Some(archive) = &archive {
at_rest::Sealer::new(
sqlite.clone(),
AtRestPurpose::TranscriptArchive,
archive.clone(),
)
.spawn();
}
if cold_tier {
ColdPass {
store: sqlite.clone(),
interval: cold_pass::PASS_INTERVAL,
pause: cold_pass::PAUSE_BETWEEN_SEGMENTS,
}
.spawn();
}
if let (Some(index), Some(coverage), Some(embedder)) = (&raw_index, &raw_coverage, &embedder) {
let embedder = embedder.clone();
RawIndexer {
index: index.clone(),
archive: archive.clone(),
store: sqlite.clone(),
embed: Arc::new(move |texts: &[&str]| embedder.embed_batch(texts)),
pause: PAUSE_AFTER_EMBED,
coverage: coverage.clone(),
}
.spawn();
}
let bootstrap = Arc::new(Bootstrap::start(&sqlite, &data_dir));
let live = Arc::new(LiveIngest::default());
Watcher {
sqlite: sqlite.clone(),
embedder: embedder.clone(),
machine_id: machine.id.clone(),
health: health.clone(),
archive: archive.clone(),
live: live.clone(),
clock: clock.clone(),
bootstrap: bootstrap.clone(),
}
.spawn();
vaults::spawn(
sqlite.clone(),
embedder.clone(),
config.sources.obsidian_vaults.clone(),
machine.id.clone(),
clock.clone(),
bootstrap.clone(),
);
Backfill {
store: sqlite.clone(),
embedder: embedder.clone(),
machine_id: machine.id.clone(),
projects_dir: watcher::projects_dir(),
archive: archive.clone(),
live,
health: health.clone(),
pause: PAUSE_BETWEEN_FILES,
clock: clock.clone(),
}
.spawn_gated(bootstrap.clone());
sources::Sources {
store: sqlite.clone(),
embedder: embedder.clone().map(|e| e as Arc<dyn DocumentEmbedder>),
machine_id: machine.id.clone(),
pull_requests: GitHub::from_env(tokio::runtime::Handle::current())
.map(|g| Arc::new(g) as Arc<dyn PullRequests>),
}
.spawn(bootstrap.clone());
if !llm_configured {
NoteCover {
store: sqlite.clone(),
}
.spawn();
}
let mut gate = WriteGate::new(data_dir.join(write_queue::DIR_NAME));
if let Some(identity) = &queue_identity {
gate = gate.sealing_to(identity.to_public());
}
let gate = Arc::new(gate);
let mut compaction = Compaction::new(
sqlite.clone(),
gate.clone(),
repair_tx.clone(),
health.clone(),
);
compaction.awaits_encryption = encryption == keys::StoreEncryption::Pending;
if let Err(e) = stored_redaction::spawn_pass(sqlite.clone(), embedder.clone(), compaction) {
tracing::error!("cannot start the stored-content redaction pass: {e}");
}
models::spawn_idle_unload(embedder.clone(), reranker.clone());
let progress = ProgressCheck {
store: sqlite.clone(),
data_dir: data_dir.clone(),
health: health.clone(),
llm_configured,
};
let discovery = Net::Lan;
let pairing = Arc::new(second_brain_api::pairing::Pairing::new(
second_brain_api::pairing::PairingConfig {
discovery: Some(discovery.clone()),
..Default::default()
},
));
second_brain_api::pairing::spawn_route_advertiser(
sqlite.clone(),
pairing.clone(),
machine.name.clone(),
data_dir.clone(),
);
let _mdns_responder = second_brain_api::discovery::spawn_responder(
&discovery,
sqlite.clone(),
pairing.clone(),
machine.name.clone(),
);
let device_access = Arc::new(DeviceAccess::for_this_user());
device_access.sweep(&sqlite, &data_dir);
SyncLoop::over_ssh(
data_dir.clone(),
sqlite.clone(),
gate.clone(),
clock.clone(),
health.clone(),
device_access.clone(),
bootstrap.clone(),
)
.with_discovery(discovery)
.spawn();
second_brain_api::log_floor::spawn(sqlite.clone(), gate.clone(), health.clone());
second_brain_api::snapshot_merge::spawn_resumer(
sqlite.clone(),
data_dir.clone(),
gate.clone(),
health.clone(),
);
second_brain_api::asides::spawn(data_dir.clone(), health.clone());
StoreEncryptor::new(
sqlite.clone(),
gate.clone(),
health.clone(),
data_dir.clone(),
key_storage.clone(),
encryption,
)
.spawn();
let state = Arc::new(AppState {
store,
sqlite,
indexes,
raw_index,
raw_coverage,
archive,
reranker,
tunables: second_brain_api::daemon_tunables(config.retrieval.tunables()),
embedder,
machine,
maintenance,
repair_tx,
near_duplicate_threshold: config.distill.near_duplicate_threshold,
near_duplicates,
health: health.clone(),
gate: gate.clone(),
clock,
data_dir: data_dir.clone(),
pairing,
bootstrap,
device_access,
});
FootprintCheck {
paths: StorePaths {
raw_index: raw_dir.clone(),
db: db_path,
indexes: index_dir,
archive: archive_dir,
},
state: state.clone(),
health: health.clone(),
}
.spawn();
let app = second_brain_api::app(state, api_token.clone());
let announced = health.clone();
tokio::spawn(async move {
progress.start().await;
announced.announce(Utc::now());
});
let replayer = WriteQueue::new(
data_dir.join(write_queue::DIR_NAME),
app.clone(),
api_token,
health,
gate,
);
match queue_identity {
Some(identity) => replayer.opening_with(identity),
None => replayer,
}
.spawn();
second_brain_api::self_update::spawn();
startup.ready(app);
tracing::info!("startup finished; serving every route on {bind}");
match server.await {
Ok(Ok(())) => {}
Ok(Err(e)) => {
tracing::error!("serving on {bind} stopped: {e}");
std::process::exit(1);
}
Err(e) => {
tracing::error!("the server task on {bind} ended abnormally: {e}");
std::process::exit(1);
}
}
}
fn at_rest_keys(sqlite: &SqliteStore, purpose: AtRestPurpose) -> Option<SealKeys> {
match sqlite.at_rest_keys(purpose) {
Ok(keys) => keys.map(|k| k.keys),
Err(e) => {
tracing::error!("cannot read the {purpose:?} at-rest keys: {e:#}");
None
}
}
}
fn attach_cold_tier(sqlite: &Arc<SqliteStore>, db_path: &Path, data_dir: &Path) -> bool {
let device = keys::load_device_keys(data_dir).unwrap_or_else(|e| {
tracing::error!("cold tier: device keys unreadable, segments stay sealed: {e:#}");
None
});
let keys = Arc::new(EnvelopeArchiveKeys::new(sqlite, device));
match ColdTier::open(cold::dir_for_db(db_path), keys) {
Ok(tier) => sqlite.attach_cold_tier(Arc::new(tier)),
Err(e) => {
tracing::error!("cold tier unavailable: {e:#}");
false
}
}
}
fn check_hardware(
data_dir: &Path,
config_dir: &Path,
storage: &keys::KeyStorage,
) -> Option<keys::HardwareCheck> {
match keys::check_hardware(data_dir, config_dir, storage) {
Ok(check) => {
log_hardware(&check);
Some(check)
}
Err(e) if e.downcast_ref::<keys::RemintFailed>().is_some() => {
tracing::error!("{e:#}; stopping so this copy never runs as the original");
std::process::exit(1);
}
Err(e) => {
tracing::error!("cannot check the device keys against this hardware: {e:#}");
None
}
}
}
fn log_hardware(check: &keys::HardwareCheck) {
match check {
keys::HardwareCheck::Reminted {
previous_machine_id,
machine_id,
} => tracing::warn!(
"this data dir was made on other hardware, so this device took machine id \
{machine_id} (was {}) and new device keys; pair it again with `sb pair` on a \
device that syncs, then `sb pair <code>` here",
previous_machine_id.as_deref().unwrap_or("unknown")
),
keys::HardwareCheck::Unreadable => tracing::info!(
"no hardware id is readable here, so a copied data dir cannot be told from the \
original"
),
keys::HardwareCheck::Recorded => {
tracing::info!("recorded the hardware the device keys run on")
}
_ => {}
}
}