code-system-graph-core 1.2.1

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
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
//! Canonical HTTP route shapes and the single provider index shared by every HTTP link kind.
//!
//! Providers are indexed by method and canonical shape. Consumers, tests, and implementation
//! anchors resolve through the same index, so a concrete request path such as `/orders/42` reaches
//! the `/orders/{id}` template regardless of the framework syntax that declared it.

use std::collections::BTreeMap;

use code_system_graph_model::{
    Edge, EdgeId, EdgeKind, EpistemicStatus, Evidence, EvidenceId, HttpLinkGap, HttpLinkGapReason, HttpLinkReport, Node, NodeId, RepoId, stable_id
};

use crate::linker::{http_link_ambiguity, sort_http_ambiguities};
use crate::{
    BoundaryRole, DeclaredImplementation, DeclaredTestCase, HttpBoundary, HttpLinkAmbiguity, HttpLinkResolution, InfrastructureDocument, InfrastructureResourceKind
};

/// One segment of a canonical route template.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum RouteSegment {
    /// Literal path segment.
    Static(String),
    /// Single-segment parameter such as `{id}`, `:id`, `<int:id>`, `{id:int}`, or `[id]`.
    Param,
    /// Literal pieces separated by single-segment parameters, such as `{name}.json`.
    /// The first and last pieces may be empty; parameter names are discarded.
    Mixed(Vec<String>),
    /// Parameter that consumes one or more remaining segments, such as `{*rest}`, `{path...}`,
    /// `<path:rest>`, `[...slug]`, or `*filepath`.
    CatchAll,
}

/// Framework-independent route template. Parameter names are not part of the shape.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RouteShape {
    segments: Vec<RouteSegment>,
}

impl RouteShape {
    /// Parses a normalized path or route template. Query strings and fragments are ignored.
    #[must_use]
    pub fn parse(path: &str) -> Self {
        let path = path.split(['?', '#']).next().unwrap_or_default();
        let mut segments = Vec::new();
        for segment in path.split('/').filter(|segment| !segment.is_empty()) {
            let classified = classify_segment(segment);
            let terminal = classified == RouteSegment::CatchAll;
            segments.push(classified);
            if terminal {
                break;
            }
        }
        Self { segments }
    }

    /// Returns the canonical segments in path order.
    #[must_use]
    pub fn segments(&self) -> &[RouteSegment] {
        &self.segments
    }

    /// Returns `true` when the shape has no parameter segment.
    #[must_use]
    pub fn is_concrete(&self) -> bool {
        self.segments
            .iter()
            .all(|segment| matches!(segment, RouteSegment::Static(_)))
    }

    /// Renders the identity form: parameters become `{}` and catch-alls become `{*}`.
    #[must_use]
    pub fn canonical(&self) -> String {
        if self.segments.is_empty() {
            return "/".to_owned();
        }
        let mut rendered = String::new();
        for segment in &self.segments {
            rendered.push('/');
            match segment {
                RouteSegment::Static(value) => rendered.push_str(value),
                RouteSegment::Param => rendered.push_str("{}"),
                RouteSegment::Mixed(literals) => rendered.push_str(&literals.join("{}")),
                RouteSegment::CatchAll => rendered.push_str("{*}"),
            }
        }
        rendered
    }
}

/// Returns the canonical identity form of a path or route template.
#[must_use]
pub fn canonical_route(path: &str) -> String {
    RouteShape::parse(path).canonical()
}

fn classify_segment(segment: &str) -> RouteSegment {
    if segment == "*" || segment == "**" {
        return RouteSegment::CatchAll;
    }
    if let Some(inner) = segment
        .strip_prefix('{')
        .and_then(|value| value.strip_suffix('}'))
        .filter(|inner| !inner.contains(['{', '}']))
    {
        return if inner.starts_with('*')
            || inner.ends_with("...")
            || inner.ends_with(":path")
            || inner.ends_with(":.*")
            || inner.ends_with(":.+")
        {
            RouteSegment::CatchAll
        } else {
            RouteSegment::Param
        };
    }
    if (segment.starts_with("[...") && segment.ends_with(']'))
        || (segment.starts_with("[[...") && segment.ends_with("]]"))
    {
        return RouteSegment::CatchAll;
    }
    if segment.len() > 2 && segment.starts_with('[') && segment.ends_with(']') {
        return RouteSegment::Param;
    }
    if let Some(inner) = segment
        .strip_prefix('<')
        .and_then(|value| value.strip_suffix('>'))
    {
        return if inner.starts_with("path:") {
            RouteSegment::CatchAll
        } else {
            RouteSegment::Param
        };
    }
    if let Some(name) = segment.strip_prefix(':').filter(|name| !name.is_empty()) {
        return if name.ends_with('*') || name.ends_with('+') {
            RouteSegment::CatchAll
        } else {
            RouteSegment::Param
        };
    }
    if segment.len() > 1 && segment.starts_with('*') {
        return RouteSegment::CatchAll;
    }
    if let Some(literals) = mixed_segment_literals(segment) {
        if literals.len() == 2 && literals.iter().all(String::is_empty) {
            return RouteSegment::Param;
        }
        return RouteSegment::Mixed(literals);
    }
    RouteSegment::Static(segment.to_owned())
}

