paperscope 0.2.12

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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
use super::connection::Connection;
use super::schema::init_db;
use crate::core::extract;
use crate::core::fields;
use crate::core::search::serialize_f32_vec;
use crate::core::search::SharedEmbedder;
use crate::core::text::{fts_segment, same_tokens};
use anyhow::{Context, Result};
use std::cell::{Cell, RefCell};
use std::collections::HashSet;
use std::fs;
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct IndexStats {
    pub scanned: usize,
    pub indexed: usize,
    pub unchanged: usize,
    pub skipped: usize,
    pub chunks: usize,
    /// Explicit extraction problems (a PDF page with no text layer, an
    /// unreadable worksheet). Reported, never silently dropped.
    pub warnings: usize,
    /// Entries this pass could not read for lack of permission — a macOS TCC
    /// grant that was never given, a POSIX mode, a Windows ACL. Counted once per
    /// pass and reported as one line per root, never one warning per file.
    #[serde(default)]
    pub denied: usize,
    /// Entries skipped before touching the filesystem because the previous pass
    /// already found them unreadable and the backoff window has not elapsed.
    #[serde(default)]
    pub backed_off: usize,
}

impl IndexStats {
    pub fn merge(&mut self, other: IndexStats) {
        self.scanned += other.scanned;
        self.indexed += other.indexed;
        self.unchanged += other.unchanged;
        self.skipped += other.skipped;
        self.chunks += other.chunks;
        self.warnings += other.warnings;
        self.denied += other.denied;
        self.backed_off += other.backed_off;
    }
}

/// A citable unit of text. Every field is copied from the source: the anchor
/// and section come from the document's own structure, never from inference.
#[derive(Debug, Clone)]
pub struct Chunk {
    pub page: i64,
    pub line_start: i64,
    pub line_end: i64,
    /// Format-specific anchor (`Sheet1!A1:H1`, `slide 2`, `para 4-9`).
    pub anchor: Option<String>,
    /// Declared section path (`Warranty / Coverage`).
    pub section: Option<String>,
    pub content: String,
}

/// Bump when extraction, chunking or the FTS projection changes meaning. A
/// mismatch invalidates every stored chunk, because the old text can no longer
/// be trusted to match what the current extractor would produce.
const INDEXER_VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), "+extract3+chunk1");

pub struct Indexer {
    conn: Connection,
    embedder: Option<SharedEmbedder>,
    max_chunk_words: usize,
    overlap_words: usize,
    ocr_enabled: bool,
}

impl Indexer {
    /// Indexer with the default OCR policy.
    ///
    /// OCR is on when its weights are present, so the daemon and the MCP tools
    /// pick it up without each caller having to remember the flag.
    pub fn new(conn: Connection, embedder: Option<SharedEmbedder>) -> Result<Self> {
        Self::with_ocr(conn, embedder, ocr_by_default())
    }

    /// Whether extraction in this indexer may run OCR.
    pub fn ocr_enabled(&self) -> bool {
        self.ocr_enabled
    }

    pub fn with_ocr(
        conn: Connection,
        embedder: Option<SharedEmbedder>,
        ocr_enabled: bool,
    ) -> Result<Self> {
        init_db(&conn)?;
        invalidate_on_upgrade(&conn)?;
        // Record the embedding model used for this index, so we can detect
        // mismatches at search time.
        if let Some(embedder) = &embedder {
            Self::record_embedding_model(&conn, embedder.model_id())?;
        }
        Ok(Self {
            conn,
            embedder,
            max_chunk_words: 400,
            overlap_words: 40,
            ocr_enabled,
        })
    }

    /// Store the embedding model ID in index_meta. This is called when the
    /// indexer is created with an embedder, so the index knows which model
    /// produced its vectors.
    fn record_embedding_model(conn: &Connection, model_id: &str) -> Result<()> {
        conn.inner().execute(
            "INSERT INTO index_meta (key, value) VALUES ('embedding_model', ?)
             ON CONFLICT(key) DO UPDATE SET value = excluded.value",
            [model_id],
        )?;
        Ok(())
    }

