vectorless 0.1.28

Reasoning-native document intelligence engine for AI
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
// Copyright (c) 2026 vectorless developers
// SPDX-License-Identifier: Apache-2.0

//! Change detection for incremental updates.
//!
//! This module provides fine-grained change detection using subtree fingerprints,
//! enabling precise identification of changed nodes without full reprocessing.

use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::path::Path;
use std::time::SystemTime;

use serde::{Deserialize, Serialize};

use crate::document::{DocumentTree, NodeId};
use crate::utils::fingerprint::{Fingerprint, Fingerprinter, NodeFingerprint};

/// Type of change detected.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ChangeType {
    /// Node was added.
    Added,
    /// Node was removed.
    Removed,
    /// Node content changed.
    Modified,
    /// Node structure changed (children added/removed).
    Restructured,
}

impl std::fmt::Display for ChangeType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ChangeType::Added => write!(f, "added"),
            ChangeType::Removed => write!(f, "removed"),
            ChangeType::Modified => write!(f, "modified"),
            ChangeType::Restructured => write!(f, "restructured"),
        }
    }
}

/// A single change in the document.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeChange {
    /// Node ID (from old tree).
    pub node_id: Option<String>,
    /// Node title (for human-readable output).
    pub title: String,
    /// Type of change.
    pub change_type: ChangeType,
    /// Node fingerprint (for modified nodes).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub fingerprint: Option<NodeFingerprint>,
}

impl NodeChange {
    /// Create a new node change.
    pub fn new(node_id: Option<String>, title: String, change_type: ChangeType) -> Self {
        Self {
            node_id,
            title,
            change_type,
            fingerprint: None,
        }
    }

    /// Add fingerprint information.
    pub fn with_fingerprint(mut self, fp: NodeFingerprint) -> Self {
        self.fingerprint = Some(fp);
        self
    }
}

/// Set of changes between two document versions.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ChangeSet {
    /// Added nodes.
    pub added: Vec<NodeChange>,
    /// Removed nodes.
    pub removed: Vec<NodeChange>,
    /// Modified nodes (content changed).
    pub modified: Vec<NodeChange>,
    /// Restructured nodes (children changed).
    pub restructured: Vec<NodeChange>,
}

impl ChangeSet {
    /// Create an empty change set.
    pub fn new() -> Self {
        Self::default()
    }

    /// Check if there are any changes.
    pub fn is_empty(&self) -> bool {
        self.added.is_empty()
            && self.removed.is_empty()
            && self.modified.is_empty()
            && self.restructured.is_empty()
    }

    /// Get total number of changes.
    pub fn total_changes(&self) -> usize {
        self.added.len() + self.removed.len() + self.modified.len() + self.restructured.len()
    }

    /// Merge another change set into this one.
    pub fn merge(&mut self, other: ChangeSet) {
        self.added.extend(other.added);
        self.removed.extend(other.removed);
        self.modified.extend(other.modified);
        self.restructured.extend(other.restructured);
    }

    /// Get all changed node IDs.
    pub fn changed_node_ids(&self) -> Vec<&str> {
        let mut ids: Vec<&str> = Vec::new();
        for change in &self.added {
            if let Some(ref id) = change.node_id {
                ids.push(id.as_str());
            }
        }
        for change in &self.modified {
            if let Some(ref id) = change.node_id {
                ids.push(id.as_str());
            }
        }
        for change in &self.restructured {
            if let Some(ref id) = change.node_id {
                ids.push(id.as_str());
            }
        }
        ids
    }
}

/// Document-level change detection result.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DocumentChangeInfo {
    /// Document ID.
    pub doc_id: String,
    /// Overall content fingerprint.
    pub content_fp: Fingerprint,
    /// Node-level fingerprints.
    pub node_fingerprints: HashMap<String, NodeFingerprint>,
    /// Last modification time.
    pub modified_at: chrono::DateTime<chrono::Utc>,
    /// Processing version (incremented when processing algorithm changes).
    pub processing_version: u32,
}

