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

//! Core types for the retrieval system.

use serde::{Deserialize, Serialize};

use super::context::{PruningStrategy, TokenEstimation};
use crate::document::NodeId;

/// Query complexity level for adaptive strategy selection.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum QueryComplexity {
    /// Simple queries that can be solved with keyword matching.
    Simple,

    /// Medium complexity queries requiring semantic understanding.
    Medium,

    /// Complex queries requiring deep LLM reasoning.
    Complex,
}

impl Default for QueryComplexity {
    fn default() -> Self {
        Self::Medium
    }
}

/// Strategy preference for retrieval.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StrategyPreference {
    /// Automatically select strategy based on query complexity.
    Auto,

    /// Force keyword-based strategy (fast, no LLM).
    ForceKeyword,

    /// Force LLM strategy (deep reasoning).
    ForceLlm,

    /// Force hybrid strategy (BM25 + LLM refinement).
    ForceHybrid,

    /// Force cross-document strategy (multi-document retrieval).
    ForceCrossDocument,

    /// Force page-range strategy (filter by page range).
    ForcePageRange,
}

impl Default for StrategyPreference {
    fn default() -> Self {
        Self::Auto
    }
}

/// Sufficiency level for incremental retrieval.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SufficiencyLevel {
    /// Information is sufficient, stop retrieving.
    Sufficient,

    /// Partial information, can continue if needed.
    PartialSufficient,

    /// Information is insufficient, continue retrieving.
    Insufficient,
}

impl Default for SufficiencyLevel {
    fn default() -> Self {
        Self::Insufficient
    }
}

/// Options for retrieval operations.
#[derive(Debug, Clone)]
pub struct RetrieveOptions {
    /// Maximum number of results to return.
    pub top_k: usize,

    /// Beam width for multi-path search.
    pub beam_width: usize,

    /// Maximum search iterations.
    pub max_iterations: usize,

    /// Whether to include node content in results.
    pub include_content: bool,

    /// Whether to include node summaries in results.
    pub include_summaries: bool,

    /// Minimum relevance score (0.0 - 1.0).
    pub min_score: f32,

    /// Strategy preference.
    pub strategy: StrategyPreference,

    /// Enable sufficiency checking for incremental retrieval.
    pub sufficiency_check: bool,

    /// Maximum tokens for sufficiency threshold.
    pub max_tokens: usize,

    /// Enable result caching.
    pub enable_cache: bool,

    /// Pruning strategy for context building.
    pub pruning_strategy: super::PruningStrategy,

    /// Token estimation mode.
    pub token_estimation: super::TokenEstimation,

    /// Whether to use async context building for large documents.
    pub use_async_context: bool,

    /// Enable streaming retrieval results.
    ///
    /// When enabled, use `query_stream()` to receive incremental
    /// `RetrieveEvent`s as each pipeline stage completes. When disabled
    /// (default), the standard `query()` returns a single final result.
    pub streaming: bool,

    /// Cross-document graph for graph-aware retrieval boosting.
    pub document_graph: Option<std::sync::Arc<crate::graph::DocumentGraph>>,

    /// Search fallback chain: algorithm names tried in order until min_score is met.
    /// Options: "beam", "mcts", "pure_pilot".
    /// Default: ["beam", "mcts", "pure_pilot"]
    pub fallback_chain: Vec<String>,
}

impl Default for RetrieveOptions {
    fn default() -> Self {
        Self {
            top_k: 5,
            beam_width: 3,
            max_iterations: 10,
            include_content: true,
            include_summaries: true,
            min_score: 0.1,
            strategy: StrategyPreference::Auto,
            sufficiency_check: true,
            max_tokens: 4000,
            enable_cache: true,
            pruning_strategy: super::PruningStrategy::default(),
            token_estimation: super::TokenEstimation::default(),
            use_async_context: false,
            streaming: false,
            document_graph: None,
            fallback_chain: vec!["beam".into(), "mcts".into(), "pure_pilot".into()],
        }
    }
}

impl RetrieveOptions {
    /// Create new retrieve options with defaults.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the maximum number of results to return.
    #[must_use]
    pub fn with_top_k(mut self, top_k: usize) -> Self {
        self.top_k = top_k;
        self
    }