    pub fn connection(&self) -> &Connection {
        &self.conn
    }

    pub fn index_roots(&self, roots: &[PathBuf]) -> Result<IndexStats> {
        let mut stats = IndexStats::default();
        // Two views of the same rows, loaded once instead of querying per file:
        //
        // - `recent` is the backoff: paths tried within the window and skipped
        //   without touching the filesystem.
        // - `known` is every path ever recorded as unreadable, with no time
        //   limit. A row means "denied, and not yet proven readable again", so it
        //   is cleared only when the path is actually read, never because time
        //   passed. The backoff must not be able to expire the evidence.
        let (known, recent) = self.denied_state()?;
        let known = RefCell::new(known);
        // Directories pruned by the filter, counted here because `filter_entry`
        // cannot touch `stats` while the iterator holds it.
        let pruned_dirs = Cell::new(0usize);

        for root in roots {
            if root.is_file() {
                // A file root obeys the same backoff as a file inside a walk, or
                // a single unreadable file in the config would be re-read and
                // re-warned every five minutes — the loop this change exists to
                // remove.
                if recent.contains(root) {
                    stats.backed_off += 1;
                    continue;
                }
                // A root that is itself unreadable gets the same treatment as an
                // unreadable file inside one: reported, remembered, not fatal.
                // Propagating here made `--file` (and `--root <file>`) exit 2
                // while the identical file inside a walked root was fine.
                match self.index_file(root) {
                    Ok(file_stats) => {
                        stats.merge(file_stats);
                        self.forget_recorded(root, &known);
                    }
                    Err(err) if is_permission_denied(&err) => {
                        self.note_denied(root, &format!("{err:#}"));
                        known.borrow_mut().insert(root.clone());
                        stats.denied += 1;
                        tracing::warn!("{}", denied_summary(root, 1));
                    }
                    Err(err) => return Err(err),
                }
                continue;
            }
            let mut root_denied = 0usize;
            for entry in walkdir::WalkDir::new(root)
                .follow_links(false)
                .into_iter()
                // Hidden entries are skipped; a directory inside its backoff
                // window is pruned whole, while a denied *file* stays in the walk
                // so it can be counted as deferred rather than vanishing.
                .filter_entry(|e| {
                    if is_hidden(e.file_name()) {
                        return false;
                    }
                    if e.file_type().is_dir() && recent.contains(e.path()) {
                        pruned_dirs.set(pruned_dirs.get() + 1);
                        return false;
                    }
                    true
                })
            {
                let entry = match entry {
                    Ok(e) => e,
                    Err(e) => {
                        // A directory we may not read is not a per-file problem:
                        // remember it, keep walking, and say it once at the end
                        // of the root. One line per denied entry is how a single
                        // revoked grant became 1,070 warnings per five minutes.
                        if is_walk_permission_denied(&e) {
                            if let Some(path) = e.path() {
                                self.note_denied(path, &format!("{e}"));
                                let first_time = known.borrow_mut().insert(path.to_path_buf());
                                if first_time {
                                    root_denied += 1;
                                }
                            } else {
                                root_denied += 1;
                            }
                        } else {
                            // Only a real refusal is a permission problem. A path
                            // that vanished mid-walk is a walk error, and telling
                            // the user to grant Full Disk Access for it would be a
                            // lie they cannot act on. `walkdir::Error`'s `Display`
                            // already names the path and the io error; the `#`
                            // alternate flag is meaningless on it.
                            tracing::warn!("walk error: {e}");
                        }
                        stats.skipped += 1;
                        continue;
                    }
                };
                // Anything we can now see is no longer unreadable. Clearing here
                // (only for paths that were recorded) is what replaces the old
                // time-based expiry, and keeps the table bounded.
                self.forget_recorded(entry.path(), &known);
                // `follow_links(false)` means symlinks are neither files nor
                // directories here, so they are never followed.
                if entry.file_type().is_file() {
                    if recent.contains(entry.path()) {
                        stats.backed_off += 1;
                        continue;
                    }
                    // One unreadable file must not abort a whole directory.
                    match self.index_file(entry.path()) {
                        Ok(file_stats) => stats.merge(file_stats),
                        Err(err) => {
                            if is_permission_denied(&err) {
                                self.note_denied(entry.path(), &format!("{err:#}"));
                                let first_time =
                                    known.borrow_mut().insert(entry.path().to_path_buf());
                                if first_time {
                                    root_denied += 1;
                                }
                            } else {
                                tracing::warn!("{}: {err:#}", entry.path().display());
                            }
                            stats.skipped += 1;
                        }
                    }
                }
            }
            stats.denied += root_denied;
            if root_denied > 0 {
                tracing::warn!("{}", denied_summary(root, root_denied));
            }
        }
        stats.backed_off += pruned_dirs.get();
        // Drop rows for paths that no longer exist at all. This is safe because
        // every row was written after a `stat` succeeded: a path that answers
        // `exists()` is still there, denied or not. Age alone never expires a row.
        if let Err(err) = self.forget_vanished_unreadable() {
            tracing::warn!("could not tidy the unreadable cache: {err:#}");
        }
        Ok(stats)
    }

