diplodocus 0.15.0

Build coherent documentation websites for polyglot software 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
//! Assemble explicitly declared sources before reference validation and storage.
//!
//! Loading prepares authored content and resolves API declarations. Rust targets
//! use native compiler tooling, and C++ targets invoke Doxygen. R command
//! adapters generate documentation when configured and discover native APIs.
//! Extraction also completes native semantics and examples before publication.
//! These operations require current command authority from their caller and do
//! not publish a snapshot or site. The resulting IR still
//! needs document-reference, relationship-version, and asset validation before
//! publication; assembly is not a rendering trust token.

use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};

use crate::configuration::{ConfigurationError, WorkspaceConfiguration, parse_configuration};
use crate::configuration_validation::validate_configuration_with_source;
use crate::diagnostics::{Diagnostic, DiagnosticCode, DiagnosticPath, DiagnosticSource, Severity};
use crate::documents::{QmdPreparation, includes};
use crate::execution::{
    ExecutionCancellation, ExecutionDeadlines, ExecutionFailure, PageExecutionResult,
};
use crate::ir::{Fingerprint, SourceLocation, Workspace};
use crate::paths::{PathResolutionError, ResolvedWorkspacePaths, resolve_workspace_paths};
use crate::provenance::{
    DeclaredSourceInputs, ProvenanceError, StaticProvenance, collect_static_provenance,
    fingerprint_bytes,
};
use crate::reporting::{Phase, Reporting};

mod adapters;
mod api_examples;
mod authored;
mod bundles;
mod documenter;
mod documenter_examples;
mod doxygen_native;
mod julia_environment;
mod julia_runtime;
pub(crate) use julia_runtime::assemble_for_extraction;
#[cfg(target_os = "linux")]
mod execution;
mod package_docs;
mod packages;
mod python_native;
pub(crate) mod r_native;
mod rd_examples;
mod references;
mod reuse;
mod rmarkdown;
mod rust_native;

pub use crate::ir::IncludeSourceRange;
pub(crate) use includes::remap as remap_include_diagnostic;
pub(crate) use includes::watch_inputs as include_watch_inputs;

/// Conventional license inputs shared by package discovery and preview watching.
pub(crate) const PACKAGE_LICENSE_FILENAMES: &[&str] = &[
    "LICENSE",
    "LICENSE.md",
    "LICENCE",
    "LICENCE.md",
    "LICENSE-MIT",
    "LICENSE-APACHE",
    "COPYING",
    "COPYING.md",
];

