codanna 0.9.23

Code Intelligence for Large Language Models
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
use crate::relationship::RelationshipMetadata;
use crate::storage::{StorageError, StorageResult};
use crate::vector::{ClusterId, VectorId};
use crate::{FileId, SymbolId};
use serde::{Deserialize, Serialize};
use tantivy::{TantivyDocument as Document, schema::Value};

use super::DocumentIndex;

/// Metadata for tracking vector-related information per document
#[derive(Debug, Clone, PartialEq)]
pub struct VectorMetadata {
    /// The vector ID associated with this document (maps to SymbolId)
    pub vector_id: Option<VectorId>,
    /// The cluster assignment for this vector
    pub cluster_id: Option<ClusterId>,
    /// Version of the embedding model used to generate this vector
    pub embedding_version: u32,
}

/// Internal representation for JSON serialization
#[derive(Serialize, Deserialize)]
struct VectorMetadataJson {
    vector_id: Option<u32>,
    cluster_id: Option<u32>,
    embedding_version: u32,
}

impl VectorMetadata {
    /// Creates a new VectorMetadata with no vector assignment
    pub fn new(embedding_version: u32) -> Self {
        Self {
            vector_id: None,
            cluster_id: None,
            embedding_version,
        }
    }

    /// Creates VectorMetadata with full vector information
    pub fn with_vector(vector_id: VectorId, cluster_id: ClusterId, embedding_version: u32) -> Self {
        Self {
            vector_id: Some(vector_id),
            cluster_id: Some(cluster_id),
            embedding_version,
        }
    }

    /// Checks if this document has an associated vector
    pub fn has_vector(&self) -> bool {
        self.vector_id.is_some()
    }

    /// Serializes the metadata to a JSON string for storage in Tantivy
    pub fn to_json(&self) -> StorageResult<String> {
        let json_repr = VectorMetadataJson {
            vector_id: self.vector_id.map(|id| id.get()),
            cluster_id: self.cluster_id.map(|id| id.get()),
            embedding_version: self.embedding_version,
        };
        serde_json::to_string(&json_repr).map_err(|e| {
            StorageError::Serialization(format!("Failed to serialize VectorMetadata: {e}"))
        })
    }

    /// Deserializes metadata from a JSON string
    pub fn from_json(json: &str) -> StorageResult<Self> {
        let json_repr: VectorMetadataJson = serde_json::from_str(json).map_err(|e| {
            StorageError::Serialization(format!("Failed to deserialize VectorMetadata: {e}"))
        })?;

        Ok(Self {
            vector_id: json_repr.vector_id.and_then(VectorId::new),
            cluster_id: json_repr.cluster_id.and_then(ClusterId::new),
            embedding_version: json_repr.embedding_version,
        })
    }
}

/// Encode the stored scope_context field. Explicit JSON, `null` when absent;
/// exhaustive over variants by serde derivation.
fn encode_scope_context(scope: Option<&crate::ScopeContext>) -> String {
    serde_json::to_string(&scope)
        .expect("ScopeContext is a closed enum of strings and bools; serialization is infallible")
}

/// Decode the stored scope_context field. Undecodable values (including
/// pre-JSON legacy strings) read as no-scope-info: the index is session-scoped
/// and rebuilt, never migrated.
fn decode_scope_context(s: &str) -> Option<crate::ScopeContext> {
    serde_json::from_str(s).ok().flatten()
}