    /// Set the beam width for multi-path search.
    #[must_use]
    pub fn with_beam_width(mut self, beam_width: usize) -> Self {
        self.beam_width = beam_width;
        self
    }

    /// Set the maximum search iterations.
    #[must_use]
    pub fn with_max_iterations(mut self, max_iterations: usize) -> Self {
        self.max_iterations = max_iterations;
        self
    }

    /// Set whether to include node content in results.
    #[must_use]
    pub fn with_include_content(mut self, include: bool) -> Self {
        self.include_content = include;
        self
    }

    /// Set whether to include node summaries in results.
    #[must_use]
    pub fn with_include_summaries(mut self, include: bool) -> Self {
        self.include_summaries = include;
        self
    }

    /// Set the minimum relevance score.
    #[must_use]
    pub fn with_min_score(mut self, min_score: f32) -> Self {
        self.min_score = min_score;
        self
    }

    /// Set the strategy preference.
    #[must_use]
    pub fn with_strategy(mut self, strategy: StrategyPreference) -> Self {
        self.strategy = strategy;
        self
    }

    /// Set whether to enable sufficiency checking.
    #[must_use]
    pub fn with_sufficiency_check(mut self, enable: bool) -> Self {
        self.sufficiency_check = enable;
        self
    }

    /// Set the maximum tokens for sufficiency threshold.
    #[must_use]
    pub fn with_max_tokens(mut self, max_tokens: usize) -> Self {
        self.max_tokens = max_tokens;
        self
    }

    /// Set whether to enable result caching.
    #[must_use]
    pub fn with_enable_cache(mut self, enable: bool) -> Self {
        self.enable_cache = enable;
        self
    }

    /// Set pruning strategy for context building.
    #[must_use]
    pub fn with_pruning_strategy(mut self, strategy: PruningStrategy) -> Self {
        self.pruning_strategy = strategy;
        self
    }

    /// Set token estimation mode.
    #[must_use]
    pub fn with_token_estimation(mut self, mode: TokenEstimation) -> Self {
        self.token_estimation = mode;
        self
    }

    /// Enable async context building for large documents.
    #[must_use]
    pub fn with_async_context(mut self, enable: bool) -> Self {
        self.use_async_context = enable;
        self
    }

    /// Enable streaming retrieval results.
    #[must_use]
    pub fn with_streaming(mut self, enable: bool) -> Self {
        self.streaming = enable;
        self
    }

    /// Set the cross-document graph for graph-aware retrieval boosting.
    #[must_use]
    pub fn with_document_graph(
        mut self,
        graph: std::sync::Arc<crate::graph::DocumentGraph>,
    ) -> Self {
        self.document_graph = Some(graph);
        self
    }

    /// Set the search fallback chain.
    ///
    /// Algorithm names: "beam", "mcts", "pure_pilot".
    /// Primary algorithm is prepended automatically by the Plan stage.
    #[must_use]
    pub fn with_fallback_chain(mut self, chain: Vec<String>) -> Self {
        self.fallback_chain = chain;
        self
    }
}

/// A single retrieval result.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RetrievalResult {
    /// Node ID in the tree.
    pub node_id: Option<String>,

    /// Node title.
    pub title: String,

    /// Node content (if included).
    pub content: Option<String>,

    /// Node summary (if included).
    pub summary: Option<String>,

    /// Relevance score (0.0 - 1.0).
    pub score: f32,

    /// Depth in the tree.
    pub depth: usize,

    /// Page range (for PDFs).
    pub page_range: Option<(usize, usize)>,
}

impl RetrievalResult {
    /// Create a new retrieval result.
    #[must_use]
    pub fn new(title: impl Into<String>) -> Self {
        Self {
            node_id: None,
            title: title.into(),
            content: None,
            summary: None,
            score: 1.0,
            depth: 0,
            page_range: None,
        }
    }

    /// Set the node ID.
    #[must_use]
    pub fn with_node_id(mut self, id: impl Into<String>) -> Self {
        self.node_id = Some(id.into());
        self
    }

    /// Set the content.
    #[must_use]
    pub fn with_content(mut self, content: impl Into<String>) -> Self {
        self.content = Some(content.into());
        self
    }

    /// Set the summary.
    #[must_use]
    pub fn with_summary(mut self, summary: impl Into<String>) -> Self {
        self.summary = Some(summary.into());
        self
    }

