code-system-graph-core 1.1.0

Core extraction, linking, and query engine for Code System Graph.
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
use std::collections::BTreeMap;

use code_system_graph_model::{
    Edge, EdgeId, EdgeKind, EpistemicStatus, Evidence, EvidenceId, Node, NodeId, NodeKind, Provenance, RepoId, stable_id
};

use crate::linker::{http_link_ambiguity, sort_http_ambiguities};
use crate::{
    BoundaryRole, ContractImplementationConfig, HttpBoundary, HttpLinkResolution, IntegrationTestConfig, LinkError, normalize_http_path
};

/// Declared cross-language test case and its validated HTTP target.
#[derive(Debug, Clone, PartialEq)]
pub struct DeclaredTestCase {
    /// Federated test-case node.
    pub node: Node,
    /// Canonical target HTTP method.
    pub method: String,
    /// Canonical target HTTP path.
    pub path: String,
    /// Evidence from the test source declaration.
    pub evidence: Evidence,
}

/// Declared source symbol and the HTTP contract it implements.
#[derive(Debug, Clone, PartialEq)]
pub struct DeclaredImplementation {
    /// Lightweight source symbol node.
    pub node: Node,
    /// Canonical implemented HTTP method.
    pub method: String,
    /// Canonical implemented HTTP path.
    pub path: String,
    /// Evidence from the source declaration and file fingerprint.
    pub evidence: Evidence,
}

/// Creates a test-case node from strict declared metadata.
#[must_use]
pub fn declared_test_case(
    repo_id: RepoId,
    config: &IntegrationTestConfig,
    content_hash: Option<String>,
) -> DeclaredTestCase {
    let method = config.validates.method.trim().to_ascii_uppercase();
    let path = normalize_http_path(&config.validates.path);
    let stable_key = format!(
        "test:{}:{}:{}:{}:{}",
        repo_id.as_str(),
        config.language.trim().to_ascii_lowercase(),
        config.framework.trim().to_ascii_lowercase(),
        config.path,
        config.name
    );
    let evidence_key = format!("{stable_key}:{method}:{path}");
    DeclaredTestCase {
        node: Node {
            id: NodeId::new(stable_id("node", &stable_key)),
            kind: NodeKind::TestCase,
            repo_id: Some(repo_id.clone()),
            stable_key,
            label: format!("{}/{}::{}", config.language, config.framework, config.name),
        },
        method,
        path,
        evidence: Evidence {
            id: EvidenceId::new(stable_id("evidence", &evidence_key)),
            repo_id: Some(repo_id),
            file_path: Some(config.path.clone()),
            start_line: None,
            end_line: None,
            extractor: "code-system-graph.tests.declared".to_owned(),
            extractor_version: env!("CARGO_PKG_VERSION").to_owned(),
            provenance: Provenance::Declared,
            confidence: 1.0,
            observed_at_commit: None,
            content_hash,
            note: Some(format!(
                "{} test declared with {}",
                config.language, config.framework
            )),
        },
    }
}

/// Creates a lightweight implementation anchor from strict declared metadata.
#[must_use]
pub fn declared_implementation(
    repo_id: RepoId,
    config: &ContractImplementationConfig,
    content_hash: Option<String>,
) -> DeclaredImplementation {
    let method = config.implements.method.trim().to_ascii_uppercase();
    let path = normalize_http_path(&config.implements.path);
    let identity = crate::SourceSymbolIdentity::new(
        repo_id.clone(),
        &config.language,
        &config.path,
        &config.symbol,
    );
    let stable_key = identity.stable_key();
    let evidence_key = format!("{stable_key}:{method}:{path}");
    DeclaredImplementation {
        node: identity.node(format!("{}::{}", config.language, config.symbol)),
        method,
        path,
        evidence: Evidence {
            id: EvidenceId::new(stable_id("evidence", &evidence_key)),
            repo_id: Some(repo_id),
            file_path: Some(config.path.clone()),
            start_line: None,
            end_line: None,
            extractor: "code-system-graph.implementations.declared".to_owned(),
            extractor_version: env!("CARGO_PKG_VERSION").to_owned(),
            provenance: Provenance::Declared,
            confidence: 1.0,
            observed_at_commit: None,
            content_hash,
            note: Some(format!(
                "{} symbol declared as contract implementation",
                config.language
            )),
        },
    }
}

/// Links declared tests to exact HTTP providers with bilateral evidence.
///
/// Tests with no observed provider remain unlinked instead of inventing a target.
///
/// Duplicate providers remain fail-closed for compatibility. Use
/// [`link_declared_tests_with_ambiguities`] to preserve ambiguities as data while continuing with
/// unrelated contracts.
///
/// # Errors
///
/// Returns [`LinkError::AmbiguousProvider`] instead of silently omitting an ambiguous relationship.
pub fn link_declared_tests(
    tests: &[DeclaredTestCase],
    boundaries: &[HttpBoundary],
) -> Result<Vec<Edge>, LinkError> {
    link_declared_tests_with_ambiguities(tests, boundaries).into_legacy_result()
}

