fix: complete legacy CBB import recovery and guard late native submission

This commit is contained in:
阿白叔 committed 2026-11-03 16:03:14 +08:00
1 parent d36b6bcad0
commit 7527800128
12 files changed
+326 -25

No files matched your search

+41 -3
View File
@@ -161,17 +161,22 @@ class NativeManagementAdapter:
from .desktop_worker import DesktopWorker
request_path = evidence/'request.json'
desktop, timeout = job['desktop'], job['timeout_seconds']
deadline = time.monotonic() + timeout
add_job = job['operation'] in ('add', 'probe_create_add', 'probe_readd')
if add_job:
job = {**job, 'native_add_deadline': deadline}
_write_json(request_path, job)
command = ([sys.executable] if getattr(sys, 'frozen', False)
else [sys.executable, '-m', 'cbb_refresh_verify'])
worker = (self.worker_factory or DesktopWorker)(command + ['--management-worker', str(request_path)], desktop)
deadline = time.monotonic() + timeout
while worker.poll() is None and time.monotonic() < deadline:
time.sleep(.1)
if worker.poll() is None:
# DesktopWorker.close() requests a kill without a cancellation file.
# Keep this exact worker alive until its native call returns normally.
_retained_workers.append(worker)
if add_job:
(evidence/'cancel-native-add.signal').touch()
return {'success': False, 'safe_to_continue': False,
'detail': f'TIMEOUT: 原生调用尚未返回,保留私有桌面 {desktop},请检查 {evidence}'}
try:
@@ -278,12 +283,14 @@ class NativeManagementAdapter:
target, pcb, _ = source_project(projects[0], request.top_level_block)
_idle_check(request.library, target, pcb)
owner_safe = False
before_refs = {str(p): hash_file(p) for p in (target, pcb.parent/'pcb.prj')}
from .maintenance_entry import maintenance_project_files
project_files = maintenance_project_files(target, pcb)
before_refs = {str(p): hash_file(p) for p in project_files}
_align_library(target, pcb, request.library)
if project_library(target) != Path(request.library).resolve():
raise ValueError('Registered project references do not resolve to the exact target library.')
_write_json(evidence/'reference-alignment.json', {'before': before_refs,
'after': {str(p): hash_file(p) for p in (target, pcb.parent/'pcb.prj')}, 'library': job['library']})
'after': {str(p): hash_file(p) for p in project_files}, 'library': job['library']})
catalog = self.catalog(request.library, evidence/'reopen-aligned')
owner_safe = catalog.safe_to_continue
entries = [entry for entry in catalog.entries if entry.name == request.name and entry.partition == request.partition]
@@ -614,6 +621,15 @@ def _probe_add_dialog(app, job, desktop, owned_pids, root, *, library=None):
_close_probe_dialogs(desktop, owned_pids)
def _require_add_supervisor(job, root):
from .library_lock import _supervisor_alive
deadline = job.get('native_add_deadline')
if ((Path(root)/'cancel-native-add.signal').exists()
or deadline is not None and time.monotonic() >= deadline
or not _supervisor_alive(job['supervisor_pid'])):
raise RuntimeError('NATIVE_ADD_CANCELLED: 操作已超时、停止或监督进程退出,未提交新的导入。')
def _probe_add_dialog_impl(app, job, desktop, owned_pids, root, *, library=None):
"""Submit the proven Logical-Physical Add dialog on an owned library."""
import pythoncom, win32con, win32gui, win32process
@@ -649,7 +665,9 @@ def _probe_add_dialog_impl(app, job, desktop, owned_pids, root, *, library=None)
return hwnd
pythoncom.PumpWaitingMessages(); time.sleep(.1)
raise TimeoutError(f'原生窗口未出现:{title}')
_require_add_supervisor(job, root)
app.OnReusableBlocksEditor()
_require_add_supervisor(job, root)
editor = await_window('Reusable Blocks')
tabs = Desktop(backend='uia').window(handle=editor).descendants(control_type='TabItem')
physical = [tab for tab in tabs if tab.window_text() == 'Logical - Physical']
@@ -680,9 +698,11 @@ def _probe_add_dialog_impl(app, job, desktop, owned_pids, root, *, library=None)
ok = win32gui.GetDlgItem(dialog, 1083)
if not ok:
raise RuntimeError('原生新增确认按钮缺失。')
_require_add_supervisor(job, root)
log_baseline = snapshot_add_log(job['library'])
_write_json(root/'native-add-log-baseline.json', log_baseline)
observed_failures = {}
_require_add_supervisor(job, root)
win32gui.PostMessage(ok, win32con.BM_CLICK, 0, 0)
success = None
while time.monotonic() < deadline:
@@ -900,6 +920,7 @@ def _catalog_com(job, root):
stop = threading.Event()
refused = []
observed = []
acknowledged_notices = set()
owned_pids = set()
def observe_dialogs():
while not stop.wait(.1):
@@ -920,6 +941,23 @@ def _catalog_com(job, root):
cancel = win32gui.GetDlgItem(hwnd, 2)
if cancel:
win32gui.PostMessage(cancel, win32con.BM_CLICK, 0, 0)
elif (job['operation'] in ('add', 'probe_create_add', 'probe_readd')
and win32gui.GetClassName(hwnd) == '#32770'):
from .native_add_failures import missing_other_pcb_notice
controls = [{'hwnd': int(child), 'id': win32gui.GetDlgCtrlID(child),
'class': win32gui.GetClassName(child), 'text': win32gui.GetWindowText(child),
'enabled': bool(win32gui.IsWindowEnabled(child))} for child in children]
notice = missing_other_pcb_notice(job['library'], job.get('name'), title,
'\n'.join(c['text'] for c in controls if c['class'] == 'Static'),
[c for c in controls if c['class'] == 'Button'])
key = (int(hwnd), text)
if notice and key not in acknowledged_notices:
acknowledged_notices.add(key)
with (root/'native-information.jsonl').open('a', encoding='utf-8') as stream:
stream.write(json.dumps({'pid': pid, 'hwnd': int(hwnd), 'text': text,
'missing_names': notice['names'], 'action': 'acknowledge_unrelated_missing_pcb'},
ensure_ascii=False)+'\n')
win32gui.PostMessage(notice['button'], win32con.BM_CLICK, 0, 0)
except Exception:
continue
app = library = None