fix(snapshot): validate evidence generations and bound unstable reads

This commit is contained in:
阿白叔 committed 2026-10-15 10:10:29 +08:00
1 parent e3b0f8940c
commit e265ca515d
2 files changed
+140 -2

No files matched your search

@@ -1,6 +1,6 @@
"""Read-only execution view reconstructed from one cached set of handoff files."""
from __future__ import annotations
from dataclasses import dataclass
from dataclasses import dataclass, replace
from pathlib import Path
from .errors import TaskCenterError
from .quick_run_protocol import QuickRunProgress
@@ -19,11 +19,39 @@ class _ReadFiles:
def __init__(self, control):
self.control = control
self.texts = {}
self.roots = {}
def checkpoint(self):
if self.control is not None:
self.control.checkpoint()
def _manifest(self, root):
self.checkpoint()
def stamp(path):
self.checkpoint()
try:
stat = path.stat()
return (stat.st_dev, stat.st_ino, stat.st_size, stat.st_mtime_ns, stat.st_ctime_ns)
except OSError:
return None
root_stamp = stamp(root)
try:
# Include sidecar evidence used by hard-failure recovery as well as
# command/status and flags; creation/removal changes the root stamp.
entries = tuple(root.iterdir())
except OSError:
entries = ()
result = {path.name: stamp(path) for path in entries}
self.checkpoint()
return root_stamp, result
def track_root(self, root):
if root not in self.roots:
self.roots[root] = self._manifest(root)
def stable(self):
return all(self._manifest(root) == before for root, before in self.roots.items())
def path(self, path):
return _CachedPath(Path(path), self)
@@ -61,9 +89,23 @@ class _CachedPath:
def read_snapshot(preparer, task_id, projects, rules, *, copy_part_number_to_value=False,
read_control=None, project_path=None):
for _ in range(3):
files = _ReadFiles(read_control)
snapshot = _read_snapshot_once(preparer, task_id, projects, rules,
copy_part_number_to_value=copy_part_number_to_value, project_path=project_path, files=files)
if files.stable():
return snapshot
# Never publish executable continuation coordinates from a changing set.
progress = replace(snapshot.progress, next_project_path=None, next_old_part_number=None,
next_new_part_number=None, next_schematic_name=None, next_sheet_number=None)
files.checkpoint()
return replace(snapshot, progress=progress, interrupted=True)
def _read_snapshot_once(preparer, task_id, projects, rules, *, copy_part_number_to_value=False,
project_path=None, files):
# Import lazily to preserve the historical quick_run private parser boundaries.
from . import quick_run as api
files = _ReadFiles(read_control)
files.checkpoint()
selected = api._selected_projects(projects)
queued = api._queue_projects(projects)
@@ -83,6 +125,7 @@ def read_snapshot(preparer, task_id, projects, rules, *, copy_part_number_to_val
yield from sorted((path for path in history.glob('obsolete-*') if path.is_dir()), reverse=True)
for candidate in candidates():
files.checkpoint()
files.track_root(candidate)
command = candidate / 'command.tsv'
if not command.is_file() or not (candidate / 'status.tsv').is_file():
continue