compose-lens 0.2.0

Loss-aware parsing, processing, validation, and rendering of Compose projects
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
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
//! Ordered, caller-supplied Compose project loading.

mod include_paths;

use crate::diagnostic::{Diagnostic, DiagnosticCode, DiagnosticLabel, Severity};
use crate::interpolation::{
    DocumentInterpolation, EnvironmentProvider, InterpolationOptions, interpolate_document_with_options,
};
use crate::merge::{MergedProject, merge_project};
use crate::model::{
    ComposeDocument, ConfigDefinition, IncludeItem, Located, ModelDefinition, ModelParse, NetworkDefinition,
    SecretDefinition, VolumeDefinition,
};
use crate::project::{ProjectResource, ProjectService, ProjectView, build_project_view};
use crate::source::{SourceId, SourceSpan};
use crate::syntax::{SyntaxDocument, SyntaxParseError};
use std::collections::BTreeMap;
use std::error::Error;
use std::fmt;
use std::path::{Path, PathBuf};
use std::sync::Arc;

pub use include_paths::{
    INCLUDE_PROJECT_DIRECTORY_UNRESOLVED, IncludeProjectDirectoryEntry, IncludeProjectDirectoryPlan,
    IncludeProjectDirectoryRequest, IncludeProjectDirectoryResolution, IncludeProjectDirectoryResolveError,
    IncludeProjectDirectoryResolver, IncludeProjectDirectoryStatus,
};

/// An effective include declaration could not be modeled safely enough to authorize loading.
pub const INCLUDE_UNMODELED: DiagnosticCode = DiagnosticCode::new("compose.include.unmodeled");
/// The caller denied an include request.
pub const INCLUDE_LOADER_DENIED: DiagnosticCode = DiagnosticCode::new("compose.include.loader-denied");
/// The caller's include loader failed to supply a project.
pub const INCLUDE_LOADER_FAILED: DiagnosticCode = DiagnosticCode::new("compose.include.loader-failed");
/// The caller supplied no documents for an included project.
pub const INCLUDE_EMPTY_RESULT: DiagnosticCode = DiagnosticCode::new("compose.include.empty-result");
/// An included identity appears again on the active traversal stack.
pub const INCLUDE_CYCLE: DiagnosticCode = DiagnosticCode::new("compose.include.cycle");
/// A source identifier was reused anywhere in an include traversal.
pub const INCLUDE_DUPLICATE_SOURCE_ID: DiagnosticCode = DiagnosticCode::new("compose.include.duplicate-source-id");
/// A caller-supplied include project could not enter the existing ordered loader.
pub const INCLUDE_PROJECT_LOAD_FAILED: DiagnosticCode = DiagnosticCode::new("compose.include.project-load-failed");
/// The root input contains no documents.
pub const INCLUDE_EMPTY_ROOT: DiagnosticCode = DiagnosticCode::new("compose.include.empty-root");
/// An included definition collides with an already selected parent definition.
pub const INCLUDE_RESOURCE_CONFLICT: DiagnosticCode = DiagnosticCode::new("compose.include.resource-conflict");

/// The caller-defined location of one Compose document.
///
/// The label is for display and may be a path, URI, or synthetic name. The document directory is
/// retained verbatim for later path-resolution decisions; `ComposeLens` does not canonicalize it or
/// access the file system.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DocumentOrigin {
    label: String,
    directory: PathBuf,
}

impl DocumentOrigin {
    /// Creates an explicit document origin.
    #[must_use]
    pub fn new(label: impl Into<String>, directory: impl Into<PathBuf>) -> Self {
        Self {
            label: label.into(),
            directory: directory.into(),
        }
    }

    /// Returns the caller-defined display label.
    #[must_use]
    pub fn label(&self) -> &str {
        &self.label
    }

    /// Returns the caller-supplied directory associated with this document.
    #[must_use]
    pub fn directory(&self) -> &Path {
        &self.directory
    }
}

/// One source document supplied to the project loader.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DocumentInput {
    source_id: SourceId,
    origin: DocumentOrigin,
    source: Arc<str>,
}

impl DocumentInput {
    /// Creates an input without reading the file system or process environment.
    #[must_use]
    pub fn new(source_id: SourceId, origin: DocumentOrigin, source: impl Into<Arc<str>>) -> Self {
        Self {
            source_id,
            origin,
            source: source.into(),
        }
    }

    /// Returns the caller-managed source identifier.
    #[must_use]
    pub const fn source_id(&self) -> SourceId {
        self.source_id
    }

    /// Returns the caller-defined document origin.
    #[must_use]
    pub const fn origin(&self) -> &DocumentOrigin {
        &self.origin
    }

    /// Returns the supplied source text.
    #[must_use]
    pub fn source_text(&self) -> &str {
        &self.source
    }
}

/// A caller-defined canonical identity for one includable Compose project.
///
/// The caller, not `ComposeLens`, establishes identity equivalence. In particular, this type does
/// not open, join, normalize, or canonicalize paths.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct IncludeIdentity(String);

impl IncludeIdentity {
    /// Creates an opaque identity whose canonical form is owned by the caller.
    #[must_use]
    pub fn new(canonical: impl Into<String>) -> Self {
        Self(canonical.into())
    }

    /// Returns the caller-defined canonical identity text.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for IncludeIdentity {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.0)
    }
}

/// A caller-created ordered project input returned by an [`IncludeLoader`].
///
/// This is equally suitable for the traversal root. `ComposeLens` keeps every origin and source
/// text supplied here, but it never discovers documents from the identity or include paths.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IncludedProjectInput {
    identity: IncludeIdentity,
    documents: Vec<DocumentInput>,
}

impl IncludedProjectInput {
    /// Creates one project input from caller-created documents in merge order.
    #[must_use]
    pub fn new(identity: IncludeIdentity, documents: impl IntoIterator<Item = DocumentInput>) -> Self {
        Self {
            identity,
            documents: documents.into_iter().collect(),
        }
    }