/// A failed source assembly or execution, without a publishable workspace.
#[derive(Debug, thiserror::Error)]
pub enum AssemblyError {
    /// A package bundle could not be retrieved, validated, or composed.
    #[error("package bundle {path}: {message}")]
    Bundle {
        /// Selected artifact path or release URL.
        path: PathBuf,
        /// Failure details.
        message: String,
    },
    /// Configuration parsing failed.
    #[error(transparent)]
    Configuration(#[from] ConfigurationError),
    /// Declared paths failed their boundary checks.
    #[error(transparent)]
    Path(#[from] PathResolutionError),
    /// Static source observations failed.
    #[error(transparent)]
    Provenance(#[from] ProvenanceError),
    /// Independent source errors in deterministic diagnostic order.
    #[error("workspace assembly failed with source diagnostics")]
    Diagnostics(Vec<Diagnostic>),
    /// File reading or temporary staging failed.
    #[error("workspace source or staging I/O failed: {0}")]
    Io(#[from] std::io::Error),
    /// A consumed input no longer agrees with the assembled evidence.
    #[error("workspace inputs changed during assembly or execution")]
    InputsChanged,
    /// The caller canceled the workspace operation between active page sessions.
    #[error("workspace execution was canceled")]
    Cancelled,
    /// Authored execution failed after supervised kernel cleanup.
    #[error(transparent)]
    Execution(#[from] ExecutionFailure),
    /// Cleanup failed after another failure; preserve both causes.
    #[error("{cause}; workspace staging cleanup also failed: {source}")]
    Cleanup {
        /// Original failure.
        cause: Box<AssemblyError>,
        /// Staging cleanup error.
        source: std::io::Error,
    },
}

impl From<includes::IncludeError> for AssemblyError {
    fn from(error: includes::IncludeError) -> Self {
        match error {
            includes::IncludeError::Path(error) => Self::Path(error),
            includes::IncludeError::Diagnostics(diagnostics) => Self::Diagnostics(diagnostics),
        }
    }
}

/// One original authored page and its optional QMD preparation.
#[derive(Debug)]
pub struct PreparedPage {
    pub(crate) source_ranges: Vec<IncludeSourceRange>,
    pub(crate) included_resource_targets: std::collections::BTreeSet<String>,
    pub(crate) source: String,
    pub(crate) path: PathBuf,
    pub(crate) relative: DiagnosticPath,
    pub(crate) location: SourceLocation,
    pub(crate) collection: usize,
    pub(crate) preparation: Option<QmdPreparation>,
}
impl PreparedPage {
    /// Original file ranges and include chains for the expanded source.
    pub fn source_ranges(&self) -> &[IncludeSourceRange] {
        &self.source_ranges
    }
    /// Expanded UTF-8 source used for parsing and execution identity.
    pub fn source(&self) -> &str {
        &self.source
    }
    /// Pure authored preparation, including disabled cells and page vetoes.
    pub fn preparation(&self) -> Option<&QmdPreparation> {
        self.preparation.as_ref()
    }
}

/// Static source assembly; local paths and original inputs never serialize.
#[derive(Debug)]
pub struct WorkspaceSources {
    reporting: Reporting,
    workspace: Workspace,
    configuration: WorkspaceConfiguration,
    configuration_path: PathBuf,
    configuration_source: String,
    paths: ResolvedWorkspacePaths,
    prepared_pages: BTreeMap<String, PreparedPage>,
    package_files: BTreeMap<String, package_docs::PackageFiles>,
    rmd_inputs: BTreeMap<String, Vec<PathBuf>>,
    doxygen_observations: Vec<doxygen_native::DoxygenObservation>,
    r_observations: Vec<r_native::RObservation>,
    cache_observations: CacheObservations,
    python_interpreter: Option<PathBuf>,
    python_doctests: BTreeMap<String, crate::extractors::python::native::DoctestPlans>,
    python_environments: BTreeMap<
        String,
        (
            api_examples::PythonEnvironment,
            crate::incremental::Manifest,
        ),
    >,
    selections: DeclaredSourceInputs,
    evidence: StaticProvenance,
    pub(crate) bundles: bundles::ImportedBundles,
    bundle_export: bool,
    pub(crate) native_assets: BTreeMap<DiagnosticPath, crate::validation::ContentAsset>,
}

#[derive(Debug, Default)]
struct CacheObservations(Vec<crate::incremental::Manifest>);

impl CacheObservations {
    fn push(&mut self, manifest: crate::incremental::Manifest) {
        // Retain conflicting observations so a later guard still rejects changed inputs.
        if !self.contains(&manifest) {
            self.0.push(manifest);
        }
    }

    fn contains(&self, manifest: &crate::incremental::Manifest) -> bool {
        self.0.contains(manifest)
    }

    fn iter(&self) -> impl Iterator<Item = &crate::incremental::Manifest> {
        self.0.iter()
    }
}

impl Extend<crate::incremental::Manifest> for CacheObservations {
    fn extend<T: IntoIterator<Item = crate::incremental::Manifest>>(&mut self, manifests: T) {
        for manifest in manifests {
            self.push(manifest);
        }
    }
}

impl WorkspaceSources {
    /// Portable records assembled so far, without a claim of final validation.
    pub fn workspace(&self) -> &Workspace {
        &self.workspace
    }
    /// Original authored inputs keyed by stable page identity.
    pub fn prepared_pages(&self) -> &BTreeMap<String, PreparedPage> {
        &self.prepared_pages
    }
    /// Explicit declarations consumed by this assembly.
    pub fn configuration(&self) -> &WorkspaceConfiguration {
        &self.configuration
    }
    /// Runtime boundaries for later declared-input watching and asset collection.
    pub fn paths(&self) -> &ResolvedWorkspacePaths {
        &self.paths
    }

    /// Exact selected source files, metadata, and declared environment inputs.
    /// These local paths are used for output protection and preview watching.
    pub fn input_paths(&self) -> impl Iterator<Item = PathBuf> + '_ {
        self.evidence
            .inputs
            .iter()
            .flat_map(|(repository, files)| {
                let root = &self
                    .paths
                    .repositories
                    .iter()
                    .find(|r| &r.id == repository)
                    .expect("observed repository")
                    .path;
                files.keys().map(move |path| root.join(path.as_str()))
            })
            .chain(self.bundles.input_paths())
            .chain(
                self.r_observations
                    .iter()
                    .flat_map(|observation| observation.paths().cloned()),
            )
            .chain(
                self.cache_observations
                    .iter()
                    .flat_map(|manifest| manifest.paths().cloned()),
            )
    }

    pub(crate) fn doxygen_watch_roots(&self) -> impl Iterator<Item = &PathBuf> {
        self.doxygen_observations
            .iter()
            .flat_map(|observation| observation.roots())
    }

    pub(crate) fn dependency_watch_roots(&self) -> impl Iterator<Item = &PathBuf> {
        self.cache_observations
            .iter()
            .flat_map(|manifest| manifest.roots())
            .chain(
                self.r_observations
                    .iter()
                    .flat_map(|observation| observation.dependency_roots()),
            )
    }

    pub(crate) fn dependency_library_roots(&self) -> impl Iterator<Item = &PathBuf> {
        self.cache_observations
            .iter()
            .flat_map(|manifest| manifest.native_library_roots())
            .chain(
                self.r_observations
                    .iter()
                    .flat_map(|observation| observation.dependency_library_roots()),
            )
    }

    /// Reread selected bytes and containment before accepting a later operation.
    ///
    /// This detects ordinary edits, including a later page changing an earlier
    /// page. It cannot snapshot undeclared reads or changes after this check.
    pub fn revalidate(&self) -> Result<(), AssemblyError> {
        let timing = self.reporting.phase(Phase::SourceVerification, None, None);
        let result = crate::incremental::verification_scope(|| self.revalidate_inner());
        if result.is_ok() {
            timing.finish();
        }
        result
    }

    fn revalidate_inner(&self) -> Result<(), AssemblyError> {
        if self
            .cache_observations
            .iter()
            .any(|manifest| !manifest.verify())
        {
            return Err(AssemblyError::InputsChanged);
        }
        for observation in &self.r_observations {
            observation.verify()?;
        }
        for observation in &self.doxygen_observations {
            observation.verify()?;
        }
        if fs::read(&self.configuration_path)? != self.configuration_source.as_bytes() {
            return Err(AssemblyError::InputsChanged);
        }
        let mut configuration =
            parse_configuration(&self.configuration_source).map_err(|source| {
                ConfigurationError::Parse {
                    path: self.configuration_path.clone(),
                    source,
                }
            })?;
        self.bundles.revalidate()?;
        if !self.bundle_export {
            rmarkdown::collections(&self.configuration_path, &mut configuration)?;
        }
        if configuration != self.configuration {
            return Err(AssemblyError::InputsChanged);
        }
        let paths = resolve_workspace_paths(&self.configuration_path, &self.configuration)?;
        if paths != self.paths {
            return Err(AssemblyError::InputsChanged);
        }
        for (index, collection) in self.configuration.content.iter().enumerate() {
            let content = &paths.content[index];
            if authored::discover(
                &content.path,
                collection.format,
                &paths.repositories[content.repository_index],
            )? != self.selections.content[&collection.id]
            {
                return Err(AssemblyError::InputsChanged);
            }
        }
        if package_docs::discover(&paths, &self.workspace)? != self.package_files {
            return Err(AssemblyError::InputsChanged);
        }
        if rmarkdown::inputs(&paths, &self.configuration)? != self.rmd_inputs {
            return Err(AssemblyError::InputsChanged);
        }
        let current = collect_static_provenance(
            &self.configuration_path,
            &self.configuration,
            &self.selections,
        )?;
        if current.inputs != self.evidence.inputs {
            return Err(AssemblyError::InputsChanged);
        }
        Ok(())
    }
}

/// Local settings for an explicitly authorized workspace execution attempt.
pub struct WorkspaceExecution<'a> {
    /// Existing directory beneath which the attempt owns temporary staging.
    pub staging_parent: &'a Path,
    /// Phase limits shared by this attempt's page sessions.
    pub deadlines: ExecutionDeadlines,
    /// Cooperative cancellation, also observed between pages.
    pub cancellation: ExecutionCancellation<'a>,
}

/// Executed workspace and its private staging lifetime.
///
/// Each page retains its validated wrappers alongside the portable IR. Dropping
/// this owner removes the attempt's staging, including assets of earlier pages.
/// Consumers must copy verified bytes into their own transaction before disposal.
#[derive(Debug)]
pub struct ExecutedWorkspace {
    sources: WorkspaceSources,
    pages: BTreeMap<String, PageExecutionResult>,
    staging: Option<tempfile::TempDir>,
}
impl ExecutedWorkspace {
    /// Portable records with complete executed-cell outputs and provenance.
    pub fn workspace(&self) -> &Workspace {
        self.sources.workspace()
    }
    /// Current page results with active validation evidence and retained files.
    pub fn executed_pages(&self) -> &BTreeMap<String, PageExecutionResult> {
        &self.pages
    }
    /// Original declarations, source preparations, and runtime boundaries.
    pub fn sources(&self) -> &WorkspaceSources {
        &self.sources
    }
    /// Explicitly discard staging and report filesystem cleanup failure.
    pub fn discard(mut self) -> Result<(), AssemblyError> {
        self.pages.clear();
        if let Some(staging) = self.staging.take() {
            staging.close()?;
        }
        Ok(())
    }
}

/// Read and assemble declared sources using the discovery rules of `check`.
/// Documentation examples do not execute.
/// Rust targets use the prepared compiler and may compile dependencies and run
/// build scripts. C++ targets invoke Doxygen and any configured input filters.
/// Python targets import the prepared checkout and invoke Sphinx autodoc.
/// Julia targets load the verified checkout and reflect on its API and docs.
/// R targets load package code and may regenerate documentation in the checkout.
///
/// The returned workspace is ready for reference validation.
/// Semantic page identity depends on collection ID and relative source path,
/// never a route.
///
/// # Errors
/// Reports malformed declarations, unsafe paths, conflicting fragments,
/// unresolved concepts, source diagnostics, I/O, or changed input observations.
pub fn assemble_workspace(path: impl AsRef<Path>) -> Result<WorkspaceSources, AssemblyError> {
    // Callers may already own a Tokio runtime; a separate thread keeps this
    // synchronous API usable from both synchronous and asynchronous hosts.
    let path = path.as_ref().to_owned();
    std::thread::Builder::new()
        .name("native-documentation-check".into())
        .spawn(move || {
            let runtime = tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()?;
            runtime.block_on(async {
                let mut cancellation: ExecutionCancellation<'_> = Box::pin(std::future::pending());
                assemble_for_check(
                    &path,
                    ExecutionDeadlines::default(),
                    &mut cancellation,
                    &Reporting::default(),
                )
                .await
            })
        })?
        .join()
        .map_err(|_| {
            AssemblyError::Io(std::io::Error::other(
                "Native documentation check thread failed",
            ))
        })?
}

async fn rust_results(
    path: &Path,
    run_examples: bool,
    deadlines: ExecutionDeadlines,
    cancellation: &mut ExecutionCancellation<'_>,
    reporting: &Reporting,
) -> Result<BTreeMap<(String, String), rust_native::RustNativeExtraction>, AssemblyError> {
    let loading = reporting.stage("Loading workspace", None);
    let configuration_source = fs::read(path)?;
    let configuration = crate::configuration::load_configuration(path)?;
    reject_errors(validate_configuration_with_source(
        &configuration,
        "diplodocus.yaml".try_into().unwrap(),
    ))?;
    let paths = resolve_workspace_paths(path, &configuration)?;
    loading.finish();
    let mut progress = reporting.stage(
        "Extracting Rust APIs",
        Some(target_count(&configuration, "rust")),
    );
    let cache = paths.configuration_directory.join(".diplodocus/cache");
    let mut results = BTreeMap::new();
    for (index, (declaration, package)) in configuration
        .packages
        .iter()
        .zip(&paths.packages)
        .enumerate()
    {
        let repository = &paths.repositories[package.repository_index];
        let result_cache = crate::incremental::package_context(&configuration, &paths, index);
        for (target, selected) in declaration.targets.iter().zip(&package.targets) {
            if target.extractor == "rust" {
                progress.item(&format!("{}:{}", package.id, target.id));
                let result = rust_native::extract_target_cached(
                    repository,
                    package,
                    selected,
                    &target.rust.clone().unwrap_or_default(),
                    &cache,
                    run_examples,
                    reporting,
                    deadlines,
                    cancellation,
                    result_cache.as_ref(),
                )
                .await?;
                results.insert((package.id.clone(), target.id.clone()), result);
                progress.advance();
            }
        }
    }
    if fs::read(path)? != configuration_source {
        return Err(AssemblyError::InputsChanged);
    }
    progress.finish();
    Ok(results)
}

pub(crate) async fn assemble_for_check(
    path: &Path,
    deadlines: ExecutionDeadlines,
    cancellation: &mut ExecutionCancellation<'_>,
    reporting: &Reporting,
) -> Result<WorkspaceSources, AssemblyError> {
    let rust = rust_results(path, false, deadlines, cancellation, reporting).await?;
    let doxygen = doxygen_results(path, deadlines, cancellation, reporting).await?;
    let r = r_results(
        path,
        deadlines,
        cancellation,
        reporting,
        &mut r_native::GeneratedWrites::default(),
    )
    .await?;
    let python = python_native::results(path, deadlines, cancellation, reporting).await?;
    let julia = julia_runtime::results(path, deadlines, cancellation, reporting).await?;
    assemble_with_native(
        path,
        NativeExtractions {
            julia,
            rust,
            doxygen,
            r,
            python,
        },
        false,
        reporting,
        cancellation,
    )
    .await
}

async fn doxygen_results(
    path: &Path,
    deadlines: ExecutionDeadlines,
    cancellation: &mut ExecutionCancellation<'_>,
    reporting: &Reporting,
) -> Result<BTreeMap<(String, String), doxygen_native::DoxygenNativeExtraction>, AssemblyError> {
    let before = fs::read(path)?;
    let configuration = crate::configuration::load_configuration(path)?;
    reject_errors(validate_configuration_with_source(
        &configuration,
        "diplodocus.yaml".try_into().unwrap(),
    ))?;
    let paths = resolve_workspace_paths(path, &configuration)?;
    let mut progress = reporting.stage(
        "Extracting C++ APIs",
        Some(target_count(&configuration, "doxygen")),
    );
    let mut results = BTreeMap::new();
    for (index, (declaration, package)) in configuration
        .packages
        .iter()
        .zip(&paths.packages)
        .enumerate()
    {
        let repository = &paths.repositories[package.repository_index];
        let cache = crate::incremental::package_context(&configuration, &paths, index);
        for (target, selected) in declaration.targets.iter().zip(&package.targets) {
            if target.extractor == "doxygen" {
                progress.item(&format!("{}:{}", package.id, target.id));
                results.insert(
                    (package.id.clone(), target.id.clone()),
                    doxygen_native::extract_target(
                        repository,
                        package,
                        selected,
                        reporting,
                        deadlines,
                        cancellation,
                        cache.as_ref(),
                    )
                    .await?,
                );
                progress.advance();
            }
        }
    }
    if fs::read(path)? != before {
        return Err(AssemblyError::InputsChanged);
    }
    progress.finish();
    Ok(results)
}

async fn r_results(
    path: &Path,
    deadlines: ExecutionDeadlines,
    cancellation: &mut ExecutionCancellation<'_>,
    reporting: &Reporting,
    journal: &mut r_native::GeneratedWrites,
) -> Result<BTreeMap<(String, String), r_native::RNativeExtraction>, AssemblyError> {
    let before = fs::read(path)?;
    let configuration = crate::configuration::load_configuration(path)?;
    reject_errors(validate_configuration_with_source(
        &configuration,
        "diplodocus.yaml".try_into().unwrap(),
    ))?;
    let paths = resolve_workspace_paths(path, &configuration)?;
    let configuration_path = fs::canonicalize(path)?;
    for (declaration, package) in configuration.packages.iter().zip(&paths.packages) {
        if let Some(settings) = &declaration.r {
            for output in &settings.generated_outputs {
                let output = r_native::resolved_output(package, output)?;
                if configuration_path.starts_with(&output)
                    || output.starts_with(&configuration_path)
                    || paths.packages.iter().zip(&configuration.packages).any(
                        |(other, declaration)| {
                            other.id != package.id
                                && (other.path.starts_with(&output)
                                    || (!package.path.starts_with(&other.path)
                                        && output.starts_with(&other.path))
                                    || other.metadata_path.starts_with(&output)
                                    || output.starts_with(&other.metadata_path)
                                    || declaration.r.as_ref().is_some_and(|settings| {
                                        settings
                                            .generated_outputs
                                            .iter()
                                            .chain(&settings.extra_inputs)
                                            .any(|path| {
                                                let path = other.path.join(path);
                                                path.starts_with(&output)
                                                    || output.starts_with(&path)
                                            })
                                    }))
                        },
                    )
                {
                    return Err(AssemblyError::Diagnostics(vec![Diagnostic::new(
                        DiagnosticCode::InvalidConfiguration,
                        Severity::Error,
                        "Generated R outputs must not overlap workspace configuration or another package.",
                    )]));
                }
            }
        }
    }
    let results = r_native::results(
        &configuration,
        &paths,
        deadlines,
        cancellation,
        reporting,
        journal,
    )
    .await?;
    if fs::read(path)? != before {
        return Err(AssemblyError::InputsChanged);
    }
    Ok(results)
}

struct NativeExtractions {
    python: BTreeMap<(String, String), python_native::PythonNativeExtraction>,
    julia: BTreeMap<(String, String), julia_runtime::JuliaNativeExtraction>,
    rust: BTreeMap<(String, String), rust_native::RustNativeExtraction>,
    doxygen: BTreeMap<(String, String), doxygen_native::DoxygenNativeExtraction>,
    r: BTreeMap<(String, String), r_native::RNativeExtraction>,
}

async fn assemble_with_native(
    path: &Path,
    mut native: NativeExtractions,
    bundle_export: bool,
    reporting: &Reporting,
    cancellation: &mut ExecutionCancellation<'_>,
) -> Result<WorkspaceSources, AssemblyError> {
    let text = fs::read_to_string(path).map_err(|source| ConfigurationError::Read {
        path: path.to_owned(),
        source,
    })?;
    let mut configuration =
        parse_configuration(&text).map_err(|source| ConfigurationError::Parse {
            path: path.to_owned(),
            source,
        })?;
    let config_name = DiagnosticPath::try_from(
        path.file_name()
            .and_then(|s| s.to_str())
            .unwrap_or("diplodocus.yaml"),
    )
    .map_err(|_| AssemblyError::InputsChanged)?;
    reject_errors(validate_configuration_with_source(
        &configuration,
        config_name.clone(),
    ))?;
    if bundle_export {
        bundles::validate_export(&configuration)?;
    } else {
        rmarkdown::collections(path, &mut configuration)?;
    }
    reject_errors(validate_configuration_with_source(
        &configuration,
        config_name.clone(),
    ))?;
    let paths = resolve_workspace_paths(path, &configuration)?;
    let mut workspace = Workspace {
        name: configuration.project.name.clone(),
        ..Workspace::default()
    };
    let mut selections = DeclaredSourceInputs::default();
    let mut observed = BTreeMap::new();
    let mut cache_observations = CacheObservations::default();
    cache_observations.extend(
        native
            .rust
            .values()
            .filter_map(|result| result.observation.clone()),
    );
    cache_observations.extend(
        native
            .python
            .values()
            .flat_map(|result| result.observations.iter().cloned()),
    );
    cache_observations.extend(
        native
            .julia
            .values()
            .flat_map(|result| result.observations.iter().cloned()),
    );
    for package in &configuration.packages {
        cache_observations.push(crate::incremental::supplemental_manifest(
            &configuration,
            &paths,
            package,
        )?);
    }
    for (index, collection) in configuration.content.iter().enumerate() {
        let repository = &paths.repositories[paths.content[index].repository_index];
        let inputs = collection
            .incremental
            .extra_inputs
            .iter()
            .map(|path| crate::incremental::checked_incremental_path(&repository.path, path))
            .collect::<Result<Vec<_>, _>>()?;
        cache_observations.push(crate::incremental::Manifest::capture(
            &inputs,
            &collection.incremental.environment_variables,
        )?);
    }
    let doxygen_observations = native
        .doxygen
        .values()
        .map(|result| result.observation.clone())
        .collect();
    let r_observations = native
        .r
        .values()
        .map(|result| result.observation.clone())
        .collect();
    let python_interpreter = native
        .python
        .values()
        .next()
        .map(|result| result.interpreter.clone());
    let python_environments = native
        .python
        .iter()
        .filter_map(|((package, _), result)| {
            let environment = result.environment.as_ref()?;
            // Example reuse needs consistent coverage for every target. One
            // target's environment cannot stand in for an opaque loader in another.
            native
                .python
                .iter()
                .filter(|((owner, _), _)| owner == package)
                .all(|(_, result)| {
                    result
                        .environment
                        .as_ref()
                        .is_some_and(|(candidate, _)| candidate == &environment.0)
                })
                .then(|| (package.clone(), environment.clone()))
        })
        .collect();
    let rmd_inputs = rmarkdown::inputs(&paths, &configuration)?;
    for (collection, resolved) in configuration.content.iter().zip(&paths.content) {
        if let Some(files) = rmd_inputs.get(&collection.id) {
            let repository = &paths.repositories[resolved.repository_index];
            selections
                .package_documentation
                .entry(repository.id.clone())
                .or_default()
                .extend(files.iter().cloned());
            for file in files {
                let path = repository.path.join(file);
                observe(
                    &mut observed,
                    &repository.source_location(&path, None)?,
                    fingerprint_bytes(&fs::read(path)?),
                )?;
            }
        }
    }
    let python_doctests = packages::assemble(
        &configuration,
        &paths,
        &mut workspace,
        &mut selections,
        &mut observed,
        &mut native,
        reporting,
    )?;
    let prepared_pages = authored::assemble(
        &configuration,
        &paths,
        &mut workspace,
        &mut selections,
        &mut observed,
        reporting,
    )?;

    let package_files = package_docs::assemble(
        &paths,
        &mut workspace,
        &prepared_pages,
        &mut selections,
        &mut observed,
    )?;

    let evidence = collect_static_provenance(path, &configuration, &selections)?;
    for ((repository, path), fingerprint) in observed {
        if evidence
            .inputs
            .get(&repository)
            .and_then(|inputs| inputs.get(&path))
            != Some(&fingerprint)
        {
            return Err(AssemblyError::InputsChanged);
        }
    }
    workspace.repositories = evidence.repositories.clone();
    let bundles = bundles::ImportedBundles::load(
        &configuration,
        &paths.configuration_directory,
        &mut workspace,
        cancellation,
    )
    .await?;
    documenter::assemble(&mut workspace, &prepared_pages);
    references::concepts_and_relationships(&configuration, &mut workspace, &config_name);
    reject_errors(workspace.diagnostics.iter().cloned().collect())?;
    let result = WorkspaceSources {
        reporting: reporting.clone(),
        workspace,
        configuration,
        // Configuration-relative declarations retain the caller's directory,
        // even when the configuration file itself is a symlink elsewhere.
        configuration_path: std::env::current_dir()?.join(path),
        configuration_source: text,
        paths,
        prepared_pages,
        package_files,
        rmd_inputs,
        doxygen_observations,
        r_observations,
        cache_observations,
        python_interpreter,
        python_doctests,
        python_environments,
        selections,
        evidence,
        bundles,
        bundle_export,
        native_assets: BTreeMap::new(),
    };
    result.revalidate()?;
    Ok(result)
}

fn target_count(configuration: &WorkspaceConfiguration, extractor: &str) -> usize {
    configuration
        .packages
        .iter()
        .flat_map(|package| &package.targets)
        .filter(|target| target.extractor == extractor)
        .count()
}

type ObservedInputs = BTreeMap<(String, DiagnosticPath), Fingerprint>;

fn observe(
    inputs: &mut ObservedInputs,
    source: &SourceLocation,
    fingerprint: Fingerprint,
) -> Result<(), AssemblyError> {
    if inputs
        .insert(
            (source.repository.clone(), source.path.clone()),
            fingerprint.clone(),
        )
        .is_some_and(|old| old != fingerprint)
    {
        return Err(AssemblyError::InputsChanged);
    }
    Ok(())
}

fn reject_errors(diagnostics: Vec<Diagnostic>) -> Result<(), AssemblyError> {
    if diagnostics.iter().any(|d| d.severity == Severity::Error) {
        Err(AssemblyError::Diagnostics(diagnostics))
    } else {
        Ok(())
    }
}

fn diagnostic(code: DiagnosticCode, message: impl Into<String>) -> Diagnostic {
    Diagnostic::new(code, Severity::Error, message)
}

fn portable(path: &Path) -> Result<DiagnosticPath, AssemblyError> {
    DiagnosticPath::try_from(path.to_str().ok_or(AssemblyError::InputsChanged)?)
        .map_err(|_| AssemblyError::InputsChanged)
}

fn relative(path: &Path, base: &Path) -> Result<Option<DiagnosticPath>, AssemblyError> {
    let relative = path
        .strip_prefix(base)
        .map_err(|_| AssemblyError::InputsChanged)?;
    if relative.as_os_str().is_empty() {
        Ok(None)
    } else {
        portable(relative).map(Some)
    }
}

fn source(source: &SourceLocation) -> DiagnosticSource {
    DiagnosticSource::Repository {
        repository: source.repository.clone(),
        path: source.path.clone(),
    }
}