1use std::path::PathBuf;
8
9use serde::{Deserialize, Serialize};
10
11use crate::serde_path;
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
15#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
16#[serde(rename_all = "kebab-case")]
17pub enum SemanticCapability {
18 SymbolUse,
20 SymbolTrace,
22 ApiSurface,
24 SymbolImpact,
26 TypeCoupling,
28}
29
30#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
32#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
33#[serde(rename_all = "kebab-case")]
34pub enum SemanticCompletenessRequirement {
35 #[default]
37 BestEffort,
38 Complete,
40}
41
42#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
44#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
45#[serde(rename_all = "kebab-case")]
46pub enum SemanticCompleteness {
47 Complete,
49 Partial,
51 #[default]
53 Unavailable,
54}
55
56#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
58#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
59#[serde(rename_all = "kebab-case")]
60pub enum SemanticAnalysisMode {
61 #[default]
63 Syntactic,
64 TypeAware,
66}
67
68#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
70#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
71pub struct SemanticAnalysisIdentity {
72 pub mode: SemanticAnalysisMode,
74 pub semantic_schema_version: u32,
76 pub capabilities: Vec<SemanticCapability>,
78 pub project_config_hash: String,
80 pub backend_family: String,
82 pub completeness: SemanticCompleteness,
84}
85
86pub const DEFERRED_PROJECT_CONFIG_HASH: &str = "deferred:no-semantic-queries";
92
93impl Default for SemanticAnalysisIdentity {
94 fn default() -> Self {
95 Self {
96 mode: SemanticAnalysisMode::Syntactic,
97 semantic_schema_version: 1,
98 capabilities: Vec::new(),
99 project_config_hash: String::new(),
100 backend_family: String::new(),
101 completeness: SemanticCompleteness::Complete,
102 }
103 }
104}
105
106impl SemanticAnalysisIdentity {
107 #[must_use]
110 pub fn syntactic() -> Self {
111 Self::default()
112 }
113
114 #[must_use]
118 pub fn incompatible_fields(&self, other: &Self) -> Vec<&'static str> {
119 let mut fields = Vec::new();
120 if self.mode != other.mode {
121 fields.push("mode");
122 }
123 if self.semantic_schema_version != other.semantic_schema_version {
124 fields.push("semantic_schema_version");
125 }
126 if self.capabilities != other.capabilities {
127 fields.push("capabilities");
128 }
129 let project_hash_deferred = self.project_config_hash == DEFERRED_PROJECT_CONFIG_HASH
130 || other.project_config_hash == DEFERRED_PROJECT_CONFIG_HASH;
131 if !project_hash_deferred && self.project_config_hash != other.project_config_hash {
132 fields.push("project_config_hash");
133 }
134 if self.backend_family != other.backend_family {
135 fields.push("backend_family");
136 }
137 if self.completeness != other.completeness {
138 fields.push("completeness");
139 }
140 fields
141 }
142}
143
144#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
146#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
147#[serde(rename_all = "kebab-case")]
148pub enum SemanticNamespace {
149 #[default]
151 Value,
152 Type,
154}
155
156#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
158#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
159pub struct SemanticSymbol {
160 #[serde(serialize_with = "serde_path::serialize")]
162 pub path: PathBuf,
163 pub namespace: SemanticNamespace,
165 pub declaration_kind: String,
167 pub exported_name: String,
169 pub local_name: String,
171 #[serde(default, skip_serializing_if = "Option::is_none")]
173 pub owner: Option<String>,
174 pub line: u32,
176 pub col: u32,
178}
179
180#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
182#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
183pub struct SemanticSourceLocation {
184 #[serde(serialize_with = "serde_path::serialize")]
186 pub path: PathBuf,
187 pub line: u32,
189 pub col: u32,
191}
192
193#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
195#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
196#[serde(rename_all = "kebab-case")]
197pub enum SemanticGapReason {
198 NoProject,
200 AmbiguousProject,
202 BlockingDiagnostics,
204 SvelteVirtualModuleExports,
207 UnknownSymbol,
209 UnknownEntryPoint,
211 EvidenceLimit,
213 DynamicBehavior,
215 VirtualDispatch,
218 DynamicMemberAccess,
220 DecoratedDeclaration,
222 OptionalContract,
224 AccessorPair,
226 OverloadSet,
228 AttachedComment,
230 AbstractDeclaration,
232 IncompleteProjectCoverage,
234 FrameworkContractProvenance,
237 Capacity,
239 UnsupportedSyntax,
241}
242
243#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
245#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
246pub struct SemanticOmission {
247 pub reason_code: SemanticGapReason,
249 pub count: usize,
251}
252
253#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
255#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
256#[serde(rename_all = "kebab-case")]
257pub enum SemanticCandidateDecisionKind {
258 ConfirmedUsed,
260 ContractPreserved,
262 ConfirmedNoStaticReferences,
264 RetainedAbstained,
266 RetainedUnresolved,
268}
269
270#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
272#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
273#[serde(rename_all = "kebab-case")]
274pub enum SemanticContractRelation {
275 InterfaceImplementation,
277 AbstractImplementation,
279 Override,
281 OptionalContract,
283 DependencyHook,
287}
288
289#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
291#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
292pub struct SemanticContractEvidence {
293 pub relation: SemanticContractRelation,
295 pub declaration: SemanticSymbol,
297 pub optional: bool,
299}
300
301#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
303#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
304#[serde(rename_all = "kebab-case")]
305pub enum SemanticFrameworkRelation {
306 Extends,
308 Implements,
310}
311
312#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
314#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
315pub struct SemanticFrameworkContract {
316 pub framework: String,
318 pub package: String,
320 pub heritage_symbol: String,
322 pub heritage_names: Vec<String>,
324 pub relation: SemanticFrameworkRelation,
326 pub members: Vec<String>,
328}
329
330#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
332#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
333pub struct SemanticFrameworkContractEvidence {
334 pub framework: String,
336 pub package: String,
338 pub relation: SemanticFrameworkRelation,
340 pub declaration: SemanticSymbol,
342}
343
344#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
346#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
347pub struct SemanticEditGuard {
348 pub start: usize,
350 pub end: usize,
352 pub declaration_sha256: String,
354}
355
356#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
358#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
359pub struct SemanticCandidateDecision {
360 pub query_id: usize,
362 pub subject: SemanticSymbol,
364 pub decision: SemanticCandidateDecisionKind,
366 pub status: SemanticCompleteness,
368 pub owning_projects: Vec<String>,
370 #[serde(default, skip_serializing_if = "Vec::is_empty")]
372 pub evidence: Vec<SemanticReference>,
373 #[serde(default, skip_serializing_if = "Option::is_none")]
375 pub contract: Option<SemanticContractEvidence>,
376 #[serde(default, skip_serializing_if = "Option::is_none")]
378 pub framework_contract: Option<SemanticFrameworkContractEvidence>,
379 pub closed_world_eligible: bool,
381 #[serde(default, skip_serializing_if = "Option::is_none")]
383 pub edit_guard: Option<SemanticEditGuard>,
384 #[serde(default, skip_serializing_if = "Option::is_none")]
386 pub reason_code: Option<SemanticGapReason>,
387 pub explanation: String,
389 #[serde(default, skip_serializing_if = "Vec::is_empty")]
391 pub actions: Vec<String>,
392 pub total_evidence_count: usize,
394 pub truncated: bool,
396 #[serde(default, skip_serializing_if = "Vec::is_empty")]
398 pub omissions: Vec<SemanticOmission>,
399}
400
401#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
403#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
404pub struct SemanticQuerySummary {
405 pub query_id: usize,
407 pub capability: SemanticCapability,
409 pub assertion: String,
411 pub status: SemanticCompleteness,
413 #[serde(default, skip_serializing_if = "Option::is_none")]
415 pub reason_code: Option<SemanticGapReason>,
416 pub total_evidence_count: usize,
418 pub truncated: bool,
420 #[serde(default, skip_serializing_if = "Vec::is_empty")]
422 pub omissions: Vec<SemanticOmission>,
423 #[serde(default, skip_serializing_if = "Vec::is_empty")]
425 pub actions: Vec<String>,
426}
427
428#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
430#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
431pub struct SemanticReference {
432 #[serde(serialize_with = "serde_path::serialize")]
434 pub path: PathBuf,
435 pub line: u32,
437 pub col: u32,
439 pub role: String,
441 pub namespace: SemanticNamespace,
443 #[serde(default, skip_serializing_if = "Vec::is_empty")]
445 pub via: Vec<SemanticAliasHop>,
446}
447
448#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
450#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
451pub struct SemanticAliasHop {
452 #[serde(serialize_with = "serde_path::serialize")]
454 pub path: PathBuf,
455 pub from_name: String,
457 pub to_name: String,
459 pub relation: String,
461}
462
463#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
465#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
466pub struct SemanticSymbolTrace {
467 pub target: SemanticSymbol,
469 pub identity: SemanticAnalysisIdentity,
471 pub selected_project: String,
473 pub assertion: String,
475 pub status: SemanticCompleteness,
477 pub references: Vec<SemanticReference>,
479 pub total_reference_count: usize,
481 pub checker_evidence_count: usize,
483 pub graph_evidence_count: usize,
485 pub truncated: bool,
487 #[serde(default, skip_serializing_if = "Vec::is_empty")]
489 pub omissions: Vec<SemanticOmission>,
490 #[serde(default, skip_serializing_if = "Vec::is_empty")]
492 pub actions: Vec<String>,
493}
494
495#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
497#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
498pub struct PublicTypeReference {
499 pub declaration: SemanticSymbol,
501 pub relation: String,
503}
504
505#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
507#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
508pub struct ApiSurfaceEntry {
509 pub exposed: SemanticSymbol,
511 pub origin: SemanticSymbol,
513 pub signature_fingerprint: String,
515 pub referenced_types: Vec<PublicTypeReference>,
517}
518
519#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
521#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
522pub struct SemanticPrivateTypeLeak {
523 pub exposed: SemanticSymbol,
525 pub private_declaration: SemanticSymbol,
527 pub relation: String,
529 #[serde(default, skip_serializing_if = "Option::is_none")]
531 pub diagnostic_code: Option<u32>,
532}
533
534#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
536#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
537pub struct ApiSurfaceResult {
538 pub assertion: String,
540 pub status: SemanticCompleteness,
542 pub entries: Vec<ApiSurfaceEntry>,
544 pub private_type_leaks: Vec<SemanticPrivateTypeLeak>,
546 #[serde(default, skip_serializing_if = "Vec::is_empty")]
548 pub omissions: Vec<SemanticOmission>,
549 #[serde(default, skip_serializing_if = "Vec::is_empty")]
551 pub actions: Vec<String>,
552}
553
554#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
556#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
557pub struct SemanticImpactPath {
558 #[serde(serialize_with = "serde_path::serialize")]
560 pub path: PathBuf,
561 pub relation: String,
563 pub distance: usize,
565 #[serde(default, skip_serializing_if = "Vec::is_empty")]
567 #[serde(serialize_with = "serde_path::serialize_vec")]
568 pub via: Vec<PathBuf>,
569}
570
571#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
573#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
574#[serde(rename_all = "kebab-case")]
575pub enum SemanticImpactConfidence {
576 High,
579 Bounded,
582 Unavailable,
584}
585
586impl std::fmt::Display for SemanticImpactConfidence {
587 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
588 formatter.write_str(match self {
589 Self::High => "high",
590 Self::Bounded => "bounded",
591 Self::Unavailable => "unavailable",
592 })
593 }
594}
595
596#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
598#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
599pub struct SemanticSymbolImpact {
600 pub target: SemanticSymbol,
602 pub identity: SemanticAnalysisIdentity,
604 pub selected_project: String,
606 pub assertion: String,
608 pub status: SemanticCompleteness,
610 pub direct_consumers: Vec<SemanticImpactPath>,
612 pub total_direct_consumer_count: usize,
614 pub affected_files: Vec<SemanticImpactPath>,
616 pub total_affected_file_count: usize,
618 pub targeted_tests: Vec<SemanticImpactPath>,
620 pub total_targeted_test_count: usize,
622 pub confidence: SemanticImpactConfidence,
624 #[serde(default, skip_serializing_if = "Vec::is_empty")]
626 pub omissions: Vec<SemanticOmission>,
627 #[serde(default, skip_serializing_if = "Vec::is_empty")]
629 pub actions: Vec<String>,
630}
631
632#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
634#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
635pub struct TypeCouplingEdge {
636 pub source: SemanticSymbol,
638 pub target: SemanticSymbol,
640 pub relation: String,
642 pub evidence: SemanticSourceLocation,
644 pub scope: String,
646}
647
648#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
650#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
651pub struct TypeCouplingFile {
652 #[serde(serialize_with = "serde_path::serialize")]
654 pub path: PathBuf,
655 pub public_api_depends_on: usize,
657 #[serde(default, skip_serializing_if = "Vec::is_empty")]
659 #[serde(serialize_with = "serde_path::serialize_vec")]
660 pub public_api_depends_on_files: Vec<PathBuf>,
661 pub public_types_used_by: usize,
663 #[serde(default, skip_serializing_if = "Vec::is_empty")]
665 #[serde(serialize_with = "serde_path::serialize_vec")]
666 pub public_types_used_by_files: Vec<PathBuf>,
667 pub edges: Vec<TypeCouplingEdge>,
669}
670
671#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
673#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
674pub struct TypeCouplingCycle {
675 #[serde(serialize_with = "serde_path::serialize_vec")]
677 pub files: Vec<PathBuf>,
678}
679
680#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
682#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
683pub struct TypeCouplingSummary {
684 pub scope: String,
686 pub direction: String,
688 pub project_size: usize,
690 pub files_analyzed: usize,
692 pub distinct_coupled_files: usize,
694 pub edge_count: usize,
696 pub coupled_file_pct: f64,
698 pub p50_distinct_connections: f64,
700 pub p90_distinct_connections: f64,
702 pub p95_public_types_used_by: f64,
704 pub p95_public_api_depends_on: f64,
706 pub high_coupling_pct: f64,
708 pub concentration: f64,
710 pub cycle_count: usize,
712}
713
714#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
716#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
717pub struct TypeCouplingReport {
718 pub identity: SemanticAnalysisIdentity,
720 pub assertion: String,
722 pub status: SemanticCompleteness,
724 #[serde(default, skip_serializing_if = "Option::is_none")]
726 pub summary: Option<TypeCouplingSummary>,
727 pub files: Vec<TypeCouplingFile>,
729 #[serde(default, skip_serializing_if = "Vec::is_empty")]
731 pub top_contributors: Vec<TypeCouplingFile>,
732 #[serde(default, skip_serializing_if = "Vec::is_empty")]
734 pub cycles: Vec<TypeCouplingCycle>,
735 #[serde(default, skip_serializing_if = "Vec::is_empty")]
737 pub omissions: Vec<SemanticOmission>,
738 #[serde(default, skip_serializing_if = "Vec::is_empty")]
740 pub actions: Vec<String>,
741}
742
743#[cfg(test)]
744mod tests {
745 use super::*;
746
747 #[test]
748 fn semantic_identity_reports_each_compatibility_dimension() {
749 let syntactic = SemanticAnalysisIdentity::syntactic();
750 assert!(syntactic.incompatible_fields(&syntactic).is_empty());
751
752 let type_aware = SemanticAnalysisIdentity {
753 mode: SemanticAnalysisMode::TypeAware,
754 semantic_schema_version: 2,
755 capabilities: vec![SemanticCapability::SymbolUse],
756 project_config_hash: "sha256:project".to_string(),
757 backend_family: "typescript-go".to_string(),
758 completeness: SemanticCompleteness::Partial,
759 };
760 assert_eq!(
761 syntactic.incompatible_fields(&type_aware),
762 vec![
763 "mode",
764 "semantic_schema_version",
765 "capabilities",
766 "project_config_hash",
767 "backend_family",
768 "completeness",
769 ]
770 );
771
772 let mut deferred = type_aware.clone();
773 deferred.project_config_hash = DEFERRED_PROJECT_CONFIG_HASH.to_string();
774 assert!(
775 deferred
776 .incompatible_fields(&type_aware)
777 .iter()
778 .all(|field| *field != "project_config_hash")
779 );
780 }
781}