    pub fn index_file(&self, path: &Path) -> Result<IndexStats> {
        let mut stats = IndexStats {
            scanned: 1,
            ..Default::default()
        };
        let path_string = path.to_string_lossy().to_string();
        if !extract::is_supported(&path_string) {
            stats.skipped = 1;
            return Ok(stats);
        }
        let metadata = std::fs::symlink_metadata(path)?;
        if metadata.file_type().is_symlink() || !metadata.is_file() {
            stats.skipped = 1;
            return Ok(stats);
        }
        // Respect each extractor's own size cap instead of a single global cap,
        // so a 100 MB PDF isn't skipped just because the default is 64 MB (C3).
        // Find every extractor that claims this extension and use the largest cap.
        let mut max_allowed: u64 = 0;
        for extractor in extract::extractors() {
            if extractor
                .extensions()
                .contains(&extract::extension_of(&path_string).as_str())
            {
                max_allowed = max_allowed.max(extractor.capabilities().max_file_bytes);
            }
        }
        if max_allowed == 0 || metadata.len() > max_allowed {
            stats.skipped = 1;
            return Ok(stats);
        }

        let mtime = metadata
            .modified()?
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis() as i64;
        let size = metadata.len() as i64;

        // Cheap pre-check: identical source path + mtime + size means nothing to
        // do. Archive members share the outer archive's mtime/size.
        let unchanged: Option<i64> = self
            .conn
            .inner()
            .query_row(
                "SELECT 1 FROM chunks WHERE source_path = ? AND mtime = ? AND size = ? LIMIT 1",
                rusqlite::params![path_string, mtime, size],
                |row| row.get(0),
            )
            .optional()?;
        if unchanged.is_some() {
            stats.unchanged = 1;
            return Ok(stats);
        }

        let bytes = std::fs::read(path).with_context(|| format!("reading {path_string}"))?;
        let hash = blake3::hash(&bytes);

        // A new mtime or size does not always mean new content: `touch -r`,
        // restores from backup and rsync all preserve bytes. If the content hash
        // still matches, only the recorded mtime/size change and no parsing,
        // chunking or embedding work is repeated.
        let stored_hash: Option<Vec<u8>> = self
            .conn
            .inner()
            .query_row(
                "SELECT content_hash FROM chunks WHERE source_path = ? LIMIT 1",
                [&path_string],
                |row| row.get(0),
            )
            .optional()?;
        if stored_hash.as_deref() == Some(hash.as_bytes().as_slice()) {
            self.conn.inner().execute(
                "UPDATE chunks SET mtime = ?, size = ? WHERE source_path = ?",
                rusqlite::params![mtime, size, path_string],
            )?;
            stats.unchanged = 1;
            return Ok(stats);
        }

        let docs = match extract::extract_named(&path_string, &bytes, self.ocr_enabled)? {
            Some(docs) => docs,
            None => {
                stats.skipped = 1;
                return Ok(stats);
            }
        };

        let tx = self.conn.inner().unchecked_transaction()?;
        tx.execute(
            "DELETE FROM chunks_vec WHERE rowid IN (SELECT id FROM chunks WHERE source_path = ?)",
            [&path_string],
        )?;
        tx.execute("DELETE FROM chunks WHERE source_path = ?", [&path_string])?;

        let mut inserted = 0usize;
        let mut warnings = 0usize;
        for doc in &docs {
            for warning in &doc.warnings {
                tracing::warn!("{path_string}: {warning}");
                warnings += 1;
            }
            let chunks = chunk_blocks(&doc.blocks, self.max_chunk_words, self.overlap_words);
            for chunk in &chunks {
                // `fts_text` / `fts_section` hold only text that segmentation
                // actually changed. Latin text segments to itself, and storing
                // that copy doubled the size of most indexes; the triggers fall
                // back to `content` and `section` instead.
                //
                // Compare whitespace-separated tokens, not bytes: segmentation
                // normalises whitespace, and FTS5 tokenises newlines and spaces
                // identically, so `a\nb` and `a b` produce the same index and
                // must not be stored twice.
                let mut fts_text = fts_segment(&chunk.content);
                if same_tokens(&fts_text, &chunk.content) {
                    fts_text.clear();
                }
                let section = chunk.section.as_deref().unwrap_or("");
                let mut fts_section = fts_segment(section);
                if same_tokens(&fts_section, section) {
                    fts_section.clear();
                }
                let metadata = fields::analyse(&chunk.content);
                tx.execute(
                    "INSERT INTO chunks
                       (path, source_path, page, line_start, line_end, anchor, section, content,
                        fts_text, fts_section, content_hash, mtime, size, metadata)
                     VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
                    rusqlite::params![
                        doc.path,
                        path_string,
                        chunk.page,
                        chunk.line_start,
                        chunk.line_end,
                        chunk.anchor,
                        chunk.section,
                        chunk.content,
                        fts_text,
                        fts_section,
                        hash.as_bytes().as_slice(),
                        mtime,
                        size,
                        metadata,
                    ],
                )?;
                let id = tx.last_insert_rowid();
                if let Some(embedder) = &self.embedder {
                    let vector = embedder.embed(&chunk.content)?;
                    tx.execute(
                        "INSERT INTO chunks_vec (rowid, embedding) VALUES (?, ?)",
                        rusqlite::params![id, serialize_f32_vec(&vector)],
                    )?;
                }
                inserted += 1;
            }
        }
        tx.commit()?;

        stats.warnings = warnings;
        if inserted == 0 {
            stats.skipped = 1;
        } else {
            stats.indexed = 1;
            stats.chunks = inserted;
        }
        Ok(stats)
    }