/// Links declared tests while preserving duplicate-provider decisions.
#[must_use]
pub fn link_declared_tests_with_ambiguities(
    tests: &[DeclaredTestCase],
    boundaries: &[HttpBoundary],
) -> HttpLinkResolution {
    let mut providers: BTreeMap<(&str, &str), Vec<&HttpBoundary>> = BTreeMap::new();
    for provider in boundaries
        .iter()
        .filter(|boundary| boundary.role == BoundaryRole::Provider)
    {
        let candidates = providers
            .entry((&provider.method, &provider.path))
            .or_default();
        if let Some(existing) = candidates
            .iter()
            .position(|candidate| candidate.node.id == provider.node.id)
        {
            if provider.evidence.confidence > candidates[existing].evidence.confidence {
                candidates[existing] = provider;
            }
        } else {
            candidates.push(provider);
        }
    }
    let mut edges = Vec::new();
    let mut ambiguities = Vec::new();
    for test in tests {
        let Some(candidates) = providers.get(&(test.method.as_str(), test.path.as_str())) else {
            continue;
        };
        if candidates.len() > 1 {
            ambiguities.push(http_link_ambiguity(
                &test.method,
                &test.path,
                candidates.iter().map(|candidate| &candidate.node.id),
            ));
            continue;
        }
        let provider = candidates[0];
        let edge_key = format!(
            "{}:validates:{}",
            test.node.id.as_str(),
            provider.node.id.as_str()
        );
        edges.push(Edge {
            id: EdgeId::new(stable_id("edge", &edge_key)),
            source: test.node.id.clone(),
            target: provider.node.id.clone(),
            kind: EdgeKind::Validates,
            confidence: test.evidence.confidence.min(provider.evidence.confidence),
            status: consensus_status(test.evidence.confidence.min(provider.evidence.confidence)),
            evidence: vec![test.evidence.id.clone(), provider.evidence.id.clone()],
        });
    }
    edges.sort_by(|left, right| left.id.cmp(&right.id));
    sort_http_ambiguities(&mut ambiguities);
    HttpLinkResolution { edges, ambiguities }
}

/// Links HTTP provider contracts to declared source implementations.
///
/// Duplicate providers remain fail-closed for compatibility. Use
/// [`link_declared_implementations_with_ambiguities`] to preserve ambiguities as data while
/// continuing with unrelated contracts.
///
/// # Errors
///
/// Returns [`LinkError::AmbiguousProvider`] instead of silently omitting an ambiguous relationship.
pub fn link_declared_implementations(
    implementations: &[DeclaredImplementation],
    boundaries: &[HttpBoundary],
) -> Result<Vec<Edge>, LinkError> {
    link_declared_implementations_with_ambiguities(implementations, boundaries).into_legacy_result()
}

/// Links declared implementations while preserving duplicate-provider decisions.
#[must_use]
pub fn link_declared_implementations_with_ambiguities(
    implementations: &[DeclaredImplementation],
    boundaries: &[HttpBoundary],
) -> HttpLinkResolution {
    let mut edges = BTreeMap::<EdgeId, Edge>::new();
    let mut ambiguities = Vec::new();
    for implementation in implementations {
        let candidates = boundaries
            .iter()
            .filter(|boundary| {
                boundary.role == BoundaryRole::Provider
                    && boundary.node.repo_id == implementation.node.repo_id
                    && boundary.method == implementation.method
                    && boundary.path == implementation.path
            })
            .fold(
                BTreeMap::<NodeId, &HttpBoundary>::new(),
                |mut candidates, boundary| {
                    candidates
                        .entry(boundary.node.id.clone())
                        .and_modify(|existing| {
                            if boundary.evidence.confidence > existing.evidence.confidence {
                                *existing = boundary;
                            }
                        })
                        .or_insert(boundary);
                    candidates
                },
            )
            .into_values()
            .collect::<Vec<_>>();
        if candidates.len() > 1 {
            ambiguities.push(http_link_ambiguity(
                &implementation.method,
                &implementation.path,
                candidates.iter().map(|candidate| &candidate.node.id),
            ));
            continue;
        }
        let Some(provider) = candidates.first() else {
            continue;
        };
        let edge_key = format!(
            "{}:implemented_by:{}",
            provider.node.id.as_str(),
            implementation.node.id.as_str()
        );
        let confidence = provider
            .evidence
            .confidence
            .min(implementation.evidence.confidence);
        let edge = Edge {
            id: EdgeId::new(stable_id("edge", &edge_key)),
            source: provider.node.id.clone(),
            target: implementation.node.id.clone(),
            kind: EdgeKind::ImplementedBy,
            confidence,
            status: consensus_status(confidence),
            evidence: vec![
                provider.evidence.id.clone(),
                implementation.evidence.id.clone(),
            ],
        };
        edges
            .entry(edge.id.clone())
            .and_modify(|existing| merge_equivalent_implementation_edge(existing, &edge))
            .or_insert(edge);
    }
    sort_http_ambiguities(&mut ambiguities);
    HttpLinkResolution {
        edges: edges.into_values().collect(),
        ambiguities,
    }
}