fn mixed_segment_literals(segment: &str) -> Option<Vec<String>> {
    let mut remaining = segment;
    let mut literals = Vec::new();
    while let Some((prefix, parameter)) = remaining.split_once('{') {
        let (name, suffix) = parameter.split_once('}')?;
        if name.contains('{') || prefix.contains('}') {
            return None;
        }
        // JavaScript interpolation uses `${name}` for the same path parameter.
        literals.push(prefix.strip_suffix('$').unwrap_or(prefix).to_owned());
        remaining = suffix;
    }
    if literals.is_empty() || remaining.contains('}') {
        return None;
    }
    literals.push(remaining.to_owned());
    Some(literals)
}

fn mixed_segment_matches(literals: &[String], value: &str) -> bool {
    let [prefix, middle @ .., suffix] = literals else {
        return false;
    };
    let Some(remaining) = value.strip_prefix(prefix.as_str()) else {
        return false;
    };
    let Some(mut remaining) = remaining.strip_suffix(suffix.as_str()) else {
        return false;
    };
    for literal in middle {
        // Each parameter consumes at least one character, including adjacent parameters.
        let Some(first) = remaining.chars().next() else {
            return false;
        };
        remaining = &remaining[first.len_utf8()..];
        let Some((_, rest)) = remaining.split_once(literal.as_str()) else {
            return false;
        };
        remaining = rest;
    }
    !remaining.is_empty()
}

/// Returns the normalized authority of an absolute `http`/`https` URL.
pub(crate) fn url_authority(url: &str) -> Option<String> {
    let rest = url
        .strip_prefix("http://")
        .or_else(|| url.strip_prefix("https://"))?;
    let authority = rest.split(['/', '?', '#']).next().unwrap_or_default();
    normalize_authority(authority)
}

/// Normalizes a `host[:port]` authority: lower-case, without user information or a trailing
/// root dot. Returns `None` for an empty host or a value containing a scheme, path, or whitespace.
#[must_use]
pub fn normalize_authority(value: &str) -> Option<String> {
    let value = value.trim();
    if value.is_empty() || value.contains(['/', '?', '#', ' ', '\t']) || value.contains("://") {
        return None;
    }
    let host_and_port = value.rsplit_once('@').map_or(value, |(_, host)| host);
    let (host, port) = split_port(host_and_port);
    let host = host.trim_end_matches('.').to_ascii_lowercase();
    if host.is_empty() || port.is_some_and(|port| port.parse::<u16>().is_err()) {
        return None;
    }
    Some(match port {
        Some(port) if host.contains(':') => format!("[{host}]:{port}"),
        Some(port) => format!("{host}:{port}"),
        None => host,
    })
}

fn split_port(authority: &str) -> (&str, Option<&str>) {
    if let Some(rest) = authority.strip_prefix('[') {
        return match rest.split_once(']') {
            Some((host, tail)) => (host, tail.strip_prefix(':')),
            None => (authority, None),
        };
    }
    match authority.rsplit_once(':') {
        Some((host, port)) if !host.contains(':') => (host, Some(port)),
        _ => (authority, None),
    }
}

fn authority_host(authority: &str) -> &str {
    split_port(authority).0
}

/// Returns `true` for loopback and wildcard hosts used by locally started services and tests.
fn is_loopback(host: &str) -> bool {
    matches!(
        host,
        "localhost" | "::1" | "0.0.0.0" | "host.docker.internal"
    ) || host.ends_with(".localhost")
        || host.starts_with("127.")
}

/// Reduces cluster-internal service DNS names such as `orders.shop.svc.cluster.local` to the
/// service name.
fn service_host(host: &str) -> &str {
    let trimmed = host
        .strip_suffix(".svc.cluster.local")
        .or_else(|| host.strip_suffix(".svc"));
    trimmed.map_or(host, |name| name.split('.').next().unwrap_or(name))
}

/// Repository that serves each known authority.
///
/// Declared authorities come from the manifest and strictly restrict resolution to their
/// repository. Inferred authorities come from deployment declarations; when the inferred repository
/// has no matching route, resolution falls back to workspace rules. A host inferred for several
/// repositories is not used.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct AuthorityMap {
    declared: BTreeMap<String, RepoId>,
    inferred: BTreeMap<String, Option<RepoId>>,
}