    /// Returns the caller-defined canonical project identity.
    #[must_use]
    pub const fn identity(&self) -> &IncludeIdentity {
        &self.identity
    }

    /// Returns documents in caller-selected merge order.
    #[must_use]
    pub fn documents(&self) -> &[DocumentInput] {
        &self.documents
    }
}

/// One effective, caller-authorized include request.
///
/// Every path-like value remains raw source data in declared order. The loader receives this
/// complete context and alone decides whether and how any document may be obtained.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IncludeRequest {
    parent_identity: IncludeIdentity,
    parent_base_directory: PathBuf,
    declaration_span: SourceSpan,
    declaration_origin: Option<DocumentOrigin>,
    item: IncludeItem,
    paths: Vec<Located<String>>,
    env_files: Vec<Located<String>>,
    project_directory: Option<Located<String>>,
}

impl IncludeRequest {
    fn from_item(
        parent_identity: IncludeIdentity,
        parent_base_directory: PathBuf,
        declaration_origin: Option<DocumentOrigin>,
        item: IncludeItem,
    ) -> Option<Self> {
        let declaration_span = include_item_span(&item)?;
        let (paths, env_files, project_directory) = match &item {
            IncludeItem::Short(path) => (vec![path.clone()], Vec::new(), None),
            IncludeItem::Long(include) if include.unmodeled_fields().is_empty() && !include.paths().is_empty() => (
                include.paths().to_vec(),
                include.env_files().to_vec(),
                include.project_directory().cloned(),
            ),
            IncludeItem::Long(_) | IncludeItem::Unmodeled => return None,
        };
        Some(Self {
            parent_identity,
            parent_base_directory,
            declaration_span,
            declaration_origin,
            item,
            paths,
            env_files,
            project_directory,
        })
    }

    /// Returns the identity of the project that declared this request.
    #[must_use]
    pub const fn parent_identity(&self) -> &IncludeIdentity {
        &self.parent_identity
    }

    /// Returns the parent project's first-document directory exactly as supplied by the caller.
    #[must_use]
    pub fn parent_base_directory(&self) -> &Path {
        &self.parent_base_directory
    }

    /// Returns the complete span of the effective declaration.
    #[must_use]
    pub const fn declaration_span(&self) -> SourceSpan {
        self.declaration_span
    }

    /// Returns the source identifier containing the effective declaration.
    #[must_use]
    pub const fn declaration_source_id(&self) -> SourceId {
        self.declaration_span.source_id()
    }

    /// Returns the origin of the document containing the effective declaration, when available.
    #[must_use]
    pub const fn declaration_origin(&self) -> Option<&DocumentOrigin> {
        self.declaration_origin.as_ref()
    }

    /// Returns the complete effective typed include item without interpolation.
    #[must_use]
    pub const fn item(&self) -> &IncludeItem {
        &self.item
    }

    /// Returns raw include paths in declared order.
    #[must_use]
    pub fn paths(&self) -> &[Located<String>] {
        &self.paths
    }

    /// Returns raw include environment-file declarations in declared order.
    #[must_use]
    pub fn env_files(&self) -> &[Located<String>] {
        &self.env_files
    }

    /// Returns the raw optional project-directory declaration.
    #[must_use]
    pub const fn project_directory(&self) -> Option<&Located<String>> {
        self.project_directory.as_ref()
    }
}

/// The only authorization and I/O boundary used by recursive include traversal.
pub trait IncludeLoader {
    /// Authorizes and loads one requested included project.
    ///
    /// Implementations may use files, editor buffers, archives, URIs, or another caller policy.
    /// `ComposeLens` itself performs none of those operations.
    ///
    /// # Errors
    ///
    /// Returns an [`IncludeLoadError`] when caller policy denies the request or the selected
    /// external source cannot supply a project input.
    fn load_include(&self, request: &IncludeRequest) -> Result<IncludedProjectInput, IncludeLoadError>;
}

/// A caller-controlled include loading outcome that prevents traversal of one requested edge.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum IncludeLoadError {
    /// The caller's policy denied the request.
    Denied(String),
    /// The caller could not load an authorized request.
    Failed(String),
}

impl IncludeLoadError {
    /// Creates a policy-denial result without exposing it through diagnostics by default.
    #[must_use]
    pub fn denied(message: impl Into<String>) -> Self {
        Self::Denied(message.into())
    }

    /// Creates a loader-failure result without exposing it through diagnostics by default.
    #[must_use]
    pub fn failed(message: impl Into<String>) -> Self {
        Self::Failed(message.into())
    }
}

impl fmt::Display for IncludeLoadError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Denied(message) | Self::Failed(message) => formatter.write_str(message),
        }
    }
}

impl Error for IncludeLoadError {}

/// One parsed document in an ordered Compose project.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LoadedDocument {
    origin: DocumentOrigin,
    syntax: SyntaxDocument,
    syntax_diagnostics: Vec<Diagnostic>,
    model: ModelParse,
}

impl LoadedDocument {
    /// Returns the document's source identifier.
    #[must_use]
    pub const fn source_id(&self) -> SourceId {
        self.syntax.source_id()
    }

    /// Returns the explicit origin retained for this document.
    #[must_use]
    pub const fn origin(&self) -> &DocumentOrigin {
        &self.origin
    }

    /// Returns the loss-aware syntax document.
    #[must_use]
    pub const fn syntax(&self) -> &SyntaxDocument {
        &self.syntax
    }

    /// Returns recoverable YAML syntax diagnostics.
    #[must_use]
    pub fn syntax_diagnostics(&self) -> &[Diagnostic] {
        &self.syntax_diagnostics
    }

    /// Returns the recoverable typed-model parse result.
    #[must_use]
    pub const fn model(&self) -> &ModelParse {
        &self.model
    }

    /// Reports whether syntax and typed-model parsing emitted no error diagnostics.
    #[must_use]
    pub fn is_valid(&self) -> bool {
        self.syntax_diagnostics
            .iter()
            .chain(self.model.diagnostics())
            .all(|diagnostic| diagnostic.severity() != Severity::Error)
    }
}