impl DocumentChangeInfo {
    /// Create a new document change info.
    pub fn new(doc_id: &str) -> Self {
        Self {
            doc_id: doc_id.to_string(),
            content_fp: Fingerprint::zero(),
            node_fingerprints: HashMap::new(),
            modified_at: chrono::Utc::now(),
            processing_version: 1,
        }
    }

    /// Update from a tree.
    pub fn update_from_tree(&mut self, tree: &DocumentTree) {
        self.content_fp = compute_tree_fingerprint(tree);
        self.node_fingerprints = compute_all_node_fingerprints(tree);
        self.modified_at = chrono::Utc::now();
    }
}

/// Change detector for incremental updates.
///
/// Supports both simple hash-based detection and fine-grained
/// subtree fingerprint-based detection.
pub struct ChangeDetector {
    /// Content fingerprints by document ID.
    content_fps: HashMap<String, Fingerprint>,

    /// Node-level fingerprints by document ID.
    node_fps: HashMap<String, HashMap<String, NodeFingerprint>>,

    /// File modification times by document ID.
    mtimes: HashMap<String, SystemTime>,

    /// Processing versions by document ID.
    processing_versions: HashMap<String, u32>,

    /// Current processing version (for algorithm upgrades).
    current_processing_version: u32,
}

impl ChangeDetector {
    /// Create a new change detector.
    pub fn new() -> Self {
        Self {
            content_fps: HashMap::new(),
            node_fps: HashMap::new(),
            mtimes: HashMap::new(),
            processing_versions: HashMap::new(),
            current_processing_version: 1,
        }
    }

    /// Set the current processing version.
    pub fn with_processing_version(mut self, version: u32) -> Self {
        self.current_processing_version = version;
        self
    }

    /// Compute hash of content (simple u64 hash).
    fn hash_content(content: &str) -> u64 {
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        content.hash(&mut hasher);
        hasher.finish()
    }

    /// Check if a file needs reindexing based on mtime.
    pub fn needs_reindex_by_mtime(&self, doc_id: &str, path: &Path) -> bool {
        let Some(recorded_mtime) = self.mtimes.get(doc_id) else {
            return true; // Never indexed
        };

        let Ok(metadata) = std::fs::metadata(path) else {
            return true; // Can't read file
        };

        let Ok(current_mtime) = metadata.modified() else {
            return true;
        };

        current_mtime > *recorded_mtime
    }

    /// Check if content needs reindexing based on fingerprint.
    pub fn needs_reindex_by_hash(&self, doc_id: &str, content: &str) -> bool {
        let current_fp = Fingerprint::from_str(content);

        match self.content_fps.get(doc_id) {
            Some(recorded_fp) => recorded_fp != &current_fp,
            None => true,
        }
    }

    /// Check if document needs reindexing based on fingerprint.
    pub fn needs_reindex_by_fingerprint(&self, doc_id: &str, new_fp: &Fingerprint) -> bool {
        match self.content_fps.get(doc_id) {
            Some(recorded_fp) => recorded_fp != new_fp,
            None => true,
        }
    }

    /// Check if processing version has changed.
    pub fn needs_reindex_by_version(&self, doc_id: &str) -> bool {
        match self.processing_versions.get(doc_id) {
            Some(recorded_version) => *recorded_version < self.current_processing_version,
            None => true,
        }
    }

    /// Record document state after indexing.
    pub fn record(&mut self, doc_id: &str, content: &str, path: Option<&Path>) {
        self.record_with_tree(doc_id, content, None, path);
    }

