From 87ec3acc20f81b9e27a3c70b552e2a384f2312c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E7=99=BD=E5=8F=94?= <11794527+absfan@user.noreply.gitee.com> Date: Thu, 15 Oct 2026 09:39:53 +0800 Subject: [PATCH] Hash project content before reusing discovery evidence --- .../trial_task_center/discovery_cache.py | 28 ++++++++++++--- tests/v1_1/test_discovery_cache.py | 35 +++++++++++++++++++ 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/dx_productivity/trial_task_center/discovery_cache.py b/src/dx_productivity/trial_task_center/discovery_cache.py index d4c909e..e43d5ff 100644 --- a/src/dx_productivity/trial_task_center/discovery_cache.py +++ b/src/dx_productivity/trial_task_center/discovery_cache.py @@ -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)) diff --git a/tests/v1_1/test_discovery_cache.py b/tests/v1_1/test_discovery_cache.py index fb40ee1..cda0c2c 100644 --- a/tests/v1_1/test_discovery_cache.py +++ b/tests/v1_1/test_discovery_cache.py @@ -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