Advance native LaTeX execution and preview
This commit is contained in:
1 parent
91f9d76b16
commit
2741546ca8
414 files changed
+99872
-933
No files matched your search
@@ -0,0 +1,252 @@
|
||||
//! Acceptance driver: real LaTeX failures/recovery and strict observed inputs.
|
||||
//! All project mutations occur in newly created copies of the frozen A4 case.
|
||||
use std::{
|
||||
fs,
|
||||
path::{Path, PathBuf},
|
||||
sync::Arc,
|
||||
};
|
||||
use texrra_engine::native::{
|
||||
AttemptPhase, CompileError, CompileRequest, CompiledDocument, Entry, HostObservation,
|
||||
HostPreparationPhase, NativeCompiler,
|
||||
};
|
||||
use texrra_tex_core::{artifacts::file_version, execution::JobTime, resources::WorldConfig};
|
||||
|
||||
fn request(cwd: &Path, image: &Path, roots: &[PathBuf]) -> Result<CompileRequest, String> {
|
||||
let mut r = CompileRequest::new(
|
||||
WorldConfig::from_environment(cwd, roots.to_vec())?,
|
||||
"main.tex",
|
||||
"main.pdf",
|
||||
JobTime::new(2026, 11, 16, 123)
|
||||
.unwrap()
|
||||
.with_pdf_clock(0, 480)
|
||||
.unwrap(),
|
||||
);
|
||||
r.entry = Entry::Format(image.to_owned());
|
||||
r.etex = true;
|
||||
r.limits.steps = 600_000_000;
|
||||
r.resource_manifest = Some("resources.json".into());
|
||||
r.layout_manifest = Some("layout.json".into());
|
||||
Ok(r)
|
||||
}
|
||||
fn compile(c: &NativeCompiler, r: CompileRequest) -> Result<Arc<CompiledDocument>, String> {
|
||||
c.compile_detailed(r).map_err(|e| e.to_string())
|
||||
}
|
||||
fn same(a: &CompiledDocument, b: &CompiledDocument) {
|
||||
assert_eq!(a.pdf, b.pdf);
|
||||
assert_eq!(a.pages, b.pages);
|
||||
assert_eq!(a.source_map.pages, b.source_map.pages);
|
||||
assert_eq!(a.execution.source_versions, b.execution.source_versions);
|
||||
}
|
||||
fn save(path: &Path, value: &impl serde::Serialize) -> Result<(), String> {
|
||||
fs::write(
|
||||
path,
|
||||
serde_json::to_vec_pretty(value).map_err(|e| e.to_string())?,
|
||||
)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
fn run() -> Result<(), String> {
|
||||
let mut args = std::env::args_os().skip(1);
|
||||
let image = PathBuf::from(
|
||||
args.next()
|
||||
.ok_or("native_attempt_replay IMAGE NEW_OUTPUT_DIR TEXMF_ROOT...")?,
|
||||
)
|
||||
.canonicalize()
|
||||
.map_err(|e| e.to_string())?;
|
||||
let out = PathBuf::from(args.next().ok_or("missing output directory")?);
|
||||
let roots: Vec<PathBuf> = args.map(PathBuf::from).collect();
|
||||
if roots.is_empty() {
|
||||
return Err("explicit TEXMF roots required".into());
|
||||
}
|
||||
fs::create_dir(&out).map_err(|e| e.to_string())?;
|
||||
let out = out.canonicalize().map_err(|e| e.to_string())?;
|
||||
let fixture =
|
||||
Path::new(env!("CARGO_MANIFEST_DIR")).join("examples/native-format-stage/a4-project");
|
||||
let main = fs::read(fixture.join("main.tex")).map_err(|e| e.to_string())?;
|
||||
let child = fs::read(fixture.join("chapter.tex")).map_err(|e| e.to_string())?;
|
||||
let original = [
|
||||
file_version(&fixture.join("main.tex"))?,
|
||||
file_version(&fixture.join("chapter.tex"))?,
|
||||
];
|
||||
let shared = out.join("shared");
|
||||
let clean = out.join("clean");
|
||||
for p in [&shared, &clean] {
|
||||
fs::create_dir(p).map_err(|e| e.to_string())?;
|
||||
fs::write(p.join("main.tex"), &main).map_err(|e| e.to_string())?;
|
||||
fs::write(p.join("chapter.tex"), &child).map_err(|e| e.to_string())?;
|
||||
}
|
||||
let compiler = NativeCompiler::default();
|
||||
let reference = NativeCompiler::default();
|
||||
let mut baseline = None;
|
||||
let mut expected = None;
|
||||
for _ in 0..3 {
|
||||
baseline = Some(compile(&compiler, request(&shared, &image, &roots)?)?);
|
||||
expected = Some(compile(&reference, request(&clean, &image, &roots)?)?);
|
||||
}
|
||||
let baseline = baseline.unwrap();
|
||||
let expected = expected.unwrap();
|
||||
same(&baseline, &expected);
|
||||
let stable_aux = fs::read(shared.join("main.aux")).map_err(|e| e.to_string())?;
|
||||
assert!(baseline.inputs.file(&image).is_some());
|
||||
assert_eq!(
|
||||
&*baseline.inputs.file(&shared.join("main.tex")).unwrap().data,
|
||||
&main
|
||||
);
|
||||
let bad_main = String::from_utf8(main.clone())
|
||||
.unwrap()
|
||||
.replace(r"\end{document}", "\\input failure.tex\n\\end{document}");
|
||||
assert_ne!(bad_main.as_bytes(), main);
|
||||
fs::write(shared.join("main.tex"), bad_main).map_err(|e| e.to_string())?;
|
||||
fs::write(
|
||||
shared.join("failure.tex"),
|
||||
br"\immediate\openout15=partial.dat\immediate\write15{PARTIAL}\texrraUndefinedAttemptProof",
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let mut failed_request = request(&shared, &image, &roots)?;
|
||||
failed_request.failure_manifest = Some(out.join("failed-attempt-full.json"));
|
||||
let failure = compiler
|
||||
.compile_detailed(failed_request)
|
||||
.err()
|
||||
.ok_or("expected execution failure")?;
|
||||
assert_eq!(failure.phase, AttemptPhase::Executing);
|
||||
assert!(matches!(failure.error, CompileError::Execution(_)));
|
||||
assert!(
|
||||
failure.evidence_gaps.is_empty(),
|
||||
"{:?}",
|
||||
failure.evidence_gaps
|
||||
);
|
||||
assert_eq!(failure.host_preparation.phase, HostPreparationPhase::Ready);
|
||||
let policy = failure.host_preparation.ledger.as_ref().unwrap();
|
||||
assert!(policy.eof && policy.error.is_none());
|
||||
assert_eq!(
|
||||
policy.version,
|
||||
failure
|
||||
.resources
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.generated_artifacts
|
||||
.ledger_input
|
||||
);
|
||||
assert!(failure.host_runtime.iter().any(
|
||||
|o| matches!(o,HostObservation::Operation {name,result:Ok(()),..} if name=="open output")
|
||||
));
|
||||
save(
|
||||
&out.join("failed-host.json"),
|
||||
&serde_json::json!({"preparation":failure.host_preparation,"runtime":failure.host_runtime,"world_failure":failure.world_failure}),
|
||||
)?;
|
||||
let failed = failure.failed_execution.as_ref().unwrap();
|
||||
assert!(
|
||||
failed
|
||||
.evidence
|
||||
.sources
|
||||
.iter()
|
||||
.any(|s| s.name.ends_with("failure.tex"))
|
||||
);
|
||||
assert_eq!(fs::read(shared.join("partial.dat")).unwrap(), b"PARTIAL\n");
|
||||
assert!(Arc::ptr_eq(&baseline, &compiler.latest().unwrap()));
|
||||
save(&out.join("failed-execution.json"), failed)?;
|
||||
save(&out.join("failed-resources.json"), &failure.resources)?;
|
||||
let mut failed_generated = Vec::new();
|
||||
for (index, write) in failure
|
||||
.resources
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.generated_artifacts
|
||||
.writes
|
||||
.iter()
|
||||
.enumerate()
|
||||
{
|
||||
if let Some(version) = &write.after {
|
||||
let saved = out.join(format!("failed-generated-{index}.bin"));
|
||||
fs::write(&saved, fs::read(&version.path).map_err(|e| e.to_string())?)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let receipt = file_version(&saved)?.unwrap();
|
||||
assert_eq!(receipt.sha256, version.sha256);
|
||||
assert_eq!(receipt.bytes, version.bytes);
|
||||
failed_generated.push(serde_json::json!({"path":write.path,"before":write.before,"after":version,"saved":receipt}));
|
||||
}
|
||||
}
|
||||
let failed_inputs = failure.inputs.as_ref().unwrap();
|
||||
for name in ["main.tex", "chapter.tex", "failure.tex"] {
|
||||
fs::remove_file(shared.join(name)).map_err(|e| e.to_string())?;
|
||||
}
|
||||
// Failed LaTeX really truncated/wrote aux. Replay keeps real I/O; reset the
|
||||
// known starting generated version explicitly, rather than pinning it.
|
||||
fs::write(shared.join("main.aux"), &stable_aux).map_err(|e| e.to_string())?;
|
||||
let replay_failure = compiler
|
||||
.compile_detailed(request(&shared, &image, &roots)?.with_inputs(failed_inputs.clone()))
|
||||
.err()
|
||||
.ok_or("expected replay failure")?;
|
||||
assert_eq!(
|
||||
replay_failure.failed_execution.as_ref().unwrap().error,
|
||||
failed.error
|
||||
);
|
||||
assert_eq!(
|
||||
replay_failure
|
||||
.failed_execution
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.evidence
|
||||
.source_versions,
|
||||
failed.evidence.source_versions
|
||||
);
|
||||
save(
|
||||
&out.join("replayed-failure-resources.json"),
|
||||
&replay_failure.resources,
|
||||
)?;
|
||||
fs::write(shared.join("main.aux"), &stable_aux).map_err(|e| e.to_string())?;
|
||||
let replay = compile(
|
||||
&compiler,
|
||||
request(&shared, &image, &roots)?.with_inputs(baseline.inputs.clone()),
|
||||
)?;
|
||||
same(&baseline, &replay);
|
||||
assert_eq!(fs::read(shared.join("main.aux")).unwrap(), stable_aux);
|
||||
fs::write(out.join("replayed.pdf"), replay.pdf.as_ref().unwrap()).map_err(|e| e.to_string())?;
|
||||
save(&out.join("replayed-source-map.json"), &replay.source_map)?;
|
||||
let mut unknown = request(&shared, &image, &roots)?.with_inputs(baseline.inputs.clone());
|
||||
unknown.input = "uncaptured.tex".into();
|
||||
fs::write(shared.join("uncaptured.tex"), &main).map_err(|e| e.to_string())?;
|
||||
let denied = compiler
|
||||
.compile_detailed(unknown)
|
||||
.err()
|
||||
.ok_or("unobserved external input must fail")?;
|
||||
assert_eq!(denied.phase, AttemptPhase::Preparing);
|
||||
assert!(
|
||||
denied
|
||||
.to_string()
|
||||
.contains("snapshot has no lookup observation")
|
||||
);
|
||||
fs::write(shared.join("main.tex"), &main).map_err(|e| e.to_string())?;
|
||||
fs::write(shared.join("chapter.tex"), &child).map_err(|e| e.to_string())?;
|
||||
let recovered = compile(&compiler, request(&shared, &image, &roots)?)?;
|
||||
same(&recovered, &expected);
|
||||
assert_eq!(
|
||||
original,
|
||||
[
|
||||
file_version(&fixture.join("main.tex"))?,
|
||||
file_version(&fixture.join("chapter.tex"))?
|
||||
]
|
||||
);
|
||||
let captured: Vec<_> = baseline
|
||||
.inputs
|
||||
.files()
|
||||
.map(|(p, f)| serde_json::json!({"path":p,"version":f.ok().map(|f| &f.version)}))
|
||||
.collect();
|
||||
save(
|
||||
&out.join("report.json"),
|
||||
&serde_json::json!({"passed":true,"scope":"full native LaTeX attempts and strict captured external inputs; generated state explicitly restored for equivalent starting worlds; no incremental/performance claim",
|
||||
"format":file_version(&image)?,"original_sources":original,"compile_attempts":11,"executed_jobs":10,"baseline_revision":baseline.revision(),"replayed_revision":replay.revision(),"recovered_revision":recovered.revision(),
|
||||
"captured":captured,"failed_phase":format!("{:?}",failure.phase),"failed_sources":failed.evidence.source_versions,"failed_generated":failure.resources.as_ref().unwrap().generated_artifacts,
|
||||
"failure_report":file_version(&out.join("failed-attempt-full.json"))?,"failed_host":file_version(&out.join("failed-host.json"))?,"host_evidence_verified":true,
|
||||
"failed_generated_saved":failed_generated,
|
||||
"failure_primary_preserved":true,"old_publication_preserved":true,"partial_io_preserved":true,"removed_project_sources_replayed":true,"unknown_physical_file_denied":true,"replay_equals_baseline_and_clean":true,"recovery_equals_clean":true,
|
||||
"replayed_pdf":file_version(&out.join("replayed.pdf"))?,"replayed_source_map":file_version(&out.join("replayed-source-map.json"))?}),
|
||||
)?;
|
||||
println!("real LaTeX failed attempt, strict replay and clean recovery passed");
|
||||
Ok(())
|
||||
}
|
||||
fn main() {
|
||||
if let Err(e) = run() {
|
||||
eprintln!("{e}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user