paperscope 0.2.7

Local-first AI document search with exact page and line citations. One binary: CLI + MCP server for LLMs. PDF, DOCX and 15+ formats, no cloud, no telemetry.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! First-run automation.
//!
//! The installer cannot set Paper up: `install.sh`, `cargo install` and the
//! Claude plugin have no common post-install hook, and an installer should not
//! start a long-lived service. So the work happens here, on the first ordinary
//! command, and only once.
//!
//! Effects, in order:
//!   1. register the background indexer as a user service (launchd/systemd), or
//!      fall back to a detached daemon for this login session;
//!   2. the daemon downloads the default models in the background (the
//!      embedding model first, then the optional reranker);
//!   3. the daemon indexes the auto-discovered roots immediately (instead of
//!      waiting for the first five-minute scheduler tick).
//!
//! Escape hatches: `PAPER_NO_AUTOSTART=1` (never), `PAPER_OFFLINE=1` (no
//! network at all), `PAPER_FORCE_BOOTSTRAP=1` (run even when `PAPER_HOME` is
//! set), and `paper daemon-control uninstall` (turn it off afterwards).
//!
//! `CI` suppresses it too: a build machine must never index its own files.

use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use crate::core::paths::Paths;

/// Bumped when the state file layout changes; an older file is ignored.
const STATE_VERSION: &str = "1";
/// Do not retry a failed service install more often than this.
const RETRY_AFTER: Duration = Duration::from_secs(60 * 60);
/// A lock older than this belongs to a crashed run and may be taken over.
const LOCK_STALE: Duration = Duration::from_secs(15 * 60);

const STATE_FILE: &str = "bootstrap.state";
const LOCK_FILE: &str = "bootstrap.lock";

/// Commands that must not trigger onboarding: they either manage the daemon
/// themselves, are diagnostics, or are explicitly user-driven work.
const SKIP_COMMANDS: &[&str] = &[
    "bootstrap",
    "daemon",
    "daemon-control",
    "doctor",
    "models",
    "index",
];

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum ServiceState {
    /// Never attempted.
    Pending,
    /// Service installed, or a session daemon was started successfully.
    Installed,
    /// Every attempt failed; retried later, but not on every command.
    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(),
        }
    }
}

/// Environment switches, read once so the decision is testable without touching
/// the process environment.
#[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;
    }
    // `PAPER_HOME` marks tests and portable installs; never write services there.
    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)
}

/// Guards the one-time setup against parallel invocations (an editor can start
/// several MCP sessions at once, each calling `paper serve`).
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);
    }
}

/// Called from `main` before dispatching `command`. Never fails the command and
/// never blocks on the network: the daemon does the slow work.
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;
    };

    // Another process may have finished while we waited.
    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);
}

/// Download the default models if they are missing. Runs inside the daemon, so
/// the user's command has already returned. Failure is not fatal: keyword
/// search keeps working and the next daemon start tries again.
///
/// The embedding model comes first because indexing needs it. The reranker is
/// best-effort and second: it only improves ordering, so a failed or skipped
/// download costs accuracy, never functionality. `PAPER_NO_RERANKER=1` keeps
/// the ~1.1 GB reranker off disk entirely (small disk, metered connection).
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;

    // OCR weights are small (~16 MB) and independent of the embedding stack, so
    // they are fetched even in a keyword-only build. A failure only disables
    // OCR; indexing still proceeds with whatever text layers exist.
    #[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");
        }
    }
}

/// True when the index holds no chunks yet, which is the moment to index
/// immediately rather than wait for the five-minute incremental tick.
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));
    }
}