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
use std::collections::BTreeMap;
use std::path::PathBuf;

use super::local::{
    canonical, digest, exact_version, fingerprint, normalize_language, read_declared, roots,
};
use super::{
    BuildObservation, CanonicalValue, IdentityError, LaunchIdentityInput, RepositoryFile,
    SourceSnapshot, content_digest,
};
use crate::configuration::{
    ContentConfiguration, ExecutionConfiguration, ExecutionEngine, ExecutionMode,
};
use crate::documents::{AuthoredFormat, QmdPreparation, prepare_collection_document};
use crate::execution::{
    EffectiveCellOptions, ExecutionDeadlines, ExecutionDefaults, ExecutionFailure,
    ExecutionFailureKind, ExecutionPolicies, OutputVisibility, PageExecutionRequest,
};
use crate::ir::{InputFingerprint, SourceLocation};
use serde_json::{Value, json};

/// Explicit local input facts supplied by the engine adapter.
#[derive(Clone)]
pub struct IdentityInputs {
    /// Declared canonical repository roots.
    pub repositories: BTreeMap<String, PathBuf>,
    /// The authored source declaration.
    pub page: RepositoryFile,
    /// Explicit declarations, not cached digest assertions.
    pub declared_files: Vec<RepositoryFile>,
    /// Running engine and build observations.
    pub build: BuildObservation,
    /// The same resolved facts that will be used for spawning.
    pub launch: LaunchIdentityInput,
}
impl std::fmt::Debug for IdentityInputs {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("IdentityInputs { <private> }")
    }
}
/// Exact validated kernel-info fields observed from the current live session.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RuntimeObservation {
    /// Kernel implementation name.
    pub implementation: String,
    /// Exact implementation version.
    pub implementation_version: String,
    /// Runtime language, normalized only by the documented aliases.
    pub language: String,
    /// Exact language version.
    pub language_version: String,
    /// Exact protocol version, including the minor version.
    pub protocol_version: String,
}
/// Immutable local capture of prepared source and declared input bytes.
///
/// ```compile_fail
/// use diplodocus::execution::identity::InputSnapshot;
/// fn serialize<T: serde::Serialize>() {}
/// serialize::<InputSnapshot>();
/// ```
pub struct InputSnapshot {
    inputs: IdentityInputs,
    request: PageExecutionRequest,
    source: SourceSnapshot,
    files: Vec<(RepositoryFile, Vec<u8>)>,
    environment: Vec<InputFingerprint>,
}
impl std::fmt::Debug for InputSnapshot {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("InputSnapshot { <private> }")
    }
}
/// Immutable key constructed from a bound snapshot and current observations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExecutionIdentity {
    value: CanonicalValue,
    key: String,
}
impl ExecutionIdentity {
    /// Portable exact key input, shared by cache and provenance projections.
    pub fn key_input(&self) -> &CanonicalValue {
        &self.value
    }
    /// Domain-separated page key.
    pub fn key(&self) -> &str {
        &self.key
    }
}
fn failure(request: &PageExecutionRequest) -> ExecutionFailure {
    let kind = ExecutionFailureKind::InputChanged;
    ExecutionFailure {
        kind,
        diagnostics: vec![
            kind.to_diagnostic(&request.page.collection, request.page.source.clone()),
        ],
        cleanup_diagnostics: vec![],
    }
}
/// Capture and bind source and declared inputs before any kernel launch.
/// Caller digest assertions are checked against freshly read declared bytes.
pub async fn snapshot_inputs(
    inputs: IdentityInputs,
    request: &PageExecutionRequest,
    prepared_source: &[u8],
) -> Result<InputSnapshot, ExecutionFailure> {
    let source = SourceSnapshot::read(&inputs.repositories, &inputs.page, None)
        .await
        .map_err(|_| failure(request))?;
    if source.bytes != prepared_source {
        return Err(failure(request));
    }
    snapshot_source_inputs(inputs, request, source).await
}

