Validate stable native inventories across ICDB session updates

This commit is contained in:
阿白叔 committed 2026-10-15 15:01:27 +08:00
1 parent 4d8ba4c5bb
commit a332e7bd59
2 files changed
+82 -5

No files matched your search

@@ -38,7 +38,8 @@ def project_fingerprint(project: Path, excluded_root: Path) -> tuple | None:
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:
backup_directory = relative == ('projectbackup', 'backups')
if path.resolve() == excluded or name.casefold() == '.git' or audit_directory or backup_directory:
continue
info = path.lstat()
if _unsafe(info):
@@ -47,7 +48,8 @@ def project_fingerprint(project: Path, excluded_root: Path) -> tuple | None:
dirs[:] = kept
for name in sorted(files):
path = current / name
if path.suffix.casefold() == '.log':
relative = tuple(part.casefold() for part in path.relative_to(root).parts)
if path.suffix.casefold() == '.log' or relative == ('log files', 'projectbackuplog.txt'):
continue
info = path.lstat()
if _unsafe(info) or not stat.S_ISREG(info.st_mode):
@@ -85,6 +87,18 @@ def _unsafe(info):
getattr(info, 'st_file_attributes', 0) & getattr(stat, 'FILE_ATTRIBUTE_REPARSE_POINT', 0x400))
def _without_runtime_database(stamp):
if stamp is None or len(stamp) != 2:
return None
return (stamp[0], tuple(row for row in stamp[1]
if row[0].replace('\\', '/').casefold() != 'database/icdb.dat'))
def _only_database_changed(before, after):
design = _without_runtime_database(before)
return design is not None and before != after and design == _without_runtime_database(after)
class DiscoveryCache:
"""One preparer owns one cache; the lock also serializes cache discovery."""
def __init__(self, data_root: Path, capacity: int = 32):
@@ -113,8 +127,12 @@ class DiscoveryCache:
# project still gets fresh native discovery, with no cache reuse.
if before is None:
return loader()
# Native first-open may normalize an old database. Discard that
# discovery and reopen once; only a verified stable pass is usable.
# Native opens may update database session metadata on every read.
# Accept that case only after two equal native results while every
# other design file stays stable across both passes. The complete
# final database hash still invalidates later cache reuse.
previous = None
previous_design = None
for attempt in range(2):
if attempt:
before = project_fingerprint(project, self.data_root)
@@ -122,13 +140,20 @@ class DiscoveryCache:
after = project_fingerprint(project, self.data_root)
if before is not None and before == after:
break
if _only_database_changed(before, after):
design = _without_runtime_database(after)
if previous is not None and previous == discovery and previous_design == design:
break
previous, previous_design = deepcopy(discovery), design
else:
previous = previous_design = None
else:
raise TaskCenterError("quick_run_sheet_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', {})
if (before is not None and before == after and sheets
if (after is not None and sheets
and set(sheets) == set(matches)
and (len(getattr(discovery, 'components', ())) + len(sheets)
+ sum(len(parts) for parts in matches.values())) <= 100000):