    /// Record document state with tree (for fine-grained detection).
    pub fn record_with_tree(
        &mut self,
        doc_id: &str,
        content: &str,
        tree: Option<&DocumentTree>,
        path: Option<&Path>,
    ) {
        // Record content fingerprint
        let content_fp = Fingerprint::from_str(content);
        self.content_fps.insert(doc_id.to_string(), content_fp);

        // Record node fingerprints if tree provided
        if let Some(tree) = tree {
            let node_fps = compute_all_node_fingerprints(tree);
            self.node_fps.insert(doc_id.to_string(), node_fps);
        }

        // Record mtime if path provided
        if let Some(path) = path {
            if let Ok(metadata) = std::fs::metadata(path) {
                if let Ok(mtime) = metadata.modified() {
                    self.mtimes.insert(doc_id.to_string(), mtime);
                }
            }
        }

        // Record processing version
        self.processing_versions
            .insert(doc_id.to_string(), self.current_processing_version);
    }

    /// Record document from ChangeInfo.
    pub fn record_change_info(&mut self, info: &DocumentChangeInfo, path: Option<&Path>) {
        self.content_fps
            .insert(info.doc_id.clone(), info.content_fp);
        self.node_fps
            .insert(info.doc_id.clone(), info.node_fingerprints.clone());
        self.processing_versions
            .insert(info.doc_id.clone(), info.processing_version);

        if let Some(path) = path {
            if let Ok(metadata) = std::fs::metadata(path) {
                if let Ok(mtime) = metadata.modified() {
                    self.mtimes.insert(info.doc_id.clone(), mtime);
                }
            }
        }
    }

    /// Detect changes between two trees using fingerprints.
    pub fn detect_changes(&self, old_tree: &DocumentTree, new_tree: &DocumentTree) -> ChangeSet {
        let mut changes = ChangeSet::new();

        // Collect fingerprints from both trees
        let old_fps = compute_all_node_fingerprints(old_tree);
        let new_fps = compute_all_node_fingerprints(new_tree);

        // Build title -> (string_key, Fingerprint) maps by traversing trees
        // We store owned Strings to avoid lifetime issues
        let old_by_title: HashMap<String, (String, NodeFingerprint)> = {
            let mut map = HashMap::new();
            for node_id in old_tree.traverse() {
                if let Some(node) = old_tree.get(node_id) {
                    let key = node
                        .node_id
                        .clone()
                        .unwrap_or_else(|| format!("node_{:?}", node_id.0));
                    if let Some(fp) = old_fps.get(&key) {
                        map.insert(node.title.clone(), (key, fp.clone()));
                    }
                }
            }
            map
        };

        let new_by_title: HashMap<String, (String, NodeFingerprint)> = {
            let mut map = HashMap::new();
            for node_id in new_tree.traverse() {
                if let Some(node) = new_tree.get(node_id) {
                    let key = node
                        .node_id
                        .clone()
                        .unwrap_or_else(|| format!("node_{:?}", node_id.0));
                    if let Some(fp) = new_fps.get(&key) {
                        map.insert(node.title.clone(), (key, fp.clone()));
                    }
                }
            }
            map
        };

        // Find added nodes
        for (title, (node_key, fp)) in &new_by_title {
            if !old_by_title.contains_key(title) {
                changes.added.push(
                    NodeChange::new(Some(node_key.clone()), title.clone(), ChangeType::Added)
                        .with_fingerprint(fp.clone()),
                );
            }
        }

        // Find removed nodes
        for (title, (node_key, fp)) in &old_by_title {
            if !new_by_title.contains_key(title) {
                changes.removed.push(
                    NodeChange::new(Some(node_key.clone()), title.clone(), ChangeType::Removed)
                        .with_fingerprint(fp.clone()),
                );
            }
        }

        // Find modified nodes
        for (title, (new_key, new_fp)) in &new_by_title {
            if let Some((_old_key, old_fp)) = old_by_title.get(title) {
                if new_fp.content_changed(old_fp) {
                    changes.modified.push(
                        NodeChange::new(Some(new_key.clone()), title.clone(), ChangeType::Modified)
                            .with_fingerprint(new_fp.clone()),
                    );
                } else if new_fp.subtree_changed(old_fp) {
                    changes.restructured.push(
                        NodeChange::new(
                            Some(new_key.clone()),
                            title.clone(),
                            ChangeType::Restructured,
                        )
                        .with_fingerprint(new_fp.clone()),
                    );
                }
            }
        }

        changes
    }