/// An ordered set of parsed Compose documents and their path origins.
///
/// File order is semantically significant. The first document supplies the project directory used
/// by Compose's multi-file relative-path rules, while every document retains its own origin for
/// provenance and future `include` support.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LoadedProject {
    documents: Vec<LoadedDocument>,
    base_directory: PathBuf,
    diagnostics: Vec<Diagnostic>,
}

impl LoadedProject {
    /// Parses ordered, caller-supplied documents into a loaded project.
    ///
    /// Recoverable YAML and typed-model problems remain available in [`Self::diagnostics`]. No
    /// interpolation or merge is performed by this operation.
    ///
    /// # Errors
    ///
    /// Returns [`ProjectLoadError`] when no document is supplied, a source identifier is reused, or
    /// one source exceeds the syntax tree's byte-offset capacity.
    pub fn load(inputs: impl IntoIterator<Item = DocumentInput>) -> Result<Self, ProjectLoadError> {
        let inputs: Vec<_> = inputs.into_iter().collect();
        let Some(first) = inputs.first() else {
            return Err(ProjectLoadError::EmptyProject);
        };

        let mut source_origins = BTreeMap::new();
        for input in &inputs {
            if let Some(first_origin) = source_origins.insert(input.source_id, input.origin.label.clone()) {
                return Err(ProjectLoadError::DuplicateSourceId {
                    source_id: input.source_id,
                    first_origin,
                    duplicate_origin: input.origin.label.clone(),
                });
            }
        }

        let base_directory = first.origin.directory.clone();
        let mut documents = Vec::with_capacity(inputs.len());
        let mut diagnostics = Vec::new();
        for input in inputs {
            let syntax = SyntaxDocument::parse(input.source_id, input.source).map_err(|error| {
                ProjectLoadError::SyntaxCapacity {
                    origin: input.origin.clone(),
                    error,
                }
            })?;
            let (syntax, syntax_diagnostics) = syntax.into_parts();
            let model = ComposeDocument::parse(&syntax);
            diagnostics.extend(syntax_diagnostics.iter().cloned());
            diagnostics.extend(model.diagnostics().iter().cloned());
            documents.push(LoadedDocument {
                origin: input.origin,
                syntax,
                syntax_diagnostics,
                model,
            });
        }

        Ok(Self {
            documents,
            base_directory,
            diagnostics,
        })
    }

    /// Returns documents in caller-supplied merge order.
    #[must_use]
    pub fn documents(&self) -> &[LoadedDocument] {
        &self.documents
    }

    /// Finds a document by its unique source identifier.
    #[must_use]
    pub fn document(&self, source_id: SourceId) -> Option<&LoadedDocument> {
        self.documents.iter().find(|document| document.source_id() == source_id)
    }

    /// Returns the project directory inherited from the first document.
    #[must_use]
    pub fn base_directory(&self) -> &Path {
        &self.base_directory
    }

    /// Returns aggregated syntax and typed-model diagnostics in document order.
    #[must_use]
    pub fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }

    /// Reports whether loading emitted no error diagnostics.
    #[must_use]
    pub fn is_valid(&self) -> bool {
        self.diagnostics
            .iter()
            .all(|diagnostic| diagnostic.severity() != Severity::Error)
    }

    /// Interpolates each document independently, in file order, without modifying the project.
    #[must_use]
    pub fn interpolate(&self, environment: &dyn EnvironmentProvider) -> ProjectInterpolation {
        self.interpolate_with_options(environment, InterpolationOptions::default())
    }

    /// Interpolates each document independently with explicit options.
    #[must_use]
    pub fn interpolate_with_options(
        &self,
        environment: &dyn EnvironmentProvider,
        options: InterpolationOptions,
    ) -> ProjectInterpolation {
        let documents: Vec<_> = self
            .documents
            .iter()
            .map(|document| interpolate_document_with_options(document.syntax(), environment, options))
            .collect();
        let diagnostics = documents
            .iter()
            .flat_map(|document| document.diagnostics().iter().cloned())
            .collect();
        ProjectInterpolation { documents, diagnostics }
    }
}

/// One visited project occurrence in an [`IncludeResolution`].
///
/// A repeated identity is retained as a separate occurrence when it is reached by distinct
/// non-cyclic edges. Traversal intentionally does not cache those diamonds.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IncludeNode {
    index: usize,
    identity: IncludeIdentity,
    inputs: IncludedProjectInput,
    origins: Vec<DocumentOrigin>,
    loaded_project: Option<LoadedProject>,
    merged_project: Option<MergedProject>,
    project_view: Option<ProjectView>,
}

impl IncludeNode {
    /// Returns this occurrence's stable index within [`IncludeResolution::nodes`].
    #[must_use]
    pub const fn index(&self) -> usize {
        self.index
    }

    /// Returns this occurrence's caller-defined identity.
    #[must_use]
    pub const fn identity(&self) -> &IncludeIdentity {
        &self.identity
    }

    /// Returns the complete caller-created input retained for this occurrence.
    #[must_use]
    pub const fn inputs(&self) -> &IncludedProjectInput {
        &self.inputs
    }

    /// Returns source origins in the input's merge order.
    #[must_use]
    pub fn origins(&self) -> &[DocumentOrigin] {
        &self.origins
    }

    /// Returns the ordered loaded project when no fatal load boundary failed.
    #[must_use]
    pub const fn loaded_project(&self) -> Option<&LoadedProject> {
        self.loaded_project.as_ref()
    }

    /// Returns the authored, no-interpolation merge when a mapping-root project was available.
    #[must_use]
    pub const fn merged_project(&self) -> Option<&MergedProject> {
        self.merged_project.as_ref()
    }

    /// Returns the effective typed project view used to discover child include declarations.
    #[must_use]
    pub const fn project_view(&self) -> Option<&ProjectView> {
        self.project_view.as_ref()
    }
}

