use std::collections::BTreeMap;
use super::assets::PageAssetStore;
use super::identity::{CanonicalError, CanonicalValue};
use super::output_safety::*;
use super::*;
use crate::ir::{CellOutputKind, Fingerprint, OutputRepresentation};
use crate::provenance::fingerprint_bytes;
mod canonical;
mod check;
mod prepared;
#[cfg(test)]
mod tests;
pub use canonical::{RepresentationContent, RepresentationProjection, project_representation};
pub use prepared::PreparedExecution;
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum RecordValidationError {
#[error("Execution evidence does not match its prepared inputs or validated content.")]
Association,
#[error(transparent)]
Asset(#[from] super::assets::AssetError),
}
impl From<CanonicalError> for RecordValidationError {
fn from(_: CanonicalError) -> Self {
Self::Association
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct ValidatedPage {
record: PageExecutionRecord,
slots: BTreeMap<(usize, usize), SlotEvidence>,
diagnostics: Vec<ExecutionDiagnostic>,
diagnostic_offset: usize,
}
#[derive(Debug, Clone, Copy)]
pub enum ValidatedRepresentationRef<'a> {
Text(&'a str),
Markdown(&'a ValidatedMarkdown),
Html(&'a ValidatedHtml),
Asset(&'a ExecutionAsset),
}
#[cfg_attr(
not(target_os = "linux"),
allow(
dead_code,
reason = "The production engine is supported only on Linux."
)
)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum OwnedRepresentation {
Text(String),
Markdown {
value: Box<ValidatedMarkdown>,
origin: OutputOrigin,
},
Html {
value: ValidatedHtml,
},
Asset(ExecutionAsset),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct SlotEvidence {
pub owning_cell: usize,
pub slot: usize,
pub origin: OutputProducer,
pub representations: Vec<OwnedRepresentation>,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct OutputProducer {
pub cell: usize,
pub slot: Option<usize>,
pub cell_span: crate::ir::SourceSpan,
}
impl From<OutputOrigin> for OutputProducer {
fn from(origin: OutputOrigin) -> Self {
Self {
cell: origin.cell,
slot: Some(origin.slot),
cell_span: origin.cell_span,
}
}
}
#[derive(Debug, Default)]
pub(crate) struct DiagnosticEvidence {
pub before: Vec<crate::diagnostics::Diagnostic>,
pub execution: Vec<ExecutionDiagnostic>,
pub after: Vec<crate::diagnostics::Diagnostic>,
}
impl From<Vec<ExecutionDiagnostic>> for DiagnosticEvidence {
fn from(execution: Vec<ExecutionDiagnostic>) -> Self {
Self {
execution,
..Self::default()
}
}
}
impl ValidatedPage {
#[cfg(target_os = "linux")]
pub(crate) fn accept_cache(&mut self, before: Vec<crate::diagnostics::Diagnostic>) {
for cell in &mut self.record.cells {
for output in &mut cell.outputs {
for index in &mut output.diagnostic_indices {
*index += before.len();
}
}
}
self.diagnostic_offset = before.len();
self.record.diagnostics.splice(0..0, before);
if let Some(provenance) = &mut self.record.provenance
&& let crate::ir::ProvenanceActivity::Execution { origin, .. } =
&mut provenance.execution.activity
{
*origin = crate::ir::ExecutionOrigin::Cache;
}
}
pub fn record(&self) -> &PageExecutionRecord {
&self.record
}
pub fn portable_record(&self) -> PageExecutionRecord {
self.record.clone()
}
pub fn representation(
&self,
cell: usize,
slot: usize,
index: usize,
) -> Option<ValidatedRepresentationRef<'_>> {
self.slots
.get(&(cell, slot))?
.representations
.get(index)
.map(|value| match value {
OwnedRepresentation::Text(value) => ValidatedRepresentationRef::Text(value),
OwnedRepresentation::Markdown { value, .. } => {
ValidatedRepresentationRef::Markdown(value)
}
OwnedRepresentation::Html { value, .. } => ValidatedRepresentationRef::Html(value),
OwnedRepresentation::Asset(value) => ValidatedRepresentationRef::Asset(value),
})
}
pub fn referenced_assets(&self) -> impl Iterator<Item = &ExecutionAsset> {
self.record.assets.iter()
}
pub fn diagnostics(&self) -> &[ExecutionDiagnostic] {
&self.diagnostics
}
pub fn execution_diagnostic_offset(&self) -> usize {
self.diagnostic_offset
}
pub fn output_origin(&self, cell: usize, slot: usize) -> Option<&OutputProducer> {
self.slots.get(&(cell, slot)).map(|slot| &slot.origin)
}
pub fn representation_origin(
&self,
cell: usize,
slot: usize,
index: usize,
) -> Option<&OutputOrigin> {
match self.slots.get(&(cell, slot))?.representations.get(index)? {
OwnedRepresentation::Markdown { origin, .. } => Some(origin),
_ => None,
}
}
pub fn canonical_representation(
&self,
cell: usize,
slot: usize,
index: usize,
) -> Result<Option<RepresentationProjection>, CanonicalError> {
let Some(output) = self
.record
.cells
.iter()
.find(|c| c.ordinal == cell)
.and_then(|c| c.outputs.iter().find(|o| o.slot == slot))
else {
return Ok(None);
};
if let Some(value) = self.representation(cell, slot, index) {
return project_representation(match value {
ValidatedRepresentationRef::Text(value) => RepresentationContent::Text(value),
ValidatedRepresentationRef::Markdown(value) => {
RepresentationContent::Markdown(value.canonical_content())
}
ValidatedRepresentationRef::Html(value) => {
RepresentationContent::Html(value.canonical_content())
}
ValidatedRepresentationRef::Asset(value) => {
RepresentationContent::Asset(AssetUse::from(value))
}
})
.map(Some);
}
if index != 0 {
return Ok(None);
}
match &output.output.kind {
CellOutputKind::Error {
name,
message,
traceback,
} => project_representation(RepresentationContent::Error {
name,
value: message,
traceback,
})
.map(Some),
_ if output.unsupported_placeholder().is_some() => project_representation(
RepresentationContent::Unsupported(&output.offered_mime_types),
)
.map(Some),
_ => Ok(None),
}
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct PageExecutionResult {
validated: ValidatedPage,
staged_assets: Vec<StagedExecutionAsset>,
}
impl PageExecutionResult {
#[cfg(target_os = "linux")]
pub(crate) fn append_cache_warnings(&mut self, warnings: Vec<crate::diagnostics::Diagnostic>) {
self.validated.record.diagnostics.extend(warnings);
}
pub fn validated(&self) -> &ValidatedPage {
&self.validated
}
pub fn staged_assets(&self) -> &[StagedExecutionAsset] {
&self.staged_assets
}
#[cfg_attr(
not(target_os = "linux"),
allow(
dead_code,
reason = "The production engine is supported only on Linux."
)
)]
pub(crate) fn retain(
validated: ValidatedPage,
mut store: PageAssetStore,
) -> Result<Self, ExecutionFailure> {
if let Err(error) = store.verify_assets(&validated.record.page, &validated.record.assets) {
let kind = error
.failure_kind()
.unwrap_or(ExecutionFailureKind::OutputValidation);
let mut failure = ExecutionFailure {
kind,
diagnostics: vec![kind.to_diagnostic(
&validated.record.page.collection,
validated.record.page.source.clone(),
)],
cleanup_diagnostics: vec![],
};
if store.rollback().is_err() {
failure
.cleanup_diagnostics
.push(ExecutionFailureKind::Cleanup.to_diagnostic(
&validated.record.page.collection,
validated.record.page.source.clone(),
));
}
return Err(failure);
}
let references: Vec<_> = validated
.record
.assets
.iter()
.map(|a| a.reference.clone())
.collect();
let retained = store.retain(&references)?;
Ok(Self {
validated,
staged_assets: retained.staged_assets,
})
}
}