Hash project content before reusing discovery evidence
This commit is contained in:
1 parent
78f3b0c32a
commit
87ec3acc20
2 files changed
+59
-4
No files matched your search
@@ -1,13 +1,14 @@
|
||||
"""Conservative, bounded process-local reuse of read-only CAD discovery.
|
||||
|
||||
Metadata fingerprints cover every project-tree file except generated logs and
|
||||
explicit tool data. Links, unreadable paths and changing discovery are misses.
|
||||
Content and metadata fingerprints cover every project-tree file except generated
|
||||
logs and explicit tool data. Links, unreadable paths and changing discovery are misses.
|
||||
This is an optimization, never a replacement for execution identity checks.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import OrderedDict
|
||||
from copy import deepcopy
|
||||
import hashlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
import stat
|
||||
@@ -48,8 +49,23 @@ def project_fingerprint(project: Path, excluded_root: Path) -> tuple | None:
|
||||
info = path.lstat()
|
||||
if _unsafe(info) or not stat.S_ISREG(info.st_mode):
|
||||
return None
|
||||
rows.append((str(path.relative_to(root)), info.st_dev, info.st_ino,
|
||||
info.st_size, info.st_mtime_ns, info.st_ctime_ns))
|
||||
before = _file_stamp(info)
|
||||
with path.open('rb') as stream:
|
||||
handle_before = _file_stamp(os.fstat(stream.fileno()))
|
||||
# Windows path stat and handle stat expose different ctime
|
||||
# semantics. Compare their identity/size/mtime, then compare
|
||||
# each source's full stamp against itself after hashing.
|
||||
if handle_before[:4] != before[:4]:
|
||||
return None
|
||||
digest = hashlib.file_digest(stream, 'sha256').digest()
|
||||
if _file_stamp(os.fstat(stream.fileno())) != handle_before:
|
||||
return None
|
||||
# Check the path as well as the handle: atomic replacement can
|
||||
# leave a readable handle referring to the obsolete file.
|
||||
after = path.lstat()
|
||||
if _unsafe(after) or _file_stamp(after) != before:
|
||||
return None
|
||||
rows.append((str(path.relative_to(root)), *before, digest))
|
||||
if not project.is_file():
|
||||
return None
|
||||
return (str(project.resolve()), tuple(sorted(rows)))
|
||||
@@ -57,6 +73,10 @@ def project_fingerprint(project: Path, excluded_root: Path) -> tuple | None:
|
||||
return None
|
||||
|
||||
|
||||
def _file_stamp(info):
|
||||
return (info.st_dev, info.st_ino, info.st_size, info.st_mtime_ns, info.st_ctime_ns)
|
||||
|
||||
|
||||
def _unsafe(info):
|
||||
return stat.S_ISLNK(info.st_mode) or bool(
|
||||
getattr(info, 'st_file_attributes', 0) & getattr(stat, 'FILE_ATTRIBUTE_REPARSE_POINT', 0x400))
|
||||
|
||||
@@ -183,3 +183,38 @@ def test_tool_evidence_inside_project_tree_is_excluded(tmp_path):
|
||||
preparer.discover_project(project, ('old',))
|
||||
preparer.discover_project(project, ('old',))
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
def test_same_size_schematic_edit_with_restored_mtime_rechecks_negative_matches(tmp_path):
|
||||
import os
|
||||
project, preparer, calls = prepared(tmp_path)
|
||||
sheet = project.parent / 'sch' / 'MAIN.1'
|
||||
sheet.write_text('nohit', encoding='ascii')
|
||||
def discover(path, parts):
|
||||
calls.append(parts)
|
||||
matches = frozenset({'old'}) if sheet.read_text(encoding='ascii') == 'oldxx' else frozenset()
|
||||
return _ProjectDiscovery((('MAIN', '1'),), {('MAIN', '1'): matches})
|
||||
preparer._discover_project_sheets = discover
|
||||
assert preparer.discover_project(project, ('old',)).matched_old_parts[('MAIN', '1')] == frozenset()
|
||||
original = sheet.stat()
|
||||
sheet.write_text('oldxx', encoding='ascii')
|
||||
os.utime(sheet, ns=(original.st_atime_ns, original.st_mtime_ns))
|
||||
assert sheet.stat().st_size == original.st_size
|
||||
assert sheet.stat().st_mtime_ns == original.st_mtime_ns
|
||||
assert preparer.discover_project(project, ('old',)).matched_old_parts[('MAIN', '1')] == frozenset({'old'})
|
||||
assert len(calls) == 2
|
||||
|
||||
|
||||
def test_file_changed_during_hashing_has_no_reusable_fingerprint(tmp_path, monkeypatch):
|
||||
import hashlib
|
||||
from dx_productivity.trial_task_center.discovery_cache import project_fingerprint
|
||||
project, preparer, _ = prepared(tmp_path)
|
||||
sheet = project.parent / 'sch' / 'MAIN.1'
|
||||
digest = hashlib.file_digest
|
||||
def changing(stream, algorithm):
|
||||
result = digest(stream, algorithm)
|
||||
if Path(stream.name) == sheet:
|
||||
sheet.write_bytes(b'changed while hashing')
|
||||
return result
|
||||
monkeypatch.setattr(hashlib, 'file_digest', changing)
|
||||
assert project_fingerprint(project, preparer.data_root) is None
|
||||
Reference in new issue
Block a user