/// One requested include edge in traversal order.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IncludeEdge {
    parent: IncludeIdentity,
    child: IncludeIdentity,
    parent_node_index: usize,
    child_node_index: usize,
    request_index: usize,
    cycle: bool,
}

impl IncludeEdge {
    /// Returns the identity that declared the include.
    #[must_use]
    pub const fn parent(&self) -> &IncludeIdentity {
        &self.parent
    }

    /// Returns the identity supplied by the caller's loader for the child.
    #[must_use]
    pub const fn child(&self) -> &IncludeIdentity {
        &self.child
    }

    /// Returns the occurrence index that declared this edge.
    #[must_use]
    pub const fn parent_node_index(&self) -> usize {
        self.parent_node_index
    }

    /// Returns the retained target occurrence index.
    ///
    /// A cycle targets its existing active occurrence; every other successful edge targets the
    /// retained occurrence loaded for that request.
    #[must_use]
    pub const fn child_node_index(&self) -> usize {
        self.child_node_index
    }

    /// Returns the matching [`IncludeResolution::requests`] index.
    #[must_use]
    pub const fn request_index(&self) -> usize {
        self.request_index
    }

    /// Reports whether this edge targets an already active occurrence.
    #[must_use]
    pub const fn is_cycle(&self) -> bool {
        self.cycle
    }
}

/// One of the six top-level definition namespaces composed from includes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum IncludeResourceNamespace {
    /// Compose services.
    Services,
    /// Compose networks.
    Networks,
    /// Compose volumes.
    Volumes,
    /// Compose configs.
    Configs,
    /// Compose secrets.
    Secrets,
    /// Individual Compose model definitions.
    Models,
}

impl IncludeResourceNamespace {
    const fn as_str(self) -> &'static str {
        match self {
            Self::Services => "service",
            Self::Networks => "network",
            Self::Volumes => "volume",
            Self::Configs => "config",
            Self::Secrets => "secret",
            Self::Models => "model",
        }
    }
}

/// Source and occurrence evidence for one selected or conflicting definition.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IncludeDefinitionEvidence {
    occurrence_index: usize,
    identity: IncludeIdentity,
    source: Option<SourceSpan>,
    source_label: Option<String>,
}

impl IncludeDefinitionEvidence {
    /// Returns the retained project occurrence index.
    #[must_use]
    pub const fn occurrence_index(&self) -> usize {
        self.occurrence_index
    }

    /// Returns the caller-defined identity of the retained project occurrence.
    #[must_use]
    pub const fn identity(&self) -> &IncludeIdentity {
        &self.identity
    }

    /// Returns the effective definition source, when the definition had one.
    #[must_use]
    pub const fn source(&self) -> Option<SourceSpan> {
        self.source
    }

    /// Returns the caller-defined label for [`Self::source`], when available.
    #[must_use]
    pub fn source_label(&self) -> Option<&str> {
        self.source_label.as_deref()
    }
}

/// One typed definition selected during include composition.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IncludeDefinition<T> {
    name: String,
    definition: T,
    evidence: IncludeDefinitionEvidence,
}

impl<T> IncludeDefinition<T> {
    /// Returns the Compose definition name.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns the typed effective definition.
    #[must_use]
    pub const fn definition(&self) -> &T {
        &self.definition
    }

    /// Returns occurrence and source evidence for this selected definition.
    #[must_use]
    pub const fn evidence(&self) -> &IncludeDefinitionEvidence {
        &self.evidence
    }
}

/// A same-name conflict that prevented an included definition from being imported.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IncludeResourceConflict {
    namespace: IncludeResourceNamespace,
    name: String,
    edge_index: usize,
    incoming: IncludeDefinitionEvidence,
    incumbent: IncludeDefinitionEvidence,
}

impl IncludeResourceConflict {
    /// Returns the colliding Compose namespace.
    #[must_use]
    pub const fn namespace(&self) -> IncludeResourceNamespace {
        self.namespace
    }

    /// Returns the shared definition name.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns the include-edge index through which the incoming candidate was considered.
    #[must_use]
    pub const fn edge_index(&self) -> usize {
        self.edge_index
    }

    /// Returns the child-side candidate that was not imported.
    #[must_use]
    pub const fn incoming(&self) -> &IncludeDefinitionEvidence {
        &self.incoming
    }

    /// Returns the already selected parent-side candidate.
    #[must_use]
    pub const fn incumbent(&self) -> &IncludeDefinitionEvidence {
        &self.incumbent
    }
}

/// The typed definitions selected for one retained include occurrence.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IncludeComposition {
    node_index: usize,
    services: Vec<IncludeDefinition<ProjectService>>,
    networks: Vec<IncludeDefinition<NetworkDefinition>>,
    volumes: Vec<IncludeDefinition<VolumeDefinition>>,
    configs: Vec<IncludeDefinition<ConfigDefinition>>,
    secrets: Vec<IncludeDefinition<SecretDefinition>>,
    models: Vec<IncludeDefinition<ModelDefinition>>,
}

impl IncludeComposition {
    /// Returns the retained occurrence represented by this composition.
    #[must_use]
    pub const fn node_index(&self) -> usize {
        self.node_index
    }

    /// Returns selected services in local-then-depth-first import order.
    #[must_use]
    pub fn services(&self) -> &[IncludeDefinition<ProjectService>] {
        &self.services
    }

    /// Finds a selected service by name.
    #[must_use]
    pub fn service(&self, name: &str) -> Option<&IncludeDefinition<ProjectService>> {
        self.services.iter().find(|definition| definition.name == name)
    }

    /// Returns selected networks in local-then-depth-first import order.
    #[must_use]
    pub fn networks(&self) -> &[IncludeDefinition<NetworkDefinition>] {
        &self.networks
    }

    /// Finds a selected network by name.
    #[must_use]
    pub fn network(&self, name: &str) -> Option<&IncludeDefinition<NetworkDefinition>> {
        self.networks.iter().find(|definition| definition.name == name)
    }