impl DocumentIndex {
    /// Add a symbol document to the index (must call start_batch first)
    pub fn add_document(&self, symbol: &crate::Symbol, file_path: &str) -> StorageResult<()> {
        let writer_lock = self.writer.read().map_err(|_| StorageError::LockPoisoned)?;
        let writer = writer_lock.as_ref().ok_or(StorageError::NoActiveBatch)?;

        let mut doc = Document::new();
        doc.add_text(self.schema.doc_type, "symbol");
        doc.add_u64(self.schema.symbol_id, symbol.id.value() as u64);
        doc.add_u64(self.schema.file_id, symbol.file_id.value() as u64);
        doc.add_text(self.schema.name, &symbol.name);
        doc.add_text(self.schema.name_text, &symbol.name); // Also add to full-text searchable field
        doc.add_text(self.schema.file_path, file_path);
        doc.add_u64(self.schema.line_number, symbol.range.start_line as u64);
        doc.add_u64(self.schema.column, symbol.range.start_column as u64);
        doc.add_u64(self.schema.end_line, symbol.range.end_line as u64);
        doc.add_u64(self.schema.end_column, symbol.range.end_column as u64);

        if let Some(comment) = symbol.doc_comment.as_deref() {
            doc.add_text(self.schema.doc_comment, comment);
        }

        if let Some(sig) = symbol.signature.as_deref() {
            doc.add_text(self.schema.signature, sig);
        }

        // Add string fields for filtering
        doc.add_text(
            self.schema.module_path,
            symbol.module_path.as_deref().unwrap_or(""),
        );
        doc.add_text(self.schema.kind, format!("{:?}", symbol.kind));
        doc.add_u64(self.schema.visibility, symbol.visibility as u64);

        doc.add_text(
            self.schema.scope_context,
            encode_scope_context(symbol.scope_context.as_ref()),
        );

        doc.add_text(
            self.schema.language,
            symbol
                .language_id
                .as_ref()
                .map(|id| id.as_str())
                .unwrap_or(""),
        );

        writer.add_document(doc)?;

        Ok(())
    }

    /// Convert a Tantivy document to a Symbol
    pub(super) fn document_to_symbol(&self, doc: &Document) -> StorageResult<crate::Symbol> {
        use crate::{Range, Symbol, SymbolKind, Visibility};

        let symbol_id = doc
            .get_first(self.schema.symbol_id)
            .and_then(|v| v.as_u64())
            .ok_or(StorageError::InvalidFieldValue {
                field: "symbol_id".to_string(),
                reason: "missing from document".to_string(),
            })?;

        let name = doc
            .get_first(self.schema.name)
            .and_then(|v| v.as_str())
            .ok_or(StorageError::InvalidFieldValue {
                field: "name".to_string(),
                reason: "missing from document".to_string(),
            })?
            .to_string();

        let kind_str = doc
            .get_first(self.schema.kind)
            .and_then(|v| v.as_str())
            .ok_or(StorageError::InvalidFieldValue {
                field: "kind".to_string(),
                reason: "missing from document".to_string(),
            })?;
        let kind = SymbolKind::from_str_with_default(kind_str);

        let file_id = doc
            .get_first(self.schema.file_id)
            .and_then(|v| v.as_u64())
            .ok_or(StorageError::InvalidFieldValue {
                field: "file_id".to_string(),
                reason: "missing from document".to_string(),
            })?;

        let start_line = doc
            .get_first(self.schema.line_number)
            .and_then(|v| v.as_u64())
            .unwrap_or(0) as u32;

        let start_col = doc
            .get_first(self.schema.column)
            .and_then(|v| v.as_u64())
            .unwrap_or(0) as u16;

        let end_line = doc
            .get_first(self.schema.end_line)
            .and_then(|v| v.as_u64())
            .unwrap_or(start_line as u64) as u32;

        let end_col = doc
            .get_first(self.schema.end_column)
            .and_then(|v| v.as_u64())
            .unwrap_or(0) as u16;

        let signature = doc
            .get_first(self.schema.signature)
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());