    /// Re-index an already-known file (used by watch mode).
    pub fn reindex_file(&self, path: &Path) -> Result<IndexStats> {
        self.index_file(path)
    }

    /// Every recorded-denied path, plus the subset still inside the backoff
    /// window.
    ///
    /// The two are deliberately different questions. "Should I try this path
    /// again yet?" is a function of time. "May I delete the indexed content
    /// behind this path?" is not: a grant revoked ten minutes ago is still
    /// revoked, and a row that expires by age hands the pruner a false all-clear.
    fn denied_state(&self) -> Result<(HashSet<PathBuf>, HashSet<PathBuf>)> {
        let cutoff = now_unix() - DENIED_BACKOFF.as_secs() as i64;
        let mut stmt = self
            .conn
            .inner()
            .prepare("SELECT path, last_attempt FROM unreadable")?;
        let rows = stmt.query_map([], |row| {
            Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)?))
        })?;
        let mut known = HashSet::new();
        let mut recent = HashSet::new();
        for row in rows {
            let (path, last_attempt) = row?;
            let path = PathBuf::from(path);
            if last_attempt > cutoff {
                recent.insert(path.clone());
            }
            known.insert(path);
        }
        Ok((known, recent))
    }

    /// Every path recorded as unreadable, regardless of how long ago it was
    /// tried. The pruner's guard.
    fn unreadable_paths(&self) -> Result<Vec<PathBuf>> {
        let mut stmt = self.conn.inner().prepare("SELECT path FROM unreadable")?;
        let rows = stmt.query_map([], |row| row.get::<_, String>(0))?;
        Ok(rows
            .collect::<std::result::Result<Vec<_>, _>>()?
            .into_iter()
            .map(PathBuf::from)
            .collect())
    }

    /// Clear a path we have just proven readable. Only paths in `known` are
    /// touched, so this costs nothing for the ordinary case.
    fn forget_recorded(&self, path: &Path, known: &RefCell<HashSet<PathBuf>>) {
        // `remove` needs a mutable borrow; take it, then release it before the
        // database call so no borrow is held across it.
        let was_recorded = known.borrow_mut().remove(path);
        if !was_recorded {
            return;
        }
        if let Err(err) = self.forget_unreadable(path) {
            tracing::warn!(
                "could not clear the unreadable mark on {}: {err:#}",
                path.display()
            );
        }
    }

    fn forget_unreadable(&self, path: &Path) -> Result<()> {
        self.conn.inner().execute(
            "DELETE FROM unreadable WHERE path = ?1",
            [path.to_string_lossy()],
        )?;
        Ok(())
    }

    /// Drop rows whose path is gone from the filesystem entirely.
    ///
    /// Safe because every row was written after a `stat` succeeded: if the path
    /// still answers `exists()`, it is there — denied or not — and its row stays.
    /// A deleted file or directory can never be relevant again.
    fn forget_vanished_unreadable(&self) -> Result<()> {
        let mut stale = Vec::new();
        for path in self.unreadable_paths()? {
            if !path.exists() {
                stale.push(path);
            }
        }
        for path in stale {
            self.forget_unreadable(&path)?;
        }
        Ok(())
    }

    /// Remember that `path` could not be read, and when it was last tried.
    ///
    /// Bookkeeping must never fail an indexing run: a locked database here would
    /// turn "one file is unreadable" into "the whole pass failed", which is a
    /// worse outcome than losing the backoff for one pass.
    fn note_denied(&self, path: &Path, reason: &str) {
        if let Err(err) = self.remember_unreadable(path, reason) {
            tracing::warn!(
                "could not record {} as unreadable ({err:#}); it will be retried next pass",
                path.display()
            );
        }
    }

    fn remember_unreadable(&self, path: &Path, reason: &str) -> Result<()> {
        let now = now_unix();
        self.conn.inner().execute(
            "INSERT INTO unreadable (path, reason, first_seen, last_attempt)
             VALUES (?1, ?2, ?3, ?3)
             ON CONFLICT(path) DO UPDATE SET
                 reason = excluded.reason,
                 last_attempt = excluded.last_attempt",
            rusqlite::params![path.to_string_lossy(), reason, now],
        )?;
        Ok(())
    }

    /// Drop every chunk belonging to files that no longer exist on disk.
    pub fn prune_missing(&self) -> Result<usize> {
        let mut stmt = self
            .conn
            .inner()
            .prepare("SELECT DISTINCT source_path FROM chunks")?;
        let paths: Vec<String> = stmt
            .query_map([], |row| row.get(0))?
            .collect::<std::result::Result<_, _>>()?;
        drop(stmt);

        // One `stat` per indexed document is unavoidable, but the deletes are
        // not: batching them into a single transaction turns N fsyncs into one.
        //
        // A path inside an unreadable directory answers `exists() == false` too,
        // so pruning on that alone deleted the indexed content of exactly the
        // roots this change exists to tolerate (revoked TCC grant → "1 removed"
        // → search returns nothing). Never prune what we could not read.
        //
        // No backoff cutoff here, on purpose. The pruner does not know which
        // roots this pass walked — `paper index --file x`, `--root <another root>`
        // and `--watch` all prune without walking the denied root — so a
        // ten-minute window would hand it a false all-clear the moment the rows
        // aged out. A row means "denied and not since proven readable"; only a
        // successful read removes it.
        let unreadable = self.unreadable_paths()?;
        let hidden_by_permission = |path: &str| {
            let path = Path::new(path);
            unreadable.iter().any(|denied| path.starts_with(denied))
        };
        let missing: Vec<String> = paths
            .into_iter()
            .filter(|path| {
                !path.is_empty() && !Path::new(path).exists() && !hidden_by_permission(path)
            })
            .collect();
        if missing.is_empty() {
            return Ok(0);
        }

        let tx = self.conn.inner().unchecked_transaction()?;
        let mut removed = 0usize;
        {
            let mut del_vec = tx.prepare(
                "DELETE FROM chunks_vec WHERE rowid IN
                     (SELECT id FROM chunks WHERE source_path = ?1)",
            )?;
            let mut del_chunks = tx.prepare("DELETE FROM chunks WHERE source_path = ?1")?;
            for path in &missing {
                // Re-verify existence immediately before delete so a file
                // recreated between the check and the delete is not pruned
                // (TOCTOU, I8).
                if !path.is_empty() && !Path::new(path).exists() {
                    del_vec.execute([path])?;
                    del_chunks.execute([path])?;
                    removed += 1;
                }
            }
        }
        tx.commit()?;
        // Checkpoint and truncate the WAL so deleted pages are reclaimed and
        // the -wal/-shm files don't grow unbounded.
        self.conn
            .inner()
            .execute_batch("PRAGMA wal_checkpoint(TRUNCATE);")?;
        Ok(removed)
    }
}