    /// Returns selected volumes in local-then-depth-first import order.
    #[must_use]
    pub fn volumes(&self) -> &[IncludeDefinition<VolumeDefinition>] {
        &self.volumes
    }

    /// Finds a selected volume by name.
    #[must_use]
    pub fn volume(&self, name: &str) -> Option<&IncludeDefinition<VolumeDefinition>> {
        self.volumes.iter().find(|definition| definition.name == name)
    }

    /// Returns selected configs in local-then-depth-first import order.
    #[must_use]
    pub fn configs(&self) -> &[IncludeDefinition<ConfigDefinition>] {
        &self.configs
    }

    /// Finds a selected config by name.
    #[must_use]
    pub fn config(&self, name: &str) -> Option<&IncludeDefinition<ConfigDefinition>> {
        self.configs.iter().find(|definition| definition.name == name)
    }

    /// Returns selected secrets in local-then-depth-first import order.
    #[must_use]
    pub fn secrets(&self) -> &[IncludeDefinition<SecretDefinition>] {
        &self.secrets
    }

    /// Finds a selected secret by name.
    #[must_use]
    pub fn secret(&self, name: &str) -> Option<&IncludeDefinition<SecretDefinition>> {
        self.secrets.iter().find(|definition| definition.name == name)
    }

    /// Returns selected individual model definitions in local-then-depth-first import order.
    #[must_use]
    pub fn models(&self) -> &[IncludeDefinition<ModelDefinition>] {
        &self.models
    }

    /// Finds a selected model definition by name.
    #[must_use]
    pub fn model(&self, name: &str) -> Option<&IncludeDefinition<ModelDefinition>> {
        self.models.iter().find(|definition| definition.name == name)
    }
}

/// The I/O-free outcome of composing an [`IncludeResolution`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IncludeCompositionResult {
    compositions: Vec<IncludeComposition>,
    diagnostics: Vec<Diagnostic>,
    conflicts: Vec<IncludeResourceConflict>,
}

impl IncludeCompositionResult {
    /// Returns the root composition, when the root occurrence was retained.
    #[must_use]
    pub fn root(&self) -> Option<&IncludeComposition> {
        self.compositions.first()
    }

    /// Returns compositions indexed by [`IncludeNode::index`].
    #[must_use]
    pub fn compositions(&self) -> &[IncludeComposition] {
        &self.compositions
    }

    /// Finds one retained occurrence's composition by node index.
    #[must_use]
    pub fn composition(&self, node_index: usize) -> Option<&IncludeComposition> {
        self.compositions.get(node_index)
    }

    /// Returns traversal diagnostics followed by composition conflict warnings.
    #[must_use]
    pub fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }

    /// Returns every non-imported same-name candidate in deterministic import order.
    #[must_use]
    pub fn conflicts(&self) -> &[IncludeResourceConflict] {
        &self.conflicts
    }

    /// Reports whether traversal and composition emitted no error diagnostic.
    #[must_use]
    pub fn is_valid(&self) -> bool {
        self.diagnostics
            .iter()
            .all(|diagnostic| diagnostic.severity() != Severity::Error)
    }

    /// Reports whether traversal completed without errors and no include resource conflicted.
    #[must_use]
    pub fn is_complete(&self) -> bool {
        self.is_valid() && self.conflicts.is_empty()
    }
}

/// A partial or complete depth-first traversal of caller-authorized Compose includes.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IncludeResolution {
    root: IncludeIdentity,
    nodes: Vec<IncludeNode>,
    edges: Vec<IncludeEdge>,
    requests: Vec<IncludeRequest>,
    diagnostics: Vec<Diagnostic>,
}

impl IncludeResolution {
    /// Loads and traverses one root project in depth-first effective-include order.
    ///
    /// Each node uses the established ordered loader, then an authored no-interpolation merge and
    /// native project view before child declarations are considered. The returned graph remains
    /// inspectable after loader denials, loader failures, malformed declarations, duplicate source
    /// identifiers, cycles, or malformed project inputs. Resources from children are never merged
    /// or imported into their parents.
    #[must_use]
    pub fn load(root: IncludedProjectInput, loader: &dyn IncludeLoader) -> Self {
        let root_identity = root.identity().clone();
        let mut resolution = Self {
            root: root_identity.clone(),
            nodes: Vec::new(),
            edges: Vec::new(),
            requests: Vec::new(),
            diagnostics: Vec::new(),
        };
        let mut active = Vec::new();
        let mut source_origins = BTreeMap::new();
        visit_project(root, loader, &mut active, &mut source_origins, &mut resolution, None);
        resolution
    }

    /// Returns the root identity selected by the caller.
    #[must_use]
    pub const fn root(&self) -> &IncludeIdentity {
        &self.root
    }

    /// Returns visited project occurrences in depth-first traversal order.
    #[must_use]
    pub fn nodes(&self) -> &[IncludeNode] {
        &self.nodes
    }

    /// Returns successful loader edges in request order.
    #[must_use]
    pub fn edges(&self) -> &[IncludeEdge] {
        &self.edges
    }

    /// Returns every effective declaration submitted to the caller's loader in order.
    #[must_use]
    pub fn requests(&self) -> &[IncludeRequest] {
        &self.requests
    }

