vectorless 0.1.29

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
// Copyright (c) 2026 vectorless developers
// SPDX-License-Identifier: Apache-2.0

//! Cross-document retrieval strategy.
//!
//! Retrieves relevant content from multiple documents, aggregating
//! results into a unified response.

use async_trait::async_trait;
use std::sync::Arc;

use super::r#trait::{NodeEvaluation, RetrievalStrategy, StrategyCapabilities};
use crate::document::{DocumentTree, NodeId};
use crate::graph::DocumentGraph;
use crate::retrieval::RetrievalContext;
use crate::retrieval::types::QueryComplexity;

/// Document identifier for cross-document retrieval.
pub type DocumentId = String;

/// A document with its tree structure for cross-document retrieval.
pub struct DocumentEntry {
    /// Unique document identifier.
    pub id: DocumentId,
    /// Document title or name.
    pub title: String,
    /// The document tree.
    pub tree: DocumentTree,
}

impl DocumentEntry {
    /// Create a new document entry.
    pub fn new(id: impl Into<String>, title: impl Into<String>, tree: DocumentTree) -> Self {
        Self {
            id: id.into(),
            title: title.into(),
            tree,
        }
    }
}

/// Result from a single document in cross-document retrieval.
#[derive(Debug, Clone)]
pub struct DocumentResult {
    /// Document ID.
    pub doc_id: DocumentId,
    /// Document title.
    pub doc_title: String,
    /// Node evaluation results from this document.
    pub evaluations: Vec<(NodeId, NodeEvaluation)>,
    /// Best score from this document.
    pub best_score: f32,
}

/// Strategy for merging results from multiple documents.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum MergeStrategy {
    /// Take top-k results across all documents (default).
    #[default]
    TopK,
    /// Take best result from each document.
    BestPerDocument,
    /// Weight results by document relevance score.
    WeightedByRelevance,
    /// Use graph connectivity to boost connected documents.
    GraphBoosted,
}

/// Configuration for cross-document retrieval.
#[derive(Debug, Clone)]
pub struct CrossDocumentConfig {
    /// Maximum number of documents to search.
    pub max_documents: usize,
    /// Maximum results per document.
    pub max_results_per_doc: usize,
    /// Maximum total results.
    pub max_total_results: usize,
    /// Minimum score threshold for including results.
    pub min_score: f32,
    /// How to merge results from multiple documents.
    pub merge_strategy: MergeStrategy,
    /// Whether to search documents in parallel.
    pub parallel_search: bool,
}

impl Default for CrossDocumentConfig {
    fn default() -> Self {
        Self {
            max_documents: 10,
            max_results_per_doc: 3,
            max_total_results: 10,
            min_score: 0.3,
            merge_strategy: MergeStrategy::TopK,
            parallel_search: true,
        }
    }
}

/// Cross-document retrieval strategy.
///
/// Searches multiple documents and aggregates results based on
/// the configured merge strategy.
///
/// # Example
///
/// ```rust,ignore
/// use vectorless::retrieval::strategy::{CrossDocumentStrategy, DocumentEntry};
///
/// let docs = vec![
///     DocumentEntry::new("doc1", "Manual A", tree1),
///     DocumentEntry::new("doc2", "Manual B", tree2),
/// ];
///
/// let strategy = CrossDocumentStrategy::new(inner_strategy)
///     .with_config(CrossDocumentConfig {
///         max_documents: 5,
///         max_results_per_doc: 2,
///         ..Default::default()
///     });
/// ```
pub struct CrossDocumentStrategy {
    /// Inner strategy for searching individual documents.
    inner: Box<dyn RetrievalStrategy>,
    /// Configuration.
    config: CrossDocumentConfig,
    /// Documents to search.
    documents: Vec<DocumentEntry>,
    /// Optional document graph for graph-aware ranking.
    graph: Option<Arc<DocumentGraph>>,
}

impl CrossDocumentStrategy {
    /// Create a new cross-document strategy.
    pub fn new(inner: Box<dyn RetrievalStrategy>) -> Self {
        Self {
            inner,
            config: CrossDocumentConfig::default(),
            documents: Vec::new(),
            graph: None,
        }
    }

    /// Create with configuration.
    pub fn with_config(mut self, config: CrossDocumentConfig) -> Self {
        self.config = config;
        self
    }

    /// Add a document to search.
    pub fn add_document(&mut self, doc: DocumentEntry) {
        if self.documents.len() < self.config.max_documents {
            self.documents.push(doc);
        }
    }

    /// Set documents to search.
    pub fn with_documents(mut self, documents: Vec<DocumentEntry>) -> Self {
        self.documents = documents
            .into_iter()
            .take(self.config.max_documents)
            .collect();
        self
    }

    /// Get the number of documents.
    pub fn document_count(&self) -> usize {
        self.documents.len()
    }