/// Resolution target of one call authority.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum AuthorityTarget<'a> {
    Workspace,
    Declared(&'a RepoId),
    Inferred(&'a RepoId),
    External,
}

impl AuthorityMap {
    /// Creates an empty map.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Declares the repository serving a normalized `host` or `host:port` authority.
    pub fn declare(&mut self, authority: String, repository: RepoId) {
        self.declared.insert(authority, repository);
    }

    /// Records a host inferred from a deployment declaration in `repository`.
    pub fn infer(&mut self, host: &str, repository: &RepoId) {
        let Some(host) = normalize_authority(host) else {
            return;
        };
        let host = service_host(authority_host(&host)).to_owned();
        self.inferred
            .entry(host)
            .and_modify(|existing| {
                if existing.as_ref() != Some(repository) {
                    *existing = None;
                }
            })
            .or_insert_with(|| Some(repository.clone()));
    }

    /// Records every service, deployment, and host alias declared by an infrastructure document.
    pub fn infer_from_infrastructure(
        &mut self,
        repository: &RepoId,
        document: &InfrastructureDocument,
    ) {
        for unit in &document.deployment_units {
            for host in std::iter::once(&unit.name)
                .chain(&unit.service_names)
                .chain(&unit.host_aliases)
            {
                self.infer(host, repository);
            }
        }
        for resource in document
            .resources
            .iter()
            .filter(|resource| resource.kind == InfrastructureResourceKind::Service)
        {
            self.infer(&resource.name, repository);
        }
    }

    fn lookup(&self, authority: &str) -> AuthorityTarget<'_> {
        let host = authority_host(authority);
        if is_loopback(host) {
            return AuthorityTarget::Workspace;
        }
        if let Some(repository) = self
            .declared
            .get(authority)
            .or_else(|| self.declared.get(host))
        {
            return AuthorityTarget::Declared(repository);
        }
        match self.inferred.get(service_host(host)) {
            Some(Some(repository)) => AuthorityTarget::Inferred(repository),
            Some(None) => AuthorityTarget::Workspace,
            None => AuthorityTarget::External,
        }
    }
}

/// How a caller constrains which repositories may provide the operation it invokes.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub enum CallScope {
    /// Any repository may provide the operation.
    #[default]
    Workspace,
    /// The call runs in-process against the application in its own repository, such as a
    /// `TestClient(app)`, `httptest`, or `MockMvc` request.
    InProcess,
    /// The call targets a normalized `host:port` authority.
    Authority(String),
    /// The call targets a host outside the workspace and is never linked.
    External,
}

/// Providers sharing one canonical shape, de-duplicated by node identity.
#[derive(Debug, Default)]
struct ProviderSet<'a> {
    providers: Vec<&'a HttpBoundary>,
}

impl<'a> ProviderSet<'a> {
    fn insert(&mut self, provider: &'a HttpBoundary) {
        if let Some(existing) = self
            .providers
            .iter_mut()
            .find(|candidate| candidate.node.id == provider.node.id)
        {
            if provider.evidence.confidence > existing.evidence.confidence {
                *existing = provider;
            }
        } else {
            self.providers.push(provider);
        }
    }

    fn accepted(&self, accept: &impl Fn(&HttpBoundary) -> bool) -> Vec<&'a HttpBoundary> {
        self.providers
            .iter()
            .copied()
            .filter(|provider| accept(provider))
            .collect()
    }
}

#[derive(Debug, Default)]
struct RouteNode<'a> {
    statics: BTreeMap<String, RouteNode<'a>>,
    mixed: BTreeMap<Vec<String>, RouteNode<'a>>,
    param: Option<Box<RouteNode<'a>>>,
    catch_all: ProviderSet<'a>,
    terminal: ProviderSet<'a>,
}

impl<'a> RouteNode<'a> {
    fn insert(&mut self, segments: &[RouteSegment], provider: &'a HttpBoundary) {
        let Some((first, rest)) = segments.split_first() else {
            self.terminal.insert(provider);
            return;
        };
        match first {
            RouteSegment::Static(value) => self
                .statics
                .entry(value.clone())
                .or_default()
                .insert(rest, provider),
            RouteSegment::Param => self
                .param
                .get_or_insert_with(Box::default)
                .insert(rest, provider),
            RouteSegment::Mixed(literals) => self
                .mixed
                .entry(literals.clone())
                .or_default()
                .insert(rest, provider),
            RouteSegment::CatchAll => self.catch_all.insert(provider),
        }
    }

    /// Finds the most specific accepted providers: at every segment a static match is preferred
    /// over a mixed segment, then a whole-segment parameter, then a catch-all.
    fn find(
        &self,
        query: &[RouteSegment],
        accept: &impl Fn(&HttpBoundary) -> bool,
    ) -> Vec<&'a HttpBoundary> {
        let Some((first, rest)) = query.split_first() else {
            return self.terminal.accepted(accept);
        };
        if let RouteSegment::Static(value) = first
            && let Some(child) = self.statics.get(value)
        {
            let found = child.find(rest, accept);
            if !found.is_empty() {
                return found;
            }
        }
        let mut mixed_matches = Vec::new();
        for (literals, child) in &self.mixed {
            let matches = match first {
                RouteSegment::Static(value) => mixed_segment_matches(literals, value),
                RouteSegment::Mixed(query_literals) => literals == query_literals,
                RouteSegment::Param | RouteSegment::CatchAll => false,
            };
            if matches {
                mixed_matches.extend(child.find(rest, accept));
            }
        }
        if !mixed_matches.is_empty() {
            return mixed_matches;
        }
        if !matches!(first, RouteSegment::CatchAll)
            && let Some(child) = &self.param
        {
            let found = child.find(rest, accept);
            if !found.is_empty() {
                return found;
            }
        }
        self.catch_all.accepted(accept)
    }
}