    /// Returns diagnostics from every reached loading, merge, view, and traversal boundary.
    #[must_use]
    pub fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }

    /// Composes already loaded child occurrences without authorizing further loading or performing I/O.
    ///
    /// The pass first composes every non-cycle child recursively, then imports absent definitions
    /// into each parent after that parent's ordinary multi-file merge. Same-namespace same-name
    /// candidates remain separate conflict records and never enter ordinary Compose merge rules.
    #[must_use]
    pub fn compose(&self) -> IncludeCompositionResult {
        let mut diagnostics = self.diagnostics.clone();
        let mut conflicts = Vec::new();
        let mut compositions = vec![None; self.nodes.len()];
        for node_index in 0..self.nodes.len() {
            let _ = compose_node(node_index, self, &mut compositions, &mut diagnostics, &mut conflicts);
        }
        IncludeCompositionResult {
            compositions: compositions.into_iter().flatten().collect(),
            diagnostics,
            conflicts,
        }
    }

    /// Plans effective include project directories through an explicit caller-owned resolver.
    ///
    /// Only explicit `project_directory` declarations invoke the resolver. Root and child defaults
    /// reuse the caller-supplied first-document directories already retained by the traversal.
    #[must_use]
    pub fn plan_project_directories(
        &self,
        resolver: &dyn IncludeProjectDirectoryResolver,
    ) -> IncludeProjectDirectoryPlan {
        include_paths::plan_project_directories(self, resolver)
    }

    /// Reports whether traversal reached no error diagnostic.
    #[must_use]
    pub fn is_valid(&self) -> bool {
        self.diagnostics
            .iter()
            .all(|diagnostic| diagnostic.severity() != Severity::Error)
    }
}

fn visit_project(
    input: IncludedProjectInput,
    include_loader: &dyn IncludeLoader,
    active: &mut Vec<(IncludeIdentity, usize)>,
    source_origins: &mut BTreeMap<SourceId, DocumentOrigin>,
    resolution: &mut IncludeResolution,
    request_span: Option<SourceSpan>,
) -> usize {
    if input.documents().is_empty() {
        resolution.diagnostics.push(include_diagnostic(
            if request_span.is_some() {
                INCLUDE_EMPTY_RESULT
            } else {
                INCLUDE_EMPTY_ROOT
            },
            "included Compose project contains no documents",
            request_span,
        ));
        retain_unloaded_node(input, resolution);
        return resolution.nodes.len() - 1;
    }

    if !register_source_ids(input.documents(), source_origins, resolution, request_span) {
        retain_unloaded_node(input, resolution);
        return resolution.nodes.len() - 1;
    }

    let Some(prepared) = load_include_node(input, resolution, request_span) else {
        return resolution.nodes.len() - 1;
    };
    let PreparedIncludeNode {
        identity,
        base_directory,
        items,
        node_index,
    } = prepared;
    active.push((identity.clone(), node_index));

    visit_includes(
        items,
        &identity,
        &base_directory,
        node_index,
        include_loader,
        (active, source_origins, resolution),
    );

    let popped = active.pop();
    debug_assert_eq!(popped.as_ref().map(|(identity, _)| identity), Some(&identity));
    node_index
}

fn visit_includes(
    items: Vec<IncludeItem>,
    identity: &IncludeIdentity,
    base_directory: &Path,
    node_index: usize,
    include_loader: &dyn IncludeLoader,
    state: (
        &mut Vec<(IncludeIdentity, usize)>,
        &mut BTreeMap<SourceId, DocumentOrigin>,
        &mut IncludeResolution,
    ),
) {
    let (active, source_origins, resolution) = state;
    for item in items {
        let declaration_span = include_item_span(&item);
        let declaration_origin = declaration_span.and_then(|span| {
            resolution.nodes[node_index]
                .inputs()
                .documents()
                .iter()
                .find(|document| document.source_id() == span.source_id())
                .map(|document| document.origin().clone())
        });
        let Some(request) =
            IncludeRequest::from_item(identity.clone(), base_directory.to_path_buf(), declaration_origin, item)
        else {
            resolution.diagnostics.push(include_diagnostic(
                INCLUDE_UNMODELED,
                "effective include declaration is malformed or contains unmodeled members",
                declaration_span,
            ));
            continue;
        };
        let request_index = resolution.requests.len();
        let request_span = request.declaration_span();
        let child = match include_loader.load_include(&request) {
            Ok(child) => child,
            Err(IncludeLoadError::Denied(_)) => {
                resolution.diagnostics.push(include_diagnostic(
                    INCLUDE_LOADER_DENIED,
                    "include loader denied this declaration",
                    Some(request_span),
                ));
                resolution.requests.push(request);
                continue;
            }
            Err(IncludeLoadError::Failed(_)) => {
                resolution.diagnostics.push(include_diagnostic(
                    INCLUDE_LOADER_FAILED,
                    "include loader failed to supply this declaration",
                    Some(request_span),
                ));
                resolution.requests.push(request);
                continue;
            }
        };
        let child_identity = child.identity().clone();
        resolution.requests.push(request);
        let edge_index = resolution.edges.len();
        if let Some((_, active_node_index)) = active
            .iter()
            .find(|(active_identity, _)| active_identity == &child_identity)
        {
            resolution.edges.push(IncludeEdge {
                parent: identity.clone(),
                child: child_identity,
                parent_node_index: node_index,
                child_node_index: *active_node_index,
                request_index,
                cycle: true,
            });
            resolution.diagnostics.push(
                include_diagnostic(
                    INCLUDE_CYCLE,
                    "include identity is already active in this traversal",
                    Some(request_span),
                )
                .with_note("the caller controls identity canonicalization"),
            );
            continue;
        }
        let child_node_index = visit_project(
            child,
            include_loader,
            active,
            source_origins,
            resolution,
            Some(request_span),
        );
        resolution.edges.insert(
            edge_index,
            IncludeEdge {
                parent: identity.clone(),
                child: child_identity,
                parent_node_index: node_index,
                child_node_index,
                request_index,
                cycle: false,
            },
        );
    }
}

struct PreparedIncludeNode {
    identity: IncludeIdentity,
    base_directory: PathBuf,
    items: Vec<IncludeItem>,
    node_index: usize,
}