pub(crate) async fn snapshot_source_inputs(
    inputs: IdentityInputs,
    request: &PageExecutionRequest,
    source: SourceSnapshot,
) -> Result<InputSnapshot, ExecutionFailure> {
    capture(inputs, request, source)
        .await
        .map_err(|_| failure(request))
}
async fn capture(
    inputs: IdentityInputs,
    request: &PageExecutionRequest,
    source: SourceSnapshot,
) -> Result<InputSnapshot, IdentityError> {
    roots(&inputs.repositories).await?;
    validate_build(&inputs.build)?;
    if inputs.launch.resolver().repositories != inputs.repositories
        || inputs.page.repository != request.page.source.repository
        || inputs.page.path != request.page.source.path
        || inputs.launch.resolver().repository != inputs.page.repository
        || inputs.launch.selector() != request.kernel.to_ascii_lowercase()
    {
        return Err(IdentityError);
    }
    let parent = std::path::Path::new(inputs.page.path.as_str())
        .parent()
        .and_then(|p| p.to_str())
        .filter(|s| !s.is_empty())
        .unwrap_or(".");
    if inputs.launch.resolver().working_directory != parent
        || request
            .page
            .working_directory
            .as_ref()
            .map_or(".", |p| p.as_str())
            != parent
    {
        return Err(IdentityError);
    }
    source
        .revalidate(&inputs.repositories, &inputs.page)
        .await?;
    if digest(&request.page.source_fingerprint)? != content_digest(&source.bytes) {
        return Err(IdentityError);
    }
    validate_prepared(request, &source.bytes)?;
    inputs.launch.revalidate().await?;
    let mut declarations = inputs.declared_files.clone();
    declarations.sort();
    if declarations.windows(2).any(|pair| pair[0] == pair[1]) {
        return Err(IdentityError);
    }
    let mut files = Vec::new();
    let mut environment = Vec::new();
    for declaration in declarations {
        let bytes = read_declared(&inputs.repositories, &declaration).await?;
        environment.push(InputFingerprint {
            source: SourceLocation {
                repository: declaration.repository.clone(),
                path: declaration.path.clone(),
                span: None,
            },
            fingerprint: fingerprint(&content_digest(&bytes))?,
        });
        files.push((declaration, bytes));
    }
    let mut request = request.clone();
    sort_environment(&mut request.declared_environment_inputs);
    if request.declared_environment_inputs != environment {
        return Err(IdentityError);
    }
    Ok(InputSnapshot {
        inputs,
        request,
        source,
        files,
        environment,
    })
}
fn sort_environment(environment: &mut [InputFingerprint]) {
    environment.sort_by(|a, b| {
        (&a.source.repository, &a.source.path).cmp(&(&b.source.repository, &b.source.path))
    });
}
pub(crate) fn validate_prepared(
    request: &PageExecutionRequest,
    source: &[u8],
) -> Result<QmdPreparation, IdentityError> {
    if request.page.format != AuthoredFormat::Qmd
        || request.page.mode != ExecutionMode::Execute
        || request.page.page_veto
        || request.page.source.span.is_some()
        || request.page.collection.is_empty()
        || request.page.qmd_policy != "qmd-mvp-v1"
        || request.page.parser_version != crate::provenance::PANACHE_VERSION
    {
        return Err(IdentityError);
    }
    let collection = ContentConfiguration {
        id: request.page.collection.clone(),
        owner: "project".into(),
        repository: request.page.source.repository.clone(),
        path: ".".into(),
        mount: String::new(),
        format: AuthoredFormat::Qmd,
        navigation: Default::default(),
        incremental: Default::default(),
        execution: ExecutionConfiguration {
            mode: ExecutionMode::Execute,
            engine: Some(ExecutionEngine::Jupyter),
            kernel: Some(request.kernel.clone()),
            julia_project: None,
            declared_environment_inputs: Vec::new(),
        },
    };
    let prepared = prepare_collection_document(
        std::str::from_utf8(source).map_err(|_| IdentityError)?,
        &collection,
    )
    .map_err(|_| IdentityError)?
    .preparation
    .ok_or(IdentityError)?;
    if !prepared.execution_eligible
        || prepared.page_veto != request.page.page_veto
        || prepared.defaults != request.defaults
        || prepared.cells != request.cells
    {
        return Err(IdentityError);
    }
    Ok(prepared)
}
fn validate_build(build: &BuildObservation) -> Result<(), IdentityError> {
    digest(&build.executable_digest)?;
    let roles = [
        "async-runtime",
        "authored-parser",
        "fragment-parser",
        "html-parser",
        "html-sanitizer",
        "jupyter-protocol",
        "jupyter-transport",
        "raster-decoder",
        "raster-validator",
        "svg-parser",
        "svg-validator",
    ];
    if roles.iter().any(|r| !build.components.contains_key(*r))
        || !exact_version(&build.engine_version)
        || build.components.iter().any(|(role, component)| {
            role.is_empty() || component.name.is_empty() || !exact_version(&component.version)
        })
        || build.platform.os != "linux"
        || build.platform.architecture.is_empty()
        || build.platform.target.is_empty()
    {
        return Err(IdentityError);
    }
    Ok(())
}
impl InputSnapshot {
    /// Complete identity after readiness with the current kernel information.
    pub fn identity(
        &self,
        runtime: &RuntimeObservation,
        request: &PageExecutionRequest,
        deadlines: &ExecutionDeadlines,
    ) -> Result<ExecutionIdentity, ExecutionFailure> {
        self.make_identity(runtime, request, deadlines)
            .map_err(|_| failure(request))
    }
    fn make_identity(
        &self,
        runtime: &RuntimeObservation,
        request: &PageExecutionRequest,
        deadlines: &ExecutionDeadlines,
    ) -> Result<ExecutionIdentity, IdentityError> {
        let mut request = request.clone();
        sort_environment(&mut request.declared_environment_inputs);
        if request != self.request {
            return Err(IdentityError);
        }
        if [
            &runtime.implementation,
            &runtime.implementation_version,
            &runtime.language,
            &runtime.language_version,
            &runtime.protocol_version,
        ]
        .iter()
        .any(|s| s.trim().is_empty() || s.contains('\0'))
            || normalize_language(&runtime.language) != self.inputs.launch.language()
        {
            return Err(IdentityError);
        }
        let protocol: Vec<_> = runtime.protocol_version.split('.').collect();
        if protocol.len() != 2 || protocol[0] != "5" || protocol[1].parse::<u64>().is_err() {
            return Err(IdentityError);
        }
        let limits = [
            deadlines.startup,
            deadlines.cell,
            deadlines.terminal_sync,
            deadlines.interrupt,
            deadlines.shutdown,
            deadlines.termination,
            deadlines.forced_exit,
        ];
        if limits.contains(&0) {
            return Err(IdentityError);
        }
        let cells:Vec<_>=request.cells.iter().map(|cell| {
            let language=cell.cell.language.as_ref().map(|s|normalize_language(s));
            let eligible=language.as_deref()==Some(self.inputs.launch.language()) && cell.options.execution.eval.value;
            json!({"ordinal":cell.ordinal,"language":language,"eligible":eligible,"submitted_source_digest":eligible.then(||content_digest(cell.cell.source.as_bytes())),"effective":effective(&cell.options)})
        }).collect();
        if !cells.iter().any(|c| c["eligible"] == true) {
            return Err(IdentityError);
        }
        let build = &self.inputs.build;
        let components: Vec<_> = build
            .components
            .iter()
            .map(|(role, c)| json!({"role":role,"name":c.name,"version":c.version}))
            .collect();
        let environment:Vec<_>=self.environment.iter().map(|e|Ok(json!({"repository":e.source.repository,"path":e.source.path,"digest":digest(&e.fingerprint)?}))).collect::<Result<_,IdentityError>>()?;
        let policies = ExecutionPolicies::default();
        let mut value = json!({
            "schema":"page-execution-key-v1","schemas":{"encoding":"execution-json-v1","artifact":"page-execution-artifact-v1","ir":"execution-result-v1"},
            "page":{"repository":request.page.source.repository,"collection":request.page.collection,"path":request.page.source.path,"working_directory":request.page.working_directory.as_ref().map_or(".",|p|p.as_str()),"format":"qmd","source_digest":digest(&request.page.source_fingerprint)?},
            "options":{"mode":"execute","page_veto":false,"defaults":defaults(&request.defaults),"cells":cells},
            "engine":{"id":"jupyter","version":build.engine_version,"build_digest":digest(&build.executable_digest)?},
            "policies":{"qmd":request.page.qmd_policy,"execution":policies.execution,"mime":policies.mime,"html":policies.html,"svg":policies.svg},
            "components":components,"platform":build.platform,"deadlines_ms":deadlines,"environment_inputs":environment,
            "kernel":{"name":self.inputs.launch.selector(),"spec_digest":self.inputs.launch.spec_digest(),"launch_digest":self.inputs.launch.launch_digest(),"search":self.inputs.launch.search(),"interrupt_mode":self.inputs.launch.interrupt_mode(),"runtime":{"implementation":runtime.implementation,"implementation_version":runtime.implementation_version,"language":normalize_language(&runtime.language),"language_version":runtime.language_version,"protocol_version":runtime.protocol_version}}
        });
        if self.source.has_includes() {
            value["schema"] = json!("page-execution-key-v2");
            value["page"]["collection_root"] = json!(
                self.source
                    .collection_root(&self.inputs.repositories[&self.inputs.page.repository],)?
                    .as_ref()
                    .map_or(".", |path| path.as_str())
            );
            value["page"]["source_inputs"] = json!(self.source.inputs.iter().map(|(file, bytes)| {
                json!({"repository":file.repository,"path":file.path,"digest":content_digest(bytes)})
            }).collect::<Vec<_>>());
        }
        let value = canonical(value)?;
        let key = super::key_digest(&value).map_err(|_| IdentityError)?;
        Ok(ExecutionIdentity { value, key })
    }
    /// Read-only observed environment evidence for the final prepared request.
    pub fn environment_inputs(&self) -> &[InputFingerprint] {
        &self.environment
    }
    /// Revalidate after cleanup before accepting either execution or restoration.
    pub async fn revalidate(&self, launch: &LaunchIdentityInput) -> Result<(), ExecutionFailure> {
        self.recheck(launch)
            .await
            .map_err(|_| failure(&self.request))
    }
    async fn recheck(&self, launch: &LaunchIdentityInput) -> Result<(), IdentityError> {
        roots(&self.inputs.repositories).await?;
        self.source
            .revalidate(&self.inputs.repositories, &self.inputs.page)
            .await?;
        for (file, bytes) in &self.files {
            if read_declared(&self.inputs.repositories, file).await? != *bytes {
                return Err(IdentityError);
            }
        }
        self.inputs.launch.revalidate().await?;
        launch.revalidate().await?;
        if launch.resolver().repositories != self.inputs.repositories
            || !self.inputs.launch.same_observation(launch)
        {
            return Err(IdentityError);
        }
        Ok(())
    }
}
fn defaults(options: &ExecutionDefaults) -> Value {
    json!({"eval":options.eval.value,"echo":options.echo.value,"output":match options.output.value {OutputVisibility::Show=>json!(true),OutputVisibility::Hide=>json!(false),OutputVisibility::AsIs=>json!("asis")},"include":options.include.value,"error":options.error.value})
}
fn effective(options: &EffectiveCellOptions) -> Value {
    let mut value = defaults(&options.execution);
    let object = value.as_object_mut().unwrap();
    object.insert("label".into(), json!(options.label.value));
    object.insert("fig-alt".into(), json!(options.fig_alt.value));
    object.insert("fig-cap".into(), json!(options.fig_cap.value));
    object.insert("fig-subcap".into(), json!(options.fig_subcap.value));
    value
}