/// Outcome of resolving one HTTP call against the provider index.
#[derive(Debug, PartialEq)]
enum RouteResolution<'a> {
    Unique(&'a HttpBoundary),
    Ambiguous(Vec<&'a HttpBoundary>),
    Unmatched,
}

/// Per-method tries of provider shapes.
#[derive(Debug, Default)]
struct RouteIndex<'a> {
    methods: BTreeMap<&'a str, RouteNode<'a>>,
}

impl<'a> RouteIndex<'a> {
    fn new(boundaries: &'a [HttpBoundary]) -> Self {
        let mut index = Self::default();
        for provider in boundaries
            .iter()
            .filter(|boundary| boundary.role == BoundaryRole::Provider)
        {
            index
                .methods
                .entry(provider.method.as_str())
                .or_default()
                .insert(RouteShape::parse(&provider.path).segments(), provider);
        }
        index
    }

    /// Applies the provider scope rules in order: an explicit repository restriction (mapped
    /// authority or in-process client), then the caller's own repository, then a unique provider
    /// in the workspace. Anything else is ambiguous.
    fn resolve(
        &self,
        method: &str,
        path: &str,
        caller_repository: Option<&RepoId>,
        restriction: Option<&RepoId>,
    ) -> RouteResolution<'a> {
        let Some(root) = self.methods.get(method) else {
            return RouteResolution::Unmatched;
        };
        let shape = RouteShape::parse(path);
        let mut found = match restriction {
            Some(repository) => root.find(shape.segments(), &|provider: &HttpBoundary| {
                provider.node.repo_id.as_ref() == Some(repository)
            }),
            None => root.find(shape.segments(), &|_: &HttpBoundary| true),
        };
        if found.len() > 1
            && let Some(caller) = caller_repository
        {
            let local = found
                .iter()
                .copied()
                .filter(|provider| provider.node.repo_id.as_ref() == Some(caller))
                .collect::<Vec<_>>();
            if local.len() == 1 {
                found = local;
            }
        }
        match found.as_slice() {
            [] => RouteResolution::Unmatched,
            [provider] => RouteResolution::Unique(provider),
            _ => RouteResolution::Ambiguous(found),
        }
    }
}

/// Repository restriction implied by a call scope.
#[derive(Debug, Clone, Copy)]
enum Restriction<'a> {
    /// Resolve across the workspace.
    Workspace,
    /// Resolve only inside one repository.
    Strict(&'a RepoId),
    /// Prefer one repository and fall back to the workspace when it has no match.
    Preferred(&'a RepoId),
    /// The call leaves the workspace and is never linked.
    External,
}

fn scope_restriction<'a>(
    scope: &'a CallScope,
    caller_repository: Option<&'a RepoId>,
    authorities: &'a AuthorityMap,
) -> Restriction<'a> {
    match scope {
        CallScope::Workspace => Restriction::Workspace,
        CallScope::InProcess => {
            caller_repository.map_or(Restriction::Workspace, Restriction::Strict)
        }
        CallScope::Authority(authority) => match authorities.lookup(authority) {
            AuthorityTarget::Workspace => Restriction::Workspace,
            AuthorityTarget::Declared(repository) => Restriction::Strict(repository),
            AuthorityTarget::Inferred(repository) => Restriction::Preferred(repository),
            AuthorityTarget::External => Restriction::External,
        },
        CallScope::External => Restriction::External,
    }
}

/// Outcome of resolving one scoped call.
enum CallResolution<'a> {
    Route(RouteResolution<'a>),
    External,
}

fn resolve_call<'a>(
    index: &RouteIndex<'a>,
    method: &str,
    path: &str,
    caller_repository: Option<&'a RepoId>,
    scope: &'a CallScope,
    authorities: &'a AuthorityMap,
) -> CallResolution<'a> {
    let resolution = match scope_restriction(scope, caller_repository, authorities) {
        Restriction::Workspace => index.resolve(method, path, caller_repository, None),
        Restriction::Strict(repository) => {
            index.resolve(method, path, caller_repository, Some(repository))
        }
        Restriction::Preferred(repository) => {
            match index.resolve(method, path, caller_repository, Some(repository)) {
                RouteResolution::Unmatched => index.resolve(method, path, caller_repository, None),
                resolution => resolution,
            }
        }
        Restriction::External => return CallResolution::External,
    };
    CallResolution::Route(resolution)
}