use rusqlite::OptionalExtension;

/// Wipe stored chunks when the extractor/chunker version changes.
///
/// Without this, upgrading Paper would leave chunks produced by an older parser
/// in place, and their anchors could point at text the current parser no longer
/// produces. A full reindex is the honest outcome; it happens once per upgrade.
fn invalidate_on_upgrade(conn: &Connection) -> Result<()> {
    let stored: Option<String> = conn
        .inner()
        .query_row(
            "SELECT value FROM index_meta WHERE key = 'indexer_version'",
            [],
            |row| row.get(0),
        )
        .optional()?;
    if stored.as_deref() == Some(INDEXER_VERSION) {
        return Ok(());
    }
    let tx = conn.inner().unchecked_transaction()?;
    tx.execute("DELETE FROM chunks_vec", [])?;
    tx.execute("DELETE FROM chunks", [])?;
    tx.execute(
        "INSERT INTO index_meta (key, value) VALUES ('indexer_version', ?)
         ON CONFLICT(key) DO UPDATE SET value = excluded.value",
        [INDEXER_VERSION],
    )?;
    tx.commit()?;
    Ok(())
}

fn is_hidden(name: &std::ffi::OsStr) -> bool {
    name.to_string_lossy().starts_with('.')
}

/// How long an unreadable path is left alone before it is tried again. Long
/// enough that the five-minute tick does not turn one revoked grant into a
/// warning every pass; short enough that fixing the permission (or granting
/// Full Disk Access) takes effect without restarting anything.
const DENIED_BACKOFF: Duration = Duration::from_secs(10 * 60);