    /// Get nodes that need reprocessing (summary regeneration).
    ///
    /// This returns nodes where either:
    /// - Content changed (summary may need update)
    /// - Processing version changed (all summaries need update)
    pub fn get_nodes_needing_reprocess(
        &self,
        doc_id: &str,
        new_tree: &DocumentTree,
    ) -> Option<Vec<String>> {
        let old_fps = self.node_fps.get(doc_id)?;
        let new_fps = compute_all_node_fingerprints(new_tree);

        let mut needs_reprocess = Vec::new();

        // If processing version changed, all nodes need reprocessing
        if self.needs_reindex_by_version(doc_id) {
            return Some(new_fps.keys().cloned().collect());
        }

        // Otherwise, only changed nodes need reprocessing
        for (node_key, new_fp) in &new_fps {
            if let Some(old_fp) = old_fps.get(node_key) {
                // Content changed or subtree structure changed
                if new_fp.content_changed(old_fp) || new_fp.subtree_changed(old_fp) {
                    needs_reprocess.push(node_key.clone());
                }
            } else {
                // New node
                needs_reprocess.push(node_key.clone());
            }
        }

        Some(needs_reprocess)
    }

    /// Clear stored data for a document.
    pub fn clear(&mut self, doc_id: &str) {
        self.content_fps.remove(doc_id);
        self.node_fps.remove(doc_id);
        self.mtimes.remove(doc_id);
        self.processing_versions.remove(doc_id);
    }

    /// Get the current content fingerprint for a document.
    pub fn get_content_fingerprint(&self, doc_id: &str) -> Option<&Fingerprint> {
        self.content_fps.get(doc_id)
    }

    /// Get all node fingerprints for a document.
    pub fn get_node_fingerprints(&self, doc_id: &str) -> Option<&HashMap<String, NodeFingerprint>> {
        self.node_fps.get(doc_id)
    }

    /// Serialize state for persistence.
    pub fn to_state(&self) -> ChangeDetectorState {
        ChangeDetectorState {
            content_fps: self.content_fps.clone(),
            node_fps: self.node_fps.clone(),
            processing_versions: self.processing_versions.clone(),
        }
    }

    /// Restore state from persistence.
    pub fn from_state(state: ChangeDetectorState) -> Self {
        Self {
            content_fps: state.content_fps,
            node_fps: state.node_fps,
            mtimes: HashMap::new(),
            processing_versions: state.processing_versions,
            current_processing_version: 1,
        }
    }
}

impl Default for ChangeDetector {
    fn default() -> Self {
        Self::new()
    }
}

/// Serializable state for change detector.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChangeDetectorState {
    /// Content fingerprints by document ID.
    pub content_fps: HashMap<String, Fingerprint>,
    /// Node fingerprints by document ID.
    pub node_fps: HashMap<String, HashMap<String, NodeFingerprint>>,
    /// Processing versions by document ID.
    pub processing_versions: HashMap<String, u32>,
}

// =============================================================================
// Helper Functions
// =============================================================================

/// Compute the overall fingerprint for a tree.
pub fn compute_tree_fingerprint(tree: &DocumentTree) -> Fingerprint {
    let root_fp = compute_node_fingerprint(tree, tree.root());
    root_fp.subtree
}

/// Compute content fingerprint for a single node.
fn compute_node_content_fp(tree: &DocumentTree, node_id: NodeId) -> Fingerprint {
    let node = match tree.get(node_id) {
        Some(n) => n,
        None => return Fingerprint::zero(),
    };

    Fingerprinter::new()
        .with_str(&node.title)
        .with_str(&node.content)
        .with_option_str(node.node_id.as_deref())
        .into_fingerprint()
}

