Resume validated native checkpoints inside included sources

This commit is contained in:
gloamxun committed 2026-11-29 06:04:51 +08:00
1 parent fb5cb5ada9
commit 84ad682939
8 files changed
+537 -88

No files matched your search

@@ -26,6 +26,10 @@ impl DependencyReads {
}
}
pub(super) fn observed_statistics(&self) -> bool {
self.other.contains(&Dependency::ResourceStatistics)
}
pub(super) fn into_ordered(self) -> BTreeSet<Dependency> {
let byte_catcodes = (0..=u8::MAX)
.filter(|&index| {
+110 -31
View File
@@ -305,12 +305,22 @@ impl Default for Executor {
}
}
/// Physical position available for a continuation at a command boundary.
pub struct CheckpointSource<'a> {
pub id: usize,
pub name: &'a str,
pub bytes: usize,
pub frontier: usize,
pub is_entry: bool,
}
/// A live, in-memory kernel continuation at an entry-file command boundary.
/// The host must validate dependencies and replay all prefix effects before
/// resumption. The checkpoint itself validates the entry bytes already loaded.
pub struct EntryCheckpoint {
executor: Executor,
entry: usize,
active_source: usize,
initial_depth: usize,
incoming_state: u64,
frontier: usize,
@@ -331,10 +341,42 @@ impl EntryCheckpoint {
}
pub fn accepts_entry_bytes(&self, name: &str, data: &[u8]) -> bool {
let Some(InputFrame::Source { mouth, .. }) = self.executor.inputs.first() else {
unreachable!("entry checkpoints start with the physical source frame");
};
self.executor.sources[self.entry].name == name && mouth.matches_loaded_prefix(data)
let entry = &self.executor.sources[self.entry];
// A suspended scanner may only accept unchanged bytes when its
// boundary cannot be authenticated as a physical line.
entry.name == name && (entry.data == data || self.matches_scanned_source(self.entry, data))
}
pub fn entry_frontier(&self) -> usize {
self.source_mouth(self.entry).physical_frontier().unwrap_or(0)
}
pub fn nested_source_id(&self) -> Option<usize> {
(self.active_source != self.entry).then_some(self.active_source)
}
pub fn nested_source(&self) -> Option<&str> {
(self.active_source != self.entry)
.then(|| self.executor.sources[self.active_source].name.as_str())
}
pub fn accepts_nested_source(&self, name: &str, data: &[u8]) -> bool {
self.nested_source() == Some(name)
&& self.matches_scanned_source(self.active_source, data)
}
fn matches_scanned_source(&self, id: usize, data: &[u8]) -> bool {
// A statistics event can already include the complete source length.
(self.executor.sources[id].data.len() == data.len()
|| !self.executor.reads.observed_statistics())
&& self.source_mouth(id).matches_loaded_prefix(data)
}
fn source_mouth(&self, id: usize) -> &Mouth {
self.executor.inputs.iter().rev().find_map(|frame| match frame {
InputFrame::Source { source, mouth, .. } if *source == id => Some(mouth),
_ => None,
}).expect("checkpoint retains the active scanner")
}
pub fn resume_after_host_replay(
@@ -343,21 +385,23 @@ impl EntryCheckpoint {
files: &mut impl SourceProvider,
cancellation: Cancellation,
) -> Option<Result<Execution, ExecutionFailure>> {
self.resume_observed(source, files, cancellation, None, None, |_, _, _, _| {})
self.resume_observed(source, None, files, cancellation, None, None, |_, _, _, _| {})
}
pub(super) fn resume_capturing_entries_after_host_replay<F: SourceProvider, S>(
pub(super) fn resume_capturing_sources_after_host_replay<F: SourceProvider, S>(
&self,
source: SourceFile,
nested: Option<SourceFile>,
files: &mut F,
cancellation: Cancellation,
line_break_cache: std::sync::Arc<super::LineBreakCache>,
previous: Option<&Execution>,
select: impl FnMut(usize, usize) -> Option<S>,
select: impl FnMut(CheckpointSource<'_>, usize) -> Option<S>,
capture: impl FnMut(S, EntryCheckpoint, &mut F),
) -> Option<Result<Execution, ExecutionFailure>> {
self.resume_observed(
source,
nested,
files,
cancellation,
Some(line_break_cache),
@@ -369,22 +413,27 @@ impl EntryCheckpoint {
fn resume_observed<F: SourceProvider>(
&self,
source: SourceFile,
nested: Option<SourceFile>,
files: &mut F,
cancellation: Cancellation,
line_break_cache: Option<std::sync::Arc<super::LineBreakCache>>,
previous: Option<&Execution>,
capture_boundary: impl FnMut(&mut Executor, usize, bool, &mut F),
) -> Option<Result<Execution, ExecutionFailure>> {
if !self.accepts_entry(&source) {
if !self.accepts_entry(&source)
|| match &nested {
Some(nested) => !self.accepts_nested_source(&nested.name, &nested.data),
None => self.nested_source().is_some(),
}
{
return None;
}
let mut executor = self.executor.clone().with_cancellation(cancellation);
executor.line_break_cache = line_break_cache;
let Some(InputFrame::Source { mouth, .. }) = executor.inputs.first_mut() else {
unreachable!("entry checkpoints start with the physical source frame");
};
mouth.rebind_loaded_prefix(source.data.clone());
executor.sources[self.entry] = Arc::new(source);
executor.rebind_checkpoint_source(self.entry, source);
if let Some(nested) = nested {
executor.rebind_checkpoint_source(self.active_source, nested);
}
let result = executor
.check_cancellation()
.and_then(|_| {
@@ -401,6 +450,17 @@ impl EntryCheckpoint {
}
impl Executor {
fn rebind_checkpoint_source(&mut self, id: usize, file: SourceFile) {
let mouth = self.inputs.iter_mut().rev().find_map(|frame| match frame {
InputFrame::Source { source, mouth, .. } if *source == id => Some(mouth),
_ => None,
}).expect("checkpoint retains the active scanner");
if self.sources[id].as_ref() != &file {
mouth.rebind_loaded_prefix(file.data.clone());
self.sources[id] = Arc::new(file);
}
}
pub fn with_byte_translation(mut self, table: ByteTranslation) -> Self {
self.state.byte_translation = Some(Arc::new(table));
self
@@ -3749,10 +3809,24 @@ impl Executor {
/// Observe eligible boundaries during normal execution. Only clone the
/// state when the caller selects a boundary, giving it its matching host.
pub fn run_capturing_entries<F: SourceProvider, S>(
self,
file: SourceFile,
files: &mut F,
mut select: impl FnMut(usize, usize) -> Option<S>,
capture: impl FnMut(S, EntryCheckpoint, &mut F),
) -> Result<Execution, ExecutionFailure> {
self.run_capturing_sources(
file, files,
|source, pages| source.is_entry.then(|| select(source.frontier, pages)).flatten(),
capture,
)
}
pub fn run_capturing_sources<F: SourceProvider, S>(
mut self,
file: SourceFile,
files: &mut F,
select: impl FnMut(usize, usize) -> Option<S>,
select: impl FnMut(CheckpointSource<'_>, usize) -> Option<S>,
capture: impl FnMut(S, EntryCheckpoint, &mut F),
) -> Result<Execution, ExecutionFailure> {
let incoming_state = self.state.fingerprint();
@@ -3766,7 +3840,7 @@ impl Executor {
fn entry_observer<F: SourceProvider, S>(
incoming_state: u64,
mut select: impl FnMut(usize, usize) -> Option<S>,
mut select: impl FnMut(CheckpointSource<'_>, usize) -> Option<S>,
mut capture: impl FnMut(S, EntryCheckpoint, &mut F),
) -> impl FnMut(&mut Self, usize, bool, &mut F) {
move |engine, initial_depth, pages_processed, files| {
@@ -3774,35 +3848,39 @@ impl Executor {
return;
}
let Some(InputFrame::Source {
source,
mouth,
eof_seen: false,
pseudo_trace: None,
..
source: entry, eof_seen: false, pseudo_trace: None, ..
}) = engine.inputs.first()
else {
return;
};
// Retain the active scanner plus its suspended parents. A pseudo
// input or an EOF hook has no replaceable physical byte prefix.
let Some(InputFrame::Source {
source, mouth, eof_seen: false, pseudo_trace: None, ..
}) = engine.inputs.iter().rev().find(|frame| matches!(frame, InputFrame::Source { .. }))
else {
return;
};
let Some(frontier) = mouth.physical_frontier() else {
return;
};
// Keep the active macro and argument cursors, but a second
// physical source would have an independent unread frontier.
if engine.inputs[1..]
.iter()
.any(|frame| matches!(frame, InputFrame::Source { .. }))
{
return;
}
let Some(slot) = select(frontier, engine.shipped_pages) else {
let (entry, active_source) = (*entry, *source);
let file = &engine.sources[active_source];
let position = CheckpointSource {
id: active_source,
name: &file.name,
bytes: file.data.len(),
frontier,
is_entry: active_source == entry,
};
let Some(slot) = select(position, engine.shipped_pages) else {
return;
};
if engine.sources.iter().enumerate().any(|(index, other)| {
index != *source && other.name == engine.sources[*source].name
index != active_source && other.name == file.name
}) {
return;
}
let entry = *source;
engine.events.seal_prefix();
let mut executor = engine.clone();
executor.cancellation = None;
@@ -3810,6 +3888,7 @@ impl Executor {
let checkpoint = EntryCheckpoint {
executor,
entry,
active_source,
initial_depth,
incoming_state,
frontier,
+1 -1
View File
@@ -68,7 +68,7 @@ pub use case::CaseCode;
pub use context::{ContextFrame, ContextKind, InputContext};
pub use device::{NativeShipout, ShipoutDevice, ShipoutSpecial};
pub use diagnostics::{DiagnosticBox, DiagnosticNode, ShowBoxReport};
pub use engine::{EntryCheckpoint, Executor, Limits};
pub use engine::{CheckpointSource, EntryCheckpoint, Executor, Limits};
pub use font_catalog::{
FontCatalog, FontCharacter, FontId, FontInstance, FontLimits, FontResource,
};
@@ -222,6 +222,21 @@ impl RevisionJob {
self.finish_execution(executed, files, None)
}
/// Capture selected physical source continuations, including child input.
pub fn run_capturing_sources<F: SourceProvider, S>(
self,
executor: Executor,
source: SourceFile,
files: &mut F,
select: impl FnMut(super::CheckpointSource<'_>, usize) -> Option<S>,
capture: impl FnMut(S, EntryCheckpoint, &mut F),
) -> Result<Arc<RevisionExecution>, RevisionFailure> {
let executed = executor
.with_cancellation(self.cancellation.clone())
.run_capturing_sources(source, files, select, capture);
self.finish_execution(executed, files, None)
}
/// Resume after the host has validated prefix dependencies and replayed
/// their effects into the new revision's isolated file host.
pub fn resume_entry_after_host_replay(
@@ -245,10 +260,29 @@ impl RevisionJob {
line_break_cache: Arc<LineBreakCache>,
select: impl FnMut(usize, usize) -> Option<S>,
capture: impl FnMut(S, EntryCheckpoint, &mut F),
) -> Option<Result<Arc<RevisionExecution>, RevisionFailure>> {
let mut select = select;
self.resume_capturing_sources_after_host_replay(
checkpoint, source, None, files, line_break_cache,
|source, pages| source.is_entry.then(|| select(source.frontier, pages)).flatten(),
capture,
)
}
pub fn resume_capturing_sources_after_host_replay<F: SourceProvider, S>(
self,
checkpoint: &EntryCheckpoint,
source: SourceFile,
nested: Option<SourceFile>,
files: &mut F,
line_break_cache: Arc<LineBreakCache>,
select: impl FnMut(super::CheckpointSource<'_>, usize) -> Option<S>,
capture: impl FnMut(S, EntryCheckpoint, &mut F),
) -> Option<Result<Arc<RevisionExecution>, RevisionFailure>> {
let previous = self.session.latest();
let executed = checkpoint.resume_capturing_entries_after_host_replay(
let executed = checkpoint.resume_capturing_sources_after_host_replay(
source,
nested,
files,
self.cancellation.clone(),
line_break_cache,