infiniloom_engine/index/context/
types.rs

1//! Type definitions for context expansion.
2//!
3//! This module contains all the data structures used by the context expander,
4//! including change classification, context results, and impact summaries.
5
6/// Context expansion depth levels
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
8pub enum ContextDepth {
9    /// L1: Containing functions only
10    L1,
11    /// L2: Direct dependents (default)
12    #[default]
13    L2,
14    /// L3: Transitive dependents
15    L3,
16}
17
18impl PartialOrd for ContextDepth {
19    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
20        Some(self.cmp(other))
21    }
22}
23
24impl Ord for ContextDepth {
25    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
26        let self_num = match self {
27            ContextDepth::L1 => 1,
28            ContextDepth::L2 => 2,
29            ContextDepth::L3 => 3,
30        };
31        let other_num = match other {
32            ContextDepth::L1 => 1,
33            ContextDepth::L2 => 2,
34            ContextDepth::L3 => 3,
35        };
36        self_num.cmp(&other_num)
37    }
38}
39
40/// A change in a diff
41#[derive(Debug, Clone)]
42pub struct DiffChange {
43    /// File path (for renames, this is the NEW path)
44    pub file_path: String,
45    /// Old file path (only set for renames)
46    pub old_path: Option<String>,
47    /// Changed line ranges (start, end)
48    pub line_ranges: Vec<(u32, u32)>,
49    /// Type of change
50    pub change_type: ChangeType,
51    /// Raw diff content (the actual +/- lines), if requested
52    pub diff_content: Option<String>,
53}
54
55/// Type of change
56#[derive(Debug, Clone, Copy, PartialEq, Eq)]
57pub enum ChangeType {
58    Added,
59    Modified,
60    Deleted,
61    Renamed,
62}
63
64/// More detailed change classification for smart expansion
65#[derive(Debug, Clone, Copy, PartialEq, Eq)]
66pub enum ChangeClassification {
67    /// New code added - include dependents at normal priority
68    NewCode,
69    /// Function signature changed - include ALL callers at high priority
70    SignatureChange,
71    /// Type/struct/class definition changed - include all usages
72    TypeDefinitionChange,
73    /// Function body only changed - lower impact, only direct dependents
74    ImplementationChange,
75    /// Code deleted - callers will break, highest priority
76    Deletion,
77    /// File renamed - importers need updating
78    FileRename,
79    /// Import/dependency changed - may affect resolution
80    ImportChange,
81    /// Documentation/comment only - minimal impact
82    DocumentationOnly,
83}
84
85/// Expanded context result
86#[derive(Debug, Clone)]
87pub struct ExpandedContext {
88    /// Changed symbols (directly modified)
89    pub changed_symbols: Vec<ContextSymbol>,
90    /// Changed files
91    pub changed_files: Vec<ContextFile>,
92    /// Dependent symbols (affected by changes)
93    pub dependent_symbols: Vec<ContextSymbol>,
94    /// Dependent files
95    pub dependent_files: Vec<ContextFile>,
96    /// Related tests
97    pub related_tests: Vec<ContextFile>,
98    /// Call chains involving changed symbols
99    pub call_chains: Vec<CallChain>,
100    /// Summary of impact
101    pub impact_summary: ImpactSummary,
102    /// Total estimated tokens
103    pub total_tokens: u32,
104}
105
106/// A symbol in the context
107#[derive(Debug, Clone)]
108pub struct ContextSymbol {
109    /// Symbol ID
110    pub id: u32,
111    /// Symbol name
112    pub name: String,
113    /// Symbol kind (function, class, etc.)
114    pub kind: String,
115    /// File path
116    pub file_path: String,
117    /// Start line
118    pub start_line: u32,
119    /// End line
120    pub end_line: u32,
121    /// Signature
122    pub signature: Option<String>,
123    /// Why this symbol is relevant
124    pub relevance_reason: String,
125    /// Relevance score (0.0 - 1.0)
126    pub relevance_score: f32,
127}
128
129/// A file in the context
130#[derive(Debug, Clone)]
131pub struct ContextFile {
132    /// File ID
133    pub id: u32,
134    /// File path
135    pub path: String,
136    /// Language
137    pub language: String,
138    /// Why this file is relevant
139    pub relevance_reason: String,
140    /// Relevance score (0.0 - 1.0)
141    pub relevance_score: f32,
142    /// Estimated tokens
143    pub tokens: u32,
144    /// Relevant sections (line ranges)
145    pub relevant_sections: Vec<(u32, u32)>,
146    /// Raw diff content (the actual +/- lines), if available
147    pub diff_content: Option<String>,
148    /// Extracted snippets for LLM context
149    pub snippets: Vec<ContextSnippet>,
150}
151
152/// A snippet of source context for a file
153#[derive(Debug, Clone)]
154pub struct ContextSnippet {
155    /// Start line (1-indexed)
156    pub start_line: u32,
157    /// End line (1-indexed)
158    pub end_line: u32,
159    /// Why this snippet was included
160    pub reason: String,
161    /// Snippet content
162    pub content: String,
163}
164
165/// A call chain for understanding impact
166#[derive(Debug, Clone)]
167pub struct CallChain {
168    /// Symbols in the chain (from caller to callee)
169    pub symbols: Vec<String>,
170    /// Files involved
171    pub files: Vec<String>,
172}
173
174/// Summary of the impact
175#[derive(Debug, Clone, Default)]
176pub struct ImpactSummary {
177    /// Impact level (low, medium, high, critical)
178    pub level: ImpactLevel,
179    /// Number of directly affected files
180    pub direct_files: usize,
181    /// Number of transitively affected files
182    pub transitive_files: usize,
183    /// Number of affected symbols
184    pub affected_symbols: usize,
185    /// Number of affected tests
186    pub affected_tests: usize,
187    /// Breaking changes detected
188    pub breaking_changes: Vec<String>,
189    /// Description of the impact
190    pub description: String,
191}
192
193/// Impact severity level
194#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
195pub enum ImpactLevel {
196    #[default]
197    Low,
198    Medium,
199    High,
200    Critical,
201}
202
203impl ImpactLevel {
204    pub fn name(&self) -> &'static str {
205        match self {
206            Self::Low => "low",
207            Self::Medium => "medium",
208            Self::High => "high",
209            Self::Critical => "critical",
210        }
211    }
212}