    /// Set the score.
    #[must_use]
    pub fn with_score(mut self, score: f32) -> Self {
        self.score = score;
        self
    }

    /// Set the depth.
    #[must_use]
    pub fn with_depth(mut self, depth: usize) -> Self {
        self.depth = depth;
        self
    }

    /// Set the page range.
    #[must_use]
    pub fn with_page_range(mut self, start: usize, end: usize) -> Self {
        self.page_range = Some((start, end));
        self
    }
}

/// Complete retrieval response.
#[derive(Debug, Clone)]
pub struct RetrieveResponse {
    /// Retrieved results.
    pub results: Vec<RetrievalResult>,

    /// Aggregated content.
    pub content: String,

    /// Overall confidence score.
    pub confidence: f32,

    /// Whether information is sufficient.
    pub is_sufficient: bool,

    /// Strategy that was used.
    pub strategy_used: String,

    /// Detected query complexity.
    pub complexity: QueryComplexity,

    /// Reasoning chain explaining how results were found.
    pub reasoning_chain: ReasoningChain,

    /// Total tokens used.
    pub tokens_used: usize,
}

impl Default for RetrieveResponse {
    fn default() -> Self {
        Self {
            results: Vec::new(),
            content: String::new(),
            confidence: 0.0,
            is_sufficient: false,
            strategy_used: String::new(),
            complexity: QueryComplexity::Medium,
            reasoning_chain: ReasoningChain::default(),
            tokens_used: 0,
        }
    }
}

impl RetrieveResponse {
    /// Create a new empty response.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Check if there are any results.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.results.is_empty()
    }

    /// Get the number of results.
    #[must_use]
    pub fn len(&self) -> usize {
        self.results.len()
    }
}

/// A single navigation step in the search trace.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NavigationStep {
    /// Node ID visited.
    pub node_id: String,

    /// Node title.
    pub title: String,

    /// Relevance score at this step.
    pub score: f32,

    /// Decision made at this step.
    pub decision: NavigationDecision,

    /// Depth in tree.
    pub depth: usize,
}

/// Navigation decision at each step.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum NavigationDecision {
    /// Go to the specified child.
    GoToChild(usize),

    /// This node contains the answer.
    ThisIsTheAnswer,

    /// Explore multiple children.
    ExploreMore,

    /// Skip this branch.
    Skip,

    /// Backtrack from a dead-end node to a previously shelved alternative.
    /// Contains the title of the dead-end node being abandoned.
    BacktrackFrom(String),
}

/// Pipeline stage name for reasoning chain provenance.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum StageName {
    /// Query analysis stage.
    Analyze,
    /// Strategy planning stage.
    Plan,
    /// Tree search stage.
    Search,
    /// Sufficiency evaluation stage.
    Evaluate,
}

impl std::fmt::Display for StageName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Analyze => write!(f, "analyze"),
            Self::Plan => write!(f, "plan"),
            Self::Search => write!(f, "search"),
            Self::Evaluate => write!(f, "evaluate"),
        }
    }
}

/// Summary of an LLM call made during a reasoning step.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LlmCallSummary {
    /// Truncated prompt summary for display.
    pub prompt_summary: String,
    /// Tokens consumed by this call.
    pub tokens_used: usize,
    /// Model identifier.
    pub model: String,
}

/// A candidate node considered but not selected during reasoning.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReasoningCandidate {
    /// Node ID.
    pub node_id: String,
    /// Node title.
    pub title: String,
    /// Relevance score of this candidate.
    pub score: f32,
}

/// A single step in the reasoning chain.
///
/// Unlike `NavigationStep` which only records "where" the search went,
/// `ReasoningStep` also records "why" — the decision rationale,
/// candidates considered, strategy used, and any LLM calls made.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReasoningStep {
    /// Which pipeline stage produced this step.
    pub stage: StageName,
    /// Node ID visited (if applicable).
    pub node_id: Option<String>,
    /// Node title (if applicable).
    pub title: Option<String>,
    /// Relevance score at this step.
    pub score: f32,
    /// Decision made at this step.
    pub decision: NavigationDecision,
    /// Depth in tree.
    pub depth: usize,
    /// Human-readable explanation of why this decision was made.
    pub reasoning: String,
    /// Candidates considered but not selected at this step.
    pub candidates: Vec<ReasoningCandidate>,
    /// Strategy used at this step (e.g. "keyword", "hybrid").
    pub strategy_used: Option<String>,
    /// LLM call summary, if an LLM was consulted.
    pub llm_call: Option<LlmCallSummary>,
    /// Reference identifiers followed from this step (cross-reference tracking).
    pub references_followed: Vec<String>,
}