/// Links HTTP consumers (`calls_remote`), tests (`validates`), and implementation anchors
/// (`implemented_by`) to providers through one route index.
///
/// Concrete paths match provider templates; the most specific template wins and equal-shape
/// providers are narrowed by scope. Remaining ties are reported as ambiguities with their
/// candidates instead of edges. `authorities` maps the hosts named by absolute URLs to the
/// repository that serves them; calls to unknown hosts leave the workspace and are reported as
/// external rather than unmatched.
#[must_use]
pub fn link_http_routes(
    boundaries: &[HttpBoundary],
    tests: &[DeclaredTestCase],
    implementations: &[DeclaredImplementation],
    authorities: &AuthorityMap,
) -> HttpLinkResolution {
    let index = RouteIndex::new(boundaries);
    let mut sink = LinkSink::default();

    let consumers = boundaries
        .iter()
        .filter(|boundary| boundary.role == BoundaryRole::Consumer)
        .map(|consumer| RemoteCall {
            node: &consumer.node,
            method: &consumer.method,
            path: &consumer.path,
            scope: &consumer.scope,
            evidence: &consumer.evidence,
            kind: EdgeKind::CallsRemote,
            relation: "calls_remote",
        });
    let test_calls = tests.iter().map(|test| RemoteCall {
        node: &test.node,
        method: &test.method,
        path: &test.path,
        scope: &test.scope,
        evidence: &test.evidence,
        kind: EdgeKind::Validates,
        relation: "validates",
    });
    for call in consumers.chain(test_calls) {
        link_remote_call(&mut sink, &index, authorities, &call);
    }

    for implementation in implementations {
        let Some(repository) = implementation.node.repo_id.as_ref() else {
            continue;
        };
        match index.resolve(
            &implementation.method,
            &implementation.path,
            Some(repository),
            Some(repository),
        ) {
            RouteResolution::Unique(provider) => sink.link(
                &provider.node.id,
                &implementation.node.id,
                EdgeKind::ImplementedBy,
                "implemented_by",
                provider
                    .evidence
                    .confidence
                    .min(implementation.evidence.confidence),
                vec![
                    provider.evidence.id.clone(),
                    implementation.evidence.id.clone(),
                ],
            ),
            RouteResolution::Ambiguous(candidates) => {
                sink.ambiguous(&implementation.method, &implementation.path, &candidates);
            }
            RouteResolution::Unmatched => {}
        }
    }

    sink.finish()
}

/// Consumer or test call resolved against provider routes.
struct RemoteCall<'a> {
    node: &'a Node,
    method: &'a str,
    path: &'a str,
    scope: &'a CallScope,
    evidence: &'a Evidence,
    kind: EdgeKind,
    relation: &'static str,
}

fn link_remote_call<'a>(
    sink: &mut LinkSink,
    index: &RouteIndex<'a>,
    authorities: &'a AuthorityMap,
    call: &RemoteCall<'a>,
) {
    let caller = call.node.repo_id.as_ref();
    match resolve_call(
        index,
        call.method,
        call.path,
        caller,
        call.scope,
        authorities,
    ) {
        CallResolution::Route(RouteResolution::Unique(provider)) => {
            sink.report.coverage.linked += 1;
            sink.link(
                &call.node.id,
                &provider.node.id,
                call.kind,
                call.relation,
                call.evidence.confidence.min(provider.evidence.confidence),
                vec![call.evidence.id.clone(), provider.evidence.id.clone()],
            );
        }
        CallResolution::Route(RouteResolution::Ambiguous(candidates)) => {
            sink.ambiguous(call.method, call.path, &candidates);
            sink.gap(call, HttpLinkGapReason::Ambiguous, &candidates);
        }
        CallResolution::Route(RouteResolution::Unmatched) => {
            sink.gap(call, HttpLinkGapReason::NoProvider, &[]);
        }
        CallResolution::External => sink.gap(call, HttpLinkGapReason::External, &[]),
    }
}

/// Accumulates de-duplicated route edges and unresolved ambiguities.
#[derive(Debug, Default)]
struct LinkSink {
    edges: BTreeMap<EdgeId, Edge>,
    ambiguities: Vec<HttpLinkAmbiguity>,
    report: HttpLinkReport,
}

impl LinkSink {
    fn link(
        &mut self,
        source: &NodeId,
        target: &NodeId,
        kind: EdgeKind,
        relation: &str,
        confidence: f32,
        evidence: Vec<EvidenceId>,
    ) {
        let edge = Edge {
            id: EdgeId::new(stable_id(
                "edge",
                &format!("{}:{relation}:{}", source.as_str(), target.as_str()),
            )),
            source: source.clone(),
            target: target.clone(),
            kind,
            confidence,
            status: consensus_status(confidence),
            evidence,
        };
        self.edges
            .entry(edge.id.clone())
            .and_modify(|existing| merge_equivalent_edge(existing, &edge))
            .or_insert(edge);
    }

    fn ambiguous(&mut self, method: &str, path: &str, candidates: &[&HttpBoundary]) {
        self.ambiguities.push(http_link_ambiguity(
            method,
            path,
            candidates.iter().map(|candidate| &candidate.node.id),
        ));
    }

