Resume native editor jobs through verified checkpoints

This commit is contained in:
gloamxun committed 2026-11-19 09:35:33 +08:00
1 parent 31364ae33e
commit 0abf700817
19 files changed
+2311 -93

No files matched your search

+157 -14
View File
@@ -21,18 +21,18 @@ mod math;
mod nesting;
mod page;
mod paragraphs;
mod pdf_color;
mod pdf_catalog;
mod pdf_color;
mod pdf_graphics;
mod pdf_links;
mod pdf_image;
mod pdf_links;
mod quantities;
mod restores;
mod scanning;
mod scheduling;
mod statistics;
#[derive(Clone, Copy, Debug, serde::Serialize, serde::Deserialize)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Limits {
pub steps: usize,
pub tokens: usize,
@@ -65,6 +65,7 @@ impl From<Token> for InputToken {
}
}
}
#[derive(Clone)]
enum InputFrame {
Source {
source: usize,
@@ -110,6 +111,7 @@ enum TheValue {
Tokens(Vec<Token>),
Text(std::string::String),
}
#[derive(Clone)]
struct Conditional {
primitive: Primitive,
inverted: bool,
@@ -137,6 +139,7 @@ impl ConditionalLimit {
}
}
#[derive(Clone)]
pub struct Executor {
state: State,
limits: Limits,
@@ -197,6 +200,60 @@ impl Default for Executor {
}
}
/// 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,
initial_depth: usize,
incoming_state: u64,
frontier: usize,
}
impl EntryCheckpoint {
pub fn frontier(&self) -> usize {
self.frontier
}
pub fn completed_steps(&self) -> usize {
self.executor.steps
}
pub fn accepts_entry(&self, source: &SourceFile) -> bool {
self.accepts_entry_bytes(&source.name, &source.data)
}
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)
}
pub fn resume_after_host_replay(
&self,
source: SourceFile,
files: &mut impl SourceProvider,
cancellation: Cancellation,
) -> Option<Result<Execution, ExecutionFailure>> {
if !self.accepts_entry(&source) {
return None;
}
let mut executor = self.executor.clone().with_cancellation(cancellation);
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] = source;
let result = executor
.check_cancellation()
.and_then(|_| executor.run_active_input(self.initial_depth, files, |_, _, _| {}));
let result = executor.finish_observed_input(result, files);
Some(executor.finish_execution(result, self.incoming_state))
}
}
impl Executor {
// Native jobs enforce the no-shell-escape policy for immediate and shipped writes.
const SHELL_ESCAPE: i32 = 0;
@@ -541,7 +598,9 @@ impl Executor {
Ok(Some(input))
} else {
match self.meaning(&input.token) {
Meaning::Primitive(Primitive::Expandafter) => self.expand_after(&input.token, files),
Meaning::Primitive(Primitive::Expandafter) => {
self.expand_after(&input.token, files)
}
meaning => self.expand_one_inner(input, meaning, files, definition_context),
}
};
@@ -2874,7 +2933,72 @@ impl Executor {
files: &mut impl SourceProvider,
) -> Result<Execution, ExecutionFailure> {
let incoming_state = self.state.fingerprint();
let result = self.run_observed(file, files);
let result = self.run_observed(file, files, |_, _, _| {});
self.finish_execution(result, incoming_state)
}
/// Capture the first eligible entry command boundary at or beyond the
/// requested physical byte. The callback captures the matching host state.
pub fn run_capturing_entry<F: SourceProvider, H>(
mut self,
file: SourceFile,
files: &mut F,
minimum_frontier: usize,
capture_host: impl FnOnce(&mut F) -> H,
) -> (
Result<Execution, ExecutionFailure>,
Option<(EntryCheckpoint, H)>,
) {
let incoming_state = self.state.fingerprint();
let mut capture_host = Some(capture_host);
let mut captured = None;
let result = self.run_observed(file, files, |engine, initial_depth, files| {
if capture_host.is_none() || engine.job_ended {
return;
}
let Some(InputFrame::Source {
source,
mouth,
eof_seen: false,
pseudo_trace: None,
..
}) = engine.inputs.first()
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 frontier < minimum_frontier
|| engine.inputs[1..]
.iter()
.any(|frame| matches!(frame, InputFrame::Source { .. }))
|| engine.sources.iter().enumerate().any(|(index, other)| {
index != *source && other.name == engine.sources[*source].name
})
{
return;
}
let mut executor = engine.clone();
executor.cancellation = None;
executor.line_break_cache = None;
let checkpoint = EntryCheckpoint {
executor,
entry: *source,
initial_depth,
incoming_state,
frontier,
};
captured = Some((checkpoint, capture_host.take().unwrap()(files)));
});
(self.finish_execution(result, incoming_state), captured)
}
fn finish_execution(
mut self,
result: Result<(), Error>,
incoming_state: u64,
) -> Result<Execution, ExecutionFailure> {
self.collect_meaning_reads();
if let Err(error) = result {
use sha2::{Digest, Sha256};
@@ -2932,17 +3056,25 @@ impl Executor {
steps: self.steps,
})
}
fn run_observed(
fn run_observed<F: SourceProvider>(
&mut self,
file: SourceFile,
files: &mut impl SourceProvider,
files: &mut F,
capture_boundary: impl FnMut(&Self, usize, &mut F),
) -> Result<(), Error> {
self.check_cancellation()?;
self.change_interaction_mode(self.interaction_mode, files)?;
let result = self
.run_input(file, files)
.and_then(|()| self.finish_pdf_catalog());
if let Err(mut error) = result {
let result = self.prepare_input(file, files).and_then(|initial_depth| {
self.run_active_input(initial_depth, files, capture_boundary)
});
self.finish_observed_input(result, files)
}
fn finish_observed_input(
&mut self,
result: Result<(), Error>,
files: &mut impl SourceProvider,
) -> Result<(), Error> {
if let Err(mut error) = result.and_then(|()| self.finish_pdf_catalog()) {
if let Err(sink) = files.observe_error(&error) {
error
.message
@@ -2966,11 +3098,11 @@ impl Executor {
self.check_cancellation()?;
Ok(())
}
fn run_input(
fn prepare_input(
&mut self,
file: SourceFile,
files: &mut impl SourceProvider,
) -> Result<(), Error> {
) -> Result<usize, Error> {
if self.job_name.is_none() {
self.job_name = std::path::Path::new(&file.name)
.file_stem()
@@ -3021,7 +3153,16 @@ impl Executor {
self.push_mode_hook(TokenParameter::Everyjob)?;
}
}
Ok(initial_depth)
}
fn run_active_input<F: SourceProvider>(
&mut self,
initial_depth: usize,
files: &mut F,
mut capture_boundary: impl FnMut(&Self, usize, &mut F),
) -> Result<(), Error> {
loop {
capture_boundary(self, initial_depth, files);
self.process_native_pages(files)?;
let Some(input) = self.expanded(files)? else {
break;
@@ -3713,7 +3854,9 @@ impl Executor {
},
)?;
}
Some(p @ (Chardef | Mathchardef | Countdef | Toksdef | Dimendef | Skipdef | Muskipdef)) => {
Some(
p @ (Chardef | Mathchardef | Countdef | Toksdef | Dimendef | Skipdef | Muskipdef),
) => {
self.define_shorthand(p, global, files)?;
}
Some(Dimen | Skip | Muskip) => {