fn now_unix() -> i64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs() as i64)
        .unwrap_or(0)
}

/// Whether an error chain contains the filesystem refusing access. `anyhow`
/// context wraps the kind behind a message, so the chain is walked rather than
/// the text pattern-matched.
fn is_permission_denied(error: &anyhow::Error) -> bool {
    error.chain().any(|cause| {
        cause
            .downcast_ref::<std::io::Error>()
            .is_some_and(|io| io.kind() == std::io::ErrorKind::PermissionDenied)
    })
}

fn is_walk_permission_denied(error: &walkdir::Error) -> bool {
    // Only an actual refusal. `NotFound` is a path that vanished between the
    // readdir and the stat — a real error, but not one a TCC grant fixes, and
    // certainly not one to tell the user to grant Full Disk Access for.
    error
        .io_error()
        .is_some_and(|io| io.kind() == std::io::ErrorKind::PermissionDenied)
}

/// What the user can actually do about a denied path, in words that match the
/// platform they are on. A Linux user sent to System Settings is a bug report.
fn permission_remedy() -> &'static str {
    if cfg!(target_os = "macos") {
        "grant access in System Settings → Privacy & Security → Full Disk Access or Files and \
         Folders"
    } else if cfg!(windows) {
        "grant your user read permission on the folder (Properties → Security)"
    } else {
        "fix the directory permissions (`ls -ld` on the path)"
    }
}