/// Compute fingerprint for a node and its subtree.
fn compute_node_fingerprint(tree: &DocumentTree, node_id: NodeId) -> NodeFingerprint {
    let node = match tree.get(node_id) {
        Some(n) => n,
        None => return NodeFingerprint::zero(),
    };

    // Content fingerprint
    let content_fp = Fingerprinter::new()
        .with_str(&node.title)
        .with_str(&node.content)
        .with_option_str(node.node_id.as_deref())
        .into_fingerprint();

    // Check if leaf node
    let children = tree.children(node_id);
    if children.is_empty() {
        return NodeFingerprint::leaf(content_fp);
    }

    // Compute subtree fingerprint from children
    let mut subtree_fp = Fingerprinter::new();
    subtree_fp.write_fingerprint(&content_fp);

    for child_id in children {
        let child_fp = compute_node_fingerprint(tree, child_id);
        subtree_fp.write_fingerprint(&child_fp.subtree);
    }

    NodeFingerprint::new(content_fp, subtree_fp.into_fingerprint())
}

/// Compute fingerprints for all nodes in a tree.
/// Returns a map from string key (for persistence) to NodeFingerprint.
pub fn compute_all_node_fingerprints(tree: &DocumentTree) -> HashMap<String, NodeFingerprint> {
    let mut fingerprints = HashMap::new();

    for node_id in tree.traverse() {
        if let Some(node) = tree.get(node_id) {
            let key = node
                .node_id
                .clone()
                .unwrap_or_else(|| format!("node_{:?}", node_id.0));
            let fp = compute_node_fingerprint(tree, node_id);
            fingerprints.insert(key, fp);
        }
    }

    fingerprints
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::document::DocumentTree;

    #[test]
    fn test_change_detector_new() {
        let detector = ChangeDetector::new();
        assert!(detector.content_fps.is_empty());
    }

    #[test]
    fn test_needs_reindex_by_hash() {
        let mut detector = ChangeDetector::new();

        // First time: always needs reindex
        assert!(detector.needs_reindex_by_hash("doc1", "content"));

        // Record the content
        detector.record("doc1", "content", None);

        // Same content: no reindex needed
        assert!(!detector.needs_reindex_by_hash("doc1", "content"));

        // Different content: needs reindex
        assert!(detector.needs_reindex_by_hash("doc1", "new content"));
    }

    #[test]
    fn test_change_set() {
        let mut changes = ChangeSet::new();
        assert!(changes.is_empty());

        changes.added.push(NodeChange::new(
            Some("node1".to_string()),
            "Title".to_string(),
            ChangeType::Added,
        ));

        assert!(!changes.is_empty());
        assert_eq!(changes.total_changes(), 1);
    }

    #[test]
    fn test_processing_version() {
        let mut detector = ChangeDetector::new().with_processing_version(2);
        detector.record("doc1", "content", None);

        // Version matches, no reindex needed
        assert!(!detector.needs_reindex_by_version("doc1"));

        // Create new detector with higher version
        let detector2 = ChangeDetector::new().with_processing_version(3);
        assert!(detector2.needs_reindex_by_version("doc1"));
    }

    #[test]
    fn test_node_fingerprint() {
        let mut tree = DocumentTree::new("Root", "root content");
        let child = tree.add_child(tree.root(), "Child", "child content");

        let root_fp = compute_node_fingerprint(&tree, tree.root());
        let child_fp = compute_node_fingerprint(&tree, child);

        // Child is a leaf, content == subtree
        assert_eq!(child_fp.content, child_fp.subtree);

        // Root is not a leaf
        assert_ne!(root_fp.content, root_fp.subtree);
    }

    #[test]
    fn test_fingerprint_serialization() {
        let mut detector = ChangeDetector::new();
        let mut tree = DocumentTree::new("Root", "content");
        tree.add_child(tree.root(), "Section", "section content");

        detector.record_with_tree("doc1", "content", Some(&tree), None);

        let state = detector.to_state();
        let json = serde_json::to_string(&state).unwrap();
        let restored: ChangeDetectorState = serde_json::from_str(&json).unwrap();

        assert_eq!(state.content_fps, restored.content_fps);
    }
}