From 061e48400144fa8e9fadcf352a4ce49c89aff08c Mon Sep 17 00:00:00 2001 From: GloamXun Date: Sun, 29 Nov 2026 09:12:17 +0800 Subject: [PATCH] Reject changed nested file sizes before checkpoint replay --- docs/P2_M1_PROGRESS.md | 27 ++++++++++++++++++ src/native_checkpoint_guard.rs | 51 ++++++++++++++++++++++++---------- src/native_checkpoint_probe.rs | 25 +++++++++++++---- tests/native_pipeline.rs | 28 ++++++++++++------- 4 files changed, 101 insertions(+), 30 deletions(-) diff --git a/docs/P2_M1_PROGRESS.md b/docs/P2_M1_PROGRESS.md index ca6fa61..67651e9 100644 --- a/docs/P2_M1_PROGRESS.md +++ b/docs/P2_M1_PROGRESS.md @@ -18,6 +18,33 @@ | W6 会话 | 实施中 | 共享库/CLI 与当前原生文字/数学/规则来源进入公共结果;真实 A4 副本九类/27 revision 与独立干净作业页面/PDF/aux/映射一致,新增数学场景独立参考通过;详细失败/捕获重放及宿主/离线/CLI观察已通过;原子项目版本/已知生成初态及TeX流记录已接通;原样 12 页 NeurIPS 的中部编辑与恢复已验证前后缀复用、仅更新一页,且与独立全量一致。全部副作用/交叉编辑、旧来源与正式性能门禁仍待闭合 | | W7 总验收 | 未满足 | 原有和新增测试、原样项目、格式与会话门禁全部通过才能结项 | +## 2026-11-29 子文件大小变化的检查点预检 + +子文件内部检查点记录已执行的真实文件大小查询。项目的查询配置 +及不可变文件路径集合相同时,选择检查点时比对已有查询值。 +文件大小变化会直接选更早的可继续检查点。被选中的检查点继续按 +实际调用顺序重放。仅修改已读取行之外的等长内容可保留子文件 +检查点,整文件读取、读取范围和文件日期继续按各自实际观察校验。 + +原样 A4 的 LaTeX 内核在打开子文件前会查询字节数。174 → 183 +及 183 → 174 时,原行为两次都在 46 个成功重放调用之后遇到 +大小变化并丢弃草稿。新行为在建立草稿前绕过该检查点。两次修订 +从提交至 Prepared 的临时文件新建数均从 4 降到 2。复用和实际 +执行步数逐项对应,异长编辑与还原仍分别执行 79,031 和 79,001 步。 + +六次 A4 主、子文件编辑和恢复的完整执行、PDF、SVG、来源映射、 +读取轨迹、aux 和日志均与独立全量一致,四项定向回归通过。 +统计准备阶段的实际文件创建,正式浏览器可见时延和峰值内存 +继续按原门槛验收。原样文件、格式与证据在 +target/p4-nested-size-guard/,SHA256SUMS 指纹为 +944252eee05f04e328c460560e66fc4755d840d1f381267fd1722ab4ea61f072。 + +另有独立同源样例只查询一个文件的大小。内容等长改变、观测值仍为 +4 字节时,可见页面无变化,却重执行 27,175 步。改成 11 字节后 +按实际值只更新第一页。两次资源读记录包含当前文件版本,全部 +与独立全量一致。下一入口是区分只观察长度的实际依赖和内容依赖, +同时保留生成文件、跨文件及实时验收门槛。 + ## 2026-11-29 检查点按分区共享命令绑定 命令表以 32 个写时复制的分区共享给检查点。命令查找用一次散列选取 diff --git a/src/native_checkpoint_guard.rs b/src/native_checkpoint_guard.rs index b605dd1..228fc3d 100644 --- a/src/native_checkpoint_guard.rs +++ b/src/native_checkpoint_guard.rs @@ -616,8 +616,8 @@ impl PublishedCheckpoints { )?; let selected = selected.expect("resume preparation chose a verified checkpoint"); let guard = selected.guard(); - let partial_source = guard.nested_source.as_deref() - .and_then(|path| project.file(path)).map(|file| &file.version); + let partial_source = guard.nested_source.as_ref() + .and_then(|source| project.file(&source.path)).map(|file| &file.version); for (index, call) in selected.calls().iter().enumerate() { check(cancellation)?; if let Err(error) = call.replay(&mut prepared.files, partial_source) { @@ -692,9 +692,9 @@ impl CachedCheckpoint { { return Err("checkpoint entry prefix changed".into()); } - if let Some(path) = &self.guard.nested_source { - let child = project.file(path).ok_or("checkpoint child is unavailable")?; - if !self.entry.accepts_nested_source(&path.to_string_lossy(), &child.data) { + if let Some(source) = &self.guard.nested_source { + let child = project.file(&source.path).ok_or("checkpoint child is unavailable")?; + if !self.entry.accepts_nested_source(&source.path.to_string_lossy(), &child.data) { return Err("checkpoint child prefix changed".into()); } } @@ -774,7 +774,7 @@ impl SelectedCheckpoint { let frontier = checkpoint.entry.frontier(); work.retained_checkpoints = self.set.entries.len(); work.reused_frontier = Some(frontier); - work.reused_source = checkpoint.guard.nested_source.clone(); + work.reused_source = checkpoint.guard.nested_source.as_ref().map(|source| source.path.clone()); work.reused_steps = checkpoint.entry.completed_steps(); work.replayed_calls = checkpoint.replay_count; let changed_prefix = || CompileError::Host("prepared checkpoint prefix changed".into()); @@ -794,8 +794,8 @@ impl SelectedCheckpoint { let mut schedule = CaptureSchedule::new(source.data.len(), None, edit); let mut nested_schedule = NestedSchedule::default(); let entry_frontier = checkpoint.entry.entry_frontier(); - let current_nested = checkpoint.guard.nested_source.as_deref() - .and_then(|path| request.project.as_ref()?.file(path)); + let current_nested = checkpoint.guard.nested_source.as_ref() + .and_then(|source| request.project.as_ref()?.file(&source.path)); let nested_source = current_nested.map(|file| SourceFile { name: file.version.path.display().to_string(), data: file.data.to_vec(), @@ -930,12 +930,17 @@ fn changed_line_start(previous: &[u8], current: &[u8]) -> Option { ) } +struct NestedSourceGuard { + path: PathBuf, + observed_size: Option, +} + struct PrefixInputGuard { original: AttemptRequest, project: Arc, initial: GeneratedInitialState, entry: PathBuf, - nested_source: Option, + nested_source: Option, immutable: BTreeSet, immutable_stream_prefixes: BTreeMap, generated: BTreeSet, @@ -1057,16 +1062,21 @@ impl PrefixInputGuard { fn allow_nested_source(&mut self, path: &Path, trace: &ResourceTrace, host: &RecordingHost<'_>) -> bool { let Some(file) = self.project.file(path) else { return false }; - // Presence and size queries can be replayed against the new bytes. - // All content reads other than the active scanner pin the old bytes. - if !host.source_read_is_rebindable(&file.version) - || trace.stream_reads.iter().any(|read| read.path == path) + // The scanner pins its loaded prefix. A size query also pins the + // observed byte count. Other content reads still pin the full input. + let Some(observations) = host.rebindable_source_observations(&file.version) else { + return false; + }; + if trace.stream_reads.iter().any(|read| read.path == path) || trace.file_modifications.iter().any(|read| read.path == path) || trace.generated_lookups.iter().any(|read| read.target == path) { return false; } - self.nested_source = Some(path.to_path_buf()); + self.nested_source = Some(NestedSourceGuard { + path: path.to_path_buf(), + observed_size: observations.size, + }); true } @@ -1095,11 +1105,22 @@ impl PrefixInputGuard { if !self.project.same_lookup_configuration(current)? { return Err("checkpoint lookup configuration changed".into()); } + // An immutable source's recorded size can rule out the checkpoint + // before drafting or replay. A changed namespace still goes through + // the live lookup and all successful candidates replay their calls. + if let Some(source) = &self.nested_source + && let Some(expected) = source.observed_size + && let Some(file) = current.file(&source.path) + && file.version.bytes != expected + && self.project.immutable_paths().eq(current.immutable_paths()) + { + return Err(format!("checkpoint file size changed: {}", source.path.display())); + } for path in &self.immutable { let same_bytes = self.project.file(path).zip(current.file(path)).is_some_and( |(old, new)| { old.version == new.version - || self.nested_source.as_ref() == Some(path) + || self.nested_source.as_ref().is_some_and(|source| &source.path == path) || self.immutable_stream_prefixes.get(path).is_some_and(|&end| { new.data.get(..end) == Some(&old.data[..end]) }) diff --git a/src/native_checkpoint_probe.rs b/src/native_checkpoint_probe.rs index dec394b..1f14aa2 100644 --- a/src/native_checkpoint_probe.rs +++ b/src/native_checkpoint_probe.rs @@ -31,6 +31,10 @@ pub(super) struct SizeRead { read: Option, } +pub(super) struct RebindableSourceObservations { + pub(super) size: Option, +} + impl Call { pub(super) fn replay(&self, files: &mut Files, partial_source: Option<&ResourceVersion>) -> Result<(), String> { match self { @@ -105,19 +109,30 @@ impl<'a> RecordingHost<'a> { (count < MAX_REPLAY_CALLS).then(|| (self.files.resource_trace(), count)) } - pub(super) fn source_read_is_rebindable(&self, version: &ResourceVersion) -> bool { - let Some(calls) = &self.calls else { return false }; + pub(super) fn rebindable_source_observations( + &self, + version: &ResourceVersion, + ) -> Option { + let calls = self.calls.as_ref()?; let mut source_reads = 0; let mut size_reads = 0; for call in calls { match call { Call::ReadSource(read) if read.expected.as_ref() == Ok(version) => source_reads += 1, - Call::FileSize(read) if read.read.as_ref().is_some_and(|r| r.path == version.path) => size_reads += 1, + Call::FileSize(read) if read.read.as_ref().is_some_and(|r| r.path == version.path) => { + if read.observed != Ok(Some(version.bytes)) { + return None; + } + size_reads += 1; + } _ => {} } } - source_reads == 1 && self.files.resource_trace().reads.iter() - .filter(|read| read.path == version.path).count() == source_reads + size_reads + let actual_reads = self.files.resource_trace().reads.iter() + .filter(|read| read.path == version.path).count(); + (source_reads == 1 && actual_reads == source_reads + size_reads).then_some( + RebindableSourceObservations { size: (size_reads > 0).then_some(version.bytes) }, + ) } pub(super) fn files(&self) -> &Files { diff --git a/tests/native_pipeline.rs b/tests/native_pipeline.rs index 3ea74ca..9914495 100644 --- a/tests/native_pipeline.rs +++ b/tests/native_pipeline.rs @@ -656,19 +656,27 @@ fn child_checkpoints_reuse_only_the_validated_physical_prefix() { prior = after; } - // A whole-file consumer makes changes to any byte observable before input. - let probed = make_project(&format!(r"\count0=\pdffilesize{{leaf.tex}}\relax + // A size probe allows new bytes of the same length past the scanner + // frontier. A length change invalidates the earlier numeric result. + let mut probed = make_project(&format!(r"\count0=\pdffilesize{{leaf.tex}}\relax {main}")); let guarded = NativeCompiler::default().with_automatic_entry_checkpoints(); guarded.compile(request.clone().with_project(probed.clone())).unwrap(); - let changed = original.replace("100 points", "3000 points"); - let probed = probed.with_file_edits(2, [(leaf.clone(), Arc::from(changed.into_bytes()))].into()).unwrap(); - let job = request.with_project(probed); - let after = guarded.compile(job.clone()).unwrap(); - let full = NativeCompiler::default().compile(job).unwrap(); - assert_ne!(after.entry_checkpoint_work.reused_source.as_deref(), Some(leaf.as_path())); - assert!(after.execution.execution == full.execution.execution); - assert_eq!(after.pdf, full.pdf); + + for (revision, edit, reuse_leaf) in [(2, "200 points", true), (3, "3000 points", false)] { + let changed = original.replace("100 points", edit); + let bytes = changed.len() as i32; + probed = probed.with_file_edits(revision, [(leaf.clone(), Arc::from(changed.into_bytes()))].into()).unwrap(); + let job = request.clone().with_project(probed.clone()); + let after = guarded.compile(job.clone()).unwrap(); + let full = NativeCompiler::default().compile(job).unwrap(); + assert_eq!(after.entry_checkpoint_work.reused_source.as_deref() == Some(leaf.as_path()), reuse_leaf, + "{:?}", after.entry_checkpoint_work); + assert_eq!(after.execution.execution.state.count(0), bytes); + assert!(after.execution.execution == full.execution.execution); + assert_eq!(after.execution.execution_resources, full.execution.execution_resources); + assert_eq!(after.pdf, full.pdf); + } fs::remove_dir_all(root).unwrap(); }