ucp-codegraph 0.1.16

CodeGraph extraction and projection for UCP
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
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};
use std::path::PathBuf;
use ucm_core::Document;

pub const CODEGRAPH_PROFILE: &str = "codegraph";
pub const CODEGRAPH_PROFILE_VERSION: &str = "v1";
pub const CODEGRAPH_PROFILE_MARKER: &str = "codegraph.v1";
pub const CODEGRAPH_EXTRACTOR_VERSION: &str = "ucp-codegraph-extractor.v1";

pub(crate) const META_NODE_CLASS: &str = "node_class";
pub(crate) const META_LOGICAL_KEY: &str = "logical_key";
pub(crate) const META_CODEREF: &str = "coderef";
pub(crate) const META_LANGUAGE: &str = "language";
pub(crate) const META_SYMBOL_KIND: &str = "symbol_kind";
pub(crate) const META_SYMBOL_NAME: &str = "name";
pub(crate) const META_EXPORTED: &str = "exported";

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CodeGraphSeverity {
    Error,
    Warning,
    Info,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CodeGraphDiagnostic {
    pub severity: CodeGraphSeverity,
    pub code: String,
    pub message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub path: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub logical_key: Option<String>,
}

impl CodeGraphDiagnostic {
    pub(crate) fn error(code: &str, message: impl Into<String>) -> Self {
        Self {
            severity: CodeGraphSeverity::Error,
            code: code.to_string(),
            message: message.into(),
            path: None,
            logical_key: None,
        }
    }

    pub(crate) fn warning(code: &str, message: impl Into<String>) -> Self {
        Self {
            severity: CodeGraphSeverity::Warning,
            code: code.to_string(),
            message: message.into(),
            path: None,
            logical_key: None,
        }
    }

    pub(crate) fn info(code: &str, message: impl Into<String>) -> Self {
        Self {
            severity: CodeGraphSeverity::Info,
            code: code.to_string(),
            message: message.into(),
            path: None,
            logical_key: None,
        }
    }

    pub(crate) fn with_path(mut self, path: impl Into<String>) -> Self {
        self.path = Some(path.into());
        self
    }

    pub(crate) fn with_logical_key(mut self, logical_key: impl Into<String>) -> Self {
        self.logical_key = Some(logical_key.into());
        self
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct CodeGraphValidationResult {
    pub valid: bool,
    pub diagnostics: Vec<CodeGraphDiagnostic>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct CodeGraphStats {
    pub total_nodes: usize,
    pub repository_nodes: usize,
    pub directory_nodes: usize,
    pub file_nodes: usize,
    pub symbol_nodes: usize,
    pub total_edges: usize,
    pub reference_edges: usize,
    pub export_edges: usize,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub languages: BTreeMap<String, usize>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CodeGraphBuildStatus {
    Success,
    PartialSuccess,
    FailedValidation,
}

#[derive(Debug, Clone)]
pub struct CodeGraphBuildResult {
    pub document: Document,
    pub diagnostics: Vec<CodeGraphDiagnostic>,
    pub stats: CodeGraphStats,
    pub profile_version: String,
    pub canonical_fingerprint: String,
    pub status: CodeGraphBuildStatus,
    pub incremental: Option<CodeGraphIncrementalStats>,
}

impl CodeGraphBuildResult {
    pub fn has_errors(&self) -> bool {
        self.diagnostics
            .iter()
            .any(|d| d.severity == CodeGraphSeverity::Error)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodeGraphBuildInput {
    pub repository_path: PathBuf,
    pub commit_hash: String,
    #[serde(default)]
    pub config: CodeGraphExtractorConfig,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodeGraphIncrementalBuildInput {
    pub build: CodeGraphBuildInput,
    pub state_file: PathBuf,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct CodeGraphIncrementalStats {
    #[serde(default)]
    pub requested: bool,
    #[serde(default)]
    pub scanned_files: usize,
    #[serde(default)]
    pub state_entries: usize,
    #[serde(default)]
    pub direct_invalidated_files: usize,
    #[serde(default)]
    pub surface_changed_files: usize,
    #[serde(default)]
    pub reused_files: usize,
    #[serde(default)]
    pub rebuilt_files: usize,
    #[serde(default)]
    pub added_files: usize,
    #[serde(default)]
    pub changed_files: usize,
    #[serde(default)]
    pub deleted_files: usize,
    #[serde(default)]
    pub invalidated_files: usize,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub full_rebuild_reason: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CodeGraphExtractorConfig {
    #[serde(default = "default_include_extensions")]
    pub include_extensions: Vec<String>,
    #[serde(default = "default_exclude_dirs")]
    pub exclude_dirs: Vec<String>,
    #[serde(default = "default_continue_on_parse_error")]
    pub continue_on_parse_error: bool,
    #[serde(default)]
    pub include_hidden: bool,
    #[serde(default = "default_max_file_bytes")]
    pub max_file_bytes: usize,
    #[serde(default = "default_emit_export_edges")]
    pub emit_export_edges: bool,
}

impl Default for CodeGraphExtractorConfig {
    fn default() -> Self {
        Self {
            include_extensions: default_include_extensions(),
            exclude_dirs: default_exclude_dirs(),
            continue_on_parse_error: default_continue_on_parse_error(),
            include_hidden: false,
            max_file_bytes: default_max_file_bytes(),
            emit_export_edges: default_emit_export_edges(),
        }
    }
}

fn default_include_extensions() -> Vec<String> {
    vec!["rs", "py", "ts", "tsx", "js", "jsx"]
        .into_iter()
        .map(|s| s.to_string())
        .collect()
}

fn default_exclude_dirs() -> Vec<String> {
    vec![".git", "target", "node_modules", "dist", "build"]
        .into_iter()
        .map(|s| s.to_string())
        .collect()
}

fn default_continue_on_parse_error() -> bool {
    true
}

fn default_max_file_bytes() -> usize {
    2 * 1024 * 1024
}

fn default_emit_export_edges() -> bool {
    true
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum CodeLanguage {
    Rust,
    Python,
    TypeScript,
    JavaScript,
}

impl CodeLanguage {
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            Self::Rust => "rust",
            Self::Python => "python",
            Self::TypeScript => "typescript",
            Self::JavaScript => "javascript",
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct RepoFile {
    pub(crate) absolute_path: PathBuf,
    pub(crate) relative_path: String,
    pub(crate) language: CodeLanguage,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct ExtractedSymbol {
    pub(crate) name: String,
    pub(crate) qualified_name: String,
    pub(crate) identity: String,
    pub(crate) parent_identity: Option<String>,
    pub(crate) kind: String,
    pub(crate) description: Option<String>,
    pub(crate) modifiers: ExtractedModifiers,
    pub(crate) inputs: Vec<ExtractedInput>,
    pub(crate) output: Option<String>,
    pub(crate) type_info: Option<String>,
    pub(crate) exported: bool,
    pub(crate) start_line: usize,
    pub(crate) start_col: usize,
    pub(crate) end_line: usize,
    pub(crate) end_col: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct ExtractedInput {
    pub(crate) name: String,
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub(crate) type_name: Option<String>,
}

#[derive(Debug, Clone, Default)]
pub(crate) struct ExtractedSignature {
    pub(crate) inputs: Vec<ExtractedInput>,
    pub(crate) output: Option<String>,
    pub(crate) type_info: Option<String>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub(crate) struct ExtractedModifiers {
    #[serde(rename = "async", skip_serializing_if = "is_false")]
    pub(crate) is_async: bool,
    #[serde(skip_serializing_if = "is_false")]
    pub(crate) generator: bool,
    #[serde(rename = "static", skip_serializing_if = "is_false")]
    pub(crate) is_static: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) visibility: Option<String>,
}

impl ExtractedModifiers {
    pub(crate) fn is_empty(&self) -> bool {
        !self.is_async && !self.generator && !self.is_static && self.visibility.is_none()
    }
}

fn is_false(value: &bool) -> bool {
    !*value
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct ExtractedImport {
    pub(crate) module: String,
    pub(crate) symbols: Vec<String>,
    pub(crate) bindings: Vec<ImportBinding>,
    pub(crate) module_aliases: Vec<String>,
    pub(crate) reexported: bool,
    pub(crate) wildcard: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub(crate) struct ImportBinding {
    pub(crate) source_name: String,
    pub(crate) local_name: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct ExtractedRelationship {
    pub(crate) source_identity: String,
    pub(crate) relation: String,
    pub(crate) target_expr: String,
    pub(crate) target_name: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct ExtractedUsage {
    pub(crate) source_identity: String,
    pub(crate) target_expr: String,
    pub(crate) target_name: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct ExtractedAlias {
    pub(crate) name: String,
    pub(crate) target_expr: String,
    pub(crate) target_name: String,
    pub(crate) owner_identity: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum ImportResolution {
    Resolved(String),
    External,
    Unresolved,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub(crate) struct FileAnalysis {
    pub(crate) file_description: Option<String>,
    pub(crate) symbols: Vec<ExtractedSymbol>,
    pub(crate) imports: Vec<ExtractedImport>,
    pub(crate) relationships: Vec<ExtractedRelationship>,
    pub(crate) usages: Vec<ExtractedUsage>,
    pub(crate) aliases: Vec<ExtractedAlias>,
    pub(crate) export_bindings: Vec<ImportBinding>,
    pub(crate) exported_symbol_names: BTreeSet<String>,
    pub(crate) default_exported_symbol_names: BTreeSet<String>,
    pub(crate) diagnostics: Vec<CodeGraphDiagnostic>,
}

#[derive(Debug, Clone)]
pub(crate) struct FileAnalysisRecord {
    pub(crate) file: String,
    pub(crate) language: CodeLanguage,
    pub(crate) imports: Vec<ExtractedImport>,
    pub(crate) relationships: Vec<ExtractedRelationship>,
    pub(crate) usages: Vec<ExtractedUsage>,
    pub(crate) aliases: Vec<ExtractedAlias>,
    pub(crate) export_bindings: Vec<ImportBinding>,
}

impl ExtractedImport {
    pub(crate) fn module(module: impl Into<String>) -> Self {
        Self {
            module: module.into(),
            symbols: Vec::new(),
            bindings: Vec::new(),
            module_aliases: Vec::new(),
            reexported: false,
            wildcard: false,
        }
    }

    pub(crate) fn symbol(module: impl Into<String>, symbol: impl Into<String>) -> Self {
        let symbol = symbol.into();
        Self {
            module: module.into(),
            symbols: vec![symbol.clone()],
            bindings: vec![ImportBinding::same(symbol)],
            module_aliases: Vec::new(),
            reexported: false,
            wildcard: false,
        }
    }

    pub(crate) fn bindings(module: impl Into<String>, bindings: Vec<ImportBinding>) -> Self {
        let mut symbols = bindings
            .iter()
            .map(|binding| binding.source_name.clone())
            .collect::<Vec<_>>();
        symbols.sort();
        symbols.dedup();

        Self {
            module: module.into(),
            symbols,
            bindings,
            module_aliases: Vec::new(),
            reexported: false,
            wildcard: false,
        }
    }

    pub(crate) fn with_module_alias(mut self, alias: impl Into<String>) -> Self {
        let alias = alias.into();
        if !alias.is_empty() && !self.module_aliases.contains(&alias) {
            self.module_aliases.push(alias);
        }
        self
    }

    pub(crate) fn reexported(mut self) -> Self {
        self.reexported = true;
        self
    }

    pub(crate) fn wildcard(mut self) -> Self {
        self.wildcard = true;
        self
    }
}

impl ImportBinding {
    pub(crate) fn new(source_name: impl Into<String>, local_name: impl Into<String>) -> Self {
        Self {
            source_name: source_name.into(),
            local_name: local_name.into(),
        }
    }

    pub(crate) fn same(name: impl Into<String>) -> Self {
        let name = name.into();
        Self::new(name.clone(), name)
    }
}

impl ExtractedRelationship {
    pub(crate) fn new(
        source_identity: impl Into<String>,
        relation: impl Into<String>,
        target_expr: impl Into<String>,
        target_name: impl Into<String>,
    ) -> Self {
        Self {
            source_identity: source_identity.into(),
            relation: relation.into(),
            target_expr: target_expr.into(),
            target_name: target_name.into(),
        }
    }
}

impl ExtractedUsage {
    pub(crate) fn new(
        source_identity: impl Into<String>,
        target_expr: impl Into<String>,
        target_name: impl Into<String>,
    ) -> Self {
        Self {
            source_identity: source_identity.into(),
            target_expr: target_expr.into(),
            target_name: target_name.into(),
        }
    }
}

impl ExtractedAlias {
    pub(crate) fn new(
        name: impl Into<String>,
        target_expr: impl Into<String>,
        target_name: impl Into<String>,
        owner_identity: Option<&str>,
    ) -> Self {
        Self {
            name: name.into(),
            target_expr: target_expr.into(),
            target_name: target_name.into(),
            owner_identity: owner_identity.map(str::to_string),
        }
    }
}