"""User-requested native inspection with or without an existing advertisement. An advertisement is never deleted, quarantined or called stale by this module. Exclusive access and exact project binding permit an official inspection. """ from contextlib import contextmanager, ExitStack from dataclasses import asdict, replace import json import os from pathlib import Path, PureWindowsPath import re import stat from uuid import uuid4 def assert_native_idle(): from .owner_probe import query_owners from .native_management import _retained_workers from .native_export_guard import assert_retained_exports_idle from .native_engineering_identity import _OWNER_NAMES assert_retained_exports_idle() if any(worker.poll() is None for worker in _retained_workers): raise ValueError('MAINTENANCE_OWNER_ACTIVE: 管理工作进程尚未返回') names = ('ExpeditionPCB.exe', 'LibraryManager.exe', 'viewdraw.exe', 'iCDBProjectBackup.exe', 'ReuseBlockImporter.exe', 'CleanWGLib.exe', 'cscript.exe', 'wscript.exe', 'python.exe', 'pythonw.exe', 'CbbRefreshVerify.exe', *sorted(_OWNER_NAMES)) owners = [] for item in query_owners(names): if item['pid'] == os.getpid(): continue name, command = item['name'].casefold(), item['command'].casefold() automation = name in ('python.exe', 'pythonw.exe', 'cbbrefreshverify.exe') if not automation or not command or any(flag in command for flag in ( '--verify-worker', '--repair-worker', '--management-worker')): owners.append(item) if owners: raise ValueError(f'MAINTENANCE_OWNER_ACTIVE: 请正常关闭原生程序/操作助手:{owners}') def address_snapshot(paths): from .scoped_manifest import hash_file return {str(path.resolve()): {'sha256': hash_file(path), 'size': path.stat().st_size, 'mtime_ns': path.stat().st_mtime_ns} for path in paths} def _scoped_addresses(request, *, check=None, entries=None): from .scoped_manifest import scan_tree root = Path(request.lmc_file).parent addresses = [] if entries is None: entries = scan_tree(root, check=check) if check is not None: check() for path in entries: if path.name.casefold() != 'saddress.adr': continue parts = tuple(part.casefold() for part in path.relative_to(root).parts) independent = (len(parts) >= 6 and parts[0] == 'reusableblockslp' and parts[1] != request.block_name.casefold() and parts[2] == 'work' and parts[3:] in (('database', 'cdbsvr', 'saddress.adr'), ('pcb', 'database', 'cdbsvr', 'saddress.adr'))) if not independent: # Current project and shared/unknown scopes remain visible to the # selector; only exact independent other-CBB Work stores are omitted. addresses.append(path) return addresses def maintenance_project_files(project, pcb): """A legacy linked PCB need not contain the auxiliary PCB/pcb.prj. Its explicit ProjectFile must bind back to the main project. Never guess template.prj (or another PRJ) as an alternative database repair target. """ from .backup_recovery import _project_text project, pcb = Path(project), Path(pcb) auxiliary = pcb.parent/'pcb.prj' if auxiliary.is_file(): return project, auxiliary if os.path.lexists(auxiliary): raise ValueError(f'MAINTENANCE_TARGET_UNCONFIRMED: 工程配置不是文件:{auxiliary}') references = re.findall(r'^KEY ProjectFile "([^"\r\n]+)"', _project_text(pcb), re.M) if (len(references) != 1 or (pcb.parent/references[0]).resolve() != project.resolve()): raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: 缺少辅助 pcb.prj,且 PCB 的 ProjectFile 未唯一指向当前主工程') return (project,) def select_maintenance_target(request, *, check=None, entries=None): from .verify_runner import preflight from .backup_recovery import _project_text from .native_repair import RepairTarget from .scoped_manifest import scan_tree if request.maintenance is not True: raise ValueError('MAINTENANCE_INTENT_REQUIRED') project, pcb = preflight(request) scan_tree(project.parent, check=check) candidates = [] for candidate in maintenance_project_files(project, pcb): if not candidate.is_file(): raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: 缺少工程配置') text = _project_text(candidate) values = re.findall(r'^KEY iCDBDir "([^"]*)"', text, re.M) if candidate != project and not re.search(r'^KEY iCDBDir\b', text, re.M): continue if (len(values) != 1 or not values[0] or PureWindowsPath(values[0]).drive or PureWindowsPath(values[0]).root): raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: iCDBDir 必须唯一且为工程内相对路径') database = (candidate.parent/values[0]).resolve() # The proven Project Backup route binds PRJ.parent/database; do not # guess alternate stores or route shared data through another PRJ. if database != (candidate.parent/'database').resolve() or not database.is_relative_to(project.parent.resolve()): raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: 未验证的 PRJ 数据库映射') if not (database/'icdb.dat').is_file(): raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: 缺少 icdb.dat') candidates.append(RepairTarget(candidate, candidate.parent/'database')) addresses = _scoped_addresses(request, check=check, entries=entries) if not addresses: # A crashed project may retain its internal open state after the server # advertisement disappears. Explicit maintenance must still inspect it. references = re.findall(r'^KEY ProjectFile "([^"\r\n]+)"', _project_text(pcb), re.M) if len(references) != 1 or (pcb.parent/references[0]).resolve() != project.resolve(): raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: PCB 的 ProjectFile 未唯一指向所选主工程') if len(candidates) != 1: raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: 无地址记录且存在多个工程数据库,不能推测修复目标') return candidates[0], project, pcb, addresses if len(addresses) != 1: raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: 需要唯一工程地址记录;共享或多个数据库不能推测修复') matches = [target for target in candidates if (target.database/'cdbsvr/sAddress.adr').resolve() == addresses[0].resolve()] if len(matches) != 1: raise ValueError('SHARED_DATABASE_REPAIR_UNSUPPORTED: 地址记录不属于唯一匹配的工程数据库') return matches[0], project, pcb, addresses @contextmanager def maintenance_target_guard(request, expected=None, *, stop=None): """Simultaneously hold native database files, preserving every address.""" import win32con, win32file from .scoped_manifest import scan_tree, hash_file def check(): if stop is not None and stop.is_set(): raise ValueError('MAINTENANCE_CANCELLED: 已取消占用检查') check() library_root = Path(request.lmc_file).parent # Share one complete snapshot for target/address selection and file locks. # Refresh it again while the handles are held; do not reuse across guards. entries = scan_tree(library_root, check=check) target, project, pcb, addresses = select_maintenance_target(request, check=check, entries=entries) baseline = address_snapshot(addresses) context = {'library': str(Path(request.lmc_file).resolve()), 'block': request.block_name, 'project': str(target.project.resolve()), 'database': str(target.database.resolve()), 'addresses': baseline, 'engineering_references': {str(p.resolve()): hash_file(p) for p in (pcb, *maintenance_project_files(project, pcb))}} if expected is not None and context != expected: raise ValueError('MAINTENANCE_SOURCE_CHANGED: 目标或地址记录在检查后变化') assert_native_idle() check() def native_files(snapshot): return {path for path, info in snapshot.items() if stat.S_ISREG(info.st_mode) and ( path.suffix.casefold() == '.lmc' or path.name.casefold() in ('icdb.dat', 'layoutdb.lyt'))} files = native_files(entries) with ExitStack() as stack: for path in sorted(files): check() handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) stack.callback(handle.Close) assert_native_idle() current = scan_tree(library_root, check=check) if native_files(current) != files: raise ValueError('MAINTENANCE_SOURCE_CHANGED: 检查期间库文件集合发生变化') current_addresses = _scoped_addresses(request, check=check, entries=current) if address_snapshot(current_addresses) != baseline: raise ValueError('MAINTENANCE_SOURCE_CHANGED: 地址记录在检查期间变化') yield {'target': target, 'context': context, 'project': project, 'pcb': pcb} def maintenance_needed(request): # An explicit maintenance request must reach native inspection even when # no address/checkpoint remains. Selection and ownership are guarded later. if request.maintenance is True: return True from .library_lock import has_blocker from .icdb_ownership import unreleased_addresses from .verify_runner import preflight if has_blocker(request.lmc_file): return True project, pcb = preflight(request) return bool(unreleased_addresses(request.lmc_file, project, pcb)) def run_existing_address_maintenance(request, *, verify, progress=None, stop=None): from .library_lock import LibraryLock from .native_repair import (run_native_repair, native_success_confirmed, repair_release_state, _native_identity_and_exit_confirmed) from .verify_runner import VerifyResult root = Path(request.evidence_dir).resolve()/('maintenance-' + uuid4().hex[:12]) library_root = Path(request.lmc_file).resolve().parent if root.is_relative_to(library_root) or library_root.is_relative_to(root): return VerifyResult('BLOCKED', stderr='维护证据必须位于目标库之外', safe_to_continue=False) root.mkdir(parents=True, exist_ok=False) record = {'operation': 'explicit_project_maintenance', 'library': str(request.lmc_file), 'block': request.block_name, 'stage': 'preflight'} report = root/'result.json' owner_safe = False recovery = None def emit(stage, **values): record['stage'] = stage if progress: progress({'stage': stage, 'block': request.block_name, **values}) def save(result): result = replace(result, evidence_path=report) record['result'] = asdict(result) report.write_text(json.dumps(record, ensure_ascii=False, default=str, indent=2), encoding='utf-8') return result def native_event(event): emit(event['stage'], **{key: value for key, value in event.items() if key != 'stage'}) try: if stop is not None and stop.is_set(): return save(VerifyResult('CANCELLED', stderr='维护已取消,未启动原生操作')) emit('preflight', detail='正在核对工程、库文件和进程占用;可请求取消') lease = LibraryLock.for_maintenance(request.lmc_file, root/'takeover', lambda: maintenance_target_guard(request, stop=stop), safe_to_release=lambda: owner_safe) with lease: checked = lease.maintenance_proof target, context = checked['target'], checked['context'] record['maintenance_context'] = context if stop is not None and stop.is_set(): raise ValueError('MAINTENANCE_CANCELLED: 已取消,未启动原生检查') emit('icdb_ownership_check', detail='调用原生 Project Backup 检查占用及恢复要求,不执行 Repair') inspection = run_native_repair(target, root/'native-inspection', mentor_bin=request.mentor_bin, timeout_seconds=min(30, request.timeout_seconds), progress=native_event, maintenance_context=context, inspection_only=True, stop=stop) record['native_inspection'] = inspection from .native_occupancy import confirmed_inspection if not confirmed_inspection(target, inspection): return save(VerifyResult('BLOCKED', safe_to_continue=False, error_category='ICDB_INSPECTION_UNCONFIRMED', stderr='原生占用检查未确认或已取消,未请求 Repair。' + str(inspection.get('detail', inspection.get('status'))))) if inspection['status'] == 'IN_USE': return save(VerifyResult('BLOCKED', safe_to_continue=False, error_category='ICDB_DATABASE_IN_USE', stderr='原生工具确认工程正在使用,未请求 Repair。')) # Native loading may retire an old address itself. Permit only # disappearance, never a new/changed advertisement or project. with maintenance_target_guard(request, stop=stop) as refreshed: updated = refreshed['context'] if ({k: v for k, v in context.items() if k != 'addresses'} != {k: v for k, v in updated.items() if k != 'addresses'} or any(context['addresses'].get(k) != v for k, v in updated['addresses'].items())): raise ValueError('MAINTENANCE_SOURCE_CHANGED: 原生检查后工程或地址身份变化') context = updated record['post_inspection_context'] = context if not inspection['repair_required']: owner_safe = not bool(context['addresses']) return save(VerifyResult('CHECKED', safe_to_continue=owner_safe, stdout='原生确认未占用,未报告明确修复要求;未执行 Repair,不能算修复成功。', stderr='' if owner_safe else '历史地址记录仍存在;检查未替代恢复或会话释放。')) if stop is not None and stop.is_set(): raise ValueError('MAINTENANCE_CANCELLED: 已取消,未开始 Repair') emit('icdb_diagnosis_confirmed', detail='本轮原生检查明确要求修复当前数据库,且确认未占用') native_root = root/'native-repair'; native_root.mkdir() attempt = native_root/'attempt.json' with attempt.open('x', encoding='utf-8') as stream: json.dump({'intent': 'explicit_maintenance', 'target': asdict(target), 'maintenance_context': context}, stream, default=str, ensure_ascii=False) emit('native_repair_start', detail='核对占用后由官方工具检查/修复;应用不自动备份') # Repeat immediately before dispatch; the worker repeats before # launching the native process as well. with maintenance_target_guard(request, context, stop=stop): pass recovery = run_native_repair(target, native_root, mentor_bin=request.mentor_bin, timeout_seconds=max(60, request.timeout_seconds), progress=native_event, maintenance_context=context, stop=stop) assert_native_idle() recovery = {**recovery, 'native_status': recovery.get('status'), 'validation_passed': False} recovery['release'] = repair_release_state(request, target, recovery) safe = recovery['release'].get('safe_to_continue') is True confirmed = recovery.get('status') == 'REPAIRED' and safe and native_success_confirmed(target, recovery) evidence = recovery.get('evidence') completed_failure = (recovery.get('status') in ('MISSING_HEALTHY_BACKUP', 'NATIVE_REPAIR_FAILED') and isinstance(evidence, dict) and evidence.get('failure_complete') is True and _native_identity_and_exit_confirmed(target, recovery)) # Physical release is necessary, but does not approve historical # restoration/unknown/busy outcomes or an incomplete failure report. owner_safe = safe and (confirmed or completed_failure) if confirmed and stop is not None and stop.is_set(): recovery['status'] = 'REPAIR_VALIDATION_CANCELLED' return save(VerifyResult('FAIL', safe_to_continue=owner_safe, stderr='原生修复已结束,但用户取消了后续校验;不报告修复成功。', recovery_attempts=(recovery,))) if confirmed: emit('repair_verify', detail='原生修复及释放已确认,重新打开同一工程校验') from .post_repair_consent import PostRepairProof owner_safe = False proof = PostRepairProof.create(request, target, recovery) verified = verify(replace(request, maintenance=False, auto_repair=False, diagnostic_only=False, post_repair_proof=proof, interactive_input=False, native_lm_verify=False)) owner_safe = verified.safe_to_continue good = verified.status == 'PASS' and verified.library_flag == 'GREEN' and owner_safe recovery.update(status='REPAIRED' if good else 'REPAIR_VALIDATION_FAILED', validation_passed=good, validation_evidence=str(verified.evidence_path or '')) if good and verified.evidence_path: restored = Path(verified.evidence_path).parent/'pcb-version-restoration.json' if restored.exists(): version = json.loads(restored.read_text(encoding='utf-8')) if (version.get('confirm_sent') is not True or version.get('historical_restore') is not True or Path(version['project']).resolve() != checked['project'].resolve() or version.get('choice') not in ('last_user_saved', 'autosave')): raise ValueError('PCB_VERSION_RESTORATION_EVIDENCE_INVALID') recovery.update(status='RESTORED_REVIEW_REQUIRED', pcb_version=version) verified = replace(verified, stdout=verified.stdout + f'\n已按用户选择恢复 PCB({version["date"]})并通过校验;版本待核对。') if verified.error_category == 'PCB_VERSION_CHOICE_REQUIRED': owner_safe = False good = False recovery.update(status='PCB_VERSION_CHOICE_REQUIRED', validation_passed=False) verified = replace(verified, safe_to_continue=False, stderr='ICDB原生修复已完成;PCB需选择恢复版本,尚未校验通过。\n' + verified.stderr) return save(replace(verified, status='PASS' if good else 'FAIL', recovery_attempts=(recovery,))) if recovery.get('status') == 'REPAIRED': recovery['status'] = 'UNCONFIRMED'; owner_safe = False return save(VerifyResult('FAIL', stderr=f'{recovery["status"]}: {recovery.get("detail", "原生维护未确认修复")}', safe_to_continue=owner_safe, recovery_attempts=(recovery,), error_category='ICDB_MAINTENANCE_RESULT')) except Exception as error: return save(VerifyResult('BLOCKED' if recovery is None else 'FAIL', stderr=str(error), safe_to_continue=False, recovery_attempts=(recovery,) if recovery else (), error_category='ICDB_MAINTENANCE_BLOCKED', error_stage=record['stage']))