    /// Set the document graph for graph-aware ranking.
    pub fn with_graph(mut self, graph: Arc<DocumentGraph>) -> Self {
        self.graph = Some(graph);
        self
    }

    /// Apply graph-based score boosting to merged results.
    ///
    /// For each high-confidence result (score > 0.5), find its graph neighbors
    /// and boost their scores by `boost_factor * edge_weight`.
    fn apply_graph_boost(
        &self,
        results: &mut Vec<(DocumentId, NodeId, NodeEvaluation)>,
        boost_factor: f32,
    ) {
        let graph = match self.graph {
            Some(ref g) => g,
            None => return,
        };

        // Collect doc_ids with high scores
        let high_score_docs: Vec<(String, f32)> = results
            .iter()
            .filter(|(_, _, eval)| eval.score > 0.5)
            .map(|(doc_id, _, eval)| (doc_id.clone(), eval.score))
            .collect();

        if high_score_docs.is_empty() {
            return;
        }

        // For each high-score doc, boost its graph neighbors
        for (doc_id, base_score) in &high_score_docs {
            let neighbors = graph.get_neighbors(doc_id);
            for edge in neighbors {
                // Find results from the neighbor doc and boost them
                for result in results.iter_mut() {
                    if result.0 == edge.target_doc_id {
                        let boost = boost_factor * edge.weight * base_score;
                        result.2.score += boost;
                    }
                }
            }
        }

        // Re-sort by score after boosting
        results.sort_by(|a, b| {
            b.2.score
                .partial_cmp(&a.2.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
    }

    /// Search a single document and return results.
    ///
    /// Performs depth-first traversal: evaluates top-level nodes first,
    /// then recursively explores children of high-scoring nodes.
    async fn search_document(
        &self,
        doc: &DocumentEntry,
        context: &RetrievalContext,
    ) -> DocumentResult {
        let root_id = doc.tree.root();
        let children = doc.tree.children(root_id);

        // Phase 1: Evaluate top-level nodes
        let top_evaluations = self
            .inner
            .evaluate_nodes(&doc.tree, &children, context)
            .await;

        let mut scored_nodes: Vec<(NodeId, NodeEvaluation)> = children
            .into_iter()
            .zip(top_evaluations.into_iter())
            .filter(|(_, eval)| eval.score >= self.config.min_score)
            .collect();

        // Phase 2: Depth traversal — explore children of high-scoring nodes
        let high_score_nodes: Vec<NodeId> = scored_nodes
            .iter()
            .filter(|(_, eval)| eval.score >= self.config.min_score * 1.5)
            .map(|(id, _)| *id)
            .collect();

        for node_id in high_score_nodes {
            let depth_results = self.search_subtree(&doc.tree, node_id, context, 0, 2).await;
            scored_nodes.extend(depth_results);
        }

        // Sort by score descending
        scored_nodes.sort_by(|a, b| {
            b.1.score
                .partial_cmp(&a.1.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Deduplicate by node_id
        scored_nodes.dedup_by(|a, b| a.0 == b.0);

        // Limit results per document
        scored_nodes.truncate(self.config.max_results_per_doc);

        let best_score = scored_nodes.first().map(|(_, e)| e.score).unwrap_or(0.0);

        DocumentResult {
            doc_id: doc.id.clone(),
            doc_title: doc.title.clone(),
            evaluations: scored_nodes,
            best_score,
        }
    }

    /// Recursively search a subtree, evaluating children of high-scoring nodes.
    fn search_subtree<'a>(
        &'a self,
        tree: &'a DocumentTree,
        parent_id: NodeId,
        context: &'a RetrievalContext,
        current_depth: usize,
        max_depth: usize,
    ) -> std::pin::Pin<
        Box<dyn std::future::Future<Output = Vec<(NodeId, NodeEvaluation)>> + Send + 'a>,
    > {
        Box::pin(async move {
            if current_depth >= max_depth {
                return Vec::new();
            }

            let children = tree.children(parent_id);
            if children.is_empty() {
                return Vec::new();
            }

            let evaluations = self.inner.evaluate_nodes(tree, &children, context).await;

            let mut results = Vec::new();
            let mut explore_further = Vec::new();

            for (node_id, eval) in children.into_iter().zip(evaluations.into_iter()) {
                if eval.score >= self.config.min_score {
                    results.push((node_id, eval.clone()));
                }
                // Only explore deeper if score is promising
                if eval.score >= self.config.min_score * 1.5 {
                    explore_further.push(node_id);
                }
            }

            // Recurse into promising children
            for child_id in explore_further {
                let deeper = self
                    .search_subtree(tree, child_id, context, current_depth + 1, max_depth)
                    .await;
                results.extend(deeper);
            }

            results
        })
    }

    /// Merge results from all documents.
    fn merge_results(
        &self,
        doc_results: Vec<DocumentResult>,
    ) -> Vec<(DocumentId, NodeId, NodeEvaluation)> {
        match self.config.merge_strategy {
            MergeStrategy::TopK => {
                // Collect all results and sort by score
                let mut all_results: Vec<_> = doc_results
                    .into_iter()
                    .flat_map(|doc| {
                        doc.evaluations
                            .into_iter()
                            .map(move |(node_id, eval)| (doc.doc_id.clone(), node_id, eval))
                    })
                    .collect();

                all_results.sort_by(|a, b| {
                    b.2.score
                        .partial_cmp(&a.2.score)
                        .unwrap_or(std::cmp::Ordering::Equal)
                });
                all_results.truncate(self.config.max_total_results);
                all_results
            }

            MergeStrategy::BestPerDocument => {
                // Take the best result from each document
                doc_results
                    .into_iter()
                    .filter_map(|doc| {
                        doc.evaluations
                            .into_iter()
                            .next()
                            .map(|(node_id, eval)| (doc.doc_id, node_id, eval))
                    })
                    .take(self.config.max_total_results)
                    .collect()
            }

            MergeStrategy::WeightedByRelevance => {
                // Weight by document's best score
                let max_doc_score = doc_results
                    .iter()
                    .map(|d| d.best_score)
                    .fold(0.0_f32, f32::max);

                let mut all_results: Vec<_> = doc_results
                    .into_iter()
                    .flat_map(|doc| {
                        let weight = if max_doc_score > 0.0 {
                            doc.best_score / max_doc_score
                        } else {
                            1.0
                        };
                        doc.evaluations.into_iter().map(move |(node_id, mut eval)| {
                            eval.score *= weight;
                            (doc.doc_id.clone(), node_id, eval)
                        })
                    })
                    .collect();

                all_results.sort_by(|a, b| {
                    b.2.score
                        .partial_cmp(&a.2.score)
                        .unwrap_or(std::cmp::Ordering::Equal)
                });
                all_results.truncate(self.config.max_total_results);
                all_results
            }

            MergeStrategy::GraphBoosted => {
                // First do TopK merge
                let mut all_results: Vec<_> = doc_results
                    .into_iter()
                    .flat_map(|doc| {
                        doc.evaluations
                            .into_iter()
                            .map(move |(node_id, eval)| (doc.doc_id.clone(), node_id, eval))
                    })
                    .collect();

                all_results.sort_by(|a, b| {
                    b.2.score
                        .partial_cmp(&a.2.score)
                        .unwrap_or(std::cmp::Ordering::Equal)
                });

                // Apply graph-based boosting
                self.apply_graph_boost(&mut all_results, 0.15);

                all_results.truncate(self.config.max_total_results);
                all_results
            }
        }
    }
}

#[async_trait]
impl RetrievalStrategy for CrossDocumentStrategy {
    async fn evaluate_node(
        &self,
        tree: &DocumentTree,
        node_id: NodeId,
        context: &RetrievalContext,
    ) -> NodeEvaluation {
        // Delegate to inner strategy
        self.inner.evaluate_node(tree, node_id, context).await
    }

    async fn evaluate_nodes(
        &self,
        tree: &DocumentTree,
        node_ids: &[NodeId],
        context: &RetrievalContext,
    ) -> Vec<NodeEvaluation> {
        // Delegate to inner strategy
        self.inner.evaluate_nodes(tree, node_ids, context).await
    }

    fn name(&self) -> &'static str {
        "cross_document"
    }

    fn capabilities(&self) -> StrategyCapabilities {
        let inner_caps = self.inner.capabilities();
        StrategyCapabilities {
            uses_llm: inner_caps.uses_llm,
            uses_embeddings: inner_caps.uses_embeddings,
            supports_sufficiency: true,
            typical_latency_ms: inner_caps.typical_latency_ms * self.documents.len().min(5) as u64,
        }
    }

    fn suitable_for_complexity(&self, complexity: QueryComplexity) -> bool {
        // Cross-document is suitable for all complexity levels
        matches!(
            complexity,
            QueryComplexity::Simple | QueryComplexity::Medium | QueryComplexity::Complex
        )
    }

    fn estimate_cost(&self, node_count: usize) -> super::r#trait::StrategyCost {
        let inner_cost = self.inner.estimate_cost(node_count);
        super::r#trait::StrategyCost {
            llm_calls: inner_cost.llm_calls * self.documents.len().min(self.config.max_documents),
            tokens: inner_cost.tokens * self.documents.len().min(self.config.max_documents),
        }
    }
}

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

    #[test]
    fn test_config_default() {
        let config = CrossDocumentConfig::default();
        assert_eq!(config.max_documents, 10);
        assert_eq!(config.max_results_per_doc, 3);
        assert_eq!(config.max_total_results, 10);
        assert_eq!(config.merge_strategy, MergeStrategy::TopK);
    }

    #[test]
    fn test_merge_strategy_default() {
        let strategy = MergeStrategy::default();
        assert!(matches!(strategy, MergeStrategy::TopK));
    }
}