        let doc_comment = doc
            .get_first(self.schema.doc_comment)
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());

        let module_path = doc
            .get_first(self.schema.module_path)
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());

        // Get visibility from stored field
        let visibility = doc
            .get_first(self.schema.visibility)
            .and_then(|v| v.as_u64())
            .map(|v| match v {
                0 => Visibility::Public,
                1 => Visibility::Crate,
                2 => Visibility::Module,
                3 => Visibility::Private,
                _ => Visibility::Private,
            })
            .unwrap_or(Visibility::Private);

        let scope_context = doc
            .get_first(self.schema.scope_context)
            .and_then(|v| v.as_str())
            .and_then(decode_scope_context);

        Ok(Symbol {
            id: SymbolId(symbol_id as u32),
            name: name.into(),
            kind,
            file_id: FileId(file_id as u32),
            range: Range {
                start_line,
                start_column: start_col,
                end_line,
                end_column: end_col,
            },
            file_path: doc
                .get_first(self.schema.file_path)
                .and_then(|v| v.as_str())
                .unwrap_or("<unknown>")
                .into(),
            signature: signature.map(|s| s.into()),
            doc_comment: doc_comment.map(|s| s.into()),
            module_path: module_path.map(|s| s.into()),
            visibility,
            scope_context,
            language_id: {
                // Read the language field from the document and convert to LanguageId
                // using the language registry (which maintains the static strings)
                doc.get_first(self.schema.language)
                    .and_then(|v| v.as_str())
                    .filter(|s| !s.is_empty())
                    .and_then(|lang_str| {
                        // Use the global registry to convert string to LanguageId
                        // This maintains language-agnostic storage while properly
                        // converting to the type-safe LanguageId at retrieval time
                        crate::parsing::get_registry()
                            .lock()
                            .ok()
                            .and_then(|registry| registry.find_language_id(lang_str))
                    })
            },
        })
    }

    /// Reconstruct `RelationshipMetadata` from a stored relationship document.
    /// Union-gate: returns `Some(metadata)` if ANY of `{line, column, context,
    /// receiver, static_call}` is present; each field independently `Option`-set.
    /// Symmetric with `store_relationship`'s per-field-conditional write.
    pub(super) fn metadata_for_relationship_doc(
        &self,
        doc: &Document,
    ) -> Option<RelationshipMetadata> {
        let line = doc
            .get_first(self.schema.relation_line)
            .and_then(|v| v.as_u64())
            .map(|n| n as u32);
        let column = doc
            .get_first(self.schema.relation_column)
            .and_then(|v| v.as_u64())
            .map(|n| n as u16);
        let context = doc
            .get_first(self.schema.relation_context)
            .and_then(|v| v.as_str());
        let receiver = doc
            .get_first(self.schema.relation_receiver)
            .and_then(|v| v.as_str());
        let static_call = doc
            .get_first(self.schema.relation_static_call)
            .and_then(|v| v.as_u64())
            .is_some_and(|n| n != 0);

        if line.is_none()
            && column.is_none()
            && context.is_none()
            && receiver.is_none()
            && !static_call
        {
            return None;
        }

        let mut metadata = RelationshipMetadata::new();
        metadata.line = line;
        metadata.column = column;
        metadata.context = context.map(Into::into);
        metadata.receiver = receiver.map(Into::into);
        metadata.static_call = static_call;
        Some(metadata)
    }
}

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

    use tempfile::TempDir;

    #[test]
    fn test_three_reconstruction_paths_agree_on_context_only_metadata() {
        // Story 8 slice 4 (backlog B): all three reconstruction sites must
        // return the same metadata for the same on-disk doc. Context-only
        // metadata exercises the gating divergence — write side emits each
        // field independently, so read side must also reconstruct each field
        // independently. Pre-refactor: get_relationships_from / _to use
        // AND-gate (line+column required) → metadata=None. query_relationships
        // uses union-gate → metadata=Some({context}). Post-refactor: all three
        // converge on union-gate via metadata_for_relationship_doc().
        let temp_dir = TempDir::new().unwrap();
        let settings = crate::config::Settings::default();
        let index = DocumentIndex::new(temp_dir.path(), &settings).unwrap();

        index.start_batch().unwrap();

        let from_id = SymbolId::new(1).unwrap();
        let to_id = SymbolId::new(2).unwrap();
        let meta = RelationshipMetadata::new().with_context("inside main function");
        let rel = crate::Relationship::new(crate::RelationKind::Calls).with_metadata(meta);

        index.store_relationship(from_id, to_id, &rel).unwrap();
        index.commit_batch().unwrap();

        let from_rels = index
            .get_relationships_from(from_id, crate::RelationKind::Calls)
            .unwrap();
        let to_rels = index
            .get_relationships_to(to_id, crate::RelationKind::Calls)
            .unwrap();
        let all_rels = index.query_relationships().unwrap();

        let from_meta = from_rels[0].2.metadata.as_ref();
        let to_meta = to_rels[0].2.metadata.as_ref();
        let all_meta = all_rels[0].2.metadata.as_ref();

        assert!(
            from_meta.is_some(),
            "get_relationships_from must reconstruct context-only metadata"
        );
        assert!(
            to_meta.is_some(),
            "get_relationships_to must reconstruct context-only metadata"
        );
        assert!(
            all_meta.is_some(),
            "query_relationships must reconstruct context-only metadata"
        );

        let from_meta = from_meta.unwrap();
        assert_eq!(from_meta.context.as_deref(), Some("inside main function"));
        assert_eq!(from_meta.line, None);
        assert_eq!(from_meta.column, None);
        assert_eq!(from_meta.context, to_meta.unwrap().context);
        assert_eq!(from_meta.context, all_meta.unwrap().context);
    }

    #[test]
    fn test_relation_metadata_legacy_defaults_via_from() {
        let temp_dir = TempDir::new().unwrap();
        let settings = crate::config::Settings::default();
        let index = DocumentIndex::new(temp_dir.path(), &settings).unwrap();

        index.start_batch().unwrap();

        let from_id = SymbolId::new(1).unwrap();
        let to_id = SymbolId::new(2).unwrap();
        let meta = RelationshipMetadata::new().at_position(7, 2);
        let rel = crate::Relationship::new(crate::RelationKind::Calls).with_metadata(meta);

        index.store_relationship(from_id, to_id, &rel).unwrap();
        index.commit_batch().unwrap();

        let rels = index
            .get_relationships_from(from_id, crate::RelationKind::Calls)
            .unwrap();
        assert_eq!(rels.len(), 1);

        let (_, _, r) = &rels[0];
        let m = r.metadata.as_ref().expect("metadata should be present");
        assert_eq!(m.line, Some(7));
        assert_eq!(m.column, Some(2));
        assert_eq!(m.receiver, None);
        assert!(!m.static_call);
    }

    #[test]
    fn test_vector_metadata_creation() {
        // Test creating metadata without vector
        let metadata = VectorMetadata::new(1);
        assert_eq!(metadata.vector_id, None);
        assert_eq!(metadata.cluster_id, None);
        assert_eq!(metadata.embedding_version, 1);
        assert!(!metadata.has_vector());

        // Test creating metadata with vector
        let vector_id = VectorId::new(42).unwrap();
        let cluster_id = ClusterId::new(5).unwrap();
        let metadata_with_vector = VectorMetadata::with_vector(vector_id, cluster_id, 2);
        assert_eq!(metadata_with_vector.vector_id, Some(vector_id));
        assert_eq!(metadata_with_vector.cluster_id, Some(cluster_id));
        assert_eq!(metadata_with_vector.embedding_version, 2);
        assert!(metadata_with_vector.has_vector());
    }

    #[test]
    fn test_vector_metadata_serialization() {
        // Test serialization of metadata without vector
        let metadata = VectorMetadata::new(1);
        let json = metadata.to_json().unwrap();
        let deserialized = VectorMetadata::from_json(&json).unwrap();
        assert_eq!(metadata, deserialized);

        // Test serialization of metadata with vector
        let vector_id = VectorId::new(123).unwrap();
        let cluster_id = ClusterId::new(7).unwrap();
        let metadata_with_vector = VectorMetadata::with_vector(vector_id, cluster_id, 3);
        let json = metadata_with_vector.to_json().unwrap();
        let deserialized = VectorMetadata::from_json(&json).unwrap();
        assert_eq!(metadata_with_vector, deserialized);

        // Verify JSON structure
        assert!(json.contains("\"vector_id\""));
        assert!(json.contains("\"cluster_id\""));
        assert!(json.contains("\"embedding_version\":3"));
    }

    #[test]
    fn test_vector_metadata_deserialization_error() {
        // Test invalid JSON
        let invalid_json = "{ invalid json }";
        let result = VectorMetadata::from_json(invalid_json);
        assert!(result.is_err());
        match result {
            Err(StorageError::Serialization(msg)) => {
                assert!(msg.contains("Failed to deserialize VectorMetadata"));
            }
            _ => panic!("Expected Serialization error"),
        }
    }

    #[test]
    fn test_vector_metadata_tantivy_roundtrip() {
        use tantivy::schema::{STORED, SchemaBuilder, TEXT};
        use tantivy::{Index, TantivyDocument, doc};

        // Create a simple schema with a metadata field
        let mut schema_builder = SchemaBuilder::default();
        let metadata_field = schema_builder.add_text_field("vector_metadata", TEXT | STORED);
        let schema = schema_builder.build();

        // Create an in-memory index
        let index = Index::create_in_ram(schema);
        let mut index_writer = index.writer(50_000_000).unwrap();

        // Create test metadata
        let vector_id = VectorId::new(999).unwrap();
        let cluster_id = ClusterId::new(42).unwrap();
        let metadata = VectorMetadata::with_vector(vector_id, cluster_id, 5);

        // Serialize and store in document
        let json = metadata.to_json().unwrap();
        let doc = doc!(metadata_field => json.clone());
        index_writer.add_document(doc).unwrap();
        index_writer.commit().unwrap();

        // Read back from index
        let reader = index.reader().unwrap();
        let searcher = reader.searcher();
        let doc: TantivyDocument = searcher.doc(tantivy::DocAddress::new(0, 0)).unwrap();

        // Deserialize and verify
        let stored_json = doc
            .get_first(metadata_field)
            .and_then(|v| v.as_str())
            .unwrap();
        let retrieved_metadata = VectorMetadata::from_json(stored_json).unwrap();

        assert_eq!(metadata, retrieved_metadata);
        assert_eq!(json, stored_json);
    }

    #[test]
    fn test_scope_context_codec_round_trips_every_variant() {
        use crate::{ScopeContext, SymbolKind};

        const ALL_KINDS: [SymbolKind; 14] = [
            SymbolKind::Function,
            SymbolKind::Method,
            SymbolKind::Struct,
            SymbolKind::Enum,
            SymbolKind::Trait,
            SymbolKind::Interface,
            SymbolKind::Class,
            SymbolKind::Module,
            SymbolKind::Variable,
            SymbolKind::Constant,
            SymbolKind::Field,
            SymbolKind::Parameter,
            SymbolKind::TypeAlias,
            SymbolKind::Macro,
        ];

        let mut cases: Vec<Option<ScopeContext>> = vec![
            None,
            Some(ScopeContext::Module),
            Some(ScopeContext::Global),
            Some(ScopeContext::Package),
            Some(ScopeContext::Parameter),
            Some(ScopeContext::ClassMember { class_name: None }),
            Some(ScopeContext::ClassMember {
                class_name: Some("com.example.MyClass".into()),
            }),
        ];
        for hoisted in [false, true] {
            for parent_name in [None, Some("outer_fn")] {
                cases.push(Some(ScopeContext::Local {
                    hoisted,
                    parent_name: parent_name.map(Into::into),
                    parent_kind: None,
                }));
                for kind in ALL_KINDS {
                    cases.push(Some(ScopeContext::Local {
                        hoisted,
                        parent_name: parent_name.map(Into::into),
                        parent_kind: Some(kind),
                    }));
                }
            }
        }

        for case in cases {
            let encoded = encode_scope_context(case.as_ref());
            let decoded = decode_scope_context(&encoded);
            assert_eq!(decoded, case, "round-trip failed via {encoded:?}");
        }
    }

    #[test]
    fn test_scope_context_survives_tantivy_roundtrip() {
        let temp_dir = TempDir::new().unwrap();
        let settings = crate::config::Settings::default();
        let index = DocumentIndex::new(temp_dir.path(), &settings).unwrap();

        let scope = crate::ScopeContext::Local {
            hoisted: true,
            parent_name: Some("run_pipeline".into()),
            parent_kind: Some(SymbolKind::Struct),
        };
        let symbol = crate::Symbol::new(
            SymbolId::new(7).unwrap(),
            "worker",
            SymbolKind::Variable,
            FileId::new(1).unwrap(),
            crate::Range::new(4, 8, 4, 14),
        )
        .with_scope(scope.clone());

        index.start_batch().unwrap();
        index.index_symbol(&symbol, "src/lib.rs").unwrap();
        index.commit_batch().unwrap();

        let retrieved = index
            .find_symbol_by_id(SymbolId::new(7).unwrap())
            .unwrap()
            .expect("symbol stored by this test");
        assert_eq!(retrieved.scope_context, Some(scope));
    }
}