/// Complete reasoning chain for a retrieval operation.
///
/// Provides an ordered, auditable trace of every decision the engine made
/// from query analysis through final evaluation. This is the core
/// differentiator — not just results, but *why* these results.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ReasoningChain {
    /// Ordered reasoning steps.
    pub steps: Vec<ReasoningStep>,
}

impl ReasoningChain {
    /// Create an empty reasoning chain.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Append a reasoning step.
    pub fn push(&mut self, step: ReasoningStep) {
        self.steps.push(step);
    }

    /// Number of reasoning steps.
    #[must_use]
    pub fn len(&self) -> usize {
        self.steps.len()
    }

    /// Whether the chain is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.steps.is_empty()
    }

    /// Build a human-readable summary of the full chain.
    #[must_use]
    pub fn summary(&self) -> String {
        self.steps
            .iter()
            .map(|s| {
                let node_info = s.title.as_deref().unwrap_or("(no node)");
                format!(
                    "[{}] {} (score={:.2}): {}",
                    s.stage, node_info, s.score, s.reasoning
                )
            })
            .collect::<Vec<_>>()
            .join("\n")
    }
}

/// Search path for multi-path algorithms.
///
/// Tracks the sequence of nodes visited, along with the reasoning
/// for each navigation step. This reasoning is fed back into the
/// LLM context so the Pilot can understand how it arrived at the
/// current position and avoid repeating mistakes.
#[derive(Debug, Clone)]
pub struct SearchPath {
    /// Nodes in the path.
    pub nodes: Vec<NodeId>,

    /// Cumulative score.
    pub score: f32,

    /// Leaf node (if path ends at leaf).
    pub leaf: Option<NodeId>,

    /// Per-step reasoning for why each node was chosen.
    ///
    /// Same length as `nodes`. Each entry is the reason the
    /// corresponding node was selected. `None` means no reason
    /// was captured (e.g., algorithm-only fallback).
    pub step_reasons: Vec<Option<String>>,
}

impl SearchPath {
    /// Create a new empty path.
    #[must_use]
    pub fn new() -> Self {
        Self {
            nodes: Vec::new(),
            score: 0.0,
            leaf: None,
            step_reasons: Vec::new(),
        }
    }

    /// Create a path from a single node.
    #[must_use]
    pub fn from_node(node_id: NodeId, score: f32) -> Self {
        Self {
            nodes: vec![node_id],
            score,
            leaf: Some(node_id),
            step_reasons: vec![None],
        }
    }

    /// Extend the path with a new node and optional reason.
    #[must_use]
    pub fn extend(&self, node_id: NodeId, score: f32) -> Self {
        let mut nodes = self.nodes.clone();
        let mut step_reasons = self.step_reasons.clone();
        nodes.push(node_id);
        step_reasons.push(None);
        Self {
            nodes,
            score: self.score + score,
            leaf: Some(node_id),
            step_reasons,
        }
    }

    /// Extend the path with a new node and a reason for choosing it.
    #[must_use]
    pub fn extend_with_reason(
        &self,
        node_id: NodeId,
        score: f32,
        reason: impl Into<String>,
    ) -> Self {
        let mut nodes = self.nodes.clone();
        let mut step_reasons = self.step_reasons.clone();
        nodes.push(node_id);
        step_reasons.push(Some(reason.into()));
        Self {
            nodes,
            score: self.score + score,
            leaf: Some(node_id),
            step_reasons,
        }
    }
}

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

/// Statistics for a retrieval operation.
#[derive(Debug, Clone, Default)]
pub struct RetrievalStats {
    /// Number of nodes visited.
    pub nodes_visited: usize,

    /// Number of LLM calls made.
    pub llm_calls: usize,

    /// Time spent in milliseconds.
    pub time_ms: u64,

    /// Tokens consumed.
    pub tokens_used: usize,

    /// Cache hits.
    pub cache_hits: usize,

    /// Cache misses.
    pub cache_misses: usize,
}