use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use crate::core::paths::Paths;
const STATE_VERSION: &str = "1";
const RETRY_AFTER: Duration = Duration::from_secs(60 * 60);
const LOCK_STALE: Duration = Duration::from_secs(15 * 60);
const STATE_FILE: &str = "bootstrap.state";
const LOCK_FILE: &str = "bootstrap.lock";
const SKIP_COMMANDS: &[&str] = &[
"bootstrap",
"daemon",
"daemon-control",
"doctor",
"models",
"index",
];
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum ServiceState {
Pending,
Installed,
Unavailable,
}
#[derive(Debug)]
struct State {
service: ServiceState,
updated: u64,
}
impl Default for State {
fn default() -> Self {
Self {
service: ServiceState::Pending,
updated: 0,
}
}
}
impl State {
fn load(path: &Path) -> Self {
let Ok(text) = fs::read_to_string(path) else {
return Self::default();
};
if !text
.lines()
.any(|line| line.trim() == format!("version={STATE_VERSION}"))
{
return Self::default();
}
let mut state = Self::default();
for line in text.lines() {
let Some((key, value)) = line.split_once('=') else {
continue;
};
match key.trim() {
"service" => {
state.service = match value.trim() {
"installed" => ServiceState::Installed,
"unavailable" => ServiceState::Unavailable,
_ => ServiceState::Pending,
}
}
"updated" => state.updated = value.trim().parse().unwrap_or(0),
_ => {}
}
}
state
}
fn save(&self, path: &Path) -> io::Result<()> {
let contents = format!(
"version={STATE_VERSION}\nservice={}\nupdated={}\n",
match self.service {
ServiceState::Pending => "pending",
ServiceState::Installed => "installed",
ServiceState::Unavailable => "unavailable",
},
self.updated
);
let tmp = path.with_extension("tmp");
fs::write(&tmp, contents)?;
fs::rename(&tmp, path)
}
fn should_retry(&self, now: u64) -> bool {
match self.service {
ServiceState::Installed => false,
ServiceState::Pending => true,
ServiceState::Unavailable => now.saturating_sub(self.updated) >= RETRY_AFTER.as_secs(),
}
}
}
#[derive(Clone, Copy, Debug, Default)]
struct Switches {
offline: bool,
no_autostart: bool,
ci: bool,
portable: bool,
force: bool,
}
impl Switches {
fn from_process() -> Self {
Self {
offline: present("PAPER_OFFLINE"),
no_autostart: present("PAPER_NO_AUTOSTART"),
ci: present("CI"),
portable: present("PAPER_HOME"),
force: present("PAPER_FORCE_BOOTSTRAP"),
}
}
}
fn present(name: &str) -> bool {
match std::env::var(name) {
Ok(value) => !value.is_empty() && value != "0",
Err(_) => false,
}
}
fn should_run(command: &str, switches: &Switches) -> bool {
if switches.offline || switches.no_autostart || switches.ci {
return false;
}
if switches.portable && !switches.force {
return false;
}
!SKIP_COMMANDS.contains(&command)
}
fn now_unix() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0)
}
struct BootstrapLock {
path: PathBuf,
}
impl BootstrapLock {
fn acquire(path: &Path) -> Option<Self> {
if fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(path)
.is_ok()
{
return Some(Self {
path: path.to_path_buf(),
});
}
let stale = fs::metadata(path)
.and_then(|meta| meta.modified())
.ok()
.and_then(|modified| modified.elapsed().ok())
.map(|age| age > LOCK_STALE)
.unwrap_or(true);
if !stale {
return None;
}
let _ = fs::remove_file(path);
fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(path)
.ok()
.map(|_| Self {
path: path.to_path_buf(),
})
}
}
impl Drop for BootstrapLock {
fn drop(&mut self) {
let _ = fs::remove_file(&self.path);
}
}
pub async fn maybe_autostart(command: &str) {
let switches = Switches::from_process();
if !should_run(command, &switches) {
return;
}
let paths = Paths::new();
if paths.ensure_dirs().is_err() {
return;
}
let state_path = paths.base_dir().join(STATE_FILE);
let lock_path = paths.base_dir().join(LOCK_FILE);
let state = State::load(&state_path);
if !state.should_retry(now_unix()) {
return;
}
let Some(_lock) = BootstrapLock::acquire(&lock_path) else {
return;
};
let state = State::load(&state_path);
if !state.should_retry(now_unix()) {
return;
}
eprintln!(
"paper: first run — setting up background indexing for your documents \
(disable with PAPER_NO_AUTOSTART=1)"
);
let next = match crate::cli::daemon::ensure_service() {
Ok(()) => State {
service: ServiceState::Installed,
updated: now_unix(),
},
Err(error) => {
tracing::warn!("could not install the Paper background service: {error:#}");
State {
service: ServiceState::Unavailable,
updated: now_unix(),
}
}
};
let _ = next.save(&state_path);
}
pub async fn ensure_models(workspace: &crate::cli::Workspace) {
let switches = Switches::from_process();
if switches.offline || switches.no_autostart {
return;
}
#[cfg(feature = "embeddings")]
{
if !workspace.embedding_model_installed() {
tracing::info!("downloading the default embedding model in the background");
if let Err(error) = workspace
.model_manager
.ensure(crate::core::models::DEFAULT_EMBEDDING)
.await
{
tracing::warn!(
"embedding model download failed ({error:#}); continuing with keyword search"
);
}
}
if std::env::var_os("PAPER_NO_RERANKER").is_none()
&& !workspace
.model_manager
.is_installed(crate::core::models::OPTIONAL_RERANKER)
{
tracing::info!("downloading the optional reranker in the background");
if let Err(error) = workspace
.model_manager
.ensure(crate::core::models::OPTIONAL_RERANKER)
.await
{
tracing::warn!("reranker download failed ({error:#}); results stay dense+keyword");
}
}
}
#[cfg(not(feature = "embeddings"))]
let _ = workspace;
#[cfg(feature = "ocr")]
if std::env::var_os("PAPER_NO_OCR").is_none() && !crate::core::extract::ocr::models_present() {
tracing::info!("downloading OCR models in the background");
if let Err(error) = crate::core::extract::ocr::ensure_models().await {
tracing::warn!("OCR model download failed ({error:#}); scanned pages stay unindexed");
}
}
}
pub fn index_is_empty(paths: &Paths) -> bool {
let Ok(connection) = crate::core::index::Connection::open(&paths.db_path()) else {
return true;
};
if crate::core::index::init_db(&connection).is_err() {
return true;
}
connection
.query_i64("SELECT COUNT(*) FROM chunks")
.map(|count| count == 0)
.unwrap_or(true)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn skips_when_disabled() {
let enabled = Switches::default();
assert!(should_run("search", &enabled));
for switches in [
Switches {
offline: true,
..enabled
},
Switches {
no_autostart: true,
..enabled
},
Switches {
ci: true,
..enabled
},
] {
assert!(!should_run("search", &switches));
}
}
#[test]
fn skips_portable_installs_unless_forced() {
let portable = Switches {
portable: true,
..Switches::default()
};
assert!(!should_run("search", &portable));
assert!(should_run(
"search",
&Switches {
force: true,
..portable
}
));
}
#[test]
fn skips_commands_that_manage_the_daemon() {
for command in SKIP_COMMANDS {
assert!(!should_run(command, &Switches::default()), "{command}");
}
for command in ["search", "answer", "serve", "aggregate"] {
assert!(should_run(command, &Switches::default()), "{command}");
}
}
#[test]
fn state_round_trips() {
let dir = tempfile::tempdir().expect("tempdir");
let path = dir.path().join(STATE_FILE);
let mut state = State::load(&path);
assert_eq!(state.service, ServiceState::Pending);
assert!(state.should_retry(0));
state.service = ServiceState::Installed;
state.updated = 42;
state.save(&path).expect("save");
let loaded = State::load(&path);
assert_eq!(loaded.service, ServiceState::Installed);
assert_eq!(loaded.updated, 42);
assert!(!loaded.should_retry(10_000));
}
#[test]
fn failed_installs_retry_after_the_backoff() {
let dir = tempfile::tempdir().expect("tempdir");
let path = dir.path().join(STATE_FILE);
let state = State {
service: ServiceState::Unavailable,
updated: 1_000,
};
state.save(&path).expect("save");
let loaded = State::load(&path);
assert!(!loaded.should_retry(1_000 + RETRY_AFTER.as_secs() - 1));
assert!(loaded.should_retry(1_000 + RETRY_AFTER.as_secs()));
}
#[test]
fn unknown_state_file_is_ignored() {
let dir = tempfile::tempdir().expect("tempdir");
let path = dir.path().join(STATE_FILE);
fs::write(&path, "version=0\nservice=installed\n").expect("write");
assert_eq!(State::load(&path).service, ServiceState::Pending);
}
#[test]
fn empty_index_is_reported_as_empty() {
let dir = tempfile::tempdir().expect("tempdir");
let paths = Paths::with_base(dir.path());
paths.ensure_dirs().expect("dirs");
assert!(index_is_empty(&paths));
}
}