fn load_include_node(
    input: IncludedProjectInput,
    resolution: &mut IncludeResolution,
    request_span: Option<SourceSpan>,
) -> Option<PreparedIncludeNode> {
    let identity = input.identity().clone();
    let origins = input
        .documents()
        .iter()
        .map(|document| document.origin().clone())
        .collect();
    let Ok(project) = LoadedProject::load(input.documents().iter().cloned()) else {
        resolution.diagnostics.push(include_diagnostic(
            INCLUDE_PROJECT_LOAD_FAILED,
            "included Compose project could not enter the ordered loader",
            request_span,
        ));
        resolution.nodes.push(IncludeNode {
            index: resolution.nodes.len(),
            identity,
            inputs: input,
            origins,
            loaded_project: None,
            merged_project: None,
            project_view: None,
        });
        return None;
    };

    let merge = merge_project(&project, None);
    resolution.diagnostics.extend(merge.diagnostics().iter().cloned());
    let merged_project = merge.project().cloned();
    let (project_view, view_diagnostics) = match merged_project.as_ref() {
        Some(merged) => build_project_view(merged, None).into_parts(),
        None => (None, Vec::new()),
    };
    resolution.diagnostics.extend(view_diagnostics);
    let base_directory = project.base_directory().to_path_buf();
    let items = project_view
        .as_ref()
        .and_then(ProjectView::include)
        .map(|includes| includes.value().items().to_vec())
        .unwrap_or_default();
    let node_index = resolution.nodes.len();
    resolution.nodes.push(IncludeNode {
        index: node_index,
        identity: identity.clone(),
        inputs: input,
        origins,
        loaded_project: Some(project),
        merged_project,
        project_view,
    });
    Some(PreparedIncludeNode {
        identity,
        base_directory,
        items,
        node_index,
    })
}

fn retain_unloaded_node(input: IncludedProjectInput, resolution: &mut IncludeResolution) {
    let origins = input
        .documents()
        .iter()
        .map(|document| document.origin().clone())
        .collect();
    resolution.nodes.push(IncludeNode {
        index: resolution.nodes.len(),
        identity: input.identity().clone(),
        inputs: input,
        origins,
        loaded_project: None,
        merged_project: None,
        project_view: None,
    });
}

fn register_source_ids(
    documents: &[DocumentInput],
    source_origins: &mut BTreeMap<SourceId, DocumentOrigin>,
    resolution: &mut IncludeResolution,
    request_span: Option<SourceSpan>,
) -> bool {
    let mut accepted = true;
    for document in documents {
        match source_origins.entry(document.source_id()) {
            std::collections::btree_map::Entry::Occupied(_) => {
                resolution.diagnostics.push(include_diagnostic(
                    INCLUDE_DUPLICATE_SOURCE_ID,
                    "include traversal reused a caller-managed source identifier",
                    request_span,
                ));
                accepted = false;
            }
            std::collections::btree_map::Entry::Vacant(entry) => {
                entry.insert(document.origin().clone());
            }
        }
    }
    accepted
}

fn include_item_span(item: &IncludeItem) -> Option<SourceSpan> {
    match item {
        IncludeItem::Short(path) => Some(path.span()),
        IncludeItem::Long(include) => Some(include.span()),
        IncludeItem::Unmodeled => None,
    }
}

fn include_diagnostic(code: DiagnosticCode, message: &'static str, span: Option<SourceSpan>) -> Diagnostic {
    let diagnostic = Diagnostic::new(code, Severity::Error, message);
    match span {
        Some(span) => diagnostic.with_label(DiagnosticLabel::primary(span, "include declaration")),
        None => diagnostic,
    }
}

fn compose_node(
    node_index: usize,
    resolution: &IncludeResolution,
    compositions: &mut [Option<IncludeComposition>],
    diagnostics: &mut Vec<Diagnostic>,
    conflicts: &mut Vec<IncludeResourceConflict>,
) -> IncludeComposition {
    if let Some(composition) = &compositions[node_index] {
        return composition.clone();
    }

    let mut composition = local_composition(node_index, resolution);
    for (edge_index, edge) in resolution.edges.iter().enumerate() {
        if edge.parent_node_index != node_index || edge.cycle {
            continue;
        }
        let child = compose_node(edge.child_node_index, resolution, compositions, diagnostics, conflicts);
        import_definitions(
            IncludeResourceNamespace::Services,
            edge_index,
            &mut composition.services,
            &child.services,
            edge,
            diagnostics,
            conflicts,
        );
        import_definitions(
            IncludeResourceNamespace::Networks,
            edge_index,
            &mut composition.networks,
            &child.networks,
            edge,
            diagnostics,
            conflicts,
        );
        import_definitions(
            IncludeResourceNamespace::Volumes,
            edge_index,
            &mut composition.volumes,
            &child.volumes,
            edge,
            diagnostics,
            conflicts,
        );
        import_definitions(
            IncludeResourceNamespace::Configs,
            edge_index,
            &mut composition.configs,
            &child.configs,
            edge,
            diagnostics,
            conflicts,
        );
        import_definitions(
            IncludeResourceNamespace::Secrets,
            edge_index,
            &mut composition.secrets,
            &child.secrets,
            edge,
            diagnostics,
            conflicts,
        );
        import_definitions(
            IncludeResourceNamespace::Models,
            edge_index,
            &mut composition.models,
            &child.models,
            edge,
            diagnostics,
            conflicts,
        );
    }
    compositions[node_index] = Some(composition.clone());
    composition
}

fn local_composition(node_index: usize, resolution: &IncludeResolution) -> IncludeComposition {
    let node = &resolution.nodes[node_index];
    let Some(view) = node.project_view() else {
        return IncludeComposition {
            node_index,
            services: Vec::new(),
            networks: Vec::new(),
            volumes: Vec::new(),
            configs: Vec::new(),
            secrets: Vec::new(),
            models: Vec::new(),
        };
    };

    IncludeComposition {
        node_index,
        services: view
            .services()
            .iter()
            .map(|service| {
                local_definition(
                    service.name().value(),
                    service.clone(),
                    node_index,
                    node,
                    service
                        .provenance()
                        .effective_source()
                        .or_else(|| service.name().effective_source()),
                )
            })
            .collect(),
        networks: local_resources(view.networks(), node_index, node),
        volumes: local_resources(view.volumes(), node_index, node),
        configs: local_resources(view.configs(), node_index, node),
        secrets: local_resources(view.secrets(), node_index, node),
        models: view
            .models()
            .into_iter()
            .flat_map(|models| models.value().definitions())
            .map(|model| local_definition(model.key().value(), model.clone(), node_index, node, Some(model.span())))
            .collect(),
    }
}