    fn gap(
        &mut self,
        call: &RemoteCall<'_>,
        reason: HttpLinkGapReason,
        candidates: &[&HttpBoundary],
    ) {
        let coverage = &mut self.report.coverage;
        match reason {
            HttpLinkGapReason::NoProvider => coverage.no_provider += 1,
            HttpLinkGapReason::Ambiguous => coverage.ambiguous += 1,
            HttpLinkGapReason::External => coverage.external += 1,
        }
        let mut candidates = candidates
            .iter()
            .map(|candidate| candidate.node.id.clone())
            .collect::<Vec<_>>();
        candidates.sort();
        candidates.dedup();
        self.report.gaps.push(HttpLinkGap {
            caller: call.node.id.clone(),
            method: call.method.to_owned(),
            path: call.path.to_owned(),
            reason,
            candidates,
        });
    }

    fn finish(mut self) -> HttpLinkResolution {
        sort_http_ambiguities(&mut self.ambiguities);
        self.report.gaps.sort();
        self.report.gaps.dedup();
        HttpLinkResolution {
            edges: self.edges.into_values().collect(),
            ambiguities: self.ambiguities,
            report: self.report,
        }
    }
}

fn merge_equivalent_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, HttpLinkGapReason, RepoId};

    use super::{
        AuthorityMap, CallScope, RouteSegment, RouteShape, canonical_route, link_http_routes, normalize_authority, url_authority
    };
    use crate::{HttpBoundary, HttpConsumerConfig, extract_openapi};

    fn provider(repo: &str, method: &str, path: &str) -> HttpBoundary {
        let document = format!(
            "openapi: 3.0.3\npaths:\n  {path}:\n    {}: {{}}\n",
            method.to_ascii_lowercase()
        );
        match extract_openapi(&RepoId::new(repo), "openapi.yaml", &document) {
            Ok(mut boundaries) => boundaries.remove(0),
            Err(error) => panic!("provider fixture must be valid: {error}"),
        }
    }

    fn consumer(repo: &str, method: &str, path: &str) -> HttpBoundary {
        HttpBoundary::consumer(
            RepoId::new(repo),
            &HttpConsumerConfig {
                method: method.to_owned(),
                path: path.to_owned(),
                source: "src/client.ts".to_owned(),
            },
        )
    }

    fn linked_targets(boundaries: &[HttpBoundary]) -> Vec<String> {
        let resolution = link_http_routes(boundaries, &[], &[], &AuthorityMap::new());
        resolution
            .edges
            .iter()
            .filter(|edge| edge.kind == EdgeKind::CallsRemote)
            .filter_map(|edge| {
                boundaries
                    .iter()
                    .find(|boundary| boundary.node.id == edge.target)
                    .map(|boundary| boundary.path.clone())
            })
            .collect()
    }

    #[test]
    fn route_shape_should_normalize_every_framework_parameter_syntax() {
        let parameter = "/orders/{}";
        for template in [
            "/orders/{id}",
            "/orders/:id",
            "/orders/<int:id>",
            "/orders/<id>",
            "/orders/{id:int}",
            "/orders/{id:[0-9]+}",
            "/orders/[id]",
            "/orders/${orderId}",
        ] {
            assert_eq!(canonical_route(template), parameter, "{template}");
        }
        for template in [
            "/files/[...slug]",
            "/files/[[...slug]]",
            "/files/{*rest}",
            "/files/{path...}",
            "/files/{rest:path}",
            "/files/<path:rest>",
            "/files/*filepath",
            "/files/:rest*",
            "/files/*",
            "/files/**",
        ] {
            assert_eq!(canonical_route(template), "/files/{*}", "{template}");
        }
        assert_eq!(canonical_route("/"), "/");
        assert_eq!(canonical_route("/orders/42?expand=true"), "/orders/42");
        assert!(RouteShape::parse("/orders/42").is_concrete());
        assert_eq!(
            RouteShape::parse("/v1/{tenant}/orders").segments(),
            &[
                RouteSegment::Static("v1".to_owned()),
                RouteSegment::Param,
                RouteSegment::Static("orders".to_owned()),
            ]
        );
    }

    #[test]
    fn concrete_path_should_link_to_the_most_specific_template() {
        let boundaries = [
            provider("repo:api", "GET", "/orders/{id}"),
            provider("repo:api", "GET", "/orders/export"),
            provider("repo:files", "GET", "/{tenant}/export"),
            provider("repo:files", "GET", "/orders/{rest:path}"),
            consumer("repo:web", "GET", "/orders/42"),
            consumer("repo:web", "GET", "/orders/export"),
            consumer("repo:web", "GET", "/orders/42/lines/7"),
        ];

        let mut targets = linked_targets(&boundaries);
        targets.sort();

        assert_eq!(
            targets,
            vec![
                "/orders/export".to_owned(),
                "/orders/{id}".to_owned(),
                "/orders/{rest:path}".to_owned(),
            ]
        );
    }

    #[test]
    fn equal_shapes_should_prefer_the_callers_repository_then_report_ambiguity() {
        let local = [
            provider("repo:web", "POST", "/orders"),
            provider("repo:api", "POST", "/orders"),
            consumer("repo:web", "POST", "/orders"),
        ];
        assert_eq!(linked_targets(&local), vec!["/orders".to_owned()]);

        let remote = [
            provider("repo:api-a", "POST", "/orders/{id}"),
            provider("repo:api-b", "POST", "/orders/:orderId"),
            consumer("repo:web", "POST", "/orders/9"),
        ];
        let resolution = link_http_routes(&remote, &[], &[], &AuthorityMap::new());
        assert_eq!(resolution.edges, Vec::new());
        assert_eq!(resolution.ambiguities.len(), 1);
        assert_eq!(resolution.ambiguities[0].path, "/orders/9");
        assert_eq!(resolution.ambiguities[0].candidates.len(), 2);
    }

    #[test]
    fn scoped_calls_should_resolve_only_inside_their_restriction() {
        let mut in_process = consumer("repo:api-b", "GET", "/health");
        in_process.scope = CallScope::InProcess;
        let mut mapped = consumer("repo:web", "GET", "/health");
        mapped.scope = CallScope::Authority("orders-api:8080".to_owned());
        let mut external = consumer("repo:web", "GET", "/health");
        external.scope = CallScope::External;
        let boundaries = [
            provider("repo:api-a", "GET", "/health"),
            provider("repo:api-b", "GET", "/health"),
            in_process,
            mapped,
            external,
        ];
        let mut authorities = AuthorityMap::new();
        authorities.declare("orders-api:8080".to_owned(), RepoId::new("repo:api-a"));

        let resolution = link_http_routes(&boundaries, &[], &[], &authorities);
        let mut pairs = resolution
            .edges
            .iter()
            .filter_map(|edge| {
                let source = boundaries.iter().find(|item| item.node.id == edge.source)?;
                let target = boundaries.iter().find(|item| item.node.id == edge.target)?;
                Some((
                    source.node.repo_id.clone()?.as_str().to_owned(),
                    target.node.repo_id.clone()?.as_str().to_owned(),
                ))
            })
            .collect::<Vec<_>>();
        pairs.sort();

        assert_eq!(
            pairs,
            vec![
                ("repo:api-b".to_owned(), "repo:api-b".to_owned()),
                ("repo:web".to_owned(), "repo:api-a".to_owned()),
            ]
        );
        assert_eq!(resolution.ambiguities, Vec::new());
        assert!(
            resolution
                .report
                .gaps
                .iter()
                .any(|call| call.reason == HttpLinkGapReason::External)
        );
    }

    fn linked_repositories(boundaries: &[HttpBoundary], authorities: &AuthorityMap) -> Vec<String> {
        link_http_routes(boundaries, &[], &[], authorities)
            .edges
            .iter()
            .filter_map(|edge| {
                let target = boundaries.iter().find(|item| item.node.id == edge.target)?;
                Some(target.node.repo_id.clone()?.as_str().to_owned())
            })
            .collect()
    }

    fn authority_consumer(authority: &str) -> HttpBoundary {
        let mut call = consumer("repo:web", "GET", "/orders/42");
        call.scope = CallScope::Authority(authority.to_owned());
        call
    }

    #[test]
    fn authorities_should_resolve_loopback_declared_inferred_and_external_hosts() {
        let providers = [
            provider("repo:api-a", "GET", "/orders/{id}"),
            provider("repo:api-b", "GET", "/orders/{id}"),
        ];
        let mut authorities = AuthorityMap::new();
        authorities.declare("orders-api".to_owned(), RepoId::new("repo:api-a"));
        authorities.infer("billing", &RepoId::new("repo:api-b"));
        authorities.infer("shared", &RepoId::new("repo:api-a"));
        authorities.infer("shared", &RepoId::new("repo:api-b"));
        let resolve = |authority: &str| {
            let mut boundaries = providers.to_vec();
            boundaries.push(authority_consumer(authority));
            linked_repositories(&boundaries, &authorities)
        };

        assert_eq!(resolve("orders-api:8080"), vec!["repo:api-a".to_owned()]);
        assert_eq!(
            resolve("billing.payments.svc.cluster.local"),
            vec!["repo:api-b".to_owned()]
        );
        assert_eq!(resolve("localhost:8080"), Vec::<String>::new());
        assert_eq!(resolve("shared:80"), Vec::<String>::new());
        assert_eq!(resolve("api.example.com"), Vec::<String>::new());

        let mut single = vec![provider("repo:api-a", "GET", "/orders/{id}")];
        single.push(authority_consumer("127.0.0.1:3000"));
        assert_eq!(
            linked_repositories(&single, &authorities),
            vec!["repo:api-a".to_owned()]
        );
    }

    #[test]
    fn inferred_authority_should_fall_back_to_the_workspace_when_its_repository_has_no_route() {
        let mut boundaries = vec![provider("repo:api-a", "GET", "/orders/{id}")];
        boundaries.push(authority_consumer("gateway:8080"));
        let mut authorities = AuthorityMap::new();
        authorities.infer("gateway", &RepoId::new("repo:edge"));
        assert_eq!(
            linked_repositories(&boundaries, &authorities),
            vec!["repo:api-a".to_owned()]
        );

        let mut strict = AuthorityMap::new();
        strict.declare("gateway".to_owned(), RepoId::new("repo:edge"));
        assert_eq!(
            linked_repositories(&boundaries, &strict),
            Vec::<String>::new()
        );
    }

    #[test]
    fn authority_normalization_should_reject_schemes_and_paths() {
        assert_eq!(
            url_authority("https://User@Orders-API.:8080/v1?x=1"),
            Some("orders-api:8080".to_owned())
        );
        assert_eq!(
            url_authority("http://[::1]:9000/x"),
            Some("[::1]:9000".to_owned())
        );
        assert_eq!(normalize_authority("http://orders"), None);
        assert_eq!(normalize_authority("orders/v1"), None);
        assert_eq!(normalize_authority("orders:http"), None);
        assert_eq!(normalize_authority("Orders"), Some("orders".to_owned()));
    }

    #[test]
    fn provider_identity_should_ignore_parameter_names() {
        let openapi = provider("repo:api", "GET", "/orders/{orderId}");
        let express = provider("repo:api", "GET", "/orders/:id");

        assert_eq!(openapi.node.id, express.node.id);
    }

    #[test]
    fn mixed_route_segments_should_preserve_literals_in_their_identity() {
        for (template, canonical) in [
            ("/files/{name}.json", "/files/{}.json"),
            ("/files/report-{name}", "/files/report-{}"),
            ("/files/{name}.{format}", "/files/{}.{}"),
            ("/files/${name}.json", "/files/{}.json"),
        ] {
            assert_eq!(canonical_route(template), canonical, "{template}");
            assert!(!RouteShape::parse(template).is_concrete(), "{template}");
        }
        let json = provider("repo:api", "GET", "/files/{name}.json");
        let renamed = provider("repo:api", "GET", "/files/{id}.json");
        let plain = provider("repo:api", "GET", "/files/{id}");
        let xml = provider("repo:api", "GET", "/files/{id}.xml");
        assert_eq!(json.node.id, renamed.node.id);
        assert_ne!(json.node.id, plain.node.id);
        assert_ne!(json.node.id, xml.node.id);
    }

    #[test]
    fn mixed_route_segments_should_link_only_calls_satisfying_every_literal() {
        for (template, matching, mismatching) in [
            (
                "/files/{name}.json",
                "/files/readme.json",
                "/files/readme.xml",
            ),
            (
                "/files/report-{name}",
                "/files/report-readme",
                "/files/other-readme",
            ),
            (
                "/files/{name}.{format}",
                "/files/readme.json",
                "/files/readme",
            ),
            (
                "/files/${name}.json",
                "/files/readme.json",
                "/files/readme.xml",
            ),
            ("/files/{a}{b}.json", "/files/ab.json", "/files/a.json"),
        ] {
            let boundaries = [
                provider("repo:api", "GET", template),
                consumer("repo:web", "GET", matching),
                consumer("repo:web", "GET", mismatching),
            ];
            assert_eq!(
                linked_targets(&boundaries),
                vec![template.to_owned()],
                "{template}"
            );
        }
    }

    #[test]
    fn mixed_route_segments_should_rank_between_static_and_whole_segment_parameters() {
        for (call, target) in [
            ("/files/readme.json", "/files/readme.json"),
            ("/files/other.json", "/files/{name}.json"),
            ("/files/readme.xml", "/files/{id}"),
            ("/files/{id}.json", "/files/{name}.json"),
        ] {
            let boundaries = [
                provider("repo:api", "GET", "/files/{id}"),
                provider("repo:api", "GET", "/files/{name}.json"),
                provider("repo:api", "GET", "/files/readme.json"),
                consumer("repo:web", "GET", call),
            ];
            assert_eq!(
                linked_targets(&boundaries),
                vec![target.to_owned()],
                "{call}"
            );
        }
    }

    #[test]
    fn overlapping_mixed_route_segments_should_report_ambiguity() {
        let boundaries = [
            provider("repo:api", "GET", "/files/{name}.json"),
            provider("repo:api", "GET", "/files/report-{name}"),
            consumer("repo:web", "GET", "/files/report-readme.json"),
        ];
        let resolution = link_http_routes(&boundaries, &[], &[], &AuthorityMap::new());
        assert_eq!(resolution.edges, Vec::new());
        assert_eq!(resolution.ambiguities.len(), 1);
        assert_eq!(resolution.ambiguities[0].candidates.len(), 2);
    }

    #[test]
    fn mixed_route_parameters_should_consume_nonempty_unicode_values() {
        for (call, expected_links) in [
            ("/files/éñ.json", 1),
            ("/files/é.json", 0),
            ("/files/.json", 0),
        ] {
            let boundaries = [
                provider("repo:api", "GET", "/files/{a}{b}.json"),
                consumer("repo:web", "GET", call),
            ];
            assert_eq!(linked_targets(&boundaries).len(), expected_links, "{call}");
        }
    }
}