/// The one line a root gets when part of it is unreadable: what happened, how
/// much of it, and what the user can actually do about it.
fn denied_summary(root: &Path, entries: usize) -> String {
    let entry = if entries == 1 { "entry" } else { "entries" };
    format!(
        "{}: {entries} {entry} could not be read (permission denied) — {}; Paper retries in {} \
         minutes and picks the files up on the next pass once access is granted",
        root.display(),
        permission_remedy(),
        DENIED_BACKOFF.as_secs() / 60
    )
}

pub fn is_indexable(path: &Path) -> bool {
    extract::is_supported(&path.to_string_lossy())
}

/// Split text into overlapping chunks while preserving 1-based line numbers so
/// every hit can be cited as `path:line_start-line_end`.
///
/// Markdown headings start a new chunk, which keeps sections from being cut in
/// half and gives the retriever cleaner units.
pub fn chunk_text(text: &str, max_words: usize, overlap_words: usize) -> Vec<Chunk> {
    let max_words = max_words.max(1);
    let overlap = overlap_words.min(max_words - 1);
    let lines: Vec<&str> = text.lines().collect();
    let mut chunks = Vec::new();

    let mut current: Vec<&str> = Vec::new();
    let mut current_words = 0usize;
    let mut start_line = 1usize;

    for (idx, line) in lines.iter().enumerate() {
        let line_no = idx + 1;
        let words = line.split_whitespace().count();
        let heading = is_markdown_heading(line);
        if !current.is_empty() && (current_words + words > max_words || heading) {
            chunks.push(Chunk {
                page: 0,
                line_start: start_line as i64,
                line_end: (line_no - 1) as i64,
                anchor: None,
                section: None,
                content: current.join("\n"),
            });
            // Overlap: keep the tail of the previous chunk for context. A
            // heading is a hard boundary, so it never inherits the old tail.
            let mut kept: Vec<&str> = Vec::new();
            let mut kept_words = 0usize;
            if !heading {
                for prev in current.iter().rev() {
                    let w = prev.split_whitespace().count();
                    if kept_words + w > overlap {
                        break;
                    }
                    kept.insert(0, prev);
                    kept_words += w;
                }
            }
            start_line = line_no.saturating_sub(kept.len());
            current = kept;
            current_words = kept_words;
        }
        if current.is_empty() {
            start_line = line_no;
        }
        current.push(line);
        current_words += words;
    }
    if current.iter().any(|l| !l.trim().is_empty()) {
        chunks.push(Chunk {
            page: 0,
            line_start: start_line as i64,
            line_end: lines.len().max(1) as i64,
            anchor: None,
            section: None,
            content: current.join("\n"),
        });
    }
    chunks
}

fn is_markdown_heading(line: &str) -> bool {
    let trimmed = line.trim_start();
    let hashes = trimmed.chars().take_while(|c| *c == '#').count();
    (1..=6).contains(&hashes) && trimmed.chars().nth(hashes) == Some(' ')
}

/// Turn extracted blocks into citable chunks. Blocks are never merged across
/// anchors or sections, so a citation can always name the exact page/cell/slide
/// and the section it belongs to.
pub fn chunk_blocks(
    blocks: &[extract::Block],
    max_words: usize,
    overlap_words: usize,
) -> Vec<Chunk> {
    let mut out = Vec::new();
    for block in blocks {
        for chunk in chunk_text(&block.text, max_words, overlap_words) {
            let (line_start, line_end) = if block.line_start > 0 {
                (
                    block.line_start + chunk.line_start - 1,
                    block.line_start + chunk.line_end - 1,
                )
            } else {
                (0, 0)
            };
            out.push(Chunk {
                page: block.page,
                line_start,
                line_end,
                anchor: block.anchor.clone(),
                section: block.section.clone(),
                content: chunk.content,
            });
        }
    }
    out
}