fn local_resources<T: Clone>(
    resources: &[ProjectResource<T>],
    node_index: usize,
    node: &IncludeNode,
) -> Vec<IncludeDefinition<T>> {
    resources
        .iter()
        .map(|resource| {
            local_definition(
                resource.name().value(),
                resource.definition().value().clone(),
                node_index,
                node,
                resource
                    .definition()
                    .effective_source()
                    .or_else(|| resource.name().effective_source()),
            )
        })
        .collect()
}

fn local_definition<T>(
    name: &str,
    definition: T,
    node_index: usize,
    node: &IncludeNode,
    source: Option<SourceSpan>,
) -> IncludeDefinition<T> {
    let source_label = source.and_then(|source| {
        node.origins()
            .iter()
            .zip(node.inputs().documents())
            .find(|(_, input)| input.source_id() == source.source_id())
            .map(|(origin, _)| origin.label().to_owned())
    });
    IncludeDefinition {
        name: name.to_owned(),
        definition,
        evidence: IncludeDefinitionEvidence {
            occurrence_index: node_index,
            identity: node.identity().clone(),
            source,
            source_label,
        },
    }
}

fn import_definitions<T: Clone>(
    namespace: IncludeResourceNamespace,
    edge_index: usize,
    parent: &mut Vec<IncludeDefinition<T>>,
    child: &[IncludeDefinition<T>],
    edge: &IncludeEdge,
    diagnostics: &mut Vec<Diagnostic>,
    conflicts: &mut Vec<IncludeResourceConflict>,
) {
    for incoming in child {
        if let Some(incumbent) = parent.iter().find(|candidate| candidate.name == incoming.name) {
            let conflict = IncludeResourceConflict {
                namespace,
                name: incoming.name.clone(),
                edge_index,
                incoming: incoming.evidence.clone(),
                incumbent: incumbent.evidence.clone(),
            };
            diagnostics.push(include_resource_conflict_diagnostic(&conflict, edge));
            conflicts.push(conflict);
        } else {
            parent.push(incoming.clone());
        }
    }
}

fn include_resource_conflict_diagnostic(conflict: &IncludeResourceConflict, edge: &IncludeEdge) -> Diagnostic {
    let mut diagnostic = Diagnostic::new(
        INCLUDE_RESOURCE_CONFLICT,
        Severity::Warning,
        format!(
            "included {} `{}` conflicts with an already selected definition",
            conflict.namespace.as_str(),
            conflict.name
        ),
    );
    if let Some(source) = conflict.incoming.source {
        diagnostic = diagnostic.with_label(DiagnosticLabel::primary(
            source,
            format!(
                "incoming {} `{}` from {}",
                conflict.namespace.as_str(),
                conflict.name,
                conflict.incoming.source_label().unwrap_or("included occurrence")
            ),
        ));
    }
    if let Some(source) = conflict.incumbent.source {
        diagnostic = diagnostic.with_label(DiagnosticLabel::secondary(
            source,
            format!(
                "incumbent {} `{}` from {}",
                conflict.namespace.as_str(),
                conflict.name,
                conflict.incumbent.source_label().unwrap_or("parent occurrence")
            ),
        ));
    }
    diagnostic.with_note(format!(
        "declaring include edge #{}, occurrence #{} ({}) -> #{} ({})",
        conflict.edge_index, edge.parent_node_index, edge.parent, edge.child_node_index, edge.child
    ))
}

/// Per-file interpolation overlays for one loaded project.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProjectInterpolation {
    documents: Vec<DocumentInterpolation>,
    diagnostics: Vec<Diagnostic>,
}

impl ProjectInterpolation {
    /// Returns document overlays in file order.
    #[must_use]
    pub fn documents(&self) -> &[DocumentInterpolation] {
        &self.documents
    }

    /// Finds a document overlay by source identifier.
    #[must_use]
    pub fn document(&self, source_id: SourceId) -> Option<&DocumentInterpolation> {
        self.documents.iter().find(|document| document.source_id() == source_id)
    }

    /// Returns aggregated interpolation diagnostics in file order.
    #[must_use]
    pub fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }

    /// Reports whether interpolation emitted no error diagnostics.
    #[must_use]
    pub fn is_valid(&self) -> bool {
        self.diagnostics
            .iter()
            .all(|diagnostic| diagnostic.severity() != Severity::Error)
    }
}

/// A fatal project-loading failure.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ProjectLoadError {
    /// At least one Compose document is required to establish ordering and a base directory.
    EmptyProject,
    /// Two inputs reused a caller-managed source identifier.
    DuplicateSourceId {
        /// The reused identifier.
        source_id: SourceId,
        /// The first document's display label.
        first_origin: String,
        /// The later document's display label.
        duplicate_origin: String,
    },
    /// A document exceeded the syntax tree's byte-offset capacity.
    SyntaxCapacity {
        /// The rejected document's origin.
        origin: DocumentOrigin,
        /// The underlying parser capacity error.
        error: SyntaxParseError,
    },
}

impl fmt::Display for ProjectLoadError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::EmptyProject => formatter.write_str("a Compose project requires at least one document"),
            Self::DuplicateSourceId {
                source_id,
                first_origin,
                duplicate_origin,
            } => write!(
                formatter,
                "{source_id} is assigned to both `{first_origin}` and `{duplicate_origin}`"
            ),
            Self::SyntaxCapacity { origin, error } => write!(formatter, "{}: {error}", origin.label),
        }
    }
}

impl Error for ProjectLoadError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::SyntaxCapacity { error, .. } => Some(error),
            Self::EmptyProject | Self::DuplicateSourceId { .. } => None,
        }
    }
}