From 260e270be88ca987f9cc5384f834a4186a1d5cf2 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 10:23:52 +0800 Subject: [PATCH] fix(discovery): stabilize first-open normalization before caching --- .../trial_task_center/discovery_cache.py | 20 +++- tests/v1_1/test_discovery_cache.py | 91 ++++++++++++++++++- 2 files changed, 105 insertions(+), 6 deletions(-) diff --git a/src/dx_productivity/trial_task_center/discovery_cache.py b/src/dx_productivity/trial_task_center/discovery_cache.py index e43d5ff..1a77c39 100644 --- a/src/dx_productivity/trial_task_center/discovery_cache.py +++ b/src/dx_productivity/trial_task_center/discovery_cache.py @@ -35,7 +35,10 @@ def project_fingerprint(project: Path, excluded_root: Path) -> tuple | None: kept = [] for name in sorted(dirs): path = current / name - if path.resolve() == excluded or name.casefold() == '.git': + relative = tuple(part.casefold() for part in path.relative_to(root).parts) + audit_directory = (len(relative) == 3 and relative[:2] == ('database', 'cdbsvr') + and relative[2] in {'log', 'plog', 'events'}) + if path.resolve() == excluded or name.casefold() == '.git' or audit_directory: continue info = path.lstat() if _unsafe(info): @@ -106,11 +109,18 @@ class DiscoveryCache: and (not collect_components or has_components)): self._entries[key] = entry return deepcopy(discovery) - discovery = loader() - after = project_fingerprint(project, self.data_root) - if before is not None and before != after: + # Native first-open may normalize an old database. Discard that + # discovery and reopen once; only a verified stable pass is usable. + for attempt in range(2): + if attempt: + before = project_fingerprint(project, self.data_root) + discovery = loader() + after = project_fingerprint(project, self.data_root) + if before is not None and before == after: + break + else: raise TaskCenterError("quick_run_sheet_discovery", - "project contents changed during discovery; retry read-only discovery") + "project contents changed or could not be verified during discovery; retry read-only discovery") # Refuse incomplete inventories and bound both projects and row count. sheets = getattr(discovery, 'sheets', ()) matches = getattr(discovery, 'matched_old_parts', {}) diff --git a/tests/v1_1/test_discovery_cache.py b/tests/v1_1/test_discovery_cache.py index cda0c2c..acfe1d6 100644 --- a/tests/v1_1/test_discovery_cache.py +++ b/tests/v1_1/test_discovery_cache.py @@ -69,7 +69,7 @@ def test_changed_during_discovery_is_not_cached(tmp_path): preparer.discover_project(project, ('old',)) with pytest.raises(TaskCenterError, match='changed'): preparer.discover_project(project, ('old',)) - assert len(calls) == 2 + assert len(calls) == 4 def test_clone_has_independent_cache_and_configuration(tmp_path): @@ -218,3 +218,92 @@ def test_file_changed_during_hashing_has_no_reusable_fingerprint(tmp_path, monke return result monkeypatch.setattr(hashlib, 'file_digest', changing) assert project_fingerprint(project, preparer.data_root) is None + + +def test_first_open_normalization_retries_and_caches_only_stable_second_pass(tmp_path): + project, preparer, calls = prepared(tmp_path) + database = project.parent / 'database' + database.mkdir() + icdb = database / 'icdb.dat' + icdb.write_text('old database') + ole = project.parent / 'OLE' / 'sch' + ole.mkdir(parents=True) + ole_sheet = ole / 'MAIN.1' + ole_sheet.write_text('old normalized data') + def normalize_once(path, parts, *, collect_components=False): + calls.append(parts) + if len(calls) == 1: + icdb.write_text('normalized database') + ole_sheet.write_text('normalized schematic') + return _ProjectDiscovery((('MAIN', '1'),), {('MAIN', '1'): frozenset()}) + return _ProjectDiscovery((('MAIN', '1'),), {('MAIN', '1'): frozenset({'old'})}) + preparer._discover_project_sheets = normalize_once + assert preparer.discover_project(project, ('old',)).matched_old_parts[('MAIN', '1')] == frozenset({'old'}) + assert len(calls) == 2 + assert preparer.discover_project(project, ('old',)).matched_old_parts[('MAIN', '1')] == frozenset({'old'}) + assert len(calls) == 2 + + +@pytest.mark.parametrize('directory', ['log', 'plog', 'events']) +def test_cdbsvr_audit_churn_does_not_rediscover(tmp_path, directory): + project, preparer, calls = prepared(tmp_path) + audit = project.parent / 'database' / 'cdbsvr' / directory + audit.mkdir(parents=True) + audit_file = audit / '000001.dat' + audit_file.write_text('audit') + original = preparer._discover_project_sheets + def audit_during_discovery(*args, **kwargs): + result = original(*args, **kwargs) + audit_file.write_text('audit grew during native open') + return result + preparer._discover_project_sheets = audit_during_discovery + preparer.discover_project(project, ('old',)) + audit_file.write_text('audit changed again after native close') + (audit / '000002.dat').write_text('new audit') + preparer.discover_project(project, ('old',)) + assert len(calls) == 1 + + +@pytest.mark.parametrize('relative', ['database/icdb.dat', 'OLE/sch/MAIN.1', 'sch/MAIN.1']) +def test_authoritative_same_size_content_changes_are_hashed_despite_restored_timestamps(tmp_path, relative): + import os + project, preparer, calls = prepared(tmp_path) + authoritative = project.parent / relative + authoritative.parent.mkdir(parents=True, exist_ok=True) + authoritative.write_bytes(b'nohit') + def discover(path, parts): + calls.append(parts) + matches = frozenset({'old'}) if authoritative.read_bytes() == b'oldxx' else frozenset() + return _ProjectDiscovery((('MAIN', '1'),), {('MAIN', '1'): matches}) + preparer._discover_project_sheets = discover + assert not preparer.discover_project(project, ('old',)).matched_old_parts[('MAIN', '1')] + stamp = authoritative.stat() + authoritative.write_bytes(b'oldxx') + os.utime(authoritative, ns=(stamp.st_atime_ns, stamp.st_mtime_ns)) + assert preparer.discover_project(project, ('old',)).matched_old_parts[('MAIN', '1')] == frozenset({'old'}) + assert len(calls) == 2 + + +def test_unstable_rediscovery_never_falls_back_to_cached_negative_matches(tmp_path): + project, preparer, calls = prepared(tmp_path) + sheet = project.parent / 'sch' / 'MAIN.1' + def negative(path, parts): + calls.append(parts) + return _ProjectDiscovery((('MAIN', '1'),), {('MAIN', '1'): frozenset()}) + preparer._discover_project_sheets = negative + assert not preparer.discover_project(project, ('old',)).matched_old_parts[('MAIN', '1')] + sheet.write_text('externally changed') + def unstable(path, parts): + calls.append(parts) + sheet.write_text('changed during pass ' + str(len(calls))) + return _ProjectDiscovery((('MAIN', '1'),), {('MAIN', '1'): frozenset({'old'})}) + preparer._discover_project_sheets = unstable + with pytest.raises(TaskCenterError, match='changed'): + preparer.discover_project(project, ('old',)) + assert len(calls) == 3 + def stable(path, parts): + calls.append(parts) + return _ProjectDiscovery((('MAIN', '1'),), {('MAIN', '1'): frozenset({'old'})}) + preparer._discover_project_sheets = stable + assert preparer.discover_project(project, ('old',)).matched_old_parts[('MAIN', '1')] == frozenset({'old'}) + assert len(calls) == 4