/// Convenience used by the CLI: atomically index into a temp database, then
/// rename into place. This guarantees that a crash mid-index never leaves a
/// corrupted `chunks.db`.
pub fn index_with_defaults(
    db_path: &Path,
    roots: &[PathBuf],
    embedder: Option<SharedEmbedder>,
) -> Result<IndexStats> {
    index_with_defaults_atomic(db_path, roots, embedder)
}

/// Internal: perform a full index into `db_path.tmp` then atomically replace
/// `db_path`. On cross-device rename failure, fall back to copy + remove.
fn index_with_defaults_atomic(
    db_path: &Path,
    roots: &[PathBuf],
    embedder: Option<SharedEmbedder>,
) -> Result<IndexStats> {
    let tmp_path = db_path.with_extension("db.tmp");

    // Remove any stale temp file from a previous interrupted run.
    let _ = fs::remove_file(&tmp_path);

    // Open the temp database with creation-time pragmas (page_size, etc.).
    let conn = Connection::open_or_create(&tmp_path).context("opening temp index database")?;
    let stats = Indexer::new(conn, embedder)?.index_roots(roots)?;

    // Commit is implicit in Indexer::index_roots (each file is its own tx).
    // Now atomically promote the temp file.
    atomic_replace(&tmp_path, db_path)?;

    Ok(stats)
}

/// Atomically replace `dst` with `src` using rename, with cross-device fallback.
fn atomic_replace(src: &Path, dst: &Path) -> Result<()> {
    // Try the fast path: same filesystem, atomic rename.
    match fs::rename(src, dst) {
        Ok(()) => {}
        Err(e) if e.kind() == std::io::ErrorKind::CrossesDevices => {
            // Fallback: copy + remove. Not atomic across devices, but the best
            // we can do. The destination is only visible after copy completes.
            fs::copy(src, dst).with_context(|| {
                format!(
                    "copying {} -> {} (cross-device)",
                    src.display(),
                    dst.display()
                )
            })?;
            fs::remove_file(src)
                .with_context(|| format!("removing temp file {}", src.display()))?;
        }
        Err(e) => {
            return Err(e)
                .with_context(|| format!("renaming {} -> {}", src.display(), dst.display()))
        }
    }
    Ok(())
}

/// Whether OCR should run when the caller did not ask explicitly.
fn ocr_by_default() -> bool {
    #[cfg(feature = "ocr")]
    {
        crate::core::extract::ocr::enabled(false)
    }
    #[cfg(not(feature = "ocr"))]
    {
        false
    }
}

#[cfg(test)]
mod tests {
    use super::IndexStats;

    /// A daemon from before this change sends no `denied`/`backed_off`; a newer
    /// client must still read its reply rather than erroring on the missing keys.
    #[test]
    fn stats_from_an_older_daemon_deserialize() {
        let old = r#"{"scanned":3,"indexed":1,"unchanged":0,"skipped":2,"chunks":4,"warnings":0}"#;
        let stats: IndexStats = serde_json::from_str(old).expect("an older payload must parse");
        assert_eq!(stats.indexed, 1);
        assert_eq!((stats.denied, stats.backed_off), (0, 0));
    }

    /// …and a newer daemon's reply still parses on a client that predates the
    /// fields, because the struct does not deny unknown keys.
    #[test]
    fn stats_carry_the_permission_counters_over_the_wire() {
        let stats = IndexStats {
            denied: 2,
            backed_off: 3,
            ..IndexStats::default()
        };
        let json = serde_json::to_string(&stats).expect("serialize");
        let back: IndexStats = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(back, stats);
    }
}