fn merge_equivalent_implementation_edge(existing: &mut Edge, candidate: &Edge) {
    existing.confidence = existing.confidence.max(candidate.confidence);
    existing.status = consensus_status(existing.confidence);
    existing.evidence.extend(candidate.evidence.iter().cloned());
    existing.evidence.sort();
    existing.evidence.dedup();
}

fn consensus_status(confidence: f32) -> EpistemicStatus {
    if confidence >= 1.0 {
        EpistemicStatus::Confirmed
    } else {
        EpistemicStatus::Inferred
    }
}

#[cfg(test)]
mod tests {
    use code_system_graph_model::{EdgeKind, EpistemicStatus, RepoId};

    use super::{declared_test_case, link_declared_implementations, link_declared_tests};
    use crate::{
        HttpContractConfig, IntegrationTestConfig, extract_openapi, parse_rust_source, source_observations_to_graph
    };

    #[test]
    fn declared_python_test_should_validate_rust_provider_with_bilateral_evidence() {
        let test = declared_test_case(
            RepoId::new("repo:tests"),
            &IntegrationTestConfig {
                name: "test_create_order".to_owned(),
                path: "tests/test_orders.py".to_owned(),
                framework: "pytest".to_owned(),
                language: "python".to_owned(),
                validates: HttpContractConfig {
                    method: "POST".to_owned(),
                    path: "/api/orders".to_owned(),
                },
            },
            Some("content:test".to_owned()),
        );
        let providers = extract_openapi(
            &RepoId::new("repo:rust-api"),
            "openapi.yaml",
            "openapi: 3.1.0\npaths:\n  /api/orders:\n    post: {}\n",
        );
        let result = providers
            .map_err(|error| error.to_string())
            .and_then(|providers| {
                link_declared_tests(&[test], &providers).map_err(|error| error.to_string())
            });

        assert!(matches!(
            result,
            Ok(edges)
                if edges.len() == 1
                    && edges[0].kind == EdgeKind::Validates
                    && edges[0].evidence.len() == 2
        ));
    }

    #[test]
    fn executable_route_should_outweigh_divergent_utoipa_advisory() {
        let repo = RepoId::new("repo:rust-api");
        let observations = parse_rust_source(
            r#"
use actix_web::get;

#[utoipa::path(get, path = "/documented")]
#[get("/runtime")]
async fn handler() {}
"#,
        );
        let facts =
            source_observations_to_graph(&repo, "src/routes.rs", "content:routes", &observations);
        let edges = link_declared_implementations(&facts.implementations, &facts.boundaries)
            .expect("both exact declarations should remain linkable");
        let mut consensus = edges
            .iter()
            .filter_map(|edge| {
                let path = facts
                    .boundaries
                    .iter()
                    .find(|boundary| boundary.node.id == edge.source)?
                    .path
                    .clone();
                Some((path, edge.confidence, edge.status))
            })
            .collect::<Vec<_>>();
        consensus.sort_by(|left, right| left.0.cmp(&right.0));

        assert_eq!(
            consensus,
            vec![
                ("/documented".to_owned(), 0.75, EpistemicStatus::Inferred),
                ("/runtime".to_owned(), 1.0, EpistemicStatus::Confirmed),
            ]
        );
    }

    #[test]
    fn executable_route_should_merge_matching_utoipa_advisory() {
        let repo = RepoId::new("repo:rust-api");
        let observations = parse_rust_source(
            r#"
use actix_web::get;

#[utoipa::path(get, path = "/payments")]
#[get("/payments")]
async fn handler() {}
"#,
        );
        let facts =
            source_observations_to_graph(&repo, "src/routes.rs", "content:routes", &observations);
        let edges = link_declared_implementations(&facts.implementations, &facts.boundaries)
            .expect("matching declarations should merge into one implementation edge");

        assert!(matches!(
            edges.as_slice(),
            [edge]
                if edge.kind == EdgeKind::ImplementedBy
                    && (edge.confidence - 1.0).abs() < f32::EPSILON
                    && edge.status == EpistemicStatus::Confirmed
                    && edge.evidence.len() == 3
        ));
    }
}