Batch draft transcripts and reuse verified source versions

This commit is contained in:
gloamxun committed 2026-11-29 01:11:42 +08:00
1 parent 373204a8d8
commit c6b5c9ed56
4 files changed
+108 -47

No files matched your search

+45 -20
View File
@@ -12,7 +12,7 @@ use std::{
collections::{BTreeMap, BTreeSet},
ffi::OsStr,
fs::{self, File, OpenOptions},
io::{self, BufRead, BufReader, Seek, Write},
io::{self, BufRead, BufReader, BufWriter, Seek, Write},
path::{Path, PathBuf},
sync::{
Arc,
@@ -167,7 +167,7 @@ pub struct Files {
next_handle: u64,
stream_io: StreamJournal,
log_path: PathBuf,
log: Option<File>,
log: Option<BufWriter<File>>,
draft: Option<DraftEffects>,
protected: Vec<PathBuf>,
producer: PathBuf,
@@ -186,6 +186,13 @@ pub struct Files {
preparation: HostPreparation,
runtime_observations: RefCell<HostPreparation>,
}
fn transcript_writer(file: File, draft: bool) -> BufWriter<File> {
// Draft bytes remain private until cleanup and publication. Live jobs
// keep reporting actual writes to the transcript during execution.
let capacity = if draft { 8192 } else { 0 };
BufWriter::with_capacity(capacity, file)
}
fn output_column(previous: usize, text: &str) -> usize {
match text.rsplit_once('\n') {
Some((_, tail)) => tail.chars().count(),
@@ -561,7 +568,7 @@ impl Files {
next_handle: 0,
stream_io: StreamJournal::default(),
log_path,
log: Some(log),
log: Some(transcript_writer(log, draft.is_some())),
draft,
protected,
producer: source.to_owned(),
@@ -890,6 +897,20 @@ impl Files {
.map(PathBuf::as_path),
)
}
pub(crate) fn read_versioned_source(
&mut self,
name: &str,
from: &str,
) -> Result<(SourceFile, ResourceVersion), String> {
let resource = self.world.read_tex_input(name, Path::new(from))?;
self.observe_resource(ResourceKind::Tex, &resource);
let source = SourceFile {
name: resource.version.path.display().to_string(),
data: resource.data,
};
Ok((source, resource.version))
}
fn observe_resource(&mut self, kind: ResourceKind, resource: &LoadedResource) {
let path = &resource.version.path;
if kind == ResourceKind::Tex && self.ledger.owns(path) && !self.is_protected(path) {
@@ -1081,14 +1102,12 @@ impl Files {
};
if destination != WriteDestination::TerminalOnly {
if self.log.is_none() {
self.log = Some(
File::create(
self.draft
.as_ref()
.map_or(&self.log_path, |draft| &draft.log.staged.path),
)
.map_err(|e| format!("native log {}: {e}", self.log_path.display()))?,
);
let path = self.draft
.as_ref()
.map_or(&self.log_path, |draft| &draft.log.staged.path);
let file = File::create(path)
.map_err(|e| format!("native log {}: {e}", self.log_path.display()))?;
self.log = Some(transcript_writer(file, self.draft.is_some()));
}
let log = self.log.as_mut().unwrap();
if start_line && self.log_column != 0 {
@@ -1189,11 +1208,16 @@ impl Files {
queue: &DraftSyncQueue,
cancellation: &Cancellation,
) -> Result<Option<DraftPublication>, String> {
let draft = self.draft.take().ok_or("native draft host required")?;
let draft = self.draft.as_ref().ok_or("native draft host required")?;
if !draft.finished {
return Err("native draft must finish successfully before publication".into());
}
let log = self.log.take();
let draft = self.draft.take().expect("finished draft");
let log = self.log.take().map(|log| {
let (file, buffer) = log.into_parts();
debug_assert!(buffer.is_ok_and(|bytes| bytes.is_empty()));
file
});
let initial = self
.preparation
.generated_initial
@@ -1441,6 +1465,13 @@ impl PendingDraft {
impl Drop for Files {
fn drop(&mut self) {
if self.draft.is_some() {
// A discarded draft has no reader. Drop pending bytes without
// triggering BufWriter's implicit flush on cancellation/rejection.
if let Some(log) = self.log.take() {
drop(log.into_parts());
}
}
// Keep the caller's fatal diagnostic on its own physical line when
// execution aborts before finish closes the input entry.
if self.terminal_sink && self.terminal_column != 0 {
@@ -1638,13 +1669,7 @@ impl SourceProvider for Files {
self.emit_diagnostic(WriteDestination::TerminalOnly, terminal, false)
}
fn read(&mut self, name: &str, from: &str) -> Result<SourceFile, String> {
let resource = self.world.read_tex_input(name, Path::new(from))?;
self.observe_resource(ResourceKind::Tex, &resource);
let path = resource.version.path;
Ok(SourceFile {
name: path.display().to_string(),
data: resource.data,
})
self.read_versioned_source(name, from).map(|(source, _)| source)
}
fn resource(
&mut self,